Access.php
7.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
namespace AIOSEO\Plugin\Common\Utils;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Access {
/**
* Capabilities for our users.
*
* @since 4.0.0
*
* @var array
*/
protected $capabilities = [
'aioseo_dashboard',
'aioseo_general_settings',
'aioseo_search_appearance_settings',
'aioseo_social_networks_settings',
'aioseo_sitemap_settings',
'aioseo_link_assistant_settings',
'aioseo_redirects_manage',
'aioseo_page_redirects_manage',
'aioseo_redirects_settings',
'aioseo_seo_analysis_settings',
'aioseo_search_statistics_settings',
'aioseo_tools_settings',
'aioseo_feature_manager_settings',
'aioseo_page_analysis',
'aioseo_page_general_settings',
'aioseo_page_advanced_settings',
'aioseo_page_schema_settings',
'aioseo_page_social_settings',
'aioseo_page_link_assistant_settings',
'aioseo_page_redirects_settings',
'aioseo_local_seo_settings',
'aioseo_page_local_seo_settings',
'aioseo_about_us_page',
'aioseo_setup_wizard'
];
/**
* Whether we're already updating the roles during this request.
*
* @since 4.2.7
*
* @var bool
*/
protected $isUpdatingRoles = false;
/**
* Roles we check capabilities against.
*
* @since 4.0.0
*
* @var array
*/
protected $roles = [
'superadmin' => 'superadmin',
'administrator' => 'administrator',
'editor' => 'editor',
'author' => 'author',
'contributor' => 'contributor'
];
/**
* Class constructor.
*
* @since 4.0.0
*/
public function __construct() {
// This needs to run before 1000 so that our update migrations and other hook callbacks can pull the roles.
add_action( 'init', [ $this, 'setRoles' ], 999 );
}
/**
* Sets the roles on the instance.
*
* @since 4.1.5
*
* @return void
*/
public function setRoles() {
$adminRoles = [];
$allRoles = aioseo()->helpers->getUserRoles();
foreach ( $allRoles as $roleName => $wpRole ) {
$role = get_role( $roleName );
if ( $this->isAdmin( $roleName ) || $role->has_cap( 'publish_posts' ) ) {
$adminRoles[ $roleName ] = $roleName;
}
}
$this->roles = array_merge( $this->roles, $adminRoles );
}
/**
* Adds capabilities into WordPress for the current user.
* Only on activation or settings saved.
*
* @since 4.0.0
*
* @return void
*/
public function addCapabilities() {
$this->isUpdatingRoles = true;
foreach ( $this->roles as $wpRole => $role ) {
$roleObject = get_role( $wpRole );
if ( ! is_object( $roleObject ) ) {
continue;
}
if ( $this->isAdmin( $role ) ) {
$roleObject->add_cap( 'aioseo_manage_seo' );
}
if ( current_user_can( 'edit_posts' ) ) {
$postCapabilities = [
'aioseo_page_analysis',
'aioseo_page_general_settings',
'aioseo_page_advanced_settings',
'aioseo_page_schema_settings',
'aioseo_page_social_settings',
];
foreach ( $postCapabilities as $capability ) {
$roleObject->add_cap( $capability );
}
}
}
$this->removeCapabilities();
}
/**
* Removes capabilities for any unknown role.
*
* @since 4.0.0
*
* @return void
*/
public function removeCapabilities() {
$this->isUpdatingRoles = true;
// Clear out capabilities for unknown roles.
$wpRoles = wp_roles();
$allRoles = $wpRoles->roles;
foreach ( $allRoles as $key => $wpRole ) {
$checkRole = is_multisite() ? 'superadmin' : 'administrator';
if ( $checkRole === $key ) {
continue;
}
if ( in_array( $key, $this->roles, true ) ) {
continue;
}
$role = get_role( $key );
if ( empty( $role ) ) {
continue;
}
// Any Admin can remain.
if ( $this->isAdmin( $key ) ) {
continue;
}
foreach ( $this->capabilities as $capability ) {
if ( $role->has_cap( $capability ) ) {
$role->remove_cap( $capability );
}
}
$role->remove_cap( 'aioseo_manage_seo' );
}
}
/**
* Checks if the current user has the capability.
*
* @since 4.0.0
*
* @param string $capability The capability to check against.
* @param string|null $checkRole A role to check against.
* @return bool Whether or not the user has this capability.
*/
public function hasCapability( $capability, $checkRole = null ) {
// Only admins have access.
if ( $this->isAdmin( $checkRole ) ) {
return true;
}
if (
(
$this->can( 'publish_posts', $checkRole ) ||
$this->can( 'edit_posts', $checkRole )
) &&
false !== strpos( $capability, 'aioseo_page_' )
) {
return true;
}
return false;
}
/**
* Gets all the capabilities for the current user.
*
* @since 4.0.0
*
* @param string|null $role A role to check against.
* @return array An array of capabilities.
*/
public function getAllCapabilities( $role = null ) {
$capabilities = [];
foreach ( $this->getCapabilityList() as $capability ) {
$capabilities[ $capability ] = $this->hasCapability( $capability, $role );
}
$capabilities['aioseo_admin'] = $this->isAdmin( $role );
$capabilities['aioseo_manage_seo'] = $this->isAdmin( $role );
$capabilities['aioseo_about_us_page'] = $this->canManage( $role );
return $capabilities;
}
/**
* Returns the capability list.
*
* @return 4.1.3
*
* @return array An array of capabilities.
*/
public function getCapabilityList() {
return $this->capabilities;
}
/**
* If the current user is an admin, or superadmin, they have access to all caps regardless.
*
* @since 4.0.0
*
* @param string|null $role The role to check admin privileges if we have one.
* @return bool Whether not the user/role is an admin.
*/
public function isAdmin( $role = null ) {
if ( $role ) {
if ( ( is_multisite() && 'superadmin' === $role ) || 'administrator' === $role ) {
return true;
}
return false;
}
if ( ( is_multisite() && current_user_can( 'superadmin' ) ) || current_user_can( 'administrator' ) ) {
return true;
}
return false;
}
/**
* Check if the passed in role can publish posts.
*
* @since 4.0.9
*
* @param string $capability The capability to check against.
* @param string $role The role to check.
* @return boolean True if the role can publish.
*/
protected function can( $capability, $role ) {
if ( empty( $role ) ) {
return current_user_can( $capability );
}
$wpRoles = wp_roles();
$allRoles = $wpRoles->roles;
foreach ( $allRoles as $key => $wpRole ) {
if ( $key === $role ) {
$r = get_role( $key );
if ( $r->has_cap( $capability ) ) {
return true;
}
}
}
return false;
}
/**
* Checks if the current user can manage AIOSEO.
*
* @since 4.0.0
*
* @param string|null $checkRole A role to check against.
* @return bool Whether or not the user can manage AIOSEO.
*/
public function canManage( $checkRole = null ) {
return $this->isAdmin( $checkRole );
}
/**
* Gets all options that the user does not have access to manage.
*
* @since 4.1.3
*
* @param string $role The given role.
* @return array An array with the option names.
*/
public function getNotAllowedOptions() {
return [];
}
/**
* Gets all page fields that the user does not have access to manage.
*
* @since 4.1.3
*
* @param string $role The given role.
* @return array An array with the field names.
*/
public function getNotAllowedPageFields() {
return [];
}
/**
* Returns Roles.
*
* @since 4.0.17
*
* @return array An array of role names.
*/
public function getRoles() {
return $this->roles;
}
}