102 lines
3.9 KiB
PHP
102 lines
3.9 KiB
PHP
<?php
|
|
/**
|
|
* 用户页面共享辅助函数
|
|
*
|
|
* 分页渲染和空状态卡片,供本人中心和公开主页共用。
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
if (!function_exists('aksrt_render_profile_pagination')) {
|
|
/**
|
|
* 渲染分页
|
|
*
|
|
* @param int $total_pages 总页数
|
|
* @param int $current 当前页
|
|
* @param string $param URL 参数名
|
|
*/
|
|
function aksrt_render_profile_pagination($total_pages, $current, $param, $base_url = '') {
|
|
if ($total_pages <= 1) {
|
|
return;
|
|
}
|
|
$base = $base_url !== '' ? $base_url : remove_query_arg($param);
|
|
$base = remove_query_arg($param, $base);
|
|
$base = add_query_arg($param, '%#%', $base);
|
|
$links = paginate_links([
|
|
'base' => $base,
|
|
'format' => '',
|
|
'current' => $current,
|
|
'total' => $total_pages,
|
|
'prev_text' => '<span aria-hidden="true">‹</span><span class="screen-reader-text">' . esc_html__('上一页', 'aksrt') . '</span>',
|
|
'next_text' => '<span aria-hidden="true">›</span><span class="screen-reader-text">' . esc_html__('下一页', 'aksrt') . '</span>',
|
|
'mid_size' => 1,
|
|
'end_size' => 1,
|
|
]);
|
|
if ($links) {
|
|
echo '<nav class="profile-pagination" aria-label="' . esc_attr__('分页', 'aksrt') . '">' . $links . '</nav>'; // phpcs:ignore
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!function_exists('aksrt_render_asset_pagination')) {
|
|
/**
|
|
* Render user asset pagination without URL state.
|
|
*/
|
|
function aksrt_render_asset_pagination(int $total_pages, int $current, string $asset): void {
|
|
if ($total_pages <= 1) {
|
|
return;
|
|
}
|
|
|
|
$current = max(1, min($total_pages, $current));
|
|
$pages = array_values(array_unique(array_filter([
|
|
1,
|
|
$current - 1,
|
|
$current,
|
|
$current + 1,
|
|
$total_pages,
|
|
], static fn(int $page): bool => $page >= 1 && $page <= $total_pages)));
|
|
sort($pages);
|
|
|
|
echo '<nav class="profile-pagination" aria-label="' . esc_attr__('分页', 'aksrt') . '">';
|
|
if ($current > 1) {
|
|
echo '<button type="button" class="page-numbers prev" data-aksrt-asset-page="' . (int) ($current - 1) . '" data-aksrt-asset-key="' . esc_attr($asset) . '"><span aria-hidden="true">‹</span><span class="screen-reader-text">' . esc_html__('上一页', 'aksrt') . '</span></button>';
|
|
}
|
|
$previous = 0;
|
|
foreach ($pages as $page) {
|
|
if ($previous > 0 && $page > $previous + 1) {
|
|
echo '<span class="page-numbers dots">…</span>';
|
|
}
|
|
if ($page === $current) {
|
|
echo '<span class="page-numbers current" aria-current="page">' . (int) $page . '</span>';
|
|
} else {
|
|
echo '<button type="button" class="page-numbers" data-aksrt-asset-page="' . (int) $page . '" data-aksrt-asset-key="' . esc_attr($asset) . '">' . (int) $page . '</button>';
|
|
}
|
|
$previous = $page;
|
|
}
|
|
if ($current < $total_pages) {
|
|
echo '<button type="button" class="page-numbers next" data-aksrt-asset-page="' . (int) ($current + 1) . '" data-aksrt-asset-key="' . esc_attr($asset) . '"><span aria-hidden="true">›</span><span class="screen-reader-text">' . esc_html__('下一页', 'aksrt') . '</span></button>';
|
|
}
|
|
echo '</nav>';
|
|
}
|
|
}
|
|
|
|
if (!function_exists('aksrt_render_profile_empty')) {
|
|
/**
|
|
* 渲染空状态卡片
|
|
*/
|
|
function aksrt_render_profile_empty($icon_svg, $title, $desc = '', $cta_url = '', $cta_label = '') {
|
|
echo '<div class="profile-empty">';
|
|
echo '<div class="profile-empty__icon" aria-hidden="true">' . $icon_svg . '</div>'; // phpcs:ignore
|
|
echo '<h4 class="profile-empty__title">' . esc_html($title) . '</h4>';
|
|
if ($desc) {
|
|
echo '<p class="profile-empty__desc">' . esc_html($desc) . '</p>';
|
|
}
|
|
if ($cta_url && $cta_label) {
|
|
echo '<a class="btn-primary btn-sm profile-empty__cta" href="' . esc_url($cta_url) . '">' . esc_html($cta_label) . '</a>';
|
|
}
|
|
echo '</div>';
|
|
}
|
|
}
|