PluginProbe
bbPress / 2.6.19
bbPress v2.6.19
2.6.19 2.6.18 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 All 74 releases
bbpress / includes / users / template.php

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

2,595 lines 67.9 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 (r6332)
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 (r6332)
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 (r6332)
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 (r6332)
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 (r6332)
53 * @access public
54 * @var WP_User
55 */
56 public $user;
57
58 /**
59 * PHP5 constructor.
60 *
61 * @since 2.6.0 bbPress (r6334)
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 (r6332)
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 (r6332)
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 (r6332)
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 (r6332)
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 (r6332)
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 (r6332)
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 (r6332)
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 (r6332)
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 (r6332)
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.1.0 bbPress (r3634)
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 (r4672)
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 (r4672)
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 (r6562)
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 (r5120)
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 (r5120)
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 (r5120)
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 (r5120)
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 (r5120)
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 (r5120)
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 (r2660)
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 (r2660)
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 (r6307) 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 (r6307) 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 // Validate the object before checking its favorite state
1167 if ( empty( $user_id ) || empty( $object_id ) || ! bbp_current_user_can_toggle_engagement( $object_id, $object_type, 'favorite', 'remove' ) ) {
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 $toggle_action = ! empty( $is_fav ) ? 'remove' : 'add';
1179 if ( ! bbp_current_user_can_toggle_engagement( $object_id, $object_type, 'favorite', $toggle_action ) ) {
1180 return false;
1181 }
1182
1183 if ( ! empty( $is_fav ) ) {
1184 $text = $r['favorited'];
1185 $q_args = array(
1186 'action' => 'bbp_favorite_remove',
1187 'object_id' => $object_id
1188 );
1189 } else {
1190 $text = $r['favorite'];
1191 $q_args = array(
1192 'action' => 'bbp_favorite_add',
1193 'object_id' => $object_id
1194 );
1195 }
1196
1197 // Custom redirect
1198 if ( ! empty( $r['redirect_to'] ) ) {
1199 $q_args['redirect_to'] = urlencode( $r['redirect_to'] );
1200 }
1201
1202 // URL
1203 $url = esc_url( wp_nonce_url( add_query_arg( $q_args ), 'toggle-favorite_' . $object_id ) );
1204 $sub = $is_fav ? ' class="is-favorite"' : '';
1205 $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'] );
1206
1207 // Initial output is wrapped in a span, ajax output is hooked to this
1208 if ( ! empty( $wrap ) ) {
1209 $html = '<span id="favorite-toggle">' . $html . '</span>';
1210 }
1211
1212 // Filter & return
1213 return apply_filters( 'bbp_get_user_favorites_link', $html, $r, $user_id, $object_id );
1214 }
1215
1216 /** Subscriptions *************************************************************/
1217
1218 /**
1219 * Output the link to the user's subscriptions page (profile page)
1220 *
1221 * @since 2.0.0 bbPress (r2688)
1222 * @since 2.6.0 bbPress (r6308) Add pagination if in the loop
1223 *
1224 * @param int $user_id Optional. User id
1225 */
1226 function bbp_subscriptions_permalink( $user_id = 0 ) {
1227 echo esc_url( bbp_get_subscriptions_permalink( $user_id ) );
1228 }
1229 /**
1230 * Return the link to the user's subscriptions page (profile page)
1231 *
1232 * @since 2.0.0 bbPress (r2688)
1233 * @since 2.6.0 bbPress (r6308) Add pagination if in the loop
1234 *
1235 * @param int $user_id Optional. User id
1236 * @return string Permanent link to user subscriptions page
1237 */
1238 function bbp_get_subscriptions_permalink( $user_id = 0 ) {
1239
1240 // Use displayed user ID if there is one, and one isn't requested
1241 $user_id = bbp_get_user_id( $user_id );
1242 if ( empty( $user_id ) ) {
1243 return false;
1244 }
1245
1246 // Bail if intercepted
1247 $intercept = bbp_maybe_intercept( 'bbp_pre_get_subscriptions_permalink', func_get_args() );
1248 if ( bbp_is_intercepted( $intercept ) ) {
1249 return $intercept;
1250 }
1251
1252 // Get user profile URL
1253 $profile_url = bbp_get_user_profile_url( $user_id );
1254 $page = 0;
1255 $paged = false;
1256
1257 // Get pagination data
1258 if ( bbpress()->topic_query->in_the_loop ) {
1259 $page = (int) bbpress()->topic_query->paged;
1260 $paged = (bool) bbpress()->topic_query->in_the_loop;
1261
1262 } elseif ( bbpress()->forum_query->in_the_loop ) {
1263 $page = (int) bbpress()->forum_query->paged;
1264 $paged = (bool) bbpress()->forum_query->in_the_loop;
1265 }
1266
1267 // Pretty permalinks
1268 if ( bbp_use_pretty_urls() ) {
1269
1270 // Base URL
1271 $url = trailingslashit( $profile_url ) . bbp_get_user_subscriptions_slug();
1272
1273 // Add page
1274 if ( ( true === $paged ) && ( $page > 1 ) ) {
1275 $url = trailingslashit( $url ) . bbp_get_paged_slug() . '/' . $page;
1276 }
1277
1278 // Ensure correct trailing slash
1279 $url = user_trailingslashit( $url );
1280
1281 // Unpretty permalinks
1282 } else {
1283
1284 // Base arguments
1285 $args = array(
1286 bbp_get_user_subscriptions_rewrite_id() => bbp_get_user_subscriptions_slug(),
1287 );
1288
1289 // Add page
1290 if ( ( true === $paged ) && ( $page > 1 ) ) {
1291 $args['page'] = $page;
1292 }
1293
1294 // Add arguments
1295 $url = add_query_arg( $args, $profile_url );
1296 }
1297
1298 // Filter & return
1299 return apply_filters( 'bbp_get_subscriptions_permalink', $url, $user_id );
1300 }
1301
1302 /**
1303 * Output the link to subscribe/unsubscribe from a topic
1304 *
1305 * @since 2.0.0 bbPress (r2668)
1306 * @since 2.6.0 bbPress (r6307) Add 'redirect_to' support
1307 *
1308 * @param array $args See {@link bbp_get_user_subscribe_link()}
1309 * @param int $user_id Optional. User id
1310 * @param bool $wrap Optional. If you want to wrap the link in <span id="subscription-toggle">.
1311 */
1312 function bbp_user_subscribe_link( $args = array(), $user_id = 0, $wrap = true ) {
1313 echo bbp_get_user_subscribe_link( $args, $user_id, $wrap );
1314 }
1315 /**
1316 * Return the link to subscribe/unsubscribe from a forum or topic
1317 *
1318 * @since 2.0.0 bbPress (r2668)
1319 * @since 2.6.0 bbPress (r6307) Add 'redirect_to' support
1320 *
1321 * @param array $args This function supports these arguments:
1322 * - subscribe: Subscribe text
1323 * - unsubscribe: Unsubscribe text
1324 * - user_id: User id
1325 * - topic_id: Topic id
1326 * - forum_id: Forum id
1327 * - before: Before the link
1328 * - after: After the link
1329 * @param int $user_id Optional. User id
1330 * @param bool $wrap Optional. If you want to wrap the link in <span id="subscription-toggle">.
1331 * @return string Permanent link to topic
1332 */
1333 function bbp_get_user_subscribe_link( $args = array(), $user_id = 0, $wrap = true ) {
1334
1335 // Bail if subscriptions are inactive
1336 if ( ! bbp_is_subscriptions_active() ) {
1337 return;
1338 }
1339
1340 // Parse arguments against default values
1341 $r = bbp_parse_args(
1342 $args,
1343 array(
1344 'subscribe' => esc_html__( 'Subscribe', 'bbpress' ),
1345 'unsubscribe' => esc_html__( 'Unsubscribe', 'bbpress' ),
1346 'user_id' => 0,
1347 'object_id' => 0,
1348 'object_type' => 'post',
1349 'before' => '',
1350 'after' => '',
1351 'redirect_to' => '',
1352
1353 // Deprecated. Use object_id.
1354 'forum_id' => 0,
1355 'topic_id' => 0
1356 ),
1357 'get_user_subscribe_link'
1358 );
1359
1360 // Validate user
1361 $user_id = bbp_get_user_id( $r['user_id'], true, true );
1362 $object_type = sanitize_key( $r['object_type'] );
1363
1364 // Back-compat for deprecated arguments
1365 if ( ! empty( $r['topic_id'] ) ) {
1366 $object_id = absint( $r['topic_id'] );
1367 } elseif ( ! empty( $r['forum_id'] ) ) {
1368 $object_id = absint( $r['forum_id'] );
1369 } else {
1370 $object_id = absint( $r['object_id'] );
1371 }
1372
1373 // Validate the object before checking its subscription state
1374 if ( empty( $user_id ) || empty( $object_id ) || ! bbp_current_user_can_toggle_engagement( $object_id, $object_type, 'subscription', 'remove' ) ) {
1375 return false;
1376 }
1377
1378 // No link if you can't edit yourself
1379 if ( ! current_user_can( 'edit_user', $user_id ) ) {
1380 return false;
1381 }
1382
1383 // Decide which link to show
1384 $is_subscribed = bbp_is_user_subscribed( $user_id, $object_id, $object_type );
1385 $toggle_action = ! empty( $is_subscribed ) ? 'remove' : 'add';
1386 if ( ! bbp_current_user_can_toggle_engagement( $object_id, $object_type, 'subscription', $toggle_action ) ) {
1387 return false;
1388 }
1389
1390 if ( ! empty( $is_subscribed ) ) {
1391 $text = $r['unsubscribe'];
1392 $q_args = array(
1393 'action' => 'bbp_unsubscribe',
1394 'object_id' => $object_id,
1395 'object_type' => $object_type
1396 );
1397 } else {
1398 $text = $r['subscribe'];
1399 $q_args = array(
1400 'action' => 'bbp_subscribe',
1401 'object_id' => $object_id,
1402 'object_type' => $object_type
1403 );
1404 }
1405
1406 // Custom redirect
1407 if ( ! empty( $r['redirect_to'] ) ) {
1408 $q_args['redirect_to'] = urlencode( $r['redirect_to'] );
1409 }
1410
1411 // URL
1412 $url = esc_url( wp_nonce_url( add_query_arg( $q_args ), 'toggle-subscription_post_' . $object_id ) );
1413 $sub = $is_subscribed ? ' class="is-subscribed"' : '';
1414 $html = sprintf( '%s<span id="subscribe-%d" %s><a href="%s" class="subscription-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-subscription_post_' . $object_id ), $text, $r['after'] );
1415
1416 // Initial output is wrapped in a span, ajax output is hooked to this
1417 if ( ! empty( $wrap ) ) {
1418 $html = '<span id="subscription-toggle">' . $html . '</span>';
1419 }
1420
1421 // Filter & return
1422 return apply_filters( 'bbp_get_user_subscribe_link', $html, $r, $user_id, $object_id );
1423 }
1424
1425 /** Edit User *****************************************************************/
1426
1427 /**
1428 * Display profile edit success notice on user edit page
1429 *
1430 * @since 2.0.0 bbPress (r2688)
1431 */
1432 function bbp_notice_edit_user_success() {
1433
1434 // Bail if no updated argument
1435 if ( empty( $_GET['updated'] ) ) {
1436 return;
1437 }
1438
1439 // Bail if not on users own profile
1440 if ( ! bbp_is_single_user_edit() ) {
1441 return;
1442 } ?>
1443
1444 <div class="bbp-template-notice">
1445 <ul>
1446 <li><?php esc_html_e( 'User updated.', 'bbpress' ); ?></li>
1447 </ul>
1448 </div>
1449
1450 <?php
1451 }
1452
1453 /**
1454 * Display pending email change notice on user edit page
1455 *
1456 * @since 2.6.0 bbPress (r5661)
1457 */
1458 function bbp_notice_edit_user_pending_email() {
1459
1460 // Bail if not on users own profile
1461 if ( ! bbp_is_user_home_edit() ) {
1462 return;
1463 }
1464
1465 // Check for pending email address change
1466 $user_id = bbp_get_displayed_user_id();
1467 $key = '_new_email';
1468 $new_email = get_user_meta( $user_id, $key, true );
1469
1470 // Bail if no pending email address change
1471 if ( empty( $new_email['newemail'] ) ) {
1472 return;
1473 }
1474
1475 // Build the nonced URL to dismiss the pending change
1476 $user_url = bbp_get_user_profile_edit_url( $user_id );
1477 $nonce = "dismiss-{$user_id}{$key}";
1478 $args = array(
1479 'action' => 'bbp-update-user-email',
1480 'dismiss' => "{$user_id}{$key}"
1481 );
1482
1483 // Build the variables to pass into printf()
1484 $dismiss_url = wp_nonce_url( add_query_arg( $args, $user_url ), $nonce );
1485 $dismiss_link = '<a href="' . esc_url( $dismiss_url ) . '">' . esc_html_x( 'Cancel', 'Dismiss pending user email address change', 'bbpress' ) . '</a>';
1486 $coded_email = '<code>' . esc_html( $new_email['newemail'] ) . '</code>'; ?>
1487
1488 <div class="bbp-template-notice info">
1489 <ul>
1490 <li><?php printf(
1491 /* translators: 1: New email address, 2: Dismiss change link */
1492 esc_html__( 'There is a pending email address change to %1$s. %2$s', 'bbpress' ),
1493 $coded_email,
1494 $dismiss_link
1495 );
1496 ?></li>
1497 </ul>
1498 </div>
1499
1500 <?php
1501 }
1502
1503 /**
1504 * Super admin privileges notice
1505 *
1506 * @since 2.0.0 bbPress (r2688)
1507 */
1508 function bbp_notice_edit_user_is_super_admin() {
1509 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() ) ) : ?>
1510
1511 <div class="bbp-template-notice important">
1512 <ul>
1513 <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>
1514 </ul>
1515 </div>
1516
1517 <?php endif;
1518 }
1519
1520 /**
1521 * Drop down for selecting the user's display name
1522 *
1523 * @since 2.0.0 bbPress (r2688)
1524 */
1525 function bbp_edit_user_display_name() {
1526 $bbp = bbpress();
1527 $public_display = array();
1528 $public_display['display_username'] = $bbp->displayed_user->user_login;
1529
1530 if ( ! empty( $bbp->displayed_user->nickname ) ) {
1531 $public_display['display_nickname'] = $bbp->displayed_user->nickname;
1532 }
1533
1534 if ( ! empty( $bbp->displayed_user->first_name ) ) {
1535 $public_display['display_firstname'] = $bbp->displayed_user->first_name;
1536 }
1537
1538 if ( ! empty( $bbp->displayed_user->last_name ) ) {
1539 $public_display['display_lastname'] = $bbp->displayed_user->last_name;
1540 }
1541
1542 if ( ! empty( $bbp->displayed_user->first_name ) && ! empty( $bbp->displayed_user->last_name ) ) {
1543 $public_display['display_firstlast'] = $bbp->displayed_user->first_name . ' ' . $bbp->displayed_user->last_name;
1544 $public_display['display_lastfirst'] = $bbp->displayed_user->last_name . ' ' . $bbp->displayed_user->first_name;
1545 }
1546
1547 // Only add this if it isn't duplicated elsewhere
1548 if ( ! in_array( $bbp->displayed_user->display_name, $public_display, true ) ) {
1549 $public_display = array( 'display_displayname' => $bbp->displayed_user->display_name ) + $public_display;
1550 }
1551
1552 $public_display = array_map( 'trim', $public_display );
1553 $public_display = array_unique( $public_display ); ?>
1554
1555 <select name="display_name" id="display_name">
1556
1557 <?php foreach ( $public_display as $id => $item ) : ?>
1558
1559 <option id="<?php echo esc_attr( $id ); ?>" value="<?php echo esc_attr( $item ); ?>"<?php selected( $bbp->displayed_user->display_name, $item ); ?>><?php echo esc_html( $item ); ?></option>
1560
1561 <?php endforeach; ?>
1562
1563 </select>
1564
1565 <?php
1566 }
1567
1568 /**
1569 * Output blog role selector (for user edit)
1570 *
1571 * @since 2.2.0 bbPress (r4288)
1572 */
1573 function bbp_edit_user_blog_role() {
1574
1575 // Bail if no user is being edited
1576 if ( ! bbp_is_single_user_edit() || ! bbp_current_user_can_edit_user_field( 'site_role', bbp_get_displayed_user_id() ) ) {
1577 return;
1578 }
1579
1580 // Get users current blog role
1581 $user_role = bbp_get_user_blog_role( bbp_get_displayed_user_id() );
1582
1583 // Get the blog roles
1584 $blog_roles = bbp_get_blog_roles(); ?>
1585
1586 <select name="role" id="role">
1587 <option value=""><?php esc_html_e( '&mdash; No role for this site &mdash;', 'bbpress' ); ?></option>
1588
1589 <?php foreach ( $blog_roles as $role => $details ) : ?>
1590
1591 <option <?php selected( $user_role, $role ); ?> value="<?php echo esc_attr( $role ); ?>"><?php echo bbp_translate_user_role( $details['name'] ); ?></option>
1592
1593 <?php endforeach; ?>
1594
1595 </select>
1596
1597 <?php
1598 }
1599
1600 /**
1601 * Output forum role selector (for user edit)
1602 *
1603 * @since 2.2.0 bbPress (r4288)
1604 */
1605 function bbp_edit_user_forums_role() {
1606
1607 // Bail if no user is being edited
1608 if ( ! bbp_is_single_user_edit() || ! bbp_current_user_can_edit_user_field( 'forum_role', bbp_get_displayed_user_id() ) ) {
1609 return;
1610 }
1611
1612 // Get the user's current forum role
1613 $user_role = bbp_get_user_role( bbp_get_displayed_user_id() );
1614
1615 // Get the folum roles
1616 $dynamic_roles = bbp_get_user_editable_forum_roles( bbp_get_displayed_user_id() ); ?>
1617
1618 <select name="bbp-forums-role" id="bbp-forums-role">
1619 <option value=""><?php esc_html_e( '&mdash; No role for these forums &mdash;', 'bbpress' ); ?></option>
1620
1621 <?php foreach ( $dynamic_roles as $role => $details ) : ?>
1622
1623 <option <?php selected( $user_role, $role ); ?> value="<?php echo esc_attr( $role ); ?>"><?php echo bbp_translate_user_role( $details['name'] ); ?></option>
1624
1625 <?php endforeach; ?>
1626
1627 </select>
1628
1629 <?php
1630 }
1631
1632 /**
1633 * Filter whether password fields are displayed on a bbPress user profile.
1634 *
1635 * @since 2.6.17 bbPress (r7514)
1636 *
1637 * @param bool $show Whether to show the password fields.
1638 * @param WP_User $profile_user User being edited.
1639 * @return bool Whether to show the password fields.
1640 */
1641 function bbp_filter_user_edit_password_fields( $show, $profile_user ) {
1642 if ( $show && ! is_admin() && bbp_is_single_user_edit() ) {
1643 $show = bbp_current_user_can_edit_user_field( 'password', $profile_user->ID );
1644 }
1645
1646 return $show;
1647 }
1648
1649 /**
1650 * Return user contact methods select box
1651 *
1652 * @since 2.0.0 bbPress (r2688)
1653 * @return string User contact methods
1654 */
1655 function bbp_edit_user_contact_methods() {
1656
1657 // Get the core WordPress contact methods
1658 $contact_methods = wp_get_user_contact_methods( bbpress()->displayed_user );
1659
1660 // Filter & return
1661 return (array) apply_filters( 'bbp_edit_user_contact_methods', $contact_methods );
1662 }
1663
1664 /**
1665 * Output the language chooser (for user edit)
1666 *
1667 * @since 2.6.0 bbPress (r6489)
1668 *
1669 * @param array $args See wp_dropdown_languages()
1670 * @return string
1671 */
1672 function bbp_edit_user_language( $args = array() ) {
1673
1674 // Bail if no user is being edited
1675 if ( ! bbp_is_single_user_edit() ) {
1676 return;
1677 }
1678
1679 // Output the dropdown
1680 bbp_user_languages_dropdown( $args );
1681 }
1682
1683 /** Requests ******************************************************************/
1684
1685 /**
1686 * Verify if a POST request came from a user profile edit form.
1687 *
1688 * Used to avoid cross-site request forgeries when checking posted user profile
1689 * form content.
1690 *
1691 * @since 2.6.15 bbPress (r7398)
1692 *
1693 * @param int $user_id
1694 *
1695 * @return bool True if this is a user profile post request with valid nonce
1696 */
1697 function bbp_is_user_profile_form_post_request( $user_id = 0 ) {
1698
1699 // Bail if no user ID was passed
1700 if ( empty( $user_id ) ) {
1701 return false;
1702 }
1703
1704 // Bail if not a post request
1705 if ( ! bbp_is_post_request() ) {
1706 return false;
1707 }
1708
1709 // Build the nonce action string
1710 $action = 'update-user_' . $user_id;
1711
1712 // Editing an existing user
1713 if ( bbp_verify_nonce_request( $action ) ) {
1714 return true;
1715 }
1716
1717 // Nonce fallback for admin profile requests where URL validation can vary
1718 if ( isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], $action ) ) {
1719 return true;
1720 }
1721
1722 return false;
1723 }
1724
1725 /** Topics Created ************************************************************/
1726
1727 /**
1728 * Output the link to the user's topics
1729 *
1730 * @since 2.2.0 bbPress (r4228)
1731 *
1732 * @param int $user_id Optional. User id
1733 */
1734 function bbp_user_topics_created_url( $user_id = 0 ) {
1735 echo esc_url( bbp_get_user_topics_created_url( $user_id ) );
1736 }
1737 /**
1738 * Return the link to the user's topics
1739 *
1740 * @since 2.2.0 bbPress (r4228)
1741 *
1742 * @param int $user_id Optional. User id
1743 * @return string Permanent link to user profile page
1744 */
1745 function bbp_get_user_topics_created_url( $user_id = 0 ) {
1746
1747 // Use displayed user ID if there is one, and one isn't requested
1748 $user_id = bbp_get_user_id( $user_id );
1749 if ( empty( $user_id ) ) {
1750 return false;
1751 }
1752
1753 // Bail if intercepted
1754 $intercept = bbp_maybe_intercept( 'bbp_pre_get_user_topics_created_url', func_get_args() );
1755 if ( bbp_is_intercepted( $intercept ) ) {
1756 return $intercept;
1757 }
1758
1759 // Get user profile URL
1760 $profile_url = bbp_get_user_profile_url( $user_id );
1761
1762 // Pretty permalinks
1763 if ( bbp_use_pretty_urls() ) {
1764 $url = trailingslashit( $profile_url ) . bbp_get_topic_archive_slug();
1765 $url = user_trailingslashit( $url );
1766
1767 // Unpretty permalinks
1768 } else {
1769 $url = add_query_arg(
1770 array(
1771 bbp_get_user_topics_rewrite_id() => '1',
1772 ),
1773 $profile_url
1774 );
1775 }
1776
1777 // Filter & return
1778 return apply_filters( 'bbp_get_user_topics_created_url', $url, $user_id );
1779 }
1780
1781 /** Replies Created ************************************************************/
1782
1783 /**
1784 * Output the link to the user's replies
1785 *
1786 * @since 2.2.0 bbPress (r4228)
1787 *
1788 * @param int $user_id Optional. User id
1789 */
1790 function bbp_user_replies_created_url( $user_id = 0 ) {
1791 echo esc_url( bbp_get_user_replies_created_url( $user_id ) );
1792 }
1793 /**
1794 * Return the link to the user's replies
1795 *
1796 * @since 2.2.0 bbPress (r4228)
1797 *
1798 * @param int $user_id Optional. User id
1799 * @return string Permanent link to user profile page
1800 */
1801 function bbp_get_user_replies_created_url( $user_id = 0 ) {
1802
1803 // Use displayed user ID if there is one, and one isn't requested
1804 $user_id = bbp_get_user_id( $user_id );
1805 if ( empty( $user_id ) ) {
1806 return false;
1807 }
1808
1809 // Bail if intercepted
1810 $intercept = bbp_maybe_intercept( 'bbp_pre_get_user_replies_created_url', func_get_args() );
1811 if ( bbp_is_intercepted( $intercept ) ) {
1812 return $intercept;
1813 }
1814
1815 // Get user profile URL
1816 $profile_url = bbp_get_user_profile_url( $user_id );
1817
1818 // Pretty permalinks
1819 if ( bbp_use_pretty_urls() ) {
1820 $url = trailingslashit( $profile_url ) . bbp_get_reply_archive_slug();
1821 $url = user_trailingslashit( $url );
1822
1823 // Unpretty permalinks
1824 } else {
1825 $url = add_query_arg(
1826 array(
1827 bbp_get_user_replies_rewrite_id() => '1',
1828 ),
1829 $profile_url
1830 );
1831 }
1832
1833 // Filter & return
1834 return apply_filters( 'bbp_get_user_replies_created_url', $url, $user_id );
1835 }
1836
1837 /** Engagements ***************************************************************/
1838
1839 /**
1840 * Output 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 */
1846 function bbp_user_engagements_url( $user_id = 0 ) {
1847 echo esc_url( bbp_get_user_engagements_url( $user_id ) );
1848 }
1849 /**
1850 * Return the link to the user's engagements
1851 *
1852 * @since 2.6.0 bbPress (r6320)
1853 *
1854 * @param int $user_id Optional. User id
1855 * @return string Permanent link to user profile page
1856 */
1857 function bbp_get_user_engagements_url( $user_id = 0 ) {
1858
1859 // Use displayed user ID if there is one, and one isn't requested
1860 $user_id = bbp_get_user_id( $user_id );
1861 if ( empty( $user_id ) ) {
1862 return false;
1863 }
1864
1865 // Bail if intercepted
1866 $intercept = bbp_maybe_intercept( 'bbp_pre_get_user_engagements_url', func_get_args() );
1867 if ( bbp_is_intercepted( $intercept ) ) {
1868 return $intercept;
1869 }
1870
1871 // Get user profile URL
1872 $profile_url = bbp_get_user_profile_url( $user_id );
1873
1874 // Pretty permalinks
1875 if ( bbp_use_pretty_urls() ) {
1876 $url = trailingslashit( $profile_url ) . bbp_get_user_engagements_slug();
1877 $url = user_trailingslashit( $url );
1878
1879 // Unpretty permalinks
1880 } else {
1881 $url = add_query_arg(
1882 array(
1883 bbp_get_user_engagements_rewrite_id() => '1',
1884 ),
1885 $profile_url
1886 );
1887 }
1888
1889 // Filter & return
1890 return apply_filters( 'bbp_get_user_engagements_url', $url, $user_id );
1891 }
1892
1893 /** Language ******************************************************************/
1894
1895 /**
1896 * Output the select element used to save a user's language
1897 *
1898 * @since 2.6.0 bbPress (r6489)
1899 *
1900 * @param array $args See wp_dropdown_languages()
1901 */
1902 function bbp_user_languages_dropdown( $args = array() ) {
1903 echo bbp_get_user_languages_dropdown( $args );
1904 }
1905
1906 /**
1907 * Return the select element used to save a user's language.
1908 *
1909 * @since 2.6.0 bbPress (r6489)
1910 *
1911 * @param array $args See wp_dropdown_languages()
1912 * @return string
1913 */
1914 function bbp_get_user_languages_dropdown( $args = array() ) {
1915
1916 // Get user language
1917 $user_id = ! empty( $args['user_id'] )
1918 ? bbp_get_user_id( $args['user_id'], false, false )
1919 : bbp_get_displayed_user_id();
1920
1921 // Get user locale
1922 $user_locale = ! empty( $user_id )
1923 ? get_userdata( $user_id )->locale
1924 : 'site-default';
1925
1926 // Get all languages
1927 $languages = get_available_languages();
1928
1929 // No locale for English
1930 if ( 'en_US' === $user_locale ) {
1931 $user_locale = '';
1932
1933 // Fallback to site-default if there is a mismatch
1934 } elseif ( '' === $user_locale || ! in_array( $user_locale, $languages, true ) ) {
1935 $user_locale = 'site-default';
1936 }
1937
1938 // Don't pass user ID in
1939 unset( $args['user_id'] );
1940
1941 // Parse arguments
1942 $r = bbp_parse_args(
1943 $args,
1944 array(
1945 'name' => 'locale',
1946 'id' => 'locale',
1947 'selected' => $user_locale,
1948 'languages' => $languages,
1949 'echo' => false,
1950 'show_available_translations' => false,
1951 'show_option_site_default' => true
1952 ),
1953 'user_languages_dropdown'
1954 );
1955
1956 // Get the markup for the languages drop-down
1957 $retval = wp_dropdown_languages( $r );
1958
1959 // Filter & return
1960 return apply_filters( 'bbp_get_user_languages_dropdown', $retval, $r, $args );
1961 }
1962
1963 /** Login *********************************************************************/
1964
1965 /**
1966 * Handle the login and registration template notices
1967 *
1968 * @since 2.0.0 bbPress (r2970)
1969 */
1970 function bbp_login_notices() {
1971
1972 // loggedout was passed
1973 if ( ! empty( $_GET['loggedout'] ) && ( 'true' === $_GET['loggedout'] ) ) {
1974 bbp_add_error( 'loggedout', esc_html__( 'You are now logged out.', 'bbpress' ), 'message' );
1975
1976 // registration is disabled
1977 } elseif ( ! empty( $_GET['registration'] ) && ( 'disabled' === $_GET['registration'] ) ) {
1978 bbp_add_error( 'registerdisabled', esc_html__( 'New user registration is currently not allowed.', 'bbpress' ) );
1979
1980 // Prompt user to check their email
1981 } elseif ( ! empty( $_GET['checkemail'] ) && in_array( $_GET['checkemail'], array( 'confirm', 'newpass', 'registered' ), true ) ) {
1982
1983 switch ( $_GET['checkemail'] ) {
1984
1985 // Email needs confirmation
1986 case 'confirm' :
1987 bbp_add_error( 'confirm', esc_html__( 'Check your e-mail for the confirmation link.', 'bbpress' ), 'message' );
1988 break;
1989
1990 // User requested a new password
1991 case 'newpass' :
1992 bbp_add_error( 'newpass', esc_html__( 'Check your e-mail for your new password.', 'bbpress' ), 'message' );
1993 break;
1994
1995 // User is newly registered
1996 case 'registered' :
1997 bbp_add_error( 'registered', esc_html__( 'Registration complete. Please check your e-mail.', 'bbpress' ), 'message' );
1998 break;
1999 }
2000 }
2001 }
2002
2003 /**
2004 * Redirect a user back to their profile if they are already logged in.
2005 *
2006 * This should be used before {@link get_header()} is called in template files
2007 * where the user should never have access to the contents of that file.
2008 *
2009 * @since 2.0.0 bbPress (r2815)
2010 *
2011 * @param string $url The URL to redirect to
2012 */
2013 function bbp_logged_in_redirect( $url = '' ) {
2014
2015 // Bail if user is not logged in
2016 if ( ! is_user_logged_in() ) {
2017 return;
2018 }
2019
2020 // Setup the profile page to redirect to
2021 $redirect_to = ! empty( $url ) ? $url : bbp_get_user_profile_url( bbp_get_current_user_id() );
2022
2023 // Do a safe redirect
2024 bbp_redirect( $redirect_to );
2025 }
2026
2027 /**
2028 * Output the required hidden fields when logging in
2029 *
2030 * @since 2.0.0 bbPress (r2815)
2031 */
2032 function bbp_user_login_fields() {
2033 ?>
2034
2035 <input type="hidden" name="user-cookie" value="1" />
2036
2037 <?php
2038
2039 // Allow custom login redirection
2040 $redirect_to = apply_filters( 'bbp_user_login_redirect_to', '' );
2041 bbp_redirect_to_field( $redirect_to );
2042
2043 // Prevent intention hi-jacking of log-in form
2044 wp_nonce_field( 'bbp-user-login' );
2045 }
2046
2047 /** Register ******************************************************************/
2048
2049 /**
2050 * Output the required hidden fields when registering
2051 *
2052 * @since 2.0.0 bbPress (r2815)
2053 */
2054 function bbp_user_register_fields() {
2055 ?>
2056
2057 <input type="hidden" name="action" value="register" />
2058 <input type="hidden" name="user-cookie" value="1" />
2059
2060 <?php
2061
2062 // Allow custom registration redirection
2063 $redirect_to = apply_filters( 'bbp_user_register_redirect_to', '' );
2064 bbp_redirect_to_field( add_query_arg( array( 'checkemail' => 'registered' ), $redirect_to ) );
2065
2066 // Prevent intention hi-jacking of sign-up form
2067 wp_nonce_field( 'bbp-user-register' );
2068 }
2069
2070 /** Lost Password *************************************************************/
2071
2072 /**
2073 * Output the required hidden fields when user lost password
2074 *
2075 * @since 2.0.0 bbPress (r2815)
2076 */
2077 function bbp_user_lost_pass_fields() {
2078 ?>
2079
2080 <input type="hidden" name="user-cookie" value="1" />
2081
2082 <?php
2083
2084 // Allow custom lost pass redirection
2085 $redirect_to = apply_filters( 'bbp_user_lost_pass_redirect_to', get_permalink() );
2086 bbp_redirect_to_field( add_query_arg( array( 'checkemail' => 'confirm' ), $redirect_to ) );
2087
2088 // Prevent intention hi-jacking of lost pass form
2089 wp_nonce_field( 'bbp-user-lost-pass' );
2090 }
2091
2092 /** Author Avatar *************************************************************/
2093
2094 /**
2095 * Output the author link of a post
2096 *
2097 * @since 2.0.0 bbPress (r2874)
2098 *
2099 * @param array $args Optional. If it is an integer, it is used as post id.
2100 */
2101 function bbp_author_link( $args = array() ) {
2102 echo bbp_get_author_link( $args );
2103 }
2104 /**
2105 * Return the author link of the post
2106 *
2107 * @since 2.0.0 bbPress (r2874)
2108 *
2109 * @param array $args Optional. If an integer, it is used as reply id.
2110 * @return string Author link of reply
2111 */
2112 function bbp_get_author_link( $args = array() ) {
2113
2114 $post_id = is_numeric( $args ) ? (int) $args : 0;
2115
2116 // Parse arguments against default values
2117 $r = bbp_parse_args(
2118 $args,
2119 array(
2120 'post_id' => $post_id,
2121 'link_title' => '',
2122 'type' => 'both',
2123 'size' => 80,
2124 'sep' => ''
2125 ),
2126 'get_author_link'
2127 );
2128
2129 // An explicitly empty post ID must not fall back to the current loop post
2130 if ( ( is_numeric( $args ) || ( is_array( $args ) && array_key_exists( 'post_id', $args ) ) ) && empty( $r['post_id'] ) ) {
2131 return apply_filters( 'bbp_get_author_link', '-', $r, $args );
2132 }
2133
2134 // Confirmed topic
2135 if ( bbp_is_topic( $r['post_id'] ) ) {
2136 return bbp_get_topic_author_link( $r );
2137
2138 // Confirmed reply
2139 } elseif ( bbp_is_reply( $r['post_id'] ) ) {
2140 return bbp_get_reply_author_link( $r );
2141 }
2142
2143 // Default return value
2144 $author_link = '';
2145
2146 // Neither a reply nor a topic, so could be a revision
2147 if ( ! empty( $r['post_id'] ) ) {
2148
2149 // Get some useful reply information
2150 $user_id = get_post_field( 'post_author', $r['post_id'] );
2151 $author_url = bbp_get_user_profile_url( $user_id );
2152 $anonymous = bbp_is_reply_anonymous( $r['post_id'] );
2153
2154 // Generate title with the display name of the author
2155 if ( empty( $r['link_title'] ) ) {
2156 $author = get_the_author_meta( 'display_name', $user_id );
2157 $title = empty( $anonymous )
2158 /* translators: %s: Author's display name */
2159 ? esc_attr__( "View %s's profile", 'bbpress' )
2160 /* translators: %s: Author's display name */
2161 : esc_attr__( "Visit %s's website", 'bbpress' );
2162
2163 $r['link_title'] = sprintf( $title, $author );
2164 }
2165
2166 // Setup title and author_links array
2167 $author_links = array();
2168 $link_title = ! empty( $r['link_title'] )
2169 ? ' title="' . esc_attr( $r['link_title'] ) . '"'
2170 : '';
2171
2172 // Get avatar (unescaped, because HTML)
2173 if ( ( 'avatar' === $r['type'] ) || ( 'both' === $r['type'] ) ) {
2174 $author_links['avatar'] = get_avatar( $user_id, $r['size'] );
2175 }
2176
2177 // Get display name (escaped, because never HTML)
2178 if ( ( 'name' === $r['type'] ) || ( 'both' === $r['type'] ) ) {
2179 $author_links['name'] = esc_html( get_the_author_meta( 'display_name', $user_id ) );
2180 }
2181
2182 // Empty array
2183 $links = array();
2184 $sprint = '<span %1$s>%2$s</span>';
2185
2186 // Wrap each link
2187 foreach ( $author_links as $link => $link_text ) {
2188 $link_class = ' class="bbp-author-' . esc_attr( $link ) . '"';
2189 $links[] = sprintf( $sprint, $link_class, $link_text );
2190 }
2191
2192 // Juggle
2193 $author_links = $links;
2194 unset( $links );
2195
2196 // Filter sections
2197 $sections = apply_filters( 'bbp_get_author_links', $author_links, $r, $args );
2198
2199 // Assemble sections into author link
2200 $author_link = implode( $r['sep'], $sections );
2201
2202 // Only wrap in link if profile exists
2203 if ( empty( $anonymous ) && bbp_user_has_profile( $user_id ) ) {
2204 $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 );
2205 }
2206 }
2207
2208 // Filter & return
2209 return apply_filters( 'bbp_get_author_link', $author_link, $r, $args );
2210 }
2211
2212 /** Capabilities **************************************************************/
2213
2214 /**
2215 * Check if the user can access a specific forum
2216 *
2217 * @since 2.0.0 bbPress (r3057)
2218 * @since 2.6.19 bbPress (r7676) Check forum ancestors by default.
2219 *
2220 * @return bool
2221 */
2222 function bbp_user_can_view_forum( $args = array() ) {
2223
2224 // Parse arguments against default values
2225 $r = bbp_parse_args(
2226 $args,
2227 array(
2228 'user_id' => bbp_get_current_user_id(),
2229 'forum_id' => bbp_get_forum_id(),
2230 'check_ancestors' => true
2231 ),
2232 'user_can_view_forum'
2233 );
2234
2235 // Validate parsed values
2236 $user_id = bbp_get_user_id( $r['user_id'], false, false );
2237 $forum_id = bbp_get_forum_id( $r['forum_id'] );
2238 $retval = false;
2239
2240 // User is a keymaster
2241 if ( ! empty( $user_id ) && bbp_is_user_keymaster( $user_id ) ) {
2242 $retval = true;
2243
2244 // Forum is public, and user can read forums or is not logged in
2245 } elseif ( bbp_is_forum_public( $forum_id, $r['check_ancestors'] ) ) {
2246 $retval = true;
2247
2248 // Forum is private, and user can see it
2249 } elseif ( bbp_is_forum_private( $forum_id, $r['check_ancestors'] ) && user_can( $user_id, 'read_forum', $forum_id ) ) {
2250 $retval = true;
2251
2252 // Forum is hidden, and user can see it
2253 } elseif ( bbp_is_forum_hidden( $forum_id, $r['check_ancestors'] ) && user_can( $user_id, 'read_forum', $forum_id ) ) {
2254 $retval = true;
2255 }
2256
2257 // Filter & return
2258 return apply_filters( 'bbp_user_can_view_forum', $retval, $forum_id, $user_id );
2259 }
2260
2261 /**
2262 * Check if the current user can publish topics
2263 *
2264 * @since 2.0.0 bbPress (r3366)
2265 *
2266 * @return bool
2267 */
2268 function bbp_current_user_can_publish_topics() {
2269
2270 // Users need to earn access
2271 $retval = false;
2272
2273 // Always allow keymasters
2274 if ( bbp_is_user_keymaster() ) {
2275 $retval = true;
2276
2277 // Do not allow anonymous if not enabled
2278 } elseif ( ! is_user_logged_in() && bbp_allow_anonymous() ) {
2279 $retval = true;
2280
2281 // User is logged in
2282 } elseif ( current_user_can( 'publish_topics' ) ) {
2283 $retval = true;
2284 }
2285
2286 // Filter & return
2287 return (bool) apply_filters( 'bbp_current_user_can_publish_topics', $retval );
2288 }
2289
2290 /**
2291 * Check if the current user can publish forums
2292 *
2293 * @since 2.1.0 bbPress (r3549)
2294 *
2295 * @return bool
2296 */
2297 function bbp_current_user_can_publish_forums() {
2298
2299 // Users need to earn access
2300 $retval = false;
2301
2302 // Always allow keymasters
2303 if ( bbp_is_user_keymaster() ) {
2304 $retval = true;
2305
2306 // User is logged in
2307 } elseif ( current_user_can( 'publish_forums' ) ) {
2308 $retval = true;
2309 }
2310
2311 // Filter & return
2312 return (bool) apply_filters( 'bbp_current_user_can_publish_forums', $retval );
2313 }
2314
2315 /**
2316 * Check if the current user can publish replies
2317 *
2318 * @since 2.0.0 bbPress (r3366)
2319 *
2320 * @return bool
2321 */
2322 function bbp_current_user_can_publish_replies() {
2323
2324 // Users need to earn access
2325 $retval = false;
2326
2327 // Always allow keymasters
2328 if ( bbp_is_user_keymaster() ) {
2329 $retval = true;
2330
2331 // Do not allow anonymous if not enabled
2332 } elseif ( ! is_user_logged_in() && bbp_allow_anonymous() ) {
2333 $retval = true;
2334
2335 // User is logged in
2336 } elseif ( current_user_can( 'publish_replies' ) ) {
2337 $retval = true;
2338 }
2339
2340 // Filter & return
2341 return (bool) apply_filters( 'bbp_current_user_can_publish_replies', $retval );
2342 }
2343
2344 /** Forms *********************************************************************/
2345
2346 /**
2347 * The following functions should be turned into mapped meta capabilities in a
2348 * future version. They exist only to remove complex logistical capability
2349 * checks from within template parts.
2350 */
2351
2352 /**
2353 * Get the forums the current user has the ability to see and post to
2354 *
2355 * @since 2.0.0 bbPress (r3127)
2356 *
2357 * @param array $args
2358 *
2359 * @return array
2360 */
2361 function bbp_get_forums_for_current_user( $args = array() ) {
2362
2363 // Parse arguments against default values
2364 $r = bbp_parse_args(
2365 $args,
2366 array(
2367 'post_type' => bbp_get_forum_post_type(),
2368 'post_status' => bbp_get_public_status_id(),
2369 'post__not_in' => bbp_exclude_forum_ids( 'array' ),
2370 'numberposts' => -1
2371 ),
2372 'get_forums_for_current_user'
2373 );
2374
2375 // Get the forums
2376 $forums = get_posts( $r );
2377
2378 // No availabe forums
2379 if ( empty( $forums ) ) {
2380 $forums = false;
2381 }
2382
2383 // Filter & return
2384 return apply_filters( 'bbp_get_forums_for_current_user', $forums, $r, $args );
2385 }
2386
2387 /**
2388 * Performs a series of checks to ensure the current user can create forums.
2389 *
2390 * @since 2.1.0 bbPress (r3549)
2391 *
2392 * @return bool
2393 */
2394 function bbp_current_user_can_access_create_forum_form() {
2395
2396 // Users need to earn access
2397 $retval = false;
2398
2399 // Always allow keymasters
2400 if ( bbp_is_user_keymaster() ) {
2401 $retval = true;
2402
2403 // Looking at a single forum & forum is open
2404 } elseif ( ( is_page() || is_single() ) && bbp_is_forum_open() ) {
2405 $retval = bbp_current_user_can_publish_forums();
2406
2407 // User can edit this forum
2408 } else {
2409 $retval = current_user_can( 'edit_forum', bbp_get_forum_id() );
2410 }
2411
2412 // Filter & return
2413 return (bool) apply_filters( 'bbp_current_user_can_access_create_forum_form', (bool) $retval );
2414 }
2415
2416 /**
2417 * Performs a series of checks to ensure the current user can create topics.
2418 *
2419 * @since 2.0.0 bbPress (r3127)
2420 *
2421 * @return bool
2422 */
2423 function bbp_current_user_can_access_create_topic_form() {
2424
2425 // Users need to earn access
2426 $retval = false;
2427
2428 // Always allow keymasters
2429 if ( bbp_is_user_keymaster() ) {
2430 $retval = true;
2431
2432 // Looking at a single forum & forum is open
2433 } elseif ( ( bbp_is_single_forum() || is_page() || is_single() ) && bbp_is_forum_open() ) {
2434 $retval = bbp_current_user_can_publish_topics();
2435
2436 // User can edit this topic
2437 } else {
2438 $retval = current_user_can( 'edit_topic', bbp_get_topic_id() );
2439 }
2440
2441 // Filter & return
2442 return (bool) apply_filters( 'bbp_current_user_can_access_create_topic_form', (bool) $retval );
2443 }
2444
2445 /**
2446 * Performs a series of checks to ensure the current user can create replies.
2447 *
2448 * @since 2.0.0 bbPress (r3366)
2449 *
2450 * @return bool
2451 */
2452 function bbp_current_user_can_access_create_reply_form() {
2453
2454 // Users need to earn access
2455 $retval = false;
2456
2457 // Always allow keymasters
2458 if ( bbp_is_user_keymaster() ) {
2459 $retval = true;
2460
2461 // Looking at single topic (and singulars), topic is open, and forum is open
2462 } elseif ( ( bbp_is_single_topic() || is_singular() ) && bbp_is_topic_open() && bbp_is_forum_open() && bbp_is_topic_published() ) {
2463 $retval = bbp_current_user_can_publish_replies();
2464
2465 // User can edit this reply
2466 } elseif ( bbp_get_reply_id() ) {
2467 $retval = current_user_can( 'edit_reply', bbp_get_reply_id() );
2468
2469 // User can edit this topic
2470 } elseif ( bbp_get_topic_id() ) {
2471 $retval = current_user_can( 'edit_topic', bbp_get_topic_id() );
2472 }
2473
2474 // Filter & return
2475 return (bool) apply_filters( 'bbp_current_user_can_access_create_reply_form', (bool) $retval );
2476 }
2477
2478 /**
2479 * Check if the current user can access structural topic moderation actions.
2480 *
2481 * This covers topic merges, topic splits, and reply moves. Their handlers
2482 * perform the same capability checks independently when processing a request.
2483 *
2484 * @since 2.6.19 bbPress (r7618)
2485 *
2486 * @param int $topic_id Optional. Topic ID.
2487 * @return bool True if the current user can moderate and edit the topic.
2488 */
2489 function bbp_current_user_can_access_topic_moderation( $topic_id = 0 ) {
2490 $topic_id = bbp_get_topic_id( $topic_id );
2491 $retval = ! empty( $topic_id )
2492 && current_user_can( 'moderate', $topic_id )
2493 && current_user_can( 'edit_topic', $topic_id );
2494
2495 // Filter & return
2496 return (bool) apply_filters( 'bbp_current_user_can_access_topic_moderation', $retval, $topic_id );
2497 }
2498
2499 /**
2500 * Performs a series of checks to ensure the current user should see the
2501 * anonymous user form fields.
2502 *
2503 * @since 2.5.0 bbPress (r5120)
2504 *
2505 * @return bool
2506 */
2507 function bbp_current_user_can_access_anonymous_user_form() {
2508
2509 // Users need to earn access
2510 $retval = false;
2511
2512 // User is not logged in, and anonymous posting is allowed
2513 if ( bbp_is_anonymous() ) {
2514 $retval = true;
2515
2516 // User is editing a topic, and topic is authored by anonymous user
2517 } elseif ( bbp_is_topic_edit() && bbp_is_topic_anonymous() ) {
2518 $retval = true;
2519
2520 // User is editing a reply, and reply is authored by anonymous user
2521 } elseif ( bbp_is_reply_edit() && bbp_is_reply_anonymous() ) {
2522 $retval = true;
2523 }
2524
2525 // Filter & return
2526 return (bool) apply_filters( 'bbp_current_user_can_access_anonymous_user_form', (bool) $retval );
2527 }
2528
2529 /** Moderators ****************************************************************/
2530
2531 /**
2532 * Output the moderators of a forum
2533 *
2534 * @since 2.6.0 bbPress (r6056)
2535 *
2536 * @param int $forum_id Optional. Topic id
2537 * @param array $args See {@link bbp_get_moderator_list()}
2538 */
2539 function bbp_moderator_list( $forum_id = 0, $args = array() ) {
2540 echo bbp_get_moderator_list( $forum_id, $args );
2541 }
2542
2543 /**
2544 * Return the moderators for an object
2545 *
2546 * @since 2.6.0 bbPress (r6056)
2547 *
2548 * @param int $object_id Optional. Object id
2549 * @param array $args This function supports these arguments:
2550 * - before: Before the tag list
2551 * - sep: Tag separator
2552 * - after: After the tag list
2553 *
2554 * @return string Moderator list of the object
2555 */
2556 function bbp_get_moderator_list( $object_id = 0, $args = array() ) {
2557
2558 // Parse arguments against default values
2559 $r = bbp_parse_args(
2560 $args,
2561 array(
2562 'before' => '<div class="bbp-moderators"><p>' . esc_html__( 'Moderators:', 'bbpress' ) . '&nbsp;',
2563 'sep' => ', ',
2564 'after' => '</p></div>',
2565 'none' => ''
2566 ),
2567 'get_moderator_list'
2568 );
2569
2570 // Get forum moderators
2571 $user_ids = bbp_get_moderator_ids( $object_id );
2572 if ( ! empty( $user_ids ) ) {
2573
2574 // In admin, use nicenames
2575 if ( is_admin() ) {
2576 $users = bbp_get_user_nicenames_from_ids( $user_ids );
2577
2578 // In theme, use display names & profile links
2579 } else {
2580 foreach ( $user_ids as $user_id ) {
2581 $users[] = bbp_get_user_profile_link( $user_id );
2582 }
2583 }
2584
2585 $retval = $r['before'] . implode( $r['sep'], $users ) . $r['after'];
2586
2587 // No forum moderators
2588 } else {
2589 $retval = $r['none'];
2590 }
2591
2592 // Filter & return
2593 return apply_filters( 'bbp_get_moderator_list', $retval );
2594 }
2595