Files
wordpress-theme-aksrt/inc/helpers/jio-homepage.php
T
kate522andClaude Opus 5 427968a721 chore(branding): remove leftover upstream theme identifiers
The theme was renamed Zibll/子比 → AKSRT → Jio, but upstream branding
survived the sweeps in CSS classes, the SVG sprite prefix, a persisted
widget field ID, JS events, admin Vue class names, test infrastructure
and docblocks.

Renames:
- zib-widget / zib-widget-wrap / zib-slider → jio-widget-box /
  jio-widget-box-wrap / jio-slider-box (jio-widget is taken by the modern
  widget shell, jio-box-* by the card primitives)
- zib-partners-scroll + zps-* → jio-partners-scroll + jps-*
- zib-combo-info-card + zci-* → jio-combo-info-card + jci-*
- zibsvg- → jiosvg-, including the exported picker helpers and the
  fa-zibsvg- family-prefix guard
- zibpay_type → pay_type; the dual-read fallback in PostQuery collapses
  to a single key
- [zibajax="submit"] dropped (no PHP emitter); zib_ajax.success →
  jio_ajax.success
- aks-* admin Vue classes → jio-*
- zib_palette() → palette()

User-facing vendor traces are gone: the floating "more" button default no
longer ships a zibll.com link or 子比主题 marketing copy, the notice
widget ships no preset promo entries, and the four 官网教程 links are
removed from the post-list admin schema.

Test infrastructure renamed to neutral names (docker-compose.reference.yml,
Caddyfile, article-baseline.json, modern-widgets-contract.test.cjs) with
their DB/volume/env/slug identifiers following.

Kept intentionally, documented in tests/fixtures/README.md: the golden
fixture reference fingerprint (a SHA-256 record of an external directory,
read by validate-widget-golden / generate-widget-golden-fixture /
audit-widget-system), the doesNotMatch guards that prove the old tokens are
gone, and the reference theme's own meta key in the visual fixtures.

No backward-compat or data migration: saved widget configs holding old
icon keys or the old field ID silently lose them, per the project's
rewrite-over-compatibility rule.

Verified: 132/132 node contract tests, php tests/widget-system/run.php
(146 assertions), audit-admin-contracts, audit-psr4-paths and
validate-widget-golden all green; tracked bundles regenerated and in sync.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-11-02 00:06:54 +08:00

340 lines
9.6 KiB
PHP

<?php
/**
* Jio Fixed Homepage Helpers
*
* Rebuilds the homepage around a fixed legacy-like skeleton instead of the
* old freeform home modules canvas.
*
* @package Jio
*/
defined('ABSPATH') || exit;
if (!function_exists('jio_is_fixed_homepage_request')) {
/**
* Whether the current request should use the fixed homepage layout.
*/
function jio_is_fixed_homepage_request(): bool {
if (is_admin()) {
return false;
}
return is_front_page() || is_home();
}
}
if (!function_exists('jio_homepage_get_current_page')) {
/**
* Resolve the current homepage page number for both blog and static-front
* pagination contexts.
*/
function jio_homepage_get_current_page(): int {
return max(1, (int) get_query_var('paged'), (int) get_query_var('page'));
}
}
if (!function_exists('jio_homepage_get_post_types')) {
/**
* Homepage latest feed post types.
* Resources are ordinary posts with meta — always post.
*
* @return string[]
*/
function jio_homepage_get_post_types(): array {
return ['post'];
}
}
if (!function_exists('jio_homepage_get_tab_configs')) {
/**
* Homepage category tabs with full config.
*
* Returns enabled tabs in config order, each with 'term', 'title', 'orderby'.
* Preserves every configured row in the saved order.
*
* @return array<int, array{term:WP_Term, title:string, orderby:string, term_id:int}>
*/
function jio_homepage_get_tab_configs(): array {
if (!class_exists('\Jio\Settings\Settings')) {
return [];
}
$structured_tabs = \Jio\Settings\Settings::homepage_tabs();
if (empty($structured_tabs)) {
return [];
}
$configs = [];
foreach ($structured_tabs as $tab) {
if ($tab['term_id'] <= 0) {
continue;
}
$term = get_term($tab['term_id']);
if (!$term || is_wp_error($term)) {
continue;
}
if ($term->taxonomy !== 'category' || (int) $term->count === 0) {
continue;
}
$configs[] = [
'term' => $term,
'term_id' => (int) $tab['term_id'],
'title' => $tab['title'] !== '' ? $tab['title'] : $term->name,
'orderby' => $tab['orderby'] ?: '',
];
}
return $configs;
}
}
if (!function_exists('jio_homepage_get_sort_mode')) {
/**
* Current homepage main-stream sort mode.
*/
function jio_homepage_get_sort_mode(): string {
$allowed = class_exists('\Jio\Settings\Settings')
? \Jio\Settings\Settings::homepage_allowed_sorts()
: ['modified', 'date', 'views', 'like', 'comment_count', 'favorite', 'rand'];
$default_sort = class_exists('\Jio\Settings\Settings')
? \Jio\Settings\Settings::homepage_default_sort()
: 'date';
$override_sort = get_query_var('jio_homepage_sort_override');
$sort = is_string($override_sort) && $override_sort !== ''
? sanitize_key($override_sort)
: $default_sort;
return in_array($sort, $allowed, true) ? $sort : $default_sort;
}
}
if (!function_exists('jio_homepage_get_request_sort')) {
/**
* Main-stream sort requested by URL (?orderby=).
*
* 排序菜单(.ajax-option)点击后原位刷新的响应页靠它同序。
*/
function jio_homepage_get_request_sort(): string {
$get_sort = isset($_GET['orderby']) ? sanitize_key(wp_unslash((string) $_GET['orderby'])) : '';
if ($get_sort === '') {
return '';
}
$allowed = class_exists('\Jio\Settings\Settings')
? \Jio\Settings\Settings::homepage_allowed_sorts()
: ['modified', 'date', 'views', 'like', 'comment_count', 'favorite', 'rand'];
return in_array($get_sort, $allowed, true) ? $get_sort : '';
}
}
if (!function_exists('jio_homepage_apply_sort_args')) {
/**
* Apply sort args to a query configuration.
*/
function jio_homepage_apply_sort_args(array $args): array {
$sort_mode = jio_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, 'jio_post_views');
break;
case 'like':
$args = $apply_meta_sort($args, '_jio_like_count');
break;
case 'favorite':
$args = $apply_meta_sort($args, '_jio_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('jio_homepage_apply_named_sort_args')) {
/**
* Apply a specific named sort to query args (used by per-tab orderby).
*/
function jio_homepage_apply_named_sort_args(array $args, string $sort_mode): array {
$allowed = class_exists('\Jio\Settings\Settings')
? \Jio\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('jio_homepage_sort_override');
set_query_var('jio_homepage_sort_override', $sort_mode);
$result = jio_homepage_apply_sort_args($args);
set_query_var('jio_homepage_sort_override', $previous);
return $result;
}
}
if (!function_exists('jio_homepage_render_ajax_option_menu')) {
/**
* 首页排序菜单。
*
* 输出 .ajax-option.ajax-replace[win-ajax-replace="filter"],点击 .ajax-next
* 排序链接由 runtime post_ajax 原位替换 #index-tab-main 内的列表。
*/
function jio_homepage_render_ajax_option_menu(): void {
if (!empty($_GET['nofilter'])) {
return;
}
if (!class_exists('\Jio\Settings\Settings') || !\Jio\Settings\Settings::home_list_sort_enabled()) {
return;
}
$con = jio_homepage_get_option_orderby_but([
'lists' => (array) \Jio\Settings\Settings::home_list_sort_options(),
'dropdown' => (bool) \Jio\Settings\Settings::home_list_sort_dropdown(),
]);
if (!$con) {
return;
}
echo '<div class="ajax-option ajax-replace" win-ajax-replace="filter">' . $con . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '<div></div>'; // 空白内容,解决css奇数偶数行
}
}
if (!function_exists('jio_homepage_get_option_orderby_but')) {
/**
* 排序方式按钮组。
*
* @param array{lists?:string[],dropdown?:bool} $option Sort option config.
*/
function jio_homepage_get_option_orderby_but($option = []): string {
$defaults = [
'lists' => [],
'dropdown' => false,
];
$option = wp_parse_args((array) $option, $defaults);
if (!$option['lists'] && !$option['dropdown']) {
return '';
}
$labels = class_exists('\Jio\Settings\Settings')
? \Jio\Settings\Settings::homepage_sort_labels()
: ['modified' => '更新', 'date' => '发布', 'views' => '浏览', 'like' => '点赞', 'comment_count' => '评论', 'favorite' => '收藏', 'rand' => '随机'];
$dropdown_but = '';
$but = '';
$uri = jio_homepage_url_del_paged(jio_homepage_get_current_url());
foreach ($option['lists'] as $key) {
if (!isset($labels[$key])) {
continue;
}
$_class = 'ajax-next';
if (isset($_GET['orderby']) && sanitize_key(wp_unslash((string) $_GET['orderby'])) === $key) {
$_class .= ' focus-color';
}
$href = add_query_arg(['orderby' => $key], $uri);
$but .= '<a rel="nofollow" ajax-replace="true" class="' . esc_attr($_class) . '" href="' . esc_url($href) . '">' . esc_html($labels[$key]) . '</a>';
}
if ($option['dropdown']) {
foreach ($labels as $key => $value) {
$_class = 'ajax-next';
if (isset($_GET['orderby']) && sanitize_key(wp_unslash((string) $_GET['orderby'])) === $key) {
$_class .= ' focus-color';
}
$href = add_query_arg(['orderby' => $key], $uri);
$dropdown_but .= '<li><a rel="nofollow" ajax-replace="true" class="' . esc_attr($_class) . '" href="' . esc_url($href) . '">' . esc_html($value) . '</a></li>';
}
}
if (!$but && !$dropdown_but) {
return '';
}
$is_dropdown = ($option['dropdown'] && $dropdown_but) ? true : false;
$d_but = $is_dropdown
? '<a href="javascript:;" data-toggle="dropdown"><span name="cat">' . esc_html__('排序', 'jio') . '</span><i class="fa fas fa-fw fa-sort opacity5" aria-hidden="true"></i></a>'
: esc_html__('排序', 'jio');
$html = '<div class="flex ac">';
$html .= '<div class="option-dropdown splitters-this-r dropdown flex0">';
$html .= $d_but;
$html .= $is_dropdown ? '<ul class="dropdown-menu">' . $dropdown_but . '</ul>' : '';
$html .= '</div>';
$html .= '<ul class="list-inline scroll-x mini-scrollbar option-items">' . $but . '</ul>';
$html .= '</div>';
return $html;
}
}
if (!function_exists('jio_homepage_get_current_url')) {
/**
* 当前页面链接。
*/
function jio_homepage_get_current_url(): string {
$home_url = home_url();
$host = preg_replace('/^(http|https)(:\/\/)(?)([^\/]+).*$/im', '$1$2$3', $home_url);
return $host . add_query_arg(null, false);
}
}
if (!function_exists('jio_homepage_url_del_paged')) {
/**
* 移除分页参数的链接。
*/
function jio_homepage_url_del_paged(string $url): string {
$url = remove_query_arg(['paged', 'page'], $url);
global $wp_rewrite;
return preg_replace('/\/' . $wp_rewrite->pagination_base . '\/\d*/', '', $url);
}
}