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()) {