| 1 |
<?php |
| 2 |
/* |
| 3 |
* Copyright (c) 2023 LatePoint LLC. All rights reserved. |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace LatePoint\Misc; |
| 7 |
|
| 8 |
class Role { |
| 9 |
public ?string $user_type = null; |
| 10 |
public ?string $name; |
| 11 |
|
| 12 |
public ?string $wp_capability = null; |
| 13 |
public ?string $wp_role = null; |
| 14 |
|
| 15 |
protected array $capabilities = []; |
| 16 |
|
| 17 |
function __construct( $user_type = null, $wp_role = null ) { |
| 18 |
if ( $user_type ) { |
| 19 |
$this->user_type = $user_type; |
| 20 |
|
| 21 |
switch ( $this->user_type ) { |
| 22 |
case LATEPOINT_USER_TYPE_ADMIN: |
| 23 |
$this->name = __( 'Administrator', 'latepoint' ); |
| 24 |
$this->wp_role = LATEPOINT_WP_ADMIN_ROLE; |
| 25 |
break; |
| 26 |
case LATEPOINT_USER_TYPE_AGENT: |
| 27 |
$this->name = apply_filters( 'latepoint_role_display_name', __( 'LatePoint Agent', 'latepoint' ), LATEPOINT_USER_TYPE_AGENT ); |
| 28 |
$this->wp_role = LATEPOINT_WP_AGENT_ROLE; |
| 29 |
break; |
| 30 |
case LATEPOINT_USER_TYPE_CUSTOM: |
| 31 |
$this->name = __( 'New Custom Role', 'latepoint' ); |
| 32 |
$this->wp_role = $wp_role ?? \OsRolesHelper::generate_role_id(); |
| 33 |
break; |
| 34 |
} |
| 35 |
$this->set_capabilities(); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
public function register_in_wp(): \WP_Role { |
| 40 |
remove_role( $this->wp_role ); |
| 41 |
add_role( $this->wp_role, $this->name ); |
| 42 |
$role = get_role( $this->wp_role ); |
| 43 |
$role->add_cap( 'read' ); |
| 44 |
$role->add_cap( 'upload_files' ); |
| 45 |
switch ( $this->user_type ) { |
| 46 |
case LATEPOINT_USER_TYPE_AGENT: |
| 47 |
$role->add_cap( 'edit_bookings' ); |
| 48 |
break; |
| 49 |
case LATEPOINT_USER_TYPE_CUSTOM: |
| 50 |
$role->add_cap( 'manage_latepoint' ); |
| 51 |
break; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Custom role that was just been registered in LatePoint |
| 56 |
* |
| 57 |
* @since 4.7.0 |
| 58 |
* @hook latepoint_register_role |
| 59 |
* |
| 60 |
* @param {WP_Role} $wp_role custom role that was just been added |
| 61 |
* @param {Role} $role role that is used to register wp role |
| 62 |
* @returns {WP_Role} The filtered wp role object |
| 63 |
*/ |
| 64 |
return apply_filters( 'latepoint_register_role', $role, $this ); |
| 65 |
} |
| 66 |
|
| 67 |
public function as_array_to_save() { |
| 68 |
return [ |
| 69 |
'user_type' => $this->user_type, |
| 70 |
'wp_role' => $this->wp_role, |
| 71 |
'name' => $this->name, |
| 72 |
'capabilities' => $this->capabilities, |
| 73 |
]; |
| 74 |
} |
| 75 |
|
| 76 |
public function set_from_params( $params ) { |
| 77 |
$this->user_type = $params['user_type'] ?? LATEPOINT_USER_TYPE_CUSTOM; |
| 78 |
$this->wp_role = $params['wp_role'] ?? ''; |
| 79 |
$this->name = $params['name'] ?? ''; |
| 80 |
$this->capabilities = $params['capabilities'] ?? []; |
| 81 |
} |
| 82 |
|
| 83 |
public static function get_from_wp_role( string $wp_role ): Role { |
| 84 |
switch ( $wp_role ) { |
| 85 |
case LATEPOINT_WP_ADMIN_ROLE: |
| 86 |
$role = new self( LATEPOINT_USER_TYPE_ADMIN, LATEPOINT_WP_ADMIN_ROLE ); |
| 87 |
break; |
| 88 |
case LATEPOINT_WP_AGENT_ROLE: |
| 89 |
$role = new self( LATEPOINT_USER_TYPE_AGENT, LATEPOINT_WP_AGENT_ROLE ); |
| 90 |
|
| 91 |
break; |
| 92 |
default: |
| 93 |
// custom role |
| 94 |
$custom_roles = \OsRolesHelper::get_custom_roles(); |
| 95 |
$role = new self(); |
| 96 |
if ( isset( $custom_roles[ $wp_role ] ) ) { |
| 97 |
$role->set_from_params( $custom_roles[ $wp_role ] ); |
| 98 |
} |
| 99 |
break; |
| 100 |
} |
| 101 |
return $role; |
| 102 |
} |
| 103 |
|
| 104 |
public static function generate_from_params( array $params ): Role { |
| 105 |
$role = new self(); |
| 106 |
$role->set_from_params( $params ); |
| 107 |
return $role; |
| 108 |
} |
| 109 |
|
| 110 |
public function is_action_permitted( $action ) { |
| 111 |
return in_array( $action, $this->capabilities ); |
| 112 |
} |
| 113 |
|
| 114 |
public function get_wp_role_display_name() { |
| 115 |
switch ( $this->user_type ) { |
| 116 |
case LATEPOINT_USER_TYPE_ADMIN: |
| 117 |
$display_name = __( 'Administrator', 'latepoint' ); |
| 118 |
break; |
| 119 |
case LATEPOINT_USER_TYPE_AGENT: |
| 120 |
$display_name = apply_filters( 'latepoint_role_display_name', __( 'LatePoint Agent', 'latepoint' ), LATEPOINT_USER_TYPE_AGENT ); |
| 121 |
break; |
| 122 |
case LATEPOINT_USER_TYPE_CUSTOM: |
| 123 |
$display_name = $this->name; |
| 124 |
break; |
| 125 |
} |
| 126 |
return $display_name ?? 'n/a'; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
public function set_capabilities() { |
| 131 |
switch ( $this->user_type ) { |
| 132 |
case LATEPOINT_USER_TYPE_ADMIN: |
| 133 |
// admin role has access to all actions by default, and can't be changed |
| 134 |
$this->capabilities = \OsRolesHelper::get_all_available_actions_list(); |
| 135 |
break; |
| 136 |
case LATEPOINT_USER_TYPE_AGENT: |
| 137 |
$this->capabilities = \OsRolesHelper::get_capabilities_list_for_agent_role(); |
| 138 |
break; |
| 139 |
case LATEPOINT_USER_TYPE_CUSTOM: |
| 140 |
$custom_roles = \OsRolesHelper::get_custom_roles(); |
| 141 |
$this->capabilities = $custom_roles[ $this->wp_role ] ?? []; |
| 142 |
break; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
public function get_capabilities(): array { |
| 147 |
if ( empty( $this->capabilities ) ) { |
| 148 |
$this->set_capabilities(); |
| 149 |
} |
| 150 |
return $this->capabilities; |
| 151 |
} |
| 152 |
} |
| 153 |
|