| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\Api; |
| 4 |
|
| 5 |
use FluentCart\Api\Resource\CustomerResource; |
| 6 |
use FluentCart\Api\Resource\UserResource; |
| 7 |
use FluentCart\App\App; |
| 8 |
use FluentCart\Framework\Support\Arr; |
| 9 |
|
| 10 |
class User |
| 11 |
{ |
| 12 |
public function register($data) |
| 13 |
{ |
| 14 |
$this->validateAndCreate($data); |
| 15 |
} |
| 16 |
|
| 17 |
public function login($data) |
| 18 |
{ |
| 19 |
$this->validateAndLogin($data); |
| 20 |
} |
| 21 |
|
| 22 |
public function validateAndLogin(array $data) |
| 23 |
{ |
| 24 |
$userLogin = trim(Arr::get($data, 'user_login', '')); |
| 25 |
$password = Arr::get($data, 'password', ''); |
| 26 |
$rememberMe = Arr::get($data, 'remember_me', false) === 'on'; |
| 27 |
|
| 28 |
if (!$userLogin) { |
| 29 |
return $this->sendError(__('Email or username is required', 'fluent-cart'), 'missing_login'); |
| 30 |
} |
| 31 |
|
| 32 |
if (!$password) { |
| 33 |
return $this->sendError(__('Password is required', 'fluent-cart'), 'missing_password'); |
| 34 |
} |
| 35 |
|
| 36 |
$credentials = [ |
| 37 |
'user_login' => is_email($userLogin) ? sanitize_email($userLogin) : sanitize_user($userLogin, false), |
| 38 |
'user_password' => $password, |
| 39 |
'remember' => $rememberMe |
| 40 |
]; |
| 41 |
|
| 42 |
$user = wp_signon($credentials, is_ssl()); |
| 43 |
|
| 44 |
if (is_wp_error($user)) { |
| 45 |
return $this->sendError($user->get_error_message(), 'login_failed', 401); |
| 46 |
} |
| 47 |
|
| 48 |
// Log in the user |
| 49 |
wp_set_current_user($user->ID); |
| 50 |
wp_set_auth_cookie($user->ID, $rememberMe); |
| 51 |
|
| 52 |
$redirectUrl = (new StoreSettings())->getCustomerProfilePage() . '#/profile'; |
| 53 |
|
| 54 |
return $this->sendSuccess([ |
| 55 |
'message' => __('Login successful', 'fluent-cart'), |
| 56 |
'redirect_url' => $redirectUrl |
| 57 |
]); |
| 58 |
} |
| 59 |
|
| 60 |
public function validateAndCreate(array $data) |
| 61 |
{ |
| 62 |
$email = sanitize_email(Arr::get($data, 'email', '')); |
| 63 |
|
| 64 |
if (get_current_user_id()) { |
| 65 |
return $this->sendError(__('You are already logged in', 'fluent-cart'), 'already_logged_in'); |
| 66 |
} |
| 67 |
|
| 68 |
if (!is_email($email)) { |
| 69 |
return $this->sendError(__('Please enter a valid email address', 'fluent-cart'), 'invalid_email'); |
| 70 |
} |
| 71 |
|
| 72 |
if (email_exists($email)) { |
| 73 |
return $this->sendError(__('Email already exists', 'fluent-cart'), 'email_exists'); |
| 74 |
} |
| 75 |
|
| 76 |
$fullName = sanitize_text_field(Arr::get($data, 'full_name')); |
| 77 |
$nameParts = explode(' ', $fullName, 2); |
| 78 |
|
| 79 |
$firstName = $nameParts[0] ?? ''; |
| 80 |
$lastName = $nameParts[1] ?? ''; |
| 81 |
|
| 82 |
$processedData = [ |
| 83 |
'email' => $email, |
| 84 |
'password' => Arr::get($data, 'password'), |
| 85 |
'first_name' => $firstName, |
| 86 |
'last_name' => $lastName, |
| 87 |
'username' => $email |
| 88 |
]; |
| 89 |
|
| 90 |
if (!$processedData['password']) { |
| 91 |
$processedData['password'] = wp_generate_password(8); |
| 92 |
} |
| 93 |
|
| 94 |
do_action('fluent_cart/user/before_registration', $processedData); |
| 95 |
|
| 96 |
$this->createUser($processedData); |
| 97 |
} |
| 98 |
|
| 99 |
public function createUser($processedData) |
| 100 |
{ |
| 101 |
$userId = wp_create_user( |
| 102 |
$processedData['username'], |
| 103 |
$processedData['password'], |
| 104 |
$processedData['email'] |
| 105 |
); |
| 106 |
|
| 107 |
if (is_wp_error($userId)) { |
| 108 |
return $this->sendError($userId->get_error_message(), 'user_creation_failed'); |
| 109 |
} |
| 110 |
|
| 111 |
CustomerResource::create([ |
| 112 |
'first_name' => $processedData['first_name'], |
| 113 |
'last_name' => $processedData['last_name'], |
| 114 |
'wp_user' => 'no', |
| 115 |
'email' => $processedData['email'], |
| 116 |
'user_id' => $userId |
| 117 |
]); |
| 118 |
|
| 119 |
$this->updateUser($processedData, $userId); |
| 120 |
$this->setUserLoggedIn($userId); |
| 121 |
$this->sendEmail($userId); |
| 122 |
|
| 123 |
|
| 124 |
$checkoutPage = (new StoreSettings())->getCustomerProfilePage() . '#/profile'; |
| 125 |
|
| 126 |
return $this->sendSuccess([ |
| 127 |
'message' => __('User created successfully! You are now logged in.', 'fluent-cart'), |
| 128 |
'code' => 'user_created', |
| 129 |
'redirect_url' => $checkoutPage |
| 130 |
]); |
| 131 |
} |
| 132 |
|
| 133 |
protected function sendEmail($userId) |
| 134 |
{ |
| 135 |
// This will send an email with a password setup link |
| 136 |
\wp_new_user_notification($userId, null, 'user'); |
| 137 |
} |
| 138 |
|
| 139 |
protected function updateUser($processedData, $userId) |
| 140 |
{ |
| 141 |
$firstName = Arr::get($processedData, 'first_name'); |
| 142 |
$lastName = Arr::get($processedData, 'last_name'); |
| 143 |
$name = trim($firstName. ' ' . $lastName); |
| 144 |
|
| 145 |
$data = array_filter([ |
| 146 |
'ID' => $userId, |
| 147 |
'first_name' => $firstName, |
| 148 |
'last_name' => $lastName, |
| 149 |
'nickname' => $name, |
| 150 |
'user_nicename' => $name, |
| 151 |
'display_name' => $name, |
| 152 |
'user_url' => Arr::get($processedData, 'user_url') |
| 153 |
]); |
| 154 |
|
| 155 |
if ($name) { |
| 156 |
wp_update_user($data); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
protected function setUserLoggedIn($userId) |
| 161 |
{ |
| 162 |
wp_clear_auth_cookie(); |
| 163 |
wp_set_current_user($userId); |
| 164 |
wp_set_auth_cookie($userId); |
| 165 |
} |
| 166 |
|
| 167 |
protected function sendError($message, $code = 'error', $status = 400) |
| 168 |
{ |
| 169 |
wp_send_json_error([ |
| 170 |
'message' => $message, |
| 171 |
'code' => $code |
| 172 |
], $status); |
| 173 |
} |
| 174 |
|
| 175 |
protected function sendSuccess($data = [], $status = 200) |
| 176 |
{ |
| 177 |
wp_send_json_success($data, $status); |
| 178 |
} |
| 179 |
|
| 180 |
} |
| 181 |
|