|
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
|
31 |
|
{ |
|
14
|
|
|
if (empty($roles)) { |
|
15
|
31 |
|
$roles = $this->roles(); |
|
16
|
31 |
|
} |
|
17
|
31 |
|
$wpRole = get_role($role); |
|
18
|
|
|
if (empty($wpRole) || !array_key_exists($role, $roles)) { |
|
19
|
|
|
return; |
|
20
|
31 |
|
} |
|
21
|
31 |
|
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
|
2 |
|
{ |
|
31
|
|
|
return in_array($capability, $this->capabilities()) |
|
32
|
2 |
|
? current_user_can($this->capability($capability), ...$args) |
|
33
|
|
|
: current_user_can($capability, ...$args); |
|
34
|
2 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return string[] |
|
38
|
|
|
*/ |
|
39
|
|
|
public function capabilities(): array |
|
40
|
2 |
|
{ |
|
41
|
|
|
$capabilities = [ |
|
42
|
2 |
|
'create_posts', |
|
43
|
2 |
|
'delete_others_posts', |
|
44
|
2 |
|
'delete_post', |
|
45
|
2 |
|
'delete_posts', |
|
46
|
2 |
|
'delete_private_posts', |
|
47
|
2 |
|
'delete_published_posts', |
|
48
|
2 |
|
'edit_others_posts', |
|
49
|
2 |
|
'edit_post', |
|
50
|
2 |
|
'edit_posts', |
|
51
|
2 |
|
'edit_private_posts', |
|
52
|
2 |
|
'edit_published_posts', |
|
53
|
2 |
|
'publish_posts', |
|
54
|
2 |
|
'read_post', |
|
55
|
2 |
|
'read_private_posts', |
|
56
|
2 |
|
'respond_to_others_post', |
|
57
|
2 |
|
'respond_to_others_posts', |
|
58
|
2 |
|
'respond_to_post', |
|
59
|
2 |
|
'respond_to_posts', |
|
60
|
2 |
|
'assign_terms', |
|
61
|
2 |
|
'delete_terms', |
|
62
|
2 |
|
'edit_terms', |
|
63
|
2 |
|
'manage_terms', |
|
64
|
2 |
|
]; |
|
65
|
2 |
|
return glsr()->filterArray('capabilities', $capabilities); |
|
66
|
2 |
|
} |
|
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
|
31 |
|
if (str_contains($capability, 'terms')) { |
|
74
|
|
|
return str_replace('terms', glsr()->post_type.'_terms', $capability); |
|
75
|
31 |
|
} |
|
76
|
31 |
|
return $capability; |
|
77
|
|
|
} |
|
78
|
31 |
|
|
|
79
|
31 |
|
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
|
31 |
|
} |
|
113
|
|
|
|
|
114
|
31 |
|
/** |
|
115
|
31 |
|
* @return array[] |
|
116
|
|
|
*/ |
|
117
|
|
|
public function roles(): array |
|
118
|
|
|
{ |
|
119
|
|
|
$roles = [ |
|
120
|
|
|
'administrator' => [ |
|
121
|
31 |
|
'create_posts', |
|
122
|
|
|
'delete_others_posts', |
|
123
|
31 |
|
'delete_posts', |
|
124
|
31 |
|
'delete_private_posts', |
|
125
|
31 |
|
'delete_published_posts', |
|
126
|
31 |
|
'edit_others_posts', |
|
127
|
31 |
|
'edit_posts', |
|
128
|
31 |
|
'edit_private_posts', |
|
129
|
31 |
|
'edit_published_posts', |
|
130
|
31 |
|
'publish_posts', |
|
131
|
31 |
|
'read_private_posts', |
|
132
|
31 |
|
'respond_to_others_posts', |
|
133
|
31 |
|
'respond_to_posts', |
|
134
|
31 |
|
'assign_terms', |
|
135
|
31 |
|
'delete_terms', |
|
136
|
31 |
|
'edit_terms', |
|
137
|
31 |
|
'manage_terms', |
|
138
|
31 |
|
], |
|
139
|
31 |
|
'editor' => [ |
|
140
|
31 |
|
'create_posts', |
|
141
|
31 |
|
'delete_others_posts', |
|
142
|
31 |
|
'delete_posts', |
|
143
|
31 |
|
'delete_private_posts', |
|
144
|
31 |
|
'delete_published_posts', |
|
145
|
31 |
|
'edit_others_posts', |
|
146
|
31 |
|
'edit_posts', |
|
147
|
31 |
|
'edit_private_posts', |
|
148
|
31 |
|
'edit_published_posts', |
|
149
|
31 |
|
'publish_posts', |
|
150
|
31 |
|
'read_private_posts', |
|
151
|
31 |
|
'respond_to_others_posts', |
|
152
|
31 |
|
'respond_to_posts', |
|
153
|
31 |
|
'assign_terms', |
|
154
|
31 |
|
'delete_terms', |
|
155
|
31 |
|
'edit_terms', |
|
156
|
31 |
|
'manage_terms', |
|
157
|
31 |
|
], |
|
158
|
31 |
|
'author' => [ |
|
159
|
31 |
|
'create_posts', |
|
160
|
31 |
|
'delete_posts', |
|
161
|
31 |
|
'delete_published_posts', |
|
162
|
31 |
|
'edit_posts', |
|
163
|
31 |
|
'edit_published_posts', |
|
164
|
31 |
|
'publish_posts', |
|
165
|
31 |
|
'respond_to_posts', |
|
166
|
31 |
|
'assign_terms', |
|
167
|
31 |
|
'delete_terms', |
|
168
|
31 |
|
'edit_terms', |
|
169
|
31 |
|
'manage_terms', |
|
170
|
31 |
|
], |
|
171
|
31 |
|
'contributor' => [ |
|
172
|
31 |
|
'delete_posts', |
|
173
|
31 |
|
'edit_posts', |
|
174
|
31 |
|
'respond_to_posts', |
|
175
|
31 |
|
'assign_terms', |
|
176
|
31 |
|
], |
|
177
|
31 |
|
]; |
|
178
|
31 |
|
return glsr()->filterArray('roles', $roles); |
|
179
|
31 |
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|