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 / UserRoles / UserRoles.php
ameliabooking / src / Infrastructure / WP / UserRoles Last commit date
SuperAdminRoleService.php 1 week ago UserRoles.php 1 week ago
UserRoles.php
139 lines
1 <?php
2
3 namespace AmeliaBooking\Infrastructure\WP\UserRoles;
4
5 use AmeliaBooking\Infrastructure\WP\config\Roles;
6 use AmeliaBooking\Infrastructure\WP\InstallActions\ActivationRolesHook;
7
8 /**
9 * Class UserRoles
10 *
11 * @package AmeliaBooking\Infrastructure\WP
12 */
13 class UserRoles
14 {
15 /**
16 * @param $roles
17 */
18 public static function init($roles)
19 {
20 /** @var array $roles */
21 foreach ($roles as $role) {
22 if (!\wp_roles()->is_role($role['name'])) {
23 \add_role($role['name'], $role['label'], $role['capabilities']);
24 } else {
25 self::updateRoleLabel($role['name'], $role['label']);
26 }
27 }
28 }
29
30 /**
31 * Re-apply Amelia role display names (white-label plugin name when enabled).
32 */
33 public static function syncRoleLabels()
34 {
35 self::syncSuperAdminRoleAvailability();
36
37 $roles = new Roles();
38
39 self::init($roles());
40 }
41
42 /**
43 * Register or remove SuperAdmin based on Elite (Developer) licence.
44 */
45 public static function syncSuperAdminRoleAvailability()
46 {
47 if (SuperAdminRoleService::isAvailable()) {
48 if (!\wp_roles()->is_role(SuperAdminRoleService::ROLE)) {
49 ActivationRolesHook::init();
50 }
51
52 return;
53 }
54
55 // Strip the role from users before removing the role definition so
56 // usermeta does not resurrect SuperAdmin when Elite is re-enabled.
57 if (\wp_roles()->is_role(SuperAdminRoleService::ROLE)) {
58 foreach (\get_users(['role' => SuperAdminRoleService::ROLE]) as $user) {
59 $user->remove_role(SuperAdminRoleService::ROLE);
60 }
61
62 \remove_role(SuperAdminRoleService::ROLE);
63 }
64 }
65
66 /**
67 * Hide SuperAdmin from WP role dropdowns when the licence is not Elite.
68 *
69 * @param array $roles
70 *
71 * @return array
72 */
73 public static function filterEditableRoles($roles)
74 {
75 if (!SuperAdminRoleService::isAvailable()) {
76 unset($roles[SuperAdminRoleService::ROLE]);
77 }
78
79 return $roles;
80 }
81
82 /**
83 * @param string $roleName
84 * @param string $roleLabel
85 */
86 private static function updateRoleLabel($roleName, $roleLabel)
87 {
88 $wpRoles = \wp_roles();
89
90 if (
91 !isset($wpRoles->roles[$roleName]['name']) ||
92 $wpRoles->roles[$roleName]['name'] === $roleLabel
93 ) {
94 return;
95 }
96
97 $wpRoles->roles[$roleName]['name'] = $roleLabel;
98 $wpRoles->role_names[$roleName] = $roleLabel;
99
100 if ($wpRoles->use_db) {
101 \update_option($wpRoles->role_key, $wpRoles->roles);
102 }
103 }
104
105 /**
106 * Return the current user amelia role
107 *
108 * @param $wpUser
109 * @return string|null
110 */
111 public static function getUserAmeliaRole($wpUser)
112 {
113 if (
114 in_array('administrator', $wpUser->roles, true) ||
115 \is_super_admin($wpUser->ID) ||
116 (
117 SuperAdminRoleService::isAvailable() &&
118 in_array(SuperAdminRoleService::ROLE, (array)$wpUser->roles, true)
119 )
120 ) {
121 return 'admin';
122 }
123
124 if (in_array('wpamelia-manager', $wpUser->roles, true)) {
125 return 'manager';
126 }
127
128 if (in_array('wpamelia-provider', $wpUser->roles, true)) {
129 return 'provider';
130 }
131
132 if (in_array('wpamelia-customer', $wpUser->roles, true)) {
133 return 'customer';
134 }
135
136 return null;
137 }
138 }
139