feat(admin): plate moderator roles, advanced limits, coupon scope edit/batch

This commit is contained in:
2026-07-15 03:30:08 +08:00
parent 733a1670ec
commit deee48245f
8 changed files with 849 additions and 338 deletions
@@ -41,10 +41,7 @@ class AKSRT_Admin_Pay_Menus {
add_action('admin_post_aksrt_pay_update_after_sale', [$this, 'handle_update_after_sale']);
add_action('admin_post_aksrt_pay_create_after_sale', [$this, 'handle_create_after_sale']);
add_action('admin_post_aksrt_pay_after_sale_action', [$this, 'handle_after_sale_action']);
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_disable_coupon', [$this, 'handle_disable_coupon']);
add_action('admin_post_aksrt_pay_enable_coupon', [$this, 'handle_enable_coupon']);
// Coupon admin_post handlers live in AKSRT_Admin_Pay_Coupons.
add_action('admin_post_aksrt_pay_settle_income', [$this, 'handle_settle_income']);
add_action('admin_post_aksrt_pay_settle_rebate', [$this, 'handle_settle_rebate']);
add_action('admin_post_aksrt_pay_cancel_rebate', [$this, 'handle_cancel_rebate']);
@@ -78,7 +75,13 @@ class AKSRT_Admin_Pay_Menus {
'coupons' => [
'slug' => 'aksrt_coupons',
'title' => __('优惠码', 'aksrt'),
'callback' => [$this, 'render_coupons_page'],
'callback' => static function (): void {
if (class_exists('AKSRT_Admin_Pay_Coupons')) {
AKSRT_Admin_Pay_Coupons::get_instance()->render_page();
return;
}
wp_die(esc_html__('优惠码模块未初始化。', 'aksrt'));
},
],
'charge_cards' => [
'slug' => 'aksrt_charge_cards',
@@ -221,13 +224,6 @@ class AKSRT_Admin_Pay_Menus {
'after_sale_failed' => __('售后状态更新失败。', 'aksrt'),
'after_sale_created' => __('售后工单已创建。', 'aksrt'),
'after_sale_create_failed' => __('售后工单创建失败。', 'aksrt'),
'coupon_created' => __('优惠码已创建。', 'aksrt'),
'coupon_create_failed'=> __('优惠码创建失败(可能重复)。', 'aksrt'),
'coupon_batch_ok' => __('优惠码批量生成完成。', 'aksrt'),
'coupon_batch_failed' => __('优惠码批量生成失败。', 'aksrt'),
'coupon_disabled' => __('优惠码已禁用。', 'aksrt'),
'coupon_enabled' => __('优惠码已启用。', 'aksrt'),
'coupon_failed' => __('优惠码操作失败。', 'aksrt'),
'income_settled' => __('分成已结算。', 'aksrt'),
'income_failed' => __('分成结算失败。', 'aksrt'),
'rebate_settled' => __('返佣已结算。', 'aksrt'),
@@ -1608,246 +1604,6 @@ class AKSRT_Admin_Pay_Menus {
exit;
}
public function render_coupons_page(): void {
$this->ensure_cap();
$this->render_header(__('优惠码', 'aksrt'));
if (!class_exists('AKSRT_Coupon')) {
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'])) : '';
$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_Coupon::get_instance();
$type_labels = [
'fixed' => __('固定金额', 'aksrt'),
'percent' => __('折扣百分比', 'aksrt'),
];
$status_labels = [
'active' => __('可用', 'aksrt'),
'disabled' => __('已禁用', 'aksrt'),
];
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>';
echo '<p><label>' . esc_html__('适用范围', 'aksrt') . '<br><select name="scope_type">';
echo '<option value="">' . esc_html__('全部商品', 'aksrt') . '</option>';
echo '<option value="include">' . esc_html__('仅指定商品 ID', 'aksrt') . '</option>';
echo '<option value="exclude">' . esc_html__('排除商品 ID', 'aksrt') . '</option>';
echo '<option value="category">' . esc_html__('指定分类 ID', 'aksrt') . '</option>';
echo '</select></label></p>';
echo '<p><label>' . esc_html__('范围 ID', 'aksrt') . '<br><input type="text" name="scope_ids" class="regular-text" placeholder="' . esc_attr__('逗号分隔,如 12,34', 'aksrt') . '" /></label></p>';
echo '<p><label>' . esc_html__('过期时间', 'aksrt') . '<br><input type="datetime-local" name="expires_at" /></label></p>';
submit_button(__('创建', 'aksrt'), 'primary', '', false);
echo '</form>';
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>';
echo '<p><label>' . esc_html__('过期时间', 'aksrt') . '<br><input type="datetime-local" name="expires_at" /></label></p>';
submit_button(__('批量生成', 'aksrt'), 'secondary', '', false);
echo '</form>';
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>';
}
echo '<form method="get" style="margin:12px 0;">';
echo '<input type="hidden" name="page" value="aksrt_coupons" />';
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'] ?? [];
$total = (int) ($result['total'] ?? 0);
$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_coupon_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>';
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_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')) . '">';
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>';
} else {
echo '—';
}
echo '</td></tr>';
}
echo '</tbody></table>';
$this->render_pagination($pages, $page_num);
$this->render_footer();
}
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_Coupon')) {
$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';
}
}
$id = AKSRT_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_coupon_product_scope_from_request(),
'expires_at' => $expires,
]);
$ok = $id > 0;
}
wp_safe_redirect($this->page_url('aksrt_coupons', ['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_Coupon')) {
$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';
}
}
$result = AKSRT_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),
'expires_at' => $expires,
], (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_coupons', [
'aksrt_pay_msg' => $ok ? 'coupon_batch_ok' : 'coupon_batch_failed',
'aksrt_coupon_generated' => $created,
]));
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_Coupon') && AKSRT_Coupon::get_instance()->disable($id);
wp_safe_redirect($this->page_url('aksrt_coupons', ['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_Coupon') && AKSRT_Coupon::get_instance()->enable($id);
wp_safe_redirect($this->page_url('aksrt_coupons', ['aksrt_pay_msg' => $ok ? 'coupon_enabled' : 'coupon_failed']));
exit;
}
/* ------------------------------------------------------------------
* Income / rebate
* ----------------------------------------------------------------*/
@@ -2215,53 +1971,6 @@ class AKSRT_Admin_Pay_Menus {
echo '</ol></div>';
}
/**
* @return array<string,mixed>
*/
private function parse_coupon_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'] = ['aksrt_product_cat', 'category'];
}
return $scope;
}
/**
* @param mixed $scope
*/
private function format_coupon_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 render_pagination(int $pages, int $page_num): void {
if ($pages <= 1) {
return;