PluginProbe
bbPress / 2.2
bbPress v2.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, at includes/users/capabilities.php

614 lines 15.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 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 main 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 * Helper function hooked to 'bbp_edit_user_profile_update' action to save or
129 * update user roles and capabilities.
130 *
131 * @since bbPress (r4235)
132 *
133 * @param int $user_id
134 * @uses bbp_reset_user_caps() to reset caps
135 * @usse bbp_save_user_caps() to save caps
136 */
137 function bbp_profile_update_role( $user_id = 0 ) {
138
139 // Bail if no user ID was passed
140 if ( empty( $user_id ) )
141 return;
142
143 // Bail if no role
144 if ( ! isset( $_POST['bbp-forums-role'] ) )
145 return;
146
147 // Fromus role we want the user to have
148 $new_role = sanitize_text_field( $_POST['bbp-forums-role'] );
149 $forums_role = bbp_get_user_role( $user_id );
150
151 // Set the new forums role
152 if ( $new_role != $forums_role ) {
153 bbp_set_user_role( $user_id, $new_role );
154 }
155 }
156
157 /**
158 * Add the default role to the current user if needed
159 *
160 * This function will bail if the forum is not global in a multisite
161 * installation of WordPress, or if the user is marked as spam or deleted.
162 *
163 * @since bbPress (r3380)
164 *
165 * @uses is_user_logged_in() To bail if user is not logged in
166 * @uses bbp_get_user_role() To bail if user already has a role
167 * @uses bbp_is_user_inactive() To bail if user is inactive
168 * @uses bbp_allow_global_access() To know whether to save role to database
169 * @uses bbp_get_user_role_map() To get the WP to BBP role map array
170 * @uses bbp_get_default_role() To get the site's default forums role
171 * @uses get_option()
172 *
173 * @return If not multisite, not global, or user is deleted/spammed
174 */
175 function bbp_set_current_user_default_role() {
176
177 /** Sanity ****************************************************************/
178
179 // Bail if deactivating bbPress
180 if ( bbp_is_deactivation() )
181 return;
182
183 // Catch all, to prevent premature user initialization
184 if ( ! did_action( 'set_current_user' ) )
185 return;
186
187 // Bail if not logged in or already a member of this site
188 if ( ! is_user_logged_in() )
189 return;
190
191 // Get the current user ID
192 $user_id = bbp_get_current_user_id();
193
194 // Bail if user already has a forums role
195 if ( bbp_get_user_role( $user_id ) )
196 return;
197
198 // Bail if user is marked as spam or is deleted
199 if ( bbp_is_user_inactive( $user_id ) )
200 return;
201
202 /** Ready *****************************************************************/
203
204 // Load up bbPress once
205 $bbp = bbpress();
206
207 // Get whether or not to add a role to the user account
208 $add_to_site = bbp_allow_global_access();
209
210 // Get the current user's WordPress role. Set to empty string if none found.
211 $user_role = isset( $bbp->current_user->roles ) ? array_shift( $bbp->current_user->roles ) : '';
212
213 // Loop through the role map, and grant the proper bbPress role
214 foreach ( (array) bbp_get_user_role_map() as $wp_role => $bbp_role ) {
215
216 // User's role matches a possible WordPress role (including none at all)
217 if ( $user_role == $wp_role ) {
218
219 // Add role to user account, making them a user of this site
220 if ( true == $add_to_site ) {
221
222 // Override map to prevent accidental "Visitor"
223 if ( empty( $wp_role ) ) {
224 $bbp_role = bbp_get_default_role();
225 }
226
227 // Add the mapped forums role to the user's account
228 $bbp->current_user->add_role( $bbp_role );
229
230 // @todo Add the default site role too?
231 //$bbp->current_user->add_role( get_option( 'default_role' ) );
232
233 // Dynamically assign capabilities, making them "anonymous"
234 } else {
235 $bbp->current_user->caps[$bbp_role] = true;
236 $bbp->current_user->get_role_caps();
237 }
238
239 break;
240 }
241 }
242 }
243
244 /**
245 * Return a map of WordPress roles to bbPress roles. Used to automatically grant
246 * appropriate bbPress roles to WordPress users that wouldn't already have a
247 * role in the forums. Also guarantees WordPress admins get the Keymaster role.
248 *
249 * @since bbPress (r4334)
250 *
251 * @return array Filtered array of WordPress roles to bbPress roles
252 */
253 function bbp_get_user_role_map() {
254
255 // Get the default role once here
256 $default_role = bbp_get_default_role();
257
258 // Return filtered results, forcing admins to keymasters.
259 return (array) apply_filters( 'bbp_get_user_role_map', array (
260 'administrator' => bbp_get_keymaster_role(),
261 'editor' => $default_role,
262 'author' => $default_role,
263 'contributor' => $default_role,
264 'subscriber' => $default_role,
265 '' => bbp_get_visitor_role()
266 ) );
267 }
268
269 /** User Status ***************************************************************/
270
271 /**
272 * Checks if the user has been marked as a spammer.
273 *
274 * @since bbPress (r3355)
275 *
276 * @param int $user_id int The ID for the user.
277 * @return bool True if spammer, False if not.
278 */
279 function bbp_is_user_spammer( $user_id = 0 ) {
280
281 // Default to current user
282 if ( empty( $user_id ) && is_user_logged_in() )
283 $user_id = bbp_get_current_user_id();
284
285 // No user to check
286 if ( empty( $user_id ) )
287 return false;
288
289 // Assume user is not spam
290 $is_spammer = false;
291
292 // Get user data
293 $user = get_userdata( $user_id );
294
295 // No user found
296 if ( empty( $user ) ) {
297 $is_spammer = false;
298
299 // Check if spam
300 } elseif ( !empty( $user->spam ) ) {
301 $is_spammer = true;
302 }
303
304 return (bool) apply_filters( 'bp_core_is_user_spammer', $is_spammer );
305 }
306
307 /**
308 * Mark a users topics and replies as spam when the user is marked as spam
309 *
310 * @since bbPress (r3405)
311 *
312 * @global WPDB $wpdb
313 * @param int $user_id Optional. User ID to spam. Defaults to displayed user.
314
315 * @uses bbp_is_single_user()
316 * @uses bbp_is_user_home()
317 * @uses bbp_get_displayed_user_field()
318 * @uses is_super_admin()
319 * @uses get_blogs_of_user()
320 * @uses get_current_blog_id()
321 * @uses bbp_get_topic_post_type()
322 * @uses bbp_get_reply_post_type()
323 * @uses switch_to_blog()
324 * @uses get_post_type()
325 * @uses bbp_spam_topic()
326 * @uses bbp_spam_reply()
327 * @uses restore_current_blog()
328 *
329 * @return If no user ID passed
330 */
331 function bbp_make_spam_user( $user_id = 0 ) {
332
333 // Use displayed user if it's not yourself
334 if ( empty( $user_id ) && bbp_is_single_user() && !bbp_is_user_home() )
335 $user_id = bbp_get_displayed_user_id();
336
337 // Bail if no user ID
338 if ( empty( $user_id ) )
339 return;
340
341 // Bail if user ID is super admin
342 if ( is_super_admin( $user_id ) )
343 return;
344
345 // Arm the torpedos
346 global $wpdb;
347
348 // Get the blog IDs of the user to mark as spam
349 $blogs = get_blogs_of_user( $user_id, true );
350
351 // If user has no blogs, they are a guest on this site
352 if ( empty( $blogs ) )
353 $blogs[$wpdb->blogid] = array();
354
355 // Make array of post types to mark as spam
356 $post_types = array( bbp_get_topic_post_type(), bbp_get_reply_post_type() );
357 $post_types = "'" . implode( "', '", $post_types ) . "'";
358 $status = bbp_get_public_status_id();
359
360 // Loop through blogs and remove their posts
361 foreach ( (array) array_keys( $blogs ) as $blog_id ) {
362
363 // Switch to the blog ID
364 switch_to_blog( $blog_id );
365
366 // Get topics and replies
367 $posts = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_author = {$user_id} AND post_status = '{$status}' AND post_type IN ({$post_types})" );
368
369 // Loop through posts and spam them
370 if ( !empty( $posts ) ) {
371 foreach ( $posts as $post_id ) {
372
373 // The routines for topics ang replies are different, so use the
374 // correct one based on the post type
375 switch ( get_post_type( $post_id ) ) {
376
377 case bbp_get_topic_post_type() :
378 bbp_spam_topic( $post_id );
379 break;
380
381 case bbp_get_reply_post_type() :
382 bbp_spam_reply( $post_id );
383 break;
384 }
385 }
386 }
387
388 // Switch back to current blog
389 restore_current_blog();
390 }
391 }
392
393 /**
394 * Mark a users topics and replies as spam when the user is marked as spam
395 *
396 * @since bbPress (r3405)
397 *
398 * @global WPDB $wpdb
399 * @param int $user_id Optional. User ID to unspam. Defaults to displayed user.
400 *
401 * @uses bbp_is_single_user()
402 * @uses bbp_is_user_home()
403 * @uses bbp_get_displayed_user_field()
404 * @uses is_super_admin()
405 * @uses get_blogs_of_user()
406 * @uses bbp_get_topic_post_type()
407 * @uses bbp_get_reply_post_type()
408 * @uses switch_to_blog()
409 * @uses get_post_type()
410 * @uses bbp_unspam_topic()
411 * @uses bbp_unspam_reply()
412 * @uses restore_current_blog()
413 *
414 * @return If no user ID passed
415 */
416 function bbp_make_ham_user( $user_id = 0 ) {
417
418 // Use displayed user if it's not yourself
419 if ( empty( $user_id ) && bbp_is_single_user() && !bbp_is_user_home() )
420 $user_id = bbp_get_displayed_user_field();
421
422 // Bail if no user ID
423 if ( empty( $user_id ) )
424 return;
425
426 // Bail if user ID is super admin
427 if ( is_super_admin( $user_id ) )
428 return;
429
430 // Arm the torpedos
431 global $wpdb;
432
433 // Get the blog IDs of the user to mark as spam
434 $blogs = get_blogs_of_user( $user_id, true );
435
436 // If user has no blogs, they are a guest on this site
437 if ( empty( $blogs ) )
438 $blogs[$wpdb->blogid] = array();
439
440 // Make array of post types to mark as spam
441 $post_types = array( bbp_get_topic_post_type(), bbp_get_reply_post_type() );
442 $post_types = "'" . implode( "', '", $post_types ) . "'";
443 $status = bbp_get_spam_status_id();
444
445 // Loop through blogs and remove their posts
446 foreach ( (array) array_keys( $blogs ) as $blog_id ) {
447
448 // Switch to the blog ID
449 switch_to_blog( $blog_id );
450
451 // Get topics and replies
452 $posts = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_author = {$user_id} AND post_status = '{$status}' AND post_type IN ({$post_types})" );
453
454 // Loop through posts and spam them
455 if ( !empty( $posts ) ) {
456 foreach ( $posts as $post_id ) {
457
458 // The routines for topics ang replies are different, so use the
459 // correct one based on the post type
460 switch ( get_post_type( $post_id ) ) {
461
462 case bbp_get_topic_post_type() :
463 bbp_unspam_topic( $post_id );
464 break;
465
466 case bbp_get_reply_post_type() :
467 bbp_unspam_reply( $post_id );
468 break;
469 }
470 }
471 }
472
473 // Switch back to current blog
474 restore_current_blog();
475 }
476 }
477
478 /**
479 * Checks if the user has been marked as deleted.
480 *
481 * @since bbPress (r3355)
482 *
483 * @param int $user_id int The ID for the user.
484 * @return bool True if deleted, False if not.
485 */
486 function bbp_is_user_deleted( $user_id = 0 ) {
487
488 // Default to current user
489 if ( empty( $user_id ) && is_user_logged_in() )
490 $user_id = bbp_get_current_user_id();
491
492 // No user to check
493 if ( empty( $user_id ) )
494 return false;
495
496 // Assume user is not deleted
497 $is_deleted = false;
498
499 // Get user data
500 $user = get_userdata( $user_id );
501
502 // No user found
503 if ( empty( $user ) ) {
504 $is_deleted = true;
505
506 // Check if deleted
507 } elseif ( !empty( $user->deleted ) ) {
508 $is_deleted = true;
509 }
510
511 return (bool) apply_filters( 'bp_core_is_user_deleted', $is_deleted );
512 }
513
514 /**
515 * Checks if user is active
516 *
517 * @since bbPress (r3502)
518 *
519 * @uses is_user_logged_in() To check if user is logged in
520 * @uses bbp_get_displayed_user_id() To get current user ID
521 * @uses bbp_is_user_spammer() To check if user is spammer
522 * @uses bbp_is_user_deleted() To check if user is deleted
523 *
524 * @param int $user_id The user ID to check
525 * @return bool True if public, false if not
526 */
527 function bbp_is_user_active( $user_id = 0 ) {
528
529 // Default to current user
530 if ( empty( $user_id ) && is_user_logged_in() )
531 $user_id = bbp_get_current_user_id();
532
533 // No user to check
534 if ( empty( $user_id ) )
535 return false;
536
537 // Check spam
538 if ( bbp_is_user_spammer( $user_id ) )
539 return false;
540
541 // Check deleted
542 if ( bbp_is_user_deleted( $user_id ) )
543 return false;
544
545 // Assume true if not spam or deleted
546 return true;
547 }
548
549 /**
550 * Checks if user is not active.
551 *
552 * @since bbPress (r3502)
553 *
554 * @uses is_user_logged_in() To check if user is logged in
555 * @uses bbp_get_displayed_user_id() To get current user ID
556 * @uses bbp_is_user_active() To check if user is active
557 *
558 * @param int $user_id The user ID to check. Defaults to current user ID
559 * @return bool True if inactive, false if active
560 */
561 function bbp_is_user_inactive( $user_id = 0 ) {
562
563 // Default to current user
564 if ( empty( $user_id ) && is_user_logged_in() )
565 $user_id = bbp_get_current_user_id();
566
567 // No user to check
568 if ( empty( $user_id ) )
569 return false;
570
571 // Return the inverse of active
572 return !bbp_is_user_active( $user_id );
573 }
574
575 /**
576 * Does a user have a profile for the current site
577 *
578 * @since bbPress (r4362)
579 *
580 * @param int $user_id User ID to check
581 * @param int $blog_id Blog ID to check
582 *
583 * @uses bbp_get_user_id() To verify the user ID
584 * @uses get_userdata() To get the user's data
585 * @uses is_super_admin() To determine if user can see inactive users
586 * @uses bbp_is_user_inactive() To check if user is spammer or deleted
587 * @uses apply_filters() To allow override of this functions result
588 *
589 * @return boolean Whether or not the user has a profile on this blog_id
590 */
591 function bbp_user_has_profile( $user_id = 0 ) {
592
593 // Assume every user has a profile
594 $retval = true;
595
596 // Validate user ID, default to displayed or current user
597 $user_id = bbp_get_user_id( $user_id, true, true );
598
599 // Try to get this user's data
600 $user = get_userdata( $user_id );
601
602 // No user found, return false
603 if ( empty( $user ) ) {
604 $retval = false;
605
606 // User is inactive, and current user is not a super admin
607 } elseif ( ! is_super_admin() && bbp_is_user_inactive( $user->ID ) ) {
608 $retval = false;
609 }
610
611 // Filter and return
612 return (bool) apply_filters( 'bbp_show_user_profile', $retval, $user_id );
613 }
614