Files
wordpress-theme-aksrt/archive-resources.php
T

189 lines
6.4 KiB
PHP

<?php
/**
* Resource Archive Template
*
* Virtual list at /resources/ — ordinary posts marked with _aksrt_is_resource.
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
get_header();
$current_orderby = isset($_GET['orderby']) ? sanitize_key((string) $_GET['orderby']) : 'date';
$allowed_orderby = ['date', 'downloads', 'views'];
if (!in_array($current_orderby, $allowed_orderby, true)) {
$current_orderby = 'date';
}
$current_price = isset($_GET['price']) ? sanitize_key((string) $_GET['price']) : 'all';
if (!in_array($current_price, ['all', 'free', 'paid'], true)) {
$current_price = 'all';
}
$price_min = isset($_GET['price_min']) && $_GET['price_min'] !== '' ? (float) $_GET['price_min'] : '';
$price_max = isset($_GET['price_max']) && $_GET['price_max'] !== '' ? (float) $_GET['price_max'] : '';
$archive_url = function_exists('aksrt_resource_archive_url') ? aksrt_resource_archive_url() : home_url('/resources/');
$sort_options = [
'date' => __('最新', 'aksrt'),
'downloads' => __('热门下载', 'aksrt'),
'views' => __('阅读最多', 'aksrt'),
];
$price_options = [
'all' => __('全部', 'aksrt'),
'free' => __('免费', 'aksrt'),
'paid' => __('付费', 'aksrt'),
];
$preserve = [];
if ($current_orderby !== 'date') {
$preserve['orderby'] = $current_orderby;
}
if ($current_price !== 'all') {
$preserve['price'] = $current_price;
}
if ($price_min !== '' && $price_min > 0) {
$preserve['price_min'] = $price_min;
}
if ($price_max !== '') {
$preserve['price_max'] = $price_max;
}
$has_filter = $current_orderby !== 'date' || $current_price !== 'all' || $price_min !== '' || $price_max !== '';
?>
<main id="main-content" class="site-main container" tabindex="-1">
<header class="resource-archive-header page-cover">
<div class="container">
<?php if (function_exists('aksrt_breadcrumbs')) : ?>
<nav class="resource-archive-header__breadcrumbs" aria-label="<?php esc_attr_e('面包屑', 'aksrt'); ?>">
<?php aksrt_breadcrumbs(); ?>
</nav>
<?php endif; ?>
<h1 class="resource-archive-header__title"><?php esc_html_e('资源', 'aksrt'); ?></h1>
<p class="resource-archive-header__sub">
<?php
global $wp_query;
/* translators: %s: resource count */
printf(esc_html__('共 %s 个资源', 'aksrt'), esc_html(number_format_i18n((int) $wp_query->found_posts)));
?>
</p>
</div>
</header>
<div class="content-wrap">
<div class="content-layout content-layout--no-sidebar">
<div class="content-layout__primary resource-archive">
<nav class="resource-archive__toolbar" aria-label="<?php esc_attr_e('筛选', 'aksrt'); ?>">
<div class="resource-archive__filter-group">
<span class="resource-archive__filter-label"><?php esc_html_e('排序', 'aksrt'); ?></span>
<ul class="resource-archive__sort">
<?php foreach ($sort_options as $key => $label) :
$args = $preserve;
if ($key === 'date') {
unset($args['orderby']);
} else {
$args['orderby'] = $key;
}
$url = $args ? add_query_arg($args, $archive_url) : $archive_url;
$active = $current_orderby === $key;
?>
<li>
<a href="<?php echo esc_url($url); ?>"
class="resource-archive__sort-link<?php echo $active ? ' is-active' : ''; ?>"
<?php echo $active ? 'aria-current="true"' : ''; ?>>
<?php echo esc_html($label); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
<div class="resource-archive__filter-group">
<span class="resource-archive__filter-label"><?php esc_html_e('价格', 'aksrt'); ?></span>
<ul class="resource-archive__sort">
<?php foreach ($price_options as $key => $label) :
$args = $preserve;
if ($key === 'all') {
unset($args['price']);
} else {
$args['price'] = $key;
}
unset($args['price_min'], $args['price_max']);
$url = $args ? add_query_arg($args, $archive_url) : $archive_url;
$active = $current_price === $key;
?>
<li>
<a href="<?php echo esc_url($url); ?>"
class="resource-archive__sort-link<?php echo $active ? ' is-active' : ''; ?>"
<?php echo $active ? 'aria-current="true"' : ''; ?>>
<?php echo esc_html($label); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
<form class="resource-archive__price-range" method="get" action="<?php echo esc_url($archive_url); ?>">
<?php
if ($current_orderby !== 'date') {
echo '<input type="hidden" name="orderby" value="' . esc_attr($current_orderby) . '">';
}
if ($current_price !== 'all') {
echo '<input type="hidden" name="price" value="' . esc_attr($current_price) . '">';
}
?>
<label class="resource-archive__price-input">
<span><?php esc_html_e('最低', 'aksrt'); ?></span>
<input type="number" name="price_min" min="0" step="1" value="<?php echo esc_attr((string) $price_min); ?>">
</label>
<span class="resource-archive__price-sep">—</span>
<label class="resource-archive__price-input">
<span><?php esc_html_e('最高', 'aksrt'); ?></span>
<input type="number" name="price_max" min="0" step="1" value="<?php echo esc_attr((string) $price_max); ?>">
</label>
<button type="submit" class="btn-primary btn-sm"><?php esc_html_e('应用', 'aksrt'); ?></button>
</form>
<?php if ($has_filter) : ?>
<a class="resource-archive__reset" href="<?php echo esc_url($archive_url); ?>">
<i class="fas fa-times-circle" aria-hidden="true"></i>
<?php esc_html_e('清除筛选', 'aksrt'); ?>
</a>
<?php endif; ?>
</nav>
<?php if (have_posts()) : ?>
<div class="posts-row ajaxpager tab-pane">
<?php
set_query_var('aksrt_post_card_layout', 'card');
while (have_posts()) :
the_post();
get_template_part('templates/parts/content', 'post-card');
endwhile;
set_query_var('aksrt_post_card_layout', '');
?>
</div>
<?php aksrt_render_pagination(); ?>
<?php else : ?>
<div class="no-results">
<p><?php esc_html_e('暂无资源。', 'aksrt'); ?></p>
<?php if ($has_filter) : ?>
<p><a class="btn-outline btn-sm" href="<?php echo esc_url($archive_url); ?>"><?php esc_html_e('清除筛选条件', 'aksrt'); ?></a></p>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
</div>
</div>
</main>
<?php
get_footer();