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

496 lines 11.2 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 // Add the Moderator role and add the default role caps. Mod caps are added by the bbp_add_caps () function
28 $default = get_role( get_option( 'default_role' ) );
29
30 // Moderators are default role + forum moderating caps in bbp_add_caps()
31 add_role( 'bbp_moderator', __( 'Forum Moderator', 'bbpress' ), $default->capabilities );
32
33 do_action( 'bbp_add_roles' );
34 }
35
36 /**
37 * Adds capabilities to WordPress user roles.
38 *
39 * This is called on plugin activation.
40 *
41 * @since bbPress (r2608)
42 *
43 * @uses get_role() To get the administrator, default and moderator roles
44 * @uses WP_Role::add_cap() To add various capabilities
45 * @uses do_action() Calls 'bbp_add_caps'
46 */
47 function bbp_add_caps() {
48 global $wp_roles;
49
50 // Loop through available roles
51 foreach( $wp_roles->roles as $role => $details ) {
52
53 // Load this role
54 $this_role = get_role( $role );
55
56 // Loop through caps for this role and remove them
57 foreach ( bbp_get_caps_for_role( $role ) as $cap ) {
58 $this_role->add_cap( $cap );
59 }
60 }
61
62 do_action( 'bbp_add_caps' );
63 }
64
65 /**
66 * Removes capabilities from WordPress user roles.
67 *
68 * This is called on plugin deactivation.
69 *
70 * @since bbPress (r2608)
71 *
72 * @uses get_role() To get the administrator and default roles
73 * @uses WP_Role::remove_cap() To remove various capabilities
74 * @uses do_action() Calls 'bbp_remove_caps'
75 */
76 function bbp_remove_caps() {
77 global $wp_roles;
78
79 // Loop through available roles
80 foreach( $wp_roles->roles as $role => $details ) {
81
82 // Load this role
83 $this_role = get_role( $role );
84
85 // Loop through caps for this role and remove them
86 foreach ( bbp_get_caps_for_role( $role ) as $cap ) {
87 $this_role->remove_cap( $cap );
88 }
89 }
90
91 do_action( 'bbp_remove_caps' );
92 }
93
94 /**
95 * Removes bbPress-specific user roles.
96 *
97 * This is called on plugin deactivation.
98 *
99 * @since bbPress (r2741)
100 *
101 * @uses remove_role() To remove our roles
102 * @uses do_action() Calls 'bbp_remove_roles'
103 */
104 function bbp_remove_roles() {
105
106 // Remove the Moderator role
107 remove_role( 'bbp_moderator' );
108
109 do_action( 'bbp_remove_roles' );
110 }
111
112 /**
113 * Maps forum/topic/reply caps to built in WordPress caps
114 *
115 * @since bbPress (r2593)
116 *
117 * @param array $caps Capabilities for meta capability
118 * @param string $cap Capability name
119 * @param int $user_id User id
120 * @param mixed $args Arguments
121 * @uses get_post() To get the post
122 * @uses get_post_type_object() To get the post type object
123 * @uses apply_filters() Calls 'bbp_map_meta_caps' with caps, cap, user id and
124 * args
125 * @return array Actual capabilities for meta capability
126 */
127 function bbp_map_meta_caps( $caps, $cap, $user_id, $args ) {
128
129 switch ( $cap ) {
130
131 // Reading
132 case 'read_forum' :
133 case 'read_topic' :
134 case 'read_reply' :
135
136 if ( $post = get_post( $args[0] ) ) {
137 $caps = array();
138 $post_type = get_post_type_object( $post->post_type );
139
140 if ( 'private' != $post->post_status )
141 $caps[] = 'read';
142 elseif ( (int) $user_id == (int) $post->post_author )
143 $caps[] = 'read';
144 else
145 $caps[] = $post_type->cap->read_private_posts;
146 }
147
148 break;
149
150 // Editing
151 case 'edit_forums' :
152 case 'edit_topics' :
153 case 'edit_replies' :
154
155 // Add do_not_allow cap if user is spam or deleted
156 if ( bbp_is_user_spammer( $user_id ) || bbp_is_user_deleted( $user_id ) )
157 $caps = array( 'do_not_allow' );
158
159 break;
160
161 case 'edit_forum' :
162 case 'edit_topic' :
163 case 'edit_reply' :
164
165 if ( $post = get_post( $args[0] ) ) {
166 $caps = array();
167 $post_type = get_post_type_object( $post->post_type );
168
169 // Add 'do_not_allow' cap if user is spam or deleted
170 if ( bbp_is_user_spammer( $user_id ) || bbp_is_user_deleted( $user_id ) )
171 $caps[] = 'do_not_allow';
172
173 // Map to edit_posts
174 elseif ( (int) $user_id == (int) $post->post_author )
175 $caps[] = $post_type->cap->edit_posts;
176
177 // Map to edit_others_posts
178 else
179 $caps[] = $post_type->cap->edit_others_posts;
180 }
181
182 break;
183
184 // Deleting
185 case 'delete_forum' :
186
187 if ( $post = get_post( $args[0] ) ) {
188 $caps = array();
189 $post_type = get_post_type_object( $post->post_type );
190
191 // Add 'do_not_allow' cap if user is spam or deleted
192 if ( bbp_is_user_spammer( $user_id ) || bbp_is_user_deleted( $user_id ) )
193 $caps[] = 'do_not_allow';
194
195 // Map to delete_posts
196 elseif ( (int) $user_id == (int) $post->post_author )
197 $caps[] = $post_type->cap->delete_posts;
198
199 // Map to delete_others_posts
200 else
201 $caps[] = $post_type->cap->delete_others_posts;
202 }
203
204 break;
205
206 case 'delete_topic' :
207 case 'delete_reply' :
208
209 if ( $post = get_post( $args[0] ) ) {
210 $caps = array();
211 $post_type = get_post_type_object( $post->post_type );
212
213 // Add 'do_not_allow' cap if user is spam or deleted
214 if ( bbp_is_user_spammer( $user_id ) || bbp_is_user_deleted( $user_id ) )
215 $caps[] = 'do_not_allow';
216
217 // Map to delete_others_posts
218 else
219 $caps[] = $post_type->cap->delete_others_posts;
220 }
221
222 break;
223 }
224
225 return apply_filters( 'bbp_map_meta_caps', $caps, $cap, $user_id, $args );
226 }
227
228 /**
229 * Return forum capabilities
230 *
231 * @since bbPress (r2593)
232 *
233 * @uses apply_filters() Calls 'bbp_get_forum_caps' with the capabilities
234 * @return array Forum capabilities
235 */
236 function bbp_get_forum_caps() {
237
238 // Forum meta caps
239 $caps = array (
240 'delete_posts' => 'delete_forums',
241 'delete_others_posts' => 'delete_others_forums'
242 );
243
244 return apply_filters( 'bbp_get_forum_caps', $caps );
245 }
246
247 /**
248 * Return topic capabilities
249 *
250 * @since bbPress (r2593)
251 *
252 * @uses apply_filters() Calls 'bbp_get_topic_caps' with the capabilities
253 * @return array Topic capabilities
254 */
255 function bbp_get_topic_caps() {
256
257 // Topic meta caps
258 $caps = array (
259 'delete_posts' => 'delete_topics',
260 'delete_others_posts' => 'delete_others_topics'
261 );
262
263 return apply_filters( 'bbp_get_topic_caps', $caps );
264 }
265
266 /**
267 * Return reply capabilities
268 *
269 * @since bbPress (r2593)
270 *
271 * @uses apply_filters() Calls 'bbp_get_reply_caps' with the capabilities
272 * @return array Reply capabilities
273 */
274 function bbp_get_reply_caps () {
275
276 // Reply meta caps
277 $caps = array (
278 'edit_posts' => 'edit_replies',
279 'edit_others_posts' => 'edit_others_replies',
280 'publish_posts' => 'publish_replies',
281 'read_private_posts' => 'read_private_replies',
282 'delete_posts' => 'delete_replies',
283 'delete_others_posts' => 'delete_others_replies'
284 );
285
286 return apply_filters( 'bbp_get_reply_caps', $caps );
287 }
288
289 /**
290 * Return topic tag capabilities
291 *
292 * @since bbPress (r2593)
293 *
294 * @uses apply_filters() Calls 'bbp_get_topic_tag_caps' with the capabilities
295 * @return array Topic tag capabilities
296 */
297 function bbp_get_topic_tag_caps () {
298
299 // Topic tag meta caps
300 $caps = array (
301 'manage_terms' => 'manage_topic_tags',
302 'edit_terms' => 'edit_topic_tags',
303 'delete_terms' => 'delete_topic_tags',
304 'assign_terms' => 'assign_topic_tags'
305 );
306
307 return apply_filters( 'bbp_get_topic_tag_caps', $caps );
308 }
309
310
311 /**
312 * Returns an array of capabilities based on the role that is being requested.
313 *
314 * @since bbPress (r2994)
315 *
316 * @param string $role Optional. Defaults to The role to load caps for
317 * @uses apply_filters() Allow return value to be filtered
318 *
319 * @return array Capabilities for $role
320 */
321 function bbp_get_caps_for_role( $role = '' ) {
322
323 // Which role are we looking for?
324 switch ( $role ) {
325
326 // Administrator
327 case 'administrator' :
328
329 $caps = array(
330
331 // Forum caps
332 'publish_forums',
333 'edit_forums',
334 'edit_others_forums',
335 'delete_forums',
336 'delete_others_forums',
337 'read_private_forums',
338 'read_hidden_forums',
339
340 // Topic caps
341 'publish_topics',
342 'edit_topics',
343 'edit_others_topics',
344 'delete_topics',
345 'delete_others_topics',
346 'read_private_topics',
347
348 // Reply caps
349 'publish_replies',
350 'edit_replies',
351 'edit_others_replies',
352 'delete_replies',
353 'delete_others_replies',
354 'read_private_replies',
355
356 // Topic tag caps
357 'manage_topic_tags',
358 'edit_topic_tags',
359 'delete_topic_tags',
360 'assign_topic_tags',
361
362 // Misc
363 'moderate',
364 'throttle',
365 'view_trash'
366 );
367
368 break;
369
370 // Moderator
371 case 'bbp_moderator' :
372
373 $caps = array(
374
375 // Forum caps
376 'read_private_forums',
377 'read_hidden_forums',
378
379 // Topic caps
380 'publish_topics',
381 'edit_topics',
382 'edit_others_topics',
383 'delete_topics',
384 'delete_others_topics',
385 'read_private_topics',
386
387 // Reply caps
388 'publish_replies',
389 'edit_replies',
390 'edit_others_replies',
391 'delete_replies',
392 'delete_others_replies',
393 'read_private_replies',
394
395 // Topic tag caps
396 'manage_topic_tags',
397 'edit_topic_tags',
398 'delete_topic_tags',
399 'assign_topic_tags',
400
401 // Misc
402 'moderate',
403 'throttle',
404 'view_trash',
405 );
406
407 break;
408
409 // Other specific roles
410 case 'editor' :
411 case 'author' :
412 case 'contributor' :
413 case 'subscriber' :
414 default :
415
416 $caps = array(
417
418 // Topic caps
419 'publish_topics',
420 'edit_topics',
421
422 // Reply caps
423 'publish_replies',
424 'edit_replies',
425
426 // Topic tag caps
427 'assign_topic_tags',
428
429 );
430
431 break;
432 }
433
434 return apply_filters( 'bbp_get_caps_for_role', $caps, $role );
435 }
436
437 /**
438 * Add the default role and mapped bbPress caps to the current user if needed
439 *
440 * This function will bail if the forum is not global in a multisite
441 * installation of WordPress, or if the user is marked as spam or deleted.
442 *
443 * @since bbPress (r3380)
444 *
445 * @uses is_multisite()
446 * @uses bbp_allow_global_access()
447 * @uses bbp_is_user_deleted()
448 * @uses bbp_is_user_spammer()
449 * @uses is_user_logged_in()
450 * @uses current_user_can()
451 * @uses get_option()
452 * @uses bbp_get_caps_for_role()
453 *
454 * @global bbPress $bbp
455 * @return If not multisite, not global, or user is deleted/spammed
456 */
457 function bbp_global_access_role_mask() {
458
459 // Bail if not multisite or forum is not global
460 if ( !is_multisite() || !bbp_allow_global_access() )
461 return;
462
463 // Bail if user is marked as spam or is deleted
464 if ( bbp_is_user_deleted() || bbp_is_user_spammer() )
465 return;
466
467 // Normal user is logged in but has no caps
468 if ( is_user_logged_in() && current_user_can( 'exist' ) && !current_user_can( 'read' ) ) {
469 global $bbp;
470
471 // Get default role for this site
472 $default_role = get_option( 'default_role' );
473
474 // Get bbPress caps for the default role
475 $caps_for_role = bbp_get_caps_for_role( $default_role );
476
477 // Set all caps to true
478 foreach ( $caps_for_role as $cap ) {
479 $mapped_meta_caps[$cap] = true;
480 }
481
482 // Add 'read' cap just in case
483 $mapped_meta_caps['read'] = true;
484
485 // Allow global access caps to be manipulated
486 $mapped_meta_caps = apply_filters( 'bbp_global_access_mapped_meta_caps', $mapped_meta_caps );
487
488 // Assign the role and mapped caps to the current user
489 $bbp->current_user->roles[0] = $default_role;
490 $bbp->current_user->caps = $mapped_meta_caps;
491 $bbp->current_user->allcaps = $mapped_meta_caps;
492 }
493 }
494
495 ?>
496