feat(admin): rebuild page display settings

This commit is contained in:
2026-08-19 00:07:13 +08:00
parent 09b1e8fd35
commit 480e2bf07e
117 changed files with 1815 additions and 9081 deletions
-220
View File
@@ -1,220 +0,0 @@
<?php
/**
* @package AKSRT
*/
namespace AKSRT\Core;
defined('ABSPATH') || exit;
class HomeModules {
const PREVIEW_QUERY_VAR = 'aksrt_preview';
const PREVIEW_TRANSIENT_PREFIX = 'aksrt_home_preview_';
private static $instance = null;
private function __construct() {}
public static function get_instance(): self {
if (null === self::$instance) self::$instance = new self();
return self::$instance;
}
public function init(): void {
add_filter('query_vars', [$this, 'add_query_var']);
add_action('rest_api_init', [$this, 'register_routes']);
}
public function add_query_var(array $vars): array {
$vars[] = self::PREVIEW_QUERY_VAR;
return $vars;
}
public function register_routes(): void {
register_rest_route('aksrt/v1', '/preview/homepage', [
'methods' => 'POST',
'callback' => [$this, 'rest_create_preview'],
'permission_callback' => function () {
return current_user_can('manage_options');
},
]);
}
/**
* 获取并校验模块列表(含预览/默认回退逻辑)。
*/
private static function resolve_modules(): array {
$modules = [];
// 预览模式
$preview_id = get_query_var(self::PREVIEW_QUERY_VAR);
if ($preview_id && current_user_can('manage_options')) {
$cached = get_transient(self::PREVIEW_TRANSIENT_PREFIX . sanitize_key((string) $preview_id));
if (is_array($cached)) {
$modules = $cached;
}
}
if (empty($modules) && class_exists('\AKSRT\Settings\Settings')) {
$modules = \AKSRT\Settings\Settings::get_homepage_modules();
}
// 校验:过滤无效条目,若全部无效则清空以触发默认模板回退
if (!empty($modules)) {
$allowed = self::allowed_types();
$valid = array_filter($modules, function ($row) use ($allowed) {
return is_array($row) && !empty($row['type']) && in_array($row['type'], $allowed, true);
});
$modules = empty($valid) ? [] : array_values($valid);
}
if (empty($modules)) {
$modules = self::get_default_template('resource_standard');
}
return $modules;
}
/**
* 渲染入口(首页 CMS 形态调用)。
*
* 优先级:
* 1. ?aksrt_preview={id} 且管理员 → 读 transient 渲染
* 2. option homepage_modules
* 3. 若空 → 渲染默认模板(resource_standard)
*/
public static function render(): void {
$modules = self::resolve_modules();
echo '<div class="home-modules">';
foreach ($modules as $idx => $row) {
if (!is_array($row) || empty($row['type'])) continue;
$type = sanitize_key((string) $row['type']);
$config = isset($row['config']) && is_array($row['config']) ? $row['config'] : [];
set_query_var('aksrt_module_config', $config);
set_query_var('aksrt_module_index', $idx);
get_template_part('templates/parts/home-modules/module', $type);
}
// 清理
set_query_var('aksrt_module_config', null);
set_query_var('aksrt_module_index', null);
echo '</div>';
}
/**
* 全屏模块类型(不受侧栏约束)。
*/
public static function fullbleed_types(): array {
return [];
}
/**
* 分区渲染:仅渲染指定区域的模块。
*
* @param string $zone 'fullbleed' | 'body'
*/
public static function render_zone(string $zone = 'body'): void {
$modules = self::resolve_modules();
$fullbleed = self::fullbleed_types();
$filtered = array_filter($modules, function ($row) use ($fullbleed, $zone) {
$type = $row['type'] ?? '';
$is_fullbleed = in_array($type, $fullbleed, true);
return $zone === 'fullbleed' ? $is_fullbleed : !$is_fullbleed;
});
if (empty($filtered)) return;
$wrapper_class = $zone === 'fullbleed' ? 'home-modules home-modules--fullbleed' : 'home-modules';
echo '<div class="' . esc_attr($wrapper_class) . '">';
foreach ($filtered as $idx => $row) {
if (!is_array($row) || empty($row['type'])) continue;
$type = sanitize_key((string) $row['type']);
$config = isset($row['config']) && is_array($row['config']) ? $row['config'] : [];
set_query_var('aksrt_module_config', $config);
set_query_var('aksrt_module_index', $idx);
get_template_part('templates/parts/home-modules/module', $type);
}
set_query_var('aksrt_module_config', null);
set_query_var('aksrt_module_index', null);
echo '</div>';
}
/**
* 创建预览会话。
*/
public function rest_create_preview(\WP_REST_Request $request) {
$params = $request->get_json_params();
if (!is_array($params)) $params = $request->get_params();
$modules = isset($params['modules']) && is_array($params['modules']) ? $params['modules'] : [];
$clean = [];
$allowed_types = self::allowed_types();
foreach ($modules as $row) {
if (!is_array($row)) continue;
$type = isset($row['type']) ? sanitize_key((string) $row['type']) : '';
if (!in_array($type, $allowed_types, true)) continue;
$clean[] = [
'type' => $type,
'config' => isset($row['config']) && is_array($row['config']) ? $row['config'] : [],
];
}
$id = wp_generate_uuid4();
set_transient(self::PREVIEW_TRANSIENT_PREFIX . $id, $clean, 30 * MINUTE_IN_SECONDS);
return new \WP_REST_Response([
'preview_id' => $id,
'preview_url' => add_query_arg(self::PREVIEW_QUERY_VAR, $id, home_url('/')),
'expires_in' => 30 * MINUTE_IN_SECONDS,
], 200);
}
public static function allowed_types(): array {
return [
'banner', 'category', 'tabs', 'ranking',
'recent', 'tag_cloud', 'friend_links', 'html', 'hotspot',
'category_recommend_cards',
// Phase 5: 运营型模块
'text_title', 'button_group', 'spacing', 'faq',
'data_grid', 'highlight_cards', 'partner_marquee', 'product_showcase',
// Widget-区等价模块(补齐 zibll widget-user / widget-more 能力)
'user', 'more',
];
}
/**
* 默认模板(3 套预设)。
*/
public static function get_default_template(string $name): array {
switch ($name) {
case 'news':
return [
['type' => 'banner', 'config' => []],
['type' => 'category', 'config' => ['count' => 8, 'columns' => 4]],
['type' => 'category', 'config' => ['count' => 8, 'columns' => 4]],
['type' => 'tag_cloud', 'config' => ['limit' => 30]],
];
case 'mixed':
return [
['type' => 'recent', 'config' => ['count' => 6, 'layout' => 'card']],
['type' => 'category', 'config' => ['count' => 6, 'columns' => 3]],
['type' => 'category', 'config' => ['count' => 6, 'columns' => 3]],
];
case 'resource_standard':
default:
return [
['type' => 'tabs', 'config' => ['count' => 8]],
['type' => 'recent', 'config' => ['count' => 8, 'layout' => 'card']],
['type' => 'ranking', 'config' => ['metric' => 'downloads', 'count' => 6]],
];
}
}
}
-19
View File
@@ -111,7 +111,6 @@ trait ScriptsPart1b {
$enqueue_module('layout');
$enqueue_module('footer');
$enqueue_module('floating-buttons');
$enqueue_module('theme-background');
$enqueue_module('enhancements');
// 4a-b. 全局补充样式(骨架屏、空状态、分页、加载指示器)
@@ -681,24 +680,6 @@ trait ScriptsPart1b {
);
}
// Legacy freeform CMS home modules: only for non-fixed CMS archive previews.
$is_cms_home = ($is_home_or_archive && $is_cms_mode) && !$is_fixed_homepage;
// 管理员预览模式(?aksrt_preview=...):无论站点风格都允许加载模块系统资源
$is_preview = is_user_logged_in() && current_user_can('manage_options') && isset($_GET['aksrt_preview']);
if ($is_cms_home || $is_preview) {
$enqueue_module('home-modules');
$enqueue_module('post-card'); // 模块内复用
wp_enqueue_script(
'aksrt-cms-home-modules',
$base_uri . '/assets/js/cms-home-modules.js',
['loader_js'],
$version,
true
);
}
if ($is_fixed_homepage) {
$enqueue_module('post-card');
$homepage_style_deps = ['_main', 'aksrt-layout', 'aksrt-post-card'];
-1
View File
@@ -69,7 +69,6 @@ class Setup {
add_image_size('aksrt-cover', 1200, 675, true);
add_image_size('aksrt-card', 400, 260, true);
add_image_size('aksrt-thumb', 150, 150, true);
add_image_size('aksrt-banner', 1920, 500, true);
// 内容宽度(用于区块编辑器)
if (!isset($GLOBALS['content_width'])) {
+43 -9
View File
@@ -94,10 +94,10 @@ class TermMeta {
$sidebar_enabled = false;
}
$cover_url = isset($saved['cover_url']) ? esc_url_raw((string) $saved['cover_url']) : '';
if ($cover_url === '' && $taxonomy === 'category' && !empty($global['cover_enabled']) && class_exists('\AKSRT\Settings\Settings')) {
$cover_url = \AKSRT\Settings\Settings::get_category_cover_url($term->term_id);
}
$cover_url = isset($saved['cover_url']) ? esc_url_raw((string) $saved['cover_url']) : '';
if ($cover_url === '' && !empty($global['cover_enabled'])) {
$cover_url = esc_url_raw((string) ($global['default_cover'] ?? ''));
}
$description = isset($saved['description']) ? sanitize_textarea_field((string) $saved['description']) : '';
if ($description === '') {
@@ -110,7 +110,15 @@ class TermMeta {
'show_sidebar' => $sidebar_enabled,
'show_cover' => $header_style === 'cover' && !empty($global['cover_enabled']) && $cover_url !== '',
'show_desc' => $taxonomy === 'post_tag' ? $description !== '' : (!empty($global['desc_enabled']) && $description !== ''),
'filter_bar' => $taxonomy === 'category' && !empty($global['filter_bar']),
'filter_bar' => !empty($global['filter_bar']),
'post_count' => !empty($global['post_count']),
'orderby_enabled' => !empty($global['orderby_enabled']),
'orderby' => sanitize_key((string) ($global['orderby'] ?? 'date')),
'orderby_option'=> is_array($global['orderby_option'] ?? null) ? $global['orderby_option'] : [],
'ajax_category_enabled' => !empty($global['ajax_category_enabled']),
'ajax_tag_enabled' => !empty($global['ajax_tag_enabled']),
'ajax_category' => is_array($global['ajax_category'] ?? null) ? $global['ajax_category'] : [],
'ajax_tag' => is_array($global['ajax_tag'] ?? null) ? $global['ajax_tag'] : [],
'per_page' => max(4, min(48, (int) ($global['per_page'] ?? 12))),
'cover_url' => $cover_url,
'description' => $description,
@@ -153,10 +161,36 @@ class TermMeta {
}
$settings = self::resolve_archive_settings($term);
if (!empty($settings['per_page'])) {
$query->set('posts_per_page', (int) $settings['per_page']);
}
}
if (!empty($settings['per_page'])) {
$query->set('posts_per_page', (int) $settings['per_page']);
}
$tax_query = is_array($query->get('tax_query')) ? $query->get('tax_query') : [];
$filter_cat = isset($_GET['aksrt_filter_cat']) ? absint(wp_unslash($_GET['aksrt_filter_cat'])) : 0;
$filter_tag = isset($_GET['aksrt_filter_tag']) ? absint(wp_unslash($_GET['aksrt_filter_tag'])) : 0;
if ($filter_cat > 0) {
$tax_query[] = ['taxonomy' => 'category', 'field' => 'term_id', 'terms' => [$filter_cat]];
}
if ($filter_tag > 0) {
$tax_query[] = ['taxonomy' => 'post_tag', 'field' => 'term_id', 'terms' => [$filter_tag]];
}
if ($tax_query !== []) {
$query->set('tax_query', $tax_query);
}
$orderby = isset($_GET['orderby']) ? sanitize_key(wp_unslash((string) $_GET['orderby'])) : 'date';
$allowed = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::homepage_allowed_sorts()
: ['modified', 'date', 'views', 'like', 'comment_count', 'favorite', 'rand'];
if (!in_array($orderby, $allowed, true)) {
$orderby = 'date';
}
if (function_exists('aksrt_homepage_apply_named_sort_args')) {
foreach (aksrt_homepage_apply_named_sort_args([], $orderby) as $key => $value) {
$query->set($key, $value);
}
}
}
/**
* Render fields for "Add new term" form.