评论区硬切换实现

This commit is contained in:
kate522 committed 2026-08-30 17:34:29 +08:00
1 parent ed914d2eec
commit b8ef4d81a4
32 files changed
+946 -1008

No files matched your search

+1
View File
@@ -23,6 +23,7 @@ require_once AKSRT_DIR . '/inc/helpers/aksrt-ip-geo.php';
require_once AKSRT_DIR . '/inc/helpers/comments/content.php';
require_once AKSRT_DIR . '/inc/helpers/comments/actions.php';
require_once AKSRT_DIR . '/inc/helpers/comments/geo.php';
require_once AKSRT_DIR . '/inc/helpers/comments/icons.php';
require_once AKSRT_DIR . '/inc/helpers/comments/form-parts.php';
require_once AKSRT_DIR . '/inc/helpers/comments/item.php';
require_once AKSRT_DIR . '/inc/helpers/aksrt-flow.php';
+52 -45
View File
@@ -93,35 +93,33 @@ if (!function_exists('aksrt_comments_build_context')) :
? $settings['toolbar']
: ['code' => true, 'image' => true];
$phrases = is_array($settings['quick_phrases'] ?? null) ? $settings['quick_phrases'] : [];
return [
'settings' => $settings,
'comment_count' => (int) get_comments_number(),
'is_logged_in' => is_user_logged_in(),
'current_user' => wp_get_current_user(),
'require_login' => !empty($settings['require_login']),
'order' => $order,
'reverse_comments' => $order === 'newest',
'is_hottest' => $order === 'hottest',
'author_only' => isset($_GET['aksrt_author_only']) && (int) $_GET['aksrt_author_only'] === 1,
'post_author_id' => (int) get_the_author_meta('ID'),
'avatar_size' => $avatar_size,
'max_depth' => max(1, (int) ($settings['nested_depth'] ?? 3)),
'empty_text' => (string) ($settings['empty_text'] ?? __('暂无评论,来做第一个评论的人吧~', 'aksrt')),
'toolbar' => $toolbar,
'upload_enabled' => !empty($toolbar['upload_img']),
'box_placeholder' => (string) ($settings['box_placeholder'] ?? __('说点什么吧...', 'aksrt')),
'submit_text' => (string) ($settings['submit_text'] ?? __('提交评论', 'aksrt')),
'title_text' => (string) ($settings['title'] ?? __('评论', 'aksrt')),
'corderby_enabled' => !empty($settings['corderby']),
'close_comments' => !empty($settings['close_comments']),
'quick_s' => !isset($settings['quick_s']) || !empty($settings['quick_s']),
'quick_phrases' => $phrases,
'oauth_providers' => class_exists('\AKSRT\Auth\OAuth')
? \AKSRT\Auth\OAuth::get_instance()->get_auth_providers_data()
: [],
];
return [
'settings' => $settings,
'comment_count' => (int) get_comments_number(),
'is_logged_in' => is_user_logged_in(),
'current_user' => wp_get_current_user(),
'require_login' => !empty($settings['require_login']),
'order' => $order,
'reverse_comments' => $order === 'newest',
'is_hottest' => $order === 'hottest',
'author_only' => isset($_GET['aksrt_author_only']) && (int) $_GET['aksrt_author_only'] === 1,
'post_author_id' => (int) get_the_author_meta('ID'),
'avatar_size' => $avatar_size,
'max_depth' => max(1, (int) ($settings['nested_depth'] ?? 3)),
'empty_text' => (string) ($settings['empty_text'] ?? __('暂无评论,来做第一个评论的人吧~', 'aksrt')),
'toolbar' => $toolbar,
'upload_enabled' => !empty($toolbar['upload_img']),
'link_enabled' => !empty($toolbar['link']),
'user_url_enabled' => !empty($settings['user_url']),
'box_placeholder' => (string) ($settings['box_placeholder'] ?? __('说点什么吧...', 'aksrt')),
'submit_text' => (string) ($settings['submit_text'] ?? __('提交评论', 'aksrt')),
'title_text' => (string) ($settings['title'] ?? __('评论', 'aksrt')),
'corderby_enabled' => !empty($settings['corderby']),
'close_comments' => !empty($settings['close_comments']),
'oauth_providers' => class_exists('\AKSRT\Auth\OAuth')
? \AKSRT\Auth\OAuth::get_instance()->get_auth_providers_data()
: [],
];
}
endif;
@@ -409,7 +407,7 @@ if (!function_exists('aksrt_comments_render_signarea')) :
<?php if (get_option('users_can_register')) :
$register_url = function_exists('aksrt_get_register_url') ? aksrt_get_register_url() : wp_registration_url();
?>
<a href="javascript:;" data-register-url="<?php echo esc_url($register_url); ?>" class="signup-loader ml10 but c-yellow padding-lg"><i class="fa fa-fw fa-user-plus mr10" aria-hidden="true"></i><?php esc_html_e('注册', 'aksrt'); ?></a>
<a href="javascript:;" data-register-url="<?php echo esc_url($register_url); ?>" class="signup-loader ml10 but c-yellow padding-lg"><?php echo aksrt_get_comment_icon('signup', 'icon mr10'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- 固定 sprite 标记。 ?><?php esc_html_e('注册', 'aksrt'); ?></a>
<?php endif; ?>
</p>
<?php if (!empty($oauth_providers)) : ?>
@@ -448,6 +446,9 @@ if (!function_exists('aksrt_comments_render_form')) :
$comment_email = isset($_COOKIE['comment_author_email_' . COOKIEHASH])
? sanitize_text_field(wp_unslash((string) $_COOKIE['comment_author_email_' . COOKIEHASH]))
: '';
$comment_author_url = isset($_COOKIE['comment_author_url_' . COOKIEHASH])
? esc_url_raw(wp_unslash((string) $_COOKIE['comment_author_url_' . COOKIEHASH]))
: '';
$placeholder = (string) $ctx['box_placeholder'];
$post_id = (int) get_the_ID();
$can_comment = function_exists('aksrt_user_can')
@@ -471,13 +472,13 @@ if (!function_exists('aksrt_comments_render_form')) :
<div class="fixed-body"></div>
<form id="commentform" method="post" action="<?php echo esc_url(site_url('/wp-comments-post.php')); ?>">
<div class="flex ac">
<div class="comt-title text-center flex0 mr10">
<?php if ($is_logged_in && $current_user instanceof WP_User) : ?>
<div class="comt-avatar mb10">
<?php echo get_avatar($current_user->ID, 60, '', '', ['class' => 'avatar']); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</div>
<p class="text-ellipsis muted-2-color"><?php echo esc_html($current_user->display_name); ?></p>
<?php else : ?>
<div class="comt-title text-center flex0 mr10">
<?php if ($is_logged_in && $current_user instanceof WP_User) : ?>
<div class="comt-avatar mb10">
<?php echo get_avatar($current_user->ID, 60, '', '', ['class' => 'avatar']); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</div>
<p class="text-ellipsis muted-2-color"><?php echo function_exists('aksrt_render_user_identity_chips') ? aksrt_render_user_identity_chips($current_user->ID, 'comment') : ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- chips 内部已转义。 ?><?php echo esc_html($current_user->display_name); ?></p>
<?php else : ?>
<div class="comt-avatar mb10">
<?php echo get_avatar(0, 60, '', '', ['class' => 'avatar']); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</div>
@@ -524,6 +525,14 @@ if (!function_exists('aksrt_comments_render_form')) :
<div class="abs-right muted-color"><i class="fa fa-fw fa-envelope-o" aria-hidden="true"></i></div>
<i class="line-form-line"></i>
</li>
<?php if (!empty($ctx['user_url_enabled'])) : ?>
<li class="line-form">
<input type="text" name="url" class="line-form-input" tabindex="3" value="<?php echo esc_attr($comment_author_url); ?>" placeholder="">
<div class="scale-placeholder"><?php esc_html_e('博客地址', 'aksrt'); ?></div>
<div class="abs-right muted-color"><i class="fa fa-fw fa-link" aria-hidden="true"></i></div>
<i class="line-form-line"></i>
</li>
<?php endif; ?>
</ul>
</div>
<?php
@@ -623,7 +632,7 @@ if (!function_exists('aksrt_comments_render')) :
* Compose the full comments surface.
*/
function aksrt_comments_render(): void {
if (post_password_required()) {
if (post_password_required() || !comments_open()) {
return;
}
@@ -652,12 +661,10 @@ if (!function_exists('aksrt_comments_render')) :
</div>
<div class="no_webshot main-bg theme-box box-body radius8 main-shadow">
<?php if (comments_open()) : ?>
<?php if (!empty($ctx['require_login']) && empty($ctx['is_logged_in'])) : ?>
<?php aksrt_comments_render_signarea($ctx); ?>
<?php else : ?>
<?php aksrt_comments_render_form($ctx); ?>
<?php endif; ?>
<?php if (!empty($ctx['require_login']) && empty($ctx['is_logged_in'])) : ?>
<?php aksrt_comments_render_signarea($ctx); ?>
<?php else : ?>
<?php aksrt_comments_render_form($ctx); ?>
<?php endif; ?>
<div id="postcomments">
@@ -670,7 +677,7 @@ if (!function_exists('aksrt_comments_render')) :
if (have_comments()) {
aksrt_comments_render_list_items($ctx);
} elseif (!post_password_required()) {
} else {
aksrt_comments_render_empty($ctx);
echo '<div class="pagenav hide"><div class="next-page ajax-next"><a href="#"></a></div></div>';
}
+6 -26
View File
@@ -39,9 +39,9 @@ if (!function_exists('aksrt_get_input_expand_but')) :
/**
* legacy-style expand toolbar button + dropdown (legacy: legacy).
*
* @param string $type code|image|quick|link.
* @param string $type code|image|link.
* @param mixed $arg1 Type-specific arg.
* @param array<string,mixed> $ctx Optional context (upload, phrases).
* @param array<string,mixed> $ctx Optional context (upload).
* @return string
*/
function aksrt_get_input_expand_but(string $type, $arg1 = null, array $ctx = []): string {
@@ -59,25 +59,6 @@ if (!function_exists('aksrt_get_input_expand_but')) :
$dropdown .= '</div>';
break;
case 'quick':
$phrases = [];
if (is_string($arg1) && $arg1 !== '') {
$phrases = array_values(array_filter(array_map('trim', preg_split('/\r\n|\r|\n/', $arg1) ?: [])));
} elseif (!empty($ctx['quick_phrases']) && is_array($ctx['quick_phrases'])) {
$phrases = $ctx['quick_phrases'];
}
$sys_lists = '';
foreach ($phrases as $p) {
$sys_lists .= '<div class="quick-reply-item">' . esc_html((string) $p) . '</div>';
}
if ($sys_lists === '') {
$sys_lists = '<div class="text-center muted-2-color" style="margin: 60px 0;">' . esc_html__('系统暂无快捷回复', 'aksrt') . '</div>';
}
$but = '<a class="but btn-input-expand input-quick mr6" href="javascript:;"><i class="fa fa-fw fa-bolt" aria-hidden="true"></i><span class="hide-sm">' . esc_html__('快捷回复', 'aksrt') . '</span></a>';
$dropdown = '<div class="relative dropdown-quick-often"><div class="quick-often-box scroll-y mini-scrollbar">' . $sys_lists . '</div></div>';
break;
case 'code':
$but = '<a class="but btn-input-expand input-code mr6" href="javascript:;"><i class="fa fa-fw fa-code" aria-hidden="true"></i><span class="hide-sm">' . esc_html__('代码', 'aksrt') . '</span></a>';
$dropdown = '<div class="dropdown-code">';
@@ -114,15 +95,14 @@ endif;
if (!function_exists('aksrt_render_comment_expand_toolbar')) :
/**
* Render left-side expand tools based on settings (legacy order: code/image/link/quick).
* Render left-side expand tools based on settings (legacy order: code/image/link).
*
* @param array<string,mixed> $ctx Comments context.
*/
function aksrt_render_comment_expand_toolbar(array $ctx): void {
$toolbar = is_array($ctx['toolbar'] ?? null) ? $ctx['toolbar'] : [];
$upload = !empty($ctx['upload_enabled']);
$phrases = is_array($ctx['quick_phrases'] ?? null) ? $ctx['quick_phrases'] : [];
$quick_s = !isset($ctx['quick_s']) || !empty($ctx['quick_s']);
$link_s = !empty($ctx['link_enabled']);
if (!empty($toolbar['code'])) {
echo aksrt_get_input_expand_but('code'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
@@ -130,8 +110,8 @@ if (!function_exists('aksrt_render_comment_expand_toolbar')) :
if (!empty($toolbar['image'])) {
echo aksrt_get_input_expand_but('image', $upload, ['upload' => $upload]); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
if ($quick_s) {
echo aksrt_get_input_expand_but('quick', null, ['quick_phrases' => $phrases]); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
if ($link_s) {
echo aksrt_get_input_expand_but('link'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
endif;
+25
View File
@@ -0,0 +1,25 @@
<?php
/**
* Comment sprite icons — <use> references into the runtime svg-icon sprite.
*
* Mirrors zib_get_svg() from the reference comments module. Symbol
* definitions are injected by assets/js/runtime/svg-icon.js, which the
* vendor runtime lazy-loads on every page (main.js: tbquire(['svg-icon'])).
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
if (!function_exists('aksrt_get_comment_icon')) :
/**
* Sprite-referenced SVG icon (legacy: zib_get_svg).
*
* @param string $name Sprite key (like / topping / approve / menu_2 / signup).
* @param string $class Classes for the svg element.
* @return string
*/
function aksrt_get_comment_icon(string $name, string $class = 'icon'): string {
return '<svg class="' . esc_attr($class) . '" aria-hidden="true"><use xlink:href="#icon-' . esc_attr($name) . '"></use></svg>';
}
endif;
+17 -8
View File
@@ -29,13 +29,15 @@ if (!function_exists('aksrt_get_comment_like_html')) :
$like_count = function_exists('aksrt_get_comment_likes') ? (int) aksrt_get_comment_likes($comment_id) : 0;
$is_liked = function_exists('aksrt_is_comment_liked') && aksrt_is_comment_liked($comment_id);
$classes = 'action action-comment-like muted-2-color flex0 ml10' . ($is_liked ? ' actived' : '');
$icon = function_exists('aksrt_get_comment_icon') ? aksrt_get_comment_icon('like', 'icon mr3') : '';
return sprintf(
'<a href="javascript:;" data-comment-action="like" data-action="comment_like" class="%1$s" data-pid="%2$d" data-active="%3$s" aria-pressed="%4$s"><i class="fa fa-thumbs-up mr3" aria-hidden="true"></i><count>%5$s</count></a>',
'<a href="javascript:;" data-comment-action="like" data-action="comment_like" class="%1$s" data-pid="%2$d" data-active="%3$s" aria-pressed="%4$s">%5$s<text></text><count>%6$s</count></a>',
esc_attr($classes),
$comment_id,
$is_liked ? '1' : '0',
$is_liked ? 'true' : 'false',
$icon, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- fixed sprite markup.
esc_html((string) $like_count)
);
}
@@ -88,7 +90,8 @@ if (!function_exists('aksrt_get_comments_user_name_html')) :
$topping = '';
if ($is_pinned) {
$topping = '<badge class="badge-topping jb-blue mr6 flex0" title="' . esc_attr__('置顶评论', 'aksrt') . '" data-toggle="tooltip"><i class="fa fa-thumbtack" aria-hidden="true"></i></badge>';
$topping_icon = function_exists('aksrt_get_comment_icon') ? aksrt_get_comment_icon('topping') : '';
$topping = '<badge class="badge-topping jb-blue mr6 flex0" title="' . esc_attr__('置顶评论', 'aksrt') . '" data-toggle="tooltip">' . $topping_icon . '</badge>';
}
$user_name = '';
@@ -166,10 +169,11 @@ if (!function_exists('aksrt_get_comments_action_dropdown_html')) :
$lists .= '<li><a class="comment-edit-link" data-commentid="' . (int) $comment->comment_ID . '" data-postid="' . (int) $comment->comment_post_ID . '" href="javascript:;"><i class="fa fa-edit mr10 fa-fw" aria-hidden="true"></i>' . esc_html__('编辑', 'aksrt') . '</a></li>';
}
if (!empty($perms['can_moderate'])) {
$text = $is_pending ? __('批准', 'aksrt') : __('驳回', 'aksrt');
$class = $is_pending ? 'approve' : 'unapprove';
$action = $is_pending ? 'approve' : 'hold';
$lists .= '<li><a class="comment-approve-link ' . $class . '" data-comment-action="' . $action . '" data-commentid="' . (int) $comment->comment_ID . '" data-postid="' . (int) $comment->comment_post_ID . '" href="javascript:;"><i class="fa fa-check mr10 fa-fw" aria-hidden="true"></i><text>' . esc_html($text) . '</text></a></li>';
$text = $is_pending ? __('批准', 'aksrt') : __('驳回', 'aksrt');
$class = $is_pending ? 'approve' : 'unapprove';
$action = $is_pending ? 'approve' : 'hold';
$approve_icon = function_exists('aksrt_get_comment_icon') ? aksrt_get_comment_icon('approve', 'mr10 icon fa-fw') : '';
$lists .= '<li><a class="comment-approve-link ' . $class . '" data-comment-action="' . $action . '" data-commentid="' . (int) $comment->comment_ID . '" data-postid="' . (int) $comment->comment_post_ID . '" href="javascript:;">' . $approve_icon . '<text>' . esc_html($text) . '</text></a></li>';
}
if (!empty($perms['can_delete'])) {
$lists .= '<li><a class="comment-trash-link c-red" data-comment-action="delete" data-commentid="' . (int) $comment->comment_ID . '" data-postid="' . (int) $comment->comment_post_ID . '" href="javascript:;"><i class="fa fa-trash-o mr10 fa-fw" aria-hidden="true"></i>' . esc_html__('删除', 'aksrt') . '</a></li>';
@@ -179,7 +183,8 @@ if (!function_exists('aksrt_get_comments_action_dropdown_html')) :
return '';
}
$icon_a = '<a href="javascript:;" class="muted-color padding-6" data-toggle="dropdown"><i class="fa fa-ellipsis-h" aria-hidden="true"></i></a>';
$menu_icon = function_exists('aksrt_get_comment_icon') ? aksrt_get_comment_icon('menu_2') : '';
$icon_a = '<a href="javascript:;" class="muted-color padding-6" data-toggle="dropdown">' . $menu_icon . '</a>';
return '<span class="dropdown drop-fixed-sm padding-6">' . $icon_a . '<ul class="dropdown-menu">' . $lists . '</ul></span>';
}
@@ -198,6 +203,8 @@ if (!function_exists('aksrt_get_comment_footer_html')) :
function aksrt_get_comment_footer_html(WP_Comment $comment, array $args, int $depth, array $settings = []): string {
$is_pending = '0' === (string) $comment->comment_approved;
$is_mobile = wp_is_mobile();
$badg_approve = '';
if ($is_pending) {
$badg_approve = '<span class="badg c-red badg-sm">' . esc_html__('待审核', 'aksrt') . '</span>';
@@ -208,7 +215,7 @@ if (!function_exists('aksrt_get_comment_footer_html')) :
$ago = function_exists('aksrt_time_ago')
? aksrt_time_ago((string) $comment->comment_date)
: human_time_diff(strtotime((string) $comment->comment_date), current_time('timestamp')) . __('前', 'aksrt');
$time_html = '<span class="comt-author" title="' . esc_attr($absolute) . '">' . esc_html($ago) . '</span>';
$time_html = '<span class="comt-author"' . ($is_mobile ? ' data-toggle="tooltip"' : '') . ' title="' . esc_attr($absolute) . '">' . esc_html($ago) . '</span>';
$city_html = function_exists('aksrt_get_comment_city_badge_html')
? aksrt_get_comment_city_badge_html($comment)
@@ -236,6 +243,8 @@ if (!function_exists('aksrt_get_comment_footer_html')) :
if ($link) {
// 去掉 WP 内联 onclick(addComment.moveForm),回复由 comment-move-form.js 事件委托处理。
$link = preg_replace('/ onclick="[^"]*"/i', '', $link);
// legacy 契约:aria-label 换成 tooltip 触发属性。
$link = preg_replace('# aria-label=#', $is_mobile ? ' title=' : ' data-toggle="tooltip" title=', $link);
if (strpos($link, 'data-commentid') === false) {
$link = str_replace(
'comment-reply-link',