feat(widgets): author/user card more-posts strip + ranking empty/TOP
Add shared aksrt-user-card helpers (mini list + swiper-scroll card), wire AuthorCard/UserCard show_posts options, ranking empty state and top_badge.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
/**
|
||||
* Post author card (zibll widget_ui_avatar).
|
||||
* Post author card (zibll widget_ui_avatar + zib_get_user_card_box depth).
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
@@ -16,7 +16,7 @@ class WidgetAuthorCard extends \WP_Widget {
|
||||
'aksrt_author_card',
|
||||
esc_html__('AKSRT 文章作者卡片', 'aksrt'),
|
||||
[
|
||||
'description' => esc_html__('在文章/帖子侧栏显示当前内容作者信息。', 'aksrt'),
|
||||
'description' => esc_html__('在文章/帖子侧栏显示当前内容作者信息与更多文章。', 'aksrt'),
|
||||
'classname' => 'widget_aksrt_author_card',
|
||||
]
|
||||
);
|
||||
@@ -41,8 +41,20 @@ class WidgetAuthorCard extends \WP_Widget {
|
||||
return;
|
||||
}
|
||||
|
||||
$show_bg = !isset($instance['show_img_bg']) || !empty($instance['show_img_bg']);
|
||||
$show_bg = !isset($instance['show_img_bg']) || !empty($instance['show_img_bg']);
|
||||
$show_stats = !isset($instance['show_stats']) || !empty($instance['show_stats']);
|
||||
$show_posts = !isset($instance['show_posts']) || !empty($instance['show_posts']);
|
||||
$post_style = sanitize_key((string) ($instance['post_style'] ?? 'mini'));
|
||||
if (!in_array($post_style, ['card', 'mini'], true)) {
|
||||
// Legacy show_img checkbox from zibll.
|
||||
if (isset($instance['show_img'])) {
|
||||
$post_style = !empty($instance['show_img']) ? 'card' : 'mini';
|
||||
} else {
|
||||
$post_style = 'mini';
|
||||
}
|
||||
}
|
||||
$limit = max(0, min(12, (int) ($instance['limit'] ?? 6)));
|
||||
$orderby = sanitize_key((string) ($instance['orderby'] ?? 'date'));
|
||||
|
||||
$avatar = function_exists('aksrt_get_avatar_url')
|
||||
? aksrt_get_avatar_url($author_id, ['size' => 120])
|
||||
@@ -68,35 +80,39 @@ class WidgetAuthorCard extends \WP_Widget {
|
||||
$followers = \AKSRT\User\Follow::get_instance()->get_follower_count($author_id);
|
||||
}
|
||||
|
||||
$name_html = function_exists('aksrt_get_user_name')
|
||||
? aksrt_get_user_name(['id' => $author_id, 'follow' => false, 'class' => 'flex ac jc'])
|
||||
: '<a href="' . esc_url($home) . '">' . esc_html($user->display_name) . '</a>';
|
||||
|
||||
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 '<div class="aksrt-author-card' . ($show_bg ? ' has-cover' : '') . '">';
|
||||
echo '<div class="aksrt-author-card user-card author zib-widget' . ($show_bg ? ' has-cover' : '') . '">';
|
||||
if ($show_bg) {
|
||||
$bg = $cover !== '' ? ' style="background-image:url(' . esc_url($cover) . ')"' : '';
|
||||
echo '<div class="aksrt-author-card__cover"' . $bg . '></div>';
|
||||
echo '<div class="aksrt-author-card__cover user-cover graphic"' . $bg . '></div>';
|
||||
}
|
||||
echo '<div class="aksrt-author-card__body">';
|
||||
echo '<a class="aksrt-author-card__avatar" href="' . esc_url($home) . '"><img src="' . esc_url($avatar) . '" alt="" width="72" height="72" loading="lazy" /></a>';
|
||||
echo '<a class="aksrt-author-card__name" href="' . esc_url($home) . '">' . esc_html($user->display_name) . '</a>';
|
||||
echo '<div class="aksrt-author-card__body card-content">';
|
||||
echo '<div class="user-content">';
|
||||
echo '<a class="aksrt-author-card__avatar user-avatar" href="' . esc_url($home) . '"><img class="avatar" src="' . esc_url($avatar) . '" alt="" width="72" height="72" loading="lazy" /></a>';
|
||||
echo '<div class="aksrt-author-card__name user-name">' . $name_html . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
if ($bio !== '') {
|
||||
echo '<div class="aksrt-author-card__bio">' . esc_html(wp_trim_words($bio, 28)) . '</div>';
|
||||
echo '<div class="aksrt-author-card__bio user-desc muted-2-color em09">' . esc_html(wp_trim_words($bio, 28)) . '</div>';
|
||||
}
|
||||
$show_badge = !isset($instance['show_badge']) || !empty($instance['show_badge']);
|
||||
if ($show_badge && class_exists('\AKSRT\User\Badges') && \AKSRT\User\Badges::public_display()) {
|
||||
$wear = \AKSRT\User\Badges::get_wear($author_id);
|
||||
if ($wear === '') {
|
||||
// zibll alias fallback
|
||||
$wear = sanitize_key((string) get_user_meta($author_id, 'wear_medal', true));
|
||||
}
|
||||
if ($wear !== '') {
|
||||
$map = \AKSRT\User\Badges::get_dictionary_map();
|
||||
if (isset($map[$wear])) {
|
||||
$badge = $map[$wear];
|
||||
echo '<div class="aksrt-author-card__badge tone-' . esc_attr((string) ($badge['tone'] ?? 'neutral')) . '">';
|
||||
echo '<div class="aksrt-author-card__badge author-tag tone-' . esc_attr((string) ($badge['tone'] ?? 'neutral')) . '">';
|
||||
if (!empty($badge['icon_url'])) {
|
||||
echo '<img src="' . esc_url((string) $badge['icon_url']) . '" alt="" width="16" height="16" loading="lazy" /> ';
|
||||
} else {
|
||||
@@ -118,18 +134,37 @@ class WidgetAuthorCard extends \WP_Widget {
|
||||
}
|
||||
$cuid = get_current_user_id();
|
||||
$show_follow = !isset($instance['show_follow']) || !empty($instance['show_follow']);
|
||||
echo '<div class="aksrt-author-card__actions user-btns">';
|
||||
if ($show_follow && $cuid > 0 && $cuid !== $author_id && class_exists('\AKSRT\User\Follow')) {
|
||||
$following = \AKSRT\User\Follow::get_instance()->is_following($cuid, $author_id);
|
||||
$label = $following ? __('已关注', 'aksrt') : __('关注', 'aksrt');
|
||||
echo '<button type="button" class="aksrt-author-card__btn aksrt-author-card__follow' . ($following ? ' is-following' : '') . '" data-aksrt-follow="' . (int) $author_id . '">' . esc_html($label) . '</button>';
|
||||
echo '<button type="button" class="aksrt-author-card__btn aksrt-author-card__follow but' . ($following ? ' is-following' : '') . '" data-aksrt-follow="' . (int) $author_id . '">' . esc_html($label) . '</button>';
|
||||
}
|
||||
echo '<a class="aksrt-author-card__btn" href="' . esc_url($home) . '">' . esc_html__('查看主页', 'aksrt') . '</a>';
|
||||
echo '<a class="aksrt-author-card__btn but" href="' . esc_url($home) . '">' . esc_html__('查看主页', 'aksrt') . '</a>';
|
||||
echo '</div>';
|
||||
echo '</div>'; // .user-content
|
||||
|
||||
if ($show_posts && $limit > 0 && function_exists('aksrt_render_user_card_posts')) {
|
||||
echo aksrt_render_user_card_posts([ // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
'user_id' => $author_id,
|
||||
'limit' => $limit,
|
||||
'orderby' => $orderby,
|
||||
'post_style' => $post_style,
|
||||
'post_type' => 'post',
|
||||
'show_empty' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
echo '</div></div>';
|
||||
|
||||
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
public function form($instance): void {
|
||||
$post_style = sanitize_key((string) ($instance['post_style'] ?? 'mini'));
|
||||
if (!in_array($post_style, ['card', 'mini'], true)) {
|
||||
$post_style = !empty($instance['show_img']) ? 'card' : 'mini';
|
||||
}
|
||||
?>
|
||||
<p class="description"><?php esc_html_e('自动读取当前文章/帖子作者;也可固定用户 ID。', 'aksrt'); ?></p>
|
||||
<p>
|
||||
@@ -146,12 +181,44 @@ class WidgetAuthorCard extends \WP_Widget {
|
||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_img_bg')); ?>" value="1" <?php checked(!isset($instance['show_img_bg']) || !empty($instance['show_img_bg'])); ?>> <?php esc_html_e('显示封面', 'aksrt'); ?></label><br>
|
||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_stats')); ?>" value="1" <?php checked(!isset($instance['show_stats']) || !empty($instance['show_stats'])); ?>> <?php esc_html_e('显示统计', 'aksrt'); ?></label><br>
|
||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_follow')); ?>" value="1" <?php checked(!isset($instance['show_follow']) || !empty($instance['show_follow'])); ?>> <?php esc_html_e('显示关注按钮', 'aksrt'); ?></label><br>
|
||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_badge')); ?>" value="1" <?php checked(!isset($instance['show_badge']) || !empty($instance['show_badge'])); ?>> <?php esc_html_e('显示佩戴徽章', 'aksrt'); ?></label>
|
||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_badge')); ?>" value="1" <?php checked(!isset($instance['show_badge']) || !empty($instance['show_badge'])); ?>> <?php esc_html_e('显示佩戴徽章', 'aksrt'); ?></label><br>
|
||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_posts')); ?>" value="1" <?php checked(!isset($instance['show_posts']) || !empty($instance['show_posts'])); ?>> <?php esc_html_e('显示更多文章', 'aksrt'); ?></label>
|
||||
</p>
|
||||
<p>
|
||||
<label><?php esc_html_e('文章数量', 'aksrt'); ?>
|
||||
<input class="tiny-text" type="number" min="0" max="12" name="<?php echo esc_attr($this->get_field_name('limit')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['limit'] ?? 6))); ?>">
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label><?php esc_html_e('文章样式', 'aksrt'); ?>
|
||||
<select name="<?php echo esc_attr($this->get_field_name('post_style')); ?>">
|
||||
<option value="mini" <?php selected($post_style, 'mini'); ?>><?php esc_html_e('迷你列表', 'aksrt'); ?></option>
|
||||
<option value="card" <?php selected($post_style, 'card'); ?>><?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('orderby')); ?>">
|
||||
<option value="date" <?php selected(($instance['orderby'] ?? 'date'), 'date'); ?>><?php esc_html_e('最新', 'aksrt'); ?></option>
|
||||
<option value="views" <?php selected(($instance['orderby'] ?? ''), 'views'); ?>><?php esc_html_e('阅读量', 'aksrt'); ?></option>
|
||||
<option value="comment_count" <?php selected(($instance['orderby'] ?? ''), 'comment_count'); ?>><?php esc_html_e('评论数', 'aksrt'); ?></option>
|
||||
<option value="rand" <?php selected(($instance['orderby'] ?? ''), 'rand'); ?>><?php esc_html_e('随机', 'aksrt'); ?></option>
|
||||
</select>
|
||||
</label>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function update($new_instance, $old_instance): array {
|
||||
$post_style = sanitize_key((string) ($new_instance['post_style'] ?? 'mini'));
|
||||
if (!in_array($post_style, ['card', 'mini'], true)) {
|
||||
$post_style = 'mini';
|
||||
}
|
||||
$orderby = sanitize_key((string) ($new_instance['orderby'] ?? 'date'));
|
||||
if (!in_array($orderby, ['date', 'views', 'comment_count', 'modified', 'rand'], true)) {
|
||||
$orderby = 'date';
|
||||
}
|
||||
return [
|
||||
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
|
||||
'user_id' => max(0, (int) ($new_instance['user_id'] ?? 0)),
|
||||
@@ -159,6 +226,10 @@ class WidgetAuthorCard extends \WP_Widget {
|
||||
'show_stats' => !empty($new_instance['show_stats']) ? 1 : 0,
|
||||
'show_follow' => !empty($new_instance['show_follow']) ? 1 : 0,
|
||||
'show_badge' => !empty($new_instance['show_badge']) ? 1 : 0,
|
||||
'show_posts' => !empty($new_instance['show_posts']) ? 1 : 0,
|
||||
'limit' => max(0, min(12, (int) ($new_instance['limit'] ?? 6))),
|
||||
'post_style' => $post_style,
|
||||
'orderby' => $orderby,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ class WidgetUserCard extends \WP_Widget {
|
||||
parent::__construct(
|
||||
'aksrt_user_card',
|
||||
__('用户卡片', 'aksrt'),
|
||||
['description' => __('未登录显示登录入口;登录后显示当前用户卡片、签到与快捷按钮。', 'aksrt')]
|
||||
['description' => __('未登录显示登录入口;登录后显示当前用户卡片、签到、快捷按钮与更多文章。', 'aksrt')]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -24,9 +24,18 @@ class WidgetUserCard extends \WP_Widget {
|
||||
$show_stats = !empty($instance['show_stats']) || !isset($instance['show_stats']);
|
||||
$show_actions = !empty($instance['show_button']) || !isset($instance['show_button']);
|
||||
$show_checkin = !empty($instance['show_checkin']) || !isset($instance['show_checkin']);
|
||||
// zibll user widget forces show_posts=false; author widget enables it.
|
||||
// AKSRT keeps optional show_posts for current-user card (default off).
|
||||
$show_posts = !empty($instance['show_posts']);
|
||||
$loged_title = (string) ($instance['loged_title'] ?? __('Hi!请登录', 'aksrt'));
|
||||
$btn1_text = (string) ($instance['button_1_text'] ?? __('发布文章', 'aksrt'));
|
||||
$btn2_text = (string) ($instance['button_2_text'] ?? __('用户中心', 'aksrt'));
|
||||
$post_style = sanitize_key((string) ($instance['post_style'] ?? 'mini'));
|
||||
if (!in_array($post_style, ['card', 'mini'], true)) {
|
||||
$post_style = 'mini';
|
||||
}
|
||||
$limit = max(0, min(12, (int) ($instance['limit'] ?? 6)));
|
||||
$orderby = sanitize_key((string) ($instance['orderby'] ?? 'date'));
|
||||
|
||||
// Fixed user id wins; else current user.
|
||||
$user_id = !empty($instance['user_id']) ? (int) $instance['user_id'] : 0;
|
||||
@@ -63,7 +72,6 @@ class WidgetUserCard extends \WP_Widget {
|
||||
? aksrt_get_user_center_url()
|
||||
: $profile_url;
|
||||
$compose_url = home_url('/');
|
||||
// Prefer front submit / write page when available.
|
||||
if (function_exists('get_pages')) {
|
||||
$pages = get_pages(['meta_key' => '_wp_page_template', 'meta_value' => 'templates/pages/page-submit.php', 'number' => 1]);
|
||||
if (!empty($pages[0])) {
|
||||
@@ -90,14 +98,32 @@ class WidgetUserCard extends \WP_Widget {
|
||||
$following_count = \AKSRT\User\Follow::get_instance()->get_following_count($user_id);
|
||||
}
|
||||
|
||||
$name_html = function_exists('aksrt_get_user_name')
|
||||
? aksrt_get_user_name(['id' => $user_id, 'follow' => !$is_self, 'class' => 'flex ac jc'])
|
||||
: '<a href="' . esc_url($profile_url) . '">' . esc_html($user->display_name) . '</a>';
|
||||
|
||||
echo '<div class="aksrt-user-card user-card zib-widget widget">';
|
||||
if ($show_cover) {
|
||||
$bg = $cover !== '' ? ' style="background-image:url(' . esc_url($cover) . ')"' : '';
|
||||
echo '<div class="aksrt-user-card__cover user-cover graphic"' . $bg . '></div>';
|
||||
if ($cover !== '') {
|
||||
echo '<div class="aksrt-user-card__cover user-cover graphic" style="background-image:url(' . esc_url($cover) . ')"></div>';
|
||||
} else {
|
||||
// Prefer site default cover image when available.
|
||||
$default_cover = '';
|
||||
if (class_exists('\AKSRT\Settings\Settings') && method_exists('\AKSRT\Settings\Settings', 'get_user_cover_default')) {
|
||||
$default_cover = (string) \AKSRT\Settings\Settings::get_user_cover_default();
|
||||
}
|
||||
if ($default_cover === '' && function_exists('get_theme_mod')) {
|
||||
$default_cover = (string) get_theme_mod('aksrt_user_cover', '');
|
||||
}
|
||||
if ($default_cover !== '') {
|
||||
echo '<div class="aksrt-user-card__cover user-cover graphic" style="background-image:url(' . esc_url($default_cover) . ')"></div>';
|
||||
} else {
|
||||
echo '<div class="aksrt-user-card__cover user-cover graphic is-empty"></div>';
|
||||
}
|
||||
}
|
||||
}
|
||||
echo '<div class="aksrt-user-card__body card-content">';
|
||||
if ($show_checkin && $is_self && function_exists('aksrt_header_user_checkin_btn')) {
|
||||
// Reuse compact check-in button markup (without details if already long).
|
||||
$btn = aksrt_header_user_checkin_btn($user_id);
|
||||
if ($btn !== '') {
|
||||
echo '<div class="aksrt-user-card__checkin">' . $btn . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
@@ -106,7 +132,7 @@ class WidgetUserCard extends \WP_Widget {
|
||||
echo '<div class="aksrt-user-card__avatar user-avatar">';
|
||||
echo '<a href="' . esc_url($profile_url) . '"><img src="' . esc_url($avatar_url) . '" alt="' . esc_attr($user->display_name) . '" loading="lazy" width="72" height="72" /></a>';
|
||||
echo '</div>';
|
||||
echo '<h4 class="aksrt-user-card__name user-name"><a href="' . esc_url($profile_url) . '">' . esc_html($user->display_name) . '</a></h4>';
|
||||
echo '<h4 class="aksrt-user-card__name user-name">' . $name_html . '</h4>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
if ($bio !== '') {
|
||||
echo '<p class="aksrt-user-card__bio user-desc">' . esc_html(wp_trim_words($bio, 24)) . '</p>';
|
||||
}
|
||||
@@ -125,6 +151,18 @@ class WidgetUserCard extends \WP_Widget {
|
||||
} elseif ($show_actions) {
|
||||
echo '<div class="aksrt-user-card__actions"><a class="aksrt-user-card__btn aksrt-user-card__btn--primary" href="' . esc_url($profile_url) . '">' . esc_html__('查看主页', 'aksrt') . '</a></div>';
|
||||
}
|
||||
|
||||
if ($show_posts && $limit > 0 && function_exists('aksrt_render_user_card_posts')) {
|
||||
echo aksrt_render_user_card_posts([ // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
'user_id' => $user_id,
|
||||
'limit' => $limit,
|
||||
'orderby' => $orderby,
|
||||
'post_style' => $post_style,
|
||||
'post_type' => 'post',
|
||||
'show_empty' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
echo '</div></div>';
|
||||
|
||||
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
@@ -149,7 +187,15 @@ class WidgetUserCard extends \WP_Widget {
|
||||
|
||||
echo '<div class="aksrt-user-card user-card zib-widget widget is-guest">';
|
||||
if ($show_cover) {
|
||||
echo '<div class="aksrt-user-card__cover user-cover graphic is-empty"></div>';
|
||||
$default_cover = '';
|
||||
if (class_exists('\AKSRT\Settings\Settings') && method_exists('\AKSRT\Settings\Settings', 'get_user_cover_default')) {
|
||||
$default_cover = (string) \AKSRT\Settings\Settings::get_user_cover_default();
|
||||
}
|
||||
if ($default_cover !== '') {
|
||||
echo '<div class="aksrt-user-card__cover user-cover graphic" style="background-image:url(' . esc_url($default_cover) . ')"></div>';
|
||||
} else {
|
||||
echo '<div class="aksrt-user-card__cover user-cover graphic is-empty"></div>';
|
||||
}
|
||||
}
|
||||
echo '<div class="aksrt-user-card__body card-content">';
|
||||
echo '<div class="aksrt-user-card__avatar user-avatar"><img src="' . esc_url($default_avatar) . '" alt="" width="72" height="72" loading="lazy" /></div>';
|
||||
@@ -167,6 +213,10 @@ class WidgetUserCard extends \WP_Widget {
|
||||
}
|
||||
|
||||
public function form($instance): void {
|
||||
$post_style = sanitize_key((string) ($instance['post_style'] ?? 'mini'));
|
||||
if (!in_array($post_style, ['card', 'mini'], true)) {
|
||||
$post_style = 'mini';
|
||||
}
|
||||
?>
|
||||
<p>
|
||||
<label><?php esc_html_e('标题(可选)', 'aksrt'); ?>
|
||||
@@ -197,22 +247,58 @@ class WidgetUserCard extends \WP_Widget {
|
||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_img_bg')); ?>" value="1" <?php checked(!isset($instance['show_img_bg']) || !empty($instance['show_img_bg'])); ?>> <?php esc_html_e('显示封面', 'aksrt'); ?></label><br>
|
||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_stats')); ?>" value="1" <?php checked(!isset($instance['show_stats']) || !empty($instance['show_stats'])); ?>> <?php esc_html_e('显示统计', 'aksrt'); ?></label><br>
|
||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_button')); ?>" value="1" <?php checked(!isset($instance['show_button']) || !empty($instance['show_button'])); ?>> <?php esc_html_e('显示操作按钮', 'aksrt'); ?></label><br>
|
||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_checkin')); ?>" value="1" <?php checked(!isset($instance['show_checkin']) || !empty($instance['show_checkin'])); ?>> <?php esc_html_e('显示签到(本人)', 'aksrt'); ?></label>
|
||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_checkin')); ?>" value="1" <?php checked(!isset($instance['show_checkin']) || !empty($instance['show_checkin'])); ?>> <?php esc_html_e('显示签到(本人)', 'aksrt'); ?></label><br>
|
||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_posts')); ?>" value="1" <?php checked(!empty($instance['show_posts'])); ?>> <?php esc_html_e('显示更多文章', 'aksrt'); ?></label>
|
||||
</p>
|
||||
<p>
|
||||
<label><?php esc_html_e('文章数量', 'aksrt'); ?>
|
||||
<input class="tiny-text" type="number" min="0" max="12" name="<?php echo esc_attr($this->get_field_name('limit')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['limit'] ?? 6))); ?>">
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label><?php esc_html_e('文章样式', 'aksrt'); ?>
|
||||
<select name="<?php echo esc_attr($this->get_field_name('post_style')); ?>">
|
||||
<option value="mini" <?php selected($post_style, 'mini'); ?>><?php esc_html_e('迷你列表', 'aksrt'); ?></option>
|
||||
<option value="card" <?php selected($post_style, 'card'); ?>><?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('orderby')); ?>">
|
||||
<option value="date" <?php selected(($instance['orderby'] ?? 'date'), 'date'); ?>><?php esc_html_e('最新', 'aksrt'); ?></option>
|
||||
<option value="views" <?php selected(($instance['orderby'] ?? ''), 'views'); ?>><?php esc_html_e('阅读量', 'aksrt'); ?></option>
|
||||
<option value="comment_count" <?php selected(($instance['orderby'] ?? ''), 'comment_count'); ?>><?php esc_html_e('评论数', 'aksrt'); ?></option>
|
||||
<option value="rand" <?php selected(($instance['orderby'] ?? ''), 'rand'); ?>><?php esc_html_e('随机', 'aksrt'); ?></option>
|
||||
</select>
|
||||
</label>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function update($new_instance, $old_instance): array {
|
||||
$post_style = sanitize_key((string) ($new_instance['post_style'] ?? 'mini'));
|
||||
if (!in_array($post_style, ['card', 'mini'], true)) {
|
||||
$post_style = 'mini';
|
||||
}
|
||||
$orderby = sanitize_key((string) ($new_instance['orderby'] ?? 'date'));
|
||||
if (!in_array($orderby, ['date', 'views', 'comment_count', 'modified', 'rand'], true)) {
|
||||
$orderby = 'date';
|
||||
}
|
||||
return [
|
||||
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
|
||||
'user_id' => max(0, (int) ($new_instance['user_id'] ?? 0)),
|
||||
'loged_title' => sanitize_text_field((string) ($new_instance['loged_title'] ?? '')),
|
||||
'button_1_text' => sanitize_text_field((string) ($new_instance['button_1_text'] ?? '')),
|
||||
'button_2_text' => sanitize_text_field((string) ($new_instance['button_2_text'] ?? '')),
|
||||
'show_img_bg' => !empty($new_instance['show_img_bg']) ? 1 : 0,
|
||||
'show_stats' => !empty($new_instance['show_stats']) ? 1 : 0,
|
||||
'show_button' => !empty($new_instance['show_button']) ? 1 : 0,
|
||||
'show_checkin' => !empty($new_instance['show_checkin']) ? 1 : 0,
|
||||
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
|
||||
'user_id' => max(0, (int) ($new_instance['user_id'] ?? 0)),
|
||||
'loged_title' => sanitize_text_field((string) ($new_instance['loged_title'] ?? '')),
|
||||
'button_1_text' => sanitize_text_field((string) ($new_instance['button_1_text'] ?? '')),
|
||||
'button_2_text' => sanitize_text_field((string) ($new_instance['button_2_text'] ?? '')),
|
||||
'show_img_bg' => !empty($new_instance['show_img_bg']) ? 1 : 0,
|
||||
'show_stats' => !empty($new_instance['show_stats']) ? 1 : 0,
|
||||
'show_button' => !empty($new_instance['show_button']) ? 1 : 0,
|
||||
'show_checkin' => !empty($new_instance['show_checkin']) ? 1 : 0,
|
||||
'show_posts' => !empty($new_instance['show_posts']) ? 1 : 0,
|
||||
'limit' => max(0, min(12, (int) ($new_instance['limit'] ?? 6))),
|
||||
'post_style' => $post_style,
|
||||
'orderby' => $orderby,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,10 +25,9 @@ class WidgetUserRanking extends \WP_Widget {
|
||||
|
||||
$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'];
|
||||
echo $args['before_title'] . esc_html($title) . $args['after_title'];
|
||||
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
|
||||
if (!empty($instance['checkin_btn']) && is_user_logged_in() && function_exists('aksrt_header_user_checkin_btn')) {
|
||||
$cbtn = aksrt_header_user_checkin_btn(get_current_user_id());
|
||||
@@ -37,6 +36,14 @@ class WidgetUserRanking extends \WP_Widget {
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($users)) {
|
||||
echo '<p class="aksrt-user-ranking__empty muted-2-color">' . esc_html__('暂无排行数据', 'aksrt') . '</p>';
|
||||
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
return;
|
||||
}
|
||||
|
||||
$top_badge = !empty($instance['top_badge']);
|
||||
|
||||
echo '<ul class="aksrt-user-ranking">';
|
||||
$rank = 0;
|
||||
foreach ($users as $user) {
|
||||
@@ -46,7 +53,12 @@ class WidgetUserRanking extends \WP_Widget {
|
||||
? aksrt_get_user_home_url($user['id'])
|
||||
: get_author_posts_url($user['id']);
|
||||
|
||||
echo '<li class="aksrt-user-ranking__item' . esc_attr($rank_class) . '">';
|
||||
echo '<li class="aksrt-user-ranking__item relative' . esc_attr($rank_class) . '">';
|
||||
|
||||
if ($top_badge && $rank <= 3) {
|
||||
$tone = $rank === 1 ? 'jb-red' : ($rank === 2 ? 'jb-yellow' : 'b-gray');
|
||||
echo '<badge class="img-badge left hot ' . esc_attr($tone) . '"><i>TOP' . (int) $rank . '</i></badge>';
|
||||
}
|
||||
|
||||
// 排名
|
||||
echo '<span class="aksrt-user-ranking__rank">' . esc_html((string) $rank) . '</span>';
|
||||
@@ -76,7 +88,7 @@ class WidgetUserRanking extends \WP_Widget {
|
||||
}
|
||||
echo '</ul>';
|
||||
|
||||
echo $args['after_widget'];
|
||||
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
private function get_ranking(string $rank_by, int $count, string $desc_mode = 'desc'): array {
|
||||
@@ -287,6 +299,12 @@ class WidgetUserRanking extends \WP_Widget {
|
||||
<?php esc_html_e('顶部显示签到按钮', 'aksrt'); ?>
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label>
|
||||
<input type="checkbox" name="<?php echo esc_attr($this->get_field_name('top_badge')); ?>" value="1" <?php checked(!empty($instance['top_badge'])); ?>>
|
||||
<?php esc_html_e('前三名 TOP 角标', 'aksrt'); ?>
|
||||
</label>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
|
||||
@@ -297,6 +315,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['top_badge'] = !empty($new_instance['top_badge']);
|
||||
$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