wp/wp-includes/class-wp-role.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 21 48c4eec2b7e6
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    23 
    23 
    24 	/**
    24 	/**
    25 	 * List of capabilities the role contains.
    25 	 * List of capabilities the role contains.
    26 	 *
    26 	 *
    27 	 * @since 2.0.0
    27 	 * @since 2.0.0
    28 	 * @var array
    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.
    29 	 */
    30 	 */
    30 	public $capabilities;
    31 	public $capabilities;
    31 
    32 
    32 	/**
    33 	/**
    33 	 * Constructor - Set up object properties.
    34 	 * Constructor - Set up object properties.
    34 	 *
    35 	 *
    35 	 * The list of capabilities, must have the key as the name of the capability
    36 	 * 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 	 * and the value a boolean of whether it is granted to the role.
    37 	 *
    38 	 *
    38 	 * @since 2.0.0
    39 	 * @since 2.0.0
    39 	 *
    40 	 *
    40 	 * @param string $role Role name.
    41 	 * @param string $role         Role name.
    41 	 * @param array $capabilities List of capabilities.
    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.
    42 	 */
    44 	 */
    43 	public function __construct( $role, $capabilities ) {
    45 	public function __construct( $role, $capabilities ) {
    44 		$this->name         = $role;
    46 		$this->name         = $role;
    45 		$this->capabilities = $capabilities;
    47 		$this->capabilities = $capabilities;
    46 	}
    48 	}
    48 	/**
    50 	/**
    49 	 * Assign role a capability.
    51 	 * Assign role a capability.
    50 	 *
    52 	 *
    51 	 * @since 2.0.0
    53 	 * @since 2.0.0
    52 	 *
    54 	 *
    53 	 * @param string $cap Capability name.
    55 	 * @param string $cap   Capability name.
    54 	 * @param bool $grant Whether role has capability privilege.
    56 	 * @param bool   $grant Whether role has capability privilege.
    55 	 */
    57 	 */
    56 	public function add_cap( $cap, $grant = true ) {
    58 	public function add_cap( $cap, $grant = true ) {
    57 		$this->capabilities[ $cap ] = $grant;
    59 		$this->capabilities[ $cap ] = $grant;
    58 		wp_roles()->add_cap( $this->name, $cap, $grant );
    60 		wp_roles()->add_cap( $this->name, $cap, $grant );
    59 	}
    61 	}
    60 
    62 
    61 	/**
    63 	/**
    62 	 * Removes a capability from a role.
    64 	 * Removes a capability from a role.
    63 	 *
       
    64 	 * This is a container for WP_Roles::remove_cap() to remove the
       
    65 	 * capability from the role. That is to say, that WP_Roles::remove_cap()
       
    66 	 * implements the functionality, but it also makes sense to use this class,
       
    67 	 * because you don't need to enter the role name.
       
    68 	 *
    65 	 *
    69 	 * @since 2.0.0
    66 	 * @since 2.0.0
    70 	 *
    67 	 *
    71 	 * @param string $cap Capability name.
    68 	 * @param string $cap Capability name.
    72 	 */
    69 	 */
    76 	}
    73 	}
    77 
    74 
    78 	/**
    75 	/**
    79 	 * Determines whether the role has the given capability.
    76 	 * Determines whether the role has the given capability.
    80 	 *
    77 	 *
    81 	 * The capabilities is passed through the {@see 'role_has_cap'} filter.
       
    82 	 * The first parameter for the hook is the list of capabilities the class
       
    83 	 * has assigned. The second parameter is the capability name to look for.
       
    84 	 * The third and final parameter for the hook is the role name.
       
    85 	 *
       
    86 	 * @since 2.0.0
    78 	 * @since 2.0.0
    87 	 *
    79 	 *
    88 	 * @param string $cap Capability name.
    80 	 * @param string $cap Capability name.
    89 	 * @return bool True if the role has the given capability. False otherwise.
    81 	 * @return bool Whether the role has the given capability.
    90 	 */
    82 	 */
    91 	public function has_cap( $cap ) {
    83 	public function has_cap( $cap ) {
    92 		/**
    84 		/**
    93 		 * Filters which capabilities a role has.
    85 		 * Filters which capabilities a role has.
    94 		 *
    86 		 *
    95 		 * @since 2.0.0
    87 		 * @since 2.0.0
    96 		 *
    88 		 *
    97 		 * @param bool[] $capabilities Associative array of capabilities for the role.
    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.
    98 		 * @param string $cap          Capability name.
    91 		 * @param string $cap          Capability name.
    99 		 * @param string $name         Role name.
    92 		 * @param string $name         Role name.
   100 		 */
    93 		 */
   101 		$capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );
    94 		$capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );
   102 
    95