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


Generated: Wed Sep 18 01:00:03 2019 Cross-referenced by PHPXref 0.7.1