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 +135 -145 2.2.12.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,16 +89,14 @@
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 100 'read_hidden_forums' => true,
103 101
104 102 // Topic caps
@@ -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
@@ -223,41 +186,21 @@
223 186
224 187 // Primary caps
225 188 'spectate' => true,
226 189 'participate' => true,
227 - 'moderate' => false,
228 - 'throttle' => false,
229 - 'view_trash' => false,
230 190
231 191 // Forum caps
232 - 'publish_forums' => false,
233 - 'edit_forums' => false,
234 - 'edit_others_forums' => false,
235 - 'delete_forums' => false,
236 - 'delete_others_forums' => false,
237 192 'read_private_forums' => true,
238 - 'read_hidden_forums' => false,
239 193
240 194 // Topic caps
241 195 'publish_topics' => true,
242 196 'edit_topics' => true,
243 - 'edit_others_topics' => false,
244 - 'delete_topics' => false,
245 - 'delete_others_topics' => false,
246 - 'read_private_topics' => false,
247 197
248 198 // Reply caps
249 199 'publish_replies' => true,
250 200 'edit_replies' => true,
251 - 'edit_others_replies' => false,
252 - 'delete_replies' => false,
253 - 'delete_others_replies' => false,
254 - 'read_private_replies' => false,
255 201
256 202 // Topic tag caps
257 - 'manage_topic_tags' => false,
258 - 'edit_topic_tags' => false,
259 - 'delete_topic_tags' => false,
260 203 'assign_topic_tags' => true,
261 204 );
262 205
263 206 break;
@@ -262,8 +205,9 @@
262 205
263 206 break;
264 207 }
265 208
209 + // Filter & return
266 210 return apply_filters( 'bbp_get_caps_for_role', $caps, $role );
267 211 }
268 212
269 213 /**
@@ -268,14 +212,14 @@
268 212
269 213 /**
270 214 * Adds capabilities to WordPress user roles.
271 215 *
272 - * @since bbPress (r2608)
216 + * @since 2.0.0 bbPress (r2608)
273 217 */
274 218 function bbp_add_caps() {
275 219
276 220 // Loop through available roles and add caps
277 - foreach( bbp_get_wp_roles()->role_objects as $role ) {
221 + foreach ( bbp_get_wp_roles()->role_objects as $role ) {
278 222 foreach ( bbp_get_caps_for_role( $role->name ) as $cap => $value ) {
279 223 $role->add_cap( $cap, $value );
280 224 }
281 225 }
@@ -285,14 +229,14 @@
285 229
286 230 /**
287 231 * Removes capabilities from WordPress user roles.
288 232 *
289 - * @since bbPress (r2608)
233 + * @since 2.0.0 bbPress (r2608)
290 234 */
291 235 function bbp_remove_caps() {
292 236
293 237 // Loop through available roles and remove caps
294 - foreach( bbp_get_wp_roles()->role_objects as $role ) {
238 + foreach ( bbp_get_wp_roles()->role_objects as $role ) {
295 239 foreach ( array_keys( bbp_get_caps_for_role( $role->name ) ) as $cap ) {
296 240 $role->remove_cap( $cap );
297 241 }
298 242 }
@@ -300,23 +244,28 @@
300 244 do_action( 'bbp_remove_caps' );
301 245 }
302 246
303 247 /**
304 - * Get the $wp_roles global without needing to declare it everywhere
248 + * Get the available roles, minus the dynamic roles that come with bbPress
305 249 *
306 - * @since bbPress (r4293)
250 + * @since 2.4.0 bbPress (r5064)
307 251 *
308 - * @global WP_Roles $wp_roles
309 - * @return WP_Roles
252 + * @return array
310 253 */
311 -function bbp_get_wp_roles() {
312 - global $wp_roles;
254 +function bbp_get_blog_roles() {
313 255
314 - // Load roles if not set
315 - if ( ! isset( $wp_roles ) )
316 - $wp_roles = new WP_Roles();
256 + // Get WordPress's roles (returns $wp_roles global)
257 + $wp_roles = bbp_get_wp_roles();
317 258
318 - 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 );
319 268 }
320 269
321 270 /** Forum Roles ***************************************************************/
322 271
@@ -324,34 +273,54 @@
324 273 * Add the bbPress roles to the $wp_roles global.
325 274 *
326 275 * We do this to avoid adding these values to the database.
327 276 *
328 - * @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
329 285 */
330 -function bbp_add_forums_roles() {
331 - $wp_roles = bbp_get_wp_roles();
286 +function bbp_add_forums_roles( $wp_roles = null ) {
332 287
333 - foreach( bbp_get_dynamic_roles() as $role_id => $details ) {
334 - $wp_roles->roles[$role_id] = $details;
335 - $wp_roles->role_objects[$role_id] = new WP_Role( $details['name'], $details['capabilities'] );
336 - $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();
337 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;
338 310 }
339 311
340 312 /**
341 313 * Helper function to add filter to option_wp_user_roles
342 314 *
343 - * @since bbPress (r4363)
315 + * @since 2.2.0 bbPress (r4363)
316 + * @deprecated 2.6.0 bbPress (r6105)
344 317 *
345 318 * @see _bbp_reinit_dynamic_roles()
346 - *
347 - * @global WPDB $wpdb Used to get the database prefix
348 319 */
349 320 function bbp_filter_user_roles_option() {
350 - global $wpdb;
321 + $role_key = bbp_db()->prefix . 'user_roles';
351 322
352 - $role_key = $wpdb->prefix . 'user_roles';
353 -
354 323 add_filter( 'option_' . $role_key, '_bbp_reinit_dynamic_roles' );
355 324 }
356 325
357 326 /**
@@ -362,13 +331,17 @@
362 331 *
363 332 * Because dynamic multiple roles is a new concept in WordPress, we work around
364 333 * it here for now, knowing that improvements will come to WordPress core later.
365 334 *
366 - * @see switch_to_blog()
367 - * @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()
368 340 * @see WP_Roles::_init()
369 341 *
370 - * @since bbPress (r4363)
342 + * @since 2.2.0 bbPress (r4363)
343 + * @deprecated 2.6.0 bbPress (r6105)
371 344 *
372 345 * @internal Used by bbPress to reinitialize dynamic roles on blog switch
373 346 *
374 347 * @param array $roles
@@ -374,10 +347,10 @@
374 347 * @param array $roles
375 348 * @return array Combined array of database roles and dynamic bbPress roles
376 349 */
377 350 function _bbp_reinit_dynamic_roles( $roles = array() ) {
378 - foreach( bbp_get_dynamic_roles() as $role_id => $details ) {
379 - $roles[$role_id] = $details;
351 + foreach ( bbp_get_dynamic_roles() as $role_id => $details ) {
352 + $roles[ $role_id ] = $details;
380 353 }
381 354 return $roles;
382 355 }
383 356
@@ -393,45 +366,45 @@
393 366 * capability can edit others to be administrators, even if they are
394 367 * only editors or authors. This filter allows admins to delegate
395 368 * user management.
396 369 *
397 - * @since bbPress (r4284)
370 + * @since 2.2.0 bbPress (r4284)
371 + * @since 2.6.0 bbPress (r6117) Use bbpress()->roles
398 372 *
399 373 * @return array
400 374 */
401 375 function bbp_get_dynamic_roles() {
402 - return (array) apply_filters( 'bbp_get_dynamic_roles', array(
403 376
404 - // Keymaster
405 - bbp_get_keymaster_role() => array(
406 - 'name' => __( 'Keymaster', 'bbpress' ),
407 - 'capabilities' => bbp_get_caps_for_role( bbp_get_keymaster_role() )
408 - ),
377 + // Defaults
378 + $to_array = array();
379 + $roles = bbpress()->roles;
409 380
410 - // Moderator
411 - bbp_get_moderator_role() => array(
412 - 'name' => __( 'Moderator', 'bbpress' ),
413 - 'capabilities' => bbp_get_caps_for_role( bbp_get_moderator_role() )
414 - ),
381 + // Convert WP_Roles objects to arrays
382 + foreach ( $roles as $role_id => $wp_role ) {
383 + $to_array[ $role_id ] = (array) $wp_role;
384 + }
415 385
416 - // Participant
417 - bbp_get_participant_role() => array(
418 - 'name' => __( 'Participant', 'bbpress' ),
419 - 'capabilities' => bbp_get_caps_for_role( bbp_get_participant_role() )
420 - ),
386 + // Filter & return
387 + return (array) apply_filters( 'bbp_get_dynamic_roles', $to_array, $roles );
388 +}
421 389
422 - // Spectator
423 - bbp_get_spectator_role() => array(
424 - 'name' => __( 'Spectator', 'bbpress' ),
425 - 'capabilities' => bbp_get_caps_for_role( bbp_get_spectator_role() )
426 - ),
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 + : '';
427 404
428 - // Blocked
429 - bbp_get_blocked_role() => array(
430 - 'name' => __( 'Blocked', 'bbpress' ),
431 - 'capabilities' => bbp_get_caps_for_role( bbp_get_blocked_role() )
432 - )
433 - ) );
405 + // Filter & return
406 + return apply_filters( 'bbp_get_dynamic_role_name', $role, $role_id, $roles );
434 407 }
435 408
436 409 /**
437 410 * Removes the bbPress roles from the editable roles array
@@ -438,12 +411,12 @@
438 411 *
439 412 * This used to use array_diff_assoc() but it randomly broke before 2.2 release.
440 413 * Need to research what happened, and if there's a way to speed this up.
441 414 *
442 - * @since bbPress (r4303)
415 + * @since 2.2.0 bbPress (r4303)
443 416 *
444 417 * @param array $all_roles All registered roles
445 - * @return array
418 + * @return array
446 419 */
447 420 function bbp_filter_blog_editable_roles( $all_roles = array() ) {
448 421
449 422 // Loop through bbPress roles
@@ -452,10 +425,10 @@
452 425 // Loop through WordPress roles
453 426 foreach ( array_keys( $all_roles ) as $wp_role ) {
454 427
455 428 // If keys match, unset
456 - if ( $wp_role == $bbp_role ) {
457 - unset( $all_roles[$wp_role] );
429 + if ( $wp_role === $bbp_role ) {
430 + unset( $all_roles[ $wp_role ] );
458 431 }
459 432 }
460 433 }
461 434
@@ -464,14 +437,15 @@
464 437
465 438 /**
466 439 * The keymaster role for bbPress users
467 440 *
468 - * @since bbPress (r4284)
441 + * @since 2.2.0 bbPress (r4284)
469 442 *
470 - * @uses apply_filters() Allow override of hardcoded keymaster role
471 443 * @return string
472 444 */
473 445 function bbp_get_keymaster_role() {
446 +
447 + // Filter & return
474 448 return apply_filters( 'bbp_get_keymaster_role', 'bbp_keymaster' );
475 449 }
476 450
477 451 /**
@@ -476,14 +450,15 @@
476 450
477 451 /**
478 452 * The moderator role for bbPress users
479 453 *
480 - * @since bbPress (r3410)
454 + * @since 2.0.0 bbPress (r3410)
481 455 *
482 - * @uses apply_filters() Allow override of hardcoded moderator role
483 456 * @return string
484 457 */
485 458 function bbp_get_moderator_role() {
459 +
460 + // Filter & return
486 461 return apply_filters( 'bbp_get_moderator_role', 'bbp_moderator' );
487 462 }
488 463
489 464 /**
@@ -488,14 +463,15 @@
488 463
489 464 /**
490 465 * The participant role for registered user that can participate in forums
491 466 *
492 - * @since bbPress (r3410)
467 + * @since 2.0.0 bbPress (r3410)
493 468 *
494 - * @uses apply_filters() Allow override of hardcoded participant role
495 469 * @return string
496 470 */
497 471 function bbp_get_participant_role() {
472 +
473 + // Filter & return
498 474 return apply_filters( 'bbp_get_participant_role', 'bbp_participant' );
499 475 }
500 476
501 477 /**
@@ -500,14 +476,15 @@
500 476
501 477 /**
502 478 * The spectator role is for registered users without any capabilities
503 479 *
504 - * @since bbPress (r3860)
480 + * @since 2.1.0 bbPress (r3860)
505 481 *
506 - * @uses apply_filters() Allow override of hardcoded spectator role
507 482 * @return string
508 483 */
509 484 function bbp_get_spectator_role() {
485 +
486 + // Filter & return
510 487 return apply_filters( 'bbp_get_spectator_role', 'bbp_spectator' );
511 488 }
512 489
513 490 /**
@@ -512,34 +489,47 @@
512 489
513 490 /**
514 491 * The blocked role is for registered users that cannot spectate or participate
515 492 *
516 - * @since bbPress (r4284)
493 + * @since 2.2.0 bbPress (r4284)
517 494 *
518 - * @uses apply_filters() Allow override of hardcoded blocked role
519 495 * @return string
520 496 */
521 497 function bbp_get_blocked_role() {
498 +
499 + // Filter & return
522 500 return apply_filters( 'bbp_get_blocked_role', 'bbp_blocked' );
523 501 }
524 502
525 -/** Deprecated ****************************************************************/
526 -
527 503 /**
528 504 * Adds bbPress-specific user roles.
529 505 *
530 - * @since bbPress (r2741)
531 - * @deprecated since version 2.2
506 + * @since 2.0.0 bbPress (r2741)
507 + *
508 + * @deprecated 2.2.0 bbPress (r4164)
532 509 */
533 510 function bbp_add_roles() {
534 - _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' );
535 512 }
536 513
537 514 /**
538 - * Removes bbPress-specific user roles.
515 + * Removes bbPress-specific user roles from the `wp_user_roles` array.
539 516 *
540 - * @since bbPress (r2741)
541 - * @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)
542 524 */
543 525 function bbp_remove_roles() {
544 - _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' );
545 535 }