Files
wordpress-theme-aksrt/inc/helpers/jio-homepage.php
T
kate522andClaude Opus 5 35bb96e82f refactor: rename the theme brand and every identifier from AKSRT to Jio
Full-project rename. `git grep -i aksrt` is now empty apart from the
DBMigrator 3.3.0 changelog note that documents the rename itself.

Code identifiers
- PHP namespace AKSRT\ -> Jio\ (composer PSR-4 + 378 declarations)
- global constants AKSRT_* -> JIO_* (VERSION/DIR/URI, PAY_TYPE_*, CAPTCHA_*)
- procedural functions, hooks, options, meta keys, table names,
  cookies, query vars, ajax actions and nonces: aksrt_ -> jio_
- text domain aksrt -> jio; Theme Name -> Jio
- JS globals: aksrtData/aksrtAdminData/... -> jioData/jioAdminData/...,
  window.AKSRTCaptcha & friends -> window.JioCaptcha, Aksrt* -> Jio*
- Vue components Aks*.vue -> Jio*.vue (15 files renamed + imports)
- CSS classes/data-attrs/custom properties: aksrt- -> jio-
- REST namespace aksrt/v1 -> jio/v1 (incl. payment gateway callbacks)

Storage layer
- DB tables {prefix}aksrt_* -> {prefix}jio_*; SCHEMA_VERSION 3.2.0 -> 3.3.0
  so dbDelta creates the new names on install
- widget id_base aksrt_widget_* -> jio_widget_*
- uninstall.php drop list rebuilt from the actual migrator tables
  (dropped 3 stale entries, added the 17 missing ones)

Removed
- hardcoded https://api.aksrt.com/version.json default update endpoint;
  get_endpoint() now reads the 「更新端点」 setting and do_check() no-ops
  when it is empty

Files renamed: inc/helpers/aksrt-*.php -> jio-*.php (54),
assets/admin/src/components/form/Aks*.vue -> Jio*.vue (15).
Build artifacts regenerated: assets/css/build, assets/js/runtime,
assets/admin/build.

Verified: audit-psr4-paths errors=0, audit-admin-contracts errors=0
(warnings=56, unchanged baseline), 813/813 widget-system tests,
44/44 phpunit, php -l clean across all PHP.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-11-01 20:05:04 +08:00

340 lines
9.8 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=), legacy zib_sift_posts_per_orde.
*
* 排序菜单(.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')) {
/**
* 首页排序菜单,1:1 zib_ajax_option_menu('home')。
*
* 输出 .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')) {
/**
* 排序方式按钮组,1:1 zib_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')) {
/**
* 当前页面链接,1:1 zib_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')) {
/**
* 移除分页参数的链接,1:1 zib_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);
}
}