| 1 |
<?php |
| 2 |
/** |
| 3 |
* Register Custom Role |
| 4 |
* |
| 5 |
* @package Timetics |
| 6 |
*/ |
| 7 |
namespace Timetics\Base; |
| 8 |
|
| 9 |
defined( 'ABSPATH' ) || exit; |
| 10 |
|
| 11 |
use Timetics\Utils\Singleton; |
| 12 |
|
| 13 |
/** |
| 14 |
* Class Role |
| 15 |
*/ |
| 16 |
class Role { |
| 17 |
use Singleton; |
| 18 |
|
| 19 |
/** |
| 20 |
* Initialize |
| 21 |
* |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
public function init() { |
| 25 |
$this->register_role(); |
| 26 |
$this->add_cap(); |
| 27 |
$this->set_role(); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Register role for staff |
| 32 |
* |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
public function register_role() { |
| 36 |
$roles = $this->get_roles(); |
| 37 |
|
| 38 |
foreach ( $roles as $role ) { |
| 39 |
// add_role() is a no-op if the role already exists, so on upgrade an |
| 40 |
// existing timetics-staff role keeps whatever caps it had before — |
| 41 |
// prune anything no longer in the definition below. |
| 42 |
$existing = get_role( $role['name'] ); |
| 43 |
|
| 44 |
add_role( |
| 45 |
$role['name'], |
| 46 |
$role['display_name'], |
| 47 |
$role['capabilities'] |
| 48 |
); |
| 49 |
|
| 50 |
if ( $existing ) { |
| 51 |
foreach ( array_diff( array_keys( $existing->capabilities ), array_keys( $role['capabilities'] ) ) as $stale_cap ) { |
| 52 |
$existing->remove_cap( $stale_cap ); |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Get roles |
| 60 |
* |
| 61 |
* @return array |
| 62 |
*/ |
| 63 |
public function get_roles() { |
| 64 |
$roles = [ |
| 65 |
[ |
| 66 |
'name' => 'timetics-staff', |
| 67 |
'display_name' => esc_html__( 'Staff', 'timetics' ), |
| 68 |
'capabilities' => [ |
| 69 |
'read' => true, |
| 70 |
'upload_files' => true, |
| 71 |
'read_booking' => true, |
| 72 |
'read_meeting' => true, |
| 73 |
'edit_profile' => true, |
| 74 |
'manage_timetics' => true, |
| 75 |
|
| 76 |
], |
| 77 |
], |
| 78 |
[ |
| 79 |
'name' => 'timetics-customer', |
| 80 |
'display_name' => esc_html__( 'Timetics Customer', 'timetics' ), |
| 81 |
'capabilities' => [ |
| 82 |
'read' => true, |
| 83 |
], |
| 84 |
], |
| 85 |
]; |
| 86 |
|
| 87 |
return apply_filters( 'timetics_staff_roles', $roles ); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Add capabilites to a role |
| 92 |
* |
| 93 |
* @return void |
| 94 |
*/ |
| 95 |
public function add_cap() { |
| 96 |
global $wp_roles; |
| 97 |
|
| 98 |
$capabilities = $this->get_capabilities(); |
| 99 |
$wp_roles->add_cap( 'administrator', 'manage_timetics' ); |
| 100 |
|
| 101 |
foreach ( $capabilities as $cap_group ) { |
| 102 |
foreach ( $cap_group as $cap ) { |
| 103 |
$wp_roles->add_cap( 'administrator', $cap ); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Get capabilities |
| 110 |
* |
| 111 |
* @return array |
| 112 |
*/ |
| 113 |
public function get_capabilities() { |
| 114 |
$capability_types = ['meeting', 'booking', 'staff']; |
| 115 |
|
| 116 |
$capabilites = []; |
| 117 |
|
| 118 |
foreach ( $capability_types as $capability_type ) { |
| 119 |
$capabilites[$capability_type] = [ |
| 120 |
// Post type. |
| 121 |
"edit_{$capability_type}", |
| 122 |
"read_{$capability_type}", |
| 123 |
"delete_{$capability_type}", |
| 124 |
"edit_{$capability_type}s", |
| 125 |
"edit_others_{$capability_type}s", |
| 126 |
"publish_{$capability_type}s", |
| 127 |
"read_private_{$capability_type}s", |
| 128 |
"delete_{$capability_type}s", |
| 129 |
"delete_private_{$capability_type}s", |
| 130 |
"delete_published_{$capability_type}s", |
| 131 |
"delete_others_{$capability_type}s", |
| 132 |
"edit_private_{$capability_type}s", |
| 133 |
]; |
| 134 |
} |
| 135 |
|
| 136 |
return $capabilites; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Set user role |
| 141 |
* |
| 142 |
* @return void |
| 143 |
*/ |
| 144 |
public function set_role() { |
| 145 |
$users = get_users( ['role' => 'administrator'] ); |
| 146 |
|
| 147 |
foreach( $users as $user ) { |
| 148 |
$user->add_role('timetics-staff'); |
| 149 |
$user->add_role('timetics-customer'); |
| 150 |
} |
| 151 |
|
| 152 |
$users = get_users( ['role' => 'timetics-staff'] ); |
| 153 |
|
| 154 |
foreach ( $users as $user ) { |
| 155 |
$user->add_role('timetics-customer'); |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|