PluginProbe
bbPress / 2.6.17
bbPress v2.6.17
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 / includes / users / functions.php

functions.php in bbPress 2.6.17, at includes/users/functions.php

1,289 lines 36.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress User Functions
5 *
6 * @package bbPress
7 * @subpackage Functions
8 */
9
10 // Exit if accessed directly
11 defined( 'ABSPATH' ) || exit;
12
13 /**
14 * Redirect back to $url when attempting to use the login page
15 *
16 * @since 2.0.0 bbPress (r2815)
17 *
18 * @param string $url The url
19 * @param string $raw_url Raw url
20 * @param object $user User object
21 */
22 function bbp_redirect_login( $url = '', $raw_url = '', $user = '' ) {
23
24 // Raw redirect_to was passed, so use it
25 if ( ! empty( $raw_url ) ) {
26 $url = $raw_url;
27
28 // $url was manually set in wp-login.php to redirect to admin
29 } elseif ( admin_url() === $url ) {
30 $url = home_url();
31
32 // $url is empty
33 } elseif ( empty( $url ) ) {
34 $url = home_url();
35 }
36
37 // Filter & return
38 return apply_filters( 'bbp_redirect_login', $url, $raw_url, $user );
39 }
40
41 /**
42 * Is an anonymous topic/reply being made?
43 *
44 * @since 2.0.0 bbPress (r2688)
45 *
46 * @return bool True if anonymous is allowed and user is not logged in, false if
47 * anonymous is not allowed or user is logged in
48 */
49 function bbp_is_anonymous() {
50 $is_anonymous = ( ! is_user_logged_in() && bbp_allow_anonymous() );
51
52 // Filter & return
53 return (bool) apply_filters( 'bbp_is_anonymous', $is_anonymous );
54 }
55
56 /**
57 * Echoes the values for current poster (uses WP comment cookies)
58 *
59 * @since 2.0.0 bbPress (r2734)
60 *
61 * @param string $key Which value to echo?
62 */
63 function bbp_current_anonymous_user_data( $key = '' ) {
64 echo esc_attr( bbp_get_current_anonymous_user_data( $key ) );
65 }
66
67 /**
68 * Get the cookies for current poster (uses WP comment cookies).
69 *
70 * @since 2.0.0 bbPress (r2734)
71 *
72 * @param string $key Optional. Which value to get? If not given, then
73 * an array is returned.
74 * @return string|array Cookie(s) for current poster
75 */
76 function bbp_get_current_anonymous_user_data( $key = '' ) {
77
78 // Array of allowed cookie names
79 $cookie_names = array(
80 'name' => 'comment_author',
81 'email' => 'comment_author_email',
82 'url' => 'comment_author_url',
83
84 // Here just for the sake of them, use the above ones
85 'comment_author' => 'comment_author',
86 'comment_author_email' => 'comment_author_email',
87 'comment_author_url' => 'comment_author_url',
88 );
89
90 // Get the current poster's info from the cookies
91 $bbp_current_poster = wp_get_current_commenter();
92
93 // Sanitize the cookie key being retrieved
94 $key = sanitize_key( $key );
95
96 // Maybe return a specific key
97 if ( ! empty( $key ) && in_array( $key, array_keys( $cookie_names ), true ) ) {
98 return $bbp_current_poster[ $cookie_names[ $key ] ];
99 }
100
101 // Return all keys
102 return $bbp_current_poster;
103 }
104
105 /**
106 * Set the cookies for current poster (uses WP comment cookies)
107 *
108 * @since 2.0.0 bbPress (r2734)
109 *
110 * @param array $anonymous_data Optional - if it's an anonymous post. Do not
111 * supply if supplying $author_id. Should be
112 * sanitized (see {@link bbp_filter_anonymous_post_data()}
113 */
114 function bbp_set_current_anonymous_user_data( $anonymous_data = array() ) {
115
116 // Bail if empty or not an array
117 if ( empty( $anonymous_data ) || ! is_array( $anonymous_data ) ) {
118 return;
119 }
120
121 // Setup cookie expiration
122 $lifetime = (int) apply_filters( 'comment_cookie_lifetime', 30000000 );
123 $expiry = time() + $lifetime;
124 $secure = ( 'https' === parse_url( home_url(), PHP_URL_SCHEME ) );
125
126 // Set the cookies
127 setcookie( 'comment_author_' . COOKIEHASH, $anonymous_data['bbp_anonymous_name'], $expiry, COOKIEPATH, COOKIE_DOMAIN, $secure );
128 setcookie( 'comment_author_email_' . COOKIEHASH, $anonymous_data['bbp_anonymous_email'], $expiry, COOKIEPATH, COOKIE_DOMAIN, $secure );
129 setcookie( 'comment_author_url_' . COOKIEHASH, $anonymous_data['bbp_anonymous_website'], $expiry, COOKIEPATH, COOKIE_DOMAIN, $secure );
130 }
131
132 /**
133 * Get the poster IP address
134 *
135 * @since 2.0.0 bbPress (r3120)
136 * @since 2.6.0 bbPress (r5609) Added `empty()` check for unit tests
137 *
138 * @return string
139 */
140 function bbp_current_author_ip() {
141
142 // Check for remote address
143 $remote_address = ! empty( $_SERVER['REMOTE_ADDR'] )
144 ? wp_unslash( $_SERVER['REMOTE_ADDR'] )
145 : '127.0.0.1';
146
147 // Remove any unsavory bits
148 $retval = preg_replace( '/[^0-9a-fA-F:., ]/', '', $remote_address );
149
150 // Filter & return
151 return apply_filters( 'bbp_current_author_ip', $retval, $remote_address );
152 }
153
154 /**
155 * Get the poster user agent
156 *
157 * @since 2.0.0 bbPress (r3446)
158 *
159 * @return string
160 */
161 function bbp_current_author_ua() {
162 $retval = ! empty( $_SERVER['HTTP_USER_AGENT'] )
163 ? mb_substr( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ), 0, 254 )
164 : '';
165
166 // Filter & return
167 return apply_filters( 'bbp_current_author_ua', $retval );
168 }
169
170 /** Edit **********************************************************************/
171
172 /**
173 * Filter user profile data according to the current user's field permissions.
174 *
175 * @since 2.6.17
176 *
177 * @param array $data Submitted user profile data.
178 * @param int $user_id User being edited.
179 * @return array Filtered user profile data.
180 */
181 function bbp_filter_user_edit_post_data( $data = array(), $user_id = 0 ) {
182 $user_id = bbp_get_user_id( $user_id, false, false );
183 $user = get_userdata( $user_id );
184
185 // Remove general profile fields.
186 if ( ! bbp_current_user_can_edit_user_field( 'profile', $user_id ) ) {
187 unset(
188 $data['first_name'],
189 $data['last_name'],
190 $data['nickname'],
191 $data['display_name'],
192 $data['url'],
193 $data['description'],
194 $data['locale']
195 );
196
197 // Remove dynamic WordPress contact methods.
198 if ( ! empty( $user ) ) {
199 foreach ( array_keys( wp_get_user_contact_methods( $user ) ) as $contact_method ) {
200 unset( $data[ $contact_method ] );
201 }
202 }
203 }
204
205 // Preserve the existing email address.
206 if ( ! bbp_current_user_can_edit_user_field( 'email', $user_id ) ) {
207 $data['email'] = ! empty( $user->user_email )
208 ? $user->user_email
209 : '';
210 }
211
212 // Remove password fields.
213 if ( ! bbp_current_user_can_edit_user_field( 'password', $user_id ) ) {
214 unset( $data['pass1'], $data['pass2'] );
215 }
216
217 // Remove the WordPress site role.
218 if ( ! bbp_current_user_can_edit_user_field( 'site_role', $user_id ) ) {
219 unset( $data['role'] );
220 }
221
222 // Filter & return.
223 return (array) apply_filters( 'bbp_filter_user_edit_post_data', $data, $user_id, bbp_get_current_user_id() );
224 }
225
226 /**
227 * Return whether an email change requires confirmation from the edited user.
228 *
229 * Self-service changes require confirmation by default, while privileged edits
230 * to another user update the address directly.
231 *
232 * @since 2.6.17
233 *
234 * @param int $user_id User being edited.
235 * @return bool Whether confirmation is required.
236 */
237 function bbp_user_email_change_requires_confirmation( $user_id = 0 ) {
238 $user_id = bbp_get_user_id( $user_id, false, false );
239 $retval = ( bbp_get_current_user_id() === $user_id );
240
241 // Filter & return.
242 return (bool) apply_filters( 'bbp_user_email_change_requires_confirmation', $retval, $user_id, bbp_get_current_user_id() );
243 }
244
245 /**
246 * Handles the front end user editing from POST requests
247 *
248 * @since 2.0.0 bbPress (r2790)
249 *
250 * @param string $action The requested action to compare this function to
251 */
252 function bbp_edit_user_handler( $action = '' ) {
253
254 // Bail if action is not `bbp-update-user`
255 if ( 'bbp-update-user' !== $action ) {
256 return;
257 }
258
259 // Bail if in wp-admin
260 if ( is_admin() ) {
261 return;
262 }
263
264 // Get the displayed user ID
265 $user_id = bbp_get_displayed_user_id();
266
267 // Request check
268 if ( ! bbp_is_user_profile_form_post_request( $user_id ) ) {
269 bbp_add_error( 'bbp_update_user_nonce', __( '<strong>Error</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
270 return;
271 }
272
273 // Cap check
274 if ( ! current_user_can( 'edit_user', $user_id ) ) {
275 bbp_add_error( 'bbp_update_user_capability', __( '<strong>Error</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
276 return;
277 }
278
279 // Enforce field-level profile permissions before validating and saving.
280 $_POST = bbp_filter_user_edit_post_data( $_POST, $user_id );
281
282 // Empty email check
283 if ( empty( $_POST['email'] ) ) {
284 bbp_add_error( 'bbp_user_email_empty', __( '<strong>Error</strong>: That is not a valid email address.', 'bbpress' ), array( 'form-field' => 'email' ) );
285 return;
286 }
287
288 // Get the users current email address to use for comparisons
289 $user_email = bbp_get_displayed_user_field( 'user_email', 'raw' );
290
291 // Bail if no email change
292 if ( $user_email !== $_POST['email'] ) {
293
294 // Check that new email address is valid
295 if ( ! is_email( $_POST['email'] ) ) {
296 bbp_add_error( 'bbp_user_email_invalid', __( '<strong>Error</strong>: That is not a valid email address.', 'bbpress' ), array( 'form-field' => 'email' ) );
297 return;
298 }
299
300 // Check if email address is already in use
301 if ( email_exists( $_POST['email'] ) ) {
302 bbp_add_error( 'bbp_user_email_taken', __( '<strong>Error</strong>: That email address is already in use.', 'bbpress' ), array( 'form-field' => 'email' ) );
303 return;
304 }
305
306 if ( bbp_user_email_change_requires_confirmation( $user_id ) ) {
307 // Update the option.
308 $option = array(
309 'hash' => md5( $_POST['email'] . time() . wp_rand() ),
310 'newemail' => $_POST['email'],
311 );
312 update_user_meta( $user_id, '_new_email', $option );
313
314 // Attempt to notify the user of email address change.
315 bbp_edit_user_email_send_notification( $user_id, $option );
316
317 // Set the POST email variable back to the user's email address
318 // so `edit_user()` does not attempt to update it. This is not ideal,
319 // but it's also what send_confirmation_on_profile_email() does.
320 $_POST['email'] = $user_email;
321 }
322 }
323
324 // Do action based on who's profile you're editing
325 $edit_action = bbp_is_user_home_edit()
326 ? 'personal_options_update'
327 : 'edit_user_profile_update';
328
329 do_action( $edit_action, $user_id );
330
331 // Prevent edit_user() from wiping out the user's Toolbar on front setting
332 if ( ! isset( $_POST['admin_bar_front'] ) && _get_admin_bar_pref( 'front', $user_id ) ) {
333 $_POST['admin_bar_front'] = 1;
334 }
335
336 // Bail if errors already exist
337 if ( bbp_has_errors() ) {
338 return;
339 }
340
341 // Handle user edit
342 $edit_user = edit_user( $user_id );
343
344 // Error(s) editng the user, so copy them into the global
345 if ( is_wp_error( $edit_user ) ) {
346 bbpress()->errors = $edit_user;
347
348 // Successful edit to redirect
349 } elseif ( is_integer( $edit_user ) ) {
350
351 // Maybe update super admin ability
352 if ( is_multisite() && ! bbp_is_user_home_edit() && current_user_can( 'manage_network_options' ) && is_super_admin() ) {
353 empty( $_POST['super_admin'] )
354 ? revoke_super_admin( $edit_user )
355 : grant_super_admin( $edit_user );
356 }
357
358 // Redirect
359 $args = array( 'updated' => 'true' );
360 $user_url = bbp_get_user_profile_edit_url( $edit_user );
361 $redirect = add_query_arg( $args, $user_url );
362
363 bbp_redirect( $redirect );
364 }
365 }
366
367 /**
368 * Handles user email address updating from GET requests
369 *
370 * @since 2.6.0 bbPress (r5660)
371 *
372 * @param string $action
373 */
374 function bbp_user_email_change_handler( $action = '' ) {
375
376 // Bail if action is not `bbp-update-user-email`
377 if ( 'bbp-update-user-email' !== $action ) {
378 return;
379 }
380
381 // Bail if not on users own profile
382 if ( ! bbp_is_user_home_edit() ) {
383 return;
384 }
385
386 // Bail if not attempting to modify user email address
387 if ( empty( $_GET['newuseremail'] ) && empty( $_GET['dismiss'] ) ) {
388 return;
389 }
390
391 // Get the displayed user ID & option key
392 $user_id = bbp_get_displayed_user_id();
393 $key = '_new_email';
394 $redirect_to = bbp_get_user_profile_edit_url( $user_id );
395
396 // Execute confirmed email change.
397 if ( ! empty( $_GET['newuseremail'] ) ) {
398
399 // Check for email address change option
400 $new_email = get_user_meta( $user_id, $key, true );
401
402 // Redirect if *no* email address change exists
403 if ( false === $new_email ) {
404 bbp_redirect( $redirect_to );
405 }
406
407 // Cleanup & redirect if *invalid* email address change exists
408 if ( empty( $new_email['hash'] ) || empty( $new_email['newemail'] ) ) {
409 delete_user_meta( $user_id, $key );
410
411 bbp_redirect( $redirect_to );
412 }
413
414 // Compare hashes, and update user if hashes match
415 if ( hash_equals( $new_email['hash'], $_GET['newuseremail'] ) ) {
416
417 // Does another user have this email address already?
418 if ( email_exists( $new_email['newemail'] ) ) {
419 delete_user_meta( $user_id, $key );
420
421 bbp_add_error( 'bbp_user_email_taken', __( '<strong>Error</strong>: That email address is already in use.', 'bbpress' ), array( 'form-field' => 'email' ) );
422
423 // Email address is good to change to
424 } else {
425
426 // Create a stdClass (for easy call to wp_update_user())
427 $user = new stdClass();
428 $user->ID = $user_id;
429 $user->user_email = esc_html( trim( $new_email['newemail'] ) );
430
431 // Attempt to update user email
432 $update_user = wp_update_user( $user );
433
434 // Error(s) editing the user, so copy them into the global
435 if ( is_wp_error( $update_user ) ) {
436 bbpress()->errors = $update_user;
437
438 // All done, so redirect and show the updated message
439 } else {
440
441 // Update signups table, if signups table & entry exists
442 // For Multisite & BuddyPress compatibility
443 $bbp_db = bbp_db();
444 if ( ! empty( $bbp_db->signups ) && $bbp_db->get_var( $bbp_db->prepare( "SELECT user_login FROM {$bbp_db->signups} WHERE user_login = %s", bbp_get_displayed_user_field( 'user_login', 'raw' ) ) ) ) {
445 $bbp_db->query( $bbp_db->prepare( "UPDATE {$bbp_db->signups} SET user_email = %s WHERE user_login = %s", $user->user_email, bbp_get_displayed_user_field( 'user_login', 'raw' ) ) );
446 }
447
448 delete_user_meta( $user_id, $key );
449
450 bbp_redirect( add_query_arg( array( 'updated' => 'true' ), $redirect_to ) );
451 }
452 }
453 }
454
455 // Delete new email address from user options
456 } elseif ( ! empty( $_GET['dismiss'] ) && ( "{$user_id}{$key}" === $_GET['dismiss'] ) ) {
457 if ( ! bbp_verify_nonce_request( "dismiss-{$user_id}{$key}" ) ) {
458 bbp_add_error( 'bbp_dismiss_new_email_nonce', __( '<strong>Error</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
459 return;
460 }
461
462 delete_user_meta( $user_id, $key );
463 bbp_redirect( $redirect_to );
464 }
465 }
466
467 /**
468 * Sends an email when an email address change occurs on POST requests
469 *
470 * @since 2.6.0 bbPress (r5660)
471 *
472 * @see send_confirmation_on_profile_email()
473 */
474 function bbp_edit_user_email_send_notification( $user_id = 0, $args = array() ) {
475
476 // Parse args
477 $r = bbp_parse_args(
478 $args,
479 array(
480 'hash' => '',
481 'newemail' => '',
482 )
483 );
484
485 // Bail if any relevant parameters are empty
486 if ( empty( $user_id ) || empty( $r['hash'] ) || empty( $r['newemail'] ) ) {
487 bbp_add_error( 'bbp_user_email_invalid_hash', __( '<strong>Error</strong>: An error occurred while updating your email address.', 'bbpress' ), array( 'form-field' => 'email' ) );
488 return;
489 }
490
491 // Build the nonced URL to dismiss the pending change
492 $user_login = bbp_get_displayed_user_field( 'user_login', 'raw' );
493 $user_url = bbp_get_user_profile_edit_url( $user_id );
494 $confirm_url = add_query_arg(
495 array(
496 'action' => 'bbp-update-user-email',
497 'newuseremail' => $r['hash']
498 ),
499 $user_url
500 );
501
502 /* translators: 1: Username, 2: Confirmation URL, 3: New email address, 4: Site name, 5: Site URL */
503 $email_text = __(
504 '%1$s
505
506 Someone requested a change to the email address on your account.
507
508 Please click the following link to confirm this change:
509 %2$s
510
511 If you did not request this, you can safely ignore and delete this notification.
512
513 This email was sent to: %3$s
514
515 Regards,
516 The %4$s Team
517 %5$s',
518 'bbpress'
519 );
520
521 /**
522 * Filter the email text sent when a user changes emails.
523 *
524 * The following strings have a special meaning and will get replaced dynamically:
525 *
526 * %1$s - The current user's username
527 * %2$s - The link to click on to confirm the email change
528 * %3$s - The new email
529 * %4$s - The name of the site
530 * %5$s - The URL to the site
531 *
532 * @param string $email_text Text in the email.
533 * @param string $r New user email that the current user has changed to.
534 */
535 $content = apply_filters( 'bbp_user_email_update_content', $email_text, $r );
536
537 // Build the email message
538 $message = sprintf( $content, $user_login, $confirm_url, $r['newemail'], get_site_option( 'site_name' ), network_home_url() );
539
540 // Build the email subject
541 /* translators: %s: Site name */
542 $subject = sprintf( __( '[%s] New Email Address', 'bbpress' ), wp_specialchars_decode( get_option( 'blogname' ) ) );
543
544 // Send the email
545 wp_mail( $r['newemail'], $subject, $message );
546 }
547
548 /**
549 * Conditionally hook the core WordPress output actions to the end of the
550 * default user's edit profile template
551 *
552 * This allows clever plugin authors to conditionally unhook the WordPress core
553 * output actions if they don't want any unexpected junk to appear there, and
554 * also avoids needing to pollute the templates with additional logic and actions.
555 *
556 * @since 2.2.0 bbPress (r4273)
557 */
558 function bbp_user_edit_after() {
559 $action = bbp_is_user_home_edit() ? 'show_user_profile' : 'edit_user_profile';
560
561 do_action( $action, get_userdata( bbp_get_displayed_user_id() ) );
562 }
563
564 /** User Queries **************************************************************/
565
566 /**
567 * Get the topics that a user created
568 *
569 * @since 2.0.0 bbPress (r2660)
570 * @since 2.6.0 bbPress (r6618) Signature changed to accept an array of arguments
571 *
572 * @param array $args Optional. Arguments to pass into bbp_has_topics()
573 *
574 * @return bool True if user has started topics, otherwise false
575 */
576 function bbp_get_user_topics_started( $args = array() ) {
577
578 // Backwards compat for pre-2.6.0
579 if ( is_numeric( $args ) ) {
580 $args = array(
581 'author' => bbp_get_user_id( $args, false, false )
582 );
583 }
584
585 // Default arguments
586 $defaults = array(
587 'author' => bbp_get_displayed_user_id()
588 );
589
590 // Parse arguments
591 $r = bbp_parse_args( $args, $defaults, 'get_user_topics_started' );
592
593 // Get the topics
594 $query = bbp_has_topics( $r );
595 $user_id = $r['author'];
596
597 // Filter & return
598 return apply_filters( 'bbp_get_user_topics_started', $query, $user_id, $r, $args );
599 }
600
601 /**
602 * Get the replies that a user created
603 *
604 * @since 2.2.0 bbPress (r4225)
605 * @since 2.6.0 bbPress (r6618) Signature changed to accept an array of arguments
606 *
607 * @param array $args Optional. Arguments to pass into bbp_has_replies()
608 *
609 * @return bool True if user has created replies, otherwise false
610 */
611 function bbp_get_user_replies_created( $args = array() ) {
612
613 // Backwards compat for pre-2.6.0
614 if ( is_numeric( $args ) ) {
615 $args = array(
616 'author' => bbp_get_user_id( $args, false, false ),
617 'post_type' => bbp_get_reply_post_type(),
618 'order' => 'DESC'
619 );
620 }
621
622 // Default arguments
623 $defaults = array(
624 'author' => bbp_get_displayed_user_id(),
625 'post_type' => bbp_get_reply_post_type(),
626 'order' => 'DESC'
627 );
628
629 // Parse arguments
630 $r = bbp_parse_args( $args, $defaults, 'get_user_replies_created' );
631
632 // Get the replies
633 $query = bbp_has_replies( $r );
634 $user_id = $r['author'];
635
636 // Filter & return
637 return apply_filters( 'bbp_get_user_replies_created', $query, $user_id, $r, $args );
638 }
639
640 /**
641 * Get user IDs from nicenames
642 *
643 * This function is primarily used when saving object moderators
644 *
645 * @since 2.6.0 bbPress
646 *
647 * @param mixed $user_nicenames
648 * @return array
649 */
650 function bbp_get_user_ids_from_nicenames( $user_nicenames = array() ) {
651
652 // Default value
653 $retval = array();
654
655 // Only query if nicenames
656 if ( ! empty( $user_nicenames ) ) {
657
658 // Maybe explode by comma
659 $user_nicenames = ( is_string( $user_nicenames ) && strstr( $user_nicenames, ',' ) )
660 ? explode( ',', $user_nicenames )
661 : (array) $user_nicenames;
662
663 // Sanitize each nicename in the array
664 $user_nicenames = array_map( 'sanitize_title', $user_nicenames );
665
666 // Get users
667 $users = get_users(
668 array(
669 'nicename__in' => $user_nicenames
670 )
671 );
672
673 // Pluck or empty
674 if ( ! empty( $users ) ) {
675 $retval = wp_list_pluck( $users, 'ID' );
676 }
677 }
678
679 // Filter & return
680 return (array) apply_filters( 'bbp_get_user_ids_from_nicenames', $retval, $user_nicenames );
681 }
682
683 /**
684 * Get user nicenames from IDs
685 *
686 * This function is primarily used when saving object moderators
687 *
688 * @since 2.6.0 bbPress
689 *
690 * @param mixed $user_ids
691 * @return array
692 */
693 function bbp_get_user_nicenames_from_ids( $user_ids = array() ) {
694
695 // Default value
696 $retval = array();
697
698 // Only query if nicenames
699 if ( ! empty( $user_ids ) ) {
700
701 // Get users
702 $users = get_users(
703 array(
704 'include' => $user_ids
705 )
706 );
707
708 // Pluck or empty
709 if ( ! empty( $users ) ) {
710 $retval = wp_list_pluck( $users, 'user_nicename' );
711 }
712 }
713
714 // Filter & return
715 return (array) apply_filters( 'bbp_get_user_nicenames_from_ids', $retval, $user_ids );
716 }
717
718 /** Post Counts ***************************************************************/
719
720 /**
721 * Return the raw database count of topics by a user
722 *
723 * @since 2.1.0 bbPress (r3633)
724 *
725 * @param int $user_id User ID to get count for
726 *
727 * @return int Raw DB count of topics
728 */
729 function bbp_get_user_topic_count_raw( $user_id = 0 ) {
730 $user_id = bbp_get_user_id( $user_id );
731 $bbp_db = bbp_db();
732 $statii = "'" . implode( "', '", bbp_get_public_topic_statuses() ) . "'";
733 $sql = "SELECT COUNT(*)
734 FROM {$bbp_db->posts}
735 WHERE post_type = %s
736 AND post_status IN ({$statii})
737 AND post_author = %d";
738
739 $query = $bbp_db->prepare( $sql, bbp_get_topic_post_type(), $user_id );
740 $count = (int) $bbp_db->get_var( $query );
741
742 // Filter & return
743 return (int) apply_filters( 'bbp_get_user_topic_count_raw', $count, $user_id );
744 }
745
746 /**
747 * Return the raw database count of replies by a user
748 *
749 * @since 2.1.0 bbPress (r3633)
750 *
751 * @param int $user_id User ID to get count for
752 *
753 * @return int Raw DB count of replies
754 */
755 function bbp_get_user_reply_count_raw( $user_id = 0 ) {
756 $user_id = bbp_get_user_id( $user_id );
757 $bbp_db = bbp_db();
758 $statii = "'" . implode( "', '", bbp_get_public_reply_statuses() ) . "'";
759 $sql = "SELECT COUNT(*)
760 FROM {$bbp_db->posts}
761 WHERE post_type = %s
762 AND post_status IN ({$statii})
763 AND post_author = %d";
764
765 $query = $bbp_db->prepare( $sql, bbp_get_reply_post_type(), $user_id );
766 $count = (int) $bbp_db->get_var( $query );
767
768 // Filter & return
769 return (int) apply_filters( 'bbp_get_user_reply_count_raw', $count, $user_id );
770 }
771
772 /**
773 * Bump the topic count for a user by a certain amount.
774 *
775 * @since 2.6.0 bbPress (r5309)
776 * @since 2.6.17 Rebuild the count when the user option is missing.
777 *
778 * @param int $user_id
779 * @param int $difference
780 */
781 function bbp_bump_user_topic_count( $user_id = 0, $difference = 1 ) {
782
783 // Bail if no bump
784 if ( empty( $difference ) ) {
785 return false;
786 }
787
788 // Validate user ID
789 $user_id = bbp_get_user_id( $user_id );
790 if ( empty( $user_id ) ) {
791 return false;
792 }
793
794 // Get the current count, accounting for persisted changes if it is missing
795 $difference = (int) $difference;
796 $count = ( false === get_user_option( '_bbp_topic_count', $user_id ) )
797 ? bbp_get_user_topic_count_raw( $user_id ) - $difference
798 : bbp_get_user_topic_count( $user_id, true );
799
800 $user_topic_count = (int) bbp_number_not_negative( $count + $difference );
801
802 // Add them up and filter them
803 $new_count = (int) apply_filters( 'bbp_bump_user_topic_count', $user_topic_count, $user_id, $difference, $count );
804
805 // Preserve absolute count filters before using the atomic difference
806 $difference = ( $new_count === $user_topic_count )
807 ? $new_count - $count
808 : false;
809
810 return bbp_update_user_topic_count( $user_id, $new_count, $difference );
811 }
812
813 /**
814 * Bump the reply count for a user by a certain amount.
815 *
816 * @since 2.6.0 bbPress (r5309)
817 * @since 2.6.17 Rebuild the count when the user option is missing.
818 *
819 * @param int $user_id
820 * @param int $difference
821 */
822 function bbp_bump_user_reply_count( $user_id = 0, $difference = 1 ) {
823
824 // Bail if no bump
825 if ( empty( $difference ) ) {
826 return false;
827 }
828
829 // Validate user ID
830 $user_id = bbp_get_user_id( $user_id );
831 if ( empty( $user_id ) ) {
832 return false;
833 }
834
835 // Get the current count, accounting for persisted changes if it is missing
836 $difference = (int) $difference;
837 $count = ( false === get_user_option( '_bbp_reply_count', $user_id ) )
838 ? bbp_get_user_reply_count_raw( $user_id ) - $difference
839 : bbp_get_user_reply_count( $user_id, true );
840
841 $user_reply_count = (int) bbp_number_not_negative( $count + $difference );
842
843 // Add them up and filter them
844 $new_count = (int) apply_filters( 'bbp_bump_user_reply_count', $user_reply_count, $user_id, $difference, $count );
845
846 // Preserve absolute count filters before using the atomic difference
847 $difference = ( $new_count === $user_reply_count )
848 ? $new_count - $count
849 : false;
850
851 return bbp_update_user_reply_count( $user_id, $new_count, $difference );
852 }
853
854 /**
855 * Update user counts when a topic or reply changes authors.
856 *
857 * @since 2.6.17
858 *
859 * @param int $post_id Post ID.
860 * @param WP_Post $post_after Post object following the update.
861 * @param WP_Post $post_before Post object before the update.
862 */
863 function bbp_update_counts_on_post_author_change( $post_id = 0, $post_after = false, $post_before = false ) {
864
865 // Bail if the author or post type did not change as expected
866 if ( ( $post_after->post_author === $post_before->post_author ) || ( $post_after->post_type !== $post_before->post_type ) ) {
867 return;
868 }
869
870 // Set topic public membership
871 if ( bbp_get_topic_post_type() === $post_after->post_type ) {
872 $public_statuses = bbp_get_public_topic_statuses();
873 $was_public = in_array( $post_before->post_status, $public_statuses, true );
874 $is_public = in_array( $post_after->post_status, $public_statuses, true );
875 $is_topic = true;
876
877 // Set reply public membership
878 } elseif ( bbp_get_reply_post_type() === $post_after->post_type ) {
879 $public_statuses = bbp_get_public_reply_statuses();
880 $was_public = in_array( $post_before->post_status, $public_statuses, true );
881 $is_public = in_array( $post_after->post_status, $public_statuses, true );
882 $is_topic = false;
883
884 // Bail if this is not a topic or reply
885 } else {
886 return;
887 }
888
889 // The transition callback already handles posts that were not public
890 if ( ! $was_public ) {
891 return;
892 }
893
894 // Transfer a public contribution between authors
895 if ( $is_public ) {
896 if ( $is_topic ) {
897 bbp_bump_user_topic_count( $post_before->post_author, -1 );
898 bbp_bump_user_topic_count( $post_after->post_author, 1 );
899 } else {
900 bbp_bump_user_reply_count( $post_before->post_author, -1 );
901 bbp_bump_user_reply_count( $post_after->post_author, 1 );
902 }
903
904 // Repair both authors after the transition callback targeted the new author
905 } else {
906 foreach ( bbp_get_unique_array_values( array( $post_before->post_author, $post_after->post_author ) ) as $user_id ) {
907 if ( $is_topic ) {
908 bbp_update_user_topic_count( $user_id, bbp_get_user_topic_count_raw( $user_id ) );
909 } else {
910 bbp_update_user_reply_count( $user_id, bbp_get_user_reply_count_raw( $user_id ) );
911 }
912 }
913 }
914 }
915
916 /**
917 * Update topic engagements when a topic or reply changes authors.
918 *
919 * @since 2.6.17
920 *
921 * @param int $post_id Post ID.
922 * @param WP_Post $post_after Post object following the update.
923 * @param WP_Post $post_before Post object before the update.
924 */
925 function bbp_recalculate_engagements_on_post_author_change( $post_id = 0, $post_after = false, $post_before = false ) {
926
927 // Bail if the author did not change
928 if ( $post_after->post_author === $post_before->post_author ) {
929 return;
930 }
931
932 // Get the topic ID from a topic or reply
933 if ( bbp_get_topic_post_type() === $post_after->post_type ) {
934 $topic_id = $post_id;
935 } elseif ( bbp_get_reply_post_type() === $post_after->post_type ) {
936 $topic_id = bbp_get_reply_topic_id( $post_id );
937 } else {
938 return;
939 }
940
941 // Recalculate engagements and their count
942 bbp_recalculate_topic_engagements( $topic_id );
943 bbp_update_topic_voice_count( $topic_id );
944 }
945
946 /**
947 * Update counts and engagements when a deleted user's posts are reassigned.
948 *
949 * WordPress reassigns post authors directly in the database, bypassing the
950 * normal post update actions. Record affected topics before that write, then
951 * repair the replacement user's counts and those topics after it completes.
952 *
953 * @since 2.6.17
954 *
955 * @param int $user_id ID of the user being deleted.
956 * @param int|null $reassign ID of the user receiving the posts.
957 */
958 function bbp_update_counts_on_user_reassignment( $user_id = 0, $reassign = null ) {
959 static $topic_ids = array();
960
961 $user_id = (int) $user_id;
962 $reassign = (int) $reassign;
963
964 // Bail if posts are not being reassigned to another user
965 if ( empty( $user_id ) || empty( $reassign ) || ( $user_id === $reassign ) ) {
966 return;
967 }
968
969 $key = get_current_blog_id() . ':' . $user_id . ':' . $reassign;
970
971 // Record affected topics before WordPress changes their authors directly
972 if ( 'delete_user' === current_filter() ) {
973 $bbp_db = bbp_db();
974 $topic_type = bbp_get_topic_post_type();
975 $reply_type = bbp_get_reply_post_type();
976 $query = $bbp_db->prepare(
977 "SELECT DISTINCT CASE WHEN post_type = %s THEN ID ELSE post_parent END FROM {$bbp_db->posts} WHERE post_author = %d AND post_type IN ( %s, %s )",
978 $topic_type,
979 $user_id,
980 $topic_type,
981 $reply_type
982 );
983
984 $topic_ids[ $key ] = wp_parse_id_list( array_filter( $bbp_db->get_col( $query ) ) );
985 return;
986 }
987
988 // Bail unless WordPress completed the reassignment recorded above
989 if ( ( 'deleted_user' !== current_filter() ) || ! isset( $topic_ids[ $key ] ) ) {
990 return;
991 }
992
993 $affected_topic_ids = $topic_ids[ $key ];
994 unset( $topic_ids[ $key ] );
995
996 // Bail if the deleted user did not author any topics or replies
997 if ( empty( $affected_topic_ids ) ) {
998 return;
999 }
1000
1001 // Recount contributions for the replacement user
1002 bbp_update_user_topic_count( $reassign, bbp_get_user_topic_count_raw( $reassign ) );
1003 bbp_update_user_reply_count( $reassign, bbp_get_user_reply_count_raw( $reassign ) );
1004
1005 // Rebuild engagements and voices for each affected topic
1006 foreach ( $affected_topic_ids as $topic_id ) {
1007 bbp_recalculate_topic_engagements( $topic_id, true );
1008 bbp_update_topic_voice_count( $topic_id );
1009 }
1010 }
1011
1012 /**
1013 * Helper function used to increase (by one) the count of topics for a user when
1014 * a topic is published.
1015 *
1016 * @since 2.6.0 bbPress (r5309)
1017 *
1018 * @param int $topic_id Topic ID.
1019 */
1020 function bbp_increase_user_topic_count( $topic_id = 0 ) {
1021
1022 // Bail if topic is not public
1023 if ( ! bbp_is_topic_public( $topic_id ) ) {
1024 return false;
1025 }
1026
1027 $user_id = bbp_get_topic_author_id( $topic_id );
1028 return bbp_bump_user_topic_count( $user_id, 1 );
1029 }
1030
1031 /**
1032 * Helper function used to increase (by one) the count of replies for a user when
1033 * a reply is published.
1034 *
1035 * @since 2.6.0 bbPress (r5309)
1036 *
1037 * @param int $reply_id Reply ID.
1038 */
1039 function bbp_increase_user_reply_count( $reply_id = 0 ) {
1040
1041 // Bail if reply is not public
1042 if ( ! bbp_is_reply_public( $reply_id ) ) {
1043 return false;
1044 }
1045
1046 $user_id = bbp_get_reply_author_id( $reply_id );
1047 return bbp_bump_user_reply_count( $user_id, 1 );
1048 }
1049
1050 /**
1051 * Helper function used to decrease (by one) the count of topics for a user when
1052 * a topic is unpublished.
1053 *
1054 * @since 2.6.0 bbPress (r5309)
1055 *
1056 * @param int $topic_id Topic ID.
1057 */
1058 function bbp_decrease_user_topic_count( $topic_id = 0 ) {
1059
1060 // Bail if topic is not public
1061 if ( ! bbp_is_topic_public( $topic_id ) ) {
1062 return false;
1063 }
1064
1065 $user_id = bbp_get_topic_author_id( $topic_id );
1066 return bbp_bump_user_topic_count( $user_id, -1 );
1067 }
1068
1069 /**
1070 * Helper function used to decrease (by one) the count of replies for a user when
1071 * a reply is unpublished.
1072 *
1073 * @since 2.6.0 bbPress (r5309)
1074 *
1075 * @param int $reply_id Reply ID.
1076 */
1077 function bbp_decrease_user_reply_count( $reply_id = 0 ) {
1078
1079 // Bail if reply is not public
1080 if ( ! bbp_is_reply_public( $reply_id ) ) {
1081 return false;
1082 }
1083
1084 $user_id = bbp_get_reply_author_id( $reply_id );
1085 return bbp_bump_user_reply_count( $user_id, -1 );
1086 }
1087
1088 /** Permissions ***************************************************************/
1089
1090 /**
1091 * Redirect if unauthorized user is attempting to edit another user
1092 *
1093 * This is hooked to 'bbp_template_redirect' and controls the conditions under
1094 * which a user can edit another user (or themselves.) If these conditions are
1095 * met, we assume a user cannot perform this task, and look for ways they can
1096 * earn the ability to access this template.
1097 *
1098 * @since 2.1.0 bbPress (r3605)
1099 */
1100 function bbp_check_user_edit() {
1101
1102 // Bail if not editing a user
1103 if ( ! bbp_is_single_user_edit() ) {
1104 return;
1105 }
1106
1107 // Default to false
1108 $redirect = true;
1109 $user_id = bbp_get_displayed_user_id();
1110
1111 // Allow user to edit their own profile
1112 if ( bbp_is_user_home_edit() ) {
1113 $redirect = false;
1114
1115 // Allow if current user can edit the displayed user
1116 } elseif ( current_user_can( 'edit_user', $user_id ) ) {
1117 $redirect = false;
1118
1119 // Allow if user can manage network users, or edit-any is enabled
1120 } elseif ( current_user_can( 'manage_network_users' ) || apply_filters( 'enable_edit_any_user_configuration', false ) ) {
1121 $redirect = false;
1122 }
1123
1124 // Allow conclusion to be overridden
1125 $redirect = (bool) apply_filters( 'bbp_check_user_edit', $redirect, $user_id );
1126
1127 // Bail if not redirecting
1128 if ( false === $redirect ) {
1129 return;
1130 }
1131
1132 // Filter redirect URL
1133 $profile_url = bbp_get_user_profile_url( $user_id );
1134 $redirect_to = apply_filters( 'bbp_check_user_edit_redirect_to', $profile_url, $user_id );
1135
1136 // Redirect
1137 bbp_redirect( $redirect_to );
1138 }
1139
1140 /**
1141 * Check if a user is blocked, or cannot spectate the forums.
1142 *
1143 * @since 2.0.0 bbPress (r2996)
1144 */
1145 function bbp_forum_enforce_blocked() {
1146
1147 // Bail if not logged in or keymaster
1148 if ( ! is_user_logged_in() || bbp_is_user_keymaster() ) {
1149 return;
1150 }
1151
1152 // Set 404 if in bbPress and user cannot spectate
1153 if ( is_bbpress() && ! current_user_can( 'spectate' ) ) {
1154 bbp_set_404();
1155 }
1156 }
1157
1158 /** Sanitization **************************************************************/
1159
1160 /**
1161 * Sanitize displayed user data, when viewing and editing any user.
1162 *
1163 * This somewhat monolithic function handles the escaping and sanitization of
1164 * user data for a bbPress profile. There are two reasons this all happens here:
1165 *
1166 * 1. bbPress took a similar approach to WordPress, and funnels all user profile
1167 * data through a central helper. This eventually calls sanitize_user_field()
1168 * which applies a few context based filters, which some third party plugins
1169 * might be relying on bbPress to play nicely with.
1170 *
1171 * 2. Early versions of bbPress 2.x templates did not escape this data meaning
1172 * a backwards compatible approach like this one was necessary to protect
1173 * existing installations that may have custom template parts.
1174 *
1175 * @since 2.6.0 bbPress (r5368)
1176 *
1177 * @param string $value
1178 * @param string $field
1179 * @param string $context
1180 * @return string
1181 */
1182 function bbp_sanitize_displayed_user_field( $value = '', $field = '', $context = 'display' ) {
1183
1184 // Bail if not editing or displaying (maybe we'll do more here later)
1185 if ( ! in_array( $context, array( 'edit', 'display' ), true ) ) {
1186 return $value;
1187 }
1188
1189 // By default, no filter set (consider making this an array later)
1190 $filter = false;
1191
1192 // Big switch statement to decide which user field we're sanitizing and how
1193 switch ( $field ) {
1194
1195 // Description is a paragraph
1196 case 'description' :
1197 $filter = ( 'edit' === $context ) ? '' : 'wp_kses_data';
1198 break;
1199
1200 // Email addresses are sanitized with a specific function
1201 case 'user_email' :
1202 $filter = 'sanitize_email';
1203 break;
1204
1205 // Name & login fields
1206 case 'user_login' :
1207 case 'display_name' :
1208 case 'first_name' :
1209 case 'last_name' :
1210 case 'nick_name' :
1211 $filter = ( 'edit' === $context ) ? 'esc_attr' : 'esc_html';
1212 break;
1213
1214 // wp-includes/default-filters.php escapes this for us via esc_url()
1215 case 'user_url' :
1216 break;
1217 }
1218
1219 // Run any applicable filters on the value
1220 if ( ! empty( $filter ) ) {
1221 $value = call_user_func( $filter, $value );
1222 }
1223
1224 return $value;
1225 }
1226
1227 /** Converter *****************************************************************/
1228
1229 /**
1230 * Convert passwords from previous platform encryption to WordPress encryption.
1231 *
1232 * @since 2.1.0 bbPress (r3813)
1233 * @since 2.6.10 bbPress (r7244) Switched from direct query to get_user_by()
1234 */
1235 function bbp_user_maybe_convert_pass() {
1236
1237 // Sanitize login
1238 $login = ! empty( $_POST['log'] )
1239 ? sanitize_user( wp_unslash( $_POST['log'] ) )
1240 : '';
1241
1242 // Sanitize password
1243 $pass = ! empty( $_POST['pwd'] )
1244 ? trim( $_POST['pwd'] )
1245 : '';
1246
1247 // Bail if no username or password
1248 if ( empty( $login ) || empty( $pass ) ) {
1249 return;
1250 }
1251
1252 // Get user by login...
1253 $user = get_user_by( 'login', $login );
1254
1255 // ...or get user by email
1256 if ( empty( $user ) && strpos( $login, '@' ) ) {
1257 $user = get_user_by( 'email', $login );
1258 }
1259
1260 // Bail if no user
1261 if ( empty( $user ) ) {
1262 return;
1263 }
1264
1265 // Get converter class from usermeta
1266 $class = get_user_meta( $user->ID, '_bbp_class', true );
1267
1268 // Bail if no converter class in meta
1269 if ( empty( $class ) || ! is_string( $class ) ) {
1270 return;
1271 }
1272
1273 // Setup the converter
1274 bbp_setup_converter();
1275
1276 // Try to instantiate the converter class
1277 $converter = bbp_new_converter( $class );
1278
1279 // Bail if no converter
1280 if ( empty( $converter ) ) {
1281 return;
1282 }
1283
1284 // Try to call the password conversion callback method
1285 if ( ( $converter instanceof BBP_Converter_Base ) && method_exists( $converter, 'callback_pass' ) ) {
1286 $converter->callback_pass( $login, $pass );
1287 }
1288 }
1289