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

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