PluginProbe
bbPress / 2.6.15
bbPress v2.6.15
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 2.2.2 All 71 releases
bbpress / includes / users / template.php

template.php in bbPress 2.6.15, at includes/users/template.php

2,480 lines 65.7 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 Template Tags
5 *
6 * @package bbPress
7 * @subpackage TemplateTags
8 */
9
10 // Exit if accessed directly
11 defined( 'ABSPATH' ) || exit;
12
13 /** User Loop *****************************************************************/
14
15 /**
16 * Extension of WP_User_Query to allow easy looping
17 *
18 * @since 2.6.0 bbPress (r6330)
19 */
20 class BBP_User_Query extends WP_User_Query {
21
22 /**
23 * The amount of users for the current query.
24 *
25 * @since 2.6.0 bbPress (r6330)
26 * @access public
27 * @var int
28 */
29 public $user_count = 0;
30
31 /**
32 * Index of the current item in the loop.
33 *
34 * @since 2.6.0 bbPress (r6330)
35 * @access public
36 * @var int
37 */
38 public $current_user = -1;
39
40 /**
41 * Whether the loop has started and the caller is in the loop.
42 *
43 * @since 2.6.0 bbPress (r6330)
44 * @access public
45 * @var bool
46 */
47 public $in_the_loop = false;
48
49 /**
50 * The current user.
51 *
52 * @since 2.6.0 bbPress (r6330)
53 * @access public
54 * @var WP_User
55 */
56 public $user;
57
58 /**
59 * PHP5 constructor.
60 *
61 * @since 2.6.0 bbPress (r6330)
62 * @access public
63 *
64 * @param null|string|array $query Optional. The query variables.
65 */
66 public function __construct( $query = null ) {
67 if ( ! empty( $query ) ) {
68 parent::__construct( $query );
69 $this->user_count = count( $this->results );
70 }
71 }
72
73 /**
74 * Set up the next user and iterate current user index.
75 *
76 * @since 2.6.0 bbPress (r6330)
77 * @access public
78 *
79 * @return WP_User Next user.
80 */
81 public function next_user() {
82 $this->current_user++;
83 $this->user = $this->results[ $this->current_user ];
84
85 return $this->user;
86 }
87
88 /**
89 * Sets up the current user.
90 *
91 * Retrieves the next user, sets up the user, sets the 'in the loop'
92 * property to true.
93 *
94 * @since 2.6.0 bbPress (r6330)
95 * @access public
96 *
97 * @global WP_User $user
98 */
99 public function the_user() {
100 $this->in_the_loop = true;
101
102 // loop has just started
103 if ( $this->current_user === -1 ) {
104
105 /**
106 * Fires once the loop is started.
107 *
108 * @since 2.6.0 bbPress (r6330)
109 *
110 * @param WP_Query &$this The WP_Query instance (passed by reference).
111 */
112 do_action_ref_array( 'loop_start', array( &$this ) );
113 }
114
115 $this->next_user();
116 }
117
118 /**
119 * Determines whether there are more users available in the loop.
120 *
121 * Calls the {@see 'loop_end'} action when the loop is complete.
122 *
123 * @since 2.6.0 bbPress (r6330)
124 * @access public
125 *
126 * @return bool True if users are available, false if end of loop.
127 */
128 public function have_users() {
129 if ( ( $this->current_user + 1 ) < $this->user_count ) {
130 return true;
131 } elseif ( ( ( $this->current_user + 1 ) === $this->user_count ) && ( $this->user_count > 0 ) ) {
132
133 /**
134 * Fires once the loop has ended.
135 *
136 * @since 2.6.0 bbPress (r6330)
137 *
138 * @param WP_Query &$this The WP_Query instance (passed by reference).
139 */
140 do_action_ref_array( 'loop_end', array( &$this ) );
141
142 // Do some cleaning up after the loop
143 $this->rewind_users();
144 }
145
146 $this->in_the_loop = false;
147
148 return false;
149 }
150
151 /**
152 * Rewind the users and reset user index.
153 *
154 * @since 2.6.0 bbPress (r6330)
155 * @access public
156 */
157 public function rewind_users() {
158 $this->current_user = -1;
159
160 if ( $this->user_count > 0 ) {
161 $this->user = $this->results[ 0 ];
162 }
163 }
164 }
165
166 /**
167 * The main user loop.
168 *
169 * @since 2.6.0 bbPress (r6330)
170 *
171 * @param array $args All the arguments supported by {@link WP_User_Query}
172 * @return object Multidimensional array of user information
173 */
174 function bbp_has_users( $args = array() ) {
175
176 // Parse arguments with default user query for most circumstances
177 $r = bbp_parse_args( $args, array(
178 'include' => array(),
179 'orderby' => 'login',
180 'order' => 'ASC',
181 'count_total' => false,
182 'fields' => 'all',
183 ), 'has_users' );
184
185 // Run the query
186 $bbp = bbpress();
187 $bbp->user_query = new BBP_User_Query( $r );
188
189 // Filter & return
190 return apply_filters( 'bbp_has_users', $bbp->user_query->have_users(), $bbp->user_query );
191 }
192
193 /**
194 * Whether there are more users available in the loop
195 *
196 * @since 2.6.0 bbPress (r2464)
197 *
198 * @return object User information
199 */
200 function bbp_users() {
201 return bbpress()->user_query->have_users();
202 }
203
204 /**
205 * Loads up the current user in the loop
206 *
207 * @since 2.6.0 bbPress (r2464)
208 *
209 * @return object User information
210 */
211 function bbp_the_user() {
212 return bbpress()->user_query->the_user();
213 }
214
215 /** Users *********************************************************************/
216
217 /**
218 * Output a validated user id
219 *
220 * @since 2.0.0 bbPress (r2729)
221 *
222 * @param int $user_id Optional. User id
223 * @param bool $displayed_user_fallback Fallback on displayed user?
224 * @param bool $current_user_fallback Fallback on current user?
225 */
226 function bbp_user_id( $user_id = 0, $displayed_user_fallback = true, $current_user_fallback = false ) {
227 echo bbp_get_user_id( $user_id, $displayed_user_fallback, $current_user_fallback );
228 }
229 /**
230 * Return a validated user id
231 *
232 * @since 2.0.0 bbPress (r2729)
233 *
234 * @param int $user_id Optional. User id
235 * @param bool $displayed_user_fallback Fallback on displayed user?
236 * @param bool $current_user_fallback Fallback on current user?
237 * @return int Validated user id
238 */
239 function bbp_get_user_id( $user_id = 0, $displayed_user_fallback = true, $current_user_fallback = false ) {
240 $bbp = bbpress();
241
242 // Easy empty checking
243 if ( ! empty( $user_id ) && is_numeric( $user_id ) ) {
244 $bbp_user_id = $user_id;
245
246 // Currently inside a user loop
247 } elseif ( ! empty( $bbp->user_query->in_the_loop ) && isset( $bbp->user_query->user->ID ) ) {
248 $bbp_user_id = $bbp->user_query->user->ID;
249
250 // Currently viewing or editing a user
251 } elseif ( ( true === $displayed_user_fallback ) && ! empty( $bbp->displayed_user->ID ) ) {
252 $bbp_user_id = $bbp->displayed_user->ID;
253
254 // Maybe fallback on the current_user ID
255 } elseif ( ( true === $current_user_fallback ) && ! empty( $bbp->current_user->ID ) ) {
256 $bbp_user_id = $bbp->current_user->ID;
257
258 // Failsafe
259 } else {
260 $bbp_user_id = 0;
261 }
262
263 // Filter & return
264 return (int) apply_filters( 'bbp_get_user_id', (int) $bbp_user_id, $displayed_user_fallback, $current_user_fallback );
265 }
266
267 /**
268 * Output ID of current user
269 *
270 * @since 2.0.0 bbPress (r2574)
271 */
272 function bbp_current_user_id() {
273 echo bbp_get_current_user_id();
274 }
275 /**
276 * Return ID of current user
277 *
278 * @since 2.0.0 bbPress (r2574)
279 *
280 * @return int Current user id
281 */
282 function bbp_get_current_user_id() {
283
284 // Filter & return
285 return (int) apply_filters( 'bbp_get_current_user_id', bbp_get_user_id( 0, false, true ) );
286 }
287
288 /**
289 * Output ID of displayed user
290 *
291 * @since 2.0.0 bbPress (r2688)
292 */
293 function bbp_displayed_user_id() {
294 echo bbp_get_displayed_user_id();
295 }
296 /**
297 * Return ID of displayed user
298 *
299 * @since 2.0.0 bbPress (r2688)
300 *
301 * @return int Displayed user id
302 */
303 function bbp_get_displayed_user_id() {
304
305 // Filter & return
306 return apply_filters( 'bbp_get_displayed_user_id', bbp_get_user_id( 0, true, false ) );
307 }
308
309 /**
310 * Output a sanitized user field value
311 *
312 * This function relies on the $filter parameter to decide how to sanitize
313 * the field value that it finds. Since it uses the WP_User object's magic
314 * __get() method, it can also be used to get user_meta values.
315 *
316 * @since 2.0.0 bbPress (r2688)
317 *
318 * @param string $field Field to get
319 * @param string $filter How to filter the field value (null|raw|db|display|edit)
320 */
321 function bbp_displayed_user_field( $field = '', $filter = 'display' ) {
322 echo bbp_get_displayed_user_field( $field, $filter );
323 }
324 /**
325 * Return a sanitized user field value
326 *
327 * This function relies on the $filter parameter to decide how to sanitize
328 * the field value that it finds. Since it uses the WP_User object's magic
329 * __get() method, it can also be used to get user_meta values.
330 *
331 * @since 2.0.0 bbPress (r2688)
332 *
333 * @param string $field Field to get
334 * @param string $filter How to filter the field value (null|raw|db|display|edit)
335 * @see WP_User::__get() for more on how the value is retrieved
336 * @see sanitize_user_field() for more on how the value is sanitized
337 * @return string|bool Value of the field if it exists, else false
338 */
339 function bbp_get_displayed_user_field( $field = '', $filter = 'display' ) {
340
341 // Get the displayed user
342 $user = bbpress()->displayed_user;
343
344 // Juggle the user filter property because we don't want to muck up how
345 // other code might interact with this object.
346 $old_filter = $user->filter;
347 $user->filter = $filter;
348
349 // Get the field value from the WP_User object. We don't need to perform
350 // an isset() because the WP_User::__get() does it for us.
351 $value = $user->$field;
352
353 // Put back the user filter property that was previously juggled above.
354 $user->filter = $old_filter;
355
356 // Filter & return
357 return apply_filters( 'bbp_get_displayed_user_field', $value, $field, $filter );
358 }
359
360 /**
361 * Output name of current user
362 *
363 * @since 2.0.0 bbPress (r2574)
364 */
365 function bbp_current_user_name() {
366 echo esc_attr( bbp_get_current_user_name() );
367 }
368 /**
369 * Return name of current user
370 *
371 * @since 2.0.0 bbPress (r2574)
372 *
373 * @return string
374 */
375 function bbp_get_current_user_name() {
376
377 // Default current user name
378 $default = bbp_get_fallback_display_name();
379
380 // Check the $user_identity global
381 $current_user_name = is_user_logged_in()
382 ? bbp_get_global_object( 'user_identity', false, $default )
383 : $default;
384
385 // Filter & return
386 return apply_filters( 'bbp_get_current_user_name', $current_user_name );
387 }
388
389 /**
390 * Output avatar of current user
391 *
392 * @since 2.0.0 bbPress (r2574)
393 *
394 * @param int $size Size of the avatar. Defaults to 40
395 */
396 function bbp_current_user_avatar( $size = 40 ) {
397 echo bbp_get_current_user_avatar( $size );
398 }
399
400 /**
401 * Return avatar of current user
402 *
403 * @since 2.0.0 bbPress (r2574)
404 *
405 * @param int $size Size of the avatar. Defaults to 40
406 * @return string Current user avatar
407 */
408 function bbp_get_current_user_avatar( $size = 40 ) {
409
410 $user = bbp_get_current_user_id();
411 if ( empty( $user ) ) {
412 $user = bbp_get_current_anonymous_user_data( 'email' );
413 }
414
415 $avatar = get_avatar( $user, $size );
416
417 // Filter & return
418 return apply_filters( 'bbp_get_current_user_avatar', $avatar, $size );
419 }
420
421 /**
422 * Output link to the profile page of a user
423 *
424 * @since 2.0.0 bbPress (r2688)
425 *
426 * @param int $user_id Optional. User id
427 */
428 function bbp_user_profile_link( $user_id = 0 ) {
429 echo bbp_get_user_profile_link( $user_id );
430 }
431 /**
432 * Return link to the profile page of a user
433 *
434 * @since 2.0.0 bbPress (r2688)
435 *
436 * @param int $user_id Optional. User id
437 * @return string User profile link
438 */
439 function bbp_get_user_profile_link( $user_id = 0 ) {
440
441 // Validate user id
442 $user_id = bbp_get_user_id( $user_id );
443 if ( empty( $user_id ) ) {
444 return false;
445 }
446
447 // Get the user
448 $user = get_userdata( $user_id );
449 if ( empty( $user ) ) {
450 return false;
451 }
452
453 // Display Name
454 $name = ! empty( $user->display_name )
455 ? $user->display_name
456 : bbp_get_fallback_display_name();
457
458 // URL
459 $url = bbp_get_user_profile_url( $user_id );
460
461 // Link
462 $link = ! empty( $url )
463 ? '<a href="' . esc_url( $url ) . '">' . esc_html( $name ) . '</a>'
464 : esc_html( $name );
465
466 // Filter & return
467 return (string) apply_filters( 'bbp_get_user_profile_link', $link, $user_id );
468 }
469
470 /**
471 * Output a users nicename to the screen
472 *
473 * @since 2.3.0 bbPress (r4671)
474 *
475 * @param int $user_id User ID whose nicename to get
476 * @param array $args before|after|user_id|force
477 */
478 function bbp_user_nicename( $user_id = 0, $args = array() ) {
479 echo bbp_get_user_nicename( $user_id, $args );
480 }
481 /**
482 * Return a users nicename to the screen
483 *
484 * @since 2.3.0 bbPress (r4671)
485 *
486 * @param int $user_id User ID whose nicename to get
487 * @param array $args before|after|user_id|force
488 * @return string User nicename, maybe wrapped in before/after strings
489 */
490 function bbp_get_user_nicename( $user_id = 0, $args = array() ) {
491
492 // Bail if no user ID passed
493 $user_id = bbp_get_user_id( $user_id );
494 if ( empty( $user_id ) ) {
495 return false;
496 }
497
498 // Parse default arguments
499 $r = bbp_parse_args( $args, array(
500 'user_id' => $user_id,
501 'before' => '',
502 'after' => '',
503 'force' => ''
504 ), 'get_user_nicename' );
505
506 // Force the nicename (likely from a previous user query)
507 if ( ! empty( $r['force'] ) ) {
508 $nicename = (string) $r['force'];
509
510 // Maybe fallback to getting the nicename from user data
511 } elseif ( ! empty( $r['user_id'] ) ) {
512 $user = get_userdata( $r['user_id'] );
513 $nicename = ! empty( $user )
514 ? $user->user_nicename
515 : '';
516
517 // Maybe fallback to empty string so filter still applies
518 } else {
519 $nicename = '';
520 }
521
522 // Maybe wrap the nicename
523 $retval = ! empty( $nicename )
524 ? $r['before'] . esc_html( $nicename ) . $r['after']
525 : '';
526
527 // Filter & return
528 return (string) apply_filters( 'bbp_get_user_nicename', $retval, $user_id, $r, $args );
529 }
530
531 /**
532 * Output URL to the profile page of a user
533 *
534 * @since 2.0.0 bbPress (r2688)
535 *
536 * @param int $user_id Optional. User id
537 * @param string $user_nicename Optional. User nicename
538 */
539 function bbp_user_profile_url( $user_id = 0, $user_nicename = '' ) {
540 echo esc_url( bbp_get_user_profile_url( $user_id, $user_nicename ) );
541 }
542 /**
543 * Return URL to the profile page of a user
544 *
545 * @since 2.0.0 bbPress (r2688)
546 *
547 * @param int $user_id Optional. User id
548 * @param string $user_nicename Optional. User nicename
549 * @return string User profile url
550 */
551 function bbp_get_user_profile_url( $user_id = 0, $user_nicename = '' ) {
552
553 // Use displayed user ID if there is one, and one isn't requested
554 $user_id = bbp_get_user_id( $user_id );
555 if ( empty( $user_id ) ) {
556 return false;
557 }
558
559 // Bail if intercepted
560 $intercept = bbp_maybe_intercept( 'bbp_pre_get_user_profile_url', func_get_args() );
561 if ( bbp_is_intercepted( $intercept ) ) {
562 return $intercept;
563 }
564
565 // Pretty permalinks
566 if ( bbp_use_pretty_urls() ) {
567
568 // Get username if not passed
569 if ( empty( $user_nicename ) ) {
570 $user_nicename = bbp_get_user_nicename( $user_id );
571 }
572
573 // Run through home_url()
574 $url = trailingslashit( bbp_get_root_url() . bbp_get_user_slug() ) . $user_nicename;
575 $url = user_trailingslashit( $url );
576 $url = home_url( $url );
577
578 // Unpretty permalinks
579 } else {
580 $url = add_query_arg( array(
581 bbp_get_user_rewrite_id() => $user_id
582 ), home_url( '/' ) );
583 }
584
585 // Filter & return
586 return apply_filters( 'bbp_get_user_profile_url', $url, $user_id, $user_nicename );
587 }
588
589 /**
590 * Output link to the profile edit page of a user
591 *
592 * @since 2.0.0 bbPress (r2688)
593 *
594 * @param int $user_id Optional. User id
595 */
596 function bbp_user_profile_edit_link( $user_id = 0 ) {
597 echo bbp_get_user_profile_edit_link( $user_id );
598 }
599 /**
600 * Return link to the profile edit page of a user
601 *
602 * @since 2.0.0 bbPress (r2688)
603 *
604 * @param int $user_id Optional. User id
605 * @return string User profile edit link
606 */
607 function bbp_get_user_profile_edit_link( $user_id = 0 ) {
608
609 // Validate user id
610 $user_id = bbp_get_user_id( $user_id );
611 if ( empty( $user_id ) ) {
612 return false;
613 }
614
615 // Get the user
616 $user = get_userdata( $user_id );
617 if ( empty( $user ) ) {
618 return false;
619 }
620
621 // Display Name
622 $name = ! empty( $user->display_name )
623 ? $user->display_name
624 : bbp_get_fallback_display_name();
625
626 // URL
627 $url = bbp_get_user_profile_edit_url( $user_id );
628
629 // Link
630 $link = ! empty( $url )
631 ? '<a href="' . esc_url( $url ) . '">' . esc_html( $name ) . '</a>'
632 : esc_html( $name );
633
634 // Filter & return
635 return (string) apply_filters( 'bbp_get_user_profile_edit_link', $link, $user_id );
636 }
637
638 /**
639 * Output URL to the profile edit page of a user
640 *
641 * @since 2.0.0 bbPress (r2688)
642 *
643 * @param int $user_id Optional. User id
644 * @param string $user_nicename Optional. User nicename
645 */
646 function bbp_user_profile_edit_url( $user_id = 0, $user_nicename = '' ) {
647 echo esc_url( bbp_get_user_profile_edit_url( $user_id, $user_nicename ) );
648 }
649 /**
650 * Return URL to the profile edit page of a user
651 *
652 * @since 2.0.0 bbPress (r2688)
653 *
654 * @param int $user_id Optional. User id
655 * @param string $user_nicename Optional. User nicename
656 * @return string
657 */
658 function bbp_get_user_profile_edit_url( $user_id = 0, $user_nicename = '' ) {
659
660 $user_id = bbp_get_user_id( $user_id );
661 if ( empty( $user_id ) ) {
662 return false;
663 }
664
665 // Bail if intercepted
666 $intercept = bbp_maybe_intercept( 'bbp_pre_get_user_profile_edit_url', func_get_args() );
667 if ( bbp_is_intercepted( $intercept ) ) {
668 return $intercept;
669 }
670
671 // Get user profile URL
672 $profile_url = bbp_get_user_profile_url( $user_id, $user_nicename );
673
674 // Pretty permalinks
675 if ( bbp_use_pretty_urls() ) {
676 $url = trailingslashit( $profile_url ) . bbp_get_edit_slug();
677 $url = user_trailingslashit( $url );
678
679 // Unpretty permalinks
680 } else {
681 $url = add_query_arg( array(
682 bbp_get_edit_rewrite_id() => '1'
683 ), $profile_url );
684 }
685
686 // Filter & return
687 return apply_filters( 'bbp_get_user_edit_profile_url', $url, $user_id, $user_nicename );
688 }
689
690 /**
691 * Output a user's main role for display
692 *
693 * @since 2.1.0 bbPress (r3860)
694 *
695 * @param int $user_id
696 */
697 function bbp_user_display_role( $user_id = 0 ) {
698 echo esc_attr( bbp_get_user_display_role( $user_id ) );
699 }
700 /**
701 * Return a user's main role for display
702 *
703 * @since 2.1.0 bbPress (r3860)
704 *
705 * @param int $user_id
706 * @return string
707 */
708 function bbp_get_user_display_role( $user_id = 0 ) {
709
710 // Validate user id
711 $user_id = bbp_get_user_id( $user_id );
712
713 // User is not registered
714 if ( empty( $user_id ) ) {
715 $role = esc_html__( 'Guest', 'bbpress' );
716
717 // User is not active
718 } elseif ( bbp_is_user_inactive( $user_id ) ) {
719 $role = esc_html__( 'Inactive', 'bbpress' );
720
721 // User have a role
722 } else {
723 $role_id = bbp_get_user_role( $user_id );
724 $role = bbp_get_dynamic_role_name( $role_id );
725 }
726
727 // No role found so default to generic "Member"
728 if ( empty( $role ) ) {
729 $role = esc_html__( 'Member', 'bbpress' );
730 }
731
732 // Filter & return
733 return apply_filters( 'bbp_get_user_display_role', $role, $user_id );
734 }
735
736 /**
737 * Output the link to the admin section
738 *
739 * @since 2.0.0 bbPress (r2827)
740 *
741 * @param array $args Optional. See {@link bbp_get_admin_link()}
742 */
743 function bbp_admin_link( $args = array() ) {
744 echo bbp_get_admin_link( $args );
745 }
746 /**
747 * Return the link to the admin section
748 *
749 * @since 2.0.0 bbPress (r2827)
750 *
751 * @param array $args Optional. This function supports these arguments:
752 * - text: The text
753 * - before: Before the lnk
754 * - after: After the link
755 * @return The link
756 */
757 function bbp_get_admin_link( $args = array() ) {
758
759 // Bail if user cannot globally moderate
760 if ( ! current_user_can( 'moderate' ) ) {
761 return;
762 }
763
764 if ( ! empty( $args ) && is_string( $args ) && ( false === strpos( $args, '=' ) ) ) {
765 $args = array( 'text' => $args );
766 }
767
768 // Parse arguments against default values
769 $r = bbp_parse_args( $args, array(
770 'text' => esc_html__( 'Admin', 'bbpress' ),
771 'before' => '',
772 'after' => ''
773 ), 'get_admin_link' );
774
775 $retval = $r['before'] . '<a href="' . esc_url( admin_url() ) . '">' . $r['text'] . '</a>' . $r['after'];
776
777 // Filter & return
778 return apply_filters( 'bbp_get_admin_link', $retval, $r, $args );
779 }
780
781 /** User IP *******************************************************************/
782
783 /**
784 * Output the author IP address of a post
785 *
786 * @since 2.0.0 bbPress (r3120)
787 *
788 * @param array $args Optional. If it is an integer, it is used as post id.
789 */
790 function bbp_author_ip( $args = array() ) {
791 echo bbp_get_author_ip( $args );
792 }
793 /**
794 * Return the author IP address of a post
795 *
796 * @since 2.0.0 bbPress (r3120)
797 *
798 * @param array $args Optional. If an integer, it is used as reply id.
799 * @return string Author link of reply
800 */
801 function bbp_get_author_ip( $args = array() ) {
802
803 // Used as post id
804 $post_id = is_numeric( $args ) ? (int) $args : 0;
805
806 // Parse arguments against default values
807 $r = bbp_parse_args( $args, array(
808 'post_id' => $post_id,
809 'before' => '<span class="bbp-author-ip">(',
810 'after' => ')</span>'
811 ), 'get_author_ip' );
812
813 // Get the author IP meta value
814 $author_ip = get_post_meta( $r['post_id'], '_bbp_author_ip', true );
815 $author_ip = ! empty( $author_ip )
816 ? $r['before'] . esc_html( $author_ip ) . $r['after']
817 : '';
818
819 // Filter & return
820 return apply_filters( 'bbp_get_author_ip', $author_ip, $r, $args );
821 }
822
823 /** Anonymous Fields **********************************************************/
824
825 /**
826 * Get the default name that's displayed when a user cannot be identified.
827 *
828 * This might happen if a user was deleted but their content was retained, or
829 * if something went wrong during saving anonymous user data to the database.
830 *
831 * @since 2.6.0 bbPress (r6561)
832 *
833 * @param int $object_id For additional context only, usually a post ID
834 *
835 * @return string
836 */
837 function bbp_get_fallback_display_name( $object_id = 0 ) {
838 $name = esc_html__( 'Anonymous', 'bbpress' );
839
840 // Filter & return
841 return apply_filters( 'bbp_get_anonymous_name', $name, $object_id );
842 }
843
844 /**
845 * Output the author display-name of a topic or reply.
846 *
847 * Convenience function to ensure proper template functions are called
848 * and correct filters are executed. Used primarily to display topic
849 * and reply author information in the anonymous form template-part.
850 *
851 * @since 2.5.0 bbPress (r5119)
852 *
853 * @param int $post_id
854 */
855 function bbp_author_display_name( $post_id = 0 ) {
856 echo esc_attr( bbp_get_author_display_name( $post_id ) );
857 }
858
859 /**
860 * Return the author display-name of a topic or reply.
861 *
862 * Convenience function to ensure proper template functions are called
863 * and correct filters are executed. Used primarily to display topic
864 * and reply author information in the anonymous form template-part.
865 *
866 * @since 2.5.0 bbPress (r5119)
867 *
868 * @param int $post_id
869 *
870 * @return string The name of the author
871 */
872 function bbp_get_author_display_name( $post_id = 0 ) {
873
874 // Define local variable(s)
875 $retval = '';
876
877 // Topic edit
878 if ( bbp_is_topic_edit() ) {
879 $retval = bbp_get_topic_author_display_name( $post_id );
880
881 // Reply edit
882 } elseif ( bbp_is_reply_edit() ) {
883 $retval = bbp_get_reply_author_display_name( $post_id );
884
885 // Not an edit, so rely on current user cookie data
886 } else {
887 $retval = bbp_get_current_anonymous_user_data( 'name' );
888 }
889
890 // Filter & return
891 return apply_filters( 'bbp_get_author_display_name', $retval, $post_id );
892 }
893
894 /**
895 * Output the author email of a topic or reply.
896 *
897 * Convenience function to ensure proper template functions are called
898 * and correct filters are executed. Used primarily to display topic
899 * and reply author information in the anonymous user form template-part.
900 *
901 * @since 2.5.0 bbPress (r5119)
902 *
903 * @param int $post_id
904 */
905 function bbp_author_email( $post_id = 0 ) {
906 echo esc_attr( bbp_get_author_email( $post_id ) );
907 }
908
909 /**
910 * Return the author email of a topic or reply.
911 *
912 * Convenience function to ensure proper template functions are called
913 * and correct filters are executed. Used primarily to display topic
914 * and reply author information in the anonymous user form template-part.
915 *
916 * @since 2.5.0 bbPress (r5119)
917 *
918 * @param int $post_id
919 *
920 * @return string The email of the author
921 */
922 function bbp_get_author_email( $post_id = 0 ) {
923
924 // Define local variable(s)
925 $retval = '';
926
927 // Topic edit
928 if ( bbp_is_topic_edit() ) {
929 $retval = bbp_get_topic_author_email( $post_id );
930
931 // Reply edit
932 } elseif ( bbp_is_reply_edit() ) {
933 $retval = bbp_get_reply_author_email( $post_id );
934
935 // Not an edit, so rely on current user cookie data
936 } else {
937 $retval = bbp_get_current_anonymous_user_data( 'email' );
938 }
939
940 // Filter & return
941 return apply_filters( 'bbp_get_author_email', $retval, $post_id );
942 }
943
944 /**
945 * Output the author url of a topic or reply.
946 *
947 * Convenience function to ensure proper template functions are called
948 * and correct filters are executed. Used primarily to display topic
949 * and reply author information in the anonymous user form template-part.
950 *
951 * @since 2.5.0 bbPress (r5119)
952 *
953 * @param int $post_id
954 */
955 function bbp_author_url( $post_id = 0 ) {
956 echo esc_attr( bbp_get_author_url( $post_id ) );
957 }
958
959 /**
960 * Return the author url of a topic or reply.
961 *
962 * Convenience function to ensure proper template functions are called
963 * and correct filters are executed. Used primarily to display topic
964 * and reply author information in the anonymous user form template-part.
965 *
966 * @since 2.5.0 bbPress (r5119)
967 *
968 * @param int $post_id
969 *
970 * @return string The url of the author
971 */
972 function bbp_get_author_url( $post_id = 0 ) {
973
974 // Define local variable(s)
975 $retval = '';
976
977 // Topic edit
978 if ( bbp_is_topic_edit() ) {
979 $retval = bbp_get_topic_author_url( $post_id );
980
981 // Reply edit
982 } elseif ( bbp_is_reply_edit() ) {
983 $retval = bbp_get_reply_author_url( $post_id );
984
985 // Not an edit, so rely on current user cookie data
986 } else {
987 $retval = bbp_get_current_anonymous_user_data( 'url' );
988 }
989
990 // Filter & return
991 return apply_filters( 'bbp_get_author_url', $retval, $post_id );
992 }
993
994 /** Favorites *****************************************************************/
995
996 /**
997 * Output the link to the user's favorites page (profile page)
998 *
999 * @since 2.0.0 bbPress (r2652)
1000 * @since 2.6.0 bbPress (r6308) Add pagination if in the loop
1001 *
1002 * @param int $user_id Optional. User id
1003 */
1004 function bbp_favorites_permalink( $user_id = 0 ) {
1005 echo esc_url( bbp_get_favorites_permalink( $user_id ) );
1006 }
1007 /**
1008 * Return the link to the user's favorites page (profile page)
1009 *
1010 * @since 2.0.0 bbPress (r2652)
1011 * @since 2.6.0 bbPress (r6308) Add pagination if in the loop
1012 *
1013 * @param int $user_id Optional. User id
1014 * @return string Permanent link to user profile page
1015 */
1016 function bbp_get_favorites_permalink( $user_id = 0 ) {
1017
1018 // Use displayed user ID if there is one, and one isn't requested
1019 $user_id = bbp_get_user_id( $user_id );
1020 if ( empty( $user_id ) ) {
1021 return false;
1022 }
1023
1024 // Bail if intercepted
1025 $intercept = bbp_maybe_intercept( 'bbp_pre_get_favorites_permalink', func_get_args() );
1026 if ( bbp_is_intercepted( $intercept ) ) {
1027 return $intercept;
1028 }
1029
1030 // Get user profile URL & page
1031 $profile_url = bbp_get_user_profile_url( $user_id );
1032 $page = (int) bbpress()->topic_query->paged;
1033 $paged = (bool) bbpress()->topic_query->in_the_loop;
1034
1035 // Pretty permalinks
1036 if ( bbp_use_pretty_urls() ) {
1037
1038 // Base URL
1039 $url = trailingslashit( $profile_url ) . bbp_get_user_favorites_slug();
1040
1041 // Add page
1042 if ( ( true === $paged ) && ( $page > 1 ) ) {
1043 $url = trailingslashit( $url ) . bbp_get_paged_slug() . '/' . $page;
1044 }
1045
1046 // Ensure correct trailing slash
1047 $url = user_trailingslashit( $url );
1048
1049 // Unpretty permalinks
1050 } else {
1051
1052 // Base arguments
1053 $args = array(
1054 bbp_get_user_favorites_rewrite_id() => bbp_get_user_favorites_slug(),
1055 );
1056
1057 // Add page
1058 if ( ( true === $paged ) && ( $page > 1 ) ) {
1059 $args['page'] = $page;
1060 }
1061
1062 // Add arguments
1063 $url = add_query_arg( $args, $profile_url );
1064 }
1065
1066 // Filter & return
1067 return apply_filters( 'bbp_get_favorites_permalink', $url, $user_id );
1068 }
1069
1070 /**
1071 * Output the link to make a topic favorite/remove a topic from favorites
1072 *
1073 * @since 2.0.0 bbPress (r2652)
1074 * @since 2.6.0 bbPress (r6308) Add 'redirect_to' support
1075 *
1076 * @param array $args See {@link bbp_get_user_favorites_link()}
1077 * @param int $user_id Optional. User id
1078 * @param bool $wrap Optional. If you want to wrap the link in <span id="favorite-toggle">.
1079 */
1080 function bbp_user_favorites_link( $args = array(), $user_id = 0, $wrap = true ) {
1081 echo bbp_get_user_favorites_link( $args, $user_id, $wrap );
1082 }
1083 /**
1084 * User favorites link
1085 *
1086 * Return the link to make a topic favorite/remove a topic from
1087 * favorites
1088 *
1089 * @since 2.0.0 bbPress (r2652)
1090 * @since 2.6.0 bbPress (r6308) Add 'redirect_to' support
1091 *
1092 * @param array $args This function supports these arguments:
1093 * - subscribe: Favorite text
1094 * - unsubscribe: Unfavorite text
1095 * - user_id: User id
1096 * - topic_id: Topic id
1097 * - before: Before the link
1098 * - after: After the link
1099 * @param int $user_id Optional. User id
1100 * @param int $topic_id Optional. Topic id
1101 * @param bool $wrap Optional. If you want to wrap the link in <span id="favorite-toggle">. See ajax_favorite()
1102 * @return string User favorites link
1103 */
1104 function bbp_get_user_favorites_link( $args = array(), $user_id = 0, $wrap = true ) {
1105
1106 // Bail if favorites are inactive
1107 if ( ! bbp_is_favorites_active() ) {
1108 return false;
1109 }
1110
1111 // Parse arguments against default values
1112 $r = bbp_parse_args( $args, array(
1113 'favorite' => esc_html__( 'Favorite', 'bbpress' ),
1114 'favorited' => esc_html__( 'Unfavorite', 'bbpress' ),
1115 'user_id' => 0,
1116 'object_id' => 0,
1117 'object_type' => 'post',
1118 'before' => '',
1119 'after' => '',
1120 'redirect_to' => '',
1121
1122 // Deprecated. Use object_id.
1123 'forum_id' => 0,
1124 'topic_id' => 0
1125 ), 'get_user_favorites_link' );
1126
1127 // Validate user and object ID's
1128 $user_id = bbp_get_user_id( $r['user_id'], true, true );
1129 $object_type = sanitize_key( $r['object_type'] );
1130
1131 // Back-compat for deprecated arguments
1132 if ( ! empty( $r['topic_id'] ) ) {
1133 $object_id = absint( $r['topic_id'] );
1134 } elseif ( ! empty( $r['forum_id'] ) ) {
1135 $object_id = absint( $r['forum_id'] );
1136 } else {
1137 $object_id = absint( $r['object_id'] );
1138 }
1139
1140 // Bail if empty
1141 if ( empty( $user_id ) || empty( $object_id ) || empty( $object_type ) ) {
1142 return false;
1143 }
1144
1145 // No link if you can't edit yourself
1146 if ( ! current_user_can( 'edit_user', $user_id ) ) {
1147 return false;
1148 }
1149
1150 // Decide which link to show
1151 $is_fav = bbp_is_user_favorite( $user_id, $object_id );
1152 if ( ! empty( $is_fav ) ) {
1153 $text = $r['favorited'];
1154 $q_args = array(
1155 'action' => 'bbp_favorite_remove',
1156 'object_id' => $object_id
1157 );
1158 } else {
1159 $text = $r['favorite'];
1160 $q_args = array(
1161 'action' => 'bbp_favorite_add',
1162 'object_id' => $object_id
1163 );
1164 }
1165
1166 // Custom redirect
1167 if ( ! empty( $r['redirect_to'] ) ) {
1168 $q_args['redirect_to'] = urlencode( $r['redirect_to'] );
1169 }
1170
1171 // URL
1172 $url = esc_url( wp_nonce_url( add_query_arg( $q_args ), 'toggle-favorite_' . $object_id ) );
1173 $sub = $is_fav ? ' class="is-favorite"' : '';
1174 $html = sprintf( '%s<span id="favorite-%d" %s><a href="%s" class="favorite-toggle" data-bbp-object-id="%d" data-bbp-object-type="%s" data-bbp-nonce="%s">%s</a></span>%s', $r['before'], $object_id, $sub, $url, $object_id, $object_type, wp_create_nonce( 'toggle-favorite_' . $object_id ), $text, $r['after'] );
1175
1176 // Initial output is wrapped in a span, ajax output is hooked to this
1177 if ( ! empty( $wrap ) ) {
1178 $html = '<span id="favorite-toggle">' . $html . '</span>';
1179 }
1180
1181 // Filter & return
1182 return apply_filters( 'bbp_get_user_favorites_link', $html, $r, $user_id, $object_id );
1183 }
1184
1185 /** Subscriptions *************************************************************/
1186
1187 /**
1188 * Output the link to the user's subscriptions page (profile page)
1189 *
1190 * @since 2.0.0 bbPress (r2688)
1191 * @since 2.6.0 bbPress (r6308) Add pagination if in the loop
1192 *
1193 * @param int $user_id Optional. User id
1194 */
1195 function bbp_subscriptions_permalink( $user_id = 0 ) {
1196 echo esc_url( bbp_get_subscriptions_permalink( $user_id ) );
1197 }
1198 /**
1199 * Return the link to the user's subscriptions page (profile page)
1200 *
1201 * @since 2.0.0 bbPress (r2688)
1202 * @since 2.6.0 bbPress (r6308) Add pagination if in the loop
1203 *
1204 * @param int $user_id Optional. User id
1205 * @return string Permanent link to user subscriptions page
1206 */
1207 function bbp_get_subscriptions_permalink( $user_id = 0 ) {
1208
1209 // Use displayed user ID if there is one, and one isn't requested
1210 $user_id = bbp_get_user_id( $user_id );
1211 if ( empty( $user_id ) ) {
1212 return false;
1213 }
1214
1215 // Bail if intercepted
1216 $intercept = bbp_maybe_intercept( 'bbp_pre_get_subscriptions_permalink', func_get_args() );
1217 if ( bbp_is_intercepted( $intercept ) ) {
1218 return $intercept;
1219 }
1220
1221 // Get user profile URL
1222 $profile_url = bbp_get_user_profile_url( $user_id );
1223 $page = 0;
1224 $paged = false;
1225
1226 // Get pagination data
1227 if ( bbpress()->topic_query->in_the_loop ) {
1228 $page = (int) bbpress()->topic_query->paged;
1229 $paged = (bool) bbpress()->topic_query->in_the_loop;
1230
1231 } elseif ( bbpress()->forum_query->in_the_loop ) {
1232 $page = (int) bbpress()->forum_query->paged;
1233 $paged = (bool) bbpress()->forum_query->in_the_loop;
1234 }
1235
1236 // Pretty permalinks
1237 if ( bbp_use_pretty_urls() ) {
1238
1239 // Base URL
1240 $url = trailingslashit( $profile_url ) . bbp_get_user_subscriptions_slug();
1241
1242 // Add page
1243 if ( ( true === $paged ) && ( $page > 1 ) ) {
1244 $url = trailingslashit( $url ) . bbp_get_paged_slug() . '/' . $page;
1245 }
1246
1247 // Ensure correct trailing slash
1248 $url = user_trailingslashit( $url );
1249
1250 // Unpretty permalinks
1251 } else {
1252
1253 // Base arguments
1254 $args = array(
1255 bbp_get_user_subscriptions_rewrite_id() => bbp_get_user_subscriptions_slug(),
1256 );
1257
1258 // Add page
1259 if ( ( true === $paged ) && ( $page > 1 ) ) {
1260 $args['page'] = $page;
1261 }
1262
1263 // Add arguments
1264 $url = add_query_arg( $args, $profile_url );
1265 }
1266
1267 // Filter & return
1268 return apply_filters( 'bbp_get_subscriptions_permalink', $url, $user_id );
1269 }
1270
1271 /**
1272 * Output the link to subscribe/unsubscribe from a topic
1273 *
1274 * @since 2.0.0 bbPress (r2668)
1275 * @since 2.6.0 bbPress (r6308) Add 'redirect_to' support
1276 *
1277 * @param array $args See {@link bbp_get_user_subscribe_link()}
1278 * @param int $user_id Optional. User id
1279 * @param bool $wrap Optional. If you want to wrap the link in <span id="subscription-toggle">.
1280 */
1281 function bbp_user_subscribe_link( $args = array(), $user_id = 0, $wrap = true ) {
1282 echo bbp_get_user_subscribe_link( $args, $user_id, $wrap );
1283 }
1284 /**
1285 * Return the link to subscribe/unsubscribe from a forum or topic
1286 *
1287 * @since 2.0.0 bbPress (r2668)
1288 * @since 2.6.0 bbPress (r6308) Add 'redirect_to' support
1289 *
1290 * @param array $args This function supports these arguments:
1291 * - subscribe: Subscribe text
1292 * - unsubscribe: Unsubscribe text
1293 * - user_id: User id
1294 * - topic_id: Topic id
1295 * - forum_id: Forum id
1296 * - before: Before the link
1297 * - after: After the link
1298 * @param int $user_id Optional. User id
1299 * @param bool $wrap Optional. If you want to wrap the link in <span id="subscription-toggle">.
1300 * @return string Permanent link to topic
1301 */
1302 function bbp_get_user_subscribe_link( $args = array(), $user_id = 0, $wrap = true ) {
1303
1304 // Bail if subscriptions are inactive
1305 if ( ! bbp_is_subscriptions_active() ) {
1306 return;
1307 }
1308
1309 // Parse arguments against default values
1310 $r = bbp_parse_args( $args, array(
1311 'subscribe' => esc_html__( 'Subscribe', 'bbpress' ),
1312 'unsubscribe' => esc_html__( 'Unsubscribe', 'bbpress' ),
1313 'user_id' => 0,
1314 'object_id' => 0,
1315 'object_type' => 'post',
1316 'before' => '',
1317 'after' => '',
1318 'redirect_to' => '',
1319
1320 // Deprecated. Use object_id.
1321 'forum_id' => 0,
1322 'topic_id' => 0
1323 ), 'get_user_subscribe_link' );
1324
1325 // Validate user
1326 $user_id = bbp_get_user_id( $r['user_id'], true, true );
1327 $object_type = sanitize_key( $r['object_type'] );
1328
1329 // Back-compat for deprecated arguments
1330 if ( ! empty( $r['topic_id'] ) ) {
1331 $object_id = absint( $r['topic_id'] );
1332 } elseif ( ! empty( $r['forum_id'] ) ) {
1333 $object_id = absint( $r['forum_id'] );
1334 } else {
1335 $object_id = absint( $r['object_id'] );
1336 }
1337
1338 // Bail if anything is missing
1339 if ( empty( $user_id ) || empty( $object_id ) || empty( $object_type ) ) {
1340 return false;
1341 }
1342
1343 // No link if you can't edit yourself
1344 if ( ! current_user_can( 'edit_user', $user_id ) ) {
1345 return false;
1346 }
1347
1348 // Decide which link to show
1349 $is_subscribed = bbp_is_user_subscribed( $user_id, $object_id );
1350 if ( ! empty( $is_subscribed ) ) {
1351 $text = $r['unsubscribe'];
1352 $q_args = array(
1353 'action' => 'bbp_unsubscribe',
1354 'object_id' => $object_id,
1355 'object_type' => $object_type
1356 );
1357 } else {
1358 $text = $r['subscribe'];
1359 $q_args = array(
1360 'action' => 'bbp_subscribe',
1361 'object_id' => $object_id,
1362 'object_type' => $object_type
1363 );
1364 }
1365
1366 // Custom redirect
1367 if ( ! empty( $r['redirect_to'] ) ) {
1368 $q_args['redirect_to'] = urlencode( $r['redirect_to'] );
1369 }
1370
1371 // URL
1372 $url = esc_url( wp_nonce_url( add_query_arg( $q_args ), 'toggle-subscription_' . $object_id ) );
1373 $sub = $is_subscribed ? ' class="is-subscribed"' : '';
1374 $html = sprintf( '%s<span id="subscribe-%d" %s><a href="%s" class="subscription-toggle" data-bbp-object-id="%d" data-bbp-object-type="%d" data-bbp-nonce="%s">%s</a></span>%s', $r['before'], $object_id, $sub, $url, $object_id, $object_type, wp_create_nonce( 'toggle-subscription_' . $object_id ), $text, $r['after'] );
1375
1376 // Initial output is wrapped in a span, ajax output is hooked to this
1377 if ( ! empty( $wrap ) ) {
1378 $html = '<span id="subscription-toggle">' . $html . '</span>';
1379 }
1380
1381 // Filter & return
1382 return apply_filters( 'bbp_get_user_subscribe_link', $html, $r, $user_id, $object_id );
1383 }
1384
1385
1386 /** Edit User *****************************************************************/
1387
1388 /**
1389 * Display profile edit success notice on user edit page
1390 *
1391 * @since 2.0.0 bbPress (r2688)
1392 */
1393 function bbp_notice_edit_user_success() {
1394
1395 // Bail if no updated argument
1396 if ( empty( $_GET['updated'] ) ) {
1397 return;
1398 }
1399
1400 // Bail if not on users own profile
1401 if ( ! bbp_is_single_user_edit() ) {
1402 return;
1403 } ?>
1404
1405 <div class="bbp-template-notice">
1406 <ul>
1407 <li><?php esc_html_e( 'User updated.', 'bbpress' ); ?></li>
1408 </ul>
1409 </div>
1410
1411 <?php
1412 }
1413
1414 /**
1415 * Display pending email change notice on user edit page
1416 *
1417 * @since 2.6.0 bbPress (r5660)
1418 */
1419 function bbp_notice_edit_user_pending_email() {
1420
1421 // Bail if not on users own profile
1422 if ( ! bbp_is_user_home_edit() ) {
1423 return;
1424 }
1425
1426 // Check for pending email address change
1427 $user_id = bbp_get_displayed_user_id();
1428 $key = '_new_email';
1429 $new_email = get_user_meta( $user_id, $key, true );
1430
1431 // Bail if no pending email address change
1432 if ( empty( $new_email['newemail'] ) ) {
1433 return;
1434 }
1435
1436 // Build the nonced URL to dismiss the pending change
1437 $user_url = bbp_get_user_profile_edit_url( $user_id );
1438 $nonce = "dismiss-{$user_id}{$key}";
1439 $args = array(
1440 'action' => 'bbp-update-user-email',
1441 'dismiss' => "{$user_id}{$key}"
1442 );
1443
1444 // Build the variables to pass into printf()
1445 $dismiss_url = wp_nonce_url( add_query_arg( $args, $user_url ), $nonce );
1446 $dismiss_link = '<a href="' . esc_url( $dismiss_url ) . '">' . esc_html_x( 'Cancel', 'Dismiss pending user email address change', 'bbpress' ) . '</a>';
1447 $coded_email = '<code>' . esc_html( $new_email['newemail'] ) . '</code>'; ?>
1448
1449 <div class="bbp-template-notice info">
1450 <ul>
1451 <li><?php printf( esc_html__( 'There is a pending email address change to %1$s. %2$s', 'bbpress' ), $coded_email, $dismiss_link ); ?></li>
1452 </ul>
1453 </div>
1454
1455 <?php
1456 }
1457
1458 /**
1459 * Super admin privileges notice
1460 *
1461 * @since 2.0.0 bbPress (r2688)
1462 */
1463 function bbp_notice_edit_user_is_super_admin() {
1464 if ( is_multisite() && ( bbp_is_single_user() || bbp_is_single_user_edit() ) && current_user_can( 'manage_network_options' ) && is_super_admin( bbp_get_displayed_user_id() ) ) : ?>
1465
1466 <div class="bbp-template-notice important">
1467 <ul>
1468 <li><?php bbp_is_user_home() || bbp_is_user_home_edit() ? esc_html_e( 'You have super admin privileges.', 'bbpress' ) : esc_html_e( 'This user has super admin privileges.', 'bbpress' ); ?></li>
1469 </ul>
1470 </div>
1471
1472 <?php endif;
1473 }
1474
1475 /**
1476 * Drop down for selecting the user's display name
1477 *
1478 * @since 2.0.0 bbPress (r2688)
1479 */
1480 function bbp_edit_user_display_name() {
1481 $bbp = bbpress();
1482 $public_display = array();
1483 $public_display['display_username'] = $bbp->displayed_user->user_login;
1484
1485 if ( ! empty( $bbp->displayed_user->nickname ) ) {
1486 $public_display['display_nickname'] = $bbp->displayed_user->nickname;
1487 }
1488
1489 if ( ! empty( $bbp->displayed_user->first_name ) ) {
1490 $public_display['display_firstname'] = $bbp->displayed_user->first_name;
1491 }
1492
1493 if ( ! empty( $bbp->displayed_user->last_name ) ) {
1494 $public_display['display_lastname'] = $bbp->displayed_user->last_name;
1495 }
1496
1497 if ( ! empty( $bbp->displayed_user->first_name ) && ! empty( $bbp->displayed_user->last_name ) ) {
1498 $public_display['display_firstlast'] = $bbp->displayed_user->first_name . ' ' . $bbp->displayed_user->last_name;
1499 $public_display['display_lastfirst'] = $bbp->displayed_user->last_name . ' ' . $bbp->displayed_user->first_name;
1500 }
1501
1502 // Only add this if it isn't duplicated elsewhere
1503 if ( ! in_array( $bbp->displayed_user->display_name, $public_display, true ) ) {
1504 $public_display = array( 'display_displayname' => $bbp->displayed_user->display_name ) + $public_display;
1505 }
1506
1507 $public_display = array_map( 'trim', $public_display );
1508 $public_display = array_unique( $public_display ); ?>
1509
1510 <select name="display_name" id="display_name">
1511
1512 <?php foreach ( $public_display as $id => $item ) : ?>
1513
1514 <option id="<?php echo $id; ?>" value="<?php echo esc_attr( $item ); ?>"<?php selected( $bbp->displayed_user->display_name, $item ); ?>><?php echo $item; ?></option>
1515
1516 <?php endforeach; ?>
1517
1518 </select>
1519
1520 <?php
1521 }
1522
1523 /**
1524 * Output blog role selector (for user edit)
1525 *
1526 * @since 2.0.0 bbPress (r2688)
1527 */
1528 function bbp_edit_user_blog_role() {
1529
1530 // Bail if no user is being edited
1531 if ( ! bbp_is_single_user_edit() ) {
1532 return;
1533 }
1534
1535 // Get users current blog role
1536 $user_role = bbp_get_user_blog_role( bbp_get_displayed_user_id() );
1537
1538 // Get the blog roles
1539 $blog_roles = bbp_get_blog_roles(); ?>
1540
1541 <select name="role" id="role">
1542 <option value=""><?php esc_html_e( '&mdash; No role for this site &mdash;', 'bbpress' ); ?></option>
1543
1544 <?php foreach ( $blog_roles as $role => $details ) : ?>
1545
1546 <option <?php selected( $user_role, $role ); ?> value="<?php echo esc_attr( $role ); ?>"><?php echo bbp_translate_user_role( $details['name'] ); ?></option>
1547
1548 <?php endforeach; ?>
1549
1550 </select>
1551
1552 <?php
1553 }
1554
1555 /**
1556 * Output forum role selector (for user edit)
1557 *
1558 * @since 2.2.0 bbPress (r4284)
1559 */
1560 function bbp_edit_user_forums_role() {
1561
1562 // Bail if no user is being edited
1563 if ( ! bbp_is_single_user_edit() ) {
1564 return;
1565 }
1566
1567 // Get the user's current forum role
1568 $user_role = bbp_get_user_role( bbp_get_displayed_user_id() );
1569
1570 // Get the folum roles
1571 $dynamic_roles = bbp_get_dynamic_roles();
1572
1573 // Only keymasters can set other keymasters
1574 if ( ! bbp_is_user_keymaster() ) {
1575 unset( $dynamic_roles[ bbp_get_keymaster_role() ] );
1576 } ?>
1577
1578 <select name="bbp-forums-role" id="bbp-forums-role">
1579 <option value=""><?php esc_html_e( '&mdash; No role for these forums &mdash;', 'bbpress' ); ?></option>
1580
1581 <?php foreach ( $dynamic_roles as $role => $details ) : ?>
1582
1583 <option <?php selected( $user_role, $role ); ?> value="<?php echo esc_attr( $role ); ?>"><?php echo bbp_translate_user_role( $details['name'] ); ?></option>
1584
1585 <?php endforeach; ?>
1586
1587 </select>
1588
1589 <?php
1590 }
1591
1592 /**
1593 * Return user contact methods select box
1594 *
1595 * @since 2.0.0 bbPress (r2688)
1596 * @return string User contact methods
1597 */
1598 function bbp_edit_user_contact_methods() {
1599
1600 // Get the core WordPress contact methods
1601 $contact_methods = wp_get_user_contact_methods( bbpress()->displayed_user );
1602
1603 // Filter & return
1604 return (array) apply_filters( 'bbp_edit_user_contact_methods', $contact_methods );
1605 }
1606
1607 /**
1608 * Output the language chooser (for user edit)
1609 *
1610 * @since 2.6.0 bbPress (r6488)
1611 *
1612 * @param array $args See wp_dropdown_languages()
1613 * @return string
1614 */
1615 function bbp_edit_user_language( $args = array() ) {
1616
1617 // Bail if no user is being edited
1618 if ( ! bbp_is_single_user_edit() ) {
1619 return;
1620 }
1621
1622 // Output the dropdown
1623 bbp_user_languages_dropdown( $args );
1624 }
1625
1626 /** Requests ******************************************************************/
1627
1628 /**
1629 * Verify if a POST request came from a user profile edit form.
1630 *
1631 * Used to avoid cross-site request forgeries when checking posted user profile
1632 * form content.
1633 *
1634 * @since 2.6.15 bbPress (r7397)
1635 *
1636 * @param int $user_id
1637 *
1638 * @return bool True if this is a user profile post request with valid nonce
1639 */
1640 function bbp_is_user_profile_form_post_request( $user_id = 0 ) {
1641
1642 // Bail if no user ID was passed
1643 if ( empty( $user_id ) ) {
1644 return false;
1645 }
1646
1647 // Bail if not a post request
1648 if ( ! bbp_is_post_request() ) {
1649 return false;
1650 }
1651
1652 // Build the nonce action string
1653 $action = 'update-user_' . $user_id;
1654
1655 // Editing an existing user
1656 if ( bbp_verify_nonce_request( $action ) ) {
1657 return true;
1658 }
1659
1660 // Nonce fallback for admin profile requests where URL validation can vary
1661 if ( isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], $action ) ) {
1662 return true;
1663 }
1664
1665 return false;
1666 }
1667
1668 /** Topics Created ************************************************************/
1669
1670 /**
1671 * Output the link to the user's topics
1672 *
1673 * @since 2.2.0 bbPress (r4225)
1674 *
1675 * @param int $user_id Optional. User id
1676 */
1677 function bbp_user_topics_created_url( $user_id = 0 ) {
1678 echo esc_url( bbp_get_user_topics_created_url( $user_id ) );
1679 }
1680 /**
1681 * Return the link to the user's topics
1682 *
1683 * @since 2.2.0 bbPress (r4225)
1684 *
1685 * @param int $user_id Optional. User id
1686 * @return string Permanent link to user profile page
1687 */
1688 function bbp_get_user_topics_created_url( $user_id = 0 ) {
1689
1690 // Use displayed user ID if there is one, and one isn't requested
1691 $user_id = bbp_get_user_id( $user_id );
1692 if ( empty( $user_id ) ) {
1693 return false;
1694 }
1695
1696 // Bail if intercepted
1697 $intercept = bbp_maybe_intercept( 'bbp_pre_get_user_topics_created_url', func_get_args() );
1698 if ( bbp_is_intercepted( $intercept ) ) {
1699 return $intercept;
1700 }
1701
1702 // Get user profile URL
1703 $profile_url = bbp_get_user_profile_url( $user_id );
1704
1705 // Pretty permalinks
1706 if ( bbp_use_pretty_urls() ) {
1707 $url = trailingslashit( $profile_url ) . bbp_get_topic_archive_slug();
1708 $url = user_trailingslashit( $url );
1709
1710 // Unpretty permalinks
1711 } else {
1712 $url = add_query_arg( array(
1713 bbp_get_user_topics_rewrite_id() => '1',
1714 ), $profile_url );
1715 }
1716
1717 // Filter & return
1718 return apply_filters( 'bbp_get_user_topics_created_url', $url, $user_id );
1719 }
1720
1721 /** Replies Created ************************************************************/
1722
1723 /**
1724 * Output the link to the user's replies
1725 *
1726 * @since 2.2.0 bbPress (r4225)
1727 *
1728 * @param int $user_id Optional. User id
1729 */
1730 function bbp_user_replies_created_url( $user_id = 0 ) {
1731 echo esc_url( bbp_get_user_replies_created_url( $user_id ) );
1732 }
1733 /**
1734 * Return the link to the user's replies
1735 *
1736 * @since 2.2.0 bbPress (r4225)
1737 *
1738 * @param int $user_id Optional. User id
1739 * @return string Permanent link to user profile page
1740 */
1741 function bbp_get_user_replies_created_url( $user_id = 0 ) {
1742
1743 // Use displayed user ID if there is one, and one isn't requested
1744 $user_id = bbp_get_user_id( $user_id );
1745 if ( empty( $user_id ) ) {
1746 return false;
1747 }
1748
1749 // Bail if intercepted
1750 $intercept = bbp_maybe_intercept( 'bbp_pre_get_user_replies_created_url', func_get_args() );
1751 if ( bbp_is_intercepted( $intercept ) ) {
1752 return $intercept;
1753 }
1754
1755 // Get user profile URL
1756 $profile_url = bbp_get_user_profile_url( $user_id );
1757
1758 // Pretty permalinks
1759 if ( bbp_use_pretty_urls() ) {
1760 $url = trailingslashit( $profile_url ) . bbp_get_reply_archive_slug();
1761 $url = user_trailingslashit( $url );
1762
1763 // Unpretty permalinks
1764 } else {
1765 $url = add_query_arg( array(
1766 bbp_get_user_replies_rewrite_id() => '1',
1767 ), $profile_url );
1768 }
1769
1770 // Filter & return
1771 return apply_filters( 'bbp_get_user_replies_created_url', $url, $user_id );
1772 }
1773
1774 /** Engagements ***************************************************************/
1775
1776 /**
1777 * Output the link to the user's engagements
1778 *
1779 * @since 2.6.0 bbPress (r6320)
1780 *
1781 * @param int $user_id Optional. User id
1782 */
1783 function bbp_user_engagements_url( $user_id = 0 ) {
1784 echo esc_url( bbp_get_user_engagements_url( $user_id ) );
1785 }
1786 /**
1787 * Return the link to the user's engagements
1788 *
1789 * @since 2.6.0 bbPress (r6320)
1790 *
1791 * @param int $user_id Optional. User id
1792 * @return string Permanent link to user profile page
1793 */
1794 function bbp_get_user_engagements_url( $user_id = 0 ) {
1795
1796 // Use displayed user ID if there is one, and one isn't requested
1797 $user_id = bbp_get_user_id( $user_id );
1798 if ( empty( $user_id ) ) {
1799 return false;
1800 }
1801
1802 // Bail if intercepted
1803 $intercept = bbp_maybe_intercept( 'bbp_pre_get_user_engagements_url', func_get_args() );
1804 if ( bbp_is_intercepted( $intercept ) ) {
1805 return $intercept;
1806 }
1807
1808 // Get user profile URL
1809 $profile_url = bbp_get_user_profile_url( $user_id );
1810
1811 // Pretty permalinks
1812 if ( bbp_use_pretty_urls() ) {
1813 $url = trailingslashit( $profile_url ) . bbp_get_user_engagements_slug();
1814 $url = user_trailingslashit( $url );
1815
1816 // Unpretty permalinks
1817 } else {
1818 $url = add_query_arg( array(
1819 bbp_get_user_engagements_rewrite_id() => '1',
1820 ), $profile_url );
1821 }
1822
1823 // Filter & return
1824 return apply_filters( 'bbp_get_user_engagements_url', $url, $user_id );
1825 }
1826
1827 /** Language ******************************************************************/
1828
1829 /**
1830 * Output the select element used to save a user's language
1831 *
1832 * @since 2.6.0 bbPress (r6488)
1833 *
1834 * @param array $args See wp_dropdown_languages()
1835 */
1836 function bbp_user_languages_dropdown( $args = array() ) {
1837 echo bbp_get_user_languages_dropdown( $args );
1838 }
1839
1840 /**
1841 * Return the select element used to save a user's language.
1842 *
1843 * @since 2.6.0 bbPress (r6488)
1844 *
1845 * @param array $args See wp_dropdown_languages()
1846 * @return string
1847 */
1848 function bbp_get_user_languages_dropdown( $args = array() ) {
1849
1850 // Get user language
1851 $user_id = ! empty( $args['user_id'] )
1852 ? bbp_get_user_id( $args['user_id'], false, false )
1853 : bbp_get_displayed_user_id();
1854
1855 // Get user locale
1856 $user_locale = ! empty( $user_id )
1857 ? get_userdata( $user_id )->locale
1858 : 'site-default';
1859
1860 // Get all languages
1861 $languages = get_available_languages();
1862
1863 // No locale for English
1864 if ( 'en_US' === $user_locale ) {
1865 $user_locale = '';
1866
1867 // Fallback to site-default if there is a mismatch
1868 } elseif ( '' === $user_locale || ! in_array( $user_locale, $languages, true ) ) {
1869 $user_locale = 'site-default';
1870 }
1871
1872 // Don't pass user ID in
1873 unset( $args['user_id'] );
1874
1875 // Parse arguments
1876 $r = bbp_parse_args( $args, array(
1877 'name' => 'locale',
1878 'id' => 'locale',
1879 'selected' => $user_locale,
1880 'languages' => $languages,
1881 'echo' => false,
1882 'show_available_translations' => false,
1883 'show_option_site_default' => true
1884 ), 'user_languages_dropdown' );
1885
1886 // Get the markup for the languages drop-down
1887 $retval = wp_dropdown_languages( $r );
1888
1889 // Filter & return
1890 return apply_filters( 'bbp_get_user_languages_dropdown', $retval, $r, $args );
1891 }
1892
1893 /** Login *********************************************************************/
1894
1895 /**
1896 * Handle the login and registration template notices
1897 *
1898 * @since 2.0.0 bbPress (r2970)
1899 */
1900 function bbp_login_notices() {
1901
1902 // loggedout was passed
1903 if ( ! empty( $_GET['loggedout'] ) && ( 'true' === $_GET['loggedout'] ) ) {
1904 bbp_add_error( 'loggedout', esc_html__( 'You are now logged out.', 'bbpress' ), 'message' );
1905
1906 // registration is disabled
1907 } elseif ( ! empty( $_GET['registration'] ) && ( 'disabled' === $_GET['registration'] ) ) {
1908 bbp_add_error( 'registerdisabled', esc_html__( 'New user registration is currently not allowed.', 'bbpress' ) );
1909
1910 // Prompt user to check their email
1911 } elseif ( ! empty( $_GET['checkemail'] ) && in_array( $_GET['checkemail'], array( 'confirm', 'newpass', 'registered' ), true ) ) {
1912
1913 switch ( $_GET['checkemail'] ) {
1914
1915 // Email needs confirmation
1916 case 'confirm' :
1917 bbp_add_error( 'confirm', esc_html__( 'Check your e-mail for the confirmation link.', 'bbpress' ), 'message' );
1918 break;
1919
1920 // User requested a new password
1921 case 'newpass' :
1922 bbp_add_error( 'newpass', esc_html__( 'Check your e-mail for your new password.', 'bbpress' ), 'message' );
1923 break;
1924
1925 // User is newly registered
1926 case 'registered' :
1927 bbp_add_error( 'registered', esc_html__( 'Registration complete. Please check your e-mail.', 'bbpress' ), 'message' );
1928 break;
1929 }
1930 }
1931 }
1932
1933 /**
1934 * Redirect a user back to their profile if they are already logged in.
1935 *
1936 * This should be used before {@link get_header()} is called in template files
1937 * where the user should never have access to the contents of that file.
1938 *
1939 * @since 2.0.0 bbPress (r2815)
1940 *
1941 * @param string $url The URL to redirect to
1942 */
1943 function bbp_logged_in_redirect( $url = '' ) {
1944
1945 // Bail if user is not logged in
1946 if ( ! is_user_logged_in() ) {
1947 return;
1948 }
1949
1950 // Setup the profile page to redirect to
1951 $redirect_to = ! empty( $url ) ? $url : bbp_get_user_profile_url( bbp_get_current_user_id() );
1952
1953 // Do a safe redirect
1954 bbp_redirect( $redirect_to );
1955 }
1956
1957 /**
1958 * Output the required hidden fields when logging in
1959 *
1960 * @since 2.0.0 bbPress (r2815)
1961 */
1962 function bbp_user_login_fields() {
1963 ?>
1964
1965 <input type="hidden" name="user-cookie" value="1" />
1966
1967 <?php
1968
1969 // Allow custom login redirection
1970 $redirect_to = apply_filters( 'bbp_user_login_redirect_to', '' );
1971 bbp_redirect_to_field( $redirect_to );
1972
1973 // Prevent intention hi-jacking of log-in form
1974 wp_nonce_field( 'bbp-user-login' );
1975 }
1976
1977 /** Register ******************************************************************/
1978
1979 /**
1980 * Output the required hidden fields when registering
1981 *
1982 * @since 2.0.0 bbPress (r2815)
1983 */
1984 function bbp_user_register_fields() {
1985 ?>
1986
1987 <input type="hidden" name="action" value="register" />
1988 <input type="hidden" name="user-cookie" value="1" />
1989
1990 <?php
1991
1992 // Allow custom registration redirection
1993 $redirect_to = apply_filters( 'bbp_user_register_redirect_to', '' );
1994 bbp_redirect_to_field( add_query_arg( array( 'checkemail' => 'registered' ), $redirect_to ) );
1995
1996 // Prevent intention hi-jacking of sign-up form
1997 wp_nonce_field( 'bbp-user-register' );
1998 }
1999
2000 /** Lost Password *************************************************************/
2001
2002 /**
2003 * Output the required hidden fields when user lost password
2004 *
2005 * @since 2.0.0 bbPress (r2815)
2006 */
2007 function bbp_user_lost_pass_fields() {
2008 ?>
2009
2010 <input type="hidden" name="user-cookie" value="1" />
2011
2012 <?php
2013
2014 // Allow custom lost pass redirection
2015 $redirect_to = apply_filters( 'bbp_user_lost_pass_redirect_to', get_permalink() );
2016 bbp_redirect_to_field( add_query_arg( array( 'checkemail' => 'confirm' ), $redirect_to ) );
2017
2018 // Prevent intention hi-jacking of lost pass form
2019 wp_nonce_field( 'bbp-user-lost-pass' );
2020 }
2021
2022 /** Author Avatar *************************************************************/
2023
2024 /**
2025 * Output the author link of a post
2026 *
2027 * @since 2.0.0 bbPress (r2875)
2028 *
2029 * @param array $args Optional. If it is an integer, it is used as post id.
2030 */
2031 function bbp_author_link( $args = array() ) {
2032 echo bbp_get_author_link( $args );
2033 }
2034 /**
2035 * Return the author link of the post
2036 *
2037 * @since 2.0.0 bbPress (r2875)
2038 *
2039 * @param array $args Optional. If an integer, it is used as reply id.
2040 * @return string Author link of reply
2041 */
2042 function bbp_get_author_link( $args = array() ) {
2043
2044 $post_id = is_numeric( $args ) ? (int) $args : 0;
2045
2046 // Parse arguments against default values
2047 $r = bbp_parse_args( $args, array(
2048 'post_id' => $post_id,
2049 'link_title' => '',
2050 'type' => 'both',
2051 'size' => 80,
2052 'sep' => ''
2053 ), 'get_author_link' );
2054
2055 // Confirmed topic
2056 if ( bbp_is_topic( $r['post_id'] ) ) {
2057 return bbp_get_topic_author_link( $r );
2058
2059 // Confirmed reply
2060 } elseif ( bbp_is_reply( $r['post_id'] ) ) {
2061 return bbp_get_reply_author_link( $r );
2062 }
2063
2064 // Default return value
2065 $author_link = '';
2066
2067 // Neither a reply nor a topic, so could be a revision
2068 if ( ! empty( $r['post_id'] ) ) {
2069
2070 // Get some useful reply information
2071 $user_id = get_post_field( 'post_author', $r['post_id'] );
2072 $author_url = bbp_get_user_profile_url( $user_id );
2073 $anonymous = bbp_is_reply_anonymous( $r['post_id'] );
2074
2075 // Generate title with the display name of the author
2076 if ( empty( $r['link_title'] ) ) {
2077 $author = get_the_author_meta( 'display_name', $user_id );
2078 $title = empty( $anonymous )
2079 ? esc_attr__( "View %s's profile", 'bbpress' )
2080 : esc_attr__( "Visit %s's website", 'bbpress' );
2081
2082 $r['link_title'] = sprintf( $title, $author );
2083 }
2084
2085 // Setup title and author_links array
2086 $author_links = array();
2087 $link_title = ! empty( $r['link_title'] )
2088 ? ' title="' . esc_attr( $r['link_title'] ) . '"'
2089 : '';
2090
2091 // Get avatar (unescaped, because HTML)
2092 if ( ( 'avatar' === $r['type'] ) || ( 'both' === $r['type'] ) ) {
2093 $author_links['avatar'] = get_avatar( $user_id, $r['size'] );
2094 }
2095
2096 // Get display name (escaped, because never HTML)
2097 if ( ( 'name' === $r['type'] ) || ( 'both' === $r['type'] ) ) {
2098 $author_links['name'] = esc_html( get_the_author_meta( 'display_name', $user_id ) );
2099 }
2100
2101 // Empty array
2102 $links = array();
2103 $sprint = '<span %1$s>%2$s</span>';
2104
2105 // Wrap each link
2106 foreach ( $author_links as $link => $link_text ) {
2107 $link_class = ' class="bbp-author-' . esc_attr( $link ) . '"';
2108 $links[] = sprintf( $sprint, $link_class, $link_text );
2109 }
2110
2111 // Juggle
2112 $author_links = $links;
2113 unset( $links );
2114
2115 // Filter sections
2116 $sections = apply_filters( 'bbp_get_author_links', $author_links, $r, $args );
2117
2118 // Assemble sections into author link
2119 $author_link = implode( $r['sep'], $sections );
2120
2121 // Only wrap in link if profile exists
2122 if ( empty( $anonymous ) && bbp_user_has_profile( $user_id ) ) {
2123 $author_link = sprintf( '<a href="%1$s"%2$s%3$s>%4$s</a>', esc_url( $author_url ), $link_title, ' class="bbp-author-link"', $author_link );
2124 }
2125 }
2126
2127 // Filter & return
2128 return apply_filters( 'bbp_get_author_link', $author_link, $r, $args );
2129 }
2130
2131 /** Capabilities **************************************************************/
2132
2133 /**
2134 * Check if the user can access a specific forum
2135 *
2136 * @since 2.0.0 bbPress (r3127)
2137 *
2138 * @return bool
2139 */
2140 function bbp_user_can_view_forum( $args = array() ) {
2141
2142 // Parse arguments against default values
2143 $r = bbp_parse_args( $args, array(
2144 'user_id' => bbp_get_current_user_id(),
2145 'forum_id' => bbp_get_forum_id(),
2146 'check_ancestors' => false
2147 ), 'user_can_view_forum' );
2148
2149 // Validate parsed values
2150 $user_id = bbp_get_user_id( $r['user_id'], false, false );
2151 $forum_id = bbp_get_forum_id( $r['forum_id'] );
2152 $retval = false;
2153
2154 // User is a keymaster
2155 if ( ! empty( $user_id ) && bbp_is_user_keymaster( $user_id ) ) {
2156 $retval = true;
2157
2158 // Forum is public, and user can read forums or is not logged in
2159 } elseif ( bbp_is_forum_public( $forum_id, $r['check_ancestors'] ) ) {
2160 $retval = true;
2161
2162 // Forum is private, and user can see it
2163 } elseif ( bbp_is_forum_private( $forum_id, $r['check_ancestors'] ) && user_can( $user_id, 'read_forum', $forum_id ) ) {
2164 $retval = true;
2165
2166 // Forum is hidden, and user can see it
2167 } elseif ( bbp_is_forum_hidden( $forum_id, $r['check_ancestors'] ) && user_can( $user_id, 'read_forum', $forum_id ) ) {
2168 $retval = true;
2169 }
2170
2171 // Filter & return
2172 return apply_filters( 'bbp_user_can_view_forum', $retval, $forum_id, $user_id );
2173 }
2174
2175 /**
2176 * Check if the current user can publish topics
2177 *
2178 * @since 2.0.0 bbPress (r3127)
2179 *
2180 * @return bool
2181 */
2182 function bbp_current_user_can_publish_topics() {
2183
2184 // Users need to earn access
2185 $retval = false;
2186
2187 // Always allow keymasters
2188 if ( bbp_is_user_keymaster() ) {
2189 $retval = true;
2190
2191 // Do not allow anonymous if not enabled
2192 } elseif ( ! is_user_logged_in() && bbp_allow_anonymous() ) {
2193 $retval = true;
2194
2195 // User is logged in
2196 } elseif ( current_user_can( 'publish_topics' ) ) {
2197 $retval = true;
2198 }
2199
2200 // Filter & return
2201 return (bool) apply_filters( 'bbp_current_user_can_publish_topics', $retval );
2202 }
2203
2204 /**
2205 * Check if the current user can publish forums
2206 *
2207 * @since 2.1.0 bbPress (r3549)
2208 *
2209 * @return bool
2210 */
2211 function bbp_current_user_can_publish_forums() {
2212
2213 // Users need to earn access
2214 $retval = false;
2215
2216 // Always allow keymasters
2217 if ( bbp_is_user_keymaster() ) {
2218 $retval = true;
2219
2220 // User is logged in
2221 } elseif ( current_user_can( 'publish_forums' ) ) {
2222 $retval = true;
2223 }
2224
2225 // Filter & return
2226 return (bool) apply_filters( 'bbp_current_user_can_publish_forums', $retval );
2227 }
2228
2229 /**
2230 * Check if the current user can publish replies
2231 *
2232 * @since 2.0.0 bbPress (r3127)
2233 *
2234 * @return bool
2235 */
2236 function bbp_current_user_can_publish_replies() {
2237
2238 // Users need to earn access
2239 $retval = false;
2240
2241 // Always allow keymasters
2242 if ( bbp_is_user_keymaster() ) {
2243 $retval = true;
2244
2245 // Do not allow anonymous if not enabled
2246 } elseif ( ! is_user_logged_in() && bbp_allow_anonymous() ) {
2247 $retval = true;
2248
2249 // User is logged in
2250 } elseif ( current_user_can( 'publish_replies' ) ) {
2251 $retval = true;
2252 }
2253
2254 // Filter & return
2255 return (bool) apply_filters( 'bbp_current_user_can_publish_replies', $retval );
2256 }
2257
2258 /** Forms *********************************************************************/
2259
2260 /**
2261 * The following functions should be turned into mapped meta capabilities in a
2262 * future version. They exist only to remove complex logistical capability
2263 * checks from within template parts.
2264 */
2265
2266 /**
2267 * Get the forums the current user has the ability to see and post to
2268 *
2269 * @since 2.0.0 bbPress (r3127)
2270 *
2271 * @param array $args
2272 *
2273 * @return array
2274 */
2275 function bbp_get_forums_for_current_user( $args = array() ) {
2276
2277 // Parse arguments against default values
2278 $r = bbp_parse_args( $args, array(
2279 'post_type' => bbp_get_forum_post_type(),
2280 'post_status' => bbp_get_public_status_id(),
2281 'post__not_in' => bbp_exclude_forum_ids( 'array' ),
2282 'numberposts' => -1
2283 ), 'get_forums_for_current_user' );
2284
2285 // Get the forums
2286 $forums = get_posts( $r );
2287
2288 // No availabe forums
2289 if ( empty( $forums ) ) {
2290 $forums = false;
2291 }
2292
2293 // Filter & return
2294 return apply_filters( 'bbp_get_forums_for_current_user', $forums, $r, $args );
2295 }
2296
2297 /**
2298 * Performs a series of checks to ensure the current user can create forums.
2299 *
2300 * @since 2.1.0 bbPress (r3549)
2301 *
2302 * @return bool
2303 */
2304 function bbp_current_user_can_access_create_forum_form() {
2305
2306 // Users need to earn access
2307 $retval = false;
2308
2309 // Always allow keymasters
2310 if ( bbp_is_user_keymaster() ) {
2311 $retval = true;
2312
2313 // Looking at a single forum & forum is open
2314 } elseif ( ( is_page() || is_single() ) && bbp_is_forum_open() ) {
2315 $retval = bbp_current_user_can_publish_forums();
2316
2317 // User can edit this forum
2318 } else {
2319 $retval = current_user_can( 'edit_forum', bbp_get_forum_id() );
2320 }
2321
2322 // Filter & return
2323 return (bool) apply_filters( 'bbp_current_user_can_access_create_forum_form', (bool) $retval );
2324 }
2325
2326 /**
2327 * Performs a series of checks to ensure the current user can create topics.
2328 *
2329 * @since 2.0.0 bbPress (r3127)
2330 *
2331 * @return bool
2332 */
2333 function bbp_current_user_can_access_create_topic_form() {
2334
2335 // Users need to earn access
2336 $retval = false;
2337
2338 // Always allow keymasters
2339 if ( bbp_is_user_keymaster() ) {
2340 $retval = true;
2341
2342 // Looking at a single forum & forum is open
2343 } elseif ( ( bbp_is_single_forum() || is_page() || is_single() ) && bbp_is_forum_open() ) {
2344 $retval = bbp_current_user_can_publish_topics();
2345
2346 // User can edit this topic
2347 } else {
2348 $retval = current_user_can( 'edit_topic', bbp_get_topic_id() );
2349 }
2350
2351 // Filter & return
2352 return (bool) apply_filters( 'bbp_current_user_can_access_create_topic_form', (bool) $retval );
2353 }
2354
2355 /**
2356 * Performs a series of checks to ensure the current user can create replies.
2357 *
2358 * @since 2.0.0 bbPress (r3127)
2359 *
2360 * @return bool
2361 */
2362 function bbp_current_user_can_access_create_reply_form() {
2363
2364 // Users need to earn access
2365 $retval = false;
2366
2367 // Always allow keymasters
2368 if ( bbp_is_user_keymaster() ) {
2369 $retval = true;
2370
2371 // Looking at single topic (and singulars), topic is open, and forum is open
2372 } elseif ( ( bbp_is_single_topic() || is_singular() ) && bbp_is_topic_open() && bbp_is_forum_open() && bbp_is_topic_published() ) {
2373 $retval = bbp_current_user_can_publish_replies();
2374
2375 // User can edit this reply
2376 } elseif ( bbp_get_reply_id() ) {
2377 $retval = current_user_can( 'edit_reply', bbp_get_reply_id() );
2378
2379 // User can edit this topic
2380 } elseif ( bbp_get_topic_id() ) {
2381 $retval = current_user_can( 'edit_topic', bbp_get_topic_id() );
2382 }
2383
2384 // Filter & return
2385 return (bool) apply_filters( 'bbp_current_user_can_access_create_reply_form', (bool) $retval );
2386 }
2387
2388 /**
2389 * Performs a series of checks to ensure the current user should see the
2390 * anonymous user form fields.
2391 *
2392 * @since 2.5.0 bbPress (r5119)
2393 *
2394 * @return bool
2395 */
2396 function bbp_current_user_can_access_anonymous_user_form() {
2397
2398 // Users need to earn access
2399 $retval = false;
2400
2401 // User is not logged in, and anonymous posting is allowed
2402 if ( bbp_is_anonymous() ) {
2403 $retval = true;
2404
2405 // User is editing a topic, and topic is authored by anonymous user
2406 } elseif ( bbp_is_topic_edit() && bbp_is_topic_anonymous() ) {
2407 $retval = true;
2408
2409 // User is editing a reply, and reply is authored by anonymous user
2410 } elseif ( bbp_is_reply_edit() && bbp_is_reply_anonymous() ) {
2411 $retval = true;
2412 }
2413
2414 // Filter & return
2415 return (bool) apply_filters( 'bbp_current_user_can_access_anonymous_user_form', (bool) $retval );
2416 }
2417
2418 /** Moderators ****************************************************************/
2419
2420 /**
2421 * Output the moderators of a forum
2422 *
2423 * @since 2.6.0 bbPress
2424 *
2425 * @param int $forum_id Optional. Topic id
2426 * @param array $args See {@link bbp_get_moderator_list()}
2427 */
2428 function bbp_moderator_list( $forum_id = 0, $args = array() ) {
2429 echo bbp_get_moderator_list( $forum_id, $args );
2430 }
2431
2432 /**
2433 * Return the moderators for an object
2434 *
2435 * @since 2.6.0 bbPress
2436 *
2437 * @param int $object_id Optional. Object id
2438 * @param array $args This function supports these arguments:
2439 * - before: Before the tag list
2440 * - sep: Tag separator
2441 * - after: After the tag list
2442 *
2443 * @return string Moderator list of the object
2444 */
2445 function bbp_get_moderator_list( $object_id = 0, $args = array() ) {
2446
2447 // Parse arguments against default values
2448 $r = bbp_parse_args( $args, array(
2449 'before' => '<div class="bbp-moderators"><p>' . esc_html__( 'Moderators:', 'bbpress' ) . '&nbsp;',
2450 'sep' => ', ',
2451 'after' => '</p></div>',
2452 'none' => ''
2453 ), 'get_moderator_list' );
2454
2455 // Get forum moderators
2456 $user_ids = bbp_get_moderator_ids( $object_id );
2457 if ( ! empty( $user_ids ) ) {
2458
2459 // In admin, use nicenames
2460 if ( is_admin() ) {
2461 $users = bbp_get_user_nicenames_from_ids( $user_ids );
2462
2463 // In theme, use display names & profile links
2464 } else {
2465 foreach ( $user_ids as $user_id ) {
2466 $users[] = bbp_get_user_profile_link( $user_id );
2467 }
2468 }
2469
2470 $retval = $r['before'] . implode( $r['sep'], $users ) . $r['after'];
2471
2472 // No forum moderators
2473 } else {
2474 $retval = $r['none'];
2475 }
2476
2477 // Filter & return
2478 return apply_filters( 'bbp_get_moderator_list', $retval );
2479 }
2480