feat(widgets): RecentComments posts-mini identity + like depth

Align comment mini list with zibll: posts-mini rows, avatar box,
user name/identity, time_ago, comment like, and multi-ID filters.
This commit is contained in:
2026-07-21 08:59:27 +08:00
parent d31b3c9f91
commit dff9d7dc1c
+130 -37
View File
@@ -1,6 +1,6 @@
<?php
/**
* Recent comments mini list (zibll widget_ui_new_comment + remote HTML).
* Recent comments mini list (zibll widget_ui_new_comment → zib_widget_comments).
*
* @package AKSRT
*/
@@ -13,65 +13,149 @@ class RecentCommentsWidget extends \WP_Widget {
public function __construct() {
parent::__construct('aksrt_recent_comments', esc_html__('AKSRT 最新评论', 'aksrt'), [
'description' => esc_html__('显示最新评论列表', 'aksrt'),
'description' => esc_html__('显示最新评论列表(posts-mini + 身份/点赞)。', 'aksrt'),
]);
}
/**
* @param array<string,mixed> $instance
*/
public static function render_html(array $instance): string {
$count = max(1, min(20, (int) ($instance['count'] ?? $instance['limit'] ?? 5)));
$show_avatar = !isset($instance['show_avatar']) || !empty($instance['show_avatar']);
$outpost = max(0, (int) ($instance['outpost'] ?? 0));
$outer = sanitize_text_field((string) ($instance['outer'] ?? ''));
// zibll: outpost = post__not_in (multi-ID), outer = author__not_in (multi-ID).
// Also keep optional keyword filter via outer_keywords for legacy installs.
$outpost_raw = (string) ($instance['outpost'] ?? '');
$outer_raw = (string) ($instance['outer'] ?? '');
$keywords = sanitize_text_field((string) ($instance['outer_keywords'] ?? ''));
$post_not_in = array_values(array_filter(array_map('intval', preg_split('/[,,\s]+/', $outpost_raw) ?: [])));
// Heuristic: if outer looks numeric/id-list → author__not_in; else treat as keyword filter (legacy).
$author_not_in = [];
$legacy_keys = [];
if ($outer_raw !== '') {
$parts = array_filter(array_map('trim', preg_split('/[,,\s]+/', $outer_raw) ?: []));
$all_numeric = $parts && array_reduce($parts, static function ($ok, $p) {
return $ok && ctype_digit((string) $p);
}, true);
if ($all_numeric) {
$author_not_in = array_values(array_filter(array_map('intval', $parts)));
} else {
$legacy_keys = $parts;
}
}
if ($keywords !== '') {
$legacy_keys = array_merge($legacy_keys, array_filter(array_map('trim', preg_split('/[,,]+/', $keywords) ?: [])));
}
$query = [
'number' => $count,
'number' => $count * 2, // over-fetch when filtering keywords.
'status' => 'approve',
'post_status' => 'publish',
'post_type' => ['post'],
'orderby' => 'comment_date_gmt',
'order' => 'DESC',
];
if ($outpost > 0) {
$query['post__not_in'] = [$outpost];
// Include forum topics when forum is active.
if (post_type_exists('forum_topic') || post_type_exists('forum_post')) {
$query['post_type'][] = post_type_exists('forum_topic') ? 'forum_topic' : 'forum_post';
}
if ($post_not_in) {
$query['post__not_in'] = $post_not_in;
}
if ($author_not_in) {
$query['author__not_in'] = $author_not_in;
}
$comments = get_comments($query);
if ($outer !== '' && $comments) {
$keys = array_filter(array_map('trim', preg_split('/[,,]/', $outer) ?: []));
if ($keys) {
$comments = array_values(array_filter($comments, static function ($c) use ($keys) {
$body = (string) $c->comment_content;
foreach ($keys as $k) {
if ($k !== '' && str_contains($body, $k)) {
return false;
}
if ($legacy_keys && $comments) {
$comments = array_values(array_filter($comments, static function ($c) use ($legacy_keys) {
$body = (string) $c->comment_content;
foreach ($legacy_keys as $k) {
if ($k !== '' && str_contains($body, $k)) {
return false;
}
return true;
}));
}
}
return true;
}));
}
$comments = array_slice($comments ?: [], 0, $count);
if (!$comments) {
return '<p class="widget-empty ajax-item">' . esc_html__('暂无评论', 'aksrt') . '</p>';
return function_exists('aksrt_get_ajax_null')
? aksrt_get_ajax_null(__('暂无评论', 'aksrt'))
: '<p class="widget-empty ajax-item">' . esc_html__('暂无评论', 'aksrt') . '</p>';
}
ob_start();
echo '<div class="widget-recent-comments comment-mini-lists ajax-item">';
// zibll: .box-body.comment-mini-lists.zib-widget + .posts-mini rows.
echo '<div class="box-body comment-mini-lists zib-widget ajax-item widget-recent-comments">';
foreach ($comments as $comment) {
$post = get_post($comment->comment_post_ID);
if (!$post) {
continue;
$cid = (int) $comment->comment_ID;
$user_id = (int) $comment->user_id;
$link = get_comment_link($comment);
$time = human_time_diff(strtotime((string) $comment->comment_date), current_time('timestamp'));
// Content: strip imgs/code like zibll noimg filter.
$cont = get_comment_text($cid);
$cont = convert_smilies($cont);
$cont = preg_replace('/\<img(.*?)\>/i', '[' . __('图片', 'aksrt') . ']', $cont);
$cont = preg_replace('/\[code]([\s\S]*?)\[\/code]/i', '[' . __('代码', 'aksrt') . ']', (string) $cont);
$cont = wp_strip_all_tags((string) $cont);
$cont = wp_trim_words($cont, 40, '…');
// Name with identity.
if ($user_id > 0 && function_exists('aksrt_get_user_name')) {
$user_name = aksrt_get_user_name([
'id' => $user_id,
'class' => 'inflex ac relative-h',
'follow' => false,
'medal' => true,
'level' => true,
'auth' => true,
]);
} elseif ($user_id > 0) {
$home = function_exists('aksrt_get_user_home_url') ? aksrt_get_user_home_url($user_id) : get_author_posts_url($user_id);
$user_name = '<a href="' . esc_url($home) . '">' . esc_html(get_comment_author($comment)) . '</a>';
} else {
$user_name = esc_html(get_comment_author($comment));
}
echo '<div class="widget-recent-comments__item">';
$avatar = '';
if ($show_avatar) {
echo '<div class="widget-recent-comments__avatar">' . get_avatar($comment, 32) . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
$avatar_url = $user_id > 0 && function_exists('aksrt_get_avatar_url')
? aksrt_get_avatar_url($user_id, ['size' => 42])
: get_avatar_url($comment, ['size' => 42]);
$avatar = '<div class="avatar-img"><img class="avatar" src="' . esc_url($avatar_url) . '" alt="" width="36" height="36" loading="lazy" /></div>';
}
echo '<div class="widget-recent-comments__body">';
echo '<div class="widget-recent-comments__meta">';
echo '<strong class="widget-recent-comments__author">' . esc_html($comment->comment_author) . '</strong>';
echo '<span class="widget-recent-comments__time">' . esc_html(human_time_diff(strtotime($comment->comment_date), current_time('timestamp'))) . '</span>';
echo '</div>';
echo '<p class="widget-recent-comments__text"><a href="' . esc_url(get_comment_link($comment)) . '">' . esc_html(wp_trim_words($comment->comment_content, 15, '...')) . '</a></p>';
echo '<div class="widget-recent-comments__post muted-2-color em09"><a href="' . esc_url(get_permalink($post)) . '">' . esc_html(get_the_title($post)) . '</a></div>';
$like = function_exists('aksrt_get_comment_like_html')
? aksrt_get_comment_like_html($comment, true)
: '';
echo '<div class="posts-mini">';
echo $avatar; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '<div class="posts-mini-con em09 ml10 flex xx jsb">';
echo '<p class="flex jsb">';
echo '<span class="flex1 flex">';
echo $user_name; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '<span class="flex0 icon-spot muted-3-color" title="' . esc_attr((string) $comment->comment_date) . '">' . esc_html($time) . '</span>';
echo '</span>';
if ($like !== '') {
echo '<span class="ml10 flex0">' . $like . '</span>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
echo '</p>';
echo '<a class="muted-color text-ellipsis-5" href="' . esc_url($link) . '">' . esc_html($cont) . '</a>';
echo '</div></div>';
}
echo '</div>';
return (string) ob_get_clean();
}
/**
* @param array<string,mixed> $args
* @param array<string,mixed> $instance
*/
public function widget($args, $instance): void {
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
$title = apply_filters('widget_title', (string) ($instance['title'] ?? __('最新评论', 'aksrt')), $instance, $this->id_base);
@@ -85,6 +169,9 @@ class RecentCommentsWidget extends \WP_Widget {
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* @param array<string,mixed> $instance
*/
public function form($instance): void {
?>
<p>
@@ -101,24 +188,30 @@ class RecentCommentsWidget extends \WP_Widget {
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_avatar')); ?>" value="1" <?php checked(!isset($instance['show_avatar']) || !empty($instance['show_avatar'])); ?>> <?php esc_html_e('显示头像', 'aksrt'); ?></label>
</p>
<p>
<label><?php esc_html_e('排除文章 ID', 'aksrt'); ?>
<input class="tiny-text" type="number" min="0" name="<?php echo esc_attr($this->get_field_name('outpost')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['outpost'] ?? 0))); ?>">
<label><?php esc_html_e('排除文章 ID(逗号)', 'aksrt'); ?>
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('outpost')); ?>" value="<?php echo esc_attr((string) ($instance['outpost'] ?? '')); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('排除关键词(逗号)', 'aksrt'); ?>
<label><?php esc_html_e('排除作者 ID(逗号)', 'aksrt'); ?>
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('outer')); ?>" value="<?php echo esc_attr((string) ($instance['outer'] ?? '')); ?>">
</label>
<small><?php esc_html_e('填数字 ID 时按作者排除;填文字时按内容关键词排除(兼容旧配置)。', 'aksrt'); ?></small>
</p>
<?php
}
/**
* @param array<string,mixed> $new_instance
* @param array<string,mixed> $old_instance
* @return array<string,mixed>
*/
public function update($new_instance, $old_instance): array {
return [
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
'count' => max(1, min(20, (int) ($new_instance['count'] ?? 5))),
'show_avatar' => !empty($new_instance['show_avatar']) ? 1 : 0,
'outpost' => max(0, (int) ($new_instance['outpost'] ?? 0)),
'outpost' => sanitize_text_field((string) ($new_instance['outpost'] ?? '')),
'outer' => sanitize_text_field((string) ($new_instance['outer'] ?? '')),
];
}