CreateWPUser.php
130 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Infrastructure\WP\UserService; |
| 4 | |
| 5 | use AmeliaBooking\Infrastructure\WP\UserRoles\SuperAdminRoleService; |
| 6 | use WP_Error; |
| 7 | |
| 8 | /** |
| 9 | * Class CreateWPUser |
| 10 | * |
| 11 | * @package AmeliaBooking\Infrastructure\WP\UserService |
| 12 | */ |
| 13 | class CreateWPUser |
| 14 | { |
| 15 | /** |
| 16 | * @param string $email |
| 17 | * @param string $firstName |
| 18 | * @param string $lastName |
| 19 | * @param string|null $role |
| 20 | * @param bool $sendNewUserNotification |
| 21 | * |
| 22 | * @return mixed |
| 23 | */ |
| 24 | public function create($email, $firstName, $lastName, $role = null, $sendNewUserNotification = true) |
| 25 | { |
| 26 | if (username_exists($email)) { |
| 27 | $user = get_user_by('login', $email); |
| 28 | if ($user) { |
| 29 | $this->addRole($role, $user->ID); |
| 30 | return $user->ID; |
| 31 | } |
| 32 | return null; |
| 33 | } elseif (email_exists($email)) { |
| 34 | $user = get_user_by('email', $email); |
| 35 | if ($user) { |
| 36 | $this->addRole($role, $user->ID); |
| 37 | return $user->ID; |
| 38 | } |
| 39 | return null; |
| 40 | } |
| 41 | |
| 42 | $userId = wp_create_user($email, wp_generate_password(), $email); |
| 43 | |
| 44 | wp_update_user( |
| 45 | [ |
| 46 | 'ID' => $userId, |
| 47 | 'first_name' => $firstName, |
| 48 | 'last_name' => $lastName, |
| 49 | ] |
| 50 | ); |
| 51 | |
| 52 | if ($userId instanceof WP_Error) { |
| 53 | return null; |
| 54 | } |
| 55 | |
| 56 | $this->setRole($role, $userId); |
| 57 | |
| 58 | if ($sendNewUserNotification) { |
| 59 | // Wrapped in try/catch because wp_new_user_notification() calls wp_mail() which uses |
| 60 | // WordPress's own PHPMailer with the mail() transport. On servers where mail() is disabled, |
| 61 | // this throws a fatal Error that would otherwise abort the entire booking process. |
| 62 | try { |
| 63 | wp_new_user_notification($userId, null, 'user'); |
| 64 | } catch (\Throwable $e) { |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return (int)$userId; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @param int $id |
| 73 | * @param string|null $role |
| 74 | * |
| 75 | * @return mixed |
| 76 | */ |
| 77 | public function update($id, $role = null) |
| 78 | { |
| 79 | $this->addRole($role, $id); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @param string $role |
| 84 | * @param int $userId |
| 85 | */ |
| 86 | private function setRole($role, $userId) |
| 87 | { |
| 88 | if ($role) { |
| 89 | $user = new \WP_User($userId); |
| 90 | if ($this->isSuperAdminConflict($role, $user)) { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | if (get_role($role)) { |
| 95 | $user->set_role($role); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @param string $role |
| 102 | * @param int $userId |
| 103 | */ |
| 104 | private function addRole($role, $userId) |
| 105 | { |
| 106 | if ($role) { |
| 107 | $user = new \WP_User($userId); |
| 108 | if ($this->isSuperAdminConflict($role, $user)) { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | if (get_role($role)) { |
| 113 | $user->add_role($role); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @param string $role |
| 120 | * @param \WP_User $user |
| 121 | * |
| 122 | * @return bool |
| 123 | */ |
| 124 | private function isSuperAdminConflict($role, $user) |
| 125 | { |
| 126 | return in_array(SuperAdminRoleService::ROLE, (array)$user->roles, true) && |
| 127 | in_array($role, ['wpamelia-customer', 'wpamelia-provider', 'wpamelia-manager'], true); |
| 128 | } |
| 129 | } |
| 130 |