PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.7.0
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.7.0
1.7.0 1.6.6 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 All 50 releases
fluent-cart / app / Hooks / Handlers / UserHandler.php

UserHandler.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.7.0, at app/Hooks/Handlers/UserHandler.php

104 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Hooks\Handlers;
4
5 use FluentCart\App\Helpers\Status;
6 use FluentCart\App\Services\CustomerIdentity\EmailVerificationService;
7 use FluentCart\App\Models\Customer;
8 use FluentCart\Framework\Support\Arr;
9
10 class UserHandler
11 {
12 public function register()
13 {
14 add_action('delete_user', [$this, 'userDeleteHandler'], 10, 1);
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);
18
19 // Let's handle auto user registration!
20 add_action('fluent_cart/cart_completed', [$this, 'maybeCreateUser'], 10, 1);
21
22 add_action('profile_update', [$this, 'handleWpUserProfileUpdated'], 10, 3);
23
24 }
25
26 public function handleWpUserProfileUpdated($userId, $oldData, $newData = [])
27 {
28 // State is recorded only while the store requires verification; accounts
29 // without it are treated as existing accounts if verification is enabled later.
30 if (!EmailVerificationService::isEnabled()) {
31 return;
32 }
33
34 $user = $userId ? get_userdata($userId) : false;
35 $oldEmail = is_object($oldData) && isset($oldData->user_email) ? wp_unslash($oldData->user_email) : '';
36 if (!$user || EmailVerificationService::isSame($oldEmail, $user->user_email)) {
37 return;
38 }
39
40 // Account changes do not prove inbox ownership or change customer contact details.
41 EmailVerificationService::markPending((int) $userId, $user->user_email);
42 }
43
44 public function maybeCreateUser($data)
45 {
46 $cart = Arr::get($data, 'cart');
47 $order = Arr::get($data, 'order');
48 $customer = $order->customer;
49
50 if ($customer->getWpUserId()) {
51 return; // User already exists
52 }
53
54 $willCreateUser = $order->type === Status::ORDER_TYPE_SUBSCRIPTION;
55
56 if (!$willCreateUser) {
57 // check if cart has set it
58 $willCreateUser = $cart && Arr::get($cart, '_register_user', false);
59 }
60
61 if (!$willCreateUser) {
62 // get the global settings where we may have auto create user enabled
63 $willCreateUser = ''; // TODO: get the global settings
64 }
65
66 if (!$willCreateUser) {
67 return; // No need to create user
68 }
69
70 // create the user from the customer data
71
72
73 }
74
75 /**
76 * @return void
77 */
78 public function userDeleteHandler($userId)
79 {
80 if (!$userId) {
81 return;
82 }
83
84 // Unlink by identity. Matching on the account's email unlinked whichever
85 // customer happened to hold it — possibly another account's — and left
86 // this account's own customers pointing at a dead user id when their
87 // contact address had drifted from the account's.
88 Customer::query()->where('user_id', $userId)->update(['user_id' => null]);
89 }
90
91 public function userRegistrationHandler($userId)
92 {
93 if (!EmailVerificationService::isEnabled()) {
94 return;
95 }
96
97 $user = $userId ? get_userdata($userId) : false;
98 if ($user) {
99 EmailVerificationService::markPending((int) $userId, $user->user_email);
100 }
101 }
102
103 }
104