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