refactor(classes): organize inc/classes into domain folders

Move ~94 root class shells and root *-traits into domain directories
(core, auth, user, credits, content, payment/gateways, forum, shop,
sms, ai, messages, extensions, widgets, settings, admin). Update
bootstrap require paths, co-locate traits with owners, fix forum/user
relative requires, drop duplicate shop controller requires, and wire
home-modules load. Path-only; class names and init order unchanged.
This commit is contained in:
kate522 committed 2026-07-17 11:11:57 +08:00
1 parent 2a8fe68048
commit ce1c13b947
115 files changed
+171 -224

No files matched your search

@@ -0,0 +1,396 @@
<?php
defined('ABSPATH') || exit;
trait AKSRT_OAuth_Part_1 {
private function get_baidu_user_data(array $config, string $code, string $redirect_uri): array {
$oauth = new \Yurun\OAuthLogin\Baidu\OAuth2(
$config['client_id'],
$config['client_secret'],
$redirect_uri
);
$oauth->getAccessToken($_GET['state'] ?? '', $code, $_GET['state'] ?? null);
$userInfo = $oauth->getUserInfo();
return [
'provider' => 'baidu',
'provider_id' => (string) ($userInfo['userid'] ?? ''),
'username' => $userInfo['username'] ?? '',
'name' => $userInfo['realname'] ?? $userInfo['username'] ?? '',
'email' => $userInfo['email'] ?? '',
'avatar_url' => $userInfo['portrait'] ? ('https://himg.bdimg.com/sys/portrait/item/' . $userInfo['portrait'] . '.jpg') : '',
];
}
private function get_alipay_user_data(array $config, string $code, string $redirect_uri): array {
$oauth = new \Yurun\OAuthLogin\Alipay\OAuth2(
$config['client_id'],
$config['client_secret'],
$redirect_uri
);
$oauth->getAccessToken($_GET['state'] ?? '', $code, $_GET['state'] ?? null);
$userInfo = $oauth->getUserInfo();
return [
'provider' => 'alipay',
'provider_id' => (string) ($userInfo['user_id'] ?? ''),
'username' => $userInfo['user_name'] ?? '',
'name' => $userInfo['user_name'] ?? '',
'email' => $userInfo['email'] ?? '',
'avatar_url' => $userInfo['avatar'] ?? '',
'gender' => ($userInfo['gender'] ?? '') === 'm' ? 'male' : (($userInfo['gender'] ?? '') === 'f' ? 'female' : ''),
];
}
private function get_google_auth_url(array $config, string $redirect_uri, string $state): string {
$params = http_build_query([
'client_id' => $config['client_id'],
'redirect_uri' => $redirect_uri,
'response_type' => 'code',
'scope' => 'openid email profile',
'state' => $state,
'access_type' => 'offline',
]);
return 'https://accounts.google.com/o/oauth2/v2/auth?' . $params;
}
private function get_google_user_data(array $config, string $code, string $redirect_uri): array {
// 换取 token
$response = wp_remote_post('https://oauth2.googleapis.com/token', [
'body' => [
'code' => $code,
'client_id' => $config['client_id'],
'client_secret' => $config['client_secret'],
'redirect_uri' => $redirect_uri,
'grant_type' => 'authorization_code',
],
]);
if (is_wp_error($response)) {
throw new \Exception($response->get_error_message());
}
$token_data = json_decode(wp_remote_retrieve_body($response), true);
if (empty($token_data['access_token'])) {
throw new \Exception(__('Google token 获取失败', 'aksrt'));
}
// 获取用户信息
$user_response = wp_remote_get('https://www.googleapis.com/oauth2/v2/userinfo', [
'headers' => ['Authorization' => 'Bearer ' . $token_data['access_token']],
]);
if (is_wp_error($user_response)) {
throw new \Exception($user_response->get_error_message());
}
$user_info = json_decode(wp_remote_retrieve_body($user_response), true);
return [
'provider' => 'google',
'provider_id' => (string) ($user_info['id'] ?? ''),
'username' => $user_info['email'] ?? '',
'name' => $user_info['name'] ?? '',
'email' => $user_info['email'] ?? '',
'email_verified' => !empty($user_info['verified_email']),
'avatar_url' => $user_info['picture'] ?? '',
'locale' => $user_info['locale'] ?? '',
];
}
/**
* 微信公众号 OAuth2 授权 URL
*
* 使用 snsapi_userinfo scope 获取用户信息(需已认证服务号或订阅号+验证)。
*/
private function get_weixingzh_auth_url(array $config, string $redirect_uri, string $state): string {
$params = http_build_query([
'appid' => $config['client_id'],
'redirect_uri' => $redirect_uri,
'response_type' => 'code',
'scope' => 'snsapi_userinfo',
'state' => $state,
]);
return 'https://open.weixin.qq.com/connect/oauth2/authorize?' . $params . '#wechat_redirect';
}
/**
* 微信公众号 OAuth2 获取用户信息
*
* 1. code → access_token + openid
* 2. access_token + openid → userinfo
*/
private function get_weixingzh_user_data(array $config, string $code, string $redirect_uri): array {
// 1. 换取 access_token
$token_url = add_query_arg([
'appid' => $config['client_id'],
'secret' => $config['client_secret'],
'code' => $code,
'grant_type' => 'authorization_code',
], 'https://api.weixin.qq.com/sns/oauth2/access_token');
$response = wp_remote_get($token_url);
if (is_wp_error($response)) {
throw new \Exception($response->get_error_message());
}
$token_data = json_decode(wp_remote_retrieve_body($response), true);
if (empty($token_data['access_token']) || empty($token_data['openid'])) {
$errmsg = $token_data['errmsg'] ?? __('微信公众号 token 获取失败', 'aksrt');
throw new \Exception($errmsg);
}
// 2. 获取用户信息
$user_url = add_query_arg([
'access_token' => $token_data['access_token'],
'openid' => $token_data['openid'],
'lang' => 'zh_CN',
], 'https://api.weixin.qq.com/sns/userinfo');
$user_response = wp_remote_get($user_url);
if (is_wp_error($user_response)) {
throw new \Exception($user_response->get_error_message());
}
$user_info = json_decode(wp_remote_retrieve_body($user_response), true);
return [
'provider' => 'weixingzh',
'provider_id' => (string) ($user_info['openid'] ?? $token_data['openid']),
'username' => $user_info['nickname'] ?? '',
'name' => $user_info['nickname'] ?? '',
'email' => '',
'email_verified' => false,
'avatar_url' => $user_info['headimgurl'] ?? '',
'locale' => 'zh_CN',
'unionid' => $user_info['unionid'] ?? '',
];
}
private function resolve_callback_provider(): string {
$state = $_GET['state'] ?? '';
if ($state === '') return '';
$rest = (strpos($state, 'bind_') === 0) ? substr($state, 5) : $state;
$sep = strpos($rest, '_');
if ($sep !== false && $sep < strlen($rest) - 1) {
return sanitize_key(substr($rest, 0, $sep));
}
return '';
}
private function get_enabled_providers(): array {
if (class_exists('AKSRT_Settings') && AKSRT_Settings::oauth_proxy_mode() === 'client') {
return AKSRT_Settings::oauth_proxy_client_providers();
}
$providers = get_option('aksrt_oauth', []);
$enabled = [];
foreach (['github', 'gitee', 'qq', 'google', 'weibo', 'weixin', 'baidu', 'alipay', 'weixingzh'] as $key) {
if (!empty($providers[$key]['enabled']) && !empty($providers[$key]['client_id'])) {
$enabled[] = $key;
continue;
}
// BC: also check aksrt_settings flat keys
if (class_exists('AKSRT_Settings')) {
$s = AKSRT_Settings::get_all();
$switch_key = 'oauth_' . $key . '_s';
if (!empty($s[$switch_key])) {
$id_key = $key . '_appid';
$sec_key = $key . '_appsecret';
if ($key === 'weixingzh') {
$sec_key = 'weixingzh_appsecret';
} elseif ($key === 'github' || $key === 'gitee' || $key === 'qq') {
$sec_key = $key . '_appkey';
}
if (!empty($s[$id_key]) && !empty($s[$sec_key])) {
$enabled[] = $key;
}
}
}
}
return array_unique($enabled);
}
/**
* 获取 OAuth 提供商数据(供模板使用)
* 返回 [{name, label, icon, url}, ...]
*/
public function get_auth_providers_data(): array {
$platform_names = [
'github' => 'GitHub',
'gitee' => 'Gitee',
'qq' => 'QQ',
'google' => 'Google',
'weibo' => 'Weibo',
'weixin' => 'WeChat',
'baidu' => 'Baidu',
'alipay' => 'Alipay',
'weixingzh' => '微信公众号',
];
$result = [];
foreach ($this->get_enabled_providers() as $key) {
$url = $this->get_auth_url($key);
if (empty($url)) continue;
$result[] = [
'name' => $key,
'label' => $platform_names[$key] ?? ucfirst($key),
'icon' => function_exists('aksrt_brand_icon') ? aksrt_brand_icon((string) $key) : '',
'url' => esc_url($url),
];
}
return $result;
}
private function get_provider_config(string $provider): ?array {
$providers = get_option('aksrt_oauth', []);
$config = $providers[$provider] ?? null;
if (!empty($config['client_id']) && !empty($config['client_secret'])) {
return $config;
}
// BC: fallback to aksrt_settings flat keys
if (class_exists('AKSRT_Settings')) {
$s = AKSRT_Settings::get_all();
$id_key = $provider . '_appid';
$sec_key = $provider . '_appsecret';
if ($provider === 'github' || $provider === 'gitee' || $provider === 'qq') {
$sec_key = $provider . '_appkey';
} elseif ($provider === 'google') {
$id_key = 'google_client_id';
$sec_key = 'google_client_secret';
}
$client_id = (string) ($s[$id_key] ?? '');
$client_secret = (string) ($s[$sec_key] ?? '');
if ($client_id !== '' && $client_secret !== '') {
return [
'client_id' => $client_id,
'client_secret' => $client_secret,
'redirect_uri' => home_url('/oauth/callback/'),
];
}
}
return null;
}
private function find_or_create_user($provider, $user_data) {
$email = $user_data['email'] ?? '';
if (!empty($user_data['provider_id'])) {
$by_provider = $this->find_user_by_provider($provider, (string) $user_data['provider_id']);
if ($by_provider instanceof WP_User) {
$bindings = get_user_meta($by_provider->ID, 'aksrt_oauth_bindings', true) ?: [];
if (!is_array($bindings)) $bindings = [];
if (!in_array($provider, $bindings, true)) {
$bindings[] = $provider;
update_user_meta($by_provider->ID, 'aksrt_oauth_bindings', $bindings);
}
if (!empty($user_data['avatar_url'])) {
update_user_meta($by_provider->ID, 'aksrt_oauth_avatar', esc_url_raw($user_data['avatar_url']));
}
return $by_provider;
}
}
if (!empty($email)) {
$existing = get_user_by('email', $email);
if ($existing && !$existing instanceof WP_Error) {
$bindings = get_user_meta($existing->ID, 'aksrt_oauth_bindings', true) ?: [];
if (!is_array($bindings)) $bindings = [];
if (!in_array($provider, $bindings, true)) {
$bindings[] = $provider;
update_user_meta($existing->ID, 'aksrt_oauth_bindings', $bindings);
}
if (!empty($user_data['provider_id'])) {
update_user_meta($existing->ID, "aksrt_{$provider}_id", (string) $user_data['provider_id']);
}
if (!empty($user_data['avatar_url'])) {
update_user_meta($existing->ID, 'aksrt_oauth_avatar', esc_url_raw($user_data['avatar_url']));
}
return $existing;
}
}
return new WP_Error(
'oauth_no_match',
__('需要绑定已有账号', 'aksrt'),
[
'provider' => $provider,
'user_data' => $user_data,
]
);
}
// ============================================
// 前端UI方法
// ============================================
public function login_button(): void {
echo $this->render_social_buttons(__('使用以下方式快速登录', 'aksrt'));
}
public function register_button(): void {
echo $this->render_social_buttons(__('使用以下方式快速注册', 'aksrt'));
}
public function render_social_buttons(string $title = ''): string {
$providers = get_option('aksrt_oauth', []);
$enabled_providers = [];
foreach ($providers as $key => $cfg) {
if (!empty($cfg['enabled']) && !empty($cfg['client_id']) && in_array($key, ['github', 'gitee', 'qq', 'google', 'weibo', 'weixin', 'baidu', 'alipay'], true)) {
$enabled_providers[$key] = $cfg;
}
}
if (empty($enabled_providers)) return '';
$icons = [
'github' => aksrt_brand_icon('github'),
'gitee' => aksrt_brand_icon('gitee'),
'qq' => aksrt_brand_icon('qq'),
'google' => aksrt_brand_icon('google'),
'weibo' => aksrt_brand_icon('weibo'),
'weixin' => aksrt_brand_icon('weixin'),
'baidu' => aksrt_brand_icon('baidu'),
'alipay' => aksrt_brand_icon('alipay'),
];
ob_start();
?>
<div class="social-login-divider">
<span><?php echo esc_html($title) ?: esc_html__('or', 'aksrt'); ?></span>
</div>
<div class="oauth-row">
<?php foreach ($enabled_providers as $key => $cfg):
$url = $this->get_auth_url($key);
if (empty($url)) continue;
?>
<a href="<?php echo esc_url($url); ?>"
class="oauth-btn"
aria-label="<?php echo esc_attr(ucfirst($key)); ?>"
title="<?php echo esc_attr(ucfirst($key)); ?>">
<?php echo $icons[$key] ?? ''; ?>
</a>
<?php endforeach; ?>
</div>
<?php
return ob_get_clean();
}
public function get_login_buttons(): string {
return $this->render_social_buttons();
}
public function login_message(string $message): string {
return $message . self::get_instance()->render_social_buttons();
}
public function register_message(string $message): string {
return $message . self::get_instance()->render_social_buttons(__('或使用社交账号快速注册', 'aksrt'));
}
}
@@ -0,0 +1,166 @@
<?php
defined('ABSPATH') || exit;
trait AKSRT_OAuth_Part_2 {
public function get_frontend_bindings_data(int $user_id): array {
$providers = get_option('aksrt_oauth', []);
$bound_list = get_user_meta($user_id, 'aksrt_oauth_bindings', true) ?: [];
if (!is_array($bound_list)) $bound_list = [];
$platform_names = [
'github' => 'GitHub', 'gitee' => 'Gitee',
'qq' => 'QQ', 'google' => 'Google',
'weibo' => 'Weibo', 'weixin' => 'WeChat',
'baidu' => 'Baidu', 'alipay' => 'Alipay',
];
$icons = [
'github' => aksrt_brand_icon('github'),
'gitee' => aksrt_brand_icon('gitee'),
'qq' => aksrt_brand_icon('qq'),
'google' => aksrt_brand_icon('google'),
'weibo' => aksrt_brand_icon('weibo'),
'weixin' => aksrt_brand_icon('weixin'),
'baidu' => aksrt_brand_icon('baidu'),
'alipay' => aksrt_brand_icon('alipay'),
];
$bindings = [];
foreach ($bound_list as $binding) {
if (in_array($binding, ['github', 'gitee', 'qq', 'google', 'weibo', 'weixin', 'baidu', 'alipay'], true) && !empty($providers[$binding]['enabled'])) {
$bindings[] = [
'key' => $binding,
'name' => $platform_names[$binding] ?? ucfirst($binding),
'icon' => $icons[$binding] ?? '',
];
}
}
$available = [];
foreach (['github', 'gitee', 'qq', 'google', 'weibo', 'weixin', 'baidu', 'alipay'] as $key) {
if (in_array($key, $bound_list, true)) continue;
if (empty($providers[$key]['enabled']) || empty($providers[$key]['client_id'])) continue;
$url = $this->get_auth_url($key, 'bind');
if (empty($url)) continue;
$available[] = [
'key' => $key,
'name' => $platform_names[$key],
'icon' => $icons[$key] ?? '',
'url' => esc_url($url),
];
}
return [
'bindings' => $bindings,
'available' => $available,
];
}
public function profile_bindings(WP_User $user): void {
$bindings = get_user_meta($user->ID, 'aksrt_oauth_bindings', true) ?: [];
if (empty($bindings)) return;
$unbind_nonce = wp_create_nonce('aksrt_oauth_unbind');
?>
<h2><?php _e('社交账号绑定', 'aksrt'); ?></h2>
<table class="form-table">
<?php foreach ($bindings as $binding): ?>
<tr>
<th scope="row"><?php echo esc_html(ucfirst($binding)); ?></th>
<td><span class="badge" style="background:#d1fae5;color:#065f46;padding:4px 12px;border-radius:12px;font-size:12px;">&#10003; <?php esc_html_e('已绑定', 'aksrt'); ?></span></td>
<td>
<button type="button" class="button button-link-delete aksrt-oauth-unbind-btn"
data-provider="<?php echo esc_attr($binding); ?>"
data-user-id="<?php echo esc_attr((string) $user->ID); ?>">
<?php _e('解除绑定', 'aksrt'); ?>
</button>
</td>
</tr>
<?php endforeach; ?>
</table>
<script>
(function(){
var nonce = <?php echo wp_json_encode($unbind_nonce); ?>;
var ajaxUrl = <?php echo wp_json_encode(admin_url('admin-ajax.php')); ?>;
document.querySelectorAll('.aksrt-oauth-unbind-btn').forEach(function(btn){
btn.addEventListener('click', function(){
var msg = <?php echo wp_json_encode(__('确定要解除绑定吗?', 'aksrt')); ?>;
if(!window.confirm(msg)) return;
var fd = new FormData();
fd.append('action', 'aksrt_oauth_unbind');
fd.append('nonce', nonce);
fd.append('provider', btn.getAttribute('data-provider'));
fd.append('userId', btn.getAttribute('data-user-id'));
fetch(ajaxUrl, { method: 'POST', credentials: 'same-origin', body: fd })
.then(function(r){ return r.json(); })
.then(function(d){ if(d && d.success){ location.reload(); } });
});
});
})();
</script>
<?php
}
public function ajax_unbind(): void {
check_ajax_referer('aksrt_oauth_unbind', 'nonce');
$provider = sanitize_key($_POST['provider'] ?? '');
$user_id = isset($_POST['userId']) ? (int) $_POST['userId'] : 0;
$current_id = get_current_user_id();
if (empty($provider) || !in_array($provider, ['github', 'gitee', 'qq', 'google', 'weibo', 'weixin', 'baidu', 'alipay'], true)) {
wp_send_json_error(400);
}
if ($current_id !== $user_id || !current_user_can('read')) {
wp_send_json_error(403);
}
$bindings = get_user_meta($current_id, 'aksrt_oauth_bindings', true) ?: [];
$bindings = array_values(array_diff($bindings, [$provider]));
update_user_meta($current_id, 'aksrt_oauth_bindings', $bindings);
delete_user_meta($current_id, "aksrt_{$provider}_id");
delete_user_meta($current_id, "aksrt_{$provider}_token");
wp_send_json_success(['unbound' => true]);
}
private function find_user_by_provider(string $provider, string $provider_id): ?WP_User {
if (empty($provider_id)) return null;
$users = get_users([
'meta_key' => "aksrt_{$provider}_id",
'meta_value' => $provider_id,
'number' => 1,
]);
return !empty($users[0]) ? $users[0] : null;
}
private function bind_provider_to_user(int $user_id, string $provider, array $user_data): void {
if (!empty($user_data['provider_id'])) {
update_user_meta($user_id, "aksrt_{$provider}_id", $user_data['provider_id']);
}
$bindings = get_user_meta($user_id, 'aksrt_oauth_bindings', true) ?: [];
if (!is_array($bindings)) $bindings = [];
if (!in_array($provider, $bindings, true)) {
$bindings[] = $provider;
update_user_meta($user_id, 'aksrt_oauth_bindings', $bindings);
}
if (!empty($user_data['avatar_url'])) {
update_user_meta($user_id, 'aksrt_oauth_avatar', esc_url_raw($user_data['avatar_url']));
}
}
private function get_profile_url(): string {
if (function_exists('aksrt_get_user_center_url')) {
return aksrt_get_user_center_url();
}
$user = wp_get_current_user();
if ($user->ID) {
return home_url('/user/' . $user->user_nicename . '/');
}
return home_url('/');
}
}