"Fossies" - the Fresh Open Source Software Archive

Member "wordpress/wp-includes/class-wp-role.php" (23 Jul 2020, 2498 Bytes) of package /linux/www/wordpress-5.7-RC1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "class-wp-role.php" see the Fossies "Dox" file reference documentation.

    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 bool[] Array of key/value pairs where keys represent a capability name and boolean values
   29      *             represent whether the role has that capability.
   30      */
   31     public $capabilities;
   32 
   33     /**
   34      * Constructor - Set up object properties.
   35      *
   36      * The list of capabilities must have the key as the name of the capability
   37      * and the value a boolean of whether it is granted to the role.
   38      *
   39      * @since 2.0.0
   40      *
   41      * @param string $role         Role name.
   42      * @param bool[] $capabilities Array of key/value pairs where keys represent a capability name and boolean values
   43      *                             represent whether the role has that capability.
   44      */
   45     public function __construct( $role, $capabilities ) {
   46         $this->name         = $role;
   47         $this->capabilities = $capabilities;
   48     }
   49 
   50     /**
   51      * Assign role a capability.
   52      *
   53      * @since 2.0.0
   54      *
   55      * @param string $cap   Capability name.
   56      * @param bool   $grant Whether role has capability privilege.
   57      */
   58     public function add_cap( $cap, $grant = true ) {
   59         $this->capabilities[ $cap ] = $grant;
   60         wp_roles()->add_cap( $this->name, $cap, $grant );
   61     }
   62 
   63     /**
   64      * Removes a capability from a role.
   65      *
   66      * @since 2.0.0
   67      *
   68      * @param string $cap Capability name.
   69      */
   70     public function remove_cap( $cap ) {
   71         unset( $this->capabilities[ $cap ] );
   72         wp_roles()->remove_cap( $this->name, $cap );
   73     }
   74 
   75     /**
   76      * Determines whether the role has the given capability.
   77      *
   78      * @since 2.0.0
   79      *
   80      * @param string $cap Capability name.
   81      * @return bool Whether the role has the given capability.
   82      */
   83     public function has_cap( $cap ) {
   84         /**
   85          * Filters which capabilities a role has.
   86          *
   87          * @since 2.0.0
   88          *
   89          * @param bool[] $capabilities Array of key/value pairs where keys represent a capability name and boolean values
   90          *                             represent whether the role has that capability.
   91          * @param string $cap          Capability name.
   92          * @param string $name         Role name.
   93          */
   94         $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );
   95 
   96         if ( ! empty( $capabilities[ $cap ] ) ) {
   97             return $capabilities[ $cap ];
   98         } else {
   99             return false;
  100         }
  101     }
  102 
  103 }