| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentSupport\App\Services\Helper; |
| 6 |
use FluentSupport\Framework\Support\Arr; |
| 7 |
use FluentSupport\Framework\Request\Request; |
| 8 |
use FluentSupport\App\Hooks\Handlers\AuthHandler; |
| 9 |
|
| 10 |
class AuthController extends Controller |
| 11 |
{ |
| 12 |
public function signup(Request $request) |
| 13 |
{ |
| 14 |
$fields = AuthHandler::getSignupFields(); |
| 15 |
|
| 16 |
$rules = $this->getRules($fields); |
| 17 |
|
| 18 |
$messages = $this->getMessages($rules); |
| 19 |
|
| 20 |
$formData = apply_filters('fluent-support/signup_form_data', $request->all()); |
| 21 |
|
| 22 |
do_action('fluent-support/before_signup_validation', []); |
| 23 |
|
| 24 |
$this->validate($formData, $rules, $messages); |
| 25 |
|
| 26 |
do_action('fluent_support/after_signup_validation', $formData); |
| 27 |
|
| 28 |
$userId = $this->createUser($formData); |
| 29 |
|
| 30 |
if (is_wp_error($userId)) { |
| 31 |
return $this->response( |
| 32 |
apply_filters( |
| 33 |
'fluent-support/signup_create_user_error', |
| 34 |
['error' => $userId->get_error_message()] |
| 35 |
), 423); |
| 36 |
} |
| 37 |
|
| 38 |
do_action('fluent_support/after_creating_user'); |
| 39 |
|
| 40 |
$this->maybeUpdateUser($userId, $formData); |
| 41 |
$this->assignRole($userId); |
| 42 |
$this->login($userId); |
| 43 |
|
| 44 |
return $this->response( |
| 45 |
apply_filters('fluent-support/signup_complete_response', [ |
| 46 |
'message' => __('Successfully registered to the site.', 'fluent-support'), |
| 47 |
'redirect' => Arr::get($formData, '__redirect_to', Helper::getPortalBaseUrl()) |
| 48 |
]) |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
protected function getRules($fields = []) |
| 53 |
{ |
| 54 |
$rules = []; |
| 55 |
|
| 56 |
foreach ($fields as $fieldName => $field) { |
| 57 |
if (array_key_exists('required', $field)) { |
| 58 |
$rules[$fieldName] = 'required'; |
| 59 |
} |
| 60 |
|
| 61 |
$pipe = array_key_exists($fieldName, $rules) ? '|' : ''; |
| 62 |
|
| 63 |
if ($field['type'] === 'email') { |
| 64 |
$rules[$fieldName] = $rules[$fieldName] . $pipe . 'email'; |
| 65 |
} elseif ($field['type'] === 'password') { |
| 66 |
$rules[$fieldName] = $rules[$fieldName] . $pipe . 'min:8'; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
return apply_filters('fluent-support/signup_validation_rules', $rules); |
| 71 |
} |
| 72 |
|
| 73 |
protected function getMessages($rules = []) |
| 74 |
{ |
| 75 |
return apply_filters('fluent-support/signup_validation_messages', [], $rules); |
| 76 |
} |
| 77 |
|
| 78 |
protected function createUser($formData = []) |
| 79 |
{ |
| 80 |
$email = apply_filters('fluent-support/signup_email', Arr::get($formData, 'email')); |
| 81 |
$userName = apply_filters('fluent-support/signup_username', Arr::get($formData, 'username')); |
| 82 |
|
| 83 |
if (empty($formData['password'])) { |
| 84 |
$password = wp_generate_password(8); |
| 85 |
} else { |
| 86 |
$password = $formData['password']; |
| 87 |
} |
| 88 |
|
| 89 |
$password = apply_filters('fluent-support/signup_password', $password); |
| 90 |
|
| 91 |
do_action('fluent_support/before_creating_user', $userName, $password, $email); |
| 92 |
|
| 93 |
return wp_create_user($userName, $password, $email); |
| 94 |
} |
| 95 |
|
| 96 |
protected function maybeUpdateUser($userId, $formData) |
| 97 |
{ |
| 98 |
$name = trim(Arr::get($formData, 'first_name') . ' ' . Arr::get($formData, 'last_name')); |
| 99 |
|
| 100 |
$data = array_filter([ |
| 101 |
'ID' => $userId, |
| 102 |
'user_nicename' => $name, |
| 103 |
'display_name' => $name |
| 104 |
]); |
| 105 |
|
| 106 |
if ($name) { |
| 107 |
do_action('fluent-support/before_updating_user', $data); |
| 108 |
|
| 109 |
wp_update_user(apply_filters('fluent-support/update_user_data', $data)); |
| 110 |
|
| 111 |
do_action('fluent-support/after_updating_user', $data); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
protected function assignRole($userId) |
| 116 |
{ |
| 117 |
$user = new \WP_User($userId); |
| 118 |
|
| 119 |
do_action('fluent-support/before_assigning_role', $user); |
| 120 |
|
| 121 |
$user->set_role(apply_filters('fluent-support/user_role', 'subscriber')); |
| 122 |
|
| 123 |
do_action('fluent-support/after_assigning_role', $user); |
| 124 |
} |
| 125 |
|
| 126 |
protected function login($userId) |
| 127 |
{ |
| 128 |
do_action('fluent-support/before_logging_in_user', $userId); |
| 129 |
|
| 130 |
wp_clear_auth_cookie(); |
| 131 |
wp_set_current_user($userId); |
| 132 |
wp_set_auth_cookie($userId); |
| 133 |
|
| 134 |
do_action('fluent-support/after_logging_in_user', $userId); |
| 135 |
} |
| 136 |
} |
| 137 |
|