PluginProbe
bbPress / trunk
bbPress vtrunk
trunk 2.0 2.0-beta-1 2.0-beta-2b 2.0-beta-3 2.0-beta-3b 2.0-rc-2 2.0-rc-3 2.0-rc-4 2.0-rc-5 2.0.1 2.0.2 2.0.3 2.1 2.1-beta-1 2.1-rc1 2.1-rc2 2.1-rc3 2.1-rc4 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.2.2 All 71 releases
bbpress / includes / users / template.php

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

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