3 * User API: WP_Role class
11 * Core class used to extend the user roles API.
26 * List of capabilities the role contains.
35 * Constructor - Set up object properties.
37 * The list of capabilities, must have the key as the name of the capability
38 * and the value a boolean of whether it is granted to the role.
43 * @param string $role Role name.
44 * @param array $capabilities List of capabilities.
46 public function __construct( $role, $capabilities ) {
48 $this->capabilities = $capabilities;
52 * Assign role a capability.
57 * @param string $cap Capability name.
58 * @param bool $grant Whether role has capability privilege.
60 public function add_cap( $cap, $grant = true ) {
61 $this->capabilities[$cap] = $grant;
62 wp_roles()->add_cap( $this->name, $cap, $grant );
66 * Remove capability from role.
68 * This is a container for {@link WP_Roles::remove_cap()} to remove the
69 * capability from the role. That is to say, that {@link
70 * WP_Roles::remove_cap()} implements the functionality, but it also makes
71 * sense to use this class, because you don't need to enter the role name.
76 * @param string $cap Capability name.
78 public function remove_cap( $cap ) {
79 unset( $this->capabilities[$cap] );
80 wp_roles()->remove_cap( $this->name, $cap );
84 * Determines whether the role has the given capability.
86 * The capabilities is passed through the {@see 'role_has_cap'} filter.
87 * The first parameter for the hook is the list of capabilities the class
88 * has assigned. The second parameter is the capability name to look for.
89 * The third and final parameter for the hook is the role name.
94 * @param string $cap Capability name.
95 * @return bool True if the role has the given capability. False otherwise.
97 public function has_cap( $cap ) {
99 * Filter which capabilities a role has.
103 * @param array $capabilities Array of role capabilities.
104 * @param string $cap Capability name.
105 * @param string $name Role name.
107 $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );
109 if ( !empty( $capabilities[$cap] ) )
110 return $capabilities[$cap];