feat(admin): 支付中心列表页统一为 WP 原生分页
七个支付中心列表页(订单明细 / 售后服务 / 付费文章 / 优惠码 / 创作分成 /
提现审核 / 卡密管理)的分页收敛到共享助手 AdminListPagination,改用 WP 原生
paginate_links() 渲染。
- 基准 URL 显式以 page 参数 + 当前筛选参数拼装,不再让两个参数的
add_query_arg('paged', ...) 退回 $_SERVER['REQUEST_URI']:链接不再随当前
请求形态漂移,列条件也不再丢失
- 售后服务从写死的 limit => 100 改为 limit/offset 分页,总数接上原本零调用的
AfterSaleService::count();after_sale_status 先按状态常量归一,保证 count()
与 admin_list() 口径一致,分页条不说谎
- 每页 20 条,统一来自 AdminListPagination::DEFAULT_PER_PAGE
- 每页在查询前夹取页码,越界时回退到最后一页并重查,杜绝「空表且没有分页条」
的死胡同;页码夹取与早退也一并收敛到助手
- 提现审核的翻页链接排除 retry_id / retry_key 两个一次性动作参数
- 订单明细的标签页与翻页链接共用同一份筛选参数,不再各写一份
清理死代码:五个页面里近乎复制的 render_pagination()(其中订单明细与提现审核
两份从未被调用)、两页重复的内联 paginate_links 块、卡密管理的 render_pagenavi()
及其 docblock、PayChargeCards::DEFAULT_PER_PAGE 常量,以及只服务自定义禁用
按钮的 .tablenav-pages a.disabled 规则。
验证:tests/admin/ 76 项通过(新增 pay-pages-pagination.test.cjs,重写
charge-cards-pagination.test.cjs);用 git show HEAD 取出原文件对比,订单明细
与售后服务两页在新旧实现下渲染输出逐字节相同。
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
/**
|
||||
* 「卡密管理」分页契约。
|
||||
*
|
||||
* 背景(已修复的缺陷):
|
||||
* 背景(两次演进):
|
||||
* 1) 翻页链接此前写作 add_query_arg('paged', N) —— 只有两个参数时 WordPress 会落到
|
||||
* $_SERVER['REQUEST_URI'],链接绑在当前请求上,随请求形态漂移,列条件也容易丢。
|
||||
* 2) render_pagenavi() 的 URL 推导是一串 if/elseif。页码越界时(例如 per_page 缩小后
|
||||
@@ -12,14 +12,12 @@
|
||||
* 极端情况下 offset 落到数据之外,表格直接变空态并且连分页条都不输出,只能手改 URL 退出。
|
||||
* 3) ice_perpage 只在服务端被读取、默认每页条数偏大。条数小于默认值时永远只有一页,
|
||||
* 四个按钮恒为 disabled —— 看上去就是「分页是坏的」。
|
||||
* 处置:默认值定为 20;**不在工具栏增加「每页显示」控件**(用户明确要求),
|
||||
* ice_perpage 仅作为 URL 参数被尊重。
|
||||
*
|
||||
* 本契约固化四件事:
|
||||
* - 页码先夹到 [1, total_pages],禁用状态由夹取后的页码推导;
|
||||
* - 翻页链接基于显式基准 URL(page_url + 当前筛选参数),不再依赖 REQUEST_URI;
|
||||
* 现状:渲染已迁移到共享助手 AdminListPagination(WP 原生 paginate_links),
|
||||
* 基准 URL 与页码夹取都在那里做。本文件只固化卡密管理仍特有的部分:
|
||||
* - ice_perpage 仍作为 URL 参数被尊重,且工具栏不新增「每页显示」控件;
|
||||
* - 越界页码回退到最后一页并重查,不留空态死胡同;
|
||||
* - 每页条数默认 20,且工具栏不得新增「每页显示」控件。
|
||||
* - 筛选参数白名单化拼装,排序与每页条数随链接带走。
|
||||
*/
|
||||
|
||||
const assert = require('node:assert/strict');
|
||||
@@ -42,61 +40,58 @@ const slice = (from, to) => {
|
||||
};
|
||||
|
||||
const list = slice('private function render_list(', 'private function render_add_form(');
|
||||
const args = slice('private function list_query_args(', 'private function render_pagenavi(');
|
||||
const pager = slice('private function render_pagenavi(', 'private function print_assets(');
|
||||
const args = slice('private function list_query_args(', 'private function print_assets(');
|
||||
|
||||
test('翻页链接使用显式基准 URL,不再依赖 REQUEST_URI', () => {
|
||||
// 反面写法:只有两个参数的 add_query_arg —— 会退回到 $_SERVER['REQUEST_URI']。
|
||||
assert.doesNotMatch(page, /add_query_arg\('paged',\s*[^,)]+\)/);
|
||||
test('渲染已迁移到共享助手 AdminListPagination', () => {
|
||||
assert.match(
|
||||
list,
|
||||
/AdminListPagination::render\(self::PAGE_SLUG, \$count_all, \$paged, \$per_page, \$query_args\);/
|
||||
);
|
||||
|
||||
assert.match(pager, /\$base\s*=\s*\$query_args\s*\?\s*add_query_arg\(\$query_args,\s*\$this->page_url\)\s*:\s*\$this->page_url;/);
|
||||
assert.match(pager, /add_query_arg\('paged',\s*\$page,\s*\$base\)/);
|
||||
assert.equal(count(pager, /add_query_arg\('paged'/g), 1, '翻页 URL 只应有一个构造点');
|
||||
// 自定义标记与专属常量都应清空。
|
||||
assert.doesNotMatch(page, /render_pagenavi/);
|
||||
assert.doesNotMatch(page, /first-page button|prev-page button|next-page button|last-page button/);
|
||||
assert.doesNotMatch(page, /public const DEFAULT_PER_PAGE/);
|
||||
assert.doesNotMatch(page, /self::DEFAULT_PER_PAGE/);
|
||||
assert.match(page, /AdminListPagination::DEFAULT_PER_PAGE/);
|
||||
|
||||
// 只服务自定义禁用按钮的 CSS 规则一并删除。
|
||||
assert.doesNotMatch(page, /\.tablenav-pages a\.disabled \{ pointer-events: none; \}/);
|
||||
});
|
||||
|
||||
test('页码夹取到有效范围,禁用状态由夹取后的页码推导', () => {
|
||||
assert.match(pager, /\$total_pages\s*=\s*max\(1,\s*\(int\)\s*ceil\(\$total_count \/ \$number_per_page\)\)/);
|
||||
assert.match(pager, /\$current_page\s*=\s*min\(max\(1,\s*\(int\)\s*\(\$_GET\['paged'\] \?\? 1\)\),\s*\$total_pages\)/);
|
||||
test('ice_perpage 仍作为 URL 参数被尊重,默认每页 20', () => {
|
||||
assert.match(
|
||||
list,
|
||||
/\(int\) \(\$_REQUEST\['ice_perpage'\] \?\? AdminListPagination::DEFAULT_PER_PAGE\)/
|
||||
);
|
||||
|
||||
// 越界时 if/elseif 全不命中的分支结构不得回归。
|
||||
assert.match(pager, /\$on_first\s*=\s*\$current_page <= 1;/);
|
||||
assert.match(pager, /\$on_last\s*=\s*\$current_page >= \$total_pages;/);
|
||||
assert.equal(count(pager, /\$on_first \? '#' :/g), 2, '第一页/上一页都要按 on_first 落到 #');
|
||||
assert.equal(count(pager, /\$on_last \? '#' :/g), 2, '下一页/最后一页都要按 on_last 落到 #');
|
||||
assert.doesNotMatch(pager, /\$current_page == \$total_pages\b/);
|
||||
assert.doesNotMatch(pager, /\$prev_page_url\s*=\s*'#';/);
|
||||
});
|
||||
|
||||
test('越界页码回退到最后一页,不再出现无分页条的死胡同', () => {
|
||||
assert.match(list, /\$max_page\s*=\s*max\(1,\s*\(int\)\s*\(\$result\['max_pages'\] \?\? 1\)\)/);
|
||||
assert.match(list, /if \(!\$items && \$count_all > 0 && \$paged > \$max_page\)/);
|
||||
assert.match(list, /\$paged\s*=\s*\$max_page;/);
|
||||
// 回退之后必须重查,否则表格依旧是空的。
|
||||
assert.equal(count(list, /\$svc->get_list\(/g), 2);
|
||||
// 分页条必须在空态分支之后渲染,否则空态 return 会把分页条一起吞掉。
|
||||
assert.ok(list.indexOf('render_pagenavi(') > list.indexOf('未找到对应内容'), '分页渲染必须晚于空态处理');
|
||||
});
|
||||
|
||||
test('每页条数默认 20,工具栏不新增「每页显示」控件', () => {
|
||||
assert.match(page, /public const DEFAULT_PER_PAGE = 20;/);
|
||||
assert.match(list, /\(int\) \(\$_REQUEST\['ice_perpage'\] \?\? self::DEFAULT_PER_PAGE\)/);
|
||||
|
||||
// 明确约定:不在列表工具栏增加「每页显示」下拉/按钮。任何一处回来都视为回退。
|
||||
// 明确约定:不在列表工具栏增加「每页显示」下拉/按钮。
|
||||
assert.doesNotMatch(page, /per_page_selector/);
|
||||
assert.doesNotMatch(page, /PER_PAGE_CHOICES/);
|
||||
assert.doesNotMatch(page, /aksrt-perpage-form/);
|
||||
assert.doesNotMatch(page, /name="ice_perpage"/);
|
||||
assert.doesNotMatch(page, /order-header-right/);
|
||||
|
||||
// 但 ice_perpage 仍作为 URL 参数被尊重(无界面入口,供 URL / 程序化调用)。
|
||||
// 但 ice_perpage 仍随链接带走(无界面入口,供 URL / 程序化调用)。
|
||||
assert.match(args, /\$args\['ice_perpage'\] =/);
|
||||
|
||||
assert.match(list, /\$query_args = \$this->list_query_args\(/);
|
||||
assert.match(list, /render_pagenavi\(\$count_all, \$per_page, \$query_args\)/);
|
||||
// 工具栏恢复成「过滤链接 + 搜索表单」两段式。
|
||||
// 工具栏保持「过滤链接 + 搜索表单」两段式。
|
||||
assert.match(list, /echo '<div class="order-header"><ul class="subsubsub">' \. \$but_html \. '<\/ul>' \. \$search \. '<\/div>';/);
|
||||
});
|
||||
|
||||
test('越界页码回退到最后一页,不再出现无分页条的死胡同', () => {
|
||||
assert.match(list, /\$max_page\s*=\s*max\(1, \(int\) \(\$result\['max_pages'\] \?\? 1\)\)/);
|
||||
assert.match(list, /if \(!\$items && \$count_all > 0 && \$paged > \$max_page\)/);
|
||||
assert.match(list, /\$paged\s*=\s*\$max_page;/);
|
||||
// 回退之后必须重查,否则表格依旧是空的。
|
||||
assert.equal(count(list, /\$svc->get_list\(/g), 2);
|
||||
// 分页条必须在空态分支之后渲染,否则空态 return 会把分页条一起吞掉。
|
||||
assert.ok(
|
||||
list.indexOf('AdminListPagination::render(') > list.indexOf('未找到对应内容'),
|
||||
'分页渲染必须晚于空态处理'
|
||||
);
|
||||
});
|
||||
|
||||
test('筛选参数白名单化拼装,排序与每页条数随链接带走', () => {
|
||||
for (const key of ['status', 'type', 'other', 's', 'orderby', 'desc', 'ice_perpage']) {
|
||||
assert.match(args, new RegExp(`\\$args\\['${key}'\\] =`), `缺少 ${key}`);
|
||||
@@ -104,11 +99,7 @@ test('筛选参数白名单化拼装,排序与每页条数随链接带走', ()
|
||||
// 默认值不进 URL,避免链接里塞满无意义参数。
|
||||
assert.match(args, /\$orderby !== 'modified_time'/);
|
||||
assert.match(args, /\$order === 'ASC'/);
|
||||
assert.match(args, /\$per_page !== self::DEFAULT_PER_PAGE/);
|
||||
// paged 由 render_pagenavi 单独追加,不能混进基准参数里(否则会被自己覆盖)。
|
||||
assert.match(args, /\$per_page !== AdminListPagination::DEFAULT_PER_PAGE/);
|
||||
// paged 由 AdminListPagination 单独追加,不能混进基准参数里(否则会被自己覆盖)。
|
||||
assert.doesNotMatch(args, /\$args\['paged'\]/);
|
||||
});
|
||||
|
||||
test('禁用的翻页按钮不可点击,避免点空还滚回页首', () => {
|
||||
assert.match(page, /\.tablenav-pages a\.disabled \{ pointer-events: none; \}/);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user