*/ function aksrt_homepage_get_tab_configs(): array { 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 ($tab['term_id'] <= 0) { continue; } 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'] ?: 'modified', ]; } 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_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_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', 'like', 'comment_count', 'favorite', 'rand']; $default_sort = class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\Settings::homepage_default_sort() : 'modified'; $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 */ function aksrt_homepage_get_sort_items(): array { $all_labels = class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\Settings::homepage_sort_labels() : [ 'modified' => '更新', 'date' => '发布', 'views' => '浏览', 'like' => '点赞', 'comment_count' => '评论', 'favorite' => '收藏', '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 'views': // Canonical public views key (align hotspot consumers later). $args = $apply_meta_sort($args, 'aksrt_post_views'); break; case 'like': $args = $apply_meta_sort($args, '_aksrt_like_count'); break; case 'favorite': $args = $apply_meta_sort($args, '_aksrt_favorite_count'); break; case 'comment_count': $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', 'like', 'comment_count', 'favorite', 'rand']; if (!in_array($sort_mode, $allowed, true)) { $sort_mode = 'modified'; } $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; ?>