267 lines
12 KiB
PHP
267 lines
12 KiB
PHP
<?php
|
|
/**
|
|
* Comment list item renderer — zibll DOM contract.
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
if (!function_exists('aksrt_get_comment_like_html')) :
|
|
/**
|
|
* Header like control (zibll places like in comment-header).
|
|
*/
|
|
function aksrt_get_comment_like_html($comment, bool $enabled = true): string {
|
|
if (!$enabled) {
|
|
return '';
|
|
}
|
|
$comment_id = $comment instanceof WP_Comment ? (int) $comment->comment_ID : (int) $comment;
|
|
if ($comment_id <= 0) {
|
|
return '';
|
|
}
|
|
$like_count = function_exists('aksrt_get_comment_likes') ? 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 : '';
|
|
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>',
|
|
esc_attr($classes),
|
|
$is_liked ? '1' : '0',
|
|
$comment_id,
|
|
$is_liked ? 'true' : 'false',
|
|
$is_liked ? 'fa' : 'fa',
|
|
$like_count > 0 ? '' : ' hidden',
|
|
esc_html($count_attr)
|
|
);
|
|
}
|
|
endif;
|
|
|
|
if (!function_exists('aksrt_get_comments_user_name_html')) :
|
|
/**
|
|
* Name row: topping + author link + identity + author/hot badges.
|
|
*
|
|
* @param WP_Comment $comment Comment.
|
|
* @param array<string,mixed> $settings Display settings.
|
|
*/
|
|
function aksrt_get_comments_user_name_html(WP_Comment $comment, array $settings = []): string {
|
|
$user_id = (int) $comment->user_id;
|
|
$show_author_tag = !isset($settings['author_tag']) || !empty($settings['author_tag']);
|
|
$is_pinned = function_exists('aksrt_is_comment_pinned') && aksrt_is_comment_pinned($comment);
|
|
$is_post_author = $user_id > 0 && $user_id === (int) get_post_field('post_author', $comment->comment_post_ID);
|
|
|
|
$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>';
|
|
}
|
|
|
|
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');
|
|
}
|
|
} else {
|
|
$name = '<b class="mr6 aksrt-comment__author">' . esc_html(get_comment_author($comment)) . '</b>';
|
|
}
|
|
|
|
$badges = '';
|
|
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>';
|
|
}
|
|
|
|
return '<name class="flex ac flex1">' . $topping . $name . $badges . '</name>';
|
|
}
|
|
endif;
|
|
|
|
if (!function_exists('aksrt_get_comment_header_html')) :
|
|
/**
|
|
* @param WP_Comment $comment Comment.
|
|
* @param array<string,mixed> $settings Settings.
|
|
*/
|
|
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']);
|
|
$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>';
|
|
}
|
|
endif;
|
|
|
|
if (!function_exists('aksrt_get_comments_action_dropdown_html')) :
|
|
/**
|
|
* More menu: pin / edit / approve / delete (REST-driven via data-comment-action).
|
|
*
|
|
* @param WP_Comment $comment Comment.
|
|
* @param int $depth Depth.
|
|
*/
|
|
function aksrt_get_comments_action_dropdown_html(WP_Comment $comment, int $depth = 0): string {
|
|
$perms = function_exists('aksrt_comment_action_permissions')
|
|
? aksrt_comment_action_permissions($comment)
|
|
: ['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>';
|
|
}
|
|
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>';
|
|
}
|
|
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>';
|
|
}
|
|
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>';
|
|
}
|
|
|
|
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>';
|
|
}
|
|
endif;
|
|
|
|
if (!function_exists('aksrt_get_comment_footer_html')) :
|
|
/**
|
|
* comt-meta: time-ago · city · @parent · approve · reply · dropdown.
|
|
*
|
|
* @param WP_Comment $comment Comment.
|
|
* @param array $args wp_list_comments args.
|
|
* @param int $depth Depth.
|
|
* @param array<string,mixed> $settings Settings.
|
|
*/
|
|
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));
|
|
$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,
|
|
]
|
|
),
|
|
$comment
|
|
);
|
|
if ($link) {
|
|
// Ensure data-commentid for moveForm path.
|
|
if (strpos($link, 'data-commentid') === false) {
|
|
$link = str_replace(
|
|
'comment-reply-link',
|
|
'comment-reply-link" data-commentid="' . (int) $comment->comment_ID . '" data-postid="' . (int) $comment->comment_post_ID,
|
|
$link
|
|
);
|
|
}
|
|
$reply_html = '<span class="reply-link">' . $link . '</span>';
|
|
}
|
|
}
|
|
|
|
$actions = aksrt_get_comments_action_dropdown_html($comment, $depth);
|
|
$info = $time_html . $city_html . $parent_html . $approve_html . $reply_html;
|
|
|
|
return '<div class="comt-meta muted-2-color aksrt-comment__actions">' . $info . $actions . '</div>';
|
|
}
|
|
endif;
|
|
|
|
if (!function_exists('aksrt_comment_callback')) :
|
|
/**
|
|
* Canonical comment list item renderer (zibll structure).
|
|
*
|
|
* @param WP_Comment $comment Comment.
|
|
* @param array $args Args.
|
|
* @param int $depth Depth.
|
|
*/
|
|
function aksrt_comment_callback($comment, $args, $depth) {
|
|
$GLOBALS['comment'] = $comment;
|
|
$settings = get_query_var('aksrt_comment_settings', []);
|
|
$settings = is_array($settings) ? $settings : [];
|
|
if (empty($settings) && class_exists('\AKSRT\Settings\Settings')) {
|
|
$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.
|
|
}
|
|
endif;
|