PluginProbe
bbPress / 2.2
bbPress v2.2
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 2.2.2 All 71 releases
bbpress / includes / core / capabilities.php

capabilities.php in bbPress 2.2, at includes/core/capabilities.php

565 lines 14.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress Capabilites
5 *
6 * The functions in this file are used primarily as convenient wrappers for
7 * capability output in user profiles. This includes mapping capabilities and
8 * groups to human readable strings,
9 *
10 * @package bbPress
11 * @subpackage Capabilities
12 */
13
14 // Exit if accessed directly
15 if ( !defined( 'ABSPATH' ) ) exit;
16
17 /** Mapping *******************************************************************/
18
19 /**
20 * Returns an array of capabilities based on the role that is being requested.
21 *
22 * @since bbPress (r2994)
23 *
24 * @todo Map all of these and deprecate
25 *
26 * @param string $role Optional. Defaults to The role to load caps for
27 * @uses apply_filters() Allow return value to be filtered
28 *
29 * @return array Capabilities for $role
30 */
31 function bbp_get_caps_for_role( $role = '' ) {
32
33 // Which role are we looking for?
34 switch ( $role ) {
35
36 // Keymaster
37 case bbp_get_keymaster_role() :
38 $caps = array(
39
40 // Keymasters only
41 'keep_gate' => true,
42
43 // Primary caps
44 'spectate' => true,
45 'participate' => true,
46 'moderate' => true,
47 'throttle' => true,
48 'view_trash' => true,
49
50 // Forum caps
51 'publish_forums' => true,
52 'edit_forums' => true,
53 'edit_others_forums' => true,
54 'delete_forums' => true,
55 'delete_others_forums' => true,
56 'read_private_forums' => true,
57 'read_hidden_forums' => true,
58
59 // Topic caps
60 'publish_topics' => true,
61 'edit_topics' => true,
62 'edit_others_topics' => true,
63 'delete_topics' => true,
64 'delete_others_topics' => true,
65 'read_private_topics' => true,
66
67 // Reply caps
68 'publish_replies' => true,
69 'edit_replies' => true,
70 'edit_others_replies' => true,
71 'delete_replies' => true,
72 'delete_others_replies' => true,
73 'read_private_replies' => true,
74
75 // Topic tag caps
76 'manage_topic_tags' => true,
77 'edit_topic_tags' => true,
78 'delete_topic_tags' => true,
79 'assign_topic_tags' => true
80 );
81
82 break;
83
84 // Moderator
85 case bbp_get_moderator_role() :
86 $caps = array(
87
88 // Primary caps
89 'spectate' => true,
90 'participate' => true,
91 'moderate' => true,
92 'throttle' => true,
93 'view_trash' => false,
94
95 // Forum caps
96 'publish_forums' => true,
97 'edit_forums' => true,
98 'edit_others_forums' => false,
99 'delete_forums' => false,
100 'delete_others_forums' => false,
101 'read_private_forums' => true,
102 'read_hidden_forums' => false,
103
104 // Topic caps
105 'publish_topics' => true,
106 'edit_topics' => true,
107 'edit_others_topics' => true,
108 'delete_topics' => true,
109 'delete_others_topics' => true,
110 'read_private_topics' => true,
111
112 // Reply caps
113 'publish_replies' => true,
114 'edit_replies' => true,
115 'edit_others_replies' => true,
116 'delete_replies' => true,
117 'delete_others_replies' => true,
118 'read_private_replies' => true,
119
120 // Topic tag caps
121 'manage_topic_tags' => true,
122 'edit_topic_tags' => true,
123 'delete_topic_tags' => true,
124 'assign_topic_tags' => true,
125 );
126
127 break;
128
129 // Spectators can only read
130 case bbp_get_spectator_role() :
131 $caps = array(
132
133 // Primary caps
134 '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 );
171
172 break;
173
174 // Explicitly blocked
175 case bbp_get_blocked_role() :
176 $caps = array(
177
178 // Primary caps
179 'spectate' => false,
180 'participate' => false,
181 'moderate' => false,
182 'throttle' => false,
183 'view_trash' => false,
184
185 // Forum caps
186 'publish_forums' => false,
187 'edit_forums' => false,
188 'edit_others_forums' => false,
189 'delete_forums' => false,
190 'delete_others_forums' => false,
191 'read_private_forums' => false,
192 'read_hidden_forums' => false,
193
194 // Topic caps
195 'publish_topics' => false,
196 'edit_topics' => false,
197 'edit_others_topics' => false,
198 'delete_topics' => false,
199 'delete_others_topics' => false,
200 'read_private_topics' => false,
201
202 // Reply caps
203 'publish_replies' => false,
204 'edit_replies' => false,
205 'edit_others_replies' => false,
206 'delete_replies' => false,
207 'delete_others_replies' => false,
208 'read_private_replies' => false,
209
210 // Topic tag caps
211 'manage_topic_tags' => false,
212 'edit_topic_tags' => false,
213 'delete_topic_tags' => false,
214 'assign_topic_tags' => false,
215 );
216
217 break;
218
219 // Participant/Default
220 case bbp_get_visitor_role() :
221 case bbp_get_participant_role() :
222 default :
223 $caps = array(
224
225 // Primary caps
226 'spectate' => true,
227 'participate' => true,
228 'moderate' => false,
229 'throttle' => false,
230 'view_trash' => false,
231
232 // 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 'read_private_forums' => true,
239 'read_hidden_forums' => false,
240
241 // Topic caps
242 'publish_topics' => true,
243 'edit_topics' => true,
244 'edit_others_topics' => false,
245 'delete_topics' => false,
246 'delete_others_topics' => false,
247 'read_private_topics' => false,
248
249 // Reply caps
250 'publish_replies' => true,
251 'edit_replies' => true,
252 'edit_others_replies' => false,
253 'delete_replies' => false,
254 'delete_others_replies' => false,
255 'read_private_replies' => false,
256
257 // Topic tag caps
258 'manage_topic_tags' => false,
259 'edit_topic_tags' => false,
260 'delete_topic_tags' => false,
261 'assign_topic_tags' => true,
262 );
263
264 break;
265 }
266
267 return apply_filters( 'bbp_get_caps_for_role', $caps, $role );
268 }
269
270 /**
271 * Adds capabilities to WordPress user roles.
272 *
273 * @since bbPress (r2608)
274 */
275 function bbp_add_caps() {
276
277 // Loop through available roles and add caps
278 foreach( bbp_get_wp_roles()->role_objects as $role ) {
279 foreach ( bbp_get_caps_for_role( $role->name ) as $cap => $value ) {
280 $role->add_cap( $cap, $value );
281 }
282 }
283
284 do_action( 'bbp_add_caps' );
285 }
286
287 /**
288 * Removes capabilities from WordPress user roles.
289 *
290 * @since bbPress (r2608)
291 */
292 function bbp_remove_caps() {
293
294 // Loop through available roles and remove caps
295 foreach( bbp_get_wp_roles()->role_objects as $role ) {
296 foreach ( array_keys( bbp_get_caps_for_role( $role->name ) ) as $cap ) {
297 $role->remove_cap( $cap );
298 }
299 }
300
301 do_action( 'bbp_remove_caps' );
302 }
303
304 /**
305 * Get the $wp_roles global without needing to declare it everywhere
306 *
307 * @since bbPress (r4293)
308 *
309 * @global WP_Roles $wp_roles
310 * @return WP_Roles
311 */
312 function bbp_get_wp_roles() {
313 global $wp_roles;
314
315 // Load roles if not set
316 if ( ! isset( $wp_roles ) )
317 $wp_roles = new WP_Roles();
318
319 return $wp_roles;
320 }
321
322 /** Forum Roles ***************************************************************/
323
324 /**
325 * Add the bbPress roles to the $wp_roles global.
326 *
327 * We do this to avoid adding these values to the database.
328 *
329 * @since bbPress (r4290)
330 */
331 function bbp_add_forums_roles() {
332 $wp_roles = bbp_get_wp_roles();
333
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'];
338 }
339 }
340
341 /**
342 * Helper function to add filter to option_wp_user_roles
343 *
344 * @since bbPress (r4363)
345 *
346 * @see _bbp_reinit_dynamic_roles()
347 *
348 * @global WPDB $wpdb Used to get the database prefix
349 */
350 function bbp_filter_user_roles_option() {
351 global $wpdb;
352
353 $role_key = $wpdb->prefix . 'user_roles';
354
355 add_filter( 'option_' . $role_key, '_bbp_reinit_dynamic_roles' );
356 }
357
358 /**
359 * This is necessary because in a few places (noted below) WordPress initializes
360 * a blog's roles directly from the database option. When this happens, the
361 * $wp_roles global gets flushed, causing a user to magically lose any
362 * dynamically assigned roles or capabilities when $current_user in refreshed.
363 *
364 * Because dynamic multiple roles is a new concept in WordPress, we work around
365 * it here for now, knowing that improvements will come to WordPress core later.
366 *
367 * @see switch_to_blog()
368 * @see restore_current_blog()
369 * @see WP_Roles::_init()
370 *
371 * @since bbPress (r4363)
372 *
373 * @internal Used by bbPress to reinitialize dynamic roles on blog switch
374 *
375 * @param array $roles
376 * @return array Combined array of database roles and dynamic bbPress roles
377 */
378 function _bbp_reinit_dynamic_roles( $roles = array() ) {
379 foreach( bbp_get_dynamic_roles() as $role_id => $details ) {
380 $roles[$role_id] = $details;
381 }
382 return $roles;
383 }
384
385 /**
386 * Fetch a filtered list of forum roles that the current user is
387 * allowed to have.
388 *
389 * Simple function who's main purpose is to allow filtering of the
390 * list of forum roles so that plugins can remove inappropriate ones depending
391 * on the situation or user making edits.
392 *
393 * Specifically because without filtering, anyone with the edit_users
394 * capability can edit others to be administrators, even if they are
395 * only editors or authors. This filter allows admins to delegate
396 * user management.
397 *
398 * @since bbPress (r4284)
399 *
400 * @return array
401 */
402 function bbp_get_dynamic_roles() {
403 return (array) apply_filters( 'bbp_get_dynamic_roles', array(
404
405 // Keymaster
406 bbp_get_keymaster_role() => array(
407 'name' => __( 'Keymaster', 'bbpress' ),
408 'capabilities' => bbp_get_caps_for_role( bbp_get_keymaster_role() )
409 ),
410
411 // Moderator
412 bbp_get_moderator_role() => array(
413 'name' => __( 'Moderator', 'bbpress' ),
414 'capabilities' => bbp_get_caps_for_role( bbp_get_moderator_role() )
415 ),
416
417 // Participant
418 bbp_get_participant_role() => array(
419 'name' => __( 'Participant', 'bbpress' ),
420 'capabilities' => bbp_get_caps_for_role( bbp_get_participant_role() )
421 ),
422
423 // Spectator
424 bbp_get_spectator_role() => array(
425 'name' => __( 'Spectator', 'bbpress' ),
426 'capabilities' => bbp_get_caps_for_role( bbp_get_spectator_role() )
427 ),
428
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 ) );
441 }
442
443 /**
444 * Removes the bbPress roles from the editable roles array
445 *
446 * This used to use array_diff_assoc() but it randomly broke before 2.2 release.
447 * Need to research what happened, and if there's a way to speed this up.
448 *
449 * @since bbPress (r4303)
450 *
451 * @param array $all_roles All registered roles
452 * @return array
453 */
454 function bbp_filter_blog_editable_roles( $all_roles = array() ) {
455
456 // Loop through bbPress roles
457 foreach ( array_keys( bbp_get_dynamic_roles() ) as $bbp_role ) {
458
459 // Loop through WordPress roles
460 foreach ( array_keys( $all_roles ) as $wp_role ) {
461
462 // If keys match, unset
463 if ( $wp_role == $bbp_role ) {
464 unset( $all_roles[$wp_role] );
465 }
466 }
467 }
468
469 return $all_roles;
470 }
471
472 /**
473 * The keymaster role for bbPress users
474 *
475 * @since bbPress (r4284)
476 *
477 * @uses apply_filters() Allow override of hardcoded keymaster role
478 * @return string
479 */
480 function bbp_get_keymaster_role() {
481 return apply_filters( 'bbp_get_keymaster_role', 'bbp_keymaster' );
482 }
483
484 /**
485 * The moderator role for bbPress users
486 *
487 * @since bbPress (r3410)
488 *
489 * @uses apply_filters() Allow override of hardcoded moderator role
490 * @return string
491 */
492 function bbp_get_moderator_role() {
493 return apply_filters( 'bbp_get_moderator_role', 'bbp_moderator' );
494 }
495
496 /**
497 * The participant role for registered user that can participate in forums
498 *
499 * @since bbPress (r3410)
500 *
501 * @uses apply_filters() Allow override of hardcoded participant role
502 * @return string
503 */
504 function bbp_get_participant_role() {
505 return apply_filters( 'bbp_get_participant_role', 'bbp_participant' );
506 }
507
508 /**
509 * The spectator role is for registered users without any capabilities
510 *
511 * @since bbPress (r3860)
512 *
513 * @uses apply_filters() Allow override of hardcoded spectator role
514 * @return string
515 */
516 function bbp_get_spectator_role() {
517 return apply_filters( 'bbp_get_spectator_role', 'bbp_spectator' );
518 }
519
520 /**
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 * The blocked role is for registered users that cannot spectate or participate
534 *
535 * @since bbPress (r4284)
536 *
537 * @uses apply_filters() Allow override of hardcoded blocked role
538 * @return string
539 */
540 function bbp_get_blocked_role() {
541 return apply_filters( 'bbp_get_blocked_role', 'bbp_blocked' );
542 }
543
544 /** Deprecated ****************************************************************/
545
546 /**
547 * Adds bbPress-specific user roles.
548 *
549 * @since bbPress (r2741)
550 * @deprecated since version 2.2
551 */
552 function bbp_add_roles() {
553 _doing_it_wrong( 'bbp_add_roles', __( 'Editable forum roles no longer exist.', 'bbpress' ), '2.2' );
554 }
555
556 /**
557 * Removes bbPress-specific user roles.
558 *
559 * @since bbPress (r2741)
560 * @deprecated since version 2.2
561 */
562 function bbp_remove_roles() {
563 _doing_it_wrong( 'bbp_remove_roles', __( 'Editable forum roles no longer exist.', 'bbpress' ), '2.2' );
564 }
565