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 / template.php

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

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