esc_html__('显示最新评论列表', 'aksrt'), ]); } 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'] ?? '')); $query = [ 'number' => $count, 'status' => 'approve', 'post_status' => 'publish', ]; if ($outpost > 0) { $query['post__not_in'] = [$outpost]; } $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; } } return true; })); } } if (!$comments) { return '

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

'; } ob_start(); echo '
'; foreach ($comments as $comment) { $post = get_post($comment->comment_post_ID); if (!$post) { continue; } echo '
'; if ($show_avatar) { echo '
' . get_avatar($comment, 32) . '
'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } 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)) . '
'; echo '
'; } echo '
'; return (string) ob_get_clean(); } 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); if ($title !== '') { echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } $rest = rest_url('aksrt/v1/widgets/recent-comments'); echo '
'; echo self::render_html($instance); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo '
'; echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } public function form($instance): void { ?>

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)), 'outer' => sanitize_text_field((string) ($new_instance['outer'] ?? '')), ]; } }