[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> capabilities.php (source)

   1  <?php
   2  /**
   3   * Core User Role & Capabilities API
   4   *
   5   * @package WordPress
   6   * @subpackage Users
   7   */
   8  
   9  /**
  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 );
  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
  28   * @since 5.3.0 Formalized the existing and already documented `...$args` parameter
  29   *              by adding it to the function signature.
  30   *
  31   * @global array $post_type_meta_caps Used to get post type meta capabilities.
  32   *
  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.
  37   */
  38  function map_meta_cap( $cap, $user_id, ...$args ) {
  39      $caps = array();
  40  
  41      switch ( $cap ) {
  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';
  53              break;
  54          case 'edit_user':
  55          case 'edit_users':
  56              // Allow user to edit themselves.
  57              if ( 'edit_user' == $cap && isset( $args[0] ) && $user_id == $args[0] ) {
  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  
  76              if ( 'revision' == $post->post_type ) {
  77                  $caps[] = 'do_not_allow';
  78                  break;
  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              }
  85  
  86              $post_type = get_post_type_object( $post->post_type );
  87              if ( ! $post_type ) {
  88                  /* translators: 1: Post type, 2: Capability name. */
  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.
  97                  if ( 'delete_post' == $cap ) {
  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;
 108                  } elseif ( 'trash' == $post->post_status ) {
 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;
 125                  } elseif ( 'private' == $post->post_status ) {
 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  
 138              break;
 139          // edit_post breaks down to edit_posts, edit_published_posts, or
 140          // edit_others_posts.
 141          case 'edit_post':
 142          case 'edit_page':
 143              $post = get_post( $args[0] );
 144              if ( ! $post ) {
 145                  $caps[] = 'do_not_allow';
 146                  break;
 147              }
 148  
 149              if ( 'revision' == $post->post_type ) {
 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 ) {
 159                  /* translators: 1: Post type, 2: Capability name. */
 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              }
 164  
 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.
 168                  if ( 'edit_post' == $cap ) {
 169                      $cap = $post_type->cap->$cap;
 170                  }
 171                  break;
 172              }
 173  
 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;
 179                  } elseif ( 'trash' == $post->post_status ) {
 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                      }
 186                  } else {
 187                      // If the post is draft...
 188                      $caps[] = $post_type->cap->edit_posts;
 189                  }
 190              } else {
 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;
 196                  } elseif ( 'private' == $post->post_status ) {
 197                      $caps[] = $post_type->cap->edit_private_posts;
 198                  }
 199              }
 200  
 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              }
 208  
 209              break;
 210          case 'read_post':
 211          case 'read_page':
 212              $post = get_post( $args[0] );
 213              if ( ! $post ) {
 214                  $caps[] = 'do_not_allow';
 215                  break;
 216              }
 217  
 218              if ( 'revision' == $post->post_type ) {
 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 ) {
 228                  /* translators: 1: Post type, 2: Capability name. */
 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              }
 233  
 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.
 237                  if ( 'read_post' == $cap ) {
 238                      $cap = $post_type->cap->$cap;
 239                  }
 240                  break;
 241              }
 242  
 243              $status_obj = get_post_status_object( $post->post_status );
 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  
 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;
 260              } else {
 261                  $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
 262              }
 263              break;
 264          case 'publish_post':
 265              $post = get_post( $args[0] );
 266              if ( ! $post ) {
 267                  $caps[] = 'do_not_allow';
 268                  break;
 269              }
 270  
 271              $post_type = get_post_type_object( $post->post_type );
 272              if ( ! $post_type ) {
 273                  /* translators: 1: Post type, 2: Capability name. */
 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              }
 278  
 279              $caps[] = $post_type->cap->publish_posts;
 280              break;
 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':
 293              list( $_, $object_type, $_ ) = explode( '_', $cap );
 294              $object_id                   = (int) $args[0];
 295  
 296              $object_subtype = get_object_subtype( $object_type, $object_id );
 297  
 298              if ( empty( $object_subtype ) ) {
 299                  $caps[] = 'do_not_allow';
 300                  break;
 301              }
 302  
 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 {
 330  
 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}`.
 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.
 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                       */
 375                      $allowed = apply_filters_deprecated( "auth_{$object_type}_{$object_subtype}_meta_{$meta_key}", array( $allowed, $meta_key, $object_id, $user_id, $cap, $caps ), '4.9.8', "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}" );
 376                  }
 377  
 378                  if ( ! $allowed ) {
 379                      $caps[] = $cap;
 380                  }
 381              }
 382              break;
 383          case 'edit_comment':
 384              $comment = get_comment( $args[0] );
 385              if ( ! $comment ) {
 386                  $caps[] = 'do_not_allow';
 387                  break;
 388              }
 389  
 390              $post = get_post( $comment->comment_post_ID );
 391  
 392              /*
 393               * If the post doesn't exist, we have an orphaned comment.
 394               * Fall back to the edit_posts capability, instead.
 395               */
 396              if ( $post ) {
 397                  $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
 398              } else {
 399                  $caps = map_meta_cap( 'edit_posts', $user_id );
 400              }
 401              break;
 402          case 'unfiltered_upload':
 403              if ( defined( 'ALLOW_UNFILTERED_UPLOADS' ) && ALLOW_UNFILTERED_UPLOADS && ( ! is_multisite() || is_super_admin( $user_id ) ) ) {
 404                  $caps[] = $cap;
 405              } else {
 406                  $caps[] = 'do_not_allow';
 407              }
 408              break;
 409          case 'edit_css':
 410          case 'unfiltered_html':
 411              // Disallow unfiltered_html for all users, even admins and super admins.
 412              if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML ) {
 413                  $caps[] = 'do_not_allow';
 414              } elseif ( is_multisite() && ! is_super_admin( $user_id ) ) {
 415                  $caps[] = 'do_not_allow';
 416              } else {
 417                  $caps[] = 'unfiltered_html';
 418              }
 419              break;
 420          case 'edit_files':
 421          case 'edit_plugins':
 422          case 'edit_themes':
 423              // Disallow the file editors.
 424              if ( defined( 'DISALLOW_FILE_EDIT' ) && DISALLOW_FILE_EDIT ) {
 425                  $caps[] = 'do_not_allow';
 426              } elseif ( ! wp_is_file_mod_allowed( 'capability_edit_themes' ) ) {
 427                  $caps[] = 'do_not_allow';
 428              } elseif ( is_multisite() && ! is_super_admin( $user_id ) ) {
 429                  $caps[] = 'do_not_allow';
 430              } else {
 431                  $caps[] = $cap;
 432              }
 433              break;
 434          case 'update_plugins':
 435          case 'delete_plugins':
 436          case 'install_plugins':
 437          case 'upload_plugins':
 438          case 'update_themes':
 439          case 'delete_themes':
 440          case 'install_themes':
 441          case 'upload_themes':
 442          case 'update_core':
 443              // Disallow anything that creates, deletes, or updates core, plugin, or theme files.
 444              // Files in uploads are excepted.
 445              if ( ! wp_is_file_mod_allowed( 'capability_update_core' ) ) {
 446                  $caps[] = 'do_not_allow';
 447              } elseif ( is_multisite() && ! is_super_admin( $user_id ) ) {
 448                  $caps[] = 'do_not_allow';
 449              } elseif ( 'upload_themes' === $cap ) {
 450                  $caps[] = 'install_themes';
 451              } elseif ( 'upload_plugins' === $cap ) {
 452                  $caps[] = 'install_plugins';
 453              } else {
 454                  $caps[] = $cap;
 455              }
 456              break;
 457          case 'install_languages':
 458          case 'update_languages':
 459              if ( ! wp_is_file_mod_allowed( 'can_install_language_pack' ) ) {
 460                  $caps[] = 'do_not_allow';
 461              } elseif ( is_multisite() && ! is_super_admin( $user_id ) ) {
 462                  $caps[] = 'do_not_allow';
 463              } else {
 464                  $caps[] = 'install_languages';
 465              }
 466              break;
 467          case 'activate_plugins':
 468          case 'deactivate_plugins':
 469          case 'activate_plugin':
 470          case 'deactivate_plugin':
 471              $caps[] = 'activate_plugins';
 472              if ( is_multisite() ) {
 473                  // update_, install_, and delete_ are handled above with is_super_admin().
 474                  $menu_perms = get_site_option( 'menu_items', array() );
 475                  if ( empty( $menu_perms['plugins'] ) ) {
 476                      $caps[] = 'manage_network_plugins';
 477                  }
 478              }
 479              break;
 480          case 'resume_plugin':
 481              $caps[] = 'resume_plugins';
 482              break;
 483          case 'resume_theme':
 484              $caps[] = 'resume_themes';
 485              break;
 486          case 'delete_user':
 487          case 'delete_users':
 488              // If multisite only super admins can delete users.
 489              if ( is_multisite() && ! is_super_admin( $user_id ) ) {
 490                  $caps[] = 'do_not_allow';
 491              } else {
 492                  $caps[] = 'delete_users'; // delete_user maps to delete_users.
 493              }
 494              break;
 495          case 'create_users':
 496              if ( ! is_multisite() ) {
 497                  $caps[] = $cap;
 498              } elseif ( is_super_admin( $user_id ) || get_site_option( 'add_new_users' ) ) {
 499                  $caps[] = $cap;
 500              } else {
 501                  $caps[] = 'do_not_allow';
 502              }
 503              break;
 504          case 'manage_links':
 505              if ( get_option( 'link_manager_enabled' ) ) {
 506                  $caps[] = $cap;
 507              } else {
 508                  $caps[] = 'do_not_allow';
 509              }
 510              break;
 511          case 'customize':
 512              $caps[] = 'edit_theme_options';
 513              break;
 514          case 'delete_site':
 515              if ( is_multisite() ) {
 516                  $caps[] = 'manage_options';
 517              } else {
 518                  $caps[] = 'do_not_allow';
 519              }
 520              break;
 521          case 'edit_term':
 522          case 'delete_term':
 523          case 'assign_term':
 524              $term_id = (int) $args[0];
 525              $term    = get_term( $term_id );
 526              if ( ! $term || is_wp_error( $term ) ) {
 527                  $caps[] = 'do_not_allow';
 528                  break;
 529              }
 530  
 531              $tax = get_taxonomy( $term->taxonomy );
 532              if ( ! $tax ) {
 533                  $caps[] = 'do_not_allow';
 534                  break;
 535              }
 536  
 537              if ( 'delete_term' === $cap && ( get_option( 'default_' . $term->taxonomy ) == $term->term_id ) ) {
 538                  $caps[] = 'do_not_allow';
 539                  break;
 540              }
 541  
 542              $taxo_cap = $cap . 's';
 543  
 544              $caps = map_meta_cap( $tax->cap->$taxo_cap, $user_id, $term_id );
 545  
 546              break;
 547          case 'manage_post_tags':
 548          case 'edit_categories':
 549          case 'edit_post_tags':
 550          case 'delete_categories':
 551          case 'delete_post_tags':
 552              $caps[] = 'manage_categories';
 553              break;
 554          case 'assign_categories':
 555          case 'assign_post_tags':
 556              $caps[] = 'edit_posts';
 557              break;
 558          case 'create_sites':
 559          case 'delete_sites':
 560          case 'manage_network':
 561          case 'manage_sites':
 562          case 'manage_network_users':
 563          case 'manage_network_plugins':
 564          case 'manage_network_themes':
 565          case 'manage_network_options':
 566          case 'upgrade_network':
 567              $caps[] = $cap;
 568              break;
 569          case 'setup_network':
 570              if ( is_multisite() ) {
 571                  $caps[] = 'manage_network_options';
 572              } else {
 573                  $caps[] = 'manage_options';
 574              }
 575              break;
 576          case 'update_php':
 577              if ( is_multisite() && ! is_super_admin( $user_id ) ) {
 578                  $caps[] = 'do_not_allow';
 579              } else {
 580                  $caps[] = 'update_core';
 581              }
 582              break;
 583          case 'export_others_personal_data':
 584          case 'erase_others_personal_data':
 585          case 'manage_privacy_options':
 586              $caps[] = is_multisite() ? 'manage_network' : 'manage_options';
 587              break;
 588          default:
 589              // Handle meta capabilities for custom post types.
 590              global $post_type_meta_caps;
 591              if ( isset( $post_type_meta_caps[ $cap ] ) ) {
 592                  return map_meta_cap( $post_type_meta_caps[ $cap ], $user_id, ...$args );
 593              }
 594  
 595              // Block capabilities map to their post equivalent.
 596              $block_caps = array(
 597                  'edit_blocks',
 598                  'edit_others_blocks',
 599                  'publish_blocks',
 600                  'read_private_blocks',
 601                  'delete_blocks',
 602                  'delete_private_blocks',
 603                  'delete_published_blocks',
 604                  'delete_others_blocks',
 605                  'edit_private_blocks',
 606                  'edit_published_blocks',
 607              );
 608              if ( in_array( $cap, $block_caps, true ) ) {
 609                  $cap = str_replace( '_blocks', '_posts', $cap );
 610              }
 611  
 612              // If no meta caps match, return the original cap.
 613              $caps[] = $cap;
 614      }
 615  
 616      /**
 617       * Filters a user's capabilities depending on specific context and/or privilege.
 618       *
 619       * @since 2.8.0
 620       *
 621       * @param string[] $caps    Array of the user's capabilities.
 622       * @param string   $cap     Capability name.
 623       * @param int      $user_id The user ID.
 624       * @param array    $args    Adds the context to the cap. Typically the object ID.
 625       */
 626      return apply_filters( 'map_meta_cap', $caps, $cap, $user_id, $args );
 627  }
 628  
 629  /**
 630   * Returns whether the current user has the specified capability.
 631   *
 632   * This function also accepts an ID of an object to check against if the capability is a meta capability. Meta
 633   * capabilities such as `edit_post` and `edit_user` are capabilities used by the `map_meta_cap()` function to
 634   * map to primitive capabilities that a user or role has, such as `edit_posts` and `edit_others_posts`.
 635   *
 636   * Example usage:
 637   *
 638   *     current_user_can( 'edit_posts' );
 639   *     current_user_can( 'edit_post', $post->ID );
 640   *     current_user_can( 'edit_post_meta', $post->ID, $meta_key );
 641   *
 642   * While checking against particular roles in place of a capability is supported
 643   * in part, this practice is discouraged as it may produce unreliable results.
 644   *
 645   * Note: Will always return true if the current user is a super admin, unless specifically denied.
 646   *
 647   * @since 2.0.0
 648   * @since 5.3.0 Formalized the existing and already documented `...$args` parameter
 649   *              by adding it to the function signature.
 650   *
 651   * @see WP_User::has_cap()
 652   * @see map_meta_cap()
 653   *
 654   * @param string $capability Capability name.
 655   * @param mixed  ...$args    Optional further parameters, typically starting with an object ID.
 656   * @return bool Whether the current user has the given capability. If `$capability` is a meta cap and `$object_id` is
 657   *              passed, whether the current user has the given meta capability for the given object.
 658   */
 659  function current_user_can( $capability, ...$args ) {
 660      $current_user = wp_get_current_user();
 661  
 662      if ( empty( $current_user ) ) {
 663          return false;
 664      }
 665  
 666      return $current_user->has_cap( $capability, ...$args );
 667  }
 668  
 669  /**
 670   * Returns whether the current user has the specified capability for a given site.
 671   *
 672   * This function also accepts an ID of an object to check against if the capability is a meta capability. Meta
 673   * capabilities such as `edit_post` and `edit_user` are capabilities used by the `map_meta_cap()` function to
 674   * map to primitive capabilities that a user or role has, such as `edit_posts` and `edit_others_posts`.
 675   *
 676   * Example usage:
 677   *
 678   *     current_user_can_for_blog( $blog_id, 'edit_posts' );
 679   *     current_user_can_for_blog( $blog_id, 'edit_post', $post->ID );
 680   *     current_user_can_for_blog( $blog_id, 'edit_post_meta', $post->ID, $meta_key );
 681   *
 682   * @since 3.0.0
 683   * @since 5.3.0 Formalized the existing and already documented `...$args` parameter
 684   *              by adding it to the function signature.
 685   *
 686   * @param int    $blog_id    Site ID.
 687   * @param string $capability Capability name.
 688   * @param mixed  ...$args    Optional further parameters, typically starting with an object ID.
 689   * @return bool Whether the user has the given capability.
 690   */
 691  function current_user_can_for_blog( $blog_id, $capability, ...$args ) {
 692      $switched = is_multisite() ? switch_to_blog( $blog_id ) : false;
 693  
 694      $current_user = wp_get_current_user();
 695  
 696      if ( empty( $current_user ) ) {
 697          if ( $switched ) {
 698              restore_current_blog();
 699          }
 700          return false;
 701      }
 702  
 703      $can = $current_user->has_cap( $capability, ...$args );
 704  
 705      if ( $switched ) {
 706          restore_current_blog();
 707      }
 708  
 709      return $can;
 710  }
 711  
 712  /**
 713   * Returns whether the author of the supplied post has the specified capability.
 714   *
 715   * This function also accepts an ID of an object to check against if the capability is a meta capability. Meta
 716   * capabilities such as `edit_post` and `edit_user` are capabilities used by the `map_meta_cap()` function to
 717   * map to primitive capabilities that a user or role has, such as `edit_posts` and `edit_others_posts`.
 718   *
 719   * Example usage:
 720   *
 721   *     author_can( $post, 'edit_posts' );
 722   *     author_can( $post, 'edit_post', $post->ID );
 723   *     author_can( $post, 'edit_post_meta', $post->ID, $meta_key );
 724   *
 725   * @since 2.9.0
 726   * @since 5.3.0 Formalized the existing and already documented `...$args` parameter
 727   *              by adding it to the function signature.
 728   *
 729   * @param int|WP_Post $post       Post ID or post object.
 730   * @param string      $capability Capability name.
 731   * @param mixed       ...$args    Optional further parameters, typically starting with an object ID.
 732   * @return bool Whether the post author has the given capability.
 733   */
 734  function author_can( $post, $capability, ...$args ) {
 735      $post = get_post( $post );
 736      if ( ! $post ) {
 737          return false;
 738      }
 739  
 740      $author = get_userdata( $post->post_author );
 741  
 742      if ( ! $author ) {
 743          return false;
 744      }
 745  
 746      return $author->has_cap( $capability, ...$args );
 747  }
 748  
 749  /**
 750   * Returns whether a particular user has the specified capability.
 751   *
 752   * This function also accepts an ID of an object to check against if the capability is a meta capability. Meta
 753   * capabilities such as `edit_post` and `edit_user` are capabilities used by the `map_meta_cap()` function to
 754   * map to primitive capabilities that a user or role has, such as `edit_posts` and `edit_others_posts`.
 755   *
 756   * Example usage:
 757   *
 758   *     user_can( $user->ID, 'edit_posts' );
 759   *     user_can( $user->ID, 'edit_post', $post->ID );
 760   *     user_can( $user->ID, 'edit_post_meta', $post->ID, $meta_key );
 761   *
 762   * @since 3.1.0
 763   * @since 5.3.0 Formalized the existing and already documented `...$args` parameter
 764   *              by adding it to the function signature.
 765   *
 766   * @param int|WP_User $user       User ID or object.
 767   * @param string      $capability Capability name.
 768   * @param mixed       ...$args    Optional further parameters, typically starting with an object ID.
 769   * @return bool Whether the user has the given capability.
 770   */
 771  function user_can( $user, $capability, ...$args ) {
 772      if ( ! is_object( $user ) ) {
 773          $user = get_userdata( $user );
 774      }
 775  
 776      if ( ! $user || ! $user->exists() ) {
 777          return false;
 778      }
 779  
 780      return $user->has_cap( $capability, ...$args );
 781  }
 782  
 783  /**
 784   * Retrieves the global WP_Roles instance and instantiates it if necessary.
 785   *
 786   * @since 4.3.0
 787   *
 788   * @global WP_Roles $wp_roles WordPress role management object.
 789   *
 790   * @return WP_Roles WP_Roles global instance if not already instantiated.
 791   */
 792  function wp_roles() {
 793      global $wp_roles;
 794  
 795      if ( ! isset( $wp_roles ) ) {
 796          $wp_roles = new WP_Roles();
 797      }
 798      return $wp_roles;
 799  }
 800  
 801  /**
 802   * Retrieve role object.
 803   *
 804   * @since 2.0.0
 805   *
 806   * @param string $role Role name.
 807   * @return WP_Role|null WP_Role object if found, null if the role does not exist.
 808   */
 809  function get_role( $role ) {
 810      return wp_roles()->get_role( $role );
 811  }
 812  
 813  /**
 814   * Add role, if it does not exist.
 815   *
 816   * @since 2.0.0
 817   *
 818   * @param string $role         Role name.
 819   * @param string $display_name Display name for role.
 820   * @param bool[] $capabilities List of capabilities keyed by the capability name,
 821   *                             e.g. array( 'edit_posts' => true, 'delete_posts' => false ).
 822   * @return WP_Role|null WP_Role object if role is added, null if already exists.
 823   */
 824  function add_role( $role, $display_name, $capabilities = array() ) {
 825      if ( empty( $role ) ) {
 826          return;
 827      }
 828      return wp_roles()->add_role( $role, $display_name, $capabilities );
 829  }
 830  
 831  /**
 832   * Remove role, if it exists.
 833   *
 834   * @since 2.0.0
 835   *
 836   * @param string $role Role name.
 837   */
 838  function remove_role( $role ) {
 839      wp_roles()->remove_role( $role );
 840  }
 841  
 842  /**
 843   * Retrieve a list of super admins.
 844   *
 845   * @since 3.0.0
 846   *
 847   * @global array $super_admins
 848   *
 849   * @return string[] List of super admin logins.
 850   */
 851  function get_super_admins() {
 852      global $super_admins;
 853  
 854      if ( isset( $super_admins ) ) {
 855          return $super_admins;
 856      } else {
 857          return get_site_option( 'site_admins', array( 'admin' ) );
 858      }
 859  }
 860  
 861  /**
 862   * Determine if user is a site admin.
 863   *
 864   * @since 3.0.0
 865   *
 866   * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
 867   * @return bool True if the user is a site admin.
 868   */
 869  function is_super_admin( $user_id = false ) {
 870      if ( ! $user_id || get_current_user_id() == $user_id ) {
 871          $user = wp_get_current_user();
 872      } else {
 873          $user = get_userdata( $user_id );
 874      }
 875  
 876      if ( ! $user || ! $user->exists() ) {
 877          return false;
 878      }
 879  
 880      if ( is_multisite() ) {
 881          $super_admins = get_super_admins();
 882          if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) ) {
 883              return true;
 884          }
 885      } else {
 886          if ( $user->has_cap( 'delete_users' ) ) {
 887              return true;
 888          }
 889      }
 890  
 891      return false;
 892  }
 893  
 894  /**
 895   * Grants Super Admin privileges.
 896   *
 897   * @since 3.0.0
 898   *
 899   * @global array $super_admins
 900   *
 901   * @param int $user_id ID of the user to be granted Super Admin privileges.
 902   * @return bool True on success, false on failure. This can fail when the user is
 903   *              already a super admin or when the `$super_admins` global is defined.
 904   */
 905  function grant_super_admin( $user_id ) {
 906      // If global super_admins override is defined, there is nothing to do here.
 907      if ( isset( $GLOBALS['super_admins'] ) || ! is_multisite() ) {
 908          return false;
 909      }
 910  
 911      /**
 912       * Fires before the user is granted Super Admin privileges.
 913       *
 914       * @since 3.0.0
 915       *
 916       * @param int $user_id ID of the user that is about to be granted Super Admin privileges.
 917       */
 918      do_action( 'grant_super_admin', $user_id );
 919  
 920      // Directly fetch site_admins instead of using get_super_admins().
 921      $super_admins = get_site_option( 'site_admins', array( 'admin' ) );
 922  
 923      $user = get_userdata( $user_id );
 924      if ( $user && ! in_array( $user->user_login, $super_admins ) ) {
 925          $super_admins[] = $user->user_login;
 926          update_site_option( 'site_admins', $super_admins );
 927  
 928          /**
 929           * Fires after the user is granted Super Admin privileges.
 930           *
 931           * @since 3.0.0
 932           *
 933           * @param int $user_id ID of the user that was granted Super Admin privileges.
 934           */
 935          do_action( 'granted_super_admin', $user_id );
 936          return true;
 937      }
 938      return false;
 939  }
 940  
 941  /**
 942   * Revokes Super Admin privileges.
 943   *
 944   * @since 3.0.0
 945   *
 946   * @global array $super_admins
 947   *
 948   * @param int $user_id ID of the user Super Admin privileges to be revoked from.
 949   * @return bool True on success, false on failure. This can fail when the user's email
 950   *              is the network admin email or when the `$super_admins` global is defined.
 951   */
 952  function revoke_super_admin( $user_id ) {
 953      // If global super_admins override is defined, there is nothing to do here.
 954      if ( isset( $GLOBALS['super_admins'] ) || ! is_multisite() ) {
 955          return false;
 956      }
 957  
 958      /**
 959       * Fires before the user's Super Admin privileges are revoked.
 960       *
 961       * @since 3.0.0
 962       *
 963       * @param int $user_id ID of the user Super Admin privileges are being revoked from.
 964       */
 965      do_action( 'revoke_super_admin', $user_id );
 966  
 967      // Directly fetch site_admins instead of using get_super_admins().
 968      $super_admins = get_site_option( 'site_admins', array( 'admin' ) );
 969  
 970      $user = get_userdata( $user_id );
 971      if ( $user && 0 !== strcasecmp( $user->user_email, get_site_option( 'admin_email' ) ) ) {
 972          $key = array_search( $user->user_login, $super_admins );
 973          if ( false !== $key ) {
 974              unset( $super_admins[ $key ] );
 975              update_site_option( 'site_admins', $super_admins );
 976  
 977              /**
 978               * Fires after the user's Super Admin privileges are revoked.
 979               *
 980               * @since 3.0.0
 981               *
 982               * @param int $user_id ID of the user Super Admin privileges were revoked from.
 983               */
 984              do_action( 'revoked_super_admin', $user_id );
 985              return true;
 986          }
 987      }
 988      return false;
 989  }
 990  
 991  /**
 992   * Filters the user capabilities to grant the 'install_languages' capability as necessary.
 993   *
 994   * A user must have at least one out of the 'update_core', 'install_plugins', and
 995   * 'install_themes' capabilities to qualify for 'install_languages'.
 996   *
 997   * @since 4.9.0
 998   *
 999   * @param bool[] $allcaps An array of all the user's capabilities.
1000   * @return bool[] Filtered array of the user's capabilities.
1001   */
1002  function wp_maybe_grant_install_languages_cap( $allcaps ) {
1003      if ( ! empty( $allcaps['update_core'] ) || ! empty( $allcaps['install_plugins'] ) || ! empty( $allcaps['install_themes'] ) ) {
1004          $allcaps['install_languages'] = true;
1005      }
1006  
1007      return $allcaps;
1008  }
1009  
1010  /**
1011   * Filters the user capabilities to grant the 'resume_plugins' and 'resume_themes' capabilities as necessary.
1012   *
1013   * @since 5.2.0
1014   *
1015   * @param bool[] $allcaps An array of all the user's capabilities.
1016   * @return bool[] Filtered array of the user's capabilities.
1017   */
1018  function wp_maybe_grant_resume_extensions_caps( $allcaps ) {
1019      // Even in a multisite, regular administrators should be able to resume plugins.
1020      if ( ! empty( $allcaps['activate_plugins'] ) ) {
1021          $allcaps['resume_plugins'] = true;
1022      }
1023  
1024      // Even in a multisite, regular administrators should be able to resume themes.
1025      if ( ! empty( $allcaps['switch_themes'] ) ) {
1026          $allcaps['resume_themes'] = true;
1027      }
1028  
1029      return $allcaps;
1030  }
1031  
1032  /**
1033   * Filters the user capabilities to grant the 'view_site_health_checks' capabilities as necessary.
1034   *
1035   * @since 5.2.2
1036   *
1037   * @param bool[]   $allcaps An array of all the user's capabilities.
1038   * @param string[] $caps    Required primitive capabilities for the requested capability.
1039   * @param array    $args {
1040   *     Arguments that accompany the requested capability check.
1041   *
1042   *     @type string    $0 Requested capability.
1043   *     @type int       $1 Concerned user ID.
1044   *     @type mixed  ...$2 Optional second and further parameters, typically object ID.
1045   * }
1046   * @param WP_User  $user    The user object.
1047   * @return bool[] Filtered array of the user's capabilities.
1048   */
1049  function wp_maybe_grant_site_health_caps( $allcaps, $caps, $args, $user ) {
1050      if ( ! empty( $allcaps['install_plugins'] ) && ( ! is_multisite() || is_super_admin( $user->ID ) ) ) {
1051          $allcaps['view_site_health_checks'] = true;
1052      }
1053  
1054      return $allcaps;
1055  }
1056  
1057  return;
1058  
1059  // Dummy gettext calls to get strings in the catalog.
1060  /* translators: User role for administrators. */
1061  _x( 'Administrator', 'User role' );
1062  /* translators: User role for editors. */
1063  _x( 'Editor', 'User role' );
1064  /* translators: User role for authors. */
1065  _x( 'Author', 'User role' );
1066  /* translators: User role for contributors. */
1067  _x( 'Contributor', 'User role' );
1068  /* translators: User role for subscribers. */
1069  _x( 'Subscriber', 'User role' );


Generated: Wed Feb 26 01:00:04 2020 Cross-referenced by PHPXref 0.7.1