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