feat(comments): 评论区按 Zibll 1:1 全量复刻

- 评论项/表单/工具栏/筛选/分页/空状态 DOM 全部替换为 zibll 原始类名
  (li.comment、comt-main、comt-meta、list-inline、dropup 工具栏、
  theme-pagination/pagenav、post_ajax_loader 骨架等)
- 移植 zibll main.css L9412–9751 桌面 + 移动端评论样式,
  缺失基础组件(title-theme/dropdown-menu/line-form/tab-nav-theme 等)
  以 #comments 作用域补齐
- 删除全部 aksrt-comment__* 旧类名与旧样式,无兼容层
- JS 选择器对齐 zibll DOM:片段整块替换 #postcomments、骨架加载、
  跳页输入、prepend 插入、addComment onclick 剥离,
  移除 WP comment-reply enqueue 避免双 moveForm
- 表情资源替换为 zibll 全套 gif(内容逐字节一致,git 无差异)
- REST 后端 (CommentActions) 不变

验证:php -l 全部通过;5 个改动 JS 语法校验通过;
node --test tests/widget-system/post-visual-contract.test.cjs 12/12 通过
This commit is contained in:
2026-08-22 13:29:51 +08:00
parent 251fe86527
commit e1f176429b
15 changed files with 1286 additions and 1439 deletions
+144 -102
View File
@@ -1,6 +1,9 @@
<?php
/**
* Comment list item renderer — zibll DOM contract.
* Comment list item renderer — Zibll DOM contract (1:1).
*
* Mirrors zib_comments_list / zib_get_comment_header / zib_get_comment_footer
* from zibll/inc/functions/zib-comments-list.php.
*
* @package AKSRT
*/
@@ -9,7 +12,11 @@ defined('ABSPATH') || exit;
if (!function_exists('aksrt_get_comment_like_html')) :
/**
* Header like control (zibll places like in comment-header).
* Header like control (zibll: zib_get_comment_like).
*
* @param WP_Comment $comment Comment.
* @param bool $enabled Like enabled.
* @return string
*/
function aksrt_get_comment_like_html($comment, bool $enabled = true): string {
if (!$enabled) {
@@ -19,29 +26,59 @@ if (!function_exists('aksrt_get_comment_like_html')) :
if ($comment_id <= 0) {
return '';
}
$like_count = function_exists('aksrt_get_comment_likes') ? aksrt_get_comment_likes($comment_id) : 0;
$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 aksrt-comment__like' . ($is_liked ? ' is-active' : '');
$count_attr = $like_count > 0 ? (string) $like_count : '';
$classes = 'action action-comment-like muted-2-color flex0 ml10' . ($is_liked ? ' actived' : '');
return sprintf(
'<a href="javascript:;" class="%1$s" data-comment-action="like" data-active="%2$s" data-pid="%3$d" aria-pressed="%4$s"><i class="%5$s fa-thumbs-up" aria-hidden="true"></i><count class="aksrt-comment__like-count"%6$s>%7$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"><i class="fa fa-thumbs-up mr3" aria-hidden="true"></i><count>%5$s</count></a>',
esc_attr($classes),
$is_liked ? '1' : '0',
$comment_id,
$is_liked ? '1' : '0',
$is_liked ? 'true' : 'false',
$is_liked ? 'fa' : 'fa',
$like_count > 0 ? '' : ' hidden',
esc_html($count_attr)
esc_html((string) $like_count)
);
}
endif;
if (!function_exists('aksrt_get_comment_avatar_box')) :
/**
* Comment avatar box (zibll: zib_get_comment_avatar_box).
*
* @param WP_Comment $comment Comment.
* @param string $class Classes for the avatar wrapper.
* @param bool $link Wrap in a link when a target URL exists.
* @return string
*/
function aksrt_get_comment_avatar_box(WP_Comment $comment, string $class = 'avatar-img comt-avatar', bool $link = true): string {
$user_id = (int) $comment->user_id;
$size = 45;
$avatar = $user_id > 0
? get_avatar($user_id, $size, '', '', ['class' => 'avatar'])
: get_avatar($comment->comment_author_email, $size, '', '', ['class' => 'avatar']);
$html = '<span class="' . esc_attr($class) . '">' . $avatar . '</span>';
if (!$link) {
return $html;
}
if ($user_id > 0) {
$url = function_exists('aksrt_get_user_home_url') ? aksrt_get_user_home_url($user_id) : get_author_posts_url($user_id);
return '<a href="' . esc_url($url) . '">' . $html . '</a>';
}
if (!empty($comment->comment_author_url)) {
return '<a rel="nofollow" href="' . esc_url($comment->comment_author_url) . '">' . $html . '</a>';
}
return $html;
}
endif;
if (!function_exists('aksrt_get_comments_user_name_html')) :
/**
* Name row: topping + author link + identity + author/hot badges.
* Name row (zibll: zib_get_comments_user_name) — topping + author + badges.
*
* @param WP_Comment $comment Comment.
* @param array<string,mixed> $settings Display settings.
* @return string
*/
function aksrt_get_comments_user_name_html(WP_Comment $comment, array $settings = []): string {
$user_id = (int) $comment->user_id;
@@ -51,55 +88,65 @@ if (!function_exists('aksrt_get_comments_user_name_html')) :
$topping = '';
if ($is_pinned) {
$topping = '<badge class="badge-topping badge-topping jb-blue mr6 flex0 aksrt-comment__badge aksrt-comment__badge--pinned" title="' . esc_attr__('置顶评论', 'aksrt') . '"><i class="fa fas fa-thumbtack" aria-hidden="true"></i></badge>';
$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>';
}
$user_name = '';
if ($user_id > 0) {
$user = get_userdata($user_id);
$display = $user instanceof WP_User ? $user->display_name : get_comment_author($comment);
$url = function_exists('aksrt_get_user_home_url') ? aksrt_get_user_home_url($user_id) : get_author_posts_url($user_id);
$name = '<a class="text-ellipsis font-bold aksrt-comment__author" href="' . esc_url($url) . '">' . esc_html($display) . '</a>';
if (function_exists('aksrt_render_user_identity_chips')) {
$name .= aksrt_render_user_identity_chips($user_id, 'comment');
if ($user instanceof WP_User) {
$url = function_exists('aksrt_get_user_home_url') ? aksrt_get_user_home_url($user_id) : get_author_posts_url($user_id);
$user_name = '<a class="text-ellipsis font-bold" href="' . esc_url($url) . '">' . esc_html($user->display_name) . '</a>';
if (function_exists('aksrt_render_user_identity_chips')) {
$user_name .= aksrt_render_user_identity_chips($user_id, 'comment');
}
}
} else {
$name = '<b class="mr6 aksrt-comment__author">' . esc_html(get_comment_author($comment)) . '</b>';
} elseif (!empty($comment->comment_author_url)) {
$user_name = '<a class="text-ellipsis font-bold" rel="nofollow" href="' . esc_url($comment->comment_author_url) . '">' . esc_html($comment->comment_author) . '</a>';
}
$badges = '';
$user_name = $user_name !== '' ? $user_name : '<b class="mr6 text-ellipsis">' . esc_html($comment->comment_author) . '</b>';
$badge = '';
if ($show_author_tag && $is_post_author) {
$badges .= '<span class="badg c-green badg-sm flex0 ml3 aksrt-comment__badge">' . esc_html__('作者', 'aksrt') . '</span>';
}
if (function_exists('aksrt_is_blog_comment_hot') && aksrt_is_blog_comment_hot($comment)) {
$badges .= '<span class="badg c-yellow badg-sm flex0 ml3 aksrt-comment__badge aksrt-comment__badge--hot"><i class="fa fas fa-fire" aria-hidden="true"></i> ' . esc_html__('神评', 'aksrt') . '</span>';
$badge .= '<span class="badg c-green badg-sm flex0 ml3">' . esc_html__('作者', 'aksrt') . '</span>';
}
return '<name class="flex ac flex1">' . $topping . $name . $badges . '</name>';
return '<name class="flex ac flex1">' . $topping . $user_name . $badge . '</name>';
}
endif;
if (!function_exists('aksrt_get_comment_header_html')) :
/**
* Comment header (zibll: zib_get_comment_header).
*
* @param WP_Comment $comment Comment.
* @param array<string,mixed> $settings Settings.
* @return string
*/
function aksrt_get_comment_header_html(WP_Comment $comment, array $settings = []): string {
$avatar_size = max(24, (int) ($settings['avatar_size'] ?? 45));
$like_enabled = !isset($settings['like_enabled']) || !empty($settings['like_enabled']);
$avatar = get_avatar($comment, $avatar_size, '', '', ['class' => 'avatar-img comt-avatar']);
$avatar = aksrt_get_comment_avatar_box($comment, 'avatar-img comt-avatar');
$name = aksrt_get_comments_user_name_html($comment, $settings);
$like = aksrt_get_comment_like_html($comment, $like_enabled);
$inner = '<div class="author-box flex ac">' . $avatar . $name . $like . '</div>';
return '<div class="comment-header mb10 aksrt-comment__header">' . $inner . '</div>';
$html = '<div class="author-box flex ac">';
$html .= $avatar;
$html .= $name;
$html .= $like;
$html .= '</div>';
return '<div class="comment-header mb10">' . $html . '</div>';
}
endif;
if (!function_exists('aksrt_get_comments_action_dropdown_html')) :
/**
* More menu: pin / edit / approve / delete (REST-driven via data-comment-action).
* More-actions menu (zibll: zib_get_comments_action_list) — pin / edit / approve / delete.
*
* @param WP_Comment $comment Comment.
* @param int $depth Depth.
* @return string
*/
function aksrt_get_comments_action_dropdown_html(WP_Comment $comment, int $depth = 0): string {
$perms = function_exists('aksrt_comment_action_permissions')
@@ -107,95 +154,86 @@ if (!function_exists('aksrt_get_comments_action_dropdown_html')) :
: ['can_edit' => false, 'can_delete' => false, 'can_moderate' => false, 'can_pin' => false];
$is_pinned = function_exists('aksrt_is_comment_pinned') && aksrt_is_comment_pinned($comment);
$is_pending = '0' === (string) $comment->comment_approved;
$is_spam = 'spam' === (string) $comment->comment_approved;
$lists = '';
if (!empty($perms['can_pin']) && (int) $comment->comment_parent === 0) {
$label = $is_pinned ? __('取消置顶', 'aksrt') : __('设置置顶', 'aksrt');
$lists .= '<li><a href="javascript:;" class="comment-topping-link" data-comment-action="pin" data-pinned="' . ($is_pinned ? '1' : '0') . '"><i class="fa fas fa-thumbtack fa-fw mr10" aria-hidden="true"></i>' . esc_html($label) . '</a></li>';
$label = $is_pinned ? __('取消置顶', 'aksrt') : __('设置置顶', 'aksrt');
$lists .= '<li><a class="comment-topping wp-ajax-submit" data-comment-action="pin" data-pinned="' . ($is_pinned ? '1' : '0') . '" href="javascript:;"><i class="fa fa-thumbtack mr10 fa-fw" aria-hidden="true"></i>' . esc_html($label) . '</a></li>';
}
if (!empty($perms['can_edit'])) {
$lists .= '<li><a href="javascript:;" class="comment-edit-link" data-comment-action="edit" data-commentid="' . (int) $comment->comment_ID . '" data-postid="' . (int) $comment->comment_post_ID . '"><i class="fa fas fa-edit fa-fw mr10" aria-hidden="true"></i>' . esc_html__('编辑', 'aksrt') . '</a></li>';
$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'])) {
if ($is_pending) {
$lists .= '<li><a href="javascript:;" class="comment-approve-link approve" data-comment-action="approve"><i class="fa fas fa-check fa-fw mr10" aria-hidden="true"></i><text>' . esc_html__('批准', 'aksrt') . '</text></a></li>';
} elseif (!$is_spam) {
$lists .= '<li><a href="javascript:;" class="comment-approve-link unapprove" data-comment-action="hold"><i class="fa fas fa-eye-slash fa-fw mr10" aria-hidden="true"></i><text>' . esc_html__('驳回', 'aksrt') . '</text></a></li>';
}
$lists .= '<li><a href="javascript:;" class="comment-spam-link" data-comment-action="spam"><i class="fa fas fa-ban fa-fw mr10" aria-hidden="true"></i>' . esc_html__('垃圾', 'aksrt') . '</a></li>';
$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>';
}
if (!empty($perms['can_delete'])) {
$lists .= '<li><a href="javascript:;" class="comment-trash-link c-red" data-comment-action="delete" data-commentid="' . (int) $comment->comment_ID . '"><i class="fa fas fa-trash-alt fa-fw mr10" aria-hidden="true"></i>' . esc_html__('删除', 'aksrt') . '</a></li>';
$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>';
}
if ($lists === '') {
return '';
}
return '<span class="dropdown drop-fixed-sm padding-6 comment-action-dropdown">'
. '<a href="javascript:;" class="muted-color padding-6" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" aria-label="' . esc_attr__('更多操作', 'aksrt') . '"><i class="fa fas fa-ellipsis-h" aria-hidden="true"></i></a>'
. '<ul class="dropdown-menu">' . $lists . '</ul>'
. '</span>';
$icon_a = '<a href="javascript:;" class="muted-color padding-6" data-toggle="dropdown"><i class="fa fa-ellipsis-h" aria-hidden="true"></i></a>';
return '<span class="dropdown drop-fixed-sm padding-6">' . $icon_a . '<ul class="dropdown-menu">' . $lists . '</ul></span>';
}
endif;
if (!function_exists('aksrt_get_comment_footer_html')) :
/**
* comt-meta: time-ago · city · @parent · approve · reply · dropdown.
* comt-meta row (zibll: zib_get_comment_footer).
*
* @param WP_Comment $comment Comment.
* @param array $args wp_list_comments args.
* @param int $depth Depth.
* @param array<string,mixed> $settings Settings.
* @return string
*/
function aksrt_get_comment_footer_html(WP_Comment $comment, array $args, int $depth, array $settings = []): string {
$is_pending = '0' === (string) $comment->comment_approved;
// Time ago.
$absolute = date_i18n('Y-m-d H:i:s', strtotime((string) $comment->comment_date));
$badg_approve = '';
if ($is_pending) {
$badg_approve = '<span class="badg c-red badg-sm">' . esc_html__('待审核', 'aksrt') . '</span>';
}
$badg_approve = '<span class="badge-approve">' . $badg_approve . '</span>';
$absolute = date_i18n(__('Y年m月d日 H:i:s', 'aksrt'), strtotime((string) $comment->comment_date));
$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>';
// City.
$city_html = function_exists('aksrt_get_comment_city_badge_html')
? aksrt_get_comment_city_badge_html($comment)
: '';
// @ parent.
$parent_html = '';
if ((int) $comment->comment_parent > 0) {
$parent_author = get_comment_author((int) $comment->comment_parent);
$parent_html = '<span>@<a rel="nofollow" class="url" href="javascript:;" data-scroll-to="#comment-' . (int) $comment->comment_parent . '">' . esc_html($parent_author) . '</a></span>';
}
// Pending badge.
$approve_html = '<span class="badge-approve">';
if ($is_pending) {
$approve_html .= '<span class="badg c-red badg-sm">' . esc_html__('待审核', 'aksrt') . '</span>';
}
$approve_html .= '</span>';
// Reply.
$reply_html = '';
if (!$is_pending && $depth > 0) {
$max_depth = (int) ($args['max_depth'] ?? get_option('thread_comments_depth', 5));
$link = get_comment_reply_link(
array_merge(
$args,
[
'add_below' => 'div-comment',
'reply_text' => __('回复', 'aksrt'),
'depth' => $depth,
'max_depth' => $max_depth,
]
),
[
'add_below' => 'div-comment',
'reply_text' => __('回复', 'aksrt'),
'login_text' => __('回复', 'aksrt'),
'depth' => $depth,
'max_depth' => $max_depth,
],
$comment
);
if ($link) {
// Ensure data-commentid for moveForm path.
// 去掉 WP 内联 onclick(addComment.moveForm),回复由 comment-move-form.js 事件委托处理。
$link = preg_replace('/ onclick="[^"]*"/i', '', $link);
if (strpos($link, 'data-commentid') === false) {
$link = str_replace(
'comment-reply-link',
@@ -208,15 +246,49 @@ if (!function_exists('aksrt_get_comment_footer_html')) :
}
$actions = aksrt_get_comments_action_dropdown_html($comment, $depth);
$info = $time_html . $city_html . $parent_html . $approve_html . $reply_html;
$info = $time_html . $city_html . $parent_html . $badg_approve . $reply_html;
return '<div class="comt-meta muted-2-color aksrt-comment__actions">' . $info . $actions . '</div>';
return '<div class="comt-meta muted-2-color">' . $info . $actions . '</div>';
}
endif;
if (!function_exists('aksrt_get_comments_list_html')) :
/**
* Single comment item HTML (zibll: zib_get_comments_list).
*
* @param WP_Comment $comment Comment.
* @param array $args Args.
* @param int $depth Depth.
* @param array $settings Settings.
* @return string
*/
function aksrt_get_comments_list_html(WP_Comment $comment, array $args = [], int $depth = 0, array $settings = []): string {
$comment_id = (int) $comment->comment_ID;
$con = function_exists('aksrt_render_comment_content')
? aksrt_render_comment_content((string) get_comment_text($comment))
: wp_kses_post((string) get_comment_text($comment));
$header = aksrt_get_comment_header_html($comment, $settings);
$footer = aksrt_get_comment_footer_html($comment, $args, $depth, $settings);
$html = '<li ' . comment_class('', $comment, null, false) . ' id="comment-' . esc_attr((string) $comment_id) . '">';
$html .= '<ul class="list-inline">';
$html .= '<li class="comt-main" id="div-comment-' . esc_attr((string) $comment_id) . '">';
$html .= $header;
$html .= '<div class="comment-footer">';
$html .= '<div class="mb10 comment-content" id="comment-content-' . esc_attr((string) $comment_id) . '">' . $con . '</div>';
$html .= $footer;
$html .= '</div>';
$html .= '</li>';
$html .= '</ul>';
return $html;
}
endif;
if (!function_exists('aksrt_comment_callback')) :
/**
* Canonical comment list item renderer (zibll structure).
* Canonical wp_list_comments callback (zibll: zib_comments_list).
*
* The closing </li> is emitted by the walker after children.
*
* @param WP_Comment $comment Comment.
* @param array $args Args.
@@ -230,37 +302,7 @@ if (!function_exists('aksrt_comment_callback')) :
$settings = \AKSRT\Settings\Settings::get_comment_display_settings();
}
$comment_id = (int) $comment->comment_ID;
$depth_class = 'aksrt-depth-' . min((int) ($settings['nested_depth'] ?? 3), (int) $depth);
$is_pinned = function_exists('aksrt_is_comment_pinned') && aksrt_is_comment_pinned($comment);
$is_pending = '0' === (string) $comment->comment_approved;
$li_classes = trim($depth_class . ($is_pinned ? ' aksrt-comment--pinned' : ''));
$header = aksrt_get_comment_header_html($comment, $settings);
$footer = aksrt_get_comment_footer_html($comment, $args, (int) $depth, $settings);
if ($is_pending) {
$body = '<p class="awaiting-moderation">' . esc_html__('您的评论正在等待审核。', 'aksrt') . '</p>';
} else {
$body = function_exists('aksrt_render_comment_content')
? aksrt_render_comment_content((string) get_comment_text($comment))
: wp_kses_post((string) get_comment_text($comment));
}
echo '<li ';
comment_class('aksrt-comment ' . $li_classes);
echo ' id="comment-' . esc_attr((string) $comment_id) . '" data-comment-id="' . esc_attr((string) $comment_id) . '" data-comment-status="' . esc_attr((string) $comment->comment_approved) . '">';
echo '<ul class="list-inline">';
echo '<li class="comt-main" id="div-comment-' . esc_attr((string) $comment_id) . '">';
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- composed helpers escape.
echo $header;
echo '<div class="comment-footer">';
echo '<div class="mb10 comment-content aksrt-comment__text" id="comment-content-' . esc_attr((string) $comment_id) . '">' . $body . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $footer;
echo '</div>';
echo '</li>';
echo '</ul>';
// Closing </li> is emitted by Walker_Comment after children.
echo aksrt_get_comments_list_html($comment, is_array($args) ? $args : [], (int) $depth, $settings);
}
endif;