3 * Core User Role & Capabilities API
10 * Map meta capabilities to primitive capabilities.
12 * This does not actually compare whether the user ID has the actual capability,
13 * just what the capability or capabilities are. Meta capability list value can
14 * be 'delete_user', 'edit_user', 'remove_user', 'promote_user', 'delete_post',
15 * 'delete_page', 'edit_post', 'edit_page', 'read_post', or 'read_page'.
19 * @param string $cap Capability name.
20 * @param int $user_id User ID.
21 * @param int $object_id Optional. ID of the specific object to check against if `$cap` is a "meta" cap.
22 * "Meta" capabilities, e.g. 'edit_post', 'edit_user', etc., are capabilities used
23 * by map_meta_cap() to map to other "primitive" capabilities, e.g. 'edit_posts',
24 * 'edit_others_posts', etc. The parameter is accessed via func_get_args().
25 * @return array Actual capabilities for meta capability.
27 function map_meta_cap( $cap, $user_id ) {
28 $args = array_slice( func_get_args(), 2 );
33 $caps[] = 'remove_users';
37 $caps[] = 'promote_users';
41 // Allow user to edit itself
42 if ( 'edit_user' == $cap && isset( $args[0] ) && $user_id == $args[0] )
45 // In multisite the user must have manage_network_users caps. If editing a super admin, the user must be a super admin.
46 if ( is_multisite() && ( ( ! is_super_admin( $user_id ) && 'edit_user' === $cap && is_super_admin( $args[0] ) ) || ! user_can( $user_id, 'manage_network_users' ) ) ) {
47 $caps[] = 'do_not_allow';
49 $caps[] = 'edit_users'; // edit_user maps to edit_users.
54 $post = get_post( $args[0] );
56 $caps[] = 'do_not_allow';
60 if ( 'revision' == $post->post_type ) {
61 $post = get_post( $post->post_parent );
63 $caps[] = 'do_not_allow';
68 $post_type = get_post_type_object( $post->post_type );
70 /* translators: 1: post type, 2: capability name */
71 _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' );
72 $caps[] = 'edit_others_posts';
76 if ( ! $post_type->map_meta_cap ) {
77 $caps[] = $post_type->cap->$cap;
78 // Prior to 3.1 we would re-call map_meta_cap here.
79 if ( 'delete_post' == $cap )
80 $cap = $post_type->cap->$cap;
84 // If the post author is set and the user is the author...
85 if ( $post->post_author && $user_id == $post->post_author ) {
86 // If the post is published or scheduled...
87 if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) {
88 $caps[] = $post_type->cap->delete_published_posts;
89 } elseif ( 'trash' == $post->post_status ) {
90 $status = get_post_meta( $post->ID, '_wp_trash_meta_status', true );
91 if ( in_array( $status, array( 'publish', 'future' ), true ) ) {
92 $caps[] = $post_type->cap->delete_published_posts;
94 $caps[] = $post_type->cap->delete_posts;
97 // If the post is draft...
98 $caps[] = $post_type->cap->delete_posts;
101 // The user is trying to edit someone else's post.
102 $caps[] = $post_type->cap->delete_others_posts;
103 // The post is published or scheduled, extra cap required.
104 if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) {
105 $caps[] = $post_type->cap->delete_published_posts;
106 } elseif ( 'private' == $post->post_status ) {
107 $caps[] = $post_type->cap->delete_private_posts;
111 // edit_post breaks down to edit_posts, edit_published_posts, or
115 $post = get_post( $args[0] );
117 $caps[] = 'do_not_allow';
121 if ( 'revision' == $post->post_type ) {
122 $post = get_post( $post->post_parent );
124 $caps[] = 'do_not_allow';
129 $post_type = get_post_type_object( $post->post_type );
130 if ( ! $post_type ) {
131 /* translators: 1: post type, 2: capability name */
132 _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' );
133 $caps[] = 'edit_others_posts';
137 if ( ! $post_type->map_meta_cap ) {
138 $caps[] = $post_type->cap->$cap;
139 // Prior to 3.1 we would re-call map_meta_cap here.
140 if ( 'edit_post' == $cap )
141 $cap = $post_type->cap->$cap;
145 // If the post author is set and the user is the author...
146 if ( $post->post_author && $user_id == $post->post_author ) {
147 // If the post is published or scheduled...
148 if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) {
149 $caps[] = $post_type->cap->edit_published_posts;
150 } elseif ( 'trash' == $post->post_status ) {
151 $status = get_post_meta( $post->ID, '_wp_trash_meta_status', true );
152 if ( in_array( $status, array( 'publish', 'future' ), true ) ) {
153 $caps[] = $post_type->cap->edit_published_posts;
155 $caps[] = $post_type->cap->edit_posts;
158 // If the post is draft...
159 $caps[] = $post_type->cap->edit_posts;
162 // The user is trying to edit someone else's post.
163 $caps[] = $post_type->cap->edit_others_posts;
164 // The post is published or scheduled, extra cap required.
165 if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) {
166 $caps[] = $post_type->cap->edit_published_posts;
167 } elseif ( 'private' == $post->post_status ) {
168 $caps[] = $post_type->cap->edit_private_posts;
174 $post = get_post( $args[0] );
176 $caps[] = 'do_not_allow';
180 if ( 'revision' == $post->post_type ) {
181 $post = get_post( $post->post_parent );
183 $caps[] = 'do_not_allow';
188 $post_type = get_post_type_object( $post->post_type );
189 if ( ! $post_type ) {
190 /* translators: 1: post type, 2: capability name */
191 _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' );
192 $caps[] = 'edit_others_posts';
196 if ( ! $post_type->map_meta_cap ) {
197 $caps[] = $post_type->cap->$cap;
198 // Prior to 3.1 we would re-call map_meta_cap here.
199 if ( 'read_post' == $cap )
200 $cap = $post_type->cap->$cap;
204 $status_obj = get_post_status_object( $post->post_status );
205 if ( $status_obj->public ) {
206 $caps[] = $post_type->cap->read;
210 if ( $post->post_author && $user_id == $post->post_author ) {
211 $caps[] = $post_type->cap->read;
212 } elseif ( $status_obj->private ) {
213 $caps[] = $post_type->cap->read_private_posts;
215 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
219 $post = get_post( $args[0] );
221 $caps[] = 'do_not_allow';
225 $post_type = get_post_type_object( $post->post_type );
226 if ( ! $post_type ) {
227 /* translators: 1: post type, 2: capability name */
228 _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' );
229 $caps[] = 'edit_others_posts';
233 $caps[] = $post_type->cap->publish_posts;
235 case 'edit_post_meta':
236 case 'delete_post_meta':
237 case 'add_post_meta':
238 $post = get_post( $args[0] );
240 $caps[] = 'do_not_allow';
244 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
246 $meta_key = isset( $args[ 1 ] ) ? $args[ 1 ] : false;
248 if ( $meta_key && has_filter( "auth_post_meta_{$meta_key}" ) ) {
250 * Filter whether the user is allowed to add post meta to a post.
252 * The dynamic portion of the hook name, `$meta_key`, refers to the
253 * meta key passed to {@see map_meta_cap()}.
257 * @param bool $allowed Whether the user can add the post meta. Default false.
258 * @param string $meta_key The meta key.
259 * @param int $post_id Post ID.
260 * @param int $user_id User ID.
261 * @param string $cap Capability name.
262 * @param array $caps User capabilities.
264 $allowed = apply_filters( "auth_post_meta_{$meta_key}", false, $meta_key, $post->ID, $user_id, $cap, $caps );
267 } elseif ( $meta_key && is_protected_meta( $meta_key, 'post' ) ) {
272 $comment = get_comment( $args[0] );
274 $caps[] = 'do_not_allow';
278 $post = get_post( $comment->comment_post_ID );
281 * If the post doesn't exist, we have an orphaned comment.
282 * Fall back to the edit_posts capability, instead.
285 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
287 $caps = map_meta_cap( 'edit_posts', $user_id );
290 case 'unfiltered_upload':
291 if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) ) )
294 $caps[] = 'do_not_allow';
296 case 'unfiltered_html' :
297 // Disallow unfiltered_html for all users, even admins and super admins.
298 if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML )
299 $caps[] = 'do_not_allow';
300 elseif ( is_multisite() && ! is_super_admin( $user_id ) )
301 $caps[] = 'do_not_allow';
308 // Disallow the file editors.
309 if ( defined( 'DISALLOW_FILE_EDIT' ) && DISALLOW_FILE_EDIT )
310 $caps[] = 'do_not_allow';
311 elseif ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )
312 $caps[] = 'do_not_allow';
313 elseif ( is_multisite() && ! is_super_admin( $user_id ) )
314 $caps[] = 'do_not_allow';
318 case 'update_plugins':
319 case 'delete_plugins':
320 case 'install_plugins':
321 case 'upload_plugins':
322 case 'update_themes':
323 case 'delete_themes':
324 case 'install_themes':
325 case 'upload_themes':
327 // Disallow anything that creates, deletes, or updates core, plugin, or theme files.
328 // Files in uploads are excepted.
329 if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS ) {
330 $caps[] = 'do_not_allow';
331 } elseif ( is_multisite() && ! is_super_admin( $user_id ) ) {
332 $caps[] = 'do_not_allow';
333 } elseif ( 'upload_themes' === $cap ) {
334 $caps[] = 'install_themes';
335 } elseif ( 'upload_plugins' === $cap ) {
336 $caps[] = 'install_plugins';
341 case 'activate_plugins':
343 if ( is_multisite() ) {
344 // update_, install_, and delete_ are handled above with is_super_admin().
345 $menu_perms = get_site_option( 'menu_items', array() );
346 if ( empty( $menu_perms['plugins'] ) )
347 $caps[] = 'manage_network_plugins';
352 // If multisite only super admins can delete users.
353 if ( is_multisite() && ! is_super_admin( $user_id ) )
354 $caps[] = 'do_not_allow';
356 $caps[] = 'delete_users'; // delete_user maps to delete_users.
359 if ( !is_multisite() )
361 elseif ( is_super_admin( $user_id ) || get_site_option( 'add_new_users' ) )
364 $caps[] = 'do_not_allow';
366 case 'manage_links' :
367 if ( get_option( 'link_manager_enabled' ) )
370 $caps[] = 'do_not_allow';
373 $caps[] = 'edit_theme_options';
376 $caps[] = 'manage_options';
379 // Handle meta capabilities for custom post types.
380 $post_type_meta_caps = _post_type_meta_capabilities();
381 if ( isset( $post_type_meta_caps[ $cap ] ) ) {
382 $args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args );
383 return call_user_func_array( 'map_meta_cap', $args );
386 // If no meta caps match, return the original cap.
391 * Filter a user's capabilities depending on specific context and/or privilege.
395 * @param array $caps Returns the user's actual capabilities.
396 * @param string $cap Capability name.
397 * @param int $user_id The user ID.
398 * @param array $args Adds the context to the cap. Typically the object ID.
400 return apply_filters( 'map_meta_cap', $caps, $cap, $user_id, $args );
404 * Whether the current user has a specific capability.
406 * While checking against particular roles in place of a capability is supported
407 * in part, this practice is discouraged as it may produce unreliable results.
411 * @see WP_User::has_cap()
412 * @see map_meta_cap()
414 * @param string $capability Capability name.
415 * @param int $object_id Optional. ID of the specific object to check against if `$capability` is a "meta" cap.
416 * "Meta" capabilities, e.g. 'edit_post', 'edit_user', etc., are capabilities used
417 * by map_meta_cap() to map to other "primitive" capabilities, e.g. 'edit_posts',
418 * 'edit_others_posts', etc. Accessed via func_get_args() and passed to WP_User::has_cap(),
419 * then map_meta_cap().
420 * @return bool Whether the current user has the given capability. If `$capability` is a meta cap and `$object_id` is
421 * passed, whether the current user has the given meta capability for the given object.
423 function current_user_can( $capability ) {
424 $current_user = wp_get_current_user();
426 if ( empty( $current_user ) )
429 $args = array_slice( func_get_args(), 1 );
430 $args = array_merge( array( $capability ), $args );
432 return call_user_func_array( array( $current_user, 'has_cap' ), $args );
436 * Whether current user has a capability or role for a given blog.
440 * @param int $blog_id Blog ID
441 * @param string $capability Capability or role name.
444 function current_user_can_for_blog( $blog_id, $capability ) {
445 $switched = is_multisite() ? switch_to_blog( $blog_id ) : false;
447 $current_user = wp_get_current_user();
449 if ( empty( $current_user ) ) {
451 restore_current_blog();
456 $args = array_slice( func_get_args(), 2 );
457 $args = array_merge( array( $capability ), $args );
459 $can = call_user_func_array( array( $current_user, 'has_cap' ), $args );
462 restore_current_blog();
469 * Whether author of supplied post has capability or role.
473 * @param int|object $post Post ID or post object.
474 * @param string $capability Capability or role name.
477 function author_can( $post, $capability ) {
478 if ( !$post = get_post($post) )
481 $author = get_userdata( $post->post_author );
486 $args = array_slice( func_get_args(), 2 );
487 $args = array_merge( array( $capability ), $args );
489 return call_user_func_array( array( $author, 'has_cap' ), $args );
493 * Whether a particular user has capability or role.
497 * @param int|object $user User ID or object.
498 * @param string $capability Capability or role name.
501 function user_can( $user, $capability ) {
502 if ( ! is_object( $user ) )
503 $user = get_userdata( $user );
505 if ( ! $user || ! $user->exists() )
508 $args = array_slice( func_get_args(), 2 );
509 $args = array_merge( array( $capability ), $args );
511 return call_user_func_array( array( $user, 'has_cap' ), $args );
515 * Retrieves the global WP_Roles instance and instantiates it if necessary.
519 * @global WP_Roles $wp_roles WP_Roles global instance.
521 * @return WP_Roles WP_Roles global instance if not already instantiated.
523 function wp_roles() {
526 if ( ! isset( $wp_roles ) ) {
527 $wp_roles = new WP_Roles();
533 * Retrieve role object.
537 * @param string $role Role name.
538 * @return WP_Role|null WP_Role object if found, null if the role does not exist.
540 function get_role( $role ) {
541 return wp_roles()->get_role( $role );
545 * Add role, if it does not exist.
549 * @param string $role Role name.
550 * @param string $display_name Display name for role.
551 * @param array $capabilities List of capabilities, e.g. array( 'edit_posts' => true, 'delete_posts' => false );
552 * @return WP_Role|null WP_Role object if role is added, null if already exists.
554 function add_role( $role, $display_name, $capabilities = array() ) {
555 if ( empty( $role ) ) {
558 return wp_roles()->add_role( $role, $display_name, $capabilities );
562 * Remove role, if it exists.
566 * @param string $role Role name.
568 function remove_role( $role ) {
569 wp_roles()->remove_role( $role );
573 * Retrieve a list of super admins.
577 * @global array $super_admins
579 * @return array List of super admin logins
581 function get_super_admins() {
582 global $super_admins;
584 if ( isset($super_admins) )
585 return $super_admins;
587 return get_site_option( 'site_admins', array('admin') );
591 * Determine if user is a site admin.
595 * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
596 * @return bool True if the user is a site admin.
598 function is_super_admin( $user_id = false ) {
599 if ( ! $user_id || $user_id == get_current_user_id() )
600 $user = wp_get_current_user();
602 $user = get_userdata( $user_id );
604 if ( ! $user || ! $user->exists() )
607 if ( is_multisite() ) {
608 $super_admins = get_super_admins();
609 if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) )
612 if ( $user->has_cap('delete_users') )