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

845 lines 29.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 // Only proceed if GET is a favorite action
356 if ( 'GET' == $_SERVER['REQUEST_METHOD'] && !empty( $_GET['action'] ) && in_array( $_GET['action'], array( 'bbp_favorite_add', 'bbp_favorite_remove' ) ) && !empty( $_GET['topic_id'] ) ) {
357
358 global $bbp;
359
360 // What action is taking place?
361 $action = $_GET['action'];
362
363 // Get user_id
364 $user_id = bbp_get_user_id( 0, true, true );
365
366 // Check current user's ability to edit the user
367 if ( !current_user_can( 'edit_user', $user_id ) )
368 $bbp->errors->add( 'bbp_favorite_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'bbpress' ) );
369
370 // Load favorite info
371 if ( !$topic_id = intval( $_GET['topic_id'] ) )
372 $bbp->errors->add( 'bbp_favorite_topic_id', __( '<strong>ERROR</strong>: No topic was found! Which topic are you marking/unmarking as favorite?', 'bbpress' ) );
373
374 $is_favorite = bbp_is_user_favorite( $user_id, $topic_id );
375 $success = false;
376
377 // Handle insertion into posts table
378 if ( !empty( $topic_id ) && !empty( $user_id ) && ( !is_wp_error( $bbp->errors ) || !$bbp->errors->get_error_codes() ) ) {
379
380 if ( $is_favorite && 'bbp_favorite_remove' == $action )
381 $success = bbp_remove_user_favorite( $user_id, $topic_id );
382 elseif ( !$is_favorite && 'bbp_favorite_add' == $action )
383 $success = bbp_add_user_favorite( $user_id, $topic_id );
384
385 // Do additional favorites actions
386 do_action( 'bbp_favorites_handler', $success, $user_id, $topic_id, $action );
387
388 // Check for missing reply_id or error
389 if ( true == $success ) {
390
391 // Redirect back to new reply
392 $redirect = bbp_is_favorites( false ) ? bbp_get_favorites_permalink( $user_id ) : bbp_get_topic_permalink( $topic_id );
393 wp_redirect( $redirect );
394
395 // For good measure
396 exit();
397
398 // Handle errors
399 } else {
400 if ( $is_favorite && 'bbp_favorite_remove' == $action )
401 $bbp->errors->add( 'bbp_favorite_remove', __( '<strong>ERROR</strong>: There was a problem removing that topic from favorites!', 'bbpress' ) );
402 elseif ( !$is_favorite && 'bbp_favorite_add' == $action )
403 $bbp->errors->add( 'bbp_favorite_add', __( '<strong>ERROR</strong>: There was a problem favoriting that topic!', 'bbpress' ) );
404 }
405 }
406 }
407 }
408
409 /** Subscriptions *************************************************************/
410
411 /**
412 * Get the users who have subscribed to the topic
413 *
414 * @since bbPress (r2668)
415 *
416 * @param int $topic_id Optional. Topic id
417 * @uses wpdb::get_col() To execute our query and get the column back
418 * @uses apply_filters() Calls 'bbp_get_topic_subscribers' with the subscribers
419 * @return array|bool Results if the topic has any subscribers, otherwise false
420 */
421 function bbp_get_topic_subscribers( $topic_id = 0 ) {
422 if ( empty( $topic_id ) )
423 return;
424
425 global $wpdb;
426
427 // Get the users who have favorited the topic
428 $users = $wpdb->get_col( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '_bbp_subscriptions' and FIND_IN_SET('{$topic_id}', meta_value) > 0" );
429 $users = apply_filters( 'bbp_get_topic_subscribers', $users );
430
431 if ( !empty( $users ) )
432 return $users;
433
434 return false;
435 }
436
437 /**
438 * Get a user's subscribed topics
439 *
440 * @since bbPress (r2668)
441 *
442 * @param int $user_id Optional. User id
443 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
444 * @uses bbp_has_topics() To get the topics
445 * @uses apply_filters() Calls 'bbp_get_user_subscriptions' with the topic query
446 * and user id
447 * @return array|bool Results if user has subscriptions, otherwise false
448 */
449 function bbp_get_user_subscriptions( $user_id = 0 ) {
450
451 // Default to the displayed user
452 if ( !$user_id = bbp_get_user_id( $user_id ) )
453 return false;
454
455 // If user has subscriptions, load them
456 if ( $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id ) ) {
457 $query = bbp_has_topics( array( 'post__in' => $subscriptions ) );
458 return apply_filters( 'bbp_get_user_subscriptions', $query, $user_id );
459 }
460
461 return false;
462 }
463
464 /**
465 * Get a user's subscribed topics' ids
466 *
467 * @since bbPress (r2668)
468 *
469 * @param int $user_id Optional. User id
470 * @uses bbp_get_user_id() To get the user id
471 * @uses get_user_meta() To get the user's subscriptions
472 * @uses apply_filters() Calls 'bbp_get_user_subscribed_topic_ids' with
473 * the subscriptions and user id
474 * @return array|bool Results if user has subscriptions, otherwise false
475 */
476 function bbp_get_user_subscribed_topic_ids( $user_id = 0 ) {
477 if ( !$user_id = bbp_get_user_id( $user_id ) )
478 return false;
479
480 $subscriptions = (string) get_user_meta( $user_id, '_bbp_subscriptions', true );
481 $subscriptions = (array) explode( ',', $subscriptions );
482 $subscriptions = array_filter( $subscriptions );
483
484 return apply_filters( 'bbp_get_user_subscribed_topic_ids', $subscriptions, $user_id );
485 }
486
487 /**
488 * Check if a topic is in user's subscription list or not
489 *
490 * @since bbPress (r2668)
491 *
492 * @param int $user_id Optional. User id
493 * @param int $topic_id Optional. Topic id
494 * @uses bbp_get_user_id() To get the user id
495 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
496 * @uses bbp_get_topic() To get the topic
497 * @uses bbp_get_topic_id() To get the topic id
498 * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id,
499 * topic id and subsriptions
500 * @return bool True if the topic is in user's subscriptions, otherwise false
501 */
502 function bbp_is_user_subscribed( $user_id = 0, $topic_id = 0 ) {
503 global $post;
504
505 if ( !$user_id = bbp_get_user_id( $user_id, true, true ) )
506 return false;
507
508 $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id );
509
510 if ( !empty( $topic_id ) ) {
511 $topic = bbp_get_topic( $topic_id );
512 $topic_id = !empty( $topic ) ? $topic->ID : 0;
513 } elseif ( !$topic_id = bbp_get_topic_id() ) {
514 if ( empty( $post ) )
515 return false;
516
517 $topic_id = $post->ID;
518 }
519
520 if ( empty( $subscriptions ) || empty( $topic_id ) )
521 return false;
522
523 return apply_filters( 'bbp_is_user_subscribed', (bool) in_array( $topic_id, $subscriptions ), $user_id, $topic_id, $subscriptions );
524 }
525
526 /**
527 * Add a topic to user's subscriptions
528 *
529 * @since bbPress (r2668)
530 *
531 * @param int $user_id Optional. User id
532 * @param int $topic_id Optional. Topic id
533 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
534 * @uses bbp_get_topic() To get the topic
535 * @uses update_user_meta() To update the user's subscriptions
536 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id
537 * @return bool Always true
538 */
539 function bbp_add_user_subscription( $user_id = 0, $topic_id = 0 ) {
540 if ( empty( $user_id ) || empty( $topic_id ) )
541 return false;
542
543 $subscriptions = (array) bbp_get_user_subscribed_topic_ids( $user_id );
544
545 if ( !$topic = bbp_get_topic( $topic_id ) )
546 return false;
547
548 if ( !in_array( $topic_id, $subscriptions ) ) {
549 $subscriptions[] = $topic_id;
550 $subscriptions = array_filter( $subscriptions );
551 $subscriptions = (string) implode( ',', $subscriptions );
552 update_user_meta( $user_id, '_bbp_subscriptions', $subscriptions );
553 }
554
555 do_action( 'bbp_add_user_subscription', $user_id, $topic_id );
556
557 return true;
558 }
559
560 /**
561 * Remove a topic from user's subscriptions
562 *
563 * @since bbPress (r2668)
564 *
565 * @param int $user_id Optional. User id
566 * @param int $topic_id Optional. Topic id
567 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
568 * @uses update_user_meta() To update the user's subscriptions
569 * @uses delete_user_meta() To delete the user's subscriptions meta
570 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and
571 * topic id
572 * @return bool True if the topic was removed from user's subscriptions,
573 * otherwise false
574 */
575 function bbp_remove_user_subscription( $user_id, $topic_id ) {
576 if ( empty( $user_id ) || empty( $topic_id ) )
577 return false;
578
579 $subscriptions = (array) bbp_get_user_subscribed_topic_ids( $user_id );
580
581 if ( empty( $subscriptions ) )
582 return false;
583
584 if ( is_numeric( $pos = array_search( $topic_id, $subscriptions ) ) ) {
585 array_splice( $subscriptions, $pos, 1 );
586 $subscriptions = array_filter( $subscriptions );
587
588 if ( !empty( $subscriptions ) ) {
589 $subscriptions = implode( ',', $subscriptions );
590 update_user_meta( $user_id, '_bbp_subscriptions', $subscriptions );
591 } else {
592 delete_user_meta( $user_id, '_bbp_subscriptions' );
593 }
594 }
595
596 do_action( 'bbp_remove_user_subscription', $user_id, $topic_id );
597
598 return true;
599 }
600
601 /**
602 * Handles the front end subscribing and unsubscribing topics
603 *
604 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
605 * @uses bbp_get_user_id() To get the user id
606 * @uses current_user_can() To check if the current user can edit the user
607 * @uses bbPress:errors:add() To log the error messages
608 * @uses bbp_is_user_subscribed() To check if the topic is in user's
609 * subscriptions
610 * @uses bbp_remove_user_subscription() To remove the user subscription
611 * @uses bbp_add_user_subscription() To add the user subscription
612 * @uses do_action() Calls 'bbp_subscriptions_handler' with success, user id,
613 * topic id and action
614 * @uses bbp_is_subscription() To check if it's the subscription page
615 * @uses bbp_get_subscription_link() To get the subscription page link
616 * @uses bbp_get_topic_permalink() To get the topic permalink
617 * @uses wp_redirect() To redirect to the url
618 */
619 function bbp_subscriptions_handler() {
620
621 if ( !bbp_is_subscriptions_active() )
622 return false;
623
624 // Only proceed if GET is a favorite action
625 if ( 'GET' == $_SERVER['REQUEST_METHOD'] && !empty( $_GET['action'] ) && in_array( $_GET['action'], array( 'bbp_subscribe', 'bbp_unsubscribe' ) ) && !empty( $_GET['topic_id'] ) ) {
626
627 global $bbp;
628
629 // What action is taking place?
630 $action = $_GET['action'];
631
632 // Get user_id
633 $user_id = bbp_get_user_id( 0, true, true );
634
635 // Check current user's ability to edit the user
636 if ( !current_user_can( 'edit_user', $user_id ) )
637 $bbp->errors->add( 'bbp_subscription_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'bbpress' ) );
638
639 // Load subscription info
640 if ( !$topic_id = intval( $_GET['topic_id'] ) )
641 $bbp->errors->add( 'bbp_subscription_topic_id', __( '<strong>ERROR</strong>: No topic was found! Which topic are you subscribing/unsubscribing to?', 'bbpress' ) );
642
643 if ( !is_wp_error( $bbp->errors ) || !$bbp->errors->get_error_codes() ) {
644
645 $is_subscription = bbp_is_user_subscribed( $user_id, $topic_id );
646 $success = false;
647
648 if ( $is_subscription && 'bbp_unsubscribe' == $action )
649 $success = bbp_remove_user_subscription( $user_id, $topic_id );
650 elseif ( !$is_subscription && 'bbp_subscribe' == $action )
651 $success = bbp_add_user_subscription( $user_id, $topic_id );
652
653 // Do additional subscriptions actions
654 do_action( 'bbp_subscriptions_handler', $success, $user_id, $topic_id, $action );
655
656 // Check for missing reply_id or error
657 if ( true == $success ) {
658
659 // Redirect back to new reply
660 $redirect = bbp_is_subscriptions( false ) ? bbp_get_subscriptions_permalink( $user_id ) : bbp_get_topic_permalink( $topic_id );
661 wp_redirect( $redirect );
662
663 // For good measure
664 exit();
665
666 // Handle errors
667 } else {
668 if ( $is_subscription && 'bbp_unsubscribe' == $action )
669 $bbp->errors->add( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing from that topic!', 'bbpress' ) );
670 elseif ( !$is_subscription && 'bbp_subscribe' == $action )
671 $bbp->errors->add( 'bbp_subscribe', __( '<strong>ERROR</strong>: There was a problem subscribing to that topic!', 'bbpress' ) );
672 }
673 }
674 }
675 }
676
677 /** Edit **********************************************************************/
678
679 /**
680 * Handles the front end user editing
681 *
682 * @uses is_multisite() To check if it's a multisite
683 * @uses bbp_is_user_home() To check if the user is at home (the display page
684 * is the one of the logged in user)
685 * @uses get_option() To get the displayed user's new email id option
686 * @uses wpdb::prepare() To sanitize our sql query
687 * @uses wpdb::get_var() To execute our query and get back the variable
688 * @uses wpdb::query() To execute our query
689 * @uses wp_update_user() To update the user
690 * @uses delete_option() To delete the displayed user's email id option
691 * @uses bbp_get_user_profile_edit_url() To get the edit profile url
692 * @uses wp_redirect() To redirect to the url
693 * @uses check_admin_referer() To verify the nonce and check the referer
694 * @uses current_user_can() To check if the current user can edit the user
695 * @uses do_action() Calls 'personal_options_update' or
696 * 'edit_user_options_update' (based on if it's the user home)
697 * with the displayed user id
698 * @uses edit_user() To edit the user based on the post data
699 * @uses get_userdata() To get the user data
700 * @uses is_email() To check if the string is an email id or not
701 * @uses wpdb::get_blog_prefix() To get the blog prefix
702 * @uses is_network_admin() To check if the user is the network admin
703 * @uses is_super_admin() To check if the user is super admin
704 * @uses revoke_super_admin() To revoke super admin priviledges
705 * @uses grant_super_admin() To grant super admin priviledges
706 * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
707 */
708 function bbp_edit_user_handler() {
709
710 if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && 'bbp-update-user' == $_POST['action'] ) {
711
712 global $bbp, $wpdb;
713
714 // Execute confirmed email change. See send_confirmation_on_profile_email().
715 if ( is_multisite() && bbp_is_user_home() && isset( $_GET['newuseremail'] ) && $bbp->displayed_user->ID ) {
716
717 $new_email = get_option( $bbp->displayed_user->ID . '_new_email' );
718
719 if ( $new_email['hash'] == $_GET['newuseremail'] ) {
720 $user->ID = $bbp->displayed_user->ID;
721 $user->user_email = esc_html( trim( $new_email['newemail'] ) );
722
723 if ( $wpdb->get_var( $wpdb->prepare( "SELECT user_login FROM {$wpdb->signups} WHERE user_login = %s", $bbp->displayed_user->user_login ) ) )
724 $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->signups} SET user_email = %s WHERE user_login = %s", $user->user_email, $bbp->displayed_user->user_login ) );
725
726 wp_update_user( get_object_vars( $user ) );
727 delete_option( $bbp->displayed_user->ID . '_new_email' );
728
729 wp_redirect( add_query_arg( array( 'updated' => 'true' ), bbp_get_user_profile_edit_url( $bbp->displayed_user->ID ) ) );
730 exit;
731 }
732
733 } elseif ( is_multisite() && bbp_is_user_home() && !empty( $_GET['dismiss'] ) && $bbp->displayed_user->ID . '_new_email' == $_GET['dismiss'] ) {
734
735 delete_option( $bbp->displayed_user->ID . '_new_email' );
736 wp_redirect( add_query_arg( array( 'updated' => 'true' ), bbp_get_user_profile_edit_url( $bbp->displayed_user->ID ) ) );
737 exit;
738
739 }
740
741 check_admin_referer( 'update-user_' . $bbp->displayed_user->ID );
742
743 if ( !current_user_can( 'edit_user', $bbp->displayed_user->ID ) )
744 wp_die( __( 'What are you doing here? You do not have the permission to edit this user.', 'bbpress' ) );
745
746 if ( bbp_is_user_home() )
747 do_action( 'personal_options_update', $bbp->displayed_user->ID );
748 else
749 do_action( 'edit_user_profile_update', $bbp->displayed_user->ID );
750
751 if ( !is_multisite() ) {
752 $bbp->errors = edit_user( $bbp->displayed_user->ID ); // Handles the trouble for us ;)
753 } else {
754 $user = get_userdata( $bbp->displayed_user->ID );
755
756 // Update the email address in signups, if present.
757 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 ) ) )
758 $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->signups} SET user_email = %s WHERE user_login = %s", $_POST['email'], $user_login ) );
759
760 // WPMU must delete the user from the current blog if WP added him after editing.
761 $delete_role = false;
762 $blog_prefix = $wpdb->get_blog_prefix();
763
764 if ( $bbp->displayed_user->ID != $bbp->displayed_user->ID ) {
765 $cap = $wpdb->get_var( "SELECT meta_value FROM {$wpdb->usermeta} WHERE user_id = '{$bbp->displayed_user->ID}' AND meta_key = '{$blog_prefix}capabilities' AND meta_value = 'a:0:{}'" );
766 if ( !is_network_admin() && null == $cap && $_POST['role'] == '' ) {
767 $_POST['role'] = 'contributor';
768 $delete_role = true;
769 }
770 }
771
772 $bbp->errors = edit_user( $bbp->displayed_user->ID );
773
774 if ( $delete_role ) // stops users being added to current blog when they are edited
775 delete_user_meta( $bbp->displayed_user->ID, $blog_prefix . 'capabilities' );
776
777 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( $bbp->displayed_user->ID ) )
778 empty( $_POST['super_admin'] ) ? revoke_super_admin( $bbp->displayed_user->ID ) : grant_super_admin( $bbp->displayed_user->ID );
779 }
780
781 if ( !is_wp_error( $bbp->errors ) ) {
782 $redirect = add_query_arg( array( 'updated' => 'true' ), bbp_get_user_profile_edit_url( $bbp->displayed_user->ID ) );
783
784 wp_redirect( $redirect );
785 exit;
786 }
787 }
788 }
789
790 /** END - Edit User ***********************************************************/
791
792 /**
793 * Get the topics that a user created
794 *
795 * @since bbPress (r2660)
796 *
797 * @param int $user_id Optional. User id
798 * @uses bbp_get_user_id() To get the topic id
799 * @uses bbp_has_topics() To get the topics created by the user
800 * @return array|bool Results if the user has created topics, otherwise false
801 */
802 function bbp_get_user_topics_started( $user_id = 0 ) {
803 if ( !$user_id = bbp_get_user_id( $user_id ) )
804 return false;
805
806 // Query defaults
807 $default_query = array(
808 'author' => $user_id,
809 'show_stickies' => false,
810 'order' => 'DESC',
811 );
812
813 // Get the topics
814 if ( $query = bbp_has_topics( $default_query ) )
815 return $query;
816
817 return false;
818 }
819
820 /**
821 * Get the total number of users on the forums
822 *
823 * @since bbPress (r2769)
824 *
825 * @uses wp_cache_get() Check if query is in cache
826 * @uses wpdb::get_var() To execute our query and get the var back
827 * @uses wp_cache_set() Set the query in the cache
828 * @uses apply_filters() Calls 'bbp_get_total_users' with number of users
829 * @return int Total number of users
830 */
831 function bbp_get_total_users() {
832 global $wpdb;
833
834 if ( $bbp_total_users = wp_cache_get( 'bbp_total_users', 'bbpress' ) )
835 return $bbp_total_users;
836
837 $bbp_total_users = $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->users} USE INDEX (PRIMARY);" );
838
839 wp_cache_set( 'bbp_total_users', $bbp_total_users, 'bbpress' );
840
841 return apply_filters( 'bbp_get_total_users', (int) $bbp_total_users );
842 }
843
844 ?>
845