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