ensure_cap(); $this->render_header(__('优惠码', 'aksrt')); if (!class_exists('\AKSRT\Payment\Coupon')) { echo '

' . esc_html__('优惠码模块未初始化。', 'aksrt') . '

'; $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 '

' . esc_html__('优惠码不存在。', 'aksrt') . '

'; } $this->render_create_form(); $this->render_batch_form(); $this->render_generated_dump(); $this->render_list($svc); $this->render_footer(); } /** * @param array $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 '

' . esc_html(sprintf( /* translators: %d coupon id */ __('编辑优惠码 #%d', 'aksrt'), $id )) . '

'; echo '

← ' . esc_html__('返回列表', 'aksrt') . '

'; echo '
'; echo ''; echo ''; wp_nonce_field('aksrt_pay_update_coupon_' . $id); echo '

'; echo '

'; echo '

'; echo '

'; echo '

'; echo '

'; echo '

'; $this->render_scope_fields($scope_type, implode(',', $scope_ids)); echo '

'; submit_button(__('保存修改', 'aksrt'), 'primary', '', false); echo '

'; } private function render_create_form(): void { echo '

' . esc_html__('新建优惠码', 'aksrt') . '

'; echo '
'; echo ''; wp_nonce_field('aksrt_pay_create_coupon'); echo '

'; echo '

'; echo '

'; echo '

'; echo '

'; echo '

'; echo '

'; $this->render_scope_fields('', ''); echo '

'; submit_button(__('创建', 'aksrt'), 'primary', '', false); echo '
'; } private function render_batch_form(): void { echo '

' . esc_html__('批量生成优惠码', 'aksrt') . '

'; echo '
'; echo ''; wp_nonce_field('aksrt_pay_batch_coupons'); echo '

'; echo '

'; echo '

'; echo '

'; echo '

'; echo '

'; echo '

'; echo '

'; echo '

'; $this->render_scope_fields('', ''); echo '

'; submit_button(__('批量生成', 'aksrt'), 'secondary', '', false); echo '
'; } private function render_scope_fields(string $selected_type, string $ids_text): void { echo '

'; echo '

'; } private function render_generated_dump(): void { if (!empty($_GET['aksrt_coupon_generated'])) { $generated = (int) $_GET['aksrt_coupon_generated']; echo '

' . esc_html(sprintf( /* translators: %d count */ __('成功生成 %d 个优惠码。', 'aksrt'), $generated )) . '

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

' . esc_html__('本次生成优惠码(请及时复制保存):', 'aksrt') . '

'; echo ''; echo '
'; } } 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 = AdminListPagination::DEFAULT_PER_PAGE; $type_labels = [ 'fixed' => __('固定金额', 'aksrt'), 'percent' => __('折扣百分比', 'aksrt'), ]; $status_labels = [ 'active' => __('可用', 'aksrt'), 'disabled' => __('已禁用', 'aksrt'), ]; echo '
'; echo ''; echo ' '; echo ' '; submit_button(__('筛选', 'aksrt'), 'secondary', '', false); echo '
'; $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) ceil($total / $per_page)); // 页码越界(手工改 URL、或筛选后总数变小)会让 offset 落到数据之外:表格空掉 // 且分页条不输出,只能手改 URL 退出。回退到最后一页重查一次。 if ($page_num > $pages) { $page_num = $pages; $result = $svc->get_list([ 'status' => $status, 'search' => $search, 'limit' => $per_page, 'offset' => ($page_num - 1) * $per_page, ]); $items = $result['items'] ?? []; } echo ''; echo ''; echo ''; if (!$items) { echo ''; } foreach ($items as $row) { $id = (int) ($row['id'] ?? 0); $st = (string) ($row['status'] ?? ''); $row_type = (string) ($row['type'] ?? ''); echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; } // 翻页链接显式带上当前筛选条件(不含 paged),口径与上方筛选表单一致。 $query_args = array_filter([ 'status' => $status !== '' ? $status : null, 's' => $search !== '' ? $search : null, ], static function ($value): bool { return $value !== null; }); echo '
ID' . esc_html__('代码', 'aksrt') . '' . esc_html__('类型', 'aksrt') . '' . esc_html__('面值', 'aksrt') . '' . esc_html__('门槛/封顶', 'aksrt') . '' . esc_html__('范围', 'aksrt') . '' . esc_html__('已用/上限/每人', 'aksrt') . '' . esc_html__('状态', 'aksrt') . '' . esc_html__('过期', 'aksrt') . '' . esc_html__('操作', 'aksrt') . '
' . esc_html__('暂无优惠码', 'aksrt') . '
' . esc_html((string) $id) . '' . esc_html((string) ($row['code'] ?? '')) . '' . esc_html($type_labels[$row_type] ?? $row_type) . '' . esc_html((string) ($row['value'] ?? '')) . ($row_type === 'percent' ? '%' : '') . '' . esc_html((string) ($row['min_amount'] ?? 0)) . ' / ' . esc_html((string) ($row['max_discount'] ?? 0)) . '' . esc_html($this->format_scope_label($row['product_scope'] ?? [])) . '' . esc_html((string) ((int) ($row['used_count'] ?? 0)) . '/' . (string) ((int) ($row['usage_limit'] ?? 0)) . '/' . (string) ((int) ($row['per_user_limit'] ?? 0))) . '' . esc_html($status_labels[$st] ?? $st) . '' . esc_html((string) ($row['expires_at'] ?? '')) . ''; echo '' . esc_html__('编辑', 'aksrt') . ''; if ($st === 'active') { echo '
'; echo ''; echo ''; wp_nonce_field('aksrt_pay_disable_coupon_' . $id); submit_button(__('禁用', 'aksrt'), 'small', '', false); echo '
'; } elseif ($st === 'disabled') { echo '
'; echo ''; echo ''; wp_nonce_field('aksrt_pay_enable_coupon_' . $id); submit_button(__('启用', 'aksrt'), 'small', '', false); echo '
'; } echo '
'; AdminListPagination::render(self::PAGE_SLUG, $total, $page_num, $per_page, $query_args); } 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' => trim((string) wp_unslash($_POST['value'] ?? '0')), 'min_amount' => trim((string) wp_unslash($_POST['min_amount'] ?? '0')), 'max_discount' => trim((string) wp_unslash($_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' => trim((string) wp_unslash($_POST['value'] ?? '0')), 'min_amount' => trim((string) wp_unslash($_POST['min_amount'] ?? '0')), 'max_discount' => trim((string) wp_unslash($_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' => trim((string) wp_unslash($_POST['value'] ?? '0')), 'min_amount' => trim((string) wp_unslash($_POST['min_amount'] ?? '0')), 'max_discount' => trim((string) wp_unslash($_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 */ 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 '
'; echo '

' . esc_html($title) . '

'; $this->render_flash(); } private function render_footer(): void { echo '
'; } 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 '

' . esc_html($map[$code]) . '

'; } /** * @param array $args */ private function page_url(array $args = []): string { return add_query_arg(array_merge(['page' => self::PAGE_SLUG], $args), admin_url('admin.php')); } }