| 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 |
$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; |
| 32 |
} |
| 33 |
|
| 34 |
// Account changes do not prove inbox ownership or change customer contact details. |
| 35 |
EmailVerificationService::markPending((int) $userId, $user->user_email); |
| 36 |
} |
| 37 |
|
| 38 |
public function maybeCreateUser($data) |
| 39 |
{ |
| 40 |
$cart = Arr::get($data, 'cart'); |
| 41 |
$order = Arr::get($data, 'order'); |
| 42 |
$customer = $order->customer; |
| 43 |
|
| 44 |
if ($customer->getWpUserId()) { |
| 45 |
return; // User already exists |
| 46 |
} |
| 47 |
|
| 48 |
$willCreateUser = $order->type === Status::ORDER_TYPE_SUBSCRIPTION; |
| 49 |
|
| 50 |
if (!$willCreateUser) { |
| 51 |
// check if cart has set it |
| 52 |
$willCreateUser = $cart && Arr::get($cart, '_register_user', false); |
| 53 |
} |
| 54 |
|
| 55 |
if (!$willCreateUser) { |
| 56 |
// get the global settings where we may have auto create user enabled |
| 57 |
$willCreateUser = ''; // TODO: get the global settings |
| 58 |
} |
| 59 |
|
| 60 |
if (!$willCreateUser) { |
| 61 |
return; // No need to create user |
| 62 |
} |
| 63 |
|
| 64 |
// create the user from the customer data |
| 65 |
|
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* @return void |
| 71 |
*/ |
| 72 |
public function userDeleteHandler($userId) |
| 73 |
{ |
| 74 |
if (!$userId) { |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 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]); |
| 83 |
} |
| 84 |
|
| 85 |
public function userRegistrationHandler($userId) |
| 86 |
{ |
| 87 |
$user = $userId ? get_userdata($userId) : false; |
| 88 |
if ($user) { |
| 89 |
EmailVerificationService::markPending((int) $userId, $user->user_email); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
} |
| 94 |
|