__('按积分、文章数或粉丝数排行的用户列表。', 'aksrt')]
);
}
public function widget($args, $instance): void {
$title = !empty($instance['title']) ? $instance['title'] : __('排行榜', 'aksrt');
$rank_by = !empty($instance['rank_by']) ? $instance['rank_by'] : 'credits';
$count = max(1, min(20, (int) ($instance['count'] ?? 5)));
$show_avatar = !empty($instance['show_avatar']);
$users = $this->get_ranking($rank_by, $count);
if (empty($users)) return;
echo $args['before_widget'];
echo $args['before_title'] . esc_html($title) . $args['after_title'];
echo '
';
$rank = 0;
foreach ($users as $user) {
$rank++;
$rank_class = $rank <= 3 ? ' aksrt-user-ranking__item--top' : '';
$user_url = function_exists('aksrt_get_user_home_url')
? aksrt_get_user_home_url($user['id'])
: get_author_posts_url($user['id']);
echo '- ';
// 排名
echo '' . esc_html((string) $rank) . '';
// 头像
if ($show_avatar) {
$avatar_url = function_exists('aksrt_get_avatar_url')
? aksrt_get_avatar_url($user['id'], ['size' => 36])
: get_avatar_url($user['id'], ['size' => 36]);
echo '';
echo '
';
echo '';
}
// 昵称
echo '' . esc_html($user['name']) . '';
// 数值
echo '' . esc_html($user['value']) . '';
echo ' ';
}
echo '
';
echo $args['after_widget'];
}
private function get_ranking(string $rank_by, int $count): array {
$users = [];
switch ($rank_by) {
case 'credits':
global $wpdb;
$rows = $wpdb->get_results($wpdb->prepare(
"SELECT user_id, meta_value as credits FROM {$wpdb->usermeta}
WHERE meta_key = 'aksrt_credits'
ORDER BY CAST(meta_value AS UNSIGNED) DESC
LIMIT %d",
$count
), ARRAY_A);
foreach ((array) $rows as $row) {
$user = get_userdata((int) $row['user_id']);
if ($user) {
$users[] = [
'id' => (int) $row['user_id'],
'name' => $user->display_name,
'value' => number_format_i18n((int) $row['credits']) . ' 积分',
];
}
}
break;
case 'posts':
$blog_users = get_users([
'number' => $count,
'orderby' => 'post_count',
'order' => 'DESC',
'who' => 'authors',
]);
foreach ($blog_users as $user) {
$post_count = count_user_posts($user->ID, 'post', true);
if ($post_count > 0) {
$users[] = [
'id' => $user->ID,
'name' => $user->display_name,
'value' => number_format_i18n($post_count) . ' 篇',
];
}
}
break;
case 'followers':
if (class_exists('AKSRT_Follow')) {
global $wpdb;
$follow_table = AKSRT_DB_Migrator::table('follow');
if ($follow_table) {
$rows = $wpdb->get_results($wpdb->prepare(
"SELECT following_id, COUNT(*) as cnt
FROM {$follow_table}
GROUP BY following_id
ORDER BY cnt DESC
LIMIT %d",
$count
), ARRAY_A);
foreach ((array) $rows as $row) {
$user = get_userdata((int) $row['following_id']);
if ($user) {
$users[] = [
'id' => (int) $row['following_id'],
'name' => $user->display_name,
'value' => number_format_i18n((int) $row['cnt']) . ' 粉丝',
];
}
}
}
}
break;
}
return $users;
}
public function form($instance): void {
$title = !empty($instance['title']) ? $instance['title'] : __('排行榜', 'aksrt');
$rank_by = !empty($instance['rank_by']) ? $instance['rank_by'] : 'credits';
$count = !empty($instance['count']) ? (int) $instance['count'] : 5;
$show_avatar = !empty($instance['show_avatar']);
?>