PluginProbe
bbPress / 2.1
bbPress v2.1
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 / bbp-includes / bbp-core-caps.php

bbp-core-caps.php in bbPress 2.1, at bbp-includes/bbp-core-caps.php

721 lines 17.7 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 * @package bbPress
7 * @subpackage Capabilities
8 */
9
10 // Exit if accessed directly
11 if ( !defined( 'ABSPATH' ) ) exit;
12
13 /**
14 * Adds bbPress-specific user roles.
15 *
16 * This is called on plugin activation.
17 *
18 * @since bbPress (r2741)
19 *
20 * @uses get_option() To get the default role
21 * @uses get_role() To get the default role object
22 * @uses add_role() To add our own roles
23 * @uses do_action() Calls 'bbp_add_roles'
24 */
25 function bbp_add_roles() {
26
27 // Get new role names
28 $moderator_role = bbp_get_moderator_role();
29 $participant_role = bbp_get_participant_role();
30
31 // Add the Moderator role and add the default role caps.
32 // Mod caps are added by the bbp_add_caps() function
33 $default = get_role( get_option( 'default_role' ) );
34
35 // If role does not exist, default to read cap
36 if ( empty( $default->capabilities ) )
37 $default->capabilities = array( 'read' );
38
39 // Moderators are default role + forum moderating caps in bbp_add_caps()
40 add_role( $moderator_role, 'Forum Moderator', $default->capabilities );
41
42 // Forum Subscribers are auto added to sites with global forums
43 add_role( $participant_role, 'Forum Participant', $default->capabilities );
44
45 do_action( 'bbp_add_roles' );
46 }
47
48 /**
49 * Adds capabilities to WordPress user roles.
50 *
51 * This is called on plugin activation.
52 *
53 * @since bbPress (r2608)
54 *
55 * @uses get_role() To get the administrator, default and moderator roles
56 * @uses WP_Role::add_cap() To add various capabilities
57 * @uses do_action() Calls 'bbp_add_caps'
58 */
59 function bbp_add_caps() {
60 global $wp_roles;
61
62 // Load roles if not set
63 if ( ! isset( $wp_roles ) )
64 $wp_roles = new WP_Roles();
65
66 // Loop through available roles
67 foreach( $wp_roles->roles as $role => $details ) {
68
69 // Load this role
70 $this_role = get_role( $role );
71
72 // Loop through caps for this role and remove them
73 foreach ( bbp_get_caps_for_role( $role ) as $cap ) {
74 $this_role->add_cap( $cap );
75 }
76 }
77
78 do_action( 'bbp_add_caps' );
79 }
80
81 /**
82 * Removes capabilities from WordPress user roles.
83 *
84 * This is called on plugin deactivation.
85 *
86 * @since bbPress (r2608)
87 *
88 * @uses get_role() To get the administrator and default roles
89 * @uses WP_Role::remove_cap() To remove various capabilities
90 * @uses do_action() Calls 'bbp_remove_caps'
91 */
92 function bbp_remove_caps() {
93 global $wp_roles;
94
95 // Load roles if not set
96 if ( ! isset( $wp_roles ) )
97 $wp_roles = new WP_Roles();
98
99 // Loop through available roles
100 foreach( $wp_roles->roles as $role => $details ) {
101
102 // Load this role
103 $this_role = get_role( $role );
104
105 // Loop through caps for this role and remove them
106 foreach ( bbp_get_caps_for_role( $role ) as $cap ) {
107 $this_role->remove_cap( $cap );
108 }
109 }
110
111 do_action( 'bbp_remove_caps' );
112 }
113
114 /**
115 * Removes bbPress-specific user roles.
116 *
117 * This is called on plugin deactivation.
118 *
119 * @since bbPress (r2741)
120 *
121 * @uses remove_role() To remove our roles
122 * @uses do_action() Calls 'bbp_remove_roles'
123 */
124 function bbp_remove_roles() {
125
126 // Get new role names
127 $moderator_role = bbp_get_moderator_role();
128 $participant_role = bbp_get_participant_role();
129
130 // Remove the Moderator role
131 remove_role( $moderator_role );
132
133 // Remove the Moderator role
134 remove_role( $participant_role );
135
136 do_action( 'bbp_remove_roles' );
137 }
138
139 /**
140 * Maps forum/topic/reply caps to built in WordPress caps
141 *
142 * @since bbPress (r2593)
143 *
144 * @param array $caps Capabilities for meta capability
145 * @param string $cap Capability name
146 * @param int $user_id User id
147 * @param mixed $args Arguments
148 * @uses get_post() To get the post
149 * @uses get_post_type_object() To get the post type object
150 * @uses apply_filters() Calls 'bbp_map_meta_caps' with caps, cap, user id and
151 * args
152 * @return array Actual capabilities for meta capability
153 */
154 function bbp_map_meta_caps( $caps, $cap, $user_id, $args ) {
155
156 // What capability is being checked?
157 switch ( $cap ) {
158
159 /** Reading ***********************************************************/
160
161 case 'read_forum' :
162 case 'read_topic' :
163 case 'read_reply' :
164
165 // Get the post
166 $_post = get_post( $args[0] );
167 if ( !empty( $_post ) ) {
168
169 // Get caps for post type object
170 $post_type = get_post_type_object( $_post->post_type );
171 $caps = array();
172
173 // Post is public
174 if ( bbp_get_public_status_id() == $_post->post_status ) {
175 $caps[] = 'read';
176
177 // User is author so allow read
178 } elseif ( (int) $user_id == (int) $_post->post_author ) {
179 $caps[] = 'read';
180
181 // Unknown so map to private posts
182 } else {
183 $caps[] = $post_type->cap->read_private_posts;
184 }
185 }
186
187 break;
188
189 /** Publishing ********************************************************/
190
191 case 'publish_forums' :
192 case 'publish_topics' :
193 case 'publish_replies' :
194
195 // Add do_not_allow cap if user is spam or deleted
196 if ( bbp_is_user_inactive( $user_id ) )
197 $caps = array( 'do_not_allow' );
198
199 break;
200
201 /** Editing ***********************************************************/
202
203 // Used primarily in wp-admin
204 case 'edit_forums' :
205 case 'edit_topics' :
206 case 'edit_replies' :
207
208 // Add do_not_allow cap if user is spam or deleted
209 if ( bbp_is_user_inactive( $user_id ) )
210 $caps = array( 'do_not_allow' );
211
212 break;
213
214 // Used everywhere
215 case 'edit_forum' :
216 case 'edit_topic' :
217 case 'edit_reply' :
218
219 // Get the post
220 $_post = get_post( $args[0] );
221 if ( !empty( $_post ) ) {
222
223 // Get caps for post type object
224 $post_type = get_post_type_object( $_post->post_type );
225 $caps = array();
226
227 // Add 'do_not_allow' cap if user is spam or deleted
228 if ( bbp_is_user_inactive( $user_id ) ) {
229 $caps[] = 'do_not_allow';
230
231 // User is author so allow edit
232 } elseif ( (int) $user_id == (int) $_post->post_author ) {
233 $caps[] = $post_type->cap->edit_posts;
234
235 // Unknown, so map to edit_others_posts
236 } else {
237 $caps[] = $post_type->cap->edit_others_posts;
238 }
239 }
240
241 break;
242
243 /** Deleting **********************************************************/
244
245 // Allow forum authors to delete forums (for BuddyPress groups, etc)
246 case 'delete_forum' :
247
248 // Get the post
249 $_post = get_post( $args[0] );
250 if ( !empty( $_post ) ) {
251
252 // Get caps for post type object
253 $post_type = get_post_type_object( $_post->post_type );
254 $caps = array();
255
256 // Add 'do_not_allow' cap if user is spam or deleted
257 if ( bbp_is_user_inactive( $user_id ) ) {
258 $caps[] = 'do_not_allow';
259
260 // User is author so allow to delete
261 } elseif ( (int) $user_id == (int) $_post->post_author ) {
262 $caps[] = $post_type->cap->delete_posts;
263
264 // Unknown so map to delete_others_posts
265 } else {
266 $caps[] = $post_type->cap->delete_others_posts;
267 }
268 }
269
270 break;
271
272 case 'delete_topic' :
273 case 'delete_reply' :
274
275 // Get the post
276 $_post = get_post( $args[0] );
277 if ( !empty( $_post ) ) {
278
279 // Get caps for post type object
280 $post_type = get_post_type_object( $_post->post_type );
281 $caps = array();
282
283 // Add 'do_not_allow' cap if user is spam or deleted
284 if ( bbp_is_user_inactive( $user_id ) ) {
285 $caps[] = 'do_not_allow';
286
287 // Unknown so map to delete_others_posts
288 } else {
289 $caps[] = $post_type->cap->delete_others_posts;
290 }
291 }
292
293 break;
294 }
295
296 return apply_filters( 'bbp_map_meta_caps', $caps, $cap, $user_id, $args );
297 }
298
299 /**
300 * Return forum capabilities
301 *
302 * @since bbPress (r2593)
303 *
304 * @uses apply_filters() Calls 'bbp_get_forum_caps' with the capabilities
305 * @return array Forum capabilities
306 */
307 function bbp_get_forum_caps() {
308
309 // Forum meta caps
310 $caps = array (
311 'delete_posts' => 'delete_forums',
312 'delete_others_posts' => 'delete_others_forums'
313 );
314
315 return apply_filters( 'bbp_get_forum_caps', $caps );
316 }
317
318 /**
319 * Return topic capabilities
320 *
321 * @since bbPress (r2593)
322 *
323 * @uses apply_filters() Calls 'bbp_get_topic_caps' with the capabilities
324 * @return array Topic capabilities
325 */
326 function bbp_get_topic_caps() {
327
328 // Topic meta caps
329 $caps = array (
330 'delete_posts' => 'delete_topics',
331 'delete_others_posts' => 'delete_others_topics'
332 );
333
334 return apply_filters( 'bbp_get_topic_caps', $caps );
335 }
336
337 /**
338 * Return reply capabilities
339 *
340 * @since bbPress (r2593)
341 *
342 * @uses apply_filters() Calls 'bbp_get_reply_caps' with the capabilities
343 * @return array Reply capabilities
344 */
345 function bbp_get_reply_caps () {
346
347 // Reply meta caps
348 $caps = array (
349 'edit_posts' => 'edit_replies',
350 'edit_others_posts' => 'edit_others_replies',
351 'publish_posts' => 'publish_replies',
352 'read_private_posts' => 'read_private_replies',
353 'delete_posts' => 'delete_replies',
354 'delete_others_posts' => 'delete_others_replies'
355 );
356
357 return apply_filters( 'bbp_get_reply_caps', $caps );
358 }
359
360 /**
361 * Return topic tag capabilities
362 *
363 * @since bbPress (r2593)
364 *
365 * @uses apply_filters() Calls 'bbp_get_topic_tag_caps' with the capabilities
366 * @return array Topic tag capabilities
367 */
368 function bbp_get_topic_tag_caps () {
369
370 // Topic tag meta caps
371 $caps = array (
372 'manage_terms' => 'manage_topic_tags',
373 'edit_terms' => 'edit_topic_tags',
374 'delete_terms' => 'delete_topic_tags',
375 'assign_terms' => 'assign_topic_tags'
376 );
377
378 return apply_filters( 'bbp_get_topic_tag_caps', $caps );
379 }
380
381
382 /**
383 * Returns an array of capabilities based on the role that is being requested.
384 *
385 * @since bbPress (r2994)
386 *
387 * @param string $role Optional. Defaults to The role to load caps for
388 * @uses apply_filters() Allow return value to be filtered
389 *
390 * @return array Capabilities for $role
391 */
392 function bbp_get_caps_for_role( $role = '' ) {
393
394 // Which role are we looking for?
395 switch ( $role ) {
396
397 // Administrator
398 case 'administrator' :
399
400 $caps = array(
401
402 // Forum caps
403 'publish_forums',
404 'edit_forums',
405 'edit_others_forums',
406 'delete_forums',
407 'delete_others_forums',
408 'read_private_forums',
409 'read_hidden_forums',
410
411 // Topic caps
412 'publish_topics',
413 'edit_topics',
414 'edit_others_topics',
415 'delete_topics',
416 'delete_others_topics',
417 'read_private_topics',
418
419 // Reply caps
420 'publish_replies',
421 'edit_replies',
422 'edit_others_replies',
423 'delete_replies',
424 'delete_others_replies',
425 'read_private_replies',
426
427 // Topic tag caps
428 'manage_topic_tags',
429 'edit_topic_tags',
430 'delete_topic_tags',
431 'assign_topic_tags',
432
433 // Misc
434 'moderate',
435 'throttle',
436 'view_trash'
437 );
438
439 break;
440
441 // Moderator
442 case bbp_get_moderator_role() :
443
444 $caps = array(
445
446 // Forum caps
447 'read_private_forums',
448 'read_hidden_forums',
449
450 // Topic caps
451 'publish_topics',
452 'edit_topics',
453 'edit_others_topics',
454 'delete_topics',
455 'delete_others_topics',
456 'read_private_topics',
457
458 // Reply caps
459 'publish_replies',
460 'edit_replies',
461 'edit_others_replies',
462 'delete_replies',
463 'delete_others_replies',
464 'read_private_replies',
465
466 // Topic tag caps
467 'manage_topic_tags',
468 'edit_topic_tags',
469 'delete_topic_tags',
470 'assign_topic_tags',
471
472 // Misc
473 'moderate',
474 'throttle',
475 'view_trash'
476 );
477
478 break;
479
480 // WordPress Core Roles
481 case 'editor' :
482 case 'author' :
483 case 'contributor' :
484 case 'subscriber' :
485
486 // bbPress Participant Role
487 case bbp_get_participant_role() :
488
489 // Any other role
490 default :
491
492 $caps = array(
493
494 // Forum caps
495 'read_private_forums',
496
497 // Topic caps
498 'publish_topics',
499 'edit_topics',
500
501 // Reply caps
502 'publish_replies',
503 'edit_replies',
504
505 // Topic tag caps
506 'assign_topic_tags'
507 );
508
509 break;
510 }
511
512 return apply_filters( 'bbp_get_caps_for_role', $caps, $role );
513 }
514
515 /**
516 * Give a user the default 'Forum Participant' role when creating a topic/reply
517 * on a site they do not have a role or capability on.
518 *
519 * @since bbPress (r3410)
520 *
521 * @uses is_multisite()
522 * @uses bbp_allow_global_access()
523 * @uses bbp_is_user_inactive()
524 * @uses is_user_logged_in()
525 * @uses current_user_can()
526 * @uses WP_User::set_role()
527 *
528 * @return If user is not spam/deleted or is already capable
529 */
530 function bbp_global_access_auto_role() {
531
532 // Bail if not multisite or forum is not global
533 if ( !is_multisite() || !bbp_allow_global_access() )
534 return;
535
536 // Bail if user is not active
537 if ( bbp_is_user_inactive() )
538 return;
539
540 // Bail if user is not logged in
541 if ( !is_user_logged_in() )
542 return;
543
544 // Give the user the 'Forum Participant' role
545 if ( current_user_can( 'bbp_masked' ) ) {
546
547 // Get the default role
548 $default_role = bbp_get_participant_role();
549
550 // Set the current users default role
551 bbpress()->current_user->set_role( $default_role );
552 }
553 }
554
555 /**
556 * The participant role for registered users without roles
557 *
558 * This is primarily for multisite compatibility when users without roles on
559 * sites that have global forums enabled want to create topics and replies
560 *
561 * @since bbPress (r3860)
562 *
563 * @uses apply_filters() Allow override of hardcoded anonymous role
564 * @return string
565 */
566 function bbp_get_anonymous_role() {
567 return apply_filters( 'bbp_get_anonymous_role', 'bbp_anonymous' );
568 }
569
570 /**
571 * The participant role for registered users without roles
572 *
573 * This is primarily for multisite compatibility when users without roles on
574 * sites that have global forums enabled want to create topics and replies
575 *
576 * @since bbPress (r3410)
577 *
578 * @uses apply_filters() Allow override of hardcoded participant role
579 * @return string
580 */
581 function bbp_get_participant_role() {
582 return apply_filters( 'bbp_get_participant_role', 'bbp_participant' );
583 }
584
585 /**
586 * The moderator role for bbPress users
587 *
588 * @since bbPress (r3410)
589 *
590 * @param string $role
591 * @uses apply_filters() Allow override of hardcoded moderator role
592 * @return string
593 */
594 function bbp_get_moderator_role() {
595 return apply_filters( 'bbp_get_moderator_role', 'bbp_moderator' );
596 }
597
598 /**
599 * Add the default role and mapped bbPress caps to the current user if needed
600 *
601 * This function will bail if the forum is not global in a multisite
602 * installation of WordPress, or if the user is marked as spam or deleted.
603 *
604 * @since bbPress (r3380)
605 *
606 * @uses is_multisite()
607 * @uses bbp_allow_global_access()
608 * @uses bbp_is_user_inactive()
609 * @uses is_user_logged_in()
610 * @uses current_user_can()
611 * @uses get_option()
612 * @uses bbp_get_caps_for_role()
613 *
614 * @return If not multisite, not global, or user is deleted/spammed
615 */
616 function bbp_global_access_role_mask() {
617
618 // Bail if not multisite or forum is not global
619 if ( !is_multisite() || !bbp_allow_global_access() )
620 return;
621
622 // Bail if user is marked as spam or is deleted
623 if ( bbp_is_user_inactive() )
624 return;
625
626 // Normal user is logged in but has no caps
627 if ( is_user_logged_in() && !current_user_can( 'read' ) ) {
628
629 // Assign user the minimal participant role to map caps to
630 $default_role = bbp_get_participant_role();
631
632 // Get bbPress caps for the default role
633 $caps_for_role = bbp_get_caps_for_role( $default_role );
634
635 // Set all caps to true
636 foreach ( $caps_for_role as $cap ) {
637 $mapped_meta_caps[$cap] = true;
638 }
639
640 // Add 'read' cap just in case
641 $mapped_meta_caps['read'] = true;
642 $mapped_meta_caps['bbp_masked'] = true;
643
644 // Allow global access caps to be manipulated
645 $mapped_meta_caps = apply_filters( 'bbp_global_access_mapped_meta_caps', $mapped_meta_caps );
646
647 // Assign the role and mapped caps to the current user
648 $bbp = bbpress();
649 $bbp->current_user->roles[0] = $default_role;
650 $bbp->current_user->caps = $mapped_meta_caps;
651 $bbp->current_user->allcaps = $mapped_meta_caps;
652 }
653 }
654
655 /**
656 * Can the current user see a specific UI element?
657 *
658 * Used when registering post-types and taxonomies to decide if 'show_ui' should
659 * be set to true or false. Also used for fine-grained control over which admin
660 * sections are visible under what conditions.
661 *
662 * This function is in bbp-core-caps.php rather than in /bbp-admin so that it
663 * can be used during the bbp_register_post_types action.
664 *
665 * @since bbPress (r3944)
666 *
667 * @uses current_user_can() To check the 'moderate' capability
668 * @uses bbp_get_forum_post_type()
669 * @uses bbp_get_topic_post_type()
670 * @uses bbp_get_reply_post_type()
671 * @uses bbp_get_topic_tag_tax_id()
672 * @uses is_plugin_active()
673 * @uses is_super_admin()
674 * @return bool Results of current_user_can( 'moderate' ) check.
675 */
676 function bbp_current_user_can_see( $component = '' ) {
677
678 // Define local variable
679 $retval = false;
680
681 // Which component are we checking UI visibility for?
682 switch ( $component ) {
683
684 /** Everywhere ********************************************************/
685
686 case bbp_get_forum_post_type() : // Forums
687 case bbp_get_topic_post_type() : // Topics
688 case bbp_get_reply_post_type() : // Replies
689 case bbp_get_topic_tag_tax_id() : // Topic-Tags
690 $retval = current_user_can( 'moderate' );
691 break;
692
693 /** Admin Exclusive ***************************************************/
694
695 case 'bbp_settings_buddypress' : // BuddyPress Extension
696 $retval = ( is_plugin_active( 'buddypress/bp-loader.php' ) && defined( 'BP_VERSION' ) ) && is_super_admin();
697 break;
698
699 case 'bbp_settings_akismet' : // Akismet Extension
700 $retval = ( is_plugin_active( 'akismet/akismet.php' ) && defined( 'AKISMET_VERSION' ) ) && is_super_admin();
701 break;
702
703 case 'bbp_tools_page' : // Tools Page
704 case 'bbp_tools_repair_page' : // Tools - Repair Page
705 case 'bbp_tools_import_page' : // Tools - Import Page
706 case 'bbp_tools_reset_page' : // Tools - Reset Page
707 case 'bbp_settings?page' : // Settings Page
708 case 'bbp_settings_main' : // Settings - General
709 case 'bbp_settings_theme_compat' : // Settings - Theme compat
710 case 'bbp_settings_root_slugs' : // Settings - Root slugs
711 case 'bbp_settings_single_slugs' : // Settings - Single slugs
712 case 'bbp_settings_per_page' : // Settings - Single slugs
713 case 'bbp_settings_per_page_rss' : // Settings - Single slugs
714 default : // Anything else
715 $retval = current_user_can( bbpress()->admin->minimum_capability );
716 break;
717 }
718
719 return (bool) apply_filters( 'bbp_current_user_can_see', (bool) $retval, $component );
720 }
721