PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / 5.1.3
LatePoint – Calendar Booking Plugin for Appointments and Events v5.1.3
5.6.6 5.6.5 5.6.4 5.6.3 5.6.2 5.6.1 5.6.0 5.5.2 5.5.1 5.5.0 5.4.2 trunk 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.1.8 5.1.9 5.1.91 5.1.92 5.1.93 5.1.94 5.2.0 5.2.1 5.2.10 5.2.11 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.3.0 5.3.1 5.3.2 5.4.0 5.4.1
latepoint / lib / misc / role.php
latepoint / lib / misc Last commit date
blocked_period.php 1 year ago booked_period.php 1 year ago booking_request.php 1 year ago booking_resource.php 1 year ago booking_slot.php 1 year ago filter.php 1 year ago process_action.php 1 year ago process_event.php 1 year ago role.php 1 year ago router.php 1 year ago step.php 1 year ago stripe_connect_customer.php 1 year ago time_period.php 1 year ago user.php 1 year ago work_period.php 1 year ago
role.php
143 lines
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 = __('LatePoint Agent', 'latepoint');
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 ['user_type' => $this->user_type, 'wp_role' => $this->wp_role, 'name' => $this->name, 'capabilities' => $this->capabilities];
69 }
70
71 public function set_from_params($params){
72 $this->user_type = $params['user_type'] ?? LATEPOINT_USER_TYPE_CUSTOM;
73 $this->wp_role = $params['wp_role'] ?? '';
74 $this->name = $params['name'] ?? '';
75 $this->capabilities = $params['capabilities'] ?? [];
76 }
77
78 public static function get_from_wp_role(string $wp_role): Role{
79 switch($wp_role){
80 case LATEPOINT_WP_ADMIN_ROLE:
81 $role = new self(LATEPOINT_USER_TYPE_ADMIN, LATEPOINT_WP_ADMIN_ROLE);
82 break;
83 case LATEPOINT_WP_AGENT_ROLE:
84 $role = new self(LATEPOINT_USER_TYPE_AGENT, LATEPOINT_WP_AGENT_ROLE);
85
86 break;
87 default:
88 // custom role
89 $custom_roles = \OsRolesHelper::get_custom_roles();
90 $role = new self();
91 if(isset($custom_roles[$wp_role])) $role->set_from_params($custom_roles[$wp_role]);
92 break;
93 }
94 return $role;
95 }
96
97 public static function generate_from_params(array $params): Role{
98 $role = new self();
99 $role->set_from_params($params);
100 return $role;
101 }
102
103 public function is_action_permitted($action){
104 return in_array($action, $this->capabilities);
105 }
106
107 public function get_wp_role_display_name(){
108 switch($this->user_type) {
109 case LATEPOINT_USER_TYPE_ADMIN:
110 $display_name = __('Administrator', 'latepoint');
111 break;
112 case LATEPOINT_USER_TYPE_AGENT:
113 $display_name = __('LatePoint Agent', 'latepoint');
114 break;
115 case LATEPOINT_USER_TYPE_CUSTOM:
116 $display_name = $this->name;
117 break;
118 }
119 return $display_name ?? 'n/a';
120 }
121
122
123 public function set_capabilities(){
124 switch($this->user_type){
125 case LATEPOINT_USER_TYPE_ADMIN:
126 // admin role has access to all actions by default, and can't be changed
127 $this->capabilities = \OsRolesHelper::get_all_available_actions_list();
128 break;
129 case LATEPOINT_USER_TYPE_AGENT:
130 $this->capabilities = \OsRolesHelper::get_capabilities_list_for_agent_role();
131 break;
132 case LATEPOINT_USER_TYPE_CUSTOM:
133 $custom_roles = \OsRolesHelper::get_custom_roles();
134 $this->capabilities = $custom_roles[$this->wp_role] ?? [];
135 break;
136 }
137 }
138
139 public function get_capabilities(): array{
140 if(empty($this->capabilities)) $this->set_capabilities();
141 return $this->capabilities;
142 }
143 }