fix(frontend): route the VIP points-exchange entry through the custom currency name
/user/vip/ 档位购买卡下方的入口横条仍硬编码「积分兑换会员」,而后台
「积分配置 → 自定义货币名称」改名后不跟随。上一轮改名只覆盖了兑换弹窗
标题与会员弹窗内入口,页面自身的入口横条是另一处服务端渲染,漏改了。
入口横条(inc/helpers/aksrt-vip.php)
- aksrt_get_vip_points_exchange_entry_html 改取已加载的
catalog.currency_name,回退 Settings::get_currency_name() 再回退「积分」
同一页面上其余漏改的兑换文案一并收口,命名与入口/弹窗保持一致:
- VipServiceModal:积分不足提示 → {货币名}不足,暂时无法兑换
- VipServicePurchase.exchange_with_points:兑换未启用 / 兑换无效 /
不足三处 WP_Error、流水说明、订单备注(%1$s兑换会员 %2$d %1$s。)
currency_name 在此单独解析,get_catalog 内的局部变量不在该作用域
- LedgerPresentation:vip_points_exchange 流水原因标签
契约测试新增源断言,并用两个 PHP harness 做运行时断言:入口横条
(改名后 / 已是会员不输出 / 缺 key 回退)与弹窗(余额充足 / 不足两分支)。
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
4e62b325d2
commit
5b16bc6e15
7 files changed
+177
-11
No files matched your search
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace {
|
||||
define('ABSPATH', __DIR__ . '/');
|
||||
|
||||
function __(string $text, string $domain = ''): string { return $text; }
|
||||
function esc_html($value): string { return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8'); }
|
||||
function esc_html__($value, string $domain = ''): string { return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8'); }
|
||||
function esc_attr($value): string { return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8'); }
|
||||
function esc_attr__($value, string $domain = ''): string { return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8'); }
|
||||
function esc_url($value): string { return (string) $value; }
|
||||
function wp_kses_post($value): string { return (string) $value; }
|
||||
function aksrt_get_user_center_tab_url(string $tab): string { return '/user/' . $tab; }
|
||||
function add_action(string $hook, callable $cb, int $p = 10, int $a = 1): void {}
|
||||
}
|
||||
|
||||
namespace AKSRT\Content {
|
||||
// VipService 门面:get_catalog 由探测值驱动(含 currency_name + 可兑换套餐)。
|
||||
final class VipService {
|
||||
public static function get_instance(): self { return new self(); }
|
||||
|
||||
/** @return array<string,mixed> */
|
||||
public function get_catalog(int $user_id = 0): array {
|
||||
return [
|
||||
'currency_name' => $GLOBALS['aksrt_probe_currency'] ?? '积分',
|
||||
'points_exchange' => [
|
||||
'enabled' => true,
|
||||
'packages' => [['package_id' => 6001, 'level' => 1, 'points' => 500]],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace AKSRT\Settings {
|
||||
final class Settings {
|
||||
// 仅在 catalog 未带 currency_name 时回退,用于验证兜底路径。
|
||||
public static function get_currency_name(): string { return $GLOBALS['aksrt_probe_fallback'] ?? '积分'; }
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
require dirname(__DIR__, 2) . '/inc/helpers/aksrt-vip.php';
|
||||
|
||||
$GLOBALS['aksrt_probe_currency'] = isset($argv[1]) ? (string) $argv[1] : '书币';
|
||||
$GLOBALS['aksrt_probe_fallback'] = isset($argv[2]) ? (string) $argv[2] : '回退币';
|
||||
|
||||
// 场景 1:后台改名后,横条标题跟随自定义货币名。
|
||||
echo aksrt_get_vip_points_exchange_entry_html(1, 0), "\n";
|
||||
// 场景 2:已是会员 → 不输出。
|
||||
echo (aksrt_get_vip_points_exchange_entry_html(1, 2) === '' ? 'VIP_HIDDEN' : 'VIP_SHOWN'), "\n";
|
||||
// 场景 3:catalog 未带 currency_name → 回退到设置里的名称。
|
||||
$GLOBALS['aksrt_probe_currency'] = '';
|
||||
echo aksrt_get_vip_points_exchange_entry_html(1, 0), "\n";
|
||||
}
|
||||
@@ -51,11 +51,15 @@ namespace {
|
||||
'unit' => 'day',
|
||||
]],
|
||||
],
|
||||
'user_points' => 1000,
|
||||
'currency_name' => '积分',
|
||||
// 可用 argv 覆盖余额,用于覆盖「不足」分支(套餐价 500)。
|
||||
'user_points' => isset($GLOBALS['aksrt_probe_points']) ? (int) $GLOBALS['aksrt_probe_points'] : 1000,
|
||||
// 自定义货币名(后台「积分配置 → 自定义货币名称」):标题/我的X/
|
||||
// 不足提示都跟随它,不硬编码「积分」。
|
||||
'currency_name' => '书币',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$GLOBALS['aksrt_probe_points'] = isset($argv[1]) ? (int) $argv[1] : 1000;
|
||||
echo (new VipPointsDescriptionProbe())->get_points_exchange_modal_html(1);
|
||||
}
|
||||
@@ -35,6 +35,7 @@ const creditsJs = read('assets', 'js', 'credits.js');
|
||||
const vipCss = read('assets', 'css', 'frontend', 'vip-modal.css');
|
||||
const vipTab = read('inc', 'helpers', 'user-center-renderers', 'vip-tab.php');
|
||||
const creditsCss = read('assets', 'css', 'routes', 'user-center', 'credits.css');
|
||||
const ledger = read('src', 'Ledger', 'LedgerPresentation.php');
|
||||
|
||||
test('弹窗为参考主题 refresh_modal 同构静态壳并带 data 钩子', () => {
|
||||
assert.match(modal, /public function get_points_exchange_modal_html/);
|
||||
@@ -105,6 +106,48 @@ test('兑换说明从后台设置实际输出,保留 HTML 并显示在卡片
|
||||
assert.ok(rendered.indexOf('aksrt-vip-points-desc') < rendered.indexOf('data-aksrt-vip-points-submit'));
|
||||
});
|
||||
|
||||
test('运行时:自定义货币名改写弹窗标题/我的X/卡片单位,不足提示同源', () => {
|
||||
// harness 的 catalog.currency_name = 书币(模拟后台改名后的设置值)。
|
||||
const rendered = execFileSync(process.env.PHP_BINARY || 'php', [
|
||||
path.join(__dirname, 'vip-points-exchange-description-harness.php'),
|
||||
], { encoding: 'utf8' });
|
||||
// 标题与「我的书币」盒。
|
||||
assert.match(rendered, /书币兑换会员/);
|
||||
assert.match(rendered, /我的书币/);
|
||||
// 卡片时长行右侧的单位。
|
||||
assert.match(rendered, /500<span class="px12">书币<\/span>/);
|
||||
assert.doesNotMatch(rendered, /积分兑换会员/);
|
||||
assert.doesNotMatch(rendered, /我的积分/);
|
||||
// 余额充足 → 输出兑换 CTA 而非不足提示。
|
||||
assert.match(rendered, /data-aksrt-vip-points-submit/);
|
||||
});
|
||||
|
||||
test('运行时:余额不足时提示文案同样跟随自定义货币名', () => {
|
||||
// user_points(100) < 套餐价(500) → 走 badg c-red 分支,替换货币名后应显示「书币不足」。
|
||||
const rendered = execFileSync(process.env.PHP_BINARY || 'php', [
|
||||
path.join(__dirname, 'vip-points-exchange-description-harness.php'),
|
||||
'100',
|
||||
], { encoding: 'utf8' });
|
||||
assert.match(rendered, /书币不足,暂时无法兑换/);
|
||||
assert.doesNotMatch(rendered, /积分不足/);
|
||||
assert.doesNotMatch(rendered, /data-aksrt-vip-points-submit/);
|
||||
});
|
||||
|
||||
test('运行时:/user/vip/ 入口横条实际渲染出自定义货币名', () => {
|
||||
const out = execFileSync(process.env.PHP_BINARY || 'php', [
|
||||
path.join(__dirname, 'vip-points-entry-harness.php'),
|
||||
], { encoding: 'utf8' }).split('\n');
|
||||
|
||||
// 场景 1:后台改名后,横条标题 = 「{货币名}兑换会员」,不再硬编码「积分」。
|
||||
assert.match(out[0], /<a class="uc-vip-points-entry flex ac jsb"[^>]*data-aksrt-vip-points-open>/);
|
||||
assert.match(out[0], /书币兑换会员/);
|
||||
assert.doesNotMatch(out[0], /积分兑换会员/);
|
||||
// 场景 2:已是会员 → 入口不输出(参考主题门控)。
|
||||
assert.strictEqual(out[1], 'VIP_HIDDEN');
|
||||
// 场景 3:catalog 未带 currency_name → 回退到设置里的名称,不显示空名。
|
||||
assert.match(out[2], /回退币兑换会员/);
|
||||
});
|
||||
|
||||
test('弹窗壳静止态必须被 vip-modal.css 显式隐藏(.flex 覆盖回归护栏)', () => {
|
||||
assert.match(vipCss, /\.modal\[data-aksrt-vip-points-modal\]\s*\{\s*display:\s*none;/);
|
||||
assert.match(vipCss, /\.modal\[data-aksrt-vip-points-modal\]\.in\s*\{\s*display:\s*flex;/);
|
||||
@@ -162,6 +205,35 @@ test('/user/vip/ 入口门控与弹窗壳同源:非会员 + 兑换开启 + 有
|
||||
assert.match(footer, /int \$vip_level = -1/);
|
||||
});
|
||||
|
||||
test('/user/vip/ 入口文案跟随自定义货币名称,不硬编码「积分」', () => {
|
||||
// 横条标题取 catalog.currency_name(后台「积分配置 → 自定义货币名称」),
|
||||
// 与它打开的兑换弹窗标题(VipServiceModal)和会员弹窗内入口(pay-vip.js)同源。
|
||||
assert.match(footer, /\$currency = \(string\) \(\$catalog\['currency_name'\] \?\? ''\);/);
|
||||
assert.match(footer, /esc_html\(sprintf\(__\('%s兑换会员', 'aksrt'\), \$currency\)\)/);
|
||||
// 缺失时回退设置里的名称,再回退「积分」。
|
||||
assert.match(footer, /Settings::get_currency_name\(\)/);
|
||||
assert.doesNotMatch(footer, /esc_html__\('积分兑换会员', 'aksrt'\)/);
|
||||
// 弹窗内同一命名路径:标题 / 我的{货币名} / 不足提示都走 $currency。
|
||||
assert.match(modal, /\$currency = \(string\) \(\$catalog\['currency_name'\] \?\? __\('积分', 'aksrt'\)\);/);
|
||||
assert.doesNotMatch(modal, /esc_html__\('积分兑换会员', 'aksrt'\)/);
|
||||
});
|
||||
|
||||
test('兑换链路文案(弹窗/错误/订单备注/流水)统一跟随货币名', () => {
|
||||
// 不足提示与后端 WP_Error 同文案,均以货币名为前缀。
|
||||
assert.match(modal, /esc_html\(sprintf\(__\('%s不足,暂时无法兑换', 'aksrt'\), \$currency\)\)/);
|
||||
assert.match(catalogSource, /aksrt_points_insufficient', sprintf\(__\('%s不足,暂时无法兑换', 'aksrt'\), \$currency_name\)/);
|
||||
assert.match(catalogSource, /aksrt_points_exchange_disabled', sprintf\(__\('%s兑换会员未启用', 'aksrt'\), \$currency_name\)/);
|
||||
assert.match(catalogSource, /aksrt_invalid_price', sprintf\(__\('兑换%s无效', 'aksrt'\), \$currency_name\)/);
|
||||
// exchange_with_points 自行解析 currency_name(get_catalog 内的局部变量不在该作用域)。
|
||||
assert.match(catalogSource, /\$currency_name = \(string\) \(\$catalog\['currency_name'\] \?\? ''\);/);
|
||||
// 订单备注与流水说明。
|
||||
assert.match(catalogSource, /sprintf\(__\('%1\$s兑换会员 %2\$d %1\$s。', 'aksrt'\), \$currency_name, \$cost\)/);
|
||||
assert.match(catalogSource, /sprintf\(__\('%s兑换会员 · %s', 'aksrt'\), \$currency_name, \(string\) \(\$match\['title'\] \?\? ''\)\)/);
|
||||
// 流水原因标签(用户中心明细里 desc 为空时的回退)。
|
||||
assert.match(ledger, /'vip_points_exchange' => sprintf\(__\('%s兑换会员', 'aksrt'\), self::points_name\(\)\)/);
|
||||
assert.doesNotMatch(ledger, /'vip_points_exchange' => __\('积分兑换会员', 'aksrt'\)/);
|
||||
});
|
||||
|
||||
test('页面入口样式落在 credits.css 且与档位卡同一表面体系', () => {
|
||||
assert.match(creditsCss, /\.uc-vip-points-entry \{/);
|
||||
assert.match(creditsCss, /\.uc-vip-points-entry\s*\{[^}]*var\(--main-bg-color, var\(--surface-1, #fff\)\)/s);
|
||||
|
||||
Reference in new issue
Block a user