fix(badges): hide "我的徽章" button until user has visible badges

profile-header.php was rendering two badge buttons ("我的徽章"
opening the local modal and "徽章弹层" opening a remote modal).
Drop the remote one and gate the local one behind the new
aksrt_user_has_visible_badges() helper, which only returns true
when the user actually has at least one badge entry that exists
in the active catalog dictionary — preventing a useless empty
modal when nothing can be shown.
This commit is contained in:
2026-08-04 10:51:20 +08:00
parent e991f1a0c6
commit 3a33f7cd7f
2 changed files with 37 additions and 11 deletions
+32
View File
@@ -229,6 +229,38 @@ if (!function_exists('aksrt_get_user_name')) {
}
}
if (!function_exists('aksrt_user_has_visible_badges')) {
/**
* Whether a user owns at least one badge that exists in the current catalog.
*
* Used by user-center surfaces to decide whether to show the "我的徽章"
* button. Prevents rendering a button that opens an empty modal when the
* badge dictionary has no entries or none of the user's badges match it.
*
* @param int $user_id Target user (0 = current user).
* @return bool
*/
function aksrt_user_has_visible_badges(int $user_id = 0): bool {
if ($user_id <= 0) {
$user_id = get_current_user_id();
}
if ($user_id <= 0 || !class_exists('AKSRT\\User\\Badges')) {
return false;
}
$map = \AKSRT\User\Badges::get_dictionary_map();
if (empty($map)) {
return false;
}
$owned = \AKSRT\User\Badges::get_owned($user_id);
foreach ((array) $owned as $slug) {
if (is_string($slug) && $slug !== '' && isset($map[$slug])) {
return true;
}
}
return false;
}
}
if (!function_exists('aksrt_get_worn_medal_icon')) {
/**
* Render the worn medal icon HTML for a user (standalone, zibll parity).
@@ -86,17 +86,11 @@ 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>
<i class="fas fa-award" aria-hidden="true"></i> <?php esc_html_e('我的徽章', 'aksrt'); ?>
</button>
<?php
$remote_badge = \AKSRT\User\Badges::get_info_link((int) $user_id, 'btn-ghost btn-sm', __('徽章弹层', 'aksrt'));
if ($remote_badge !== '') {
echo $remote_badge; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
?>
<?php endif; ?>
<?php if (function_exists('aksrt_user_has_visible_badges') && aksrt_user_has_visible_badges((int) $user_id)) : ?>
<button type="button" class="btn-ghost btn-sm" data-aksrt-badges-open>
<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">