Move 288 types from global AKSRT_* + inc/classes manual requires into
namespace AKSRT\{Domain} under src/AKSRT/, wire composer.json psr-4,
and slim bootstrap to helpers/modules/hooks only. Split multi-class
widget/gateway files, update admin contract audit paths, and document
the layout in ADR/README.
182 lines
6.2 KiB
PHP
182 lines
6.2 KiB
PHP
<?php
|
|
/**
|
|
* User rewrite/routing/bootstrap flush + author redirects
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
// ============================================================
|
|
// 三、URL 路由与模板加载
|
|
// ============================================================
|
|
|
|
// --- 3a. /user/{昵称} 自定义重写 ---
|
|
add_filter('query_vars', function ($vars) {
|
|
$vars[] = 'aksrt_user_center';
|
|
$vars[] = 'user_nicename';
|
|
return $vars;
|
|
});
|
|
|
|
add_action('template_redirect', function () {
|
|
if (is_admin() || wp_doing_ajax() || is_feed() || is_embed() || is_preview()) {
|
|
return;
|
|
}
|
|
|
|
$resolved_path = aksrt_get_current_special_template_path();
|
|
if ($resolved_path === '') {
|
|
return;
|
|
}
|
|
|
|
status_header(200);
|
|
load_template($resolved_path, true);
|
|
exit;
|
|
}, 100);
|
|
|
|
add_action('init', function () {
|
|
add_rewrite_tag('%aksrt_user_center%', '([^&]+)');
|
|
add_rewrite_tag('%user_nicename%', '([^/]+)');
|
|
add_rewrite_rule('^profile/?$', 'index.php?aksrt_user_center=1', 'top');
|
|
add_rewrite_rule('^user/([^/]+)/?$', 'index.php?user_nicename=$matches[1]', 'top');
|
|
});
|
|
|
|
// --- 3b. 统一模板拦截器(核心)---
|
|
// 拦截所有使用主题特殊模板的页面请求,确保正确加载
|
|
add_filter('template_include', function ($template) {
|
|
$resolved_path = aksrt_get_current_special_template_path();
|
|
if ($resolved_path !== '') {
|
|
return $resolved_path;
|
|
}
|
|
|
|
return $template;
|
|
|
|
global $_aksrt_special_templates;
|
|
|
|
// A) /user/{昵称} 路由 → 加载公开主页模板
|
|
$nicename = get_query_var('user_nicename');
|
|
if (!empty($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 (!$tpl) return $template;
|
|
|
|
global $aksrt_profile_user;
|
|
$aksrt_profile_user = $user;
|
|
return $tpl;
|
|
}
|
|
|
|
// B) 普通页面 → 检测是否使用了我们的特殊模板,确保加载正确的模板文件
|
|
// (防止 WP 在某些情况下未能正确解析自定义页面模板)
|
|
if (is_page()) {
|
|
$page_id = get_queried_object_id();
|
|
if ($page_id) {
|
|
$page_template = get_post_meta($page_id, '_wp_page_template', true);
|
|
if ($page_template && strpos($page_template, 'templates/pages/') === 0) {
|
|
$tpl = aksrt_locate_template_file($page_template);
|
|
if ($tpl) return $tpl;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $template;
|
|
}, 99);
|
|
|
|
add_filter('template_include', function ($template) {
|
|
$resolved_path = aksrt_get_current_special_template_path();
|
|
if ($resolved_path !== '') {
|
|
return $resolved_path;
|
|
}
|
|
|
|
if (get_query_var('aksrt_user_center')) {
|
|
$profile_template = aksrt_locate_template_file('templates/pages/page-profile.php');
|
|
if ($profile_template) {
|
|
return $profile_template;
|
|
}
|
|
}
|
|
|
|
if (is_page()) {
|
|
$page_id = get_queried_object_id();
|
|
if ($page_id > 0) {
|
|
$page_template = get_post_meta($page_id, '_wp_page_template', true);
|
|
$resolved_template = aksrt_resolve_page_template_file(
|
|
is_string($page_template) ? $page_template : '',
|
|
(string) get_post_field('post_name', $page_id)
|
|
);
|
|
|
|
if ($resolved_template !== '') {
|
|
$resolved_path = aksrt_locate_template_file($resolved_template);
|
|
if ($resolved_path) {
|
|
return $resolved_path;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return $template;
|
|
}, 100);
|
|
|
|
// ============================================================
|
|
// 四、自动维护 + flush
|
|
// ============================================================
|
|
add_action('init', function () {
|
|
global $_aksrt_special_templates;
|
|
|
|
// 后台/AJAX:检查并创建缺失页面
|
|
$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') {
|
|
aksrt_ensure_template_page($requested_template_key);
|
|
}
|
|
|
|
// 统一 flush 检查
|
|
$rewrite_version = AKSRT_VERSION . ':sign-profile-routing-v1';
|
|
$flushed = get_option('aksrt_user_rules_flushed');
|
|
if ($flushed !== $rewrite_version) {
|
|
flush_rewrite_rules(false);
|
|
update_option('aksrt_user_rules_flushed', $rewrite_version);
|
|
}
|
|
});
|
|
|
|
// 页面变更时清缓存
|
|
add_action('save_post', function ($pid) { if (get_post_type($pid) === 'page') aksrt_clear_template_page_cache(); });
|
|
add_action('delete_post', function ($pid) { if (get_post_type($pid) === 'page') aksrt_clear_template_page_cache(); });
|
|
|
|
// ============================================================
|
|
// 六、作者链接改写
|
|
// ============================================================
|
|
add_filter('author_link', function ($link, $author_id) {
|
|
$user = get_userdata($author_id);
|
|
$slug = $user ? $user->user_nicename : 'user-' . $author_id;
|
|
return home_url('/user/' . trailingslashit($slug));
|
|
}, 10, 2);
|
|
|
|
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);
|
|
exit;
|
|
}
|
|
});
|
|
|
|
// ============================================================
|
|
// AJAX: 保存用户个人资料(显示名称、简介、联系方式)
|