add('empty_username', __('Error: Please enter a username.', 'fluent-community')); } elseif (!validate_username($user_login)) { $errors->add('invalid_username', __('Error: This username is invalid because it uses illegal characters. Please enter a valid username.', 'fluent-community')); $sanitized_user_login = ''; } elseif (username_exists($sanitized_user_login)) { $errors->add('username_exists', __('Error: This username is already registered. Please choose another one.', 'fluent-community')); } else { /** This filter is documented in wp-includes/user.php */ $illegal_user_logins = (array)apply_filters('illegal_user_logins', array()); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound if (in_array(strtolower($sanitized_user_login), array_map('strtolower', $illegal_user_logins), true)) { $errors->add('invalid_username', __('Error: Sorry, that username is not allowed.', 'fluent-community')); } } // Check the email address. if ('' === $user_email) { $errors->add('empty_email', __('Error: Please type your email address.', 'fluent-community')); } elseif (!is_email($user_email)) { $errors->add('invalid_email', __('Error: The email address is not correct.', 'fluent-community')); $user_email = ''; } elseif (email_exists($user_email)) { $errors->add( 'email_exists', __('Error: This email address is already registered. Please login or try resetting your password.', 'fluent-community') ); } do_action('register_post', $sanitized_user_login, $user_email, $errors); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound if ($errors->has_errors()) { return $errors; } if (!$user_pass) { $user_pass = wp_generate_password(8, false); } $data = [ 'user_login' => wp_slash($sanitized_user_login), 'user_email' => wp_slash($user_email), 'user_pass' => $user_pass ]; if (!empty($extraData['first_name'])) { $data['first_name'] = sanitize_text_field($extraData['first_name']); } if (!empty($extraData['last_name'])) { $data['last_name'] = sanitize_text_field($extraData['last_name']); } if (!empty($extraData['full_name']) && empty($extraData['first_name']) && empty($extraData['last_name'])) { $extraData['full_name'] = sanitize_text_field($extraData['full_name']); // extract the names $fullNameArray = explode(' ', $extraData['full_name']); $data['first_name'] = array_shift($fullNameArray); if ($fullNameArray) { $data['last_name'] = implode(' ', $fullNameArray); } else { $data['last_name'] = ''; } } if (!empty($extraData['description'])) { $data['description'] = sanitize_textarea_field($extraData['description']); } if (!empty($extraData['user_url']) && filter_var($extraData['user_url'], FILTER_VALIDATE_URL)) { $data['user_url'] = sanitize_url($extraData['user_url']); } if (!empty($extraData['role'])) { $data['role'] = $extraData['role']; } $user_id = wp_insert_user($data); if (!$user_id || is_wp_error($user_id)) { $errors->add('registerfail', __('Error: Could not register you. Please contact the site admin!', 'fluent-community') ); return $errors; } if (!empty($_COOKIE['wp_lang'])) { $wp_lang = sanitize_text_field(wp_unslash($_COOKIE['wp_lang'])); if (in_array($wp_lang, get_available_languages(), true)) { update_user_meta($user_id, 'locale', $wp_lang); // Set user locale if defined on registration. } } do_action('register_new_user', $user_id); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound return $user_id; } public static function makeLogin($user) { wp_clear_auth_cookie(); wp_set_current_user($user->ID, $user->user_login); wp_set_auth_cookie($user->ID, true, is_ssl()); $user = get_user_by('ID', $user->ID); if ($user) { do_action('wp_login', $user->user_login, $user); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound } return $user; } public static function isFluentAuthAvailable() { if (defined('FLUENT_AUTH_VERSION') && FLUENT_AUTH_VERSION) { return (new \FluentAuth\App\Hooks\Handlers\CustomAuthHandler())->isEnabled(); } return false; } public static function getTermsText() { $policyUrl = apply_filters('fluent_community/terms_policy_url', get_privacy_policy_url()); $termsText = __('I agree to the terms and conditions', 'fluent-community'); if ($policyUrl) { /* translators: %1$s is replaced by the text "terms and conditions", %2$s is replaced by the text "to the terms and conditions" */ $termsText = sprintf(__('I agree to the %1$s terms and conditions %2$s', 'fluent-community'), '', ''); } return $termsText; } public static function getFormFields($invitation = null) { $fields = apply_filters('fluent_community/auth/signup_fields', [ 'full_name' => [ 'label' => __('Full name', 'fluent-community'), 'placeholder' => __('Your first & last name', 'fluent-community'), 'type' => 'text', 'required' => true, 'value' => $invitation ? Arr::get($invitation->meta, 'invitee_name') : '', 'sanitize_callback' => 'sanitize_text_field' ], 'email' => [ 'type' => 'email', 'placeholder' => __('Your email address', 'fluent-community'), 'label' => __('Email Address', 'fluent-community'), 'required' => true, 'value' => $invitation ? $invitation->message : '', 'readonly' => $invitation && $invitation->message, 'sanitize_callback' => 'sanitize_email' ], 'username' => [ 'type' => 'text', 'placeholder' => __('No space or special characters', 'fluent-community'), 'label' => __('Username', 'fluent-community'), 'required' => true, 'sanitize_callback' => 'sanitize_user' ], 'password' => [ 'type' => 'password', 'placeholder' => __('Password', 'fluent-community'), 'label' => __('Account Password', 'fluent-community'), 'required' => true, 'sanitize_callback' => 'sanitize_text_field' ], 'conf_password' => [ 'type' => 'password', 'placeholder' => __('Password Confirmation', 'fluent-community'), 'label' => __('Re-type Account Password', 'fluent-community'), 'required' => true, 'sanitize_callback' => 'sanitize_text_field' ], 'terms' => [ 'type' => 'inline_checkbox', 'inline_label' => self::getTermsText(), 'required' => true ] ], $invitation); if (!self::isPasswordConfRequired()) { unset($fields['conf_password']); } return $fields; } public static function getLostPasswordUrl($redirectUrl = '') { if (self::isFluentAuthAvailable()) { $url = add_query_arg([ 'form' => 'reset_password' ], Helper::getAuthUrl()); } else { $url = wp_lostpassword_url($redirectUrl);; } return apply_filters('fluent_community/auth/lost_password_url', $url); } public static function getLoginFormFields() { return apply_filters('fluent_community/auth/login_fields', [ 'username' => [ 'type' => 'text', 'placeholder' => __('Your account email address', 'fluent-community'), 'label' => __('Email Address', 'fluent-community'), 'required' => true, 'sanitize_callback' => 'sanitize_user' ], 'password' => [ 'type' => 'password', 'placeholder' => __('Your account password', 'fluent-community'), 'label' => __('Password', 'fluent-community'), 'required' => true, 'sanitize_callback' => 'sanitize_text_field' ] ]); } public static function isPasswordConfRequired() { return apply_filters('fluent_community/autg/password_confirmation', true); } public static function isRegistrationEnabled() { $enabled = !!get_option('users_can_register'); if (!$enabled) { $generalSettinsg = Helper::generalSettings(); $enabled = $generalSettinsg['explicit_registration'] !== 'no'; } return apply_filters('fluent_community/auth/registration_enabled', $enabled); } public static function isTwoFactorEnabled() { // fluent_auth/verify_signup_email is kept for backward compatibility with FluentAuth-targeted snippets $enabled = apply_filters('fluent_auth/verify_signup_email', true); return apply_filters('fluent_community/auth/two_factor_enabled', $enabled); } public static function get2FaRegistrationCodeForm($formData) { $generalSettings = Helper::generalSettings(); try { $verifcationCode = str_pad((string) random_int(100123, 900987), 6, '0', STR_PAD_LEFT); } catch (\Exception $e) { $verifcationCode = str_pad((string) wp_rand(100123, 900987), 6, '0', STR_PAD_LEFT); } // Hash the code $codeHash = wp_hash_password($verifcationCode); // Create a token with the email and code hash $data = [ 'email' => $formData['email'], 'code_hash' => $codeHash, 'expires' => time() + 600 // 10 minutes expiry ]; $token = base64_encode(json_encode($data)); // Sign the token $signature = hash_hmac('sha256', $token, SECURE_AUTH_KEY); $signedToken = $token . '.' . $signature; /* translators: %s is replaced by the title of the site */ $mailSubject = apply_filters("fluent_community/auth/signup_verification_mail_subject", sprintf(__('Your registration verification code for %s', 'fluent-community'), Arr::get($generalSettings, 'site_title'))); $pStart = '
'; /* translators: %s is replaced by the name of the user */ $message = $pStart . sprintf(__('Hello %s,', 'fluent-community'), Arr::get($formData, 'first_name')) . '
' . $pStart . __('Thank you for registering with us! To complete the setup of your account, please enter the verification code below on the registration page.', 'fluent-community') . '' . /* translators: %s is replaced by the verification code */ $pStart . '' . sprintf(__('Verification Code: %s', 'fluent-community'), $verifcationCode) . '' . '