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

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