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

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