fix(user): show IP city on home/center and force /user + profile templates
This commit is contained in:
@@ -58,37 +58,46 @@ add_filter('template_include', function ($template) {
|
||||
return $resolved_path;
|
||||
}
|
||||
|
||||
return $template;
|
||||
|
||||
global $_aksrt_special_templates;
|
||||
|
||||
// A) /user/{昵称} 路由 → 加载公开主页模板
|
||||
$nicename = get_query_var('user_nicename');
|
||||
if (!empty($nicename)) {
|
||||
// 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);
|
||||
if (!$user) return $template; // 不存在的用户 → 默认 404
|
||||
|
||||
$tpl = aksrt_locate_template_file('templates/pages/page-user-home.php');
|
||||
if (!$tpl) {
|
||||
// 兜底:如果新模板不存在,回退到旧 profile 模板
|
||||
$tpl = aksrt_locate_template_file($_aksrt_special_templates['profile']['file']);
|
||||
if ($user instanceof WP_User) {
|
||||
$tpl = aksrt_locate_template_file('templates/pages/page-user-home.php');
|
||||
if ($tpl) {
|
||||
global $aksrt_profile_user;
|
||||
$aksrt_profile_user = $user;
|
||||
return $tpl;
|
||||
}
|
||||
}
|
||||
if (!$tpl) return $template;
|
||||
|
||||
global $aksrt_profile_user;
|
||||
$aksrt_profile_user = $user;
|
||||
return $tpl;
|
||||
// Unknown nicename: force 404 instead of blank page.
|
||||
global $wp_query;
|
||||
if ($wp_query instanceof WP_Query) {
|
||||
$wp_query->set_404();
|
||||
status_header(404);
|
||||
nocache_headers();
|
||||
}
|
||||
$four = get_query_template('404');
|
||||
return $four ?: $template;
|
||||
}
|
||||
|
||||
// B) 普通页面 → 检测是否使用了我们的特殊模板,确保加载正确的模板文件
|
||||
// (防止 WP 在某些情况下未能正确解析自定义页面模板)
|
||||
// B) profile slug page without rewrite var (permalink edge).
|
||||
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 ($page_template && strpos($page_template, 'templates/pages/') === 0) {
|
||||
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) return $tpl;
|
||||
if ($tpl) {
|
||||
return $tpl;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -136,26 +145,28 @@ add_filter('template_include', function ($template) {
|
||||
add_action('init', function () {
|
||||
global $_aksrt_special_templates;
|
||||
|
||||
// 后台/AJAX:检查并创建缺失页面
|
||||
// 后台/AJAX:检查并创建缺失页面(含 profile 用户中心)
|
||||
$pages_bootstrapped = get_option('aksrt_template_pages_bootstrapped');
|
||||
$requested_template_key = (!is_admin() && !wp_doing_ajax()) ? aksrt_get_requested_template_key() : '';
|
||||
if (is_admin() || wp_doing_ajax() || $pages_bootstrapped !== AKSRT_VERSION) {
|
||||
foreach ($_aksrt_special_templates as $key => $cfg) {
|
||||
if ($key === 'profile') {
|
||||
continue;
|
||||
}
|
||||
aksrt_ensure_template_page($key);
|
||||
}
|
||||
|
||||
if ($pages_bootstrapped !== AKSRT_VERSION) {
|
||||
update_option('aksrt_template_pages_bootstrapped', AKSRT_VERSION);
|
||||
}
|
||||
} elseif ($requested_template_key !== '' && $requested_template_key !== 'profile') {
|
||||
} elseif ($requested_template_key !== '') {
|
||||
aksrt_ensure_template_page($requested_template_key);
|
||||
}
|
||||
|
||||
// 统一 flush 检查
|
||||
$rewrite_version = AKSRT_VERSION . ':orders-cart-revenue-routing-v1';
|
||||
// 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-v2';
|
||||
$flushed = get_option('aksrt_user_rules_flushed');
|
||||
if ($flushed !== $rewrite_version) {
|
||||
flush_rewrite_rules(false);
|
||||
@@ -168,24 +179,63 @@ add_action('save_post', function ($pid) { if (get_post_type($pid) === 'page') ak
|
||||
add_action('delete_post', function ($pid) { if (get_post_type($pid) === 'page') aksrt_clear_template_page_cache(); });
|
||||
|
||||
// ============================================================
|
||||
// 六、作者链接改写
|
||||
// 六、作者链接改写 — 强制覆盖 WP 默认 /author/{slug}/
|
||||
// ============================================================
|
||||
add_filter('author_link', function ($link, $author_id) {
|
||||
$author_id = (int) $author_id;
|
||||
if ($author_id > 0 && function_exists('aksrt_get_user_home_url')) {
|
||||
return aksrt_get_user_home_url($author_id);
|
||||
}
|
||||
$user = get_userdata($author_id);
|
||||
$slug = $user ? $user->user_nicename : 'user-' . $author_id;
|
||||
$slug = $user ? $user->user_nicename : ('user-' . max(0, $author_id));
|
||||
return home_url('/user/' . trailingslashit($slug));
|
||||
}, 10, 2);
|
||||
}, 99, 2);
|
||||
|
||||
// Pretty-permalink author archives → public user home.
|
||||
add_action('template_redirect', function () {
|
||||
if (!is_author()) return;
|
||||
$author_id = get_queried_object_id();
|
||||
if ($author_id) {
|
||||
$user = get_userdata($author_id);
|
||||
$slug = $user ? $user->user_nicename : 'user-' . $author_id;
|
||||
wp_safe_redirect(home_url('/user/' . trailingslashit($slug)), 301);
|
||||
if (is_admin() || wp_doing_ajax() || is_feed() || is_embed()) {
|
||||
return;
|
||||
}
|
||||
if (!is_author()) {
|
||||
return;
|
||||
}
|
||||
$author_id = (int) get_queried_object_id();
|
||||
if ($author_id <= 0) {
|
||||
$author_id = (int) get_query_var('author');
|
||||
}
|
||||
if ($author_id <= 0) {
|
||||
return;
|
||||
}
|
||||
$target = function_exists('aksrt_get_user_home_url')
|
||||
? aksrt_get_user_home_url($author_id)
|
||||
: home_url('/user/');
|
||||
if ($target === '' || $target === home_url('/')) {
|
||||
return;
|
||||
}
|
||||
wp_safe_redirect($target, 301);
|
||||
exit;
|
||||
}, 1);
|
||||
|
||||
// Catch bare ?author=ID when is_author() did not fire.
|
||||
add_action('template_redirect', function () {
|
||||
if (is_admin() || wp_doing_ajax()) {
|
||||
return;
|
||||
}
|
||||
if (empty($_GET['author'])) {
|
||||
return;
|
||||
}
|
||||
if (get_query_var('user_nicename') || get_query_var('aksrt_user_center')) {
|
||||
return;
|
||||
}
|
||||
if (is_author()) {
|
||||
return; // handled above
|
||||
}
|
||||
$author_id = (int) $_GET['author'];
|
||||
if ($author_id > 0 && function_exists('aksrt_get_user_home_url')) {
|
||||
wp_safe_redirect(aksrt_get_user_home_url($author_id), 301);
|
||||
exit;
|
||||
}
|
||||
});
|
||||
}, 2);
|
||||
|
||||
// ============================================================
|
||||
// AJAX: 保存用户个人资料(显示名称、简介、联系方式)
|
||||
|
||||
@@ -488,8 +488,16 @@ function aksrt_ensure_template_page(string $key): void {
|
||||
$cfg = $_aksrt_special_templates[$key];
|
||||
$desired_slug = sanitize_title($cfg['slug'] ?? $key);
|
||||
|
||||
// 已有页面使用此模板?
|
||||
if (aksrt_find_template_page($cfg['file'])) return;
|
||||
// 已有页面使用此模板?仍校验模板 meta,避免空白默认页。
|
||||
$found_page = aksrt_find_template_page($cfg['file']);
|
||||
if ($found_page instanceof WP_Post) {
|
||||
$current_tpl = get_post_meta((int) $found_page->ID, '_wp_page_template', true);
|
||||
if ($current_tpl !== $cfg['file']) {
|
||||
update_post_meta((int) $found_page->ID, '_wp_page_template', $cfg['file']);
|
||||
aksrt_clear_template_page_cache();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if ($desired_slug !== '') {
|
||||
$existing_page = get_page_by_path($desired_slug, OBJECT, 'page');
|
||||
|
||||
Reference in New Issue
Block a user