feat(widgets): category tabs lazy load + ranking extras
SSR first category panel; remaining tabs fetch grid HTML via /widgets/category-tabs. User ranking adds zibll-like desc modes.
This commit is contained in:
1 parent
d508a9aef8
commit
90d84af2f5
3 files changed
+175
-21
No files matched your search
@@ -23,7 +23,8 @@ class WidgetUserRanking extends \WP_Widget {
|
||||
$count = max(1, min(20, (int) ($instance['count'] ?? 5)));
|
||||
$show_avatar = !empty($instance['show_avatar']);
|
||||
|
||||
$users = $this->get_ranking($rank_by, $count);
|
||||
$desc_mode = sanitize_key((string) ($instance['desc'] ?? 'desc'));
|
||||
$users = $this->get_ranking($rank_by, $count, $desc_mode);
|
||||
if (empty($users)) return;
|
||||
|
||||
echo $args['before_widget'];
|
||||
@@ -60,8 +61,13 @@ class WidgetUserRanking extends \WP_Widget {
|
||||
echo '</a>';
|
||||
}
|
||||
|
||||
// 昵称
|
||||
// 昵称 + 可选签名/附加信息
|
||||
echo '<div class="aksrt-user-ranking__meta">';
|
||||
echo '<a href="' . esc_url($user_url) . '" class="aksrt-user-ranking__name">' . esc_html($user['name']) . '</a>';
|
||||
if (!empty($user['desc'])) {
|
||||
echo '<div class="aksrt-user-ranking__desc em09 muted-2-color">' . esc_html((string) $user['desc']) . '</div>';
|
||||
}
|
||||
echo '</div>';
|
||||
|
||||
// 数值
|
||||
echo '<span class="aksrt-user-ranking__value">' . esc_html($user['value']) . '</span>';
|
||||
@@ -73,7 +79,7 @@ class WidgetUserRanking extends \WP_Widget {
|
||||
echo $args['after_widget'];
|
||||
}
|
||||
|
||||
private function get_ranking(string $rank_by, int $count): array {
|
||||
private function get_ranking(string $rank_by, int $count, string $desc_mode = 'desc'): array {
|
||||
$users = [];
|
||||
|
||||
switch ($rank_by) {
|
||||
@@ -93,6 +99,7 @@ class WidgetUserRanking extends \WP_Widget {
|
||||
'id' => (int) $row['user_id'],
|
||||
'name' => $user->display_name,
|
||||
'value' => number_format_i18n((int) $row['credits']) . ' 积分',
|
||||
'desc' => $this->user_extra_desc((int) $row['user_id'], $user, $desc_mode),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -112,6 +119,7 @@ class WidgetUserRanking extends \WP_Widget {
|
||||
'id' => $user->ID,
|
||||
'name' => $user->display_name,
|
||||
'value' => number_format_i18n($post_count) . ' 篇',
|
||||
'desc' => $this->user_extra_desc($user->ID, $user, $desc_mode),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -137,6 +145,7 @@ class WidgetUserRanking extends \WP_Widget {
|
||||
'id' => $uid,
|
||||
'name' => $user->display_name,
|
||||
'value' => number_format_i18n((int) $row['val']) . ' 天',
|
||||
'desc' => $this->user_extra_desc($uid, $user, $desc_mode),
|
||||
];
|
||||
}
|
||||
if (count($users) >= $count) break;
|
||||
@@ -163,6 +172,7 @@ class WidgetUserRanking extends \WP_Widget {
|
||||
'id' => $uid,
|
||||
'name' => $user->display_name,
|
||||
'value' => number_format_i18n((int) $row['val']) . ' 天',
|
||||
'desc' => $this->user_extra_desc($uid, $user, $desc_mode),
|
||||
];
|
||||
}
|
||||
if (count($users) >= $count) break;
|
||||
@@ -189,6 +199,7 @@ class WidgetUserRanking extends \WP_Widget {
|
||||
'id' => (int) $row['following_id'],
|
||||
'name' => $user->display_name,
|
||||
'value' => number_format_i18n((int) $row['cnt']) . ' 粉丝',
|
||||
'desc' => $this->user_extra_desc((int) $row['following_id'], $user, $desc_mode),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -200,6 +211,37 @@ class WidgetUserRanking extends \WP_Widget {
|
||||
return $users;
|
||||
}
|
||||
|
||||
|
||||
private function user_extra_desc(int $user_id, $user, string $mode): string {
|
||||
switch ($mode) {
|
||||
case 'user_registered':
|
||||
if ($user && !empty($user->user_registered)) {
|
||||
$days = max(0, (int) floor((time() - strtotime($user->user_registered)) / DAY_IN_SECONDS));
|
||||
return sprintf(__('加入 %d 天', 'aksrt'), $days);
|
||||
}
|
||||
return '';
|
||||
case 'last_login':
|
||||
$t = (string) get_user_meta($user_id, 'aksrt_last_login', true);
|
||||
if ($t === '') {
|
||||
$t = (string) get_user_meta($user_id, 'last_login', true);
|
||||
}
|
||||
return $t !== '' ? sprintf(__('最近登录 %s', 'aksrt'), $t) : '';
|
||||
case 'last_checkin':
|
||||
$t = (string) get_user_meta($user_id, 'aksrt_last_checkin', true);
|
||||
if ($t === '') {
|
||||
$t = (string) get_user_meta($user_id, 'last_checkin', true);
|
||||
}
|
||||
return $t !== '' ? sprintf(__('最近签到 %s', 'aksrt'), $t) : '';
|
||||
case 'desc':
|
||||
default:
|
||||
$bio = get_user_meta($user_id, 'description', true);
|
||||
if (!is_string($bio) || $bio === '') {
|
||||
$bio = $user ? (string) $user->description : '';
|
||||
}
|
||||
return $bio !== '' ? wp_trim_words($bio, 12) : '';
|
||||
}
|
||||
}
|
||||
|
||||
public function form($instance): void {
|
||||
$title = !empty($instance['title']) ? $instance['title'] : __('排行榜', 'aksrt');
|
||||
$rank_by = !empty($instance['rank_by']) ? $instance['rank_by'] : 'credits';
|
||||
@@ -230,6 +272,15 @@ class WidgetUserRanking extends \WP_Widget {
|
||||
<?php esc_html_e('显示头像', 'aksrt'); ?>
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('desc')); ?>"><?php esc_html_e('附加显示:', 'aksrt'); ?></label>
|
||||
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('desc')); ?>" name="<?php echo esc_attr($this->get_field_name('desc')); ?>">
|
||||
<option value="desc" <?php selected(($instance['desc'] ?? 'desc'), 'desc'); ?>><?php esc_html_e('用户签名', 'aksrt'); ?></option>
|
||||
<option value="user_registered" <?php selected(($instance['desc'] ?? ''), 'user_registered'); ?>><?php esc_html_e('加入天数', 'aksrt'); ?></option>
|
||||
<option value="last_login" <?php selected(($instance['desc'] ?? ''), 'last_login'); ?>><?php esc_html_e('最后登录', 'aksrt'); ?></option>
|
||||
<option value="last_checkin" <?php selected(($instance['desc'] ?? ''), 'last_checkin'); ?>><?php esc_html_e('最后签到', 'aksrt'); ?></option>
|
||||
</select>
|
||||
</p>
|
||||
<p>
|
||||
<label>
|
||||
<input type="checkbox" name="<?php echo esc_attr($this->get_field_name('checkin_btn')); ?>" value="1" <?php checked(!empty($instance['checkin_btn'])); ?>>
|
||||
@@ -246,6 +297,7 @@ class WidgetUserRanking extends \WP_Widget {
|
||||
$instance['count'] = max(1, min(20, (int) ($new_instance['count'] ?? 5)));
|
||||
$instance['show_avatar'] = !empty($new_instance['show_avatar']);
|
||||
$instance['checkin_btn'] = !empty($new_instance['checkin_btn']);
|
||||
$instance['desc'] = in_array($new_instance['desc'] ?? '', ['desc', 'user_registered', 'last_login', 'last_checkin'], true) ? $new_instance['desc'] : 'desc';
|
||||
return $instance;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user