[ Index ]

PHP Cross Reference of BBPress

title

Body

[close]

/src/includes/users/ -> capabilities.php (source)

   1  <?php
   2  
   3  /**
   4   * bbPress User Capabilities
   5   *
   6   * Used to map user capabilities to WordPress's existing capabilities.
   7   *
   8   * @package bbPress
   9   * @subpackage Capabilities
  10   */
  11  
  12  /**
  13   * Maps primary capabilities
  14   *
  15   * @since 2.2.0 bbPress (r4242)
  16   *
  17   * @param array  $caps Capabilities for meta capability.
  18   * @param string $cap Capability name.
  19   * @param int    $user_id User id.
  20   * @param array  $args Arguments.
  21   *
  22   * @return array Actual capabilities for meta capability
  23   */
  24  function bbp_map_primary_meta_caps( $caps = array(), $cap = '', $user_id = 0, $args = array() ) {
  25  
  26      // What capability is being checked?
  27      switch ( $cap ) {
  28          case 'spectate' :
  29  
  30              // Do not allow inactive users.
  31              if ( bbp_is_user_inactive( $user_id ) ) {
  32                  $caps = array( 'do_not_allow' );
  33  
  34              // Default to the current cap.
  35              } else {
  36                  $caps = array( $cap );
  37              }
  38              break;
  39  
  40          case 'participate' :
  41  
  42              // Do not allow inactive users.
  43              if ( bbp_is_user_inactive( $user_id ) ) {
  44                  $caps = array( 'do_not_allow' );
  45  
  46              // Default to the current cap.
  47              } else {
  48                  $caps = array( $cap );
  49              }
  50              break;
  51  
  52          case 'moderate' :
  53  
  54              // Do not allow inactive users.
  55              if ( bbp_is_user_inactive( $user_id ) ) {
  56                  $caps = array( 'do_not_allow' );
  57  
  58              // Keymasters can always moderate.
  59              } elseif ( bbp_is_user_keymaster( $user_id ) ) {
  60                  $caps = array( 'spectate' );
  61  
  62              // Check if user can moderate forum.
  63              } elseif ( bbp_allow_forum_mods() ) {
  64                  $caps = array( $cap );
  65  
  66                  // Bail if no post to check.
  67                  if ( empty( $args[0] ) ) {
  68                      break;
  69                  }
  70  
  71                  // Get the post.
  72                  $_post = get_post( $args[0] );
  73                  if ( empty( $_post ) ) {
  74                      break;
  75                  }
  76  
  77                  // Get forum ID for specific type of post.
  78                  switch ( $_post->post_type ) {
  79  
  80                      // Forum.
  81                      case bbp_get_forum_post_type() :
  82                          $forum_id = bbp_get_forum_id( $_post->ID );
  83                          break;
  84  
  85                      // Topic.
  86                      case bbp_get_topic_post_type() :
  87                          $forum_id = bbp_get_topic_forum_id( $_post->ID );
  88                          break;
  89  
  90                      // Reply.
  91                      case bbp_get_reply_post_type() :
  92                          $forum_id = bbp_get_reply_forum_id( $_post->ID );
  93                          break;
  94  
  95                      // Any other post type defaults to 0.
  96                      default :
  97                          $forum_id = 0;
  98                          break;
  99                  }
 100  
 101                  // Bail if no forum ID.
 102                  if ( empty( $forum_id ) ) {
 103                      break;
 104                  }
 105  
 106                  // User is mod of this forum
 107                  if ( bbp_is_object_of_user( $forum_id, $user_id, '_bbp_moderator_id' ) ) {
 108                      $caps = array( 'spectate' );
 109                  }
 110              }
 111  
 112              break;
 113  
 114          /** Super Moderators **************************************************/
 115  
 116          case 'edit_user'  :
 117          case 'edit_users' :
 118  
 119              // Moderators can edit users if super moderators is enabled
 120              if ( bbp_allow_super_mods() ) {
 121  
 122                  // Get the user ID
 123                  $_user_id = ! empty( $args[0] )
 124                      ? (int) $args[0]
 125                      : bbp_get_displayed_user_id();
 126  
 127                  // Users can always edit themselves, so only map for others
 128                  if ( ! empty( $_user_id ) && ( $_user_id !== $user_id ) ) {
 129  
 130                      // Super moderators cannot edit keymasters
 131                      if ( ! bbp_is_user_keymaster( $_user_id ) ) {
 132                          $caps = array( 'moderate' );
 133                      }
 134                  }
 135              }
 136  
 137              break;
 138      }
 139  
 140      // Filter & return
 141      return (array) apply_filters( 'bbp_map_primary_meta_caps', $caps, $cap, $user_id, $args );
 142  }
 143  
 144  /**
 145   * Set a user's role in the forums
 146   *
 147   * @since 2.1.0 bbPress (r3860)
 148   *
 149   * @param int $user_id
 150   *
 151   * @return mixed False if no change. String of new role if changed.
 152   */
 153  function bbp_set_user_role( $user_id = 0, $new_role = '' ) {
 154  
 155      // Validate user id
 156      $user_id = bbp_get_user_id( $user_id, false, false );
 157      $user    = get_userdata( $user_id );
 158  
 159      // User exists
 160      if ( ! empty( $user ) ) {
 161  
 162          // Get user forum role
 163          $role = bbp_get_user_role( $user_id );
 164  
 165          // User already has this role so no new role is set
 166          if ( $new_role === $role ) {
 167              $new_role = false;
 168  
 169          // User role is different than the new (valid) role
 170          } elseif ( bbp_is_valid_role( $new_role ) ) {
 171  
 172              // Remove the old role
 173              if ( ! empty( $role ) ) {
 174                  $user->remove_role( $role );
 175              }
 176  
 177              // Add the new role
 178              if ( ! empty( $new_role ) ) {
 179                  $user->add_role( $new_role );
 180              }
 181          }
 182  
 183      // User does don exist so return false
 184      } else {
 185          $new_role = false;
 186      }
 187  
 188      // Filter & return
 189      return apply_filters( 'bbp_set_user_role', $new_role, $user_id, $user );
 190  }
 191  
 192  /**
 193   * Return a user's forums role
 194   *
 195   * @since 2.1.0 bbPress (r3860)
 196   *
 197   * @param int $user_id
 198   *
 199   * @return string
 200   */
 201  function bbp_get_user_role( $user_id = 0 ) {
 202  
 203      // Validate user id
 204      $user_id = bbp_get_user_id( $user_id );
 205      $user    = get_userdata( $user_id );
 206      $role    = false;
 207  
 208      // User has roles so look for a bbPress one
 209      if ( ! empty( $user->roles ) ) {
 210  
 211          // Look for a bbPress role
 212          $roles = array_intersect(
 213              array_values( $user->roles ),
 214              array_keys( bbp_get_dynamic_roles() )
 215          );
 216  
 217          // If there's a role in the array, use the first one. This isn't very
 218          // smart, but since roles aren't exactly hierarchical, and bbPress
 219          // does not yet have a UI for multiple user roles, it's fine for now.
 220          if ( ! empty( $roles ) ) {
 221              $role = array_shift( $roles );
 222          }
 223      }
 224  
 225      // Filter & return
 226      return apply_filters( 'bbp_get_user_role', $role, $user_id, $user );
 227  }
 228  
 229  /**
 230   * Return a user's blog role
 231   *
 232   * @since 2.3.0 bbPress (r4446)
 233   *
 234   * @param int $user_id
 235   *
 236   * @return string
 237   */
 238  function bbp_get_user_blog_role( $user_id = 0 ) {
 239  
 240      // Validate user id
 241      $user_id = bbp_get_user_id( $user_id );
 242      $user    = get_userdata( $user_id );
 243      $role    = false;
 244  
 245      // User has roles so lets
 246      if ( ! empty( $user->roles ) ) {
 247  
 248          // Look for a non bbPress role
 249          $roles = array_intersect(
 250              array_values( $user->roles ),
 251              array_keys( bbp_get_blog_roles() )
 252          );
 253  
 254          // If there's a role in the array, use the first one. This isn't very
 255          // smart, but since roles aren't exactly hierarchical, and WordPress
 256          // does not yet have a UI for multiple user roles, it's fine for now.
 257          if ( ! empty( $roles ) ) {
 258              $role = array_shift( $roles );
 259          }
 260      }
 261  
 262      // Filter & return
 263      return apply_filters( 'bbp_get_user_blog_role', $role, $user_id, $user );
 264  }
 265  
 266  /**
 267   * Helper function hooked to 'bbp_profile_update' action to save or
 268   * update user roles and capabilities.
 269   *
 270   * @since 2.2.0 bbPress (r4235)
 271   *
 272   * @param int $user_id
 273   */
 274  function bbp_profile_update_role( $user_id = 0 ) {
 275  
 276      // Bail if no user ID was passed
 277      if ( empty( $user_id ) ) {
 278          return;
 279      }
 280  
 281      // Bail if no role
 282      if ( ! isset( $_POST['bbp-forums-role'] ) ) {
 283          return;
 284      }
 285  
 286      // Forums role we want the user to have
 287      $new_role    = sanitize_key( $_POST['bbp-forums-role'] );
 288      $forums_role = bbp_get_user_role( $user_id );
 289  
 290      // Bail if no role change
 291      if ( $new_role === $forums_role ) {
 292          return;
 293      }
 294  
 295      // Bail if trying to set their own role
 296      if ( bbp_is_user_home_edit() ) {
 297          return;
 298      }
 299  
 300      // Bail if current user cannot promote the passing user
 301      if ( ! current_user_can( 'promote_user', $user_id ) ) {
 302          return;
 303      }
 304  
 305      // Set the new forums role
 306      bbp_set_user_role( $user_id, $new_role );
 307  }
 308  
 309  /**
 310   * Check if a role ID is valid
 311   *
 312   * This helper function accepts a role ID as a string, and compares it against
 313   * the array of registered dynamic roles.
 314   *
 315   * Use this function anytime you are manually attempting to set a user role
 316   * without using the bbp_set_user_role() function, or if you need to halt
 317   * additional processing during role validation.
 318   *
 319   * @since 2.6.5
 320   *
 321   * @param string $role A well-formed (string) role ID to validate
 322   *
 323   * @return bool True if role is valid. False if role is not valid.
 324   */
 325  function bbp_is_valid_role( $role = '' ) {
 326  
 327      // Default return value
 328      $retval = false;
 329  
 330      // Skip if no role to check
 331      if ( ! empty( $role ) && is_string( $role ) ) {
 332  
 333          // Get the dynamic role IDs
 334          $roles = array_keys( bbp_get_dynamic_roles() );
 335  
 336          // Skip if no known role IDs
 337          if ( ! empty( $roles ) ) {
 338  
 339              // Is role in dynamic roles array?
 340              $retval = in_array( $role, $roles, true );
 341          }
 342      }
 343  
 344      // Filter & return
 345      return (bool) apply_filters( 'bbp_is_valid_role', $retval, $role );
 346  }
 347  
 348  /**
 349   * Add the default role to the current user if needed
 350   *
 351   * This function will bail if the forum is not global in a multisite
 352   * installation of WordPress, or if the user is marked as spam or deleted.
 353   *
 354   * @since 2.0.0 bbPress (r3380)
 355   *
 356   * @return If not multisite, not global, or user is deleted/spammed
 357   */
 358  function bbp_set_current_user_default_role() {
 359  
 360      /** Sanity ****************************************************************/
 361  
 362      // Bail if deactivating bbPress
 363      if ( bbp_is_deactivation() ) {
 364          return;
 365      }
 366  
 367      // Catch all, to prevent premature user initialization
 368      if ( ! did_action( 'set_current_user' ) ) {
 369          return;
 370      }
 371  
 372      // Bail if not logged in or already a member of this site
 373      if ( ! is_user_logged_in() ) {
 374          return;
 375      }
 376  
 377      // Get the current user ID
 378      $user_id = bbp_get_current_user_id();
 379  
 380      // Bail if user already has a forums role
 381      if ( bbp_get_user_role( $user_id ) ) {
 382          return;
 383      }
 384  
 385      // Bail if user is marked as spam or is deleted
 386      if ( bbp_is_user_inactive( $user_id ) ) {
 387          return;
 388      }
 389  
 390      /** Ready *****************************************************************/
 391  
 392      // Load up bbPress once
 393      $bbp         = bbpress();
 394  
 395      // Get whether or not to add a role to the user account
 396      $add_to_site = bbp_allow_global_access();
 397  
 398      // Get the current user's WordPress role. Set to empty string if none found.
 399      $user_role   = bbp_get_user_blog_role( $user_id );
 400  
 401      // Get the role map
 402      $role_map    = bbp_get_user_role_map();
 403  
 404      /** Forum Role ************************************************************/
 405  
 406      // Use a mapped role or default role
 407      $new_role = empty( $user_role ) || ! isset( $role_map[ $user_role ] )
 408          ? bbp_get_default_role()
 409          : $role_map[ $user_role ];
 410  
 411      /** Add or Map ************************************************************/
 412  
 413      // Add the user to the site
 414      if ( true === $add_to_site ) {
 415          bbp_set_user_role( $user_id, $new_role );
 416  
 417      // Don't add the user, but still give them the correct caps dynamically
 418      } else {
 419          $bbp->current_user->caps[ $new_role ] = true;
 420          $bbp->current_user->get_role_caps();
 421      }
 422  }
 423  
 424  /**
 425   * Return a map of WordPress roles to bbPress roles. Used to automatically grant
 426   * appropriate bbPress roles to WordPress users that wouldn't already have a
 427   * role in the forums. Also guarantees WordPress admins get the Keymaster role.
 428   *
 429   * @since 2.2.0 bbPress (r4334)
 430   *
 431   * @return array Filtered array of WordPress roles to bbPress roles
 432   */
 433  function bbp_get_user_role_map() {
 434  
 435      // Get the default role once here
 436      $default_role = bbp_get_default_role();
 437  
 438      // Filter & return
 439      return (array) apply_filters( 'bbp_get_user_role_map', array(
 440          'administrator' => bbp_get_keymaster_role(),
 441          'editor'        => $default_role,
 442          'author'        => $default_role,
 443          'contributor'   => $default_role,
 444          'subscriber'    => $default_role
 445      ) );
 446  }
 447  
 448  /** User Status ***************************************************************/
 449  
 450  /**
 451   * Checks if the user has been marked as a spammer.
 452   *
 453   * @since 2.0.0 bbPress (r3355)
 454   *
 455   * @param int $user_id int The ID for the user.
 456   * @return bool True if spammer, False if not.
 457   */
 458  function bbp_is_user_spammer( $user_id = 0 ) {
 459  
 460      // Default to current user
 461      if ( empty( $user_id ) && is_user_logged_in() ) {
 462          $user_id = bbp_get_current_user_id();
 463      }
 464  
 465      // No user to check
 466      if ( empty( $user_id ) ) {
 467          return false;
 468      }
 469  
 470      // Assume user is not spam
 471      $is_spammer = false;
 472  
 473      // Get user data
 474      $user = get_userdata( $user_id );
 475  
 476      // No user found
 477      if ( empty( $user ) ) {
 478          $is_spammer = false;
 479  
 480      // Check if spam
 481      } elseif ( ! empty( $user->spam ) ) {
 482          $is_spammer = true;
 483      }
 484  
 485      // Filter & return
 486      return (bool) apply_filters( 'bbp_core_is_user_spammer', $is_spammer );
 487  }
 488  
 489  /**
 490   * Mark a users topics and replies as spam when the user is marked as spam
 491   *
 492   * @since 2.0.0 bbPress (r3405)
 493   *
 494   * @param int $user_id Optional. User ID to spam. Defaults to displayed user.
 495   *
 496   * @return bool If no user ID passed.
 497   */
 498  function bbp_make_spam_user( $user_id = 0 ) {
 499  
 500      // Use displayed user if it's not yourself
 501      if ( empty( $user_id ) && bbp_is_single_user() && ! bbp_is_user_home() ) {
 502          $user_id = bbp_get_displayed_user_id();
 503      }
 504  
 505      // Bail if no user ID
 506      if ( empty( $user_id ) ) {
 507          return false;
 508      }
 509  
 510      // Bail if user ID is keymaster
 511      if ( bbp_is_user_keymaster( $user_id ) ) {
 512          return false;
 513      }
 514  
 515      // Arm the torpedos
 516      $bbp_db = bbp_db();
 517  
 518      // Get the blog IDs of the user to mark as spam
 519      $blogs = get_blogs_of_user( $user_id, true );
 520  
 521      // If user has no blogs, they are a guest on this site
 522      if ( empty( $blogs ) ) {
 523          $blogs[ $bbp_db->blogid ] = array();
 524      }
 525  
 526      // Get array of post types to mark as spam
 527      $post_types = array( bbp_get_topic_post_type(), bbp_get_reply_post_type() );
 528      $post_types = "'" . implode( "', '", $post_types ) . "'";
 529  
 530      // Get array of statuses to mark as spam
 531      $post_statuses = bbp_get_public_topic_statuses();
 532      $post_statuses = "'" . implode( "', '", $post_statuses ) . "'";
 533  
 534      // Loop through blogs and remove their posts
 535      foreach ( (array) array_keys( $blogs ) as $blog_id ) {
 536  
 537          // Switch to the site ID
 538          bbp_switch_to_site( $blog_id );
 539  
 540          // Get topics and replies
 541          $query = $bbp_db->prepare( "SELECT ID FROM {$bbp_db->posts} WHERE post_author = %d AND post_status IN ( {$post_statuses} ) AND post_type IN ( {$post_types} )", $user_id );
 542          $posts = $bbp_db->get_col( $query );
 543  
 544          // Loop through posts and spam them
 545          if ( ! empty( $posts ) ) {
 546              foreach ( $posts as $post_id ) {
 547  
 548                  // The routines for topics ang replies are different, so use the
 549                  // correct one based on the post type
 550                  switch ( get_post_type( $post_id ) ) {
 551  
 552                      case bbp_get_topic_post_type() :
 553                          bbp_spam_topic( $post_id );
 554                          break;
 555  
 556                      case bbp_get_reply_post_type() :
 557                          bbp_spam_reply( $post_id );
 558                          break;
 559                  }
 560              }
 561          }
 562  
 563          // Switch back to current site
 564          bbp_restore_current_site();
 565      }
 566  
 567      // Delete user options
 568      bbp_delete_user_options( $user_id );
 569  
 570      // Success
 571      return true;
 572  }
 573  
 574  /**
 575   * Mark a users topics and replies as spam when the user is marked as spam
 576   *
 577   * @since 2.0.0 bbPress (r3405)
 578   *
 579   * @param int $user_id Optional. User ID to unspam. Defaults to displayed user.
 580   *
 581   * @return bool If no user ID passed.
 582   */
 583  function bbp_make_ham_user( $user_id = 0 ) {
 584  
 585      // Use displayed user if it's not yourself
 586      if ( empty( $user_id ) && bbp_is_single_user() && ! bbp_is_user_home() ) {
 587          $user_id = bbp_get_displayed_user_id();
 588      }
 589  
 590      // Bail if no user ID
 591      if ( empty( $user_id ) ) {
 592          return false;
 593      }
 594  
 595      // Bail if user ID is keymaster
 596      if ( bbp_is_user_keymaster( $user_id ) ) {
 597          return false;
 598      }
 599  
 600      // Arm the torpedos
 601      $bbp_db = bbp_db();
 602  
 603      // Get the blog IDs of the user to mark as spam
 604      $blogs = get_blogs_of_user( $user_id, true );
 605  
 606      // If user has no blogs, they are a guest on this site
 607      if ( empty( $blogs ) ) {
 608          $blogs[ $bbp_db->blogid ] = array();
 609      }
 610  
 611      // Get array of post types to mark as spam
 612      $post_types = array( bbp_get_topic_post_type(), bbp_get_reply_post_type() );
 613      $post_types = "'" . implode( "', '", $post_types ) . "'";
 614  
 615      // Get array of statuses to unmark as spam
 616      $post_statuses = array( bbp_get_spam_status_id() );
 617      $post_statuses = "'" . implode( "', '", $post_statuses ) . "'";
 618  
 619      // Loop through blogs and remove their posts
 620      foreach ( (array) array_keys( $blogs ) as $blog_id ) {
 621  
 622          // Switch to the site ID
 623          bbp_switch_to_site( $blog_id );
 624  
 625          // Get topics and replies
 626          $query = $bbp_db->prepare( "SELECT ID FROM {$bbp_db->posts} WHERE post_author = %d AND post_status IN ( {$post_statuses} ) AND post_type IN ( {$post_types} )", $user_id );
 627          $posts = $bbp_db->get_col( $query );
 628  
 629          // Loop through posts and spam them
 630          if ( ! empty( $posts ) ) {
 631              foreach ( $posts as $post_id ) {
 632  
 633                  // The routines for topics ang replies are different, so use the
 634                  // correct one based on the post type
 635                  switch ( get_post_type( $post_id ) ) {
 636  
 637                      case bbp_get_topic_post_type() :
 638                          bbp_unspam_topic( $post_id );
 639                          break;
 640  
 641                      case bbp_get_reply_post_type() :
 642                          bbp_unspam_reply( $post_id );
 643                          break;
 644                  }
 645              }
 646          }
 647  
 648          // Switch back to current site
 649          bbp_restore_current_site();
 650      }
 651  
 652      // Update topic & reply counts
 653      bbp_update_user_topic_count( $user_id, bbp_get_user_topic_count_raw( $user_id ) );
 654      bbp_update_user_reply_count( $user_id, bbp_get_user_reply_count_raw( $user_id ) );
 655  
 656      // Update last posted (to now)
 657      bbp_update_user_last_posted( $user_id );
 658  
 659      // Success
 660      return true;
 661  }
 662  
 663  /**
 664   * Checks if the user has been marked as deleted.
 665   *
 666   * @since 2.0.0 bbPress (r3355)
 667   *
 668   * @param int $user_id int The ID for the user.
 669   * @return bool True if deleted, False if not.
 670   */
 671  function bbp_is_user_deleted( $user_id = 0 ) {
 672  
 673      // Default to current user
 674      if ( empty( $user_id ) && is_user_logged_in() ) {
 675          $user_id = bbp_get_current_user_id();
 676      }
 677  
 678      // No user to check
 679      if ( empty( $user_id ) ) {
 680          return false;
 681      }
 682  
 683      // Assume user is not deleted
 684      $is_deleted = false;
 685  
 686      // Get user data
 687      $user = get_userdata( $user_id );
 688  
 689      // No user found
 690      if ( empty( $user ) ) {
 691          $is_deleted = true;
 692  
 693      // Check if deleted
 694      } elseif ( ! empty( $user->deleted ) ) {
 695          $is_deleted = true;
 696      }
 697  
 698      // Filter & return
 699      return (bool) apply_filters( 'bbp_core_is_user_deleted', $is_deleted );
 700  }
 701  
 702  /**
 703   * Checks if user is active
 704   *
 705   * @since 2.0.0 bbPress (r3502)
 706   *
 707   * @param int $user_id The user ID to check
 708   * @return bool True if public, false if not
 709   */
 710  function bbp_is_user_active( $user_id = 0 ) {
 711  
 712      // No user to check
 713      $user_id = bbp_get_user_id( $user_id, false, true );
 714      if ( empty( $user_id ) ) {
 715          return false;
 716      }
 717  
 718      // Check spam
 719      if ( bbp_is_user_spammer( $user_id ) ) {
 720          return false;
 721      }
 722  
 723      // Check deleted
 724      if ( bbp_is_user_deleted( $user_id ) ) {
 725          return false;
 726      }
 727  
 728      // Assume true if not spam or deleted
 729      return true;
 730  }
 731  
 732  /**
 733   * Checks if user is not active.
 734   *
 735   * @since 2.0.0 bbPress (r3502)
 736   *
 737   * @param int $user_id The user ID to check. Defaults to current user ID
 738   * @return bool True if inactive, false if active
 739   */
 740  function bbp_is_user_inactive( $user_id = 0 ) {
 741      return ! bbp_is_user_active( $user_id );
 742  }
 743  
 744  /**
 745   * Checks if user is a keymaster
 746   *
 747   * @since 2.3.0 bbPress (r4783)
 748   *
 749   * @param int $user_id
 750   * @return bool True if keymaster, false if not
 751   */
 752  function bbp_is_user_keymaster( $user_id = 0 ) {
 753      $_user_id = bbp_get_user_id( $user_id, false, true );
 754      $retval   = user_can( $_user_id, 'keep_gate' );
 755  
 756      // Filter & return
 757      return (bool) apply_filters( 'bbp_is_user_keymaster', $retval, $_user_id, $user_id );
 758  }
 759  
 760  /**
 761   * Does a user have a profile for the current site
 762   *
 763   * @since 2.2.0 bbPress (r4362)
 764   *
 765   * @param int $user_id User ID to check
 766   *
 767   * @return bool Whether or not the user has a profile on this blog_id.
 768   */
 769  function bbp_user_has_profile( $user_id = 0 ) {
 770  
 771      // Assume every user has a profile
 772      $retval  = true;
 773  
 774      // Validate user ID, default to displayed or current user
 775      $user_id = bbp_get_user_id( $user_id, true, true );
 776  
 777      // Try to get this user's data
 778      $user    = get_userdata( $user_id );
 779  
 780      // No user found, return false
 781      if ( empty( $user ) ) {
 782          $retval = false;
 783  
 784      // User is inactive, and current user is not a keymaster
 785      } elseif ( ! bbp_is_user_keymaster() && bbp_is_user_inactive( $user->ID ) ) {
 786          $retval = false;
 787      }
 788  
 789      // Filter & return
 790      return (bool) apply_filters( 'bbp_show_user_profile', $retval, $user_id );
 791  }
 792  
 793  /** Moderators ****************************************************************/
 794  
 795  /**
 796   * Add a moderator to an object
 797   *
 798   * @since 2.6.0 bbPress (r6056)
 799   *
 800   * @param int    $object_id   Traditionally a post ID
 801   * @param int    $user_id     User ID
 802   * @param string $object_type Type of meta (post,term,user,comment)
 803   *
 804   * @return bool
 805   */
 806  function bbp_add_moderator( $object_id = 0, $user_id = 0, $object_type = 'post' ) {
 807      return bbp_add_user_to_object( $object_id, $user_id, '_bbp_moderator_id', $object_type );
 808  }
 809  
 810  /**
 811   * Remove a moderator user ID from an object
 812   *
 813   * @since 2.6.0 bbPress (r6056)
 814   *
 815   * @param int    $object_id   Traditionally a post ID
 816   * @param int    $user_id     User ID
 817   * @param string $object_type Type of meta (post,term,user,comment)
 818   *
 819   * @return bool
 820   */
 821  function bbp_remove_moderator( $object_id = 0, $user_id = 0, $object_type = 'post' ) {
 822      return bbp_remove_user_from_object( $object_id, $user_id, '_bbp_moderator_id', $object_type );
 823  }
 824  
 825  /**
 826   * Get user IDs of moderators for an object
 827   *
 828   * @since 2.6.0 bbPress (r6056)
 829   *
 830   * @param int    $object_id   Traditionally a post ID
 831   * @param string $object_type Type of meta (post,term,user,comment)
 832   *
 833   * @return array
 834   */
 835  function bbp_get_moderator_ids( $object_id = 0, $object_type = 'post' ) {
 836      return bbp_get_users_for_object( $object_id, '_bbp_moderator_id', $object_type );
 837  }
 838  
 839  /**
 840   * Get moderators for a specific object ID. Will return global moderators when
 841   * object ID is empty.
 842   *
 843   * @since 2.6.0 bbPress (r6056)
 844   *
 845   * @param int    $object_id   Traditionally a post ID
 846   * @param string $object_type Type of meta (post,term,user,comment)
 847   *
 848   * @return array
 849   */
 850  function bbp_get_moderators( $object_id = 0, $object_type = 'post' ) {
 851  
 852      // Get global moderators
 853      if ( empty( $object_id ) ) {
 854          $users = get_users( array(
 855              'role__in' => bbp_get_moderator_role(),
 856          ) );
 857  
 858      // Get object moderators
 859      } else {
 860          $users = get_users( array(
 861              'include' => bbp_get_moderator_ids( $object_id, $object_type ),
 862          ) );
 863      }
 864  
 865      // Filter & return
 866      return (array) apply_filters( 'bbp_get_moderators', $users, $object_id, $object_type );
 867  }


Generated: Sat Dec 21 01:00:52 2024 Cross-referenced by PHPXref 0.7.1