PluginProbe
bbPress / 2.6.17
bbPress v2.6.17
2.6.17 trunk 2.0 2.0-beta-1 2.0-beta-2b 2.0-beta-3 2.0-beta-3b 2.0-rc-2 2.0-rc-3 2.0-rc-4 2.0-rc-5 2.0.1 2.0.2 2.0.3 2.1 2.1-beta-1 2.1-rc1 2.1-rc2 2.1-rc3 2.1-rc4 2.1.1 2.1.2 2.1.3 2.2 2.2.1 All 72 releases
← All changes | includes/core/capabilities.php +136 -165 2.22.6.17 View file →
@@ -11,9 +11,9 @@
11 11 * @subpackage Capabilities
12 12 */
13 13
14 14 // Exit if accessed directly
15 -if ( !defined( 'ABSPATH' ) ) exit;
15 +defined( 'ABSPATH' ) || exit;
16 16
17 17 /** Mapping *******************************************************************/
18 18
19 19 /**
@@ -18,14 +18,13 @@
18 18
19 19 /**
20 20 * Returns an array of capabilities based on the role that is being requested.
21 21 *
22 - * @since bbPress (r2994)
22 + * @since 2.0.0 bbPress (r2994)
23 23 *
24 24 * @todo Map all of these and deprecate
25 25 *
26 26 * @param string $role Optional. Defaults to The role to load caps for
27 - * @uses apply_filters() Allow return value to be filtered
28 27 *
29 28 * @return array Capabilities for $role
30 29 */
31 30 function bbp_get_caps_for_role( $role = '' ) {
@@ -45,8 +44,9 @@
45 44 'participate' => true,
46 45 'moderate' => true,
47 46 'throttle' => true,
48 47 'view_trash' => true,
48 + 'assign_moderators' => true,
49 49
50 50 // Forum caps
51 51 'publish_forums' => true,
52 52 'edit_forums' => true,
@@ -89,18 +89,16 @@
89 89 'spectate' => true,
90 90 'participate' => true,
91 91 'moderate' => true,
92 92 'throttle' => true,
93 - 'view_trash' => false,
93 + 'view_trash' => true,
94 + 'assign_moderators' => true,
94 95
95 96 // Forum caps
96 97 'publish_forums' => true,
97 98 'edit_forums' => true,
98 - 'edit_others_forums' => false,
99 - 'delete_forums' => false,
100 - 'delete_others_forums' => false,
101 99 'read_private_forums' => true,
102 - 'read_hidden_forums' => false,
100 + 'read_hidden_forums' => true,
103 101
104 102 // Topic caps
105 103 'publish_topics' => true,
106 104 'edit_topics' => true,
@@ -131,43 +129,8 @@
131 129 $caps = array(
132 130
133 131 // Primary caps
134 132 'spectate' => true,
135 - 'participate' => false,
136 - 'moderate' => false,
137 - 'throttle' => false,
138 - 'view_trash' => false,
139 -
140 - // Forum caps
141 - 'publish_forums' => false,
142 - 'edit_forums' => false,
143 - 'edit_others_forums' => false,
144 - 'delete_forums' => false,
145 - 'delete_others_forums' => false,
146 - 'read_private_forums' => false,
147 - 'read_hidden_forums' => false,
148 -
149 - // Topic caps
150 - 'publish_topics' => false,
151 - 'edit_topics' => false,
152 - 'edit_others_topics' => false,
153 - 'delete_topics' => false,
154 - 'delete_others_topics' => false,
155 - 'read_private_topics' => false,
156 -
157 - // Reply caps
158 - 'publish_replies' => false,
159 - 'edit_replies' => false,
160 - 'edit_others_replies' => false,
161 - 'delete_replies' => false,
162 - 'delete_others_replies' => false,
163 - 'read_private_replies' => false,
164 -
165 - // Topic tag caps
166 - 'manage_topic_tags' => false,
167 - 'edit_topic_tags' => false,
168 - 'delete_topic_tags' => false,
169 - 'assign_topic_tags' => false,
170 133 );
171 134
172 135 break;
173 136
@@ -216,9 +179,8 @@
216 179
217 180 break;
218 181
219 182 // Participant/Default
220 - case bbp_get_visitor_role() :
221 183 case bbp_get_participant_role() :
222 184 default :
223 185 $caps = array(
224 186
@@ -224,41 +186,21 @@
224 186
225 187 // Primary caps
226 188 'spectate' => true,
227 189 'participate' => true,
228 - 'moderate' => false,
229 - 'throttle' => false,
230 - 'view_trash' => false,
231 190
232 191 // Forum caps
233 - 'publish_forums' => false,
234 - 'edit_forums' => false,
235 - 'edit_others_forums' => false,
236 - 'delete_forums' => false,
237 - 'delete_others_forums' => false,
238 192 'read_private_forums' => true,
239 - 'read_hidden_forums' => false,
240 193
241 194 // Topic caps
242 195 'publish_topics' => true,
243 196 'edit_topics' => true,
244 - 'edit_others_topics' => false,
245 - 'delete_topics' => false,
246 - 'delete_others_topics' => false,
247 - 'read_private_topics' => false,
248 197
249 198 // Reply caps
250 199 'publish_replies' => true,
251 200 'edit_replies' => true,
252 - 'edit_others_replies' => false,
253 - 'delete_replies' => false,
254 - 'delete_others_replies' => false,
255 - 'read_private_replies' => false,
256 201
257 202 // Topic tag caps
258 - 'manage_topic_tags' => false,
259 - 'edit_topic_tags' => false,
260 - 'delete_topic_tags' => false,
261 203 'assign_topic_tags' => true,
262 204 );
263 205
264 206 break;
@@ -263,8 +205,9 @@
263 205
264 206 break;
265 207 }
266 208
209 + // Filter & return
267 210 return apply_filters( 'bbp_get_caps_for_role', $caps, $role );
268 211 }
269 212
270 213 /**
@@ -269,14 +212,14 @@
269 212
270 213 /**
271 214 * Adds capabilities to WordPress user roles.
272 215 *
273 - * @since bbPress (r2608)
216 + * @since 2.0.0 bbPress (r2608)
274 217 */
275 218 function bbp_add_caps() {
276 219
277 220 // Loop through available roles and add caps
278 - foreach( bbp_get_wp_roles()->role_objects as $role ) {
221 + foreach ( bbp_get_wp_roles()->role_objects as $role ) {
279 222 foreach ( bbp_get_caps_for_role( $role->name ) as $cap => $value ) {
280 223 $role->add_cap( $cap, $value );
281 224 }
282 225 }
@@ -286,14 +229,14 @@
286 229
287 230 /**
288 231 * Removes capabilities from WordPress user roles.
289 232 *
290 - * @since bbPress (r2608)
233 + * @since 2.0.0 bbPress (r2608)
291 234 */
292 235 function bbp_remove_caps() {
293 236
294 237 // Loop through available roles and remove caps
295 - foreach( bbp_get_wp_roles()->role_objects as $role ) {
238 + foreach ( bbp_get_wp_roles()->role_objects as $role ) {
296 239 foreach ( array_keys( bbp_get_caps_for_role( $role->name ) ) as $cap ) {
297 240 $role->remove_cap( $cap );
298 241 }
299 242 }
@@ -301,23 +244,28 @@
301 244 do_action( 'bbp_remove_caps' );
302 245 }
303 246
304 247 /**
305 - * Get the $wp_roles global without needing to declare it everywhere
248 + * Get the available roles, minus the dynamic roles that come with bbPress
306 249 *
307 - * @since bbPress (r4293)
250 + * @since 2.4.0 bbPress (r5064)
308 251 *
309 - * @global WP_Roles $wp_roles
310 - * @return WP_Roles
252 + * @return array
311 253 */
312 -function bbp_get_wp_roles() {
313 - global $wp_roles;
254 +function bbp_get_blog_roles() {
314 255
315 - // Load roles if not set
316 - if ( ! isset( $wp_roles ) )
317 - $wp_roles = new WP_Roles();
256 + // Get WordPress's roles (returns $wp_roles global)
257 + $wp_roles = bbp_get_wp_roles();
318 258
319 - return $wp_roles;
259 + // Apply the WordPress 'editable_roles' filter to let plugins ride along.
260 + //
261 + // We use this internally via bbp_filter_blog_editable_roles() to remove
262 + // any custom bbPress roles that are added to the global.
263 + $the_roles = isset( $wp_roles->roles ) ? $wp_roles->roles : false;
264 + $all_roles = apply_filters( 'editable_roles', $the_roles );
265 +
266 + // Filter & return
267 + return apply_filters( 'bbp_get_blog_roles', $all_roles, $wp_roles );
320 268 }
321 269
322 270 /** Forum Roles ***************************************************************/
323 271
@@ -325,34 +273,54 @@
325 273 * Add the bbPress roles to the $wp_roles global.
326 274 *
327 275 * We do this to avoid adding these values to the database.
328 276 *
329 - * @since bbPress (r4290)
277 + * Note: bbPress is purposely assertive here, overwriting any keys & values
278 + * that may already exist in the $wp_roles array.
279 + *
280 + * @since 2.2.0 bbPress (r4290)
281 + *
282 + * @param WP_Roles $wp_roles The array of WP_Role objects that was initialized
283 + *
284 + * @return WP_Roles The main $wp_roles global
330 285 */
331 -function bbp_add_forums_roles() {
332 - $wp_roles = bbp_get_wp_roles();
286 +function bbp_add_forums_roles( $wp_roles = null ) {
333 287
334 - foreach( bbp_get_dynamic_roles() as $role_id => $details ) {
335 - $wp_roles->roles[$role_id] = $details;
336 - $wp_roles->role_objects[$role_id] = new WP_Role( $details['name'], $details['capabilities'] );
337 - $wp_roles->role_names[$role_id] = $details['name'];
288 + // Maybe initialize WP_Roles just-in-time, but
289 + if ( empty( $wp_roles ) && ! doing_action( 'wp_roles_init' ) ) {
290 + $wp_roles = wp_roles();
338 291 }
292 +
293 + // Bail if unexpected param type
294 + if ( ! is_a( $wp_roles, 'WP_Roles' ) ) {
295 + return;
296 + }
297 +
298 + // Get the dynamic roles
299 + $bbp_roles = bbp_get_dynamic_roles();
300 +
301 + // Loop through dynamic roles and add them to the $wp_roles array
302 + foreach ( $bbp_roles as $role_id => $details ) {
303 + $wp_roles->roles[ $role_id ] = $details;
304 + $wp_roles->role_objects[ $role_id ] = new WP_Role( $role_id, $details['capabilities'] );
305 + $wp_roles->role_names[ $role_id ] = $details['name'];
306 + }
307 +
308 + // Return the modified $wp_roles array
309 + return $wp_roles;
339 310 }
340 311
341 312 /**
342 313 * Helper function to add filter to option_wp_user_roles
343 314 *
344 - * @since bbPress (r4363)
315 + * @since 2.2.0 bbPress (r4363)
316 + * @deprecated 2.6.0 bbPress (r6105)
345 317 *
346 318 * @see _bbp_reinit_dynamic_roles()
347 - *
348 - * @global WPDB $wpdb Used to get the database prefix
349 319 */
350 320 function bbp_filter_user_roles_option() {
351 - global $wpdb;
321 + $role_key = bbp_db()->prefix . 'user_roles';
352 322
353 - $role_key = $wpdb->prefix . 'user_roles';
354 -
355 323 add_filter( 'option_' . $role_key, '_bbp_reinit_dynamic_roles' );
356 324 }
357 325
358 326 /**
@@ -363,13 +331,17 @@
363 331 *
364 332 * Because dynamic multiple roles is a new concept in WordPress, we work around
365 333 * it here for now, knowing that improvements will come to WordPress core later.
366 334 *
367 - * @see switch_to_blog()
368 - * @see restore_current_blog()
335 + * Also note that if using the $wp_user_roles global non-database approach,
336 + * bbPress does not have an intercept point to add its dynamic roles.
337 + *
338 + * @see bbp_switch_to_site()
339 + * @see bbp_restore_current_site()
369 340 * @see WP_Roles::_init()
370 341 *
371 - * @since bbPress (r4363)
342 + * @since 2.2.0 bbPress (r4363)
343 + * @deprecated 2.6.0 bbPress (r6105)
372 344 *
373 345 * @internal Used by bbPress to reinitialize dynamic roles on blog switch
374 346 *
375 347 * @param array $roles
@@ -375,10 +347,10 @@
375 347 * @param array $roles
376 348 * @return array Combined array of database roles and dynamic bbPress roles
377 349 */
378 350 function _bbp_reinit_dynamic_roles( $roles = array() ) {
379 - foreach( bbp_get_dynamic_roles() as $role_id => $details ) {
380 - $roles[$role_id] = $details;
351 + foreach ( bbp_get_dynamic_roles() as $role_id => $details ) {
352 + $roles[ $role_id ] = $details;
381 353 }
382 354 return $roles;
383 355 }
384 356
@@ -394,51 +366,45 @@
394 366 * capability can edit others to be administrators, even if they are
395 367 * only editors or authors. This filter allows admins to delegate
396 368 * user management.
397 369 *
398 - * @since bbPress (r4284)
370 + * @since 2.2.0 bbPress (r4284)
371 + * @since 2.6.0 bbPress (r6117) Use bbpress()->roles
399 372 *
400 373 * @return array
401 374 */
402 375 function bbp_get_dynamic_roles() {
403 - return (array) apply_filters( 'bbp_get_dynamic_roles', array(
404 376
405 - // Keymaster
406 - bbp_get_keymaster_role() => array(
407 - 'name' => __( 'Keymaster', 'bbpress' ),
408 - 'capabilities' => bbp_get_caps_for_role( bbp_get_keymaster_role() )
409 - ),
377 + // Defaults
378 + $to_array = array();
379 + $roles = bbpress()->roles;
410 380
411 - // Moderator
412 - bbp_get_moderator_role() => array(
413 - 'name' => __( 'Moderator', 'bbpress' ),
414 - 'capabilities' => bbp_get_caps_for_role( bbp_get_moderator_role() )
415 - ),
381 + // Convert WP_Roles objects to arrays
382 + foreach ( $roles as $role_id => $wp_role ) {
383 + $to_array[ $role_id ] = (array) $wp_role;
384 + }
416 385
417 - // Participant
418 - bbp_get_participant_role() => array(
419 - 'name' => __( 'Participant', 'bbpress' ),
420 - 'capabilities' => bbp_get_caps_for_role( bbp_get_participant_role() )
421 - ),
386 + // Filter & return
387 + return (array) apply_filters( 'bbp_get_dynamic_roles', $to_array, $roles );
388 +}
422 389
423 - // Spectator
424 - bbp_get_spectator_role() => array(
425 - 'name' => __( 'Spectator', 'bbpress' ),
426 - 'capabilities' => bbp_get_caps_for_role( bbp_get_spectator_role() )
427 - ),
390 +/**
391 + * Gets a translated role name from a role ID
392 + *
393 + * @since 2.3.0 bbPress (r4792)
394 + * @since 2.6.0 bbPress (r6117) Use bbp_translate_user_role()
395 + *
396 + * @param string $role_id
397 + * @return string Translated role name
398 + */
399 +function bbp_get_dynamic_role_name( $role_id = '' ) {
400 + $roles = bbp_get_dynamic_roles();
401 + $role = isset( $roles[ $role_id ] )
402 + ? bbp_translate_user_role( $roles[ $role_id ]['name'] )
403 + : '';
428 404
429 - // Visitor
430 - bbp_get_visitor_role() => array(
431 - 'name' => __( 'Visitor', 'bbpress' ),
432 - 'capabilities' => bbp_get_caps_for_role( bbp_get_visitor_role() )
433 - ),
434 -
435 - // Blocked
436 - bbp_get_blocked_role() => array(
437 - 'name' => __( 'Blocked', 'bbpress' ),
438 - 'capabilities' => bbp_get_caps_for_role( bbp_get_blocked_role() )
439 - )
440 - ) );
405 + // Filter & return
406 + return apply_filters( 'bbp_get_dynamic_role_name', $role, $role_id, $roles );
441 407 }
442 408
443 409 /**
444 410 * Removes the bbPress roles from the editable roles array
@@ -445,12 +411,12 @@
445 411 *
446 412 * This used to use array_diff_assoc() but it randomly broke before 2.2 release.
447 413 * Need to research what happened, and if there's a way to speed this up.
448 414 *
449 - * @since bbPress (r4303)
415 + * @since 2.2.0 bbPress (r4303)
450 416 *
451 417 * @param array $all_roles All registered roles
452 - * @return array
418 + * @return array
453 419 */
454 420 function bbp_filter_blog_editable_roles( $all_roles = array() ) {
455 421
456 422 // Loop through bbPress roles
@@ -459,10 +425,10 @@
459 425 // Loop through WordPress roles
460 426 foreach ( array_keys( $all_roles ) as $wp_role ) {
461 427
462 428 // If keys match, unset
463 - if ( $wp_role == $bbp_role ) {
464 - unset( $all_roles[$wp_role] );
429 + if ( $wp_role === $bbp_role ) {
430 + unset( $all_roles[ $wp_role ] );
465 431 }
466 432 }
467 433 }
468 434
@@ -471,14 +437,15 @@
471 437
472 438 /**
473 439 * The keymaster role for bbPress users
474 440 *
475 - * @since bbPress (r4284)
441 + * @since 2.2.0 bbPress (r4284)
476 442 *
477 - * @uses apply_filters() Allow override of hardcoded keymaster role
478 443 * @return string
479 444 */
480 445 function bbp_get_keymaster_role() {
446 +
447 + // Filter & return
481 448 return apply_filters( 'bbp_get_keymaster_role', 'bbp_keymaster' );
482 449 }
483 450
484 451 /**
@@ -483,14 +450,15 @@
483 450
484 451 /**
485 452 * The moderator role for bbPress users
486 453 *
487 - * @since bbPress (r3410)
454 + * @since 2.0.0 bbPress (r3410)
488 455 *
489 - * @uses apply_filters() Allow override of hardcoded moderator role
490 456 * @return string
491 457 */
492 458 function bbp_get_moderator_role() {
459 +
460 + // Filter & return
493 461 return apply_filters( 'bbp_get_moderator_role', 'bbp_moderator' );
494 462 }
495 463
496 464 /**
@@ -495,14 +463,15 @@
495 463
496 464 /**
497 465 * The participant role for registered user that can participate in forums
498 466 *
499 - * @since bbPress (r3410)
467 + * @since 2.0.0 bbPress (r3410)
500 468 *
501 - * @uses apply_filters() Allow override of hardcoded participant role
502 469 * @return string
503 470 */
504 471 function bbp_get_participant_role() {
472 +
473 + // Filter & return
505 474 return apply_filters( 'bbp_get_participant_role', 'bbp_participant' );
506 475 }
507 476
508 477 /**
@@ -507,58 +476,60 @@
507 476
508 477 /**
509 478 * The spectator role is for registered users without any capabilities
510 479 *
511 - * @since bbPress (r3860)
480 + * @since 2.1.0 bbPress (r3860)
512 481 *
513 - * @uses apply_filters() Allow override of hardcoded spectator role
514 482 * @return string
515 483 */
516 484 function bbp_get_spectator_role() {
485 +
486 + // Filter & return
517 487 return apply_filters( 'bbp_get_spectator_role', 'bbp_spectator' );
518 488 }
519 489
520 490 /**
521 - * The visitor role for any registered user without a set forum role.
522 - *
523 - * @since bbPress (r3860)
524 - *
525 - * @uses apply_filters() Allow override of hardcoded visitor role
526 - * @return string
527 - */
528 -function bbp_get_visitor_role() {
529 - return apply_filters( 'bbp_get_visitor_role', 'bbp_visitor' );
530 -}
531 -
532 -/**
533 491 * The blocked role is for registered users that cannot spectate or participate
534 492 *
535 - * @since bbPress (r4284)
493 + * @since 2.2.0 bbPress (r4284)
536 494 *
537 - * @uses apply_filters() Allow override of hardcoded blocked role
538 495 * @return string
539 496 */
540 497 function bbp_get_blocked_role() {
498 +
499 + // Filter & return
541 500 return apply_filters( 'bbp_get_blocked_role', 'bbp_blocked' );
542 501 }
543 502
544 -/** Deprecated ****************************************************************/
545 -
546 503 /**
547 504 * Adds bbPress-specific user roles.
548 505 *
549 - * @since bbPress (r2741)
550 - * @deprecated since version 2.2
506 + * @since 2.0.0 bbPress (r2741)
507 + *
508 + * @deprecated 2.2.0 bbPress (r4164)
551 509 */
552 510 function bbp_add_roles() {
553 - _doing_it_wrong( 'bbp_add_roles', __( 'Editable forum roles no longer exist.', 'bbpress' ), '2.2' );
511 + _doing_it_wrong( 'bbp_add_roles', esc_html__( 'Editable forum roles no longer exist.', 'bbpress' ), '2.2' );
554 512 }
555 513
556 514 /**
557 - * Removes bbPress-specific user roles.
515 + * Removes bbPress-specific user roles from the `wp_user_roles` array.
558 516 *
559 - * @since bbPress (r2741)
560 - * @deprecated since version 2.2
517 + * This is currently only used when updating, uninstalling, or resetting bbPress.
518 + *
519 + * @see bbp_admin_reset_handler()
520 + * @see bbp_do_uninstall()
521 + * @see bbp_version_updater()
522 + *
523 + * @since 2.0.0 bbPress (r2741)
561 524 */
562 525 function bbp_remove_roles() {
563 - _doing_it_wrong( 'bbp_remove_roles', __( 'Editable forum roles no longer exist.', 'bbpress' ), '2.2' );
526 +
527 + // Remove the bbPress roles
528 + foreach ( array_keys( bbp_get_dynamic_roles() ) as $bbp_role ) {
529 + remove_role( $bbp_role );
530 + }
531 +
532 + // Some early adopters may have a deprecated visitor role. It was later
533 + // replaced by the Spectator role.
534 + remove_role( 'bbp_visitor' );
564 535 }