Files
wordpress-theme-aksrt/inc/classes/user/class-aksrt-user-badges-admin.php
T
kate522 ce1c13b947 refactor(classes): organize inc/classes into domain folders
Move ~94 root class shells and root *-traits into domain directories
(core, auth, user, credits, content, payment/gateways, forum, shop,
sms, ai, messages, extensions, widgets, settings, admin). Update
bootstrap require paths, co-locate traits with owners, fix forum/user
relative requires, drop duplicate shop controller requires, and wire
home-modules load. Path-only; class names and init order unchanged.
2026-07-17 11:11:57 +08:00

124 lines
4.4 KiB
PHP

<?php
/**
* Admin grant/revoke badges on WP user profile screens.
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
class AKSRT_User_Badges_Admin {
private static $instance = null;
private function __construct() {}
public static function get_instance(): self {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
public function init(): void {
if (!is_admin()) {
return;
}
add_action('show_user_profile', [$this, 'render_section']);
add_action('edit_user_profile', [$this, 'render_section']);
add_action('personal_options_update', [$this, 'handle_update']);
add_action('edit_user_profile_update', [$this, 'handle_update']);
}
private function can_manage(): bool {
return class_exists('AKSRT_User_Badges') && AKSRT_User_Badges::can_manually_set(get_current_user_id());
}
public function render_section(WP_User $user): void {
if (!class_exists('AKSRT_User_Badges') || !AKSRT_User_Badges::enabled()) {
return;
}
$dict = AKSRT_User_Badges::get_dictionary();
if (!$dict) {
return;
}
$owned = AKSRT_User_Badges::get_owned((int) $user->ID);
$wear = AKSRT_User_Badges::get_wear((int) $user->ID);
$can = $this->can_manage();
?>
<a id="aksrt-user-badges"></a>
<h2><?php esc_html_e('AKSRT 徽章', 'aksrt'); ?></h2>
<table class="form-table" role="presentation">
<tr>
<th><label><?php esc_html_e('已授予徽章', 'aksrt'); ?></label></th>
<td>
<?php if (!$can) : ?>
<p class="description"><?php esc_html_e('当前账号无权授予/收回徽章。', 'aksrt'); ?></p>
<?php endif; ?>
<fieldset <?php disabled(!$can); ?>>
<?php foreach ($dict as $item) : ?>
<?php
$slug = $item['slug'];
$checked = in_array($slug, $owned, true);
?>
<label style="display:inline-flex;align-items:center;gap:6px;margin:0 16px 10px 0;">
<input type="checkbox" name="aksrt_user_badges[]" value="<?php echo esc_attr($slug); ?>" <?php checked($checked); ?> <?php disabled(!$can); ?>>
<?php if (!empty($item['icon_url'])) : ?>
<img src="<?php echo esc_url($item['icon_url']); ?>" alt="" width="18" height="18" style="object-fit:contain;vertical-align:middle;">
<?php endif; ?>
<span><?php echo esc_html($item['label']); ?> <code><?php echo esc_html($slug); ?></code></span>
</label>
<?php endforeach; ?>
</fieldset>
<p class="description"><?php esc_html_e('勾选即授予;取消勾选并保存即收回。', 'aksrt'); ?></p>
</td>
</tr>
<tr>
<th><label for="aksrt_wear_badge"><?php esc_html_e('当前佩戴', 'aksrt'); ?></label></th>
<td>
<select name="aksrt_wear_badge" id="aksrt_wear_badge" <?php disabled(!$can); ?>>
<option value=""><?php esc_html_e('(未佩戴)', 'aksrt'); ?></option>
<?php foreach ($dict as $item) : ?>
<?php if (!in_array($item['slug'], $owned, true) && $item['slug'] !== $wear) : ?>
<?php continue; ?>
<?php endif; ?>
<option value="<?php echo esc_attr($item['slug']); ?>" <?php selected($wear, $item['slug']); ?>>
<?php echo esc_html($item['label']); ?>
</option>
<?php endforeach; ?>
</select>
<p class="description"><?php esc_html_e('仅可佩戴已授予徽章;用户也可在前台自行佩戴。', 'aksrt'); ?></p>
</td>
</tr>
</table>
<?php
wp_nonce_field('aksrt_user_badges_admin', 'aksrt_user_badges_nonce');
}
public function handle_update(int $user_id): void {
if (!$this->can_manage()) {
return;
}
if (!isset($_POST['aksrt_user_badges_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash((string) $_POST['aksrt_user_badges_nonce'])), 'aksrt_user_badges_admin')) {
return;
}
if (!class_exists('AKSRT_User_Badges') || !AKSRT_User_Badges::enabled()) {
return;
}
$raw = isset($_POST['aksrt_user_badges']) ? (array) wp_unslash($_POST['aksrt_user_badges']) : [];
$slugs = array_map('sanitize_key', $raw);
AKSRT_User_Badges::set_owned($user_id, $slugs);
$wear = isset($_POST['aksrt_wear_badge']) ? sanitize_key((string) wp_unslash($_POST['aksrt_wear_badge'])) : '';
if ($wear === '') {
delete_user_meta($user_id, AKSRT_User_Badges::META_WEAR);
} else {
$res = AKSRT_User_Badges::wear($user_id, $wear);
if (is_wp_error($res)) {
// leave owned as-is; wear ignored if invalid
delete_user_meta($user_id, AKSRT_User_Badges::META_WEAR);
}
}
}
}