评论子系统减层:comments.php 收成挂载点

将评论策略与 section 组合迁入 aksrt-comments,
保留 callback/pagination/query-var 契约,模板只负责挂载。
This commit is contained in:
2026-07-09 22:36:21 +08:00
parent 3fe050b985
commit 00c1f9ca70
3 changed files with 329 additions and 198 deletions
+6 -198
View File
@@ -1,206 +1,14 @@
<?php
/**
* 评论模板 v4.0 Zibll-Inspired
* Comments template — thin mount point.
*
* 功能:
* - AJAX 评论提交
* - 最新/最热排序切换
* - 移动端固定底部评论输入框
* - 嵌套回复
* - 评论点赞
* - 骨架屏加载
* Composition lives in inc/helpers/aksrt-comments.php.
*
* @package AKSRT
* @version 4.0.0
*/
if (post_password_required()) {
return;
defined('ABSPATH') || exit;
if (function_exists('aksrt_comments_render')) {
aksrt_comments_render();
}
$comment_count = (int) get_comments_number();
$is_logged_in = is_user_logged_in();
$current_user = wp_get_current_user();
$comment_settings = class_exists('AKSRT_Settings')
? AKSRT_Settings::get_comment_display_settings()
: [
'order' => 'desc',
'nested_depth' => 3,
'require_login' => false,
'allow_reply' => true,
'avatar_size' => 48,
'empty_text' => __('暂无评论,来做第一个评论的人吧~', 'aksrt'),
];
$require_login = !empty($comment_settings['require_login']);
$default_comment_order = ($comment_settings['order'] ?? 'desc') === 'asc' ? 'oldest' : 'newest';
$comment_order = isset($_GET['aksrt_comment_order']) ? sanitize_key((string) wp_unslash($_GET['aksrt_comment_order'])) : $default_comment_order;
$comment_order = in_array($comment_order, ['newest', 'oldest', 'hottest'], true) ? $comment_order : 'newest';
$reverse_comments = ($comment_order === 'newest');
$is_hottest = ($comment_order === 'hottest');
if ($is_hottest) {
add_filter('comments_clauses', function ($clauses) {
global $wpdb;
$clauses['join'] .= " LEFT JOIN {$wpdb->commentmeta} AS aksrt_cm ON ({$wpdb->comments}.comment_ID = aksrt_cm.comment_id AND aksrt_cm.meta_key = 'aksrt_comment_likes')";
$clauses['orderby'] = 'CAST(aksrt_cm.meta_value AS UNSIGNED) DESC, ' . $clauses['orderby'];
return $clauses;
});
}
$author_only = isset($_GET['aksrt_author_only']) && (int) $_GET['aksrt_author_only'] === 1;
$post_author_id = (int) get_the_author_meta('ID');
$avatar_size = max(24, (int) ($comment_settings['avatar_size'] ?? 48));
$max_depth = max(1, (int) ($comment_settings['nested_depth'] ?? 3));
$allow_reply = !empty($comment_settings['allow_reply']);
$empty_text = (string) ($comment_settings['empty_text'] ?? __('暂无评论,来做第一个评论的人吧~', 'aksrt'));
$comment_toolbar = is_array($comment_settings['toolbar'] ?? null) ? $comment_settings['toolbar'] : ['smilie' => true, 'code' => true, 'image' => true];
$comment_upload_enabled = !empty($comment_settings['toolbar']['upload_img']);
$comment_box_placeholder = (string) ($comment_settings['box_placeholder'] ?? __('说点什么吧...', 'aksrt'));
$comment_submit_text = (string) ($comment_settings['submit_text'] ?? __('提交评论', 'aksrt'));
$comment_title_text = (string) ($comment_settings['title'] ?? __('评论', 'aksrt'));
set_query_var('aksrt_comment_order', $comment_order);
set_query_var('aksrt_comment_settings', $comment_settings);
?>
<?php
$oauth_providers = [];
if (class_exists('AKSRT_OAuth')) {
$oauth_providers = AKSRT_OAuth::get_instance()->get_auth_providers_data();
}
?>
<div class="theme-box" id="comments" style="<?php echo esc_attr('--aksrt-comment-avatar:' . $avatar_size . 'px;--aksrt-comment-avatar-form:' . max(40, $avatar_size) . 'px;--aksrt-comment-indent:' . $avatar_size . 'px;'); ?>">
<div class="box-body notop">
<div class="title-theme">
<?php echo esc_html($comment_title_text); ?>
<small class="comments-count"><?php echo esc_html(sprintf(__('共 %d 条', 'aksrt'), $comment_count)); ?></small>
</div>
</div>
<?php if (comments_open()) : ?>
<?php if ($require_login && !$is_logged_in) : ?>
<div class="comment-signarea text-center box-body radius8">
<i class="fas fa-lock" aria-hidden="true"></i>
<span><?php esc_html_e('登录后参与评论', 'aksrt'); ?></span>
<div class="comment-login-prompt__btns">
<a class="signin-loader but hollow" href="<?php echo esc_url(aksrt_get_login_url(get_permalink())); ?>"><?php esc_html_e('登录', 'aksrt'); ?></a>
<?php if (get_option('users_can_register')) : ?>
<a class="signup-loader but b-theme" href="<?php echo esc_url(aksrt_get_register_url()); ?>"><?php esc_html_e('注册', 'aksrt'); ?></a>
<?php endif; ?>
</div>
<?php if (!empty($oauth_providers)) : ?>
<div class="comment-signarea__oauth">
<?php foreach ($oauth_providers as $provider) :
$provider_name = (string) ($provider['name'] ?? '');
$label = (string) ($provider['label'] ?? $provider_name);
$url = (string) ($provider['url'] ?? '#');
$icon = $provider['icon'] ?? '';
?>
<a href="<?php echo esc_url($url); ?>" class="social-login-item but hollow <?php echo esc_attr($provider_name); ?>" data-provider="<?php echo esc_attr($provider_name); ?>" title="<?php echo esc_attr($label); ?>">
<?php echo $icon; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<span><?php echo esc_html($label); ?></span>
</a>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
<?php else : ?>
<div id="respond" class="comment-respond mobile-fixed">
<form id="commentform" class="comment-form" method="post" action="<?php echo esc_url(site_url('/wp-comments-post.php')); ?>">
<div class="fixed-body">
<div class="comment-form__inner">
<div class="comment-form__avatar">
<?php if ($is_logged_in) : ?>
<img src="<?php echo esc_url(get_avatar_url($current_user->ID, ['size' => $avatar_size])); ?>" alt="" width="<?php echo esc_attr((string) $avatar_size); ?>" height="<?php echo esc_attr((string) $avatar_size); ?>" class="avatar-img comt-avatar">
<?php else : ?>
<div class="comment-form__avatar-placeholder"><i class="fas fa-user"></i></div>
<?php endif; ?>
</div>
<div class="grow1 comt-box">
<div class="comt-tips" id="comment-reply-info" style="display:none;">
<span class="comt-tips-left comment-form__reply-text"></span>
<span class="comt-tips-right"><a href="javascript:;" id="cancel-comment-reply-link" class="comment-form__cancel-reply"><i class="fas fa-times"></i> <?php esc_html_e('取消', 'aksrt'); ?></a></span>
</div>
<textarea id="comment" name="comment" class="form-control grin" rows="3" placeholder="<?php echo esc_attr($comment_box_placeholder); ?>" required></textarea>
<div class="comt-ctrl">
<div class="comment-toolbar" role="toolbar" aria-label="<?php esc_attr_e('评论工具栏', 'aksrt'); ?>">
<button type="button" class="comment-toolbar__btn"<?php echo empty($comment_toolbar['smilie']) ? ' hidden' : ''; ?> title="<?php esc_attr_e('表情', 'aksrt'); ?>" data-toolbar-action="emoji"><i class="far fa-smile"></i></button>
<button type="button" class="comment-toolbar__btn"<?php echo empty($comment_toolbar['code']) ? ' hidden' : ''; ?> title="<?php esc_attr_e('插入代码', 'aksrt'); ?>" data-toolbar-action="code"><i class="fas fa-code"></i></button>
<button type="button" class="comment-toolbar__btn"<?php echo empty($comment_toolbar['image']) ? ' hidden' : ''; ?> title="<?php esc_attr_e('图片', 'aksrt'); ?>" data-toolbar-action="image"><i class="fas fa-image"></i></button>
<button type="button" class="comment-toolbar__btn"<?php echo $comment_upload_enabled ? '' : ' hidden'; ?> title="<?php esc_attr_e('上传图片', 'aksrt'); ?>" data-toolbar-action="upload-image"><i class="fas fa-cloud-upload-alt"></i></button>
<button type="button" class="comment-toolbar__btn" title="<?php esc_attr_e('快捷回复', 'aksrt'); ?>" data-toolbar-action="quick-reply"><i class="fas fa-bolt"></i></button>
</div>
<div class="comment-form__submit-area">
<?php if (!$is_logged_in) : ?>
<input type="text" name="author" class="form-control form-control-sm comment-form__guest-field" placeholder="<?php esc_attr_e('昵称', 'aksrt'); ?>" required>
<input type="email" name="email" class="form-control form-control-sm comment-form__guest-field" placeholder="<?php esc_attr_e('邮箱', 'aksrt'); ?>" required>
<?php endif; ?>
<button type="submit" class="btn btn-primary btn-sm comment-form__submit" id="comment-submit">
<i class="fas fa-paper-plane" aria-hidden="true"></i>
<?php echo esc_html($comment_submit_text); ?>
</button>
</div>
</div>
<?php if (function_exists('aksrt_is_captcha_enabled_for') && function_exists('aksrt_render_captcha') && aksrt_is_captcha_enabled_for('comment')) : ?>
<div class="comment-form__captcha">
<?php aksrt_render_captcha('comment'); ?>
</div>
<?php endif; ?>
</div>
</div>
</div>
<?php comment_id_fields(); ?>
<?php wp_nonce_field('aksrt_comment_nonce', '_aksrt_comment_nonce'); ?>
<input type="hidden" name="comment_post_ID" value="<?php echo esc_attr((string) get_the_ID()); ?>">
</form>
</div>
<?php endif; ?>
<?php endif; ?>
<?php if (have_comments()) : ?>
<?php $corderby_enabled = !empty($comment_settings['corderby']); ?>
<?php if ($corderby_enabled) : ?>
<div class="comment-filter tab-nav-theme flex ac jsb">
<ul class="list-inline comment-order-box">
<li class="<?php echo $comment_order === 'newest' ? 'active' : ''; ?>"><a href="javascript:;" class="comment-orderby" data-order="newest"><?php esc_html_e('最新', 'aksrt'); ?></a></li>
<li class="<?php echo $comment_order === 'hottest' ? 'active' : ''; ?>"><a href="javascript:;" class="comment-orderby" data-order="hottest"><?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>
<?php endif; ?>
</div>
<?php endif; ?>
<div id="postcomments">
<ol class="commentlist list-unstyled" id="comment-list">
<?php
wp_list_comments([
'style' => 'ol',
'short_ping' => true,
'avatar_size' => $avatar_size,
'max_depth' => $max_depth,
'callback' => 'aksrt_comment_callback',
'reverse_top_level' => $reverse_comments,
'reverse_children' => $reverse_comments,
]);
?>
</ol>
</div>
<?php aksrt_render_comment_pagination(); ?>
<?php elseif (!have_comments() && !post_password_required()) : ?>
<div class="comments-empty post_ajax_loader">
<div class="comments-empty__icon">
<i class="far fa-comment-dots"></i>
</div>
<p class="comments-empty__text"><?php echo esc_html($empty_text); ?></p>
<p class="comments-empty__hint"><?php esc_html_e('成为第一个评论者吧', 'aksrt'); ?></p>
</div>
<?php endif; ?>
<?php if (!comments_open() && get_comments_number() && post_type_supports(get_post_type(), 'comments')) : ?>
<p class="comments-closed"><?php esc_html_e('评论已关闭。', 'aksrt'); ?></p>
<?php endif; ?>
</div>