count_verify_status('pending');
if ($pending_verify > 0) {
$url = admin_url('users.php?page=aksrt_user_auth&status=pending');
echo '
' . esc_html__('用户身份认证申请待处理', 'aksrt') . ' ';
echo '
' . esc_html(sprintf(
/* translators: %d count */
__('您有 %d 个用户身份认证申请待处理', 'aksrt'),
$pending_verify
)) . '
';
echo '
' . esc_html__('立即处理', 'aksrt') . '
';
}
}
if ($page !== 'aksrt_user_ban' && class_exists('AKSRT_Report_Center')) {
$pending_reports = (int) AKSRT_Report_Center::get_instance()->get_pending_count();
if ($pending_reports > 0) {
$url = admin_url('users.php?page=aksrt_user_ban&tab=reports&status=pending');
echo '' . esc_html__('举报待处理', 'aksrt') . ' ';
echo '
' . esc_html(sprintf(
/* translators: %d count */
__('您有 %d 条举报待处理', 'aksrt'),
$pending_reports
)) . '
';
echo '
' . esc_html__('立即处理', 'aksrt') . '
';
}
}
}
private function ensure_cap(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('您没有权限访问此页面。', 'aksrt'));
}
}
private function flash(): void {
if (empty($_GET['aksrt_users_msg'])) {
return;
}
$code = sanitize_key((string) wp_unslash($_GET['aksrt_users_msg']));
$map = [
'message_sent' => __('消息已发送。', 'aksrt'),
'message_failed' => __('消息发送失败。', 'aksrt'),
'verify_updated' => __('认证状态已更新。', 'aksrt'),
'verify_failed' => __('认证审核失败。', 'aksrt'),
'report_updated' => __('举报状态已更新。', 'aksrt'),
'report_failed' => __('举报更新失败。', 'aksrt'),
'restriction_added' => __('限制已添加。', 'aksrt'),
'restriction_removed' => __('限制已移除。', 'aksrt'),
'restriction_failed'=> __('限制操作失败。', 'aksrt'),
];
if (!isset($map[$code])) {
return;
}
$class = str_contains($code, 'failed') ? 'notice-error' : 'notice-success';
echo '' . esc_html($map[$code]) . '
';
}
private function redirect(string $page, string $msg, array $extra = []): void {
wp_safe_redirect(add_query_arg(array_merge(['page' => $page, 'aksrt_users_msg' => $msg], $extra), admin_url('users.php')));
exit;
}
/* ------------------------------------------------------------------
* Messages
* ----------------------------------------------------------------*/
public function render_messages_page(): void {
$this->ensure_cap();
echo '' . esc_html__('消息管理', 'aksrt') . ' ';
$this->flash();
echo '
' . esc_html__('发送系统消息', 'aksrt') . ' ';
echo '
';
$page_num = max(1, (int) ($_GET['paged'] ?? 1));
$per_page = 30;
$offset = ($page_num - 1) * $per_page;
$table = class_exists('AKSRT_DB_Migrator') ? AKSRT_DB_Migrator::table('notification') : '';
if ($table === '') {
echo '
' . esc_html__('通知表不可用。', 'aksrt') . '
';
return;
}
global $wpdb;
$total = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$table}");
$items = $wpdb->get_results($wpdb->prepare(
"SELECT * FROM {$table} ORDER BY created_at DESC LIMIT %d OFFSET %d",
$per_page,
$offset
), ARRAY_A);
$pages = max(1, (int) ceil(max(1, $total) / $per_page));
echo '' . esc_html__('最近消息', 'aksrt') . ' ';
echo '';
echo 'ID ' . esc_html__('用户', 'aksrt') . ' ' . esc_html__('类型', 'aksrt') . ' ' . esc_html__('标题', 'aksrt') . ' ' . esc_html__('已读', 'aksrt') . ' ' . esc_html__('时间', 'aksrt') . ' ';
echo ' ';
if (!$items) {
echo '' . esc_html__('暂无消息', 'aksrt') . ' ';
}
foreach ((array) $items as $row) {
$user_id = (int) ($row['user_id'] ?? 0);
$user = $user_id > 0 ? get_userdata($user_id) : null;
echo '';
echo '' . esc_html((string) ($row['id'] ?? '')) . ' ';
echo '' . ($user ? esc_html($user->display_name . ' (#' . $user_id . ')') : esc_html((string) $user_id)) . ' ';
echo '' . esc_html((string) ($row['type'] ?? '')) . ' ';
echo '' . esc_html((string) ($row['title'] ?? $row['description'] ?? '')) . ' ';
echo '' . (!empty($row['is_read']) ? esc_html__('是', 'aksrt') : esc_html__('否', 'aksrt')) . ' ';
echo '' . esc_html((string) ($row['created_at'] ?? '')) . ' ';
echo ' ';
}
echo '
';
$this->pagination($pages, $page_num);
echo '';
}
public function handle_send_message(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('权限不足', 'aksrt'));
}
check_admin_referer('aksrt_users_send_message');
$user_id = isset($_POST['user_id']) ? (int) $_POST['user_id'] : 0;
$title = isset($_POST['title']) ? sanitize_text_field((string) wp_unslash($_POST['title'])) : '';
$content = isset($_POST['content']) ? sanitize_textarea_field((string) wp_unslash($_POST['content'])) : '';
if ($user_id <= 0) {
$user_id = get_current_user_id();
}
$ok = false;
if ($content !== '' && class_exists('AKSRT_Message')) {
$ok = AKSRT_Message::get_instance()->send($user_id, 'system', $content, ['title' => $title !== '' ? $title : $content]);
}
$this->redirect('aksrt_messages', $ok ? 'message_sent' : 'message_failed');
}
/* ------------------------------------------------------------------
* Invite codes
* ----------------------------------------------------------------*/
public function render_invite_codes_page(): void {
$this->ensure_cap();
echo '' . esc_html__('邀请码管理', 'aksrt') . ' ';
$this->flash();
$spa = admin_url('admin.php?page=aksrt-settings#/users/invite-codes');
echo '
' . esc_html__('SPA 批量生成(过渡)', 'aksrt') . '
';
global $wpdb;
$rows = $wpdb->get_results(
"SELECT user_id, meta_value AS invite_code FROM {$wpdb->usermeta} WHERE meta_key = 'aksrt_invite_code' ORDER BY umeta_id DESC LIMIT 200",
ARRAY_A
);
echo '
' . esc_html__('用户邀请码', 'aksrt') . ' ';
echo '
';
echo '' . esc_html__('用户', 'aksrt') . ' ' . esc_html__('邀请码', 'aksrt') . ' ' . esc_html__('邀请人数', 'aksrt') . ' ';
echo ' ';
if (!$rows) {
echo '' . esc_html__('暂无用户邀请码', 'aksrt') . ' ';
}
$invite_table = class_exists('AKSRT_DB_Migrator') ? AKSRT_DB_Migrator::table('invite') : '';
foreach ((array) $rows as $row) {
$user_id = (int) ($row['user_id'] ?? 0);
$user = $user_id > 0 ? get_userdata($user_id) : null;
$count = 0;
if ($invite_table) {
$count = (int) $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM {$invite_table} WHERE inviter_id = %d",
$user_id
));
}
echo '';
echo '' . ($user ? esc_html($user->display_name . ' (#' . $user_id . ')') : esc_html((string) $user_id)) . ' ';
echo '' . esc_html((string) ($row['invite_code'] ?? '')) . ' ';
echo '' . esc_html((string) $count) . ' ';
echo ' ';
}
echo '
';
}
/* ------------------------------------------------------------------
* Verify
* ----------------------------------------------------------------*/
private function count_verify_status(string $status): int {
global $wpdb;
return (int) $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM {$wpdb->usermeta} WHERE meta_key = 'aksrt_user_verify_status' AND meta_value = %s",
$status
));
}
public function render_verify_page(): void {
$this->ensure_cap();
echo '' . esc_html__('身份认证', 'aksrt') . ' ';
$this->flash();
$status = isset($_GET['status']) ? sanitize_key((string) wp_unslash($_GET['status'])) : 'pending';
$page_num = max(1, (int) ($_GET['paged'] ?? 1));
$per_page = 20;
$offset = ($page_num - 1) * $per_page;
echo '
';
echo ' ';
echo '';
foreach (['pending' => __('待审核', 'aksrt'), 'verified' => __('已通过', 'aksrt'), 'rejected' => __('已拒绝', 'aksrt'), '' => __('全部', 'aksrt')] as $key => $label) {
printf('%s ', esc_attr($key), selected($status, $key, false), esc_html($label));
}
echo ' ';
submit_button(__('筛选', 'aksrt'), 'secondary', '', false);
echo ' ';
global $wpdb;
$where = "INNER JOIN {$wpdb->usermeta} status ON status.user_id = u.ID AND status.meta_key = 'aksrt_user_verify_status'";
$params = [];
$sql_where = '';
if ($status !== '') {
$sql_where = ' WHERE status.meta_value = %s';
$params[] = $status;
}
$count_sql = "SELECT COUNT(DISTINCT u.ID) FROM {$wpdb->users} u {$where}{$sql_where}";
$total = $params
? (int) $wpdb->get_var($wpdb->prepare($count_sql, $params))
: (int) $wpdb->get_var($count_sql);
$sql = "SELECT u.ID, u.display_name, u.user_email, u.user_registered, status.meta_value AS verify_status,
reason.meta_value AS verify_reason
FROM {$wpdb->users} u
{$where}
LEFT JOIN {$wpdb->usermeta} reason ON reason.user_id = u.ID AND reason.meta_key = 'aksrt_user_verify_reason'
{$sql_where}
ORDER BY u.user_registered DESC
LIMIT %d OFFSET %d";
$params[] = $per_page;
$params[] = $offset;
$items = $wpdb->get_results($wpdb->prepare($sql, $params), ARRAY_A);
$pages = max(1, (int) ceil(max(1, $total) / $per_page));
echo '
';
echo 'ID ' . esc_html__('用户', 'aksrt') . ' ' . esc_html__('邮箱', 'aksrt') . ' ' . esc_html__('状态', 'aksrt') . ' ' . esc_html__('备注', 'aksrt') . ' ' . esc_html__('操作', 'aksrt') . ' ';
echo ' ';
if (!$items) {
echo '' . esc_html__('暂无认证申请', 'aksrt') . ' ';
}
foreach ((array) $items as $row) {
$uid = (int) ($row['ID'] ?? 0);
$st = (string) ($row['verify_status'] ?? '');
echo '';
echo '' . esc_html((string) $uid) . ' ';
echo '' . esc_html((string) ($row['display_name'] ?? '')) . ' ';
echo '' . esc_html((string) ($row['user_email'] ?? '')) . ' ';
echo '' . esc_html($st) . ' ';
echo '' . esc_html((string) ($row['verify_reason'] ?? '')) . ' ';
echo '';
if ($st === 'pending') {
foreach (['verified' => __('通过', 'aksrt'), 'rejected' => __('拒绝', 'aksrt')] as $action => $label) {
echo '';
echo ' ';
echo ' ';
echo ' ';
wp_nonce_field('aksrt_users_review_verify_' . $uid);
if ($action === 'rejected') {
echo ' ';
}
submit_button($label, 'small', '', false);
echo ' ';
}
} else {
echo '—';
}
echo ' ';
}
echo '
';
$this->pagination($pages, $page_num);
echo '
';
}
public function handle_review_verify(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('权限不足', 'aksrt'));
}
$user_id = isset($_POST['user_id']) ? (int) $_POST['user_id'] : 0;
$status = isset($_POST['status']) ? sanitize_key((string) wp_unslash($_POST['status'])) : '';
$note = isset($_POST['note']) ? sanitize_textarea_field((string) wp_unslash($_POST['note'])) : '';
check_admin_referer('aksrt_users_review_verify_' . $user_id);
$ok = false;
if ($user_id > 0 && in_array($status, ['verified', 'rejected'], true) && get_userdata($user_id)) {
update_user_meta($user_id, 'aksrt_user_verify_status', $status);
update_user_meta($user_id, 'aksrt_user_verify_reviewed_at', current_time('mysql'));
update_user_meta($user_id, 'aksrt_user_verify_reviewer', get_current_user_id());
if ($note !== '') {
update_user_meta($user_id, 'aksrt_user_verify_reason', $note);
} elseif ($status === 'verified') {
delete_user_meta($user_id, 'aksrt_user_verify_reason');
}
$log = get_option('aksrt_verify_review_log', []);
$log = is_array($log) ? $log : [];
$log[] = [
'user_id' => $user_id,
'status' => $status,
'reason' => $note,
'reviewer' => get_current_user_id(),
'time' => current_time('mysql'),
];
update_option('aksrt_verify_review_log', array_slice($log, -500), false);
$ok = true;
}
$this->redirect('aksrt_user_auth', $ok ? 'verify_updated' : 'verify_failed', ['status' => 'pending']);
}
/* ------------------------------------------------------------------
* Ban / reports
* ----------------------------------------------------------------*/
public function render_ban_page(): void {
$this->ensure_cap();
echo '' . esc_html__('举报&禁封', 'aksrt') . ' ';
$this->flash();
$tab = isset($_GET['tab']) ? sanitize_key((string) wp_unslash($_GET['tab'])) : 'reports';
echo '
';
printf('%s ', $tab === 'reports' ? ' nav-tab-active' : '', esc_url(admin_url('users.php?page=aksrt_user_ban&tab=reports')), esc_html__('举报', 'aksrt'));
printf('%s ', $tab === 'restrictions' ? ' nav-tab-active' : '', esc_url(admin_url('users.php?page=aksrt_user_ban&tab=restrictions')), esc_html__('禁封限制', 'aksrt'));
echo ' ';
if ($tab === 'restrictions') {
$this->render_restrictions_tab();
} else {
$this->render_reports_tab();
}
echo '
';
}
private function render_reports_tab(): void {
if (!class_exists('AKSRT_Report_Center')) {
echo '' . esc_html__('举报中心未初始化。', 'aksrt') . '
';
return;
}
$status = isset($_GET['status']) ? sanitize_key((string) wp_unslash($_GET['status'])) : 'pending';
$svc = AKSRT_Report_Center::get_instance();
$counts = $svc->get_status_counts();
echo '' . esc_html(sprintf(
/* translators: 1 pending 2 total */
__('待处理 %1$d / 共 %2$d', 'aksrt'),
(int) ($counts['pending'] ?? 0),
(int) ($counts['total'] ?? 0)
)) . '
';
echo '';
echo ' ';
echo '';
foreach (['pending' => __('待处理', 'aksrt'), 'reviewing' => __('审核中', 'aksrt'), 'resolved' => __('已处理', 'aksrt'), 'dismissed' => __('已驳回', 'aksrt'), '' => __('全部', 'aksrt')] as $key => $label) {
printf('%s ', esc_attr($key), selected($status, $key, false), esc_html($label));
}
echo ' ';
submit_button(__('筛选', 'aksrt'), 'secondary', '', false);
echo ' ';
$items = $svc->get_reports(['status' => $status, 'limit' => 50, 'offset' => 0]);
echo '';
echo 'ID ' . esc_html__('举报人', 'aksrt') . ' ' . esc_html__('目标', 'aksrt') . ' ' . esc_html__('原因', 'aksrt') . ' ' . esc_html__('状态', 'aksrt') . ' ' . esc_html__('时间', 'aksrt') . ' ' . esc_html__('操作', 'aksrt') . ' ';
echo ' ';
if (!$items) {
echo '' . esc_html__('暂无举报', 'aksrt') . ' ';
}
foreach ($items as $row) {
$id = (int) ($row['id'] ?? 0);
$st = (string) ($row['status'] ?? '');
$rid = (int) ($row['reporter_id'] ?? 0);
$ru = $rid > 0 ? get_userdata($rid) : null;
echo '';
echo '' . esc_html((string) $id) . ' ';
echo '' . ($ru ? esc_html($ru->display_name) : esc_html((string) $rid)) . ' ';
echo '' . esc_html((string) ($row['target_type'] ?? '') . ' #' . (string) ($row['target_id'] ?? '')) . ' ';
echo '' . esc_html((string) ($row['reason'] ?? '')) . ' ';
echo '' . esc_html($st) . ' ';
echo '' . esc_html((string) ($row['created_at'] ?? '')) . ' ';
echo '';
echo ' ';
echo ' ';
wp_nonce_field('aksrt_users_update_report_' . $id);
echo '';
foreach (['pending', 'reviewing', 'resolved', 'dismissed'] as $key) {
printf('%s ', esc_attr($key), selected($st, $key, false), esc_html($key));
}
echo ' ';
echo ' ';
submit_button(__('更新', 'aksrt'), 'small', '', false);
echo ' ';
}
echo '
';
}
private function render_restrictions_tab(): void {
if (!class_exists('AKSRT_User_Restriction')) {
echo '' . esc_html__('限制模块未初始化。', 'aksrt') . '
';
return;
}
$svc = AKSRT_User_Restriction::get_instance();
$stats = $svc->get_stats();
echo '' . esc_html(sprintf(
/* translators: 1 total 2 active 3 banned */
__('限制记录 %1$d · 生效中 %2$d · 封禁中 %3$d', 'aksrt'),
(int) ($stats['total'] ?? 0),
(int) ($stats['active'] ?? 0),
(int) ($stats['banned'] ?? 0)
)) . '
';
echo '' . esc_html__('添加限制', 'aksrt') . ' ';
echo '';
echo ' ';
wp_nonce_field('aksrt_users_add_restriction');
echo '' . esc_html__('用户 ID', 'aksrt') . '
';
echo '' . esc_html__('类型', 'aksrt') . '';
foreach (['warn' => __('警告', 'aksrt'), 'mute' => __('禁言', 'aksrt'), 'suspend' => __('停权', 'aksrt'), 'ban' => __('封禁', 'aksrt')] as $key => $label) {
printf('%s ', esc_attr($key), esc_html($label));
}
echo '
';
echo '' . esc_html__('范围', 'aksrt') . '';
foreach (['global' => __('全局', 'aksrt'), 'forum' => __('论坛', 'aksrt'), 'shop' => __('商城', 'aksrt'), 'comment' => __('评论', 'aksrt')] as $key => $label) {
printf('%s ', esc_attr($key), esc_html($label));
}
echo '
';
echo '' . esc_html__('原因', 'aksrt') . '
';
submit_button(__('添加', 'aksrt'), 'primary', '', false);
echo ' ';
global $wpdb;
$table = class_exists('AKSRT_DB_Migrator') ? AKSRT_DB_Migrator::table('user_restriction') : '';
if ($table === '') {
echo '' . esc_html__('限制表不可用。', 'aksrt') . '
';
return;
}
$items = $wpdb->get_results("SELECT * FROM {$table} ORDER BY id DESC LIMIT 100", ARRAY_A);
echo '';
echo 'ID ' . esc_html__('用户', 'aksrt') . ' ' . esc_html__('类型', 'aksrt') . ' ' . esc_html__('范围', 'aksrt') . ' ' . esc_html__('原因', 'aksrt') . ' ' . esc_html__('过期', 'aksrt') . ' ' . esc_html__('操作', 'aksrt') . ' ';
echo ' ';
if (!$items) {
echo '' . esc_html__('暂无限制记录', 'aksrt') . ' ';
}
foreach ((array) $items as $row) {
$id = (int) ($row['id'] ?? 0);
$user_id = (int) ($row['user_id'] ?? 0);
$user = $user_id > 0 ? get_userdata($user_id) : null;
echo '';
echo '' . esc_html((string) $id) . ' ';
echo '' . ($user ? esc_html($user->display_name) : esc_html((string) $user_id)) . ' ';
echo '' . esc_html((string) ($row['type'] ?? '')) . ' ';
echo '' . esc_html((string) ($row['scope'] ?? '')) . ' ';
echo '' . esc_html((string) ($row['reason'] ?? '')) . ' ';
echo '' . esc_html((string) ($row['expires_at'] ?? '—')) . ' ';
echo '';
echo ' ';
echo ' ';
wp_nonce_field('aksrt_users_remove_restriction_' . $id);
submit_button(__('移除', 'aksrt'), 'small', '', false);
echo ' ';
}
echo '
';
}
public function handle_update_report(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('权限不足', 'aksrt'));
}
$id = isset($_POST['report_id']) ? (int) $_POST['report_id'] : 0;
$new_status = isset($_POST['new_status']) ? sanitize_key((string) wp_unslash($_POST['new_status'])) : '';
$note = isset($_POST['note']) ? sanitize_textarea_field((string) wp_unslash($_POST['note'])) : '';
check_admin_referer('aksrt_users_update_report_' . $id);
$ok = $id > 0 && class_exists('AKSRT_Report_Center') && AKSRT_Report_Center::get_instance()->update_status($id, $new_status, $note);
$this->redirect('aksrt_user_ban', $ok ? 'report_updated' : 'report_failed', ['tab' => 'reports']);
}
public function handle_add_restriction(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('权限不足', 'aksrt'));
}
check_admin_referer('aksrt_users_add_restriction');
$user_id = isset($_POST['user_id']) ? (int) $_POST['user_id'] : 0;
$type = isset($_POST['type']) ? sanitize_key((string) wp_unslash($_POST['type'])) : 'warn';
$scope = isset($_POST['scope']) ? sanitize_key((string) wp_unslash($_POST['scope'])) : 'global';
$reason = isset($_POST['reason']) ? sanitize_text_field((string) wp_unslash($_POST['reason'])) : '';
$ok = false;
if ($user_id > 0 && $reason !== '' && class_exists('AKSRT_User_Restriction')) {
$result = AKSRT_User_Restriction::get_instance()->restrict($user_id, $type, $reason, $scope, '', get_current_user_id());
$ok = !empty($result['id']) || !empty($result['success']) || is_array($result);
}
$this->redirect('aksrt_user_ban', $ok ? 'restriction_added' : 'restriction_failed', ['tab' => 'restrictions']);
}
public function handle_remove_restriction(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('权限不足', 'aksrt'));
}
$id = isset($_POST['restriction_id']) ? (int) $_POST['restriction_id'] : 0;
check_admin_referer('aksrt_users_remove_restriction_' . $id);
$ok = $id > 0 && class_exists('AKSRT_User_Restriction') && AKSRT_User_Restriction::get_instance()->remove_restriction($id);
$this->redirect('aksrt_user_ban', $ok ? 'restriction_removed' : 'restriction_failed', ['tab' => 'restrictions']);
}
private function pagination(int $pages, int $page_num): void {
if ($pages <= 1) {
return;
}
echo '';
echo paginate_links([
'base' => add_query_arg('paged', '%#%'),
'format' => '',
'prev_text' => '«',
'next_text' => '»',
'total' => $pages,
'current' => $page_num,
]);
echo '
';
}
}