PluginProbe
bbPress / 2.1.2
bbPress v2.1.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 / bbp-includes / bbp-user-functions.php

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

1,477 lines 45.3 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 bbp_verify_nonce_request() To verify the nonce and check the request
426 * @uses current_user_can() To check if the current user can edit the user
427 * @uses bbPress:errors:add() To log the error messages
428 * @uses bbp_is_user_favorite() To check if the topic is in user's favorites
429 * @uses bbp_remove_user_favorite() To remove the user favorite
430 * @uses bbp_add_user_favorite() To add the user favorite
431 * @uses do_action() Calls 'bbp_favorites_handler' with success, user id, topic
432 * id and action
433 * @uses bbp_is_favorites() To check if it's the favorites page
434 * @uses bbp_get_favorites_link() To get the favorites page link
435 * @uses bbp_get_topic_permalink() To get the topic permalink
436 * @uses wp_safe_redirect() To redirect to the url
437 */
438 function bbp_favorites_handler() {
439
440 if ( !bbp_is_favorites_active() )
441 return false;
442
443 // Bail if not a GET action
444 if ( 'GET' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
445 return;
446
447 // Bail if required GET actions aren't passed
448 if ( empty( $_GET['topic_id'] ) || empty( $_GET['action'] ) )
449 return;
450
451 // Setup possible get actions
452 $possible_actions = array(
453 'bbp_favorite_add',
454 'bbp_favorite_remove',
455 );
456
457 // Bail if actions aren't meant for this function
458 if ( !in_array( $_GET['action'], $possible_actions ) )
459 return;
460
461 // What action is taking place?
462 $action = $_GET['action'];
463 $topic_id = intval( $_GET['topic_id'] );
464 $user_id = bbp_get_user_id( 0, true, true );
465
466 // Check for empty topic
467 if ( empty( $topic_id ) ) {
468 bbp_add_error( 'bbp_favorite_topic_id', __( '<strong>ERROR</strong>: No topic was found! Which topic are you marking/unmarking as favorite?', 'bbpress' ) );
469
470 // Check nonce
471 } elseif ( ! bbp_verify_nonce_request( 'toggle-favorite_' . $topic_id ) ) {
472 bbp_add_error( 'bbp_favorite_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
473
474 // Check current user's ability to edit the user
475 } elseif ( !current_user_can( 'edit_user', $user_id ) ) {
476 bbp_add_error( 'bbp_favorite_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'bbpress' ) );
477 }
478
479 // Bail if errors
480 if ( bbp_has_errors() )
481 return;
482
483 /** No errors *************************************************************/
484
485 $is_favorite = bbp_is_user_favorite( $user_id, $topic_id );
486 $success = false;
487
488 if ( true == $is_favorite && 'bbp_favorite_remove' == $action )
489 $success = bbp_remove_user_favorite( $user_id, $topic_id );
490 elseif ( false == $is_favorite && 'bbp_favorite_add' == $action )
491 $success = bbp_add_user_favorite( $user_id, $topic_id );
492
493 // Do additional favorites actions
494 do_action( 'bbp_favorites_handler', $success, $user_id, $topic_id, $action );
495
496 // Success!
497 if ( true == $success ) {
498
499 // Redirect back from whence we came
500 if ( bbp_is_favorites() ) {
501 $redirect = bbp_get_favorites_permalink( $user_id );
502 } elseif ( bbp_is_single_user() ) {
503 $redirect = bbp_get_user_profile_url();
504 } elseif ( is_singular( bbp_get_topic_post_type() ) ) {
505 $redirect = bbp_get_topic_permalink( $topic_id );
506 } elseif ( is_single() || is_page() ) {
507 $redirect = get_permalink();
508 }
509
510 wp_safe_redirect( $redirect );
511
512 // For good measure
513 exit();
514
515 // Fail! Handle errors
516 } elseif ( true == $is_favorite && 'bbp_favorite_remove' == $action ) {
517 bbp_add_error( 'bbp_favorite_remove', __( '<strong>ERROR</strong>: There was a problem removing that topic from favorites!', 'bbpress' ) );
518 } elseif ( false == $is_favorite && 'bbp_favorite_add' == $action ) {
519 bbp_add_error( 'bbp_favorite_add', __( '<strong>ERROR</strong>: There was a problem favoriting that topic!', 'bbpress' ) );
520 }
521 }
522
523 /** Subscriptions *************************************************************/
524
525 /**
526 * Get the users who have subscribed to the topic
527 *
528 * @since bbPress (r2668)
529 *
530 * @param int $topic_id Optional. Topic id
531 * @uses wpdb::get_col() To execute our query and get the column back
532 * @uses apply_filters() Calls 'bbp_get_topic_subscribers' with the subscribers
533 * @return array|bool Results if the topic has any subscribers, otherwise false
534 */
535 function bbp_get_topic_subscribers( $topic_id = 0 ) {
536 if ( empty( $topic_id ) ) return;
537
538 global $wpdb;
539
540 $key = $wpdb->prefix . '_bbp_subscriptions';
541 $users = wp_cache_get( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress' );
542 if ( empty( $users ) ) {
543 $users = $wpdb->get_col( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$topic_id}', meta_value) > 0" );
544 wp_cache_set( 'bbp_get_topic_subscribers_' . $topic_id, $users, 'bbpress' );
545 }
546
547 if ( !empty( $users ) ) {
548 $users = apply_filters( 'bbp_get_topic_subscribers', $users );
549 return $users;
550 }
551
552 return false;
553 }
554
555 /**
556 * Get a user's subscribed topics
557 *
558 * @since bbPress (r2668)
559 *
560 * @param int $user_id Optional. User id
561 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
562 * @uses bbp_has_topics() To get the topics
563 * @uses apply_filters() Calls 'bbp_get_user_subscriptions' with the topic query
564 * and user id
565 * @return array|bool Results if user has subscriptions, otherwise false
566 */
567 function bbp_get_user_subscriptions( $user_id = 0 ) {
568
569 // Default to the displayed user
570 $user_id = bbp_get_user_id( $user_id );
571 if ( empty( $user_id ) )
572 return false;
573
574 // If user has subscriptions, load them
575 $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id );
576 if ( !empty( $subscriptions ) ) {
577 $query = bbp_has_topics( array( 'post__in' => $subscriptions ) );
578 return apply_filters( 'bbp_get_user_subscriptions', $query, $user_id );
579 }
580
581 return false;
582 }
583
584 /**
585 * Get a user's subscribed topics' ids
586 *
587 * @since bbPress (r2668)
588 *
589 * @param int $user_id Optional. User id
590 * @uses bbp_get_user_id() To get the user id
591 * @uses get_user_option() To get the user's subscriptions
592 * @uses apply_filters() Calls 'bbp_get_user_subscribed_topic_ids' with
593 * the subscriptions and user id
594 * @return array|bool Results if user has subscriptions, otherwise false
595 */
596 function bbp_get_user_subscribed_topic_ids( $user_id = 0 ) {
597 $user_id = bbp_get_user_id( $user_id );
598 if ( empty( $user_id ) )
599 return false;
600
601 $subscriptions = (string) get_user_option( '_bbp_subscriptions', $user_id );
602 $subscriptions = (array) explode( ',', $subscriptions );
603 $subscriptions = array_filter( $subscriptions );
604
605 return apply_filters( 'bbp_get_user_subscribed_topic_ids', $subscriptions, $user_id );
606 }
607
608 /**
609 * Check if a topic is in user's subscription list or not
610 *
611 * @since bbPress (r2668)
612 *
613 * @param int $user_id Optional. User id
614 * @param int $topic_id Optional. Topic id
615 * @uses bbp_get_user_id() To get the user id
616 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
617 * @uses bbp_get_topic() To get the topic
618 * @uses bbp_get_topic_id() To get the topic id
619 * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id,
620 * topic id and subsriptions
621 * @return bool True if the topic is in user's subscriptions, otherwise false
622 */
623 function bbp_is_user_subscribed( $user_id = 0, $topic_id = 0 ) {
624
625 // Validate user
626 $user_id = bbp_get_user_id( $user_id, true, true );
627 if ( empty( $user_id ) )
628 return false;
629
630 $retval = false;
631 $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id );
632
633 if ( !empty( $subscriptions ) ) {
634
635 // Checking a specific topic id
636 if ( !empty( $topic_id ) ) {
637 $topic = bbp_get_topic( $topic_id );
638 $topic_id = !empty( $topic ) ? $topic->ID : 0;
639
640 // Using the global topic id
641 } elseif ( bbp_get_topic_id() ) {
642 $topic_id = bbp_get_topic_id();
643
644 // Use the current post id
645 } elseif ( !bbp_get_topic_id() ) {
646 $topic_id = get_the_ID();
647 }
648
649 // Is topic_id in the user's favorites
650 if ( !empty( $topic_id ) ) {
651 $retval = in_array( $topic_id, $subscriptions );
652 }
653 }
654
655 return (bool) apply_filters( 'bbp_is_user_subscribed', (bool) $retval, $user_id, $topic_id, $subscriptions );
656 }
657
658 /**
659 * Add a topic to user's subscriptions
660 *
661 * @since bbPress (r2668)
662 *
663 * @param int $user_id Optional. User id
664 * @param int $topic_id Optional. Topic id
665 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
666 * @uses bbp_get_topic() To get the topic
667 * @uses update_user_option() To update the user's subscriptions
668 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id
669 * @return bool Always true
670 */
671 function bbp_add_user_subscription( $user_id = 0, $topic_id = 0 ) {
672 if ( empty( $user_id ) || empty( $topic_id ) )
673 return false;
674
675 $subscriptions = (array) bbp_get_user_subscribed_topic_ids( $user_id );
676
677 $topic = bbp_get_topic( $topic_id );
678 if ( empty( $topic ) )
679 return false;
680
681 if ( !in_array( $topic_id, $subscriptions ) ) {
682 $subscriptions[] = $topic_id;
683 $subscriptions = array_filter( $subscriptions );
684 $subscriptions = (string) implode( ',', $subscriptions );
685 update_user_option( $user_id, '_bbp_subscriptions', $subscriptions );
686
687 wp_cache_delete( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress' );
688 }
689
690 do_action( 'bbp_add_user_subscription', $user_id, $topic_id );
691
692 return true;
693 }
694
695 /**
696 * Remove a topic from user's subscriptions
697 *
698 * @since bbPress (r2668)
699 *
700 * @param int $user_id Optional. User id
701 * @param int $topic_id Optional. Topic id
702 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
703 * @uses update_user_option() To update the user's subscriptions
704 * @uses delete_user_option() To delete the user's subscriptions meta
705 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and
706 * topic id
707 * @return bool True if the topic was removed from user's subscriptions,
708 * otherwise false
709 */
710 function bbp_remove_user_subscription( $user_id, $topic_id ) {
711 if ( empty( $user_id ) || empty( $topic_id ) )
712 return false;
713
714 $subscriptions = (array) bbp_get_user_subscribed_topic_ids( $user_id );
715
716 if ( empty( $subscriptions ) )
717 return false;
718
719 $pos = array_search( $topic_id, $subscriptions );
720 if ( is_numeric( $pos ) ) {
721 array_splice( $subscriptions, $pos, 1 );
722 $subscriptions = array_filter( $subscriptions );
723
724 if ( !empty( $subscriptions ) ) {
725 $subscriptions = implode( ',', $subscriptions );
726 update_user_option( $user_id, '_bbp_subscriptions', $subscriptions );
727 } else {
728 delete_user_option( $user_id, '_bbp_subscriptions' );
729 }
730
731 wp_cache_delete( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress' );
732 }
733
734 do_action( 'bbp_remove_user_subscription', $user_id, $topic_id );
735
736 return true;
737 }
738
739 /**
740 * Handles the front end subscribing and unsubscribing topics
741 *
742 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
743 * @uses bbp_get_user_id() To get the user id
744 * @uses bbp_verify_nonce_request() To verify the nonce and check the request
745 * @uses current_user_can() To check if the current user can edit the user
746 * @uses bbPress:errors:add() To log the error messages
747 * @uses bbp_is_user_subscribed() To check if the topic is in user's
748 * subscriptions
749 * @uses bbp_remove_user_subscription() To remove the user subscription
750 * @uses bbp_add_user_subscription() To add the user subscription
751 * @uses do_action() Calls 'bbp_subscriptions_handler' with success, user id,
752 * topic id and action
753 * @uses bbp_is_subscription() To check if it's the subscription page
754 * @uses bbp_get_subscription_link() To get the subscription page link
755 * @uses bbp_get_topic_permalink() To get the topic permalink
756 * @uses wp_safe_redirect() To redirect to the url
757 */
758 function bbp_subscriptions_handler() {
759
760 if ( !bbp_is_subscriptions_active() )
761 return false;
762
763 // Bail if not a GET action
764 if ( 'GET' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
765 return;
766
767 // Bail if required GET actions aren't passed
768 if ( empty( $_GET['topic_id'] ) || empty( $_GET['action'] ) )
769 return;
770
771 // Setup possible get actions
772 $possible_actions = array(
773 'bbp_subscribe',
774 'bbp_unsubscribe',
775 );
776
777 // Bail if actions aren't meant for this function
778 if ( !in_array( $_GET['action'], $possible_actions ) )
779 return;
780
781 // Get required data
782 $action = $_GET['action'];
783 $user_id = bbp_get_user_id( 0, true, true );
784 $topic_id = intval( $_GET['topic_id'] );
785
786 // Check for empty topic
787 if ( empty( $topic_id ) ) {
788 bbp_add_error( 'bbp_subscription_topic_id', __( '<strong>ERROR</strong>: No topic was found! Which topic are you subscribing/unsubscribing to?', 'bbpress' ) );
789
790 // Check nonce
791 } elseif ( ! bbp_verify_nonce_request( 'toggle-subscription_' . $topic_id ) ) {
792 bbp_add_error( 'bbp_subscription_topic_id', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
793
794 // Check current user's ability to edit the user
795 } elseif ( !current_user_can( 'edit_user', $user_id ) ) {
796 bbp_add_error( 'bbp_subscription_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'bbpress' ) );
797 }
798
799 // Bail if we have errors
800 if ( bbp_has_errors() )
801 return;
802
803 /** No errors *************************************************************/
804
805 $is_subscription = bbp_is_user_subscribed( $user_id, $topic_id );
806 $success = false;
807
808 if ( true == $is_subscription && 'bbp_unsubscribe' == $action )
809 $success = bbp_remove_user_subscription( $user_id, $topic_id );
810 elseif ( false == $is_subscription && 'bbp_subscribe' == $action )
811 $success = bbp_add_user_subscription( $user_id, $topic_id );
812
813 // Do additional subscriptions actions
814 do_action( 'bbp_subscriptions_handler', $success, $user_id, $topic_id, $action );
815
816 // Success!
817 if ( true == $success ) {
818
819 // Redirect back from whence we came
820 if ( bbp_is_subscriptions() ) {
821 $redirect = bbp_get_subscriptions_permalink( $user_id );
822 } elseif ( bbp_is_single_user() ) {
823 $redirect = bbp_get_user_profile_url();
824 } elseif ( is_singular( bbp_get_topic_post_type() ) ) {
825 $redirect = bbp_get_topic_permalink( $topic_id );
826 } elseif ( is_single() || is_page() ) {
827 $redirect = get_permalink();
828 }
829
830 wp_safe_redirect( $redirect );
831
832 // For good measure
833 exit();
834
835 // Fail! Handle errors
836 } elseif ( true == $is_subscription && 'bbp_unsubscribe' == $action ) {
837 bbp_add_error( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing from that topic!', 'bbpress' ) );
838 } elseif ( false == $is_subscription && 'bbp_subscribe' == $action ) {
839 bbp_add_error( 'bbp_subscribe', __( '<strong>ERROR</strong>: There was a problem subscribing to that topic!', 'bbpress' ) );
840 }
841 }
842
843 /** Edit **********************************************************************/
844
845 /**
846 * Handles the front end user editing
847 *
848 * @uses is_multisite() To check if it's a multisite
849 * @uses bbp_is_user_home() To check if the user is at home (the display page
850 * is the one of the logged in user)
851 * @uses get_option() To get the displayed user's new email id option
852 * @uses wpdb::prepare() To sanitize our sql query
853 * @uses wpdb::get_var() To execute our query and get back the variable
854 * @uses wpdb::query() To execute our query
855 * @uses wp_update_user() To update the user
856 * @uses delete_option() To delete the displayed user's email id option
857 * @uses bbp_get_user_profile_edit_url() To get the edit profile url
858 * @uses wp_safe_redirect() To redirect to the url
859 * @uses bbp_verify_nonce_request() To verify the nonce and check the request
860 * @uses current_user_can() To check if the current user can edit the user
861 * @uses do_action() Calls 'personal_options_update' or
862 * 'edit_user_options_update' (based on if it's the user home)
863 * with the displayed user id
864 * @uses edit_user() To edit the user based on the post data
865 * @uses get_userdata() To get the user data
866 * @uses is_email() To check if the string is an email id or not
867 * @uses wpdb::get_blog_prefix() To get the blog prefix
868 * @uses is_network_admin() To check if the user is the network admin
869 * @uses is_super_admin() To check if the user is super admin
870 * @uses revoke_super_admin() To revoke super admin priviledges
871 * @uses grant_super_admin() To grant super admin priviledges
872 * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
873 */
874 function bbp_edit_user_handler() {
875
876 // Bail if not a POST action
877 if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
878 return;
879
880 // Bail if action is not 'bbp-update-user'
881 if ( empty( $_POST['action'] ) || ( 'bbp-update-user' !== $_POST['action'] ) )
882 return;
883
884 // Get the displayed user ID
885 $user_id = bbp_get_displayed_user_id();
886
887 global $wpdb, $user_login, $super_admins;
888
889 // Execute confirmed email change. See send_confirmation_on_profile_email().
890 if ( is_multisite() && bbp_is_user_home_edit() && isset( $_GET['newuseremail'] ) ) {
891
892 $new_email = get_option( $user_id . '_new_email' );
893
894 if ( $new_email['hash'] == $_GET['newuseremail'] ) {
895 $user = new stdClass();
896 $user->ID = $user_id;
897 $user->user_email = esc_html( trim( $new_email['newemail'] ) );
898
899 if ( $wpdb->get_var( $wpdb->prepare( "SELECT user_login FROM {$wpdb->signups} WHERE user_login = %s", bbp_get_displayed_user_field( 'user_login' ) ) ) )
900 $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' ) ) );
901
902 wp_update_user( get_object_vars( $user ) );
903 delete_option( $user_id . '_new_email' );
904
905 wp_safe_redirect( add_query_arg( array( 'updated' => 'true' ), bbp_get_user_profile_edit_url( $user_id ) ) );
906 exit;
907 }
908
909 } elseif ( is_multisite() && bbp_is_user_home_edit() && !empty( $_GET['dismiss'] ) && ( $user_id . '_new_email' == $_GET['dismiss'] ) ) {
910
911 delete_option( $user_id . '_new_email' );
912 wp_safe_redirect( add_query_arg( array( 'updated' => 'true' ), bbp_get_user_profile_edit_url( $user_id ) ) );
913 exit;
914
915 }
916
917 // Nonce check
918 if ( ! bbp_verify_nonce_request( 'update-user_' . $user_id ) ) {
919 bbp_add_error( 'bbp_update_user_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
920 return;
921 }
922
923 // Cap check
924 if ( ! current_user_can( 'edit_user', $user_id ) ) {
925 bbp_add_error( 'bbp_update_user_capability', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
926 return;
927 }
928
929 // Do action based on who's profile you're editing
930 $edit_action = bbp_is_user_home_edit() ? 'personal_options_update' : 'edit_user_profile_update';
931 do_action( $edit_action, $user_id );
932
933 // Multisite handles the trouble for us ;)
934 if ( !is_multisite() ) {
935 $edit_user = edit_user( $user_id );
936
937 // Single site means we need to do some manual labor
938 } else {
939 $user = get_userdata( $user_id );
940
941 // Update the email address in signups, if present.
942 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 ) ) ) {
943 $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->signups} SET user_email = %s WHERE user_login = %s", $_POST['email'], $user_login ) );
944 }
945
946 // WPMU must delete the user from the current blog if WP added him after editing.
947 $delete_role = false;
948 $blog_prefix = $wpdb->get_blog_prefix();
949
950 if ( $user_id != $user_id ) {
951 $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:{}'" );
952 if ( !is_network_admin() && null == $cap && $_POST['role'] == '' ) {
953 $_POST['role'] = 'contributor';
954 $delete_role = true;
955 }
956 }
957
958 $edit_user = edit_user( $user_id );
959
960 // stops users being added to current blog when they are edited
961 if ( true === $delete_role ) {
962 delete_user_meta( $user_id, $blog_prefix . 'capabilities' );
963 }
964
965 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 ) ) {
966 empty( $_POST['super_admin'] ) ? revoke_super_admin( $user_id ) : grant_super_admin( $user_id );
967 }
968 }
969
970 // Error(s) editng the user, so copy them into the global
971 if ( is_wp_error( $edit_user ) ) {
972 bbpress()->errors = $edit_user;
973
974 // Successful edit to redirect
975 } elseif ( is_integer( $edit_user ) ) {
976 $redirect = add_query_arg( array( 'updated' => 'true' ), bbp_get_user_profile_edit_url( $edit_user ) );
977
978 wp_safe_redirect( $redirect );
979 exit;
980 }
981 }
982
983 /** END - Edit User ***********************************************************/
984
985 /**
986 * Get the topics that a user created
987 *
988 * @since bbPress (r2660)
989 *
990 * @param int $user_id Optional. User id
991 * @uses bbp_get_user_id() To get the topic id
992 * @uses bbp_has_topics() To get the topics created by the user
993 * @return array|bool Results if the user has created topics, otherwise false
994 */
995 function bbp_get_user_topics_started( $user_id = 0 ) {
996
997 // Validate user
998 $user_id = bbp_get_user_id( $user_id );
999 if ( empty( $user_id ) )
1000 return false;
1001
1002 // Query defaults
1003 $default_query = array(
1004 'author' => $user_id,
1005 'show_stickies' => false,
1006 'order' => 'DESC',
1007 );
1008
1009 // Try to get the topics
1010 $query = bbp_has_topics( $default_query );
1011 if ( empty( $query ) )
1012 return false;
1013
1014 return apply_filters( 'bbp_get_user_topics_started', $query, $user_id );
1015 }
1016
1017 /**
1018 * Get the total number of users on the forums
1019 *
1020 * @since bbPress (r2769)
1021 * @uses wp_cache_get() Check if query is in cache
1022 * @uses get_users() To execute our query and get the var back
1023 * @uses wp_cache_set() Set the query in the cache
1024 * @uses apply_filters() Calls 'bbp_get_total_users' with number of users
1025 * @return int Total number of users
1026 */
1027 function bbp_get_total_users() {
1028
1029 $bbp_total_users = wp_cache_get( 'bbp_total_users', 'bbpress' );
1030 if ( !empty( $bbp_total_users ) )
1031 return $bbp_total_users;
1032
1033 $bbp_total_users = count( get_users() );
1034
1035 wp_cache_set( 'bbp_total_users', $bbp_total_users, 'bbpress' );
1036
1037 return apply_filters( 'bbp_get_total_users', (int) $bbp_total_users );
1038 }
1039
1040 /** User Status ***************************************************************/
1041
1042 /**
1043 * Checks if the user has been marked as a spammer.
1044 *
1045 * @since bbPress (r3355)
1046 *
1047 * @param int $user_id int The ID for the user.
1048 * @return bool True if spammer, False if not.
1049 */
1050 function bbp_is_user_spammer( $user_id = 0 ) {
1051
1052 // Default to current user
1053 if ( empty( $user_id ) && is_user_logged_in() )
1054 $user_id = bbp_get_current_user_id();
1055
1056 // No user to check
1057 if ( empty( $user_id ) )
1058 return false;
1059
1060 // Assume user is not spam
1061 $is_spammer = false;
1062
1063 // Get user data
1064 $user = get_userdata( $user_id );
1065
1066 // No user found
1067 if ( empty( $user ) ) {
1068 $is_spammer = false;
1069
1070 // User found
1071 } else {
1072
1073 // Check if spam
1074 if ( !empty( $user->spam ) )
1075 $is_spammer = true;
1076
1077 if ( 1 == $user->user_status )
1078 $is_spammer = true;
1079 }
1080
1081 return apply_filters( 'bp_core_is_user_spammer', (bool) $is_spammer );
1082 }
1083
1084 /**
1085 * Mark a users topics and replies as spam when the user is marked as spam
1086 *
1087 * @since bbPress (r3405)
1088 *
1089 * @global WPDB $wpdb
1090 * @param int $user_id Optional. User ID to spam. Defaults to displayed user.
1091
1092 * @uses bbp_is_single_user()
1093 * @uses bbp_is_user_home()
1094 * @uses bbp_get_displayed_user_field()
1095 * @uses is_super_admin()
1096 * @uses get_blogs_of_user()
1097 * @uses get_current_blog_id()
1098 * @uses bbp_get_topic_post_type()
1099 * @uses bbp_get_reply_post_type()
1100 * @uses switch_to_blog()
1101 * @uses get_post_type()
1102 * @uses bbp_spam_topic()
1103 * @uses bbp_spam_reply()
1104 * @uses restore_current_blog()
1105 *
1106 * @return If no user ID passed
1107 */
1108 function bbp_make_spam_user( $user_id = 0 ) {
1109
1110 // Use displayed user if it's not yourself
1111 if ( empty( $user_id ) && bbp_is_single_user() && !bbp_is_user_home() )
1112 $user_id = bbp_get_displayed_user_id();
1113
1114 // Bail if no user ID
1115 if ( empty( $user_id ) )
1116 return;
1117
1118 // Bail if user ID is super admin
1119 if ( is_super_admin( $user_id ) )
1120 return;
1121
1122 // Arm the torpedos
1123 global $wpdb;
1124
1125 // Get the blog IDs of the user to mark as spam
1126 $blogs = get_blogs_of_user( $user_id, true );
1127
1128 // If user has no blogs, they are a guest on this site
1129 if ( empty( $blogs ) )
1130 $blogs[$wpdb->blogid] = array();
1131
1132 // We only need the keys
1133 $blogs = array_keys( $blogs );
1134
1135 // Make array of post types to mark as spam
1136 $post_types = array( bbp_get_topic_post_type(), bbp_get_reply_post_type() );
1137 $post_types = "'" . implode( "', '", $post_types ) . "'";
1138 $status = bbp_get_public_status_id();
1139
1140 // Loop through blogs and remove their posts
1141 foreach ( (array) $blogs as $blog_id ) {
1142
1143 // Switch to the blog ID
1144 switch_to_blog( $blog_id );
1145
1146 // Get topics and replies
1147 $posts = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_author = {$user_id} AND post_status = '{$status}' AND post_type IN ({$post_types})" );
1148
1149 // Loop through posts and spam them
1150 if ( !empty( $posts ) ) {
1151 foreach ( $posts as $post_id ) {
1152
1153 // The routines for topics ang replies are different, so use the
1154 // correct one based on the post type
1155 switch ( get_post_type( $post_id ) ) {
1156
1157 case bbp_get_topic_post_type() :
1158 bbp_spam_topic( $post_id );
1159 break;
1160
1161 case bbp_get_reply_post_type() :
1162 bbp_spam_reply( $post_id );
1163 break;
1164 }
1165 }
1166 }
1167
1168 // Switch back to current blog
1169 restore_current_blog();
1170 }
1171 }
1172
1173 /**
1174 * Mark a users topics and replies as spam when the user is marked as spam
1175 *
1176 * @since bbPress (r3405)
1177 *
1178 * @global WPDB $wpdb
1179 * @param int $user_id Optional. User ID to unspam. Defaults to displayed user.
1180 *
1181 * @uses bbp_is_single_user()
1182 * @uses bbp_is_user_home()
1183 * @uses bbp_get_displayed_user_field()
1184 * @uses is_super_admin()
1185 * @uses get_blogs_of_user()
1186 * @uses bbp_get_topic_post_type()
1187 * @uses bbp_get_reply_post_type()
1188 * @uses switch_to_blog()
1189 * @uses get_post_type()
1190 * @uses bbp_unspam_topic()
1191 * @uses bbp_unspam_reply()
1192 * @uses restore_current_blog()
1193 *
1194 * @return If no user ID passed
1195 */
1196 function bbp_make_ham_user( $user_id = 0 ) {
1197
1198 // Use displayed user if it's not yourself
1199 if ( empty( $user_id ) && bbp_is_single_user() && !bbp_is_user_home() )
1200 $user_id = bbp_get_displayed_user_field();
1201
1202 // Bail if no user ID
1203 if ( empty( $user_id ) )
1204 return;
1205
1206 // Bail if user ID is super admin
1207 if ( is_super_admin( $user_id ) )
1208 return;
1209
1210 // Arm the torpedos
1211 global $wpdb;
1212
1213 // Get the blog IDs of the user to mark as spam
1214 $blogs = get_blogs_of_user( $user_id, true );
1215
1216 // If user has no blogs, they are a guest on this site
1217 if ( empty( $blogs ) )
1218 $blogs[$wpdb->blogid] = array();
1219
1220 // We only need the keys
1221 $blogs = array_keys( $blogs );
1222
1223 // Make array of post types to mark as spam
1224 $post_types = array( bbp_get_topic_post_type(), bbp_get_reply_post_type() );
1225 $post_types = "'" . implode( "', '", $post_types ) . "'";
1226 $status = bbp_get_spam_status_id();
1227
1228 // Loop through blogs and remove their posts
1229 foreach ( (array) $blogs as $blog_id ) {
1230
1231 // Switch to the blog ID
1232 switch_to_blog( $blog_id );
1233
1234 // Get topics and replies
1235 $posts = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_author = {$user_id} AND post_status = '{$status}' AND post_type IN ({$post_types})" );
1236
1237 // Loop through posts and spam them
1238 if ( !empty( $posts ) ) {
1239 foreach ( $posts as $post_id ) {
1240
1241 // The routines for topics ang replies are different, so use the
1242 // correct one based on the post type
1243 switch ( get_post_type( $post_id ) ) {
1244
1245 case bbp_get_topic_post_type() :
1246 bbp_unspam_topic( $post_id );
1247 break;
1248
1249 case bbp_get_reply_post_type() :
1250 bbp_unspam_reply( $post_id );
1251 break;
1252 }
1253 }
1254 }
1255
1256 // Switch back to current blog
1257 restore_current_blog();
1258 }
1259 }
1260
1261 /**
1262 * Checks if the user has been marked as deleted.
1263 *
1264 * @since bbPress (r3355)
1265 *
1266 * @param int $user_id int The ID for the user.
1267 * @return bool True if deleted, False if not.
1268 */
1269 function bbp_is_user_deleted( $user_id = 0 ) {
1270
1271 // Default to current user
1272 if ( empty( $user_id ) && is_user_logged_in() )
1273 $user_id = bbp_get_current_user_id();
1274
1275 // No user to check
1276 if ( empty( $user_id ) )
1277 return false;
1278
1279 // Assume user is not deleted
1280 $is_deleted = false;
1281
1282 // Get user data
1283 $user = get_userdata( $user_id );
1284
1285 // No user found
1286 if ( empty( $user ) ) {
1287 $is_deleted = true;
1288
1289 // User found
1290 } else {
1291
1292 // Check if deleted
1293 if ( !empty( $user->deleted ) )
1294 $is_deleted = true;
1295
1296 if ( 2 == $user->user_status )
1297 $is_deleted = true;
1298
1299 }
1300
1301 return apply_filters( 'bp_core_is_user_deleted', (bool) $is_deleted );
1302 }
1303
1304 /**
1305 * Checks if user is active
1306 *
1307 * @since bbPress (r3502)
1308 *
1309 * @uses is_user_logged_in() To check if user is logged in
1310 * @uses bbp_get_displayed_user_id() To get current user ID
1311 * @uses bbp_is_user_spammer() To check if user is spammer
1312 * @uses bbp_is_user_deleted() To check if user is deleted
1313 *
1314 * @param int $user_id The user ID to check
1315 * @return bool True if public, false if not
1316 */
1317 function bbp_is_user_active( $user_id = 0 ) {
1318
1319 // Default to current user
1320 if ( empty( $user_id ) && is_user_logged_in() )
1321 $user_id = bbp_get_current_user_id();
1322
1323 // No user to check
1324 if ( empty( $user_id ) )
1325 return false;
1326
1327 // Check spam
1328 if ( bbp_is_user_spammer( $user_id ) )
1329 return false;
1330
1331 // Check deleted
1332 if ( bbp_is_user_deleted( $user_id ) )
1333 return false;
1334
1335 // Assume true if not spam or deleted
1336 return true;
1337 }
1338
1339 /**
1340 * Checks if user is not active.
1341 *
1342 * @since bbPress (r3502)
1343 *
1344 * @uses is_user_logged_in() To check if user is logged in
1345 * @uses bbp_get_displayed_user_id() To get current user ID
1346 * @uses bbp_is_user_active() To check if user is active
1347 *
1348 * @param int $user_id The user ID to check. Defaults to current user ID
1349 * @return bool True if inactive, false if active
1350 */
1351 function bbp_is_user_inactive( $user_id = 0 ) {
1352
1353 // Default to current user
1354 if ( empty( $user_id ) && is_user_logged_in() )
1355 $user_id = bbp_get_current_user_id();
1356
1357 // No user to check
1358 if ( empty( $user_id ) )
1359 return false;
1360
1361 // Return the inverse of active
1362 return !bbp_is_user_active( $user_id );
1363 }
1364
1365 /**
1366 * Return a user's main role
1367 *
1368 * @since bbPress (r3860)
1369 *
1370 * @param int $user_id
1371 * @uses bbp_get_user_id() To get the user id
1372 * @uses get_userdata() To get the user data
1373 * @uses apply_filters() Calls 'bbp_get_user_role' with the
1374 * role and user id
1375 * @return string
1376 */
1377 function bbp_get_user_role( $user_id = 0 ) {
1378
1379 // Validate user id
1380 $user_id = bbp_get_user_id( $user_id, false, false );
1381 if ( empty( $user_id ) )
1382 return false;
1383
1384 // Get userdata
1385 $user = get_userdata( $user_id );
1386
1387 // Get the user's main role
1388 $role = isset( $user->roles ) ? array_shift( $user->roles ) : bbp_get_anonymous_role();
1389
1390 return apply_filters( 'bbp_get_user_role', $role, $user_id, $user );
1391 }
1392
1393 /** Premissions ***************************************************************/
1394
1395 /**
1396 * Redirect if unathorized user is attempting to edit another user
1397 *
1398 * This is hooked to 'bbp_template_redirect' and controls the conditions under
1399 * which a user can edit another user (or themselves.) If these conditions are
1400 * met. We assume a user cannot perform this task, and look for ways they can
1401 * earn the ability to access this template.
1402 *
1403 * @since bbPress (r3605)
1404 *
1405 * @uses bbp_is_topic_edit()
1406 * @uses current_user_can()
1407 * @uses bbp_get_topic_id()
1408 * @uses wp_safe_redirect()
1409 * @uses bbp_get_topic_permalink()
1410 */
1411 function bbp_check_user_edit() {
1412
1413 // Bail if not editing a topic
1414 if ( ! bbp_is_single_user_edit() )
1415 return;
1416
1417 // Default to false
1418 $redirect = true;
1419
1420 // Allow user to edit their own profile
1421 if ( bbp_is_user_home_edit() ) {
1422 $redirect = false;
1423
1424 // Allow if current user can edit the displayed user
1425 } elseif ( current_user_can( 'edit_user', bbp_get_displayed_user_id() ) ) {
1426 $redirect = false;
1427
1428 // Allow if user can manage network users, or edit-any is enabled
1429 } elseif ( current_user_can( 'manage_network_users' ) || apply_filters( 'enable_edit_any_user_configuration', false ) ) {
1430 $redirect = false;
1431 }
1432
1433 // Maybe redirect back to profile page
1434 if ( true === $redirect ) {
1435 wp_safe_redirect( bbp_get_user_profile_url( bbp_get_displayed_user_id() ) );
1436 exit();
1437 }
1438 }
1439
1440 /** Converter *****************************************************************/
1441
1442 /**
1443 * Convert passwords from previous platfrom encryption to WordPress encryption.
1444 *
1445 * @since bbPress (r3813)
1446 * @global WPDB $wpdb
1447 */
1448 function bbp_user_maybe_convert_pass() {
1449
1450 // Bail if no username
1451 $username = !empty( $_POST['log'] ) ? $_POST['log'] : '';
1452 if ( empty( $username ) )
1453 return;
1454
1455 global $wpdb;
1456
1457 // Bail if no user password to convert
1458 $row = $wpdb->get_row( "SELECT * FROM {$wpdb->users} INNER JOIN {$wpdb->usermeta} ON user_id = ID WHERE meta_key = '_bbp_class' AND user_login = '{$username}' LIMIT 1" );
1459 if ( empty( $row ) || is_wp_error( $row ) )
1460 return;
1461
1462 // Setup admin (to include converter)
1463 require_once( bbpress()->plugin_dir . 'bbp-admin/bbp-admin.php' );
1464
1465 // Convert password
1466 require_once( bbpress()->admin->admin_dir . 'bbp-converter.php' );
1467 require_once( bbpress()->admin->admin_dir . 'converters/' . $row->meta_value . '.php' );
1468
1469 // Create the converter
1470 $converter = bbp_new_converter( $row->meta_value );
1471
1472 // Try to call the conversion method
1473 if ( is_a( $converter, 'BBP_Converter_Base' ) && method_exists( $converter, 'callback_pass' ) ) {
1474 $converter->callback_pass( $username, $_POST['pwd'] );
1475 }
1476 }
1477