Files
wordpress-theme-aksrt/templates/parts/content-post-card.php
T
kate522 aaf458af1c refactor(autoload): migrate theme classes to Composer PSR-4
Move 288 types from global AKSRT_* + inc/classes manual requires into
namespace AKSRT\{Domain} under src/AKSRT/, wire composer.json psr-4,
and slim bootstrap to helpers/modules/hooks only. Split multi-class
widget/gateway files, update admin contract audit paths, and document
the layout in ADR/README.
2026-07-17 13:29:22 +08:00

175 lines
8.0 KiB
PHP

<?php
/**
* Post Card / List Item Template — Unified Wrapper (v4.0)
*
* This template delegates to the unified shared renderer (aksrt_render_post).
* It preserves backward compatibility for callers using get_template_part().
*
* @package AKSRT
* @since 4.0.0
*/
defined('ABSPATH') || exit;
// If the shared renderer is available, delegate entirely
if (function_exists('aksrt_render_post')) {
$args = isset($args) && is_array($args) ? $args : [];
// Resolve layout mode from query var or args
$forced_layout = (string) get_query_var('aksrt_post_card_layout', '');
$mode = 'card';
if (is_string($forced_layout) && in_array($forced_layout, ['card', 'list', 'grid-cms'], true)) {
$mode = $forced_layout;
} elseif (!empty($args['mode']) && in_array($args['mode'], ['card', 'list', 'grid-cms', 'mini'], true)) {
$mode = $args['mode'];
} elseif (function_exists('aksrt_get_post_flow') && aksrt_get_post_flow(get_the_ID()) === 'cms') {
$mode = 'grid-cms';
} elseif (class_exists('\AKSRT\Settings\Settings')) {
$archive_layout = \AKSRT\Settings\Settings::get_archive_layout();
$mode = in_array($archive_layout, ['card', 'list', 'grid-cms'], true) ? $archive_layout : 'card';
}
// Resolve scene context
$scene = (string) get_query_var('aksrt_post_card_scene', '');
$context = ($scene === 'home' || is_front_page() || (is_home() && !is_paged())) ? 'home' : 'archive';
$config = array_merge($args, [
'mode' => $mode,
'context' => $context,
]);
echo aksrt_render_post($config); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
return;
}
// ============================================================
// Fallback: self-contained rendering when shared renderer unavailable
// ============================================================
$args = isset($args) && is_array($args) ? $args : [];
$lazy_cfg = class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\Settings::get_lazyload_config() : ['enabled' => false, 'placeholder_url' => ''];
$lazy_on = !empty($lazy_cfg['enabled']);
$placeholder_url = !empty($lazy_cfg['placeholder_url']) ? (string) $lazy_cfg['placeholder_url'] : (get_template_directory_uri() . '/assets/img/placeholder.svg');
$forced_layout = (string) get_query_var('aksrt_post_card_layout', '');
$layout = 'card';
if (is_string($forced_layout) && in_array($forced_layout, ['card', 'list', 'masonry', 'grid-cms'], true)) {
$layout = $forced_layout;
} elseif (function_exists('aksrt_get_post_flow') && aksrt_get_post_flow(get_the_ID()) === 'cms') {
$layout = 'grid-cms';
} elseif (class_exists('\AKSRT\Settings\Settings')) {
$layout = \AKSRT\Settings\Settings::get_archive_layout();
}
$post_id = get_the_ID();
$author_id = get_the_author_meta('ID');
$author = get_the_author();
$avatar_url = get_avatar_url($author_id, ['size' => 40]);
$time_ago = human_time_diff(get_the_time('U'), current_time('timestamp')) . __('前', 'aksrt');
$excerpt = wp_trim_words(get_the_excerpt(), 'list' === $layout ? 30 : 20, '...');
$tags = get_the_tags() ?: [];
$categories = get_the_category() ?: [];
$comment_count = (int) get_comments_number();
$views = function_exists('aksrt_get_post_views') ? aksrt_get_post_views($post_id) : 0;
$is_sticky = is_sticky($post_id);
$title_html = isset($args['title_html']) ? (string) $args['title_html'] : get_the_title();
$resource_badge = function_exists('aksrt_resource_card_badge_text') ? aksrt_resource_card_badge_text($post_id) : '';
$img_url = '';
if (has_post_thumbnail()) {
$img_url = get_the_post_thumbnail_url($post_id, 'medium_large');
} elseif (function_exists('aksrt_get_first_content_image_url')) {
$img_url = (string) aksrt_get_first_content_image_url($post_id);
}
$has_img = !empty($img_url);
$scene = (string) get_query_var('aksrt_post_card_scene', '');
$is_home_scene = $scene === 'home' || is_front_page() || (is_home() && !is_paged());
$category_limit = $is_home_scene ? 1 : 3;
$tag_limit = $is_home_scene ? 2 : 3;
// Badges HTML
ob_start();
?>
<div class="item-tags scroll-x no-scrollbar">
<?php if ($resource_badge !== '') : ?>
<a rel="nofollow" href="<?php echo esc_url(get_permalink($post_id) . '#posts-pay'); ?>" class="meta-pay but jb-yellow"><?php echo esc_html($resource_badge); ?></a>
<?php endif; ?>
<?php foreach (array_slice($categories, 0, $category_limit) as $category) : ?>
<a href="<?php echo esc_url(get_category_link($category->term_id)); ?>" class="but"><i class="far fa-folder-open" aria-hidden="true"></i> <?php echo esc_html($category->name); ?></a>
<?php endforeach; ?>
<?php foreach (array_slice($tags, 0, $tag_limit) as $tag) : ?>
<a href="<?php echo esc_url(get_tag_link($tag->term_id)); ?>" class="but"># <?php echo esc_html($tag->name); ?></a>
<?php endforeach; ?>
</div>
<?php
$badges_html = trim((string) ob_get_clean());
// Meta HTML
ob_start();
?>
<div class="item-meta muted-2-color flex jsb ac">
<item class="meta-author flex ac">
<img src="<?php echo esc_url($avatar_url); ?>" alt="" class="avatar-mini" width="20" height="20" loading="lazy">
<name class="meta-name"><?php echo esc_html($author); ?></name>
<item class="meta-time"><?php echo esc_html($time_ago); ?></item>
</item>
<div class="meta-right">
<item class="meta-view"><i class="fas fa-eye" aria-hidden="true"></i> <?php echo esc_html(function_exists('aksrt_format_number') ? aksrt_format_number($views) : (string) $views); ?></item>
<item class="meta-view"><i class="fas fa-comment" aria-hidden="true"></i> <?php echo esc_html((string) $comment_count); ?></item>
</div>
</div>
<?php
$meta_html = trim((string) ob_get_clean());
if ('list' === $layout) : ?>
<article <?php post_class('posts-item list ajax-item'); ?>>
<div class="item-thumbnail">
<a href="<?php the_permalink(); ?>">
<?php if ($has_img && $lazy_on) : ?>
<span class="aksrt-skeleton"></span>
<img class="fit-cover aksrt-lazy" data-src="<?php echo esc_url($img_url); ?>" src="<?php echo esc_url($placeholder_url); ?>" alt="<?php echo esc_attr(get_the_title()); ?>">
<?php elseif ($has_img) : ?>
<img class="fit-cover" src="<?php echo esc_url($img_url); ?>" alt="<?php echo esc_attr(get_the_title()); ?>">
<?php else : ?>
<img class="fit-cover" src="<?php echo esc_url($placeholder_url); ?>" alt="">
<?php endif; ?>
</a>
<?php if ($is_sticky) : ?>
<span class="img-badge left badge-sticky"><?php esc_html_e('置顶', 'aksrt'); ?></span>
<?php endif; ?>
</div>
<div class="item-body">
<h2 class="item-heading"><a href="<?php the_permalink(); ?>"><?php echo wp_kses_post($title_html); ?></a></h2>
<p class="item-excerpt"><?php echo wp_kses_post(esc_html($excerpt)); ?></p>
<?php echo $badges_html; ?>
<?php echo $meta_html; ?>
</div>
</article>
<?php else : ?>
<article <?php post_class('posts-item card ajax-item' . ($is_home_scene ? ' posts-item--home' : '')); ?>>
<div class="item-thumbnail">
<a href="<?php the_permalink(); ?>">
<?php if ($has_img && $lazy_on) : ?>
<span class="aksrt-skeleton"></span>
<img class="fit-cover aksrt-lazy" data-src="<?php echo esc_url($img_url); ?>" src="<?php echo esc_url($placeholder_url); ?>" alt="<?php echo esc_attr(get_the_title()); ?>">
<?php elseif ($has_img) : ?>
<img class="fit-cover" src="<?php echo esc_url($img_url); ?>" alt="<?php echo esc_attr(get_the_title()); ?>">
<?php else : ?>
<img class="fit-cover" src="<?php echo esc_url($placeholder_url); ?>" alt="">
<?php endif; ?>
</a>
<?php if ($is_sticky) : ?>
<span class="img-badge left badge-sticky"><?php esc_html_e('置顶', 'aksrt'); ?></span>
<?php endif; ?>
</div>
<div class="item-body">
<h2 class="item-heading"><a href="<?php the_permalink(); ?>"><?php echo wp_kses_post($title_html); ?></a></h2>
<?php echo $badges_html; ?>
<?php echo $meta_html; ?>
</div>
</article>
<?php endif; ?>