进行了一些小优化
This commit is contained in:
@@ -25,32 +25,71 @@ final class UserRenderer {
|
||||
return self::guest_card($i);
|
||||
}
|
||||
$user = get_userdata($user_id);
|
||||
return $user instanceof \WP_User ? self::profile_card($user, $i, true) : '';
|
||||
if (!$user instanceof \WP_User) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return self::card_box($user_id, [
|
||||
'class' => 'widget',
|
||||
'show_posts' => false,
|
||||
'show_checkin' => true,
|
||||
'show_img_bg' => !isset($i['show_img_bg']) || !empty($i['show_img_bg']),
|
||||
] + self::card_button_args($i));
|
||||
}
|
||||
|
||||
private static function author_card(array $i): string {
|
||||
$post = get_post();
|
||||
if (!$post instanceof \WP_Post) {
|
||||
return '';
|
||||
}
|
||||
$user = get_userdata((int) $post->post_author);
|
||||
if (!$user instanceof \WP_User) {
|
||||
return '';
|
||||
}
|
||||
$post_style = sanitize_key((string) ($i['post_style'] ?? 'mini'));
|
||||
|
||||
return self::card_box((int) $user->ID, [
|
||||
'class' => 'widget',
|
||||
'show_img_bg' => !isset($i['show_img_bg']) || !empty($i['show_img_bg']),
|
||||
'show_posts' => !isset($i['show_posts']) || !empty($i['show_posts']),
|
||||
'limit' => max(0, min(12, (int) ($i['limit'] ?? 6))),
|
||||
'orderby' => sanitize_key((string) ($i['orderby'] ?? 'date')),
|
||||
'post_style' => $post_style === 'card' ? 'card' : 'mini',
|
||||
] + self::card_button_args($i));
|
||||
}
|
||||
|
||||
private static function card_button_args(array $i): array {
|
||||
return [
|
||||
'show_button' => !isset($i['show_button']) || !empty($i['show_button']),
|
||||
'button_1' => (string) ($i['button_1'] ?? 'post'),
|
||||
'button_1_class' => (string) ($i['button_1_class'] ?? 'jb-pink'),
|
||||
'button_1_text' => (string) ($i['button_1_text'] ?? ''),
|
||||
'button_2' => (string) ($i['button_2'] ?? 'center'),
|
||||
'button_2_class' => (string) ($i['button_2_class'] ?? 'jb-blue'),
|
||||
'button_2_text' => (string) ($i['button_2_text'] ?? ''),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 未登录卡片使用独立组件结构,避免嵌套全局 .widget 卡片外观。
|
||||
* 未登录卡片(legacy zib_get_user_singin_page_box parity)。
|
||||
*/
|
||||
private static function guest_card(array $i): string {
|
||||
$login = function_exists('aksrt_get_login_url') ? aksrt_get_login_url() : wp_login_url();
|
||||
$register = function_exists('aksrt_get_register_url') ? aksrt_get_register_url() : wp_registration_url();
|
||||
$title = trim((string) ($i['loged_title'] ?? ''));
|
||||
if ($title === '') {
|
||||
$title = __('Hi!请登录', 'aksrt');
|
||||
}
|
||||
|
||||
$card_classes = 'aksrt-user-card is-guest';
|
||||
$cover = '';
|
||||
if (!empty($i['show_cover'])) {
|
||||
$cover_url = function_exists('aksrt_get_user_cover_url') ? aksrt_get_user_cover_url(0) : get_theme_file_uri('assets/img/user_t.jpg');
|
||||
$cover = '<div class="aksrt-user-card__cover"><img' . aksrt_get_lazy_attr('lazy_cover', $cover_url, 'lazyload fit-cover', aksrt_get_lazy_thumb('lg')) . ' alt=""></div>';
|
||||
} else {
|
||||
$card_classes .= ' no-cover';
|
||||
if (!isset($i['show_img_bg']) || !empty($i['show_img_bg'])) {
|
||||
$cover_url = aksrt_get_user_cover_url(0);
|
||||
$cover = '<div class="user-cover graphic" style="padding-bottom: 50%;"><img' . aksrt_get_lazy_attr('lazy_other', $cover_url, 'fit-cover user-cover', aksrt_get_lazy_thumb()) . ' alt="' . esc_attr__('用户封面', 'aksrt') . '"></div>';
|
||||
}
|
||||
|
||||
$buttons = '<a class="signin-loader aksrt-widget-button is-primary" href="' . esc_url($login) . '"><i class="fa fas fa-fw fa-sign-in-alt" aria-hidden="true"></i>' . esc_html__('登录', 'aksrt') . '</a>';
|
||||
$avatar = '<span class="avatar-img avatar-lg">' . aksrt_get_avatar_html(0, 80, ['class' => 'fit-cover avatar']) . '</span>';
|
||||
|
||||
$buttons = '<a href="javascript:;" class="signin-loader but jb-blue padding-lg"><i class="fa fas fa-fw fa-sign-in" aria-hidden="true"></i>' . esc_html__('登录', 'aksrt') . '</a>';
|
||||
if (get_option('users_can_register')) {
|
||||
$buttons .= '<a class="signup-loader aksrt-widget-button is-secondary" href="' . esc_url($register) . '"><i class="fa fas fa-fw fa-user-plus" aria-hidden="true"></i>' . esc_html__('注册', 'aksrt') . '</a>';
|
||||
$buttons .= '<a href="javascript:;" class="signup-loader ml10 but jb-yellow padding-lg">' . aksrt_renderer_svg_icon('signup') . esc_html__('注册', 'aksrt') . '</a>';
|
||||
}
|
||||
|
||||
$oauth_html = '';
|
||||
@@ -61,74 +100,124 @@ final class UserRenderer {
|
||||
}
|
||||
}
|
||||
|
||||
$avatar = '<span class="aksrt-user-card__avatar">' . aksrt_get_avatar_html(0, 80, ['class' => 'avatar fit-cover']) . '</span>';
|
||||
|
||||
return '<article class="' . esc_attr($card_classes) . '">' . $cover
|
||||
. '<div class="aksrt-user-card__body">'
|
||||
. $avatar
|
||||
. '<p class="aksrt-user-card__guest-title">' . esc_html($title) . '</p>'
|
||||
. '<div class="aksrt-user-card__guest-actions">' . $buttons . '</div>'
|
||||
return '<div class="user-card zib-widget widget">' . $cover
|
||||
. '<div class="card-content mt10">'
|
||||
. '<div class="user-content">'
|
||||
. '<div class="user-avatar">' . $avatar . '</div>'
|
||||
. '<div class="user-info mt10">'
|
||||
. '<div class="text-center box-body">'
|
||||
. '<p class="muted-color box-body em12">' . esc_html($title) . '</p>'
|
||||
. '<p>' . $buttons . '</p>'
|
||||
. $oauth_html
|
||||
. '</div></article>';
|
||||
. '</div></div></div></div></div>';
|
||||
}
|
||||
|
||||
private static function author_card(array $i): string {
|
||||
$post = get_post();
|
||||
if (!$post instanceof \WP_Post) {
|
||||
/**
|
||||
* 登录卡片(legacy zib_get_user_card_box parity)。
|
||||
*
|
||||
* @param int $user_id 卡片用户。
|
||||
* @param array $args class/show_posts/show_checkin/show_img_bg/show_button/button_1,2 与文章模块参数。
|
||||
*/
|
||||
private static function card_box(int $user_id, array $args = []): string {
|
||||
$args = wp_parse_args($args, [
|
||||
'class' => '',
|
||||
'show_posts' => true,
|
||||
'show_checkin' => false,
|
||||
'show_img_bg' => false,
|
||||
'show_button' => true,
|
||||
'button_1' => 'post',
|
||||
'button_1_class' => 'jb-pink',
|
||||
'button_1_text' => __('发布文章', 'aksrt'),
|
||||
'button_2' => 'center',
|
||||
'button_2_class' => 'jb-blue',
|
||||
'button_2_text' => __('用户中心', 'aksrt'),
|
||||
'limit' => 6,
|
||||
'orderby' => 'views',
|
||||
'post_style' => 'card',
|
||||
]);
|
||||
|
||||
$avatar = aksrt_get_avatar_box($user_id, 'avatar-img avatar-lg');
|
||||
$name = aksrt_get_user_name([
|
||||
'id' => $user_id,
|
||||
'class' => 'flex1 flex ac',
|
||||
'vip' => false,
|
||||
'medal' => true,
|
||||
'auth' => true,
|
||||
'level' => true,
|
||||
'follow' => true,
|
||||
]);
|
||||
$tag_metas = aksrt_get_user_badges($user_id);
|
||||
$desc = aksrt_get_user_description($user_id);
|
||||
$btns = '';
|
||||
$posts = '';
|
||||
$checkin = '';
|
||||
$cuid = get_current_user_id();
|
||||
|
||||
if (!$cuid || $cuid !== $user_id) {
|
||||
$args['show_button'] = false;
|
||||
}
|
||||
if ($args['show_posts'] && (int) $args['limit'] > 0) {
|
||||
$posts = aksrt_render_user_card_posts([
|
||||
'user_id' => $user_id,
|
||||
'limit' => (int) $args['limit'],
|
||||
'orderby' => (string) $args['orderby'],
|
||||
'post_type' => 'post',
|
||||
'post_style' => (string) $args['post_style'],
|
||||
]);
|
||||
}
|
||||
if ($args['show_checkin'] && $cuid === $user_id) {
|
||||
$checkin = aksrt_header_user_checkin_btn($user_id, true, 'img-badge jb-yellow');
|
||||
}
|
||||
|
||||
if ($args['show_button']) {
|
||||
$btns .= self::new_add_btn((string) $args['button_1'], 'but pw-1em mr6 ' . (string) $args['button_1_class'], (string) $args['button_1_text']);
|
||||
if ('home' === $args['button_2']) {
|
||||
$btns .= self::card_link(aksrt_get_user_home_url($user_id), (string) $args['button_2_class'], (string) $args['button_2_text']);
|
||||
} else {
|
||||
$btns .= self::card_link(aksrt_get_user_center_url(), (string) $args['button_2_class'], (string) $args['button_2_text'], true);
|
||||
}
|
||||
$btns = $btns !== '' ? '<div class="user-btns mt20">' . $btns . '</div>' : '';
|
||||
}
|
||||
|
||||
$cover = '';
|
||||
if ($args['show_img_bg']) {
|
||||
$cover_url = aksrt_get_user_cover_url($user_id);
|
||||
$cover = '<div class="user-cover graphic" style="padding-bottom: 50%;"><img' . aksrt_get_lazy_attr('lazy_cover', $cover_url, 'fit-cover user-cover', aksrt_get_lazy_thumb()) . ' alt="' . esc_attr__('用户封面', 'aksrt') . '"></div>';
|
||||
}
|
||||
|
||||
return '<div class="user-card zib-widget ' . esc_attr((string) $args['class']) . '">' . $cover
|
||||
. '<div class="card-content mt10 relative">'
|
||||
. '<div class="user-content">' . $checkin
|
||||
. '<div class="user-avatar">' . $avatar . '</div>'
|
||||
. '<div class="user-info mt20 mb10">'
|
||||
. '<div class="user-name flex jc">' . $name . '</div>'
|
||||
. '<div class="author-tag mt10 mini-scrollbar">' . $tag_metas . '</div>'
|
||||
. '<div class="user-desc mt10 muted-2-color em09">' . esc_html($desc) . '</div>'
|
||||
. $btns
|
||||
. '</div></div>'
|
||||
. $posts
|
||||
. '</div></div>';
|
||||
}
|
||||
|
||||
/**
|
||||
* 卡片按钮1(legacy zib_get_new_add_btns 的 post 类型 parity)。
|
||||
*/
|
||||
private static function new_add_btn(string $type, string $class, string $text): string {
|
||||
if ($type !== 'post') {
|
||||
return '';
|
||||
}
|
||||
$user = get_userdata((int) $post->post_author);
|
||||
return $user instanceof \WP_User ? self::profile_card($user, $i, false) : '';
|
||||
}
|
||||
|
||||
private static function profile_card(\WP_User $user, array $i, bool $self_card): string {
|
||||
$user_id = (int) $user->ID;
|
||||
$home = function_exists('aksrt_get_user_home_url') ? aksrt_get_user_home_url($user) : get_author_posts_url($user_id);
|
||||
$cover = function_exists('aksrt_get_user_cover_url') ? aksrt_get_user_cover_url($user_id) : '';
|
||||
$description = function_exists('aksrt_get_user_description') ? aksrt_get_user_description($user_id) : (string) $user->description;
|
||||
$followers = class_exists('AKSRT\\User\\Follow') ? \AKSRT\User\Follow::get_instance()->get_follower_count($user_id) : 0;
|
||||
$following = class_exists('AKSRT\\User\\Follow') ? \AKSRT\User\Follow::get_instance()->get_following_count($user_id) : 0;
|
||||
$html = '<article class="aksrt-user-card' . (!$self_card ? ' is-author' : '') . (empty($i['show_cover']) ? ' no-cover' : '') . '">';
|
||||
if (!empty($i['show_cover'])) {
|
||||
$html .= '<div class="aksrt-user-card__cover"' . ($cover ? ' style="background-image:url(' . esc_url($cover) . ')"' : '') . '></div>';
|
||||
}
|
||||
$html .= '<div class="aksrt-user-card__body"><a class="aksrt-user-card__avatar" href="' . esc_url($home) . '">' . (function_exists('aksrt_get_avatar_html') ? aksrt_get_avatar_html($user_id, 96) : get_avatar($user_id, 96)) . '</a><h3><a href="' . esc_url($home) . '">' . esc_html($user->display_name) . '</a></h3>';
|
||||
if (function_exists('aksrt_render_user_identity_chips')) {
|
||||
$html .= '<div class="aksrt-user-card__identity">' . aksrt_render_user_identity_chips($user_id, 'widget') . '</div>';
|
||||
}
|
||||
$html .= '<p>' . esc_html(wp_trim_words($description, 24)) . '</p><div class="aksrt-user-card__stats"><span><b>' . number_format_i18n(count_user_posts($user_id, 'post', true)) . '</b>' . esc_html__('文章', 'aksrt') . '</span><span><b>' . number_format_i18n($followers) . '</b>' . esc_html__('粉丝', 'aksrt') . '</span><span><b>' . number_format_i18n($following) . '</b>' . esc_html__('关注', 'aksrt') . '</span></div>';
|
||||
if (!empty($i['show_buttons'])) {
|
||||
$html .= '<div class="aksrt-user-card__actions">' . self::action((string) $i['primary_action'], $user, true) . self::action((string) $i['secondary_action'], $user, false) . '</div>';
|
||||
}
|
||||
if (!$self_card && get_current_user_id() > 0 && get_current_user_id() !== $user_id) {
|
||||
$active = class_exists('AKSRT\\User\\Follow') && \AKSRT\User\Follow::get_instance()->is_following(get_current_user_id(), $user_id);
|
||||
$html .= '<button class="aksrt-user-card__follow' . ($active ? ' is-following' : '') . '" type="button" data-aksrt-follow="' . $user_id . '">' . esc_html($active ? __('已关注', 'aksrt') : __('关注', 'aksrt')) . '</button>';
|
||||
}
|
||||
$html .= '</div>';
|
||||
if (!empty($i['show_posts']) && function_exists('aksrt_render_user_card_posts')) {
|
||||
$html .= '<div class="aksrt-user-card__posts">' . aksrt_render_user_card_posts(['user_id' => $user_id, 'limit' => 4, 'post_style' => 'mini']) . '</div>';
|
||||
}
|
||||
return $html . '</article>';
|
||||
}
|
||||
|
||||
private static function action(string $action, \WP_User $user, bool $primary): string {
|
||||
$labels = ['home' => __('个人主页', 'aksrt'), 'submit' => __('发布文章', 'aksrt'), 'vip' => __('会员中心', 'aksrt'), 'orders' => __('订单中心', 'aksrt'), 'recharge' => __('余额充值', 'aksrt'), 'signin' => __('签到', 'aksrt')];
|
||||
$variant = $primary ? ' is-primary' : ' is-secondary';
|
||||
if ($action === 'signin' && function_exists('aksrt_header_user_checkin_btn') && get_current_user_id() === (int) $user->ID) {
|
||||
return '<span class="aksrt-user-card__checkin' . $variant . '">' . aksrt_header_user_checkin_btn((int) $user->ID, true) . '</span>';
|
||||
}
|
||||
if ($action === 'home') {
|
||||
$url = function_exists('aksrt_get_user_home_url') ? aksrt_get_user_home_url($user) : get_author_posts_url((int) $user->ID);
|
||||
} elseif ($action === 'submit') {
|
||||
$url = function_exists('aksrt_get_new_post_url') ? aksrt_get_new_post_url() : home_url('/submit/');
|
||||
} else {
|
||||
$tab = $action === 'signin' ? 'credits' : $action;
|
||||
$url = function_exists('aksrt_get_user_center_url') ? aksrt_get_user_center_url($tab) : home_url('/user/');
|
||||
}
|
||||
$url = aksrt_get_new_post_url();
|
||||
if ($url === '') {
|
||||
return '';
|
||||
}
|
||||
return '<a class="aksrt-widget-button' . $variant . '" href="' . esc_url($url) . '">' . esc_html($labels[$action] ?? $labels['home']) . '</a>';
|
||||
return '<a rel="nofollow" class="newadd-btns ' . esc_attr($class) . '" href="' . esc_url($url) . '"><icon class="jb-green"><i class="fa fas fa-pencil-square" aria-hidden="true"></i></icon><text>' . esc_html($text) . '</text></a>';
|
||||
}
|
||||
|
||||
private static function card_link(string $url, string $class, string $text, bool $nofollow = false): string {
|
||||
if ($url === '') {
|
||||
return '';
|
||||
}
|
||||
return '<a' . ($nofollow ? ' rel="nofollow"' : '') . ' href="' . esc_url($url) . '" class="but pw-1em ml6 ' . esc_attr($class) . '">' . esc_html($text) . '</a>';
|
||||
}
|
||||
|
||||
private static function ranking(array $i): string {
|
||||
|
||||
Reference in New Issue
Block a user