PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.5
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.5
1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk All 48 releases
fluent-cart / api / User.php

User.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.5, at api/User.php

186 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $storeSettings = new StoreSettings();
121 $autoLogin = $storeSettings->get('auto_login_after_account_creation') === 'yes';
122 if ($autoLogin) {
123 $this->setUserLoggedIn($userId);
124 }
125 $this->sendEmail($userId);
126
127 $profilePage = $storeSettings->getCustomerProfilePage() . '#/profile';
128
129 return $this->sendSuccess([
130 'message' => $autoLogin
131 ? __('User created successfully! You are now logged in.', 'fluent-cart')
132 : __('Your account has been created. Please check your email to set your password and log in.', 'fluent-cart'),
133 'code' => 'user_created',
134 'redirect_url' => $autoLogin ? $profilePage : wp_login_url($profilePage)
135 ]);
136 }
137
138 protected function sendEmail($userId)
139 {
140 // This will send an email with a password setup link
141 \wp_new_user_notification($userId, null, 'user');
142 }
143
144 protected function updateUser($processedData, $userId)
145 {
146 $firstName = Arr::get($processedData, 'first_name');
147 $lastName = Arr::get($processedData, 'last_name');
148 $name = trim($firstName. ' ' . $lastName);
149
150 $data = array_filter([
151 'ID' => $userId,
152 'first_name' => $firstName,
153 'last_name' => $lastName,
154 'nickname' => $name,
155 'user_nicename' => $name,
156 'display_name' => $name,
157 'user_url' => Arr::get($processedData, 'user_url')
158 ]);
159
160 if ($name) {
161 wp_update_user($data);
162 }
163 }
164
165 protected function setUserLoggedIn($userId)
166 {
167 wp_clear_auth_cookie();
168 wp_set_current_user($userId);
169 wp_set_auth_cookie($userId);
170 }
171
172 protected function sendError($message, $code = 'error', $status = 400)
173 {
174 wp_send_json_error([
175 'message' => $message,
176 'code' => $code
177 ], $status);
178 }
179
180 protected function sendSuccess($data = [], $status = 200)
181 {
182 wp_send_json_success($data, $status);
183 }
184
185 }
186