105 } |
106 } |
106 return false; |
107 return false; |
107 } |
108 } |
108 |
109 |
109 /** |
110 /** |
110 * Set up the object properties. |
111 * Sets up the object properties. |
111 * |
112 * |
112 * The role key is set to the current prefix for the $wpdb object with |
113 * The role key is set to the current prefix for the $wpdb object with |
113 * 'user_roles' appended. If the $wp_user_roles global is set, then it will |
114 * 'user_roles' appended. If the $wp_user_roles global is set, then it will |
114 * be used and the role option will not be updated or used. |
115 * be used and the role option will not be updated or used. |
115 * |
116 * |
136 |
137 |
137 $this->for_site(); |
138 $this->for_site(); |
138 } |
139 } |
139 |
140 |
140 /** |
141 /** |
141 * Add role name with capabilities to list. |
142 * Adds a role name with capabilities to the list. |
142 * |
143 * |
143 * Updates the list of roles, if the role doesn't already exist. |
144 * Updates the list of roles, if the role doesn't already exist. |
144 * |
145 * |
145 * The capabilities are defined in the following format `array( 'read' => true );` |
146 * The capabilities are defined in the following format: `array( 'read' => true )`. |
146 * To explicitly deny a role a capability you set the value for that capability to false. |
147 * To explicitly deny the role a capability, set the value for that capability to false. |
147 * |
148 * |
148 * @since 2.0.0 |
149 * @since 2.0.0 |
149 * |
150 * |
150 * @param string $role Role name. |
151 * @param string $role Role name. |
151 * @param string $display_name Role display name. |
152 * @param string $display_name Role display name. |
152 * @param bool[] $capabilities List of capabilities keyed by the capability name, |
153 * @param bool[] $capabilities Optional. List of capabilities keyed by the capability name, |
153 * e.g. array( 'edit_posts' => true, 'delete_posts' => false ). |
154 * e.g. `array( 'edit_posts' => true, 'delete_posts' => false )`. |
154 * @return WP_Role|void WP_Role object, if role is added. |
155 * Default empty array. |
|
156 * @return WP_Role|void WP_Role object, if the role is added. |
155 */ |
157 */ |
156 public function add_role( $role, $display_name, $capabilities = array() ) { |
158 public function add_role( $role, $display_name, $capabilities = array() ) { |
157 if ( empty( $role ) || isset( $this->roles[ $role ] ) ) { |
159 if ( empty( $role ) || isset( $this->roles[ $role ] ) ) { |
158 return; |
160 return; |
159 } |
161 } |
188 |
190 |
189 if ( $this->use_db ) { |
191 if ( $this->use_db ) { |
190 update_option( $this->role_key, $this->roles ); |
192 update_option( $this->role_key, $this->roles ); |
191 } |
193 } |
192 |
194 |
193 if ( get_option( 'default_role' ) == $role ) { |
195 if ( get_option( 'default_role' ) === $role ) { |
194 update_option( 'default_role', 'subscriber' ); |
196 update_option( 'default_role', 'subscriber' ); |
195 } |
197 } |
196 } |
198 } |
197 |
199 |
198 /** |
200 /** |
199 * Add capability to role. |
201 * Adds a capability to role. |
200 * |
202 * |
201 * @since 2.0.0 |
203 * @since 2.0.0 |
202 * |
204 * |
203 * @param string $role Role name. |
205 * @param string $role Role name. |
204 * @param string $cap Capability name. |
206 * @param string $cap Capability name. |
234 update_option( $this->role_key, $this->roles ); |
236 update_option( $this->role_key, $this->roles ); |
235 } |
237 } |
236 } |
238 } |
237 |
239 |
238 /** |
240 /** |
239 * Retrieve role object by name. |
241 * Retrieves a role object by name. |
240 * |
242 * |
241 * @since 2.0.0 |
243 * @since 2.0.0 |
242 * |
244 * |
243 * @param string $role Role name. |
245 * @param string $role Role name. |
244 * @return WP_Role|null WP_Role object if found, null if the role does not exist. |
246 * @return WP_Role|null WP_Role object if found, null if the role does not exist. |
250 return null; |
252 return null; |
251 } |
253 } |
252 } |
254 } |
253 |
255 |
254 /** |
256 /** |
255 * Retrieve list of role names. |
257 * Retrieves a list of role names. |
256 * |
258 * |
257 * @since 2.0.0 |
259 * @since 2.0.0 |
258 * |
260 * |
259 * @return string[] List of role names. |
261 * @return string[] List of role names. |
260 */ |
262 */ |
261 public function get_names() { |
263 public function get_names() { |
262 return $this->role_names; |
264 return $this->role_names; |
263 } |
265 } |
264 |
266 |
265 /** |
267 /** |
266 * Whether role name is currently in the list of available roles. |
268 * Determines whether a role name is currently in the list of available roles. |
267 * |
269 * |
268 * @since 2.0.0 |
270 * @since 2.0.0 |
269 * |
271 * |
270 * @param string $role Role name to look up. |
272 * @param string $role Role name to look up. |
271 * @return bool |
273 * @return bool |
290 $this->role_objects[ $role ] = new WP_Role( $role, $this->roles[ $role ]['capabilities'] ); |
292 $this->role_objects[ $role ] = new WP_Role( $role, $this->roles[ $role ]['capabilities'] ); |
291 $this->role_names[ $role ] = $this->roles[ $role ]['name']; |
293 $this->role_names[ $role ] = $this->roles[ $role ]['name']; |
292 } |
294 } |
293 |
295 |
294 /** |
296 /** |
295 * After the roles have been initialized, allow plugins to add their own roles. |
297 * Fires after the roles have been initialized, allowing plugins to add their own roles. |
296 * |
298 * |
297 * @since 4.7.0 |
299 * @since 4.7.0 |
298 * |
300 * |
299 * @param WP_Roles $wp_roles A reference to the WP_Roles object. |
301 * @param WP_Roles $wp_roles A reference to the WP_Roles object. |
300 */ |
302 */ |
355 |
357 |
356 if ( ! empty( $wp_user_roles ) ) { |
358 if ( ! empty( $wp_user_roles ) ) { |
357 return $wp_user_roles; |
359 return $wp_user_roles; |
358 } |
360 } |
359 |
361 |
360 if ( is_multisite() && get_current_blog_id() != $this->site_id ) { |
362 if ( is_multisite() && get_current_blog_id() !== $this->site_id ) { |
361 remove_action( 'switch_blog', 'wp_switch_roles_and_user', 1 ); |
363 remove_action( 'switch_blog', 'wp_switch_roles_and_user', 1 ); |
362 |
364 |
363 $roles = get_blog_option( $this->site_id, $this->role_key, array() ); |
365 $roles = get_blog_option( $this->site_id, $this->role_key, array() ); |
364 |
366 |
365 add_action( 'switch_blog', 'wp_switch_roles_and_user', 1, 2 ); |
367 add_action( 'switch_blog', 'wp_switch_roles_and_user', 1, 2 ); |