*/
function aksrt_renderer_collect_post_data(array $cfg): array {
$post_id = (int) get_the_ID();
if ($post_id <= 0) {
return [];
}
$images = aksrt_renderer_resolve_images($post_id, $cfg);
$categories = get_the_category($post_id) ?: [];
$tags = get_the_tags($post_id) ?: [];
$content_images = (array) $images['gallery'];
$gallery = array_slice($content_images, 0, 4);
$allow_conditional = !empty($cfg['allow_conditional_layout']) && ($cfg['mode'] ?? 'list') === 'list';
$is_mult_thumb = false;
$is_no_thumb = false;
if ($allow_conditional) {
if (($cfg['is_no_thumb'] ?? false) !== 'disable'
&& (($cfg['list_type'] ?? 'thumb') === 'text'
|| (($cfg['list_type'] ?? 'thumb') === 'thumb_if_has' && empty($images['has_real'])))) {
$is_no_thumb = true;
} elseif (($cfg['is_mult_thumb'] ?? false) !== 'disable' && count($gallery) > 2) {
$is_mult_thumb = has_post_format(['image', 'gallery'], $post_id);
if (!$is_mult_thumb) {
foreach ($categories as $category) {
if (in_array((int) $category->term_id, array_map('intval', (array) ($cfg['mult_thumb_cat'] ?? [])), true)) {
$is_mult_thumb = true;
break;
}
}
}
}
}
if (($cfg['is_no_thumb'] ?? false) === true) {
$is_no_thumb = true;
$is_mult_thumb = false;
}
if (($cfg['is_mult_thumb'] ?? false) === true && count($gallery) > 2 && !$is_no_thumb) {
$is_mult_thumb = true;
}
$excerpt = (string) get_the_excerpt($post_id);
if ($excerpt === '') {
$excerpt = (string) get_post_field('post_content', $post_id, 'raw');
}
$author_id = (int) get_post_field('post_author', $post_id);
$author = get_userdata($author_id);
$time_value = ($cfg['time_source'] ?? 'modified') === 'date'
? (string) get_the_time('Y-m-d H:i:s', $post_id)
: (string) get_the_modified_time('Y-m-d H:i:s', $post_id);
$time_timestamp = strtotime($time_value) ?: (int) get_the_time('U', $post_id);
$subtitle = class_exists('\AKSRT\Core\PostMetaBox')
? (string) \AKSRT\Core\PostMetaBox::get_meta_value($post_id, 'subtitle', '')
: '';
$pay_meta = aksrt_get_pay_meta($post_id);
if (!is_array($pay_meta)) {
$pay_meta = [];
}
return [
'post_id' => $post_id,
'permalink' => get_permalink($post_id),
'title' => get_the_title($post_id),
'subtitle' => $subtitle,
'excerpt' => wp_trim_words(wp_strip_all_tags($excerpt), 30, '...'),
'primary' => (string) $images['primary'],
'has_real' => !empty($images['has_real']),
'gallery' => $gallery,
'gallery_count' => count($content_images),
'slides' => array_slice((array) $images['slides'], 0, 8),
'is_slide' => !empty($images['is_slide']),
'video_url' => (string) $images['video_url'],
'is_mult_thumb' => $is_mult_thumb,
'is_no_thumb' => $is_no_thumb,
'categories' => $categories,
'tags' => $tags,
'author_id' => $author_id,
'author_name' => $author instanceof WP_User ? $author->display_name : '',
'avatar_url' => get_avatar_url($author_id, ['size' => 40]),
'time_title' => $time_value,
'time_ago' => human_time_diff($time_timestamp, current_time('timestamp')) . __('前', 'aksrt'),
'views' => (int) aksrt_get_post_views($post_id),
'likes' => (int) aksrt_get_post_likes($post_id),
'comments' => (int) get_comments_number($post_id),
'comments_open' => comments_open($post_id),
'comments_link' => get_comments_link($post_id),
'post_type' => (string) get_post_type($post_id),
'is_sticky' => is_sticky($post_id),
'resource_badge' => (string) aksrt_resource_card_badge_text($post_id),
'pay_type' => sanitize_key((string) ($pay_meta['pay_type'] ?? 'no')),
'pay_limit' => (int) ($pay_meta['pay_limit'] ?? 0),
];
}
}
if (!function_exists('aksrt_renderer_thumb')) {
function aksrt_renderer_thumb(array $data, array $cfg): string {
if (!empty($data['is_no_thumb']) || (string) $data['primary'] === '') {
return '';
}
$is_contain = (($cfg['thumb']['list_thumb_fit'] ?? '') === 'contain');
$target = !empty($cfg['target_blank']) ? ' target="_blank"' : '';
$content = '';
$format_icon = '';
$default_attr = empty($data['has_real']) ? ' data-thumb="default"' : '';
$has_video_thumb = (string) $data['video_url'] !== '' && !empty($cfg['thumb']['list_thumb_video_s']);
$has_slides = !$has_video_thumb && !empty($data['is_slide']) && count((array) $data['slides']) > 0;
if ($has_video_thumb) {
$muted = !empty($cfg['thumb']['list_thumb_video_mute_s']);
$content .= '
';
$content .= '
';
$content .= '
';
$format_icon = '';
} elseif ($has_slides) {
$content .= '';
foreach ((array) $data['slides'] as $image) {
$content .= '
';
}
$content .= '
';
$format_icon = '' . count((array) $data['slides']) . '';
} else {
$content .= '
';
$format = get_post_format((int) $data['post_id']);
if ((string) $data['video_url'] !== '') {
$format_icon = '';
} elseif (in_array($format, ['image', 'gallery'], true) && (int) $data['gallery_count'] > 0) {
$format_icon = '' . (int) $data['gallery_count'] . '';
} elseif ($format === 'video') {
$format_icon = '';
}
}
if ($format_icon !== '') {
$content .= '' . $format_icon . '
';
}
if (!empty($data['is_sticky'])) {
$content .= '' . esc_html__('置顶', 'aksrt') . '';
}
$fit = $is_contain && !$has_slides ? ' contain gradient-bg' : '';
$opacity = $is_contain && !$has_slides ? ' data-opacity="0.2"' : '';
return '' . $content . '
';
}
}
if (!function_exists('aksrt_renderer_mult_thumb')) {
function aksrt_renderer_mult_thumb(array $data, array $cfg): string {
if (empty($data['is_mult_thumb'])) {
return '';
}
$fit = (($cfg['thumb']['list_thumb_fit'] ?? '') === 'contain') ? ' contain' : '';
$target = !empty($cfg['target_blank']) ? ' target="_blank"' : '';
$html = '';
foreach (array_slice((array) $data['gallery'], 0, 4) as $index => $image) {
$badge = '';
if ($index === 3 && (int) ($data['gallery_count'] ?? 0) > 4) {
$badge = '+' . ((int) $data['gallery_count'] - 4) . '
';
}
$html .= '' . $badge . '
';
}
return $html . '';
}
}
if (!function_exists('aksrt_renderer_pay_badge')) {
function aksrt_renderer_pay_badge(array $data, bool $compact = false): string {
if ((string) $data['resource_badge'] === '') {
return '';
}
$is_vip = (int) ($data['pay_limit'] ?? 0) > 0 || (string) ($data['pay_type'] ?? '') === 'vip';
$vip_level = max(1, min(2, (int) ($data['pay_limit'] ?? 1)));
$class = $is_vip ? 'jb-vip' . $vip_level : 'c-yellow';
$type_labels = function_exists('aksrt_get_pay_types') ? aksrt_get_pay_types() : [];
$title = (string) ($type_labels[(string) ($data['pay_type'] ?? '')] ?? __('付费内容', 'aksrt'));
$icon = $is_vip ? aksrt_renderer_vip_icon($vip_level) : '';
if ($compact) {
return '- ' . $icon . esc_html((string) $data['resource_badge']) . '
';
}
$tooltip = $is_vip ? ' data-toggle="tooltip" title="' . esc_attr($title) . '"' : '';
return '' . $icon . esc_html((string) $data['resource_badge']) . '';
}
}
if (!function_exists('aksrt_renderer_vip_icon')) {
function aksrt_renderer_vip_icon(int $level): string {
if (!class_exists('\AKSRT\Settings\Settings')) {
return '';
}
$settings = \AKSRT\Settings\Settings::get_payment_vip_settings();
$url = esc_url((string) ($settings['levels'][$level]['img_icon'] ?? ''));
return $url !== ''
? '
'
: '';
}
}
if (!function_exists('aksrt_renderer_badges')) {
function aksrt_renderer_badges(array $data, array $cfg): string {
$items = [];
$types = (array) ($cfg['badge_types'] ?? []);
if (in_array('pay', $types, true)) {
$pay = aksrt_renderer_pay_badge($data);
if ($pay !== '') {
$items[] = $pay;
}
}
if (empty($cfg['is_category']) && in_array('cat', $types, true)) {
$colors = ['c-blue', 'c-yellow', 'c-green', 'c-purple', 'c-red'];
foreach (array_slice((array) $data['categories'], 0, 3) as $index => $category) {
$items[] = '' . esc_html($category->name) . '';
}
}
if (empty($cfg['is_tag']) && in_array('tag', $types, true)) {
$tags = (array) $data['tags'];
usort($tags, static fn($left, $right): int => (int) $right->count <=> (int) $left->count);
foreach (array_slice($tags, 0, 3) as $tag) {
$items[] = '# ' . esc_html($tag->name) . '';
}
}
return $items ? '' . implode('', $items) . '
' : '';
}
}
if (!function_exists('aksrt_renderer_meta_number')) {
function aksrt_renderer_meta_number(int $number): string {
return (string) aksrt_format_number($number);
}
}
if (!function_exists('aksrt_renderer_svg_icon')) {
function aksrt_renderer_svg_icon(string $name): string {
return aksrt_icon_markup('zibsvg-' . $name);
}
}
if (!function_exists('aksrt_renderer_meta')) {
function aksrt_renderer_meta(array $data, array $cfg, bool $is_card = false): string {
$show_time = !empty($cfg['show_time']);
$meta_left = '';
if (empty($cfg['no_author']) && !empty($cfg['show_author'])) {
$name = (!$is_card || !$show_time) && (string) $data['author_name'] !== ''
? '' . esc_html((string) $data['author_name']) . ''
: '';
$time = $show_time
? '' . esc_html((string) $data['time_ago']) . ''
: '';
$avatar = '
';
$meta_left = '- ' . $avatar . $name . $time . '
';
} elseif ($show_time) {
$meta_left = '- ' . esc_html((string) $data['time_ago']) . '
';
}
$right = '';
if (!empty($data['comments_open']) && empty($cfg['close_comments'])) {
$right .= '- ' . aksrt_renderer_svg_icon('comment') . esc_html((string) $data['comments']) . '
';
}
$right .= '- ' . aksrt_renderer_svg_icon('view') . esc_html(aksrt_renderer_meta_number((int) $data['views'])) . '
';
if (!empty($cfg['show_like']) && (string) ($data['post_type'] ?? '') === 'post') {
$right .= '- ' . aksrt_renderer_svg_icon('like') . esc_html(aksrt_renderer_meta_number((int) $data['likes'])) . '
';
}
return '';
}
}