Files
wordpress-theme-aksrt/src/Settings/PostDisplay.php
T
kate522 66f8bb933d feat(admin): replicate reference post-page settings page 1:1
- rewrite post_list.page schema (JS+PHP 2.0.0) with the reference field set:
  three cover types with option fieldsets (image 20-200% / slide CFS_Module::slide
  full config with nested dependsOn / video 0-200%), hide-time, p_indent,
  breadcrumbs (home-text as switcher), prevnext, authordesc, maxheight slider,
  yiyan block, copyright, button toptext, front/after content, related posts
- migrate share_s to share page basic group and post_rewards_* to users
  features rewards group (runtime keys unchanged)
- remap get_post_page_settings() to reference keys, normalize slide cover
  option, hardcode non-reference runtime flags (reading progress, favorites,
  author card), drop dual-path legacy aliases
- wire slide option into aksrt_render_slider (effect/direction/spaceBetween/
  speed/interval/loop/autoplay/nav/pagination, scale vs fixed-height modes),
  clamp image scale 20-200 and video scale 0-200 with 16:9 fallback,
  wire p_indent container class + CSS, resync frontend-tags fallback and
  FacadeBulk fallback, remap PostMetaBox cover keys
- add post-page reference contract test; update stale cover-height and
  user-features contract tests

Validation: node --test 464/464 pass, audit-admin-contracts errors=0,
vite build ok, build-public-assets ok, php -l clean.
2026-09-24 16:13:00 +08:00

600 lines
33 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* @package AKSRT
*/
namespace AKSRT\Settings;
defined('ABSPATH') || exit;
class PostDisplay {
public static function get_post_list_style_settings(): array {
$settings = Settings::get_all();
$list_type = sanitize_key((string) ($settings['list_type'] ?? 'thumb'));
if (!in_array($list_type, ['text', 'thumb', 'thumb_if_has', 'card'], true)) {
$list_type = 'thumb';
}
$list_option = is_array($settings['list_list_option'] ?? null) ? $settings['list_list_option'] : [];
$list_style = sanitize_key((string) ($list_option['style'] ?? 'null'));
$list_style = $list_style === 'style2' ? 'style2' : 'null';
$list_position = sanitize_key((string) ($list_option['img_position'] ?? 'left'));
$list_position = $list_position === 'right' ? 'right' : 'left';
$card_option = is_array($settings['list_card_option'] ?? null) ? $settings['list_card_option'] : [];
$card_style = sanitize_key((string) ($card_option['style'] ?? 'null'));
$card_style = $card_style === 'style3' ? 'style3' : 'null';
$sticky_pages = is_array($settings['sticky_s'] ?? null)
? array_values(array_intersect(array_map('sanitize_key', $settings['sticky_s']), ['home', 'cat', 'tag']))
: ['home', 'cat'];
$badge_types = is_array($settings['list_badge_show'] ?? null)
? array_values(array_intersect(array_map('sanitize_key', $settings['list_badge_show']), ['pay', 'cat', 'tag']))
: ['pay', 'cat', 'tag'];
$card_cat_ids = is_array($settings['list_card_cat'] ?? null)
? array_values(array_filter(array_map('intval', $settings['list_card_cat'])))
: [];
$time_source = sanitize_key((string) ($settings['post_time_source'] ?? 'modified'));
$mobile_meta = sanitize_key((string) ($settings['list_meta_show'] ?? 'like'));
$list_show_type = sanitize_key((string) ($settings['list_show_type'] ?? 'separate'));
$list_orderby = sanitize_key((string) ($settings['list_orderby'] ?? 'modified'));
return [
'list_type' => $list_type,
'list_orderby' => in_array($list_orderby, ['date', 'modified', 'default'], true) ? $list_orderby : 'modified',
'post_time_source' => $time_source === 'date' ? 'date' : 'modified',
'sticky_s' => $sticky_pages,
'target_blank' => !empty($settings['target_blank']),
'item_heading_bold' => !empty($settings['item_heading_bold']),
'post_list_author' => !array_key_exists('post_list_author', $settings) || !empty($settings['post_list_author']),
'post_list_time' => !array_key_exists('post_list_time', $settings) || !empty($settings['post_list_time']),
'paging_ajax_s' => (string) ($settings['paging_ajax_s'] ?? '1') === '1',
'paging_ajax_ias_s' => !array_key_exists('paging_ajax_ias_s', $settings) || !empty($settings['paging_ajax_ias_s']),
'ias_max' => max(0, min(10, (int) ($settings['ias_max'] ?? 3))),
'list_meta_show' => in_array($mobile_meta, ['view', 'like', 'comm'], true) ? $mobile_meta : 'like',
'list_badge_show' => $badge_types,
'list_show_type' => in_array($list_show_type, ['separate', 'no_margin'], true) ? $list_show_type : 'separate',
'list_card_home' => !empty($settings['list_card_home']),
'list_card_tag' => !empty($settings['list_card_tag']),
'list_card_author' => !empty($settings['list_card_author']),
'list_card_cat' => $card_cat_ids,
'list_list_option' => [
'style' => $list_style,
'img_position' => $list_position,
'scale' => max(50, min(200, (int) ($list_option['scale'] ?? 70))),
],
'list_card_option' => [
'style' => $card_style,
'scale' => max(20, min(200, (int) ($card_option['scale'] ?? 70))),
],
'mult_thumb_cat' => is_array($settings['mult_thumb_cat'] ?? null) ? array_values(array_filter(array_map('intval', $settings['mult_thumb_cat']))) : [],
'ajax_trigger' => wp_kses_post((string) ($settings['ajax_trigger'] ?? '<i class="fa fas fa-angle-right"></i>加载更多')),
'ajax_nomore' => wp_kses_post((string) ($settings['ajax_nomore'] ?? '没有更多内容了')),
];
}
public static function get_post_thumb_settings(): array {
$settings = Settings::get_all();
$spare_settings = array_key_exists('spare_thumbnail', $settings)
? (array) $settings['spare_thumbnail']
: [['img' => get_template_directory_uri() . '/assets/img/thumbnail.svg']];
$spare = [];
foreach ($spare_settings as $item) {
$url = is_array($item) ? (string) ($item['img'] ?? '') : '';
if ($url !== '') {
$spare[] = esc_url_raw($url);
}
}
$fit = sanitize_key((string) ($settings['list_thumb_fit'] ?? ''));
return [
'list_thumb_slides_s' => !array_key_exists('list_thumb_slides_s', $settings) || !empty($settings['list_thumb_slides_s']),
'list_thumb_video_s' => !array_key_exists('list_thumb_video_s', $settings) || !empty($settings['list_thumb_video_s']),
'list_thumb_video_mute_s' => !array_key_exists('list_thumb_video_mute_s', $settings) || !empty($settings['list_thumb_video_mute_s']),
'thumb_postfirstimg_s' => !array_key_exists('thumb_postfirstimg_s', $settings) || !empty($settings['thumb_postfirstimg_s']),
'thumb_catimg_s' => !array_key_exists('thumb_catimg_s', $settings) || !empty($settings['thumb_catimg_s']),
'list_thumb_fit' => $fit === 'contain' ? 'contain' : '',
'thumb_postfirstimg_size' => in_array(sanitize_key((string) ($settings['thumb_postfirstimg_size'] ?? 'medium')), ['thumbnail', 'medium', 'large', 'full'], true) ? sanitize_key((string) ($settings['thumb_postfirstimg_size'] ?? 'medium')) : 'medium',
'spare_thumbnail' => $spare,
];
}
public static function get_post_filter_settings(): array {
$settings = Settings::get_all();
return [
'custom_filter_show' => is_array($settings['custom_filter_show'] ?? null) ? array_values(array_intersect(array_map('sanitize_key', $settings['custom_filter_show']), ['cat', 'tag'])) : [],
'custom_filter' => self::sanitize_custom_filter_defs($settings['custom_filter'] ?? []),
'cat_custom_filter' => self::sanitize_cat_custom_filter($settings['cat_custom_filter'] ?? []),
];
}
/**
* 归档页高级筛选配置(参考主题 zib-category.php:zib_get_ajax_option_menu 语义)。
*
* cat 页优先匹配 cat_custom_filter(绑定 cats 含当前 term 时全量覆盖筛选项目);
* 未匹配或 tag 页回退默认配置:custom_filter_show 决定是否显示自定义筛选('all')。
*
* @return array{custom_enabled:bool,custom_filters:mixed,overrides:array<string,mixed>}
*/
public static function get_archive_custom_filter_config(int $term_id, string $page): array {
$cfg = self::get_post_filter_settings();
if ($page === 'cat') {
foreach ($cfg['cat_custom_filter'] as $rule) {
if (in_array($term_id, $rule['cats'], true)) {
return [
'custom_enabled' => $rule['custom_filters'] !== [],
'custom_filters' => $rule['custom_filters'],
'overrides' => [
'cat_s' => $rule['cat_s'],
'cat_lists' => $rule['cat_lists'],
'tag_s' => $rule['tag_s'],
'tag_lists' => $rule['tag_lists'],
'orderby_s' => $rule['orderby_s'],
'orderby_lists' => $rule['orderby_lists'],
],
];
}
}
}
return [
'custom_enabled' => in_array($page, $cfg['custom_filter_show'], true),
'custom_filters' => 'all',
'overrides' => [],
];
}
public static function get_post_page_settings(): array {
$settings = Settings::get_all();
// Canonical render keys (used by aksrt_single_*). Legacy *_s aliases kept for older callers.
$breadcrumbs = !isset($settings['breadcrumbs_single_s']) || !empty($settings['breadcrumbs_single_s']);
// 参考主题语义:switcher 开 = 面包屑用“首页”替代网站名称。
$breadcrumbs_home_replace = !isset($settings['breadcrumbs_home_text']) || !empty($settings['breadcrumbs_home_text']);
$breadcrumbs_home = $breadcrumbs_home_replace ? __('首页', 'aksrt') : (string) get_bloginfo('name');
$prevnext = !isset($settings['post_prevnext_s']) || !empty($settings['post_prevnext_s']);
$authordesc = !isset($settings['post_authordesc_s']) || !empty($settings['post_authordesc_s']);
$hide_time = !empty($settings['post_single_hide_time_s']);
$share_enabled = !isset($settings['share_s']) || !empty($settings['share_s']);
$rewards_enabled = !empty($settings['post_rewards_s']);
$rewards_text = sanitize_text_field((string) ($settings['post_rewards_text'] ?? ''));
if ($rewards_text === '') {
$rewards_text = '赞赏';
}
$reward_amount = static function ($value, string $default): \AKSRT\Ledger\AssetAmount {
try {
return \AKSRT\Ledger\AssetAmount::balance(is_string($value) ? trim($value) : '');
} catch (\InvalidArgumentException $e) {
return \AKSRT\Ledger\AssetAmount::balance($default);
}
};
$rewards_amounts = [];
foreach (explode(',', (string) ($settings['post_rewards_amounts'] ?? '5.00,10.00,20.00,50.00,100.00')) as $value) {
try {
$parsed = \AKSRT\Ledger\AssetAmount::balance(trim($value));
if ($parsed->minor() > 0) {
$rewards_amounts[] = $parsed->decimal();
}
} catch (\InvalidArgumentException $e) {
continue;
}
}
if ($rewards_amounts === []) {
$rewards_amounts = ['5.00', '10.00', '20.00', '50.00', '100.00'];
}
$rewards_min_amount = $reward_amount($settings['post_rewards_min'] ?? '1.00', '1.00');
if ($rewards_min_amount->minor() <= 0) {
$rewards_min_amount = \AKSRT\Ledger\AssetAmount::balance('0.01');
}
$rewards_max_amount = $reward_amount($settings['post_rewards_max'] ?? '1000.00', '1000.00');
if ($rewards_max_amount->minor() < $rewards_min_amount->minor()) {
$rewards_max_amount = $rewards_min_amount;
}
$rewards_min = $rewards_min_amount->decimal();
$rewards_max = $rewards_max_amount->decimal();
// 内容高度限制(参考键 article_maxheight_kg / article_maxheight)。
$maxheight_enabled = !empty($settings['article_maxheight_kg']);
$maxheight = max(0, (int) ($settings['article_maxheight'] ?? 1000));
if ($maxheight <= 0) {
$maxheight = 1000;
}
// 封面(参考键 article_*_cover + option fieldset)。
$image_cover_option = is_array($settings['article_image_cover_option'] ?? null) ? $settings['article_image_cover_option'] : [];
$image_cover_scale = max(20, min(200, (int) ($image_cover_option['scale'] ?? 35)));
$video_cover_option = is_array($settings['article_video_cover_option'] ?? null) ? $settings['article_video_cover_option'] : [];
$video_cover_scale = max(0, min(200, (int) ($video_cover_option['scale'] ?? 0)));
$slide_option_raw = is_array($settings['article_slide_cover_option'] ?? null) ? $settings['article_slide_cover_option'] : [];
$slide_option = self::normalize_slide_cover_option($slide_option_raw);
// 图片灯箱(参考主题 imagelightbox*):pc_s/m_s 复选框归并为单一开关。
$lightbox_type = sanitize_key((string) ($settings['imagelightbox_type'] ?? 'group'));
if (!in_array($lightbox_type, ['group', 'alone'], true)) {
$lightbox_type = 'group';
}
$lightbox_button = static function ($value, bool $default): bool {
if (!is_array($value)) {
return $default;
}
return !empty($value['pc_s']) || !empty($value['m_s']);
};
$lightbox_thumbs = $lightbox_button($settings['imagelightbox_thumbs_s'] ?? null, true);
$lightbox_zoom = $lightbox_button($settings['imagelightbox_zoom_s'] ?? null, true);
$lightbox_full = $lightbox_button($settings['imagelightbox_full_s'] ?? null, true);
$lightbox_down = $lightbox_button($settings['imagelightbox_down_s'] ?? null, true);
$lightbox_play = $lightbox_button($settings['imagelightbox_play_s'] ?? null, true);
// 文章预置参数:新建文章时按区间随机填入浏览/点赞(参考主题 post_default_mate)。
$default_mate = is_array($settings['post_default_mate'] ?? null) ? $settings['post_default_mate'] : [];
$views_range = is_array($default_mate['views'] ?? null) ? $default_mate['views'] : [];
$like_range = is_array($default_mate['like'] ?? null) ? $default_mate['like'] : [];
$related_style = sanitize_key((string) ($settings['post_related_type'] ?? 'img'));
if (!in_array($related_style, ['img', 'list', 'text'], true)) {
$related_style = 'img';
}
$related_orderby = sanitize_key((string) ($settings['post_related_orderby'] ?? 'views'));
if (!in_array($related_orderby, ['modified', 'date', 'views', 'like', 'comment_count', 'favorite', 'rand'], true)) {
$related_orderby = 'views';
}
return [
// 参考主题无阅读进度条配置:功能常开(字段已随 1:1 复刻移除)。
'reading_progress' => true,
// Canonical + legacy breadcrumbs.
'breadcrumbs' => $breadcrumbs,
'breadcrumbs_home' => $breadcrumbs_home,
'breadcrumbs_home_replace' => $breadcrumbs_home_replace,
'breadcrumbs_single_s' => $breadcrumbs,
'breadcrumbs_home_text' => $breadcrumbs_home_replace,
'breadcrumbs_single_text' => true,
// Canonical + legacy prev/next / time; canonical author card flag.
'prevnext' => $prevnext,
'prevnext_s' => $prevnext,
'authordesc' => $authordesc,
'hide_time' => $hide_time,
'single_hide_time_s' => $hide_time,
'p_indent' => !empty($settings['post_p_indent_s']),
'article_image_cover' => !isset($settings['article_image_cover']) || !empty($settings['article_image_cover']),
'article_image_cover_scale'=> $image_cover_scale,
'article_slide_cover' => !isset($settings['article_slide_cover']) || !empty($settings['article_slide_cover']),
'article_slide_cover_option' => $slide_option,
'article_video_cover' => !isset($settings['article_video_cover']) || !empty($settings['article_video_cover']),
'article_video_cover_scale'=> $video_cover_scale,
'lightbox' => !isset($settings['imagelightbox']) || !empty($settings['imagelightbox']),
'lightbox_type' => $lightbox_type,
'lightbox_thumbs' => $lightbox_thumbs,
'lightbox_zoom' => $lightbox_zoom,
'lightbox_full' => $lightbox_full,
'lightbox_down' => $lightbox_down,
'lightbox_play' => $lightbox_play,
'related_enabled' => !isset($settings['post_related_s']) || !empty($settings['post_related_s']),
'related_count' => max(4, min(12, (int) ($settings['post_related_n'] ?? 6))),
'related_style' => $related_style,
'related_orderby' => $related_orderby,
'related_title' => sanitize_text_field((string) ($settings['related_title'] ?? '相关推荐')),
'like_enabled' => !isset($settings['post_like_s']) || !empty($settings['post_like_s']),
// 参考主题无收藏开关:收藏按钮常显(字段已随 1:1 复刻移除)。
'favorite_enabled' => true,
// Canonical + legacy share.
'share_enabled' => $share_enabled,
'share_s' => $share_enabled,
// Author card posts(参考主题无独立配置:保持默认)。
'author_card_limit' => 6,
'author_card_orderby' => 'views',
'author_card_style' => 'card',
// On-site wallet tipping(配置位于用户功能页,参考主题同位置)。
'rewards_enabled' => $rewards_enabled,
'post_rewards_s' => $rewards_enabled,
'rewards_text' => $rewards_text,
'rewards_btn_text' => $rewards_text,
'rewards_amounts' => $rewards_amounts,
'rewards_custom' => !isset($settings['post_rewards_custom']) || !empty($settings['post_rewards_custom']),
'rewards_min' => $rewards_min,
'rewards_max' => $rewards_max,
// Content max height (legacy article_maxheight_kg / article_maxheight).
'maxheight_enabled' => $maxheight_enabled,
'maxheight' => $maxheight,
'copyright_enabled' => !isset($settings['post_copyright_s']) || !empty($settings['post_copyright_s']),
'copyright_text' => (string) ($settings['post_copyright'] ?? '文章版权归作者所有,未经允许请勿转载。'),
'button_toptext' => sanitize_text_field((string) ($settings['post_button_toptext'] ?? '喜欢就支持一下吧')),
'front_content' => (string) ($settings['post_front_content'] ?? ''),
'after_content' => (string) ($settings['post_after_content'] ?? ''),
'yiyan_header' => !empty($settings['yiyan_single_content_header']),
'yiyan_footer' => !empty($settings['yiyan_single_content_footer']),
'yiyan_box' => !empty($settings['yiyan_single_box']),
'default_views_min' => max(0, min(9999, (int) ($views_range['min'] ?? 20))),
'default_views_max' => max(1, min(9999, (int) ($views_range['max'] ?? 50))),
'default_likes_min' => max(0, min(9999, (int) ($like_range['min'] ?? 5))),
'default_likes_max' => max(1, min(9999, (int) ($like_range['max'] ?? 15))),
'article_nav' => !isset($settings['article_nav']) || !empty($settings['article_nav']),
'article_nav_mobile' => !isset($settings['article_nav_mobile_nav_s']) || !empty($settings['article_nav_mobile_nav_s']),
];
}
/**
* 幻灯片封面配置归一化(参考主题 CFS_Module::slide 全字段)。
*/
private static function normalize_slide_cover_option(array $raw): array {
$effect = sanitize_key((string) ($raw['effect'] ?? 'slide'));
if (!in_array($effect, ['slide', 'fade', 'cube', 'coverflow', 'flip'], true)) {
$effect = 'slide';
}
$direction = sanitize_key((string) ($raw['direction'] ?? 'horizontal')) === 'vertical' ? 'vertical' : 'horizontal';
return [
'direction' => $direction,
'loop' => !empty($raw['loop']),
'button' => !empty($raw['button']),
'pagination' => !empty($raw['pagination']),
'effect' => $effect,
'scale_height' => !empty($raw['scale_height']),
'scale' => max(10, min(300, (int) ($raw['scale'] ?? 35))),
'auto_height' => !empty($raw['auto_height']),
'max_height' => max(120, min(800, (int) ($raw['max_height'] ?? 500))),
'min_height' => max(100, min(500, (int) ($raw['min_height'] ?? 180))),
'pc_height' => max(120, min(800, (int) ($raw['pc_height'] ?? 380))),
'm_height' => max(100, min(500, (int) ($raw['m_height'] ?? 180))),
'spacebetween' => max(0, min(500, (int) ($raw['spacebetween'] ?? 15))),
'speed' => max(0, min(3000, (int) ($raw['speed'] ?? 0))),
'autoplay' => !empty($raw['autoplay']),
'interval' => max(0, min(20, (int) ($raw['interval'] ?? 4))),
];
}
public static function get_share_settings(): array {
$settings = Settings::get_all();
return [
'enabled' => !isset($settings['share_s']) || !empty($settings['share_s']),
'wechat_appid' => sanitize_text_field((string) ($settings['wechat_share_appid'] ?? '')),
'wechat_timestamp' => sanitize_text_field((string) ($settings['wechat_share_timestamp'] ?? '')),
'wechat_nonce' => sanitize_text_field((string) ($settings['wechat_share_nonce'] ?? '')),
'wechat_signature' => sanitize_text_field((string) ($settings['wechat_share_signature'] ?? '')),
'poster_enabled' => !empty($settings['post_share_poster_s']),
'poster_qr_enabled' => !empty($settings['post_share_poster_qr_s']),
'poster_bg' => (string) ($settings['post_share_poster_bg'] ?? ''),
'social_items' => is_array($settings['social_share_items'] ?? null) ? $settings['social_share_items'] : [],
'copy_link_enabled' => !isset($settings['post_share_copylink_s']) || !empty($settings['post_share_copylink_s']),
];
}
public static function get_submit_settings(): array {
$settings = Settings::get_all();
$title_limit = is_array($settings['post_article_title_strlen_limit'] ?? null)
? $settings['post_article_title_strlen_limit']
: [];
$title_min = max(1, min(100, (int) ($title_limit['min'] ?? 5)));
$title_max = max(5, min(200, (int) ($title_limit['max'] ?? 30)));
if ($title_max < $title_min) {
$title_max = $title_min;
}
// 付费金额限制:现金/积分各自独立,0 表示不限制。
$pay_price_limit = is_array($settings['post_article_pay_price_limit'] ?? null)
? $settings['post_article_pay_price_limit']
: [];
$cash_limit = is_array($pay_price_limit['price'] ?? null) ? $pay_price_limit['price'] : [];
$points_limit = is_array($pay_price_limit['points'] ?? null) ? $pay_price_limit['points'] : [];
return [
'enabled' => !isset($settings['post_article_s']) || !empty($settings['post_article_s']),
'btn_text' => sanitize_text_field((string) ($settings['post_article_btn_txte'] ?? '')) ?: __('发布文章', 'aksrt'),
'title_min' => $title_min,
'title_max' => $title_max,
'title_placeholder' => sanitize_text_field((string) ($settings['post_t_placeholder'] ?? '')) ?: __('请输入标题', 'aksrt'),
'content_placeholder' => sanitize_textarea_field((string) ($settings['post_c_placeholder'] ?? '')) ?: __('请输入内容', 'aksrt'),
'allowed_cat_ids' => is_array($settings['post_article_cat'] ?? null) ? array_values(array_filter(array_map('intval', $settings['post_article_cat']))) : [],
'default_user_id' => max(0, (int) ($settings['post_article_user'] ?? 1)),
'allow_tags' => !isset($settings['post_article_tag']) || !empty($settings['post_article_tag']),
'custom_filter_s' => !isset($settings['post_article_custom_filter_s']) || !empty($settings['post_article_custom_filter_s']),
'pay_free_s' => !isset($settings['post_article_pay_free_s']) || !empty($settings['post_article_pay_free_s']),
'pay_price_min' => max(0, (int) ($cash_limit['min'] ?? 0)),
'pay_price_max' => max(0, (int) ($cash_limit['max'] ?? 0)),
'pay_points_min' => max(0, (int) ($points_limit['min'] ?? 0)),
'pay_points_max' => max(0, (int) ($points_limit['max'] ?? 0)),
'pay_vip_price_s' => !isset($settings['post_article_pay_vip_price_s']) || !empty($settings['post_article_pay_vip_price_s']),
'pay_vip_1_enabled' => !empty($settings['pay_user_vip_1_s']),
'pay_vip_2_enabled' => !empty($settings['pay_user_vip_2_s']),
'pay_vip_1_discount' => max(0, min(100, (int) ($settings['post_article_pay_vip_1_discount'] ?? 100))),
'pay_vip_2_discount' => max(0, min(100, (int) ($settings['post_article_pay_vip_2_discount'] ?? 100))),
];
}
public static function get_comment_display_settings(): array {
$settings = Settings::get_all();
// 评论排序由 WP 讨论设置决定(与参考主题一致:主题不再提供排序配置)。
$order = sanitize_key((string) get_option('comment_order', 'desc'));
if (!in_array($order, ['asc', 'desc'], true)) {
$order = 'desc';
}
$submit_text = trim((string) ($settings['comment_submit_text'] ?? ''));
$box_placeholder = trim((string) ($settings['comment_text'] ?? ''));
$title = trim((string) ($settings['comment_title'] ?? ''));
return [
'order' => $order,
'pagination_mode' => self::get_comment_pagination_mode(),
'paging_ajax_ias' => !isset($settings['comment_paging_ajax_ias_s']) || !empty($settings['comment_paging_ajax_ias_s']),
'paging_ajax_ias_max'=> max(0, min(10, (int) ($settings['comment_paging_ajax_ias_max'] ?? 3))),
'require_login' => Settings::comment_requires_registration(),
'box_hide' => sanitize_key((string) ($settings['comment_box_hide'] ?? '')),
'toolbar' => [
'code' => !isset($settings['comment_code']) || !empty($settings['comment_code']),
'image' => !isset($settings['comment_img']) || !empty($settings['comment_img']),
'link' => !empty($settings['comment_link']),
'upload_img' => (!isset($settings['comment_img']) || !empty($settings['comment_img']))
&& !empty($settings['comment_upload_img'])
&& is_user_logged_in()
&& function_exists('aksrt_user_can')
&& aksrt_user_can('upload_comment_image', get_current_user_id()),
],
'like_enabled' => !isset($settings['comment_like_s']) || !empty($settings['comment_like_s']),
'author_tag' => !isset($settings['comment_author_tag']) || !empty($settings['comment_author_tag']),
'corderby' => !isset($settings['comment_corderby']) || !empty($settings['comment_corderby']),
'user_edit' => !isset($settings['user_edit_comment']) || !empty($settings['user_edit_comment']),
'submit_text' => $submit_text !== '' ? $submit_text : __('提交评论', 'aksrt'),
'box_placeholder' => $box_placeholder !== '' ? $box_placeholder : __('说点什么吧...', 'aksrt'),
'title' => $title !== '' ? $title : __('评论', 'aksrt'),
'close_comments' => !empty($settings['close_comments']),
'user_url' => !empty($settings['comment_user_url_s']),
];
}
public static function apply_list_orderby($query): void {
if (is_admin() || !($query instanceof \WP_Query) || !$query->is_main_query()) {
return;
}
if (!$query->is_home() && !$query->is_archive()) {
return;
}
if ($query->get('orderby')) {
return;
}
$orderby = sanitize_key((string) (self::get_post_list_style_settings()['list_orderby'] ?? 'modified'));
if ($orderby === '' || $orderby === 'default') {
return;
}
if (in_array($orderby, ['date', 'modified'], true)) {
$query->set('orderby', $orderby);
$query->set('order', 'DESC');
}
}
public static function apply_list_per_page($query): void {
unset($query);
}
public static function get_archive_layout(): string {
$settings = self::get_post_list_style_settings();
return ($settings['list_type'] ?? 'thumb') === 'card' ? 'card' : 'list';
}
public static function get_post_pagination_mode(): string {
$settings = Settings::get_all();
if (!self::is_list_paging_ajax($settings)) {
return 'numbered';
}
return self::is_list_paging_ajax_ias($settings) ? 'infinite' : 'load_more';
}
/**
* Whether list pagination uses AJAX append (legacy paging_ajax_s).
*
* @param array<string,mixed>|null $settings
*/
public static function is_list_paging_ajax(?array $settings = null): bool {
$settings = $settings ?? Settings::get_all();
$raw = $settings['paging_ajax_s'] ?? '1';
if (is_bool($raw)) {
return $raw;
}
return (string) $raw === '1' || (string) $raw === 'true';
}
/**
* Whether AJAX list paging auto-loads on scroll (legacy paging_ajax_ias_s).
*
* @param array<string,mixed>|null $settings
*/
public static function is_list_paging_ajax_ias(?array $settings = null): bool {
$settings = $settings ?? Settings::get_all();
if (!self::is_list_paging_ajax($settings)) {
return false;
}
return !array_key_exists('paging_ajax_ias_s', $settings) || !empty($settings['paging_ajax_ias_s']);
}
public static function get_comment_pagination_mode(): string {
// 与参考主题一致:ajax_lists = AJAX 追加列表翻页,其余为数字翻页按钮。
$mode = (string) Settings::get_value('comment_paginate_type', 'default');
return $mode === 'ajax_lists' ? 'ajax_lists' : 'numbered';
}
private static function sanitize_custom_filter_defs($raw): array {
if (!is_array($raw)) {
return [];
}
$result = [];
foreach ($raw as $item) {
if (!is_array($item)) continue;
$name = sanitize_text_field((string) ($item['name'] ?? ''));
$key = sanitize_key((string) ($item['key'] ?? ''));
if ($name === '' || $key === '') continue;
$vals = [];
foreach ((array) ($item['vals'] ?? []) as $val) {
if (!is_array($val)) continue;
$vkey = sanitize_key((string) ($val['key'] ?? ''));
$vname = sanitize_text_field((string) ($val['name'] ?? ''));
if ($vkey === '' || $vname === '') continue;
$vals[] = ['key' => $vkey, 'name' => $vname];
}
$result[] = ['name' => $name, 'key' => $key, 'vals' => $vals];
}
return $result;
}
private static function sanitize_cat_custom_filter($raw): array {
if (!is_array($raw)) {
return [];
}
$result = [];
foreach ($raw as $item) {
if (!is_array($item)) continue;
$cats = is_array($item['cats'] ?? null) ? array_values(array_filter(array_map('absint', $item['cats']), static fn($id) => $id > 0)) : [];
if (empty($cats)) continue;
$result[] = [
'cats' => $cats,
'cat_s' => !empty($item['cat_s']),
'cat_lists' => self::sanitize_filter_option_lists($item['cat_lists'] ?? []),
'tag_s' => !empty($item['tag_s']),
'tag_lists' => self::sanitize_filter_option_lists($item['tag_lists'] ?? []),
'orderby_s' => !empty($item['orderby_s']),
'orderby_lists' => self::sanitize_filter_orderby_lists($item['orderby_lists'] ?? []),
'custom_filters' => is_array($item['custom_filters'] ?? null)
? array_values(array_unique(array_filter(array_map('sanitize_key', $item['custom_filters']))))
: [],
];
}
return $result;
}
/**
* 归档筛选按钮列表 fieldset(lists + dropdown,参考主题 CFS_Module::ajax_but)。
*/
private static function sanitize_filter_option_lists($raw): array {
$option = is_array($raw) ? $raw : [];
$lists = is_array($option['lists'] ?? null) ? array_values(array_filter(array_map('absint', $option['lists']), static fn($id) => $id > 0)) : [];
return [
'lists' => $lists,
'dropdown' => !empty($option['dropdown']),
];
}
/**
* 排序方式 fieldset(lists + dropdown,参考主题 CFS_Module::orderby)。
*/
private static function sanitize_filter_orderby_lists($raw): array {
$option = is_array($raw) ? $raw : [];
$allowed = Settings::homepage_allowed_sorts();
$lists = is_array($option['lists'] ?? null)
? array_values(array_intersect(array_map('sanitize_key', $option['lists']), $allowed))
: [];
if ($lists === []) {
$lists = ['modified', 'views', 'like', 'comment_count'];
}
return [
'lists' => $lists,
'dropdown' => !empty($option['dropdown']),
];
}
}