feat(user-center): deepen zibll parity and profile media
This commit is contained in:
1 parent
c13f686cdc
commit
e47ee1b029
16 files changed
+479
-65
No files matched your search
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
/**
|
||||
* Unified frontend avatar helpers.
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
if (!function_exists('aksrt_resolve_avatar_user_id')) {
|
||||
/**
|
||||
* Resolve a WordPress user ID from an avatar subject.
|
||||
*
|
||||
* @param mixed $subject User ID, WP_User, comment, post, or email.
|
||||
*/
|
||||
function aksrt_resolve_avatar_user_id($subject): int {
|
||||
if ($subject instanceof WP_User) {
|
||||
return (int) $subject->ID;
|
||||
}
|
||||
if ($subject instanceof WP_Post) {
|
||||
return (int) $subject->post_author;
|
||||
}
|
||||
if ($subject instanceof WP_Comment) {
|
||||
return (int) $subject->user_id;
|
||||
}
|
||||
if (is_object($subject) && isset($subject->user_id)) {
|
||||
return (int) $subject->user_id;
|
||||
}
|
||||
if (is_numeric($subject)) {
|
||||
return max(0, (int) $subject);
|
||||
}
|
||||
if (is_string($subject) && is_email($subject)) {
|
||||
$user = get_user_by('email', $subject);
|
||||
return $user instanceof WP_User ? (int) $user->ID : 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('aksrt_get_custom_avatar_url')) {
|
||||
/**
|
||||
* Return the locally uploaded or OAuth avatar URL.
|
||||
*/
|
||||
function aksrt_get_custom_avatar_url(int $user_id, int $size = 96): string {
|
||||
if ($user_id <= 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$attachment_id = (int) get_user_meta($user_id, 'aksrt_avatar_attachment_id', true);
|
||||
if ($attachment_id > 0) {
|
||||
$image = wp_get_attachment_image_url($attachment_id, [$size, $size]);
|
||||
if (is_string($image) && $image !== '') {
|
||||
return $image;
|
||||
}
|
||||
}
|
||||
|
||||
$oauth_avatar = esc_url_raw((string) get_user_meta($user_id, 'aksrt_oauth_avatar', true));
|
||||
return $oauth_avatar;
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('aksrt_get_avatar_url')) {
|
||||
/**
|
||||
* Resolve the canonical avatar URL used by all AKSRT frontend surfaces.
|
||||
*
|
||||
* @param mixed $subject User subject accepted by get_avatar_url().
|
||||
* @param array|int $args WordPress avatar args or a numeric size.
|
||||
*/
|
||||
function aksrt_get_avatar_url($subject, $args = []): string {
|
||||
if (is_numeric($args)) {
|
||||
$args = ['size' => (int) $args];
|
||||
}
|
||||
$args = is_array($args) ? $args : [];
|
||||
$size = max(16, min(512, (int) ($args['size'] ?? 96)));
|
||||
$user_id = aksrt_resolve_avatar_user_id($subject);
|
||||
$custom = aksrt_get_custom_avatar_url($user_id, $size);
|
||||
if ($custom !== '') {
|
||||
return $custom;
|
||||
}
|
||||
return (string) get_avatar_url($subject, array_merge($args, ['size' => $size]));
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('aksrt_get_avatar_html')) {
|
||||
/**
|
||||
* Render a canonical avatar image.
|
||||
*
|
||||
* @param mixed $subject User subject.
|
||||
* @param int $size Pixel size.
|
||||
* @param array<string,mixed> $attrs Additional safe image attributes.
|
||||
*/
|
||||
function aksrt_get_avatar_html($subject, int $size = 96, array $attrs = []): string {
|
||||
$size = max(16, min(512, $size));
|
||||
$url = aksrt_get_avatar_url($subject, ['size' => $size]);
|
||||
if ($url === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
$defaults = [
|
||||
'class' => 'avatar avatar-' . $size . ' photo',
|
||||
'alt' => '',
|
||||
'width' => (string) $size,
|
||||
'height' => (string) $size,
|
||||
'loading' => 'lazy',
|
||||
];
|
||||
$attrs = array_merge($defaults, $attrs);
|
||||
$html_attrs = '';
|
||||
foreach ($attrs as $name => $value) {
|
||||
$name = sanitize_key((string) $name);
|
||||
if ($name === '' || $value === false || $value === null) {
|
||||
continue;
|
||||
}
|
||||
$html_attrs .= ' ' . $name . '="' . esc_attr((string) $value) . '"';
|
||||
}
|
||||
return '<img src="' . esc_url($url) . '"' . $html_attrs . ' />';
|
||||
}
|
||||
}
|
||||
|
||||
add_filter('get_avatar_data', static function (array $args, $subject): array {
|
||||
$user_id = aksrt_resolve_avatar_user_id($subject);
|
||||
$custom = aksrt_get_custom_avatar_url($user_id, (int) ($args['size'] ?? 96));
|
||||
if ($custom !== '') {
|
||||
$args['url'] = $custom;
|
||||
$args['found_avatar'] = true;
|
||||
}
|
||||
return $args;
|
||||
}, 20, 2);
|
||||
@@ -388,6 +388,32 @@ if (!function_exists('aksrt_user_center_runtime_config')) {
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('aksrt_user_center_avatar_upload_config')) {
|
||||
/**
|
||||
* Avatar upload config for the private user center.
|
||||
*/
|
||||
function aksrt_user_center_avatar_upload_config(bool $enabled): array {
|
||||
if (!$enabled) {
|
||||
return [];
|
||||
}
|
||||
return [
|
||||
'enabled' => true,
|
||||
'restUrl' => rest_url('aksrt/v1/user/avatar'),
|
||||
'restNonce' => wp_create_nonce('wp_rest'),
|
||||
'buttonId' => 'aksrt-upload-avatar-btn',
|
||||
'inputId' => 'aksrt-avatar-file-input',
|
||||
'imageId' => 'aksrt-user-avatar-image',
|
||||
'fieldName' => 'avatar',
|
||||
'maxSize' => 2 * MB_IN_BYTES,
|
||||
'idleText' => __('更换头像', 'aksrt'),
|
||||
'loadingText' => __('上传中…', 'aksrt'),
|
||||
'tooLargeText' => __('图片大小不能超过 2MB', 'aksrt'),
|
||||
'successText' => __('头像已更新', 'aksrt'),
|
||||
'errorText' => __('头像上传失败', 'aksrt'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('aksrt_user_center_cover_upload_config')) {
|
||||
/**
|
||||
* Cover upload config for the private user-center hero.
|
||||
@@ -450,7 +476,9 @@ if (!function_exists('aksrt_get_user_center_render_context')) {
|
||||
$user_contacts = $dashboard['user_contacts'] ?? get_user_meta($user_id, 'aksrt_user_contacts', true);
|
||||
$user_contacts = is_array($user_contacts) ? $user_contacts : [];
|
||||
$user_background_url = (string) ($dashboard['background_url'] ?? get_user_meta($user_id, 'aksrt_background_image', true));
|
||||
$allow_avatar_upload = !empty($user_center_settings['avatar_upload']);
|
||||
$allow_bg_upload = !empty($user_center_settings['cover_upload']) && class_exists('\AKSRT\Settings\Settings') && aksrt_get_setting('allow_background_upload', false);
|
||||
$messages_enabled = !class_exists('\AKSRT\Settings\Settings') || \AKSRT\Settings\Settings::message_enabled();
|
||||
|
||||
$post_status_filter = isset($_GET['post_status']) ? sanitize_key(wp_unslash($_GET['post_status'])) : 'publish';
|
||||
if (!in_array($post_status_filter, ['all', 'publish', 'draft', 'pending', 'private'], true)) {
|
||||
@@ -472,6 +500,23 @@ if (!function_exists('aksrt_get_user_center_render_context')) {
|
||||
$currency_name = (string) ($dashboard['currency_name'] ?? __('积分', 'aksrt'));
|
||||
$balance_enabled = (bool) ($dashboard['balance_enabled'] ?? false);
|
||||
$wallet_balance = (float) ($dashboard['wallet_balance'] ?? 0.0);
|
||||
$runtime_config = aksrt_user_center_runtime_config($user_id, $tabs['default'], $active_tab, $tabs['urls']);
|
||||
$runtime_config['avatarUpload'] = aksrt_user_center_avatar_upload_config($allow_avatar_upload);
|
||||
$shop_settings = class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\Settings::get_shop_settings() : [];
|
||||
$forum_settings = class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\Settings::get_forum_settings() : [];
|
||||
$revenue_settings = class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\Settings::get_payment_revenue_settings() : [];
|
||||
$shop_enabled = !empty($shop_settings['enabled']);
|
||||
$forum_enabled = !empty($forum_settings['enabled']);
|
||||
$new_post_url = function_exists('aksrt_get_new_post_url') ? aksrt_get_new_post_url() : '';
|
||||
$new_product_url = $shop_enabled && function_exists('aksrt_get_page_url_by_template')
|
||||
? aksrt_get_page_url_by_template('templates/pages/page-product-edit.php')
|
||||
: '';
|
||||
$forum_compose_url = $forum_enabled && function_exists('aksrt_get_page_url_by_template')
|
||||
? aksrt_get_page_url_by_template('templates/forum/forum-compose.php')
|
||||
: '';
|
||||
$revenue_url = (!empty($revenue_settings['enabled']) || !empty($affiliate_settings['enabled'])) && function_exists('aksrt_get_revenue_page_url')
|
||||
? aksrt_get_revenue_page_url()
|
||||
: '';
|
||||
|
||||
$render_context = [
|
||||
'user_center_settings' => $user_center_settings,
|
||||
@@ -490,10 +535,13 @@ if (!function_exists('aksrt_get_user_center_render_context')) {
|
||||
'profile_signals' => $dashboard['profile_signals'] ?? [],
|
||||
'user_background_url' => $user_background_url,
|
||||
'allow_bg_upload' => $allow_bg_upload,
|
||||
'show_avatar_edit' => !empty($user_center_settings['avatar_upload']),
|
||||
'avatar_settings_url' => admin_url('profile.php'),
|
||||
'show_avatar_edit' => $allow_avatar_upload,
|
||||
'notification_unread_count' => function_exists('aksrt_get_user_unread_notification_count') ? aksrt_get_user_unread_notification_count($user_id) : 0,
|
||||
'messages_url' => function_exists('aksrt_get_messages_page_url') ? aksrt_get_messages_page_url() : '',
|
||||
'messages_url' => $messages_enabled && function_exists('aksrt_get_messages_page_url') ? aksrt_get_messages_page_url() : '',
|
||||
'new_post_url' => $new_post_url,
|
||||
'new_product_url' => $new_product_url,
|
||||
'forum_compose_url' => $forum_compose_url,
|
||||
'revenue_url' => $revenue_url,
|
||||
'per_page' => 10,
|
||||
'posts_page' => max(1, isset($_GET['posts_page']) ? (int) $_GET['posts_page'] : 1),
|
||||
'comments_page' => max(1, isset($_GET['comments_page']) ? (int) $_GET['comments_page'] : 1),
|
||||
@@ -520,7 +568,7 @@ if (!function_exists('aksrt_get_user_center_render_context')) {
|
||||
&& \AKSRT\Settings\Settings::invite_enabled()
|
||||
&& \AKSRT\Settings\Settings::invite_registration_enabled(),
|
||||
'user_email' => (string) $current_user->user_email,
|
||||
'runtime_config' => aksrt_user_center_runtime_config($user_id, $tabs['default'], $active_tab, $tabs['urls']),
|
||||
'runtime_config' => $runtime_config,
|
||||
'cover_upload_config' => aksrt_user_center_cover_upload_config($allow_bg_upload),
|
||||
];
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ extract($context, EXTR_SKIP); // phpcs:ignore WordPress.PHP.DontExtract.extract_
|
||||
</div>
|
||||
<div class="uc-header__content">
|
||||
<div class="uc-header__avatar">
|
||||
<?php echo get_avatar($user_id, 120, '', $target_user->display_name); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
|
||||
<?php echo aksrt_get_avatar_html($user_id, 120, ['id' => 'aksrt-user-avatar-image', 'alt' => $target_user->display_name, 'loading' => 'eager']); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
|
||||
</div>
|
||||
<div class="uc-header__info">
|
||||
<h1 class="uc-header__name">
|
||||
@@ -82,16 +82,19 @@ extract($context, EXTR_SKIP); // phpcs:ignore WordPress.PHP.DontExtract.extract_
|
||||
?>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if (!empty($messages_url)) : ?>
|
||||
<a href="<?php echo esc_url($messages_url); ?>" class="btn-ghost btn-sm">
|
||||
<i class="fas fa-bell" aria-hidden="true"></i> <?php esc_html_e('消息通知', 'aksrt'); ?>
|
||||
<?php if ($notification_unread_count > 0) : ?>
|
||||
<span class="uc-header__action-badge"><?php echo esc_html((string) $notification_unread_count); ?></span>
|
||||
<?php endif; ?>
|
||||
</a>
|
||||
<?php endif; ?>
|
||||
<?php if ($show_avatar_edit) : ?>
|
||||
<a href="<?php echo esc_url($avatar_settings_url); ?>" class="btn-ghost btn-sm">
|
||||
<i class="fas fa-camera" aria-hidden="true"></i> <?php esc_html_e('更换头像', 'aksrt'); ?>
|
||||
</a>
|
||||
<button type="button" id="aksrt-upload-avatar-btn" class="btn-ghost btn-sm">
|
||||
<i class="fas fa-camera" aria-hidden="true"></i> <span><?php esc_html_e('更换头像', 'aksrt'); ?></span>
|
||||
</button>
|
||||
<input type="file" id="aksrt-avatar-file-input" accept="image/jpeg,image/png,image/webp,image/gif" hidden>
|
||||
<?php endif; ?>
|
||||
<a href="<?php echo esc_url(aksrt_get_user_home_url()); ?>" class="btn-ghost btn-sm">
|
||||
<i class="fas fa-map-marker-alt" aria-hidden="true"></i> <?php esc_html_e('我的主页', 'aksrt'); ?>
|
||||
|
||||
@@ -12,6 +12,7 @@ extract($context, EXTR_SKIP); // phpcs:ignore WordPress.PHP.DontExtract.extract_
|
||||
|
||||
require __DIR__ . '/sidebar-stats.php';
|
||||
require __DIR__ . '/sidebar-nav.php';
|
||||
require __DIR__ . '/sidebar-creator.php';
|
||||
require __DIR__ . '/sidebar-commerce.php';
|
||||
?>
|
||||
<button type="button" class="uc-sidebar__drawer-trigger" id="uc-drawer-trigger" aria-label="<?php esc_attr_e('打开导航', 'aksrt'); ?>">
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
/**
|
||||
* User center sidebar: creator and community shortcuts.
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
$creator_links = array_filter([
|
||||
[
|
||||
'url' => (string) ($new_post_url ?? ''),
|
||||
'icon' => 'pen',
|
||||
'label' => __('发布文章', 'aksrt'),
|
||||
],
|
||||
[
|
||||
'url' => (string) ($new_product_url ?? ''),
|
||||
'icon' => 'store',
|
||||
'label' => __('发布商品', 'aksrt'),
|
||||
],
|
||||
[
|
||||
'url' => (string) ($forum_compose_url ?? ''),
|
||||
'icon' => 'comments',
|
||||
'label' => __('论坛发帖', 'aksrt'),
|
||||
],
|
||||
[
|
||||
'url' => (string) ($revenue_url ?? ''),
|
||||
'icon' => 'chart-line',
|
||||
'label' => __('创作收益', 'aksrt'),
|
||||
],
|
||||
], static fn(array $item): bool => $item['url'] !== '');
|
||||
|
||||
if (!$creator_links) {
|
||||
return;
|
||||
}
|
||||
?>
|
||||
<div class="uc-sidebar__section">
|
||||
<div class="uc-sidebar__title"><?php esc_html_e('创作服务', 'aksrt'); ?></div>
|
||||
<div class="uc-sidebar__buttons">
|
||||
<?php foreach ($creator_links as $item) : ?>
|
||||
<a class="uc-sidebar__btn" href="<?php echo esc_url($item['url']); ?>">
|
||||
<i class="fas fa-<?php echo esc_attr($item['icon']); ?>" aria-hidden="true"></i>
|
||||
<span><?php echo esc_html($item['label']); ?></span>
|
||||
</a>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
</div>
|
||||
@@ -23,6 +23,7 @@ defined('ABSPATH') || exit;
|
||||
<span><?php echo esc_html($tab['label']); ?></span>
|
||||
</a>
|
||||
<?php endforeach; ?>
|
||||
<?php if (!empty($messages_url)) : ?>
|
||||
<a href="<?php echo esc_url($messages_url); ?>" class="uc-sidebar__btn">
|
||||
<i class="fas fa-bell" aria-hidden="true"></i>
|
||||
<span><?php esc_html_e('消息通知', 'aksrt'); ?></span>
|
||||
@@ -30,5 +31,6 @@ defined('ABSPATH') || exit;
|
||||
<span class="uc-sidebar__badge"><?php echo esc_html((string) $notification_unread_count); ?></span>
|
||||
<?php endif; ?>
|
||||
</a>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
@@ -24,9 +24,11 @@ defined('ABSPATH') || exit;
|
||||
<span class="uc-stats__label"><?php echo esc_html($dashboard_tabs[$stat_tab]['label']); ?></span>
|
||||
</a>
|
||||
<?php endforeach; ?>
|
||||
<?php if (!empty($messages_url)) : ?>
|
||||
<a class="uc-stats__item" href="<?php echo esc_url($messages_url); ?>">
|
||||
<span class="uc-stats__value"><?php echo esc_html((string) $notification_unread_count); ?></span>
|
||||
<span class="uc-stats__label"><?php esc_html_e('通知', 'aksrt'); ?></span>
|
||||
</a>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
Reference in new issue
Block a user