Files
wordpress-theme-aksrt/src/AKSRT/Widgets/WidgetForumHotTopics.php
T
kate522 93833ad381 feat(widgets): zibll-like in_affix sticky option across side modules
Add sidebar affix (data-affix) to ranking, user/author cards, main/hot/
paid/forum lists, comments, tag cloud, categories and text title with
shared sticky CSS.
2026-07-21 13:55:16 +08:00

274 lines
12 KiB
PHP

<?php
/**
* Forum hot topics with shared mini renderer + ajax pager.
*
* @package AKSRT
*/
namespace AKSRT\Widgets;
defined('ABSPATH') || exit;
class WidgetForumHotTopics extends \WP_Widget {
public function __construct() {
parent::__construct(
'aksrt_forum_hot_topics',
__('论坛 - 热门话题', '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'));
$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,
'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';
} 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';
}
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 $style = 'mini'): string {
if (!$query->have_posts()) {
return '';
}
$style = $style === 'rank' ? 'rank' : 'mini';
ob_start();
if ($style === 'rank') {
echo '<div class="forum-posts-lists aksrt-forum-widget__list is-rank">';
} else {
echo '<div class="forum-posts-lists aksrt-forum-widget__list is-mini">';
}
$i = $start;
while ($query->have_posts()) {
$query->the_post();
$i++;
if ($style === 'rank') {
$tone = $i === 1 ? 'jb-red' : ($i === 2 ? 'jb-yellow' : ($i === 3 ? 'b-gray' : ''));
echo '<div class="aksrt-forum-widget__rank-row relative ajax-item">';
if ($i <= 3 && $tone !== '') {
echo '<badge class="img-badge left hot ' . esc_attr($tone) . '"><i>TOP' . (int) $i . '</i></badge>';
} else {
echo '<span class="aksrt-forum-widget__rank">' . esc_html((string) $i) . '</span>';
}
if (function_exists('aksrt_render_forum_topic_mini')) {
echo aksrt_render_forum_topic_mini(null, 'ajax-item is-rank-item', true); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
} else {
$views = (int) get_post_meta(get_the_ID(), '_aksrt_views', true);
echo '<a href="' . esc_url(get_permalink()) . '" class="aksrt-forum-widget__link">' . esc_html(get_the_title()) . '</a>';
echo '<span class="aksrt-forum-widget__views"><i class="fa fa-eye" aria-hidden="true"></i> ' . esc_html((string) $views) . '</span>';
}
echo '</div>';
} 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 '<div class="aksrt-forum-widget__item ajax-item">';
echo '<a href="' . esc_url(get_permalink()) . '" class="aksrt-forum-widget__link">' . esc_html(get_the_title()) . '</a>';
echo '<span class="aksrt-forum-widget__views"><i class="fa fa-eye" aria-hidden="true"></i> ' . esc_html((string) $views) . '</span>';
echo '</div>';
}
}
echo '</div>';
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'));
$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'))
: '<p class="aksrt-forum-widget__empty ajax-item">' . esc_html__('暂无话题', 'aksrt') . '</p>';
}
$max_pages = max(1, (int) $q->max_num_pages);
$total = (int) $q->found_posts;
$has_more = $page < $max_pages;
$base_qs = array_filter([
'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' => $paginate,
], static function ($v) {
return $v !== '' && $v !== null;
});
$rest_base = add_query_arg($base_qs, rest_url('aksrt/v1/widgets/forum-topics'));
$paginate_html = '';
if ($paginate === 'ajax' && $has_more) {
$next = add_query_arg(['page' => $page + 1], $rest_base);
$paginate_html = function_exists('aksrt_get_ajax_next_paginate')
? aksrt_get_ajax_next_paginate($total, $page, $count, $next)
: '<div class="text-center theme-pagination ajax-pag"><div class="next-page ajax-next"><a href="' . esc_url($next) . '">' . esc_html__('加载更多', 'aksrt') . '</a></div></div>';
} elseif ($paginate === 'number' && $max_pages > 1) {
$paginate_html = function_exists('aksrt_get_ajax_number_paginate')
? aksrt_get_ajax_number_paginate($total, $page, $count, $rest_base)
: '<div class="ajax-pag hide"><div class="next-page ajax-next"><a href="#"></a></div></div>';
} else {
$paginate_html = '<div class="ajax-pag hide"><div class="next-page ajax-next"><a href="#"></a></div></div>';
}
return compact('html', 'paginate_html', 'has_more', 'page', 'max_pages', 'total');
}
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);
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');
$in_affix = !empty($instance['in_affix']);
$wrap = 'ajaxpager' . ($in_affix ? ' is-affix' : '');
echo '<div class="' . esc_attr($wrap) . '" data-aksrt-ajaxpager data-aksrt-ajax-url="' . esc_url($rest) . '"' . ($in_affix ? ' data-affix="true"' : '') . '>';
echo $payload['html']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $payload['paginate_html']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '<div class="post_ajax_loader" data-aksrt-ajax-loader data-aksrt-ajax-loader-hide hidden></div>';
echo '</div>';
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';
$orderby = !empty($instance['orderby']) ? $instance['orderby'] : 'views';
$style = !empty($instance['style']) ? $instance['style'] : 'mini';
$plate = (int) ($instance['plate'] ?? 0);
?>
<p>
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('标题:', 'aksrt'); ?></label>
<input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" type="text" value="<?php echo esc_attr($title); ?>">
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id('count')); ?>"><?php esc_html_e('数量:', 'aksrt'); ?></label>
<input class="tiny-text" id="<?php echo esc_attr($this->get_field_id('count')); ?>" name="<?php echo esc_attr($this->get_field_name('count')); ?>" type="number" value="<?php echo esc_attr((string) $count); ?>" min="1" max="20">
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id('period')); ?>"><?php esc_html_e('时间范围:', 'aksrt'); ?></label>
<select id="<?php echo esc_attr($this->get_field_id('period')); ?>" name="<?php echo esc_attr($this->get_field_name('period')); ?>">
<option value="all" <?php selected($period, 'all'); ?>><?php esc_html_e('全部', 'aksrt'); ?></option>
<option value="week" <?php selected($period, 'week'); ?>><?php esc_html_e('最近一周', 'aksrt'); ?></option>
<option value="month" <?php selected($period, 'month'); ?>><?php esc_html_e('最近一月', 'aksrt'); ?></option>
</select>
</p>
<p>
<label><?php esc_html_e('排序', 'aksrt'); ?>
<select name="<?php echo esc_attr($this->get_field_name('orderby')); ?>">
<option value="views" <?php selected($orderby, 'views'); ?>><?php esc_html_e('热门浏览', 'aksrt'); ?></option>
<option value="comments" <?php selected($orderby, 'comments'); ?>><?php esc_html_e('评论数', 'aksrt'); ?></option>
<option value="date" <?php selected($orderby, 'date'); ?>><?php esc_html_e('最新', 'aksrt'); ?></option>
<option value="essence" <?php selected($orderby, 'essence'); ?>><?php esc_html_e('精华', 'aksrt'); ?></option>
</select>
</label>
</p>
<p>
<label><?php esc_html_e('样式', 'aksrt'); ?>
<select name="<?php echo esc_attr($this->get_field_name('style')); ?>">
<option value="mini" <?php selected($style, 'mini'); ?>><?php esc_html_e('迷你信息行', 'aksrt'); ?></option>
<option value="rank" <?php selected($style, 'rank'); ?>><?php esc_html_e('排行榜', 'aksrt'); ?></option>
</select>
</label>
</p>
<p>
<label><?php esc_html_e('版块 ID(可选)', 'aksrt'); ?>
<input class="tiny-text" type="number" min="0" name="<?php echo esc_attr($this->get_field_name('plate')); ?>" value="<?php echo esc_attr((string) $plate); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('分页', 'aksrt'); ?>
<select name="<?php echo esc_attr($this->get_field_name('paginate')); ?>">
<option value="ajax" <?php selected(($instance['paginate'] ?? 'ajax'), 'ajax'); ?>><?php esc_html_e('AJAX', 'aksrt'); ?></option>
<option value="number" <?php selected(($instance['paginate'] ?? ''), 'number'); ?>><?php esc_html_e('数字分页', 'aksrt'); ?></option>
<option value="none" <?php selected(($instance['paginate'] ?? ''), 'none'); ?>><?php esc_html_e('无', 'aksrt'); ?></option>
</select>
</label>
</p>
<p>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('in_affix')); ?>" value="1" <?php checked(!empty($instance['in_affix'])); ?>> <?php esc_html_e('侧栏随动', 'aksrt'); ?></label>
</p>
<?php
}
public function update($new_instance, $old_instance): array {
$orderby = sanitize_key((string) ($new_instance['orderby'] ?? 'views'));
if (!in_array($orderby, ['views', 'comments', 'date', 'essence'], true)) {
$orderby = 'views';
}
$style = sanitize_key((string) ($new_instance['style'] ?? 'mini'));
if (!in_array($style, ['mini', 'rank'], true)) {
$style = 'mini';
}
return [
'title' => 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',
'orderby' => $orderby,
'style' => $style,
'plate' => max(0, (int) ($new_instance['plate'] ?? 0)),
'paginate' => in_array(sanitize_key((string) ($new_instance['paginate'] ?? 'ajax')), ['none', 'ajax', 'number'], true) ? sanitize_key((string) ($new_instance['paginate'] ?? 'ajax')) : 'ajax',
];
}
}