feat(widgets): shared render_html for oneline/comments/users

Expose remote-refreshable HTML builders for zibll-style widget
modules without full page reload.
This commit is contained in:
kate522 committed 2026-07-20 21:58:33 +08:00
1 parent 75e046ee7f
commit 6b8d7a9d0a
3 files changed
+113 -101

No files matched your search

+38 -41
View File
@@ -1,6 +1,6 @@
<?php <?php
/** /**
* Recent comments mini list (zibll widget_ui_new_comment lite). * Recent comments mini list (zibll widget_ui_new_comment + remote HTML).
* *
* @package AKSRT * @package AKSRT
*/ */
@@ -17,18 +17,11 @@ class RecentCommentsWidget extends \WP_Widget {
]); ]);
} }
public function widget($args, $instance): void { public static function render_html(array $instance): string {
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
$title = apply_filters('widget_title', (string) ($instance['title'] ?? __('最新评论', 'aksrt')), $instance, $this->id_base);
$count = max(1, min(20, (int) ($instance['count'] ?? $instance['limit'] ?? 5))); $count = max(1, min(20, (int) ($instance['count'] ?? $instance['limit'] ?? 5)));
$show_avatar = !isset($instance['show_avatar']) || !empty($instance['show_avatar']); $show_avatar = !isset($instance['show_avatar']) || !empty($instance['show_avatar']);
$outpost = max(0, (int) ($instance['outpost'] ?? 0)); $outpost = max(0, (int) ($instance['outpost'] ?? 0));
$outer = sanitize_text_field((string) ($instance['outer'] ?? '')); $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 = [ $query = [
'number' => $count, 'number' => $count,
'status' => 'approve', 'status' => 'approve',
@@ -37,10 +30,6 @@ class RecentCommentsWidget extends \WP_Widget {
if ($outpost > 0) { if ($outpost > 0) {
$query['post__not_in'] = [$outpost]; $query['post__not_in'] = [$outpost];
} }
if ($outer !== '') {
// Exclude comments containing outer keywords (comma separated).
// Implemented post-filter below.
}
$comments = get_comments($query); $comments = get_comments($query);
if ($outer !== '' && $comments) { if ($outer !== '' && $comments) {
$keys = array_filter(array_map('trim', preg_split('/[,,]/', $outer) ?: [])); $keys = array_filter(array_map('trim', preg_split('/[,,]/', $outer) ?: []));
@@ -56,32 +45,40 @@ class RecentCommentsWidget extends \WP_Widget {
})); }));
} }
} }
if (!$comments) {
if ($comments) { return '<p class="widget-empty ajax-item">' . esc_html__('暂无评论', 'aksrt') . '</p>';
echo '<div class="widget-recent-comments comment-mini-lists">';
foreach ($comments as $comment) {
$post = get_post($comment->comment_post_ID);
if (!$post) {
continue;
}
echo '<div class="widget-recent-comments__item">';
if ($show_avatar) {
echo '<div class="widget-recent-comments__avatar">' . get_avatar($comment, 32) . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
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>';
echo '</div></div>';
}
echo '</div>';
} else {
echo '<p class="widget-empty">' . esc_html__('暂无评论', 'aksrt') . '</p>';
} }
ob_start();
echo '<div class="widget-recent-comments comment-mini-lists ajax-item">';
foreach ($comments as $comment) {
$post = get_post($comment->comment_post_ID);
if (!$post) {
continue;
}
echo '<div class="widget-recent-comments__item">';
if ($show_avatar) {
echo '<div class="widget-recent-comments__avatar">' . get_avatar($comment, 32) . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
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>';
echo '</div></div>';
}
echo '</div>';
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 echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
} }
@@ -97,6 +94,9 @@ class RecentCommentsWidget extends \WP_Widget {
<input class="tiny-text" type="number" min="1" max="20" name="<?php echo esc_attr($this->get_field_name('count')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['count'] ?? 5))); ?>"> <input class="tiny-text" type="number" min="1" max="20" name="<?php echo esc_attr($this->get_field_name('count')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['count'] ?? 5))); ?>">
</label> </label>
</p> </p>
<p>
<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> <p>
<label><?php esc_html_e('排除文章 ID', 'aksrt'); ?> <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))); ?>"> <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))); ?>">
@@ -107,9 +107,6 @@ class RecentCommentsWidget extends \WP_Widget {
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('outer')); ?>" value="<?php echo esc_attr((string) ($instance['outer'] ?? '')); ?>"> <input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('outer')); ?>" value="<?php echo esc_attr((string) ($instance['outer'] ?? '')); ?>">
</label> </label>
</p> </p>
<p>
<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>
<?php <?php
} }
@@ -117,9 +114,9 @@ class RecentCommentsWidget extends \WP_Widget {
return [ return [
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')), 'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
'count' => max(1, min(20, (int) ($new_instance['count'] ?? 5))), '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' => max(0, (int) ($new_instance['outpost'] ?? 0)),
'outer' => sanitize_text_field((string) ($new_instance['outer'] ?? '')), 'outer' => sanitize_text_field((string) ($new_instance['outer'] ?? '')),
'show_avatar' => !empty($new_instance['show_avatar']) ? 1 : 0,
]; ];
} }
} }
+28 -17
View File
@@ -1,6 +1,6 @@
<?php <?php
/** /**
* Horizontal one-line posts scroller (zibll widget_ui_oneline_posts lite). * Horizontal one-line posts scroller (zibll oneline + remote HTML).
* *
* @package AKSRT * @package AKSRT
*/ */
@@ -22,16 +22,11 @@ class WidgetOnelinePosts extends \WP_Widget {
); );
} }
public function widget($args, $instance): void { public static function build_query_args(array $instance): array {
$count = max(1, min(20, (int) ($instance['count'] ?? 8))); $count = max(1, min(20, (int) ($instance['count'] ?? 8)));
$style = sanitize_key((string) ($instance['style'] ?? 'card'));
if (!in_array($style, ['card', 'mini', 'list'], true)) {
$style = 'card';
}
$orderby = sanitize_key((string) ($instance['orderby'] ?? 'date')); $orderby = sanitize_key((string) ($instance['orderby'] ?? 'date'));
$cat = (int) ($instance['cat'] ?? 0); $cat = (int) ($instance['cat'] ?? 0);
$limit_day = max(0, (int) ($instance['limit_day'] ?? 0)); $limit_day = max(0, (int) ($instance['limit_day'] ?? 0));
$query = [ $query = [
'post_type' => 'post', 'post_type' => 'post',
'post_status' => 'publish', 'post_status' => 'publish',
@@ -62,20 +57,23 @@ class WidgetOnelinePosts extends \WP_Widget {
$query['orderby'] = 'date'; $query['orderby'] = 'date';
$query['order'] = 'DESC'; $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()) { if (!$q->have_posts()) {
wp_reset_postdata(); wp_reset_postdata();
return; 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 $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo '<div class="aksrt-oneline style-' . esc_attr($style) . ' ajax-item"><div class="aksrt-oneline__track">';
$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 '<div class="aksrt-oneline style-' . esc_attr($style) . '"><div class="aksrt-oneline__track">';
while ($q->have_posts()) { while ($q->have_posts()) {
$q->the_post(); $q->the_post();
$thumb = get_the_post_thumbnail_url(get_the_ID(), $style === 'card' ? 'medium' : 'thumbnail'); $thumb = get_the_post_thumbnail_url(get_the_ID(), $style === 'card' ? 'medium' : 'thumbnail');
@@ -94,7 +92,20 @@ class WidgetOnelinePosts extends \WP_Widget {
} }
echo '</div></div>'; echo '</div></div>';
wp_reset_postdata(); 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 echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
} }
+47 -43
View File
@@ -1,6 +1,6 @@
<?php <?php
/** /**
* Registered user list (zibll widget_ui_user_lists). * Registered user list (zibll widget_ui_user_lists + remote HTML).
* *
* @package AKSRT * @package AKSRT
*/ */
@@ -22,20 +22,34 @@ class WidgetUserLists extends \WP_Widget {
); );
} }
public function widget($args, $instance): void { /** @return array<int,int> */
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))); $number = max(1, min(50, (int) ($instance['number'] ?? 8)));
$orderby = sanitize_key((string) ($instance['orderby'] ?? 'user_registered')); $orderby = sanitize_key((string) ($instance['orderby'] ?? 'user_registered'));
$order = strtoupper((string) ($instance['order'] ?? 'DESC')) === 'ASC' ? 'ASC' : 'DESC'; $order = strtoupper((string) ($instance['order'] ?? 'DESC')) === 'ASC' ? 'ASC' : 'DESC';
$include = $this->parse_ids((string) ($instance['include'] ?? '')); $include = self::parse_ids_static((string) ($instance['include'] ?? ''));
$exclude = $this->parse_ids((string) ($instance['exclude'] ?? '')); $exclude = self::parse_ids_static((string) ($instance['exclude'] ?? ''));
$hide_box = !empty($instance['hide_box']); $hide_box = !empty($instance['hide_box']);
$query = [ $query = [
'number' => $number, 'number' => $number,
'order' => $order, 'order' => $order,
'fields' => ['ID', 'display_name', 'user_nicename'], 'fields' => ['ID', 'display_name', 'user_nicename'],
]; ];
if ($include) { if ($include) {
$query['include'] = $include; $query['include'] = $include;
$query['orderby'] = 'include'; $query['orderby'] = 'include';
@@ -52,7 +66,6 @@ class WidgetUserLists extends \WP_Widget {
$query['orderby'] = 'meta_value'; $query['orderby'] = 'meta_value';
break; break;
case 'followed-user-count': case 'followed-user-count':
// Fallback: registered date; fan ranking needs denormalized meta when available.
$query['meta_key'] = 'aksrt_follower_count'; $query['meta_key'] = 'aksrt_follower_count';
$query['orderby'] = 'meta_value_num'; $query['orderby'] = 'meta_value_num';
break; break;
@@ -66,11 +79,12 @@ class WidgetUserLists extends \WP_Widget {
$users = get_users($query); $users = get_users($query);
if (!$users) { if (!$users) {
return; return function_exists('aksrt_get_ajax_null')
? aksrt_get_ajax_null(__('暂无用户', 'aksrt'))
: '<p class="widget-empty ajax-item">' . esc_html__('暂无用户', 'aksrt') . '</p>';
} }
// If follower meta empty for all, re-query by registered when fan order requested. if ($orderby === 'followed-user-count' && !$include && class_exists('\AKSRT\User\Follow')) {
if ($orderby === 'followed-user-count' && !$include) {
$has_meta = false; $has_meta = false;
foreach ($users as $u) { foreach ($users as $u) {
if ((int) get_user_meta($u->ID, 'aksrt_follower_count', true) > 0) { if ((int) get_user_meta($u->ID, 'aksrt_follower_count', true) > 0) {
@@ -78,8 +92,7 @@ class WidgetUserLists extends \WP_Widget {
break; break;
} }
} }
if (!$has_meta && class_exists('\AKSRT\User\Follow')) { if (!$has_meta) {
// Rank by live follower counts (small N only).
usort($users, static function ($a, $b) { usort($users, static function ($a, $b) {
$fa = \AKSRT\User\Follow::get_instance()->get_follower_count((int) $a->ID); $fa = \AKSRT\User\Follow::get_instance()->get_follower_count((int) $a->ID);
$fb = \AKSRT\User\Follow::get_instance()->get_follower_count((int) $b->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 ob_start();
$title = apply_filters('widget_title', (string) ($instance['title'] ?? ''), $instance, $this->id_base); $wrap = $hide_box ? 'aksrt-user-lists is-bare ajax-item' : 'aksrt-user-lists ajax-item';
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';
echo '<div class="' . esc_attr($wrap) . '">'; echo '<div class="' . esc_attr($wrap) . '">';
foreach ($users as $user) { foreach ($users as $user) {
$uid = (int) $user->ID; $uid = (int) $user->ID;
@@ -111,26 +119,22 @@ class WidgetUserLists extends \WP_Widget {
echo '</a>'; echo '</a>';
} }
echo '</div>'; echo '</div>';
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 echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
} }
/**
* @return array<int, int>
*/
private function parse_ids(string $raw): array { private function parse_ids(string $raw): array {
if ($raw === '') { return self::parse_ids_static($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));
} }
public function form($instance): void { public function form($instance): void {
@@ -187,13 +191,13 @@ class WidgetUserLists extends \WP_Widget {
$orderby = 'user_registered'; $orderby = 'user_registered';
} }
return [ return [
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')), 'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
'number' => max(1, min(50, (int) ($new_instance['number'] ?? 8))), 'number' => max(1, min(50, (int) ($new_instance['number'] ?? 8))),
'include' => sanitize_text_field((string) ($new_instance['include'] ?? '')), 'include' => sanitize_text_field((string) ($new_instance['include'] ?? '')),
'exclude' => sanitize_text_field((string) ($new_instance['exclude'] ?? '')), 'exclude' => sanitize_text_field((string) ($new_instance['exclude'] ?? '')),
'orderby' => $orderby, 'orderby' => $orderby,
'order' => strtoupper((string) ($new_instance['order'] ?? 'DESC')) === 'ASC' ? 'ASC' : 'DESC', 'order' => strtoupper((string) ($new_instance['order'] ?? 'DESC')) === 'ASC' ? 'ASC' : 'DESC',
'hide_box'=> !empty($new_instance['hide_box']) ? 1 : 0, 'hide_box' => !empty($new_instance['hide_box']) ? 1 : 0,
]; ];
} }
} }