refactor: flatten src/AKSRT → src (drop theme-identifier dir)

Move all domain folders from src/AKSRT/{Domain} to src/{Domain}; remap
Composer PSR-4 "AKSRT\\": "src/"; update audit script + README + ADR
paths; remove dead tools/build-psr4-map.php (scanned deleted inc/classes).
This commit is contained in:
2026-08-02 22:06:06 +08:00
parent 56f1a8175f
commit 8c5b1555c9
285 changed files with 26 additions and 3600 deletions
+348
View File
@@ -0,0 +1,348 @@
<?php
/**
* @package AKSRT
*/
namespace AKSRT\Admin\Native;
defined('ABSPATH') || exit;
class PayChargeCards {
public const CAPABILITY = 'manage_options';
public const PAGE_SLUG = 'aksrt_charge_cards';
private static $instance = null;
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('admin_post_aksrt_pay_disable_charge_card', [$this, 'handle_disable_charge_card']);
add_action('admin_post_aksrt_pay_batch_charge_cards', [$this, 'handle_batch_charge_cards']);
}
public function render_charge_cards_page(): void {
$this->ensure_cap();
$this->render_header(__('卡密管理', 'aksrt'));
if (!class_exists('\AKSRT\Payment\ChargeCard')) {
echo '<div class="notice notice-error"><p>' . esc_html__('卡密模块未初始化。', 'aksrt') . '</p></div>';
$this->render_footer();
return;
}
$status = isset($_GET['status']) ? sanitize_key((string) wp_unslash($_GET['status'])) : '';
$type = isset($_GET['type']) ? sanitize_key((string) wp_unslash($_GET['type'])) : '';
$search = isset($_GET['s']) ? sanitize_text_field((string) wp_unslash($_GET['s'])) : '';
$page_num = max(1, (int) ($_GET['paged'] ?? 1));
$per_page = 20;
$svc = \AKSRT\Payment\ChargeCard::get_instance();
$stats = $svc->get_stats();
$type_labels = [
'balance' => __('余额', 'aksrt'),
'vip' => __('VIP', 'aksrt'),
'credits' => __('积分', 'aksrt'),
'points' => __('积分', 'aksrt'),
];
$status_labels = [
'active' => __('可用', 'aksrt'),
'used' => __('已用', 'aksrt'),
'disabled' => __('已禁用', 'aksrt'),
'expired' => __('已过期', 'aksrt'),
];
echo '<p>' . esc_html(sprintf(
/* translators: 1: total 2: active 3: used */
__('共 %1$d 张 · 可用 %2$d · 已用 %3$d', 'aksrt'),
(int) ($stats['total'] ?? 0),
(int) ($stats['active'] ?? 0),
(int) ($stats['used'] ?? 0)
)) . '</p>';
echo '<h2>' . esc_html__('批量生成卡密', 'aksrt') . '</h2>';
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="margin:12px 0 20px;display:flex;flex-wrap:wrap;gap:8px;align-items:end;">';
echo '<input type="hidden" name="action" value="aksrt_pay_batch_charge_cards" />';
wp_nonce_field('aksrt_pay_batch_charge_cards');
echo '<p><label>' . esc_html__('类型', 'aksrt') . '<br><select name="type">';
echo '<option value="balance">' . esc_html__('余额', 'aksrt') . '</option>';
echo '<option value="vip">' . esc_html__('VIP', 'aksrt') . '</option>';
echo '<option value="credits">' . esc_html__('积分', 'aksrt') . '</option>';
echo '</select></label></p>';
echo '<p><label>' . esc_html__('数量', 'aksrt') . '<br><input type="number" name="count" min="1" max="500" value="10" /></label></p>';
echo '<p><label>' . esc_html__('长度', 'aksrt') . '<br><input type="number" name="length" min="8" max="32" value="16" /></label></p>';
echo '<p><label>' . esc_html__('前缀', 'aksrt') . '<br><input type="text" name="prefix" value="" /></label></p>';
echo '<p><label>' . esc_html__('面额/积分', 'aksrt') . '<br><input type="number" step="0.01" name="amount" value="10" /></label></p>';
echo '<p><label>' . esc_html__('VIP等级', 'aksrt') . '<br><input type="number" name="vip_level" min="1" max="2" value="1" /></label></p>';
echo '<p><label>' . esc_html__('VIP天数', 'aksrt') . '<br><input type="number" name="vip_days" min="1" value="30" /></label></p>';
echo '<p><label>' . esc_html__('过期时间', 'aksrt') . '<br><input type="datetime-local" name="expires_at" /></label></p>';
echo '<p><label>' . esc_html__('备注', 'aksrt') . '<br><input type="text" name="note" value="" /></label></p>';
submit_button(__('生成', 'aksrt'), 'primary', '', false);
echo '</form>';
if (!empty($_GET['aksrt_generated'])) {
$generated = (int) $_GET['aksrt_generated'];
echo '<div class="notice notice-success"><p>' . esc_html(sprintf(
/* translators: %d count */
__('成功生成 %d 张卡密。', 'aksrt'),
$generated
)) . '</p></div>';
}
$generated_codes = get_transient('aksrt_pay_card_codes_' . get_current_user_id());
if (is_array($generated_codes) && $generated_codes) {
delete_transient('aksrt_pay_card_codes_' . get_current_user_id());
echo '<div class="notice notice-info"><p><strong>' . esc_html__('本次生成卡密(请及时复制保存):', 'aksrt') . '</strong></p>';
echo '<textarea class="large-text code" rows="' . esc_attr((string) min(12, max(3, count($generated_codes)))) . '" readonly onclick="this.select();">' . esc_textarea(implode("\n", $generated_codes)) . '</textarea>';
echo '</div>';
}
echo '<form method="get" style="margin:12px 0;">';
echo '<input type="hidden" name="page" value="aksrt_charge_cards" />';
echo '<select name="status">';
foreach ([
'' => __('全部状态', 'aksrt'),
'active' => __('可用', 'aksrt'),
'used' => __('已用', 'aksrt'),
'disabled' => __('已禁用', 'aksrt'),
'expired' => __('已过期', 'aksrt'),
] as $key => $label) {
printf(
'<option value="%s"%s>%s</option>',
esc_attr($key),
selected($status, $key, false),
esc_html($label)
);
}
echo '</select> ';
echo '<select name="type">';
foreach ([
'' => __('全部类型', 'aksrt'),
'balance' => __('余额', 'aksrt'),
'vip' => __('VIP', 'aksrt'),
'credits' => __('积分', 'aksrt'),
] as $key => $label) {
printf(
'<option value="%s"%s>%s</option>',
esc_attr($key),
selected($type, $key, false),
esc_html($label)
);
}
echo '</select> ';
echo '<input type="search" name="s" value="' . esc_attr($search) . '" placeholder="' . esc_attr__('密码 / 卡号 / 备注', 'aksrt') . '" /> ';
submit_button(__('筛选', 'aksrt'), 'secondary', '', false);
echo '</form>';
$result = $svc->get_list([
'status' => $status,
'type' => $type,
'search' => $search,
'limit' => $per_page,
'offset' => ($page_num - 1) * $per_page,
]);
$items = $result['items'] ?? [];
$total = (int) ($result['total'] ?? 0);
$pages = max(1, (int) ($result['max_pages'] ?? 1));
echo '<table class="wp-list-table widefat fixed striped">';
echo '<thead><tr>';
echo '<th>ID</th>';
echo '<th>' . esc_html__('类型', 'aksrt') . '</th>';
echo '<th>' . esc_html__('密码', 'aksrt') . '</th>';
echo '<th>' . esc_html__('面额', 'aksrt') . '</th>';
echo '<th>' . esc_html__('状态', 'aksrt') . '</th>';
echo '<th>' . esc_html__('过期', 'aksrt') . '</th>';
echo '<th>' . esc_html__('使用者', 'aksrt') . '</th>';
echo '<th>' . esc_html__('创建时间', 'aksrt') . '</th>';
echo '<th>' . esc_html__('操作', 'aksrt') . '</th>';
echo '</tr></thead><tbody>';
if (!$items) {
echo '<tr><td colspan="9">' . esc_html__('暂无卡密', 'aksrt') . '</td></tr>';
}
foreach ($items as $row) {
$id = (int) ($row['id'] ?? 0);
$st = (string) ($row['status'] ?? '');
$row_type = (string) ($row['type'] ?? '');
$user_id = (int) ($row['used_by'] ?? $row['user_id'] ?? 0);
$user = $user_id > 0 ? get_userdata($user_id) : null;
echo '<tr>';
echo '<td>' . esc_html((string) $id) . '</td>';
echo '<td>' . esc_html($type_labels[$row_type] ?? $row_type) . '</td>';
echo '<td><code>' . esc_html((string) ($row['password'] ?? $row['code'] ?? '')) . '</code></td>';
echo '<td>' . esc_html((string) ($row['amount'] ?? '')) . '</td>';
echo '<td>' . esc_html($status_labels[$st] ?? $st) . '</td>';
echo '<td>' . esc_html((string) ($row['expires_at'] ?? '—')) . '</td>';
echo '<td>' . ($user ? esc_html($user->display_name) : '—') . '</td>';
echo '<td>' . esc_html((string) ($row['created_at'] ?? '')) . '</td>';
echo '<td>';
if ($st === 'active') {
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '">';
echo '<input type="hidden" name="action" value="aksrt_pay_disable_charge_card" />';
echo '<input type="hidden" name="card_id" value="' . esc_attr((string) $id) . '" />';
wp_nonce_field('aksrt_pay_disable_charge_card_' . $id);
submit_button(__('禁用', 'aksrt'), 'small', '', false);
echo '</form>';
} else {
echo '—';
}
echo '</td>';
echo '</tr>';
}
echo '</tbody></table>';
if ($pages > 1) {
echo '<div class="tablenav"><div class="tablenav-pages">';
echo paginate_links([
'base' => add_query_arg('paged', '%#%'),
'format' => '',
'prev_text' => '&laquo;',
'next_text' => '&raquo;',
'total' => $pages,
'current' => $page_num,
]);
echo '</div></div>';
}
$this->render_footer();
}
public function handle_disable_charge_card(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('权限不足', 'aksrt'));
}
$id = isset($_POST['card_id']) ? (int) $_POST['card_id'] : 0;
check_admin_referer('aksrt_pay_disable_charge_card_' . $id);
$ok = false;
if ($id > 0 && class_exists('\AKSRT\Payment\ChargeCard')) {
$ok = \AKSRT\Payment\ChargeCard::get_instance()->disable($id);
}
wp_safe_redirect($this->page_url('aksrt_charge_cards', [
'aksrt_pay_msg' => $ok ? 'card_disabled' : 'card_failed',
]));
exit;
}
public function handle_batch_charge_cards(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('权限不足', 'aksrt'));
}
check_admin_referer('aksrt_pay_batch_charge_cards');
if (!class_exists('\AKSRT\Payment\ChargeCard')) {
wp_safe_redirect($this->page_url('aksrt_charge_cards', ['aksrt_pay_msg' => 'card_batch_failed']));
exit;
}
$type = isset($_POST['type']) ? sanitize_key((string) wp_unslash($_POST['type'])) : 'balance';
$count = isset($_POST['count']) ? (int) $_POST['count'] : 10;
$length = isset($_POST['length']) ? (int) $_POST['length'] : 16;
$prefix = isset($_POST['prefix']) ? sanitize_text_field((string) wp_unslash($_POST['prefix'])) : '';
$amount = isset($_POST['amount']) ? (float) wp_unslash($_POST['amount']) : 0;
$note = isset($_POST['note']) ? sanitize_textarea_field((string) wp_unslash($_POST['note'])) : '';
$level = isset($_POST['vip_level']) ? max(1, min(2, (int) $_POST['vip_level'])) : 1;
$days = isset($_POST['vip_days']) ? max(1, (int) $_POST['vip_days']) : 30;
$expires = isset($_POST['expires_at']) ? sanitize_text_field((string) wp_unslash($_POST['expires_at'])) : '';
if ($expires !== '') {
$expires = str_replace('T', ' ', $expires);
if (strlen($expires) === 16) {
$expires .= ':00';
}
}
if ($type === 'points') {
$type = 'credits';
}
$meta = [];
if ($type === 'balance') {
$meta['amount'] = max(0, $amount);
} elseif ($type === 'credits') {
$meta['points'] = max(0, (int) $amount);
} elseif ($type === 'vip') {
$meta['level'] = $level;
$meta['days'] = $days;
}
$codes = \AKSRT\Payment\ChargeCard::get_instance()->batch_create($type, $meta, $count, $length, $prefix, $expires, $note);
$ok = !empty($codes);
if ($ok) {
set_transient('aksrt_pay_card_codes_' . get_current_user_id(), $codes, 10 * MINUTE_IN_SECONDS);
}
wp_safe_redirect($this->page_url('aksrt_charge_cards', [
'aksrt_pay_msg' => $ok ? 'card_batch_ok' : 'card_batch_failed',
'aksrt_generated' => count($codes),
]));
exit;
}
private function ensure_cap(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('您没有权限访问此页面。', 'aksrt'));
}
}
private function render_header(string $title): void {
echo '<div class="wrap">';
echo '<h1>' . esc_html($title) . '</h1>';
$this->render_flash();
}
private function render_footer(): void {
echo '</div>';
}
private function render_flash(): void {
if (empty($_GET['aksrt_pay_msg'])) {
return;
}
$code = sanitize_key((string) wp_unslash($_GET['aksrt_pay_msg']));
$map = [
'card_disabled' => __('充值卡已禁用。', 'aksrt'),
'card_failed' => __('充值卡禁用失败。', 'aksrt'),
'card_batch_ok' => __('卡密批量生成完成。', 'aksrt'),
'card_batch_failed' => __('卡密批量生成失败。', 'aksrt'),
];
if (!isset($map[$code])) {
return;
}
$class = str_contains($code, 'failed') ? 'notice-error' : 'notice-success';
echo '<div class="notice ' . esc_attr($class) . ' is-dismissible"><p>' . esc_html($map[$code]) . '</p></div>';
}
private function page_url(string $slug, array $args = []): string {
return add_query_arg(array_merge(['page' => $slug], $args), admin_url('admin.php'));
}
private function render_pagination(int $pages, int $page_num): void {
if ($pages <= 1) {
return;
}
echo '<div class="tablenav"><div class="tablenav-pages">';
echo paginate_links([
'base' => add_query_arg('paged', '%#%'),
'format' => '',
'prev_text' => '&laquo;',
'next_text' => '&raquo;',
'total' => $pages,
'current' => $page_num,
]);
echo '</div></div>';
}
}
+476
View File
@@ -0,0 +1,476 @@
<?php
/**
* @package AKSRT
*/
namespace AKSRT\Admin\Native;
defined('ABSPATH') || exit;
class PayCoupons {
public const CAPABILITY = 'manage_options';
public const PAGE_SLUG = 'aksrt_coupons';
private static $instance = null;
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('admin_post_aksrt_pay_create_coupon', [$this, 'handle_create_coupon']);
add_action('admin_post_aksrt_pay_batch_coupons', [$this, 'handle_batch_coupons']);
add_action('admin_post_aksrt_pay_update_coupon', [$this, 'handle_update_coupon']);
add_action('admin_post_aksrt_pay_disable_coupon', [$this, 'handle_disable_coupon']);
add_action('admin_post_aksrt_pay_enable_coupon', [$this, 'handle_enable_coupon']);
}
public function render_page(): void {
$this->ensure_cap();
$this->render_header(__('优惠码', 'aksrt'));
if (!class_exists('\AKSRT\Payment\Coupon')) {
echo '<div class="notice notice-error"><p>' . esc_html__('优惠码模块未初始化。', 'aksrt') . '</p></div>';
$this->render_footer();
return;
}
$svc = \AKSRT\Payment\Coupon::get_instance();
$edit_id = isset($_GET['edit_id']) ? (int) $_GET['edit_id'] : 0;
$edit_row = $edit_id > 0 ? $svc->get($edit_id) : null;
if ($edit_id > 0 && $edit_row) {
$this->render_edit_form($edit_row);
} elseif ($edit_id > 0) {
echo '<div class="notice notice-error"><p>' . esc_html__('优惠码不存在。', 'aksrt') . '</p></div>';
}
$this->render_create_form();
$this->render_batch_form();
$this->render_generated_dump();
$this->render_list($svc);
$this->render_footer();
}
/**
* @param array<string,mixed> $row
*/
private function render_edit_form(array $row): void {
$id = (int) ($row['id'] ?? 0);
$scope = is_array($row['product_scope'] ?? null) ? $row['product_scope'] : [];
$scope_type = sanitize_key((string) ($scope['type'] ?? ''));
$scope_ids = array_map('intval', (array) ($scope['ids'] ?? []));
$expires = (string) ($row['expires_at'] ?? '');
$expires_local = $expires !== '' ? str_replace(' ', 'T', substr($expires, 0, 16)) : '';
echo '<h2>' . esc_html(sprintf(
/* translators: %d coupon id */
__('编辑优惠码 #%d', 'aksrt'),
$id
)) . '</h2>';
echo '<p><a href="' . esc_url($this->page_url()) . '">&larr; ' . esc_html__('返回列表', 'aksrt') . '</a></p>';
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="margin-bottom:20px;display:flex;gap:8px;flex-wrap:wrap;align-items:end;">';
echo '<input type="hidden" name="action" value="aksrt_pay_update_coupon" />';
echo '<input type="hidden" name="coupon_id" value="' . esc_attr((string) $id) . '" />';
wp_nonce_field('aksrt_pay_update_coupon_' . $id);
echo '<p><label>' . esc_html__('代码', 'aksrt') . '<br><input type="text" value="' . esc_attr((string) ($row['code'] ?? '')) . '" class="regular-text" readonly /></label></p>';
echo '<p><label>' . esc_html__('类型', 'aksrt') . '<br><select name="type">';
foreach (['fixed' => __('固定金额', 'aksrt'), 'percent' => __('折扣百分比', 'aksrt')] as $k => $label) {
printf('<option value="%s"%s>%s</option>', esc_attr($k), selected((string) ($row['type'] ?? ''), $k, false), esc_html($label));
}
echo '</select></label></p>';
echo '<p><label>' . esc_html__('面值', 'aksrt') . '<br><input type="number" step="0.01" name="value" value="' . esc_attr((string) ($row['value'] ?? 0)) . '" /></label></p>';
echo '<p><label>' . esc_html__('最低消费', 'aksrt') . '<br><input type="number" step="0.01" name="min_amount" value="' . esc_attr((string) ($row['min_amount'] ?? 0)) . '" /></label></p>';
echo '<p><label>' . esc_html__('最高优惠(百分比用)', 'aksrt') . '<br><input type="number" step="0.01" name="max_discount" value="' . esc_attr((string) ($row['max_discount'] ?? 0)) . '" /></label></p>';
echo '<p><label>' . esc_html__('总次数(0=不限)', 'aksrt') . '<br><input type="number" name="usage_limit" value="' . esc_attr((string) ($row['usage_limit'] ?? 0)) . '" /></label></p>';
echo '<p><label>' . esc_html__('每人限次(0=不限)', 'aksrt') . '<br><input type="number" name="per_user_limit" value="' . esc_attr((string) ($row['per_user_limit'] ?? 0)) . '" /></label></p>';
$this->render_scope_fields($scope_type, implode(',', $scope_ids));
echo '<p><label>' . esc_html__('过期时间', 'aksrt') . '<br><input type="datetime-local" name="expires_at" value="' . esc_attr($expires_local) . '" /></label></p>';
submit_button(__('保存修改', 'aksrt'), 'primary', '', false);
echo '</form><hr>';
}
private function render_create_form(): void {
echo '<h2>' . esc_html__('新建优惠码', 'aksrt') . '</h2>';
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="margin-bottom:20px;display:flex;gap:8px;flex-wrap:wrap;align-items:end;">';
echo '<input type="hidden" name="action" value="aksrt_pay_create_coupon" />';
wp_nonce_field('aksrt_pay_create_coupon');
echo '<p><label>' . esc_html__('代码', 'aksrt') . '<br><input type="text" name="code" required /></label></p>';
echo '<p><label>' . esc_html__('类型', 'aksrt') . '<br><select name="type"><option value="fixed">' . esc_html__('固定金额', 'aksrt') . '</option><option value="percent">' . esc_html__('折扣百分比', 'aksrt') . '</option></select></label></p>';
echo '<p><label>' . esc_html__('面值', 'aksrt') . '<br><input type="number" step="0.01" name="value" value="0" /></label></p>';
echo '<p><label>' . esc_html__('最低消费', 'aksrt') . '<br><input type="number" step="0.01" name="min_amount" value="0" /></label></p>';
echo '<p><label>' . esc_html__('最高优惠(百分比用)', 'aksrt') . '<br><input type="number" step="0.01" name="max_discount" value="0" /></label></p>';
echo '<p><label>' . esc_html__('总次数(0=不限)', 'aksrt') . '<br><input type="number" name="usage_limit" value="0" /></label></p>';
echo '<p><label>' . esc_html__('每人限次(0=不限)', 'aksrt') . '<br><input type="number" name="per_user_limit" value="0" /></label></p>';
$this->render_scope_fields('', '');
echo '<p><label>' . esc_html__('过期时间', 'aksrt') . '<br><input type="datetime-local" name="expires_at" /></label></p>';
submit_button(__('创建', 'aksrt'), 'primary', '', false);
echo '</form>';
}
private function render_batch_form(): void {
echo '<h2>' . esc_html__('批量生成优惠码', 'aksrt') . '</h2>';
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="margin-bottom:20px;display:flex;gap:8px;flex-wrap:wrap;align-items:end;">';
echo '<input type="hidden" name="action" value="aksrt_pay_batch_coupons" />';
wp_nonce_field('aksrt_pay_batch_coupons');
echo '<p><label>' . esc_html__('数量', 'aksrt') . '<br><input type="number" name="count" min="1" max="100" value="10" /></label></p>';
echo '<p><label>' . esc_html__('长度', 'aksrt') . '<br><input type="number" name="length" min="6" max="24" value="10" /></label></p>';
echo '<p><label>' . esc_html__('前缀', 'aksrt') . '<br><input type="text" name="prefix" value="" /></label></p>';
echo '<p><label>' . esc_html__('类型', 'aksrt') . '<br><select name="type"><option value="fixed">' . esc_html__('固定金额', 'aksrt') . '</option><option value="percent">' . esc_html__('折扣百分比', 'aksrt') . '</option></select></label></p>';
echo '<p><label>' . esc_html__('面值', 'aksrt') . '<br><input type="number" step="0.01" name="value" value="10" /></label></p>';
echo '<p><label>' . esc_html__('最低消费', 'aksrt') . '<br><input type="number" step="0.01" name="min_amount" value="0" /></label></p>';
echo '<p><label>' . esc_html__('最高优惠', 'aksrt') . '<br><input type="number" step="0.01" name="max_discount" value="0" /></label></p>';
echo '<p><label>' . esc_html__('总次数(0=不限)', 'aksrt') . '<br><input type="number" name="usage_limit" value="1" /></label></p>';
echo '<p><label>' . esc_html__('每人限次', 'aksrt') . '<br><input type="number" name="per_user_limit" value="1" /></label></p>';
$this->render_scope_fields('', '');
echo '<p><label>' . esc_html__('过期时间', 'aksrt') . '<br><input type="datetime-local" name="expires_at" /></label></p>';
submit_button(__('批量生成', 'aksrt'), 'secondary', '', false);
echo '</form>';
}
private function render_scope_fields(string $selected_type, string $ids_text): void {
echo '<p><label>' . esc_html__('适用范围', 'aksrt') . '<br><select name="scope_type">';
foreach ([
'' => __('全部商品', 'aksrt'),
'include' => __('仅指定商品 ID', 'aksrt'),
'exclude' => __('排除商品 ID', 'aksrt'),
'category' => __('指定分类 ID', 'aksrt'),
] as $k => $label) {
printf('<option value="%s"%s>%s</option>', esc_attr($k), selected($selected_type, $k, false), esc_html($label));
}
echo '</select></label></p>';
echo '<p><label>' . esc_html__('范围 ID', 'aksrt') . '<br><input type="text" name="scope_ids" class="regular-text" value="' . esc_attr($ids_text) . '" placeholder="' . esc_attr__('逗号分隔,如 12,34', 'aksrt') . '" /></label></p>';
}
private function render_generated_dump(): void {
if (!empty($_GET['aksrt_coupon_generated'])) {
$generated = (int) $_GET['aksrt_coupon_generated'];
echo '<div class="notice notice-success"><p>' . esc_html(sprintf(
/* translators: %d count */
__('成功生成 %d 个优惠码。', 'aksrt'),
$generated
)) . '</p></div>';
}
$generated_codes = get_transient('aksrt_pay_coupon_codes_' . get_current_user_id());
if (is_array($generated_codes) && $generated_codes) {
delete_transient('aksrt_pay_coupon_codes_' . get_current_user_id());
echo '<div class="notice notice-info"><p><strong>' . esc_html__('本次生成优惠码(请及时复制保存):', 'aksrt') . '</strong></p>';
echo '<textarea class="large-text code" rows="' . esc_attr((string) min(12, max(3, count($generated_codes)))) . '" readonly onclick="this.select();">' . esc_textarea(implode("\n", $generated_codes)) . '</textarea>';
echo '</div>';
}
}
private function render_list(\AKSRT\Payment\Coupon $svc): void {
$status = isset($_GET['status']) ? sanitize_key((string) wp_unslash($_GET['status'])) : '';
$search = isset($_GET['s']) ? sanitize_text_field((string) wp_unslash($_GET['s'])) : '';
$page_num = max(1, (int) ($_GET['paged'] ?? 1));
$per_page = 20;
$type_labels = [
'fixed' => __('固定金额', 'aksrt'),
'percent' => __('折扣百分比', 'aksrt'),
];
$status_labels = [
'active' => __('可用', 'aksrt'),
'disabled' => __('已禁用', 'aksrt'),
];
echo '<form method="get" style="margin:12px 0;">';
echo '<input type="hidden" name="page" value="' . esc_attr(self::PAGE_SLUG) . '" />';
echo '<select name="status">';
foreach (['' => __('全部状态', 'aksrt'), 'active' => __('可用', 'aksrt'), 'disabled' => __('已禁用', 'aksrt')] as $key => $label) {
printf('<option value="%s"%s>%s</option>', esc_attr($key), selected($status, $key, false), esc_html($label));
}
echo '</select> ';
echo '<input type="search" name="s" value="' . esc_attr($search) . '" placeholder="' . esc_attr__('搜索代码', 'aksrt') . '" /> ';
submit_button(__('筛选', 'aksrt'), 'secondary', '', false);
echo '</form>';
$result = $svc->get_list([
'status' => $status,
'search' => $search,
'limit' => $per_page,
'offset' => ($page_num - 1) * $per_page,
]);
$items = $result['items'] ?? [];
$pages = max(1, (int) ($result['total_pages'] ?? 1));
echo '<table class="wp-list-table widefat fixed striped"><thead><tr>';
echo '<th>ID</th><th>' . esc_html__('代码', 'aksrt') . '</th><th>' . esc_html__('类型', 'aksrt') . '</th><th>' . esc_html__('面值', 'aksrt') . '</th><th>' . esc_html__('门槛/封顶', 'aksrt') . '</th><th>' . esc_html__('范围', 'aksrt') . '</th><th>' . esc_html__('已用/上限/每人', 'aksrt') . '</th><th>' . esc_html__('状态', 'aksrt') . '</th><th>' . esc_html__('过期', 'aksrt') . '</th><th>' . esc_html__('操作', 'aksrt') . '</th>';
echo '</tr></thead><tbody>';
if (!$items) {
echo '<tr><td colspan="10">' . esc_html__('暂无优惠码', 'aksrt') . '</td></tr>';
}
foreach ($items as $row) {
$id = (int) ($row['id'] ?? 0);
$st = (string) ($row['status'] ?? '');
$row_type = (string) ($row['type'] ?? '');
echo '<tr>';
echo '<td>' . esc_html((string) $id) . '</td>';
echo '<td><code>' . esc_html((string) ($row['code'] ?? '')) . '</code></td>';
echo '<td>' . esc_html($type_labels[$row_type] ?? $row_type) . '</td>';
echo '<td>' . esc_html((string) ($row['value'] ?? '')) . ($row_type === 'percent' ? '%' : '') . '</td>';
echo '<td>' . esc_html((string) ($row['min_amount'] ?? 0)) . ' / ' . esc_html((string) ($row['max_discount'] ?? 0)) . '</td>';
echo '<td>' . esc_html($this->format_scope_label($row['product_scope'] ?? [])) . '</td>';
echo '<td>' . esc_html((string) ((int) ($row['used_count'] ?? 0)) . '/' . (string) ((int) ($row['usage_limit'] ?? 0)) . '/' . (string) ((int) ($row['per_user_limit'] ?? 0))) . '</td>';
echo '<td>' . esc_html($status_labels[$st] ?? $st) . '</td>';
echo '<td>' . esc_html((string) ($row['expires_at'] ?? '')) . '</td>';
echo '<td style="display:flex;gap:6px;flex-wrap:wrap;align-items:center;">';
echo '<a class="button button-small" href="' . esc_url($this->page_url(['edit_id' => $id])) . '">' . esc_html__('编辑', 'aksrt') . '</a>';
if ($st === 'active') {
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="display:inline;">';
echo '<input type="hidden" name="action" value="aksrt_pay_disable_coupon" />';
echo '<input type="hidden" name="coupon_id" value="' . esc_attr((string) $id) . '" />';
wp_nonce_field('aksrt_pay_disable_coupon_' . $id);
submit_button(__('禁用', 'aksrt'), 'small', '', false);
echo '</form>';
} elseif ($st === 'disabled') {
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="display:inline;">';
echo '<input type="hidden" name="action" value="aksrt_pay_enable_coupon" />';
echo '<input type="hidden" name="coupon_id" value="' . esc_attr((string) $id) . '" />';
wp_nonce_field('aksrt_pay_enable_coupon_' . $id);
submit_button(__('启用', 'aksrt'), 'small', '', false);
echo '</form>';
}
echo '</td></tr>';
}
echo '</tbody></table>';
$this->render_pagination($pages, $page_num);
}
public function handle_create_coupon(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('权限不足', 'aksrt'));
}
check_admin_referer('aksrt_pay_create_coupon');
$ok = false;
if (class_exists('\AKSRT\Payment\Coupon')) {
$id = \AKSRT\Payment\Coupon::get_instance()->create([
'code' => sanitize_text_field((string) wp_unslash($_POST['code'] ?? '')),
'type' => sanitize_key((string) wp_unslash($_POST['type'] ?? 'fixed')),
'value' => (float) ($_POST['value'] ?? 0),
'min_amount' => (float) ($_POST['min_amount'] ?? 0),
'max_discount' => (float) ($_POST['max_discount'] ?? 0),
'usage_limit' => (int) ($_POST['usage_limit'] ?? 0),
'per_user_limit' => (int) ($_POST['per_user_limit'] ?? 0),
'product_scope' => $this->parse_product_scope_from_request(),
'expires_at' => $this->normalize_expires_from_request(),
]);
$ok = $id > 0;
}
wp_safe_redirect($this->page_url(['aksrt_pay_msg' => $ok ? 'coupon_created' : 'coupon_create_failed']));
exit;
}
public function handle_batch_coupons(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('权限不足', 'aksrt'));
}
check_admin_referer('aksrt_pay_batch_coupons');
$ok = false;
$created = 0;
$codes = [];
if (class_exists('\AKSRT\Payment\Coupon')) {
$result = \AKSRT\Payment\Coupon::get_instance()->batch_create([
'type' => sanitize_key((string) wp_unslash($_POST['type'] ?? 'fixed')),
'value' => (float) ($_POST['value'] ?? 0),
'min_amount' => (float) ($_POST['min_amount'] ?? 0),
'max_discount' => (float) ($_POST['max_discount'] ?? 0),
'usage_limit' => (int) ($_POST['usage_limit'] ?? 0),
'per_user_limit' => (int) ($_POST['per_user_limit'] ?? 0),
'product_scope' => $this->parse_product_scope_from_request(),
'expires_at' => $this->normalize_expires_from_request(),
], (int) ($_POST['count'] ?? 10), (int) ($_POST['length'] ?? 10), sanitize_text_field((string) wp_unslash($_POST['prefix'] ?? '')));
$created = (int) ($result['created'] ?? 0);
$codes = is_array($result['codes'] ?? null) ? $result['codes'] : [];
$ok = $created > 0;
if ($ok) {
set_transient('aksrt_pay_coupon_codes_' . get_current_user_id(), $codes, 10 * MINUTE_IN_SECONDS);
}
}
wp_safe_redirect($this->page_url([
'aksrt_pay_msg' => $ok ? 'coupon_batch_ok' : 'coupon_batch_failed',
'aksrt_coupon_generated' => $created,
]));
exit;
}
public function handle_update_coupon(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('权限不足', 'aksrt'));
}
$id = isset($_POST['coupon_id']) ? (int) $_POST['coupon_id'] : 0;
check_admin_referer('aksrt_pay_update_coupon_' . $id);
$ok = false;
if ($id > 0 && class_exists('\AKSRT\Payment\Coupon')) {
$ok = \AKSRT\Payment\Coupon::get_instance()->update($id, [
'type' => sanitize_key((string) wp_unslash($_POST['type'] ?? 'fixed')),
'value' => (float) ($_POST['value'] ?? 0),
'min_amount' => (float) ($_POST['min_amount'] ?? 0),
'max_discount' => (float) ($_POST['max_discount'] ?? 0),
'usage_limit' => (int) ($_POST['usage_limit'] ?? 0),
'per_user_limit' => (int) ($_POST['per_user_limit'] ?? 0),
'product_scope' => $this->parse_product_scope_from_request(),
'expires_at' => $this->normalize_expires_from_request(),
]);
}
wp_safe_redirect($this->page_url([
'aksrt_pay_msg' => $ok ? 'coupon_updated' : 'coupon_update_failed',
'edit_id' => $id,
]));
exit;
}
public function handle_disable_coupon(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('权限不足', 'aksrt'));
}
$id = isset($_POST['coupon_id']) ? (int) $_POST['coupon_id'] : 0;
check_admin_referer('aksrt_pay_disable_coupon_' . $id);
$ok = $id > 0 && class_exists('\AKSRT\Payment\Coupon') && \AKSRT\Payment\Coupon::get_instance()->disable($id);
wp_safe_redirect($this->page_url(['aksrt_pay_msg' => $ok ? 'coupon_disabled' : 'coupon_failed']));
exit;
}
public function handle_enable_coupon(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('权限不足', 'aksrt'));
}
$id = isset($_POST['coupon_id']) ? (int) $_POST['coupon_id'] : 0;
check_admin_referer('aksrt_pay_enable_coupon_' . $id);
$ok = $id > 0 && class_exists('\AKSRT\Payment\Coupon') && \AKSRT\Payment\Coupon::get_instance()->enable($id);
wp_safe_redirect($this->page_url(['aksrt_pay_msg' => $ok ? 'coupon_enabled' : 'coupon_failed']));
exit;
}
/**
* @return array<string,mixed>
*/
private function parse_product_scope_from_request(): array {
$type = isset($_POST['scope_type']) ? sanitize_key((string) wp_unslash($_POST['scope_type'])) : '';
if ($type === '' || !in_array($type, ['include', 'exclude', 'category'], true)) {
return [];
}
$raw = isset($_POST['scope_ids']) ? (string) wp_unslash($_POST['scope_ids']) : '';
$parts = preg_split('/[\s,;]+/', $raw) ?: [];
$ids = array_values(array_unique(array_filter(array_map('intval', $parts))));
if (!$ids) {
return [];
}
$scope = [
'type' => $type,
'ids' => $ids,
];
if ($type === 'category') {
$scope['taxonomies'] = ['category'];
}
return $scope;
}
private function normalize_expires_from_request(): string {
$expires = isset($_POST['expires_at']) ? sanitize_text_field((string) wp_unslash($_POST['expires_at'])) : '';
if ($expires === '') {
return '';
}
$expires = str_replace('T', ' ', $expires);
if (strlen($expires) === 16) {
$expires .= ':00';
}
return $expires;
}
/**
* @param mixed $scope
*/
private function format_scope_label($scope): string {
if (is_string($scope) && $scope !== '') {
$decoded = json_decode($scope, true);
$scope = is_array($decoded) ? $decoded : [];
}
if (!is_array($scope) || empty($scope)) {
return __('全部', 'aksrt');
}
$type = sanitize_key((string) ($scope['type'] ?? ''));
$ids = array_map('intval', (array) ($scope['ids'] ?? []));
$id_text = $ids ? implode(',', $ids) : '—';
$map = [
'include' => __('含商品', 'aksrt'),
'exclude' => __('排除商品', 'aksrt'),
'category' => __('分类', 'aksrt'),
];
$label = $map[$type] ?? $type;
return $label . ':' . $id_text;
}
private function ensure_cap(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('您没有权限访问此页面。', 'aksrt'));
}
}
private function render_header(string $title): void {
echo '<div class="wrap">';
echo '<h1>' . esc_html($title) . '</h1>';
$this->render_flash();
}
private function render_footer(): void {
echo '</div>';
}
private function render_flash(): void {
if (empty($_GET['aksrt_pay_msg'])) {
return;
}
$code = sanitize_key((string) wp_unslash($_GET['aksrt_pay_msg']));
$map = [
'coupon_created' => __('优惠码已创建。', 'aksrt'),
'coupon_create_failed' => __('优惠码创建失败(可能重复)。', 'aksrt'),
'coupon_batch_ok' => __('优惠码批量生成完成。', 'aksrt'),
'coupon_batch_failed' => __('优惠码批量生成失败。', 'aksrt'),
'coupon_updated' => __('优惠码已更新。', 'aksrt'),
'coupon_update_failed' => __('优惠码更新失败。', 'aksrt'),
'coupon_disabled' => __('优惠码已禁用。', 'aksrt'),
'coupon_enabled' => __('优惠码已启用。', 'aksrt'),
'coupon_failed' => __('优惠码操作失败。', 'aksrt'),
];
if (!isset($map[$code])) {
return;
}
$class = str_contains($code, 'failed') ? 'notice-error' : 'notice-success';
echo '<div class="notice ' . esc_attr($class) . ' is-dismissible"><p>' . esc_html($map[$code]) . '</p></div>';
}
/**
* @param array<string,mixed> $args
*/
private function page_url(array $args = []): string {
return add_query_arg(array_merge(['page' => self::PAGE_SLUG], $args), admin_url('admin.php'));
}
private function render_pagination(int $pages, int $page_num): void {
if ($pages <= 1) {
return;
}
echo '<div class="tablenav"><div class="tablenav-pages">';
echo paginate_links([
'base' => add_query_arg('paged', '%#%'),
'format' => '',
'prev_text' => '&laquo;',
'next_text' => '&raquo;',
'total' => $pages,
'current' => $page_num,
]);
echo '</div></div>';
}
}
+200
View File
@@ -0,0 +1,200 @@
<?php
/**
* @package AKSRT
*/
namespace AKSRT\Admin\Native;
defined('ABSPATH') || exit;
class PayDownloadLogs {
public const CAPABILITY = 'manage_options';
public const PAGE_SLUG = 'aksrt_download_logs';
private static $instance = null;
public static function get_instance(): self {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
public function init(): void {
// Read-only.
}
public function render_page(): void {
$this->ensure_cap();
echo '<div class="wrap"><h1>' . esc_html__('下载记录', 'aksrt') . '</h1>';
if (!class_exists('\AKSRT\Core\DBMigrator')) {
echo '<div class="notice notice-error"><p>' . esc_html__('数据库服务不可用。', 'aksrt') . '</p></div></div>';
return;
}
$table = \AKSRT\Core\DBMigrator::table('dl_token');
if ($table === '') {
echo '<div class="notice notice-error"><p>' . esc_html__('下载令牌表未初始化。', 'aksrt') . '</p></div></div>';
return;
}
$post_id = isset($_GET['post_id']) ? max(0, (int) $_GET['post_id']) : 0;
$user_id = isset($_GET['user_id']) ? max(0, (int) $_GET['user_id']) : 0;
$only_used = !isset($_GET['used']) || (string) $_GET['used'] !== '0';
$page_num = max(1, (int) ($_GET['paged'] ?? 1));
$per_page = 30;
$result = $this->query_logs($table, [
'post_id' => $post_id,
'user_id' => $user_id,
'only_used' => $only_used,
'limit' => $per_page,
'offset' => ($page_num - 1) * $per_page,
]);
$items = $result['items'];
$total = $result['total'];
$pages = max(1, (int) ceil($total / $per_page));
echo '<p class="description">' . esc_html__('基于下载令牌表(aksrt_dl_token)。已使用记录会保留一段时间后清理;未使用且过期的令牌会被定期删除。', 'aksrt') . '</p>';
echo '<form method="get" style="margin:12px 0;">';
echo '<input type="hidden" name="page" value="' . esc_attr(self::PAGE_SLUG) . '" />';
echo '<label>' . esc_html__('文章 ID', 'aksrt') . ' <input type="number" min="0" name="post_id" value="' . esc_attr($post_id > 0 ? (string) $post_id : '') . '" class="small-text" /></label> ';
echo '<label>' . esc_html__('用户 ID', 'aksrt') . ' <input type="number" min="0" name="user_id" value="' . esc_attr($user_id > 0 ? (string) $user_id : '') . '" class="small-text" /></label> ';
echo '<label><input type="checkbox" name="used" value="1" ' . checked($only_used, true, false) . ' /> ' . esc_html__('仅已使用', 'aksrt') . '</label> ';
// When unchecked, pass used=0 explicitly.
if (!$only_used) {
// no-op; checkbox not checked
}
submit_button(__('筛选', 'aksrt'), 'secondary', '', false);
echo ' <a class="button" href="' . esc_url(admin_url('admin.php?page=' . self::PAGE_SLUG)) . '">' . esc_html__('重置', 'aksrt') . '</a>';
echo '</form>';
// Fix: unchecked checkbox means used filter off — re-read without default if form submitted with no used.
// Already handled via isset($_GET['used']).
echo '<table class="wp-list-table widefat fixed striped">';
echo '<thead><tr>';
echo '<th>' . esc_html__('时间', 'aksrt') . '</th>';
echo '<th>' . esc_html__('用户', 'aksrt') . '</th>';
echo '<th>' . esc_html__('文章', 'aksrt') . '</th>';
echo '<th>' . esc_html__('源序号', 'aksrt') . '</th>';
echo '<th>' . esc_html__('积分消耗', 'aksrt') . '</th>';
echo '<th>' . esc_html__('使用', 'aksrt') . '</th>';
echo '<th>' . esc_html__('过期', 'aksrt') . '</th>';
echo '<th>' . esc_html__('IP', 'aksrt') . '</th>';
echo '</tr></thead><tbody>';
if (!$items) {
echo '<tr><td colspan="8">' . esc_html__('暂无下载记录。', 'aksrt') . '</td></tr>';
}
foreach ($items as $row) {
$uid = (int) ($row['user_id'] ?? 0);
$pid = (int) ($row['post_id'] ?? 0);
$user = $uid > 0 ? get_userdata($uid) : null;
$title = $pid > 0 ? get_the_title($pid) : '';
if ($title === '') {
$title = $pid > 0 ? sprintf(__('(已删除 #%d)', 'aksrt'), $pid) : '—';
}
$edit = $pid > 0 ? get_edit_post_link($pid, 'raw') : '';
$used = (int) ($row['used_count'] ?? 0);
$max = (int) ($row['max_uses'] ?? 1);
echo '<tr>';
echo '<td>' . esc_html((string) ($row['created_at'] ?? '')) . '</td>';
echo '<td>';
if ($user) {
echo esc_html($user->display_name . ' (#' . $uid . ')');
echo '<br><a href="' . esc_url(admin_url('admin.php?page=' . self::PAGE_SLUG . '&user_id=' . $uid)) . '">' . esc_html__('仅此用户', 'aksrt') . '</a>';
} else {
echo esc_html((string) $uid);
}
echo '</td>';
echo '<td>';
if ($edit) {
echo '<a href="' . esc_url($edit) . '">' . esc_html($title) . '</a>';
} else {
echo esc_html($title);
}
if ($pid > 0) {
echo '<br><a href="' . esc_url(admin_url('admin.php?page=' . self::PAGE_SLUG . '&post_id=' . $pid)) . '">' . esc_html__('仅此文章', 'aksrt') . '</a>';
}
echo '</td>';
echo '<td>' . esc_html((string) (int) ($row['source_index'] ?? 0)) . '</td>';
echo '<td>' . esc_html((string) (int) ($row['cost'] ?? 0)) . '</td>';
echo '<td>' . esc_html($used . ' / ' . $max) . ($used > 0 ? ' <span class="dashicons dashicons-yes" style="color:green;"></span>' : '') . '</td>';
echo '<td>' . esc_html((string) ($row['expires_at'] ?? '')) . '</td>';
echo '<td><code>' . esc_html((string) ($row['ip_bind'] ?? '')) . '</code></td>';
echo '</tr>';
}
echo '</tbody></table>';
if ($pages > 1) {
echo '<div class="tablenav"><div class="tablenav-pages">';
echo paginate_links([
'base' => add_query_arg('paged', '%#%'),
'format' => '',
'prev_text' => '&laquo;',
'next_text' => '&raquo;',
'total' => $pages,
'current' => $page_num,
]);
echo '</div></div>';
}
echo '</div>';
}
/**
* @param array{post_id?:int,user_id?:int,only_used?:bool,limit?:int,offset?:int} $args
* @return array{items: array<int,array<string,mixed>>, total:int}
*/
private function query_logs(string $table, array $args): array {
global $wpdb;
$where = ['1=1'];
$params = [];
$post_id = (int) ($args['post_id'] ?? 0);
if ($post_id > 0) {
$where[] = 'post_id = %d';
$params[] = $post_id;
}
$user_id = (int) ($args['user_id'] ?? 0);
if ($user_id > 0) {
$where[] = 'user_id = %d';
$params[] = $user_id;
}
if (!empty($args['only_used'])) {
$where[] = 'used_count > 0';
}
$where_sql = implode(' AND ', $where);
$limit = max(1, min(100, (int) ($args['limit'] ?? 30)));
$offset = max(0, (int) ($args['offset'] ?? 0));
$count_sql = "SELECT COUNT(*) FROM {$table} WHERE {$where_sql}";
$total = $params
? (int) $wpdb->get_var($wpdb->prepare($count_sql, $params))
: (int) $wpdb->get_var($count_sql);
$sql = "SELECT * FROM {$table} WHERE {$where_sql} ORDER BY created_at DESC LIMIT %d OFFSET %d";
$list_params = $params;
$list_params[] = $limit;
$list_params[] = $offset;
$rows = $wpdb->get_results($wpdb->prepare($sql, $list_params), ARRAY_A);
return [
'items' => is_array($rows) ? $rows : [],
'total' => $total,
];
}
private function ensure_cap(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('您没有权限访问此页面。', 'aksrt'));
}
}
}
+183
View File
@@ -0,0 +1,183 @@
<?php
/**
* @package AKSRT
*/
namespace AKSRT\Admin\Native;
defined('ABSPATH') || exit;
class PayIncome {
public const CAPABILITY = 'manage_options';
public const PAGE_SLUG = 'aksrt_income';
private static $instance = null;
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('admin_post_aksrt_pay_settle_income', [$this, 'handle_settle_income']);
}
public function render_income_page(): void {
$this->ensure_cap();
$this->render_header(__('创作分成', 'aksrt'));
if (!class_exists('\AKSRT\Payment\Revenue')) {
echo '<div class="notice notice-error"><p>' . esc_html__('分成模块未初始化。', 'aksrt') . '</p></div>';
$this->render_footer();
return;
}
$status = isset($_GET['status']) ? sanitize_key((string) wp_unslash($_GET['status'])) : '';
$user_id = isset($_GET['user_id']) ? max(0, (int) $_GET['user_id']) : 0;
$page_num = max(1, (int) ($_GET['paged'] ?? 1));
$per_page = 20;
$svc = \AKSRT\Payment\Revenue::get_instance();
$stats = $user_id > 0 ? $svc->get_stats($user_id) : $svc->get_stats();
echo '<p>' . esc_html(sprintf(
/* translators: 1: total 2: pending 3: settled */
__('合计 %1$s · 待结算 %2$s · 已结算 %3$s', 'aksrt'),
number_format((float) ($stats['total'] ?? 0), 2),
number_format((float) ($stats['pending'] ?? 0), 2),
number_format((float) ($stats['settled'] ?? 0), 2)
)) . '</p>';
echo '<form method="get" style="margin:12px 0;">';
echo '<input type="hidden" name="page" value="aksrt_income" />';
echo '<select name="status">';
foreach (['' => __('全部状态', 'aksrt'), 'pending' => __('待结算', 'aksrt'), 'settled' => __('已结算', 'aksrt')] as $key => $label) {
printf('<option value="%s"%s>%s</option>', esc_attr($key), selected($status, $key, false), esc_html($label));
}
echo '</select> ';
echo '<input type="number" min="0" name="user_id" value="' . esc_attr($user_id > 0 ? (string) $user_id : '') . '" placeholder="' . esc_attr__('用户 ID', 'aksrt') . '" class="small-text" /> ';
submit_button(__('筛选', 'aksrt'), 'secondary', '', false);
if ($user_id > 0) {
echo ' <a class="button button-small" href="' . esc_url($this->page_url('aksrt_income')) . '">' . esc_html__('清除用户过滤', 'aksrt') . '</a>';
}
echo '</form>';
$list_args = [
'status' => $status,
'limit' => $per_page,
'offset' => ($page_num - 1) * $per_page,
];
if ($user_id > 0) {
$list_args['user_id'] = $user_id;
}
$result = $svc->get_list($list_args);
$items = $result['items'] ?? [];
$total = (int) ($result['total'] ?? 0);
$pages = max(1, (int) ($result['max_pages'] ?? 1));
echo '<table class="wp-list-table widefat fixed striped"><thead><tr>';
echo '<th>ID</th><th>' . esc_html__('用户', 'aksrt') . '</th><th>' . esc_html__('订单号', 'aksrt') . '</th><th>' . esc_html__('金额', 'aksrt') . '</th><th>' . esc_html__('状态', 'aksrt') . '</th><th>' . esc_html__('时间', 'aksrt') . '</th><th>' . esc_html__('操作', 'aksrt') . '</th>';
echo '</tr></thead><tbody>';
if (!$items) {
echo '<tr><td colspan="7">' . esc_html__('暂无分成记录', 'aksrt') . '</td></tr>';
}
foreach ($items as $row) {
$id = (int) ($row['id'] ?? 0);
$user_id = (int) ($row['user_id'] ?? 0);
$user = $user_id > 0 ? get_userdata($user_id) : null;
$st = (string) ($row['status'] ?? '');
echo '<tr>';
echo '<td>' . esc_html((string) $id) . '</td>';
echo '<td>' . ($user ? esc_html($user->display_name) : esc_html((string) $user_id)) . '</td>';
echo '<td><code>' . esc_html((string) ($row['order_number'] ?? '')) . '</code></td>';
echo '<td>' . esc_html(number_format((float) ($row['amount'] ?? 0), 2)) . '</td>';
echo '<td>' . esc_html($st) . '</td>';
echo '<td>' . esc_html((string) ($row['created_at'] ?? '')) . '</td>';
echo '<td>';
if ($st === 'pending') {
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '">';
echo '<input type="hidden" name="action" value="aksrt_pay_settle_income" />';
echo '<input type="hidden" name="revenue_id" value="' . esc_attr((string) $id) . '" />';
wp_nonce_field('aksrt_pay_settle_income_' . $id);
submit_button(__('结算', 'aksrt'), 'small', '', false);
echo '</form>';
} else {
echo '—';
}
echo '</td></tr>';
}
echo '</tbody></table>';
$this->render_pagination($pages, $page_num);
$this->render_footer();
}
public function handle_settle_income(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('权限不足', 'aksrt'));
}
$id = isset($_POST['revenue_id']) ? (int) $_POST['revenue_id'] : 0;
check_admin_referer('aksrt_pay_settle_income_' . $id);
$ok = $id > 0 && class_exists('\AKSRT\Payment\Revenue') && \AKSRT\Payment\Revenue::get_instance()->settle($id);
wp_safe_redirect($this->page_url('aksrt_income', ['aksrt_pay_msg' => $ok ? 'income_settled' : 'income_failed']));
exit;
}
private function ensure_cap(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('您没有权限访问此页面。', 'aksrt'));
}
}
private function render_header(string $title): void {
echo '<div class="wrap">';
echo '<h1>' . esc_html($title) . '</h1>';
$this->render_flash();
}
private function render_footer(): void {
echo '</div>';
}
private function render_flash(): void {
if (empty($_GET['aksrt_pay_msg'])) {
return;
}
$code = sanitize_key((string) wp_unslash($_GET['aksrt_pay_msg']));
$map = [
'income_settled' => __('分成已结算。', 'aksrt'),
'income_failed' => __('分成结算失败。', 'aksrt'),
];
if (!isset($map[$code])) {
return;
}
$class = str_contains($code, 'failed') ? 'notice-error' : 'notice-success';
echo '<div class="notice ' . esc_attr($class) . ' is-dismissible"><p>' . esc_html($map[$code]) . '</p></div>';
}
private function page_url(string $slug, array $args = []): string {
return add_query_arg(array_merge(['page' => $slug], $args), admin_url('admin.php'));
}
private function render_pagination(int $pages, int $page_num): void {
if ($pages <= 1) {
return;
}
echo '<div class="tablenav"><div class="tablenav-pages">';
echo paginate_links([
'base' => add_query_arg('paged', '%#%'),
'format' => '',
'prev_text' => '&laquo;',
'next_text' => '&raquo;',
'total' => $pages,
'current' => $page_num,
]);
echo '</div></div>';
}
}
+251
View File
@@ -0,0 +1,251 @@
<?php
/**
* @package AKSRT
*/
namespace AKSRT\Admin\Native;
defined('ABSPATH') || exit;
class PayMenus {
public const CAPABILITY = 'manage_options';
public const MENU_SLUG = 'aksrt_pay';
private static $instance = null;
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('admin_menu', [$this, 'register_menus']);
add_action('admin_notices', [$this, 'pending_notices']);
// Domain admin_post handlers are registered by dedicated modules.
}
/**
* @return array<string, array{slug:string,title:string,callback:callable}>
*/
private function pages(): array {
return [
'overview' => [
'slug' => self::MENU_SLUG,
'title' => __('概览', 'aksrt'),
'callback' => [$this, 'render_overview_page'],
],
'orders' => [
'slug' => 'aksrt_orders',
'title' => __('订单', 'aksrt'),
'callback' => static function (): void {
if (class_exists('AKSRT\Admin\Native\PayOrders')) {
PayOrders::get_instance()->render_orders_page();
return;
}
wp_die(esc_html__('订单模块未初始化。', 'aksrt'));
},
],
'products' => [
'slug' => 'aksrt_pay_products',
'title' => __('付费文章', 'aksrt'),
'callback' => static function (): void {
if (class_exists('AKSRT\\Admin\\Native\\PayProducts')) {
PayProducts::get_instance()->render_products_page();
return;
}
wp_die(esc_html__('付费文章模块未初始化。', 'aksrt'));
},
],
'download_logs' => [
'slug' => 'aksrt_download_logs',
'title' => __('下载记录', 'aksrt'),
'callback' => static function (): void {
if (class_exists('AKSRT\\Admin\\Native\\PayDownloadLogs')) {
PayDownloadLogs::get_instance()->render_page();
return;
}
wp_die(esc_html__('下载记录模块未初始化。', 'aksrt'));
},
],
'coupons' => [
'slug' => 'aksrt_coupons',
'title' => __('优惠码', 'aksrt'),
'callback' => static function (): void {
if (class_exists('AKSRT\\Admin\\Native\\PayCoupons')) {
PayCoupons::get_instance()->render_page();
return;
}
wp_die(esc_html__('优惠码模块未初始化。', 'aksrt'));
},
],
'charge_cards' => [
'slug' => 'aksrt_charge_cards',
'title' => __('卡密管理', 'aksrt'),
'callback' => static function (): void {
if (class_exists('AKSRT\\Admin\\Native\\PayChargeCards')) {
PayChargeCards::get_instance()->render_charge_cards_page();
return;
}
wp_die(esc_html__('卡密模块未初始化。', 'aksrt'));
},
],
'income' => [
'slug' => 'aksrt_income',
'title' => __('创作分成', 'aksrt'),
'callback' => static function (): void {
if (class_exists('AKSRT\\Admin\\Native\\PayIncome')) {
PayIncome::get_instance()->render_income_page();
return;
}
wp_die(esc_html__('分成模块未初始化。', 'aksrt'));
},
],
'rebate' => [
'slug' => 'aksrt_rebate',
'title' => __('推广返佣', 'aksrt'),
'callback' => static function (): void {
if (class_exists('AKSRT\\Admin\\Native\\PayRebate')) {
PayRebate::get_instance()->render_rebate_page();
return;
}
wp_die(esc_html__('返佣模块未初始化。', 'aksrt'));
},
],
'withdraw' => [
'slug' => 'aksrt_withdraw',
'title' => __('提现审核', 'aksrt'),
'callback' => static function (): void {
if (class_exists('AKSRT\\Admin\\Native\\PayWithdrawals')) {
PayWithdrawals::get_instance()->render_withdrawals_page();
return;
}
wp_die(esc_html__('提现模块未初始化。', 'aksrt'));
},
],
];
}
public function register_menus(): void {
if (!current_user_can(self::CAPABILITY)) {
return;
}
add_menu_page(
__('支付中心', 'aksrt'),
__('支付中心', 'aksrt'),
self::CAPABILITY,
self::MENU_SLUG,
[$this, 'render_overview_page'],
'dashicons-cart',
36
);
foreach ($this->pages() as $page) {
add_submenu_page(
self::MENU_SLUG,
$page['title'],
$page['title'],
self::CAPABILITY,
$page['slug'],
$page['callback']
);
}
}
public function pending_notices(): void {
if (!current_user_can(self::CAPABILITY)) {
return;
}
$page = isset($_GET['page']) ? sanitize_key((string) wp_unslash($_GET['page'])) : '';
if ($page !== 'aksrt_withdraw' && class_exists('\AKSRT\Payment\Withdrawal')) {
$pending = (int) \AKSRT\Payment\Withdrawal::get_instance()->get_count(['status' => \AKSRT\Payment\Withdrawal::STATUS_PENDING]);
if ($pending > 0) {
$url = admin_url('admin.php?page=aksrt_withdraw&status=pending');
echo '<div class="notice notice-info is-dismissible">';
echo '<h3>' . esc_html__('提现申请待处理', 'aksrt') . '</h3>';
echo '<p>' . esc_html(sprintf(
/* translators: %d: pending withdrawal count */
__('您有 %d 个提现申请待处理', 'aksrt'),
$pending
)) . '</p>';
echo '<p><a class="button button-primary" href="' . esc_url($url) . '">' . esc_html__('立即处理', 'aksrt') . '</a></p>';
echo '</div>';
}
}
}
private function ensure_cap(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('您没有权限访问此页面。', 'aksrt'));
}
}
private function render_header(string $title): void {
echo '<div class="wrap">';
echo '<h1>' . esc_html($title) . '</h1>';
}
private function render_footer(): void {
echo '</div>';
}
private function page_url(string $slug, array $args = []): string {
return add_query_arg(array_merge(['page' => $slug], $args), admin_url('admin.php'));
}
public function render_overview_page(): void {
$this->ensure_cap();
$this->render_header(__('支付中心', 'aksrt'));
$order_stats = class_exists('\AKSRT\Payment\OrderRepository')
? \AKSRT\Payment\OrderRepository::get_instance()->get_stats()
: [];
$withdraw_stats = class_exists('\AKSRT\Payment\Withdrawal')
? \AKSRT\Payment\Withdrawal::get_instance()->get_stats()
: [];
$card_stats = class_exists('\AKSRT\Payment\ChargeCard')
? \AKSRT\Payment\ChargeCard::get_instance()->get_stats()
: [];
echo '<p class="description">' . esc_html__('运营数据入口。配置项仍在 AKSRT 主题设置 SPA。', 'aksrt') . '</p>';
echo '<div class="aksrt-pay-stats" style="display:flex;flex-wrap:wrap;gap:16px;margin:20px 0;">';
$this->stat_card(__('订单总数', 'aksrt'), (string) (int) ($order_stats['total'] ?? 0), $this->page_url('aksrt_orders'));
$this->stat_card(__('待支付', 'aksrt'), (string) (int) ($order_stats['pending'] ?? 0), $this->page_url('aksrt_orders', ['status' => 'pending']));
$this->stat_card(__('已支付', 'aksrt'), (string) (int) ($order_stats['paid'] ?? 0), $this->page_url('aksrt_orders', ['status' => 'paid']));
$this->stat_card(__('营收', 'aksrt'), number_format((float) ($order_stats['revenue'] ?? 0), 2), $this->page_url('aksrt_orders'));
$this->stat_card(__('待审提现', 'aksrt'), (string) (int) ($withdraw_stats['pending'] ?? 0), $this->page_url('aksrt_withdraw', ['status' => 'pending']));
$this->stat_card(__('可用卡密', 'aksrt'), (string) (int) ($card_stats['active'] ?? 0), $this->page_url('aksrt_charge_cards', ['status' => 'active']));
echo '</div>';
echo '<h2>' . esc_html__('快捷入口', 'aksrt') . '</h2><ul>';
foreach ($this->pages() as $page) {
if ($page['slug'] === self::MENU_SLUG) {
continue;
}
echo '<li><a href="' . esc_url($this->page_url($page['slug'])) . '">' . esc_html($page['title']) . '</a></li>';
}
echo '</ul>';
$spa = admin_url('admin.php?page=aksrt-settings#/payment/gateway');
echo '<p><a class="button" href="' . esc_url($spa) . '">' . esc_html__('打开支付配置(SPA)', 'aksrt') . '</a></p>';
$this->render_footer();
}
private function stat_card(string $label, string $value, string $url): void {
echo '<a href="' . esc_url($url) . '" style="display:block;min-width:140px;padding:16px;background:#fff;border:1px solid #c3c4c7;border-radius:4px;text-decoration:none;color:inherit;">';
echo '<div style="font-size:12px;color:#646970;">' . esc_html($label) . '</div>';
echo '<div style="font-size:22px;font-weight:600;margin-top:6px;">' . esc_html($value) . '</div>';
echo '</a>';
}
}
+529
View File
@@ -0,0 +1,529 @@
<?php
/**
* @package AKSRT
*/
namespace AKSRT\Admin\Native;
defined('ABSPATH') || exit;
class PayOrders {
public const CAPABILITY = 'manage_options';
public const PAGE_SLUG = 'aksrt_orders';
private static $instance = null;
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('admin_post_aksrt_pay_update_order_status', [$this, 'handle_update_order_status']);
add_action('admin_post_aksrt_pay_update_order_note', [$this, 'handle_update_order_note']);
add_action('admin_post_aksrt_pay_clear_closed_orders', [$this, 'handle_clear_closed_orders']);
add_action('admin_post_aksrt_pay_delete_order', [$this, 'handle_delete_order']);
}
public function render_orders_page(): void {
$this->ensure_cap();
$this->render_header(__('订单', 'aksrt'));
if (!class_exists('\AKSRT\Payment\OrderRepository')) {
echo '<div class="notice notice-error"><p>' . esc_html__('订单服务不可用。', 'aksrt') . '</p></div>';
$this->render_footer();
return;
}
$detail_order = isset($_GET['order']) ? sanitize_text_field((string) wp_unslash($_GET['order'])) : '';
if ($detail_order !== '') {
$this->render_order_detail_page($detail_order);
$this->render_footer();
return;
}
$status = isset($_GET['status']) ? sanitize_key((string) wp_unslash($_GET['status'])) : '';
$search = isset($_GET['s']) ? sanitize_text_field((string) wp_unslash($_GET['s'])) : '';
$product_type = isset($_GET['product_type']) ? sanitize_key((string) wp_unslash($_GET['product_type'])) : '';
$product_id = isset($_GET['product_id']) ? max(0, (int) $_GET['product_id']) : 0;
$user_id = isset($_GET['user_id']) ? max(0, (int) $_GET['user_id']) : 0;
$pay_method = isset($_GET['payment_method']) ? sanitize_key((string) wp_unslash($_GET['payment_method'])) : '';
$page_num = max(1, (int) ($_GET['paged'] ?? 1));
$per_page = 20;
$repo = \AKSRT\Payment\OrderRepository::get_instance();
$args = [
'limit' => $per_page,
'offset' => ($page_num - 1) * $per_page,
];
if ($status !== '' && $status !== 'all') {
$args['status'] = $status;
}
if ($product_type !== '') {
$args['product_type'] = $product_type;
}
if ($product_id > 0) {
$args['product_id'] = $product_id;
}
if ($user_id > 0) {
$args['user_id'] = $user_id;
}
if ($pay_method !== '') {
$args['payment_method'] = $pay_method;
}
$items = $repo->get_orders_admin($args, $search);
$total = $repo->get_orders_count($args, $search);
$pages = max(1, (int) ceil($total / $per_page));
$labels = class_exists('\AKSRT\Core\DomainModel')
? \AKSRT\Core\DomainModel::order_status_labels()
: [];
// Type tabs (zibll order subsubsub parity).
$type_tabs = [
'' => __('全部类型', 'aksrt'),
'resource' => __('内容付费', 'aksrt'),
'vip' => __('VIP', 'aksrt'),
'recharge' => __('充值', 'aksrt'),
'shop_product' => __('商城商品', 'aksrt'),
];
echo '<ul class="subsubsub" style="float:none;margin:8px 0 12px;">';
$tab_bits = [];
foreach ($type_tabs as $tkey => $tlabel) {
$url = $this->page_url('aksrt_orders', array_filter([
'product_type' => $tkey !== '' ? $tkey : null,
'product_id' => $product_id > 0 ? $product_id : null,
'user_id' => $user_id > 0 ? $user_id : null,
'payment_method' => $pay_method !== '' ? $pay_method : null,
'status' => ($status !== '' && $status !== 'all') ? $status : null,
]));
$cls = ($product_type === $tkey) ? 'current' : '';
$tab_bits[] = '<li><a class="' . esc_attr($cls) . '" href="' . esc_url($url) . '">' . esc_html($tlabel) . '</a></li>';
}
echo implode(' | ', $tab_bits);
echo '</ul><div style="clear:both;"></div>';
// Payment method quick tabs.
$method_tabs = [
'' => __('全部支付', 'aksrt'),
'balance' => __('余额', 'aksrt'),
'points' => __('积分', 'aksrt'),
'alipay' => __('支付宝', 'aksrt'),
'wechat' => __('微信', 'aksrt'),
];
echo '<ul class="subsubsub" style="float:none;margin:0 0 12px;">';
$m_bits = [];
foreach ($method_tabs as $mkey => $mlabel) {
$url = $this->page_url('aksrt_orders', array_filter([
'product_type' => $product_type !== '' ? $product_type : null,
'product_id' => $product_id > 0 ? $product_id : null,
'user_id' => $user_id > 0 ? $user_id : null,
'payment_method' => $mkey !== '' ? $mkey : null,
'status' => ($status !== '' && $status !== 'all') ? $status : null,
]));
$cls = ($pay_method === $mkey) ? 'current' : '';
$m_bits[] = '<li><a class="' . esc_attr($cls) . '" href="' . esc_url($url) . '">' . esc_html($mlabel) . '</a></li>';
}
echo implode(' | ', $m_bits);
echo '</ul><div style="clear:both;"></div>';
echo '<form method="get" style="margin:12px 0;">';
echo '<input type="hidden" name="page" value="aksrt_orders" />';
if ($product_type !== '') {
echo '<input type="hidden" name="product_type" value="' . esc_attr($product_type) . '" />';
}
if ($product_id > 0) {
echo '<input type="hidden" name="product_id" value="' . esc_attr((string) $product_id) . '" />';
}
if ($pay_method !== '') {
echo '<input type="hidden" name="payment_method" value="' . esc_attr($pay_method) . '" />';
}
echo '<select name="status">';
echo '<option value="">' . esc_html__('全部状态', 'aksrt') . '</option>';
foreach ($labels as $key => $label) {
printf(
'<option value="%s"%s>%s</option>',
esc_attr($key),
selected($status, $key, false),
esc_html($label)
);
}
echo '</select> ';
echo '<input type="number" min="0" name="user_id" value="' . esc_attr($user_id > 0 ? (string) $user_id : '') . '" placeholder="' . esc_attr__('用户 ID', 'aksrt') . '" class="small-text" /> ';
echo '<input type="search" name="s" value="' . esc_attr($search) . '" placeholder="' . esc_attr__('订单号 / 类型 / 用户', 'aksrt') . '" /> ';
submit_button(__('筛选', 'aksrt'), 'secondary', '', false);
if ($product_id > 0 || $product_type !== '' || $user_id > 0 || $pay_method !== '') {
$filter_bits = [];
if ($product_type !== '') {
$filter_bits[] = sprintf(__('类型=%s', 'aksrt'), $product_type);
}
if ($product_id > 0) {
$filter_bits[] = sprintf(__('商品ID=%d', 'aksrt'), $product_id);
}
if ($user_id > 0) {
$filter_bits[] = sprintf(__('用户=%d', 'aksrt'), $user_id);
}
if ($pay_method !== '') {
$filter_bits[] = sprintf(__('支付=%s', 'aksrt'), $pay_method);
}
echo ' <span class="description">' . esc_html(implode(' · ', $filter_bits)) . '</span>';
echo ' <a class="button button-small" href="' . esc_url($this->page_url('aksrt_orders')) . '">' . esc_html__('清除过滤', 'aksrt') . '</a>';
}
echo '</form>';
// Hygiene actions (zibll clear_order / delete).
echo '<p style="margin:8px 0 12px;">';
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="display:inline;" onsubmit="return confirm(\'' . esc_js(__('将删除 14 天前所有已关闭/已过期订单,不可恢复。确认清理?', 'aksrt')) . '\');">';
echo '<input type="hidden" name="action" value="aksrt_pay_clear_closed_orders" />';
echo '<input type="hidden" name="days" value="14" />';
wp_nonce_field('aksrt_pay_clear_closed_orders');
submit_button(__('清理 14 天前关闭订单', 'aksrt'), 'secondary', '', false);
echo '</form>';
echo '</p>';
echo '<table class="wp-list-table widefat fixed striped">';
echo '<thead><tr>';
echo '<th>' . esc_html__('订单号', 'aksrt') . '</th>';
echo '<th>' . esc_html__('用户', 'aksrt') . '</th>';
echo '<th>' . esc_html__('类型', 'aksrt') . '</th>';
echo '<th>' . esc_html__('金额', 'aksrt') . '</th>';
echo '<th>' . esc_html__('状态', 'aksrt') . '</th>';
echo '<th>' . esc_html__('时间', 'aksrt') . '</th>';
echo '<th>' . esc_html__('操作', 'aksrt') . '</th>';
echo '</tr></thead><tbody>';
if (!$items) {
echo '<tr><td colspan="7">' . esc_html__('暂无订单', 'aksrt') . '</td></tr>';
}
foreach ($items as $row) {
$order_number = (string) ($row['order_number'] ?? '');
$user_id = (int) ($row['user_id'] ?? 0);
$user = $user_id > 0 ? get_userdata($user_id) : null;
$status_key = (string) ($row['status'] ?? '');
$status_label = $labels[$status_key] ?? $status_key;
echo '<tr>';
echo '<td><code>' . esc_html($order_number) . '</code></td>';
echo '<td>' . ($user ? esc_html($user->display_name . ' (#' . $user_id . ')') : esc_html((string) $user_id)) . '</td>';
echo '<td>' . esc_html((string) ($row['product_type'] ?? '')) . '</td>';
echo '<td>' . esc_html(number_format((float) ($row['amount'] ?? 0), 2)) . ' ' . esc_html((string) ($row['currency'] ?? '')) . '</td>';
echo '<td>' . esc_html($status_label) . '</td>';
echo '<td>' . esc_html((string) ($row['created_at'] ?? '')) . '</td>';
echo '<td style="display:flex;gap:6px;flex-wrap:wrap;align-items:center;">';
echo '<a class="button button-small" href="' . esc_url($this->page_url('aksrt_orders', ['order' => $order_number])) . '">' . esc_html__('详情', 'aksrt') . '</a>';
$this->render_order_quick_status_buttons($order_number, $status_key);
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="display:flex;gap:4px;align-items:center;">';
echo '<input type="hidden" name="action" value="aksrt_pay_update_order_status" />';
echo '<input type="hidden" name="order_number" value="' . esc_attr($order_number) . '" />';
wp_nonce_field('aksrt_pay_update_order_status_' . $order_number);
echo '<select name="new_status">';
foreach ($labels as $key => $label) {
printf(
'<option value="%s"%s>%s</option>',
esc_attr($key),
selected($status_key, $key, false),
esc_html($label)
);
}
echo '</select>';
submit_button(__('更新', 'aksrt'), 'small', '', false);
echo '</form>';
if (in_array($status_key, ['cancelled', 'expired'], true)) {
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="display:inline;" onsubmit="return confirm(\'' . esc_js(__('确认永久删除此订单?不可恢复。', 'aksrt')) . '\');">';
echo '<input type="hidden" name="action" value="aksrt_pay_delete_order" />';
echo '<input type="hidden" name="order_number" value="' . esc_attr($order_number) . '" />';
wp_nonce_field('aksrt_pay_delete_order_' . $order_number);
submit_button(__('删除', 'aksrt'), 'small', '', false);
echo '</form>';
}
echo '</td>';
echo '</tr>';
}
echo '</tbody></table>';
if ($pages > 1) {
echo '<div class="tablenav"><div class="tablenav-pages">';
echo paginate_links([
'base' => add_query_arg('paged', '%#%'),
'format' => '',
'prev_text' => '&laquo;',
'next_text' => '&raquo;',
'total' => $pages,
'current' => $page_num,
]);
echo '</div></div>';
}
$this->render_footer();
}
private function render_order_detail_page(string $order_number): void {
$repo = \AKSRT\Payment\OrderRepository::get_instance();
$order = $repo->get($order_number);
if (!$order) {
echo '<div class="notice notice-error"><p>' . esc_html__('订单不存在。', 'aksrt') . '</p></div>';
echo '<p><a class="button" href="' . esc_url($this->page_url('aksrt_orders')) . '">' . esc_html__('返回订单列表', 'aksrt') . '</a></p>';
return;
}
$labels = class_exists('\AKSRT\Core\DomainModel')
? \AKSRT\Core\DomainModel::order_status_labels()
: [];
$context = $repo->get_admin_order_context($order_number);
$user_id = (int) ($order['user_id'] ?? 0);
$user = $user_id > 0 ? get_userdata($user_id) : null;
$status = (string) ($order['status'] ?? '');
$meta = is_array($order['meta_payload'] ?? null) ? $order['meta_payload'] : [];
$customer_note = (string) ($context['customer_note'] ?? $order['note_text'] ?? '');
$admin_note = is_array($meta) ? (string) ($meta['admin_note'] ?? '') : '';
if ($admin_note === '' && $customer_note === '' && !empty($order['note']) && !is_array(json_decode((string) $order['note'], true))) {
$admin_note = (string) $order['note'];
}
echo '<p><a class="button" href="' . esc_url($this->page_url('aksrt_orders')) . '">&larr; ' . esc_html__('返回订单列表', 'aksrt') . '</a></p>';
echo '<table class="widefat striped" style="max-width:960px;"><tbody>';
$rows = [
__('订单号', 'aksrt') => $order_number,
__('用户', 'aksrt') => $user ? $user->display_name . ' (#' . $user_id . ')' : (string) $user_id,
__('商品类型', 'aksrt') => (string) ($order['product_type'] ?? ''),
__('商品 ID', 'aksrt') => (string) ($order['product_id'] ?? ''),
__('金额', 'aksrt') => number_format((float) ($order['amount'] ?? 0), 2) . ' ' . (string) ($order['currency'] ?? ''),
__('状态', 'aksrt') => $labels[$status] ?? $status,
__('支付方式', 'aksrt') => (string) ($order['payment_method'] ?? ''),
__('交易号', 'aksrt') => (string) ($order['payment_transaction_id'] ?? ''),
__('创建时间', 'aksrt') => (string) ($order['created_at'] ?? ''),
__('支付时间', 'aksrt') => (string) ($order['paid_at'] ?? ''),
__('买家备注', 'aksrt') => $customer_note,
];
foreach ($rows as $label => $value) {
echo '<tr><th style="width:160px;">' . esc_html($label) . '</th><td>' . esc_html((string) $value) . '</td></tr>';
}
if (!empty($context)) {
echo '<tr><th>' . esc_html__('附加上下文', 'aksrt') . '</th><td><pre style="white-space:pre-wrap;margin:0;">' . esc_html(wp_json_encode($context, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)) . '</pre></td></tr>';
}
echo '</tbody></table>';
echo '<h2 style="margin-top:20px;">' . esc_html__('运营备注', 'aksrt') . '</h2>';
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="max-width:960px;">';
echo '<input type="hidden" name="action" value="aksrt_pay_update_order_note" />';
echo '<input type="hidden" name="order_number" value="' . esc_attr($order_number) . '" />';
wp_nonce_field('aksrt_pay_update_order_note_' . $order_number);
echo '<p><textarea name="admin_note" rows="3" class="large-text">' . esc_textarea($admin_note) . '</textarea></p>';
submit_button(__('保存备注', 'aksrt'), 'secondary', '', false);
echo '</form>';
echo '<h2 style="margin-top:20px;">' . esc_html__('更新状态', 'aksrt') . '</h2>';
echo '<div style="display:flex;gap:8px;flex-wrap:wrap;align-items:center;margin-bottom:10px;">';
$this->render_order_quick_status_buttons($order_number, $status, true);
echo '</div>';
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="display:flex;gap:8px;align-items:center;">';
echo '<input type="hidden" name="action" value="aksrt_pay_update_order_status" />';
echo '<input type="hidden" name="order_number" value="' . esc_attr($order_number) . '" />';
echo '<input type="hidden" name="redirect_order" value="' . esc_attr($order_number) . '" />';
wp_nonce_field('aksrt_pay_update_order_status_' . $order_number);
echo '<select name="new_status">';
foreach ($labels as $key => $label) {
printf('<option value="%s"%s>%s</option>', esc_attr($key), selected($status, $key, false), esc_html($label));
}
echo '</select> ';
submit_button(__('保存状态', 'aksrt'), 'primary', '', false);
echo '</form>';
}
public function handle_update_order_status(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('权限不足', 'aksrt'));
}
$order_number = isset($_POST['order_number']) ? sanitize_text_field((string) wp_unslash($_POST['order_number'])) : '';
$new_status = isset($_POST['new_status']) ? sanitize_key((string) wp_unslash($_POST['new_status'])) : '';
check_admin_referer('aksrt_pay_update_order_status_' . $order_number);
$ok = false;
if ($order_number !== '' && $new_status !== '' && class_exists('\AKSRT\Payment\OrderRepository')) {
$ok = \AKSRT\Payment\OrderRepository::get_instance()->update_status($order_number, $new_status, ['force' => true]);
}
$args = ['aksrt_pay_msg' => $ok ? 'order_updated' : 'order_failed'];
$redirect_order = isset($_POST['redirect_order']) ? sanitize_text_field((string) wp_unslash($_POST['redirect_order'])) : '';
if ($redirect_order !== '') {
$args['order'] = $redirect_order;
}
wp_safe_redirect($this->page_url('aksrt_orders', $args));
exit;
}
public function handle_update_order_note(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('权限不足', 'aksrt'));
}
$order_number = isset($_POST['order_number']) ? sanitize_text_field((string) wp_unslash($_POST['order_number'])) : '';
$admin_note = isset($_POST['admin_note']) ? sanitize_textarea_field((string) wp_unslash($_POST['admin_note'])) : '';
check_admin_referer('aksrt_pay_update_order_note_' . $order_number);
$ok = false;
if ($order_number !== '' && class_exists('\AKSRT\Payment\OrderRepository')) {
$ok = \AKSRT\Payment\OrderRepository::get_instance()->update_admin_note($order_number, $admin_note);
}
wp_safe_redirect($this->page_url('aksrt_orders', [
'order' => $order_number,
'aksrt_pay_msg' => $ok ? 'order_note_updated' : 'order_note_failed',
]));
exit;
}
public function handle_clear_closed_orders(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('权限不足', 'aksrt'));
}
check_admin_referer('aksrt_pay_clear_closed_orders');
$days = isset($_POST['days']) ? max(1, min(3650, (int) $_POST['days'])) : 14;
$deleted = 0;
if (class_exists('\AKSRT\Payment\OrderRepository')) {
$deleted = \AKSRT\Payment\OrderRepository::get_instance()->clear_closed_orders($days);
}
wp_safe_redirect($this->page_url('aksrt_orders', [
'aksrt_pay_msg' => $deleted > 0 ? 'orders_cleared' : 'orders_clear_empty',
'cleared' => $deleted,
]));
exit;
}
public function handle_delete_order(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('权限不足', 'aksrt'));
}
$order_number = isset($_POST['order_number']) ? sanitize_text_field((string) wp_unslash($_POST['order_number'])) : '';
check_admin_referer('aksrt_pay_delete_order_' . $order_number);
$ok = false;
if ($order_number !== '' && class_exists('\AKSRT\Payment\OrderRepository')) {
$order = \AKSRT\Payment\OrderRepository::get_instance()->get($order_number);
$status = is_array($order) ? (string) ($order['status'] ?? '') : '';
// Only allow hard-delete of closed/expired rows (safety).
if (in_array($status, ['cancelled', 'expired'], true)) {
$ok = \AKSRT\Payment\OrderRepository::get_instance()->delete_order($order_number);
}
}
wp_safe_redirect($this->page_url('aksrt_orders', [
'aksrt_pay_msg' => $ok ? 'order_deleted' : 'order_delete_failed',
]));
exit;
}
private function render_order_quick_status_buttons(string $order_number, string $current_status, bool $redirect_detail = false): void {
$actions = [
'completed' => __('完成', 'aksrt'),
'refunded' => __('退款', 'aksrt'),
'cancelled' => __('关闭', 'aksrt'),
];
foreach ($actions as $status => $label) {
if ($status === $current_status) {
continue;
}
$confirm = '';
if ($status === 'refunded') {
$confirm = __('确认将该订单标记为已退款?', 'aksrt');
} elseif ($status === 'cancelled') {
$confirm = __('确认关闭该订单?', 'aksrt');
} elseif ($status === 'completed') {
$confirm = __('确认完成该订单?', 'aksrt');
}
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="display:inline;">';
echo '<input type="hidden" name="action" value="aksrt_pay_update_order_status" />';
echo '<input type="hidden" name="order_number" value="' . esc_attr($order_number) . '" />';
echo '<input type="hidden" name="new_status" value="' . esc_attr($status) . '" />';
if ($redirect_detail) {
echo '<input type="hidden" name="redirect_order" value="' . esc_attr($order_number) . '" />';
}
wp_nonce_field('aksrt_pay_update_order_status_' . $order_number);
printf(
'<button type="submit" class="button button-small"%s>%s</button>',
$confirm !== '' ? ' onclick="return confirm(\'' . esc_js($confirm) . '\');"' : '',
esc_html($label)
);
echo '</form>';
}
}
private function ensure_cap(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('您没有权限访问此页面。', 'aksrt'));
}
}
private function render_header(string $title): void {
echo '<div class="wrap">';
echo '<h1>' . esc_html($title) . '</h1>';
$this->render_flash();
}
private function render_footer(): void {
echo '</div>';
}
private function render_flash(): void {
if (empty($_GET['aksrt_pay_msg'])) {
return;
}
$code = sanitize_key((string) wp_unslash($_GET['aksrt_pay_msg']));
$map = [
'order_updated' => __('订单状态已更新。', 'aksrt'),
'order_failed' => __('订单状态更新失败。', 'aksrt'),
'order_note_updated' => __('订单备注已保存。', 'aksrt'),
'order_note_failed' => __('订单备注保存失败。', 'aksrt'),
'orders_cleared' => sprintf(
/* translators: %d deleted count */
__('已清理 %d 条关闭订单。', 'aksrt'),
max(0, (int) ($_GET['cleared'] ?? 0))
),
'orders_clear_empty' => __('没有可清理的关闭订单。', 'aksrt'),
'order_deleted' => __('订单已删除。', 'aksrt'),
'order_delete_failed' => __('订单删除失败(仅允许删除已关闭/过期订单)。', 'aksrt'),
'shipping_updated' => __('物流状态已更新。', 'aksrt'),
'shipping_failed' => __('物流状态更新失败。', 'aksrt'),
'shipping_created' => __('发货记录已创建。', 'aksrt'),
'shipping_create_failed' => __('发货记录创建失败。', 'aksrt'),
'after_sale_created' => __('售后工单已创建。', 'aksrt'),
'after_sale_create_failed' => __('售后工单创建失败。', 'aksrt'),
];
if (!isset($map[$code])) {
return;
}
$class = str_contains($code, 'failed') ? 'notice-error' : 'notice-success';
echo '<div class="notice ' . esc_attr($class) . ' is-dismissible"><p>' . esc_html($map[$code]) . '</p></div>';
}
private function page_url(string $slug, array $args = []): string {
return add_query_arg(array_merge(['page' => $slug], $args), admin_url('admin.php'));
}
private function render_pagination(int $pages, int $page_num): void {
if ($pages <= 1) {
return;
}
echo '<div class="tablenav"><div class="tablenav-pages">';
echo paginate_links([
'base' => add_query_arg('paged', '%#%'),
'format' => '',
'prev_text' => '&laquo;',
'next_text' => '&raquo;',
'total' => $pages,
'current' => $page_num,
]);
echo '</div></div>';
}
}
+368
View File
@@ -0,0 +1,368 @@
<?php
/**
* @package AKSRT
*/
namespace AKSRT\Admin\Native;
defined('ABSPATH') || exit;
class PayProducts {
public const CAPABILITY = 'manage_options';
public const PAGE_SLUG = 'aksrt_pay_products';
private static $instance = null;
public static function get_instance(): self {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
public function init(): void {
// Read-only page; menu callback only.
}
public function render_products_page(): void {
$this->ensure_cap();
$this->render_header(__('付费文章', 'aksrt'));
if (!class_exists('\AKSRT\Payment\OrderRepository') || !class_exists('\AKSRT\Core\DomainModel')) {
echo '<div class="notice notice-error"><p>' . esc_html__('订单服务不可用。', 'aksrt') . '</p></div>';
$this->render_footer();
return;
}
$pay_type = isset($_GET['pay_type']) ? sanitize_key((string) wp_unslash($_GET['pay_type'])) : '';
$search = isset($_GET['s']) ? sanitize_text_field((string) wp_unslash($_GET['s'])) : '';
$page_num = max(1, (int) ($_GET['paged'] ?? 1));
$per_page = 20;
$type_labels = \AKSRT\Core\DomainModel::pay_type_labels();
unset($type_labels[\AKSRT\Core\DomainModel::PAY_TYPE_NO]);
$query = $this->query_paid_posts([
'pay_type' => $pay_type,
'search' => $search,
'page' => $page_num,
'per_page' => $per_page,
]);
$posts = $query['posts'];
$total = $query['total'];
$pages = max(1, (int) ceil($total / $per_page));
$sales_map = [];
if ($posts && class_exists('\AKSRT\Payment\OrderRepository')) {
$ids = array_map(static function ($p) {
return (int) $p->ID;
}, $posts);
$sales_map = \AKSRT\Payment\OrderRepository::get_instance()->get_content_sales_map(
$ids,
\AKSRT\Core\DomainModel::ORDER_PRODUCT_RESOURCE
);
}
$this->render_filters($pay_type, $search, $type_labels);
echo '<table class="wp-list-table widefat fixed striped">';
echo '<thead><tr>';
echo '<th style="width:28%;">' . esc_html__('商品', 'aksrt') . '</th>';
echo '<th>' . esc_html__('ID', 'aksrt') . '</th>';
echo '<th>' . esc_html__('付费类型', 'aksrt') . '</th>';
echo '<th>' . esc_html__('模式', 'aksrt') . '</th>';
echo '<th>' . esc_html__('标价', 'aksrt') . '</th>';
echo '<th>' . esc_html__('销售均价', 'aksrt') . '</th>';
echo '<th>' . esc_html__('订单销量', 'aksrt') . '</th>';
echo '<th>' . esc_html__('Meta 销量', 'aksrt') . '</th>';
echo '<th>' . esc_html__('销售总额', 'aksrt') . '</th>';
echo '<th>' . esc_html__('操作', 'aksrt') . '</th>';
echo '</tr></thead><tbody>';
if (!$posts) {
echo '<tr><td colspan="10">' . esc_html__('暂无付费文章。', 'aksrt') . '</td></tr>';
}
foreach ($posts as $post) {
$post_id = (int) $post->ID;
$meta = function_exists('aksrt_get_pay_meta') ? aksrt_get_pay_meta($post) : [];
$pt = \AKSRT\Core\DomainModel::normalize_pay_type((string) ($meta['pay_type'] ?? \AKSRT\Core\DomainModel::PAY_TYPE_NO));
$pt_label = $type_labels[$pt] ?? $pt;
$modo = (($meta['pay_modo'] ?? '0') === 'points') ? 'points' : '0';
$modo_label = $modo === 'points' ? __('积分', 'aksrt') : __('现金', 'aksrt');
if ($modo === 'points') {
$list_price = (int) ceil((float) ($meta['points_price'] ?? $meta['price'] ?? 0));
$price_text = sprintf(__('%d 积分', 'aksrt'), $list_price);
} else {
$list_price = round((float) ($meta['price'] ?? 0), 2);
$price_text = number_format($list_price, 2);
}
$sales = $sales_map[$post_id] ?? ['sales_count' => 0, 'avg_amount' => 0.0, 'revenue' => 0.0];
$db_sales = (int) ($sales['sales_count'] ?? 0);
$avg = (float) ($sales['avg_amount'] ?? 0);
$revenue = (float) ($sales['revenue'] ?? 0);
$meta_sales = (int) get_post_meta($post_id, \AKSRT\Core\DomainModel::RESOURCE_SALES_VOLUME_KEY, true);
$edit_url = get_edit_post_link($post_id, 'raw');
$title = get_the_title($post_id);
if ($title === '') {
$title = sprintf(__('(无标题 #%d)', 'aksrt'), $post_id);
}
$orders_url = $this->page_url('aksrt_orders', [
'product_type' => \AKSRT\Core\DomainModel::ORDER_PRODUCT_RESOURCE,
'product_id' => $post_id,
]);
echo '<tr>';
echo '<td>';
if ($edit_url) {
echo '<a href="' . esc_url($edit_url) . '"><strong>' . esc_html($title) . '</strong></a>';
} else {
echo '<strong>' . esc_html($title) . '</strong>';
}
echo '</td>';
echo '<td>' . esc_html((string) $post_id) . '</td>';
echo '<td>' . esc_html((string) $pt_label) . '</td>';
echo '<td>' . esc_html($modo_label) . '</td>';
echo '<td>' . esc_html($price_text) . '</td>';
echo '<td>' . esc_html(number_format($avg, 2)) . '</td>';
echo '<td>' . esc_html((string) $db_sales) . '</td>';
echo '<td>' . esc_html((string) max(0, $meta_sales)) . '</td>';
echo '<td>' . esc_html(number_format($revenue, 2)) . '</td>';
echo '<td style="display:flex;gap:6px;flex-wrap:wrap;">';
if ($edit_url) {
echo '<a class="button button-small" href="' . esc_url($edit_url) . '">' . esc_html__('编辑', 'aksrt') . '</a>';
}
echo '<a class="button button-small" href="' . esc_url($orders_url) . '">' . esc_html__('订单', 'aksrt') . '</a>';
if ($pt === \AKSRT\Core\DomainModel::PAY_TYPE_DOWNLOAD) {
$dl_url = $this->page_url('aksrt_download_logs', ['post_id' => $post_id]);
echo '<a class="button button-small" href="' . esc_url($dl_url) . '">' . esc_html__('下载记录', 'aksrt') . '</a>';
}
$permalink = get_permalink($post_id);
if ($permalink) {
echo '<a class="button button-small" target="_blank" rel="noopener noreferrer" href="' . esc_url($permalink) . '">' . esc_html__('查看', 'aksrt') . '</a>';
}
echo '</td>';
echo '</tr>';
}
echo '</tbody></table>';
$this->render_pagination($pages, $page_num);
$this->render_footer();
}
/**
* @param array{pay_type?:string,search?:string,page?:int,per_page?:int} $args
* @return array{posts: \WP_Post[], total: int}
*/
private function query_paid_posts(array $args): array {
$pay_type = sanitize_key((string) ($args['pay_type'] ?? ''));
$search = (string) ($args['search'] ?? '');
$page = max(1, (int) ($args['page'] ?? 1));
$per_page = max(1, min(100, (int) ($args['per_page'] ?? 20)));
$meta_query = [
'relation' => 'AND',
[
'key' => \AKSRT\Core\DomainModel::PAY_META_KEY,
'compare' => 'EXISTS',
],
];
// Prefer denormalized type key when present; still exclude "no".
if ($pay_type !== '' && $pay_type !== \AKSRT\Core\DomainModel::PAY_TYPE_NO) {
$meta_query[] = [
'relation' => 'OR',
[
'key' => \AKSRT\Core\DomainModel::PAY_TYPE_KEY,
'value' => $pay_type,
'compare' => '=',
],
[
'key' => \AKSRT\Core\DomainModel::PAY_META_KEY,
'value' => '"pay_type";s:' . strlen($pay_type) . ':"' . $pay_type . '"',
'compare' => 'LIKE',
],
];
} else {
$meta_query[] = [
'relation' => 'OR',
[
'key' => \AKSRT\Core\DomainModel::PAY_TYPE_KEY,
'value' => \AKSRT\Core\DomainModel::PAY_TYPE_NO,
'compare' => '!=',
],
[
'key' => \AKSRT\Core\DomainModel::PAY_TYPE_KEY,
'compare' => 'NOT EXISTS',
],
];
}
$q_args = [
'post_type' => 'post',
'post_status' => ['publish', 'draft', 'pending', 'private', 'future'],
'posts_per_page' => $per_page,
'paged' => $page,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => $meta_query,
'fields' => 'all',
'no_found_rows' => false,
'update_post_meta_cache' => true,
'update_post_term_cache' => false,
];
if ($search !== '') {
if (ctype_digit($search)) {
$q_args['post__in'] = [(int) $search];
} else {
$q_args['s'] = $search;
}
}
$q = new \WP_Query($q_args);
$posts = [];
foreach ($q->posts as $post) {
if (!($post instanceof \WP_Post)) {
continue;
}
// Drop posts whose pay bag is effectively free/no.
if (function_exists('aksrt_is_paid_post') && !aksrt_is_paid_post($post)) {
continue;
}
if ($pay_type !== '' && function_exists('aksrt_get_pay_meta')) {
$meta = aksrt_get_pay_meta($post);
$pt = \AKSRT\Core\DomainModel::normalize_pay_type((string) ($meta['pay_type'] ?? ''));
if ($pt !== $pay_type) {
continue;
}
}
$posts[] = $post;
}
// When post-filtering drops rows, fall back to order-driven list if empty page
// so operators still see historical sold posts without pay meta.
if (empty($posts) && $search === '' && $pay_type === '' && $page === 1) {
$order_posts = $this->posts_from_order_summary($per_page, 0);
if (!empty($order_posts['posts'])) {
return $order_posts;
}
}
return [
'posts' => $posts,
'total' => (int) $q->found_posts,
];
}
/**
* Fallback: posts that already have paid resource orders.
*
* @return array{posts: \WP_Post[], total: int}
*/
private function posts_from_order_summary(int $limit, int $offset): array {
if (!class_exists('\AKSRT\Payment\OrderRepository')) {
return ['posts' => [], 'total' => 0];
}
$summary = \AKSRT\Payment\OrderRepository::get_instance()->get_content_sales_summary([
'product_type' => \AKSRT\Core\DomainModel::ORDER_PRODUCT_RESOURCE,
'limit' => $limit,
'offset' => $offset,
'orderby' => 'sales_count',
'order' => 'DESC',
]);
$posts = [];
foreach ($summary['items'] as $item) {
$pid = (int) ($item['product_id'] ?? 0);
$post = $pid > 0 ? get_post($pid) : null;
if ($post instanceof \WP_Post && $post->post_type === 'post') {
$posts[] = $post;
}
}
return [
'posts' => $posts,
'total' => (int) ($summary['total'] ?? count($posts)),
];
}
/**
* @param array<string, string> $type_labels
*/
private function render_filters(string $pay_type, string $search, array $type_labels): void {
echo '<form method="get" style="margin:12px 0;">';
echo '<input type="hidden" name="page" value="' . esc_attr(self::PAGE_SLUG) . '" />';
echo '<select name="pay_type">';
echo '<option value="">' . esc_html__('全部付费类型', 'aksrt') . '</option>';
foreach ($type_labels as $key => $label) {
printf(
'<option value="%s"%s>%s</option>',
esc_attr($key),
selected($pay_type, $key, false),
esc_html($label)
);
}
echo '</select> ';
echo '<input type="search" name="s" value="' . esc_attr($search) . '" placeholder="' . esc_attr__('标题或文章 ID', 'aksrt') . '" /> ';
submit_button(__('筛选', 'aksrt'), 'secondary', '', false);
echo '</form>';
echo '<ul class="subsubsub" style="margin:0 0 12px;float:none;">';
$base = $this->page_url(self::PAGE_SLUG);
$all_class = $pay_type === '' ? 'current' : '';
echo '<li><a class="' . esc_attr($all_class) . '" href="' . esc_url($base) . '">' . esc_html__('全部', 'aksrt') . '</a></li>';
foreach ($type_labels as $key => $label) {
$url = $this->page_url(self::PAGE_SLUG, array_filter([
'pay_type' => $key,
's' => $search !== '' ? $search : null,
]));
$cls = $pay_type === $key ? 'current' : '';
echo ' | <li><a class="' . esc_attr($cls) . '" href="' . esc_url($url) . '">' . esc_html($label) . '</a></li>';
}
echo '</ul><div style="clear:both;"></div>';
echo '<p class="description">' . esc_html__('本页统计内容付费文章(product_type=resource),不含商城实体商品。订单销量来自已支付/已发货/已完成订单;Meta 销量为缓存计数。', 'aksrt') . '</p>';
}
private function ensure_cap(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('您没有权限访问此页面。', 'aksrt'));
}
}
private function render_header(string $title): void {
echo '<div class="wrap">';
echo '<h1>' . esc_html($title) . '</h1>';
}
private function render_footer(): void {
echo '</div>';
}
private function page_url(string $slug, array $args = []): string {
return add_query_arg(array_merge(['page' => $slug], $args), admin_url('admin.php'));
}
private function render_pagination(int $pages, int $page_num): void {
if ($pages <= 1) {
return;
}
echo '<div class="tablenav"><div class="tablenav-pages">';
echo paginate_links([
'base' => add_query_arg('paged', '%#%'),
'format' => '',
'prev_text' => '&laquo;',
'next_text' => '&raquo;',
'total' => $pages,
'current' => $page_num,
]);
echo '</div></div>';
}
}
+202
View File
@@ -0,0 +1,202 @@
<?php
/**
* @package AKSRT
*/
namespace AKSRT\Admin\Native;
defined('ABSPATH') || exit;
class PayRebate {
public const CAPABILITY = 'manage_options';
public const PAGE_SLUG = 'aksrt_rebate';
private static $instance = null;
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('admin_post_aksrt_pay_settle_rebate', [$this, 'handle_settle_rebate']);
add_action('admin_post_aksrt_pay_cancel_rebate', [$this, 'handle_cancel_rebate']);
}
public function render_rebate_page(): void {
$this->ensure_cap();
$this->render_header(__('推广返佣', 'aksrt'));
if (!class_exists('\AKSRT\Payment\Rebate')) {
echo '<div class="notice notice-error"><p>' . esc_html__('返佣模块未初始化。', 'aksrt') . '</p></div>';
$this->render_footer();
return;
}
$status = isset($_GET['status']) ? sanitize_key((string) wp_unslash($_GET['status'])) : '';
$user_id = isset($_GET['user_id']) ? max(0, (int) $_GET['user_id']) : (isset($_GET['referrer_id']) ? max(0, (int) $_GET['referrer_id']) : 0);
$page_num = max(1, (int) ($_GET['paged'] ?? 1));
$per_page = 20;
$svc = \AKSRT\Payment\Rebate::get_instance();
$stats = $svc->get_stats();
echo '<p>' . esc_html(sprintf(
/* translators: 1: total 2: pending 3: settled amount */
__('记录 %1$d · 待结算 %2$d · 已结算金额 %3$s', 'aksrt'),
(int) ($stats['total'] ?? 0),
(int) ($stats['pending'] ?? 0),
number_format((float) ($stats['settled_amount'] ?? 0), 2)
)) . '</p>';
echo '<form method="get" style="margin:12px 0;">';
echo '<input type="hidden" name="page" value="aksrt_rebate" />';
echo '<select name="status">';
foreach (['' => __('全部状态', 'aksrt'), 'pending' => __('待结算', 'aksrt'), 'settled' => __('已结算', 'aksrt'), 'cancelled' => __('已取消', 'aksrt')] as $key => $label) {
printf('<option value="%s"%s>%s</option>', esc_attr($key), selected($status, $key, false), esc_html($label));
}
echo '</select> ';
echo '<input type="number" min="0" name="user_id" value="' . esc_attr($user_id > 0 ? (string) $user_id : '') . '" placeholder="' . esc_attr__('推广用户 ID', 'aksrt') . '" class="small-text" /> ';
submit_button(__('筛选', 'aksrt'), 'secondary', '', false);
if ($user_id > 0) {
echo ' <a class="button button-small" href="' . esc_url($this->page_url('aksrt_rebate')) . '">' . esc_html__('清除用户过滤', 'aksrt') . '</a>';
}
echo '</form>';
$list_args = [
'status' => $status,
'limit' => $per_page,
'offset' => ($page_num - 1) * $per_page,
];
if ($user_id > 0) {
$list_args['user_id'] = $user_id;
}
$result = $svc->get_list($list_args);
$items = $result['items'] ?? [];
$total = (int) ($result['total'] ?? 0);
$pages = max(1, (int) ceil(max(1, $total) / $per_page));
echo '<table class="wp-list-table widefat fixed striped"><thead><tr>';
echo '<th>ID</th><th>' . esc_html__('用户', 'aksrt') . '</th><th>' . esc_html__('订单号', 'aksrt') . '</th><th>' . esc_html__('金额', 'aksrt') . '</th><th>' . esc_html__('状态', 'aksrt') . '</th><th>' . esc_html__('时间', 'aksrt') . '</th><th>' . esc_html__('操作', 'aksrt') . '</th>';
echo '</tr></thead><tbody>';
if (!$items) {
echo '<tr><td colspan="7">' . esc_html__('暂无返佣记录', 'aksrt') . '</td></tr>';
}
foreach ($items as $row) {
$id = (int) ($row['id'] ?? 0);
$user_id = (int) ($row['user_id'] ?? 0);
$user = $user_id > 0 ? get_userdata($user_id) : null;
$st = (string) ($row['status'] ?? '');
echo '<tr>';
echo '<td>' . esc_html((string) $id) . '</td>';
echo '<td>' . ($user ? esc_html($user->display_name) : esc_html((string) $user_id)) . '</td>';
echo '<td><code>' . esc_html((string) ($row['order_number'] ?? '')) . '</code></td>';
echo '<td>' . esc_html(number_format((float) ($row['amount'] ?? 0), 2)) . '</td>';
echo '<td>' . esc_html($st) . '</td>';
echo '<td>' . esc_html((string) ($row['created_at'] ?? '')) . '</td>';
echo '<td style="display:flex;gap:4px;">';
if ($st === 'pending') {
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '">';
echo '<input type="hidden" name="action" value="aksrt_pay_settle_rebate" />';
echo '<input type="hidden" name="rebate_id" value="' . esc_attr((string) $id) . '" />';
wp_nonce_field('aksrt_pay_settle_rebate_' . $id);
submit_button(__('结算', 'aksrt'), 'small', '', false);
echo '</form>';
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '">';
echo '<input type="hidden" name="action" value="aksrt_pay_cancel_rebate" />';
echo '<input type="hidden" name="rebate_id" value="' . esc_attr((string) $id) . '" />';
wp_nonce_field('aksrt_pay_cancel_rebate_' . $id);
submit_button(__('取消', 'aksrt'), 'small', '', false);
echo '</form>';
} else {
echo '—';
}
echo '</td></tr>';
}
echo '</tbody></table>';
$this->render_pagination($pages, $page_num);
$this->render_footer();
}
public function handle_settle_rebate(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('权限不足', 'aksrt'));
}
$id = isset($_POST['rebate_id']) ? (int) $_POST['rebate_id'] : 0;
check_admin_referer('aksrt_pay_settle_rebate_' . $id);
$ok = $id > 0 && class_exists('\AKSRT\Payment\Rebate') && \AKSRT\Payment\Rebate::get_instance()->settle($id);
wp_safe_redirect($this->page_url('aksrt_rebate', ['aksrt_pay_msg' => $ok ? 'rebate_settled' : 'rebate_failed']));
exit;
}
public function handle_cancel_rebate(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('权限不足', 'aksrt'));
}
$id = isset($_POST['rebate_id']) ? (int) $_POST['rebate_id'] : 0;
check_admin_referer('aksrt_pay_cancel_rebate_' . $id);
$ok = $id > 0 && class_exists('\AKSRT\Payment\Rebate') && \AKSRT\Payment\Rebate::get_instance()->cancel($id);
wp_safe_redirect($this->page_url('aksrt_rebate', ['aksrt_pay_msg' => $ok ? 'rebate_cancelled' : 'rebate_failed']));
exit;
}
private function ensure_cap(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('您没有权限访问此页面。', 'aksrt'));
}
}
private function render_header(string $title): void {
echo '<div class="wrap">';
echo '<h1>' . esc_html($title) . '</h1>';
$this->render_flash();
}
private function render_footer(): void {
echo '</div>';
}
private function render_flash(): void {
if (empty($_GET['aksrt_pay_msg'])) {
return;
}
$code = sanitize_key((string) wp_unslash($_GET['aksrt_pay_msg']));
$map = [
'rebate_settled' => __('返佣已结算。', 'aksrt'),
'rebate_cancelled' => __('返佣已取消。', 'aksrt'),
'rebate_failed' => __('返佣操作失败。', 'aksrt'),
];
if (!isset($map[$code])) {
return;
}
$class = str_contains($code, 'failed') ? 'notice-error' : 'notice-success';
echo '<div class="notice ' . esc_attr($class) . ' is-dismissible"><p>' . esc_html($map[$code]) . '</p></div>';
}
private function page_url(string $slug, array $args = []): string {
return add_query_arg(array_merge(['page' => $slug], $args), admin_url('admin.php'));
}
private function render_pagination(int $pages, int $page_num): void {
if ($pages <= 1) {
return;
}
echo '<div class="tablenav"><div class="tablenav-pages">';
echo paginate_links([
'base' => add_query_arg('paged', '%#%'),
'format' => '',
'prev_text' => '&laquo;',
'next_text' => '&raquo;',
'total' => $pages,
'current' => $page_num,
]);
echo '</div></div>';
}
}
+258
View File
@@ -0,0 +1,258 @@
<?php
/**
* @package AKSRT
*/
namespace AKSRT\Admin\Native;
defined('ABSPATH') || exit;
class PayWithdrawals {
public const CAPABILITY = 'manage_options';
public const PAGE_SLUG = 'aksrt_withdraw';
private static $instance = null;
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('admin_post_aksrt_pay_update_withdrawal', [$this, 'handle_update_withdrawal']);
}
public function render_withdrawals_page(): void {
$this->ensure_cap();
$this->render_header(__('提现审核', 'aksrt'));
if (!class_exists('\AKSRT\Payment\Withdrawal')) {
echo '<div class="notice notice-error"><p>' . esc_html__('提现模块未初始化。', 'aksrt') . '</p></div>';
$this->render_footer();
return;
}
$status = isset($_GET['status']) ? sanitize_key((string) wp_unslash($_GET['status'])) : '';
$page_num = max(1, (int) ($_GET['paged'] ?? 1));
$per_page = 20;
$svc = \AKSRT\Payment\Withdrawal::get_instance();
$stats = $svc->get_stats();
echo '<p>' . esc_html(sprintf(
/* translators: 1: total 2: pending 3: completed 4: paid */
__('共 %1$d 条 · 待处理 %2$d · 已完成 %3$d · 已打款 %4$s', 'aksrt'),
(int) ($stats['total'] ?? 0),
(int) ($stats['pending'] ?? 0),
(int) ($stats['completed'] ?? 0),
number_format((float) ($stats['total_paid'] ?? 0), 2)
)) . '</p>';
echo '<form method="get" style="margin:12px 0;">';
echo '<input type="hidden" name="page" value="aksrt_withdraw" />';
echo '<select name="status">';
$status_options = [
'' => __('全部状态', 'aksrt'),
'pending' => __('待处理', 'aksrt'),
'approved' => __('已通过', 'aksrt'),
'processing' => __('处理中', 'aksrt'),
'completed' => __('已完成', 'aksrt'),
'rejected' => __('已拒绝', 'aksrt'),
];
foreach ($status_options as $key => $label) {
printf(
'<option value="%s"%s>%s</option>',
esc_attr($key),
selected($status, $key, false),
esc_html($label)
);
}
echo '</select> ';
submit_button(__('筛选', 'aksrt'), 'secondary', '', false);
echo '</form>';
$items = $svc->get_all([
'status' => $status,
'limit' => $per_page,
'offset' => ($page_num - 1) * $per_page,
]);
$total = $svc->get_count(['status' => $status]);
$pages = max(1, (int) ceil($total / $per_page));
echo '<table class="wp-list-table widefat fixed striped">';
echo '<thead><tr>';
echo '<th>ID</th>';
echo '<th>' . esc_html__('用户', 'aksrt') . '</th>';
echo '<th>' . esc_html__('金额', 'aksrt') . '</th>';
echo '<th>' . esc_html__('方式', 'aksrt') . '</th>';
echo '<th>' . esc_html__('账户', 'aksrt') . '</th>';
echo '<th>' . esc_html__('状态', 'aksrt') . '</th>';
echo '<th>' . esc_html__('申请时间', 'aksrt') . '</th>';
echo '<th>' . esc_html__('操作', 'aksrt') . '</th>';
echo '</tr></thead><tbody>';
if (!$items) {
echo '<tr><td colspan="8">' . esc_html__('暂无提现记录', 'aksrt') . '</td></tr>';
}
foreach ($items as $row) {
$id = (int) ($row['id'] ?? 0);
$user_id = (int) ($row['user_id'] ?? 0);
$user = $user_id > 0 ? get_userdata($user_id) : null;
$st = (string) ($row['status'] ?? '');
$gross = (float) ($row['gross_amount'] ?? $row['amount'] ?? 0);
$net = (float) ($row['amount'] ?? 0);
$fee = (int) ($row['fee_rate'] ?? 0);
$acct = (string) ($row['display_account'] ?? $row['account_info'] ?? '');
$amount_txt = number_format($net, 2);
if ($gross > 0 && abs($gross - $net) > 0.001) {
$amount_txt = sprintf(
/* translators: 1: gross 2: net 3: fee percent */
__('申请 %1$s / 到账 %2$s(手续费 %3$d%%)', 'aksrt'),
number_format($gross, 2),
number_format($net, 2),
$fee
);
}
echo '<tr>';
echo '<td>' . esc_html((string) $id) . '</td>';
echo '<td>' . ($user ? esc_html($user->display_name . ' (#' . $user_id . ')') : esc_html((string) $user_id)) . '</td>';
echo '<td>' . esc_html($amount_txt) . '</td>';
echo '<td>' . esc_html((string) ($row['method'] ?? '')) . '</td>';
echo '<td>' . esc_html($acct) . '</td>';
echo '<td>' . esc_html($status_options[$st] ?? $st) . '</td>';
echo '<td>' . esc_html((string) ($row['created_at'] ?? '')) . '</td>';
echo '<td>';
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="display:flex;gap:4px;flex-wrap:wrap;align-items:center;">';
echo '<input type="hidden" name="action" value="aksrt_pay_update_withdrawal" />';
echo '<input type="hidden" name="withdrawal_id" value="' . esc_attr((string) $id) . '" />';
wp_nonce_field('aksrt_pay_update_withdrawal_' . $id);
echo '<select name="new_status">';
foreach ($status_options as $key => $label) {
if ($key === '') {
continue;
}
printf(
'<option value="%s"%s>%s</option>',
esc_attr($key),
selected($st, $key, false),
esc_html($label)
);
}
echo '</select>';
echo '<input type="text" name="note" placeholder="' . esc_attr__('备注', 'aksrt') . '" value="" />';
submit_button(__('更新', 'aksrt'), 'small', '', false);
echo '</form>';
echo '</td>';
echo '</tr>';
}
echo '</tbody></table>';
if ($pages > 1) {
echo '<div class="tablenav"><div class="tablenav-pages">';
echo paginate_links([
'base' => add_query_arg('paged', '%#%'),
'format' => '',
'prev_text' => '&laquo;',
'next_text' => '&raquo;',
'total' => $pages,
'current' => $page_num,
]);
echo '</div></div>';
}
$this->render_footer();
}
public function handle_update_withdrawal(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('权限不足', 'aksrt'));
}
$id = isset($_POST['withdrawal_id']) ? (int) $_POST['withdrawal_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_pay_update_withdrawal_' . $id);
$msg = 'withdraw_failed';
if ($id > 0 && $new_status !== '' && class_exists('\AKSRT\Payment\Withdrawal')) {
$result = \AKSRT\Payment\Withdrawal::get_instance()->process_status_change($id, $new_status, $note);
if (!is_wp_error($result) && $result) {
$msg = 'withdraw_updated';
} elseif (is_wp_error($result) && $result->get_error_code() === 'aksrt_balance') {
$msg = 'withdraw_refund_failed';
}
}
wp_safe_redirect($this->page_url('aksrt_withdraw', [
'aksrt_pay_msg' => $msg,
]));
exit;
}
/* ------------------------------------------------------------------
* Income / rebate
* ----------------------------------------------------------------*/
private function ensure_cap(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('您没有权限访问此页面。', 'aksrt'));
}
}
private function render_header(string $title): void {
echo '<div class="wrap">';
echo '<h1>' . esc_html($title) . '</h1>';
$this->render_flash();
}
private function render_footer(): void {
echo '</div>';
}
private function render_flash(): void {
if (empty($_GET['aksrt_pay_msg'])) {
return;
}
$code = sanitize_key((string) wp_unslash($_GET['aksrt_pay_msg']));
$map = [
'withdraw_updated' => __('提现状态已更新。', 'aksrt'),
'withdraw_failed' => __('提现状态更新失败。', 'aksrt'),
'withdraw_refund_failed' => __('状态未更新:余额退回失败,请检查钱包后重试。', 'aksrt'),
];
if (!isset($map[$code])) {
return;
}
$class = str_contains($code, 'failed') ? 'notice-error' : 'notice-success';
echo '<div class="notice ' . esc_attr($class) . ' is-dismissible"><p>' . esc_html($map[$code]) . '</p></div>';
}
private function page_url(string $slug, array $args = []): string {
return add_query_arg(array_merge(['page' => $slug], $args), admin_url('admin.php'));
}
private function render_pagination(int $pages, int $page_num): void {
if ($pages <= 1) {
return;
}
echo '<div class="tablenav"><div class="tablenav-pages">';
echo paginate_links([
'base' => add_query_arg('paged', '%#%'),
'format' => '',
'prev_text' => '&laquo;',
'next_text' => '&raquo;',
'total' => $pages,
'current' => $page_num,
]);
echo '</div></div>';
}
}
+817
View File
@@ -0,0 +1,817 @@
<?php
/**
* @package AKSRT
*/
namespace AKSRT\Admin\Native;
defined('ABSPATH') || exit;
class UsersMenus {
use UsersMenusVerify;
public const CAPABILITY = 'manage_options';
private static $instance = null;
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('admin_menu', [$this, 'register_menus']);
add_action('admin_notices', [$this, 'pending_notices']);
add_action('admin_post_aksrt_users_send_message', [$this, 'handle_send_message']);
add_action('admin_post_aksrt_users_review_verify', [$this, 'handle_review_verify']);
add_action('admin_post_aksrt_users_delete_verify', [$this, 'handle_delete_verify']);
add_action('admin_post_aksrt_users_update_report', [$this, 'handle_update_report']);
add_action('admin_post_aksrt_users_add_restriction', [$this, 'handle_add_restriction']);
add_action('admin_post_aksrt_users_remove_restriction', [$this, 'handle_remove_restriction']);
add_action('admin_post_aksrt_users_process_appeal', [$this, 'handle_process_appeal']);
add_action('admin_post_aksrt_users_batch_invite_codes', [$this, 'handle_batch_invite_codes']);
add_action('admin_post_aksrt_users_disable_invite_code', [$this, 'handle_disable_invite_code']);
}
public function register_menus(): void {
if (!current_user_can(self::CAPABILITY)) {
return;
}
add_submenu_page(
'users.php',
__('消息管理', 'aksrt'),
__('消息管理', 'aksrt'),
self::CAPABILITY,
'aksrt_messages',
[$this, 'render_messages_page']
);
add_submenu_page(
'users.php',
__('邀请码管理', 'aksrt'),
__('邀请码管理', 'aksrt'),
self::CAPABILITY,
'aksrt_invite_codes',
[$this, 'render_invite_codes_page']
);
add_submenu_page(
'users.php',
__('身份认证', 'aksrt'),
__('身份认证', 'aksrt'),
self::CAPABILITY,
'aksrt_user_auth',
[$this, 'render_verify_page']
);
add_submenu_page(
'users.php',
__('举报&禁封', 'aksrt'),
__('举报&禁封', 'aksrt'),
self::CAPABILITY,
'aksrt_user_ban',
[$this, 'render_ban_page']
);
}
public function pending_notices(): void {
if (!current_user_can(self::CAPABILITY)) {
return;
}
$page = isset($_GET['page']) ? sanitize_key((string) wp_unslash($_GET['page'])) : '';
if ($page !== 'aksrt_user_auth') {
$pending_verify = $this->count_verify_status('pending');
if ($pending_verify > 0) {
$url = admin_url('users.php?page=aksrt_user_auth&status=pending');
echo '<div class="notice notice-info is-dismissible"><h3>' . esc_html__('用户身份认证申请待处理', 'aksrt') . '</h3>';
echo '<p>' . esc_html(sprintf(
/* translators: %d count */
__('您有 %d 个用户身份认证申请待处理', 'aksrt'),
$pending_verify
)) . '</p>';
echo '<p><a class="button button-primary" href="' . esc_url($url) . '">' . esc_html__('立即处理', 'aksrt') . '</a></p></div>';
}
}
if ($page !== 'aksrt_user_ban' && class_exists('\AKSRT\Messages\ReportCenter')) {
$pending_reports = (int) \AKSRT\Messages\ReportCenter::get_instance()->get_pending_count();
if ($pending_reports > 0) {
$url = admin_url('users.php?page=aksrt_user_ban&tab=reports&status=pending');
echo '<div class="notice notice-info is-dismissible"><h3>' . esc_html__('举报待处理', 'aksrt') . '</h3>';
echo '<p>' . esc_html(sprintf(
/* translators: %d count */
__('您有 %d 条举报待处理', 'aksrt'),
$pending_reports
)) . '</p>';
echo '<p><a class="button button-primary" href="' . esc_url($url) . '">' . esc_html__('立即处理', 'aksrt') . '</a></p></div>';
}
}
if ($page !== 'aksrt_user_ban' && class_exists('\AKSRT\User\BanAppeal')) {
$pending_appeals = count(get_users([
'meta_key' => \AKSRT\User\BanAppeal::META_STATUS,
'meta_value' => 'pending',
'number' => 50,
'fields' => 'ID',
]));
if ($pending_appeals > 0) {
$url = admin_url('users.php?page=aksrt_user_ban&tab=appeals&status=pending');
echo '<div class="notice notice-warning is-dismissible"><h3>' . esc_html__('禁封申诉待处理', 'aksrt') . '</h3>';
echo '<p>' . esc_html(sprintf(__('您有 %d 条禁封申诉待处理', 'aksrt'), $pending_appeals)) . '</p>';
echo '<p><a class="button button-primary" href="' . esc_url($url) . '">' . esc_html__('立即处理', 'aksrt') . '</a></p></div>';
}
}
}
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_deleted' => __('认证记录已删除。', 'aksrt'),
'verify_failed' => __('认证审核失败。', 'aksrt'),
'report_updated' => __('举报状态已更新。', 'aksrt'),
'report_failed' => __('举报更新失败。', 'aksrt'),
'restriction_added' => __('限制已添加。', 'aksrt'),
'restriction_removed' => __('限制已移除。', 'aksrt'),
'restriction_failed' => __('限制操作失败。', 'aksrt'),
'appeal_approved' => __('申诉已通过,限制已解除。', 'aksrt'),
'appeal_rejected' => __('申诉已驳回。', 'aksrt'),
'appeal_failed' => __('申诉处理失败。', 'aksrt'),
'invite_batch_ok' => __('邀请码批量生成完成。', 'aksrt'),
'invite_batch_failed' => __('邀请码批量生成失败。', 'aksrt'),
'invite_disabled' => __('邀请码已禁用。', 'aksrt'),
'invite_failed' => __('邀请码操作失败。', 'aksrt'),
];
if (!isset($map[$code])) {
return;
}
$class = str_contains($code, 'failed') ? 'notice-error' : 'notice-success';
$message = $map[$code];
if ($code === 'verify_failed' && isset($_GET['aksrt_verify_error'])) {
$message = sanitize_text_field((string) wp_unslash($_GET['aksrt_verify_error']));
}
echo '<div class="notice ' . esc_attr($class) . ' is-dismissible"><p>' . esc_html($message) . '</p></div>';
}
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 '<div class="wrap"><h1>' . esc_html__('消息管理', 'aksrt') . '</h1>';
$this->flash();
echo '<h2>' . esc_html__('发送系统消息', 'aksrt') . '</h2>';
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="margin-bottom:20px;">';
echo '<input type="hidden" name="action" value="aksrt_users_send_message" />';
wp_nonce_field('aksrt_users_send_message');
echo '<p><label>' . esc_html__('用户 ID(0=广播给管理员自己测试)', 'aksrt') . '<br><input type="number" name="user_id" value="0" min="0" /></label></p>';
echo '<p><label>' . esc_html__('标题', 'aksrt') . '<br><input type="text" name="title" class="regular-text" required /></label></p>';
echo '<p><label>' . esc_html__('内容', 'aksrt') . '<br><textarea name="content" class="large-text" rows="4" required></textarea></label></p>';
submit_button(__('发送', 'aksrt'));
echo '</form>';
$page_num = max(1, (int) ($_GET['paged'] ?? 1));
$per_page = 30;
$offset = ($page_num - 1) * $per_page;
$table = class_exists('\AKSRT\Core\DBMigrator') ? \AKSRT\Core\DBMigrator::table('notification') : '';
if ($table === '') {
echo '<div class="notice notice-warning"><p>' . esc_html__('通知表不可用。', 'aksrt') . '</p></div></div>';
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 '<h2>' . esc_html__('最近消息', 'aksrt') . '</h2>';
echo '<table class="wp-list-table widefat fixed striped"><thead><tr>';
echo '<th>ID</th><th>' . esc_html__('用户', 'aksrt') . '</th><th>' . esc_html__('类型', 'aksrt') . '</th><th>' . esc_html__('标题', 'aksrt') . '</th><th>' . esc_html__('已读', 'aksrt') . '</th><th>' . esc_html__('时间', 'aksrt') . '</th>';
echo '</tr></thead><tbody>';
if (!$items) {
echo '<tr><td colspan="6">' . esc_html__('暂无消息', 'aksrt') . '</td></tr>';
}
foreach ((array) $items as $row) {
$user_id = (int) ($row['user_id'] ?? 0);
$user = $user_id > 0 ? get_userdata($user_id) : null;
echo '<tr>';
echo '<td>' . esc_html((string) ($row['id'] ?? '')) . '</td>';
echo '<td>' . ($user ? esc_html($user->display_name . ' (#' . $user_id . ')') : esc_html((string) $user_id)) . '</td>';
echo '<td>' . esc_html((string) ($row['type'] ?? '')) . '</td>';
echo '<td>' . esc_html((string) ($row['title'] ?? $row['description'] ?? '')) . '</td>';
echo '<td>' . (!empty($row['is_read']) ? esc_html__('是', 'aksrt') : esc_html__('否', 'aksrt')) . '</td>';
echo '<td>' . esc_html((string) ($row['created_at'] ?? '')) . '</td>';
echo '</tr>';
}
echo '</tbody></table>';
$this->pagination($pages, $page_num);
echo '</div>';
}
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\Messages\Message')) {
$ok = \AKSRT\Messages\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 '<div class="wrap"><h1>' . esc_html__('邀请码管理', 'aksrt') . '</h1>';
$this->flash();
echo '<h2>' . esc_html__('批量生成独立邀请码', 'aksrt') . '</h2>';
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="margin:12px 0 20px;display:flex;flex-wrap:wrap;gap:8px;align-items:end;">';
echo '<input type="hidden" name="action" value="aksrt_users_batch_invite_codes" />';
wp_nonce_field('aksrt_users_batch_invite_codes');
echo '<p><label>' . esc_html__('数量', 'aksrt') . '<br><input type="number" name="count" min="1" max="100" value="10" /></label></p>';
echo '<p><label>' . esc_html__('长度', 'aksrt') . '<br><input type="number" name="length" min="4" max="12" value="6" /></label></p>';
echo '<p><label>' . esc_html__('前缀', 'aksrt') . '<br><input type="text" name="prefix" value="" /></label></p>';
echo '<p><label>' . esc_html__('备注', 'aksrt') . '<br><input type="text" name="note" value="" class="regular-text" /></label></p>';
submit_button(__('生成', 'aksrt'), 'primary', '', false);
echo '</form>';
if (!empty($_GET['aksrt_generated'])) {
echo '<div class="notice notice-success"><p>' . esc_html(sprintf(
/* translators: %d count */
__('成功生成 %d 个邀请码。', 'aksrt'),
(int) $_GET['aksrt_generated']
)) . '</p></div>';
}
$standalone = get_option('aksrt_standalone_invite_codes', []);
$standalone = is_array($standalone) ? array_reverse(array_values($standalone)) : [];
echo '<h2>' . esc_html__('独立邀请码', 'aksrt') . '</h2>';
echo '<table class="wp-list-table widefat fixed striped"><thead><tr>';
echo '<th>' . esc_html__('邀请码', 'aksrt') . '</th><th>' . esc_html__('状态', 'aksrt') . '</th><th>' . esc_html__('批次', 'aksrt') . '</th><th>' . esc_html__('备注', 'aksrt') . '</th><th>' . esc_html__('创建时间', 'aksrt') . '</th><th>' . esc_html__('操作', 'aksrt') . '</th>';
echo '</tr></thead><tbody>';
if (!$standalone) {
echo '<tr><td colspan="6">' . esc_html__('暂无独立邀请码', 'aksrt') . '</td></tr>';
}
$shown = 0;
foreach ($standalone as $entry) {
if ($shown >= 200) {
break;
}
$code = sanitize_key((string) ($entry['code'] ?? ''));
if ($code === '') {
continue;
}
$disabled = !empty($entry['disabled']);
$used = !empty($entry['used']) || !empty($entry['used_by']);
$status = $disabled ? __('已禁用', 'aksrt') : ($used ? __('已使用', 'aksrt') : __('可用', 'aksrt'));
echo '<tr>';
echo '<td><code>' . esc_html($code) . '</code></td>';
echo '<td>' . esc_html($status) . '</td>';
echo '<td>' . esc_html((string) ($entry['batch_id'] ?? '')) . '</td>';
echo '<td>' . esc_html((string) ($entry['note'] ?? '')) . '</td>';
echo '<td>' . esc_html((string) ($entry['created_at'] ?? '')) . '</td>';
echo '<td>';
if (!$disabled && !$used) {
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '">';
echo '<input type="hidden" name="action" value="aksrt_users_disable_invite_code" />';
echo '<input type="hidden" name="code" value="' . esc_attr($code) . '" />';
wp_nonce_field('aksrt_users_disable_invite_code_' . $code);
submit_button(__('禁用', 'aksrt'), 'small', '', false);
echo '</form>';
} else {
echo '—';
}
echo '</td></tr>';
$shown++;
}
echo '</tbody></table>';
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 '<h2>' . esc_html__('用户邀请码', 'aksrt') . '</h2>';
echo '<table class="wp-list-table widefat fixed striped"><thead><tr>';
echo '<th>' . esc_html__('用户', 'aksrt') . '</th><th>' . esc_html__('邀请码', 'aksrt') . '</th><th>' . esc_html__('邀请人数', 'aksrt') . '</th>';
echo '</tr></thead><tbody>';
if (!$rows) {
echo '<tr><td colspan="3">' . esc_html__('暂无用户邀请码', 'aksrt') . '</td></tr>';
}
$invite_table = class_exists('\AKSRT\Core\DBMigrator') ? \AKSRT\Core\DBMigrator::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 '<tr>';
echo '<td>' . ($user ? esc_html($user->display_name . ' (#' . $user_id . ')') : esc_html((string) $user_id)) . '</td>';
echo '<td><code>' . esc_html((string) ($row['invite_code'] ?? '')) . '</code></td>';
echo '<td>' . esc_html((string) $count) . '</td>';
echo '</tr>';
}
echo '</tbody></table></div>';
}
public function handle_batch_invite_codes(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('权限不足', 'aksrt'));
}
check_admin_referer('aksrt_users_batch_invite_codes');
$count = isset($_POST['count']) ? max(1, min(100, (int) $_POST['count'])) : 10;
$length = isset($_POST['length']) ? max(4, min(12, (int) $_POST['length'])) : 6;
$prefix = isset($_POST['prefix']) ? sanitize_key((string) wp_unslash($_POST['prefix'])) : '';
$note = isset($_POST['note']) ? sanitize_textarea_field((string) wp_unslash($_POST['note'])) : '';
$batch = 'INV-' . gmdate('YmdHis') . '-' . wp_generate_password(4, false, false);
$alphabet = 'abcdefghjkmnpqrstuvwxyz23456789';
$codes = get_option('aksrt_standalone_invite_codes', []);
$codes = is_array($codes) ? array_values($codes) : [];
$existing = [];
foreach ($codes as $entry) {
$c = sanitize_key((string) ($entry['code'] ?? ''));
if ($c !== '') {
$existing[$c] = true;
}
}
global $wpdb;
$generated = [];
for ($i = 0; $i < $count; $i++) {
$code = '';
for ($attempt = 0; $attempt < 20; $attempt++) {
$candidate = $prefix;
for ($j = 0; $j < $length; $j++) {
$candidate .= $alphabet[random_int(0, strlen($alphabet) - 1)];
}
$candidate = sanitize_key($candidate);
if ($candidate === '' || isset($existing[$candidate])) {
continue;
}
$user_hit = $wpdb->get_var($wpdb->prepare(
"SELECT umeta_id FROM {$wpdb->usermeta} WHERE meta_key = 'aksrt_invite_code' AND meta_value = %s LIMIT 1",
$candidate
));
if ($user_hit) {
continue;
}
$code = $candidate;
break;
}
if ($code === '') {
continue;
}
$existing[$code] = true;
$codes[] = [
'code' => $code,
'created_at' => current_time('mysql'),
'used' => false,
'used_by' => 0,
'used_at' => '',
'disabled' => false,
'note' => $note,
'batch_id' => $batch,
'created_by' => get_current_user_id(),
];
$generated[] = $code;
}
update_option('aksrt_standalone_invite_codes', array_values($codes), false);
$ok = !empty($generated);
$this->redirect('aksrt_invite_codes', $ok ? 'invite_batch_ok' : 'invite_batch_failed', [
'aksrt_generated' => count($generated),
]);
}
public function handle_disable_invite_code(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('权限不足', 'aksrt'));
}
$code = isset($_POST['code']) ? sanitize_key((string) wp_unslash($_POST['code'])) : '';
check_admin_referer('aksrt_users_disable_invite_code_' . $code);
$ok = false;
if ($code !== '') {
$codes = get_option('aksrt_standalone_invite_codes', []);
$codes = is_array($codes) ? $codes : [];
foreach ($codes as $i => $entry) {
if (sanitize_key((string) ($entry['code'] ?? '')) !== $code) {
continue;
}
$entry['disabled'] = true;
$codes[$i] = $entry;
$ok = true;
break;
}
if ($ok) {
update_option('aksrt_standalone_invite_codes', array_values($codes), false);
}
}
$this->redirect('aksrt_invite_codes', $ok ? 'invite_disabled' : 'invite_failed');
}
/* ------------------------------------------------------------------
* Ban / reports
* ----------------------------------------------------------------*/
public function render_ban_page(): void {
$this->ensure_cap();
echo '<div class="wrap"><h1>' . esc_html__('举报&禁封', 'aksrt') . '</h1>';
$this->flash();
$tab = isset($_GET['tab']) ? sanitize_key((string) wp_unslash($_GET['tab'])) : 'reports';
echo '<h2 class="nav-tab-wrapper">';
printf('<a class="nav-tab%s" href="%s">%s</a>', $tab === 'reports' ? ' nav-tab-active' : '', esc_url(admin_url('users.php?page=aksrt_user_ban&tab=reports')), esc_html__('举报', 'aksrt'));
printf('<a class="nav-tab%s" href="%s">%s</a>', $tab === 'restrictions' ? ' nav-tab-active' : '', esc_url(admin_url('users.php?page=aksrt_user_ban&tab=restrictions')), esc_html__('禁封限制', 'aksrt'));
printf('<a class="nav-tab%s" href="%s">%s</a>', $tab === 'appeals' ? ' nav-tab-active' : '', esc_url(admin_url('users.php?page=aksrt_user_ban&tab=appeals')), esc_html__('禁封申诉', 'aksrt'));
echo '</h2>';
if ($tab === 'restrictions') {
$this->render_restrictions_tab();
} elseif ($tab === 'appeals') {
$this->render_appeals_tab();
} else {
$this->render_reports_tab();
}
echo '</div>';
}
private function render_reports_tab(): void {
if (!class_exists('\AKSRT\Messages\ReportCenter')) {
echo '<div class="notice notice-error"><p>' . esc_html__('举报中心未初始化。', 'aksrt') . '</p></div>';
return;
}
$status = isset($_GET['status']) ? sanitize_key((string) wp_unslash($_GET['status'])) : 'pending';
$svc = \AKSRT\Messages\ReportCenter::get_instance();
$counts = $svc->get_status_counts();
echo '<p>' . esc_html(sprintf(
/* translators: 1 pending 2 total */
__('待处理 %1$d / 共 %2$d', 'aksrt'),
(int) ($counts['pending'] ?? 0),
(int) ($counts['total'] ?? 0)
)) . '</p>';
echo '<form method="get" style="margin:12px 0;">';
echo '<input type="hidden" name="page" value="aksrt_user_ban" /><input type="hidden" name="tab" value="reports" />';
echo '<select name="status">';
foreach (['pending' => __('待处理', 'aksrt'), 'reviewing' => __('审核中', 'aksrt'), 'resolved' => __('已处理', 'aksrt'), 'dismissed' => __('已驳回', 'aksrt'), '' => __('全部', 'aksrt')] as $key => $label) {
printf('<option value="%s"%s>%s</option>', esc_attr($key), selected($status, $key, false), esc_html($label));
}
echo '</select> ';
submit_button(__('筛选', 'aksrt'), 'secondary', '', false);
echo '</form>';
$items = $svc->get_reports(['status' => $status, 'limit' => 50, 'offset' => 0]);
echo '<table class="wp-list-table widefat fixed striped"><thead><tr>';
echo '<th>ID</th><th>' . esc_html__('举报人', 'aksrt') . '</th><th>' . esc_html__('目标', 'aksrt') . '</th><th>' . esc_html__('原因', 'aksrt') . '</th><th>' . esc_html__('状态', 'aksrt') . '</th><th>' . esc_html__('时间', 'aksrt') . '</th><th>' . esc_html__('操作', 'aksrt') . '</th>';
echo '</tr></thead><tbody>';
if (!$items) {
echo '<tr><td colspan="7">' . esc_html__('暂无举报', 'aksrt') . '</td></tr>';
}
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 '<tr>';
echo '<td>' . esc_html((string) $id) . '</td>';
echo '<td>' . ($ru ? esc_html($ru->display_name) : esc_html((string) $rid)) . '</td>';
echo '<td>' . esc_html((string) ($row['target_type'] ?? '') . ' #' . (string) ($row['target_id'] ?? '')) . '</td>';
echo '<td>' . esc_html((string) ($row['reason'] ?? '')) . '</td>';
echo '<td>' . esc_html($st) . '</td>';
echo '<td>' . esc_html((string) ($row['created_at'] ?? '')) . '</td>';
echo '<td><form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="display:flex;gap:4px;align-items:center;">';
echo '<input type="hidden" name="action" value="aksrt_users_update_report" />';
echo '<input type="hidden" name="report_id" value="' . esc_attr((string) $id) . '" />';
wp_nonce_field('aksrt_users_update_report_' . $id);
echo '<select name="new_status">';
foreach (['pending', 'reviewing', 'resolved', 'dismissed'] as $key) {
printf('<option value="%s"%s>%s</option>', esc_attr($key), selected($st, $key, false), esc_html($key));
}
echo '</select>';
echo '<input type="text" name="note" placeholder="' . esc_attr__('备注', 'aksrt') . '" />';
submit_button(__('更新', 'aksrt'), 'small', '', false);
echo '</form></td></tr>';
}
echo '</tbody></table>';
}
private function render_restrictions_tab(): void {
if (!class_exists('\AKSRT\User\Restriction')) {
echo '<div class="notice notice-error"><p>' . esc_html__('限制模块未初始化。', 'aksrt') . '</p></div>';
return;
}
$svc = \AKSRT\User\Restriction::get_instance();
$stats = $svc->get_stats();
echo '<p>' . 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)
)) . '</p>';
echo '<h2>' . esc_html__('添加限制', 'aksrt') . '</h2>';
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="margin-bottom:20px;display:flex;gap:8px;flex-wrap:wrap;align-items:end;">';
echo '<input type="hidden" name="action" value="aksrt_users_add_restriction" />';
wp_nonce_field('aksrt_users_add_restriction');
echo '<p><label>' . esc_html__('用户 ID', 'aksrt') . '<br><input type="number" name="user_id" min="1" required /></label></p>';
echo '<p><label>' . esc_html__('类型', 'aksrt') . '<br><select name="type">';
foreach (['warn' => __('警告', 'aksrt'), 'mute' => __('禁言', 'aksrt'), 'suspend' => __('停权', 'aksrt'), 'ban' => __('封禁', 'aksrt')] as $key => $label) {
printf('<option value="%s">%s</option>', esc_attr($key), esc_html($label));
}
echo '</select></label></p>';
echo '<p><label>' . esc_html__('范围', 'aksrt') . '<br><select name="scope">';
foreach (['global' => __('全局', 'aksrt'), 'comment' => __('评论', 'aksrt')] as $key => $label) {
printf('<option value="%s">%s</option>', esc_attr($key), esc_html($label));
}
echo '</select></label></p>';
echo '<p><label>' . esc_html__('原因', 'aksrt') . '<br><input type="text" name="reason" class="regular-text" required /></label></p>';
submit_button(__('添加', 'aksrt'), 'primary', '', false);
echo '</form>';
global $wpdb;
$table = class_exists('\AKSRT\Core\DBMigrator') ? \AKSRT\Core\DBMigrator::table('user_restriction') : '';
if ($table === '') {
echo '<div class="notice notice-warning"><p>' . esc_html__('限制表不可用。', 'aksrt') . '</p></div>';
return;
}
$items = $wpdb->get_results("SELECT * FROM {$table} ORDER BY id DESC LIMIT 100", ARRAY_A);
echo '<table class="wp-list-table widefat fixed striped"><thead><tr>';
echo '<th>ID</th><th>' . esc_html__('用户', 'aksrt') . '</th><th>' . esc_html__('类型', 'aksrt') . '</th><th>' . esc_html__('范围', 'aksrt') . '</th><th>' . esc_html__('原因', 'aksrt') . '</th><th>' . esc_html__('过期', 'aksrt') . '</th><th>' . esc_html__('操作', 'aksrt') . '</th>';
echo '</tr></thead><tbody>';
if (!$items) {
echo '<tr><td colspan="7">' . esc_html__('暂无限制记录', 'aksrt') . '</td></tr>';
}
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 '<tr>';
echo '<td>' . esc_html((string) $id) . '</td>';
echo '<td>' . ($user ? esc_html($user->display_name) : esc_html((string) $user_id)) . '</td>';
echo '<td>' . esc_html((string) ($row['type'] ?? '')) . '</td>';
echo '<td>' . esc_html((string) ($row['scope'] ?? '')) . '</td>';
echo '<td>' . esc_html((string) ($row['reason'] ?? '')) . '</td>';
echo '<td>' . esc_html((string) ($row['expires_at'] ?? '—')) . '</td>';
echo '<td><form method="post" action="' . esc_url(admin_url('admin-post.php')) . '">';
echo '<input type="hidden" name="action" value="aksrt_users_remove_restriction" />';
echo '<input type="hidden" name="restriction_id" value="' . esc_attr((string) $id) . '" />';
wp_nonce_field('aksrt_users_remove_restriction_' . $id);
submit_button(__('移除', 'aksrt'), 'small', '', false);
echo '</form></td></tr>';
}
echo '</tbody></table>';
}
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\Messages\ReportCenter') && \AKSRT\Messages\ReportCenter::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 render_appeals_tab(): void {
if (!class_exists('\AKSRT\User\BanAppeal')) {
echo '<div class="notice notice-error"><p>' . esc_html__('申诉模块未初始化。', 'aksrt') . '</p></div>';
return;
}
$status = isset($_GET['status']) ? sanitize_key((string) wp_unslash($_GET['status'])) : 'pending';
if (!in_array($status, ['pending', 'approved', 'rejected', 'all'], true)) {
$status = 'pending';
}
echo '<form method="get" style="margin:12px 0;">';
echo '<input type="hidden" name="page" value="aksrt_user_ban" /><input type="hidden" name="tab" value="appeals" />';
echo '<select name="status">';
foreach (['pending' => __('待处理', 'aksrt'), 'approved' => __('已通过', 'aksrt'), 'rejected' => __('已驳回', 'aksrt'), 'all' => __('全部', 'aksrt')] as $key => $label) {
printf('<option value="%s"%s>%s</option>', esc_attr($key), selected($status, $key, false), esc_html($label));
}
echo '</select> ';
submit_button(__('筛选', 'aksrt'), 'secondary', '', false);
echo '</form>';
$users = get_users([
'meta_key' => \AKSRT\User\BanAppeal::META_STATUS,
'meta_compare' => 'EXISTS',
'number' => 100,
'fields' => ['ID', 'display_name', 'user_login', 'user_email'],
'orderby' => 'ID',
'order' => 'DESC',
]);
echo '<table class="wp-list-table widefat fixed striped"><thead><tr>';
echo '<th>UID</th><th>' . esc_html__('用户', 'aksrt') . '</th><th>' . esc_html__('状态', 'aksrt') . '</th><th>' . esc_html__('类型', 'aksrt') . '</th><th>' . esc_html__('说明', 'aksrt') . '</th><th>' . esc_html__('提交时间', 'aksrt') . '</th><th>' . esc_html__('操作', 'aksrt') . '</th>';
echo '</tr></thead><tbody>';
$shown = 0;
foreach ($users as $u) {
$uid = (int) $u->ID;
$st = \AKSRT\User\BanAppeal::get_status($uid);
if ($st === '') {
continue;
}
if ($status !== 'all' && $st !== $status) {
continue;
}
$payload = \AKSRT\User\BanAppeal::get_payload($uid);
$shown++;
echo '<tr>';
echo '<td>' . esc_html((string) $uid) . '</td>';
echo '<td>' . esc_html($u->display_name . ' (' . $u->user_login . ')') . '<br><span class="description">' . esc_html($u->user_email) . '</span></td>';
echo '<td>' . esc_html($st) . '</td>';
echo '<td>' . esc_html((string) ($payload['reason_key'] ?? '—')) . '</td>';
echo '<td>' . esc_html((string) ($payload['reason'] ?? '')) . '</td>';
echo '<td>' . esc_html((string) ($payload['time'] ?? '—')) . '</td>';
echo '<td>';
if ($st === 'pending') {
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="display:flex;gap:6px;flex-wrap:wrap;align-items:center;">';
echo '<input type="hidden" name="action" value="aksrt_users_process_appeal" />';
echo '<input type="hidden" name="user_id" value="' . esc_attr((string) $uid) . '" />';
wp_nonce_field('aksrt_users_process_appeal_' . $uid);
echo '<input type="text" name="note" placeholder="' . esc_attr__('处理备注', 'aksrt') . '" class="regular-text" />';
echo '<button type="submit" name="decision" value="approve" class="button button-primary">' . esc_html__('通过并解封', 'aksrt') . '</button>';
echo '<button type="submit" name="decision" value="reject" class="button">' . esc_html__('驳回', 'aksrt') . '</button>';
echo '</form>';
} else {
echo '—';
}
echo '</td></tr>';
}
if ($shown < 1) {
echo '<tr><td colspan="7">' . esc_html__('暂无申诉', 'aksrt') . '</td></tr>';
}
echo '</tbody></table>';
}
public function handle_process_appeal(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('权限不足', 'aksrt'));
}
$user_id = isset($_POST['user_id']) ? (int) $_POST['user_id'] : 0;
$decision = isset($_POST['decision']) ? sanitize_key((string) wp_unslash($_POST['decision'])) : '';
$note = isset($_POST['note']) ? sanitize_textarea_field((string) wp_unslash($_POST['note'])) : '';
check_admin_referer('aksrt_users_process_appeal_' . $user_id);
if ($user_id <= 0 || !in_array($decision, ['approve', 'reject'], true) || !class_exists('\AKSRT\User\BanAppeal')) {
$this->redirect('aksrt_user_ban', 'appeal_failed', ['tab' => 'appeals']);
}
$status = $decision === 'approve' ? 'approved' : 'rejected';
update_user_meta($user_id, \AKSRT\User\BanAppeal::META_STATUS, $status);
$payload = \AKSRT\User\BanAppeal::get_payload($user_id);
$payload['admin_note'] = $note;
$payload['handled_at'] = current_time('Y-m-d H:i');
$payload['handled_by'] = get_current_user_id();
$payload['decision'] = $status;
update_user_meta($user_id, \AKSRT\User\BanAppeal::META_PAYLOAD, $payload);
$history = get_user_meta($user_id, \AKSRT\User\BanAppeal::META_HISTORY, true);
$history = is_array($history) ? $history : [];
$history[] = $payload;
update_user_meta($user_id, \AKSRT\User\BanAppeal::META_HISTORY, array_slice($history, -30));
if ($decision === 'approve' && class_exists('\AKSRT\User\Restriction')) {
global $wpdb;
$table = class_exists('\AKSRT\Core\DBMigrator') ? \AKSRT\Core\DBMigrator::table('user_restriction') : '';
if ($table !== '') {
$wpdb->query($wpdb->prepare(
"UPDATE {$table} SET status = 'lifted', expires_at = %s WHERE user_id = %d AND type IN ('ban','suspend','mute')",
current_time('mysql'),
$user_id
));
}
$svc = \AKSRT\User\Restriction::get_instance();
if (method_exists($svc, 'clear_user_bans')) {
$svc->clear_user_bans($user_id);
} elseif (method_exists($svc, 'lift_ban')) {
$svc->lift_ban($user_id);
}
}
if (class_exists('\AKSRT\Messages\Message')) {
$msg = $decision === 'approve'
? __('你的禁封申诉已通过,限制已解除。', 'aksrt')
: __('你的禁封申诉未通过,可修改后重新提交。', 'aksrt');
if ($note !== '') {
$msg .= ' ' . sprintf(__('备注:%s', 'aksrt'), $note);
}
\AKSRT\Messages\Message::get_instance()->send(
$user_id,
'ban_appeal',
$msg,
[
'title' => $decision === 'approve' ? __('申诉通过', 'aksrt') : __('申诉驳回', 'aksrt'),
]
);
}
do_action('aksrt_ban_appeal_processed', $user_id, $decision, $note);
$this->redirect(
'aksrt_user_ban',
$decision === 'approve' ? 'appeal_approved' : 'appeal_rejected',
['tab' => 'appeals']
);
}
private function pagination(int $pages, int $page_num): void {
if ($pages <= 1) {
return;
}
echo '<div class="tablenav"><div class="tablenav-pages">';
echo paginate_links([
'base' => add_query_arg('paged', '%#%'),
'format' => '',
'prev_text' => '&laquo;',
'next_text' => '&raquo;',
'total' => $pages,
'current' => $page_num,
]);
echo '</div></div>';
}
}
+224
View File
@@ -0,0 +1,224 @@
<?php
/**
* Native WordPress identity certification operations.
*
* @package AKSRT
*/
namespace AKSRT\Admin\Native;
defined('ABSPATH') || exit;
trait UsersMenusVerify {
private function count_verify_status(string $status): int {
global $wpdb;
$table = \AKSRT\Core\DBMigrator::table('identity_application');
return (int) $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$table} WHERE status = %s", $status));
}
public function render_verify_page(): void {
$this->ensure_cap();
$filters = $this->get_verify_filters();
$page_num = max(1, (int) ($_GET['paged'] ?? 1));
$result = $this->query_verify_requests($filters, $page_num, 20);
$counts = $this->get_verify_status_counts();
echo '<div class="wrap"><h1>' . esc_html__('身份认证', 'aksrt') . '</h1>';
$this->flash();
echo '<p>' . esc_html(sprintf(__('人工待审 %1$d · 已通过 %2$d · 已驳回 %3$d · 已取消 %4$d', 'aksrt'), $counts['pending'], $counts['verified'], $counts['rejected'], $counts['cancelled'])) . '</p>';
$this->render_verify_filters($filters);
$this->render_verify_table($result['items']);
$this->pagination($result['pages'], $page_num);
echo '</div>';
}
/** @return array{status:string,keyword:string,date_from:string,date_to:string} */
private function get_verify_filters(): array {
$status = isset($_GET['status']) ? sanitize_key((string) wp_unslash($_GET['status'])) : 'pending';
if (!in_array($status, ['pending', 'verified', 'rejected', 'cancelled', ''], true)) {
$status = 'pending';
}
return [
'status' => $status,
'keyword' => isset($_GET['keyword']) ? sanitize_text_field((string) wp_unslash($_GET['keyword'])) : '',
'date_from' => $this->sanitize_verify_date($_GET['date_from'] ?? ''),
'date_to' => $this->sanitize_verify_date($_GET['date_to'] ?? ''),
];
}
private function sanitize_verify_date($value): string {
$value = sanitize_text_field((string) wp_unslash($value));
return preg_match('/^\d{4}-\d{2}-\d{2}$/', $value) ? $value : '';
}
/** @return array{pending:int,verified:int,rejected:int,cancelled:int,total:int} */
private function get_verify_status_counts(): array {
global $wpdb;
$table = \AKSRT\Core\DBMigrator::table('identity_application');
$counts = ['pending' => 0, 'verified' => 0, 'rejected' => 0, 'cancelled' => 0, 'total' => 0];
$rows = $wpdb->get_results("SELECT status, COUNT(*) total FROM {$table} GROUP BY status", ARRAY_A);
foreach ((array) $rows as $row) {
$status = (string) ($row['status'] ?? '');
$total = (int) ($row['total'] ?? 0);
$public = \AKSRT\Identity\TransitionService::public_status($status);
if (in_array($status, ['manual_pending', 'provider_failed', 'provider_unsupported', 'provider_expired'], true)) {
$counts['pending'] += $total;
} elseif (isset($counts[$public])) {
$counts[$public] += $total;
}
$counts['total'] += $total;
}
return $counts;
}
/** @return array{items:array<int,array<string,mixed>>,pages:int,total:int} */
private function query_verify_requests(array $filters, int $page_num, int $per_page): array {
global $wpdb;
$table = \AKSRT\Core\DBMigrator::table('identity_application');
$where = ['1=1'];
$params = [];
if ($filters['status'] === 'pending') {
$where[] = "a.status IN ('manual_pending','provider_failed','provider_unsupported','provider_expired')";
} elseif ($filters['status'] !== '') {
$where[] = 'a.status = %s';
$params[] = $filters['status'];
}
if ($filters['keyword'] !== '') {
$like = '%' . $wpdb->esc_like($filters['keyword']) . '%';
$where[] = '(CAST(u.ID AS CHAR) LIKE %s OR u.display_name LIKE %s OR u.user_login LIKE %s OR u.user_email LIKE %s OR a.public_name LIKE %s OR a.application_uuid LIKE %s)';
array_push($params, $like, $like, $like, $like, $like, $like);
}
if ($filters['date_from'] !== '') {
$where[] = 'a.created_at >= %s';
$params[] = $filters['date_from'] . ' 00:00:00';
}
if ($filters['date_to'] !== '') {
$where[] = 'a.created_at <= %s';
$params[] = $filters['date_to'] . ' 23:59:59';
}
$where_sql = implode(' AND ', $where);
$total_sql = "SELECT COUNT(*) FROM {$table} a INNER JOIN {$wpdb->users} u ON u.ID = a.user_id WHERE {$where_sql}";
$total = $params ? (int) $wpdb->get_var($wpdb->prepare($total_sql, $params)) : (int) $wpdb->get_var($total_sql);
$query_params = array_merge($params, [$per_page, ($page_num - 1) * $per_page]);
$sql = "SELECT a.*, u.display_name, u.user_login, u.user_email FROM {$table} a INNER JOIN {$wpdb->users} u ON u.ID = a.user_id WHERE {$where_sql} ORDER BY a.created_at DESC, a.id DESC LIMIT %d OFFSET %d";
$items = $wpdb->get_results($wpdb->prepare($sql, $query_params), ARRAY_A);
return ['items' => is_array($items) ? $items : [], 'pages' => max(1, (int) ceil(max(1, $total) / $per_page)), 'total' => $total];
}
private function render_verify_filters(array $filters): void {
echo '<form method="get" style="margin:12px 0;display:flex;gap:8px;align-items:end;flex-wrap:wrap;">';
echo '<input type="hidden" name="page" value="aksrt_user_auth" />';
echo '<label>' . esc_html__('状态', 'aksrt') . '<br><select name="status">';
foreach (['pending' => __('人工待审', 'aksrt'), 'verified' => __('已通过', 'aksrt'), 'rejected' => __('已驳回', 'aksrt'), 'cancelled' => __('已取消', 'aksrt'), '' => __('全部', 'aksrt')] as $key => $label) {
printf('<option value="%s"%s>%s</option>', esc_attr($key), selected($filters['status'], $key, false), esc_html($label));
}
echo '</select></label>';
echo '<label>' . esc_html__('搜索', 'aksrt') . '<br><input type="search" name="keyword" value="' . esc_attr($filters['keyword']) . '" /></label>';
echo '<label>' . esc_html__('申请起始日期', 'aksrt') . '<br><input type="date" name="date_from" value="' . esc_attr($filters['date_from']) . '" /></label>';
echo '<label>' . esc_html__('申请结束日期', 'aksrt') . '<br><input type="date" name="date_to" value="' . esc_attr($filters['date_to']) . '" /></label>';
submit_button(__('筛选', 'aksrt'), 'secondary', '', false);
echo '</form>';
}
private function render_verify_table(array $items): void {
echo '<table class="wp-list-table widefat fixed striped"><thead><tr><th>用户</th><th>状态</th><th>申请资料</th><th>审核记录</th><th>操作</th></tr></thead><tbody>';
if ($items === []) {
echo '<tr><td colspan="5">' . esc_html__('暂无认证申请', 'aksrt') . '</td></tr>';
}
foreach ($items as $row) {
$this->render_verify_row($row);
}
echo '</tbody></table>';
}
private function render_verify_row(array $row): void {
$user_id = (int) $row['user_id'];
$status = (string) $row['status'];
$application = (new \AKSRT\Identity\ApplicationRepository())->find_by_id((int) $row['id']) ?: $row;
$history = (new \AKSRT\Identity\EventRepository())->for_application((int) $row['id']);
echo '<tr><td><strong><a href="' . esc_url(get_edit_user_link($user_id)) . '">' . esc_html((string) $row['display_name']) . '</a></strong>';
echo '<div class="description">@' . esc_html((string) $row['user_login']) . ' · UID ' . $user_id . '</div><div>' . esc_html((string) $row['user_email']) . '</div></td>';
echo '<td>' . wp_kses_post($this->verify_status_badge($status)) . '<div class="description">' . esc_html((string) $row['provider']) . ' / ' . esc_html((string) $row['channel']) . '</div></td>';
echo '<td><strong>' . esc_html((string) $row['public_name']) . '</strong><div>' . esc_html((string) $row['type']) . '</div>';
echo '<div class="description">UUID: ' . esc_html((string) $row['application_uuid']) . '</div>';
if (!empty($row['provider_result_code'])) echo '<div>' . esc_html__('云结果:', 'aksrt') . esc_html((string) $row['provider_result_code']) . '</div>';
foreach ((new \AKSRT\Identity\EvidenceService())->items($application, true) as $item) {
echo '<a href="' . esc_url($item['url']) . '" target="_blank" rel="noopener noreferrer"><img src="' . esc_url($item['thumbnail_url']) . '" alt="" style="width:64px;height:64px;object-fit:cover;margin:8px 8px 0 0;border-radius:4px;" /></a>';
}
echo '</td><td>';
if (!empty($row['review_note'])) echo '<div>' . esc_html((string) $row['review_note']) . '</div>';
if ($history !== []) echo '<details><summary>' . esc_html(sprintf(__('事件记录(%d)', 'aksrt'), count($history))) . '</summary><ol>';
foreach ($history as $event) echo '<li>' . esc_html((string) $event['created_at'] . ' · ' . (string) $event['event_type'] . ' · ' . (string) $event['to_status']) . '</li>';
if ($history !== []) echo '</ol></details>';
echo '</td><td>';
$this->render_verify_actions($row);
echo '</td></tr>';
}
private function verify_status_badge(string $status): string {
$labels = ['manual_pending' => __('人工待审', 'aksrt'), 'verified' => __('已通过', 'aksrt'), 'rejected' => __('已驳回', 'aksrt'), 'cancelled' => __('已取消', 'aksrt'), 'provider_pending' => __('等待核身', 'aksrt'), 'provider_processing' => __('云端处理中', 'aksrt'), 'provider_failed' => __('云认证失败', 'aksrt'), 'provider_unsupported' => __('云端不支持', 'aksrt'), 'provider_expired' => __('会话已过期', 'aksrt')];
return '<span style="display:inline-block;padding:2px 8px;border:1px solid currentColor;border-radius:999px;">' . esc_html($labels[$status] ?? $status) . '</span>';
}
private function render_verify_actions(array $row): void {
$status = (string) $row['status'];
if (in_array($status, ['manual_pending', 'provider_failed', 'provider_unsupported', 'provider_expired'], true)) {
foreach (['verified' => __('通过认证', 'aksrt'), 'rejected' => __('驳回申请', 'aksrt')] as $next => $label) {
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="display:grid;gap:6px;margin-bottom:8px;">';
echo '<input type="hidden" name="action" value="aksrt_users_review_verify" /><input type="hidden" name="user_id" value="' . (int) $row['user_id'] . '" /><input type="hidden" name="application_uuid" value="' . esc_attr((string) $row['application_uuid']) . '" /><input type="hidden" name="expected_status" value="' . esc_attr($status) . '" /><input type="hidden" name="status" value="' . esc_attr($next) . '" />';
wp_nonce_field('aksrt_users_review_verify_' . (int) $row['user_id']);
echo '<textarea name="note" rows="2" ' . ($next === 'rejected' ? 'required' : '') . '></textarea><button class="button ' . ($next === 'verified' ? 'button-primary' : '') . '">' . esc_html($label) . '</button></form>';
}
} elseif ($status === 'verified') {
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '"><input type="hidden" name="action" value="aksrt_users_review_verify" /><input type="hidden" name="user_id" value="' . (int) $row['user_id'] . '" /><input type="hidden" name="application_uuid" value="' . esc_attr((string) $row['application_uuid']) . '" /><input type="hidden" name="expected_status" value="verified" /><input type="hidden" name="status" value="cancelled" />';
wp_nonce_field('aksrt_users_review_verify_' . (int) $row['user_id']);
echo '<textarea name="note" required></textarea><button class="button button-link-delete">' . esc_html__('取消认证', 'aksrt') . '</button></form>';
} else {
echo '<span class="description">' . esc_html__('无需人工操作', 'aksrt') . '</span>';
}
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="margin-top:8px;" onsubmit="return confirm(\'' . esc_js(__('确定删除这条身份认证记录?关联事件和人工材料也会删除。', 'aksrt')) . '\');">';
echo '<input type="hidden" name="action" value="aksrt_users_delete_verify" /><input type="hidden" name="application_uuid" value="' . esc_attr((string) $row['application_uuid']) . '" />';
wp_nonce_field('aksrt_users_delete_verify_' . (string) $row['application_uuid']);
echo '<button class="button button-link-delete">' . esc_html__('删除记录', 'aksrt') . '</button></form>';
}
public function handle_review_verify(): void {
if (!current_user_can('aksrt_review_identity') && !current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('权限不足', 'aksrt'));
}
$user_id = (int) ($_POST['user_id'] ?? 0);
check_admin_referer('aksrt_users_review_verify_' . $user_id);
$uuid = sanitize_text_field((string) wp_unslash($_POST['application_uuid'] ?? ''));
$expected = sanitize_key((string) wp_unslash($_POST['expected_status'] ?? ''));
$status = sanitize_key((string) wp_unslash($_POST['status'] ?? ''));
$note = sanitize_textarea_field((string) wp_unslash($_POST['note'] ?? ''));
$application = (new \AKSRT\Identity\ApplicationRepository())->find_by_uuid($uuid);
$result = (!$application || (int) $application['user_id'] !== $user_id || (string) $application['status'] !== $expected)
? new \WP_Error('aksrt_verify_conflict', __('认证状态已变化,请刷新后重试', 'aksrt'))
: \AKSRT\User\AuthApply::review($user_id, $status, $note, get_current_user_id());
$extra = ['status' => $status === 'cancelled' ? 'cancelled' : 'pending'];
if (is_wp_error($result)) $extra['aksrt_verify_error'] = $result->get_error_message();
$this->redirect('aksrt_user_auth', is_wp_error($result) ? 'verify_failed' : 'verify_updated', $extra);
}
public function handle_delete_verify(): void {
if (!current_user_can('aksrt_review_identity') && !current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('权限不足', 'aksrt'));
}
$uuid = sanitize_text_field((string) wp_unslash($_POST['application_uuid'] ?? ''));
check_admin_referer('aksrt_users_delete_verify_' . $uuid);
$repo = new \AKSRT\Identity\ApplicationRepository();
$application = $repo->find_by_uuid($uuid);
if (!$application) {
$this->redirect('aksrt_user_auth', 'verify_failed', ['aksrt_verify_error' => __('认证记录不存在', 'aksrt')]);
}
(new \AKSRT\Identity\EvidenceService())->delete((array) ($application['evidence_ids'] ?? []));
(new \AKSRT\Identity\EventRepository())->delete_for_application((int) $application['id']);
$deleted = $repo->delete((int) $application['id']);
$this->redirect('aksrt_user_auth', $deleted ? 'verify_deleted' : 'verify_failed', $deleted ? [] : ['aksrt_verify_error' => __('认证记录删除失败', 'aksrt')]);
}
private function get_verify_history(int $user_id): array {
return \AKSRT\User\AuthApply::get_review_history($user_id);
}
}