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 * @global array $post_type_meta_caps Used to get post type meta capabilities.
21 * @param string $cap Capability name.
22 * @param int $user_id User ID.
23 * @param int $object_id Optional. ID of the specific object to check against if `$cap` is a "meta" cap.
24 * "Meta" capabilities, e.g. 'edit_post', 'edit_user', etc., are capabilities used
25 * by map_meta_cap() to map to other "primitive" capabilities, e.g. 'edit_posts',
26 * 'edit_others_posts', etc. The parameter is accessed via func_get_args().
27 * @return array Actual capabilities for meta capability.
29 function map_meta_cap( $cap, $user_id ) {
30 $args = array_slice( func_get_args(), 2 );
35 $caps[] = 'remove_users';
39 $caps[] = 'promote_users';
43 // Allow user to edit itself
44 if ( 'edit_user' == $cap && isset( $args[0] ) && $user_id == $args[0] )
47 // In multisite the user must have manage_network_users caps. If editing a super admin, the user must be a super admin.
48 if ( is_multisite() && ( ( ! is_super_admin( $user_id ) && 'edit_user' === $cap && is_super_admin( $args[0] ) ) || ! user_can( $user_id, 'manage_network_users' ) ) ) {
49 $caps[] = 'do_not_allow';
51 $caps[] = 'edit_users'; // edit_user maps to edit_users.
56 $post = get_post( $args[0] );
58 $caps[] = 'do_not_allow';
62 if ( 'revision' == $post->post_type ) {
63 $post = get_post( $post->post_parent );
65 $caps[] = 'do_not_allow';
70 $post_type = get_post_type_object( $post->post_type );
72 /* translators: 1: post type, 2: capability name */
73 _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' );
74 $caps[] = 'edit_others_posts';
78 if ( ! $post_type->map_meta_cap ) {
79 $caps[] = $post_type->cap->$cap;
80 // Prior to 3.1 we would re-call map_meta_cap here.
81 if ( 'delete_post' == $cap )
82 $cap = $post_type->cap->$cap;
86 // If the post author is set and the user is the author...
87 if ( $post->post_author && $user_id == $post->post_author ) {
88 // If the post is published or scheduled...
89 if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) {
90 $caps[] = $post_type->cap->delete_published_posts;
91 } elseif ( 'trash' == $post->post_status ) {
92 $status = get_post_meta( $post->ID, '_wp_trash_meta_status', true );
93 if ( in_array( $status, array( 'publish', 'future' ), true ) ) {
94 $caps[] = $post_type->cap->delete_published_posts;
96 $caps[] = $post_type->cap->delete_posts;
99 // If the post is draft...
100 $caps[] = $post_type->cap->delete_posts;
103 // The user is trying to edit someone else's post.
104 $caps[] = $post_type->cap->delete_others_posts;
105 // The post is published or scheduled, extra cap required.
106 if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) {
107 $caps[] = $post_type->cap->delete_published_posts;
108 } elseif ( 'private' == $post->post_status ) {
109 $caps[] = $post_type->cap->delete_private_posts;
113 // edit_post breaks down to edit_posts, edit_published_posts, or
117 $post = get_post( $args[0] );
119 $caps[] = 'do_not_allow';
123 if ( 'revision' == $post->post_type ) {
124 $post = get_post( $post->post_parent );
126 $caps[] = 'do_not_allow';
131 $post_type = get_post_type_object( $post->post_type );
132 if ( ! $post_type ) {
133 /* translators: 1: post type, 2: capability name */
134 _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' );
135 $caps[] = 'edit_others_posts';
139 if ( ! $post_type->map_meta_cap ) {
140 $caps[] = $post_type->cap->$cap;
141 // Prior to 3.1 we would re-call map_meta_cap here.
142 if ( 'edit_post' == $cap )
143 $cap = $post_type->cap->$cap;
147 // If the post author is set and the user is the author...
148 if ( $post->post_author && $user_id == $post->post_author ) {
149 // If the post is published or scheduled...
150 if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) {
151 $caps[] = $post_type->cap->edit_published_posts;
152 } elseif ( 'trash' == $post->post_status ) {
153 $status = get_post_meta( $post->ID, '_wp_trash_meta_status', true );
154 if ( in_array( $status, array( 'publish', 'future' ), true ) ) {
155 $caps[] = $post_type->cap->edit_published_posts;
157 $caps[] = $post_type->cap->edit_posts;
160 // If the post is draft...
161 $caps[] = $post_type->cap->edit_posts;
164 // The user is trying to edit someone else's post.
165 $caps[] = $post_type->cap->edit_others_posts;
166 // The post is published or scheduled, extra cap required.
167 if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) {
168 $caps[] = $post_type->cap->edit_published_posts;
169 } elseif ( 'private' == $post->post_status ) {
170 $caps[] = $post_type->cap->edit_private_posts;
176 $post = get_post( $args[0] );
178 $caps[] = 'do_not_allow';
182 if ( 'revision' == $post->post_type ) {
183 $post = get_post( $post->post_parent );
185 $caps[] = 'do_not_allow';
190 $post_type = get_post_type_object( $post->post_type );
191 if ( ! $post_type ) {
192 /* translators: 1: post type, 2: capability name */
193 _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' );
194 $caps[] = 'edit_others_posts';
198 if ( ! $post_type->map_meta_cap ) {
199 $caps[] = $post_type->cap->$cap;
200 // Prior to 3.1 we would re-call map_meta_cap here.
201 if ( 'read_post' == $cap )
202 $cap = $post_type->cap->$cap;
206 $status_obj = get_post_status_object( $post->post_status );
207 if ( $status_obj->public ) {
208 $caps[] = $post_type->cap->read;
212 if ( $post->post_author && $user_id == $post->post_author ) {
213 $caps[] = $post_type->cap->read;
214 } elseif ( $status_obj->private ) {
215 $caps[] = $post_type->cap->read_private_posts;
217 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
221 $post = get_post( $args[0] );
223 $caps[] = 'do_not_allow';
227 $post_type = get_post_type_object( $post->post_type );
228 if ( ! $post_type ) {
229 /* translators: 1: post type, 2: capability name */
230 _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' );
231 $caps[] = 'edit_others_posts';
235 $caps[] = $post_type->cap->publish_posts;
237 case 'edit_post_meta':
238 case 'delete_post_meta':
239 case 'add_post_meta':
240 $post = get_post( $args[0] );
242 $caps[] = 'do_not_allow';
246 $post_type = get_post_type( $post );
248 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
250 $meta_key = isset( $args[ 1 ] ) ? $args[ 1 ] : false;
252 if ( $meta_key && ( has_filter( "auth_post_meta_{$meta_key}" ) || has_filter( "auth_post_{$post_type}_meta_{$meta_key}" ) ) ) {
254 * Filters whether the user is allowed to add post meta to a post.
256 * The dynamic portion of the hook name, `$meta_key`, refers to the
257 * meta key passed to map_meta_cap().
261 * @param bool $allowed Whether the user can add the post meta. Default false.
262 * @param string $meta_key The meta key.
263 * @param int $post_id Post ID.
264 * @param int $user_id User ID.
265 * @param string $cap Capability name.
266 * @param array $caps User capabilities.
268 $allowed = apply_filters( "auth_post_meta_{$meta_key}", false, $meta_key, $post->ID, $user_id, $cap, $caps );
271 * Filters whether the user is allowed to add post meta to a post of a given type.
273 * The dynamic portions of the hook name, `$meta_key` and `$post_type`,
274 * refer to the meta key passed to map_meta_cap() and the post type, respectively.
278 * @param bool $allowed Whether the user can add the post meta. Default false.
279 * @param string $meta_key The meta key.
280 * @param int $post_id Post ID.
281 * @param int $user_id User ID.
282 * @param string $cap Capability name.
283 * @param array $caps User capabilities.
285 $allowed = apply_filters( "auth_post_{$post_type}_meta_{$meta_key}", $allowed, $meta_key, $post->ID, $user_id, $cap, $caps );
289 } elseif ( $meta_key && is_protected_meta( $meta_key, 'post' ) ) {
294 $comment = get_comment( $args[0] );
296 $caps[] = 'do_not_allow';
300 $post = get_post( $comment->comment_post_ID );
303 * If the post doesn't exist, we have an orphaned comment.
304 * Fall back to the edit_posts capability, instead.
307 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
309 $caps = map_meta_cap( 'edit_posts', $user_id );
312 case 'unfiltered_upload':
313 if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) ) )
316 $caps[] = 'do_not_allow';
318 case 'unfiltered_html' :
319 // Disallow unfiltered_html for all users, even admins and super admins.
320 if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML )
321 $caps[] = 'do_not_allow';
322 elseif ( is_multisite() && ! is_super_admin( $user_id ) )
323 $caps[] = 'do_not_allow';
330 // Disallow the file editors.
331 if ( defined( 'DISALLOW_FILE_EDIT' ) && DISALLOW_FILE_EDIT )
332 $caps[] = 'do_not_allow';
333 elseif ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )
334 $caps[] = 'do_not_allow';
335 elseif ( is_multisite() && ! is_super_admin( $user_id ) )
336 $caps[] = 'do_not_allow';
340 case 'update_plugins':
341 case 'delete_plugins':
342 case 'install_plugins':
343 case 'upload_plugins':
344 case 'update_themes':
345 case 'delete_themes':
346 case 'install_themes':
347 case 'upload_themes':
349 // Disallow anything that creates, deletes, or updates core, plugin, or theme files.
350 // Files in uploads are excepted.
351 if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS ) {
352 $caps[] = 'do_not_allow';
353 } elseif ( is_multisite() && ! is_super_admin( $user_id ) ) {
354 $caps[] = 'do_not_allow';
355 } elseif ( 'upload_themes' === $cap ) {
356 $caps[] = 'install_themes';
357 } elseif ( 'upload_plugins' === $cap ) {
358 $caps[] = 'install_plugins';
363 case 'activate_plugins':
365 if ( is_multisite() ) {
366 // update_, install_, and delete_ are handled above with is_super_admin().
367 $menu_perms = get_site_option( 'menu_items', array() );
368 if ( empty( $menu_perms['plugins'] ) )
369 $caps[] = 'manage_network_plugins';
374 // If multisite only super admins can delete users.
375 if ( is_multisite() && ! is_super_admin( $user_id ) )
376 $caps[] = 'do_not_allow';
378 $caps[] = 'delete_users'; // delete_user maps to delete_users.
381 if ( !is_multisite() )
383 elseif ( is_super_admin( $user_id ) || get_site_option( 'add_new_users' ) )
386 $caps[] = 'do_not_allow';
388 case 'manage_links' :
389 if ( get_option( 'link_manager_enabled' ) )
392 $caps[] = 'do_not_allow';
395 $caps[] = 'edit_theme_options';
398 $caps[] = 'manage_options';
401 // Handle meta capabilities for custom post types.
402 global $post_type_meta_caps;
403 if ( isset( $post_type_meta_caps[ $cap ] ) ) {
404 $args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args );
405 return call_user_func_array( 'map_meta_cap', $args );
408 // If no meta caps match, return the original cap.
413 * Filters a user's capabilities depending on specific context and/or privilege.
417 * @param array $caps Returns the user's actual capabilities.
418 * @param string $cap Capability name.
419 * @param int $user_id The user ID.
420 * @param array $args Adds the context to the cap. Typically the object ID.
422 return apply_filters( 'map_meta_cap', $caps, $cap, $user_id, $args );
426 * Whether the current user has a specific capability.
428 * While checking against particular roles in place of a capability is supported
429 * in part, this practice is discouraged as it may produce unreliable results.
431 * Note: Will always return true if the current user is a super admin, unless specifically denied.
435 * @see WP_User::has_cap()
436 * @see map_meta_cap()
438 * @param string $capability Capability name.
439 * @param int $object_id Optional. ID of the specific object to check against if `$capability` is a "meta" cap.
440 * "Meta" capabilities, e.g. 'edit_post', 'edit_user', etc., are capabilities used
441 * by map_meta_cap() to map to other "primitive" capabilities, e.g. 'edit_posts',
442 * 'edit_others_posts', etc. Accessed via func_get_args() and passed to WP_User::has_cap(),
443 * then map_meta_cap().
444 * @return bool Whether the current user has the given capability. If `$capability` is a meta cap and `$object_id` is
445 * passed, whether the current user has the given meta capability for the given object.
447 function current_user_can( $capability ) {
448 $current_user = wp_get_current_user();
450 if ( empty( $current_user ) )
453 $args = array_slice( func_get_args(), 1 );
454 $args = array_merge( array( $capability ), $args );
456 return call_user_func_array( array( $current_user, 'has_cap' ), $args );
460 * Whether current user has a capability or role for a given site.
464 * @param int $blog_id Site ID.
465 * @param string $capability Capability or role name.
468 function current_user_can_for_blog( $blog_id, $capability ) {
469 $switched = is_multisite() ? switch_to_blog( $blog_id ) : false;
471 $current_user = wp_get_current_user();
473 if ( empty( $current_user ) ) {
475 restore_current_blog();
480 $args = array_slice( func_get_args(), 2 );
481 $args = array_merge( array( $capability ), $args );
483 $can = call_user_func_array( array( $current_user, 'has_cap' ), $args );
486 restore_current_blog();
493 * Whether author of supplied post has capability or role.
497 * @param int|object $post Post ID or post object.
498 * @param string $capability Capability or role name.
501 function author_can( $post, $capability ) {
502 if ( !$post = get_post($post) )
505 $author = get_userdata( $post->post_author );
510 $args = array_slice( func_get_args(), 2 );
511 $args = array_merge( array( $capability ), $args );
513 return call_user_func_array( array( $author, 'has_cap' ), $args );
517 * Whether a particular user has capability or role.
521 * @param int|object $user User ID or object.
522 * @param string $capability Capability or role name.
525 function user_can( $user, $capability ) {
526 if ( ! is_object( $user ) )
527 $user = get_userdata( $user );
529 if ( ! $user || ! $user->exists() )
532 $args = array_slice( func_get_args(), 2 );
533 $args = array_merge( array( $capability ), $args );
535 return call_user_func_array( array( $user, 'has_cap' ), $args );
539 * Retrieves the global WP_Roles instance and instantiates it if necessary.
543 * @global WP_Roles $wp_roles WP_Roles global instance.
545 * @return WP_Roles WP_Roles global instance if not already instantiated.
547 function wp_roles() {
550 if ( ! isset( $wp_roles ) ) {
551 $wp_roles = new WP_Roles();
557 * Retrieve role object.
561 * @param string $role Role name.
562 * @return WP_Role|null WP_Role object if found, null if the role does not exist.
564 function get_role( $role ) {
565 return wp_roles()->get_role( $role );
569 * Add role, if it does not exist.
573 * @param string $role Role name.
574 * @param string $display_name Display name for role.
575 * @param array $capabilities List of capabilities, e.g. array( 'edit_posts' => true, 'delete_posts' => false );
576 * @return WP_Role|null WP_Role object if role is added, null if already exists.
578 function add_role( $role, $display_name, $capabilities = array() ) {
579 if ( empty( $role ) ) {
582 return wp_roles()->add_role( $role, $display_name, $capabilities );
586 * Remove role, if it exists.
590 * @param string $role Role name.
592 function remove_role( $role ) {
593 wp_roles()->remove_role( $role );
597 * Retrieve a list of super admins.
601 * @global array $super_admins
603 * @return array List of super admin logins
605 function get_super_admins() {
606 global $super_admins;
608 if ( isset($super_admins) )
609 return $super_admins;
611 return get_site_option( 'site_admins', array('admin') );
615 * Determine if user is a site admin.
619 * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
620 * @return bool True if the user is a site admin.
622 function is_super_admin( $user_id = false ) {
623 if ( ! $user_id || $user_id == get_current_user_id() )
624 $user = wp_get_current_user();
626 $user = get_userdata( $user_id );
628 if ( ! $user || ! $user->exists() )
631 if ( is_multisite() ) {
632 $super_admins = get_super_admins();
633 if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) )
636 if ( $user->has_cap('delete_users') )
644 * Grants Super Admin privileges.
648 * @global array $super_admins
650 * @param int $user_id ID of the user to be granted Super Admin privileges.
651 * @return bool True on success, false on failure. This can fail when the user is
652 * already a super admin or when the `$super_admins` global is defined.
654 function grant_super_admin( $user_id ) {
655 // If global super_admins override is defined, there is nothing to do here.
656 if ( isset( $GLOBALS['super_admins'] ) || ! is_multisite() ) {
661 * Fires before the user is granted Super Admin privileges.
665 * @param int $user_id ID of the user that is about to be granted Super Admin privileges.
667 do_action( 'grant_super_admin', $user_id );
669 // Directly fetch site_admins instead of using get_super_admins()
670 $super_admins = get_site_option( 'site_admins', array( 'admin' ) );
672 $user = get_userdata( $user_id );
673 if ( $user && ! in_array( $user->user_login, $super_admins ) ) {
674 $super_admins[] = $user->user_login;
675 update_site_option( 'site_admins' , $super_admins );
678 * Fires after the user is granted Super Admin privileges.
682 * @param int $user_id ID of the user that was granted Super Admin privileges.
684 do_action( 'granted_super_admin', $user_id );
691 * Revokes Super Admin privileges.
695 * @global array $super_admins
697 * @param int $user_id ID of the user Super Admin privileges to be revoked from.
698 * @return bool True on success, false on failure. This can fail when the user's email
699 * is the network admin email or when the `$super_admins` global is defined.
701 function revoke_super_admin( $user_id ) {
702 // If global super_admins override is defined, there is nothing to do here.
703 if ( isset( $GLOBALS['super_admins'] ) || ! is_multisite() ) {
708 * Fires before the user's Super Admin privileges are revoked.
712 * @param int $user_id ID of the user Super Admin privileges are being revoked from.
714 do_action( 'revoke_super_admin', $user_id );
716 // Directly fetch site_admins instead of using get_super_admins()
717 $super_admins = get_site_option( 'site_admins', array( 'admin' ) );
719 $user = get_userdata( $user_id );
720 if ( $user && 0 !== strcasecmp( $user->user_email, get_site_option( 'admin_email' ) ) ) {
721 if ( false !== ( $key = array_search( $user->user_login, $super_admins ) ) ) {
722 unset( $super_admins[$key] );
723 update_site_option( 'site_admins', $super_admins );
726 * Fires after the user's Super Admin privileges are revoked.
730 * @param int $user_id ID of the user Super Admin privileges were revoked from.
732 do_action( 'revoked_super_admin', $user_id );