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 |