210 lines
8.6 KiB
PHP
210 lines
8.6 KiB
PHP
<?php
|
||
/**
|
||
* Public user-home template context builder.
|
||
*
|
||
* Keeps page-user-home.php as a thin shell that only mounts parts.
|
||
*
|
||
* @package AKSRT
|
||
*/
|
||
|
||
defined('ABSPATH') || exit;
|
||
|
||
if (!function_exists('aksrt_get_user_home_page_context')) {
|
||
/**
|
||
* Build the render context for the public user homepage shell.
|
||
*
|
||
* @param WP_User $user Target user.
|
||
* @return array<string, mixed>
|
||
*/
|
||
function aksrt_get_user_home_page_context(WP_User $user): array {
|
||
$user_id = (int) $user->ID;
|
||
|
||
$dashboard = function_exists('aksrt_get_user_dashboard_view')
|
||
? aksrt_get_user_dashboard_view($user_id, ['viewer_id' => get_current_user_id()])
|
||
: [];
|
||
|
||
$is_own_profile = !empty($dashboard['is_own_profile']);
|
||
$user_home_settings = class_exists('\AKSRT\Settings\Settings')
|
||
? \AKSRT\Settings\Settings::get_user_home_settings()
|
||
: [
|
||
'tabs' => ['posts', 'favorites', 'comments', 'follow'],
|
||
'sidebar' => false,
|
||
'cover' => true,
|
||
'follow' => true,
|
||
'posts_per_page' => 10,
|
||
];
|
||
|
||
$post_count = (int) ($dashboard['post_count'] ?? 0);
|
||
$comment_count = (int) ($dashboard['comment_count'] ?? 0);
|
||
$favorite_post_ids = $dashboard['favorite_post_ids'] ?? [];
|
||
$favorite_count = (int) ($dashboard['favorite_count'] ?? 0);
|
||
$profile_details = $dashboard['profile_details'] ?? [];
|
||
$per_page = max(1, (int) ($user_home_settings['posts_per_page'] ?? 10));
|
||
$credits_enabled = !empty($dashboard['credits_enabled']);
|
||
$credit_balance = $dashboard['credit_balance'] ?? 0.0;
|
||
$currency_name = $dashboard['currency_name'] ?? __('积分', 'aksrt');
|
||
|
||
$user_home_manager = class_exists('\AKSRT\User\Home') ? \AKSRT\User\Home::get_instance() : null;
|
||
$visible_tabs = $user_home_manager ? $user_home_manager->get_visible_tabs($is_own_profile) : [];
|
||
|
||
$enabled_tabs = array_keys($visible_tabs);
|
||
if (empty($enabled_tabs)) {
|
||
$enabled_tabs = ['posts', 'comments', 'favorites'];
|
||
}
|
||
|
||
// Apply admin-configured display order.
|
||
$order = $user_home_settings['tabs_order'] ?? [];
|
||
$ordered = [];
|
||
foreach ((array) $order as $tab_key) {
|
||
if (in_array($tab_key, $enabled_tabs, true)) {
|
||
$ordered[] = $tab_key;
|
||
}
|
||
}
|
||
foreach ($enabled_tabs as $tab_key) {
|
||
if (!in_array($tab_key, $ordered, true)) {
|
||
$ordered[] = $tab_key;
|
||
}
|
||
}
|
||
$enabled_tabs = $ordered;
|
||
|
||
$pc_hide = $user_home_settings['tabs_pc_hide'] ?? [];
|
||
$mobile_hide = $user_home_settings['tabs_mobile_hide'] ?? [];
|
||
$enabled_tabs_pc = array_values(array_diff($enabled_tabs, (array) $pc_hide));
|
||
$enabled_tabs_mobile = array_values(array_diff($enabled_tabs, (array) $mobile_hide));
|
||
$is_mobile = function_exists('wp_is_mobile') && wp_is_mobile();
|
||
$display_tabs = $is_mobile ? $enabled_tabs_mobile : $enabled_tabs_pc;
|
||
$default_tab = !empty($display_tabs) ? $display_tabs[0] : $enabled_tabs[0];
|
||
|
||
$follow_count = 0;
|
||
if (class_exists('\AKSRT\User\Follow')) {
|
||
$follow_count = (int) \AKSRT\User\Follow::get_instance()->get_follower_count($user_id);
|
||
}
|
||
|
||
$tab_counts = [
|
||
'posts' => $post_count,
|
||
'comments' => $comment_count,
|
||
'favorites' => $favorite_count,
|
||
'credits' => $credits_enabled && $is_own_profile ? (int) round((float) $credit_balance) : 0,
|
||
'follow' => $follow_count,
|
||
];
|
||
|
||
$tab_labels = [
|
||
'posts' => __('文章', 'aksrt'),
|
||
'comments' => __('评论', 'aksrt'),
|
||
'favorites' => __('收藏', 'aksrt'),
|
||
'credits' => sprintf(__('我的%s', 'aksrt'), $currency_name),
|
||
'follow' => __('粉丝', 'aksrt'), // legacy: 粉丝
|
||
];
|
||
foreach ($visible_tabs as $tab_key => $tab_config) {
|
||
if (!empty($tab_config['label'])) {
|
||
$tab_labels[$tab_key] = (string) $tab_config['label'];
|
||
}
|
||
}
|
||
|
||
$active_tab = $default_tab;
|
||
|
||
$home_url = function_exists('aksrt_get_user_home_url') ? aksrt_get_user_home_url($user) : home_url('/');
|
||
|
||
return [
|
||
'user' => $user,
|
||
'user_id' => $user_id,
|
||
'dashboard' => $dashboard,
|
||
'is_own_profile' => $is_own_profile,
|
||
'user_home_settings' => $user_home_settings,
|
||
'profile_details' => $profile_details,
|
||
'post_count' => $post_count,
|
||
'comment_count' => $comment_count,
|
||
'favorite_post_ids' => $favorite_post_ids,
|
||
'favorite_count' => $favorite_count,
|
||
'per_page' => $per_page,
|
||
'credits_enabled' => $credits_enabled,
|
||
'credit_balance' => $credit_balance,
|
||
'currency_name' => $currency_name,
|
||
'enabled_tabs' => $enabled_tabs,
|
||
'tabs_pc_hide' => (array) $pc_hide,
|
||
'tabs_mobile_hide' => (array) $mobile_hide,
|
||
'default_tab' => $default_tab,
|
||
'active_tab' => $active_tab,
|
||
'tab_counts' => $tab_counts,
|
||
'tab_labels' => $tab_labels,
|
||
'home_url' => $home_url,
|
||
'posts_page' => 1,
|
||
'comments_page' => 1,
|
||
'favorites_page' => 1,
|
||
'follow_sub' => 'followers',
|
||
'follow_page' => 1,
|
||
'svg_empty_post' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><path d="M14 2v6h6"/><path d="M9 13h6M9 17h6M9 9h1"/></svg>',
|
||
'svg_empty_comment' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>',
|
||
'svg_empty_favorite' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"/></svg>',
|
||
'svg_empty_follow' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><path d="M20.84 4.61a5.5 5.5 0 0 0-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 0 0-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 0 0 0-7.78z"/></svg>',
|
||
];
|
||
}
|
||
}
|
||
|
||
if (!function_exists('aksrt_render_user_home_pagination')) {
|
||
/**
|
||
* 渲染公开用户主页面板的无刷新数字分页(按钮驱动,无 URL 参数)。
|
||
*
|
||
* 复用 .profile-pagination / .page-numbers 既有样式契约。
|
||
*
|
||
* @param int $total_pages 总页数。
|
||
* @param int $current 当前页。
|
||
*/
|
||
function aksrt_render_user_home_pagination(int $total_pages, int $current): void {
|
||
$total_pages = max(1, $total_pages);
|
||
$current = min(max(1, $current), $total_pages);
|
||
if ($total_pages <= 1) {
|
||
return;
|
||
}
|
||
|
||
$items = [];
|
||
for ($i = 1; $i <= $total_pages; $i++) {
|
||
if ($i === 1 || $i === $total_pages || abs($i - $current) <= 1) {
|
||
$items[] = (string) $i;
|
||
} elseif ($items && end($items) !== '…') {
|
||
$items[] = '…';
|
||
}
|
||
}
|
||
|
||
echo '<nav class="profile-pagination uh-pagination" aria-label="' . esc_attr__('分页', 'aksrt') . '">';
|
||
if ($current > 1) {
|
||
echo '<button type="button" class="page-numbers" data-uh-page="' . esc_attr((string) ($current - 1)) . '"><span aria-hidden="true">‹</span><span class="screen-reader-text">' . esc_html__('上一页', 'aksrt') . '</span></button>';
|
||
}
|
||
foreach ($items as $item) {
|
||
if ($item === '…') {
|
||
echo '<span class="page-numbers dots">…</span>';
|
||
continue;
|
||
}
|
||
if ((int) $item === $current) {
|
||
echo '<span class="page-numbers current" aria-current="page">' . esc_html($item) . '</span>';
|
||
continue;
|
||
}
|
||
echo '<button type="button" class="page-numbers" data-uh-page="' . esc_attr($item) . '">' . esc_html($item) . '</button>';
|
||
}
|
||
if ($current < $total_pages) {
|
||
echo '<button type="button" class="page-numbers" data-uh-page="' . esc_attr((string) ($current + 1)) . '"><span aria-hidden="true">›</span><span class="screen-reader-text">' . esc_html__('下一页', 'aksrt') . '</span></button>';
|
||
}
|
||
echo '</nav>';
|
||
}
|
||
}
|
||
|
||
if (!function_exists('aksrt_capture_user_home_panel')) {
|
||
/**
|
||
* 捕获公开用户主页面板 body(templates/parts/user-home/panel-{tab}.php)。
|
||
*
|
||
* @param string $tab 面板键名。
|
||
* @param array<string, mixed> $ctx aksrt_get_user_home_page_context() 上下文。
|
||
* @return string 捕获的 HTML,模板不存在时为空串。
|
||
*/
|
||
function aksrt_capture_user_home_panel(string $tab, array $ctx): string {
|
||
$template = locate_template(['templates/parts/user-home/panel-' . sanitize_key($tab) . '.php'], false, false);
|
||
if (!$template) {
|
||
return '';
|
||
}
|
||
|
||
ob_start();
|
||
load_template($template, false, $ctx);
|
||
return (string) ob_get_clean();
|
||
}
|
||
}
|