Files
wordpress-theme-aksrt/inc/helpers/aksrt-homepage.php
T

395 lines
12 KiB
PHP

<?php
/**
* AKSRT Fixed Homepage Helpers
*
* Rebuilds the homepage around a fixed Zibll-like skeleton instead of the
* old freeform home modules canvas.
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
if (!function_exists('aksrt_is_fixed_homepage_request')) {
/**
* Whether the current request should use the fixed homepage layout.
*/
function aksrt_is_fixed_homepage_request(): bool {
if (is_admin()) {
return false;
}
return is_front_page() || is_home();
}
}
if (!function_exists('aksrt_homepage_get_current_page')) {
/**
* Resolve the current homepage page number for both blog and static-front
* pagination contexts.
*/
function aksrt_homepage_get_current_page(): int {
return max(1, (int) get_query_var('paged'), (int) get_query_var('page'));
}
}
if (!function_exists('aksrt_homepage_get_post_types')) {
/**
* Homepage latest feed post types.
* Resources are ordinary posts with meta — always post.
*
* @return string[]
*/
function aksrt_homepage_get_post_types(): array {
return ['post'];
}
}
if (!function_exists('aksrt_homepage_get_tab_terms')) {
/**
* Homepage category tabs with full config.
*
* Returns enabled tabs in config order, each with 'term', 'title', 'orderby'.
* Uses homepage_tab_limit from settings.
*
* @return array<int, array{term:WP_Term, title:string, orderby:string, term_id:int}>
*/
function aksrt_homepage_get_tab_configs(): array {
$limit = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::homepage_tab_limit()
: 4;
if (!class_exists('\AKSRT\Settings\Settings')) {
return [];
}
$structured_tabs = \AKSRT\Settings\Settings::homepage_tabs();
if (empty($structured_tabs)) {
return [];
}
$excluded_cat_ids = array_flip(\AKSRT\Settings\Settings::home_exclude_category_ids());
$configs = [];
foreach ($structured_tabs as $tab) {
if (empty($tab['enabled']) || $tab['term_id'] <= 0) {
continue;
}
if (count($configs) >= $limit) {
break;
}
if (isset($excluded_cat_ids[$tab['term_id']])) {
continue;
}
$term = get_term($tab['term_id']);
if (!$term || is_wp_error($term)) {
continue;
}
if ($term->taxonomy !== 'category') {
continue;
}
$configs[] = [
'term' => $term,
'term_id' => (int) $tab['term_id'],
'title' => $tab['title'] !== '' ? $tab['title'] : $term->name,
'orderby' => $tab['orderby'] ?: 'date',
];
}
return $configs;
}
/**
* Homepage category tabs — returns WP_Term objects for backward compat.
*
* @param int $limit Unused, reads from settings.
* @return WP_Term[]
*/
function aksrt_homepage_get_tab_terms(int $limit = 4): array {
$configs = aksrt_homepage_get_tab_configs();
return array_column($configs, 'term');
}
}
if (!function_exists('aksrt_homepage_get_tab_posts_per_page')) {
/**
* Per-tab homepage post count.
*/
function aksrt_homepage_get_tab_posts_per_page(): int {
if (class_exists('\AKSRT\Settings\Settings')) {
$count = \AKSRT\Settings\Settings::homepage_tab_posts_per_page();
if ($count > 0) {
return max(1, min(20, $count));
}
}
return 6;
}
}
if (!function_exists('aksrt_homepage_get_default_view_mode')) {
/**
* Default homepage view mode.
*/
function aksrt_homepage_get_default_view_mode(): string {
if (class_exists('\AKSRT\Settings\Settings')) {
return \AKSRT\Settings\Settings::homepage_default_view();
}
return 'list';
}
}
if (!function_exists('aksrt_homepage_sticky_tabs_enabled')) {
/**
* Whether homepage tabs should stick below the primary nav.
*/
function aksrt_homepage_sticky_tabs_enabled(): bool {
if (class_exists('\AKSRT\Settings\Settings')) {
return \AKSRT\Settings\Settings::homepage_sticky_tabs_enabled();
}
return true;
}
}
if (!function_exists('aksrt_homepage_module_hero_enabled')) {
/**
* Whether the fixed homepage should render the built-in hero section.
*/
function aksrt_homepage_module_hero_enabled(): bool {
if (class_exists('\AKSRT\Settings\Settings')) {
return \AKSRT\Settings\Settings::homepage_module_hero_enabled();
}
return true;
}
}
if (!function_exists('aksrt_homepage_module_banner_enabled')) {
/**
* Whether the fixed homepage should render the built-in banner section.
*/
function aksrt_homepage_module_banner_enabled(): bool {
if (class_exists('\AKSRT\Settings\Settings')) {
return \AKSRT\Settings\Settings::homepage_module_banner_enabled();
}
return true;
}
}
if (!function_exists('aksrt_homepage_get_sort_mode')) {
/**
* Current homepage main-stream sort mode.
*/
function aksrt_homepage_get_sort_mode(): string {
$allowed = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::homepage_allowed_sorts()
: ['modified', 'date', 'views', 'likes', 'comments', 'favorites', 'downloads', 'rand'];
$default_sort = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::homepage_default_sort()
: 'date';
$override_sort = get_query_var('aksrt_homepage_sort_override');
$sort = is_string($override_sort) && $override_sort !== ''
? sanitize_key($override_sort)
: (isset($_GET['home_sort']) ? sanitize_key(wp_unslash((string) $_GET['home_sort'])) : $default_sort);
return in_array($sort, $allowed, true) ? $sort : $default_sort;
}
}
if (!function_exists('aksrt_homepage_get_sort_items')) {
/**
* Configured sort bar items for the main stream.
*
* @return array<string,string>
*/
function aksrt_homepage_get_sort_items(): array {
$all_labels = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::homepage_sort_labels()
: [
'modified' => '更新', 'date' => '发布', 'views' => '浏览',
'likes' => '点赞', 'comments' => '评论', 'favorites' => '收藏',
'downloads' => '下载', 'rand' => '随机',
];
$enabled_keys = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::home_list_sort_options()
: array_keys($all_labels);
$items = [];
foreach ($enabled_keys as $key) {
if (isset($all_labels[$key])) {
$items[$key] = $all_labels[$key];
}
}
return !empty($items) ? $items : ['modified' => '更新', 'date' => '发布'];
}
}
if (!function_exists('aksrt_homepage_apply_sort_args')) {
/**
* Apply sort args to a query configuration.
*/
function aksrt_homepage_apply_sort_args(array $args): array {
$sort_mode = aksrt_homepage_get_sort_mode();
// Meta sorts: keep posts without the meta key via OR NOT EXISTS so lists never collapse empty.
$apply_meta_sort = static function (array $args, string $meta_key): array {
$args['meta_key'] = $meta_key;
$args['orderby'] = [
'meta_value_num' => 'DESC',
'date' => 'DESC',
];
$args['meta_query'] = [
'relation' => 'OR',
[
'key' => $meta_key,
'compare' => 'EXISTS',
],
[
'key' => $meta_key,
'compare' => 'NOT EXISTS',
],
];
return $args;
};
switch ($sort_mode) {
case 'modified':
$args['orderby'] = 'modified';
$args['order'] = 'DESC';
break;
case 'downloads':
$args = $apply_meta_sort($args, '_aksrt_res_download_count');
break;
case 'views':
// Canonical public views key (align hotspot consumers later).
$args = $apply_meta_sort($args, 'aksrt_post_views');
break;
case 'likes':
$args = $apply_meta_sort($args, '_aksrt_like_count');
break;
case 'favorites':
$args = $apply_meta_sort($args, '_aksrt_favorite_count');
break;
case 'comments':
$args['orderby'] = 'comment_count';
$args['order'] = 'DESC';
break;
case 'rand':
$args['orderby'] = 'rand';
unset($args['order']);
break;
case 'date':
default:
$args['orderby'] = 'date';
$args['order'] = 'DESC';
break;
}
return $args;
}
}
if (!function_exists('aksrt_homepage_apply_named_sort_args')) {
/**
* Apply a specific named sort to query args (used by per-tab orderby).
*/
function aksrt_homepage_apply_named_sort_args(array $args, string $sort_mode): array {
$allowed = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::homepage_allowed_sorts()
: ['modified', 'date', 'views', 'likes', 'comments', 'favorites', 'downloads', 'rand'];
if (!in_array($sort_mode, $allowed, true)) {
$sort_mode = 'date';
}
$previous = get_query_var('aksrt_homepage_sort_override');
set_query_var('aksrt_homepage_sort_override', $sort_mode);
$result = aksrt_homepage_apply_sort_args($args);
set_query_var('aksrt_homepage_sort_override', $previous);
return $result;
}
}
if (!function_exists('aksrt_homepage_render_sortbar')) {
/**
* Render the main-stream sort bar.
*/
function aksrt_homepage_render_sortbar(): void {
if (class_exists('\AKSRT\Settings\Settings') && !\AKSRT\Settings\Settings::home_list_sort_enabled()) {
return;
}
$current = aksrt_homepage_get_sort_mode();
$sort_ajax_enabled = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::homepage_sort_ajax_enabled()
: true;
$is_dropdown = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::home_list_sort_dropdown()
: false;
$base_url = remove_query_arg('home_sort', get_pagenum_link(aksrt_homepage_get_current_page()));
$sort_items = aksrt_homepage_get_sort_items();
$all_labels = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::homepage_sort_labels()
: $sort_items;
?>
<div class="aksrt-homepage__sortbar" data-sort-ajax="<?php echo $sort_ajax_enabled ? 'true' : 'false'; ?>" aria-label="<?php esc_attr_e('Homepage Sort', 'aksrt'); ?>">
<?php if ($is_dropdown) : ?>
<div class="aksrt-homepage__sortbar-dropdown">
<button type="button" class="aksrt-homepage__sortbar-current" data-sort-dropdown-trigger>
<?php echo esc_html($all_labels[$current] ?? $current); ?>
<i class="fas fa-caret-down" aria-hidden="true"></i>
</button>
<div class="aksrt-homepage__sortbar-dropdown-menu" hidden>
<?php foreach ($sort_items as $sort_key => $label) : ?>
<a
class="aksrt-homepage__sortbar-dropdown-item<?php echo $sort_key === $current ? ' is-active' : ''; ?>"
href="<?php echo esc_url(add_query_arg('home_sort', $sort_key, $base_url)); ?>"
data-sort="<?php echo esc_attr($sort_key); ?>"
>
<?php echo esc_html($label); ?>
</a>
<?php endforeach; ?>
</div>
</div>
<?php else : ?>
<div class="aksrt-homepage__sortbar-links">
<?php foreach ($sort_items as $sort_key => $label) : ?>
<a
class="aksrt-homepage__sortbar-link<?php echo $sort_key === $current ? ' is-active' : ''; ?>"
href="<?php echo esc_url(add_query_arg('home_sort', $sort_key, $base_url)); ?>"
data-sort="<?php echo esc_attr($sort_key); ?>"
>
<?php echo esc_html($label); ?>
</a>
<?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<?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<?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>
</div>
<?php
}
}