]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/class-wp-role.php
WordPress 4.4.1-scripts
[autoinstalls/wordpress.git] / wp-includes / class-wp-role.php
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          * @access public
21          * @var string
22          */
23         public $name;
24
25         /**
26          * List of capabilities the role contains.
27          *
28          * @since 2.0.0
29          * @access public
30          * @var array
31          */
32         public $capabilities;
33
34         /**
35          * Constructor - Set up object properties.
36          *
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.
39          *
40          * @since 2.0.0
41          * @access public
42          *
43          * @param string $role Role name.
44          * @param array $capabilities List of capabilities.
45          */
46         public function __construct( $role, $capabilities ) {
47                 $this->name = $role;
48                 $this->capabilities = $capabilities;
49         }
50
51         /**
52          * Assign role a capability.
53          *
54          * @since 2.0.0
55          * @access public
56          *
57          * @param string $cap Capability name.
58          * @param bool $grant Whether role has capability privilege.
59          */
60         public function add_cap( $cap, $grant = true ) {
61                 $this->capabilities[$cap] = $grant;
62                 wp_roles()->add_cap( $this->name, $cap, $grant );
63         }
64
65         /**
66          * Remove capability from role.
67          *
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.
72          *
73          * @since 2.0.0
74          * @access public
75          *
76          * @param string $cap Capability name.
77          */
78         public function remove_cap( $cap ) {
79                 unset( $this->capabilities[$cap] );
80                 wp_roles()->remove_cap( $this->name, $cap );
81         }
82
83         /**
84          * Whether role has capability.
85          *
86          * The capabilities is passed through the 'role_has_cap' filter. The first
87          * parameter for the hook is the list of capabilities the class has
88          * assigned. The second parameter is the capability name to look for. The
89          * third and final parameter for the hook is the role name.
90          *
91          * @since 2.0.0
92          * @access public
93          *
94          * @param string $cap Capability name.
95          * @return bool True, if user has capability. False, if doesn't have capability.
96          */
97         public function has_cap( $cap ) {
98                 /**
99                  * Filter which capabilities a role has.
100                  *
101                  * @since 2.0.0
102                  *
103                  * @param array  $capabilities Array of role capabilities.
104                  * @param string $cap          Capability name.
105                  * @param string $name         Role name.
106                  */
107                 $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );
108                 if ( !empty( $capabilities[$cap] ) )
109                         return $capabilities[$cap];
110                 else
111                         return false;
112         }
113
114 }