PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.4.8
Booking for Appointments and Events Calendar – Amelia v2.4.8
2.4.9 2.4.8 2.4.7 2.4.6 2.4.5 2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Infrastructure / WP / UserService / CreateWPUser.php
ameliabooking / src / Infrastructure / WP / UserService Last commit date
CreateWPUser.php 6 days ago UserAvatar.php 7 years ago UserService.php 2 months ago
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