| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use FluentCart\App\Helpers\Status; |
| 6 |
use FluentCart\App\Models\AppliedCoupon; |
| 7 |
use FluentCart\App\Models\Cart; |
| 8 |
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 |
use FluentCart\Framework\Support\Arr; |
| 15 |
|
| 16 |
class UserHandler |
| 17 |
{ |
| 18 |
public function register() |
| 19 |
{ |
| 20 |
add_action('delete_user', [$this, 'userDeleteHandler'], 10, 1); |
| 21 |
add_action('user_register', [$this, 'userRegistrationHandler'], 10, 1); |
| 22 |
|
| 23 |
// Let's handle auto user registration! |
| 24 |
add_action('fluent_cart/cart_completed', [$this, 'maybeCreateUser'], 10, 1); |
| 25 |
|
| 26 |
add_action('profile_update', [$this, 'handleWpUserProfileUpdated'], 10, 3); |
| 27 |
|
| 28 |
} |
| 29 |
|
| 30 |
public function handleWpUserProfileUpdated($userId, $oldData, $newData = []) |
| 31 |
{ |
| 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 |
| 36 |
} |
| 37 |
|
| 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 |
|
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
public function maybeCreateUser($data) |
| 72 |
{ |
| 73 |
$cart = Arr::get($data, 'cart'); |
| 74 |
$order = Arr::get($data, 'order'); |
| 75 |
$customer = $order->customer; |
| 76 |
|
| 77 |
if ($customer->getWpUserId(true)) { |
| 78 |
return; // User already exists |
| 79 |
} |
| 80 |
|
| 81 |
$willCreateUser = $order->type === Status::ORDER_TYPE_SUBSCRIPTION; |
| 82 |
|
| 83 |
if (!$willCreateUser) { |
| 84 |
// check if cart has set it |
| 85 |
$willCreateUser = $cart && Arr::get($cart, '_register_user', false); |
| 86 |
} |
| 87 |
|
| 88 |
if (!$willCreateUser) { |
| 89 |
// get the global settings where we may have auto create user enabled |
| 90 |
$willCreateUser = ''; // TODO: get the global settings |
| 91 |
} |
| 92 |
|
| 93 |
if (!$willCreateUser) { |
| 94 |
return; // No need to create user |
| 95 |
} |
| 96 |
|
| 97 |
// create the user from the customer data |
| 98 |
|
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* @return void |
| 104 |
*/ |
| 105 |
public function userDeleteHandler($userId) |
| 106 |
{ |
| 107 |
$user = get_user_by('ID', $userId); |
| 108 |
if (!$user) { |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 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 |
} |
| 121 |
} |
| 122 |
|
| 123 |
public function userRegistrationHandler($userId) |
| 124 |
{ |
| 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; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 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 |
} |
| 170 |
|