PluginProbe
bbPress / 2.2.2
bbPress v2.2.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 / users / capabilities.php

capabilities.php in bbPress 2.2.2, at includes/users/capabilities.php

647 lines 16.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress User Capabilites
5 *
6 * Used to map user capabilities to WordPress's existing capabilities.
7 *
8 * @package bbPress
9 * @subpackage Capabilities
10 */
11
12 /**
13 * Maps primary capabilities
14 *
15 * @since bbPress (r4242)
16 *
17 * @param array $caps Capabilities for meta capability
18 * @param string $cap Capability name
19 * @param int $user_id User id
20 * @param mixed $args Arguments
21 * @uses apply_filters() Filter mapped results
22 * @return array Actual capabilities for meta capability
23 */
24 function bbp_map_primary_meta_caps( $caps = array(), $cap = '', $user_id = 0, $args = array() ) {
25
26 // What capability is being checked?
27 switch ( $cap ) {
28 case 'spectate' :
29 case 'participate' :
30 case 'moderate' :
31
32 // Do not allow inactive users
33 if ( bbp_is_user_inactive( $user_id ) ) {
34 $caps = array( 'do_not_allow' );
35
36 // Moderators are always participants
37 } else {
38 $caps = array( $cap );
39 }
40
41 break;
42 }
43
44 return apply_filters( 'bbp_map_primary_meta_caps', $caps, $cap, $user_id, $args );
45 }
46
47 /**
48 * Return a user's main role
49 *
50 * @since bbPress (r3860)
51 *
52 * @param int $user_id
53 * @uses bbp_get_user_id() To get the user id
54 * @uses get_userdata() To get the user data
55 * @uses apply_filters() Calls 'bbp_set_user_role' with the role and user id
56 * @return string
57 */
58 function bbp_set_user_role( $user_id = 0, $new_role = '' ) {
59
60 // Validate user id
61 $user_id = bbp_get_user_id( $user_id, false, false );
62 $user = get_userdata( $user_id );
63
64 // User exists
65 if ( !empty( $user ) ) {
66
67 // Get users forum role
68 $role = bbp_get_user_role( $user_id );
69
70 // User already has this role so no new role is set
71 if ( $new_role == $role ) {
72 $new_role = false;
73
74 // Users role is different than the new role
75 } else {
76
77 // Remove the old role
78 if ( ! empty( $role ) ) {
79 $user->remove_role( $role );
80 }
81
82 // Add the new role
83 if ( !empty( $new_role ) ) {
84 $user->add_role( $new_role );
85 }
86 }
87
88 // User does don exist so return false
89 } else {
90 $new_role = false;
91 }
92
93 return apply_filters( 'bbp_set_user_role', $new_role, $user_id, $user );
94 }
95
96 /**
97 * Return a user's forums role
98 *
99 * @since bbPress (r3860)
100 *
101 * @param int $user_id
102 * @uses bbp_get_user_id() To get the user id
103 * @uses get_userdata() To get the user data
104 * @uses apply_filters() Calls 'bbp_get_user_role' with the role and user id
105 * @return string
106 */
107 function bbp_get_user_role( $user_id = 0 ) {
108
109 // Validate user id
110 $user_id = bbp_get_user_id( $user_id, false, false );
111 $user = get_userdata( $user_id );
112 $role = false;
113
114 // User has roles so lets
115 if ( ! empty( $user->roles ) ) {
116 $roles = array_intersect( array_values( $user->roles ), array_keys( bbp_get_dynamic_roles() ) );
117
118 // If there's a role in the array, use the first one
119 if ( !empty( $roles ) ) {
120 $role = array_shift( array_values( $roles ) );
121 }
122 }
123
124 return apply_filters( 'bbp_get_user_role', $role, $user_id, $user );
125 }
126
127 /**
128 * Return a user's blog role
129 *
130 * @since bbPress (r4446)
131 *
132 * @param int $user_id
133 * @uses bbp_get_user_id() To get the user id
134 * @uses get_userdata() To get the user data
135 * @uses apply_filters() Calls 'bbp_get_user_blog_role' with the role and user id
136 * @return string
137 */
138 function bbp_get_user_blog_role( $user_id = 0 ) {
139 global $wp_roles;
140
141 // This really shold not be necessary anymore, and will likely be removed
142 // at a later date. If roles aren't loaded yet, something else is wrong.
143 if ( ! isset( $wp_roles ) )
144 $wp_roles = new WP_Roles();
145
146 // Validate user id
147 $user_id = bbp_get_user_id( $user_id, false, false );
148 $user = get_userdata( $user_id );
149 $role = false;
150 $all_roles = apply_filters( 'editable_roles', $wp_roles->roles );
151
152 // User has roles so lets
153 if ( ! empty( $user->roles ) ) {
154 $roles = array_intersect( array_values( $user->roles ), array_keys( $all_roles ) );
155
156 // If there's a role in the array, use the first one
157 if ( !empty( $roles ) ) {
158 $role = array_shift( array_values( $roles ) );
159 }
160 }
161
162 return apply_filters( 'bbp_get_user_blog_role', $role, $user_id, $user );
163 }
164
165 /**
166 * Helper function hooked to 'bbp_edit_user_profile_update' action to save or
167 * update user roles and capabilities.
168 *
169 * @since bbPress (r4235)
170 *
171 * @param int $user_id
172 * @uses bbp_reset_user_caps() to reset caps
173 * @usse bbp_save_user_caps() to save caps
174 */
175 function bbp_profile_update_role( $user_id = 0 ) {
176
177 // Bail if no user ID was passed
178 if ( empty( $user_id ) )
179 return;
180
181 // Bail if no role
182 if ( ! isset( $_POST['bbp-forums-role'] ) )
183 return;
184
185 // Fromus role we want the user to have
186 $new_role = sanitize_text_field( $_POST['bbp-forums-role'] );
187 $forums_role = bbp_get_user_role( $user_id );
188
189 // Set the new forums role
190 if ( $new_role != $forums_role ) {
191 bbp_set_user_role( $user_id, $new_role );
192 }
193 }
194
195 /**
196 * Add the default role to the current user if needed
197 *
198 * This function will bail if the forum is not global in a multisite
199 * installation of WordPress, or if the user is marked as spam or deleted.
200 *
201 * @since bbPress (r3380)
202 *
203 * @uses is_user_logged_in() To bail if user is not logged in
204 * @uses bbp_get_user_role() To bail if user already has a role
205 * @uses bbp_is_user_inactive() To bail if user is inactive
206 * @uses bbp_allow_global_access() To know whether to save role to database
207 * @uses bbp_get_user_role_map() To get the WP to BBP role map array
208 * @uses bbp_get_default_role() To get the site's default forums role
209 * @uses get_option()
210 *
211 * @return If not multisite, not global, or user is deleted/spammed
212 */
213 function bbp_set_current_user_default_role() {
214
215 /** Sanity ****************************************************************/
216
217 // Bail if deactivating bbPress
218 if ( bbp_is_deactivation() )
219 return;
220
221 // Catch all, to prevent premature user initialization
222 if ( ! did_action( 'set_current_user' ) )
223 return;
224
225 // Bail if not logged in or already a member of this site
226 if ( ! is_user_logged_in() )
227 return;
228
229 // Get the current user ID
230 $user_id = bbp_get_current_user_id();
231
232 // Bail if user already has a forums role
233 if ( bbp_get_user_role( $user_id ) )
234 return;
235
236 // Bail if user is marked as spam or is deleted
237 if ( bbp_is_user_inactive( $user_id ) )
238 return;
239
240 /** Ready *****************************************************************/
241
242 // Load up bbPress once
243 $bbp = bbpress();
244
245 // Get whether or not to add a role to the user account
246 $add_to_site = bbp_allow_global_access();
247
248 // Get the current user's WordPress role. Set to empty string if none found.
249 $user_role = bbp_get_user_blog_role( $user_id );
250
251 // Get the role map
252 $role_map = bbp_get_user_role_map();
253
254 /** Forum Role ************************************************************/
255
256 // Use a mapped role
257 if ( isset( $role_map[$user_role] ) ) {
258 $new_role = $role_map[$user_role];
259
260 // Use the default role
261 } else {
262 $new_role = bbp_get_default_role();
263 }
264
265 /** Add or Map ************************************************************/
266
267 // Add the user to the site
268 if ( true == $add_to_site ) {
269 $bbp->current_user->add_role( $new_role );
270
271 // Don't add the user, but still give them the correct caps dynamically
272 } else {
273 $bbp->current_user->caps[$new_role] = true;
274 $bbp->current_user->get_role_caps();
275 }
276 }
277
278 /**
279 * Return a map of WordPress roles to bbPress roles. Used to automatically grant
280 * appropriate bbPress roles to WordPress users that wouldn't already have a
281 * role in the forums. Also guarantees WordPress admins get the Keymaster role.
282 *
283 * @since bbPress (r4334)
284 *
285 * @return array Filtered array of WordPress roles to bbPress roles
286 */
287 function bbp_get_user_role_map() {
288
289 // Get the default role once here
290 $default_role = bbp_get_default_role();
291
292 // Return filtered results, forcing admins to keymasters.
293 return (array) apply_filters( 'bbp_get_user_role_map', array (
294 'administrator' => bbp_get_keymaster_role(),
295 'editor' => $default_role,
296 'author' => $default_role,
297 'contributor' => $default_role,
298 'subscriber' => $default_role
299 ) );
300 }
301
302 /** User Status ***************************************************************/
303
304 /**
305 * Checks if the user has been marked as a spammer.
306 *
307 * @since bbPress (r3355)
308 *
309 * @param int $user_id int The ID for the user.
310 * @return bool True if spammer, False if not.
311 */
312 function bbp_is_user_spammer( $user_id = 0 ) {
313
314 // Default to current user
315 if ( empty( $user_id ) && is_user_logged_in() )
316 $user_id = bbp_get_current_user_id();
317
318 // No user to check
319 if ( empty( $user_id ) )
320 return false;
321
322 // Assume user is not spam
323 $is_spammer = false;
324
325 // Get user data
326 $user = get_userdata( $user_id );
327
328 // No user found
329 if ( empty( $user ) ) {
330 $is_spammer = false;
331
332 // Check if spam
333 } elseif ( !empty( $user->spam ) ) {
334 $is_spammer = true;
335 }
336
337 return (bool) apply_filters( 'bp_core_is_user_spammer', $is_spammer );
338 }
339
340 /**
341 * Mark a users topics and replies as spam when the user is marked as spam
342 *
343 * @since bbPress (r3405)
344 *
345 * @global WPDB $wpdb
346 * @param int $user_id Optional. User ID to spam. Defaults to displayed user.
347
348 * @uses bbp_is_single_user()
349 * @uses bbp_is_user_home()
350 * @uses bbp_get_displayed_user_field()
351 * @uses is_super_admin()
352 * @uses get_blogs_of_user()
353 * @uses get_current_blog_id()
354 * @uses bbp_get_topic_post_type()
355 * @uses bbp_get_reply_post_type()
356 * @uses switch_to_blog()
357 * @uses get_post_type()
358 * @uses bbp_spam_topic()
359 * @uses bbp_spam_reply()
360 * @uses restore_current_blog()
361 *
362 * @return If no user ID passed
363 */
364 function bbp_make_spam_user( $user_id = 0 ) {
365
366 // Use displayed user if it's not yourself
367 if ( empty( $user_id ) && bbp_is_single_user() && !bbp_is_user_home() )
368 $user_id = bbp_get_displayed_user_id();
369
370 // Bail if no user ID
371 if ( empty( $user_id ) )
372 return;
373
374 // Bail if user ID is super admin
375 if ( is_super_admin( $user_id ) )
376 return;
377
378 // Arm the torpedos
379 global $wpdb;
380
381 // Get the blog IDs of the user to mark as spam
382 $blogs = get_blogs_of_user( $user_id, true );
383
384 // If user has no blogs, they are a guest on this site
385 if ( empty( $blogs ) )
386 $blogs[$wpdb->blogid] = array();
387
388 // Make array of post types to mark as spam
389 $post_types = array( bbp_get_topic_post_type(), bbp_get_reply_post_type() );
390 $post_types = "'" . implode( "', '", $post_types ) . "'";
391 $status = bbp_get_public_status_id();
392
393 // Loop through blogs and remove their posts
394 foreach ( (array) array_keys( $blogs ) as $blog_id ) {
395
396 // Switch to the blog ID
397 switch_to_blog( $blog_id );
398
399 // Get topics and replies
400 $posts = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_author = {$user_id} AND post_status = '{$status}' AND post_type IN ({$post_types})" );
401
402 // Loop through posts and spam them
403 if ( !empty( $posts ) ) {
404 foreach ( $posts as $post_id ) {
405
406 // The routines for topics ang replies are different, so use the
407 // correct one based on the post type
408 switch ( get_post_type( $post_id ) ) {
409
410 case bbp_get_topic_post_type() :
411 bbp_spam_topic( $post_id );
412 break;
413
414 case bbp_get_reply_post_type() :
415 bbp_spam_reply( $post_id );
416 break;
417 }
418 }
419 }
420
421 // Switch back to current blog
422 restore_current_blog();
423 }
424 }
425
426 /**
427 * Mark a users topics and replies as spam when the user is marked as spam
428 *
429 * @since bbPress (r3405)
430 *
431 * @global WPDB $wpdb
432 * @param int $user_id Optional. User ID to unspam. Defaults to displayed user.
433 *
434 * @uses bbp_is_single_user()
435 * @uses bbp_is_user_home()
436 * @uses bbp_get_displayed_user_field()
437 * @uses is_super_admin()
438 * @uses get_blogs_of_user()
439 * @uses bbp_get_topic_post_type()
440 * @uses bbp_get_reply_post_type()
441 * @uses switch_to_blog()
442 * @uses get_post_type()
443 * @uses bbp_unspam_topic()
444 * @uses bbp_unspam_reply()
445 * @uses restore_current_blog()
446 *
447 * @return If no user ID passed
448 */
449 function bbp_make_ham_user( $user_id = 0 ) {
450
451 // Use displayed user if it's not yourself
452 if ( empty( $user_id ) && bbp_is_single_user() && !bbp_is_user_home() )
453 $user_id = bbp_get_displayed_user_field();
454
455 // Bail if no user ID
456 if ( empty( $user_id ) )
457 return;
458
459 // Bail if user ID is super admin
460 if ( is_super_admin( $user_id ) )
461 return;
462
463 // Arm the torpedos
464 global $wpdb;
465
466 // Get the blog IDs of the user to mark as spam
467 $blogs = get_blogs_of_user( $user_id, true );
468
469 // If user has no blogs, they are a guest on this site
470 if ( empty( $blogs ) )
471 $blogs[$wpdb->blogid] = array();
472
473 // Make array of post types to mark as spam
474 $post_types = array( bbp_get_topic_post_type(), bbp_get_reply_post_type() );
475 $post_types = "'" . implode( "', '", $post_types ) . "'";
476 $status = bbp_get_spam_status_id();
477
478 // Loop through blogs and remove their posts
479 foreach ( (array) array_keys( $blogs ) as $blog_id ) {
480
481 // Switch to the blog ID
482 switch_to_blog( $blog_id );
483
484 // Get topics and replies
485 $posts = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_author = {$user_id} AND post_status = '{$status}' AND post_type IN ({$post_types})" );
486
487 // Loop through posts and spam them
488 if ( !empty( $posts ) ) {
489 foreach ( $posts as $post_id ) {
490
491 // The routines for topics ang replies are different, so use the
492 // correct one based on the post type
493 switch ( get_post_type( $post_id ) ) {
494
495 case bbp_get_topic_post_type() :
496 bbp_unspam_topic( $post_id );
497 break;
498
499 case bbp_get_reply_post_type() :
500 bbp_unspam_reply( $post_id );
501 break;
502 }
503 }
504 }
505
506 // Switch back to current blog
507 restore_current_blog();
508 }
509 }
510
511 /**
512 * Checks if the user has been marked as deleted.
513 *
514 * @since bbPress (r3355)
515 *
516 * @param int $user_id int The ID for the user.
517 * @return bool True if deleted, False if not.
518 */
519 function bbp_is_user_deleted( $user_id = 0 ) {
520
521 // Default to current user
522 if ( empty( $user_id ) && is_user_logged_in() )
523 $user_id = bbp_get_current_user_id();
524
525 // No user to check
526 if ( empty( $user_id ) )
527 return false;
528
529 // Assume user is not deleted
530 $is_deleted = false;
531
532 // Get user data
533 $user = get_userdata( $user_id );
534
535 // No user found
536 if ( empty( $user ) ) {
537 $is_deleted = true;
538
539 // Check if deleted
540 } elseif ( !empty( $user->deleted ) ) {
541 $is_deleted = true;
542 }
543
544 return (bool) apply_filters( 'bp_core_is_user_deleted', $is_deleted );
545 }
546
547 /**
548 * Checks if user is active
549 *
550 * @since bbPress (r3502)
551 *
552 * @uses is_user_logged_in() To check if user is logged in
553 * @uses bbp_get_displayed_user_id() To get current user ID
554 * @uses bbp_is_user_spammer() To check if user is spammer
555 * @uses bbp_is_user_deleted() To check if user is deleted
556 *
557 * @param int $user_id The user ID to check
558 * @return bool True if public, false if not
559 */
560 function bbp_is_user_active( $user_id = 0 ) {
561
562 // Default to current user
563 if ( empty( $user_id ) && is_user_logged_in() )
564 $user_id = bbp_get_current_user_id();
565
566 // No user to check
567 if ( empty( $user_id ) )
568 return false;
569
570 // Check spam
571 if ( bbp_is_user_spammer( $user_id ) )
572 return false;
573
574 // Check deleted
575 if ( bbp_is_user_deleted( $user_id ) )
576 return false;
577
578 // Assume true if not spam or deleted
579 return true;
580 }
581
582 /**
583 * Checks if user is not active.
584 *
585 * @since bbPress (r3502)
586 *
587 * @uses is_user_logged_in() To check if user is logged in
588 * @uses bbp_get_displayed_user_id() To get current user ID
589 * @uses bbp_is_user_active() To check if user is active
590 *
591 * @param int $user_id The user ID to check. Defaults to current user ID
592 * @return bool True if inactive, false if active
593 */
594 function bbp_is_user_inactive( $user_id = 0 ) {
595
596 // Default to current user
597 if ( empty( $user_id ) && is_user_logged_in() )
598 $user_id = bbp_get_current_user_id();
599
600 // No user to check
601 if ( empty( $user_id ) )
602 return false;
603
604 // Return the inverse of active
605 return !bbp_is_user_active( $user_id );
606 }
607
608 /**
609 * Does a user have a profile for the current site
610 *
611 * @since bbPress (r4362)
612 *
613 * @param int $user_id User ID to check
614 * @param int $blog_id Blog ID to check
615 *
616 * @uses bbp_get_user_id() To verify the user ID
617 * @uses get_userdata() To get the user's data
618 * @uses is_super_admin() To determine if user can see inactive users
619 * @uses bbp_is_user_inactive() To check if user is spammer or deleted
620 * @uses apply_filters() To allow override of this functions result
621 *
622 * @return boolean Whether or not the user has a profile on this blog_id
623 */
624 function bbp_user_has_profile( $user_id = 0 ) {
625
626 // Assume every user has a profile
627 $retval = true;
628
629 // Validate user ID, default to displayed or current user
630 $user_id = bbp_get_user_id( $user_id, true, true );
631
632 // Try to get this user's data
633 $user = get_userdata( $user_id );
634
635 // No user found, return false
636 if ( empty( $user ) ) {
637 $retval = false;
638
639 // User is inactive, and current user is not a super admin
640 } elseif ( ! is_super_admin() && bbp_is_user_inactive( $user->ID ) ) {
641 $retval = false;
642 }
643
644 // Filter and return
645 return (bool) apply_filters( 'bbp_show_user_profile', $retval, $user_id );
646 }
647