esc_html__('按浏览量/评论数/点赞数排序的热门文章列表', 'aksrt'), 'customize_selective_refresh' => true, ] ); } public static function build_query_args(array $instance, int $paged = 1): array { $count = max(1, min(20, (int) ($instance['count'] ?? 5))); $order_by = sanitize_key((string) ($instance['order_by'] ?? 'views')); $days = max(0, (int) ($instance['days'] ?? 30)); $query_args = [ 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => $count, 'paged' => max(1, $paged), 'ignore_sticky_posts' => true, ]; if ($days > 0 && $days < 365) { $query_args['date_query'] = [['after' => $days . ' days ago']]; } switch ($order_by) { case 'comments': $query_args['orderby'] = 'comment_count'; $query_args['order'] = 'DESC'; break; case 'likes': $query_args['meta_key'] = '_aksrt_like_count'; $query_args['orderby'] = 'meta_value_num'; $query_args['order'] = 'DESC'; break; case 'favorites': $query_args['meta_key'] = '_aksrt_favorite_count'; $query_args['orderby'] = 'meta_value_num'; $query_args['order'] = 'DESC'; break; case 'downloads': $query_args['meta_key'] = '_aksrt_res_download_count'; $query_args['orderby'] = 'meta_value_num'; $query_args['order'] = 'DESC'; break; case 'views': default: $query_args['meta_key'] = 'aksrt_post_views'; $query_args['orderby'] = 'meta_value_num'; $query_args['order'] = 'DESC'; break; } return $query_args; } public static function render_items_html(\WP_Query $query, array $instance, int $start = 0): string { if (!$query->have_posts()) { return ''; } $show_thumb = !empty($instance['show_thumb']); $order_by = sanitize_key((string) ($instance['order_by'] ?? 'views')); $placeholder = get_template_directory_uri() . '/assets/img/placeholder.svg'; ob_start(); $index = $start; while ($query->have_posts()) { $query->the_post(); $index++; $rank_class = $index <= 3 ? ' rank-top' : ''; $pid = get_the_ID(); if ($order_by === 'comments') { $metric = (int) get_comments_number($pid); $metric_icon = 'fa-comment-o'; } elseif ($order_by === 'likes') { $metric = function_exists('aksrt_get_post_likes') ? (int) aksrt_get_post_likes($pid) : (int) get_post_meta($pid, '_aksrt_like_count', true); $metric_icon = 'fa-heart'; } elseif ($order_by === 'favorites') { $metric = (int) get_post_meta($pid, '_aksrt_favorite_count', true); $metric_icon = 'fa-star'; } elseif ($order_by === 'downloads') { $metric = (int) get_post_meta($pid, '_aksrt_res_download_count', true); $metric_icon = 'fa-download'; } else { $metric = (int) get_post_meta($pid, 'aksrt_post_views', true); $metric_icon = 'fa-eye'; } $metric_txt = function_exists('aksrt_format_number') ? aksrt_format_number($metric) : (string) $metric; ?>
' . $items . '' : ''; if ($html === '' && $page === 1) { $html = function_exists('aksrt_get_widget_null') ? aksrt_get_widget_null(__('暂无热门文章', '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, 'order_by' => sanitize_key((string) ($instance['order_by'] ?? 'views')), 'days' => max(0, (int) ($instance['days'] ?? 30)), 'show_thumb' => !empty($instance['show_thumb']) ? 1 : 0, 'paginate' => 'ajax', ], rest_url('aksrt/v1/widgets/hot-posts')); $paginate_html = function_exists('aksrt_get_ajax_next_paginate') ? aksrt_get_ajax_next_paginate($total, $page, $count, $next) : '
sanitize_text_field($new_instance['title'] ?? ''), 'count' => absint($new_instance['count'] ?? 5), 'order_by' => in_array($new_instance['order_by'] ?? '', ['views', 'comments', 'likes', 'favorites', 'downloads'], true) ? $new_instance['order_by'] : 'views', 'days' => absint($new_instance['days'] ?? 30), 'show_thumb' => !empty($new_instance['show_thumb']) ? 1 : 0, 'paginate' => sanitize_key((string) ($new_instance['paginate'] ?? 'ajax')) === 'none' ? 'none' : 'ajax', ]; } }