Files
wordpress-theme-aksrt/templates/parts/user/author-hero.php
T
2026-08-23 13:42:35 +08:00

191 lines
11 KiB
PHP

<?php
/**
* Author Hero — shared hero section for author archive and public user homepage.
*
* Aligns with legacy(): cover, avatar, name, identity, desc,
* and header actions. Content stats live in the tab bar, not a body stats row.
*
* Contract: get_template_part(..., null, $args)
* user WP_User
* dashboard array from aksrt_get_user_dashboard_view()
* user_home_settings array
* is_author_page bool
* tab_counts array (optional)
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
$args = is_array($args ?? null) ? $args : [];
$user = $args['user'] ?? null;
$dashboard = is_array($args['dashboard'] ?? null) ? $args['dashboard'] : [];
$settings = is_array($args['user_home_settings'] ?? null) ? $args['user_home_settings'] : [];
$is_author = !empty($args['is_author_page']);
$tab_counts = is_array($args['tab_counts'] ?? null) ? $args['tab_counts'] : [];
if (!($user instanceof WP_User)) {
return;
}
$user_id = (int) $user->ID;
$home_url = function_exists('aksrt_get_user_home_url') ? aksrt_get_user_home_url($user) : home_url('/');
$show_cover = !array_key_exists('cover', $settings) || !empty($settings['cover']);
$show_follow = !array_key_exists('follow', $settings) || !empty($settings['follow']);
$show_avatar = !array_key_exists('avatar', $settings) || !empty($settings['avatar']);
$show_name = !array_key_exists('name', $settings) || !empty($settings['name']);
$show_desc = !array_key_exists('desc', $settings) || !empty($settings['desc']);
$show_message = !array_key_exists('message', $settings) || !empty($settings['message']);
$profile_details = is_array($dashboard['profile_details'] ?? null) ? $dashboard['profile_details'] : [];
// Registration time is always present, so the details dialog always has
// content and the "更多资料" menu action is always available.
$registered_time = get_date_from_gmt((string) $user->user_registered, 'Y-m-d H:i:s');
$last_login_ts = (int) get_user_meta($user_id, 'aksrt_last_login', true);
$last_login_time = $last_login_ts > 0 ? wp_date('Y-m-d H:i:s', $last_login_ts) : '';
$has_more_details = $registered_time !== '' || $last_login_time !== '' || !empty($profile_details['website']) || !empty($profile_details['qq']) || !empty($profile_details['wechat']) || !empty($profile_details['github']);
$signals = $dashboard['profile_signals'] ?? [];
$notice = $dashboard['profile_notice'] ?? null;
$show_report = !empty($dashboard['show_report']);
$bg_url = $dashboard['background_url'] ?? '';
$is_own = !empty($dashboard['is_own_profile']);
$follower_count = (int) ($tab_counts['follow'] ?? 0);
if ($follower_count <= 0 && class_exists('\AKSRT\User\Follow')) {
$follower_count = (int) \AKSRT\User\Follow::get_instance()->get_follower_count($user_id);
}
$messages_url = function_exists('aksrt_get_messages_page_url') ? aksrt_get_messages_page_url() : '';
?><div class="aksrt-author-hero<?php echo $show_cover ? '' : ' aksrt-author-hero--no-cover'; ?>">
<?php if ($show_cover) : ?>
<div class="aksrt-author-hero__cover">
<img<?php echo aksrt_get_lazy_attr('lazy_cover', (string) ($bg_url ? $bg_url : get_theme_file_uri('assets/img/person_background.png')), 'aksrt-author-hero__cover-img', aksrt_get_lazy_thumb('lg')); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> alt="">
<div class="aksrt-author-hero__cover-mask"></div>
<?php if ($follower_count > 0) : ?>
<div class="aksrt-author-hero__cover-metas" aria-label="<?php esc_attr_e('主页数据', 'aksrt'); ?>">
<span class="aksrt-author-hero__cover-meta" title="<?php esc_attr_e('粉丝', 'aksrt'); ?>">
<i class="fa fas fa-heart" aria-hidden="true"></i>
<?php echo esc_html((string) $follower_count); ?>
</span>
</div>
<?php endif; ?>
<?php if ($has_more_details || ($show_report && !$is_own)) : ?>
<div class="aksrt-cover-menu aksrt-cover-menu--author" data-aksrt-menu>
<button type="button" class="aksrt-cover-menu__trigger" aria-haspopup="true" aria-expanded="false" aria-label="<?php esc_attr_e('个人主页操作', 'aksrt'); ?>"><i class="fa fas fa-ellipsis-h" aria-hidden="true"></i></button>
<div class="aksrt-cover-menu__panel" role="menu" hidden>
<?php if ($has_more_details) : ?><button type="button" role="menuitem" data-aksrt-profile-details><i class="fa fas fa-address-card" aria-hidden="true"></i><?php esc_html_e('更多资料', 'aksrt'); ?></button><?php endif; ?>
<?php if ($show_report && !$is_own) : ?>
<?php if (is_user_logged_in()) : ?><button type="button" role="menuitem" data-aksrt-report='{"target_type":"user","target_id":<?php echo (int) $user_id; ?>}'><i class="fa fas fa-flag" aria-hidden="true"></i><?php esc_html_e('举报用户', 'aksrt'); ?></button>
<?php else : ?><a role="menuitem" href="<?php echo esc_url(aksrt_get_login_url($home_url)); ?>"><i class="fa fas fa-flag" aria-hidden="true"></i><?php esc_html_e('举报用户', 'aksrt'); ?></a><?php endif; ?>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
</div>
<?php endif; ?>
<div class="aksrt-author-hero__body">
<?php if ($show_avatar) : ?>
<div class="aksrt-author-hero__avatar">
<?php
if (function_exists('aksrt_get_avatar_html')) {
echo aksrt_get_avatar_html($user_id, 120, ['loading' => 'lazy']); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
} else {
echo get_avatar($user_id, 120, '', $user->display_name); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
?>
</div>
<?php endif; ?>
<div class="aksrt-author-hero__info">
<?php if ($show_name) : ?>
<h1 class="aksrt-author-hero__name">
<?php if (function_exists('aksrt_get_user_name')) : ?>
<?php echo aksrt_get_user_name(['id' => $user_id, 'class' => 'aksrt-author-hero__name-inner', 'vip' => true, 'medal' => false, 'auth' => true, 'level' => true, 'follow' => false]); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php else : ?>
<?php echo esc_html($user->display_name); ?>
<?php endif; ?>
</h1>
<?php endif; ?>
<?php
// legacy parity: the badge overview leads the identity row.
$city_badge = function_exists('aksrt_get_user_city_badge_html')
? aksrt_get_user_city_badge_html($user_id, 'user_home', 'badg')
: '';
$medal_entry = function_exists('aksrt_render_user_medal_entry')
? aksrt_render_user_medal_entry($user_id, 'user-home', $is_own)
: '';
?>
<?php if ($medal_entry !== '' || $city_badge !== '') : ?>
<div class="aksrt-author-hero__identity user-identity flex ac hh">
<?php echo $medal_entry; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php echo $city_badge; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</div>
<?php endif; ?>
<?php if ($show_desc && $user->description) : ?>
<p class="aksrt-author-hero__desc"><?php echo esc_html($user->description); ?></p>
<?php endif; ?>
<?php if (!empty($notice) && is_array($notice)) : ?>
<div class="aksrt-author-hero__notice aksrt-author-hero__notice--<?php echo esc_attr((string) ($notice['tone'] ?? 'muted')); ?>">
<?php echo esc_html((string) ($notice['message'] ?? '')); ?>
</div>
<?php endif; ?>
</div>
<div class="aksrt-author-hero__actions">
<?php if ($is_own) : ?>
<a href="<?php echo esc_url(aksrt_get_user_center_url()); ?>" class="btn-primary btn-sm">
<i class="fa fas fa-user" aria-hidden="true"></i>
<span><?php esc_html_e('用户中心', 'aksrt'); ?></span>
</a>
<?php if ($show_message && $messages_url !== '') : ?>
<a href="<?php echo esc_url($messages_url); ?>" class="btn-ghost btn-sm">
<i class="fa fas fa-bell" aria-hidden="true"></i>
<span><?php esc_html_e('消息', 'aksrt'); ?></span>
</a>
<?php endif; ?>
<?php else : ?>
<?php if ($show_follow) : ?>
<?php if (is_user_logged_in()) : ?>
<button type="button" class="btn-outline btn-sm aksrt-author-hero__follow-btn" data-aksrt-follow="<?php echo (int) $user_id; ?>" title="<?php esc_attr_e('关注该用户', 'aksrt'); ?>">
<i class="fa fas fa-heart" aria-hidden="true"></i>
<span><?php esc_html_e('关注', 'aksrt'); ?></span>
</button>
<?php else : ?>
<a href="<?php echo esc_url(aksrt_get_login_url($home_url)); ?>" class="btn-outline btn-sm aksrt-author-hero__follow-btn">
<i class="fa fas fa-heart" aria-hidden="true"></i>
<span><?php esc_html_e('登录后关注', 'aksrt'); ?></span>
</a>
<?php endif; ?>
<?php endif; ?>
<?php endif; ?>
<?php if ($is_author && function_exists('aksrt_render_admin_quick_link')) { aksrt_render_admin_quick_link('page-display/user-home', __('用户主页设置', 'aksrt')); } ?>
</div>
</div>
</div>
<?php if ($has_more_details) : ?>
<div class="aksrt-profile-details-modal" id="aksrt-profile-details-modal" hidden role="dialog" aria-modal="true" aria-labelledby="aksrt-profile-details-title">
<div class="aksrt-profile-details-modal__backdrop" data-aksrt-modal-close></div>
<div class="aksrt-profile-details-modal__dialog" tabindex="-1">
<button type="button" class="aksrt-profile-details-modal__close" data-aksrt-modal-close aria-label="<?php esc_attr_e('关闭', 'aksrt'); ?>">&times;</button>
<h2 id="aksrt-profile-details-title"><?php esc_html_e('更多资料', 'aksrt'); ?></h2>
<dl>
<?php $verify_state = is_array($dashboard['verify_state'] ?? null) ? $dashboard['verify_state'] : []; ?>
<?php if (($verify_state['state'] ?? '') !== '' && ($verify_state['label'] ?? '') !== '') : ?><div><dt><?php esc_html_e('认证', 'aksrt'); ?></dt><dd><?php echo esc_html((string) $verify_state['label']); ?></dd></div><?php endif; ?>
<?php if (!empty($profile_details['gender'])) : ?><div><dt><?php esc_html_e('性别', 'aksrt'); ?></dt><dd><?php echo esc_html($profile_details['gender'] === 'male' ? __('男', 'aksrt') : __('女', 'aksrt')); ?></dd></div><?php endif; ?>
<?php if (!empty($profile_details['website'])) : ?><div><dt><?php esc_html_e('个人网站', 'aksrt'); ?></dt><dd><a href="<?php echo esc_url($profile_details['website']); ?>" target="_blank" rel="noopener noreferrer"><?php echo esc_html($profile_details['website']); ?></a></dd></div><?php endif; ?>
<?php if (!empty($profile_details['github'])) : ?><div><dt>GitHub</dt><dd><a href="<?php echo esc_url($profile_details['github']); ?>" target="_blank" rel="noopener noreferrer"><?php echo esc_html($profile_details['github']); ?></a></dd></div><?php endif; ?>
<?php if (!empty($profile_details['qq'])) : ?><div><dt>QQ</dt><dd><?php echo esc_html($profile_details['qq']); ?></dd></div><?php endif; ?>
<?php if (!empty($profile_details['wechat'])) : ?><div><dt><?php esc_html_e('微信', 'aksrt'); ?></dt><dd><?php echo esc_html($profile_details['wechat']); ?></dd></div><?php endif; ?>
<?php if ($registered_time !== '') : ?><div><dt><?php esc_html_e('注册时间', 'aksrt'); ?></dt><dd><?php echo esc_html($registered_time); ?></dd></div><?php endif; ?>
<?php if ($last_login_time !== '') : ?><div><dt><?php esc_html_e('最后登录', 'aksrt'); ?></dt><dd><?php echo esc_html($last_login_time); ?></dd></div><?php endif; ?>
</dl>
</div>
</div>
<?php endif; ?>