[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> class-wp-role.php (source)

   1  <?php
   2  /**
   3   * User API: WP_Role class
   4   *
   5   * @package WordPress
   6   * @subpackage Users
   7   * @since 4.4.0
   8   */
   9  
  10  /**
  11   * Core class used to extend the user roles API.
  12   *
  13   * @since 2.0.0
  14   */
  15  class WP_Role {
  16      /**
  17       * Role name.
  18       *
  19       * @since 2.0.0
  20       * @var string
  21       */
  22      public $name;
  23  
  24      /**
  25       * List of capabilities the role contains.
  26       *
  27       * @since 2.0.0
  28       * @var array
  29       */
  30      public $capabilities;
  31  
  32      /**
  33       * Constructor - Set up object properties.
  34       *
  35       * The list of capabilities, must have the key as the name of the capability
  36       * and the value a boolean of whether it is granted to the role.
  37       *
  38       * @since 2.0.0
  39       *
  40       * @param string $role         Role name.
  41       * @param bool[] $capabilities List of capabilities keyed by the capability name,
  42       *                             e.g. array( 'edit_posts' => true, 'delete_posts' => false ).
  43       */
  44  	public function __construct( $role, $capabilities ) {
  45          $this->name         = $role;
  46          $this->capabilities = $capabilities;
  47      }
  48  
  49      /**
  50       * Assign role a capability.
  51       *
  52       * @since 2.0.0
  53       *
  54       * @param string $cap Capability name.
  55       * @param bool $grant Whether role has capability privilege.
  56       */
  57  	public function add_cap( $cap, $grant = true ) {
  58          $this->capabilities[ $cap ] = $grant;
  59          wp_roles()->add_cap( $this->name, $cap, $grant );
  60      }
  61  
  62      /**
  63       * Removes a capability from a role.
  64       *
  65       * This is a container for WP_Roles::remove_cap() to remove the
  66       * capability from the role. That is to say, that WP_Roles::remove_cap()
  67       * implements the functionality, but it also makes sense to use this class,
  68       * because you don't need to enter the role name.
  69       *
  70       * @since 2.0.0
  71       *
  72       * @param string $cap Capability name.
  73       */
  74  	public function remove_cap( $cap ) {
  75          unset( $this->capabilities[ $cap ] );
  76          wp_roles()->remove_cap( $this->name, $cap );
  77      }
  78  
  79      /**
  80       * Determines whether the role has the given capability.
  81       *
  82       * The capabilities is passed through the {@see 'role_has_cap'} filter.
  83       * The first parameter for the hook is the list of capabilities the class
  84       * has assigned. The second parameter is the capability name to look for.
  85       * The third and final parameter for the hook is the role name.
  86       *
  87       * @since 2.0.0
  88       *
  89       * @param string $cap Capability name.
  90       * @return bool True if the role has the given capability. False otherwise.
  91       */
  92  	public function has_cap( $cap ) {
  93          /**
  94           * Filters which capabilities a role has.
  95           *
  96           * @since 2.0.0
  97           *
  98           * @param bool[] $capabilities Associative array of capabilities for the role.
  99           * @param string $cap          Capability name.
 100           * @param string $name         Role name.
 101           */
 102          $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );
 103  
 104          if ( ! empty( $capabilities[ $cap ] ) ) {
 105              return $capabilities[ $cap ];
 106          } else {
 107              return false;
 108          }
 109      }
 110  
 111  }


Generated: Sun Feb 23 01:00:03 2020 Cross-referenced by PHPXref 0.7.1