[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-members/ -> bp-members-functions.php (source)

   1  <?php
   2  /**
   3   * BuddyPress Member Functions.
   4   *
   5   * Functions specific to the members component.
   6   *
   7   * @package BuddyPress
   8   * @subpackage MembersFunctions
   9   * @since 1.5.0
  10   */
  11  
  12  // Exit if accessed directly.
  13  defined( 'ABSPATH' ) || exit;
  14  
  15  /**
  16   * Check for the existence of a Members directory page.
  17   *
  18   * @since 1.5.0
  19   *
  20   * @return bool True if found, otherwise false.
  21   */
  22  function bp_members_has_directory() {
  23      $bp = buddypress();
  24  
  25      return (bool) !empty( $bp->pages->members->id );
  26  }
  27  
  28  /**
  29   * Define the slug constants for the Members component.
  30   *
  31   * Handles the three slug constants used in the Members component -
  32   * BP_MEMBERS_SLUG, BP_REGISTER_SLUG, and BP_ACTIVATION_SLUG. If these
  33   * constants are not overridden in wp-config.php or bp-custom.php, they are
  34   * defined here to match the slug of the corresponding WP pages.
  35   *
  36   * In general, fallback values are only used during initial BP page creation,
  37   * when no slugs have been explicitly defined.
  38   *
  39   * @since 1.5.0
  40   */
  41  function bp_core_define_slugs() {
  42      $bp = buddypress();
  43  
  44      // No custom members slug.
  45      if ( !defined( 'BP_MEMBERS_SLUG' ) ) {
  46          if ( !empty( $bp->pages->members ) ) {
  47              define( 'BP_MEMBERS_SLUG', $bp->pages->members->slug );
  48          } else {
  49              define( 'BP_MEMBERS_SLUG', 'members' );
  50          }
  51      }
  52  
  53      // No custom registration slug.
  54      if ( !defined( 'BP_REGISTER_SLUG' ) ) {
  55          if ( !empty( $bp->pages->register ) ) {
  56              define( 'BP_REGISTER_SLUG', $bp->pages->register->slug );
  57          } else {
  58              define( 'BP_REGISTER_SLUG', 'register' );
  59          }
  60      }
  61  
  62      // No custom activation slug.
  63      if ( !defined( 'BP_ACTIVATION_SLUG' ) ) {
  64          if ( !empty( $bp->pages->activate ) ) {
  65              define( 'BP_ACTIVATION_SLUG', $bp->pages->activate->slug );
  66          } else {
  67              define( 'BP_ACTIVATION_SLUG', 'activate' );
  68          }
  69      }
  70  }
  71  add_action( 'bp_setup_globals', 'bp_core_define_slugs', 11 );
  72  
  73  /**
  74   * Fetch an array of users based on the parameters passed.
  75   *
  76   * Since BuddyPress 1.7, bp_core_get_users() uses BP_User_Query. If you
  77   * need backward compatibility with BP_Core_User::get_users(), filter the
  78   * bp_use_legacy_user_query value, returning true.
  79   *
  80   * @since 1.2.0
  81   *
  82   * @param array|string $args {
  83   *     Array of arguments. All are optional. See {@link BP_User_Query} for
  84   *     a more complete description of arguments.
  85   *     @type string       $type                Sort order. Default: 'active'.
  86   *     @type int          $user_id             Limit results to friends of a user. Default: false.
  87   *     @type mixed        $exclude             IDs to exclude from results. Default: false.
  88   *     @type string       $search_terms        Limit to users matching search terms. Default: false.
  89   *     @type string       $meta_key            Limit to users with a meta_key. Default: false.
  90   *     @type string       $meta_value          Limit to users with a meta_value (with meta_key). Default: false.
  91   *     @type array|string $member_type         Array or comma-separated string of member types.
  92   *     @type array|string $member_type__in     Array or comma-separated string of member types.
  93   *                                             `$member_type` takes precedence over this parameter.
  94   *     @type array|string $member_type__not_in Array or comma-separated string of member types to be excluded.
  95   *     @type mixed        $include             Limit results by user IDs. Default: false.
  96   *     @type int          $per_page            Results per page. Default: 20.
  97   *     @type int          $page                Page of results. Default: 1.
  98   *     @type bool         $populate_extras     Fetch optional extras. Default: true.
  99   *     @type string|bool  $count_total         How to do total user count. Default: 'count_query'.
 100   * }
 101   * @return array
 102   */
 103  function bp_core_get_users( $args = '' ) {
 104  
 105      // Parse the user query arguments.
 106      $r = bp_parse_args( $args, array(
 107          'type'                => 'active',     // Active, newest, alphabetical, random or popular.
 108          'user_id'             => false,        // Pass a user_id to limit to only friend connections for this user.
 109          'exclude'             => false,        // Users to exclude from results.
 110          'search_terms'        => false,        // Limit to users that match these search terms.
 111          'meta_key'            => false,        // Limit to users who have this piece of usermeta.
 112          'meta_value'          => false,        // With meta_key, limit to users where usermeta matches this value.
 113          'member_type'         => '',
 114          'member_type__in'     => '',
 115          'member_type__not_in' => '',
 116          'include'             => false,        // Pass comma separated list of user_ids to limit to only these users.
 117          'per_page'            => 20,           // The number of results to return per page.
 118          'page'                => 1,            // The page to return if limiting per page.
 119          'populate_extras'     => true,         // Fetch the last active, where the user is a friend, total friend count, latest update.
 120          'count_total'         => 'count_query' // What kind of total user count to do, if any. 'count_query', 'sql_calc_found_rows', or false.
 121      ), 'core_get_users' );
 122  
 123      // For legacy users. Use of BP_Core_User::get_users() is deprecated.
 124      if ( apply_filters( 'bp_use_legacy_user_query', false, __FUNCTION__, $r ) ) {
 125          $retval = BP_Core_User::get_users(
 126              $r['type'],
 127              $r['per_page'],
 128              $r['page'],
 129              $r['user_id'],
 130              $r['include'],
 131              $r['search_terms'],
 132              $r['populate_extras'],
 133              $r['exclude'],
 134              $r['meta_key'],
 135              $r['meta_value']
 136          );
 137  
 138      // Default behavior as of BuddyPress 1.7.0.
 139      } else {
 140  
 141          // Get users like we were asked to do...
 142          $users = new BP_User_Query( $r );
 143  
 144          // ...but reformat the results to match bp_core_get_users() behavior.
 145          $retval = array(
 146              'users' => array_values( $users->results ),
 147              'total' => $users->total_users
 148          );
 149      }
 150  
 151      /**
 152       * Filters the results of the user query.
 153       *
 154       * @since 1.2.0
 155       *
 156       * @param array $retval Array of users for the current query.
 157       * @param array $r      Array of parsed query arguments.
 158       */
 159      return apply_filters( 'bp_core_get_users', $retval, $r );
 160  }
 161  
 162  /**
 163   * Return the domain for the passed user: e.g. http://example.com/members/andy/.
 164   *
 165   * @since 1.0.0
 166   *
 167   * @param int         $user_id       The ID of the user.
 168   * @param string|bool $user_nicename Optional. user_nicename of the user.
 169   * @param string|bool $user_login    Optional. user_login of the user.
 170   * @return string
 171   */
 172  function bp_core_get_user_domain( $user_id = 0, $user_nicename = false, $user_login = false ) {
 173  
 174      if ( empty( $user_id ) ) {
 175          return;
 176      }
 177  
 178      $username = bp_core_get_username( $user_id, $user_nicename, $user_login );
 179  
 180      if ( bp_is_username_compatibility_mode() ) {
 181          $username = rawurlencode( $username );
 182      }
 183  
 184      $after_domain = bp_core_enable_root_profiles() ? $username : bp_get_members_root_slug() . '/' . $username;
 185      $domain       = trailingslashit( bp_get_root_domain() . '/' . $after_domain );
 186  
 187      // Don't use this filter.  Subject to removal in a future release.
 188      // Use the 'bp_core_get_user_domain' filter instead.
 189      $domain       = apply_filters( 'bp_core_get_user_domain_pre_cache', $domain, $user_id, $user_nicename, $user_login );
 190  
 191      /**
 192       * Filters the domain for the passed user.
 193       *
 194       * @since 1.0.1
 195       *
 196       * @param string $domain        Domain for the passed user.
 197       * @param int    $user_id       ID of the passed user.
 198       * @param string $user_nicename User nicename of the passed user.
 199       * @param string $user_login    User login of the passed user.
 200       */
 201      return apply_filters( 'bp_core_get_user_domain', $domain, $user_id, $user_nicename, $user_login );
 202  }
 203  
 204  /**
 205   * Fetch everything in the wp_users table for a user, without any usermeta.
 206   *
 207   * @since 1.2.0
 208   *
 209   * @param  int $user_id The ID of the user.
 210   * @return array|bool Array of data on success, boolean false on failure.
 211   */
 212  function bp_core_get_core_userdata( $user_id = 0 ) {
 213      if ( empty( $user_id ) ) {
 214          return false;
 215      }
 216  
 217      // Get core user data
 218      $userdata = BP_Core_User::get_core_userdata( $user_id );
 219  
 220      /**
 221       * Filters the userdata for a passed user.
 222       *
 223       * @since 1.2.0
 224       *
 225       * @param array|bool $userdata Array of user data for a passed user on success, boolean false on failure.
 226       */
 227      return apply_filters( 'bp_core_get_core_userdata', $userdata );
 228  }
 229  
 230  /**
 231   * Return the ID of a user, based on user_login.
 232   *
 233   * No longer used.
 234   *
 235   * @todo Deprecate.
 236   *
 237   * @param string $user_login user_login of the user being queried.
 238   * @return int
 239   */
 240  function bp_core_get_displayed_userid( $user_login ) {
 241      return apply_filters( 'bp_core_get_displayed_userid', bp_core_get_userid( $user_login ) );
 242  }
 243  
 244  /**
 245   * Return the user ID based on a user's user_login.
 246   *
 247   * @since 1.0.0
 248   *
 249   * @param string $username user_login to check.
 250   * @return int|null The ID of the matched user on success, null on failure.
 251   */
 252  function bp_core_get_userid( $username = '' ) {
 253      if ( empty( $username ) ) {
 254          return false;
 255      }
 256  
 257      $user = get_user_by( 'login', $username );
 258  
 259      /**
 260       * Filters the ID of a user, based on user_login.
 261       *
 262       * @since 1.0.1
 263       *
 264       * @param int|null $value    ID of the user or null.
 265       * @param string   $username User login to check.
 266       */
 267      return apply_filters( 'bp_core_get_userid', ! empty( $user->ID ) ? $user->ID : NULL, $username );
 268  }
 269  
 270  /**
 271   * Return the user ID based on a user's user_nicename.
 272   *
 273   * @since 1.2.3
 274   *
 275   * @param string $user_nicename user_nicename to check.
 276   * @return int|null The ID of the matched user on success, null on failure.
 277   */
 278  function bp_core_get_userid_from_nicename( $user_nicename = '' ) {
 279      if ( empty( $user_nicename ) ) {
 280          return false;
 281      }
 282  
 283      $user = get_user_by( 'slug', $user_nicename );
 284  
 285      /**
 286       * Filters the user ID based on user_nicename.
 287       *
 288       * @since 1.2.3
 289       *
 290       * @param int|null $value         ID of the user or null.
 291       * @param string   $user_nicename User nicename to check.
 292       */
 293      return apply_filters( 'bp_core_get_userid_from_nicename', ! empty( $user->ID ) ? $user->ID : NULL, $user_nicename );
 294  }
 295  
 296  /**
 297   * Return the username for a user based on their user id.
 298   *
 299   * This function is sensitive to the BP_ENABLE_USERNAME_COMPATIBILITY_MODE,
 300   * so it will return the user_login or user_nicename as appropriate.
 301   *
 302   * @since 1.0.0
 303   *
 304   * @param int         $user_id       User ID to check.
 305   * @param string|bool $user_nicename Optional. user_nicename of user being checked.
 306   * @param string|bool $user_login    Optional. user_login of user being checked.
 307   * @return string The username of the matched user or an empty string if no user is found.
 308   */
 309  function bp_core_get_username( $user_id = 0, $user_nicename = false, $user_login = false ) {
 310  
 311      if ( ! $user_nicename && ! $user_login ) {
 312          // Pull an audible and maybe use the login over the nicename.
 313          if ( bp_is_username_compatibility_mode() ) {
 314              $username = get_the_author_meta( 'login', $user_id );
 315          } else {
 316              $username = get_the_author_meta( 'nicename', $user_id );
 317          }
 318      } else {
 319          $username = bp_is_username_compatibility_mode() ? $user_login : $user_nicename;
 320      }
 321  
 322      /**
 323       * Filters the username based on originally provided user ID.
 324       *
 325       * @since 1.0.1
 326       *
 327       * @param string $username Username determined by user ID.
 328       */
 329      return apply_filters( 'bp_core_get_username', $username );
 330  }
 331  
 332  /**
 333   * Return the user_nicename for a user based on their user_id.
 334   *
 335   * This should be used for linking to user profiles and anywhere else a
 336   * sanitized and unique slug to a user is needed.
 337   *
 338   * @since 1.5.0
 339   *
 340   * @param int $user_id User ID to check.
 341   * @return string The username of the matched user or an empty string if no user is found.
 342   */
 343  function bp_members_get_user_nicename( $user_id ) {
 344      /**
 345       * Filters the user_nicename based on originally provided user ID.
 346       *
 347       * @since 1.5.0
 348       *
 349       * @param string $username User nice name determined by user ID.
 350       */
 351      return apply_filters( 'bp_members_get_user_nicename', get_the_author_meta( 'nicename', $user_id ) );
 352  }
 353  
 354  /**
 355   * Return the email address for the user based on user ID.
 356   *
 357   * @since 1.0.0
 358   *
 359   * @param int $uid User ID to check.
 360   * @return string The email for the matched user. Empty string if no user
 361   *                matches the $user_id.
 362   */
 363  function bp_core_get_user_email( $user_id ) {
 364      /**
 365       * Filters the user email for user based on user ID.
 366       *
 367       * @since 1.0.1
 368       *
 369       * @param string $email Email determined for the user.
 370       */
 371      return apply_filters( 'bp_core_get_user_email', get_the_author_meta( 'email', $user_id ) );
 372  }
 373  
 374  /**
 375   * Return a HTML formatted link for a user with the user's full name as the link text.
 376   *
 377   * Eg: <a href="http://andy.example.com/">Andy Peatling</a>
 378   *
 379   * Optional parameters will return just the name or just the URL.
 380   *
 381   * @since 1.0.0
 382   *
 383   * @param int  $user_id   User ID to check.
 384   * @param bool $no_anchor Disable URL and HTML and just return full name.
 385   *                        Default: false.
 386   * @param bool $just_link Disable full name and HTML and just return the URL
 387   *                        text. Default false.
 388   * @return string|bool The link text based on passed parameters, or false on
 389   *                     no match.
 390   */
 391  function bp_core_get_userlink( $user_id, $no_anchor = false, $just_link = false ) {
 392      $display_name = bp_core_get_user_displayname( $user_id );
 393  
 394      if ( empty( $display_name ) ) {
 395          return false;
 396      }
 397  
 398      if ( ! empty( $no_anchor ) ) {
 399          return $display_name;
 400      }
 401  
 402      if ( !$url = bp_core_get_user_domain( $user_id ) ) {
 403          return false;
 404      }
 405  
 406      if ( ! empty( $just_link ) ) {
 407          return $url;
 408      }
 409  
 410      /**
 411       * Filters the link text for the passed in user.
 412       *
 413       * @since 1.2.0
 414       *
 415       * @param string $value   Link text based on passed parameters.
 416       * @param int    $user_id ID of the user to check.
 417       */
 418      return apply_filters( 'bp_core_get_userlink', '<a href="' . esc_url( $url ) . '">' . $display_name . '</a>', $user_id );
 419  }
 420  
 421  /**
 422   * Fetch the display name for a group of users.
 423   *
 424   * Uses the 'Name' field in xprofile if available. Falls back on WP
 425   * display_name, and then user_nicename.
 426   *
 427   * @since 2.0.0
 428   *
 429   * @param array $user_ids Array of user IDs to get display names for.
 430   * @return array Associative array of the format "id" => "displayname".
 431   */
 432  function bp_core_get_user_displaynames( $user_ids ) {
 433  
 434      // Sanitize.
 435      $user_ids = wp_parse_id_list( $user_ids );
 436  
 437      // Remove dupes and empties.
 438      $user_ids = array_unique( array_filter( $user_ids ) );
 439  
 440      if ( empty( $user_ids ) ) {
 441          return array();
 442      }
 443  
 444      // Warm the WP users cache with a targeted bulk update.
 445      cache_users( $user_ids );
 446  
 447      $retval = array();
 448      foreach ( $user_ids as $user_id ) {
 449          $retval[ $user_id ] = bp_core_get_user_displayname( $user_id );
 450      }
 451  
 452      return $retval;
 453  }
 454  
 455  /**
 456   * Fetch the display name for a user.
 457   *
 458   * @since 1.0.1
 459   *
 460   * @param int|string|bool $user_id_or_username User ID or username.
 461   * @return string|bool The display name for the user in question, or false if
 462   *                     user not found.
 463   */
 464  function bp_core_get_user_displayname( $user_id_or_username ) {
 465      if ( empty( $user_id_or_username ) ) {
 466          return false;
 467      }
 468  
 469      if ( ! is_numeric( $user_id_or_username ) ) {
 470          $user_id = bp_core_get_userid( $user_id_or_username );
 471      } else {
 472          $user_id = $user_id_or_username;
 473      }
 474  
 475      if ( empty( $user_id ) ) {
 476          return false;
 477      }
 478  
 479      /**
 480       * Filters the display name for the passed in user.
 481       *
 482       * @since 1.0.1
 483       *
 484       * @param string $fullname Display name for the user.
 485       * @param int    $user_id  ID of the user to check.
 486       */
 487      return apply_filters( 'bp_core_get_user_displayname', get_the_author_meta( 'display_name', $user_id ), $user_id );
 488  }
 489  add_filter( 'bp_core_get_user_displayname', 'strip_tags', 1 );
 490  add_filter( 'bp_core_get_user_displayname', 'trim'          );
 491  add_filter( 'bp_core_get_user_displayname', 'stripslashes'  );
 492  add_filter( 'bp_core_get_user_displayname', 'esc_html'      );
 493  
 494  /**
 495   * Return the user link for the user based on user email address.
 496   *
 497   * @since 1.0.0
 498   *
 499   * @param string $email The email address for the user.
 500   * @return string The link to the users home base. False on no match.
 501   */
 502  function bp_core_get_userlink_by_email( $email ) {
 503      $user = get_user_by( 'email', $email );
 504  
 505      /**
 506       * Filters the user link for the user based on user email address.
 507       *
 508       * @since 1.0.1
 509       *
 510       * @param string|bool $value URL for the user if found, otherwise false.
 511       */
 512      return apply_filters( 'bp_core_get_userlink_by_email', bp_core_get_userlink( $user->ID, false, false, true ) );
 513  }
 514  
 515  /**
 516   * Return the user link for the user based on the supplied identifier.
 517   *
 518   * @since 1.0.0
 519   *
 520   * @param string $username If BP_ENABLE_USERNAME_COMPATIBILITY_MODE is set,
 521   *                         this should be user_login, otherwise it should
 522   *                         be user_nicename.
 523   * @return string|bool The link to the user's domain, false on no match.
 524   */
 525  function bp_core_get_userlink_by_username( $username ) {
 526      if ( bp_is_username_compatibility_mode() ) {
 527          $user_id = bp_core_get_userid( $username );
 528      } else {
 529          $user_id = bp_core_get_userid_from_nicename( $username );
 530      }
 531  
 532      /**
 533       * Filters the user link for the user based on username.
 534       *
 535       * @since 1.0.1
 536       *
 537       * @param string|bool $value URL for the user if found, otherwise false.
 538       */
 539      return apply_filters( 'bp_core_get_userlink_by_username', bp_core_get_userlink( $user_id, false, false, true ) );
 540  }
 541  
 542  /**
 543   * Return the total number of members for the installation.
 544   *
 545   * Note that this is a raw count of non-spam, activated users. It does not
 546   * account for users who have logged activity (last_active). See
 547   * {@link bp_core_get_active_member_count()}.
 548   *
 549   * @since 1.2.0
 550   *
 551   * @return int The total number of members.
 552   */
 553  function bp_core_get_total_member_count() {
 554      global $wpdb;
 555  
 556      $count = wp_cache_get( 'bp_total_member_count', 'bp' );
 557  
 558      if ( false === $count ) {
 559          $status_sql = bp_core_get_status_sql();
 560          $count = $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->users} WHERE {$status_sql}" );
 561          wp_cache_set( 'bp_total_member_count', $count, 'bp' );
 562      }
 563  
 564      /**
 565       * Filters the total number of members for the installation.
 566       *
 567       * @since 1.2.0
 568       *
 569       * @param int $count Total number of members.
 570       */
 571      return apply_filters( 'bp_core_get_total_member_count', $count );
 572  }
 573  
 574  /**
 575   * Return the total number of members, limited to those members with last_activity.
 576   *
 577   * @since 1.6.0
 578   *
 579   * @return int The number of active members.
 580   */
 581  function bp_core_get_active_member_count() {
 582      global $wpdb;
 583  
 584      $count = get_transient( 'bp_active_member_count' );
 585      if ( false === $count ) {
 586          $bp = buddypress();
 587  
 588          // Avoid a costly join by splitting the lookup.
 589          if ( is_multisite() ) {
 590              $sql = "SELECT ID FROM {$wpdb->users} WHERE (user_status != 0 OR deleted != 0 OR user_status != 0)";
 591          } else {
 592              $sql = "SELECT ID FROM {$wpdb->users} WHERE user_status != 0";
 593          }
 594  
 595          $exclude_users     = $wpdb->get_col( $sql );
 596          $exclude_users_sql = !empty( $exclude_users ) ? "AND user_id NOT IN (" . implode( ',', wp_parse_id_list( $exclude_users ) ) . ")" : '';
 597          $count             = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(user_id) FROM {$bp->members->table_name_last_activity} WHERE component = %s AND type = 'last_activity' {$exclude_users_sql}", $bp->members->id ) );
 598  
 599          set_transient( 'bp_active_member_count', $count );
 600      }
 601  
 602      /**
 603       * Filters the total number of members for the installation limited to those with last_activity.
 604       *
 605       * @since 1.6.0
 606       *
 607       * @param int $count Total number of active members.
 608       */
 609      return apply_filters( 'bp_core_get_active_member_count', $count );
 610  }
 611  
 612  /**
 613   * Process a spammed or unspammed user.
 614   *
 615   * This function is called from three places:
 616   *
 617   * - in bp_settings_action_capabilities() (from the front-end)
 618   * - by bp_core_mark_user_spam_admin()    (from wp-admin)
 619   * - bp_core_mark_user_ham_admin()        (from wp-admin)
 620   *
 621   * @since 1.6.0
 622   *
 623   * @param int    $user_id       The ID of the user being spammed/hammed.
 624   * @param string $status        'spam' if being marked as spam, 'ham' otherwise.
 625   * @param bool   $do_wp_cleanup True to force the cleanup of WordPress content
 626   *                              and status, otherwise false. Generally, this should
 627   *                              only be false if WordPress is expected to have
 628   *                              performed this cleanup independently, as when hooked
 629   *                              to 'make_spam_user'.
 630   * @return bool True on success, false on failure.
 631   */
 632  function bp_core_process_spammer_status( $user_id, $status, $do_wp_cleanup = true ) {
 633      global $wpdb;
 634  
 635      // Bail if no user ID.
 636      if ( empty( $user_id ) ) {
 637          return;
 638      }
 639  
 640      // Bail if user ID is super admin.
 641      if ( is_super_admin( $user_id ) ) {
 642          return;
 643      }
 644  
 645      // Get the functions file.
 646      if ( is_multisite() ) {
 647          require_once( ABSPATH . 'wp-admin/includes/ms.php' );
 648      }
 649  
 650      $is_spam = ( 'spam' == $status );
 651  
 652      // Only you can prevent infinite loops.
 653      remove_action( 'make_spam_user', 'bp_core_mark_user_spam_admin' );
 654      remove_action( 'make_ham_user',  'bp_core_mark_user_ham_admin'  );
 655  
 656      // Force the cleanup of WordPress content and status for multisite configs.
 657      if ( $do_wp_cleanup ) {
 658  
 659          // Get the blogs for the user.
 660          $blogs = get_blogs_of_user( $user_id, true );
 661  
 662          foreach ( (array) array_values( $blogs ) as $details ) {
 663  
 664              // Do not mark the main or current root blog as spam.
 665              if ( 1 == $details->userblog_id || bp_get_root_blog_id() == $details->userblog_id ) {
 666                  continue;
 667              }
 668  
 669              // Update the blog status.
 670              update_blog_status( $details->userblog_id, 'spam', $is_spam );
 671          }
 672  
 673          // Finally, mark this user as a spammer.
 674          if ( is_multisite() ) {
 675              update_user_status( $user_id, 'spam', $is_spam );
 676          }
 677      }
 678  
 679      // Update the user status.
 680      $wpdb->update( $wpdb->users, array( 'user_status' => $is_spam ), array( 'ID' => $user_id ) );
 681  
 682      // Clean user cache.
 683      clean_user_cache( $user_id );
 684  
 685      if ( ! is_multisite() ) {
 686          // Call multisite actions in single site mode for good measure.
 687          if ( true === $is_spam ) {
 688  
 689              /**
 690               * Fires at end of processing spammer in Dashboard if not multisite and user is spam.
 691               *
 692               * @since 1.5.0
 693               *
 694               * @param int $value user ID.
 695               */
 696              do_action( 'make_spam_user', $user_id );
 697          } else {
 698  
 699              /**
 700               * Fires at end of processing spammer in Dashboard if not multisite and user is not spam.
 701               *
 702               * @since 1.5.0
 703               *
 704               * @param int $value user ID.
 705               */
 706              do_action( 'make_ham_user', $user_id );
 707          }
 708      }
 709  
 710      // Hide this user's activity.
 711      if ( ( true === $is_spam ) && bp_is_active( 'activity' ) ) {
 712          bp_activity_hide_user_activity( $user_id );
 713      }
 714  
 715      // We need a special hook for is_spam so that components can delete data at spam time.
 716      if ( true === $is_spam ) {
 717  
 718          /**
 719           * Fires at the end of the process spammer process if the user is spam.
 720           *
 721           * @since 1.5.0
 722           *
 723           * @param int $value Displayed user ID.
 724           */
 725          do_action( 'bp_make_spam_user', $user_id );
 726      } else {
 727  
 728          /**
 729           * Fires at the end of the process spammer process if the user is not spam.
 730           *
 731           * @since 1.5.0
 732           *
 733           * @param int $value Displayed user ID.
 734           */
 735          do_action( 'bp_make_ham_user', $user_id );
 736      }
 737  
 738      /**
 739       * Fires at the end of the process for hanlding spammer status.
 740       *
 741       * @since 1.5.5
 742       *
 743       * @param int  $user_id ID of the processed user.
 744       * @param bool $is_spam The determined spam status of processed user.
 745       */
 746      do_action( 'bp_core_process_spammer_status', $user_id, $is_spam );
 747  
 748      // Put things back how we found them.
 749      add_action( 'make_spam_user', 'bp_core_mark_user_spam_admin' );
 750      add_action( 'make_ham_user',  'bp_core_mark_user_ham_admin'  );
 751  
 752      return true;
 753  }
 754  /**
 755   * Hook to WP's make_spam_user and run our custom BP spam functions.
 756   *
 757   * @since 1.6.0
 758   *
 759   * @param int $user_id The user ID passed from the make_spam_user hook.
 760   */
 761  function bp_core_mark_user_spam_admin( $user_id ) {
 762      bp_core_process_spammer_status( $user_id, 'spam', false );
 763  }
 764  add_action( 'make_spam_user', 'bp_core_mark_user_spam_admin' );
 765  
 766  /**
 767   * Hook to WP's make_ham_user and run our custom BP spam functions.
 768   *
 769   * @since 1.6.0
 770   *
 771   * @param int $user_id The user ID passed from the make_ham_user hook.
 772   */
 773  function bp_core_mark_user_ham_admin( $user_id ) {
 774      bp_core_process_spammer_status( $user_id, 'ham', false );
 775  }
 776  add_action( 'make_ham_user', 'bp_core_mark_user_ham_admin' );
 777  
 778  /**
 779   * Check whether a user has been marked as a spammer.
 780   *
 781   * @since 1.6.0
 782   *
 783   * @param int $user_id The ID for the user.
 784   * @return bool True if spammer, otherwise false.
 785   */
 786  function bp_is_user_spammer( $user_id = 0 ) {
 787  
 788      // No user to check.
 789      if ( empty( $user_id ) ) {
 790          return false;
 791      }
 792  
 793      $bp = buddypress();
 794  
 795      // Assume user is not spam.
 796      $is_spammer = false;
 797  
 798      // Setup our user.
 799      $user = false;
 800  
 801      // Get locally-cached data if available.
 802      switch ( $user_id ) {
 803          case bp_loggedin_user_id() :
 804              $user = ! empty( $bp->loggedin_user->userdata ) ? $bp->loggedin_user->userdata : false;
 805              break;
 806  
 807          case bp_displayed_user_id() :
 808              $user = ! empty( $bp->displayed_user->userdata ) ? $bp->displayed_user->userdata : false;
 809              break;
 810  
 811          case bp_get_member_user_id() :
 812              global $members_template;
 813              $user = isset( $members_template ) && isset( $members_template->member ) ? $members_template->member :  false;
 814              break;
 815      }
 816  
 817      // Manually get userdata if still empty.
 818      if ( empty( $user ) ) {
 819          $user = get_userdata( $user_id );
 820      }
 821  
 822      // No user found.
 823      if ( empty( $user ) ) {
 824          $is_spammer = false;
 825  
 826      // User found.
 827      } else {
 828  
 829          // Check if spam.
 830          if ( !empty( $user->spam ) ) {
 831              $is_spammer = true;
 832          }
 833  
 834          if ( 1 == $user->user_status ) {
 835              $is_spammer = true;
 836          }
 837      }
 838  
 839      /**
 840       * Filters whether a user is marked as a spammer.
 841       *
 842       * @since 1.6.0
 843       *
 844       * @param bool $is_spammer Whether or not user is marked as spammer.
 845       */
 846      return apply_filters( 'bp_is_user_spammer', (bool) $is_spammer );
 847  }
 848  
 849  /**
 850   * Check whether a user has been marked as deleted.
 851   *
 852   * @since 1.6.0
 853   *
 854   * @param int $user_id The ID for the user.
 855   * @return bool True if deleted, otherwise false.
 856   */
 857  function bp_is_user_deleted( $user_id = 0 ) {
 858  
 859      // No user to check.
 860      if ( empty( $user_id ) ) {
 861          return false;
 862      }
 863  
 864      $bp = buddypress();
 865  
 866      // Assume user is not deleted.
 867      $is_deleted = false;
 868  
 869      // Setup our user.
 870      $user = false;
 871  
 872      // Get locally-cached data if available.
 873      switch ( $user_id ) {
 874          case bp_loggedin_user_id() :
 875              $user = ! empty( $bp->loggedin_user->userdata ) ? $bp->loggedin_user->userdata : false;
 876              break;
 877  
 878          case bp_displayed_user_id() :
 879              $user = ! empty( $bp->displayed_user->userdata ) ? $bp->displayed_user->userdata : false;
 880              break;
 881      }
 882  
 883      // Manually get userdata if still empty.
 884      if ( empty( $user ) ) {
 885          $user = get_userdata( $user_id );
 886      }
 887  
 888      // No user found.
 889      if ( empty( $user ) ) {
 890          $is_deleted = true;
 891  
 892      // User found.
 893      } else {
 894  
 895          // Check if deleted.
 896          if ( !empty( $user->deleted ) ) {
 897              $is_deleted = true;
 898          }
 899  
 900          if ( 2 == $user->user_status ) {
 901              $is_deleted = true;
 902          }
 903      }
 904  
 905      /**
 906       * Filters whether a user is marked as deleted.
 907       *
 908       * @since 1.6.0
 909       *
 910       * @param bool $is_deleted Whether or not user is marked as deleted.
 911       */
 912      return apply_filters( 'bp_is_user_deleted', (bool) $is_deleted );
 913  }
 914  
 915  /**
 916   * Check whether a user is "active", ie neither deleted nor spammer.
 917   *
 918   * @since 1.6.0
 919   *
 920   * @param int $user_id The user ID to check.
 921   * @return bool True if active, otherwise false.
 922   */
 923  function bp_is_user_active( $user_id = 0 ) {
 924  
 925      // Default to current user.
 926      if ( empty( $user_id ) && is_user_logged_in() ) {
 927          $user_id = bp_loggedin_user_id();
 928      }
 929  
 930      // No user to check.
 931      if ( empty( $user_id ) ) {
 932          return false;
 933      }
 934  
 935      // Check spam.
 936      if ( bp_is_user_spammer( $user_id ) ) {
 937          return false;
 938      }
 939  
 940      // Check deleted.
 941      if ( bp_is_user_deleted( $user_id ) ) {
 942          return false;
 943      }
 944  
 945      // Assume true if not spam or deleted.
 946      return true;
 947  }
 948  
 949  /**
 950   * Check whether user is not active.
 951   *
 952   * @since 1.6.0
 953   *
 954   * @todo No need for the user fallback checks, since they're done in
 955   *       bp_is_user_active().
 956   *
 957   * @param int $user_id The user ID to check.
 958   * @return bool True if inactive, otherwise false.
 959   */
 960  function bp_is_user_inactive( $user_id = 0 ) {
 961  
 962      // Default to current user.
 963      if ( empty( $user_id ) && is_user_logged_in() ) {
 964          $user_id = bp_loggedin_user_id();
 965      }
 966  
 967      // No user to check.
 968      if ( empty( $user_id ) ) {
 969          return false;
 970      }
 971  
 972      // Return the inverse of active.
 973      return !bp_is_user_active( $user_id );
 974  }
 975  
 976  /**
 977   * Update a user's last activity.
 978   *
 979   * @since 1.9.0
 980   *
 981   * @param int    $user_id ID of the user being updated.
 982   * @param string $time    Time of last activity, in 'Y-m-d H:i:s' format.
 983   * @return bool True on success, false on failure.
 984   */
 985  function bp_update_user_last_activity( $user_id = 0, $time = '' ) {
 986  
 987      // Fall back on current user.
 988      if ( empty( $user_id ) ) {
 989          $user_id = bp_loggedin_user_id();
 990      }
 991  
 992      // Bail if the user id is 0, as there's nothing to update.
 993      if ( empty( $user_id ) ) {
 994          return false;
 995      }
 996  
 997      // Fall back on current time.
 998      if ( empty( $time ) ) {
 999          $time = bp_core_current_time();
1000      }
1001  
1002      // As of BuddyPress 2.0, last_activity is no longer stored in usermeta.
1003      // However, we mirror it there for backward compatibility. Do not use!
1004      // Remove our warning and re-add.
1005      remove_filter( 'update_user_metadata', '_bp_update_user_meta_last_activity_warning', 10 );
1006      remove_filter( 'get_user_metadata', '_bp_get_user_meta_last_activity_warning', 10 );
1007      bp_update_user_meta( $user_id, 'last_activity', $time );
1008      add_filter( 'update_user_metadata', '_bp_update_user_meta_last_activity_warning', 10, 4 );
1009      add_filter( 'get_user_metadata', '_bp_get_user_meta_last_activity_warning', 10, 4 );
1010  
1011      return BP_Core_User::update_last_activity( $user_id, $time );
1012  }
1013  
1014  /**
1015   * Backward compatibility for 'last_activity' usermeta fetching.
1016   *
1017   * In BuddyPress 2.0, user last_activity data was moved out of usermeta. For
1018   * backward compatibility, we continue to mirror the data there. This function
1019   * serves two purposes: it warns plugin authors of the change, and it returns
1020   * the data from the proper location.
1021   *
1022   * @since 2.0.0
1023   * @since 2.9.3 Added the `$single` parameter.
1024   *
1025   * @access private For internal use only.
1026   *
1027   * @param null   $retval Null retval value.
1028   * @param int    $object_id ID of the user.
1029   * @param string $meta_key  Meta key being fetched.
1030   * @param bool   $single    Whether a single key is being fetched (vs an array).
1031   * @return string|null
1032   */
1033  function _bp_get_user_meta_last_activity_warning( $retval, $object_id, $meta_key, $single ) {
1034      static $warned = false;
1035  
1036      if ( 'last_activity' === $meta_key ) {
1037          // Don't send the warning more than once per pageload.
1038          if ( false === $warned ) {
1039              _doing_it_wrong( 'get_user_meta( $user_id, \'last_activity\' )', __( 'User last_activity data is no longer stored in usermeta. Use bp_get_user_last_activity() instead.', 'buddypress' ), '2.0.0' );
1040              $warned = true;
1041          }
1042  
1043          $user_last_activity = bp_get_user_last_activity( $object_id );
1044          if ( $single ) {
1045              return $user_last_activity;
1046          } else {
1047              return array( $user_last_activity );
1048          }
1049      }
1050  
1051      return $retval;
1052  }
1053  add_filter( 'get_user_metadata', '_bp_get_user_meta_last_activity_warning', 10, 4 );
1054  
1055  /**
1056   * Backward compatibility for 'last_activity' usermeta setting.
1057   *
1058   * In BuddyPress 2.0, user last_activity data was moved out of usermeta. For
1059   * backward compatibility, we continue to mirror the data there. This function
1060   * serves two purposes: it warns plugin authors of the change, and it updates
1061   * the data in the proper location.
1062   *
1063   * @since 2.0.0
1064   *
1065   * @access private For internal use only.
1066   *
1067   * @param int    $meta_id    ID of the just-set usermeta row.
1068   * @param int    $object_id  ID of the user.
1069   * @param string $meta_key   Meta key being fetched.
1070   * @param string $meta_value Active time.
1071   */
1072  function _bp_update_user_meta_last_activity_warning( $meta_id, $object_id, $meta_key, $meta_value ) {
1073      if ( 'last_activity' === $meta_key ) {
1074          _doing_it_wrong( 'update_user_meta( $user_id, \'last_activity\' )', __( 'User last_activity data is no longer stored in usermeta. Use bp_update_user_last_activity() instead.', 'buddypress' ), '2.0.0' );
1075          bp_update_user_last_activity( $object_id, $meta_value );
1076      }
1077  }
1078  add_filter( 'update_user_metadata', '_bp_update_user_meta_last_activity_warning', 10, 4 );
1079  
1080  /**
1081   * Get the last activity for a given user.
1082   *
1083   * @since 1.9.0
1084   *
1085   * @param int $user_id The ID of the user.
1086   * @return string Time of last activity, in 'Y-m-d H:i:s' format, or an empty
1087   *                string if none is found.
1088   */
1089  function bp_get_user_last_activity( $user_id = 0 ) {
1090      $activity = '';
1091  
1092      $last_activity = BP_Core_User::get_last_activity( $user_id );
1093      if ( ! empty( $last_activity[ $user_id ] ) ) {
1094          $activity = $last_activity[ $user_id ]['date_recorded'];
1095      }
1096  
1097      /**
1098       * Filters the last activity for a given user.
1099       *
1100       * @since 1.9.0
1101       *
1102       * @param string $activity Time of last activity, in 'Y-m-d H:i:s' format or
1103       *                         an empty string if none found.
1104       * @param int    $user_id  ID of the user being checked.
1105       */
1106      return apply_filters( 'bp_get_user_last_activity', $activity, $user_id );
1107  }
1108  
1109  /**
1110   * Migrate last_activity data from the usermeta table to the activity table.
1111   *
1112   * Generally, this function is only run when BP is upgraded to 2.0. It can also
1113   * be called directly from the BuddyPress Tools panel.
1114   *
1115   * @since 2.0.0
1116   *
1117   * @return bool
1118   */
1119  function bp_last_activity_migrate() {
1120      global $wpdb;
1121  
1122      $bp = buddypress();
1123  
1124      // Wipe out existing last_activity data in the activity table -
1125      // this helps to prevent duplicates when pulling from the usermeta
1126      // table.
1127      $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->members->table_name_last_activity} WHERE component = %s AND type = 'last_activity'", $bp->members->id ) );
1128  
1129      $sql = "INSERT INTO {$bp->members->table_name_last_activity} (`user_id`, `component`, `type`, `action`, `content`, `primary_link`, `item_id`, `date_recorded` ) (
1130            SELECT user_id, '{$bp->members->id}' as component, 'last_activity' as type, '' as action, '' as content, '' as primary_link, 0 as item_id, meta_value AS date_recorded
1131            FROM {$wpdb->usermeta}
1132            WHERE
1133              meta_key = 'last_activity'
1134      );";
1135  
1136      return $wpdb->query( $sql );
1137  }
1138  
1139  /**
1140   * Fetch every post that is authored by the given user for the current blog.
1141   *
1142   * No longer used in BuddyPress.
1143   *
1144   * @todo Deprecate.
1145   *
1146   * @param int $user_id ID of the user being queried.
1147   * @return array Post IDs.
1148   */
1149  function bp_core_get_all_posts_for_user( $user_id = 0 ) {
1150      global $wpdb;
1151  
1152      if ( empty( $user_id ) ) {
1153          $user_id = bp_displayed_user_id();
1154      }
1155  
1156      return apply_filters( 'bp_core_get_all_posts_for_user', $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_author = %d AND post_status = 'publish' AND post_type = 'post'", $user_id ) ) );
1157  }
1158  
1159  /**
1160   * Process account deletion requests.
1161   *
1162   * Primarily used for self-deletions, as requested through Settings.
1163   *
1164   * @since 1.0.0
1165   *
1166   * @param int $user_id Optional. ID of the user to be deleted. Default: the
1167   *                     logged-in user.
1168   * @return bool True on success, false on failure.
1169   */
1170  function bp_core_delete_account( $user_id = 0 ) {
1171  
1172      // Use logged in user ID if none is passed.
1173      if ( empty( $user_id ) ) {
1174          $user_id = bp_loggedin_user_id();
1175      }
1176  
1177      // Site admins cannot be deleted.
1178      if ( is_super_admin( $user_id ) ) {
1179          return false;
1180      }
1181  
1182      // Extra checks if user is not deleting themselves.
1183      if ( bp_loggedin_user_id() !== absint( $user_id ) ) {
1184  
1185          // Bail if current user cannot delete any users.
1186          if ( ! bp_current_user_can( 'delete_users' ) ) {
1187              return false;
1188          }
1189  
1190          // Bail if current user cannot delete this user.
1191          if ( ! current_user_can_for_blog( bp_get_root_blog_id(), 'delete_user', $user_id ) ) {
1192              return false;
1193          }
1194      }
1195  
1196      /**
1197       * Fires before the processing of an account deletion.
1198       *
1199       * @since 1.6.0
1200       *
1201       * @param int $user_id ID of the user account being deleted.
1202       */
1203      do_action( 'bp_core_pre_delete_account', $user_id );
1204  
1205      // Specifically handle multi-site environment.
1206      if ( is_multisite() ) {
1207          require_once( ABSPATH . '/wp-admin/includes/ms.php'   );
1208          require_once( ABSPATH . '/wp-admin/includes/user.php' );
1209  
1210          $retval = wpmu_delete_user( $user_id );
1211  
1212      // Single site user deletion.
1213      } else {
1214          require_once( ABSPATH . '/wp-admin/includes/user.php' );
1215          $retval = wp_delete_user( $user_id );
1216      }
1217  
1218      /**
1219       * Fires after the deletion of an account.
1220       *
1221       * @since 1.6.0
1222       *
1223       * @param int $user_id ID of the user account that was deleted.
1224       */
1225      do_action( 'bp_core_deleted_account', $user_id );
1226  
1227      return $retval;
1228  }
1229  
1230  /**
1231   * Delete a user's avatar when the user is deleted.
1232   *
1233   * @since 1.9.0
1234   *
1235   * @param int $user_id ID of the user who is about to be deleted.
1236   * @return bool True on success, false on failure.
1237   */
1238  function bp_core_delete_avatar_on_user_delete( $user_id ) {
1239      return bp_core_delete_existing_avatar( array(
1240          'item_id' => $user_id,
1241          'object'  => 'user',
1242      ) );
1243  }
1244  add_action( 'wpmu_delete_user', 'bp_core_delete_avatar_on_user_delete' );
1245  add_action( 'delete_user', 'bp_core_delete_avatar_on_user_delete' );
1246  
1247  /**
1248   * Multibyte-safe ucfirst() support.
1249   *
1250   * Uses multibyte functions when available on the PHP build.
1251   *
1252   * @since 1.0.0
1253   *
1254   * @param string $str String to be upper-cased.
1255   * @return string
1256   */
1257  function bp_core_ucfirst( $str ) {
1258      if ( function_exists( 'mb_strtoupper' ) && function_exists( 'mb_substr' ) ) {
1259          $fc = mb_strtoupper( mb_substr( $str, 0, 1 ) );
1260          return $fc.mb_substr( $str, 1 );
1261      } else {
1262          return ucfirst( $str );
1263      }
1264  }
1265  
1266  /**
1267   * Prevent spammers from logging in.
1268   *
1269   * When a user logs in, check if they have been marked as a spammer. If yes
1270   * then simply redirect them to the home page and stop them from logging in.
1271   *
1272   * @since 1.1.2
1273   *
1274   * @param WP_User|WP_Error $user Either the WP_User object or the WP_Error
1275   *                               object, as passed to the 'authenticate' filter.
1276   * @return WP_User|WP_Error If the user is not a spammer, return the WP_User
1277   *                          object. Otherwise a new WP_Error object.
1278   */
1279  function bp_core_boot_spammer( $user ) {
1280  
1281      // Check to see if the $user has already failed logging in, if so return $user as-is.
1282      if ( is_wp_error( $user ) || empty( $user ) ) {
1283          return $user;
1284      }
1285  
1286      // The user exists; now do a check to see if the user is a spammer
1287      // if the user is a spammer, stop them in their tracks!
1288      if ( is_a( $user, 'WP_User' ) && ( ( is_multisite() && (int) $user->spam ) || 1 == $user->user_status ) ) {
1289          return new WP_Error( 'invalid_username', __( '<strong>ERROR</strong>: Your account has been marked as a spammer.', 'buddypress' ) );
1290      }
1291  
1292      // User is good to go!
1293      return $user;
1294  }
1295  add_filter( 'authenticate', 'bp_core_boot_spammer', 30 );
1296  
1297  /**
1298   * Delete last_activity data for the user when the user is deleted.
1299   *
1300   * @since 1.0.0
1301   *
1302   * @param int $user_id The user ID for the user to delete usermeta for.
1303   */
1304  function bp_core_remove_data( $user_id ) {
1305  
1306      // Remove last_activity data.
1307      BP_Core_User::delete_last_activity( $user_id );
1308  
1309      // Flush the cache to remove the user from all cached objects.
1310      wp_cache_flush();
1311  }
1312  add_action( 'wpmu_delete_user',  'bp_core_remove_data' );
1313  add_action( 'delete_user',       'bp_core_remove_data' );
1314  add_action( 'bp_make_spam_user', 'bp_core_remove_data' );
1315  
1316  /**
1317   * Check whether the logged-in user can edit settings for the displayed user.
1318   *
1319   * @since 1.5.0
1320   *
1321   * @return bool True if editing is allowed, otherwise false.
1322   */
1323  function bp_core_can_edit_settings() {
1324      $status = false;
1325  
1326      if ( bp_is_my_profile() ) {
1327          $status = true;
1328      } elseif ( is_super_admin( bp_displayed_user_id() ) && ! is_super_admin() ) {
1329          $status = false;
1330      } elseif ( bp_current_user_can( 'bp_moderate' ) || current_user_can( 'edit_users' ) ) {
1331          $status = true;
1332      }
1333  
1334      /**
1335       * Filters the status of whether the logged-in user can edit settings for the displayed user or not.
1336       *
1337       * @since 2.8.0
1338       *
1339       * @param bool True if editing is allowed, otherwise false.
1340       */
1341      return apply_filters( 'bp_core_can_edit_settings', $status );
1342  }
1343  
1344  /** Sign-up *******************************************************************/
1345  
1346  /**
1347   * Flush illegal names by getting and setting 'illegal_names' site option.
1348   *
1349   * @since 1.2.5
1350   */
1351  function bp_core_flush_illegal_names() {
1352      $illegal_names = get_site_option( 'illegal_names' );
1353      update_site_option( 'illegal_names', $illegal_names );
1354  }
1355  
1356  /**
1357   * Add BuddyPress-specific items to the illegal_names array.
1358   *
1359   * @since 1.2.7
1360   *
1361   * @param array|string $value    Illegal names as being saved defined in
1362   *                               Multisite settings.
1363   * @param array|string $oldvalue The old value of the option.
1364   * @return array Merged and unique array of illegal names.
1365   */
1366  function bp_core_get_illegal_names( $value = '', $oldvalue = '' ) {
1367  
1368      // Make sure $value is array.
1369      if ( empty( $value ) ) {
1370          $db_illegal_names = array();
1371      }
1372  
1373      if ( is_array( $value ) ) {
1374          $db_illegal_names = $value;
1375      } elseif ( is_string( $value ) ) {
1376          $db_illegal_names = explode( ' ', $value );
1377      }
1378  
1379      // Add the core components' slugs to the banned list even if their components aren't active.
1380      $bp_component_slugs = array(
1381          'groups',
1382          'members',
1383          'forums',
1384          'blogs',
1385          'activity',
1386          'profile',
1387          'friends',
1388          'search',
1389          'settings',
1390          'notifications',
1391          'register',
1392          'activate'
1393      );
1394  
1395      // Core constants.
1396      $slug_constants = array(
1397          'BP_GROUPS_SLUG',
1398          'BP_MEMBERS_SLUG',
1399          'BP_FORUMS_SLUG',
1400          'BP_BLOGS_SLUG',
1401          'BP_ACTIVITY_SLUG',
1402          'BP_XPROFILE_SLUG',
1403          'BP_FRIENDS_SLUG',
1404          'BP_SEARCH_SLUG',
1405          'BP_SETTINGS_SLUG',
1406          'BP_NOTIFICATIONS_SLUG',
1407          'BP_REGISTER_SLUG',
1408          'BP_ACTIVATION_SLUG',
1409      );
1410      foreach( $slug_constants as $constant ) {
1411          if ( defined( $constant ) ) {
1412              $bp_component_slugs[] = constant( $constant );
1413          }
1414      }
1415  
1416      /**
1417       * Filters the array of default illegal usernames.
1418       *
1419       * @since 1.2.2
1420       *
1421       * @param array $value Merged and unique array of illegal usernames.
1422       */
1423      $filtered_illegal_names = apply_filters( 'bp_core_illegal_usernames', array_merge( array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' ), $bp_component_slugs ) );
1424  
1425      /**
1426       * Filters the list of illegal usernames from WordPress.
1427       *
1428       * @since 3.0
1429       *
1430       * @param array Array of illegal usernames.
1431       */
1432      $wp_filtered_illegal_names = apply_filters( 'illegal_user_logins', array() );
1433  
1434      // First merge BuddyPress illegal names.
1435      $bp_merged_names           = array_merge( (array) $filtered_illegal_names, (array) $db_illegal_names );
1436  
1437      // Then merge WordPress and BuddyPress illegal names.
1438      $merged_names              = array_merge( (array) $wp_filtered_illegal_names, (array) $bp_merged_names );
1439  
1440      // Remove duplicates.
1441      $illegal_names             = array_unique( (array) $merged_names );
1442  
1443      /**
1444       * Filters the array of default illegal names.
1445       *
1446       * @since 1.2.5
1447       *
1448       * @param array $value Merged and unique array of illegal names.
1449       */
1450      return apply_filters( 'bp_core_illegal_names', $illegal_names );
1451  }
1452  add_filter( 'pre_update_site_option_illegal_names', 'bp_core_get_illegal_names', 10, 2 );
1453  
1454  /**
1455   * Check that an email address is valid for use.
1456   *
1457   * Performs the following checks:
1458   *   - Is the email address well-formed?
1459   *   - Is the email address already used?
1460   *   - If there's an email domain blacklist, is the current domain on it?
1461   *   - If there's an email domain whitelest, is the current domain on it?
1462   *
1463   * @since 1.6.2
1464   *
1465   * @param string $user_email The email being checked.
1466   * @return bool|array True if the address passes all checks; otherwise an array
1467   *                    of error codes.
1468   */
1469  function bp_core_validate_email_address( $user_email ) {
1470      $errors = array();
1471  
1472      $user_email = sanitize_email( $user_email );
1473  
1474      // Is the email well-formed?
1475      if ( ! is_email( $user_email ) ) {
1476          $errors['invalid'] = 1;
1477      }
1478  
1479      // Is the email on the Banned Email Domains list?
1480      // Note: This check only works on Multisite.
1481      if ( function_exists( 'is_email_address_unsafe' ) && is_email_address_unsafe( $user_email ) ) {
1482          $errors['domain_banned'] = 1;
1483      }
1484  
1485      // Is the email on the Limited Email Domains list?
1486      // Note: This check only works on Multisite.
1487      $limited_email_domains = get_site_option( 'limited_email_domains' );
1488      if ( is_array( $limited_email_domains ) && empty( $limited_email_domains ) == false ) {
1489          $emaildomain = substr( $user_email, 1 + strpos( $user_email, '@' ) );
1490          if ( ! in_array( $emaildomain, $limited_email_domains ) ) {
1491              $errors['domain_not_allowed'] = 1;
1492          }
1493      }
1494  
1495      // Is the email alreday in use?
1496      if ( email_exists( $user_email ) ) {
1497          $errors['in_use'] = 1;
1498      }
1499  
1500      $retval = ! empty( $errors ) ? $errors : true;
1501  
1502      return $retval;
1503  }
1504  
1505  /**
1506   * Add the appropriate errors to a WP_Error object, given results of a validation test.
1507   *
1508   * Functions like bp_core_validate_email_address() return a structured array
1509   * of error codes. bp_core_add_validation_error_messages() takes this array and
1510   * parses, adding the appropriate error messages to the WP_Error object.
1511   *
1512   * @since 1.7.0
1513   *
1514   * @see bp_core_validate_email_address()
1515   *
1516   * @param WP_Error $errors             WP_Error object.
1517   * @param array    $validation_results The return value of a validation function
1518   *                                     like bp_core_validate_email_address().
1519   */
1520  function bp_core_add_validation_error_messages( WP_Error $errors, $validation_results ) {
1521      if ( ! empty( $validation_results['invalid'] ) ) {
1522          $errors->add( 'user_email', __( 'Please check your email address.', 'buddypress' ) );
1523      }
1524  
1525      if ( ! empty( $validation_results['domain_banned'] ) ) {
1526          $errors->add( 'user_email',  __( 'Sorry, that email address is not allowed!', 'buddypress' ) );
1527      }
1528  
1529      if ( ! empty( $validation_results['domain_not_allowed'] ) ) {
1530          $errors->add( 'user_email', __( 'Sorry, that email address is not allowed!', 'buddypress' ) );
1531      }
1532  
1533      if ( ! empty( $validation_results['in_use'] ) ) {
1534          $errors->add( 'user_email', __( 'Sorry, that email address is already used!', 'buddypress' ) );
1535      }
1536  }
1537  
1538  /**
1539   * Validate a user name and email address when creating a new user.
1540   *
1541   * @since 1.2.2
1542   *
1543   * @param string $user_name  Username to validate.
1544   * @param string $user_email Email address to validate.
1545   * @return array Results of user validation including errors, if any.
1546   */
1547  function bp_core_validate_user_signup( $user_name, $user_email ) {
1548  
1549      // Make sure illegal names include BuddyPress slugs and values.
1550      bp_core_flush_illegal_names();
1551  
1552      // WordPress Multisite has its own validation. Use it, so that we
1553      // properly mirror restrictions on username, etc.
1554      if ( function_exists( 'wpmu_validate_user_signup' ) ) {
1555          $result = wpmu_validate_user_signup( $user_name, $user_email );
1556  
1557      // When not running Multisite, we perform our own validation. What
1558      // follows reproduces much of the logic of wpmu_validate_user_signup(),
1559      // minus the multisite-specific restrictions on user_login.
1560      } else {
1561          $errors = new WP_Error();
1562  
1563          /**
1564           * Filters the username before being validated.
1565           *
1566           * @since 1.5.5
1567           *
1568           * @param string $user_name Username to validate.
1569           */
1570          $user_name = apply_filters( 'pre_user_login', $user_name );
1571  
1572          // User name can't be empty.
1573          if ( empty( $user_name ) ) {
1574              $errors->add( 'user_name', __( 'Please enter a username', 'buddypress' ) );
1575          }
1576  
1577          // User name can't be on the blacklist.
1578          $illegal_names = get_site_option( 'illegal_names' );
1579          if ( in_array( $user_name, (array) $illegal_names ) ) {
1580              $errors->add( 'user_name', __( 'That username is not allowed', 'buddypress' ) );
1581          }
1582  
1583          // User name must pass WP's validity check.
1584          if ( ! validate_username( $user_name ) ) {
1585              $errors->add( 'user_name', __( 'Usernames can contain only letters, numbers, ., -, and @', 'buddypress' ) );
1586          }
1587  
1588          // Minimum of 4 characters.
1589          if ( strlen( $user_name ) < 4 ) {
1590              $errors->add( 'user_name',  __( 'Username must be at least 4 characters', 'buddypress' ) );
1591          }
1592  
1593          // No underscores. @todo Why not?
1594          if ( false !== strpos( ' ' . $user_name, '_' ) ) {
1595              $errors->add( 'user_name', __( 'Sorry, usernames may not contain the character "_"!', 'buddypress' ) );
1596          }
1597  
1598          // No usernames that are all numeric. @todo Why?
1599          $match = array();
1600          preg_match( '/[0-9]*/', $user_name, $match );
1601          if ( $match[0] == $user_name ) {
1602              $errors->add( 'user_name', __( 'Sorry, usernames must have letters too!', 'buddypress' ) );
1603          }
1604  
1605          // Check into signups.
1606          $signups = BP_Signup::get( array(
1607              'user_login' => $user_name,
1608          ) );
1609  
1610          $signup = isset( $signups['signups'] ) && ! empty( $signups['signups'][0] ) ? $signups['signups'][0] : false;
1611  
1612          // Check if the username has been used already.
1613          if ( username_exists( $user_name ) || ! empty( $signup ) ) {
1614              $errors->add( 'user_name', __( 'Sorry, that username already exists!', 'buddypress' ) );
1615          }
1616  
1617          // Validate the email address and process the validation results into
1618          // error messages.
1619          $validate_email = bp_core_validate_email_address( $user_email );
1620          bp_core_add_validation_error_messages( $errors, $validate_email );
1621  
1622          // Assemble the return array.
1623          $result = array(
1624              'user_name'  => $user_name,
1625              'user_email' => $user_email,
1626              'errors'     => $errors,
1627          );
1628  
1629          // Apply WPMU legacy filter.
1630          $result = apply_filters( 'wpmu_validate_user_signup', $result );
1631      }
1632  
1633      /**
1634       * Filters the result of the user signup validation.
1635       *
1636       * @since 1.2.2
1637       *
1638       * @param array $result Results of user validation including errors, if any.
1639       */
1640      return apply_filters( 'bp_core_validate_user_signup', $result );
1641  }
1642  
1643  /**
1644   * Validate blog URL and title provided at signup.
1645   *
1646   * @since 1.2.2
1647   *
1648   * @todo Why do we have this wrapper?
1649   *
1650   * @param string $blog_url   Blog URL requested during registration.
1651   * @param string $blog_title Blog title requested during registration.
1652   * @return array
1653   */
1654  function bp_core_validate_blog_signup( $blog_url, $blog_title ) {
1655      if ( ! is_multisite() || ! function_exists( 'wpmu_validate_blog_signup' ) ) {
1656          return false;
1657      }
1658  
1659      /**
1660       * Filters the validated blog url and title provided at signup.
1661       *
1662       * @since 1.2.2
1663       *
1664       * @param array $value Array with the new site data and error messages.
1665       */
1666      return apply_filters( 'bp_core_validate_blog_signup', wpmu_validate_blog_signup( $blog_url, $blog_title ) );
1667  }
1668  
1669  /**
1670   * Process data submitted at user registration and convert to a signup object.
1671   *
1672   * @since 1.2.0
1673   *
1674   * @todo There appears to be a bug in the return value on success.
1675   *
1676   * @param string $user_login    Login name requested by the user.
1677   * @param string $user_password Password requested by the user.
1678   * @param string $user_email    Email address entered by the user.
1679   * @param array  $usermeta      Miscellaneous metadata about the user (blog-specific
1680   *                              signup data, xprofile data, etc).
1681   * @return int|false True on success, WP_Error on failure.
1682   */
1683  function bp_core_signup_user( $user_login, $user_password, $user_email, $usermeta ) {
1684      $bp = buddypress();
1685  
1686      // We need to cast $user_id to pass to the filters.
1687      $user_id = false;
1688  
1689      // Multisite installs have their own install procedure.
1690      if ( is_multisite() ) {
1691          wpmu_signup_user( $user_login, $user_email, $usermeta );
1692  
1693      } else {
1694          // Format data.
1695          $user_login     = preg_replace( '/\s+/', '', sanitize_user( $user_login, true ) );
1696          $user_email     = sanitize_email( $user_email );
1697          $activation_key = wp_generate_password( 32, false );
1698  
1699          /**
1700           * WordPress's default behavior is to create user accounts
1701           * immediately at registration time. BuddyPress uses a system
1702           * borrowed from WordPress Multisite, where signups are stored
1703           * separately and accounts are only created at the time of
1704           * activation. For backward compatibility with plugins that may
1705           * be anticipating WP's default behavior, BP silently creates
1706           * accounts for registrations (though it does not use them). If
1707           * you know that you are not running any plugins dependent on
1708           * these pending accounts, you may want to save a little DB
1709           * clutter by defining setting the BP_SIGNUPS_SKIP_USER_CREATION
1710           * to true in your wp-config.php file.
1711           */
1712          if ( ! defined( 'BP_SIGNUPS_SKIP_USER_CREATION' ) || ! BP_SIGNUPS_SKIP_USER_CREATION ) {
1713              $user_id = BP_Signup::add_backcompat( $user_login, $user_password, $user_email, $usermeta );
1714  
1715              if ( is_wp_error( $user_id ) ) {
1716                  return $user_id;
1717              }
1718  
1719              bp_update_user_meta( $user_id, 'activation_key', $activation_key );
1720          }
1721  
1722          $args = array(
1723              'user_login'     => $user_login,
1724              'user_email'     => $user_email,
1725              'activation_key' => $activation_key,
1726              'meta'           => $usermeta,
1727          );
1728  
1729          BP_Signup::add( $args );
1730  
1731          /**
1732           * Filters if BuddyPress should send an activation key for a new signup.
1733           *
1734           * @since 1.2.3
1735           *
1736           * @param bool   $value          Whether or not to send the activation key.
1737           * @param int    $user_id        User ID to send activation key to.
1738           * @param string $user_email     User email to send activation key to.
1739           * @param string $activation_key Activation key to be sent.
1740           * @param array  $usermeta       Miscellaneous metadata about the user (blog-specific
1741           *                               signup data, xprofile data, etc).
1742           */
1743          if ( apply_filters( 'bp_core_signup_send_activation_key', true, $user_id, $user_email, $activation_key, $usermeta ) ) {
1744              $salutation = $user_login;
1745              if ( isset( $usermeta[ 'field_' . bp_xprofile_fullname_field_id() ] ) ) {
1746                  $salutation = $usermeta[ 'field_' . bp_xprofile_fullname_field_id() ];
1747              }
1748  
1749              bp_core_signup_send_validation_email( $user_id, $user_email, $activation_key, $salutation );
1750          }
1751      }
1752  
1753      $bp->signup->username = $user_login;
1754  
1755      /**
1756       * Fires at the end of the process to sign up a user.
1757       *
1758       * @since 1.2.2
1759       *
1760       * @param bool|WP_Error   $user_id       True on success, WP_Error on failure.
1761       * @param string          $user_login    Login name requested by the user.
1762       * @param string          $user_password Password requested by the user.
1763       * @param string          $user_email    Email address requested by the user.
1764       * @param array           $usermeta      Miscellaneous metadata about the user (blog-specific
1765       *                                       signup data, xprofile data, etc).
1766       */
1767      do_action( 'bp_core_signup_user', $user_id, $user_login, $user_password, $user_email, $usermeta );
1768  
1769      return $user_id;
1770  }
1771  
1772  /**
1773   * Create a blog and user based on data supplied at user registration.
1774   *
1775   * @since 1.2.2
1776   *
1777   * @param string $blog_domain Domain requested by user.
1778   * @param string $blog_path   Path requested by user.
1779   * @param string $blog_title  Title as entered by user.
1780   * @param string $user_name   user_login of requesting user.
1781   * @param string $user_email  Email address of requesting user.
1782   * @param string $usermeta    Miscellaneous metadata for the user.
1783   * @return bool
1784   */
1785  function bp_core_signup_blog( $blog_domain, $blog_path, $blog_title, $user_name, $user_email, $usermeta ) {
1786      if ( ! is_multisite() || ! function_exists( 'wpmu_signup_blog' ) ) {
1787          return false;
1788      }
1789  
1790      /**
1791       * Filters the result of wpmu_signup_blog().
1792       *
1793       * This filter provides no value and is retained for
1794       * backwards compatibility.
1795       *
1796       * @since 1.2.2
1797       *
1798       * @param void $value
1799       */
1800      return apply_filters( 'bp_core_signup_blog', wpmu_signup_blog( $blog_domain, $blog_path, $blog_title, $user_name, $user_email, $usermeta ) );
1801  }
1802  
1803  /**
1804   * Activate a signup, as identified by an activation key.
1805   *
1806   * @since 1.2.2
1807   *
1808   * @param string $key Activation key.
1809   * @return int|bool User ID on success, false on failure.
1810   */
1811  function bp_core_activate_signup( $key ) {
1812      global $wpdb;
1813  
1814      $user = false;
1815  
1816      // Multisite installs have their own activation routine.
1817      if ( is_multisite() ) {
1818          $user = wpmu_activate_signup( $key );
1819  
1820          // If there were errors, add a message and redirect.
1821          if ( ! empty( $user->errors ) ) {
1822              return $user;
1823          }
1824  
1825          $user_id = $user['user_id'];
1826  
1827      } else {
1828          $signups = BP_Signup::get( array(
1829              'activation_key' => $key,
1830          ) );
1831  
1832          if ( empty( $signups['signups'] ) ) {
1833              return new WP_Error( 'invalid_key', __( 'Invalid activation key.', 'buddypress' ) );
1834          }
1835  
1836          $signup = $signups['signups'][0];
1837  
1838          if ( $signup->active ) {
1839              if ( empty( $signup->domain ) ) {
1840                  return new WP_Error( 'already_active', __( 'The user is already active.', 'buddypress' ), $signup );
1841              } else {
1842                  return new WP_Error( 'already_active', __( 'The site is already active.', 'buddypress' ), $signup );
1843              }
1844          }
1845  
1846          // Password is hashed again in wp_insert_user.
1847          $password = wp_generate_password( 12, false );
1848  
1849          $user_id = username_exists( $signup->user_login );
1850  
1851          // Create the user. This should only be necessary if BP_SIGNUPS_SKIP_USER_CREATION is true.
1852          if ( ! $user_id ) {
1853              $user_id = wp_create_user( $signup->user_login, $password, $signup->user_email );
1854  
1855          // Otherwise, update the existing user's status.
1856          } elseif ( $key === bp_get_user_meta( $user_id, 'activation_key', true ) || $key === wp_hash( $user_id ) ) {
1857  
1858              // Change the user's status so they become active.
1859              if ( ! $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_status = 0 WHERE ID = %d", $user_id ) ) ) {
1860                  return new WP_Error( 'invalid_key', __( 'Invalid activation key.', 'buddypress' ) );
1861              }
1862  
1863              bp_delete_user_meta( $user_id, 'activation_key' );
1864  
1865              $user_already_created = true;
1866  
1867          } else {
1868              $user_already_exists = true;
1869          }
1870  
1871          if ( ! $user_id ) {
1872              return new WP_Error( 'create_user', __( 'Could not create user', 'buddypress' ), $signup );
1873          }
1874  
1875          // Fetch the signup so we have the data later on.
1876          $signups = BP_Signup::get( array(
1877              'activation_key' => $key,
1878          ) );
1879  
1880          $signup = isset( $signups['signups'] ) && ! empty( $signups['signups'][0] ) ? $signups['signups'][0] : false;
1881  
1882          // Activate the signup.
1883          BP_Signup::validate( $key );
1884  
1885          if ( isset( $user_already_exists ) ) {
1886              return new WP_Error( 'user_already_exists', __( 'That username is already activated.', 'buddypress' ), $signup );
1887          }
1888  
1889          // Set up data to pass to the legacy filter.
1890          $user = array(
1891              'user_id'  => $user_id,
1892              'password' => $signup->meta['password'],
1893              'meta'     => $signup->meta,
1894          );
1895  
1896          /**
1897           * Maybe notify the site admin of a new user registration.
1898           *
1899           * @since 1.2.2
1900           *
1901           * @param bool $notification Whether to send the notification or not.
1902           */
1903          if ( apply_filters( 'bp_core_send_user_registration_admin_notification', true ) ) {
1904              wp_new_user_notification( $user_id );
1905          }
1906  
1907          if ( isset( $user_already_created ) ) {
1908  
1909              /**
1910               * Fires if the user has already been created.
1911               *
1912               * @since 1.2.2
1913               *
1914               * @param int    $user_id ID of the user being checked.
1915               * @param string $key     Activation key.
1916               * @param array  $user    Array of user data.
1917               */
1918              do_action( 'bp_core_activated_user', $user_id, $key, $user );
1919              return $user_id;
1920          }
1921      }
1922  
1923      // Set any profile data.
1924      if ( bp_is_active( 'xprofile' ) ) {
1925          if ( ! empty( $user['meta']['profile_field_ids'] ) ) {
1926              $profile_field_ids = explode( ',', $user['meta']['profile_field_ids'] );
1927  
1928              foreach( (array) $profile_field_ids as $field_id ) {
1929                  $current_field = isset( $user['meta']["field_{$field_id}"] ) ? $user['meta']["field_{$field_id}"] : false;
1930  
1931                  if ( !empty( $current_field ) ) {
1932                      xprofile_set_field_data( $field_id, $user_id, $current_field );
1933                  }
1934  
1935                  /*
1936                   * Save the visibility level.
1937                   *
1938                   * Use the field's default visibility if not present, and 'public' if a
1939                   * default visibility is not defined.
1940                   */
1941                  $key = "field_{$field_id}_visibility";
1942                  if ( isset( $user['meta'][ $key ] ) ) {
1943                      $visibility_level = $user['meta'][ $key ];
1944                  } else {
1945                      $vfield           = xprofile_get_field( $field_id );
1946                      $visibility_level = isset( $vfield->default_visibility ) ? $vfield->default_visibility : 'public';
1947                  }
1948                  xprofile_set_field_visibility_level( $field_id, $user_id, $visibility_level );
1949              }
1950          }
1951      }
1952  
1953      // Replace the password automatically generated by WordPress by the one the user chose.
1954      if ( ! empty( $user['meta']['password'] ) ) {
1955          $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_pass = %s WHERE ID = %d", $user['meta']['password'], $user_id ) );
1956  
1957          /**
1958           * Make sure to clean the user's cache as we've
1959           * directly edited the password without using
1960           * wp_update_user().
1961           *
1962           * If we can't use wp_update_user() that's because
1963           * we already hashed the password at the signup step.
1964           */
1965          $uc = wp_cache_get( $user_id, 'users' );
1966  
1967          if ( ! empty( $uc->ID ) ) {
1968              clean_user_cache( $uc->ID );
1969          }
1970      }
1971  
1972      /**
1973       * Fires at the end of the user activation process.
1974       *
1975       * @since 1.2.2
1976       *
1977       * @param int    $user_id ID of the user being checked.
1978       * @param string $key     Activation key.
1979       * @param array  $user    Array of user data.
1980       */
1981      do_action( 'bp_core_activated_user', $user_id, $key, $user );
1982  
1983      return $user_id;
1984  }
1985  
1986  /**
1987   * Add default WordPress role for new signups on the BP root blog.
1988   *
1989   * @since 3.0.0
1990   *
1991   * @param int $user_id The user ID to add the default role for.
1992   */
1993  function bp_members_add_role_after_activation( $user_id ) {
1994      // Get default role to add.
1995      $role = bp_get_option( 'default_role' );
1996  
1997      // Multisite.
1998      if ( is_multisite() && ! is_user_member_of_blog( $user_id, bp_get_root_blog_id() ) ) {
1999          add_user_to_blog( bp_get_root_blog_id(), $user_id, $role );
2000  
2001      // Single-site.
2002      } elseif ( ! is_multisite() ) {
2003          $member = get_userdata( $user_id );
2004          $member->set_role( $role );
2005      }
2006  }
2007  add_action( 'bp_core_activated_user', 'bp_members_add_role_after_activation', 1 );
2008  
2009  /**
2010   * Migrate signups from pre-2.0 configuration to wp_signups.
2011   *
2012   * @since 2.0.1
2013   */
2014  function bp_members_migrate_signups() {
2015      global $wpdb;
2016  
2017      $status_2_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->users} WHERE user_status = '2'" );
2018  
2019      if ( ! empty( $status_2_ids ) ) {
2020          $signups = get_users( array(
2021              'fields'  => array(
2022                  'ID',
2023                  'user_login',
2024                  'user_pass',
2025                  'user_registered',
2026                  'user_email',
2027                  'display_name',
2028              ),
2029              'include' => $status_2_ids,
2030          ) );
2031  
2032          // Fetch activation keys separately, to avoid the all_with_meta
2033          // overhead.
2034          $status_2_ids_sql = implode( ',', $status_2_ids );
2035          $ak_data = $wpdb->get_results( "SELECT user_id, meta_value FROM {$wpdb->usermeta} WHERE meta_key = 'activation_key' AND user_id IN ({$status_2_ids_sql})" );
2036  
2037          // Rekey.
2038          $activation_keys = array();
2039          foreach ( $ak_data as $ak_datum ) {
2040              $activation_keys[ intval( $ak_datum->user_id ) ] = $ak_datum->meta_value;
2041          }
2042  
2043          unset( $status_2_ids_sql, $status_2_ids, $ak_data );
2044  
2045          // Merge.
2046          foreach ( $signups as &$signup ) {
2047              if ( isset( $activation_keys[ $signup->ID ] ) ) {
2048                  $signup->activation_key = $activation_keys[ $signup->ID ];
2049              }
2050          }
2051  
2052          // Reset the signup var as we're using it to process the migration.
2053          unset( $signup );
2054  
2055      } else {
2056          return;
2057      }
2058  
2059      foreach ( $signups as $signup ) {
2060          $meta = array();
2061  
2062          // Rebuild the activation key, if missing.
2063          if ( empty( $signup->activation_key ) ) {
2064              $signup->activation_key = wp_generate_password( 32, false );
2065          }
2066  
2067          if ( bp_is_active( 'xprofile' ) ) {
2068              $meta['field_1'] = $signup->display_name;
2069          }
2070  
2071          $meta['password'] = $signup->user_pass;
2072  
2073          $user_login = preg_replace( '/\s+/', '', sanitize_user( $signup->user_login, true ) );
2074          $user_email = sanitize_email( $signup->user_email );
2075  
2076          BP_Signup::add( array(
2077              'user_login'     => $user_login,
2078              'user_email'     => $user_email,
2079              'registered'     => $signup->user_registered,
2080              'activation_key' => $signup->activation_key,
2081              'meta'           => $meta
2082          ) );
2083  
2084          // Deleting these options will remove signups from users count.
2085          delete_user_option( $signup->ID, 'capabilities' );
2086          delete_user_option( $signup->ID, 'user_level'   );
2087      }
2088  }
2089  
2090  /**
2091   * Map a user's WP display name to the XProfile fullname field, if necessary.
2092   *
2093   * This only happens when a user is registered in wp-admin by an administrator;
2094   * during normal registration, XProfile data is provided directly by the user.
2095   *
2096   * @since 1.2.0
2097   *
2098   * @param int $user_id ID of the user.
2099   * @return bool
2100   */
2101  function bp_core_map_user_registration( $user_id ) {
2102  
2103      // Only map data when the site admin is adding users, not on registration.
2104      if ( ! is_admin() ) {
2105          return false;
2106      }
2107  
2108      // Add the user's fullname to Xprofile.
2109      if ( bp_is_active( 'xprofile' ) ) {
2110          $firstname = bp_get_user_meta( $user_id, 'first_name', true );
2111          $lastname = ' ' . bp_get_user_meta( $user_id, 'last_name', true );
2112          $name = $firstname . $lastname;
2113  
2114          if ( empty( $name ) || ' ' == $name ) {
2115              $name = bp_get_user_meta( $user_id, 'nickname', true );
2116          }
2117  
2118          xprofile_set_field_data( 1, $user_id, $name );
2119      }
2120  }
2121  add_action( 'user_register', 'bp_core_map_user_registration' );
2122  
2123  /**
2124   * Get the avatar storage directory for use during registration.
2125   *
2126   * @since 1.1.0
2127   *
2128   * @return string|bool Directory path on success, false on failure.
2129   */
2130  function bp_core_signup_avatar_upload_dir() {
2131      $bp = buddypress();
2132  
2133      if ( empty( $bp->signup->avatar_dir ) ) {
2134          return false;
2135      }
2136  
2137      $directory = 'avatars/signups';
2138      $path      = bp_core_avatar_upload_path() . '/' . $directory . '/' . $bp->signup->avatar_dir;
2139      $newbdir   = $path;
2140      $newurl    = bp_core_avatar_url() . '/' . $directory . '/' . $bp->signup->avatar_dir;
2141      $newburl   = $newurl;
2142      $newsubdir = '/' . $directory . '/' . $bp->signup->avatar_dir;
2143  
2144      /**
2145       * Filters the avatar storage directory for use during registration.
2146       *
2147       * @since 1.1.1
2148       *
2149       * @param array $value Array of path and URL values for created storage directory.
2150       */
2151      return apply_filters( 'bp_core_signup_avatar_upload_dir', array(
2152          'path'    => $path,
2153          'url'     => $newurl,
2154          'subdir'  => $newsubdir,
2155          'basedir' => $newbdir,
2156          'baseurl' => $newburl,
2157          'error'   => false
2158      ) );
2159  }
2160  
2161  /**
2162   * Send activation email to a newly registered user.
2163   *
2164   * @since 1.2.2
2165   * @since 2.5.0 Add the $user_login parameter.
2166   * @since 5.0.0 Change $user_login parameter to more general $salutation.
2167   *
2168   * @param int|bool $user_id    ID of the new user, false if BP_SIGNUPS_SKIP_USER_CREATION is true.
2169   * @param string   $user_email   Email address of the new user.
2170   * @param string   $key          Activation key.
2171   * @param string   $salutation   Optional. The name to be used as a salutation in the email.
2172   */
2173  function bp_core_signup_send_validation_email( $user_id, $user_email, $key, $salutation = '' ) {
2174      $args = array(
2175          'tokens' => array(
2176              'activate.url' => esc_url( trailingslashit( bp_get_activation_page() ) . "{$key}/" ),
2177              'key'          => $key,
2178              'user.email'   => $user_email,
2179              'user.id'      => $user_id,
2180          ),
2181      );
2182  
2183      $to = array( array( $user_email => $salutation ) );
2184  
2185      bp_send_email( 'core-user-registration', $to, $args );
2186  }
2187  
2188  /**
2189   * Display a "resend email" link when an unregistered user attempts to log in.
2190   *
2191   * @since 1.2.2
2192   *
2193   * @param WP_User|WP_Error|null $user     Either the WP_User or the WP_Error object.
2194   * @param string                $username The inputted, attempted username.
2195   * @param string                $password The inputted, attempted password.
2196   * @return WP_User|WP_Error
2197   */
2198  function bp_core_signup_disable_inactive( $user = null, $username = '', $password ='' ) {
2199      // Login form not used.
2200      if ( empty( $username ) && empty( $password ) ) {
2201          return $user;
2202      }
2203  
2204      // An existing WP_User with a user_status of 2 is either a legacy
2205      // signup, or is a user created for backward compatibility. See
2206      // {@link bp_core_signup_user()} for more details.
2207      if ( is_a( $user, 'WP_User' ) && 2 == $user->user_status ) {
2208          $user_login = $user->user_login;
2209  
2210      // If no WP_User is found corresponding to the username, this
2211      // is a potential signup.
2212      } elseif ( is_wp_error( $user ) && 'invalid_username' == $user->get_error_code() ) {
2213          $user_login = $username;
2214  
2215      // This is an activated user, so bail.
2216      } else {
2217          return $user;
2218      }
2219  
2220      // Look for the unactivated signup corresponding to the login name.
2221      $signup = BP_Signup::get( array( 'user_login' => sanitize_user( $user_login ) ) );
2222  
2223      // No signup or more than one, something is wrong. Let's bail.
2224      if ( empty( $signup['signups'][0] ) || $signup['total'] > 1 ) {
2225          return $user;
2226      }
2227  
2228      // Unactivated user account found!
2229      // Set up the feedback message.
2230      $signup_id = $signup['signups'][0]->signup_id;
2231  
2232      $resend_url_params = array(
2233          'action' => 'bp-resend-activation',
2234          'id'     => $signup_id,
2235      );
2236  
2237      $resend_url = wp_nonce_url(
2238          add_query_arg( $resend_url_params, wp_login_url() ),
2239          'bp-resend-activation'
2240      );
2241  
2242      $resend_string = '<br /><br />' . sprintf( __( 'If you have not received an email yet, <a href="%s">click here to resend it</a>.', 'buddypress' ), esc_url( $resend_url ) );
2243  
2244      return new WP_Error( 'bp_account_not_activated', __( '<strong>ERROR</strong>: Your account has not been activated. Check your email for the activation link.', 'buddypress' ) . $resend_string );
2245  }
2246  add_filter( 'authenticate', 'bp_core_signup_disable_inactive', 30, 3 );
2247  
2248  /**
2249   * On the login screen, resends the activation email for a user.
2250   *
2251   * @since 2.0.0
2252   *
2253   * @see bp_core_signup_disable_inactive()
2254   */
2255  function bp_members_login_resend_activation_email() {
2256      global $error;
2257  
2258      if ( empty( $_GET['id'] ) || empty( $_GET['_wpnonce'] ) ) {
2259          return;
2260      }
2261  
2262      // Verify nonce.
2263      if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'bp-resend-activation' ) ) {
2264          die( 'Security check' );
2265      }
2266  
2267      $signup_id = (int) $_GET['id'];
2268  
2269      // Resend the activation email.
2270      // also updates the 'last sent' and '# of emails sent' values.
2271      $resend = BP_Signup::resend( array( $signup_id ) );
2272  
2273      // Add feedback message.
2274      if ( ! empty( $resend['errors'] ) ) {
2275          $error = __( '<strong>ERROR</strong>: Your account has already been activated.', 'buddypress' );
2276      } else {
2277          $error = __( 'Activation email resent! Please check your inbox or spam folder.', 'buddypress' );
2278      }
2279  }
2280  add_action( 'login_form_bp-resend-activation', 'bp_members_login_resend_activation_email' );
2281  
2282  /**
2283   * Redirect away from wp-signup.php if BP registration templates are present.
2284   *
2285   * @since 1.1.0
2286   */
2287  function bp_core_wpsignup_redirect() {
2288  
2289      // Bail in admin or if custom signup page is broken.
2290      if ( is_admin() || ! bp_has_custom_signup_page() ) {
2291          return;
2292      }
2293  
2294      $is_wp_signup = false;
2295      if ( ! empty( $_SERVER['SCRIPT_NAME'] ) ) {
2296          $script_name_path = wp_parse_url( $_SERVER['SCRIPT_NAME'], PHP_URL_PATH );
2297  
2298          if ( 'wp-signup.php' === basename( $script_name_path ) || ( 'wp-login.php' === basename( $script_name_path ) && ! empty( $_GET['action'] ) && 'register' === $_GET['action'] ) ) {
2299              $is_wp_signup = true;
2300          }
2301      }
2302  
2303      // If this is not wp-signup.php, there's nothing to do here.
2304      if ( ! $is_wp_signup ) {
2305          return;
2306      }
2307  
2308      /*
2309       * We redirect wp-signup.php to the registration page except when it's a site signup.
2310       * In that case, redirect to the BP site creation page if available, otherwise allow
2311       * access to wp-signup.php.
2312       */
2313      $redirect_to = bp_get_signup_page();
2314  
2315      $is_site_creation = false;
2316  
2317      $referer = wp_get_referer();
2318  
2319      // A new site is being added.
2320      if ( isset( $_POST['stage'] ) && $_POST['stage'] === 'gimmeanotherblog' ) {
2321          $is_site_creation = true;
2322  
2323      // We've arrived at wp-signup.php from my-sites.php.
2324      } elseif ( $referer ) {
2325          $referer_path     = wp_parse_url( $referer, PHP_URL_PATH );
2326          $is_site_creation = false !== strpos( $referer_path, 'wp-admin/my-sites.php' );
2327      }
2328  
2329      if ( $is_site_creation ) {
2330          if ( bp_is_active( 'blogs' ) ) {
2331              $redirect_to = trailingslashit( bp_get_blogs_directory_permalink() . 'create' );
2332          } else {
2333              // Perform no redirect in this case.
2334              $redirect_to = '';
2335          }
2336      }
2337  
2338      if ( ! $redirect_to ) {
2339          return;
2340      }
2341  
2342      bp_core_redirect( $redirect_to );
2343  }
2344  add_action( 'bp_init', 'bp_core_wpsignup_redirect' );
2345  
2346  /**
2347   * Stop a logged-in user who is marked as a spammer.
2348   *
2349   * When an admin marks a live user as a spammer, that user can still surf
2350   * around and cause havoc on the site until that person is logged out.
2351   *
2352   * This code checks to see if a logged-in user is marked as a spammer.  If so,
2353   * we redirect the user back to wp-login.php with the 'reauth' parameter.
2354   *
2355   * This clears the logged-in spammer's cookies and will ask the spammer to
2356   * reauthenticate.
2357   *
2358   * Note: A spammer cannot log back in - {@see bp_core_boot_spammer()}.
2359   *
2360   * Runs on 'bp_init' at priority 5 so the members component globals are setup
2361   * before we do our spammer checks.
2362   *
2363   * This is important as the $bp->loggedin_user object is setup at priority 4.
2364   *
2365   * @since 1.8.0
2366   */
2367  function bp_stop_live_spammer() {
2368      // If we're on the login page, stop now to prevent redirect loop.
2369      $is_login = false;
2370      if ( isset( $GLOBALS['pagenow'] ) && ( false !== strpos( $GLOBALS['pagenow'], 'wp-login.php' ) ) ) {
2371          $is_login = true;
2372      } elseif ( isset( $_SERVER['SCRIPT_NAME'] ) && false !== strpos( $_SERVER['SCRIPT_NAME'], 'wp-login.php' ) ) {
2373          $is_login = true;
2374      }
2375  
2376      if ( $is_login ) {
2377          return;
2378      }
2379  
2380      // User isn't logged in, so stop!
2381      if ( ! is_user_logged_in() ) {
2382          return;
2383      }
2384  
2385      // If spammer, redirect to wp-login.php and reauthorize.
2386      if ( bp_is_user_spammer( bp_loggedin_user_id() ) ) {
2387          // Setup login args.
2388          $args = array(
2389              // Custom action used to throw an error message.
2390              'action' => 'bp-spam',
2391  
2392              // Reauthorize user to login.
2393              'reauth' => 1
2394          );
2395  
2396          /**
2397           * Filters the url used for redirection for a logged in user marked as spam.
2398           *
2399           * @since 1.8.0
2400           *
2401           * @param string $value URL to redirect user to.
2402           */
2403          $login_url = apply_filters( 'bp_live_spammer_redirect', add_query_arg( $args, wp_login_url() ) );
2404  
2405          // Redirect user to login page.
2406          wp_redirect( $login_url );
2407          die();
2408      }
2409  }
2410  add_action( 'bp_init', 'bp_stop_live_spammer', 5 );
2411  
2412  /**
2413   * Show a custom error message when a logged-in user is marked as a spammer.
2414   *
2415   * @since 1.8.0
2416   */
2417  function bp_live_spammer_login_error() {
2418      global $error;
2419  
2420      $error = __( '<strong>ERROR</strong>: Your account has been marked as a spammer.', 'buddypress' );
2421  
2422      // Shake shake shake!
2423      add_action( 'login_head', 'wp_shake_js', 12 );
2424  }
2425  add_action( 'login_form_bp-spam', 'bp_live_spammer_login_error' );
2426  
2427  /**
2428   * Get the displayed user Object
2429   *
2430   * @since 2.6.0
2431   *
2432   * @return object The displayed user object, null otherwise.
2433   */
2434  function bp_get_displayed_user() {
2435      $bp = buddypress();
2436  
2437      $displayed_user = null;
2438      if ( ! empty( $bp->displayed_user->id ) ) {
2439          $displayed_user = $bp->displayed_user;
2440      }
2441  
2442      /**
2443       * Filters the displayed_user object corresponding to the displayed member.
2444       *
2445       * @since 2.6.0
2446       *
2447       * @param object $displayed_user The displayed_user object.
2448       */
2449      return apply_filters( 'bp_get_displayed_user', $displayed_user );
2450  }
2451  
2452  /** Member Types *************************************************************/
2453  
2454  /**
2455   * Output the slug of the member type taxonomy.
2456   *
2457   * @since 2.7.0
2458   */
2459  function bp_member_type_tax_name() {
2460      echo bp_get_member_type_tax_name();
2461  }
2462  
2463      /**
2464       * Return the slug of the member type taxonomy.
2465       *
2466       * @since 2.7.0
2467       *
2468       * @return string The unique member taxonomy slug.
2469       */
2470  	function bp_get_member_type_tax_name() {
2471          /**
2472           * Filters the slug of the member type taxonomy.
2473           *
2474           * @since 2.7.0
2475           *
2476           * @param string $value Member type taxonomy slug.
2477           */
2478          return apply_filters( 'bp_get_member_type_tax_name', 'bp_member_type' );
2479      }
2480  
2481  /**
2482   * Register a member type.
2483   *
2484   * @since 2.2.0
2485   *
2486   * @param string $member_type Unique string identifier for the member type.
2487   * @param array  $args {
2488   *     Array of arguments describing the member type.
2489   *
2490   *     @type array       $labels {
2491   *         Array of labels to use in various parts of the interface.
2492   *
2493   *         @type string $name          Default name. Should typically be plural.
2494   *         @type string $singular_name Singular name.
2495   *     }
2496   *     @type bool|string $has_directory Whether the member type should have its own type-specific directory.
2497   *                                      Pass `true` to use the `$member_type` string as the type's slug.
2498   *                                      Pass a string to customize the slug. Pass `false` to disable.
2499   *                                      Default: true.
2500   * }
2501   * @return object|WP_Error Member type object on success, WP_Error object on failure.
2502   */
2503  function bp_register_member_type( $member_type, $args = array() ) {
2504      $bp = buddypress();
2505  
2506      if ( isset( $bp->members->types[ $member_type ] ) ) {
2507          return new WP_Error( 'bp_member_type_exists', __( 'Member type already exists.', 'buddypress' ), $member_type );
2508      }
2509  
2510      $r = bp_parse_args( $args, array(
2511          'labels'        => array(),
2512          'has_directory' => true,
2513      ), 'register_member_type' );
2514  
2515      $member_type = sanitize_key( $member_type );
2516  
2517      /**
2518       * Filters the list of illegal member type names.
2519       *
2520       * - 'any' is a special pseudo-type, representing items unassociated with any member type.
2521       * - 'null' is a special pseudo-type, representing users without any type.
2522       * - '_none' is used internally to denote an item that should not apply to any member types.
2523       *
2524       * @since 2.4.0
2525       *
2526       * @param array $illegal_names Array of illegal names.
2527       */
2528      $illegal_names = apply_filters( 'bp_member_type_illegal_names', array( 'any', 'null', '_none' ) );
2529      if ( in_array( $member_type, $illegal_names, true ) ) {
2530          return new WP_Error( 'bp_member_type_illegal_name', __( 'You may not register a member type with this name.', 'buddypress' ), $member_type );
2531      }
2532  
2533      // Store the post type name as data in the object (not just as the array key).
2534      $r['name'] = $member_type;
2535  
2536      // Make sure the relevant labels have been filled in.
2537      $default_name = isset( $r['labels']['name'] ) ? $r['labels']['name'] : ucfirst( $r['name'] );
2538      $r['labels'] = array_merge( array(
2539          'name'          => $default_name,
2540          'singular_name' => $default_name,
2541      ), $r['labels'] );
2542  
2543      // Directory slug.
2544      if ( $r['has_directory'] ) {
2545          // A string value is intepreted as the directory slug. Otherwise fall back on member type.
2546          if ( is_string( $r['has_directory'] ) ) {
2547              $directory_slug = $r['has_directory'];
2548          } else {
2549              $directory_slug = $member_type;
2550          }
2551  
2552          // Sanitize for use in URLs.
2553          $r['directory_slug'] = sanitize_title( $directory_slug );
2554          $r['has_directory']  = true;
2555      } else {
2556          $r['directory_slug'] = '';
2557          $r['has_directory']  = false;
2558      }
2559  
2560      $bp->members->types[ $member_type ] = $type = (object) $r;
2561  
2562      /**
2563       * Fires after a member type is registered.
2564       *
2565       * @since 2.2.0
2566       *
2567       * @param string $member_type Member type identifier.
2568       * @param object $type        Member type object.
2569       */
2570      do_action( 'bp_registered_member_type', $member_type, $type );
2571  
2572      return $type;
2573  }
2574  
2575  /**
2576   * Retrieve a member type object by name.
2577   *
2578   * @since 2.2.0
2579   *
2580   * @param string $member_type The name of the member type.
2581   * @return object A member type object.
2582   */
2583  function bp_get_member_type_object( $member_type ) {
2584      $types = bp_get_member_types( array(), 'objects' );
2585  
2586      if ( empty( $types[ $member_type ] ) ) {
2587          return null;
2588      }
2589  
2590      return $types[ $member_type ];
2591  }
2592  
2593  /**
2594   * Get a list of all registered member type objects.
2595   *
2596   * @since 2.2.0
2597   *
2598   * @see bp_register_member_type() for accepted arguments.
2599   *
2600   * @param array|string $args     Optional. An array of key => value arguments to match against
2601   *                               the member type objects. Default empty array.
2602   * @param string       $output   Optional. The type of output to return. Accepts 'names'
2603   *                               or 'objects'. Default 'names'.
2604   * @param string       $operator Optional. The logical operation to perform. 'or' means only one
2605   *                               element from the array needs to match; 'and' means all elements
2606   *                               must match. Accepts 'or' or 'and'. Default 'and'.
2607   * @return array A list of member type names or objects.
2608   */
2609  function bp_get_member_types( $args = array(), $output = 'names', $operator = 'and' ) {
2610      $types = buddypress()->members->types;
2611  
2612      $types = wp_filter_object_list( $types, $args, $operator );
2613  
2614      /**
2615       * Filters the array of member type objects.
2616       *
2617       * This filter is run before the $output filter has been applied, so that
2618       * filtering functions have access to the entire member type objects.
2619       *
2620       * @since 2.2.0
2621       *
2622       * @param array  $types     Member type objects, keyed by name.
2623       * @param array  $args      Array of key=>value arguments for filtering.
2624       * @param string $operator  'or' to match any of $args, 'and' to require all.
2625       */
2626      $types = apply_filters( 'bp_get_member_types', $types, $args, $operator );
2627  
2628      if ( 'names' === $output ) {
2629          $types = wp_list_pluck( $types, 'name' );
2630      }
2631  
2632      return $types;
2633  }
2634  
2635  /**
2636   * Set type for a member.
2637   *
2638   * @since 2.2.0
2639   *
2640   * @param int    $user_id     ID of the user.
2641   * @param string $member_type Member type.
2642   * @param bool   $append      Optional. True to append this to existing types for user,
2643   *                            false to replace. Default: false.
2644   * @return false|array $retval See {@see bp_set_object_terms()}.
2645   */
2646  function bp_set_member_type( $user_id, $member_type, $append = false ) {
2647      // Pass an empty $member_type to remove a user's type.
2648      if ( ! empty( $member_type ) && ! bp_get_member_type_object( $member_type ) ) {
2649          return false;
2650      }
2651  
2652      $retval = bp_set_object_terms( $user_id, $member_type, bp_get_member_type_tax_name(), $append );
2653  
2654      // Bust the cache if the type has been updated.
2655      if ( ! is_wp_error( $retval ) ) {
2656          wp_cache_delete( $user_id, 'bp_member_member_type' );
2657  
2658          /**
2659           * Fires just after a user's member type has been changed.
2660           *
2661           * @since 2.2.0
2662           *
2663           * @param int    $user_id     ID of the user whose member type has been updated.
2664           * @param string $member_type Member type.
2665           * @param bool   $append      Whether the type is being appended to existing types.
2666           */
2667          do_action( 'bp_set_member_type', $user_id, $member_type, $append );
2668      }
2669  
2670      return $retval;
2671  }
2672  
2673  /**
2674   * Remove type for a member.
2675   *
2676   * @since 2.3.0
2677   *
2678   * @param int    $user_id     ID of the user.
2679   * @param string $member_type Member Type.
2680   * @return bool|WP_Error
2681   */
2682  function bp_remove_member_type( $user_id, $member_type ) {
2683      // Bail if no valid member type was passed.
2684      if ( empty( $member_type ) || ! bp_get_member_type_object( $member_type ) ) {
2685          return false;
2686      }
2687  
2688      $deleted = bp_remove_object_terms( $user_id, $member_type, bp_get_member_type_tax_name() );
2689  
2690      // Bust the cache if the type has been removed.
2691      if ( ! is_wp_error( $deleted ) ) {
2692          wp_cache_delete( $user_id, 'bp_member_member_type' );
2693  
2694          /**
2695           * Fires just after a user's member type has been removed.
2696           *
2697           * @since 2.3.0
2698           *
2699           * @param int    $user_id     ID of the user whose member type has been updated.
2700           * @param string $member_type Member type.
2701           */
2702          do_action( 'bp_remove_member_type', $user_id, $member_type );
2703      }
2704  
2705      return $deleted;
2706  }
2707  
2708  /**
2709   * Get type for a member.
2710   *
2711   * @since 2.2.0
2712   *
2713   * @param int  $user_id ID of the user.
2714   * @param bool $single  Optional. Whether to return a single type string. If multiple types are found
2715   *                      for the user, the oldest one will be returned. Default: true.
2716   * @return string|array|bool On success, returns a single member type (if $single is true) or an array of member
2717   *                           types (if $single is false). Returns false on failure.
2718   */
2719  function bp_get_member_type( $user_id, $single = true ) {
2720      $types = wp_cache_get( $user_id, 'bp_member_member_type' );
2721  
2722      if ( false === $types ) {
2723          $raw_types = bp_get_object_terms( $user_id, bp_get_member_type_tax_name() );
2724  
2725          if ( ! is_wp_error( $raw_types ) ) {
2726              $types =  array();
2727  
2728              // Only include currently registered group types.
2729              foreach ( $raw_types as $mtype ) {
2730                  if ( bp_get_member_type_object( $mtype->name ) ) {
2731                      $types[] = $mtype->name;
2732                  }
2733              }
2734  
2735              wp_cache_set( $user_id, $types, 'bp_member_member_type' );
2736          }
2737      }
2738  
2739      $type = false;
2740      if ( ! empty( $types ) ) {
2741          if ( $single ) {
2742              $type = array_pop( $types );
2743          } else {
2744              $type = $types;
2745          }
2746      }
2747  
2748      /**
2749       * Filters a user's member type(s).
2750       *
2751       * @since 2.2.0
2752       *
2753       * @param string $type    Member type.
2754       * @param int    $user_id ID of the user.
2755       * @param bool   $single  Whether to return a single type string, or an array.
2756       */
2757      return apply_filters( 'bp_get_member_type', $type, $user_id, $single );
2758  }
2759  
2760  /**
2761   * Check whether the given user has a certain member type.
2762   *
2763   * @since 2.3.0
2764   *
2765   * @param int    $user_id     $user_id ID of the user.
2766   * @param string $member_type Member Type.
2767   * @return bool Whether the user has the given member type.
2768   */
2769  function bp_has_member_type( $user_id, $member_type ) {
2770      // Bail if no valid member type was passed.
2771      if ( empty( $member_type ) || ! bp_get_member_type_object( $member_type ) ) {
2772          return false;
2773      }
2774  
2775      // Get all user's member types.
2776      $types = bp_get_member_type( $user_id, false );
2777  
2778      if ( ! is_array( $types ) ) {
2779          return false;
2780      }
2781  
2782      return in_array( $member_type, $types );
2783  }
2784  
2785  /**
2786   * Delete a user's member type when the user when the user is deleted.
2787   *
2788   * @since 2.2.0
2789   *
2790   * @param int $user_id ID of the user.
2791   * @return false|array $value See {@see bp_set_member_type()}.
2792   */
2793  function bp_remove_member_type_on_user_delete( $user_id ) {
2794      return bp_set_member_type( $user_id, '' );
2795  }
2796  add_action( 'wpmu_delete_user', 'bp_remove_member_type_on_user_delete' );
2797  add_action( 'delete_user', 'bp_remove_member_type_on_user_delete' );
2798  
2799  /**
2800   * Get the "current" member type, if one is provided, in member directories.
2801   *
2802   * @since 2.3.0
2803   *
2804   * @return string
2805   */
2806  function bp_get_current_member_type() {
2807  
2808      /**
2809       * Filters the "current" member type, if one is provided, in member directories.
2810       *
2811       * @since 2.3.0
2812       *
2813       * @param string $value "Current" member type.
2814       */
2815      return apply_filters( 'bp_get_current_member_type', buddypress()->current_member_type );
2816  }


Generated: Tue Jul 16 01:01:43 2019 Cross-referenced by PHPXref 0.7.1