diff --git a/src/AKSRT/Widgets/HotPostsWidget.php b/src/AKSRT/Widgets/HotPostsWidget.php index 430635bd..8d452688 100644 --- a/src/AKSRT/Widgets/HotPostsWidget.php +++ b/src/AKSRT/Widgets/HotPostsWidget.php @@ -70,48 +70,83 @@ class HotPostsWidget extends \WP_Widget { 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'; + $show_thumb = !isset($instance['show_thumb']) || !empty($instance['show_thumb']); + $order_by = sanitize_key((string) ($instance['order_by'] ?? 'views')); + $placeholder = get_template_directory_uri() . '/assets/img/placeholder.svg'; + $top_tones = ['jb-red', 'jb-yellow', 'b-gray']; 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'; + $pid = (int) get_the_ID(); + if ($order_by === 'comments') { + $metric = (int) get_comments_number($pid); + $metric_icon = 'fa-comment-o'; + $metric_label = __('条评论', 'aksrt'); + } 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'; + $metric_label = __('人点赞', 'aksrt'); + } elseif ($order_by === 'favorites') { + $metric = (int) get_post_meta($pid, '_aksrt_favorite_count', true); + $metric_icon = 'fa-star'; + $metric_label = __('人收藏', 'aksrt'); + } elseif ($order_by === 'downloads') { + $metric = (int) get_post_meta($pid, '_aksrt_res_download_count', true); + $metric_icon = 'fa-download'; + $metric_label = __('次下载', 'aksrt'); + } else { + $metric = (int) get_post_meta($pid, 'aksrt_post_views', true); + $metric_icon = 'fa-eye'; + $metric_label = __('次阅读', 'aksrt'); + } + $metric_txt = function_exists('aksrt_format_number') ? aksrt_format_number($metric) : (string) $metric; + $tone = $top_tones[$index - 1] ?? 'b-gray'; + $top_badge = 'TOP' . (int) $index . ''; + $permalink = get_permalink($pid); + $title = get_the_title($pid); + $time_ago = human_time_diff(get_the_time('U'), current_time('timestamp')) . __('前', 'aksrt'); + + // Graphic hero for #1 (zibll zib_hot_posts). + if ($index === 1 && $start === 0) { + $thumb = get_the_post_thumbnail_url($pid, 'large'); + if (!$thumb) { + $thumb = get_the_post_thumbnail_url($pid, 'medium'); } - $metric_txt = function_exists('aksrt_format_number') ? aksrt_format_number($metric) : (string) $metric; + echo '
'; + echo ''; + echo '
'; + if ($thumb) { + echo ''; + } else { + echo ''; + } + echo '
'; + echo '
'; + echo '
' . esc_html($metric_txt . $metric_label) . '
'; + echo '
' . esc_html(wp_html_excerpt($title, 48, '…')) . '
'; + echo '
'; + echo $top_badge; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + echo '
'; + continue; + } + + $rank_class = $index <= 3 ? ' rank-top' : ''; + $thumb = $show_thumb ? get_the_post_thumbnail_url($pid, 'thumbnail') : ''; ?> -
- - - - +
+ + + + -
- -
+
+
+
+ -
@@ -121,16 +156,14 @@ class HotPostsWidget extends \WP_Widget { 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, $instance, ($page - 1) * $count); - $html = $items !== '' ? '
' . $items . '
' : ''; + $html = $items !== '' ? '
' . $items . '
' : ''; if ($html === '' && $page === 1) { $html = function_exists('aksrt_get_widget_null') ? aksrt_get_widget_null(__('暂无热门文章', 'aksrt')) diff --git a/src/AKSRT/Widgets/WidgetPaidPosts.php b/src/AKSRT/Widgets/WidgetPaidPosts.php index e4a35b26..17b4ad7a 100644 --- a/src/AKSRT/Widgets/WidgetPaidPosts.php +++ b/src/AKSRT/Widgets/WidgetPaidPosts.php @@ -61,6 +61,14 @@ class WidgetPaidPosts extends \WP_Widget { } else { $query['orderby'] = 'date'; } + $cat = max(0, (int) ($instance['cat'] ?? 0)); + if ($cat > 0) { + $query['cat'] = $cat; + } + $limit_day = max(0, (int) ($instance['limit_day'] ?? 0)); + if ($limit_day > 0) { + $query['date_query'] = [['after' => $limit_day . ' days ago']]; + } return $query; } @@ -157,9 +165,6 @@ class WidgetPaidPosts extends \WP_Widget { $paginate = sanitize_key((string) ($instance['paginate'] ?? 'ajax')); $instance['paginate'] = $paginate; $payload = self::query_payload($instance, 1); - if ($payload['html'] === '' && empty($payload['total'])) { - return; - } echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped $title = apply_filters('widget_title', (string) ($instance['title'] ?? ''), $instance, $this->id_base); @@ -206,6 +211,16 @@ class WidgetPaidPosts extends \WP_Widget {

+

+ +

+

+ +

sanitize_text_field((string) ($new_instance['title'] ?? '')), 'count' => max(1, min(20, (int) ($new_instance['count'] ?? 6))), - 'orderby' => $orderby, + 'orderby' => $orderby, + 'cat' => max(0, (int) ($new_instance['cat'] ?? 0)), + 'limit_day' => max(0, (int) ($new_instance['limit_day'] ?? 0)), 'paginate' => sanitize_key((string) ($new_instance['paginate'] ?? 'ajax')) === 'none' ? 'none' : 'ajax', ]; }