User lists render author-minicard rows; term cards/aggregation consume aksrt_graphic_card with style-1..4, meta CTA, and orderby-aware post rows.
227 lines
8.9 KiB
PHP
227 lines
8.9 KiB
PHP
<?php
|
|
/**
|
|
* Registered user list (zibll widget_ui_user_lists → zib_author_card).
|
|
*
|
|
* @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));
|
|
}
|
|
|
|
/**
|
|
* @param array<string,mixed> $instance
|
|
*/
|
|
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']);
|
|
$blur_bg = !empty($instance['blur_bg']);
|
|
|
|
$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();
|
|
// zibll: optional zib-widget + text-center user_lists of author-minicard.
|
|
$outer = $hide_box ? 'aksrt-user-lists is-bare ajax-item' : 'aksrt-user-lists zib-widget ajax-item';
|
|
echo '<div class="' . esc_attr($outer) . '">';
|
|
echo '<div class="text-center user_lists">';
|
|
foreach ($users as $user) {
|
|
$uid = (int) $user->ID;
|
|
if (function_exists('aksrt_author_minicard')) {
|
|
echo aksrt_author_minicard($uid, 'mb10', $blur_bg); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
} else {
|
|
$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="author-minicard radius8" href="' . esc_url($home) . '">';
|
|
echo '<img class="avatar" src="' . esc_url($avatar) . '" alt="" loading="lazy" width="42" height="42" /> ';
|
|
echo esc_html($name);
|
|
echo '</a>';
|
|
}
|
|
}
|
|
echo '</div></div>';
|
|
return (string) ob_get_clean();
|
|
}
|
|
|
|
/**
|
|
* @param array<string,mixed> $args
|
|
* @param array<string,mixed> $instance
|
|
*/
|
|
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
|
|
}
|
|
$rest = rest_url('aksrt/v1/widgets/user-lists');
|
|
echo '<div class="ajaxpager" data-aksrt-ajaxpager data-aksrt-ajax-url="' . esc_url($rest) . '">';
|
|
echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
echo '</div>';
|
|
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
}
|
|
|
|
/**
|
|
* @param array<string,mixed> $instance
|
|
*/
|
|
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><br>
|
|
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('blur_bg')); ?>" value="1" <?php checked(!empty($instance['blur_bg'])); ?>> <?php esc_html_e('头像模糊背景', 'aksrt'); ?></label>
|
|
</p>
|
|
<?php
|
|
}
|
|
|
|
/**
|
|
* @param array<string,mixed> $new_instance
|
|
* @param array<string,mixed> $old_instance
|
|
* @return array<string,mixed>
|
|
*/
|
|
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,
|
|
'blur_bg' => !empty($new_instance['blur_bg']) ? 1 : 0,
|
|
];
|
|
}
|
|
}
|