3 * User API: WP_Roles class
11 * Core class used to implement a user roles API.
13 * The role option is simple, the structure is organized by role name that store
14 * the name in value of the 'name' key. The capabilities are stored as an array
15 * in the value of the 'capability' key.
18 * 'rolename' => array (
19 * 'name' => 'rolename',
20 * 'capabilities' => array()
28 * List of roles and capabilities.
37 * List of the role objects.
43 public $role_objects = array();
52 public $role_names = array();
55 * Option name for storing role list.
64 * Whether to use the database for retrieval and storage.
70 public $use_db = true;
77 public function __construct() {
82 * Make private/protected methods readable for backward compatibility.
87 * @param callable $name Method to call.
88 * @param array $arguments Arguments to pass when calling.
89 * @return mixed|false Return value of the callback, false otherwise.
91 public function __call( $name, $arguments ) {
92 if ( '_init' === $name ) {
93 return call_user_func_array( array( $this, $name ), $arguments );
99 * Set up the object properties.
101 * The role key is set to the current prefix for the $wpdb object with
102 * 'user_roles' appended. If the $wp_user_roles global is set, then it will
103 * be used and the role option will not be updated or used.
108 * @global wpdb $wpdb WordPress database abstraction object.
109 * @global array $wp_user_roles Used to set the 'roles' property value.
111 protected function _init() {
112 global $wpdb, $wp_user_roles;
113 $this->role_key = $wpdb->get_blog_prefix() . 'user_roles';
114 if ( ! empty( $wp_user_roles ) ) {
115 $this->roles = $wp_user_roles;
116 $this->use_db = false;
118 $this->roles = get_option( $this->role_key );
121 if ( empty( $this->roles ) )
124 $this->role_objects = array();
125 $this->role_names = array();
126 foreach ( array_keys( $this->roles ) as $role ) {
127 $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
128 $this->role_names[$role] = $this->roles[$role]['name'];
133 * Reinitialize the object
135 * Recreates the role objects. This is typically called only by switch_to_blog()
136 * after switching wpdb to a new site ID.
141 * @global wpdb $wpdb WordPress database abstraction object.
143 public function reinit() {
144 // There is no need to reinit if using the wp_user_roles global.
145 if ( ! $this->use_db )
150 // Duplicated from _init() to avoid an extra function call.
151 $this->role_key = $wpdb->get_blog_prefix() . 'user_roles';
152 $this->roles = get_option( $this->role_key );
153 if ( empty( $this->roles ) )
156 $this->role_objects = array();
157 $this->role_names = array();
158 foreach ( array_keys( $this->roles ) as $role ) {
159 $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
160 $this->role_names[$role] = $this->roles[$role]['name'];
165 * Add role name with capabilities to list.
167 * Updates the list of roles, if the role doesn't already exist.
169 * The capabilities are defined in the following format `array( 'read' => true );`
170 * To explicitly deny a role a capability you set the value for that capability to false.
175 * @param string $role Role name.
176 * @param string $display_name Role display name.
177 * @param array $capabilities List of role capabilities in the above format.
178 * @return WP_Role|void WP_Role object, if role is added.
180 public function add_role( $role, $display_name, $capabilities = array() ) {
181 if ( empty( $role ) || isset( $this->roles[ $role ] ) ) {
185 $this->roles[$role] = array(
186 'name' => $display_name,
187 'capabilities' => $capabilities
190 update_option( $this->role_key, $this->roles );
191 $this->role_objects[$role] = new WP_Role( $role, $capabilities );
192 $this->role_names[$role] = $display_name;
193 return $this->role_objects[$role];
197 * Remove role by name.
202 * @param string $role Role name.
204 public function remove_role( $role ) {
205 if ( ! isset( $this->role_objects[$role] ) )
208 unset( $this->role_objects[$role] );
209 unset( $this->role_names[$role] );
210 unset( $this->roles[$role] );
213 update_option( $this->role_key, $this->roles );
215 if ( get_option( 'default_role' ) == $role )
216 update_option( 'default_role', 'subscriber' );
220 * Add capability to role.
225 * @param string $role Role name.
226 * @param string $cap Capability name.
227 * @param bool $grant Optional, default is true. Whether role is capable of performing capability.
229 public function add_cap( $role, $cap, $grant = true ) {
230 if ( ! isset( $this->roles[$role] ) )
233 $this->roles[$role]['capabilities'][$cap] = $grant;
235 update_option( $this->role_key, $this->roles );
239 * Remove capability from role.
244 * @param string $role Role name.
245 * @param string $cap Capability name.
247 public function remove_cap( $role, $cap ) {
248 if ( ! isset( $this->roles[$role] ) )
251 unset( $this->roles[$role]['capabilities'][$cap] );
253 update_option( $this->role_key, $this->roles );
257 * Retrieve role object by name.
262 * @param string $role Role name.
263 * @return WP_Role|null WP_Role object if found, null if the role does not exist.
265 public function get_role( $role ) {
266 if ( isset( $this->role_objects[$role] ) )
267 return $this->role_objects[$role];
273 * Retrieve list of role names.
278 * @return array List of role names.
280 public function get_names() {
281 return $this->role_names;
285 * Whether role name is currently in the list of available roles.
290 * @param string $role Role name to look up.
293 public function is_role( $role ) {
294 return isset( $this->role_names[$role] );