Files
wordpress-theme-aksrt/src/Settings/PostDisplay.php
T
kate522 4d6b2c6045 fix(admin): replicate reference post-feature settings page 1:1
- Rewrite post_list/feature schema (PHP + JS) with the reference theme's
  field set, order, defaults and dependencies; print/PDF toggles dropped
- Move the migrated reference fields off the post-page schema (lightbox
  six switches, post like, default view/like ranges) to kill the dual path
- Map PostDisplay::get_post_page_settings() to the new keys without legacy
  fallback: imagelightbox* (pc_s/m_s checkboxes merged), post_like_s,
  post_default_mate ranges, article_nav + article_nav_mobile_nav_s
- Wire article_nav runtime: content data-nav/no-nav gate with per-post
  override, mobile drawer TOC box, imgbox_type config
- Seed random views/likes on new posts from post_default_mate ranges
- Keep the print stylesheet enqueued unconditionally (no switch); drop the
  dead print/PDF settings and needs_print requirement
- Add post-feature settings reference contract test
2026-09-24 14:29:04 +08:00

522 lines
30 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 [
'enabled' => !empty($settings['filter_enabled']),
'categories' => !isset($settings['filter_categories']) || !empty($settings['filter_categories']),
'tags' => !empty($settings['filter_tags']),
'sort' => !isset($settings['filter_sort']) || !empty($settings['filter_sort']),
'ajax_enabled' => !empty($settings['filter_ajax_enabled']),
'cat_show_cat' => !isset($settings['filter_cat_show_cat']) || !empty($settings['filter_cat_show_cat']),
'cat_show_tag' => !empty($settings['filter_cat_show_tag']),
'tag_show_cat' => !isset($settings['filter_tag_show_cat']) || !empty($settings['filter_tag_show_cat']),
'tag_show_tag' => !empty($settings['filter_tag_show_tag']),
'custom_filter_show' => is_array($settings['custom_filter_show'] ?? null) ? 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'] ?? []),
];
}
public static function get_filter_config_for_category(int $term_id): array {
$cfg = self::get_post_filter_settings();
foreach ($cfg['cat_custom_filter'] as $rule) {
if (in_array($term_id, $rule['cats'], true)) {
return $rule;
}
}
return [
'cats' => [],
'cat_s' => $cfg['cat_show_cat'],
'tag_s' => $cfg['cat_show_tag'],
'orderby_s' => $cfg['sort'],
'custom_filters' => [],
];
}
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']);
$breadcrumbs_home = sanitize_text_field((string) ($settings['breadcrumbs_home_text'] ?? '首页'));
// legacy breadcrumbs_single_text: true → last crumb is "正文"; false → full title.
$breadcrumbs_single_text = !isset($settings['breadcrumbs_single_text']) || !empty($settings['breadcrumbs_single_text']);
$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();
$author_card_limit = max(0, min(12, (int) ($settings['post_author_card_limit'] ?? 6)));
$author_card_orderby = sanitize_key((string) ($settings['post_author_card_orderby'] ?? 'views'));
if (!in_array($author_card_orderby, ['views', 'date', 'modified', 'comment_count'], true)) {
$author_card_orderby = 'views';
}
$author_card_style = sanitize_key((string) ($settings['post_author_card_style'] ?? 'card'));
if (!in_array($author_card_style, ['card', 'list'], true)) {
$author_card_style = 'card';
}
$maxheight_enabled = !empty($settings['article_maxheight_kg'])
|| !empty($settings['post_article_maxheight_kg'])
|| !empty($settings['post_maxheight_kg']);
$maxheight = max(0, (int) ($settings['article_maxheight'] ?? $settings['post_article_maxheight'] ?? $settings['post_maxheight'] ?? 1000));
if ($maxheight <= 0) {
$maxheight = 1000;
}
// 图片灯箱(参考主题 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 [
'reading_progress' => !isset($settings['post_reading_progress']) || !empty($settings['post_reading_progress']),
// Canonical + legacy breadcrumbs.
'breadcrumbs' => $breadcrumbs,
'breadcrumbs_home' => $breadcrumbs_home,
'breadcrumbs_single_s' => $breadcrumbs,
'breadcrumbs_home_text' => $breadcrumbs_home,
'breadcrumbs_single_text' => $breadcrumbs_single_text,
// 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,
'article_image_cover' => !isset($settings['post_article_image_cover']) || !empty($settings['post_article_image_cover']),
'article_image_cover_scale'=> max(10, min(100, (int) ($settings['post_article_image_cover_scale'] ?? 35))),
'article_slide_cover' => !isset($settings['post_article_slide_cover']) || !empty($settings['post_article_slide_cover']),
'article_video_cover' => !isset($settings['post_article_video_cover']) || !empty($settings['post_article_video_cover']),
'article_video_cover_scale'=> max(20, min(100, (int) ($settings['post_article_video_cover_scale'] ?? 56))),
'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']),
'favorite_enabled' => !isset($settings['post_favorite_enabled']) || !empty($settings['post_favorite_enabled']),
// Canonical + legacy share.
'share_enabled' => $share_enabled,
'share_s' => $share_enabled,
// Author card posts (legacy user_card_box defaults).
'author_card_limit' => $author_card_limit,
'author_card_orderby' => $author_card_orderby,
'author_card_style' => $author_card_style,
// 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_enabled']) || !empty($settings['post_copyright_enabled']),
'copyright_text' => (string) ($settings['post_copyright_text'] ?? ''),
'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['post_yiyan_header']),
'yiyan_footer' => !empty($settings['post_yiyan_footer']),
'yiyan_box' => !empty($settings['post_yiyan_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']),
];
}
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;
$filters_raw = trim((string) ($item['custom_filters'] ?? ''));
$filters = $filters_raw === '' ? [] : array_values(array_filter(array_map('sanitize_key', preg_split('/[,,\s]+/', $filters_raw) ?: [])));
$result[] = [
'cats' => $cats,
'cat_s' => !isset($item['cat_s']) || !empty($item['cat_s']),
'tag_s' => !empty($item['tag_s']),
'orderby_s' => !isset($item['orderby_s']) || !empty($item['orderby_s']),
'custom_filters' => $filters,
];
}
return $result;
}
}