PluginProbe
bbPress / 2.0-rc-4
bbPress v2.0-rc-4
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
bbpress / bbp-includes / bbp-core-caps.php

bbp-core-caps.php in bbPress 2.0-rc-4, at bbp-includes/bbp-core-caps.php

614 lines 14.0 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 switch ( $cap ) {
157
158 // Reading
159 case 'read_forum' :
160 case 'read_topic' :
161 case 'read_reply' :
162
163 if ( $post = get_post( $args[0] ) ) {
164 $caps = array();
165 $post_type = get_post_type_object( $post->post_type );
166
167 if ( 'private' != $post->post_status )
168 $caps[] = 'read';
169 elseif ( (int) $user_id == (int) $post->post_author )
170 $caps[] = 'read';
171 else
172 $caps[] = $post_type->cap->read_private_posts;
173 }
174
175 break;
176
177 // Editing
178 case 'edit_forums' :
179 case 'edit_topics' :
180 case 'edit_replies' :
181
182 // Add do_not_allow cap if user is spam or deleted
183 if ( bbp_is_user_spammer( $user_id ) || bbp_is_user_deleted( $user_id ) )
184 $caps = array( 'do_not_allow' );
185
186 break;
187
188 case 'edit_forum' :
189 case 'edit_topic' :
190 case 'edit_reply' :
191
192 if ( $post = get_post( $args[0] ) ) {
193 $caps = array();
194 $post_type = get_post_type_object( $post->post_type );
195
196 // Add 'do_not_allow' cap if user is spam or deleted
197 if ( bbp_is_user_spammer( $user_id ) || bbp_is_user_deleted( $user_id ) )
198 $caps[] = 'do_not_allow';
199
200 // Map to edit_posts
201 elseif ( (int) $user_id == (int) $post->post_author )
202 $caps[] = $post_type->cap->edit_posts;
203
204 // Map to edit_others_posts
205 else
206 $caps[] = $post_type->cap->edit_others_posts;
207 }
208
209 break;
210
211 // Deleting
212 case 'delete_forum' :
213
214 if ( $post = get_post( $args[0] ) ) {
215 $caps = array();
216 $post_type = get_post_type_object( $post->post_type );
217
218 // Add 'do_not_allow' cap if user is spam or deleted
219 if ( bbp_is_user_spammer( $user_id ) || bbp_is_user_deleted( $user_id ) )
220 $caps[] = 'do_not_allow';
221
222 // Map to delete_posts
223 elseif ( (int) $user_id == (int) $post->post_author )
224 $caps[] = $post_type->cap->delete_posts;
225
226 // Map to delete_others_posts
227 else
228 $caps[] = $post_type->cap->delete_others_posts;
229 }
230
231 break;
232
233 case 'delete_topic' :
234 case 'delete_reply' :
235
236 if ( $post = get_post( $args[0] ) ) {
237 $caps = array();
238 $post_type = get_post_type_object( $post->post_type );
239
240 // Add 'do_not_allow' cap if user is spam or deleted
241 if ( bbp_is_user_spammer( $user_id ) || bbp_is_user_deleted( $user_id ) )
242 $caps[] = 'do_not_allow';
243
244 // Map to delete_others_posts
245 else
246 $caps[] = $post_type->cap->delete_others_posts;
247 }
248
249 break;
250 }
251
252 return apply_filters( 'bbp_map_meta_caps', $caps, $cap, $user_id, $args );
253 }
254
255 /**
256 * Return forum capabilities
257 *
258 * @since bbPress (r2593)
259 *
260 * @uses apply_filters() Calls 'bbp_get_forum_caps' with the capabilities
261 * @return array Forum capabilities
262 */
263 function bbp_get_forum_caps() {
264
265 // Forum meta caps
266 $caps = array (
267 'delete_posts' => 'delete_forums',
268 'delete_others_posts' => 'delete_others_forums'
269 );
270
271 return apply_filters( 'bbp_get_forum_caps', $caps );
272 }
273
274 /**
275 * Return topic capabilities
276 *
277 * @since bbPress (r2593)
278 *
279 * @uses apply_filters() Calls 'bbp_get_topic_caps' with the capabilities
280 * @return array Topic capabilities
281 */
282 function bbp_get_topic_caps() {
283
284 // Topic meta caps
285 $caps = array (
286 'delete_posts' => 'delete_topics',
287 'delete_others_posts' => 'delete_others_topics'
288 );
289
290 return apply_filters( 'bbp_get_topic_caps', $caps );
291 }
292
293 /**
294 * Return reply capabilities
295 *
296 * @since bbPress (r2593)
297 *
298 * @uses apply_filters() Calls 'bbp_get_reply_caps' with the capabilities
299 * @return array Reply capabilities
300 */
301 function bbp_get_reply_caps () {
302
303 // Reply meta caps
304 $caps = array (
305 'edit_posts' => 'edit_replies',
306 'edit_others_posts' => 'edit_others_replies',
307 'publish_posts' => 'publish_replies',
308 'read_private_posts' => 'read_private_replies',
309 'delete_posts' => 'delete_replies',
310 'delete_others_posts' => 'delete_others_replies'
311 );
312
313 return apply_filters( 'bbp_get_reply_caps', $caps );
314 }
315
316 /**
317 * Return topic tag capabilities
318 *
319 * @since bbPress (r2593)
320 *
321 * @uses apply_filters() Calls 'bbp_get_topic_tag_caps' with the capabilities
322 * @return array Topic tag capabilities
323 */
324 function bbp_get_topic_tag_caps () {
325
326 // Topic tag meta caps
327 $caps = array (
328 'manage_terms' => 'manage_topic_tags',
329 'edit_terms' => 'edit_topic_tags',
330 'delete_terms' => 'delete_topic_tags',
331 'assign_terms' => 'assign_topic_tags'
332 );
333
334 return apply_filters( 'bbp_get_topic_tag_caps', $caps );
335 }
336
337
338 /**
339 * Returns an array of capabilities based on the role that is being requested.
340 *
341 * @since bbPress (r2994)
342 *
343 * @param string $role Optional. Defaults to The role to load caps for
344 * @uses apply_filters() Allow return value to be filtered
345 *
346 * @return array Capabilities for $role
347 */
348 function bbp_get_caps_for_role( $role = '' ) {
349
350 // Get new role names
351 $moderator_role = bbp_get_moderator_role();
352 $participant_role = bbp_get_participant_role();
353
354 // Which role are we looking for?
355 switch ( $role ) {
356
357 // Administrator
358 case 'administrator' :
359
360 $caps = array(
361
362 // Forum caps
363 'publish_forums',
364 'edit_forums',
365 'edit_others_forums',
366 'delete_forums',
367 'delete_others_forums',
368 'read_private_forums',
369 'read_hidden_forums',
370
371 // Topic caps
372 'publish_topics',
373 'edit_topics',
374 'edit_others_topics',
375 'delete_topics',
376 'delete_others_topics',
377 'read_private_topics',
378
379 // Reply caps
380 'publish_replies',
381 'edit_replies',
382 'edit_others_replies',
383 'delete_replies',
384 'delete_others_replies',
385 'read_private_replies',
386
387 // Topic tag caps
388 'manage_topic_tags',
389 'edit_topic_tags',
390 'delete_topic_tags',
391 'assign_topic_tags',
392
393 // Misc
394 'moderate',
395 'throttle',
396 'view_trash'
397 );
398
399 break;
400
401 // Moderator
402 case $moderator_role :
403
404 $caps = array(
405
406 // Forum caps
407 'read_private_forums',
408 'read_hidden_forums',
409
410 // Topic caps
411 'publish_topics',
412 'edit_topics',
413 'edit_others_topics',
414 'delete_topics',
415 'delete_others_topics',
416 'read_private_topics',
417
418 // Reply caps
419 'publish_replies',
420 'edit_replies',
421 'edit_others_replies',
422 'delete_replies',
423 'delete_others_replies',
424 'read_private_replies',
425
426 // Topic tag caps
427 'manage_topic_tags',
428 'edit_topic_tags',
429 'delete_topic_tags',
430 'assign_topic_tags',
431
432 // Misc
433 'moderate',
434 'throttle',
435 'view_trash',
436 );
437
438 break;
439
440 // WordPress Core Roles
441 case 'editor' :
442 case 'author' :
443 case 'contributor' :
444 case 'subscriber' :
445
446 // bbPress Participant Role
447 case $participant_role :
448 default :
449
450 $caps = array(
451
452 // Topic caps
453 'publish_topics',
454 'edit_topics',
455
456 // Reply caps
457 'publish_replies',
458 'edit_replies',
459
460 // Topic tag caps
461 'assign_topic_tags',
462
463 );
464
465 break;
466 }
467
468 return apply_filters( 'bbp_get_caps_for_role', $caps, $role );
469 }
470
471 /**
472 * Give a user the default 'Forum Participant' role when creating a topic/reply
473 * on a site they do not have a role or capability on.
474 *
475 * @since bbPress (r3410)
476 *
477 * @global bbPress $bbp
478 *
479 * @uses is_multisite()
480 * @uses bbp_allow_global_access()
481 * @uses bbp_is_user_deleted()
482 * @uses bbp_is_user_spammer()
483 * @uses is_user_logged_in()
484 * @uses current_user_can()
485 * @uses WP_User::set_role()
486 *
487 * @return If user is not spam/deleted or is already capable
488 */
489 function bbp_global_access_auto_role() {
490
491 // Bail if not multisite or forum is not global
492 if ( !is_multisite() || !bbp_allow_global_access() )
493 return;
494
495 // Bail if user is marked as spam or is deleted
496 if ( bbp_is_user_deleted() || bbp_is_user_spammer() )
497 return;
498
499 // Bail if user is not logged in
500 if ( !is_user_logged_in() )
501 return;
502
503 // Give the user the 'Forum Participant' role
504 if ( current_user_can( 'bbp_masked' ) ) {
505 global $bbp;
506
507 // Get the default role
508 $default_role = bbp_get_participant_role();
509
510 // Set the current users default role
511 $bbp->current_user->set_role( $default_role );
512 }
513 }
514
515 /**
516 * The participant role for registered users without roles
517 *
518 * This is primarily for multisite compatibility when users without roles on
519 * sites that have global forums enabled want to create topics and replies
520 *
521 * @since bbPress (r3410)
522 *
523 * @param string $role
524 * @uses apply_filters()
525 * @return string
526 */
527 function bbp_get_participant_role() {
528
529 // Hardcoded participant role
530 $role = 'bbp_participant';
531
532 // Allow override
533 return apply_filters( 'bbp_get_participant_role', $role );
534 }
535
536 /**
537 * The moderator role for bbPress users
538 *
539 * @since bbPress (r3410)
540 *
541 * @param string $role
542 * @uses apply_filters()
543 * @return string
544 */
545 function bbp_get_moderator_role() {
546
547 // Hardcoded moderated user role
548 $role = 'bbp_moderator';
549
550 // Allow override
551 return apply_filters( 'bbp_get_moderator_role', $role );
552 }
553
554 /**
555 * Add the default role and mapped bbPress caps to the current user if needed
556 *
557 * This function will bail if the forum is not global in a multisite
558 * installation of WordPress, or if the user is marked as spam or deleted.
559 *
560 * @since bbPress (r3380)
561 *
562 * @uses is_multisite()
563 * @uses bbp_allow_global_access()
564 * @uses bbp_is_user_deleted()
565 * @uses bbp_is_user_spammer()
566 * @uses is_user_logged_in()
567 * @uses current_user_can()
568 * @uses get_option()
569 * @uses bbp_get_caps_for_role()
570 *
571 * @global bbPress $bbp
572 * @return If not multisite, not global, or user is deleted/spammed
573 */
574 function bbp_global_access_role_mask() {
575
576 // Bail if not multisite or forum is not global
577 if ( !is_multisite() || !bbp_allow_global_access() )
578 return;
579
580 // Bail if user is marked as spam or is deleted
581 if ( bbp_is_user_deleted() || bbp_is_user_spammer() )
582 return;
583
584 // Normal user is logged in but has no caps
585 if ( is_user_logged_in() && !current_user_can( 'read' ) ) {
586
587 // Assign user the minimal participant role to map caps to
588 $default_role = bbp_get_participant_role();
589
590 // Get bbPress caps for the default role
591 $caps_for_role = bbp_get_caps_for_role( $default_role );
592
593 // Set all caps to true
594 foreach ( $caps_for_role as $cap ) {
595 $mapped_meta_caps[$cap] = true;
596 }
597
598 // Add 'read' cap just in case
599 $mapped_meta_caps['read'] = true;
600 $mapped_meta_caps['bbp_masked'] = true;
601
602 // Allow global access caps to be manipulated
603 $mapped_meta_caps = apply_filters( 'bbp_global_access_mapped_meta_caps', $mapped_meta_caps );
604
605 // Assign the role and mapped caps to the current user
606 global $bbp;
607 $bbp->current_user->roles[0] = $default_role;
608 $bbp->current_user->caps = $mapped_meta_caps;
609 $bbp->current_user->allcaps = $mapped_meta_caps;
610 }
611 }
612
613 ?>
614