PluginProbe
bbPress / 2.0-rc-3
bbPress v2.0-rc-3
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.0-rc-3, at bbp-includes/bbp-user-functions.php

1,171 lines 36.6 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 /** Favorites *****************************************************************/
148
149 /**
150 * Get the users who have made the topic favorite
151 *
152 * @since bbPress (r2658)
153 *
154 * @param int $topic_id Optional. Topic id
155 * @uses wpdb::get_col() To execute our query and get the column back
156 * @uses apply_filters() Calls 'bbp_get_topic_favoriters' with the users and
157 * topic id
158 * @return array|bool Results if the topic has any favoriters, otherwise false
159 */
160 function bbp_get_topic_favoriters( $topic_id = 0 ) {
161 if ( empty( $topic_id ) )
162 return;
163
164 global $wpdb;
165
166 // Get the users who have favorited the topic
167 $users = $wpdb->get_col( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '_bbp_favorites' and FIND_IN_SET('{$topic_id}', meta_value) > 0" );
168 $users = apply_filters( 'bbp_get_topic_favoriters', $users, $topic_id );
169
170 if ( !empty( $users ) )
171 return $users;
172
173 return false;
174 }
175
176 /**
177 * Get a user's favorite topics
178 *
179 * @since bbPress (r2652)
180 *
181 * @param int $user_id Optional. User id
182 * @uses bbp_get_user_favorites_topic_ids() To get the user's favorites
183 * @uses bbp_has_topics() To get the topics
184 * @uses apply_filters() Calls 'bbp_get_user_favorites' with the topic query and
185 * user id
186 * @return array|bool Results if user has favorites, otherwise false
187 */
188 function bbp_get_user_favorites( $user_id = 0 ) {
189 if ( !$user_id = bbp_get_user_id( $user_id ) )
190 return false;
191
192 // If user has favorites, load them
193 if ( $favorites = bbp_get_user_favorites_topic_ids( $user_id ) ) {
194
195 // Setup the topics query
196 $topics_query = bbp_has_topics( array( 'post__in' => $favorites ) );
197
198 return apply_filters( 'bbp_get_user_favorites', $topics_query, $user_id );
199 }
200
201 return false;
202 }
203
204 /**
205 * Get a user's favorite topics' ids
206 *
207 * @since bbPress (r2652)
208 *
209 * @param int $user_id Optional. User id
210 * @uses bbp_get_user_id() To get the user id
211 * @uses get_user_meta() To get the user favorites
212 * @uses apply_filters() Calls 'bbp_get_user_favorites_topic_ids' with
213 * the favorites and user id
214 * @return array|bool Results if user has favorites, otherwise false
215 */
216 function bbp_get_user_favorites_topic_ids( $user_id = 0 ) {
217 if ( !$user_id = bbp_get_user_id( $user_id ) )
218 return false;
219
220 $favorites = (string) get_user_meta( $user_id, '_bbp_favorites', true );
221 $favorites = (array) explode( ',', $favorites );
222 $favorites = array_filter( $favorites );
223
224 return apply_filters( 'bbp_get_user_favorites_topic_ids', $favorites, $user_id );
225 }
226
227 /**
228 * Check if a topic is in user's favorites or not
229 *
230 * @since bbPress (r2652)
231 *
232 * @param int $user_id Optional. User id
233 * @param int $topic_id Optional. Topic id
234 * @uses bbp_get_user_id() To get the user id
235 * @uses bbp_get_user_favorites_topic_ids() To get the user favorites
236 * @uses bbp_get_topic() To get the topic
237 * @uses bbp_get_topic_id() To get the topic id
238 * @uses apply_filters() Calls 'bbp_is_user_favorite' with the bool, user id,
239 * topic id and favorites
240 * @return bool True if the topic is in user's favorites, otherwise false
241 */
242 function bbp_is_user_favorite( $user_id = 0, $topic_id = 0 ) {
243 global $post;
244
245 if ( !$user_id = bbp_get_user_id( $user_id, true, true ) )
246 return false;
247
248 $favorites = bbp_get_user_favorites_topic_ids( $user_id );
249
250 if ( !empty( $topic_id ) ) {
251 $topic = bbp_get_topic( $topic_id );
252 $topic_id = !empty( $topic ) ? $topic->ID : 0;
253 } elseif ( !$topic_id = bbp_get_topic_id() ) {
254 if ( empty( $post ) )
255 return false;
256
257 $topic_id = $post->ID;
258 }
259
260 if ( empty( $favorites ) || empty( $topic_id ) )
261 return false;
262
263 return apply_filters( 'bbp_is_user_favorite', (bool) in_array( $topic_id, $favorites ), $user_id, $topic_id, $favorites );
264 }
265
266 /**
267 * Add a topic to user's favorites
268 *
269 * @since bbPress (r2652)
270 *
271 * @param int $user_id Optional. User id
272 * @param int $topic_id Optional. Topic id
273 * @uses bbp_get_user_favorites_topic_ids() To get the user favorites
274 * @uses update_user_meta() To update the user favorites
275 * @uses do_action() Calls 'bbp_add_user_favorite' with the user id and topic id
276 * @return bool Always true
277 */
278 function bbp_add_user_favorite( $user_id = 0, $topic_id = 0 ) {
279 if ( empty( $user_id ) || empty( $topic_id ) )
280 return false;
281
282 $favorites = (array) bbp_get_user_favorites_topic_ids( $user_id );
283
284 if ( !$topic = bbp_get_topic( $topic_id ) )
285 return false;
286
287 if ( !in_array( $topic_id, $favorites ) ) {
288 $favorites[] = $topic_id;
289 $favorites = array_filter( $favorites );
290 $favorites = (string) implode( ',', $favorites );
291 update_user_meta( $user_id, '_bbp_favorites', $favorites );
292 }
293
294 do_action( 'bbp_add_user_favorite', $user_id, $topic_id );
295
296 return true;
297 }
298
299 /**
300 * Remove a topic from user's favorites
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_favorites_topic_ids() To get the user favorites
307 * @uses update_user_meta() To update the user favorites
308 * @uses delete_user_meta() To delete the user favorites meta
309 * @uses do_action() Calls 'bbp_remove_user_favorite' with the user & topic id
310 * @return bool True if the topic was removed from user's favorites, otherwise
311 * false
312 */
313 function bbp_remove_user_favorite( $user_id, $topic_id ) {
314 if ( empty( $user_id ) || empty( $topic_id ) )
315 return false;
316
317 if ( !$favorites = (array) bbp_get_user_favorites_topic_ids( $user_id ) )
318 return false;
319
320 if ( is_numeric( $pos = array_search( $topic_id, $favorites ) ) ) {
321 array_splice( $favorites, $pos, 1 );
322 $favorites = array_filter( $favorites );
323
324 if ( !empty( $favorites ) ) {
325 $favorites = implode( ',', $favorites );
326 update_user_meta( $user_id, '_bbp_favorites', $favorites );
327 } else {
328 delete_user_meta( $user_id, '_bbp_favorites' );
329 }
330 }
331
332 do_action( 'bbp_remove_user_favorite', $user_id, $topic_id );
333
334 return true;
335 }
336
337 /**
338 * Handles the front end adding and removing of favorite topics
339 *
340 * @uses bbp_get_user_id() To get the user id
341 * @uses current_user_can() To check if the current user can edit the user
342 * @uses bbPress:errors:add() To log the error messages
343 * @uses bbp_is_user_favorite() To check if the topic is in user's favorites
344 * @uses bbp_remove_user_favorite() To remove the user favorite
345 * @uses bbp_add_user_favorite() To add the user favorite
346 * @uses do_action() Calls 'bbp_favorites_handler' with success, user id, topic
347 * id and action
348 * @uses bbp_is_favorites() To check if it's the favorites page
349 * @uses bbp_get_favorites_link() To get the favorites page link
350 * @uses bbp_get_topic_permalink() To get the topic permalink
351 * @uses wp_redirect() To redirect to the url
352 */
353 function bbp_favorites_handler() {
354
355 if ( !bbp_is_favorites_active() )
356 return false;
357
358 // Bail if not a GET action
359 if ( 'GET' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
360 return;
361
362 // Bail if required GET actions aren't passed
363 if ( empty( $_GET['topic_id'] ) || empty( $_GET['action'] ) )
364 return;
365
366 // Setup possible get actions
367 $possible_actions = array(
368 'bbp_favorite_add',
369 'bbp_favorite_remove',
370 );
371
372 // Bail if actions aren't meant for this function
373 if ( !in_array( $_GET['action'], $possible_actions ) )
374 return;
375
376 // What action is taking place?
377 $action = $_GET['action'];
378
379 // Get user_id
380 $user_id = bbp_get_user_id( 0, true, true );
381
382 // Check current user's ability to edit the user
383 if ( !current_user_can( 'edit_user', $user_id ) )
384 bbp_add_error( 'bbp_favorite_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'bbpress' ) );
385
386 // Load favorite info
387 if ( !$topic_id = intval( $_GET['topic_id'] ) )
388 bbp_add_error( 'bbp_favorite_topic_id', __( '<strong>ERROR</strong>: No topic was found! Which topic are you marking/unmarking as favorite?', 'bbpress' ) );
389
390 $is_favorite = bbp_is_user_favorite( $user_id, $topic_id );
391 $success = false;
392
393 // Handle insertion into posts table
394 if ( !empty( $topic_id ) && !empty( $user_id ) && ( !bbp_has_errors() ) ) {
395
396 if ( $is_favorite && 'bbp_favorite_remove' == $action ) {
397 $success = bbp_remove_user_favorite( $user_id, $topic_id );
398 } elseif ( !$is_favorite && 'bbp_favorite_add' == $action ) {
399 $success = bbp_add_user_favorite( $user_id, $topic_id );
400 }
401
402 // Do additional favorites actions
403 do_action( 'bbp_favorites_handler', $success, $user_id, $topic_id, $action );
404
405 // Check for missing reply_id or error
406 if ( true == $success ) {
407
408 // Redirect back to new reply
409 if ( bbp_is_favorites() ) {
410 $redirect = bbp_get_favorites_permalink( $user_id );
411 } elseif ( bbp_is_single_user() ) {
412 $redirect = bbp_get_user_profile_url();
413 } elseif ( is_singular( bbp_get_topic_post_type() ) ) {
414 $redirect = bbp_get_topic_permalink( $topic_id );
415 } elseif ( is_single() || is_page() ) {
416 $redirect = get_permalink();
417 }
418
419 wp_redirect( $redirect );
420
421 // For good measure
422 exit();
423
424 // Handle errors
425 } else {
426 if ( $is_favorite && 'bbp_favorite_remove' == $action ) {
427 bbp_add_error( 'bbp_favorite_remove', __( '<strong>ERROR</strong>: There was a problem removing that topic from favorites!', 'bbpress' ) );
428 } elseif ( !$is_favorite && 'bbp_favorite_add' == $action ) {
429 bbp_add_error( 'bbp_favorite_add', __( '<strong>ERROR</strong>: There was a problem favoriting that topic!', 'bbpress' ) );
430 }
431 }
432 }
433 }
434
435 /** Subscriptions *************************************************************/
436
437 /**
438 * Get the users who have subscribed to the topic
439 *
440 * @since bbPress (r2668)
441 *
442 * @param int $topic_id Optional. Topic id
443 * @uses wpdb::get_col() To execute our query and get the column back
444 * @uses apply_filters() Calls 'bbp_get_topic_subscribers' with the subscribers
445 * @return array|bool Results if the topic has any subscribers, otherwise false
446 */
447 function bbp_get_topic_subscribers( $topic_id = 0 ) {
448 if ( empty( $topic_id ) )
449 return;
450
451 global $wpdb;
452
453 // Get the users who have favorited the topic
454 $users = $wpdb->get_col( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '_bbp_subscriptions' and FIND_IN_SET('{$topic_id}', meta_value) > 0" );
455 $users = apply_filters( 'bbp_get_topic_subscribers', $users );
456
457 if ( !empty( $users ) )
458 return $users;
459
460 return false;
461 }
462
463 /**
464 * Get a user's subscribed topics
465 *
466 * @since bbPress (r2668)
467 *
468 * @param int $user_id Optional. User id
469 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
470 * @uses bbp_has_topics() To get the topics
471 * @uses apply_filters() Calls 'bbp_get_user_subscriptions' with the topic query
472 * and user id
473 * @return array|bool Results if user has subscriptions, otherwise false
474 */
475 function bbp_get_user_subscriptions( $user_id = 0 ) {
476
477 // Default to the displayed user
478 if ( !$user_id = bbp_get_user_id( $user_id ) )
479 return false;
480
481 // If user has subscriptions, load them
482 if ( $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id ) ) {
483 $query = bbp_has_topics( array( 'post__in' => $subscriptions ) );
484 return apply_filters( 'bbp_get_user_subscriptions', $query, $user_id );
485 }
486
487 return false;
488 }
489
490 /**
491 * Get a user's subscribed topics' ids
492 *
493 * @since bbPress (r2668)
494 *
495 * @param int $user_id Optional. User id
496 * @uses bbp_get_user_id() To get the user id
497 * @uses get_user_meta() To get the user's subscriptions
498 * @uses apply_filters() Calls 'bbp_get_user_subscribed_topic_ids' with
499 * the subscriptions and user id
500 * @return array|bool Results if user has subscriptions, otherwise false
501 */
502 function bbp_get_user_subscribed_topic_ids( $user_id = 0 ) {
503 if ( !$user_id = bbp_get_user_id( $user_id ) )
504 return false;
505
506 $subscriptions = (string) get_user_meta( $user_id, '_bbp_subscriptions', true );
507 $subscriptions = (array) explode( ',', $subscriptions );
508 $subscriptions = array_filter( $subscriptions );
509
510 return apply_filters( 'bbp_get_user_subscribed_topic_ids', $subscriptions, $user_id );
511 }
512
513 /**
514 * Check if a topic is in user's subscription list or not
515 *
516 * @since bbPress (r2668)
517 *
518 * @param int $user_id Optional. User id
519 * @param int $topic_id Optional. Topic id
520 * @uses bbp_get_user_id() To get the user id
521 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
522 * @uses bbp_get_topic() To get the topic
523 * @uses bbp_get_topic_id() To get the topic id
524 * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id,
525 * topic id and subsriptions
526 * @return bool True if the topic is in user's subscriptions, otherwise false
527 */
528 function bbp_is_user_subscribed( $user_id = 0, $topic_id = 0 ) {
529 global $post;
530
531 if ( !$user_id = bbp_get_user_id( $user_id, true, true ) )
532 return false;
533
534 $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id );
535
536 if ( !empty( $topic_id ) ) {
537 $topic = bbp_get_topic( $topic_id );
538 $topic_id = !empty( $topic ) ? $topic->ID : 0;
539 } elseif ( !$topic_id = bbp_get_topic_id() ) {
540 if ( empty( $post ) )
541 return false;
542
543 $topic_id = $post->ID;
544 }
545
546 if ( empty( $subscriptions ) || empty( $topic_id ) )
547 return false;
548
549 return apply_filters( 'bbp_is_user_subscribed', (bool) in_array( $topic_id, $subscriptions ), $user_id, $topic_id, $subscriptions );
550 }
551
552 /**
553 * Add a topic to user's subscriptions
554 *
555 * @since bbPress (r2668)
556 *
557 * @param int $user_id Optional. User id
558 * @param int $topic_id Optional. Topic id
559 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
560 * @uses bbp_get_topic() To get the topic
561 * @uses update_user_meta() To update the user's subscriptions
562 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id
563 * @return bool Always true
564 */
565 function bbp_add_user_subscription( $user_id = 0, $topic_id = 0 ) {
566 if ( empty( $user_id ) || empty( $topic_id ) )
567 return false;
568
569 $subscriptions = (array) bbp_get_user_subscribed_topic_ids( $user_id );
570
571 if ( !$topic = bbp_get_topic( $topic_id ) )
572 return false;
573
574 if ( !in_array( $topic_id, $subscriptions ) ) {
575 $subscriptions[] = $topic_id;
576 $subscriptions = array_filter( $subscriptions );
577 $subscriptions = (string) implode( ',', $subscriptions );
578 update_user_meta( $user_id, '_bbp_subscriptions', $subscriptions );
579 }
580
581 do_action( 'bbp_add_user_subscription', $user_id, $topic_id );
582
583 return true;
584 }
585
586 /**
587 * Remove a topic from user's subscriptions
588 *
589 * @since bbPress (r2668)
590 *
591 * @param int $user_id Optional. User id
592 * @param int $topic_id Optional. Topic id
593 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
594 * @uses update_user_meta() To update the user's subscriptions
595 * @uses delete_user_meta() To delete the user's subscriptions meta
596 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and
597 * topic id
598 * @return bool True if the topic was removed from user's subscriptions,
599 * otherwise false
600 */
601 function bbp_remove_user_subscription( $user_id, $topic_id ) {
602 if ( empty( $user_id ) || empty( $topic_id ) )
603 return false;
604
605 $subscriptions = (array) bbp_get_user_subscribed_topic_ids( $user_id );
606
607 if ( empty( $subscriptions ) )
608 return false;
609
610 if ( is_numeric( $pos = array_search( $topic_id, $subscriptions ) ) ) {
611 array_splice( $subscriptions, $pos, 1 );
612 $subscriptions = array_filter( $subscriptions );
613
614 if ( !empty( $subscriptions ) ) {
615 $subscriptions = implode( ',', $subscriptions );
616 update_user_meta( $user_id, '_bbp_subscriptions', $subscriptions );
617 } else {
618 delete_user_meta( $user_id, '_bbp_subscriptions' );
619 }
620 }
621
622 do_action( 'bbp_remove_user_subscription', $user_id, $topic_id );
623
624 return true;
625 }
626
627 /**
628 * Handles the front end subscribing and unsubscribing topics
629 *
630 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
631 * @uses bbp_get_user_id() To get the user id
632 * @uses current_user_can() To check if the current user can edit the user
633 * @uses bbPress:errors:add() To log the error messages
634 * @uses bbp_is_user_subscribed() To check if the topic is in user's
635 * subscriptions
636 * @uses bbp_remove_user_subscription() To remove the user subscription
637 * @uses bbp_add_user_subscription() To add the user subscription
638 * @uses do_action() Calls 'bbp_subscriptions_handler' with success, user id,
639 * topic id and action
640 * @uses bbp_is_subscription() To check if it's the subscription page
641 * @uses bbp_get_subscription_link() To get the subscription page link
642 * @uses bbp_get_topic_permalink() To get the topic permalink
643 * @uses wp_redirect() To redirect to the url
644 */
645 function bbp_subscriptions_handler() {
646
647 if ( !bbp_is_subscriptions_active() )
648 return false;
649
650 // Bail if not a GET action
651 if ( 'GET' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
652 return;
653
654 // Bail if required GET actions aren't passed
655 if ( empty( $_GET['topic_id'] ) || empty( $_GET['action'] ) )
656 return;
657
658 // Setup possible get actions
659 $possible_actions = array(
660 'bbp_subscribe',
661 'bbp_unsubscribe',
662 );
663
664 // Bail if actions aren't meant for this function
665 if ( !in_array( $_GET['action'], $possible_actions ) )
666 return;
667
668 // What action is taking place?
669 $action = $_GET['action'];
670
671 // Get user_id
672 $user_id = bbp_get_user_id( 0, true, true );
673
674 // Check current user's ability to edit the user
675 if ( !current_user_can( 'edit_user', $user_id ) )
676 bbp_add_error( 'bbp_subscription_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'bbpress' ) );
677
678 // Load subscription info
679 if ( !$topic_id = intval( $_GET['topic_id'] ) )
680 bbp_add_error( 'bbp_subscription_topic_id', __( '<strong>ERROR</strong>: No topic was found! Which topic are you subscribing/unsubscribing to?', 'bbpress' ) );
681
682 if ( !bbp_has_errors() ) {
683
684 $is_subscription = bbp_is_user_subscribed( $user_id, $topic_id );
685 $success = false;
686
687 if ( $is_subscription && 'bbp_unsubscribe' == $action ) {
688 $success = bbp_remove_user_subscription( $user_id, $topic_id );
689 } elseif ( !$is_subscription && 'bbp_subscribe' == $action ) {
690 $success = bbp_add_user_subscription( $user_id, $topic_id );
691 }
692
693 // Do additional subscriptions actions
694 do_action( 'bbp_subscriptions_handler', $success, $user_id, $topic_id, $action );
695
696 // Check for missing reply_id or error
697 if ( true == $success ) {
698
699 // Redirect back to new reply
700 if ( bbp_is_subscriptions() ) {
701 $redirect = bbp_get_subscriptions_permalink( $user_id );
702 } elseif( bbp_is_single_user() ) {
703 $redirect = bbp_get_user_profile_url();
704 } elseif ( is_singular( bbp_get_topic_post_type() ) ) {
705 $redirect = bbp_get_topic_permalink( $topic_id );
706 } elseif ( is_single() || is_page() ) {
707 $redirect = get_permalink();
708 }
709
710 wp_redirect( $redirect );
711
712 // For good measure
713 exit();
714
715 // Handle errors
716 } else {
717 if ( $is_subscription && 'bbp_unsubscribe' == $action ) {
718 bbp_add_error( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing from that topic!', 'bbpress' ) );
719 } elseif ( !$is_subscription && 'bbp_subscribe' == $action ) {
720 bbp_add_error( 'bbp_subscribe', __( '<strong>ERROR</strong>: There was a problem subscribing to that topic!', 'bbpress' ) );
721 }
722 }
723 }
724 }
725
726 /** Edit **********************************************************************/
727
728 /**
729 * Handles the front end user editing
730 *
731 * @uses is_multisite() To check if it's a multisite
732 * @uses bbp_is_user_home() To check if the user is at home (the display page
733 * is the one of the logged in user)
734 * @uses get_option() To get the displayed user's new email id option
735 * @uses wpdb::prepare() To sanitize our sql query
736 * @uses wpdb::get_var() To execute our query and get back the variable
737 * @uses wpdb::query() To execute our query
738 * @uses wp_update_user() To update the user
739 * @uses delete_option() To delete the displayed user's email id option
740 * @uses bbp_get_user_profile_edit_url() To get the edit profile url
741 * @uses wp_redirect() To redirect to the url
742 * @uses check_admin_referer() To verify the nonce and check the referer
743 * @uses current_user_can() To check if the current user can edit the user
744 * @uses do_action() Calls 'personal_options_update' or
745 * 'edit_user_options_update' (based on if it's the user home)
746 * with the displayed user id
747 * @uses edit_user() To edit the user based on the post data
748 * @uses get_userdata() To get the user data
749 * @uses is_email() To check if the string is an email id or not
750 * @uses wpdb::get_blog_prefix() To get the blog prefix
751 * @uses is_network_admin() To check if the user is the network admin
752 * @uses is_super_admin() To check if the user is super admin
753 * @uses revoke_super_admin() To revoke super admin priviledges
754 * @uses grant_super_admin() To grant super admin priviledges
755 * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
756 */
757 function bbp_edit_user_handler() {
758
759 // Bail if not a POST action
760 if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
761 return;
762
763 // Bail if action is not 'bbp-update-user'
764 if ( empty( $_POST['action'] ) || ( 'bbp-update-user' !== $_POST['action'] ) )
765 return;
766
767 // Get the displayed user ID
768 $user_id = bbp_get_displayed_user_id();
769
770 global $wpdb;
771
772 // Execute confirmed email change. See send_confirmation_on_profile_email().
773 if ( is_multisite() && bbp_is_user_home() && isset( $_GET['newuseremail'] ) ) {
774
775 $new_email = get_option( $user_id . '_new_email' );
776
777 if ( $new_email['hash'] == $_GET['newuseremail'] ) {
778 $user->ID = $user_id;
779 $user->user_email = esc_html( trim( $new_email['newemail'] ) );
780
781 if ( $wpdb->get_var( $wpdb->prepare( "SELECT user_login FROM {$wpdb->signups} WHERE user_login = %s", bbp_get_displayed_user_field( 'user_login' ) ) ) )
782 $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' ) ) );
783
784 wp_update_user( get_object_vars( $user ) );
785 delete_option( $user_id . '_new_email' );
786
787 wp_redirect( add_query_arg( array( 'updated' => 'true' ), bbp_get_user_profile_edit_url( $user_id ) ) );
788 exit;
789 }
790
791 } elseif ( is_multisite() && bbp_is_user_home() && !empty( $_GET['dismiss'] ) && ( $user_id . '_new_email' == $_GET['dismiss'] ) ) {
792
793 delete_option( $user_id . '_new_email' );
794 wp_redirect( add_query_arg( array( 'updated' => 'true' ), bbp_get_user_profile_edit_url( $user_id ) ) );
795 exit;
796
797 }
798
799 check_admin_referer( 'update-user_' . $user_id );
800
801 if ( !current_user_can( 'edit_user', $user_id ) )
802 wp_die( __( 'What are you doing here? You do not have the permission to edit this user.', 'bbpress' ) );
803
804 // Do action based on who's profile you're editing
805 $edit_action = bbp_is_user_home() ? 'personal_options_update' : 'edit_user_profile_update';
806 do_action( $edit_action, $user_id );
807
808 // Multisite handles the trouble for us ;)
809 if ( !is_multisite() ) {
810 $edit_user = edit_user( $user_id );
811
812 // Single site means we need to do some manual labor
813 } else {
814 $user = get_userdata( $user_id );
815
816 // Update the email address in signups, if present.
817 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 ) ) ) {
818 $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->signups} SET user_email = %s WHERE user_login = %s", $_POST['email'], $user_login ) );
819 }
820
821 // WPMU must delete the user from the current blog if WP added him after editing.
822 $delete_role = false;
823 $blog_prefix = $wpdb->get_blog_prefix();
824
825 if ( $user_id != $user_id ) {
826 $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:{}'" );
827 if ( !is_network_admin() && null == $cap && $_POST['role'] == '' ) {
828 $_POST['role'] = 'contributor';
829 $delete_role = true;
830 }
831 }
832
833 $edit_user = edit_user( $user_id );
834
835 // stops users being added to current blog when they are edited
836 if ( $delete_role ) {
837 delete_user_meta( $user_id, $blog_prefix . 'capabilities' );
838 }
839
840 if ( is_multisite() && is_network_admin() & !bbp_is_user_home() && current_user_can( 'manage_network_options' ) && !isset( $super_admins ) && empty( $_POST['super_admin'] ) == is_super_admin( $user_id ) ) {
841 empty( $_POST['super_admin'] ) ? revoke_super_admin( $user_id ) : grant_super_admin( $user_id );
842 }
843 }
844
845 // Error(s) editng the user, so copy them into the global
846 if ( is_wp_error( $edit_user ) ) {
847 global $bbp;
848 $bbp->errors = $edit_user;
849
850 // Successful edit to redirect
851 } elseif ( is_integer( $edit_user ) ) {
852 $redirect = add_query_arg( array( 'updated' => 'true' ), bbp_get_user_profile_edit_url( $edit_user ) );
853
854 wp_redirect( $redirect );
855 exit;
856 }
857 }
858
859 /** END - Edit User ***********************************************************/
860
861 /**
862 * Get the topics that a user created
863 *
864 * @since bbPress (r2660)
865 *
866 * @param int $user_id Optional. User id
867 * @uses bbp_get_user_id() To get the topic id
868 * @uses bbp_has_topics() To get the topics created by the user
869 * @return array|bool Results if the user has created topics, otherwise false
870 */
871 function bbp_get_user_topics_started( $user_id = 0 ) {
872 if ( !$user_id = bbp_get_user_id( $user_id ) )
873 return false;
874
875 // Query defaults
876 $default_query = array(
877 'author' => $user_id,
878 'show_stickies' => false,
879 'order' => 'DESC',
880 );
881
882 // Get the topics
883 if ( $query = bbp_has_topics( $default_query ) )
884 return $query;
885
886 return false;
887 }
888
889 /**
890 * Get the total number of users on the forums
891 *
892 * @since bbPress (r2769)
893 *
894 * @uses wp_cache_get() Check if query is in cache
895 * @uses wpdb::get_var() To execute our query and get the var back
896 * @uses wp_cache_set() Set the query in the cache
897 * @uses apply_filters() Calls 'bbp_get_total_users' with number of users
898 * @return int Total number of users
899 */
900 function bbp_get_total_users() {
901 global $wpdb;
902
903 if ( $bbp_total_users = wp_cache_get( 'bbp_total_users', 'bbpress' ) )
904 return $bbp_total_users;
905
906 $bbp_total_users = $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->users} USE INDEX (PRIMARY);" );
907
908 wp_cache_set( 'bbp_total_users', $bbp_total_users, 'bbpress' );
909
910 return apply_filters( 'bbp_get_total_users', (int) $bbp_total_users );
911 }
912
913 /** User Status ***************************************************************/
914
915 /**
916 * Checks if the user has been marked as a spammer.
917 *
918 * @since bbPress (r3355)
919 *
920 * @param int $user_id int The ID for the user.
921 * @return bool True if spammer, False if not.
922 */
923 function bbp_is_user_spammer( $user_id = 0 ) {
924
925 // Default to current user
926 if ( empty( $user_id ) && is_user_logged_in() )
927 $user_id = bbp_get_current_user_id();
928
929 // No user to check
930 if ( empty( $user_id ) )
931 return false;
932
933 // Assume user is not spam
934 $is_spammer = false;
935
936 // Get user data
937 $user = get_userdata( $user_id );
938
939 // No user found
940 if ( empty( $user ) ) {
941 $is_spammer = false;
942
943 // User found
944 } else {
945
946 // Check if spam
947 if ( !empty( $user->spam ) )
948 $is_spammer = true;
949
950 if ( 'spam' == $user->user_status )
951 $is_spammer = true;
952 }
953
954 return apply_filters( 'bp_core_is_user_spammer', (bool) $is_spammer );
955 }
956
957 /**
958 * Mark a users topics and replies as spam when the user is marked as spam
959 *
960 * @since bbPress (r3405)
961 *
962 * @global WPDB $wpdb
963 * @param int $user_id Optional. User ID to spam. Defaults to displayed user.
964
965 * @uses bbp_is_single_user()
966 * @uses bbp_is_user_home()
967 * @uses bbp_get_displayed_user_field()
968 * @uses is_super_admin()
969 * @uses get_blogs_of_user()
970 * @uses get_current_blog_id()
971 * @uses bbp_get_topic_post_type()
972 * @uses bbp_get_reply_post_type()
973 * @uses switch_to_blog()
974 * @uses get_post_type()
975 * @uses bbp_spam_topic()
976 * @uses bbp_spam_reply()
977 * @uses restore_current_blog()
978 *
979 * @return If no user ID passed
980 */
981 function bbp_make_spam_user( $user_id = 0 ) {
982
983 // Use displayed user if it's not yourself
984 if ( empty( $user_id ) && bbp_is_single_user() && !bbp_is_user_home() )
985 $user_id = bbp_get_displayed_user_id();
986
987 // Bail if no user ID
988 if ( empty( $user_id ) )
989 return;
990
991 // Bail if user ID is super admin
992 if ( is_super_admin( $user_id ) )
993 return;
994
995 // Arm the torpedos
996 global $wpdb;
997
998 // Get the blog IDs of the user to mark as spam
999 $blogs = get_blogs_of_user( $user_id, true );
1000 $blog_id = get_current_blog_id();
1001
1002 // If user has no blogs, they are a guest on this site
1003 if ( empty( $blogs ) )
1004 $blogs[$blog_id] = array();
1005
1006 // Make array of post types to mark as spam
1007 $post_types = array( bbp_get_topic_post_type(), bbp_get_reply_post_type() );
1008 $post_types = "'" . implode( "', '", $post_types ) . "'";
1009
1010 // Loop through blogs and remove their posts
1011 foreach ( (array) $blogs as $blog_id => $details ) {
1012
1013 // Switch to the blog ID
1014 switch_to_blog( $blog_id );
1015
1016 // Get topics and replies
1017 $posts = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_author = {$user_id} AND post_status = 'publish' AND post_type IN ({$post_types})" );
1018
1019 // Loop through posts and spam them
1020 if ( !empty( $posts ) ) {
1021 foreach ( $posts as $post_id ) {
1022
1023 // The routines for topics ang replies are different, so use the
1024 // correct one based on the post type
1025 switch ( get_post_type( $post_id ) ) {
1026
1027 case bbp_get_topic_post_type() :
1028 bbp_spam_topic( $post_id );
1029 break;
1030
1031 case bbp_get_reply_post_type() :
1032 bbp_spam_reply( $post_id );
1033 break;
1034 }
1035 }
1036 }
1037
1038 // Switch back to current blog
1039 restore_current_blog();
1040 }
1041 }
1042
1043 /**
1044 * Mark a users topics and replies as spam when the user is marked as spam
1045 *
1046 * @since bbPress (r3405)
1047 *
1048 * @global WPDB $wpdb
1049 * @param int $user_id Optional. User ID to unspam. Defaults to displayed user.
1050 *
1051 * @uses bbp_is_single_user()
1052 * @uses bbp_is_user_home()
1053 * @uses bbp_get_displayed_user_field()
1054 * @uses is_super_admin()
1055 * @uses get_blogs_of_user()
1056 * @uses bbp_get_topic_post_type()
1057 * @uses bbp_get_reply_post_type()
1058 * @uses switch_to_blog()
1059 * @uses get_post_type()
1060 * @uses bbp_unspam_topic()
1061 * @uses bbp_unspam_reply()
1062 * @uses restore_current_blog()
1063 *
1064 * @return If no user ID passed
1065 */
1066 function bbp_make_ham_user( $user_id = 0 ) {
1067
1068 // Use displayed user if it's not yourself
1069 if ( empty( $user_id ) && bbp_is_single_user() && !bbp_is_user_home() )
1070 $user_id = bbp_get_displayed_user_field();
1071
1072 // Bail if no user ID
1073 if ( empty( $user_id ) )
1074 return;
1075
1076 // Bail if user ID is super admin
1077 if ( is_super_admin( $user_id ) )
1078 return;
1079
1080 // Arm the torpedos
1081 global $wpdb, $bbp;
1082
1083 // Get the blog IDs of the user to mark as spam
1084 $blogs = get_blogs_of_user( $user_id, true );
1085
1086 // If user has no blogs, they are a guest on this site
1087 if ( empty( $blogs ) )
1088 $blogs[$wpdb->blogid] = array();
1089
1090 // Make array of post types to mark as spam
1091 $post_types = array( bbp_get_topic_post_type(), bbp_get_reply_post_type() );
1092 $post_types = "'" . implode( "', '", $post_types ) . "'";
1093
1094 // Loop through blogs and remove their posts
1095 foreach ( (array) $blogs as $blog_id => $details ) {
1096
1097 // Switch to the blog ID
1098 switch_to_blog( $blog_id );
1099
1100 // Get topics and replies
1101 $posts = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_author = {$user_id} AND post_status = '{$bbp->spam_status_id}' AND post_type IN ({$post_types})" );
1102
1103 // Loop through posts and spam them
1104 if ( !empty( $posts ) ) {
1105 foreach ( $posts as $post_id ) {
1106
1107 // The routines for topics ang replies are different, so use the
1108 // correct one based on the post type
1109 switch ( get_post_type( $post_id ) ) {
1110
1111 case bbp_get_topic_post_type() :
1112 bbp_unspam_topic( $post_id );
1113 break;
1114
1115 case bbp_get_reply_post_type() :
1116 bbp_unspam_reply( $post_id );
1117 break;
1118 }
1119 }
1120 }
1121
1122 // Switch back to current blog
1123 restore_current_blog();
1124 }
1125 }
1126
1127 /**
1128 * Checks if the user has been marked as deleted.
1129 *
1130 * @since bbPress (r3355)
1131 *
1132 * @param int $user_id int The ID for the user.
1133 * @return bool True if deleted, False if not.
1134 */
1135 function bbp_is_user_deleted( $user_id = 0 ) {
1136
1137 // Default to current user
1138 if ( empty( $user_id ) && is_user_logged_in() )
1139 $user_id = bbp_get_current_user_id();
1140
1141 // No user to check
1142 if ( empty( $user_id ) )
1143 return false;
1144
1145 // Assume user is not deleted
1146 $is_deleted = false;
1147
1148 // Get user data
1149 $user = get_userdata( $user_id );
1150
1151 // No user found
1152 if ( empty( $user ) ) {
1153 $is_deleted = true;
1154
1155 // User found
1156 } else {
1157
1158 // Check if deleted
1159 if ( !empty( $user->deleted ) )
1160 $is_deleted = true;
1161
1162 if ( 'deleted' == $user->user_status )
1163 $is_deleted = true;
1164
1165 }
1166
1167 return apply_filters( 'bp_core_is_user_deleted', (bool) $is_deleted );
1168 }
1169
1170 ?>
1171