| 1 |
<?php |
| 2 |
|
| 3 |
namespace GeminiLabs\SiteReviews; |
| 4 |
|
| 5 |
use GeminiLabs\SiteReviews\Helpers\Str; |
| 6 |
|
| 7 |
class Role |
| 8 |
{ |
| 9 |
/** |
| 10 |
* @param array[] $roles |
| 11 |
*/ |
| 12 |
public function addCapabilities(string $role, array $roles = []): void |
| 13 |
{ |
| 14 |
if (empty($roles)) { |
| 15 |
$roles = $this->roles(); |
| 16 |
} |
| 17 |
$wpRole = get_role($role); |
| 18 |
if (empty($wpRole) || !array_key_exists($role, $roles)) { |
| 19 |
return; |
| 20 |
} |
| 21 |
foreach ($roles[$role] as $capability) { |
| 22 |
$wpRole->add_cap($this->capability($capability)); |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* @param mixed ...$args |
| 28 |
*/ |
| 29 |
public function can(string $capability, ...$args): bool |
| 30 |
{ |
| 31 |
return in_array($capability, $this->capabilities()) |
| 32 |
? current_user_can($this->capability($capability), ...$args) |
| 33 |
: current_user_can($capability, ...$args); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* @return string[] |
| 38 |
*/ |
| 39 |
public function capabilities(): array |
| 40 |
{ |
| 41 |
$capabilities = [ |
| 42 |
'create_posts', |
| 43 |
'delete_others_posts', |
| 44 |
'delete_post', |
| 45 |
'delete_posts', |
| 46 |
'delete_private_posts', |
| 47 |
'delete_published_posts', |
| 48 |
'edit_others_posts', |
| 49 |
'edit_post', |
| 50 |
'edit_posts', |
| 51 |
'edit_private_posts', |
| 52 |
'edit_published_posts', |
| 53 |
'publish_posts', |
| 54 |
'read_post', |
| 55 |
'read_private_posts', |
| 56 |
'respond_to_others_post', |
| 57 |
'respond_to_others_posts', |
| 58 |
'respond_to_post', |
| 59 |
'respond_to_posts', |
| 60 |
'assign_terms', |
| 61 |
'delete_terms', |
| 62 |
'edit_terms', |
| 63 |
'manage_terms', |
| 64 |
]; |
| 65 |
return glsr()->filterArray('capabilities', $capabilities); |
| 66 |
} |
| 67 |
|
| 68 |
public function capability(string $capability): string |
| 69 |
{ |
| 70 |
if (str_contains($capability, 'post')) { |
| 71 |
return str_replace('post', glsr()->post_type, $capability); |
| 72 |
} |
| 73 |
if (str_contains($capability, 'terms')) { |
| 74 |
return str_replace('terms', glsr()->post_type.'_terms', $capability); |
| 75 |
} |
| 76 |
return $capability; |
| 77 |
} |
| 78 |
|
| 79 |
public function hardResetAll(): void |
| 80 |
{ |
| 81 |
$roles = $this->roles(); |
| 82 |
array_walk($roles, fn ($caps, $role) => $this->removeCapabilities($role)); |
| 83 |
array_walk($roles, fn ($caps, $role) => $this->addCapabilities($role, $roles)); |
| 84 |
} |
| 85 |
|
| 86 |
public function removeCapabilities(string $role): void |
| 87 |
{ |
| 88 |
$wpRole = get_role($role); |
| 89 |
if (empty($wpRole) || 'administrator' === $role) { // do not remove from administrator role |
| 90 |
return; |
| 91 |
} |
| 92 |
foreach ($this->capabilities() as $capability) { |
| 93 |
$wpRole->remove_cap($this->capability($capability)); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* @param array[] $roles |
| 99 |
*/ |
| 100 |
public function reset(array $roles): void |
| 101 |
{ |
| 102 |
if (empty($roles)) { |
| 103 |
return; |
| 104 |
} |
| 105 |
array_walk($roles, fn ($caps, $role) => $this->addCapabilities($role, $roles)); |
| 106 |
} |
| 107 |
|
| 108 |
public function resetAll(): void |
| 109 |
{ |
| 110 |
$roles = $this->roles(); |
| 111 |
array_walk($roles, fn ($caps, $role) => $this->addCapabilities($role, $roles)); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* @return array[] |
| 116 |
*/ |
| 117 |
public function roles(): array |
| 118 |
{ |
| 119 |
$roles = [ |
| 120 |
'administrator' => [ |
| 121 |
'create_posts', |
| 122 |
'delete_others_posts', |
| 123 |
'delete_posts', |
| 124 |
'delete_private_posts', |
| 125 |
'delete_published_posts', |
| 126 |
'edit_others_posts', |
| 127 |
'edit_posts', |
| 128 |
'edit_private_posts', |
| 129 |
'edit_published_posts', |
| 130 |
'publish_posts', |
| 131 |
'read_private_posts', |
| 132 |
'respond_to_others_posts', |
| 133 |
'respond_to_posts', |
| 134 |
'assign_terms', |
| 135 |
'delete_terms', |
| 136 |
'edit_terms', |
| 137 |
'manage_terms', |
| 138 |
], |
| 139 |
'editor' => [ |
| 140 |
'create_posts', |
| 141 |
'delete_others_posts', |
| 142 |
'delete_posts', |
| 143 |
'delete_private_posts', |
| 144 |
'delete_published_posts', |
| 145 |
'edit_others_posts', |
| 146 |
'edit_posts', |
| 147 |
'edit_private_posts', |
| 148 |
'edit_published_posts', |
| 149 |
'publish_posts', |
| 150 |
'read_private_posts', |
| 151 |
'respond_to_others_posts', |
| 152 |
'respond_to_posts', |
| 153 |
'assign_terms', |
| 154 |
'delete_terms', |
| 155 |
'edit_terms', |
| 156 |
'manage_terms', |
| 157 |
], |
| 158 |
'author' => [ |
| 159 |
'create_posts', |
| 160 |
'delete_posts', |
| 161 |
'delete_published_posts', |
| 162 |
'edit_posts', |
| 163 |
'edit_published_posts', |
| 164 |
'publish_posts', |
| 165 |
'respond_to_posts', |
| 166 |
'assign_terms', |
| 167 |
'delete_terms', |
| 168 |
'edit_terms', |
| 169 |
'manage_terms', |
| 170 |
], |
| 171 |
'contributor' => [ |
| 172 |
'delete_posts', |
| 173 |
'edit_posts', |
| 174 |
'respond_to_posts', |
| 175 |
'assign_terms', |
| 176 |
], |
| 177 |
]; |
| 178 |
return glsr()->filterArray('roles', $roles); |
| 179 |
} |
| 180 |
} |
| 181 |
|