fix(report): give preset reasons a stable non-empty slug for CJK labels

ban_settings() derived each reason slug via sanitize_key(label), which
strips CJK text to an empty string; the frontend filter then discarded
every reason (including the schema defaults) and fell back to the
hard-coded list. Derive the slug from an ASCII key when possible and
from a stable md5 short hash of the label otherwise, and relax the
frontend filter to the label only. Adds a WP-stub harness that verifies
the defaults path end to end (tests/dev/verify-ban-settings-defaults.php).
This commit is contained in:
2026-09-20 14:48:28 +08:00
parent 67547fdf23
commit 3d4179a756
4 changed files with 88 additions and 4 deletions
+10 -3
View File
@@ -216,14 +216,21 @@ class UserRuntime {
public static function ban_settings(): array {
$settings = self::read_report_section();
// 预置原因:group 数组 [{t:...}](对齐参考主题)或旧版每行一条文本
// 预置原因:group 数组 [{t:...}](对齐参考主题)或旧版每行一条文本。
// slug 不能用 sanitize_key(标签) 派生:中文标签会被清洗成空串,
// 导致前端按 slug 非空过滤后整列表被丢弃。改用标签内容的稳定短哈希
// (跨请求一致、可通过服务端 sanitize_key、ASCII 标签同样稳定)。
$reasons_raw = $settings['ban_preset_reason'] ?? null;
$reasons = [];
$reason_slug = static function (string $text): string {
$key = sanitize_key($text);
return $key !== '' ? $key : substr(md5($text), 0, 12);
};
if (is_array($reasons_raw)) {
foreach ($reasons_raw as $row) {
$text = trim((string) ($row['t'] ?? ''));
if ($text !== '') {
$reasons[] = ['slug' => sanitize_key($text), 'name' => sanitize_text_field($text)];
$reasons[] = ['slug' => $reason_slug($text), 'name' => sanitize_text_field($text)];
}
}
} else {
@@ -234,7 +241,7 @@ class UserRuntime {
}
$parts = explode('|', $line, 2);
$reasons[] = [
'slug' => sanitize_key($parts[0]),
'slug' => $reason_slug((string) ($parts[1] ?? $parts[0])),
'name' => sanitize_text_field($parts[1] ?? $parts[0]),
];
}