author | ymh <ymh.work@gmail.com> |
Tue, 15 Dec 2020 13:49:49 +0100 | |
changeset 16 | a86126ab1dd4 |
parent 9 | 177826044cd9 |
child 18 | be944660c56a |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3 |
* Core User Role & Capabilities API |
0 | 4 |
* |
5 |
* @package WordPress |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6 |
* @subpackage Users |
0 | 7 |
*/ |
8 |
||
9 |
/** |
|
16 | 10 |
* Maps meta capabilities to primitive capabilities. |
11 |
* |
|
12 |
* This function also accepts an ID of an object to map against if the capability is a meta capability. Meta |
|
13 |
* capabilities such as `edit_post` and `edit_user` are capabilities used by this function to map to primitive |
|
14 |
* capabilities that a user or role has, such as `edit_posts` and `edit_others_posts`. |
|
15 |
* |
|
16 |
* Example usage: |
|
17 |
* |
|
18 |
* map_meta_cap( 'edit_posts', $user->ID ); |
|
19 |
* map_meta_cap( 'edit_post', $user->ID, $post->ID ); |
|
20 |
* map_meta_cap( 'edit_post_meta', $user->ID, $post->ID, $meta_key ); |
|
0 | 21 |
* |
22 |
* This does not actually compare whether the user ID has the actual capability, |
|
23 |
* just what the capability or capabilities are. Meta capability list value can |
|
24 |
* be 'delete_user', 'edit_user', 'remove_user', 'promote_user', 'delete_post', |
|
25 |
* 'delete_page', 'edit_post', 'edit_page', 'read_post', or 'read_page'. |
|
26 |
* |
|
27 |
* @since 2.0.0 |
|
16 | 28 |
* @since 5.3.0 Formalized the existing and already documented `...$args` parameter |
29 |
* by adding it to the function signature. |
|
0 | 30 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
31 |
* @global array $post_type_meta_caps Used to get post type meta capabilities. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
32 |
* |
16 | 33 |
* @param string $cap Capability name. |
34 |
* @param int $user_id User ID. |
|
35 |
* @param mixed ...$args Optional further parameters, typically starting with an object ID. |
|
36 |
* @return string[] Actual capabilities for meta capability. |
|
0 | 37 |
*/ |
16 | 38 |
function map_meta_cap( $cap, $user_id, ...$args ) { |
0 | 39 |
$caps = array(); |
40 |
||
41 |
switch ( $cap ) { |
|
9 | 42 |
case 'remove_user': |
43 |
// In multisite the user must be a super admin to remove themselves. |
|
44 |
if ( isset( $args[0] ) && $user_id == $args[0] && ! is_super_admin( $user_id ) ) { |
|
45 |
$caps[] = 'do_not_allow'; |
|
46 |
} else { |
|
47 |
$caps[] = 'remove_users'; |
|
48 |
} |
|
49 |
break; |
|
50 |
case 'promote_user': |
|
51 |
case 'add_users': |
|
52 |
$caps[] = 'promote_users'; |
|
0 | 53 |
break; |
9 | 54 |
case 'edit_user': |
55 |
case 'edit_users': |
|
16 | 56 |
// Allow user to edit themselves. |
57 |
if ( 'edit_user' === $cap && isset( $args[0] ) && $user_id == $args[0] ) { |
|
9 | 58 |
break; |
59 |
} |
|
60 |
||
61 |
// In multisite the user must have manage_network_users caps. If editing a super admin, the user must be a super admin. |
|
62 |
if ( is_multisite() && ( ( ! is_super_admin( $user_id ) && 'edit_user' === $cap && is_super_admin( $args[0] ) ) || ! user_can( $user_id, 'manage_network_users' ) ) ) { |
|
63 |
$caps[] = 'do_not_allow'; |
|
64 |
} else { |
|
65 |
$caps[] = 'edit_users'; // edit_user maps to edit_users. |
|
66 |
} |
|
67 |
break; |
|
68 |
case 'delete_post': |
|
69 |
case 'delete_page': |
|
70 |
$post = get_post( $args[0] ); |
|
71 |
if ( ! $post ) { |
|
72 |
$caps[] = 'do_not_allow'; |
|
73 |
break; |
|
74 |
} |
|
75 |
||
16 | 76 |
if ( 'revision' === $post->post_type ) { |
77 |
$caps[] = 'do_not_allow'; |
|
78 |
break; |
|
9 | 79 |
} |
80 |
||
81 |
if ( ( get_option( 'page_for_posts' ) == $post->ID ) || ( get_option( 'page_on_front' ) == $post->ID ) ) { |
|
82 |
$caps[] = 'manage_options'; |
|
83 |
break; |
|
84 |
} |
|
0 | 85 |
|
9 | 86 |
$post_type = get_post_type_object( $post->post_type ); |
87 |
if ( ! $post_type ) { |
|
16 | 88 |
/* translators: 1: Post type, 2: Capability name. */ |
9 | 89 |
_doing_it_wrong( __FUNCTION__, sprintf( __( 'The post type %1$s is not registered, so it may not be reliable to check the capability "%2$s" against a post of that type.' ), $post->post_type, $cap ), '4.4.0' ); |
90 |
$caps[] = 'edit_others_posts'; |
|
91 |
break; |
|
92 |
} |
|
93 |
||
94 |
if ( ! $post_type->map_meta_cap ) { |
|
95 |
$caps[] = $post_type->cap->$cap; |
|
96 |
// Prior to 3.1 we would re-call map_meta_cap here. |
|
16 | 97 |
if ( 'delete_post' === $cap ) { |
9 | 98 |
$cap = $post_type->cap->$cap; |
99 |
} |
|
100 |
break; |
|
101 |
} |
|
102 |
||
103 |
// If the post author is set and the user is the author... |
|
104 |
if ( $post->post_author && $user_id == $post->post_author ) { |
|
105 |
// If the post is published or scheduled... |
|
106 |
if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) { |
|
107 |
$caps[] = $post_type->cap->delete_published_posts; |
|
16 | 108 |
} elseif ( 'trash' === $post->post_status ) { |
9 | 109 |
$status = get_post_meta( $post->ID, '_wp_trash_meta_status', true ); |
110 |
if ( in_array( $status, array( 'publish', 'future' ), true ) ) { |
|
111 |
$caps[] = $post_type->cap->delete_published_posts; |
|
112 |
} else { |
|
113 |
$caps[] = $post_type->cap->delete_posts; |
|
114 |
} |
|
115 |
} else { |
|
116 |
// If the post is draft... |
|
117 |
$caps[] = $post_type->cap->delete_posts; |
|
118 |
} |
|
119 |
} else { |
|
120 |
// The user is trying to edit someone else's post. |
|
121 |
$caps[] = $post_type->cap->delete_others_posts; |
|
122 |
// The post is published or scheduled, extra cap required. |
|
123 |
if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) { |
|
124 |
$caps[] = $post_type->cap->delete_published_posts; |
|
16 | 125 |
} elseif ( 'private' === $post->post_status ) { |
9 | 126 |
$caps[] = $post_type->cap->delete_private_posts; |
127 |
} |
|
128 |
} |
|
129 |
||
130 |
/* |
|
131 |
* Setting the privacy policy page requires `manage_privacy_options`, |
|
132 |
* so deleting it should require that too. |
|
133 |
*/ |
|
134 |
if ( (int) get_option( 'wp_page_for_privacy_policy' ) === $post->ID ) { |
|
135 |
$caps = array_merge( $caps, map_meta_cap( 'manage_privacy_options', $user_id ) ); |
|
136 |
} |
|
137 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
138 |
break; |
9 | 139 |
// edit_post breaks down to edit_posts, edit_published_posts, or |
16 | 140 |
// edit_others_posts. |
9 | 141 |
case 'edit_post': |
142 |
case 'edit_page': |
|
143 |
$post = get_post( $args[0] ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
144 |
if ( ! $post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
145 |
$caps[] = 'do_not_allow'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
146 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
147 |
} |
0 | 148 |
|
16 | 149 |
if ( 'revision' === $post->post_type ) { |
9 | 150 |
$post = get_post( $post->post_parent ); |
151 |
if ( ! $post ) { |
|
152 |
$caps[] = 'do_not_allow'; |
|
153 |
break; |
|
154 |
} |
|
155 |
} |
|
156 |
||
157 |
$post_type = get_post_type_object( $post->post_type ); |
|
158 |
if ( ! $post_type ) { |
|
16 | 159 |
/* translators: 1: Post type, 2: Capability name. */ |
9 | 160 |
_doing_it_wrong( __FUNCTION__, sprintf( __( 'The post type %1$s is not registered, so it may not be reliable to check the capability "%2$s" against a post of that type.' ), $post->post_type, $cap ), '4.4.0' ); |
161 |
$caps[] = 'edit_others_posts'; |
|
162 |
break; |
|
163 |
} |
|
0 | 164 |
|
9 | 165 |
if ( ! $post_type->map_meta_cap ) { |
166 |
$caps[] = $post_type->cap->$cap; |
|
167 |
// Prior to 3.1 we would re-call map_meta_cap here. |
|
16 | 168 |
if ( 'edit_post' === $cap ) { |
9 | 169 |
$cap = $post_type->cap->$cap; |
170 |
} |
|
171 |
break; |
|
172 |
} |
|
0 | 173 |
|
9 | 174 |
// If the post author is set and the user is the author... |
175 |
if ( $post->post_author && $user_id == $post->post_author ) { |
|
176 |
// If the post is published or scheduled... |
|
177 |
if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) { |
|
178 |
$caps[] = $post_type->cap->edit_published_posts; |
|
16 | 179 |
} elseif ( 'trash' === $post->post_status ) { |
9 | 180 |
$status = get_post_meta( $post->ID, '_wp_trash_meta_status', true ); |
181 |
if ( in_array( $status, array( 'publish', 'future' ), true ) ) { |
|
182 |
$caps[] = $post_type->cap->edit_published_posts; |
|
183 |
} else { |
|
184 |
$caps[] = $post_type->cap->edit_posts; |
|
185 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
186 |
} else { |
9 | 187 |
// If the post is draft... |
188 |
$caps[] = $post_type->cap->edit_posts; |
|
5 | 189 |
} |
0 | 190 |
} else { |
9 | 191 |
// The user is trying to edit someone else's post. |
192 |
$caps[] = $post_type->cap->edit_others_posts; |
|
193 |
// The post is published or scheduled, extra cap required. |
|
194 |
if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) { |
|
195 |
$caps[] = $post_type->cap->edit_published_posts; |
|
16 | 196 |
} elseif ( 'private' === $post->post_status ) { |
9 | 197 |
$caps[] = $post_type->cap->edit_private_posts; |
198 |
} |
|
0 | 199 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
200 |
|
9 | 201 |
/* |
202 |
* Setting the privacy policy page requires `manage_privacy_options`, |
|
203 |
* so editing it should require that too. |
|
204 |
*/ |
|
205 |
if ( (int) get_option( 'wp_page_for_privacy_policy' ) === $post->ID ) { |
|
206 |
$caps = array_merge( $caps, map_meta_cap( 'manage_privacy_options', $user_id ) ); |
|
207 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
208 |
|
0 | 209 |
break; |
9 | 210 |
case 'read_post': |
211 |
case 'read_page': |
|
212 |
$post = get_post( $args[0] ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
213 |
if ( ! $post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
214 |
$caps[] = 'do_not_allow'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
215 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
216 |
} |
0 | 217 |
|
16 | 218 |
if ( 'revision' === $post->post_type ) { |
9 | 219 |
$post = get_post( $post->post_parent ); |
220 |
if ( ! $post ) { |
|
221 |
$caps[] = 'do_not_allow'; |
|
222 |
break; |
|
223 |
} |
|
224 |
} |
|
225 |
||
226 |
$post_type = get_post_type_object( $post->post_type ); |
|
227 |
if ( ! $post_type ) { |
|
16 | 228 |
/* translators: 1: Post type, 2: Capability name. */ |
9 | 229 |
_doing_it_wrong( __FUNCTION__, sprintf( __( 'The post type %1$s is not registered, so it may not be reliable to check the capability "%2$s" against a post of that type.' ), $post->post_type, $cap ), '4.4.0' ); |
230 |
$caps[] = 'edit_others_posts'; |
|
231 |
break; |
|
232 |
} |
|
0 | 233 |
|
9 | 234 |
if ( ! $post_type->map_meta_cap ) { |
235 |
$caps[] = $post_type->cap->$cap; |
|
236 |
// Prior to 3.1 we would re-call map_meta_cap here. |
|
16 | 237 |
if ( 'read_post' === $cap ) { |
9 | 238 |
$cap = $post_type->cap->$cap; |
239 |
} |
|
240 |
break; |
|
241 |
} |
|
0 | 242 |
|
9 | 243 |
$status_obj = get_post_status_object( $post->post_status ); |
16 | 244 |
if ( ! $status_obj ) { |
245 |
/* translators: 1: Post status, 2: Capability name. */ |
|
246 |
_doing_it_wrong( __FUNCTION__, sprintf( __( 'The post status %1$s is not registered, so it may not be reliable to check the capability "%2$s" against a post with that status.' ), $post->post_status, $cap ), '5.4.0' ); |
|
247 |
$caps[] = 'edit_others_posts'; |
|
248 |
break; |
|
249 |
} |
|
250 |
||
9 | 251 |
if ( $status_obj->public ) { |
252 |
$caps[] = $post_type->cap->read; |
|
253 |
break; |
|
254 |
} |
|
255 |
||
256 |
if ( $post->post_author && $user_id == $post->post_author ) { |
|
257 |
$caps[] = $post_type->cap->read; |
|
258 |
} elseif ( $status_obj->private ) { |
|
259 |
$caps[] = $post_type->cap->read_private_posts; |
|
0 | 260 |
} else { |
9 | 261 |
$caps = map_meta_cap( 'edit_post', $user_id, $post->ID ); |
5 | 262 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
263 |
break; |
9 | 264 |
case 'publish_post': |
265 |
$post = get_post( $args[0] ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
266 |
if ( ! $post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
267 |
$caps[] = 'do_not_allow'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
268 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
269 |
} |
0 | 270 |
|
9 | 271 |
$post_type = get_post_type_object( $post->post_type ); |
272 |
if ( ! $post_type ) { |
|
16 | 273 |
/* translators: 1: Post type, 2: Capability name. */ |
9 | 274 |
_doing_it_wrong( __FUNCTION__, sprintf( __( 'The post type %1$s is not registered, so it may not be reliable to check the capability "%2$s" against a post of that type.' ), $post->post_type, $cap ), '4.4.0' ); |
275 |
$caps[] = 'edit_others_posts'; |
|
276 |
break; |
|
277 |
} |
|
0 | 278 |
|
9 | 279 |
$caps[] = $post_type->cap->publish_posts; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
280 |
break; |
9 | 281 |
case 'edit_post_meta': |
282 |
case 'delete_post_meta': |
|
283 |
case 'add_post_meta': |
|
284 |
case 'edit_comment_meta': |
|
285 |
case 'delete_comment_meta': |
|
286 |
case 'add_comment_meta': |
|
287 |
case 'edit_term_meta': |
|
288 |
case 'delete_term_meta': |
|
289 |
case 'add_term_meta': |
|
290 |
case 'edit_user_meta': |
|
291 |
case 'delete_user_meta': |
|
292 |
case 'add_user_meta': |
|
16 | 293 |
$object_type = explode( '_', $cap )[1]; |
294 |
$object_id = (int) $args[0]; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
295 |
|
9 | 296 |
$object_subtype = get_object_subtype( $object_type, $object_id ); |
0 | 297 |
|
9 | 298 |
if ( empty( $object_subtype ) ) { |
299 |
$caps[] = 'do_not_allow'; |
|
300 |
break; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
301 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
302 |
|
9 | 303 |
$caps = map_meta_cap( "edit_{$object_type}", $user_id, $object_id ); |
304 |
||
305 |
$meta_key = isset( $args[1] ) ? $args[1] : false; |
|
306 |
||
307 |
if ( $meta_key ) { |
|
308 |
$allowed = ! is_protected_meta( $meta_key, $object_type ); |
|
309 |
||
310 |
if ( ! empty( $object_subtype ) && has_filter( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}" ) ) { |
|
311 |
||
312 |
/** |
|
313 |
* Filters whether the user is allowed to edit a specific meta key of a specific object type and subtype. |
|
314 |
* |
|
315 |
* The dynamic portions of the hook name, `$object_type`, `$meta_key`, |
|
316 |
* and `$object_subtype`, refer to the metadata object type (comment, post, term or user), |
|
317 |
* the meta key value, and the object subtype respectively. |
|
318 |
* |
|
319 |
* @since 4.9.8 |
|
320 |
* |
|
321 |
* @param bool $allowed Whether the user can add the object meta. Default false. |
|
322 |
* @param string $meta_key The meta key. |
|
323 |
* @param int $object_id Object ID. |
|
324 |
* @param int $user_id User ID. |
|
325 |
* @param string $cap Capability name. |
|
326 |
* @param string[] $caps Array of the user's capabilities. |
|
327 |
*/ |
|
328 |
$allowed = apply_filters( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $allowed, $meta_key, $object_id, $user_id, $cap, $caps ); |
|
329 |
} else { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
330 |
|
9 | 331 |
/** |
332 |
* Filters whether the user is allowed to edit a specific meta key of a specific object type. |
|
333 |
* |
|
334 |
* Return true to have the mapped meta caps from `edit_{$object_type}` apply. |
|
335 |
* |
|
336 |
* The dynamic portion of the hook name, `$object_type` refers to the object type being filtered. |
|
337 |
* The dynamic portion of the hook name, `$meta_key`, refers to the meta key passed to map_meta_cap(). |
|
338 |
* |
|
339 |
* @since 3.3.0 As `auth_post_meta_{$meta_key}`. |
|
340 |
* @since 4.6.0 |
|
341 |
* |
|
342 |
* @param bool $allowed Whether the user can add the object meta. Default false. |
|
343 |
* @param string $meta_key The meta key. |
|
344 |
* @param int $object_id Object ID. |
|
345 |
* @param int $user_id User ID. |
|
346 |
* @param string $cap Capability name. |
|
347 |
* @param string[] $caps Array of the user's capabilities. |
|
348 |
*/ |
|
349 |
$allowed = apply_filters( "auth_{$object_type}_meta_{$meta_key}", $allowed, $meta_key, $object_id, $user_id, $cap, $caps ); |
|
350 |
} |
|
351 |
||
352 |
if ( ! empty( $object_subtype ) ) { |
|
353 |
||
354 |
/** |
|
355 |
* Filters whether the user is allowed to edit meta for specific object types/subtypes. |
|
356 |
* |
|
357 |
* Return true to have the mapped meta caps from `edit_{$object_type}` apply. |
|
358 |
* |
|
359 |
* The dynamic portion of the hook name, `$object_type` refers to the object type being filtered. |
|
360 |
* The dynamic portion of the hook name, `$object_subtype` refers to the object subtype being filtered. |
|
361 |
* The dynamic portion of the hook name, `$meta_key`, refers to the meta key passed to map_meta_cap(). |
|
362 |
* |
|
363 |
* @since 4.6.0 As `auth_post_{$post_type}_meta_{$meta_key}`. |
|
16 | 364 |
* @since 4.7.0 Renamed from `auth_post_{$post_type}_meta_{$meta_key}` to |
365 |
* `auth_{$object_type}_{$object_subtype}_meta_{$meta_key}`. |
|
366 |
* @deprecated 4.9.8 Use {@see 'auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}'} instead. |
|
9 | 367 |
* |
368 |
* @param bool $allowed Whether the user can add the object meta. Default false. |
|
369 |
* @param string $meta_key The meta key. |
|
370 |
* @param int $object_id Object ID. |
|
371 |
* @param int $user_id User ID. |
|
372 |
* @param string $cap Capability name. |
|
373 |
* @param string[] $caps Array of the user's capabilities. |
|
374 |
*/ |
|
16 | 375 |
$allowed = apply_filters_deprecated( |
376 |
"auth_{$object_type}_{$object_subtype}_meta_{$meta_key}", |
|
377 |
array( $allowed, $meta_key, $object_id, $user_id, $cap, $caps ), |
|
378 |
'4.9.8', |
|
379 |
"auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}" |
|
380 |
); |
|
9 | 381 |
} |
382 |
||
383 |
if ( ! $allowed ) { |
|
384 |
$caps[] = $cap; |
|
385 |
} |
|
386 |
} |
|
387 |
break; |
|
388 |
case 'edit_comment': |
|
389 |
$comment = get_comment( $args[0] ); |
|
390 |
if ( ! $comment ) { |
|
391 |
$caps[] = 'do_not_allow'; |
|
392 |
break; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
393 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
394 |
|
9 | 395 |
$post = get_post( $comment->comment_post_ID ); |
396 |
||
397 |
/* |
|
398 |
* If the post doesn't exist, we have an orphaned comment. |
|
399 |
* Fall back to the edit_posts capability, instead. |
|
400 |
*/ |
|
401 |
if ( $post ) { |
|
402 |
$caps = map_meta_cap( 'edit_post', $user_id, $post->ID ); |
|
403 |
} else { |
|
404 |
$caps = map_meta_cap( 'edit_posts', $user_id ); |
|
405 |
} |
|
406 |
break; |
|
407 |
case 'unfiltered_upload': |
|
408 |
if ( defined( 'ALLOW_UNFILTERED_UPLOADS' ) && ALLOW_UNFILTERED_UPLOADS && ( ! is_multisite() || is_super_admin( $user_id ) ) ) { |
|
409 |
$caps[] = $cap; |
|
410 |
} else { |
|
411 |
$caps[] = 'do_not_allow'; |
|
412 |
} |
|
413 |
break; |
|
414 |
case 'edit_css': |
|
415 |
case 'unfiltered_html': |
|
416 |
// Disallow unfiltered_html for all users, even admins and super admins. |
|
417 |
if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML ) { |
|
418 |
$caps[] = 'do_not_allow'; |
|
419 |
} elseif ( is_multisite() && ! is_super_admin( $user_id ) ) { |
|
420 |
$caps[] = 'do_not_allow'; |
|
421 |
} else { |
|
422 |
$caps[] = 'unfiltered_html'; |
|
423 |
} |
|
424 |
break; |
|
425 |
case 'edit_files': |
|
426 |
case 'edit_plugins': |
|
427 |
case 'edit_themes': |
|
428 |
// Disallow the file editors. |
|
429 |
if ( defined( 'DISALLOW_FILE_EDIT' ) && DISALLOW_FILE_EDIT ) { |
|
430 |
$caps[] = 'do_not_allow'; |
|
431 |
} elseif ( ! wp_is_file_mod_allowed( 'capability_edit_themes' ) ) { |
|
432 |
$caps[] = 'do_not_allow'; |
|
433 |
} elseif ( is_multisite() && ! is_super_admin( $user_id ) ) { |
|
434 |
$caps[] = 'do_not_allow'; |
|
435 |
} else { |
|
0 | 436 |
$caps[] = $cap; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
437 |
} |
9 | 438 |
break; |
439 |
case 'update_plugins': |
|
440 |
case 'delete_plugins': |
|
441 |
case 'install_plugins': |
|
442 |
case 'upload_plugins': |
|
443 |
case 'update_themes': |
|
444 |
case 'delete_themes': |
|
445 |
case 'install_themes': |
|
446 |
case 'upload_themes': |
|
447 |
case 'update_core': |
|
448 |
// Disallow anything that creates, deletes, or updates core, plugin, or theme files. |
|
449 |
// Files in uploads are excepted. |
|
450 |
if ( ! wp_is_file_mod_allowed( 'capability_update_core' ) ) { |
|
451 |
$caps[] = 'do_not_allow'; |
|
452 |
} elseif ( is_multisite() && ! is_super_admin( $user_id ) ) { |
|
453 |
$caps[] = 'do_not_allow'; |
|
454 |
} elseif ( 'upload_themes' === $cap ) { |
|
455 |
$caps[] = 'install_themes'; |
|
456 |
} elseif ( 'upload_plugins' === $cap ) { |
|
457 |
$caps[] = 'install_plugins'; |
|
458 |
} else { |
|
459 |
$caps[] = $cap; |
|
460 |
} |
|
461 |
break; |
|
462 |
case 'install_languages': |
|
463 |
case 'update_languages': |
|
464 |
if ( ! wp_is_file_mod_allowed( 'can_install_language_pack' ) ) { |
|
465 |
$caps[] = 'do_not_allow'; |
|
466 |
} elseif ( is_multisite() && ! is_super_admin( $user_id ) ) { |
|
467 |
$caps[] = 'do_not_allow'; |
|
468 |
} else { |
|
469 |
$caps[] = 'install_languages'; |
|
470 |
} |
|
471 |
break; |
|
472 |
case 'activate_plugins': |
|
473 |
case 'deactivate_plugins': |
|
474 |
case 'activate_plugin': |
|
475 |
case 'deactivate_plugin': |
|
476 |
$caps[] = 'activate_plugins'; |
|
477 |
if ( is_multisite() ) { |
|
478 |
// update_, install_, and delete_ are handled above with is_super_admin(). |
|
479 |
$menu_perms = get_site_option( 'menu_items', array() ); |
|
480 |
if ( empty( $menu_perms['plugins'] ) ) { |
|
481 |
$caps[] = 'manage_network_plugins'; |
|
482 |
} |
|
483 |
} |
|
0 | 484 |
break; |
9 | 485 |
case 'resume_plugin': |
486 |
$caps[] = 'resume_plugins'; |
|
487 |
break; |
|
488 |
case 'resume_theme': |
|
489 |
$caps[] = 'resume_themes'; |
|
490 |
break; |
|
491 |
case 'delete_user': |
|
492 |
case 'delete_users': |
|
493 |
// If multisite only super admins can delete users. |
|
494 |
if ( is_multisite() && ! is_super_admin( $user_id ) ) { |
|
495 |
$caps[] = 'do_not_allow'; |
|
496 |
} else { |
|
497 |
$caps[] = 'delete_users'; // delete_user maps to delete_users. |
|
498 |
} |
|
499 |
break; |
|
500 |
case 'create_users': |
|
501 |
if ( ! is_multisite() ) { |
|
502 |
$caps[] = $cap; |
|
503 |
} elseif ( is_super_admin( $user_id ) || get_site_option( 'add_new_users' ) ) { |
|
504 |
$caps[] = $cap; |
|
505 |
} else { |
|
506 |
$caps[] = 'do_not_allow'; |
|
507 |
} |
|
508 |
break; |
|
509 |
case 'manage_links': |
|
510 |
if ( get_option( 'link_manager_enabled' ) ) { |
|
511 |
$caps[] = $cap; |
|
512 |
} else { |
|
513 |
$caps[] = 'do_not_allow'; |
|
514 |
} |
|
515 |
break; |
|
516 |
case 'customize': |
|
517 |
$caps[] = 'edit_theme_options'; |
|
518 |
break; |
|
519 |
case 'delete_site': |
|
520 |
if ( is_multisite() ) { |
|
521 |
$caps[] = 'manage_options'; |
|
522 |
} else { |
|
523 |
$caps[] = 'do_not_allow'; |
|
524 |
} |
|
525 |
break; |
|
526 |
case 'edit_term': |
|
527 |
case 'delete_term': |
|
528 |
case 'assign_term': |
|
529 |
$term_id = (int) $args[0]; |
|
530 |
$term = get_term( $term_id ); |
|
531 |
if ( ! $term || is_wp_error( $term ) ) { |
|
532 |
$caps[] = 'do_not_allow'; |
|
533 |
break; |
|
534 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
535 |
|
9 | 536 |
$tax = get_taxonomy( $term->taxonomy ); |
537 |
if ( ! $tax ) { |
|
538 |
$caps[] = 'do_not_allow'; |
|
539 |
break; |
|
540 |
} |
|
541 |
||
16 | 542 |
if ( 'delete_term' === $cap |
543 |
&& ( get_option( 'default_' . $term->taxonomy ) == $term->term_id |
|
544 |
|| get_option( 'default_term_' . $term->taxonomy ) == $term->term_id ) |
|
545 |
) { |
|
9 | 546 |
$caps[] = 'do_not_allow'; |
547 |
break; |
|
548 |
} |
|
549 |
||
550 |
$taxo_cap = $cap . 's'; |
|
551 |
||
552 |
$caps = map_meta_cap( $tax->cap->$taxo_cap, $user_id, $term_id ); |
|
553 |
||
554 |
break; |
|
555 |
case 'manage_post_tags': |
|
556 |
case 'edit_categories': |
|
557 |
case 'edit_post_tags': |
|
558 |
case 'delete_categories': |
|
559 |
case 'delete_post_tags': |
|
560 |
$caps[] = 'manage_categories'; |
|
561 |
break; |
|
562 |
case 'assign_categories': |
|
563 |
case 'assign_post_tags': |
|
564 |
$caps[] = 'edit_posts'; |
|
565 |
break; |
|
566 |
case 'create_sites': |
|
567 |
case 'delete_sites': |
|
568 |
case 'manage_network': |
|
569 |
case 'manage_sites': |
|
570 |
case 'manage_network_users': |
|
571 |
case 'manage_network_plugins': |
|
572 |
case 'manage_network_themes': |
|
573 |
case 'manage_network_options': |
|
574 |
case 'upgrade_network': |
|
0 | 575 |
$caps[] = $cap; |
9 | 576 |
break; |
577 |
case 'setup_network': |
|
578 |
if ( is_multisite() ) { |
|
579 |
$caps[] = 'manage_network_options'; |
|
580 |
} else { |
|
581 |
$caps[] = 'manage_options'; |
|
582 |
} |
|
583 |
break; |
|
584 |
case 'update_php': |
|
585 |
if ( is_multisite() && ! is_super_admin( $user_id ) ) { |
|
586 |
$caps[] = 'do_not_allow'; |
|
587 |
} else { |
|
588 |
$caps[] = 'update_core'; |
|
589 |
} |
|
590 |
break; |
|
591 |
case 'export_others_personal_data': |
|
592 |
case 'erase_others_personal_data': |
|
593 |
case 'manage_privacy_options': |
|
594 |
$caps[] = is_multisite() ? 'manage_network' : 'manage_options'; |
|
595 |
break; |
|
596 |
default: |
|
597 |
// Handle meta capabilities for custom post types. |
|
598 |
global $post_type_meta_caps; |
|
599 |
if ( isset( $post_type_meta_caps[ $cap ] ) ) { |
|
16 | 600 |
return map_meta_cap( $post_type_meta_caps[ $cap ], $user_id, ...$args ); |
9 | 601 |
} |
602 |
||
603 |
// Block capabilities map to their post equivalent. |
|
604 |
$block_caps = array( |
|
605 |
'edit_blocks', |
|
606 |
'edit_others_blocks', |
|
607 |
'publish_blocks', |
|
608 |
'read_private_blocks', |
|
609 |
'delete_blocks', |
|
610 |
'delete_private_blocks', |
|
611 |
'delete_published_blocks', |
|
612 |
'delete_others_blocks', |
|
613 |
'edit_private_blocks', |
|
614 |
'edit_published_blocks', |
|
615 |
); |
|
616 |
if ( in_array( $cap, $block_caps, true ) ) { |
|
617 |
$cap = str_replace( '_blocks', '_posts', $cap ); |
|
618 |
} |
|
619 |
||
620 |
// If no meta caps match, return the original cap. |
|
0 | 621 |
$caps[] = $cap; |
622 |
} |
|
623 |
||
5 | 624 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
625 |
* Filters a user's capabilities depending on specific context and/or privilege. |
5 | 626 |
* |
627 |
* @since 2.8.0 |
|
628 |
* |
|
9 | 629 |
* @param string[] $caps Array of the user's capabilities. |
630 |
* @param string $cap Capability name. |
|
631 |
* @param int $user_id The user ID. |
|
632 |
* @param array $args Adds the context to the cap. Typically the object ID. |
|
5 | 633 |
*/ |
634 |
return apply_filters( 'map_meta_cap', $caps, $cap, $user_id, $args ); |
|
0 | 635 |
} |
636 |
||
637 |
/** |
|
16 | 638 |
* Returns whether the current user has the specified capability. |
639 |
* |
|
640 |
* This function also accepts an ID of an object to check against if the capability is a meta capability. Meta |
|
641 |
* capabilities such as `edit_post` and `edit_user` are capabilities used by the `map_meta_cap()` function to |
|
642 |
* map to primitive capabilities that a user or role has, such as `edit_posts` and `edit_others_posts`. |
|
643 |
* |
|
644 |
* Example usage: |
|
645 |
* |
|
646 |
* current_user_can( 'edit_posts' ); |
|
647 |
* current_user_can( 'edit_post', $post->ID ); |
|
648 |
* current_user_can( 'edit_post_meta', $post->ID, $meta_key ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
649 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
650 |
* While checking against particular roles in place of a capability is supported |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
651 |
* in part, this practice is discouraged as it may produce unreliable results. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
652 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
653 |
* Note: Will always return true if the current user is a super admin, unless specifically denied. |
0 | 654 |
* |
655 |
* @since 2.0.0 |
|
16 | 656 |
* @since 5.3.0 Formalized the existing and already documented `...$args` parameter |
657 |
* by adding it to the function signature. |
|
0 | 658 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
659 |
* @see WP_User::has_cap() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
660 |
* @see map_meta_cap() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
661 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
662 |
* @param string $capability Capability name. |
16 | 663 |
* @param mixed ...$args Optional further parameters, typically starting with an object ID. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
664 |
* @return bool Whether the current user has the given capability. If `$capability` is a meta cap and `$object_id` is |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
665 |
* passed, whether the current user has the given meta capability for the given object. |
0 | 666 |
*/ |
16 | 667 |
function current_user_can( $capability, ...$args ) { |
0 | 668 |
$current_user = wp_get_current_user(); |
669 |
||
9 | 670 |
if ( empty( $current_user ) ) { |
0 | 671 |
return false; |
9 | 672 |
} |
0 | 673 |
|
16 | 674 |
return $current_user->has_cap( $capability, ...$args ); |
0 | 675 |
} |
676 |
||
677 |
/** |
|
16 | 678 |
* Returns whether the current user has the specified capability for a given site. |
679 |
* |
|
680 |
* This function also accepts an ID of an object to check against if the capability is a meta capability. Meta |
|
681 |
* capabilities such as `edit_post` and `edit_user` are capabilities used by the `map_meta_cap()` function to |
|
682 |
* map to primitive capabilities that a user or role has, such as `edit_posts` and `edit_others_posts`. |
|
683 |
* |
|
684 |
* Example usage: |
|
685 |
* |
|
686 |
* current_user_can_for_blog( $blog_id, 'edit_posts' ); |
|
687 |
* current_user_can_for_blog( $blog_id, 'edit_post', $post->ID ); |
|
688 |
* current_user_can_for_blog( $blog_id, 'edit_post_meta', $post->ID, $meta_key ); |
|
0 | 689 |
* |
690 |
* @since 3.0.0 |
|
16 | 691 |
* @since 5.3.0 Formalized the existing and already documented `...$args` parameter |
692 |
* by adding it to the function signature. |
|
0 | 693 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
694 |
* @param int $blog_id Site ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
695 |
* @param string $capability Capability name. |
16 | 696 |
* @param mixed ...$args Optional further parameters, typically starting with an object ID. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
697 |
* @return bool Whether the user has the given capability. |
0 | 698 |
*/ |
16 | 699 |
function current_user_can_for_blog( $blog_id, $capability, ...$args ) { |
5 | 700 |
$switched = is_multisite() ? switch_to_blog( $blog_id ) : false; |
0 | 701 |
|
702 |
$current_user = wp_get_current_user(); |
|
703 |
||
5 | 704 |
if ( empty( $current_user ) ) { |
705 |
if ( $switched ) { |
|
706 |
restore_current_blog(); |
|
707 |
} |
|
0 | 708 |
return false; |
5 | 709 |
} |
0 | 710 |
|
16 | 711 |
$can = $current_user->has_cap( $capability, ...$args ); |
0 | 712 |
|
5 | 713 |
if ( $switched ) { |
0 | 714 |
restore_current_blog(); |
5 | 715 |
} |
0 | 716 |
|
717 |
return $can; |
|
718 |
} |
|
719 |
||
720 |
/** |
|
16 | 721 |
* Returns whether the author of the supplied post has the specified capability. |
722 |
* |
|
723 |
* This function also accepts an ID of an object to check against if the capability is a meta capability. Meta |
|
724 |
* capabilities such as `edit_post` and `edit_user` are capabilities used by the `map_meta_cap()` function to |
|
725 |
* map to primitive capabilities that a user or role has, such as `edit_posts` and `edit_others_posts`. |
|
726 |
* |
|
727 |
* Example usage: |
|
728 |
* |
|
729 |
* author_can( $post, 'edit_posts' ); |
|
730 |
* author_can( $post, 'edit_post', $post->ID ); |
|
731 |
* author_can( $post, 'edit_post_meta', $post->ID, $meta_key ); |
|
0 | 732 |
* |
733 |
* @since 2.9.0 |
|
16 | 734 |
* @since 5.3.0 Formalized the existing and already documented `...$args` parameter |
735 |
* by adding it to the function signature. |
|
0 | 736 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
737 |
* @param int|WP_Post $post Post ID or post object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
738 |
* @param string $capability Capability name. |
16 | 739 |
* @param mixed ...$args Optional further parameters, typically starting with an object ID. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
740 |
* @return bool Whether the post author has the given capability. |
0 | 741 |
*/ |
16 | 742 |
function author_can( $post, $capability, ...$args ) { |
743 |
$post = get_post( $post ); |
|
744 |
if ( ! $post ) { |
|
0 | 745 |
return false; |
9 | 746 |
} |
0 | 747 |
|
748 |
$author = get_userdata( $post->post_author ); |
|
749 |
||
9 | 750 |
if ( ! $author ) { |
0 | 751 |
return false; |
9 | 752 |
} |
0 | 753 |
|
16 | 754 |
return $author->has_cap( $capability, ...$args ); |
0 | 755 |
} |
756 |
||
757 |
/** |
|
16 | 758 |
* Returns whether a particular user has the specified capability. |
759 |
* |
|
760 |
* This function also accepts an ID of an object to check against if the capability is a meta capability. Meta |
|
761 |
* capabilities such as `edit_post` and `edit_user` are capabilities used by the `map_meta_cap()` function to |
|
762 |
* map to primitive capabilities that a user or role has, such as `edit_posts` and `edit_others_posts`. |
|
763 |
* |
|
764 |
* Example usage: |
|
765 |
* |
|
766 |
* user_can( $user->ID, 'edit_posts' ); |
|
767 |
* user_can( $user->ID, 'edit_post', $post->ID ); |
|
768 |
* user_can( $user->ID, 'edit_post_meta', $post->ID, $meta_key ); |
|
0 | 769 |
* |
770 |
* @since 3.1.0 |
|
16 | 771 |
* @since 5.3.0 Formalized the existing and already documented `...$args` parameter |
772 |
* by adding it to the function signature. |
|
0 | 773 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
774 |
* @param int|WP_User $user User ID or object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
775 |
* @param string $capability Capability name. |
16 | 776 |
* @param mixed ...$args Optional further parameters, typically starting with an object ID. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
777 |
* @return bool Whether the user has the given capability. |
0 | 778 |
*/ |
16 | 779 |
function user_can( $user, $capability, ...$args ) { |
9 | 780 |
if ( ! is_object( $user ) ) { |
0 | 781 |
$user = get_userdata( $user ); |
9 | 782 |
} |
0 | 783 |
|
9 | 784 |
if ( ! $user || ! $user->exists() ) { |
0 | 785 |
return false; |
9 | 786 |
} |
0 | 787 |
|
16 | 788 |
return $user->has_cap( $capability, ...$args ); |
0 | 789 |
} |
790 |
||
791 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
792 |
* Retrieves the global WP_Roles instance and instantiates it if necessary. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
793 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
794 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
795 |
* |
16 | 796 |
* @global WP_Roles $wp_roles WordPress role management object. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
797 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
798 |
* @return WP_Roles WP_Roles global instance if not already instantiated. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
799 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
800 |
function wp_roles() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
801 |
global $wp_roles; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
802 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
803 |
if ( ! isset( $wp_roles ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
804 |
$wp_roles = new WP_Roles(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
805 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
806 |
return $wp_roles; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
807 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
808 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
809 |
/** |
0 | 810 |
* Retrieve role object. |
811 |
* |
|
812 |
* @since 2.0.0 |
|
813 |
* |
|
814 |
* @param string $role Role name. |
|
815 |
* @return WP_Role|null WP_Role object if found, null if the role does not exist. |
|
816 |
*/ |
|
817 |
function get_role( $role ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
818 |
return wp_roles()->get_role( $role ); |
0 | 819 |
} |
820 |
||
821 |
/** |
|
822 |
* Add role, if it does not exist. |
|
823 |
* |
|
824 |
* @since 2.0.0 |
|
825 |
* |
|
16 | 826 |
* @param string $role Role name. |
0 | 827 |
* @param string $display_name Display name for role. |
16 | 828 |
* @param bool[] $capabilities List of capabilities keyed by the capability name, |
829 |
* e.g. array( 'edit_posts' => true, 'delete_posts' => false ). |
|
0 | 830 |
* @return WP_Role|null WP_Role object if role is added, null if already exists. |
831 |
*/ |
|
832 |
function add_role( $role, $display_name, $capabilities = array() ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
833 |
if ( empty( $role ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
834 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
835 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
836 |
return wp_roles()->add_role( $role, $display_name, $capabilities ); |
0 | 837 |
} |
838 |
||
839 |
/** |
|
840 |
* Remove role, if it exists. |
|
841 |
* |
|
842 |
* @since 2.0.0 |
|
843 |
* |
|
844 |
* @param string $role Role name. |
|
845 |
*/ |
|
846 |
function remove_role( $role ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
847 |
wp_roles()->remove_role( $role ); |
0 | 848 |
} |
849 |
||
850 |
/** |
|
851 |
* Retrieve a list of super admins. |
|
852 |
* |
|
853 |
* @since 3.0.0 |
|
854 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
855 |
* @global array $super_admins |
0 | 856 |
* |
16 | 857 |
* @return string[] List of super admin logins. |
0 | 858 |
*/ |
859 |
function get_super_admins() { |
|
860 |
global $super_admins; |
|
861 |
||
9 | 862 |
if ( isset( $super_admins ) ) { |
0 | 863 |
return $super_admins; |
9 | 864 |
} else { |
865 |
return get_site_option( 'site_admins', array( 'admin' ) ); |
|
866 |
} |
|
0 | 867 |
} |
868 |
||
869 |
/** |
|
870 |
* Determine if user is a site admin. |
|
871 |
* |
|
872 |
* @since 3.0.0 |
|
873 |
* |
|
874 |
* @param int $user_id (Optional) The ID of a user. Defaults to the current user. |
|
875 |
* @return bool True if the user is a site admin. |
|
876 |
*/ |
|
877 |
function is_super_admin( $user_id = false ) { |
|
16 | 878 |
if ( ! $user_id || get_current_user_id() == $user_id ) { |
0 | 879 |
$user = wp_get_current_user(); |
9 | 880 |
} else { |
0 | 881 |
$user = get_userdata( $user_id ); |
9 | 882 |
} |
0 | 883 |
|
9 | 884 |
if ( ! $user || ! $user->exists() ) { |
0 | 885 |
return false; |
9 | 886 |
} |
0 | 887 |
|
888 |
if ( is_multisite() ) { |
|
889 |
$super_admins = get_super_admins(); |
|
16 | 890 |
if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins, true ) ) { |
0 | 891 |
return true; |
9 | 892 |
} |
0 | 893 |
} else { |
9 | 894 |
if ( $user->has_cap( 'delete_users' ) ) { |
0 | 895 |
return true; |
9 | 896 |
} |
0 | 897 |
} |
898 |
||
899 |
return false; |
|
900 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
901 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
902 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
903 |
* Grants Super Admin privileges. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
904 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
905 |
* @since 3.0.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
906 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
907 |
* @global array $super_admins |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
908 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
909 |
* @param int $user_id ID of the user to be granted Super Admin privileges. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
910 |
* @return bool True on success, false on failure. This can fail when the user is |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
911 |
* already a super admin or when the `$super_admins` global is defined. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
912 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
913 |
function grant_super_admin( $user_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
914 |
// If global super_admins override is defined, there is nothing to do here. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
915 |
if ( isset( $GLOBALS['super_admins'] ) || ! is_multisite() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
916 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
917 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
918 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
919 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
920 |
* Fires before the user is granted Super Admin privileges. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
921 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
922 |
* @since 3.0.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
923 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
924 |
* @param int $user_id ID of the user that is about to be granted Super Admin privileges. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
925 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
926 |
do_action( 'grant_super_admin', $user_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
927 |
|
16 | 928 |
// Directly fetch site_admins instead of using get_super_admins(). |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
929 |
$super_admins = get_site_option( 'site_admins', array( 'admin' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
930 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
931 |
$user = get_userdata( $user_id ); |
16 | 932 |
if ( $user && ! in_array( $user->user_login, $super_admins, true ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
933 |
$super_admins[] = $user->user_login; |
9 | 934 |
update_site_option( 'site_admins', $super_admins ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
935 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
936 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
937 |
* Fires after the user is granted Super Admin privileges. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
938 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
939 |
* @since 3.0.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
940 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
941 |
* @param int $user_id ID of the user that was granted Super Admin privileges. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
942 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
943 |
do_action( 'granted_super_admin', $user_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
944 |
return true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
945 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
946 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
947 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
948 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
949 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
950 |
* Revokes Super Admin privileges. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
951 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
952 |
* @since 3.0.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
953 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
954 |
* @global array $super_admins |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
955 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
956 |
* @param int $user_id ID of the user Super Admin privileges to be revoked from. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
957 |
* @return bool True on success, false on failure. This can fail when the user's email |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
958 |
* is the network admin email or when the `$super_admins` global is defined. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
959 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
960 |
function revoke_super_admin( $user_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
961 |
// If global super_admins override is defined, there is nothing to do here. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
962 |
if ( isset( $GLOBALS['super_admins'] ) || ! is_multisite() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
963 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
964 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
965 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
966 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
967 |
* Fires before the user's Super Admin privileges are revoked. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
968 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
969 |
* @since 3.0.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
970 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
971 |
* @param int $user_id ID of the user Super Admin privileges are being revoked from. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
972 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
973 |
do_action( 'revoke_super_admin', $user_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
974 |
|
16 | 975 |
// Directly fetch site_admins instead of using get_super_admins(). |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
976 |
$super_admins = get_site_option( 'site_admins', array( 'admin' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
977 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
978 |
$user = get_userdata( $user_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
979 |
if ( $user && 0 !== strcasecmp( $user->user_email, get_site_option( 'admin_email' ) ) ) { |
16 | 980 |
$key = array_search( $user->user_login, $super_admins, true ); |
981 |
if ( false !== $key ) { |
|
9 | 982 |
unset( $super_admins[ $key ] ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
983 |
update_site_option( 'site_admins', $super_admins ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
984 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
985 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
986 |
* Fires after the user's Super Admin privileges are revoked. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
987 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
988 |
* @since 3.0.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
989 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
990 |
* @param int $user_id ID of the user Super Admin privileges were revoked from. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
991 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
992 |
do_action( 'revoked_super_admin', $user_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
993 |
return true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
994 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
995 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
996 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
997 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
998 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
999 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1000 |
* Filters the user capabilities to grant the 'install_languages' capability as necessary. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1001 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1002 |
* A user must have at least one out of the 'update_core', 'install_plugins', and |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1003 |
* 'install_themes' capabilities to qualify for 'install_languages'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1004 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1005 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1006 |
* |
9 | 1007 |
* @param bool[] $allcaps An array of all the user's capabilities. |
1008 |
* @return bool[] Filtered array of the user's capabilities. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1009 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1010 |
function wp_maybe_grant_install_languages_cap( $allcaps ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1011 |
if ( ! empty( $allcaps['update_core'] ) || ! empty( $allcaps['install_plugins'] ) || ! empty( $allcaps['install_themes'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1012 |
$allcaps['install_languages'] = true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1013 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1014 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1015 |
return $allcaps; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1016 |
} |
9 | 1017 |
|
1018 |
/** |
|
1019 |
* Filters the user capabilities to grant the 'resume_plugins' and 'resume_themes' capabilities as necessary. |
|
1020 |
* |
|
1021 |
* @since 5.2.0 |
|
1022 |
* |
|
1023 |
* @param bool[] $allcaps An array of all the user's capabilities. |
|
1024 |
* @return bool[] Filtered array of the user's capabilities. |
|
1025 |
*/ |
|
1026 |
function wp_maybe_grant_resume_extensions_caps( $allcaps ) { |
|
1027 |
// Even in a multisite, regular administrators should be able to resume plugins. |
|
1028 |
if ( ! empty( $allcaps['activate_plugins'] ) ) { |
|
1029 |
$allcaps['resume_plugins'] = true; |
|
1030 |
} |
|
1031 |
||
1032 |
// Even in a multisite, regular administrators should be able to resume themes. |
|
1033 |
if ( ! empty( $allcaps['switch_themes'] ) ) { |
|
1034 |
$allcaps['resume_themes'] = true; |
|
1035 |
} |
|
1036 |
||
1037 |
return $allcaps; |
|
1038 |
} |
|
1039 |
||
1040 |
/** |
|
1041 |
* Filters the user capabilities to grant the 'view_site_health_checks' capabilities as necessary. |
|
1042 |
* |
|
1043 |
* @since 5.2.2 |
|
1044 |
* |
|
1045 |
* @param bool[] $allcaps An array of all the user's capabilities. |
|
1046 |
* @param string[] $caps Required primitive capabilities for the requested capability. |
|
1047 |
* @param array $args { |
|
1048 |
* Arguments that accompany the requested capability check. |
|
1049 |
* |
|
1050 |
* @type string $0 Requested capability. |
|
1051 |
* @type int $1 Concerned user ID. |
|
1052 |
* @type mixed ...$2 Optional second and further parameters, typically object ID. |
|
1053 |
* } |
|
1054 |
* @param WP_User $user The user object. |
|
1055 |
* @return bool[] Filtered array of the user's capabilities. |
|
1056 |
*/ |
|
1057 |
function wp_maybe_grant_site_health_caps( $allcaps, $caps, $args, $user ) { |
|
1058 |
if ( ! empty( $allcaps['install_plugins'] ) && ( ! is_multisite() || is_super_admin( $user->ID ) ) ) { |
|
1059 |
$allcaps['view_site_health_checks'] = true; |
|
1060 |
} |
|
1061 |
||
1062 |
return $allcaps; |
|
1063 |
} |
|
1064 |
||
1065 |
return; |
|
1066 |
||
1067 |
// Dummy gettext calls to get strings in the catalog. |
|
16 | 1068 |
/* translators: User role for administrators. */ |
9 | 1069 |
_x( 'Administrator', 'User role' ); |
16 | 1070 |
/* translators: User role for editors. */ |
9 | 1071 |
_x( 'Editor', 'User role' ); |
16 | 1072 |
/* translators: User role for authors. */ |
9 | 1073 |
_x( 'Author', 'User role' ); |
16 | 1074 |
/* translators: User role for contributors. */ |
9 | 1075 |
_x( 'Contributor', 'User role' ); |
16 | 1076 |
/* translators: User role for subscribers. */ |
9 | 1077 |
_x( 'Subscriber', 'User role' ); |