From 962ab5c2907c4b2523777c3d6df3bc3b0ce02c13 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 6 Oct 2026 12:55:04 +0800 Subject: [PATCH] fix(frontend): match the reference post format options in the page set modal MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 参考主题把文章格式固定为标准/图像/画廊/视频四项,动态读取 get_theme_support('post-formats') 会让本站多出 aside/quote/link 等注册格式,选项集偏离参考主题,故改为固定列表。 --- src/Frontend/PageAdminSet.php | 20 +++++++++----- .../frontend-page-edit-harness.php | 26 ++++++++++++++++++- .../system-tools-contract.test.cjs | 5 ++++ 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/Frontend/PageAdminSet.php b/src/Frontend/PageAdminSet.php index 34f99ea1..b00117c7 100644 --- a/src/Frontend/PageAdminSet.php +++ b/src/Frontend/PageAdminSet.php @@ -412,13 +412,21 @@ class PageAdminSet { return $this->render_inputs($inputs); } - /** AKSRT 注册的文章格式(standard + add_theme_support 注册项)。 */ + /** + * 文章格式选项(1:1 对齐参考主题的固定列表)。 + * + * 参考主题把标准/图像/画廊/视频四个格式写死在弹窗里,不读 + * get_theme_support('post-formats')。本站注册的格式比参考主题多 + * (aside/quote/link 等),动态枚举会让选项集偏离参考主题, + * 因此同样固定这四项。 + */ private function post_format_options(): array { - $options = ['standard' => __('标准', 'aksrt')]; - foreach (get_theme_support('post-formats') ? (array) get_theme_support('post-formats')[0] : [] as $format) { - $options[$format] = esc_html(get_post_format_string($format)); - } - return $options; + return [ + 'standard' => __('标准', 'aksrt'), + 'image' => __('图像', 'aksrt'), + 'gallery' => __('画廊', 'aksrt'), + 'video' => __('视频', 'aksrt'), + ]; } /** 分类术语的弹窗字段(对齐参考主题 tax 分支)。 */ diff --git a/tests/widget-system/frontend-page-edit-harness.php b/tests/widget-system/frontend-page-edit-harness.php index f7790e2c..ae8b4ee2 100644 --- a/tests/widget-system/frontend-page-edit-harness.php +++ b/tests/widget-system/frontend-page-edit-harness.php @@ -21,6 +21,7 @@ namespace { $GLOBALS['aksrt_set_page_templates'] = []; $GLOBALS['aksrt_set_can'] = true; $GLOBALS['aksrt_set_scripts'] = []; + $GLOBALS['aksrt_set_post_format'] = ''; function __(string $text, string $domain = ''): string { return $text; } function esc_html__($t, $d = ''): string { return (string) $t; } @@ -48,7 +49,7 @@ namespace { function update_post_meta($id, $key, $value): void { $GLOBALS['aksrt_set_meta'][$key] = $value; } function get_the_title($id): string { return '示例标题'; } function get_the_permalink($id): string { return 'https://example.test/post'; } - function get_post_format($id) { return ''; } + function get_post_format($id) { return $GLOBALS['aksrt_set_post_format'] ?? ''; } function set_post_format($id, $format): void { $GLOBALS['aksrt_set_meta']['__format'] = $format; } function post_type_supports($type, $feature): bool { return $feature === 'post-formats'; } function get_theme_support($feature) { return $feature === 'post-formats' ? [['image', 'video']] : false; } @@ -290,6 +291,29 @@ namespace { } set_check(strpos($modal_icons, 'class="fa fa-') === false, 'modal must not use bare FA4 icon classes'); + // 文章格式:参考主题固定 standard/图像/画廊/视频 四项,不读主题注册集。 + // 无格式时 WP 的 selected() 不选中任何项,浏览器默认落到第一项 standard。 + $format_block = substr($modal_icons, (int) strpos($modal_icons, 'name="post_format"')); + $format_block = substr($format_block, 0, (int) strpos($format_block, '')); + set_check($format_block !== '', 'post inputs must include the post_format select'); + $option_order = []; + preg_match_all('/]*value="([^"]*)"/', $format_block, $m); + $option_order = $m[1]; + set_check($option_order === ['standard', 'image', 'gallery', 'video'], 'post format options must mirror the reference order'); + set_check(strpos($format_block, '>图像<') !== false, 'post format must use the reference 图像 label'); + set_check(strpos($format_block, '>画廊<') !== false, 'post format must use the reference 画廊 label'); + set_check(strpos($format_block, '>视频<') !== false, 'post format must use the reference 视频 label'); + foreach (['aside', 'quote', 'link'] as $extra) { + set_check(strpos($modal_icons, "value=\"{$extra}\"") === false, "post format options must not leak the theme-only format {$extra}"); + } + // 已设置格式时必须回显选中项。 + $GLOBALS['aksrt_set_meta']['__format'] = ''; + $GLOBALS['aksrt_set_post_format'] = 'image'; + ob_start(); + (new PageAdminSet())->render_modal(); + $modal_image = (string) ob_get_clean(); + set_check(strpos($modal_image, '