用户公开主页全局采用DOM无刷新加载

This commit is contained in:
2026-08-23 14:55:24 +08:00
parent c3f66e9a14
commit bb9a410ad6
14 changed files with 569 additions and 231 deletions
+89
View File
@@ -25,6 +25,95 @@ class Home {
public function init(): void {
$this->register_default_tabs();
add_action('rest_api_init', [$this, 'register_rest_routes']);
}
/**
* Register public user-home panel fragment routes.
*/
public function register_rest_routes(): void {
register_rest_route('aksrt/v1', '/user-home/panel/(?P<user_id>\d+)/(?P<tab>[a-z0-9_-]+)', [
'methods' => \WP_REST_Server::READABLE,
'callback' => [$this, 'rest_panel'],
'permission_callback' => '__return_true',
'args' => [
'user_id' => [
'required' => true,
'sanitize_callback' => 'absint',
'validate_callback' => static function ($value): bool {
return (int) $value > 0;
},
],
'tab' => [
'required' => true,
'sanitize_callback' => 'sanitize_key',
'validate_callback' => static function ($value): bool {
return preg_match('/^[a-z0-9_-]+$/', (string) $value) === 1;
},
],
'page' => [
'required' => false,
'default' => 1,
'sanitize_callback' => 'absint',
],
'sub' => [
'required' => false,
'default' => 'followers',
'sanitize_callback' => 'sanitize_key',
'validate_callback' => static function ($value): bool {
return in_array((string) $value, ['followers', 'following'], true);
},
],
],
]);
}
/**
* Render a public user-home panel body fragment.
*/
public function rest_panel(\WP_REST_Request $request) {
$user = get_userdata((int) $request->get_param('user_id'));
if (!$user) {
return new \WP_Error('aksrt_user_home_user', __('用户不存在', 'aksrt'), ['status' => 404]);
}
$tab = sanitize_key((string) $request->get_param('tab'));
$page = max(1, (int) $request->get_param('page'));
$sub = (string) $request->get_param('sub') === 'following' ? 'following' : 'followers';
if (!function_exists('aksrt_get_user_home_page_context')) {
require_once AKSRT_DIR . '/inc/helpers/aksrt-user-home.php';
}
$context = aksrt_get_user_home_page_context($user);
if (!in_array($tab, (array) ($context['enabled_tabs'] ?? []), true)) {
return new \WP_Error('aksrt_user_home_panel', __('用户主页面板不存在', 'aksrt'), ['status' => 404]);
}
$context['active_tab'] = $tab;
$context['follow_sub'] = $sub;
$page_keys = [
'posts' => 'posts_page',
'comments' => 'comments_page',
'favorites' => 'favorites_page',
'follow' => 'follow_page',
];
if (isset($page_keys[$tab])) {
$context[$page_keys[$tab]] = $page;
}
$html = aksrt_capture_user_home_panel($tab, $context);
if ($html === '') {
return new \WP_Error('aksrt_user_home_panel_empty', __('用户主页面板暂不可用', 'aksrt'), ['status' => 404]);
}
return rest_ensure_response([
'success' => true,
'data' => [
'tab' => $tab,
'html' => $html,
],
]);
}
/**