PluginProbe
bbPress / 2.6.6
bbPress v2.6.6
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 +125 -145 2.2.12.6.6 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,44 @@
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 + // Get the dynamic roles
289 + $bbp_roles = bbp_get_dynamic_roles();
290 +
291 + // Loop through dynamic roles and add them to the $wp_roles array
292 + foreach ( $bbp_roles as $role_id => $details ) {
293 + $wp_roles->roles[ $role_id ] = $details;
294 + $wp_roles->role_objects[ $role_id ] = new WP_Role( $role_id, $details['capabilities'] );
295 + $wp_roles->role_names[ $role_id ] = $details['name'];
337 296 }
297 +
298 + // Return the modified $wp_roles array
299 + return $wp_roles;
338 300 }
339 301
340 302 /**
341 303 * Helper function to add filter to option_wp_user_roles
342 304 *
343 - * @since bbPress (r4363)
305 + * @since 2.2.0 bbPress (r4363)
306 + * @deprecated 2.6.0 bbPress (r6105)
344 307 *
345 308 * @see _bbp_reinit_dynamic_roles()
346 - *
347 - * @global WPDB $wpdb Used to get the database prefix
348 309 */
349 310 function bbp_filter_user_roles_option() {
350 - global $wpdb;
311 + $role_key = bbp_db()->prefix . 'user_roles';
351 312
352 - $role_key = $wpdb->prefix . 'user_roles';
353 -
354 313 add_filter( 'option_' . $role_key, '_bbp_reinit_dynamic_roles' );
355 314 }
356 315
357 316 /**
@@ -362,13 +321,17 @@
362 321 *
363 322 * Because dynamic multiple roles is a new concept in WordPress, we work around
364 323 * it here for now, knowing that improvements will come to WordPress core later.
365 324 *
366 - * @see switch_to_blog()
367 - * @see restore_current_blog()
325 + * Also note that if using the $wp_user_roles global non-database approach,
326 + * bbPress does not have an intercept point to add its dynamic roles.
327 + *
328 + * @see bbp_switch_to_site()
329 + * @see bbp_restore_current_site()
368 330 * @see WP_Roles::_init()
369 331 *
370 - * @since bbPress (r4363)
332 + * @since 2.2.0 bbPress (r4363)
333 + * @deprecated 2.6.0 bbPress (r6105)
371 334 *
372 335 * @internal Used by bbPress to reinitialize dynamic roles on blog switch
373 336 *
374 337 * @param array $roles
@@ -374,10 +337,10 @@
374 337 * @param array $roles
375 338 * @return array Combined array of database roles and dynamic bbPress roles
376 339 */
377 340 function _bbp_reinit_dynamic_roles( $roles = array() ) {
378 - foreach( bbp_get_dynamic_roles() as $role_id => $details ) {
379 - $roles[$role_id] = $details;
341 + foreach ( bbp_get_dynamic_roles() as $role_id => $details ) {
342 + $roles[ $role_id ] = $details;
380 343 }
381 344 return $roles;
382 345 }
383 346
@@ -393,45 +356,45 @@
393 356 * capability can edit others to be administrators, even if they are
394 357 * only editors or authors. This filter allows admins to delegate
395 358 * user management.
396 359 *
397 - * @since bbPress (r4284)
360 + * @since 2.2.0 bbPress (r4284)
361 + * @since 2.6.0 bbPress (r6117) Use bbpress()->roles
398 362 *
399 363 * @return array
400 364 */
401 365 function bbp_get_dynamic_roles() {
402 - return (array) apply_filters( 'bbp_get_dynamic_roles', array(
403 366
404 - // Keymaster
405 - bbp_get_keymaster_role() => array(
406 - 'name' => __( 'Keymaster', 'bbpress' ),
407 - 'capabilities' => bbp_get_caps_for_role( bbp_get_keymaster_role() )
408 - ),
367 + // Defaults
368 + $to_array = array();
369 + $roles = bbpress()->roles;
409 370
410 - // Moderator
411 - bbp_get_moderator_role() => array(
412 - 'name' => __( 'Moderator', 'bbpress' ),
413 - 'capabilities' => bbp_get_caps_for_role( bbp_get_moderator_role() )
414 - ),
371 + // Convert WP_Roles objects to arrays
372 + foreach ( $roles as $role_id => $wp_role ) {
373 + $to_array[ $role_id ] = (array) $wp_role;
374 + }
415 375
416 - // Participant
417 - bbp_get_participant_role() => array(
418 - 'name' => __( 'Participant', 'bbpress' ),
419 - 'capabilities' => bbp_get_caps_for_role( bbp_get_participant_role() )
420 - ),
376 + // Filter & return
377 + return (array) apply_filters( 'bbp_get_dynamic_roles', $to_array, $roles );
378 +}
421 379
422 - // Spectator
423 - bbp_get_spectator_role() => array(
424 - 'name' => __( 'Spectator', 'bbpress' ),
425 - 'capabilities' => bbp_get_caps_for_role( bbp_get_spectator_role() )
426 - ),
380 +/**
381 + * Gets a translated role name from a role ID
382 + *
383 + * @since 2.3.0 bbPress (r4792)
384 + * @since 2.6.0 bbPress (r6117) Use bbp_translate_user_role()
385 + *
386 + * @param string $role_id
387 + * @return string Translated role name
388 + */
389 +function bbp_get_dynamic_role_name( $role_id = '' ) {
390 + $roles = bbp_get_dynamic_roles();
391 + $role = isset( $roles[ $role_id ] )
392 + ? bbp_translate_user_role( $roles[ $role_id ]['name'] )
393 + : '';
427 394
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 - ) );
395 + // Filter & return
396 + return apply_filters( 'bbp_get_dynamic_role_name', $role, $role_id, $roles );
434 397 }
435 398
436 399 /**
437 400 * Removes the bbPress roles from the editable roles array
@@ -438,12 +401,12 @@
438 401 *
439 402 * This used to use array_diff_assoc() but it randomly broke before 2.2 release.
440 403 * Need to research what happened, and if there's a way to speed this up.
441 404 *
442 - * @since bbPress (r4303)
405 + * @since 2.2.0 bbPress (r4303)
443 406 *
444 407 * @param array $all_roles All registered roles
445 - * @return array
408 + * @return array
446 409 */
447 410 function bbp_filter_blog_editable_roles( $all_roles = array() ) {
448 411
449 412 // Loop through bbPress roles
@@ -452,10 +415,10 @@
452 415 // Loop through WordPress roles
453 416 foreach ( array_keys( $all_roles ) as $wp_role ) {
454 417
455 418 // If keys match, unset
456 - if ( $wp_role == $bbp_role ) {
457 - unset( $all_roles[$wp_role] );
419 + if ( $wp_role === $bbp_role ) {
420 + unset( $all_roles[ $wp_role ] );
458 421 }
459 422 }
460 423 }
461 424
@@ -464,14 +427,15 @@
464 427
465 428 /**
466 429 * The keymaster role for bbPress users
467 430 *
468 - * @since bbPress (r4284)
431 + * @since 2.2.0 bbPress (r4284)
469 432 *
470 - * @uses apply_filters() Allow override of hardcoded keymaster role
471 433 * @return string
472 434 */
473 435 function bbp_get_keymaster_role() {
436 +
437 + // Filter & return
474 438 return apply_filters( 'bbp_get_keymaster_role', 'bbp_keymaster' );
475 439 }
476 440
477 441 /**
@@ -476,14 +440,15 @@
476 440
477 441 /**
478 442 * The moderator role for bbPress users
479 443 *
480 - * @since bbPress (r3410)
444 + * @since 2.0.0 bbPress (r3410)
481 445 *
482 - * @uses apply_filters() Allow override of hardcoded moderator role
483 446 * @return string
484 447 */
485 448 function bbp_get_moderator_role() {
449 +
450 + // Filter & return
486 451 return apply_filters( 'bbp_get_moderator_role', 'bbp_moderator' );
487 452 }
488 453
489 454 /**
@@ -488,14 +453,15 @@
488 453
489 454 /**
490 455 * The participant role for registered user that can participate in forums
491 456 *
492 - * @since bbPress (r3410)
457 + * @since 2.0.0 bbPress (r3410)
493 458 *
494 - * @uses apply_filters() Allow override of hardcoded participant role
495 459 * @return string
496 460 */
497 461 function bbp_get_participant_role() {
462 +
463 + // Filter & return
498 464 return apply_filters( 'bbp_get_participant_role', 'bbp_participant' );
499 465 }
500 466
501 467 /**
@@ -500,14 +466,15 @@
500 466
501 467 /**
502 468 * The spectator role is for registered users without any capabilities
503 469 *
504 - * @since bbPress (r3860)
470 + * @since 2.1.0 bbPress (r3860)
505 471 *
506 - * @uses apply_filters() Allow override of hardcoded spectator role
507 472 * @return string
508 473 */
509 474 function bbp_get_spectator_role() {
475 +
476 + // Filter & return
510 477 return apply_filters( 'bbp_get_spectator_role', 'bbp_spectator' );
511 478 }
512 479
513 480 /**
@@ -512,34 +479,47 @@
512 479
513 480 /**
514 481 * The blocked role is for registered users that cannot spectate or participate
515 482 *
516 - * @since bbPress (r4284)
483 + * @since 2.2.0 bbPress (r4284)
517 484 *
518 - * @uses apply_filters() Allow override of hardcoded blocked role
519 485 * @return string
520 486 */
521 487 function bbp_get_blocked_role() {
488 +
489 + // Filter & return
522 490 return apply_filters( 'bbp_get_blocked_role', 'bbp_blocked' );
523 491 }
524 492
525 -/** Deprecated ****************************************************************/
526 -
527 493 /**
528 494 * Adds bbPress-specific user roles.
529 495 *
530 - * @since bbPress (r2741)
531 - * @deprecated since version 2.2
496 + * @since 2.0.0 bbPress (r2741)
497 + *
498 + * @deprecated 2.2.0 bbPress (r4164)
532 499 */
533 500 function bbp_add_roles() {
534 - _doing_it_wrong( 'bbp_add_roles', __( 'Editable forum roles no longer exist.', 'bbpress' ), '2.2' );
501 + _doing_it_wrong( 'bbp_add_roles', esc_html__( 'Editable forum roles no longer exist.', 'bbpress' ), '2.2' );
535 502 }
536 503
537 504 /**
538 - * Removes bbPress-specific user roles.
505 + * Removes bbPress-specific user roles from the `wp_user_roles` array.
539 506 *
540 - * @since bbPress (r2741)
541 - * @deprecated since version 2.2
507 + * This is currently only used when updating, uninstalling, or resetting bbPress.
508 + *
509 + * @see bbp_admin_reset_handler()
510 + * @see bbp_do_uninstall()
511 + * @see bbp_version_updater()
512 + *
513 + * @since 2.0.0 bbPress (r2741)
542 514 */
543 515 function bbp_remove_roles() {
544 - _doing_it_wrong( 'bbp_remove_roles', __( 'Editable forum roles no longer exist.', 'bbpress' ), '2.2' );
516 +
517 + // Remove the bbPress roles
518 + foreach ( array_keys( bbp_get_dynamic_roles() ) as $bbp_role ) {
519 + remove_role( $bbp_role );
520 + }
521 +
522 + // Some early adopters may have a deprecated visitor role. It was later
523 + // replaced by the Spectator role.
524 + remove_role( 'bbp_visitor' );
545 525 }