Files
wordpress-theme-aksrt/src/Admin/Native/PayChargeCards.php
T
kate522 8c5b1555c9 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).
2026-08-02 22:06:06 +08:00

349 lines
15 KiB
PHP

<?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>';
}
}