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
← All changes | app/Hooks/Handlers/UserHandler.php +19 -95 1.3.19 → 1.6.5 View file →
@@ -2,16 +2,10 @@
2 2
3 3 namespace FluentCart\App\Hooks\Handlers;
4 4
5 5 use FluentCart\App\Helpers\Status;
6 -use FluentCart\App\Models\AppliedCoupon;
7 -use FluentCart\App\Models\Cart;
6 +use FluentCart\App\Services\CustomerIdentity\EmailVerificationService;
8 7 use FluentCart\App\Models\Customer;
9 -use FluentCart\App\Models\CustomerAddresses;
10 -use FluentCart\App\Models\CustomerMeta;
11 -use FluentCart\App\Models\Order;
12 -use FluentCart\App\Models\OrderDownloadPermission;
13 -use FluentCart\App\Models\Subscription;
14 8 use FluentCart\Framework\Support\Arr;
15 9
16 10 class UserHandler
17 11 {
@@ -18,8 +12,10 @@
18 12 public function register()
19 13 {
20 14 add_action('delete_user', [$this, 'userDeleteHandler'], 10, 1);
21 15 add_action('user_register', [$this, 'userRegistrationHandler'], 10, 1);
16 + add_action('password_reset', [EmailVerificationService::class, 'capturePasswordResetProof'], 10, 1);
17 + add_action('after_password_reset', [EmailVerificationService::class, 'verifyAfterPasswordReset'], 10, 1);
22 18
23 19 // Let's handle auto user registration!
24 20 add_action('fluent_cart/cart_completed', [$this, 'maybeCreateUser'], 10, 1);
25 21
@@ -28,47 +24,18 @@
28 24 }
29 25
30 26 public function handleWpUserProfileUpdated($userId, $oldData, $newData = [])
31 27 {
32 - $emailChanged = $oldData->user_email !== $newData['user_email'];
33 -
34 - if (!$emailChanged) {
35 - return; // we will change the first name and last name a bit later
28 + $user = $userId ? get_userdata($userId) : false;
29 + $oldEmail = is_object($oldData) && isset($oldData->user_email) ? wp_unslash($oldData->user_email) : '';
30 + if (!$user || EmailVerificationService::isSame($oldEmail, $user->user_email)) {
31 + return;
36 32 }
37 33
38 - $newEmail = $newData['user_email'];
39 - $oldEmail = $oldData->user_email;
40 -
41 -
42 - $attachToCustomer = Customer::query()->where('email', $newEmail)->first();
43 -
44 -
45 - // if $attachToCustomer is empty, then simply just update the customer email
46 - if (empty($attachToCustomer)) {
47 - $oldCustomer = Customer::query()->where('email', $oldEmail)->first();
48 - Customer::query()->where('email', $oldEmail)->update(['email' => $newEmail]);
49 - do_action('fluent_cart/customer_email_changed', [
50 - 'old_customer' => $oldCustomer,
51 - 'new_customer' => $oldCustomer,
52 - 'old_email' => $oldEmail,
53 - 'new_email' => $newEmail,
54 - 'userId' => $userId
55 - ]);
56 - } else {
57 - $oldCustomer = Customer::query()->where('email', $oldEmail)->first();
58 - if (empty($oldCustomer)) {
59 - $attachToCustomer->update(['user_id' => $userId]);
60 - return;
61 - }
62 -
63 - $this->moveCustomerResources($oldCustomer->id, $attachToCustomer->id);
64 - $oldCustomer->recountStat();
65 - $attachToCustomer->recountStat();
66 - }
67 -
34 + // Account changes do not prove inbox ownership or change customer contact details.
35 + EmailVerificationService::markPending((int) $userId, $user->user_email);
68 36 }
69 37
70 -
71 38 public function maybeCreateUser($data)
72 39 {
73 40 $cart = Arr::get($data, 'cart');
74 41 $order = Arr::get($data, 'order');
@@ -73,9 +40,9 @@
73 40 $cart = Arr::get($data, 'cart');
74 41 $order = Arr::get($data, 'order');
75 42 $customer = $order->customer;
76 43
77 - if ($customer->getWpUserId(true)) {
44 + if ($customer->getWpUserId()) {
78 45 return; // User already exists
79 46 }
80 47
81 48 $willCreateUser = $order->type === Status::ORDER_TYPE_SUBSCRIPTION;
@@ -103,67 +70,24 @@
103 70 * @return void
104 71 */
105 72 public function userDeleteHandler($userId)
106 73 {
107 - $user = get_user_by('ID', $userId);
108 - if (!$user) {
74 + if (!$userId) {
109 75 return;
110 76 }
111 77
112 - // Check if the user_email is a customer
113 - $customer = Customer::query()
114 - ->where('email', $user->user_email)
115 - ->first();
116 -
117 - // remove user_id from $customer
118 - if ($customer) {
119 - $customer->update(['user_id' => NULL]);
120 - }
78 + // Unlink by identity. Matching on the account's email unlinked whichever
79 + // customer happened to hold it — possibly another account's — and left
80 + // this account's own customers pointing at a dead user id when their
81 + // contact address had drifted from the account's.
82 + Customer::query()->where('user_id', $userId)->update(['user_id' => null]);
121 83 }
122 84
123 85 public function userRegistrationHandler($userId)
124 86 {
125 - // get user by $userId
126 - $user = get_user_by('ID', $userId);
127 -
128 - // Check if the user_email is a customer
129 - $customer = Customer::query()
130 - ->where('email', $user->user_email)
131 - ->first();
132 -
133 - if ($customer) {
134 - $this->updateCustomer($customer, $user, $userId);
135 - return;
87 + $user = $userId ? get_userdata($userId) : false;
88 + if ($user) {
89 + EmailVerificationService::markPending((int) $userId, $user->user_email);
136 90 }
137 91 }
138 92
139 - private function updateCustomer(Customer $customer, object $user, int $userId): void
140 - {
141 - $data = ['user_id' => $userId];
142 -
143 - if (!empty($user->first_name)) {
144 - $data['first_name'] = $user->first_name;
145 - }
146 -
147 - if (!empty($user->last_name)) {
148 - $data['last_name'] = $user->last_name;
149 - }
150 -
151 - $customer->update($data);
152 - }
153 -
154 - private function moveCustomerResources($fromCustomerId, $toCustomerId)
155 - {
156 - OrderDownloadPermission::query()->where('customer_id', $fromCustomerId)->update(['customer_id' => $toCustomerId]);
157 - Order::query()->where('customer_id', $fromCustomerId)->update(['customer_id' => $toCustomerId]);
158 - AppliedCoupon::query()->where('customer_id', $fromCustomerId)->update(['customer_id' => $toCustomerId]);
159 - Cart::query()->where('customer_id', $fromCustomerId)->update(['customer_id' => $toCustomerId]);
160 - CustomerMeta::query()->where('customer_id', $fromCustomerId)->update(['customer_id' => $toCustomerId]);
161 - CustomerAddresses::query()->where('customer_id', $fromCustomerId)->update(['customer_id' => $toCustomerId]);
162 - Subscription::query()->where('customer_id', $fromCustomerId)->update(['customer_id' => $toCustomerId]);
163 -
164 - do_action('fluent_cart/customer_resources_moved', [
165 - 'from_customer_id' => $fromCustomerId,
166 - 'to_customer_id' => $toCustomerId
167 - ]);
168 - }
169 93 }