diff --git a/src/AKSRT/Widgets/RecentCommentsWidget.php b/src/AKSRT/Widgets/RecentCommentsWidget.php index 5820e1aa..bfaa45d7 100644 --- a/src/AKSRT/Widgets/RecentCommentsWidget.php +++ b/src/AKSRT/Widgets/RecentCommentsWidget.php @@ -1,6 +1,6 @@ id_base); + 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'] ?? '')); - - if ($title !== '') { - echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - } - $query = [ 'number' => $count, 'status' => 'approve', @@ -37,10 +30,6 @@ class RecentCommentsWidget extends \WP_Widget { if ($outpost > 0) { $query['post__not_in'] = [$outpost]; } - if ($outer !== '') { - // Exclude comments containing outer keywords (comma separated). - // Implemented post-filter below. - } $comments = get_comments($query); if ($outer !== '' && $comments) { $keys = array_filter(array_map('trim', preg_split('/[,,]/', $outer) ?: [])); @@ -56,32 +45,40 @@ class RecentCommentsWidget extends \WP_Widget { })); } } - - if ($comments) { - echo '
'; + if (!$comments) { + return ''; } + ob_start(); + 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 + } + echo self::render_html($instance); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } @@ -97,6 +94,9 @@ class RecentCommentsWidget extends \WP_Widget { ++ +
-
- -
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'] ?? '')), - 'show_avatar' => !empty($new_instance['show_avatar']) ? 1 : 0, ]; } } diff --git a/src/AKSRT/Widgets/WidgetOnelinePosts.php b/src/AKSRT/Widgets/WidgetOnelinePosts.php index 98a445df..5051c58e 100644 --- a/src/AKSRT/Widgets/WidgetOnelinePosts.php +++ b/src/AKSRT/Widgets/WidgetOnelinePosts.php @@ -1,6 +1,6 @@ 'post', 'post_status' => 'publish', @@ -62,20 +57,23 @@ class WidgetOnelinePosts extends \WP_Widget { $query['orderby'] = 'date'; $query['order'] = 'DESC'; } + return $query; + } - $q = new \WP_Query($query); + public static function render_html(array $instance): string { + $style = sanitize_key((string) ($instance['style'] ?? 'card')); + if (!in_array($style, ['card', 'mini', 'list'], true)) { + $style = 'card'; + } + $q = new \WP_Query(self::build_query_args($instance)); if (!$q->have_posts()) { wp_reset_postdata(); - return; + return function_exists('aksrt_get_ajax_null') + ? aksrt_get_ajax_null(__('暂无文章', 'aksrt')) + : ''; } - - echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - $title = apply_filters('widget_title', (string) ($instance['title'] ?? ''), $instance, $this->id_base); - if ($title !== '') { - echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - } - - echo '
' . esc_html(wp_trim_words($comment->comment_content, 15, '...')) . '
'; - echo '