Files
wordpress-theme-aksrt/src/Auth/ServicePart1a.php
T

194 lines
6.9 KiB
PHP

<?php
/**
* @package AKSRT
*/
namespace AKSRT\Auth;
defined('ABSPATH') || exit;
trait ServicePart1a {
public static function register_user(array $post): array {
$user_login = sanitize_user(wp_unslash($post['user_login'] ?? ''), true);
$user_email = sanitize_email(wp_unslash($post['user_email'] ?? ''));
$password = (string) ($post['user_pass'] ?? '');
$confirm = (string) ($post['user_pass_confirm'] ?? '');
if (
class_exists('\AKSRT\Settings\Settings')
&& \AKSRT\Settings\Settings::signup_verification_enabled()
&& empty($post['_auth_code_verified'])
) {
return ['success' => false, 'error' => __('请先完成邮箱或手机验证', 'aksrt')];
}
$agreement = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::user_agreement_settings()
: ['enabled' => false, 'privacy' => false, 'required' => false];
if ((!empty($agreement['enabled']) || !empty($agreement['privacy'])) && !empty($agreement['required']) && empty($post['agree_terms'])) {
return ['success' => false, 'error' => __('请先同意服务条款', 'aksrt')];
}
if (empty($user_login) || !validate_username($user_login)) {
return ['success' => false, 'error' => __('请输入有效的用户名', 'aksrt')];
}
if (mb_strlen($user_login) < 3 || mb_strlen($user_login) > 60) {
return ['success' => false, 'error' => __('用户名长度需在 3-60 个字符之间', 'aksrt')];
}
if (class_exists('\AKSRT\Settings\Settings')) {
foreach (\AKSRT\Settings\Settings::nickname_blacklist() as $keyword) {
if ($keyword !== '' && mb_stripos($user_login, $keyword) !== false) {
return ['success' => false, 'error' => __('用户名包含禁止使用的关键词', 'aksrt')];
}
}
}
if (username_exists($user_login)) {
return ['success' => false, 'error' => __('该用户名已被使用', 'aksrt')];
}
if (empty($user_email) || !is_email($user_email)) {
return ['success' => false, 'error' => __('请输入有效的邮箱地址', 'aksrt')];
}
if (class_exists('\AKSRT\Settings\Settings')) {
$email_limit = \AKSRT\Settings\Settings::email_limit();
$matches = false;
foreach ((array) ($email_limit['domains'] ?? []) as $keyword) {
if ($keyword !== '' && stripos($user_email, (string) $keyword) !== false) {
$matches = true;
break;
}
}
if ((($email_limit['type'] ?? 'not') === 'not' && $matches) || (($email_limit['type'] ?? 'not') === 'allow' && !empty($email_limit['domains']) && !$matches)) {
return ['success' => false, 'error' => __('该邮箱不符合本站注册规则', 'aksrt')];
}
}
if (email_exists($user_email)) {
return ['success' => false, 'error' => __('该邮箱已被其他账号使用', 'aksrt')];
}
if (
class_exists('\AKSRT\Settings\Settings')
&& \AKSRT\Settings\Settings::signup_verification_enabled()
&& \AKSRT\Settings\Settings::signup_omit_password_confirmation()
) {
$confirm = $password;
}
$password_check = self::validate_password($password, $confirm);
if ($password_check !== true) {
return ['success' => false, 'error' => $password_check];
}
if (empty($post['_auth_code_verified']) && function_exists('aksrt_require_captcha')) {
$captcha_result = aksrt_require_captcha('register', $post);
if ($captcha_result !== true) {
return ['success' => false, 'error' => $captcha_result];
}
}
$ref_service = class_exists('\AKSRT\Payment\ReferralAttribution')
? \AKSRT\Payment\ReferralAttribution::get_instance()
: null;
if ($ref_service) {
$ref_service->clear_registration_context();
}
$registration = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::get_referral_registration_settings()
: ['enabled' => false, 'register_field' => 'hidden', 'reject_invalid' => true];
$submitted_ref = isset($post['ref']) ? sanitize_key((string) wp_unslash($post['ref'])) : '';
$field_mode = !empty($registration['enabled'])
? (string) ($registration['register_field'] ?? 'hidden')
: 'hidden';
$accept_submitted = !empty($registration['enabled']) && $field_mode !== 'hidden';
$ref_context = $ref_service
? $ref_service->prepare_registration($accept_submitted ? $submitted_ref : '')
: ['ref' => '', 'valid' => false, 'submitted_valid' => false, 'conflict' => false, 'source' => 'none'];
if ($field_mode === 'required' && empty($ref_context['valid'])) {
if ($submitted_ref === '') {
if ($ref_service) {
$ref_service->clear_registration_context();
}
return ['success' => false, 'error' => __('请填写推广码', 'aksrt')];
}
if (!empty($registration['reject_invalid'])) {
if ($ref_service) {
$ref_service->clear_registration_context();
}
return ['success' => false, 'error' => __('推广码无效,请检查后重试', 'aksrt')];
}
}
if ($submitted_ref !== '' && empty($ref_context['valid']) && !empty($registration['reject_invalid'])) {
if ($ref_service) {
$ref_service->clear_registration_context();
}
return ['success' => false, 'error' => __('推广码无效,请检查后重试', 'aksrt')];
}
$user_id = wp_create_user($user_login, $password, $user_email);
if (is_wp_error($user_id)) {
if ($ref_service) {
$ref_service->clear_registration_context();
}
return ['success' => false, 'error' => $user_id->get_error_message()];
}
$ref_notice = $ref_service ? $ref_service->take_registration_notice() : '';
$user = get_userdata($user_id);
if (!$user || is_wp_error($user)) {
return ['success' => false, 'error' => __('注册成功,但读取用户信息失败,请联系管理员', 'aksrt')];
}
self::mark_email_verified($user_id);
wp_set_auth_cookie((int) $user_id, true);
do_action('wp_login', $user->user_login, $user);
return [
'success' => true,
'user_id' => (int) $user_id,
'user' => $user,
'user_login' => $user_login,
'redirect' => home_url('/'),
'notice' => $ref_notice,
];
}
public static function finalize_oauth_binding(int $user_id, array $oauth_data, string $obk = ''): void {
if ($user_id <= 0) {
return;
}
$provider = sanitize_key((string) ($oauth_data['provider'] ?? ''));
if ($provider === '') {
return;
}
update_user_meta($user_id, "aksrt_{$provider}_id", $oauth_data['provider_id'] ?? '');
$bindings = get_user_meta($user_id, 'aksrt_oauth_bindings', true);
$bindings = is_array($bindings) ? $bindings : [];
if (!in_array($provider, $bindings, true)) {
$bindings[] = $provider;
}
update_user_meta($user_id, 'aksrt_oauth_bindings', array_values($bindings));
if (!empty($oauth_data['user_data']['avatar_url'])) {
update_user_meta($user_id, 'aksrt_oauth_avatar', esc_url_raw($oauth_data['user_data']['avatar_url']));
}
if ($obk !== '') {
delete_transient(sanitize_text_field($obk));
}
wp_set_auth_cookie($user_id, true);
}
private static function mark_email_verified(int $user_id): void {
update_user_meta($user_id, self::EMAIL_VERIFIED_META, '1');
do_action('aksrt_email_verified', $user_id);
}
}