Files
wordpress-theme-aksrt/inc/helpers/posts-renderer/config.php
T
2026-08-23 18:01:56 +08:00

255 lines
9.4 KiB
PHP

<?php
/**
* legacy 9.0 article-list configuration and image resolution.
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
if (!function_exists('aksrt_renderer_defaults')) {
/** @return array<string,mixed> */
function aksrt_renderer_defaults(): array {
return [
'type' => 'auto',
'no_author' => false,
'no_margin' => false,
'is_mult_thumb' => false,
'is_no_thumb' => false,
'is_card' => false,
'term_id' => 0,
'is_category' => is_category(),
'is_search' => is_search(),
'is_home' => is_home() || is_front_page(),
'is_author' => is_author(),
'is_tag' => is_tag(),
'highlight_query' => '',
'wrapper_class' => '',
'skip_wrapper' => false,
];
}
}
if (!function_exists('aksrt_renderer_config')) {
/**
* Resolve the one canonical legacy list/card decision matrix.
*
* @param array<string,mixed> $args Caller arguments.
* @return array<string,mixed>
*/
function aksrt_renderer_config(array $args = []): array {
$args = wp_parse_args($args, aksrt_renderer_defaults());
$list = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::get_post_list_style_settings()
: [];
$thumb = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::get_post_thumb_settings()
: [];
$list_type = sanitize_key((string) ($list['list_type'] ?? 'thumb'));
$forced_type = sanitize_key((string) $args['type']);
$is_forced = in_array($forced_type, ['list', 'card'], true);
$is_card = $forced_type === 'card' || (!$is_forced && !empty($args['is_card']));
if (!$is_forced && !$is_card && $list_type === 'card') {
$is_card = true;
}
if (!$is_forced && !$is_card) {
$is_card = (!empty($args['is_tag']) && !empty($list['list_card_tag']))
|| (!empty($args['is_home']) && !empty($list['list_card_home']))
|| (!empty($args['is_author']) && !empty($list['list_card_author']));
}
if (!$is_forced && !$is_card && !empty($args['is_category'])) {
$term_id = absint($args['term_id']);
$is_card = $term_id > 0 && in_array($term_id, (array) ($list['list_card_cat'] ?? []), true);
}
$list_option = (array) ($list['list_list_option'] ?? []);
$card_option = (array) ($list['list_card_option'] ?? []);
$style = sanitize_key((string) (($is_card ? $card_option : $list_option)['style'] ?? 'null'));
if (!in_array($style, ['style2', 'style3'], true)) {
$style = '';
}
$args['mode'] = $is_card ? 'card' : 'list';
$args['list_type'] = $list_type;
$args['style'] = $style;
$args['img_position'] = sanitize_key((string) ($list_option['img_position'] ?? 'left')) === 'right' ? 'right' : 'left';
$args['no_margin'] = !empty($args['no_margin']) || (($list['list_show_type'] ?? 'separate') === 'no_margin');
$args['no_author'] = !empty($args['no_author']) || !empty($args['is_author']);
$args['target_blank'] = !empty($list['target_blank']);
$args['heading_bold'] = !empty($list['item_heading_bold']);
$args['show_author'] = !empty($list['post_list_author']);
$args['show_time'] = !empty($list['post_list_time']);
$args['time_source'] = (string) ($list['post_time_source'] ?? 'modified');
$args['list_orderby'] = (string) ($list['list_orderby'] ?? 'modified');
$args['badge_types'] = (array) ($list['list_badge_show'] ?? ['pay', 'cat', 'tag']);
$args['mult_thumb_cat'] = (array) ($list['mult_thumb_cat'] ?? []);
$args['thumb'] = $thumb;
$args['allow_conditional_layout'] = aksrt_is_show_sidebar() || wp_is_mobile();
$page = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::get_post_page_settings()
: [];
$comments = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::get_comment_display_settings()
: [];
$args['show_like'] = !array_key_exists('like_enabled', $page) || !empty($page['like_enabled']);
$args['close_comments'] = !empty($comments['close_comments']);
return $args;
}
}
if (!function_exists('aksrt_renderer_highlight_text')) {
function aksrt_renderer_highlight_text(string $text, string $query): string {
$text = wp_strip_all_tags($text);
$query = trim($query);
if ($query === '' || $text === '') {
return esc_html($text);
}
$words = array_values(array_unique(array_filter(preg_split('/\s+/u', $query) ?: [])));
if (!$words) {
return esc_html($text);
}
$pattern = '/' . implode('|', array_map(static fn(string $word): string => preg_quote($word, '/'), $words)) . '/iu';
return (string) preg_replace($pattern, '<span class="focus-color">$0</span>', esc_html($text));
}
}
if (!function_exists('aksrt_renderer_title_markup')) {
function aksrt_renderer_title_markup(array $data, array $cfg): string {
$title = aksrt_renderer_highlight_text((string) $data['title'], (string) ($cfg['highlight_query'] ?? ''));
if ((string) ($data['subtitle'] ?? '') !== '') {
$title .= '<span class="focus-color"> ' . esc_html((string) $data['subtitle']) . '</span>';
}
return $title;
}
}
if (!function_exists('aksrt_renderer_excerpt_markup')) {
function aksrt_renderer_excerpt_markup(array $data, array $cfg): string {
return aksrt_renderer_highlight_text((string) $data['excerpt'], (string) ($cfg['highlight_query'] ?? ''));
}
}
if (!function_exists('aksrt_renderer_curated_images')) {
/** @return string[] */
function aksrt_renderer_curated_images(int $post_id, string $size, int $limit = 8): array {
$raw = class_exists('\AKSRT\Core\PostMetaBox')
? (string) \AKSRT\Core\PostMetaBox::get_meta_value($post_id, 'featured_slide', '')
: '';
$images = [];
foreach (array_filter(preg_split('/[\s,]+/', $raw) ?: []) as $token) {
$image = aksrt_renderer_sized_image((string) $token, $size);
if ($image !== '') {
$images[] = $image;
}
}
return array_slice($images, 0, $limit);
}
}
if (!function_exists('aksrt_renderer_sized_image')) {
function aksrt_renderer_sized_image(string $image, string $size): string {
$image = trim($image);
if ($image === '') {
return '';
}
$attachment_id = ctype_digit($image) ? (int) $image : 0;
if ($attachment_id === 0) {
$lookup_url = (string) preg_replace('/\?.*$/', '', $image);
$attachment_id = attachment_url_to_postid($lookup_url);
if ($attachment_id === 0) {
$full_url = (string) preg_replace('/-\d+x\d+(?=\.[^.]+$)/', '', $lookup_url);
$attachment_id = attachment_url_to_postid($full_url);
}
}
if ($attachment_id > 0) {
$sized = wp_get_attachment_image_url($attachment_id, $size);
if (is_string($sized) && $sized !== '') {
return esc_url_raw($sized);
}
}
return esc_url_raw($image);
}
}
if (!function_exists('aksrt_renderer_content_images')) {
/** @return string[] */
function aksrt_renderer_content_images(int $post_id, string $size): array {
$content = strip_shortcodes((string) get_post_field('post_content', $post_id, 'raw'));
if ($content === '' || !preg_match_all('/<img.*?(?: |\t|\r|\n)?src=[\'\"]?(.+?)[\'\"]?(?:(?: |\t|\r|\n)+.*?)?>/sim', $content, $matches)) {
return [];
}
$images = [];
foreach ((array) ($matches[1] ?? []) as $image) {
$sized = aksrt_renderer_sized_image((string) $image, $size);
if ($sized !== '') {
$images[] = $sized;
}
}
return $images;
}
}
if (!function_exists('aksrt_renderer_category_image')) {
function aksrt_renderer_category_image(int $post_id, string $size): string {
foreach ((array) get_the_category($post_id) as $category) {
$attachment_id = (int) get_term_meta((int) $category->term_id, 'aksrt_category_cover_id', true);
if ($attachment_id > 0) {
return aksrt_renderer_sized_image((string) $attachment_id, $size);
}
}
return '';
}
}
if (!function_exists('aksrt_renderer_spare_image')) {
function aksrt_renderer_spare_image(array $thumb): string {
$images = array_values(array_filter((array) ($thumb['spare_thumbnail'] ?? [])));
return $images ? (string) $images[array_rand($images)] : '';
}
}
if (!function_exists('aksrt_renderer_resolve_images')) {
/** @return array{primary:string,gallery:string[],slides:string[],has_real:bool,is_slide:bool,video_url:string} */
function aksrt_renderer_resolve_images(int $post_id, array $cfg): array {
$thumb = (array) ($cfg['thumb'] ?? []);
$size = (string) ($thumb['thumb_postfirstimg_size'] ?? 'medium');
$slides = aksrt_renderer_curated_images($post_id, $size);
$gallery = aksrt_renderer_content_images($post_id, $size);
$video_url = class_exists('\AKSRT\Core\PostMetaBox')
? esc_url_raw((string) \AKSRT\Core\PostMetaBox::get_meta_value($post_id, 'featured_video', ''))
: '';
$thumbnail_id = (int) get_post_thumbnail_id($post_id);
$primary = $thumbnail_id > 0 ? aksrt_renderer_sized_image((string) $thumbnail_id, $size) : '';
if ($primary === '') {
$primary = class_exists('\AKSRT\Core\PostMetaBox')
? esc_url_raw((string) \AKSRT\Core\PostMetaBox::get_meta_value($post_id, 'thumbnail_url', ''))
: '';
}
if ($primary === '' && !empty($thumb['thumb_postfirstimg_s']) && !empty($gallery[0])) {
$primary = (string) $gallery[0];
}
if ($primary === '' && !empty($thumb['thumb_catimg_s'])) {
$primary = aksrt_renderer_category_image($post_id, $size);
}
$has_real = $primary !== '';
if ($primary === '') {
$primary = aksrt_renderer_spare_image($thumb);
}
if ($primary === '') {
$primary = (string) aksrt_get_lazy_thumb();
}
return [
'primary' => esc_url_raw($primary),
'gallery' => $gallery,
'slides' => $slides,
'has_real' => $has_real,
'is_slide' => !empty($thumb['list_thumb_slides_s']) && count($slides) > 0,
'video_url' => $video_url,
];
}
}