Files
kate522andClaude Fable 5 38bbd917a9 fix(skeleton): align the homepage tab and sort loaders with the reference 1:1
首页「更多栏目」tab 与「排序方式」切换的骨架占位与参考主题 zibll 有实质差异,
根因不是几何宽度,而是三处结构/契约错位:

1. 骨架块自身结构与参考主题不同。card 分支用了 `<posts>` 元素 + `.post-graphic`
   包裹层(列表变体,带 margin-right:20px),而参考主题 card 骨架是
   `<div class="posts-item card">` 且 `.item-thumbnail` 是直接子元素。
   → card 骨架右侧多出一条真实卡片没有的 20px 沟槽。

2. 骨架块被错误地打上 `ajax-item`。参考主题的占位块没有该类。它会让
   `find('.ajax-item')` / ajax-list 的 `inject()` 把骨架当成真实条目。

3. 排序切换的骨架被预渲染节点顶替。aksrt_render_pagination() 在 .ajax-pag 内
   渲染了一个写死 2 条、写死 list 形状的 .post_ajax_loader;runtime post_ajax()
   会先在容器内 find('.post_ajax_loader'),找到就复用并丢弃调用方按视口动态
   拼出的骨架 → 切排序永远是 2 条 list 骨架,card 布局下也是、与视口宽度无关。
   参考主题 zib_paging() 根本没有 loader 兄弟节点。

改动:
- inc/helpers/posts-renderer/markup.php:aksrt_render_placeholder() 的
  card/list 分支改为与 zib_get_post_placeholder() 逐字等价的结构;去掉
  ajax-item 类、card 分支的 .post-graphic 包裹层,元素改用 <div>。
- inc/helpers/template-tags/pagination-permissions.php:删除预渲染 loader,
  与参考主题 zib_paging() 一致 —— 只渲染翻页控件。
- assets/js/core/pagination.js:找不到骨架节点时构造与 runtime/main.js 同款
  的文章骨架(卡片 8/6/4 条、列表 4/3 条,按视口与参考主题同阈值),
  不再退化为「转圈 + 文案」这个第二套加载语言;仅评论等非文章面保留该兜底。
- assets/js/core/pagination.js:为排序菜单(.ajax-option)与 tab 触发器
  (.post_ajax_trigger)增加避让 —— 它们归 legacy runtime 管线,
  否则一次点击发两个请求(与 ajax-list.js 的既有避让对称)。
- assets/js/runtime/main.js:post_ajax 的显隐由 fadeIn()/hide() 改为
  .is-loading 类切换。前者写 inline display,优先级高于类规则,一旦残留
  骨架永远打不开,正是骨架契约明令禁止的失败模式。

归档/分类筛选面原本另有一套独立加载语言(spinner 遮罩 + opacity 淡出),
本次一并统一到同一条 .post_ajax_loader + .is-loading 契约:
- inc/helpers/aksrt-archive.php:删除 show_loading 标记位与遮罩 DOM,
  改为在分页之后渲染文章骨架。
- assets/js/core/archive.js:showLoading/hideLoading 只切 .is-loading;
  fetchPosts() 清空内容时保留骨架与分页节点(与参考主题 success 只 hide
  不删的生命周期一致),原先会连带删掉骨架。
- assets/css/frontend/layout.css:删除 .archive__ajax-wrap.is-loading
  的 opacity 淡出与 spinner 遮罩规则。
- category.php / tag.php:删除已失效的 show_loading 实参。

验证:php -l 全部通过;php tools/audit-admin-contracts.php errors=0;
契约测试 post-visual(22) / asset-ledger(16) / message-center(8) /
lazyload(7) / frontend-lifecycle(13) / zibll-modern-widgets(10) /
page-display(18) / a11y 全绿;已重跑 build:public 刷新产物。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-10-04 03:04:02 +08:00

307 lines
9.8 KiB
PHP

<?php
/**
* Shared archive / category / tag / search stream helpers.
*
* Keeps list pages on one content-stream contract instead of four hand-rolled shells.
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
if (!function_exists('aksrt_resolve_post_list_render_config')) :
/**
* Build the legacy-shaped arguments used by every list surface.
*
* @param array<string,mixed> $args {
* @type string $context home|category|tag|author|search|archive
* @type int $term_id Optional term id for list_card_cat
* @type array<string,mixed> $overrides Optional renderer overrides (mode, style, …)
* }
* @return array<string,mixed>
*/
function aksrt_resolve_post_list_render_config(array $args = []): array {
$has_top_level_term_id = array_key_exists('term_id', $args);
$args = wp_parse_args($args, [
'context' => '',
'term_id' => 0,
'overrides' => [],
]);
$overrides = is_array($args['overrides']) ? $args['overrides'] : [];
$has_override_term_id = array_key_exists('term_id', $overrides);
$context = sanitize_key((string) ($args['context'] ?: ($overrides['context'] ?? '')));
if ($context === '') {
if (function_exists('is_front_page') && (is_front_page() || is_home())) {
$context = 'home';
} elseif (is_category()) {
$context = 'category';
} elseif (is_tag()) {
$context = 'tag';
} elseif (is_author()) {
$context = 'author';
} elseif (is_search()) {
$context = 'search';
} else {
$context = 'archive';
}
}
$term_id = $has_override_term_id ? absint($overrides['term_id']) : absint($args['term_id']);
if ($context === 'category' && !$has_top_level_term_id && !$has_override_term_id) {
$term_id = absint(get_queried_object_id());
}
$config = [
'context' => $context,
'term_id' => $term_id,
'is_home' => $context === 'home',
'is_category' => $context === 'category',
'is_tag' => $context === 'tag',
'is_author' => $context === 'author',
'is_search' => $context === 'search',
];
foreach (['type', 'term_id', 'no_author', 'no_margin', 'is_mult_thumb', 'is_no_thumb', 'is_card', 'highlight_query', 'wrapper_class', 'skip_wrapper'] as $key) {
if (array_key_exists($key, $overrides)) {
$config[$key] = $key === 'term_id' ? absint($overrides[$key]) : $overrides[$key];
}
}
return aksrt_renderer_config($config);
}
endif;
if (!function_exists('aksrt_archive_get_render_config')) :
/**
* Build render config for archive-like pages.
*
* @param array<string,mixed> $overrides Context/mode overrides.
* @return array<string,mixed>
*/
function aksrt_archive_get_render_config(array $overrides = []): array {
$context = sanitize_key((string) ($overrides['context'] ?? ''));
return aksrt_resolve_post_list_render_config([
'context' => $context,
'overrides' => $overrides,
]);
}
endif;
if (!function_exists('aksrt_archive_get_sort_state')) :
/**
* Build the canonical legacy orderby state for term archives.
*
* @param array<string,mixed> $option Page-display orderby fieldset.
* @return array{current:string,links:array<string,string>}
*/
function aksrt_archive_get_sort_state(array $option = []): array {
$all = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::homepage_allowed_sorts()
: ['modified', 'date', 'views', 'like', 'comment_count', 'favorite', 'rand'];
$lists = is_array($option['lists'] ?? null) ? $option['lists'] : [];
$lists = array_values(array_intersect(array_map('sanitize_key', $lists), $all));
if (!empty($option['dropdown'])) {
$lists = array_values(array_unique(array_merge($lists, $all)));
}
$current = isset($_GET['orderby']) ? sanitize_key(wp_unslash((string) $_GET['orderby'])) : 'date';
if (!in_array($current, $all, true)) {
$current = 'date';
}
$base_url = remove_query_arg(['orderby', 'order', 'paged']);
$links = [];
foreach ($lists as $orderby) {
$links[$orderby] = add_query_arg('orderby', $orderby, $base_url);
}
return [
'current' => $current,
'links' => $links,
];
}
endif;
if (!function_exists('aksrt_archive_has_sidebar')) :
/**
* Resolve whether a stream page should show an aside.
*
* @param bool $enabled Page-level sidebar toggle.
*/
function aksrt_archive_has_sidebar(bool $enabled = true): bool {
if (!$enabled) {
return false;
}
return function_exists('aksrt_is_show_sidebar') ? aksrt_is_show_sidebar() : false;
}
endif;
if (!function_exists('aksrt_archive_render_widget_zone')) :
/**
* Render stacked sidebars when active.
*
* @param string[] $sidebar_ids Sidebar ids.
* @param string $wrapper_class Wrapper class.
*/
function aksrt_archive_render_widget_zone(array $sidebar_ids, string $wrapper_class = ''): void {
$active = [];
foreach ($sidebar_ids as $sidebar_id) {
if (is_active_sidebar($sidebar_id)) {
$active[] = $sidebar_id;
}
}
if (empty($active)) {
return;
}
if ($wrapper_class !== '') {
echo '<div class="' . esc_attr($wrapper_class) . '">';
}
foreach ($active as $sidebar_id) {
dynamic_sidebar($sidebar_id);
}
if ($wrapper_class !== '') {
echo '</div>';
}
}
endif;
if (!function_exists('aksrt_archive_render_stream')) :
/**
* Shared list + pagination + empty state for archive surfaces.
*
* @param WP_Query|null $query Query or null for main loop.
* @param array<string,mixed> $render_config Renderer config.
* @param array<string,mixed> $args Stream args.
*/
function aksrt_archive_render_stream(?WP_Query $query, array $render_config, array $args = []): void {
$args = wp_parse_args($args, [
'page_kind' => 'archive',
'query_context' => 'archive',
'empty_text' => __('暂无文章', 'aksrt'),
'stream_class' => '',
'ajax_enabled' => false,
'ajax_attrs' => '',
]);
$classes = [
'content-stream',
'content-stream--' . sanitize_html_class((string) $args['page_kind']),
];
if (!empty($args['stream_class'])) {
foreach (preg_split('/\s+/', trim((string) $args['stream_class'])) ?: [] as $class) {
$class = sanitize_html_class($class);
if ($class !== '') {
$classes[] = $class;
}
}
}
if (!empty($args['ajax_enabled'])) {
$classes[] = 'is-ajax-enabled';
}
$mode = (string) ($render_config['mode'] ?? 'list');
$has_posts = $query instanceof WP_Query ? $query->have_posts() : have_posts();
?>
<div
class="<?php echo esc_attr(implode(' ', array_unique($classes))); ?>"
data-page-kind="<?php echo esc_attr((string) $args['page_kind']); ?>"
data-render-mode="<?php echo esc_attr($mode); ?>"
data-list-type="<?php echo esc_attr((string) ($render_config['list_type'] ?? 'thumb')); ?>"
<?php if (!empty($args['ajax_enabled'])) : ?>
data-ajax-wrap
data-query-context="<?php echo esc_attr((string) $args['query_context']); ?>"
<?php endif; ?>
<?php echo $args['ajax_attrs']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
>
<?php if ($has_posts) : ?>
<?php
// legacy 契约:分页渲染在 .ajaxpager 内部(zib_paging 位置),
// 首页更多栏目 tab 的 runtime post_ajax 依赖它提取下一页。
ob_start();
aksrt_render_pagination($query, [
'query_context' => (string) $args['query_context'],
'render_config' => $render_config,
]);
$pagination_html = (string) ob_get_clean();
// 归档面与首页共用同一套骨架契约:.post_ajax_loader + .is-loading。
// 挂在分页之后(列表末尾),archive.js 靠它在 wrap 内定位加载态。
$loader_html = '<div class="post_ajax_loader" role="status" aria-label="'
. esc_attr__('正在加载', 'aksrt') . '">'
. aksrt_render_placeholder(
(string) $render_config['mode'] === 'card' ? 'card' : 'list',
4
)
. '</div>';
aksrt_render_post_list(array_merge($render_config, [
'wrapper_class' => 'content-stream__list',
'append_inner_html' => $pagination_html . $loader_html,
]), $query);
?>
<?php else : ?>
<div class="no-results content-stream__empty">
<p><?php echo esc_html((string) $args['empty_text']); ?></p>
</div>
<?php endif; ?>
</div>
<?php
}
endif;
if (!function_exists('aksrt_archive_open_shell')) :
/**
* Open a shared archive shell.
*
* @param array<string,mixed> $args Shell args.
*/
function aksrt_archive_open_shell(array $args = []): void {
$args = wp_parse_args($args, [
'main_class' => 'site-main container',
'has_sidebar' => false,
'top_fluid' => [],
'page_kind' => 'archive',
]);
// Force container; strip content-shell* so float dual-column is not padded/max-width nested.
$main_class = trim((string) $args['main_class']);
if ($main_class === '') {
$main_class = 'site-main container';
} elseif (strpos($main_class, 'container') === false) {
$main_class .= ' container';
}
$main_class = trim((string) preg_replace('/\bcontent-shell(?:--[a-z0-9-]+)?\b/i', '', $main_class));
if (!empty($args['top_fluid']) && is_array($args['top_fluid'])) {
aksrt_archive_render_widget_zone($args['top_fluid'], 'fluid-widget-wrap');
}
?>
<main id="main-content" class="<?php echo esc_attr($main_class); ?>" tabindex="-1">
<div class="content-wrap">
<div class="content-layout">
<?php
}
endif;
if (!function_exists('aksrt_archive_close_shell')) :
/**
* Close a shared archive shell.
* Sidebar is a sibling of .content-wrap (legacy float dual-column).
*
* @param array<string,mixed> $args Shell args.
*/
function aksrt_archive_close_shell(array $args = []): void {
$args = wp_parse_args($args, [
'has_sidebar' => false,
'bottom_fluid' => [],
]);
?>
</div>
</div>
<?php if (!empty($args['has_sidebar'])) : ?>
<?php get_sidebar(); ?>
<?php endif; ?>
</main>
<?php
if (!empty($args['bottom_fluid']) && is_array($args['bottom_fluid'])) {
aksrt_archive_render_widget_zone($args['bottom_fluid'], 'fluid-widget-wrap');
}
}
endif;