feat(frontend): wire comment sort/author filter like Zibll

Apply comments_template_query_args for newest/hottest/author-only,
keep filter bar visible under empty author filters, and restyle order
tabs/buttons with Zibll-compatible classes.
This commit is contained in:
kate522 committed 2026-07-11 21:07:22 +08:00
1 parent b155936868
commit b01a89bee2
3 files changed
+165 -8

No files matched your search

+74 -7
View File
@@ -9,6 +9,60 @@
defined('ABSPATH') || exit;
if (!function_exists('aksrt_comments_template_query_args')) :
/**
* Apply order / author-only filters to the comments template query.
*
* @param array<string,mixed> $args Comment query args.
* @return array<string,mixed>
*/
function aksrt_comments_template_query_args(array $args): array {
$order = isset($_GET['aksrt_comment_order'])
? sanitize_key((string) wp_unslash($_GET['aksrt_comment_order']))
: '';
if ($order === '') {
$settings = class_exists('AKSRT_Settings')
? AKSRT_Settings::get_comment_display_settings()
: [];
$order = (($settings['order'] ?? 'desc') === 'asc') ? 'oldest' : 'newest';
}
if (!in_array($order, ['newest', 'oldest', 'hottest'], true)) {
$order = 'newest';
}
if ($order === 'oldest') {
$args['order'] = 'ASC';
$args['orderby'] = 'comment_date_gmt';
} else {
// newest + hottest: base DESC; hottest reorders via comments_clauses.
$args['order'] = 'DESC';
$args['orderby'] = 'comment_date_gmt';
}
if (isset($_GET['aksrt_author_only']) && (int) wp_unslash($_GET['aksrt_author_only']) === 1) {
$post_id = (int) get_queried_object_id();
if ($post_id <= 0 && !empty($args['post_id'])) {
$post_id = (int) $args['post_id'];
}
$author_id = $post_id > 0 ? (int) get_post_field('post_author', $post_id) : 0;
if ($author_id > 0) {
$args['user_id'] = $author_id;
}
}
return $args;
}
endif;
add_filter('comments_template_query_args', static function ($args) {
if (!is_array($args)) {
$args = [];
}
return function_exists('aksrt_comments_template_query_args')
? aksrt_comments_template_query_args($args)
: $args;
});
if (!function_exists('aksrt_comments_build_context')) :
/**
* Resolve comments runtime context once.
@@ -208,13 +262,19 @@ if (!function_exists('aksrt_comments_render_filter')) :
$author_only = !empty($ctx['author_only']);
$post_author_id = (int) $ctx['post_author_id'];
?>
<div class="comment-filter tab-nav-theme flex ac jsb">
<ul class="list-inline comment-order-box">
<li class="<?php echo $order === 'newest' ? 'active' : ''; ?>"><a href="javascript:;" class="comment-orderby" data-order="newest"><?php esc_html_e('最新', 'aksrt'); ?></a></li>
<li class="<?php echo $order === 'hottest' ? 'active' : ''; ?>"><a href="javascript:;" class="comment-orderby" data-order="hottest"><?php esc_html_e('最热', 'aksrt'); ?></a></li>
<div class="comment-filter tab-nav-theme flex ac jsb" win-ajax-replace="comment-order-btn">
<ul class="list-inline comment-order-box comment-filter__tabs">
<li class="<?php echo $order === 'newest' ? 'active' : ''; ?>">
<a href="javascript:;" class="comment-orderby" data-order="newest" rel="nofollow"><?php esc_html_e('最新', 'aksrt'); ?></a>
</li>
<li class="<?php echo $order === 'hottest' ? 'active' : ''; ?>">
<a href="javascript:;" class="comment-orderby" data-order="hottest" rel="nofollow"><?php esc_html_e('最热', 'aksrt'); ?></a>
</li>
</ul>
<?php if ($post_author_id > 0) : ?>
<a href="javascript:;" class="btn-only-author<?php echo $author_only ? ' active' : ''; ?> comment-author-filter" data-author-only="<?php echo $author_only ? '0' : '1'; ?>"><?php esc_html_e('只看作者', 'aksrt'); ?></a>
<a href="javascript:;" class="but comment-orderby btn-only-author p2-10 comment-author-filter<?php echo $author_only ? ' active' : ''; ?>" data-author-only="<?php echo $author_only ? '0' : '1'; ?>" rel="nofollow">
<?php echo $author_only ? esc_html__('查看全部', 'aksrt') : esc_html__('只看作者', 'aksrt'); ?>
</a>
<?php endif; ?>
</div>
<?php
@@ -306,10 +366,17 @@ if (!function_exists('aksrt_comments_render')) :
<?php endif; ?>
<?php endif; ?>
<?php
// Keep filter visible under author-only empty results.
$show_filter = !empty($ctx['corderby_enabled']) && ((int) $ctx['comment_count'] > 0 || !empty($ctx['author_only']) || have_comments());
if ($show_filter) {
aksrt_comments_render_filter($ctx);
}
?>
<?php if (have_comments()) : ?>
<?php aksrt_comments_render_filter($ctx); ?>
<?php aksrt_comments_render_list($ctx); ?>
<?php elseif (!have_comments() && !post_password_required()) : ?>
<?php elseif (!post_password_required()) : ?>
<?php aksrt_comments_render_empty($ctx); ?>
<?php endif; ?>