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


Generated: Wed Aug 12 01:00:03 2020 Cross-referenced by PHPXref 0.7.1