fix(user): align badge entry with medal settings

This commit is contained in:
kate522 committed 2026-08-11 19:32:52 +08:00
1 parent e1dc9ab277
commit 51b52affae
7 files changed
+143 -25

No files matched your search

+57
View File
@@ -234,6 +234,63 @@ if (!function_exists('aksrt_get_user_name')) {
}
}
if (!function_exists('aksrt_render_user_medal_entry')) {
/**
* Render the compact Zibll-style badge overview entry.
*
* The configured limit controls preview icons only; the count remains
* the complete owned badge count returned by the privacy-aware DTO.
*
* @param int $user_id
* @param string $context user-center|user-home
* @param bool $can_wear Whether the viewer may initially wear a badge.
*/
function aksrt_render_user_medal_entry(int $user_id, string $context = 'user-home', bool $can_wear = false): string {
if ($user_id <= 0 || !class_exists('AKSRT\\User\\Badges') || !class_exists('AKSRT\\Settings\\Settings')) {
return '';
}
$settings = \AKSRT\Settings\Settings::get_user_identity_settings();
$medal = is_array($settings['medal'] ?? null) ? $settings['medal'] : [];
if (empty($medal['enabled']) || ($context === 'user-home' && empty($medal['public_display']))) {
return '';
}
$data = \AKSRT\User\Badges::list_for_user($user_id);
if (empty($data['enabled']) || ($context === 'user-home' && empty($data['public_display']))) {
return '';
}
$limit = max(1, min(8, (int) ($medal['limit'] ?? 3)));
$icons = [];
foreach ((array) ($data['categories'] ?? []) as $category) {
foreach ((array) ($category['items'] ?? []) as $item) {
if (empty($item['owned'])) {
continue;
}
$icon_url = esc_url((string) ($item['icon_url'] ?? ''));
if ($icon_url === '') {
continue;
}
$label = sanitize_text_field((string) ($item['label'] ?? __('徽章', 'aksrt')));
$icons[] = '<img class="img-icon medal-icon aksrt-medal-entry__icon" src="' . $icon_url . '" alt="' . esc_attr($label) . '" title="' . esc_attr($label) . '" loading="lazy" decoding="async" />';
if (count($icons) >= $limit) {
break 2;
}
}
}
$count = max(0, (int) ($data['count'] ?? 0));
$label = $count > 0 ? sprintf(_n('%d枚徽章', '%d枚徽章', $count, 'aksrt'), $count) : __('获取徽章', 'aksrt');
$icons_html = implode('', $icons);
if ($icons_html === '') {
$icons_html = '<span class="aksrt-medal-entry__fallback" aria-hidden="true"><i class="fas fa-award"></i></span>';
}
$classes = 'aksrt-medal-entry aksrt-medal-entry--' . sanitize_html_class($context);
return '<button type="button" class="' . esc_attr($classes) . '" data-aksrt-badges-open data-aksrt-badges-user-id="' . (int) $user_id . '" data-aksrt-badges-can-wear="' . ($can_wear ? '1' : '0') . '" aria-label="' . esc_attr($label) . '"><span class="aksrt-medal-entry__icons">' . $icons_html . '</span><span class="aksrt-medal-entry__count">' . esc_html($label) . '</span><i class="fas fa-angle-right aksrt-medal-entry__arrow" aria-hidden="true"></i></button>';
}
}
if (!function_exists('aksrt_user_has_visible_badges')) {
/**
* Whether a user owns at least one badge that exists in the current catalog.
@@ -41,6 +41,9 @@ extract($context, EXTR_SKIP); // phpcs:ignore WordPress.PHP.DontExtract.extract_
<?php endif; ?>
</h1>
<div class="uc-header__meta desc user-identity flex ac hh">
<?php if (function_exists('aksrt_render_user_medal_entry')) : ?>
<?php echo aksrt_render_user_medal_entry((int) $user_id, 'user-center', true); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php endif; ?>
<span class="uc-header__meta-item" title="<?php esc_attr_e('用户名', 'aksrt'); ?>">
<i class="fas fa-user" aria-hidden="true"></i> <?php echo esc_html($target_user->user_login); ?>
</span>
@@ -61,12 +64,6 @@ extract($context, EXTR_SKIP); // phpcs:ignore WordPress.PHP.DontExtract.extract_
</div>
</div>
<div class="uc-header__actions">
<?php if (class_exists('\AKSRT\User\Badges') && \AKSRT\User\Badges::enabled()) : ?>
<button type="button" class="btn-ghost btn-sm" data-aksrt-badges-open data-aksrt-badges-user-id="<?php echo (int) $user_id; ?>" data-aksrt-badges-can-wear="1">
<i class="fas fa-award" aria-hidden="true"></i> <?php esc_html_e('我的徽章', 'aksrt'); ?>
</button>
<?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'); ?>