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

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

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