1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews; |
4
|
|
|
|
5
|
|
|
class Role |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @param string $role |
9
|
|
|
* @return void |
10
|
|
|
*/ |
11
|
7 |
|
public function addCapabilities($role) |
12
|
|
|
{ |
13
|
7 |
|
$roleCapabilities = $this->roleCapabilities(); |
14
|
7 |
|
$wpRole = get_role($role); |
|
|
|
|
15
|
7 |
|
if (empty($wpRole) || !array_key_exists($role, $roleCapabilities)) { |
16
|
|
|
return; |
17
|
|
|
} |
18
|
7 |
|
foreach ($roleCapabilities[$role] as $capability) { |
19
|
7 |
|
$wpRole->add_cap($this->normalizeCapability($capability)); |
20
|
|
|
} |
21
|
7 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $capability |
25
|
|
|
* @return bool |
26
|
|
|
*/ |
27
|
|
|
public function can($capability) |
28
|
|
|
{ |
29
|
|
|
return in_array($capability, $this->capabilities()) |
30
|
|
|
? current_user_can($this->normalizeCapability($capability)) |
|
|
|
|
31
|
|
|
: current_user_can($capability); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
|
public function hardResetAll() |
38
|
|
|
{ |
39
|
|
|
$roles = array_keys($this->roleCapabilities()); |
40
|
|
|
array_walk($roles, [$this, 'removeCapabilities']); |
41
|
|
|
array_walk($roles, [$this, 'addCapabilities']); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $role |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
|
|
public function removeCapabilities($role) |
49
|
|
|
{ |
50
|
|
|
$wpRole = get_role($role); |
|
|
|
|
51
|
|
|
if (empty($wpRole) || 'administrator' === $role) { // do not remove from administrator role |
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
foreach ($this->capabilities() as $capability) { |
55
|
|
|
$wpRole->remove_cap($this->normalizeCapability($capability)); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
7 |
|
public function resetAll() |
63
|
|
|
{ |
64
|
7 |
|
$roles = array_keys($this->roleCapabilities()); |
65
|
7 |
|
array_walk($roles, [$this, 'addCapabilities']); |
66
|
7 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
protected function capabilities() |
72
|
|
|
{ |
73
|
|
|
return [ |
74
|
|
|
'delete_others_posts', |
75
|
|
|
'delete_post', |
76
|
|
|
'delete_posts', |
77
|
|
|
'delete_private_posts', |
78
|
|
|
'delete_published_posts', |
79
|
|
|
'edit_others_posts', |
80
|
|
|
'edit_post', |
81
|
|
|
'edit_posts', |
82
|
|
|
'edit_private_posts', |
83
|
|
|
'edit_published_posts', |
84
|
|
|
'publish_posts', |
85
|
|
|
'read_post', |
86
|
|
|
'read_private_posts', |
87
|
|
|
]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $capability |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
7 |
|
protected function normalizeCapability($capability) |
95
|
|
|
{ |
96
|
7 |
|
return str_replace('post', glsr()->post_type, $capability); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
7 |
|
protected function roleCapabilities() |
103
|
|
|
{ |
104
|
|
|
return [ |
105
|
|
|
'administrator' => [ |
106
|
7 |
|
'delete_others_posts', |
107
|
|
|
'delete_posts', |
108
|
|
|
'delete_private_posts', |
109
|
|
|
'delete_published_posts', |
110
|
|
|
'edit_others_posts', |
111
|
|
|
'edit_posts', |
112
|
|
|
'edit_private_posts', |
113
|
|
|
'edit_published_posts', |
114
|
|
|
'publish_posts', |
115
|
|
|
'read_private_posts', |
116
|
|
|
], |
117
|
|
|
'editor' => [ |
118
|
|
|
'delete_others_posts', |
119
|
|
|
'delete_posts', |
120
|
|
|
'delete_private_posts', |
121
|
|
|
'delete_published_posts', |
122
|
|
|
'edit_others_posts', |
123
|
|
|
'edit_posts', |
124
|
|
|
'edit_private_posts', |
125
|
|
|
'edit_published_posts', |
126
|
|
|
'publish_posts', |
127
|
|
|
'read_private_posts', |
128
|
|
|
], |
129
|
|
|
'author' => [ |
130
|
|
|
'delete_posts', |
131
|
|
|
'delete_published_posts', |
132
|
|
|
'edit_posts', |
133
|
|
|
'edit_published_posts', |
134
|
|
|
'publish_posts', |
135
|
|
|
], |
136
|
|
|
'contributor' => [ |
137
|
|
|
'delete_posts', |
138
|
|
|
'edit_posts', |
139
|
|
|
], |
140
|
|
|
]; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|