From dff9d7dc1cc4e21a7b5e6357ffad1a23e6a0df62 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 21 Jul 2026 08:59:27 +0800 Subject: [PATCH] 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. --- src/AKSRT/Widgets/RecentCommentsWidget.php | 167 ++++++++++++++++----- 1 file changed, 130 insertions(+), 37 deletions(-) diff --git a/src/AKSRT/Widgets/RecentCommentsWidget.php b/src/AKSRT/Widgets/RecentCommentsWidget.php index 2a382d95..34a49134 100644 --- a/src/AKSRT/Widgets/RecentCommentsWidget.php +++ b/src/AKSRT/Widgets/RecentCommentsWidget.php @@ -1,6 +1,6 @@ esc_html__('显示最新评论列表', 'aksrt'), + 'description' => esc_html__('显示最新评论列表(posts-mini + 身份/点赞)。', 'aksrt'), ]); } + /** + * @param array $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 '

' . esc_html__('暂无评论', 'aksrt') . '

'; + return function_exists('aksrt_get_ajax_null') + ? aksrt_get_ajax_null(__('暂无评论', 'aksrt')) + : '

' . esc_html__('暂无评论', 'aksrt') . '

'; } + ob_start(); - echo '
'; + // zibll: .box-body.comment-mini-lists.zib-widget + .posts-mini rows. + echo '
'; 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('/\/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 = '' . esc_html(get_comment_author($comment)) . ''; + } else { + $user_name = esc_html(get_comment_author($comment)); } - echo '
'; + + $avatar = ''; if ($show_avatar) { - echo '
' . get_avatar($comment, 32) . '
'; // 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 = '
'; } - echo '
'; - echo '
'; - echo '' . esc_html($comment->comment_author) . ''; - echo '' . esc_html(human_time_diff(strtotime($comment->comment_date), current_time('timestamp'))) . ''; - echo '
'; - echo '

' . esc_html(wp_trim_words($comment->comment_content, 15, '...')) . '

'; - echo '
' . esc_html(get_the_title($post)) . '
'; + + $like = function_exists('aksrt_get_comment_like_html') + ? aksrt_get_comment_like_html($comment, true) + : ''; + + echo '
'; + echo $avatar; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + echo '
'; + echo '

'; + echo ''; + echo $user_name; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + echo '' . esc_html($time) . ''; + echo ''; + if ($like !== '') { + echo '' . $like . ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + } + echo '

'; + echo '' . esc_html($cont) . ''; echo '
'; } echo '
'; return (string) ob_get_clean(); } + /** + * @param array $args + * @param array $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 $instance + */ public function form($instance): void { ?>

@@ -101,24 +188,30 @@ class RecentCommentsWidget extends \WP_Widget {

-

-

$new_instance + * @param array $old_instance + * @return array + */ 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'] ?? '')), ]; }