__('显示论坛热门话题列表。', 'aksrt')] ); } 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; } 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 '
  • '; echo '' . esc_html((string) $i) . ''; echo ''; echo esc_html(get_the_title()); echo ''; echo ' ' . esc_html((string) $views) . ''; echo '
  • '; } wp_reset_postdata(); return (string) ob_get_clean(); } /** * @return array{html:string,paginate_html:string,has_more:bool,page:int,max_pages:int,total:int} */ public static function query_payload(array $instance, int $page = 1): array { $page = max(1, $page); $count = max(1, min(20, (int) ($instance['count'] ?? 5))); $paginate = sanitize_key((string) ($instance['paginate'] ?? 'ajax')); $q = new \WP_Query(self::build_query_args($instance, $page)); $items = self::render_items_html($q, ($page - 1) * $count); $html = $items !== '' ? '' : ''; if ($html === '' && $page === 1) { $html = '

    ' . esc_html__('暂无话题', 'aksrt') . '

    '; } $max_pages = max(1, (int) $q->max_num_pages); $total = (int) $q->found_posts; $has_more = $page < $max_pages; $paginate_html = ''; if ($paginate === 'ajax' && $has_more) { $next = add_query_arg([ 'page' => $page + 1, 'count' => $count, 'period' => sanitize_key((string) ($instance['period'] ?? 'week')), 'orderby' => sanitize_key((string) ($instance['orderby'] ?? 'views')), 'paginate' => 'ajax', ], rest_url('aksrt/v1/widgets/forum-topics')); $paginate_html = function_exists('aksrt_get_ajax_next_paginate') ? aksrt_get_ajax_next_paginate($total, $page, $count, $next) : '
    ' . esc_html__('加载更多', 'aksrt') . '
    '; } else { $paginate_html = '
    '; } return compact('html', 'paginate_html', 'has_more', 'page', 'max_pages', 'total'); } public function widget($args, $instance): void { $title = !empty($instance['title']) ? $instance['title'] : __('热门话题', 'aksrt'); $instance['paginate'] = sanitize_key((string) ($instance['paginate'] ?? 'ajax')); $payload = self::query_payload($instance, 1); 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 '
    '; echo $payload['html']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $payload['paginate_html']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo ''; echo '
    '; echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } public function form($instance): void { $title = !empty($instance['title']) ? $instance['title'] : __('热门话题', 'aksrt'); $count = !empty($instance['count']) ? (int) $instance['count'] : 5; $period = !empty($instance['period']) ? $instance['period'] : 'week'; ?>

    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', ]; } }