Expose remote-refreshable HTML builders for zibll-style widget modules without full page reload.
204 lines
8.0 KiB
PHP
204 lines
8.0 KiB
PHP
<?php
|
|
/**
|
|
* Registered user list (zibll widget_ui_user_lists + remote HTML).
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
namespace AKSRT\Widgets;
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class WidgetUserLists extends \WP_Widget {
|
|
|
|
public function __construct() {
|
|
parent::__construct(
|
|
'aksrt_user_lists',
|
|
esc_html__('AKSRT 用户列表', 'aksrt'),
|
|
[
|
|
'description' => esc_html__('展示站点用户头像网格,支持排序与筛选。', 'aksrt'),
|
|
'classname' => 'widget_aksrt_user_lists',
|
|
]
|
|
);
|
|
}
|
|
|
|
/** @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)));
|
|
$orderby = sanitize_key((string) ($instance['orderby'] ?? 'user_registered'));
|
|
$order = strtoupper((string) ($instance['order'] ?? 'DESC')) === 'ASC' ? 'ASC' : 'DESC';
|
|
$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'],
|
|
];
|
|
if ($include) {
|
|
$query['include'] = $include;
|
|
$query['orderby'] = 'include';
|
|
} else {
|
|
switch ($orderby) {
|
|
case 'display_name':
|
|
$query['orderby'] = 'display_name';
|
|
break;
|
|
case 'post_count':
|
|
$query['orderby'] = 'post_count';
|
|
break;
|
|
case 'last_login':
|
|
$query['meta_key'] = 'aksrt_last_login';
|
|
$query['orderby'] = 'meta_value';
|
|
break;
|
|
case 'followed-user-count':
|
|
$query['meta_key'] = 'aksrt_follower_count';
|
|
$query['orderby'] = 'meta_value_num';
|
|
break;
|
|
default:
|
|
$query['orderby'] = 'user_registered';
|
|
}
|
|
}
|
|
if ($exclude) {
|
|
$query['exclude'] = $exclude;
|
|
}
|
|
|
|
$users = get_users($query);
|
|
if (!$users) {
|
|
return function_exists('aksrt_get_ajax_null')
|
|
? aksrt_get_ajax_null(__('暂无用户', 'aksrt'))
|
|
: '<p class="widget-empty ajax-item">' . esc_html__('暂无用户', 'aksrt') . '</p>';
|
|
}
|
|
|
|
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) {
|
|
$has_meta = true;
|
|
break;
|
|
}
|
|
}
|
|
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);
|
|
return $fb <=> $fa;
|
|
});
|
|
}
|
|
}
|
|
|
|
ob_start();
|
|
$wrap = $hide_box ? 'aksrt-user-lists is-bare ajax-item' : 'aksrt-user-lists ajax-item';
|
|
echo '<div class="' . esc_attr($wrap) . '">';
|
|
foreach ($users as $user) {
|
|
$uid = (int) $user->ID;
|
|
$avatar = function_exists('aksrt_get_avatar_url')
|
|
? aksrt_get_avatar_url($uid, ['size' => 96])
|
|
: get_avatar_url($uid, ['size' => 96]);
|
|
$home = function_exists('aksrt_get_user_home_url')
|
|
? aksrt_get_user_home_url($uid)
|
|
: get_author_posts_url($uid);
|
|
$name = $user->display_name ?: $user->user_nicename;
|
|
echo '<a class="aksrt-user-lists__item" href="' . esc_url($home) . '" title="' . esc_attr($name) . '">';
|
|
echo '<img class="aksrt-user-lists__avatar" src="' . esc_url($avatar) . '" alt="' . esc_attr($name) . '" loading="lazy" width="48" height="48" />';
|
|
echo '<span class="aksrt-user-lists__name">' . esc_html($name) . '</span>';
|
|
echo '</a>';
|
|
}
|
|
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
|
|
}
|
|
|
|
private function parse_ids(string $raw): array {
|
|
return self::parse_ids_static($raw);
|
|
}
|
|
|
|
public function form($instance): void {
|
|
?>
|
|
<p>
|
|
<label><?php esc_html_e('标题', 'aksrt'); ?>
|
|
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php echo esc_attr((string) ($instance['title'] ?? '')); ?>">
|
|
</label>
|
|
</p>
|
|
<p>
|
|
<label><?php esc_html_e('显示数量', 'aksrt'); ?>
|
|
<input class="tiny-text" type="number" min="1" max="50" name="<?php echo esc_attr($this->get_field_name('number')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['number'] ?? 8))); ?>">
|
|
</label>
|
|
</p>
|
|
<p>
|
|
<label><?php esc_html_e('包含用户 ID(逗号)', 'aksrt'); ?>
|
|
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('include')); ?>" value="<?php echo esc_attr((string) ($instance['include'] ?? '')); ?>">
|
|
</label>
|
|
</p>
|
|
<p>
|
|
<label><?php esc_html_e('排除用户 ID(逗号)', 'aksrt'); ?>
|
|
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('exclude')); ?>" value="<?php echo esc_attr((string) ($instance['exclude'] ?? '')); ?>">
|
|
</label>
|
|
</p>
|
|
<p>
|
|
<label><?php esc_html_e('排序', 'aksrt'); ?>
|
|
<select class="widefat" name="<?php echo esc_attr($this->get_field_name('orderby')); ?>">
|
|
<option value="user_registered" <?php selected(($instance['orderby'] ?? 'user_registered'), 'user_registered'); ?>><?php esc_html_e('注册时间', 'aksrt'); ?></option>
|
|
<option value="display_name" <?php selected(($instance['orderby'] ?? ''), 'display_name'); ?>><?php esc_html_e('昵称', 'aksrt'); ?></option>
|
|
<option value="post_count" <?php selected(($instance['orderby'] ?? ''), 'post_count'); ?>><?php esc_html_e('文章数', 'aksrt'); ?></option>
|
|
<option value="last_login" <?php selected(($instance['orderby'] ?? ''), 'last_login'); ?>><?php esc_html_e('最后登录', 'aksrt'); ?></option>
|
|
<option value="followed-user-count" <?php selected(($instance['orderby'] ?? ''), 'followed-user-count'); ?>><?php esc_html_e('粉丝数', 'aksrt'); ?></option>
|
|
</select>
|
|
</label>
|
|
</p>
|
|
<p>
|
|
<label><?php esc_html_e('顺序', 'aksrt'); ?>
|
|
<select name="<?php echo esc_attr($this->get_field_name('order')); ?>">
|
|
<option value="DESC" <?php selected(($instance['order'] ?? 'DESC'), 'DESC'); ?>><?php esc_html_e('降序', 'aksrt'); ?></option>
|
|
<option value="ASC" <?php selected(($instance['order'] ?? ''), 'ASC'); ?>><?php esc_html_e('升序', 'aksrt'); ?></option>
|
|
</select>
|
|
</label>
|
|
</p>
|
|
<p>
|
|
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('hide_box')); ?>" value="1" <?php checked(!empty($instance['hide_box'])); ?>> <?php esc_html_e('不显示背景盒子', 'aksrt'); ?></label>
|
|
</p>
|
|
<?php
|
|
}
|
|
|
|
public function update($new_instance, $old_instance): array {
|
|
$orderby = sanitize_key((string) ($new_instance['orderby'] ?? 'user_registered'));
|
|
$allowed = ['user_registered', 'display_name', 'post_count', 'last_login', 'followed-user-count'];
|
|
if (!in_array($orderby, $allowed, true)) {
|
|
$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,
|
|
];
|
|
}
|
|
}
|