diff --git a/src/AKSRT/Widgets/WidgetForumHotTopics.php b/src/AKSRT/Widgets/WidgetForumHotTopics.php
index 18eb5162..80632deb 100644
--- a/src/AKSRT/Widgets/WidgetForumHotTopics.php
+++ b/src/AKSRT/Widgets/WidgetForumHotTopics.php
@@ -1,6 +1,6 @@
__('显示论坛热门话题列表。', 'aksrt')]
+ [
+ 'description' => __('显示论坛热门话题列表(头像/身份/阅读)。', 'aksrt'),
+ 'classname' => 'widget_aksrt_forum_hot_topics',
+ ]
);
}
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'));
+ $count = max(1, min(20, (int) ($instance['count'] ?? 5)));
+ $period = sanitize_key((string) ($instance['period'] ?? 'week'));
$orderby = sanitize_key((string) ($instance['orderby'] ?? 'views'));
+ $plate = max(0, (int) ($instance['plate'] ?? $instance['plate_id'] ?? 0));
+
$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,
@@ -36,38 +42,80 @@ class WidgetForumHotTopics extends \WP_Widget {
'post_status' => 'publish',
'date_query' => $date_query,
];
+
if ($orderby === 'comments') {
$query['orderby'] = 'comment_count';
- $query['order'] = 'DESC';
+ $query['order'] = 'DESC';
} elseif ($orderby === 'date') {
$query['orderby'] = 'date';
- $query['order'] = 'DESC';
+ $query['order'] = 'DESC';
+ } elseif ($orderby === 'essence') {
+ $query['meta_query'] = [[
+ 'key' => '_aksrt_essence',
+ 'value' => '1',
+ ]];
+ $query['orderby'] = 'date';
+ $query['order'] = 'DESC';
} else {
$query['meta_key'] = '_aksrt_views';
- $query['orderby'] = 'meta_value_num';
- $query['order'] = 'DESC';
+ $query['orderby'] = 'meta_value_num';
+ $query['order'] = 'DESC';
}
+
+ if ($plate > 0) {
+ $mq = isset($query['meta_query']) ? $query['meta_query'] : [];
+ $mq[] = [
+ 'key' => '_aksrt_plate_id',
+ 'value' => $plate,
+ ];
+ $query['meta_query'] = array_values($mq);
+ }
+
return $query;
}
- public static function render_items_html(\WP_Query $query, int $start = 0): string {
+ public static function render_items_html(\WP_Query $query, int $start = 0, string $style = 'mini'): string {
if (!$query->have_posts()) {
return '';
}
+ $style = $style === 'rank' ? 'rank' : 'mini';
ob_start();
+ if ($style === 'rank') {
+ echo '
';
+ } else {
+ echo '
';
+ }
$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 '';
+ if ($style === 'rank') {
+ $tone = $i === 1 ? 'jb-red' : ($i === 2 ? 'jb-yellow' : ($i === 3 ? 'b-gray' : ''));
+ echo '
';
+ } elseif (function_exists('aksrt_render_forum_topic_mini')) {
+ echo aksrt_render_forum_topic_mini(null, 'ajax-item', true); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
+ } else {
+ $views = (int) get_post_meta(get_the_ID(), '_aksrt_views', true);
+ echo '
';
+ }
}
+ echo '
';
wp_reset_postdata();
return (string) ob_get_clean();
}
@@ -76,18 +124,23 @@ class WidgetForumHotTopics extends \WP_Widget {
* @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)));
+ $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 !== '' ? '
' : '';
+ $style = sanitize_key((string) ($instance['style'] ?? 'mini'));
+ if (!in_array($style, ['mini', 'rank'], true)) {
+ $style = 'mini';
+ }
+ $q = new \WP_Query(self::build_query_args($instance, $page));
+ $html = self::render_items_html($q, ($page - 1) * $count, $style);
if ($html === '' && $page === 1) {
- $html = function_exists('aksrt_get_ajax_null') ? aksrt_get_ajax_null(__('暂无话题', 'aksrt')) : '
' . esc_html__('暂无话题', 'aksrt') . '
';
+ $html = function_exists('aksrt_get_ajax_null')
+ ? aksrt_get_ajax_null(__('暂无话题', 'aksrt'))
+ : '
' . esc_html__('暂无话题', 'aksrt') . '
';
}
$max_pages = max(1, (int) $q->max_num_pages);
- $total = (int) $q->found_posts;
- $has_more = $page < $max_pages;
+ $total = (int) $q->found_posts;
+ $has_more = $page < $max_pages;
$paginate_html = '';
if ($paginate === 'ajax' && $has_more) {
$next = add_query_arg([
@@ -95,6 +148,8 @@ class WidgetForumHotTopics extends \WP_Widget {
'count' => $count,
'period' => sanitize_key((string) ($instance['period'] ?? 'week')),
'orderby' => sanitize_key((string) ($instance['orderby'] ?? 'views')),
+ 'style' => $style,
+ 'plate' => max(0, (int) ($instance['plate'] ?? 0)),
'paginate' => 'ajax',
], rest_url('aksrt/v1/widgets/forum-topics'));
$paginate_html = function_exists('aksrt_get_ajax_next_paginate')
@@ -107,6 +162,9 @@ class WidgetForumHotTopics extends \WP_Widget {
}
public function widget($args, $instance): void {
+ if (!post_type_exists('aksrt_forum_topic')) {
+ return;
+ }
$title = !empty($instance['title']) ? $instance['title'] : __('热门话题', 'aksrt');
$instance['paginate'] = sanitize_key((string) ($instance['paginate'] ?? 'ajax'));
$payload = self::query_payload($instance, 1);
@@ -126,6 +184,9 @@ class WidgetForumHotTopics extends \WP_Widget {
$title = !empty($instance['title']) ? $instance['title'] : __('热门话题', 'aksrt');
$count = !empty($instance['count']) ? (int) $instance['count'] : 5;
$period = !empty($instance['period']) ? $instance['period'] : 'week';
+ $orderby = !empty($instance['orderby']) ? $instance['orderby'] : 'views';
+ $style = !empty($instance['style']) ? $instance['style'] : 'mini';
+ $plate = (int) ($instance['plate'] ?? 0);
?>
@@ -143,6 +204,29 @@ class WidgetForumHotTopics extends \WP_Widget {
+
+
+
+
+
+
+
+
+