收口 category/tag filter 与 term header 共享层
抽出 term header / filter chip group 共用渲染, 分类页与标签页只保留数据装配,不再各自复制 DOM 拼装。
This commit is contained in:
@@ -29,6 +29,7 @@ require_once AKSRT_DIR . '/inc/helpers/aksrt-flow.php';
|
||||
require_once AKSRT_DIR . '/inc/helpers/aksrt-homepage.php';
|
||||
require_once AKSRT_DIR . '/inc/helpers/aksrt-single.php';
|
||||
require_once AKSRT_DIR . '/inc/helpers/aksrt-archive.php';
|
||||
require_once AKSRT_DIR . '/inc/helpers/aksrt-archive-filters.php';
|
||||
require_once AKSRT_DIR . '/inc/helpers/aksrt-slider.php';
|
||||
require_once AKSRT_DIR . '/inc/helpers/aksrt-vip.php';
|
||||
require_once AKSRT_DIR . '/inc/helpers/aksrt-i18n-switch.php';
|
||||
|
||||
@@ -0,0 +1,200 @@
|
||||
<?php
|
||||
/**
|
||||
* Shared archive term header / filter bar helpers.
|
||||
*
|
||||
* Loaded by aksrt-archive.php.
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
if (!function_exists('aksrt_archive_render_term_header')) :
|
||||
/**
|
||||
* Shared term archive header for category/tag pages.
|
||||
*
|
||||
* @param array<string,mixed> $args Header args.
|
||||
*/
|
||||
function aksrt_archive_render_term_header(array $args = []): void {
|
||||
$args = wp_parse_args($args, [
|
||||
'header_style' => 'simple',
|
||||
'show_cover' => false,
|
||||
'cover_url' => '',
|
||||
'eyebrow' => '',
|
||||
'title' => '',
|
||||
'description' => '',
|
||||
'admin_path' => '',
|
||||
'admin_label' => '',
|
||||
]);
|
||||
|
||||
$header_style = (string) $args['header_style'];
|
||||
if ($header_style === 'none') {
|
||||
return;
|
||||
}
|
||||
|
||||
$show_cover = !empty($args['show_cover']) && (string) $args['cover_url'] !== '';
|
||||
$cover_url = (string) $args['cover_url'];
|
||||
?>
|
||||
<header class="page-header term-page-header term-page-header--<?php echo esc_attr($header_style); ?><?php echo ($show_cover && $header_style === 'cover') ? ' has-cover-bg' : ''; ?>">
|
||||
<?php if ($show_cover && $header_style === 'cover') : ?>
|
||||
<div class="term-page-header__cover-bg" style="background-image:url('<?php echo esc_url($cover_url); ?>')">
|
||||
<div class="term-page-header__cover-overlay"></div>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<div class="term-page-header__body<?php echo $header_style === 'cover' ? ' term-page-header__body--overlay' : ''; ?>">
|
||||
<div>
|
||||
<?php if ((string) $args['eyebrow'] !== '') : ?>
|
||||
<p class="term-page-header__eyebrow"><?php echo esc_html((string) $args['eyebrow']); ?></p>
|
||||
<?php endif; ?>
|
||||
<h1 class="entry-title"><?php echo esc_html((string) $args['title']); ?></h1>
|
||||
<?php if ((string) $args['description'] !== '') : ?>
|
||||
<p class="term-page-header__desc"><?php echo $args['description']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></p>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php
|
||||
if ((string) $args['admin_path'] !== '' && function_exists('aksrt_render_admin_quick_link')) {
|
||||
aksrt_render_admin_quick_link((string) $args['admin_path'], (string) $args['admin_label']);
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
<?php if ($show_cover && $header_style !== 'cover') : ?>
|
||||
<div class="term-page-header__cover" style="background-image:url('<?php echo esc_url($cover_url); ?>')"></div>
|
||||
<?php endif; ?>
|
||||
</header>
|
||||
<?php
|
||||
}
|
||||
endif;
|
||||
|
||||
if (!function_exists('aksrt_archive_render_filter_chip')) :
|
||||
/**
|
||||
* One archive filter chip.
|
||||
*
|
||||
* @param array<string,mixed> $chip Chip args.
|
||||
*/
|
||||
function aksrt_archive_render_filter_chip(array $chip): void {
|
||||
$chip = wp_parse_args($chip, [
|
||||
'label' => '',
|
||||
'url' => '',
|
||||
'active' => false,
|
||||
'attrs' => '',
|
||||
]);
|
||||
if ((string) $chip['label'] === '' || (string) $chip['url'] === '') {
|
||||
return;
|
||||
}
|
||||
?>
|
||||
<a class="archive-filter-bar__chip<?php echo !empty($chip['active']) ? ' is-active' : ''; ?>" href="<?php echo esc_url((string) $chip['url']); ?>"<?php echo $chip['attrs']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>><?php echo esc_html((string) $chip['label']); ?></a>
|
||||
<?php
|
||||
}
|
||||
endif;
|
||||
|
||||
if (!function_exists('aksrt_archive_render_filter_group')) :
|
||||
/**
|
||||
* One labeled chip group.
|
||||
*
|
||||
* @param array<string,mixed> $group Group args.
|
||||
*/
|
||||
function aksrt_archive_render_filter_group(array $group): void {
|
||||
$group = wp_parse_args($group, [
|
||||
'label' => '',
|
||||
'chips' => [],
|
||||
'class' => '',
|
||||
]);
|
||||
$chips = is_array($group['chips'] ?? null) ? $group['chips'] : [];
|
||||
if (empty($chips)) {
|
||||
return;
|
||||
}
|
||||
$class = trim('archive-filter-bar__main ' . (string) $group['class']);
|
||||
?>
|
||||
<div class="<?php echo esc_attr($class); ?>">
|
||||
<?php if ((string) $group['label'] !== '') : ?>
|
||||
<span class="archive-filter-bar__label"><?php echo esc_html((string) $group['label']); ?></span>
|
||||
<?php endif; ?>
|
||||
<div class="archive-filter-bar__chips">
|
||||
<?php foreach ($chips as $chip) : ?>
|
||||
<?php if (is_array($chip)) { aksrt_archive_render_filter_chip($chip); } ?>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
endif;
|
||||
|
||||
if (!function_exists('aksrt_archive_render_filter_bar')) :
|
||||
/**
|
||||
* Shared filter bar shell for category/tag archives.
|
||||
*
|
||||
* @param array<string,mixed> $args Filter bar args.
|
||||
*/
|
||||
function aksrt_archive_render_filter_bar(array $args = []): void {
|
||||
$args = wp_parse_args($args, [
|
||||
'ajax' => false,
|
||||
'ajax_archive' => '',
|
||||
'term_id' => 0,
|
||||
'layout_mode' => 'card',
|
||||
'card_style' => 'standard',
|
||||
'aria_label' => '',
|
||||
'meta_text' => '',
|
||||
'groups' => [],
|
||||
'sort_enabled' => false,
|
||||
'sort_current' => 'latest',
|
||||
'sort_links' => [],
|
||||
'sort_as_main' => false,
|
||||
]);
|
||||
|
||||
$groups = is_array($args['groups'] ?? null) ? $args['groups'] : [];
|
||||
$sort_links = is_array($args['sort_links'] ?? null) ? $args['sort_links'] : [];
|
||||
$has_sort = !empty($args['sort_enabled']) && !empty($sort_links);
|
||||
if (empty($groups) && !$has_sort && (string) $args['meta_text'] === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
$ajax = !empty($args['ajax']);
|
||||
?>
|
||||
<div
|
||||
class="archive-filter-bar<?php echo $ajax ? ' archive-filter-bar--ajax' : ''; ?>"
|
||||
aria-label="<?php echo esc_attr((string) ($args['aria_label'] !== '' ? $args['aria_label'] : __('筛选', 'aksrt'))); ?>"
|
||||
<?php if ($ajax) : ?>
|
||||
data-ajax-archive="<?php echo esc_attr((string) $args['ajax_archive']); ?>"
|
||||
data-term-id="<?php echo (int) $args['term_id']; ?>"
|
||||
data-query-context="archive"
|
||||
data-render-mode="<?php echo esc_attr((string) $args['layout_mode']); ?>"
|
||||
data-card-style="<?php echo esc_attr((string) $args['card_style']); ?>"
|
||||
<?php endif; ?>
|
||||
>
|
||||
<?php foreach ($groups as $group) : ?>
|
||||
<?php if (is_array($group)) { aksrt_archive_render_filter_group($group); } ?>
|
||||
<?php endforeach; ?>
|
||||
|
||||
<?php if ((string) $args['meta_text'] !== '') : ?>
|
||||
<div class="archive-filter-bar__meta"><?php echo esc_html((string) $args['meta_text']); ?></div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if ($has_sort) : ?>
|
||||
<?php
|
||||
$sort_chips = [];
|
||||
foreach ($sort_links as $sort_key => $sort_url) {
|
||||
$sort_chips[] = [
|
||||
'label' => $sort_key === 'oldest' ? __('最早', 'aksrt') : ($sort_key === 'popular' ? __('热门', 'aksrt') : __('最新', 'aksrt')),
|
||||
'url' => (string) $sort_url,
|
||||
'active' => (string) $args['sort_current'] === (string) $sort_key,
|
||||
'attrs' => $ajax ? ' data-filter-sort="' . esc_attr((string) $sort_key) . '"' : '',
|
||||
];
|
||||
}
|
||||
if (!empty($args['sort_as_main'])) {
|
||||
aksrt_archive_render_filter_group([
|
||||
'label' => __('排序', 'aksrt'),
|
||||
'chips' => $sort_chips,
|
||||
]);
|
||||
} else {
|
||||
?>
|
||||
<div class="archive-filter-bar__sort" aria-label="<?php esc_attr_e('排序方式', 'aksrt'); ?>">
|
||||
<?php foreach ($sort_chips as $chip) { aksrt_archive_render_filter_chip($chip); } ?>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
?>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
endif;
|
||||
Reference in New Issue
Block a user