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 '
'; - 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 '
'; - } else { - echo '

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

'; + 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 + } + 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')) + : '

' . esc_html__('暂无文章', '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 '
'; + ob_start(); + echo '
'; while ($q->have_posts()) { $q->the_post(); $thumb = get_the_post_thumbnail_url(get_the_ID(), $style === 'card' ? 'medium' : 'thumbnail'); @@ -94,7 +92,20 @@ class WidgetOnelinePosts extends \WP_Widget { } echo '
'; wp_reset_postdata(); + return (string) ob_get_clean(); + } + public function widget($args, $instance): void { + $html = self::render_html($instance); + if ($html === '') { + return; + } + 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 $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } diff --git a/src/AKSRT/Widgets/WidgetUserLists.php b/src/AKSRT/Widgets/WidgetUserLists.php index 52012158..8da26633 100644 --- a/src/AKSRT/Widgets/WidgetUserLists.php +++ b/src/AKSRT/Widgets/WidgetUserLists.php @@ -1,6 +1,6 @@ */ + public static function parse_ids_static(string $raw): array { + if ($raw === '') { + return []; + } + $ids = []; + foreach (preg_split('/[\s,]+/', $raw) ?: [] as $p) { + $id = (int) $p; + if ($id > 0) { + $ids[] = $id; + } + } + return array_values(array_unique($ids)); + } + + public static function render_html(array $instance): string { $number = max(1, min(50, (int) ($instance['number'] ?? 8))); $orderby = sanitize_key((string) ($instance['orderby'] ?? 'user_registered')); $order = strtoupper((string) ($instance['order'] ?? 'DESC')) === 'ASC' ? 'ASC' : 'DESC'; - $include = $this->parse_ids((string) ($instance['include'] ?? '')); - $exclude = $this->parse_ids((string) ($instance['exclude'] ?? '')); + $include = self::parse_ids_static((string) ($instance['include'] ?? '')); + $exclude = self::parse_ids_static((string) ($instance['exclude'] ?? '')); $hide_box = !empty($instance['hide_box']); $query = [ - 'number' => $number, - 'order' => $order, - 'fields' => ['ID', 'display_name', 'user_nicename'], + 'number' => $number, + 'order' => $order, + 'fields' => ['ID', 'display_name', 'user_nicename'], ]; - if ($include) { $query['include'] = $include; $query['orderby'] = 'include'; @@ -52,7 +66,6 @@ class WidgetUserLists extends \WP_Widget { $query['orderby'] = 'meta_value'; break; case 'followed-user-count': - // Fallback: registered date; fan ranking needs denormalized meta when available. $query['meta_key'] = 'aksrt_follower_count'; $query['orderby'] = 'meta_value_num'; break; @@ -66,11 +79,12 @@ class WidgetUserLists extends \WP_Widget { $users = get_users($query); if (!$users) { - return; + return function_exists('aksrt_get_ajax_null') + ? aksrt_get_ajax_null(__('暂无用户', 'aksrt')) + : '

' . esc_html__('暂无用户', 'aksrt') . '

'; } - // If follower meta empty for all, re-query by registered when fan order requested. - if ($orderby === 'followed-user-count' && !$include) { + if ($orderby === 'followed-user-count' && !$include && class_exists('\AKSRT\User\Follow')) { $has_meta = false; foreach ($users as $u) { if ((int) get_user_meta($u->ID, 'aksrt_follower_count', true) > 0) { @@ -78,8 +92,7 @@ class WidgetUserLists extends \WP_Widget { break; } } - if (!$has_meta && class_exists('\AKSRT\User\Follow')) { - // Rank by live follower counts (small N only). + if (!$has_meta) { usort($users, static function ($a, $b) { $fa = \AKSRT\User\Follow::get_instance()->get_follower_count((int) $a->ID); $fb = \AKSRT\User\Follow::get_instance()->get_follower_count((int) $b->ID); @@ -88,13 +101,8 @@ class WidgetUserLists extends \WP_Widget { } } - 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 - } - - $wrap = $hide_box ? 'aksrt-user-lists is-bare' : 'aksrt-user-lists'; + ob_start(); + $wrap = $hide_box ? 'aksrt-user-lists is-bare ajax-item' : 'aksrt-user-lists ajax-item'; echo '
'; foreach ($users as $user) { $uid = (int) $user->ID; @@ -111,26 +119,22 @@ class WidgetUserLists extends \WP_Widget { echo ''; } echo '
'; + return (string) ob_get_clean(); + } + public function widget($args, $instance): void { + $html = self::render_html($instance); + 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 $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } - /** - * @return array - */ private function parse_ids(string $raw): array { - if ($raw === '') { - return []; - } - $parts = preg_split('/[\s,]+/', $raw) ?: []; - $ids = []; - foreach ($parts as $p) { - $id = (int) $p; - if ($id > 0) { - $ids[] = $id; - } - } - return array_values(array_unique($ids)); + return self::parse_ids_static($raw); } public function form($instance): void { @@ -187,13 +191,13 @@ class WidgetUserLists extends \WP_Widget { $orderby = 'user_registered'; } return [ - 'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')), - 'number' => max(1, min(50, (int) ($new_instance['number'] ?? 8))), - 'include' => sanitize_text_field((string) ($new_instance['include'] ?? '')), - 'exclude' => sanitize_text_field((string) ($new_instance['exclude'] ?? '')), - 'orderby' => $orderby, - 'order' => strtoupper((string) ($new_instance['order'] ?? 'DESC')) === 'ASC' ? 'ASC' : 'DESC', - 'hide_box'=> !empty($new_instance['hide_box']) ? 1 : 0, + 'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')), + 'number' => max(1, min(50, (int) ($new_instance['number'] ?? 8))), + 'include' => sanitize_text_field((string) ($new_instance['include'] ?? '')), + 'exclude' => sanitize_text_field((string) ($new_instance['exclude'] ?? '')), + 'orderby' => $orderby, + 'order' => strtoupper((string) ($new_instance['order'] ?? 'DESC')) === 'ASC' ? 'ASC' : 'DESC', + 'hide_box' => !empty($new_instance['hide_box']) ? 1 : 0, ]; } }