Files
wordpress-theme-aksrt/inc/helpers/aksrt-single.php
T
2026-08-26 15:29:37 +08:00

214 lines
8.7 KiB
PHP

<?php
/**
* Unified single post composition (legacy-style closed surface).
*
* single.php stays a thin shell. All article assembly lives here.
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
if (!function_exists('aksrt_single_build_context')) :
/**
* Resolve the single-page view model once.
*
* @param int|null $post_id Post ID.
* @return array<string,mixed>
*/
function aksrt_single_build_context($post_id = null): array {
$post_id = $post_id ? (int) $post_id : (int) get_the_ID();
$settings = function_exists('aksrt_resolve_single_post_display_settings')
? aksrt_resolve_single_post_display_settings($post_id)
: [];
$views = function_exists('aksrt_get_post_views') ? (int) aksrt_get_post_views($post_id) : 0;
$comment_count = (int) get_comments_number($post_id);
$like_count = function_exists('aksrt_get_post_likes') ? (int) aksrt_get_post_likes($post_id) : 0;
$favorite_count = function_exists('aksrt_get_post_favorites') ? (int) aksrt_get_post_favorites($post_id) : 0;
$word_count = function_exists('aksrt_post_word_count') && !empty($settings['word_count_enabled'])
? aksrt_post_word_count($post_id)
: 0;
$reading_minutes = function_exists('aksrt_reading_minutes') && !empty($settings['reading_time_enabled'])
? aksrt_reading_minutes($post_id)
: 0;
// legacy primary meta = comment/views/likes via meta_box_html.
$meta_items = [];
$meta_box_html = function_exists('aksrt_get_single_meta_box')
? aksrt_get_single_meta_box('abs-right', 'down', $post_id)
: '';
$meta_items_secondary = [];
if ($word_count > 0) {
$meta_items_secondary[] = [
'icon' => 'fa fas fa-file-word',
'text' => sprintf(__('约 %s 字', 'aksrt'), number_format_i18n($word_count)),
];
}
if ($reading_minutes > 0) {
$meta_items_secondary[] = [
'icon' => 'fa fas fa-hourglass',
'text' => sprintf(_n('约 %d 分钟', '约 %d 分钟', $reading_minutes, 'aksrt'), $reading_minutes),
];
}
$copyright = trim((string) ($settings['copyright_text'] ?? ''));
if ($copyright === '') {
$copyright = sprintf(
__('本文由 %s 原创发布,转载请注明出处。', 'aksrt'),
get_the_author_meta('display_name', (int) get_post_field('post_author', $post_id))
);
}
$paid_settings = class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\Settings::get_paid_content_settings() : [];
$paid_display = is_array($paid_settings['display'] ?? null) ? $paid_settings['display'] : [];
$paid_box_position = sanitize_key((string) ($paid_display['box_position'] ?? 'top'));
// legacy positions: box_top | top | bottom | box_bottom (do not collapse).
if (!in_array($paid_box_position, ['box_top', 'top', 'bottom', 'box_bottom'], true)) {
$paid_box_position = 'top';
}
$show_cover = !empty($settings['show_cover']) && (string) ($settings['cover_url'] ?? '') !== '';
$pay_meta = function_exists('aksrt_get_pay_meta') ? aksrt_get_pay_meta($post_id) : ['pay_type' => 'no'];
$pay_type = (string) ($pay_meta['pay_type'] ?? 'no');
$is_paid_post = function_exists('aksrt_is_paid_post') && aksrt_is_paid_post($post_id);
// Unpaid reading fragments own their inline purchase card. Once access is
// granted, the article-level slot shows the canonical purchased card.
$show_pay_box = $is_paid_post && $pay_type !== 'no';
$breadcrumbs_html = '';
if (!empty($settings['breadcrumbs']) && function_exists('aksrt_breadcrumbs')) {
ob_start();
aksrt_breadcrumbs(
(string) ($settings['breadcrumbs_home'] ?? ($settings['breadcrumbs_home_text'] ?? '')),
['single_text' => !isset($settings['breadcrumbs_single_text']) || !empty($settings['breadcrumbs_single_text'])]
);
$breadcrumbs_html = trim((string) ob_get_clean());
}
$status_badge = function_exists('aksrt_get_post_status_badge')
? aksrt_get_post_status_badge($post_id)
: '';
$allow_view = function_exists('aksrt_get_post_allow_view_data')
? aksrt_get_post_allow_view_data($post_id)
: ['allow' => true];
if (empty($allow_view['allow'])) {
// legacy: pay box is skipped when allow_view denies.
$show_pay_box = false;
}
$layout = function_exists('aksrt_get_post_show_layout')
? aksrt_get_post_show_layout($post_id)
: '';
// Do not use wp_is_mobile() as a layout gate. It is User-Agent based and
// can disagree with the actual viewport (for example, desktop-mode mobile
// browsers). The responsive stylesheet owns narrow-screen hiding.
$show_aside = get_post_type($post_id) === 'post'
&& $layout !== 'no_sidebar'
&& aksrt_is_show_sidebar();
return [
'post_id' => $post_id,
'settings' => $settings,
'show_cover' => $show_cover,
'cover_url' => (string) ($settings['cover_url'] ?? ''),
'cover_type' => (string) ($settings['cover_type'] ?? 'none'),
'img_scale' => (int) ($settings['article_image_cover_scale'] ?? 35),
'video_scale' => (int) ($settings['article_video_cover_scale'] ?? 56),
'slide_images' => array_values(array_filter((array) ($settings['slide_images'] ?? []))),
'episode_count' => max(0, (int) ($settings['episode_count'] ?? 0)),
'video_episodes' => is_array($settings['video_episodes'] ?? null) ? array_values($settings['video_episodes']) : [],
'video_poster' => (string) ($settings['video_poster'] ?? ''),
'meta_items' => $meta_items,
'meta_items_secondary' => $meta_items_secondary,
'meta_box_html' => $meta_box_html,
'title_html' => function_exists('aksrt_get_single_title_html') ? aksrt_get_single_title_html($post_id) : esc_html(get_the_title($post_id)),
'allow_view' => $allow_view,
'views' => $views,
'comment_count' => $comment_count,
'like_count' => $like_count,
'favorite_count' => $favorite_count,
'is_liked' => function_exists('aksrt_is_post_liked') ? aksrt_is_post_liked($post_id) : false,
'is_favorited' => function_exists('aksrt_is_post_favorited') ? aksrt_is_post_favorited($post_id) : false,
'copyright_text' => $copyright,
'front_content' => (string) ($settings['front_content'] ?? ''),
'after_content' => (string) ($settings['after_content'] ?? ''),
'content_maxheight' => !empty($settings['maxheight_enabled']) ? max(0, (int) ($settings['maxheight'] ?? 0)) : 0,
'show_pay_box' => $show_pay_box,
'pay_type' => $pay_type,
'is_paid_post' => $is_paid_post,
'paid_box_position' => $paid_box_position,
'breadcrumbs_html' => $breadcrumbs_html,
'status_badge' => $status_badge,
'show_aside' => $show_aside,
];
}
endif;
if (!function_exists('aksrt_single_is_docs_page')) :
/**
* Docs fast-path: true when the current post should leave the unified single surface.
*/
function aksrt_single_is_docs_page(int $post_id): bool {
if (!class_exists('\AKSRT\Core\DocsPresenter')) {
return false;
}
return (bool) \AKSRT\Core\DocsPresenter::get_instance()->is_docs_page($post_id);
}
endif;
if (!function_exists('aksrt_single_render_author_bar')) :
/**
* Render author bar from a real args array (no ambient query vars).
*
* @param array<string,mixed> $args Partial args.
*/
function aksrt_single_render_author_bar(array $args = []): void {
$args = wp_parse_args($args, [
'extra_class' => '',
'show_actions' => false,
'show_bio' => false,
'meta_items' => [],
]);
get_template_part('templates/parts/single-author-bar', null, $args);
}
endif;
if (!function_exists('aksrt_single')) :
/**
* Compose header + article + after-box (legacy equivalent).
*
* @param array<string,mixed>|null $ctx Optional prebuilt context.
*/
function aksrt_single(?array $ctx = null): void {
$ctx = is_array($ctx) ? $ctx : aksrt_single_build_context();
if (empty($ctx['show_cover'])) {
if ($ctx['breadcrumbs_html'] !== '') {
echo $ctx['breadcrumbs_html']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
} elseif (function_exists('aksrt_breadcrumbs')) {
$settings = is_array($ctx['settings'] ?? null) ? $ctx['settings'] : [];
aksrt_breadcrumbs(
(string) ($settings['breadcrumbs_home'] ?? ($settings['breadcrumbs_home_text'] ?? '')),
['single_text' => !isset($settings['breadcrumbs_single_text']) || !empty($settings['breadcrumbs_single_text'])]
);
}
} else {
aksrt_single_render_cover($ctx);
}
do_action('aksrt_single_before', get_post());
if (function_exists('aksrt_single_pay_box_matches') && aksrt_single_pay_box_matches($ctx, 'box_top')) {
aksrt_single_render_pay_box($ctx);
}
aksrt_single_render_article($ctx);
do_action('aksrt_single_article_after', get_post());
aksrt_single_render_after_box($ctx);
if (function_exists('aksrt_single_pay_box_matches') && aksrt_single_pay_box_matches($ctx, 'box_bottom')) {
aksrt_single_render_pay_box($ctx);
}
do_action('aksrt_single_after', get_post());
}
endif;