feat(users): normalize center routes and withdrawal limits

This commit is contained in:
2026-08-12 19:44:47 +08:00
parent 9004f7093f
commit dacd0095bf
24 changed files with 326 additions and 174 deletions
+46 -25
View File
@@ -14,7 +14,8 @@ defined('ABSPATH') || exit;
// --- 3a. /user/{昵称} 自定义重写 ---
add_filter('query_vars', function ($vars) {
$vars[] = 'aksrt_user_center';
$vars[] = 'user_nicename';
$vars[] = 'aksrt_user_center_tab';
$vars[] = 'aksrt_author_identifier';
$vars[] = 'aksrt_orders';
$vars[] = 'aksrt_revenue';
return $vars;
@@ -24,6 +25,14 @@ add_action('template_redirect', function () {
if (is_admin() || wp_doing_ajax() || is_feed() || is_embed() || is_preview()) {
return;
}
$request_path = trim((string) parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_PATH), '/');
if ($request_path === 'profile' || str_starts_with($request_path, 'profile/')) {
global $wp_query;
if ($wp_query instanceof WP_Query) { $wp_query->set_404(); }
status_header(404);
nocache_headers();
return;
}
$resolved_path = aksrt_get_current_special_template_path();
if ($resolved_path === '') {
@@ -35,13 +44,36 @@ add_action('template_redirect', function () {
exit;
}, 100);
// /user/{tab} only accepts registered user-center tabs; former public /user/{slug}
// addresses must not silently resolve to the user-center home.
add_action('template_redirect', function () {
if (is_admin() || wp_doing_ajax()) {
return;
}
$tab = (string) get_query_var('aksrt_user_center_tab');
if ($tab === '') {
return;
}
$tab = $tab === 'balance' ? 'credits' : sanitize_key($tab);
$allowed = class_exists('\\AKSRT\\User\\Center')
? array_keys(\AKSRT\User\Center::get_instance()->get_dashboard_tabs())
: ['profile', 'level', 'creator', 'comments', 'favorites', 'credits', 'vip', 'orders', 'auth', 'revenue', 'settings'];
if (!in_array($tab, $allowed, true)) {
global $wp_query;
if ($wp_query instanceof WP_Query) { $wp_query->set_404(); }
status_header(404);
nocache_headers();
}
}, 1);
add_action('init', function () {
add_rewrite_tag('%aksrt_user_center%', '([^&]+)');
add_rewrite_tag('%user_nicename%', '([^/]+)');
add_rewrite_tag('%aksrt_user_center_tab%', '([^/]+)');
add_rewrite_tag('%aksrt_author_identifier%', '([^/]+)');
add_rewrite_tag('%aksrt_orders%', '([0-1])');
add_rewrite_tag('%aksrt_revenue%', '([0-1])');
add_rewrite_rule('^profile/?$', 'index.php?aksrt_user_center=1', 'top');
add_rewrite_rule('^user/([^/]+)/?$', 'index.php?user_nicename=$matches[1]', 'top');
add_rewrite_rule('^user/([a-z0-9_-]+)/?$', 'index.php?aksrt_user_center=1&aksrt_user_center_tab=$matches[1]', 'top');
add_rewrite_rule('^author/([^/]+)/?$', 'index.php?aksrt_author_identifier=$matches[1]', 'top');
// Commerce center aliases (work even before WP page is assigned).
add_rewrite_rule('^orders/?$', 'index.php?aksrt_orders=1', 'top');
add_rewrite_rule('^revenue/?$', 'index.php?aksrt_revenue=1', 'top');
@@ -56,9 +88,9 @@ add_filter('template_include', function ($template) {
}
// A) /user/{昵称} even if special-path helper returned empty (user missing → 404).
$nicename = (string) get_query_var('user_nicename');
if ($nicename !== '') {
$user = get_user_by('slug', $nicename);
$identifier = (string) get_query_var('aksrt_author_identifier');
if ($identifier !== '') {
$user = function_exists('aksrt_resolve_public_user') ? aksrt_resolve_public_user($identifier) : null;
if ($user instanceof WP_User) {
$tpl = aksrt_locate_template_file('templates/pages/page-user-home.php');
if ($tpl) {
@@ -78,18 +110,12 @@ add_filter('template_include', function ($template) {
return $four ?: $template;
}
// B) profile slug page without rewrite var (permalink edge).
// B) ordinary assigned page templates.
if (is_page()) {
$page_id = get_queried_object_id();
if ($page_id) {
$slug = (string) get_post_field('post_name', $page_id);
$page_template = get_post_meta($page_id, '_wp_page_template', true);
if ($slug === 'profile' || $page_template === 'templates/pages/page-profile.php') {
$tpl = aksrt_locate_template_file('templates/pages/page-profile.php');
if ($tpl) {
return $tpl;
}
}
if (is_string($page_template) && strpos($page_template, 'templates/pages/') === 0) {
$tpl = aksrt_locate_template_file($page_template);
if ($tpl) {
@@ -157,13 +183,8 @@ add_action('init', function () {
aksrt_ensure_template_page($requested_template_key);
}
// Always re-assert profile page template (blank WP page with slug profile is common).
if (function_exists('aksrt_ensure_template_page')) {
aksrt_ensure_template_page('profile');
}
// 统一 flush 检查 — bump when user-home rewrite / author override changes
$rewrite_version = AKSRT_VERSION . ':user-home-profile-routing-v5';
$rewrite_version = AKSRT_VERSION . ':user-center-author-routing-v6';
$flushed = get_option('aksrt_user_rules_flushed');
$rules = get_option('rewrite_rules');
$need_flush = ($flushed !== $rewrite_version)
@@ -199,15 +220,15 @@ add_filter('author_link', function ($link, $author_id) {
$user = get_userdata($author_id);
$slug = $user ? $user->user_nicename : ('user-' . max(0, $author_id));
if (!get_option('permalink_structure')) {
return add_query_arg('user_nicename', rawurlencode($slug), home_url('/'));
return add_query_arg('aksrt_author', rawurlencode($slug), home_url('/'));
}
return home_url('/user/' . trailingslashit($slug));
return home_url('/author/' . trailingslashit($slug));
}, 99, 2);
// Accept ?user_nicename= when pretty rewrites are unavailable.
add_filter('request', function ($query_vars) {
if (empty($query_vars['user_nicename']) && !empty($_GET['user_nicename'])) {
$query_vars['user_nicename'] = sanitize_title(wp_unslash((string) $_GET['user_nicename']));
if (empty($query_vars['aksrt_author_identifier']) && !empty($_GET['aksrt_author'])) {
$query_vars['aksrt_author_identifier'] = sanitize_text_field(wp_unslash((string) $_GET['aksrt_author']));
}
if (empty($query_vars['aksrt_user_center']) && isset($_GET['aksrt_user_center'])) {
$query_vars['aksrt_user_center'] = '1';
@@ -248,7 +269,7 @@ add_action('template_redirect', function () {
if (empty($_GET['author'])) {
return;
}
if (get_query_var('user_nicename') || get_query_var('aksrt_user_center')) {
if (get_query_var('aksrt_author_identifier') || get_query_var('aksrt_user_center')) {
return;
}
if (is_author()) {
+4 -4
View File
@@ -369,12 +369,12 @@ function aksrt_clear_template_page_cache(): void {
function aksrt_get_user_home_template_path(): string {
global $_aksrt_special_templates, $aksrt_profile_user;
$nicename = (string) get_query_var('user_nicename');
if ($nicename === '') {
$identifier = (string) get_query_var('aksrt_author_identifier');
if ($identifier === '') {
return '';
}
$user = get_user_by('slug', $nicename);
$user = function_exists('aksrt_resolve_public_user') ? aksrt_resolve_public_user($identifier) : null;
if (!$user instanceof WP_User) {
return '';
}
@@ -393,7 +393,7 @@ function aksrt_get_user_home_template_path(): string {
* Resolve the active special-page template path for the current request.
*/
function aksrt_get_current_special_template_path(): string {
if (get_query_var('user_nicename')) {
if (get_query_var('aksrt_author_identifier')) {
return aksrt_get_user_home_template_path();
}
+32 -39
View File
@@ -19,28 +19,13 @@ if (!function_exists('aksrt_get_user_center_url')) :
* @return string
*/
function aksrt_get_user_center_url(string $tab = ''): string {
if (get_option('permalink_structure')) {
$url = home_url('/profile/');
} else {
$url = add_query_arg('aksrt_user_center', '1', home_url('/'));
}
if ($tab !== '') {
$tab = sanitize_key($tab);
// Deep-link aliases used by header wallet / VIP CTA.
$aliases = [
'assets' => 'credits',
'vip' => 'vip', // dedicated vip tab when registered; resolver falls back if missing
'signin' => 'credits',
'invite' => 'revenue',
'promotion' => 'revenue',
];
if (isset($aliases[$tab])) {
$tab = $aliases[$tab];
}
$url = add_query_arg('tab', $tab, $url);
}
return $url;
$tab = sanitize_key($tab ?: 'profile');
$aliases = ['assets' => 'credits', 'signin' => 'credits', 'invite' => 'revenue', 'promotion' => 'revenue'];
$tab = $aliases[$tab] ?? $tab;
$path_tab = $tab === 'credits' ? 'balance' : $tab;
return get_option('permalink_structure')
? home_url('/user/' . trailingslashit($path_tab))
: add_query_arg(['aksrt_user_center' => '1', 'aksrt_user_center_tab' => $tab], home_url('/'));
}
endif;
@@ -81,7 +66,7 @@ function aksrt_get_orders_page_url(): string {
if (function_exists('aksrt_get_user_center_url')) {
return aksrt_get_user_center_url('orders');
}
return home_url('/profile/?tab=orders');
return home_url('/user/orders/');
}
endif;
@@ -96,7 +81,7 @@ function aksrt_get_revenue_page_url(): string {
if (function_exists('aksrt_get_user_center_url')) {
return aksrt_get_user_center_url('revenue');
}
return home_url('/profile/?tab=revenue');
return home_url('/user/revenue/');
}
endif;
@@ -122,29 +107,37 @@ if (!function_exists('aksrt_get_user_home_url')) :
* @return string
*/
function aksrt_get_user_home_url($user = null): string {
$slug = '';
$target = null;
$mode = class_exists('\\AKSRT\\Settings\\Settings') ? (string) (\AKSRT\Settings\Settings::get_user_home_settings()['route_identifier'] ?? 'nicename') : 'nicename';
if ($user instanceof WP_User) {
$slug = $user->user_nicename;
$target = $user;
} elseif (is_int($user) && $user > 0) {
$u = get_userdata($user);
$slug = $u ? $u->user_nicename : '';
$target = get_userdata($user);
} elseif (is_numeric($user) && (int) $user > 0) {
$u = get_userdata((int) $user);
$slug = $u ? $u->user_nicename : '';
$target = get_userdata((int) $user);
} elseif (is_string($user) && $user) {
$slug = $user;
$target = get_user_by('slug', $user);
} else {
$current = wp_get_current_user();
$slug = $current->ID ? $current->user_nicename : '';
$target = wp_get_current_user();
}
if (!$slug) {
if (!$target instanceof WP_User || !$target->ID) {
return home_url('/');
}
// Plain permalinks: Apache 404s pretty /user/{slug}/ before WP boots.
$identifier = $mode === 'uid' ? (string) $target->ID : $target->user_nicename;
if (!get_option('permalink_structure')) {
return add_query_arg('user_nicename', rawurlencode($slug), home_url('/'));
return add_query_arg('aksrt_author', rawurlencode($identifier), home_url('/'));
}
return home_url('/user/' . trailingslashit($slug));
return home_url('/author/' . trailingslashit($identifier));
}
endif;
if (!function_exists('aksrt_resolve_public_user')) :
function aksrt_resolve_public_user(string $identifier): ?WP_User {
$mode = class_exists('\\AKSRT\\Settings\\Settings') ? (string) (\AKSRT\Settings\Settings::get_user_home_settings()['route_identifier'] ?? 'nicename') : 'nicename';
if ($mode === 'uid') {
return ctype_digit($identifier) && (int) $identifier > 0 ? get_userdata((int) $identifier) ?: null : null;
}
return get_user_by('slug', sanitize_title($identifier)) ?: null;
}
endif;
@@ -154,7 +147,7 @@ if (!function_exists('aksrt_is_user_center_page')) :
*/
function aksrt_is_user_center_page(): bool {
return (bool) get_query_var('aksrt_user_center')
|| (is_page_template('templates/pages/page-profile.php') && !(bool) get_query_var('user_nicename'));
|| (is_page_template('templates/pages/page-profile.php') && !(bool) get_query_var('aksrt_author_identifier'));
}
endif;
@@ -163,7 +156,7 @@ if (!function_exists('aksrt_is_user_home_page')) :
* 判断当前是否为公开用户主页
*/
function aksrt_is_user_home_page(): bool {
return (bool) get_query_var('user_nicename')
return (bool) get_query_var('aksrt_author_identifier')
|| is_page_template('templates/pages/page-user-home.php');
}
endif;