PluginProbe
bbPress / 2.1-rc2
bbPress v2.1-rc2
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-user-functions.php

bbp-user-functions.php in bbPress 2.1-rc2, at bbp-includes/bbp-user-functions.php

1,425 lines 43.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 User Functions
5 *
6 * @package bbPress
7 * @subpackage Functions
8 */
9
10 // Exit if accessed directly
11 if ( !defined( 'ABSPATH' ) ) exit;
12
13 /**
14 * Redirect back to $url when attempting to use the login page
15 *
16 * @since bbPress (r2815)
17 *
18 * @param string $url The url
19 * @param string $raw_url Raw url
20 * @param object $user User object
21 * @uses is_wp_error() To check if the user param is a {@link WP_Error}
22 * @uses admin_url() To get the admin url
23 * @uses home_url() To get the home url
24 * @uses esc_url() To escape the url
25 * @uses wp_safe_redirect() To redirect
26 */
27 function bbp_redirect_login( $url = '', $raw_url = '', $user = '' ) {
28
29 // Raw redirect_to was passed, so use it
30 if ( !empty( $raw_url ) )
31 $url = $raw_url;
32
33 // $url was manually set in wp-login.php to redirect to admin
34 elseif ( admin_url() == $url )
35 $url = home_url();
36
37 // $url is empty
38 elseif ( empty( $url ) )
39 $url = home_url();
40
41 return apply_filters( 'bbp_redirect_login', $url, $raw_url, $user );
42 }
43
44 /**
45 * Is an anonymous topic/reply being made?
46 *
47 * @since bbPres (r2688)
48 *
49 * @uses is_user_logged_in() Is the user logged in?
50 * @uses bbp_allow_anonymous() Is anonymous posting allowed?
51 * @uses apply_filters() Calls 'bbp_is_anonymous' with the return value
52 * @return bool True if anonymous is allowed and user is not logged in, false if
53 * anonymous is not allowed or user is logged in
54 */
55 function bbp_is_anonymous() {
56 if ( !is_user_logged_in() && bbp_allow_anonymous() )
57 $is_anonymous = true;
58 else
59 $is_anonymous = false;
60
61 return apply_filters( 'bbp_is_anonymous', $is_anonymous );
62 }
63
64 /**
65 * Echoes the values for current poster (uses WP comment cookies)
66 *
67 * @since bbPress (r2734)
68 *
69 * @param string $key Which value to echo?
70 * @uses bbp_get_current_anonymous_user_data() To get the current anonymous user
71 * data
72 */
73 function bbp_current_anonymous_user_data( $key = '' ) {
74 echo bbp_get_current_anonymous_user_data( $key );
75 }
76
77 /**
78 * Get the cookies for current poster (uses WP comment cookies).
79 *
80 * @since bbPress (r2734)
81 *
82 * @param string $key Optional. Which value to get? If not given, then
83 * an array is returned.
84 * @uses sanitize_comment_cookies() To sanitize the current poster data
85 * @uses wp_get_current_commenter() To get the current poster data *
86 * @return string|array Cookie(s) for current poster
87 */
88 function bbp_get_current_anonymous_user_data( $key = '' ) {
89 $cookie_names = array(
90 'name' => 'comment_author',
91 'email' => 'comment_author_email',
92 'website' => 'comment_author_url',
93
94 // Here just for the sake of them, use the above ones
95 'comment_author' => 'comment_author',
96 'comment_author_email' => 'comment_author_email',
97 'comment_author_url' => 'comment_author_url',
98 );
99
100 sanitize_comment_cookies();
101
102 $bbp_current_poster = wp_get_current_commenter();
103
104 if ( !empty( $key ) && in_array( $key, array_keys( $cookie_names ) ) )
105 return $bbp_current_poster[$cookie_names[$key]];
106
107 return $bbp_current_poster;
108 }
109
110 /**
111 * Set the cookies for current poster (uses WP comment cookies)
112 *
113 * @since bbPress (r2734)
114 *
115 * @param array $anonymous_data With keys 'bbp_anonymous_name',
116 * 'bbp_anonymous_email', 'bbp_anonymous_website'.
117 * Should be sanitized (see
118 * {@link bbp_filter_anonymous_post_data()} for
119 * sanitization)
120 * @uses apply_filters() Calls 'comment_cookie_lifetime' for cookie lifetime.
121 * Defaults to 30000000.
122 */
123 function bbp_set_current_anonymous_user_data( $anonymous_data = array() ) {
124 if ( empty( $anonymous_data ) || !is_array( $anonymous_data ) )
125 return;
126
127 $comment_cookie_lifetime = apply_filters( 'comment_cookie_lifetime', 30000000 );
128
129 setcookie( 'comment_author_' . COOKIEHASH, $anonymous_data['bbp_anonymous_name'], time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN );
130 setcookie( 'comment_author_email_' . COOKIEHASH, $anonymous_data['bbp_anonymous_email'], time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN );
131 setcookie( 'comment_author_url_' . COOKIEHASH, $anonymous_data['bbp_anonymous_website'], time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN );
132 }
133
134 /**
135 * Get the poster IP address
136 *
137 * @since bbPress (r3120)
138 *
139 * @return string
140 */
141 function bbp_current_author_ip() {
142 $retval = preg_replace( '/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR'] );
143
144 return apply_filters( 'bbp_current_author_ip', $retval );
145 }
146
147 /**
148 * Get the poster user agent
149 *
150 * @since bbPress (r3446)
151 *
152 * @return string
153 */
154 function bbp_current_author_ua() {
155
156 // Sanity check the user agent
157 if ( !empty( $_SERVER['HTTP_USER_AGENT'] ) )
158 $retval = substr( $_SERVER['HTTP_USER_AGENT'], 0, 254 );
159 else
160 $retval = '';
161
162 return apply_filters( 'bbp_current_author_ua', $retval );
163 }
164
165 /** Post Counts ***************************************************************/
166
167 /**
168 * Return the raw database count of topics by a user
169 *
170 * @since bbPress (r3633)
171 * @global WPDB $wpdb
172 * @uses bbp_get_user_id()
173 * @uses get_posts_by_author_sql()
174 * @uses bbp_get_topic_post_type()
175 * @uses apply_filters()
176 * @return int Raw DB count of topics
177 */
178 function bbp_get_user_topic_count_raw( $user_id = 0 ) {
179 $user_id = bbp_get_user_id( $user_id );
180 if ( empty( $user_id ) )
181 return false;
182
183 global $wpdb;
184
185 $where = get_posts_by_author_sql( bbp_get_topic_post_type(), true, $user_id );
186 $count = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts} {$where}" );
187
188 return (int) apply_filters( 'bbp_get_user_topic_count_raw', $count, $user_id );
189 }
190
191 /**
192 * Return the raw database count of replies by a user
193 *
194 * @since bbPress (r3633)
195 * @global WPDB $wpdb
196 * @uses bbp_get_user_id()
197 * @uses get_posts_by_author_sql()
198 * @uses bbp_get_reply_post_type()
199 * @uses apply_filters()
200 * @return int Raw DB count of replies
201 */
202 function bbp_get_user_reply_count_raw( $user_id = 0 ) {
203 $user_id = bbp_get_user_id( $user_id );
204 if ( empty( $user_id ) )
205 return false;
206
207 global $wpdb;
208
209 $where = get_posts_by_author_sql( bbp_get_reply_post_type(), true, $user_id );
210 $count = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts} {$where}" );
211
212 return (int) apply_filters( 'bbp_get_user_reply_count_raw', $count, $user_id );
213 }
214
215 /** Favorites *****************************************************************/
216
217 /**
218 * Get the users who have made the topic favorite
219 *
220 * @since bbPress (r2658)
221 *
222 * @param int $topic_id Optional. Topic id
223 * @uses wpdb::get_col() To execute our query and get the column back
224 * @uses apply_filters() Calls 'bbp_get_topic_favoriters' with the users and
225 * topic id
226 * @return array|bool Results if the topic has any favoriters, otherwise false
227 */
228 function bbp_get_topic_favoriters( $topic_id = 0 ) {
229 if ( empty( $topic_id ) )
230 return;
231
232 global $wpdb;
233
234 // Get the users who have favorited the topic
235 $key = $wpdb->prefix . '_bbp_favorites';
236 $users = $wpdb->get_col( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$topic_id}', meta_value) > 0" );
237 $users = apply_filters( 'bbp_get_topic_favoriters', $users, $topic_id );
238
239 if ( !empty( $users ) )
240 return $users;
241
242 return false;
243 }
244
245 /**
246 * Get a user's favorite topics
247 *
248 * @since bbPress (r2652)
249 *
250 * @param int $user_id Optional. User id
251 * @uses bbp_get_user_favorites_topic_ids() To get the user's favorites
252 * @uses bbp_has_topics() To get the topics
253 * @uses apply_filters() Calls 'bbp_get_user_favorites' with the topic query and
254 * user id
255 * @return array|bool Results if user has favorites, otherwise false
256 */
257 function bbp_get_user_favorites( $user_id = 0 ) {
258 $user_id = bbp_get_user_id( $user_id );
259 if ( empty( $user_id ) )
260 return false;
261
262 // If user has favorites, load them
263 $favorites = bbp_get_user_favorites_topic_ids( $user_id );
264 if ( !empty( $favorites ) ) {
265
266 // Setup the topics query
267 $topics_query = bbp_has_topics( array( 'post__in' => $favorites ) );
268
269 return apply_filters( 'bbp_get_user_favorites', $topics_query, $user_id );
270 }
271
272 return false;
273 }
274
275 /**
276 * Get a user's favorite topics' ids
277 *
278 * @since bbPress (r2652)
279 *
280 * @param int $user_id Optional. User id
281 * @uses bbp_get_user_id() To get the user id
282 * @uses get_user_option() To get the user favorites
283 * @uses apply_filters() Calls 'bbp_get_user_favorites_topic_ids' with
284 * the favorites and user id
285 * @return array|bool Results if user has favorites, otherwise false
286 */
287 function bbp_get_user_favorites_topic_ids( $user_id = 0 ) {
288 $user_id = bbp_get_user_id( $user_id );
289 if ( empty( $user_id ) )
290 return false;
291
292 $favorites = (string) get_user_option( '_bbp_favorites', $user_id );
293 $favorites = (array) explode( ',', $favorites );
294 $favorites = array_filter( $favorites );
295
296 return apply_filters( 'bbp_get_user_favorites_topic_ids', $favorites, $user_id );
297 }
298
299 /**
300 * Check if a topic is in user's favorites or not
301 *
302 * @since bbPress (r2652)
303 *
304 * @param int $user_id Optional. User id
305 * @param int $topic_id Optional. Topic id
306 * @uses bbp_get_user_id() To get the user id
307 * @uses bbp_get_user_favorites_topic_ids() To get the user favorites
308 * @uses bbp_get_topic() To get the topic
309 * @uses bbp_get_topic_id() To get the topic id
310 * @uses apply_filters() Calls 'bbp_is_user_favorite' with the bool, user id,
311 * topic id and favorites
312 * @return bool True if the topic is in user's favorites, otherwise false
313 */
314 function bbp_is_user_favorite( $user_id = 0, $topic_id = 0 ) {
315
316 $user_id = bbp_get_user_id( $user_id, true, true );
317 if ( empty( $user_id ) )
318 return false;
319
320 $retval = false;
321 $favorites = bbp_get_user_favorites_topic_ids( $user_id );
322
323 if ( !empty( $favorites ) ) {
324
325 // Checking a specific topic id
326 if ( !empty( $topic_id ) ) {
327 $topic = bbp_get_topic( $topic_id );
328 $topic_id = !empty( $topic ) ? $topic->ID : 0;
329
330 // Using the global topic id
331 } elseif ( bbp_get_topic_id() ) {
332 $topic_id = bbp_get_topic_id();
333
334 // Use the current post id
335 } elseif ( !bbp_get_topic_id() ) {
336 $topic_id = get_the_ID();
337 }
338
339 // Is topic_id in the user's favorites
340 if ( !empty( $topic_id ) ) {
341 $retval = in_array( $topic_id, $favorites );
342 }
343 }
344
345 return (bool) apply_filters( 'bbp_is_user_favorite', (bool) $retval, $user_id, $topic_id, $favorites );
346 }
347
348 /**
349 * Add a topic to user's favorites
350 *
351 * @since bbPress (r2652)
352 *
353 * @param int $user_id Optional. User id
354 * @param int $topic_id Optional. Topic id
355 * @uses bbp_get_user_favorites_topic_ids() To get the user favorites
356 * @uses update_user_option() To update the user favorites
357 * @uses do_action() Calls 'bbp_add_user_favorite' with the user id and topic id
358 * @return bool Always true
359 */
360 function bbp_add_user_favorite( $user_id = 0, $topic_id = 0 ) {
361 if ( empty( $user_id ) || empty( $topic_id ) )
362 return false;
363
364 $favorites = (array) bbp_get_user_favorites_topic_ids( $user_id );
365 $topic = bbp_get_topic( $topic_id );
366 if ( empty( $topic ) )
367 return false;
368
369 if ( !in_array( $topic_id, $favorites ) ) {
370 $favorites[] = $topic_id;
371 $favorites = array_filter( $favorites );
372 $favorites = (string) implode( ',', $favorites );
373 update_user_option( $user_id, '_bbp_favorites', $favorites );
374 }
375
376 do_action( 'bbp_add_user_favorite', $user_id, $topic_id );
377
378 return true;
379 }
380
381 /**
382 * Remove a topic from user's favorites
383 *
384 * @since bbPress (r2652)
385 *
386 * @param int $user_id Optional. User id
387 * @param int $topic_id Optional. Topic id
388 * @uses bbp_get_user_favorites_topic_ids() To get the user favorites
389 * @uses update_user_option() To update the user favorites
390 * @uses delete_user_option() To delete the user favorites meta
391 * @uses do_action() Calls 'bbp_remove_user_favorite' with the user & topic id
392 * @return bool True if the topic was removed from user's favorites, otherwise
393 * false
394 */
395 function bbp_remove_user_favorite( $user_id, $topic_id ) {
396 if ( empty( $user_id ) || empty( $topic_id ) )
397 return false;
398
399 $favorites = (array) bbp_get_user_favorites_topic_ids( $user_id );
400 if ( empty( $favorites ) )
401 return false;
402
403 $pos = array_search( $topic_id, $favorites );
404 if ( is_numeric( $pos ) ) {
405 array_splice( $favorites, $pos, 1 );
406 $favorites = array_filter( $favorites );
407
408 if ( !empty( $favorites ) ) {
409 $favorites = implode( ',', $favorites );
410 update_user_option( $user_id, '_bbp_favorites', $favorites );
411 } else {
412 delete_user_option( $user_id, '_bbp_favorites' );
413 }
414 }
415
416 do_action( 'bbp_remove_user_favorite', $user_id, $topic_id );
417
418 return true;
419 }
420
421 /**
422 * Handles the front end adding and removing of favorite topics
423 *
424 * @uses bbp_get_user_id() To get the user id
425 * @uses current_user_can() To check if the current user can edit the user
426 * @uses bbPress:errors:add() To log the error messages
427 * @uses bbp_is_user_favorite() To check if the topic is in user's favorites
428 * @uses bbp_remove_user_favorite() To remove the user favorite
429 * @uses bbp_add_user_favorite() To add the user favorite
430 * @uses do_action() Calls 'bbp_favorites_handler' with success, user id, topic
431 * id and action
432 * @uses bbp_is_favorites() To check if it's the favorites page
433 * @uses bbp_get_favorites_link() To get the favorites page link
434 * @uses bbp_get_topic_permalink() To get the topic permalink
435 * @uses wp_safe_redirect() To redirect to the url
436 */
437 function bbp_favorites_handler() {
438
439 if ( !bbp_is_favorites_active() )
440 return false;
441
442 // Bail if not a GET action
443 if ( 'GET' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
444 return;
445
446 // Bail if required GET actions aren't passed
447 if ( empty( $_GET['topic_id'] ) || empty( $_GET['action'] ) )
448 return;
449
450 // Setup possible get actions
451 $possible_actions = array(
452 'bbp_favorite_add',
453 'bbp_favorite_remove',
454 );
455
456 // Bail if actions aren't meant for this function
457 if ( !in_array( $_GET['action'], $possible_actions ) )
458 return;
459
460 // What action is taking place?
461 $action = $_GET['action'];
462
463 // Get user_id
464 $user_id = bbp_get_user_id( 0, true, true );
465
466 // Check current user's ability to edit the user
467 if ( !current_user_can( 'edit_user', $user_id ) )
468 bbp_add_error( 'bbp_favorite_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'bbpress' ) );
469
470 // Load favorite info
471 $topic_id = intval( $_GET['topic_id'] );
472 if ( empty( $topic_id ) )
473 bbp_add_error( 'bbp_favorite_topic_id', __( '<strong>ERROR</strong>: No topic was found! Which topic are you marking/unmarking as favorite?', 'bbpress' ) );
474
475 $is_favorite = bbp_is_user_favorite( $user_id, $topic_id );
476 $success = false;
477
478 // Handle insertion into posts table
479 if ( !empty( $topic_id ) && !empty( $user_id ) && ( !bbp_has_errors() ) ) {
480
481 if ( $is_favorite && 'bbp_favorite_remove' == $action ) {
482 $success = bbp_remove_user_favorite( $user_id, $topic_id );
483 } elseif ( !$is_favorite && 'bbp_favorite_add' == $action ) {
484 $success = bbp_add_user_favorite( $user_id, $topic_id );
485 }
486
487 // Do additional favorites actions
488 do_action( 'bbp_favorites_handler', $success, $user_id, $topic_id, $action );
489
490 // Check for missing reply_id or error
491 if ( true == $success ) {
492
493 // Redirect back to new reply
494 if ( bbp_is_favorites() ) {
495 $redirect = bbp_get_favorites_permalink( $user_id );
496 } elseif ( bbp_is_single_user() ) {
497 $redirect = bbp_get_user_profile_url();
498 } elseif ( is_singular( bbp_get_topic_post_type() ) ) {
499 $redirect = bbp_get_topic_permalink( $topic_id );
500 } elseif ( is_single() || is_page() ) {
501 $redirect = get_permalink();
502 }
503
504 wp_safe_redirect( $redirect );
505
506 // For good measure
507 exit();
508
509 // Handle errors
510 } else {
511 if ( $is_favorite && 'bbp_favorite_remove' == $action ) {
512 bbp_add_error( 'bbp_favorite_remove', __( '<strong>ERROR</strong>: There was a problem removing that topic from favorites!', 'bbpress' ) );
513 } elseif ( !$is_favorite && 'bbp_favorite_add' == $action ) {
514 bbp_add_error( 'bbp_favorite_add', __( '<strong>ERROR</strong>: There was a problem favoriting that topic!', 'bbpress' ) );
515 }
516 }
517 }
518 }
519
520 /** Subscriptions *************************************************************/
521
522 /**
523 * Get the users who have subscribed to the topic
524 *
525 * @since bbPress (r2668)
526 *
527 * @param int $topic_id Optional. Topic id
528 * @uses wpdb::get_col() To execute our query and get the column back
529 * @uses apply_filters() Calls 'bbp_get_topic_subscribers' with the subscribers
530 * @return array|bool Results if the topic has any subscribers, otherwise false
531 */
532 function bbp_get_topic_subscribers( $topic_id = 0 ) {
533 if ( empty( $topic_id ) ) return;
534
535 global $wpdb;
536
537 $key = $wpdb->prefix . '_bbp_subscriptions';
538 $users = wp_cache_get( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress' );
539 if ( empty( $users ) ) {
540 $users = $wpdb->get_col( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$topic_id}', meta_value) > 0" );
541 wp_cache_set( 'bbp_get_topic_subscribers_' . $topic_id, $users, 'bbpress' );
542 }
543
544 if ( !empty( $users ) ) {
545 $users = apply_filters( 'bbp_get_topic_subscribers', $users );
546 return $users;
547 }
548
549 return false;
550 }
551
552 /**
553 * Get a user's subscribed topics
554 *
555 * @since bbPress (r2668)
556 *
557 * @param int $user_id Optional. User id
558 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
559 * @uses bbp_has_topics() To get the topics
560 * @uses apply_filters() Calls 'bbp_get_user_subscriptions' with the topic query
561 * and user id
562 * @return array|bool Results if user has subscriptions, otherwise false
563 */
564 function bbp_get_user_subscriptions( $user_id = 0 ) {
565
566 // Default to the displayed user
567 $user_id = bbp_get_user_id( $user_id );
568 if ( empty( $user_id ) )
569 return false;
570
571 // If user has subscriptions, load them
572 $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id );
573 if ( !empty( $subscriptions ) ) {
574 $query = bbp_has_topics( array( 'post__in' => $subscriptions ) );
575 return apply_filters( 'bbp_get_user_subscriptions', $query, $user_id );
576 }
577
578 return false;
579 }
580
581 /**
582 * Get a user's subscribed topics' ids
583 *
584 * @since bbPress (r2668)
585 *
586 * @param int $user_id Optional. User id
587 * @uses bbp_get_user_id() To get the user id
588 * @uses get_user_option() To get the user's subscriptions
589 * @uses apply_filters() Calls 'bbp_get_user_subscribed_topic_ids' with
590 * the subscriptions and user id
591 * @return array|bool Results if user has subscriptions, otherwise false
592 */
593 function bbp_get_user_subscribed_topic_ids( $user_id = 0 ) {
594 $user_id = bbp_get_user_id( $user_id );
595 if ( empty( $user_id ) )
596 return false;
597
598 $subscriptions = (string) get_user_option( '_bbp_subscriptions', $user_id );
599 $subscriptions = (array) explode( ',', $subscriptions );
600 $subscriptions = array_filter( $subscriptions );
601
602 return apply_filters( 'bbp_get_user_subscribed_topic_ids', $subscriptions, $user_id );
603 }
604
605 /**
606 * Check if a topic is in user's subscription list or not
607 *
608 * @since bbPress (r2668)
609 *
610 * @param int $user_id Optional. User id
611 * @param int $topic_id Optional. Topic id
612 * @uses bbp_get_user_id() To get the user id
613 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
614 * @uses bbp_get_topic() To get the topic
615 * @uses bbp_get_topic_id() To get the topic id
616 * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id,
617 * topic id and subsriptions
618 * @return bool True if the topic is in user's subscriptions, otherwise false
619 */
620 function bbp_is_user_subscribed( $user_id = 0, $topic_id = 0 ) {
621
622 // Validate user
623 $user_id = bbp_get_user_id( $user_id, true, true );
624 if ( empty( $user_id ) )
625 return false;
626
627 $retval = false;
628 $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id );
629
630 if ( !empty( $subscriptions ) ) {
631
632 // Checking a specific topic id
633 if ( !empty( $topic_id ) ) {
634 $topic = bbp_get_topic( $topic_id );
635 $topic_id = !empty( $topic ) ? $topic->ID : 0;
636
637 // Using the global topic id
638 } elseif ( bbp_get_topic_id() ) {
639 $topic_id = bbp_get_topic_id();
640
641 // Use the current post id
642 } elseif ( !bbp_get_topic_id() ) {
643 $topic_id = get_the_ID();
644 }
645
646 // Is topic_id in the user's favorites
647 if ( !empty( $topic_id ) ) {
648 $retval = in_array( $topic_id, $subscriptions );
649 }
650 }
651
652 return (bool) apply_filters( 'bbp_is_user_subscribed', (bool) $retval, $user_id, $topic_id, $subscriptions );
653 }
654
655 /**
656 * Add a topic to user's subscriptions
657 *
658 * @since bbPress (r2668)
659 *
660 * @param int $user_id Optional. User id
661 * @param int $topic_id Optional. Topic id
662 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
663 * @uses bbp_get_topic() To get the topic
664 * @uses update_user_option() To update the user's subscriptions
665 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id
666 * @return bool Always true
667 */
668 function bbp_add_user_subscription( $user_id = 0, $topic_id = 0 ) {
669 if ( empty( $user_id ) || empty( $topic_id ) )
670 return false;
671
672 $subscriptions = (array) bbp_get_user_subscribed_topic_ids( $user_id );
673
674 $topic = bbp_get_topic( $topic_id );
675 if ( empty( $topic ) )
676 return false;
677
678 if ( !in_array( $topic_id, $subscriptions ) ) {
679 $subscriptions[] = $topic_id;
680 $subscriptions = array_filter( $subscriptions );
681 $subscriptions = (string) implode( ',', $subscriptions );
682 update_user_option( $user_id, '_bbp_subscriptions', $subscriptions );
683
684 wp_cache_delete( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress' );
685 }
686
687 do_action( 'bbp_add_user_subscription', $user_id, $topic_id );
688
689 return true;
690 }
691
692 /**
693 * Remove a topic from user's subscriptions
694 *
695 * @since bbPress (r2668)
696 *
697 * @param int $user_id Optional. User id
698 * @param int $topic_id Optional. Topic id
699 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
700 * @uses update_user_option() To update the user's subscriptions
701 * @uses delete_user_option() To delete the user's subscriptions meta
702 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and
703 * topic id
704 * @return bool True if the topic was removed from user's subscriptions,
705 * otherwise false
706 */
707 function bbp_remove_user_subscription( $user_id, $topic_id ) {
708 if ( empty( $user_id ) || empty( $topic_id ) )
709 return false;
710
711 $subscriptions = (array) bbp_get_user_subscribed_topic_ids( $user_id );
712
713 if ( empty( $subscriptions ) )
714 return false;
715
716 $pos = array_search( $topic_id, $subscriptions );
717 if ( is_numeric( $pos ) ) {
718 array_splice( $subscriptions, $pos, 1 );
719 $subscriptions = array_filter( $subscriptions );
720
721 if ( !empty( $subscriptions ) ) {
722 $subscriptions = implode( ',', $subscriptions );
723 update_user_option( $user_id, '_bbp_subscriptions', $subscriptions );
724 } else {
725 delete_user_option( $user_id, '_bbp_subscriptions' );
726 }
727
728 wp_cache_delete( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress' );
729 }
730
731 do_action( 'bbp_remove_user_subscription', $user_id, $topic_id );
732
733 return true;
734 }
735
736 /**
737 * Handles the front end subscribing and unsubscribing topics
738 *
739 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
740 * @uses bbp_get_user_id() To get the user id
741 * @uses current_user_can() To check if the current user can edit the user
742 * @uses bbPress:errors:add() To log the error messages
743 * @uses bbp_is_user_subscribed() To check if the topic is in user's
744 * subscriptions
745 * @uses bbp_remove_user_subscription() To remove the user subscription
746 * @uses bbp_add_user_subscription() To add the user subscription
747 * @uses do_action() Calls 'bbp_subscriptions_handler' with success, user id,
748 * topic id and action
749 * @uses bbp_is_subscription() To check if it's the subscription page
750 * @uses bbp_get_subscription_link() To get the subscription page link
751 * @uses bbp_get_topic_permalink() To get the topic permalink
752 * @uses wp_safe_redirect() To redirect to the url
753 */
754 function bbp_subscriptions_handler() {
755
756 if ( !bbp_is_subscriptions_active() )
757 return false;
758
759 // Bail if not a GET action
760 if ( 'GET' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
761 return;
762
763 // Bail if required GET actions aren't passed
764 if ( empty( $_GET['topic_id'] ) || empty( $_GET['action'] ) )
765 return;
766
767 // Setup possible get actions
768 $possible_actions = array(
769 'bbp_subscribe',
770 'bbp_unsubscribe',
771 );
772
773 // Bail if actions aren't meant for this function
774 if ( !in_array( $_GET['action'], $possible_actions ) )
775 return;
776
777 // What action is taking place?
778 $action = $_GET['action'];
779
780 // Get user_id
781 $user_id = bbp_get_user_id( 0, true, true );
782
783 // Check current user's ability to edit the user
784 if ( !current_user_can( 'edit_user', $user_id ) )
785 bbp_add_error( 'bbp_subscription_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'bbpress' ) );
786
787 // Load subscription info
788 $topic_id = intval( $_GET['topic_id'] );
789 if ( empty( $topic_id ) )
790 bbp_add_error( 'bbp_subscription_topic_id', __( '<strong>ERROR</strong>: No topic was found! Which topic are you subscribing/unsubscribing to?', 'bbpress' ) );
791
792 if ( !bbp_has_errors() ) {
793
794 $is_subscription = bbp_is_user_subscribed( $user_id, $topic_id );
795 $success = false;
796
797 if ( $is_subscription && 'bbp_unsubscribe' == $action ) {
798 $success = bbp_remove_user_subscription( $user_id, $topic_id );
799 } elseif ( !$is_subscription && 'bbp_subscribe' == $action ) {
800 $success = bbp_add_user_subscription( $user_id, $topic_id );
801 }
802
803 // Do additional subscriptions actions
804 do_action( 'bbp_subscriptions_handler', $success, $user_id, $topic_id, $action );
805
806 // Check for missing reply_id or error
807 if ( true == $success ) {
808
809 // Redirect back to new reply
810 if ( bbp_is_subscriptions() ) {
811 $redirect = bbp_get_subscriptions_permalink( $user_id );
812 } elseif( bbp_is_single_user() ) {
813 $redirect = bbp_get_user_profile_url();
814 } elseif ( is_singular( bbp_get_topic_post_type() ) ) {
815 $redirect = bbp_get_topic_permalink( $topic_id );
816 } elseif ( is_single() || is_page() ) {
817 $redirect = get_permalink();
818 }
819
820 wp_safe_redirect( $redirect );
821
822 // For good measure
823 exit();
824
825 // Handle errors
826 } else {
827 if ( $is_subscription && 'bbp_unsubscribe' == $action ) {
828 bbp_add_error( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing from that topic!', 'bbpress' ) );
829 } elseif ( !$is_subscription && 'bbp_subscribe' == $action ) {
830 bbp_add_error( 'bbp_subscribe', __( '<strong>ERROR</strong>: There was a problem subscribing to that topic!', 'bbpress' ) );
831 }
832 }
833 }
834 }
835
836 /** Edit **********************************************************************/
837
838 /**
839 * Handles the front end user editing
840 *
841 * @uses is_multisite() To check if it's a multisite
842 * @uses bbp_is_user_home() To check if the user is at home (the display page
843 * is the one of the logged in user)
844 * @uses get_option() To get the displayed user's new email id option
845 * @uses wpdb::prepare() To sanitize our sql query
846 * @uses wpdb::get_var() To execute our query and get back the variable
847 * @uses wpdb::query() To execute our query
848 * @uses wp_update_user() To update the user
849 * @uses delete_option() To delete the displayed user's email id option
850 * @uses bbp_get_user_profile_edit_url() To get the edit profile url
851 * @uses wp_safe_redirect() To redirect to the url
852 * @uses check_admin_referer() To verify the nonce and check the referer
853 * @uses current_user_can() To check if the current user can edit the user
854 * @uses do_action() Calls 'personal_options_update' or
855 * 'edit_user_options_update' (based on if it's the user home)
856 * with the displayed user id
857 * @uses edit_user() To edit the user based on the post data
858 * @uses get_userdata() To get the user data
859 * @uses is_email() To check if the string is an email id or not
860 * @uses wpdb::get_blog_prefix() To get the blog prefix
861 * @uses is_network_admin() To check if the user is the network admin
862 * @uses is_super_admin() To check if the user is super admin
863 * @uses revoke_super_admin() To revoke super admin priviledges
864 * @uses grant_super_admin() To grant super admin priviledges
865 * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
866 */
867 function bbp_edit_user_handler() {
868
869 // Bail if not a POST action
870 if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
871 return;
872
873 // Bail if action is not 'bbp-update-user'
874 if ( empty( $_POST['action'] ) || ( 'bbp-update-user' !== $_POST['action'] ) )
875 return;
876
877 // Get the displayed user ID
878 $user_id = bbp_get_displayed_user_id();
879
880 global $wpdb, $user_login, $super_admins;
881
882 // Execute confirmed email change. See send_confirmation_on_profile_email().
883 if ( is_multisite() && bbp_is_user_home_edit() && isset( $_GET['newuseremail'] ) ) {
884
885 $new_email = get_option( $user_id . '_new_email' );
886
887 if ( $new_email['hash'] == $_GET['newuseremail'] ) {
888 $user = new stdClass();
889 $user->ID = $user_id;
890 $user->user_email = esc_html( trim( $new_email['newemail'] ) );
891
892 if ( $wpdb->get_var( $wpdb->prepare( "SELECT user_login FROM {$wpdb->signups} WHERE user_login = %s", bbp_get_displayed_user_field( 'user_login' ) ) ) )
893 $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->signups} SET user_email = %s WHERE user_login = %s", $user->user_email, bbp_get_displayed_user_field( 'user_login' ) ) );
894
895 wp_update_user( get_object_vars( $user ) );
896 delete_option( $user_id . '_new_email' );
897
898 wp_safe_redirect( add_query_arg( array( 'updated' => 'true' ), bbp_get_user_profile_edit_url( $user_id ) ) );
899 exit;
900 }
901
902 } elseif ( is_multisite() && bbp_is_user_home_edit() && !empty( $_GET['dismiss'] ) && ( $user_id . '_new_email' == $_GET['dismiss'] ) ) {
903
904 delete_option( $user_id . '_new_email' );
905 wp_safe_redirect( add_query_arg( array( 'updated' => 'true' ), bbp_get_user_profile_edit_url( $user_id ) ) );
906 exit;
907
908 }
909
910 check_admin_referer( 'update-user_' . $user_id );
911
912 if ( !current_user_can( 'edit_user', $user_id ) )
913 wp_die( __( 'What are you doing here? You do not have the permission to edit this user.', 'bbpress' ) );
914
915 // Do action based on who's profile you're editing
916 $edit_action = bbp_is_user_home_edit() ? 'personal_options_update' : 'edit_user_profile_update';
917 do_action( $edit_action, $user_id );
918
919 // Multisite handles the trouble for us ;)
920 if ( !is_multisite() ) {
921 $edit_user = edit_user( $user_id );
922
923 // Single site means we need to do some manual labor
924 } else {
925 $user = get_userdata( $user_id );
926
927 // Update the email address in signups, if present.
928 if ( $user->user_login && isset( $_POST['email'] ) && is_email( $_POST['email'] ) && $wpdb->get_var( $wpdb->prepare( "SELECT user_login FROM {$wpdb->signups} WHERE user_login = %s", $user->user_login ) ) ) {
929 $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->signups} SET user_email = %s WHERE user_login = %s", $_POST['email'], $user_login ) );
930 }
931
932 // WPMU must delete the user from the current blog if WP added him after editing.
933 $delete_role = false;
934 $blog_prefix = $wpdb->get_blog_prefix();
935
936 if ( $user_id != $user_id ) {
937 $cap = $wpdb->get_var( "SELECT meta_value FROM {$wpdb->usermeta} WHERE user_id = '{$user_id}' AND meta_key = '{$blog_prefix}capabilities' AND meta_value = 'a:0:{}'" );
938 if ( !is_network_admin() && null == $cap && $_POST['role'] == '' ) {
939 $_POST['role'] = 'contributor';
940 $delete_role = true;
941 }
942 }
943
944 $edit_user = edit_user( $user_id );
945
946 // stops users being added to current blog when they are edited
947 if ( true === $delete_role ) {
948 delete_user_meta( $user_id, $blog_prefix . 'capabilities' );
949 }
950
951 if ( is_multisite() && is_network_admin() & !bbp_is_user_home_edit() && current_user_can( 'manage_network_options' ) && !isset( $super_admins ) && empty( $_POST['super_admin'] ) == is_super_admin( $user_id ) ) {
952 empty( $_POST['super_admin'] ) ? revoke_super_admin( $user_id ) : grant_super_admin( $user_id );
953 }
954 }
955
956 // Error(s) editng the user, so copy them into the global
957 if ( is_wp_error( $edit_user ) ) {
958 bbpress()->errors = $edit_user;
959
960 // Successful edit to redirect
961 } elseif ( is_integer( $edit_user ) ) {
962 $redirect = add_query_arg( array( 'updated' => 'true' ), bbp_get_user_profile_edit_url( $edit_user ) );
963
964 wp_safe_redirect( $redirect );
965 exit;
966 }
967 }
968
969 /** END - Edit User ***********************************************************/
970
971 /**
972 * Get the topics that a user created
973 *
974 * @since bbPress (r2660)
975 *
976 * @param int $user_id Optional. User id
977 * @uses bbp_get_user_id() To get the topic id
978 * @uses bbp_has_topics() To get the topics created by the user
979 * @return array|bool Results if the user has created topics, otherwise false
980 */
981 function bbp_get_user_topics_started( $user_id = 0 ) {
982
983 // Validate user
984 $user_id = bbp_get_user_id( $user_id );
985 if ( empty( $user_id ) )
986 return false;
987
988 // Query defaults
989 $default_query = array(
990 'author' => $user_id,
991 'show_stickies' => false,
992 'order' => 'DESC',
993 );
994
995 // Try to get the topics
996 $query = bbp_has_topics( $default_query );
997 if ( empty( $query ) )
998 return false;
999
1000 return apply_filters( 'bbp_get_user_topics_started', $query, $user_id );
1001 }
1002
1003 /**
1004 * Get the total number of users on the forums
1005 *
1006 * @since bbPress (r2769)
1007 * @uses wp_cache_get() Check if query is in cache
1008 * @uses get_users() To execute our query and get the var back
1009 * @uses wp_cache_set() Set the query in the cache
1010 * @uses apply_filters() Calls 'bbp_get_total_users' with number of users
1011 * @return int Total number of users
1012 */
1013 function bbp_get_total_users() {
1014
1015 $bbp_total_users = wp_cache_get( 'bbp_total_users', 'bbpress' );
1016 if ( !empty( $bbp_total_users ) )
1017 return $bbp_total_users;
1018
1019 $bbp_total_users = count( get_users() );
1020
1021 wp_cache_set( 'bbp_total_users', $bbp_total_users, 'bbpress' );
1022
1023 return apply_filters( 'bbp_get_total_users', (int) $bbp_total_users );
1024 }
1025
1026 /** User Status ***************************************************************/
1027
1028 /**
1029 * Checks if the user has been marked as a spammer.
1030 *
1031 * @since bbPress (r3355)
1032 *
1033 * @param int $user_id int The ID for the user.
1034 * @return bool True if spammer, False if not.
1035 */
1036 function bbp_is_user_spammer( $user_id = 0 ) {
1037
1038 // Default to current user
1039 if ( empty( $user_id ) && is_user_logged_in() )
1040 $user_id = bbp_get_current_user_id();
1041
1042 // No user to check
1043 if ( empty( $user_id ) )
1044 return false;
1045
1046 // Assume user is not spam
1047 $is_spammer = false;
1048
1049 // Get user data
1050 $user = get_userdata( $user_id );
1051
1052 // No user found
1053 if ( empty( $user ) ) {
1054 $is_spammer = false;
1055
1056 // User found
1057 } else {
1058
1059 // Check if spam
1060 if ( !empty( $user->spam ) )
1061 $is_spammer = true;
1062
1063 if ( 1 == $user->user_status )
1064 $is_spammer = true;
1065 }
1066
1067 return apply_filters( 'bp_core_is_user_spammer', (bool) $is_spammer );
1068 }
1069
1070 /**
1071 * Mark a users topics and replies as spam when the user is marked as spam
1072 *
1073 * @since bbPress (r3405)
1074 *
1075 * @global WPDB $wpdb
1076 * @param int $user_id Optional. User ID to spam. Defaults to displayed user.
1077
1078 * @uses bbp_is_single_user()
1079 * @uses bbp_is_user_home()
1080 * @uses bbp_get_displayed_user_field()
1081 * @uses is_super_admin()
1082 * @uses get_blogs_of_user()
1083 * @uses get_current_blog_id()
1084 * @uses bbp_get_topic_post_type()
1085 * @uses bbp_get_reply_post_type()
1086 * @uses switch_to_blog()
1087 * @uses get_post_type()
1088 * @uses bbp_spam_topic()
1089 * @uses bbp_spam_reply()
1090 * @uses restore_current_blog()
1091 *
1092 * @return If no user ID passed
1093 */
1094 function bbp_make_spam_user( $user_id = 0 ) {
1095
1096 // Use displayed user if it's not yourself
1097 if ( empty( $user_id ) && bbp_is_single_user() && !bbp_is_user_home() )
1098 $user_id = bbp_get_displayed_user_id();
1099
1100 // Bail if no user ID
1101 if ( empty( $user_id ) )
1102 return;
1103
1104 // Bail if user ID is super admin
1105 if ( is_super_admin( $user_id ) )
1106 return;
1107
1108 // Arm the torpedos
1109 global $wpdb;
1110
1111 // Get the blog IDs of the user to mark as spam
1112 $blogs = get_blogs_of_user( $user_id, true );
1113
1114 // If user has no blogs, they are a guest on this site
1115 if ( empty( $blogs ) )
1116 $blogs[$wpdb->blogid] = array();
1117
1118 // We only need the keys
1119 $blogs = array_keys( $blogs );
1120
1121 // Make array of post types to mark as spam
1122 $post_types = array( bbp_get_topic_post_type(), bbp_get_reply_post_type() );
1123 $post_types = "'" . implode( "', '", $post_types ) . "'";
1124 $status = bbp_get_public_status_id();
1125
1126 // Loop through blogs and remove their posts
1127 foreach ( (array) $blogs as $blog_id ) {
1128
1129 // Switch to the blog ID
1130 switch_to_blog( $blog_id );
1131
1132 // Get topics and replies
1133 $posts = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_author = {$user_id} AND post_status = '{$status}' AND post_type IN ({$post_types})" );
1134
1135 // Loop through posts and spam them
1136 if ( !empty( $posts ) ) {
1137 foreach ( $posts as $post_id ) {
1138
1139 // The routines for topics ang replies are different, so use the
1140 // correct one based on the post type
1141 switch ( get_post_type( $post_id ) ) {
1142
1143 case bbp_get_topic_post_type() :
1144 bbp_spam_topic( $post_id );
1145 break;
1146
1147 case bbp_get_reply_post_type() :
1148 bbp_spam_reply( $post_id );
1149 break;
1150 }
1151 }
1152 }
1153
1154 // Switch back to current blog
1155 restore_current_blog();
1156 }
1157 }
1158
1159 /**
1160 * Mark a users topics and replies as spam when the user is marked as spam
1161 *
1162 * @since bbPress (r3405)
1163 *
1164 * @global WPDB $wpdb
1165 * @param int $user_id Optional. User ID to unspam. Defaults to displayed user.
1166 *
1167 * @uses bbp_is_single_user()
1168 * @uses bbp_is_user_home()
1169 * @uses bbp_get_displayed_user_field()
1170 * @uses is_super_admin()
1171 * @uses get_blogs_of_user()
1172 * @uses bbp_get_topic_post_type()
1173 * @uses bbp_get_reply_post_type()
1174 * @uses switch_to_blog()
1175 * @uses get_post_type()
1176 * @uses bbp_unspam_topic()
1177 * @uses bbp_unspam_reply()
1178 * @uses restore_current_blog()
1179 *
1180 * @return If no user ID passed
1181 */
1182 function bbp_make_ham_user( $user_id = 0 ) {
1183
1184 // Use displayed user if it's not yourself
1185 if ( empty( $user_id ) && bbp_is_single_user() && !bbp_is_user_home() )
1186 $user_id = bbp_get_displayed_user_field();
1187
1188 // Bail if no user ID
1189 if ( empty( $user_id ) )
1190 return;
1191
1192 // Bail if user ID is super admin
1193 if ( is_super_admin( $user_id ) )
1194 return;
1195
1196 // Arm the torpedos
1197 global $wpdb;
1198
1199 // Get the blog IDs of the user to mark as spam
1200 $blogs = get_blogs_of_user( $user_id, true );
1201
1202 // If user has no blogs, they are a guest on this site
1203 if ( empty( $blogs ) )
1204 $blogs[$wpdb->blogid] = array();
1205
1206 // We only need the keys
1207 $blogs = array_keys( $blogs );
1208
1209 // Make array of post types to mark as spam
1210 $post_types = array( bbp_get_topic_post_type(), bbp_get_reply_post_type() );
1211 $post_types = "'" . implode( "', '", $post_types ) . "'";
1212 $status = bbp_get_spam_status_id();
1213
1214 // Loop through blogs and remove their posts
1215 foreach ( (array) $blogs as $blog_id ) {
1216
1217 // Switch to the blog ID
1218 switch_to_blog( $blog_id );
1219
1220 // Get topics and replies
1221 $posts = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_author = {$user_id} AND post_status = '{$status}' AND post_type IN ({$post_types})" );
1222
1223 // Loop through posts and spam them
1224 if ( !empty( $posts ) ) {
1225 foreach ( $posts as $post_id ) {
1226
1227 // The routines for topics ang replies are different, so use the
1228 // correct one based on the post type
1229 switch ( get_post_type( $post_id ) ) {
1230
1231 case bbp_get_topic_post_type() :
1232 bbp_unspam_topic( $post_id );
1233 break;
1234
1235 case bbp_get_reply_post_type() :
1236 bbp_unspam_reply( $post_id );
1237 break;
1238 }
1239 }
1240 }
1241
1242 // Switch back to current blog
1243 restore_current_blog();
1244 }
1245 }
1246
1247 /**
1248 * Checks if the user has been marked as deleted.
1249 *
1250 * @since bbPress (r3355)
1251 *
1252 * @param int $user_id int The ID for the user.
1253 * @return bool True if deleted, False if not.
1254 */
1255 function bbp_is_user_deleted( $user_id = 0 ) {
1256
1257 // Default to current user
1258 if ( empty( $user_id ) && is_user_logged_in() )
1259 $user_id = bbp_get_current_user_id();
1260
1261 // No user to check
1262 if ( empty( $user_id ) )
1263 return false;
1264
1265 // Assume user is not deleted
1266 $is_deleted = false;
1267
1268 // Get user data
1269 $user = get_userdata( $user_id );
1270
1271 // No user found
1272 if ( empty( $user ) ) {
1273 $is_deleted = true;
1274
1275 // User found
1276 } else {
1277
1278 // Check if deleted
1279 if ( !empty( $user->deleted ) )
1280 $is_deleted = true;
1281
1282 if ( 2 == $user->user_status )
1283 $is_deleted = true;
1284
1285 }
1286
1287 return apply_filters( 'bp_core_is_user_deleted', (bool) $is_deleted );
1288 }
1289
1290 /**
1291 * Checks if user is active
1292 *
1293 * @since bbPress (r3502)
1294 *
1295 * @uses is_user_logged_in() To check if user is logged in
1296 * @uses bbp_get_displayed_user_id() To get current user ID
1297 * @uses bbp_is_user_spammer() To check if user is spammer
1298 * @uses bbp_is_user_deleted() To check if user is deleted
1299 *
1300 * @param int $user_id The user ID to check
1301 * @return bool True if public, false if not
1302 */
1303 function bbp_is_user_active( $user_id = 0 ) {
1304
1305 // Default to current user
1306 if ( empty( $user_id ) && is_user_logged_in() )
1307 $user_id = bbp_get_current_user_id();
1308
1309 // No user to check
1310 if ( empty( $user_id ) )
1311 return false;
1312
1313 // Check spam
1314 if ( bbp_is_user_spammer( $user_id ) )
1315 return false;
1316
1317 // Check deleted
1318 if ( bbp_is_user_deleted( $user_id ) )
1319 return false;
1320
1321 // Assume true if not spam or deleted
1322 return true;
1323 }
1324
1325 /**
1326 * Checks if user is not active.
1327 *
1328 * @since bbPress (r3502)
1329 *
1330 * @uses is_user_logged_in() To check if user is logged in
1331 * @uses bbp_get_displayed_user_id() To get current user ID
1332 * @uses bbp_is_user_active() To check if user is active
1333 *
1334 * @param int $user_id The user ID to check. Defaults to current user ID
1335 * @return bool True if inactive, false if active
1336 */
1337 function bbp_is_user_inactive( $user_id = 0 ) {
1338
1339 // Default to current user
1340 if ( empty( $user_id ) && is_user_logged_in() )
1341 $user_id = bbp_get_current_user_id();
1342
1343 // No user to check
1344 if ( empty( $user_id ) )
1345 return false;
1346
1347 // Return the inverse of active
1348 return !bbp_is_user_active( $user_id );
1349 }
1350
1351 /**
1352 * Return a user's main role
1353 *
1354 * @since bbPress (r3860)
1355 *
1356 * @param int $user_id
1357 * @uses bbp_get_user_id() To get the user id
1358 * @uses get_userdata() To get the user data
1359 * @uses apply_filters() Calls 'bbp_get_user_role' with the
1360 * role and user id
1361 * @return string
1362 */
1363 function bbp_get_user_role( $user_id = 0 ) {
1364
1365 // Validate user id
1366 $user_id = bbp_get_user_id( $user_id, false, false );
1367 if ( empty( $user_id ) )
1368 return false;
1369
1370 // Get userdata
1371 $user = get_userdata( $user_id );
1372
1373 // Get the user's main role
1374 $role = isset( $user->roles ) ? array_shift( $user->roles ) : bbp_get_anonymous_role();
1375
1376 return apply_filters( 'bbp_get_user_role', $role, $user_id, $user );
1377 }
1378
1379 /** Premissions ***************************************************************/
1380
1381 /**
1382 * Redirect if unathorized user is attempting to edit another user
1383 *
1384 * This is hooked to 'bbp_template_redirect' and controls the conditions under
1385 * which a user can edit another user (or themselves.) If these conditions are
1386 * met. We assume a user cannot perform this task, and look for ways they can
1387 * earn the ability to access this template.
1388 *
1389 * @since bbPress (r3605)
1390 *
1391 * @uses bbp_is_topic_edit()
1392 * @uses current_user_can()
1393 * @uses bbp_get_topic_id()
1394 * @uses wp_safe_redirect()
1395 * @uses bbp_get_topic_permalink()
1396 */
1397 function bbp_check_user_edit() {
1398
1399 // Bail if not editing a topic
1400 if ( ! bbp_is_single_user_edit() )
1401 return;
1402
1403 // Default to false
1404 $redirect = true;
1405
1406 // Allow user to edit their own profile
1407 if ( bbp_is_user_home_edit() ) {
1408 $redirect = false;
1409
1410 // Allow if current user can edit the displayed user
1411 } elseif ( current_user_can( 'edit_user', bbp_get_displayed_user_id() ) ) {
1412 $redirect = false;
1413
1414 // Allow if user can manage network users, or edit-any is enabled
1415 } elseif ( current_user_can( 'manage_network_users' ) || apply_filters( 'enable_edit_any_user_configuration', true ) ) {
1416 $redirect = false;
1417 }
1418
1419 // Maybe redirect back to profile page
1420 if ( true === $redirect ) {
1421 wp_safe_redirect( bbp_get_user_profile_url( bbp_get_displayed_user_id() ) );
1422 exit();
1423 }
1424 }
1425