|
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 array $capabilities List of capabilities. |
|
42 */ |
|
43 public function __construct( $role, $capabilities ) { |
|
44 $this->name = $role; |
|
45 $this->capabilities = $capabilities; |
|
46 } |
|
47 |
|
48 /** |
|
49 * Assign role a capability. |
|
50 * |
|
51 * @since 2.0.0 |
|
52 * |
|
53 * @param string $cap Capability name. |
|
54 * @param bool $grant Whether role has capability privilege. |
|
55 */ |
|
56 public function add_cap( $cap, $grant = true ) { |
|
57 $this->capabilities[$cap] = $grant; |
|
58 wp_roles()->add_cap( $this->name, $cap, $grant ); |
|
59 } |
|
60 |
|
61 /** |
|
62 * 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 * |
|
69 * @since 2.0.0 |
|
70 * |
|
71 * @param string $cap Capability name. |
|
72 */ |
|
73 public function remove_cap( $cap ) { |
|
74 unset( $this->capabilities[$cap] ); |
|
75 wp_roles()->remove_cap( $this->name, $cap ); |
|
76 } |
|
77 |
|
78 /** |
|
79 * Determines whether the role has the given capability. |
|
80 * |
|
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 |
|
87 * |
|
88 * @param string $cap Capability name. |
|
89 * @return bool True if the role has the given capability. False otherwise. |
|
90 */ |
|
91 public function has_cap( $cap ) { |
|
92 /** |
|
93 * Filters which capabilities a role has. |
|
94 * |
|
95 * @since 2.0.0 |
|
96 * |
|
97 * @param array $capabilities Array of role capabilities. |
|
98 * @param string $cap Capability name. |
|
99 * @param string $name Role name. |
|
100 */ |
|
101 $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name ); |
|
102 |
|
103 if ( !empty( $capabilities[$cap] ) ) |
|
104 return $capabilities[$cap]; |
|
105 else |
|
106 return false; |
|
107 } |
|
108 |
|
109 } |