fix(sidebar): collapse empty sidebar column on every scene

Sidebar visibility had two disconnected decisions: the body site-layout-*
class (via aksrt_is_show_sidebar_filter) only checked widget activity for
singles, while the homepage aside renderer skipped the column and
sidebar.php rendered an empty one. With the toggle on and no widgets, the
homepage kept a reserved gutter and squeezed the main content aside.

- collapse every scene to no-sidebar when the sidebar widget stack is empty
- resolve home_sidebar before the page branch so a static front page is
  checked against the zones the homepage renders
- guard the homepage aside with aksrt_is_show_sidebar() alone and drop the
  duplicate aksrt_homepage_has_sidebar gate
- early-return sidebar.php instead of rendering an empty column
- drop dead code: archive helper $preferred param, wp_is_mobile fallback,
  container--no-sidebar class, unreachable front-page fallback
- update post-visual-contract to pin the single visibility decision

Verification: php -l on all changed files; node --test
tests/widget-system/*.test.cjs 242/243 (the toast-contract failure is
pre-existing and unrelated).
This commit is contained in:
2026-09-05 15:40:11 +08:00
parent 2bf9b00022
commit 1839150ecb
10 changed files with 64 additions and 70 deletions
+1 -1
View File
@@ -46,7 +46,7 @@ $archive_settings = class_exists('\AKSRT\Core\TermMeta')
]; ];
$has_sidebar = function_exists('aksrt_archive_has_sidebar') $has_sidebar = function_exists('aksrt_archive_has_sidebar')
? aksrt_archive_has_sidebar('cat_sidebar', !empty($archive_settings['show_sidebar'])) ? aksrt_archive_has_sidebar(!empty($archive_settings['show_sidebar']))
: (!empty($archive_settings['show_sidebar']) && is_active_sidebar('cat_sidebar')); : (!empty($archive_settings['show_sidebar']) && is_active_sidebar('cat_sidebar'));
$description = !empty($archive_settings['show_desc']) ? wp_kses_post((string) $archive_settings['description']) : ''; $description = !empty($archive_settings['show_desc']) ? wp_kses_post((string) $archive_settings['description']) : '';
$header_style = (string) $archive_settings['header_style']; $header_style = (string) $archive_settings['header_style'];
+1 -16
View File
@@ -11,21 +11,6 @@
get_header(); get_header();
if (function_exists('aksrt_homepage_render_layout')) { aksrt_homepage_render_layout();
aksrt_homepage_render_layout();
} else {
?>
<main id="main-content" class="site-main content-shell" tabindex="-1">
<div class="content-layout content-layout--no-sidebar">
<div class="content-layout__primary">
<section class="widget widget-card box-body">
<h1 class="entry-title"><?php esc_html_e('首页骨架未加载', 'aksrt'); ?></h1>
<p><?php esc_html_e('固定首页骨架当前不可用,请检查主题加载链路。', 'aksrt'); ?></p>
</section>
</div>
</div>
</main>
<?php
}
get_footer(); get_footer();
+3 -9
View File
@@ -122,19 +122,13 @@ if (!function_exists('aksrt_archive_has_sidebar')) :
/** /**
* Resolve whether a stream page should show an aside. * Resolve whether a stream page should show an aside.
* *
* @param string $preferred Preferred sidebar id (compat only). * @param bool $enabled Page-level sidebar toggle.
* @param bool $enabled Page-level sidebar toggle.
*/ */
function aksrt_archive_has_sidebar(string $preferred = '', bool $enabled = true): bool { function aksrt_archive_has_sidebar(bool $enabled = true): bool {
// $preferred kept for call-site compat; visibility is settings/meta driven (legacy).
unset($preferred);
if (!$enabled) { if (!$enabled) {
return false; return false;
} }
if (function_exists('aksrt_is_show_sidebar')) { return function_exists('aksrt_is_show_sidebar') ? aksrt_is_show_sidebar() : false;
return aksrt_is_show_sidebar();
}
return !wp_is_mobile();
} }
endif; endif;
+4 -24
View File
@@ -139,35 +139,15 @@ if (!function_exists('aksrt_homepage_render_widget_zone')) {
} }
} }
if (!function_exists('aksrt_homepage_has_sidebar')) {
/**
* Whether the fixed homepage should render an aside column.
*/
function aksrt_homepage_has_sidebar(): bool {
$has_widgets = function_exists('aksrt_has_current_sidebar_widgets')
? aksrt_has_current_sidebar_widgets()
: aksrt_homepage_has_active_sidebars([
'all_sidebar_top',
'home_sidebar',
'all_sidebar_bottom',
]);
if (!$has_widgets) {
return false;
}
if (function_exists('aksrt_is_show_sidebar')) {
return aksrt_is_show_sidebar();
}
return true;
}
}
if (!function_exists('aksrt_homepage_render_sidebar')) { if (!function_exists('aksrt_homepage_render_sidebar')) {
/** /**
* Render the homepage aside column. * Render the homepage aside column.
*
* Visibility is aksrt_is_show_sidebar() alone; its filter already
* collapses the layout when the sidebar widget stack is empty.
*/ */
function aksrt_homepage_render_sidebar(): void { function aksrt_homepage_render_sidebar(): void {
if (!aksrt_homepage_has_sidebar()) { if (!aksrt_is_show_sidebar()) {
return; return;
} }
+11 -5
View File
@@ -41,14 +41,18 @@ function aksrt_resolve_sidebar_id(): string {
return ''; return '';
} }
if (is_page()) { // The fixed homepage renders home_sidebar for both is_home() and
return 'page_sidebar_' . get_queried_object_id(); // is_front_page(); a static front page also matches the page branch below,
} // so this branch must come first to keep visibility checks aligned with
// the renderer.
if (is_home() || is_front_page()) { if (is_home() || is_front_page()) {
return 'home_sidebar'; return 'home_sidebar';
} }
if (is_page()) {
return 'page_sidebar_' . get_queried_object_id();
}
return 'home_sidebar'; return 'home_sidebar';
} }
endif; endif;
@@ -147,7 +151,9 @@ function aksrt_is_show_sidebar_filter(): bool {
return false; return false;
} }
if (is_single() && !aksrt_has_current_sidebar_widgets()) { // An empty widget stack would leave the reserved column as a bare gutter,
// so every scene (not only singles) collapses to no-sidebar.
if (!aksrt_has_current_sidebar_widgets()) {
return false; return false;
} }
+2 -2
View File
@@ -21,8 +21,8 @@ get_header();
<?php <?php
// legacy dual-column: sidebar is a sibling of .content-wrap under main.container. // legacy dual-column: sidebar is a sibling of .content-wrap under main.container.
// Settings and page metadata own visibility; widget activity never changes // aksrt_is_show_sidebar() is the single visibility decision (settings, post/term
// the body layout after render. // meta, and widget activity); the body site-layout-* class carries the geometry.
$has_sidebar = function_exists('aksrt_is_show_sidebar') ? aksrt_is_show_sidebar() : true; $has_sidebar = function_exists('aksrt_is_show_sidebar') ? aksrt_is_show_sidebar() : true;
?> ?>
<main id="main-content" class="site-main container" tabindex="-1"> <main id="main-content" class="site-main container" tabindex="-1">
+10 -9
View File
@@ -1,19 +1,22 @@
<?php <?php
/** /**
* Sidebar template for generic pages. * Sidebar template for generic pages (archive / page / search streams).
* *
* Single/home own their asides. This template only serves archive/page contexts. * Single/home own their asides. Renders nothing when the sidebar is disabled
* Callers only include this when aksrt_is_show_sidebar() is true. * or its widget stack is empty: aksrt_is_show_sidebar() is the single
* Keep the column even if widgets are empty (legacy parity). * visibility decision and the body site-layout-* class carries the geometry,
* so an empty column is never rendered just to hold a reserved gutter.
* *
* @package AKSRT * @package AKSRT
*/ */
defined('ABSPATH') || exit; defined('ABSPATH') || exit;
$sidebar_id = function_exists('aksrt_resolve_sidebar_id') if (!function_exists('aksrt_is_show_sidebar') || !aksrt_is_show_sidebar()) {
? aksrt_resolve_sidebar_id() return;
: 'home_sidebar'; }
$sidebar_id = aksrt_resolve_sidebar_id();
?> ?>
<div class="sidebar" role="complementary" aria-label="<?php esc_attr_e('侧栏', 'aksrt'); ?>"> <div class="sidebar" role="complementary" aria-label="<?php esc_attr_e('侧栏', 'aksrt'); ?>">
@@ -23,8 +26,6 @@ $sidebar_id = function_exists('aksrt_resolve_sidebar_id')
<?php if (is_active_sidebar($sidebar_id)) : ?> <?php if (is_active_sidebar($sidebar_id)) : ?>
<?php dynamic_sidebar($sidebar_id); ?> <?php dynamic_sidebar($sidebar_id); ?>
<?php elseif ((is_home() || is_front_page()) && is_active_sidebar('home_sidebar')) : ?>
<?php dynamic_sidebar('home_sidebar'); ?>
<?php endif; ?> <?php endif; ?>
<?php if (is_active_sidebar('all_sidebar_bottom')) : ?> <?php if (is_active_sidebar('all_sidebar_bottom')) : ?>
+1 -1
View File
@@ -42,7 +42,7 @@ while (have_posts()) :
</div> </div>
<?php endif; ?> <?php endif; ?>
<main role="main" class="container<?php echo $show_aside ? '' : ' container--no-sidebar'; ?>"> <main role="main" class="container">
<div class="content-wrap"> <div class="content-wrap">
<div class="content-layout"> <div class="content-layout">
<?php if (is_active_sidebar('single_top_content')) : ?> <?php if (is_active_sidebar('single_top_content')) : ?>
+1 -1
View File
@@ -27,7 +27,7 @@ $archive_settings = class_exists('\AKSRT\Core\TermMeta')
]; ];
$has_sidebar = function_exists('aksrt_archive_has_sidebar') $has_sidebar = function_exists('aksrt_archive_has_sidebar')
? aksrt_archive_has_sidebar('tag_sidebar', !empty($archive_settings['show_sidebar'])) ? aksrt_archive_has_sidebar(!empty($archive_settings['show_sidebar']))
: (!empty($archive_settings['show_sidebar']) && is_active_sidebar('tag_sidebar')); : (!empty($archive_settings['show_sidebar']) && is_active_sidebar('tag_sidebar'));
$description = !empty($archive_settings['show_desc']) ? wp_kses_post((string) $archive_settings['description']) : ''; $description = !empty($archive_settings['show_desc']) ? wp_kses_post((string) $archive_settings['description']) : '';
$header_style = (string) $archive_settings['header_style']; $header_style = (string) $archive_settings['header_style'];
@@ -356,14 +356,42 @@ test('the pinned visual baseline records all source checksums', () => {
} }
}); });
test('single posts reserve a sidebar column only when its widget stack has content', () => { test('every scene reserves a sidebar column only when its widget stack has content', () => {
const layout = read('inc/helpers/template-tags/site-layout.php'); const layout = read('inc/helpers/template-tags/site-layout.php');
const single = read('inc/helpers/aksrt-single.php'); const single = read('inc/helpers/aksrt-single.php');
const aside = read('inc/helpers/aksrt-single-aside.php'); const aside = read('inc/helpers/aksrt-single-aside.php');
const homepage = read('inc/helpers/homepage/content.php');
const sidebarTemplate = read('sidebar.php');
const archiveHelpers = read('inc/helpers/aksrt-archive.php');
// Single visibility decision: an empty widget stack collapses every scene
// (home/category/tag/search/page/archive) to no-sidebar, not just singles.
assert.match(layout, /if \(!aksrt_has_current_sidebar_widgets\(\)\) \{\s*return false;/);
assert.doesNotMatch(layout, /is_single\(\) && !aksrt_has_current_sidebar_widgets/);
// The resolver must answer home_sidebar before the is_page() branch so a
// static front page is checked against the zones the homepage renders.
const resolver = layout.match(/function aksrt_resolve_sidebar_id\(\): string \{[\s\S]*?\n\}/);
assert.ok(resolver, 'aksrt_resolve_sidebar_id should exist');
assert.ok(
resolver[0].indexOf('is_home() || is_front_page()') < resolver[0].indexOf('is_page()'),
'home/front_page branch must precede the is_page() branch'
);
assert.match(layout, /if \(is_single\(\) && !aksrt_has_current_sidebar_widgets\(\)\) \{\s*return false;/);
assert.match(single, /\$show_aside = get_post_type\(\$post_id\) === 'post'[\s\S]*?&& aksrt_is_show_sidebar\(\);/); assert.match(single, /\$show_aside = get_post_type\(\$post_id\) === 'post'[\s\S]*?&& aksrt_is_show_sidebar\(\);/);
assert.match(aside, /get_post_type\(\$post_id\) !== 'post' \|\| empty\(\$ctx\['show_aside'\]\)/); assert.match(aside, /get_post_type\(\$post_id\) !== 'post' \|\| empty\(\$ctx\['show_aside'\]\)/);
// Homepage aside renders under the same single decision; no dual gate.
assert.match(homepage, /function aksrt_homepage_render_sidebar\(\): void \{\s*if \(!aksrt_is_show_sidebar\(\)\) \{\s*return;/);
assert.doesNotMatch(homepage, /aksrt_homepage_has_sidebar/);
// Generic sidebar template early-returns instead of rendering an empty column.
assert.match(sidebarTemplate, /if \(!function_exists\('aksrt_is_show_sidebar'\) \|\| !aksrt_is_show_sidebar\(\)\) \{\s*return;/);
assert.doesNotMatch(sidebarTemplate, /legacy parity/);
// Archive helper takes only the page-level toggle; no dead params.
assert.match(archiveHelpers, /function aksrt_archive_has_sidebar\(bool \$enabled = true\): bool/);
assert.doesNotMatch(archiveHelpers, /preferred|wp_is_mobile/);
}); });
test('article visual matrix pins every breakpoint, state and strict threshold', () => { test('article visual matrix pins every breakpoint, state and strict threshold', () => {