$cfg Renderer config. * @return array */ 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); $gallery_images = array_slice($images['gallery'], 0, $cfg['multi_thumb_limit']); $is_mult_thumb = !empty($cfg['multi_thumb_enabled']) && count($gallery_images) >= (int) $cfg['multi_thumb_min'] && $cfg['mode'] === 'list'; $author_id = (int) get_post_field('post_author', $post_id); $categories = get_the_category($post_id) ?: []; $tags = get_the_tags($post_id) ?: []; $custom_excerpt = class_exists('\AKSRT\Core\PostMetaBox') ? (string) \AKSRT\Core\PostMetaBox::get_meta_value($post_id, 'custom_excerpt', '') : ''; $custom_subtitle = (string) get_post_meta($post_id, '_subtitle', true); $excerpt_source = $custom_excerpt !== '' ? $custom_excerpt : (string) get_the_excerpt($post_id); if ($excerpt_source === '') { $excerpt_source = (string) get_post_field('post_content', $post_id, 'raw'); } $excerpt = wp_trim_words(wp_strip_all_tags($excerpt_source), $cfg['excerpt_words'], '...'); $resource_meta = function_exists('aksrt_get_resource_meta') ? aksrt_get_resource_meta($post_id) : []; $resource_strip = function_exists('aksrt_resource_meta_strip') ? aksrt_resource_meta_strip($resource_meta) : []; return [ 'post_id' => $post_id, 'permalink' => get_permalink($post_id), 'title' => get_the_title($post_id), 'subtitle' => $custom_subtitle, 'excerpt' => $excerpt, 'img_url' => $images['primary'], 'has_img' => !empty($images['has_thumb']), 'has_real_img' => !empty($images['has_real']), 'gallery_images' => $gallery_images, 'is_mult_thumb' => $is_mult_thumb, 'is_video_cover' => !empty(get_post_meta($post_id, '_aksrt_featured_video', true)) || !empty(get_post_meta($post_id, '_video_url', true)), 'author_id' => $author_id, 'avatar_url' => get_avatar_url($author_id, ['size' => 40]), 'author_name' => get_the_author(), 'categories' => $categories, 'tags' => $tags, 'comment_count' => (int) get_comments_number($post_id), 'views' => function_exists('aksrt_get_post_views') ? (int) aksrt_get_post_views($post_id) : 0, 'likes' => function_exists('aksrt_get_post_likes') ? (int) aksrt_get_post_likes($post_id) : 0, 'time_ago' => human_time_diff((int) get_the_time('U', $post_id), current_time('timestamp')), 'resource_badge' => function_exists('aksrt_resource_card_badge_text') ? (string) aksrt_resource_card_badge_text($post_id) : '', 'resource_strip' => $resource_strip, 'is_resource' => function_exists('aksrt_is_resource_post') ? aksrt_is_resource_post($post_id) : false, 'is_sticky' => is_sticky($post_id), 'thumb_ratio_css' => aksrt_renderer_thumb_ratio_value((string) $cfg['thumb_ratio']), 'thumb_ratio_key' => (string) $cfg['thumb_ratio'], ]; } } if (!function_exists('aksrt_renderer_article_attrs')) { /** * Build the article wrapper attributes. * * @param string[] $classes CSS classes. * @param array $data Collected post data. * @return string */ function aksrt_renderer_article_attrs(array $classes, array $data): string { $classes[] = 'thumb-ratio--' . str_replace(':', '-', (string) $data['thumb_ratio_key']); return sprintf( 'class="%s" style="%s"', esc_attr(implode(' ', array_unique(array_filter($classes)))), esc_attr('--aksrt-thumb-ratio:' . $data['thumb_ratio_css']) ); } } if (!function_exists('aksrt_renderer_thumb')) { /** * Render the primary thumbnail. * * @param array $data Collected post data. * @param array $cfg Renderer config. * @return string */ function aksrt_renderer_thumb(array $data, array $cfg): string { if (!$cfg['show_thumb'] || empty($data['has_img'])) { return ''; } $img = (string) $data['img_url']; $title = (string) $data['title']; $href = (string) $data['permalink']; $is_sticky = !empty($data['is_sticky']); $has_real_img = !empty($data['has_real_img']); $fit_class = !empty($cfg['thumb_fit']) && $cfg['thumb_fit'] === 'contain' ? 'fit-contain' : 'fit-cover'; $img_attr = aksrt_get_lazy_attr('lazy_posts_thumb', $img, $fit_class); $img_html = ''; $sticky_html = ($is_sticky && !empty($cfg['badge_show'])) ? '' . esc_html__('置顶', 'aksrt') . '' : ''; $format_badge = ''; if (!empty($data['is_mult_thumb']) && !empty($data['gallery_images'])) { $format_badge = '' . esc_html(count($data['gallery_images'])) . ''; } elseif (!empty($data['is_video_cover'])) { $format_badge = ''; } $empty_class = $has_real_img ? '' : ' item-thumbnail--fallback'; $target_attr = !empty($cfg['target_blank']) ? ' target="_blank"' : ''; if ($cfg['mode'] === 'list') { $graphic_class = 'post-graphic'; if (($cfg['list_img_position'] ?? 'left') === 'right') { $graphic_class .= ' order1'; } return '
' . $img_html . '' . $sticky_html . $format_badge . '
'; } return '
' . $img_html . '' . $sticky_html . $format_badge . '
'; } } if (!function_exists('aksrt_renderer_mult_thumb')) { /** * Render the multi-image strip used by list cards. * * @param array $data Collected post data. * @return string */ function aksrt_renderer_mult_thumb(array $data): string { if (empty($data['is_mult_thumb']) || empty($data['gallery_images'])) { return ''; } $html = ''; foreach (array_slice($data['gallery_images'], 0, 4) as $image) { $html .= ''; $html .= ''; $html .= ''; } $html .= ''; return $html; } } if (!function_exists('aksrt_renderer_badges')) { /** * Render taxonomy and resource badges. * * @param array $data Collected post data. * @param array $cfg Renderer config. * @return string */ function aksrt_renderer_badges(array $data, array $cfg): string { $items = []; if (!empty($cfg['show_resource_badge']) && $data['resource_badge'] !== '') { $items[] = '' . esc_html($data['resource_badge']) . ''; } if (!empty($cfg['show_badges']) && !empty($cfg['badge_show']) && $cfg['category_limit'] > 0) { foreach (array_slice($data['categories'], 0, $cfg['category_limit']) as $category) { $items[] = ' ' . esc_html($category->name) . ''; } } if (!empty($cfg['show_tags']) && $cfg['tag_limit'] > 0) { foreach (array_slice($data['tags'], 0, $cfg['tag_limit']) as $tag) { $items[] = '#' . esc_html($tag->name) . ''; } } if (!empty($data['is_resource'])) { $items[] = 'VIP'; } if (empty($items)) { return ''; } return '
' . implode('', $items) . '
'; } } if (!function_exists('aksrt_renderer_resource_strip')) { /** * Render compact resource meta chips. * * @param array $data Collected post data. * @return string */ function aksrt_renderer_resource_strip(array $data): string { if (empty($data['resource_strip']) || !is_array($data['resource_strip'])) { return ''; } $html = '
    '; foreach ($data['resource_strip'] as $item) { $html .= '
  • ' . esc_html((string) $item) . '
  • '; } $html .= '
'; return $html; } } if (!function_exists('aksrt_renderer_meta')) { /** * Render the meta bar. * * @param array $data Collected post data. * @param array $cfg Renderer config. * @return string */ function aksrt_renderer_meta(array $data, array $cfg): string { if (!$cfg['show_meta']) { return ''; } $format_number = function_exists('aksrt_format_number') ? 'aksrt_format_number' : 'strval'; if ($cfg['mode'] === 'mini') { $items = []; if (!empty($cfg['show_date'])) { $items[] = '' . esc_html($data['time_ago']) . ''; } if (!empty($cfg['show_views'])) { $items[] = ' ' . esc_html($format_number($data['views'])) . ''; } if (empty($items)) { return ''; } return '
' . implode('', $items) . '
'; } $left = []; if (!empty($cfg['show_author'])) { $left[] = '' . esc_html($data['author_name']) . ''; } if (!empty($cfg['show_date'])) { $left[] = '' . esc_html($data['time_ago']) . ''; } $right = []; if (!empty($cfg['show_comments'])) { $right[] = ' ' . esc_html((string) $data['comment_count']) . ''; } if (!empty($cfg['show_views'])) { $right[] = ' ' . esc_html($format_number($data['views'])) . ''; } if (!empty($cfg['show_likes'])) { $right[] = ' ' . esc_html($format_number($data['likes'])) . ''; } if (empty($left) && empty($right)) { return ''; } $html = '
'; if (!empty($left)) { $html .= '
' . implode('', $left) . '
'; } if (!empty($right)) { $html .= '
' . implode('', $right) . '
'; } $html .= '
'; return $html; } }