PluginProbe
bbPress / 2.6.6
bbPress v2.6.6
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.6, at includes/users/template.php

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