Files
wordpress-theme-aksrt/inc/modules/user-parts/account-ajax.php
T
kate522andClaude Opus 5 35bb96e82f refactor: rename the theme brand and every identifier from AKSRT to Jio
Full-project rename. `git grep -i aksrt` is now empty apart from the
DBMigrator 3.3.0 changelog note that documents the rename itself.

Code identifiers
- PHP namespace AKSRT\ -> Jio\ (composer PSR-4 + 378 declarations)
- global constants AKSRT_* -> JIO_* (VERSION/DIR/URI, PAY_TYPE_*, CAPTCHA_*)
- procedural functions, hooks, options, meta keys, table names,
  cookies, query vars, ajax actions and nonces: aksrt_ -> jio_
- text domain aksrt -> jio; Theme Name -> Jio
- JS globals: aksrtData/aksrtAdminData/... -> jioData/jioAdminData/...,
  window.AKSRTCaptcha & friends -> window.JioCaptcha, Aksrt* -> Jio*
- Vue components Aks*.vue -> Jio*.vue (15 files renamed + imports)
- CSS classes/data-attrs/custom properties: aksrt- -> jio-
- REST namespace aksrt/v1 -> jio/v1 (incl. payment gateway callbacks)

Storage layer
- DB tables {prefix}aksrt_* -> {prefix}jio_*; SCHEMA_VERSION 3.2.0 -> 3.3.0
  so dbDelta creates the new names on install
- widget id_base aksrt_widget_* -> jio_widget_*
- uninstall.php drop list rebuilt from the actual migrator tables
  (dropped 3 stale entries, added the 17 missing ones)

Removed
- hardcoded https://api.aksrt.com/version.json default update endpoint;
  get_endpoint() now reads the 「更新端点」 setting and do_check() no-ops
  when it is empty

Files renamed: inc/helpers/aksrt-*.php -> jio-*.php (54),
assets/admin/src/components/form/Aks*.vue -> Jio*.vue (15).
Build artifacts regenerated: assets/css/build, assets/js/runtime,
assets/admin/build.

Verified: audit-psr4-paths errors=0, audit-admin-contracts errors=0
(warnings=56, unchanged baseline), 813/813 widget-system tests,
44/44 phpunit, php -l clean across all PHP.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-11-01 20:05:04 +08:00

369 lines
13 KiB
PHP

<?php
/** User profile and account settings AJAX handlers. */
defined('ABSPATH') || exit;
if (!function_exists('jio_account_captcha_error_data')) {
function jio_account_captcha_error_data($result): array {
return [
'code' => is_wp_error($result) ? ($result->get_error_code() ?: 'jio_captcha_failed') : 'jio_captcha_failed',
'message' => is_wp_error($result)
? ($result->get_error_message() ?: __('人机验证失败,请重试', 'jio'))
: __('人机验证失败,请重试', 'jio'),
];
}
}
add_action('wp_ajax_jio_save_profile', function (): void {
if (
!isset($_POST['_profile_nonce'])
|| !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_profile_nonce'])), 'jio_profile_save')
) {
wp_send_json_error(['message' => __('安全验证失败,请刷新页面重试', 'jio')]);
}
$user_id = (int) ($_POST['user_id'] ?? 0);
$result = function_exists('jio_save_user_center_profile')
? jio_save_user_center_profile($user_id, $_POST)
: new WP_Error('jio_profile_unavailable', __('保存失败', 'jio'));
if (is_wp_error($result)) wp_send_json_error(['message' => $result->get_error_message()]);
wp_send_json_success(['message' => __('保存成功!', 'jio')]);
});
function jio_account_code_key(int $user_id, string $scene, string $target): string {
return 'jio_account_code_' . $user_id . '_' . sanitize_key($scene) . '_' . md5(strtolower(trim($target)));
}
function jio_account_store_code(int $user_id, string $scene, string $target, string $code): void {
set_transient(
jio_account_code_key($user_id, $scene, $target),
['hash' => wp_hash_password($code), 'attempts' => 0],
5 * MINUTE_IN_SECONDS
);
}
function jio_account_consume_code(int $user_id, string $scene, string $target, string $code, bool $consume = true): bool {
$key = jio_account_code_key($user_id, $scene, $target);
$stored = get_transient($key);
if (!is_array($stored) || empty($stored['hash'])) {
return false;
}
$attempts = (int) ($stored['attempts'] ?? 0) + 1;
if ($attempts > 6) {
delete_transient($key);
return false;
}
if (!wp_check_password($code, (string) $stored['hash'])) {
$stored['attempts'] = $attempts;
set_transient($key, $stored, 5 * MINUTE_IN_SECONDS);
return false;
}
if ($consume) {
delete_transient($key);
}
return true;
}
function jio_account_proof_key(int $user_id, string $kind): string {
return 'jio_account_proof_' . sanitize_key($kind) . '_' . $user_id;
}
function jio_account_set_proof(int $user_id, string $kind, string $target): void {
set_transient(
jio_account_proof_key($user_id, $kind),
['target' => strtolower(trim($target))],
30 * MINUTE_IN_SECONDS
);
}
function jio_account_consume_proof(int $user_id, string $kind, string $target, bool $consume = true): bool {
$key = jio_account_proof_key($user_id, $kind);
$proof = get_transient($key);
if (!is_array($proof) || strtolower(trim((string) ($proof['target'] ?? ''))) !== strtolower(trim($target))) {
return false;
}
if ($consume) {
delete_transient($key);
}
return true;
}
function jio_account_email_verification_required(): bool {
$settings = class_exists('\Jio\Settings\Settings') ? \Jio\Settings\Settings::get_all() : [];
return !empty($settings['email_set_captch']);
}
add_action('wp_ajax_jio_send_account_email_code', function (): void {
if (
!isset($_POST['_settings_nonce'])
|| !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_settings_nonce'])), 'jio_account_settings')
) {
wp_send_json_error(['message' => __('安全验证失败,请刷新页面重试', 'jio')]);
}
$user_id = get_current_user_id();
$user = wp_get_current_user();
if (!$user_id || !($user instanceof WP_User)) {
wp_send_json_error(['message' => __('请先登录', 'jio')]);
}
$scene = sanitize_key((string) wp_unslash($_POST['scene'] ?? ''));
if (!in_array($scene, ['verify_email', 'change_email'], true)) {
wp_send_json_error(['message' => __('无效的验证场景', 'jio')]);
}
if (!jio_account_email_verification_required()) {
wp_send_json_error(['message' => __('邮箱验证码验证未开启', 'jio')]);
}
$target = $scene === 'verify_email'
? (string) $user->user_email
: sanitize_email((string) wp_unslash($_POST['new_email'] ?? ''));
if (!is_email($target)) {
wp_send_json_error(['message' => __('邮箱格式错误', 'jio')]);
}
if ($scene === 'change_email') {
if (strtolower($target) === strtolower((string) $user->user_email)) {
wp_send_json_error(['message' => __('修改邮箱不能与现邮箱相同', 'jio')]);
}
if (email_exists($target)) {
wp_send_json_error(['message' => __('该邮箱已有绑定账号', 'jio')]);
}
}
if (function_exists('jio_require_captcha')) {
$captcha = jio_require_captcha('change_email', wp_unslash($_POST));
if ($captcha !== true) {
wp_send_json_error(jio_account_captcha_error_data($captcha));
}
}
if (class_exists('\Jio\Extensions\SendLimit')) {
$limit = \Jio\Extensions\SendLimit::check($target);
if (empty($limit['ok'])) {
wp_send_json_error(['message' => (string) $limit['message']]);
}
}
$code = str_pad((string) random_int(0, 999999), 6, '0', STR_PAD_LEFT);
$sent = class_exists('\Jio\Settings\Settings')
? \Jio\Settings\Settings::send_event_email('change_code', $target, [
'{username}' => $user->display_name,
'{site_name}' => get_bloginfo('name'),
'{code}' => $code,
])
: false;
if (!$sent) {
wp_send_json_error(['message' => __('验证码邮件发送失败,请稍后重试', 'jio')]);
}
$cooldown = class_exists('\Jio\Extensions\SendLimit') ? (int) \Jio\Extensions\SendLimit::record($target) : 0;
jio_account_store_code($user_id, $scene, $target, $code);
wp_send_json_success([
'message' => $scene === 'verify_email'
? __('验证码已发送到您的老邮箱', 'jio')
: __('验证码已发送到新邮箱', 'jio'),
'countdown' => $cooldown > 0 ? $cooldown : 60,
]);
});
add_action('wp_ajax_jio_verify_account_email', function (): void {
if (
!isset($_POST['_settings_nonce'])
|| !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_settings_nonce'])), 'jio_account_settings')
) {
wp_send_json_error(['message' => __('安全验证失败,请刷新页面重试', 'jio')]);
}
$user_id = get_current_user_id();
$user = wp_get_current_user();
if (!$user_id || !($user instanceof WP_User) || !is_email((string) $user->user_email)) {
wp_send_json_error(['message' => __('当前账号未绑定有效邮箱', 'jio')]);
}
if (!jio_account_email_verification_required()) {
wp_send_json_error(['message' => __('邮箱验证码验证未开启', 'jio')]);
}
if (function_exists('jio_require_captcha')) {
$captcha = jio_require_captcha('change_email', wp_unslash($_POST));
if ($captcha !== true) {
wp_send_json_error(jio_account_captcha_error_data($captcha));
}
}
$code = sanitize_text_field((string) wp_unslash($_POST['code'] ?? ''));
if (!jio_account_consume_code($user_id, 'verify_email', (string) $user->user_email, $code)) {
if (class_exists('\Jio\Extensions\SecurityHardening')) {
\Jio\Extensions\SecurityHardening::error_frequency_record();
}
wp_send_json_error(['message' => __('验证码错误或已过期', 'jio')]);
}
if (class_exists('\Jio\Extensions\SecurityHardening')) {
\Jio\Extensions\SecurityHardening::error_frequency_clear();
}
jio_account_set_proof($user_id, 'email_old', (string) $user->user_email);
wp_send_json_success(['message' => __('验证成功', 'jio')]);
});
add_action('wp_ajax_jio_change_password', function (): void {
if (
!isset($_POST['_settings_nonce'])
|| !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_settings_nonce'])), 'jio_account_settings')
) {
wp_send_json_error(['message' => __('安全验证失败,请刷新页面重试', 'jio')]);
}
$user_id = get_current_user_id();
if (!$user_id) {
wp_send_json_error(['message' => __('请先登录', 'jio')]);
}
$user = get_userdata($user_id);
if (!($user instanceof WP_User)) {
wp_send_json_error(['message' => __('账号信息读取失败,请刷新页面重试', 'jio')]);
}
$oauth_new = (bool) get_user_meta($user_id, 'oauth_new', true);
$password = trim((string) wp_unslash($_POST['password'] ?? ''));
$password2 = trim((string) wp_unslash($_POST['password2'] ?? ''));
if (function_exists('jio_require_captcha')) {
$captcha = jio_require_captcha('change_password', wp_unslash($_POST));
if ($captcha !== true) {
wp_send_json_error(jio_account_captcha_error_data($captcha));
}
}
if (!$oauth_new) {
$old_password = trim((string) wp_unslash($_POST['passwordold'] ?? ''));
if ($old_password === '') {
wp_send_json_error(['message' => __('请输入原密码', 'jio')]);
}
if ($old_password === $password) {
wp_send_json_error(['message' => __('新密码和原密码不能相同', 'jio')]);
}
if (class_exists('\Jio\Extensions\SecurityHardening') && ($wait = \Jio\Extensions\SecurityHardening::error_frequency_wait()) > 0) {
wp_send_json_error(['message' => sprintf(__('验证错误次数过多,请 %d 秒后再试', 'jio'), $wait)]);
}
if (!wp_check_password($old_password, $user->user_pass, $user_id)) {
if (class_exists('\Jio\Extensions\SecurityHardening')) {
\Jio\Extensions\SecurityHardening::error_frequency_record();
}
wp_send_json_error(['message' => __('原密码错误', 'jio')]);
}
}
$valid = class_exists('\Jio\Auth\Service')
? \Jio\Auth\Service::validate_password($password, $password2)
: __('认证服务不可用', 'jio');
if ($valid !== true) {
wp_send_json_error(['message' => (string) $valid]);
}
$result = wp_update_user(['ID' => $user_id, 'user_pass' => $password]);
if (is_wp_error($result)) {
wp_send_json_error(['message' => $result->get_error_message()]);
}
delete_user_meta($user_id, 'oauth_new');
if (class_exists('\Jio\Extensions\SecurityHardening')) {
\Jio\Extensions\SecurityHardening::error_frequency_clear();
}
wp_send_json_success([
'message' => $oauth_new
? __('密码设置成功', 'jio')
: __('修改成功,下次请使用新密码登录', 'jio'),
'reload' => true,
]);
});
add_action('wp_ajax_jio_change_email', function (): void {
if (
!isset($_POST['_settings_nonce'])
|| !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_settings_nonce'])), 'jio_account_settings')
) {
wp_send_json_error(['message' => __('安全验证失败,请刷新页面重试', 'jio')]);
}
$user_id = get_current_user_id();
$user = wp_get_current_user();
if (!$user_id || !($user instanceof WP_User)) {
wp_send_json_error(['message' => __('请先登录', 'jio')]);
}
$new_email = sanitize_email((string) wp_unslash($_POST['new_email'] ?? ''));
if (!is_email($new_email)) {
wp_send_json_error(['message' => __('邮箱格式错误', 'jio')]);
}
if ($user->user_email !== '' && strtolower($new_email) === strtolower((string) $user->user_email)) {
wp_send_json_error(['message' => __('修改邮箱不能与现邮箱相同', 'jio')]);
}
if (email_exists($new_email)) {
wp_send_json_error(['message' => __('该邮箱已有绑定账号', 'jio')]);
}
$agreement = function_exists('jio_get_auth_agreement_view') ? jio_get_auth_agreement_view() : ['enabled' => false, 'required' => false];
if (!empty($agreement['required']) && empty($_POST['agree_terms'])) {
wp_send_json_error(['message' => __('请先同意相关协议', 'jio')]);
}
if (function_exists('jio_require_captcha')) {
$captcha = jio_require_captcha('change_email', wp_unslash($_POST));
if ($captcha !== true) {
wp_send_json_error(jio_account_captcha_error_data($captcha));
}
}
$verification_required = jio_account_email_verification_required();
$old_email = (string) $user->user_email;
if (
$old_email !== ''
&& $verification_required
&& !jio_account_consume_proof($user_id, 'email_old', $old_email, false)
) {
wp_send_json_error(['message' => __('请先验证老邮箱', 'jio')]);
}
if (
$verification_required
&& !jio_account_consume_code(
$user_id,
'change_email',
$new_email,
sanitize_text_field((string) wp_unslash($_POST['code'] ?? '')),
false
)
) {
if (class_exists('\Jio\Extensions\SecurityHardening')) {
\Jio\Extensions\SecurityHardening::error_frequency_record();
}
wp_send_json_error(['message' => __('验证码错误或已过期', 'jio')]);
}
$result = wp_update_user(['ID' => $user_id, 'user_email' => $new_email]);
if (is_wp_error($result)) {
wp_send_json_error(['message' => $result->get_error_message() ?: __('邮箱更新失败,请稍后重试', 'jio')]);
}
if ($old_email !== '' && $verification_required) {
delete_transient(jio_account_proof_key($user_id, 'email_old'));
}
if ($verification_required) {
delete_transient(jio_account_code_key($user_id, 'change_email', $new_email));
}
if (class_exists('\Jio\Extensions\SecurityHardening')) {
\Jio\Extensions\SecurityHardening::error_frequency_clear();
}
update_user_meta($user_id, 'jio_email_verified', 1);
if ($old_email !== '' && is_email($old_email) && class_exists('\Jio\Settings\Settings')) {
\Jio\Settings\Settings::send_event_email('change_notice', $old_email, [
'{username}' => $user->display_name,
'{site_name}' => get_bloginfo('name'),
'{new_email}' => $new_email,
]);
}
wp_send_json_success([
'message' => $old_email !== '' ? __('邮箱修改成功', 'jio') : __('邮箱绑定成功', 'jio'),
'reload' => true,
]);
});