diff --git a/src/AKSRT/Widgets/WidgetForumHotTopics.php b/src/AKSRT/Widgets/WidgetForumHotTopics.php index a6cb4776..aa14b29e 100644 --- a/src/AKSRT/Widgets/WidgetForumHotTopics.php +++ b/src/AKSRT/Widgets/WidgetForumHotTopics.php @@ -1,5 +1,7 @@ __('显示论坛热门话题列表。', 'aksrt')] - ); - } + public function __construct() { + parent::__construct( + 'aksrt_forum_hot_topics', + __('论坛 - 热门话题', 'aksrt'), + ['description' => __('显示论坛热门话题列表。', 'aksrt')] + ); + } - public function widget($args, $instance): void { - $title = !empty($instance['title']) ? $instance['title'] : __('热门话题', 'aksrt'); - $count = !empty($instance['count']) ? max(1, min(20, (int) $instance['count'])) : 5; - $period = !empty($instance['period']) ? sanitize_key($instance['period']) : 'week'; + public static function build_query_args(array $instance, int $paged = 1): array { + $count = max(1, min(20, (int) ($instance['count'] ?? 5))); + $period = sanitize_key((string) ($instance['period'] ?? 'week')); + $orderby = sanitize_key((string) ($instance['orderby'] ?? 'views')); + $date_query = []; + if ($period === 'week') { + $date_query = [['after' => '7 days ago']]; + } elseif ($period === 'month') { + $date_query = [['after' => '30 days ago']]; + } + $query = [ + 'post_type' => 'aksrt_forum_topic', + 'posts_per_page' => $count, + 'paged' => max(1, $paged), + 'post_status' => 'publish', + 'date_query' => $date_query, + ]; + if ($orderby === 'comments') { + $query['orderby'] = 'comment_count'; + $query['order'] = 'DESC'; + } elseif ($orderby === 'date') { + $query['orderby'] = 'date'; + $query['order'] = 'DESC'; + } else { + $query['meta_key'] = '_aksrt_views'; + $query['orderby'] = 'meta_value_num'; + $query['order'] = 'DESC'; + } + return $query; + } - echo $args['before_widget']; - echo $args['before_title'] . esc_html($title) . $args['after_title']; + public static function render_items_html(\WP_Query $query, int $start = 0): string { + if (!$query->have_posts()) { + return ''; + } + ob_start(); + $i = $start; + while ($query->have_posts()) { + $query->the_post(); + $i++; + $views = (int) get_post_meta(get_the_ID(), '_aksrt_views', true); + echo '
+ + +
++ + +
++ + +
++ +
+ -- - -
-- - -
-- - -
- sanitize_text_field($new_instance['title'] ?? ''), + 'count' => max(1, min(20, (int) ($new_instance['count'] ?? 5))), + 'period' => in_array($new_instance['period'] ?? '', ['all', 'week', 'month'], true) ? $new_instance['period'] : 'week', + 'paginate' => sanitize_key((string) ($new_instance['paginate'] ?? 'ajax')) === 'none' ? 'none' : 'ajax', + ]; + } } diff --git a/src/AKSRT/Widgets/WidgetForumRecentTopics.php b/src/AKSRT/Widgets/WidgetForumRecentTopics.php index 97565ea4..2202bda8 100644 --- a/src/AKSRT/Widgets/WidgetForumRecentTopics.php +++ b/src/AKSRT/Widgets/WidgetForumRecentTopics.php @@ -1,5 +1,7 @@ __('显示论坛最新话题列表。', 'aksrt')] - ); - } + public function __construct() { + parent::__construct( + 'aksrt_forum_recent_topics', + __('论坛 - 最新话题', 'aksrt'), + ['description' => __('显示论坛最新话题列表。', 'aksrt')] + ); + } - public function widget($args, $instance): void { - $title = !empty($instance['title']) ? $instance['title'] : __('最新话题', 'aksrt'); - $count = !empty($instance['count']) ? max(1, min(20, (int) $instance['count'])) : 5; + public function widget($args, $instance): void { + $title = !empty($instance['title']) ? $instance['title'] : __('最新话题', 'aksrt'); + $instance['orderby'] = 'date'; + $instance['period'] = 'all'; + $instance['paginate'] = sanitize_key((string) ($instance['paginate'] ?? 'ajax')); + $payload = WidgetForumHotTopics::query_payload($instance, 1); - echo $args['before_widget']; - echo $args['before_title'] . esc_html($title) . $args['after_title']; + echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + $rest = rest_url('aksrt/v1/widgets/forum-topics'); + echo '+ + +
++ + +
++ +
+ have_posts()) { - echo ''; - wp_reset_postdata(); - } else { - echo ''; - } - - echo $args['after_widget']; - } - - public function form($instance): void { - $title = !empty($instance['title']) ? $instance['title'] : __('最新话题', 'aksrt'); - $count = !empty($instance['count']) ? (int) $instance['count'] : 5; - ?> -- - -
-- - -
- sanitize_text_field($new_instance['title'] ?? ''), + 'count' => max(1, min(20, (int) ($new_instance['count'] ?? 5))), + 'paginate' => sanitize_key((string) ($new_instance['paginate'] ?? 'ajax')) === 'none' ? 'none' : 'ajax', + ]; + } } diff --git a/src/AKSRT/Widgets/WidgetForumTopicTerms.php b/src/AKSRT/Widgets/WidgetForumTopicTerms.php index 26f426af..14da9c03 100644 --- a/src/AKSRT/Widgets/WidgetForumTopicTerms.php +++ b/src/AKSRT/Widgets/WidgetForumTopicTerms.php @@ -1,6 +1,6 @@ */ + public static function parse_ids_static(string $raw): array { + if ($raw === '') { + return []; + } + $ids = []; + foreach (preg_split('/[\s,]+/', $raw) ?: [] as $p) { + $id = (int) $p; + if ($id > 0) { + $ids[] = $id; + } + } + return array_values(array_unique($ids)); + } + + public static function render_html(array $instance): string { $tax = taxonomy_exists('aksrt_forum_tag') ? 'aksrt_forum_tag' : (taxonomy_exists('aksrt_forum_topic_cat') ? 'aksrt_forum_topic_cat' : ''); if ($tax === '') { - return; + return ''; } - $number = max(1, min(40, (int) ($instance['number'] ?? 12))); $orderby = sanitize_key((string) ($instance['orderby'] ?? 'count')); $hide_empty = !isset($instance['hide_empty']) || !empty($instance['hide_empty']); - $include = $this->parse_ids((string) ($instance['include'] ?? '')); + $include = self::parse_ids_static((string) ($instance['include'] ?? '')); $style = sanitize_key((string) ($instance['style'] ?? 'mini')); $term_args = [ 'taxonomy' => $tax, 'number' => $number, - 'hide_empty' => $hide_empty, + 'hide_empty' => (bool) $hide_empty, ]; if ($include) { $term_args['include'] = $include; $term_args['orderby'] = 'include'; + } elseif ($orderby === 'name') { + $term_args['orderby'] = 'name'; + $term_args['order'] = 'ASC'; + } elseif ($orderby === 'views') { + $term_args['meta_key'] = '_aksrt_views'; + $term_args['orderby'] = 'meta_value_num'; + $term_args['order'] = 'DESC'; } else { - if ($orderby === 'name') { - $term_args['orderby'] = 'name'; - $term_args['order'] = 'ASC'; - } elseif ($orderby === 'views') { - $term_args['meta_key'] = '_aksrt_views'; - $term_args['orderby'] = 'meta_value_num'; - $term_args['order'] = 'DESC'; - } else { - $term_args['orderby'] = 'count'; - $term_args['order'] = 'DESC'; - } + $term_args['orderby'] = 'count'; + $term_args['order'] = 'DESC'; } $terms = get_terms($term_args); if (is_wp_error($terms) || !$terms) { - return; + return function_exists('aksrt_get_ajax_null') + ? aksrt_get_ajax_null(__('暂无话题', 'aksrt')) + : ''; } - - echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - $title = apply_filters('widget_title', (string) ($instance['title'] ?? ''), $instance, $this->id_base); - if ($title !== '') { - echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - } - - $list_cls = $style === 'card' ? 'aksrt-ftag-list is-card' : 'aksrt-ftag-list is-mini'; + ob_start(); + $list_cls = $style === 'card' ? 'aksrt-ftag-list is-card ajax-item' : 'aksrt-ftag-list is-mini ajax-item'; echo '