get_method() === 'GET') { return rest_ensure_response([ 'code' => 'success', 'data' => AKSRT_Settings_Home_Cms::get_homepage_fixed_settings(), ]); } $params = $this->get_json_params($request); if (empty($params)) { $params = $request->get_params(); } if (!is_array($params)) { $params = []; } $allowed_sorts = AKSRT_Settings_Home_Cms::homepage_allowed_sorts(); $settings = []; // --- Zibll main list contract --- if (array_key_exists('home_posts_list_s', $params) || array_key_exists('home_posts_list_enabled', $params)) { $settings['home_posts_list_s'] = array_key_exists('home_posts_list_s', $params) ? !empty($params['home_posts_list_s']) : !empty($params['home_posts_list_enabled']); } if (array_key_exists('home_list1_orderby_s', $params) || array_key_exists('home_list_sort_enabled', $params)) { $settings['home_list1_orderby_s'] = array_key_exists('home_list1_orderby_s', $params) ? !empty($params['home_list1_orderby_s']) : !empty($params['home_list_sort_enabled']); } if (isset($params['home_list1_orderby_option']) && is_array($params['home_list1_orderby_option'])) { $settings['home_list1_orderby_option'] = $this->sanitize_home_list1_orderby_option( $params['home_list1_orderby_option'], $allowed_sorts ); } elseif (isset($params['home_list_sort_options']) && is_array($params['home_list_sort_options'])) { $settings['home_list1_orderby_option'] = $this->sanitize_home_list1_orderby_option( [ 'orderby' => $params['homepage_default_sort'] ?? 'modified', 'options' => $params['home_list_sort_options'], ], $allowed_sorts ); } elseif (isset($params['homepage_default_sort'])) { $default = sanitize_key((string) $params['homepage_default_sort']); if (!in_array($default, $allowed_sorts, true)) { $default = 'modified'; } $settings['home_list1_orderby_option'] = [ 'orderby' => $default, 'options' => array_values(array_unique(array_merge([$default], ['date', 'views', 'likes', 'comments']))), ]; } if (array_key_exists('index_list_title', $params) || array_key_exists('home_list_title', $params)) { $title = array_key_exists('index_list_title', $params) ? (string) $params['index_list_title'] : (string) $params['home_list_title']; $settings['index_list_title'] = sanitize_text_field($title); } if (isset($params['home_exclude_posts']) || isset($params['home_exclude_post_ids'])) { $raw = $params['home_exclude_posts'] ?? $params['home_exclude_post_ids']; $settings['home_exclude_posts'] = $this->sanitize_id_list($raw); } if (isset($params['home_exclude_cats']) || isset($params['home_exclude_category_ids'])) { $raw = $params['home_exclude_cats'] ?? $params['home_exclude_category_ids']; $settings['home_exclude_cats'] = $this->sanitize_id_list($raw); } if (isset($params['home_lists']) || isset($params['homepage_tabs'])) { $raw_tabs = $params['home_lists'] ?? $params['homepage_tabs']; $settings['home_lists'] = $this->sanitize_home_lists($raw_tabs, $allowed_sorts); } // --- AKSRT presentation extras (optional, not Zibll dual-write) --- if (isset($params['homepage_card_style'])) { $settings['homepage_card_style'] = in_array($params['homepage_card_style'], ['default', 'style2', 'style3'], true) ? $params['homepage_card_style'] : 'default'; } if (array_key_exists('homepage_multi_thumb_enabled', $params)) { $settings['homepage_multi_thumb_enabled'] = !empty($params['homepage_multi_thumb_enabled']); } if (isset($params['homepage_multi_thumb_min_images'])) { $settings['homepage_multi_thumb_min_images'] = max(2, min(4, (int) $params['homepage_multi_thumb_min_images'])); } if (array_key_exists('homepage_sidebar_mini_enabled', $params)) { $settings['homepage_sidebar_mini_enabled'] = !empty($params['homepage_sidebar_mini_enabled']); } if (isset($params['homepage_default_view'])) { $settings['homepage_default_view'] = in_array($params['homepage_default_view'], ['list', 'card'], true) ? $params['homepage_default_view'] : 'list'; } if (array_key_exists('homepage_sort_ajax_enabled', $params)) { $settings['homepage_sort_ajax_enabled'] = !empty($params['homepage_sort_ajax_enabled']); } if (array_key_exists('homepage_sticky_tabs_enabled', $params)) { $settings['homepage_sticky_tabs_enabled'] = !empty($params['homepage_sticky_tabs_enabled']); } if (isset($params['homepage_content_model'])) { $settings['homepage_content_model'] = in_array($params['homepage_content_model'], ['post', 'mixed'], true) ? $params['homepage_content_model'] : 'mixed'; } if (array_key_exists('home_list_sort_dropdown', $params)) { $settings['home_list_sort_dropdown'] = !empty($params['home_list_sort_dropdown']); } if (isset($params['homepage_tab_posts_per_page'])) { $settings['homepage_tab_posts_per_page'] = max(1, min(20, (int) $params['homepage_tab_posts_per_page'])); } if (isset($params['homepage_tab_limit'])) { $settings['homepage_tab_limit'] = max(1, min(12, (int) $params['homepage_tab_limit'])); } if (array_key_exists('homepage_module_hero_enabled', $params)) { $settings['homepage_module_hero_enabled'] = !empty($params['homepage_module_hero_enabled']); } if (array_key_exists('homepage_module_banner_enabled', $params)) { $settings['homepage_module_banner_enabled'] = !empty($params['homepage_module_banner_enabled']); } $current = get_option($option_key, []); if (!is_array($current)) { $current = []; } foreach ($settings as $key => $value) { $current[$key] = $value; } // Drop superseded AKSRT-only keys when canonical Zibll keys are written. $superseded = [ 'home_posts_list_enabled', 'home_list_sort_enabled', 'home_list_sort_options', 'home_list_title', 'home_exclude_post_ids', 'home_exclude_category_ids', 'homepage_tabs', 'homepage_default_sort', ]; foreach ($superseded as $dead_key) { if (array_key_exists($dead_key, $current) && ( array_key_exists('home_posts_list_s', $settings) || array_key_exists('home_list1_orderby_s', $settings) || array_key_exists('home_list1_orderby_option', $settings) || array_key_exists('index_list_title', $settings) || array_key_exists('home_exclude_posts', $settings) || array_key_exists('home_exclude_cats', $settings) || array_key_exists('home_lists', $settings) )) { // Only strip keys we actually replaced in this request. } } if (array_key_exists('home_posts_list_s', $settings)) { unset($current['home_posts_list_enabled']); } if (array_key_exists('home_list1_orderby_s', $settings)) { unset($current['home_list_sort_enabled']); } if (array_key_exists('home_list1_orderby_option', $settings)) { unset($current['home_list_sort_options'], $current['homepage_default_sort']); } if (array_key_exists('index_list_title', $settings)) { unset($current['home_list_title']); } if (array_key_exists('home_exclude_posts', $settings)) { unset($current['home_exclude_post_ids']); } if (array_key_exists('home_exclude_cats', $settings)) { unset($current['home_exclude_category_ids']); } if (array_key_exists('home_lists', $settings)) { unset($current['homepage_tabs']); } update_option($option_key, $current); if (class_exists('AKSRT_Settings')) { AKSRT_Settings::flush_settings_cache(); } update_option('aksrt_homepage_schema_version', gmdate('Y-m-d')); delete_transient('aksrt_homepage_cached_images'); do_action('aksrt_homepage_settings_changed'); return rest_ensure_response([ 'code' => 'success', 'message' => __('首页设置已保存', 'aksrt'), 'data' => AKSRT_Settings_Home_Cms::get_homepage_fixed_settings(), ]); } /** * @param mixed $raw * @return int[] */ private function sanitize_id_list($raw): array { if (is_string($raw)) { $parts = preg_split('/[\s,]+/', $raw) ?: []; $raw = $parts; } if (!is_array($raw)) { return []; } return array_values(array_unique(array_filter(array_map('absint', $raw), static fn($id) => $id > 0))); } /** * @param array $option * @param string[] $allowed_sorts * @return array{orderby:string,options:string[]} */ private function sanitize_home_list1_orderby_option(array $option, array $allowed_sorts): array { $orderby = sanitize_key((string) ($option['orderby'] ?? $option['default'] ?? 'modified')); if (!in_array($orderby, $allowed_sorts, true)) { $orderby = 'modified'; } $options = []; $raw_options = $option['options'] ?? $option['orderby_option'] ?? []; if (!is_array($raw_options)) { $raw_options = []; } foreach ($raw_options as $key) { $key = sanitize_key((string) $key); if (in_array($key, $allowed_sorts, true) && !in_array($key, $options, true)) { $options[] = $key; } } if ($options === []) { $options = ['modified', 'date', 'views', 'likes', 'comments']; } if (!in_array($orderby, $options, true)) { array_unshift($options, $orderby); } return [ 'orderby' => $orderby, 'options' => array_values($options), ]; } /** * @param mixed $raw_tabs * @param string[] $allowed_sorts * @return array */ private function sanitize_home_lists($raw_tabs, array $allowed_sorts): array { if (!is_array($raw_tabs)) { return []; } $tabs = []; foreach ($raw_tabs as $tab) { if (!is_array($tab)) { continue; } $term_id = absint($tab['term_id'] ?? 0); if ($term_id <= 0) { continue; } $term = get_term($term_id); if (!$term || is_wp_error($term)) { continue; } $taxonomy = (string) $term->taxonomy; if (!in_array($taxonomy, ['category', 'topics'], true)) { // Allow category always; topics if registered. if ($taxonomy !== 'category') { continue; } } $orderby = sanitize_key((string) ($tab['orderby'] ?? '')); if ($orderby !== '' && !in_array($orderby, $allowed_sorts, true)) { $orderby = 'date'; } $tabs[] = [ 'term_id' => $term_id, 'title' => sanitize_text_field((string) ($tab['title'] ?? '')), 'orderby' => $orderby, 'enabled' => !array_key_exists('enabled', $tab) || !empty($tab['enabled']), ]; } return $tabs; } }