Files
wordpress-theme-aksrt/inc/helpers/aksrt-archive.php
T
kate522 a9ee8c451f 统一 archive/category/tag/search 主内容流壳层
抽出 aksrt-archive 共享 shell/stream/render config,
四类列表页统一 content-shell + shared renderer + page-owned sidebar。
2026-07-09 22:10:43 +08:00

289 lines
8.6 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_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>}
*/
function aksrt_archive_get_list_style_settings(): array {
$settings = class_exists('AKSRT_Settings')
? AKSRT_Settings::get_post_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']) : [],
];
}
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 {
$list = aksrt_archive_get_list_style_settings();
$config = array_merge([
'context' => 'archive',
'mode' => $list['render_mode'],
'card_style' => $list['card_style'],
], $overrides);
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';
}
}
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;
}
endif;
if (!function_exists('aksrt_archive_get_sort_state')) :
/**
* Shared latest/oldest/popular sort state for term archives.
*
* @return array{current:string,links:array<string,string>}
*/
function aksrt_archive_get_sort_state(): array {
$current_orderby = sanitize_key((string) get_query_var('orderby'));
$current_order = strtoupper(sanitize_key((string) get_query_var('order')));
$current = 'latest';
if ($current_orderby === 'date' && $current_order === 'ASC') {
$current = 'oldest';
} elseif ($current_orderby === 'comment_count') {
$current = 'popular';
}
return [
'current' => $current,
'links' => [
'latest' => remove_query_arg(['orderby', 'order', 'paged']),
'oldest' => add_query_arg(['orderby' => 'date', 'order' => 'asc', 'paged' => false]),
'popular' => add_query_arg(['orderby' => 'comment_count', 'order' => 'desc', 'paged' => false]),
],
];
}
endif;
if (!function_exists('aksrt_archive_has_sidebar')) :
/**
* Resolve whether a stream page should show an aside.
*
* @param string $preferred Preferred sidebar id.
* @param bool $enabled Page-level sidebar toggle.
*/
function aksrt_archive_has_sidebar(string $preferred = '', bool $enabled = true): bool {
if (!$enabled || wp_is_mobile()) {
return false;
}
if ($preferred !== '' && is_active_sidebar($preferred)) {
return true;
}
if (function_exists('aksrt_is_show_sidebar')) {
return aksrt_is_show_sidebar();
}
$fallback = function_exists('aksrt_resolve_sidebar_id') ? aksrt_resolve_sidebar_id() : 'sidebar-blog';
return is_active_sidebar($fallback)
|| is_active_sidebar('all_sidebar_top')
|| is_active_sidebar('all_sidebar_bottom');
}
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' => '',
'show_loading' => false,
]);
$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'] ?? 'card');
$card_style = (string) ($render_config['card_style'] ?? 'standard');
$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-card-style="<?php echo esc_attr($card_style); ?>"
<?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 (!empty($args['show_loading'])) : ?>
<div class="archive__ajax-loading" aria-hidden="true"><span class="archive__ajax-spinner"></span></div>
<?php endif; ?>
<?php if ($has_posts) : ?>
<?php
if (function_exists('aksrt_render_post_list')) {
aksrt_render_post_list($query, array_merge($render_config, ['wrapper_class' => 'content-stream__list']));
}
?>
<div class="content-stream__footer">
<?php
if (function_exists('aksrt_render_pagination')) {
aksrt_render_pagination($query, [
'query_context' => (string) $args['query_context'],
'render_config' => $render_config,
]);
}
?>
</div>
<?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',
]);
$main_class = trim((string) $args['main_class']);
?>
<main id="main-content" class="<?php echo esc_attr($main_class); ?>" tabindex="-1">
<?php
if (!empty($args['top_fluid']) && is_array($args['top_fluid'])) {
aksrt_archive_render_widget_zone($args['top_fluid'], 'fluid-widget-wrap');
}
?>
<div class="content-wrap">
<div class="content-layout<?php echo !empty($args['has_sidebar']) ? '' : ' content-layout--no-sidebar'; ?>">
<div class="content-layout__primary">
<?php
}
endif;
if (!function_exists('aksrt_archive_close_shell')) :
/**
* Close a shared archive shell.
*
* @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>
<?php if (!empty($args['has_sidebar'])) : ?>
<?php get_sidebar(); ?>
<?php endif; ?>
</div>
</div>
<?php
if (!empty($args['bottom_fluid']) && is_array($args['bottom_fluid'])) {
aksrt_archive_render_widget_zone($args['bottom_fluid'], 'fluid-widget-wrap');
}
?>
</main>
<?php
}
endif;