fix(list): wire Zibll list/card display chain end-to-end

Resolve post list mode from global list_type + surface card overrides so
homepage/archives/AJAX honor admin list and card style settings.
This commit is contained in:
kate522 committed 2026-07-12 20:56:38 +08:00
1 parent b2815614d1
commit d73a7af25e
17 files changed
+444 -151

No files matched your search

+205 -27
View File
@@ -13,7 +13,7 @@ if (!function_exists('aksrt_archive_get_list_style_settings')) :
/**
* Shared list style defaults for archive surfaces.
*
* @return array{render_mode:string,card_style:string,card_cat_ids:array<int,int>}
* @return array<string,mixed>
*/
function aksrt_archive_get_list_style_settings(): array {
$settings = class_exists('AKSRT_Settings')
@@ -21,13 +21,203 @@ if (!function_exists('aksrt_archive_get_list_style_settings')) :
: [];
return [
'render_mode' => (string) ($settings['render_mode'] ?? 'card'),
'card_style' => (string) ($settings['card_style'] ?? 'standard'),
'card_cat_ids' => is_array($settings['card_cat_ids'] ?? null) ? array_map('intval', $settings['card_cat_ids']) : [],
'list_type' => (string) ($settings['list_type'] ?? 'thumb'),
'render_mode' => (string) ($settings['render_mode'] ?? 'list'),
'list_type_token' => (string) ($settings['list_type_token'] ?? ($settings['list_type'] ?? 'thumb')),
'list_style' => (string) ($settings['list_style'] ?? ''),
'card_variant' => (string) ($settings['card_variant'] ?? ''),
// Back-compat: older callers expected card_style as list_type token.
'card_style' => (string) ($settings['list_type'] ?? ($settings['card_style'] ?? 'thumb')),
'card_cat_ids' => is_array($settings['card_cat_ids'] ?? null) ? array_map('intval', $settings['card_cat_ids']) : [],
'card_home' => !empty($settings['card_home']),
'card_tag' => !empty($settings['card_tag']),
'card_topics' => !empty($settings['card_topics']),
'card_author' => !empty($settings['card_author']),
'list_show_type' => (string) ($settings['list_show_type'] ?? 'separate'),
'list_img_position'=> (string) ($settings['list_img_position'] ?? 'left'),
'show_author' => !isset($settings['show_author']) || !empty($settings['show_author']),
'show_date' => !isset($settings['show_date']) || !empty($settings['show_date']),
'show_views' => !isset($settings['show_views']) || !empty($settings['show_views']),
'show_comments' => !empty($settings['show_comments']),
'show_likes' => !empty($settings['show_likes']),
'target_blank' => !empty($settings['target_blank']),
'heading_bold' => !empty($settings['heading_bold']),
'badge_show' => !isset($settings['badge_show']) || !empty($settings['badge_show']),
'excerpt_length' => (int) ($settings['excerpt_length'] ?? 120),
];
}
endif;
if (!function_exists('aksrt_resolve_post_list_render_config')) :
/**
* Zibll-aligned list/card resolver for all surfaces.
*
* Decision order mirrors zib_mian_posts_while():
* 1) explicit override mode/type
* 2) global list_type (post_card_style): only "card" is card grid; thumb/text/thumb_if_has are list
* 3) surface card switches (home/tag/topics/author/cat IDs)
*
* @param array<string,mixed> $args {
* @type string $context home|category|tag|topics|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 {
$args = wp_parse_args($args, [
'context' => '',
'term_id' => 0,
'overrides' => [],
]);
$overrides = is_array($args['overrides']) ? $args['overrides'] : [];
if (!empty($args['context']) && empty($overrides['context'])) {
// keep context on root for merge below
}
$context = sanitize_key((string) ($args['context'] !== '' ? $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_tax('topics')) {
$context = 'topics';
} elseif (is_author()) {
$context = 'author';
} elseif (is_search()) {
$context = 'search';
} else {
$context = 'archive';
}
}
$term_id = (int) ($args['term_id'] ?? 0);
if ($term_id <= 0) {
$term_id = (int) get_queried_object_id();
}
$list = aksrt_archive_get_list_style_settings();
$list_type = sanitize_key((string) ($list['list_type'] ?? 'thumb'));
if ($list_type === '' || $list_type === 'standard') {
$list_type = 'thumb';
}
// Explicit caller mode wins (term meta, AJAX view toggle, etc.).
$forced_mode = '';
if (isset($overrides['mode']) && is_string($overrides['mode']) && $overrides['mode'] !== '') {
$forced_mode = sanitize_key($overrides['mode']);
} elseif (isset($overrides['type']) && sanitize_key((string) $overrides['type']) === 'card') {
$forced_mode = 'card';
} elseif (!empty($overrides['is_card'])) {
$forced_mode = 'card';
}
$mode = $forced_mode !== ''
? $forced_mode
: (($list_type === 'card') ? 'card' : 'list');
// Surface card overrides (only when not already card from list_type / force).
if ($mode !== 'card' && $forced_mode === '') {
if ($context === 'home' && !empty($list['card_home'])) {
$mode = 'card';
} elseif ($context === 'tag' && !empty($list['card_tag'])) {
$mode = 'card';
} elseif ($context === 'topics' && !empty($list['card_topics'])) {
$mode = 'card';
} elseif ($context === 'author' && !empty($list['card_author'])) {
$mode = 'card';
} elseif ($term_id > 0 && !empty($list['card_cat_ids']) && in_array($term_id, array_map('intval', (array) $list['card_cat_ids']), true)) {
$mode = 'card';
}
}
if (!in_array($mode, ['list', 'card', 'mini', 'grid-cms'], true)) {
$mode = 'list';
}
// Visual variant: list → style2; card → style3 (Zibll fieldsets).
$style = '';
if ($mode === 'card') {
$style = sanitize_key((string) ($list['card_variant'] ?? ''));
if (!in_array($style, ['', 'style3'], true)) {
$style = '';
}
} else {
$style = sanitize_key((string) ($list['list_style'] ?? ''));
if (!in_array($style, ['', 'style2'], true)) {
$style = '';
}
}
if (isset($overrides['style']) && is_string($overrides['style'])) {
$style = sanitize_key($overrides['style']);
if (in_array($style, ['default', 'null'], true)) {
$style = '';
}
}
$excerpt_words = max(8, (int) round(((int) ($list['excerpt_length'] ?? 120)) / 4));
$config = [
'context' => $context,
'mode' => $mode,
// list_type token for text / thumb_if_has hooks (renderer card_style key).
'card_style' => $list_type,
'style' => $style === 'default' ? '' : $style,
'list_show_type' => (string) ($list['list_show_type'] ?? 'separate'),
'list_img_position' => (string) ($list['list_img_position'] ?? 'left'),
'thumb_if_has' => ($list_type === 'thumb_if_has'),
'show_thumb' => ($list_type !== 'text'),
'show_author' => !empty($list['show_author']),
'show_date' => !empty($list['show_date']),
'show_views' => !empty($list['show_views']),
'show_comments' => !empty($list['show_comments']),
'show_likes' => !empty($list['show_likes']),
'target_blank' => !empty($list['target_blank']),
'heading_bold' => !empty($list['heading_bold']),
'badge_show' => !empty($list['badge_show']),
'show_excerpt' => $mode !== 'card',
'excerpt_words' => $excerpt_words,
];
// Multi-thumb from global thumb settings + category allowlist.
if (class_exists('AKSRT_Settings')) {
$thumb_settings = AKSRT_Settings::get_post_thumb_settings();
if (!empty($thumb_settings['multi_enabled'])) {
$config['multi_thumb_enabled'] = true;
$config['multi_thumb_limit'] = (int) ($thumb_settings['multi_count'] ?? 4);
$config['multi_thumb_min'] = 3;
}
if (($context === 'category' || $context === 'topics') && !empty($thumb_settings['mult_thumb_cat_ids']) && $term_id > 0) {
if (in_array($term_id, array_map('intval', (array) $thumb_settings['mult_thumb_cat_ids']), true)) {
$config['multi_thumb_enabled'] = true;
$config['multi_thumb_limit'] = (int) ($thumb_settings['multi_count'] ?? 4);
$config['multi_thumb_min'] = 3;
}
}
}
// Merge remaining overrides last (except mode/style already handled carefully).
foreach ($overrides as $key => $value) {
if (in_array($key, ['mode', 'type', 'is_card', 'style', 'context'], true)) {
continue;
}
$config[$key] = $value;
}
// Re-apply forced mode after merge if present.
if ($forced_mode !== '' && in_array($forced_mode, ['list', 'card', 'mini', 'grid-cms'], true)) {
$config['mode'] = $forced_mode;
}
$config['context'] = $context;
$config['style'] = $style === 'default' ? '' : $style;
return $config;
}
endif;
if (!function_exists('aksrt_archive_get_render_config')) :
/**
* Build render config for archive-like pages.
@@ -36,32 +226,20 @@ if (!function_exists('aksrt_archive_get_render_config')) :
* @return array<string,mixed>
*/
function aksrt_archive_get_render_config(array $overrides = []): array {
$list = aksrt_archive_get_list_style_settings();
$config = array_merge([
'context' => 'archive',
'mode' => $list['render_mode'],
'card_style' => $list['card_style'],
], $overrides);
$context = sanitize_key((string) ($overrides['context'] ?? ''));
$term_id = isset($overrides['term_id']) ? (int) $overrides['term_id'] : 0;
if (is_category() && !empty($list['card_cat_ids'])) {
$current_cat_id = (int) get_queried_object_id();
if (in_array($current_cat_id, $list['card_cat_ids'], true)) {
$config['mode'] = 'card';
}
// Only treat explicit mode as override; empty string means follow global resolver.
$resolver_overrides = $overrides;
if (isset($resolver_overrides['mode']) && $resolver_overrides['mode'] === '') {
unset($resolver_overrides['mode']);
}
if (is_category() && class_exists('AKSRT_Settings')) {
$thumb_settings = AKSRT_Settings::get_post_thumb_settings();
if (!empty($thumb_settings['mult_thumb_cat_ids'])) {
$current_cat_id = (int) get_queried_object_id();
if (in_array($current_cat_id, array_map('intval', (array) $thumb_settings['mult_thumb_cat_ids']), true)) {
$config['multi_thumb_enabled'] = true;
$config['multi_thumb_limit'] = (int) ($thumb_settings['multi_count'] ?? 4);
}
}
}
return $config;
return aksrt_resolve_post_list_render_config([
'context' => $context,
'term_id' => $term_id,
'overrides' => $resolver_overrides,
]);
}
endif;
+31 -12
View File
@@ -384,11 +384,18 @@ if (!function_exists('aksrt_homepage_render_sortbar')) {
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php
$view_mode = 'list';
if (function_exists('aksrt_resolve_post_list_render_config')) {
$home_view_cfg = aksrt_resolve_post_list_render_config(['context' => 'home']);
$view_mode = (($home_view_cfg['mode'] ?? 'list') === 'card') ? 'card' : 'list';
}
?>
<div class="aksrt-homepage__view-toggle" data-aksrt-view-toggle>
<button type="button" class="aksrt-homepage__view-btn is-active" data-view-mode="list" aria-label="<?php esc_attr_e('列表视图', 'aksrt'); ?>">
<button type="button" class="aksrt-homepage__view-btn<?php echo $view_mode === 'list' ? ' is-active' : ''; ?>" data-view-mode="list" aria-label="<?php esc_attr_e('列表视图', 'aksrt'); ?>">
<svg viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"><path d="M1 3h14v2H1V3zm0 4h14v2H1V7zm0 4h14v2H1v-2z"/></svg>
</button>
<button type="button" class="aksrt-homepage__view-btn" data-view-mode="card" aria-label="<?php esc_attr_e('卡片视图', 'aksrt'); ?>">
<button type="button" class="aksrt-homepage__view-btn<?php echo $view_mode === 'card' ? ' is-active' : ''; ?>" data-view-mode="card" aria-label="<?php esc_attr_e('卡片视图', 'aksrt'); ?>">
<svg viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"><path d="M1 1h4v4H1V1zm5 0h4v4H6V1zm5 0h4v4h-4V1zM1 6h4v4H1V6zm5 0h4v4H6V6zm5 0h4v4h-4V6zM1 11h4v4H1v-4zm5 0h4v4H6v-4zm5 0h4v4h-4v-4z"/></svg>
</button>
</div>
@@ -589,10 +596,12 @@ if (!function_exists('aksrt_homepage_get_latest_panel_contract')) {
unset($query_vars['paged'], $query_vars['page']);
$render_config = [
'mode' => 'list',
'context' => 'home',
];
$render_config = function_exists('aksrt_resolve_post_list_render_config')
? aksrt_resolve_post_list_render_config(['context' => 'home'])
: [
'mode' => 'list',
'context' => 'home',
];
return [
'query_context' => 'homepage_latest',
@@ -753,10 +762,12 @@ if (!function_exists('aksrt_homepage_render_post_card')) {
return;
}
$config = [
'mode' => 'list',
'context' => 'home',
];
$config = function_exists('aksrt_resolve_post_list_render_config')
? aksrt_resolve_post_list_render_config(['context' => 'home'])
: [
'mode' => 'list',
'context' => 'home',
];
echo aksrt_render_post($config); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
@@ -768,15 +779,23 @@ if (!function_exists('aksrt_homepage_render_posts_grid')) {
*/
function aksrt_homepage_render_posts_grid(?WP_Query $query = null, bool $with_pagination = false): void {
$query = $query instanceof WP_Query ? $query : aksrt_homepage_get_latest_query();
$config = function_exists('aksrt_resolve_post_list_render_config')
? aksrt_resolve_post_list_render_config(['context' => 'home'])
: [
'mode' => 'list',
'context' => 'home',
];
$config['skip_wrapper'] = true;
$view = (($config['mode'] ?? 'list') === 'card') ? 'card' : 'list';
if (function_exists('aksrt_render_post_list')) {
// Panel already provides posts-row.ajaxpager; avoid nested wrapper.
aksrt_render_post_list($query, ['mode' => 'list', 'context' => 'home', 'skip_wrapper' => true], true);
aksrt_render_post_list($query, $config, true);
} else {
if (!$query->have_posts()) {
echo '<div class="no-results"><p>' . esc_html__('暂无文章', 'aksrt') . '</p></div>';
} else {
echo '<div class="posts-row aksrt-homepage__posts" data-view="list">';
echo '<div class="posts-row aksrt-homepage__posts" data-view="' . esc_attr($view) . '">';
while ($query->have_posts()) {
$query->the_post();
aksrt_homepage_render_post_card();
+15 -3
View File
@@ -50,6 +50,7 @@ if (!function_exists('aksrt_renderer_defaults')) {
'multi_thumb_limit' => 4,
'multi_thumb_min' => 2,
'list_show_type' => 'separate',
'list_img_position' => 'left',
'number_offset' => 0,
'show_number' => false,
'extra_class' => '',
@@ -87,9 +88,15 @@ if (!function_exists('aksrt_renderer_config')) {
$cfg['show_comments'] = false;
}
if ($cfg['mode'] === 'card' || $cfg['mode'] === 'grid-cms') {
$cfg['style'] = $cfg['style'] === 'default' ? 'style3' : $cfg['style'];
if (in_array((string) $cfg['style'], ['default', 'null'], true)) {
$cfg['style'] = '';
}
$cfg['list_img_position'] = in_array(sanitize_key((string) ($cfg['list_img_position'] ?? 'left')), ['left', 'right'], true)
? sanitize_key((string) ($cfg['list_img_position'] ?? 'left'))
: 'left';
$cfg['list_show_type'] = in_array(sanitize_key((string) ($cfg['list_show_type'] ?? 'separate')), ['separate', 'no_margin'], true)
? sanitize_key((string) ($cfg['list_show_type'] ?? 'separate'))
: 'separate';
return $cfg;
}
@@ -514,7 +521,11 @@ if (!function_exists('aksrt_renderer_thumb')) {
$target_attr = !empty($cfg['target_blank']) ? ' target="_blank"' : '';
if ($cfg['mode'] === 'list') {
return '<div class="post-graphic"><div class="item-thumbnail' . esc_attr($empty_class) . '"><a href="' . esc_url($href) . '" rel="bookmark"' . $target_attr . '>'
$graphic_class = 'post-graphic';
if (($cfg['list_img_position'] ?? 'left') === 'right') {
$graphic_class .= ' order1';
}
return '<div class="' . esc_attr($graphic_class) . '"><div class="item-thumbnail' . esc_attr($empty_class) . '"><a href="' . esc_url($href) . '" rel="bookmark"' . $target_attr . '>'
. $img_html . '</a>' . $sticky_html . $format_badge . '</div></div>';
}
@@ -714,6 +725,7 @@ if (!function_exists('aksrt_renderer_list')) {
}
if (($cfg['list_show_type'] ?? 'separate') === 'no_margin') {
$classes[] = 'no-margin';
$classes[] = 'no_margin';
}
$thumb = aksrt_renderer_thumb($data, $cfg);