[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-members/classes/ -> class-bp-signup.php (source)

   1  <?php
   2  /**
   3   * Signups Management class.
   4   *
   5   * @package BuddyPress
   6   * @subpackage coreClasses
   7   * @since 2.0.0
   8   */
   9  
  10  /**
  11   * Class used to handle Signups.
  12   *
  13   * @since 2.0.0
  14   */
  15  class BP_Signup {
  16  
  17      /**
  18       * ID of the signup which the object relates to.
  19       *
  20       * @since 2.0.0
  21       * @var integer
  22       */
  23      public $id;
  24  
  25      /**
  26       * The URL to the full size of the avatar for the user.
  27       *
  28       * @since 2.0.0
  29       * @var string
  30       */
  31      public $avatar;
  32  
  33      /**
  34       * The username for the user.
  35       *
  36       * @since 2.0.0
  37       * @var string
  38       */
  39      public $user_login;
  40  
  41      /**
  42       * The email for the user.
  43       *
  44       * @since 2.0.0
  45       * @var string
  46       */
  47      public $user_email;
  48  
  49      /**
  50       * The full name of the user.
  51       *
  52       * @since 2.0.0
  53       * @var string
  54       */
  55      public $user_name;
  56  
  57      /**
  58       * Metadata associated with the signup.
  59       *
  60       * @since 2.0.0
  61       * @var array
  62       */
  63      public $meta;
  64  
  65      /**
  66       * The registered date for the user.
  67       *
  68       * @since 2.0.0
  69       * @var string
  70       */
  71      public $registered;
  72  
  73      /**
  74       * The activation key for the user.
  75       *
  76       * @since 2.0.0
  77       * @var string
  78       */
  79      public $activation_key;
  80  
  81      /**
  82       * The activated date for the user.
  83       *
  84       * @since 10.0.0
  85       * @var string
  86       */
  87      public $activated;
  88  
  89      /**
  90       * Whether the user account is activated or not.
  91       *
  92       * @since 10.0.0
  93       * @var bool
  94       */
  95      public $active;
  96  
  97      /**
  98       * The date that the last activation email was sent.
  99       *
 100       * @since 10.0.0
 101       * @var string
 102       */
 103      public $date_sent;
 104  
 105      /**
 106       * Was the last activation email sent in the last 24 hours?
 107       *
 108       * @since 10.0.0
 109       * @var bool
 110       */
 111      public $recently_sent;
 112  
 113      /**
 114       * The number of activation emails sent to this user.
 115       *
 116       * @since 10.0.0
 117       * @var int
 118       */
 119      public $count_sent;
 120  
 121      /**
 122       * The domain for the signup.
 123       *
 124       * @since 10.0.0
 125       * @var string
 126       */
 127      public $domain;
 128  
 129      /**
 130       * The path for the signup.
 131       *
 132       * @since 10.0.0
 133       * @var string
 134       */
 135      public $path;
 136  
 137      /**
 138       * The title for the signup.
 139       *
 140       * @since 10.0.0
 141       * @var string
 142       */
 143      public $title;
 144  
 145  
 146      /** Public Methods *******************************************************/
 147  
 148      /**
 149       * Class constructor.
 150       *
 151       * @since 2.0.0
 152       *
 153       * @param integer $signup_id The ID for the signup being queried.
 154       */
 155  	public function __construct( $signup_id = 0 ) {
 156          if ( ! empty( $signup_id ) ) {
 157              $this->id = $signup_id;
 158              $this->populate();
 159          }
 160      }
 161  
 162      /**
 163       * Populate the instantiated class with data based on the signup_id provided.
 164       *
 165       * @since 2.0.0
 166       */
 167  	public function populate() {
 168          global $wpdb;
 169  
 170          // Get BuddyPress.
 171          $bp = buddypress();
 172  
 173          // Check cache for signup data.
 174          $signup = wp_cache_get( $this->id, 'bp_signups' );
 175  
 176          // Cache missed, so query the DB.
 177          if ( false === $signup ) {
 178              $signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->members->table_name_signups} WHERE signup_id = %d", $this->id ) );
 179  
 180              wp_cache_set( $this->id, $signup, 'bp_signups' );
 181          }
 182  
 183          // No signup found so set the ID and bail.
 184          if ( empty( $signup ) || is_wp_error( $signup ) ) {
 185              $this->id = 0;
 186              return;
 187          }
 188  
 189          /*
 190           * Add every db column to the object.
 191           */
 192          $this->signup_id      = $this->id;
 193          $this->domain         = $signup->domain;
 194          $this->path           = $signup->path;
 195          $this->title          = $signup->title;
 196          $this->user_login     = $signup->user_login;
 197          $this->user_email     = $signup->user_email;
 198          $this->registered     = $signup->registered;
 199          $this->activated      = $signup->activated;
 200          $this->active         = (bool) $signup->active;
 201          $this->activation_key = $signup->activation_key;
 202          $this->meta           = maybe_unserialize( $signup->meta );
 203  
 204          // Add richness.
 205          $this->avatar    = get_avatar( $signup->user_email, 32 );
 206          $this->user_name = ! empty( $this->meta['field_1'] ) ? wp_unslash( $this->meta['field_1'] ) : '';
 207  
 208          // When was the activation email sent?
 209          if ( isset( $this->meta['sent_date'] ) && '0000-00-00 00:00:00' !== $this->meta['sent_date'] ) {
 210              $this->date_sent = $this->meta['sent_date'];
 211  
 212              // Sent date defaults to date of registration.
 213          } else {
 214              $this->date_sent = $signup->registered;
 215          }
 216  
 217          /**
 218           * Calculate a diff between now & last time
 219           * an activation link has been resent.
 220           */
 221          $sent_at = mysql2date( 'U', $this->date_sent );
 222          $now     = current_time( 'timestamp', true );
 223          $diff    = $now - $sent_at;
 224  
 225          /**
 226           * Set a boolean to track whether an activation link
 227           * was sent in the last day.
 228           */
 229          $this->recently_sent = ( $diff < 1 * DAY_IN_SECONDS );
 230  
 231          // How many times has the activation email been sent?
 232          if ( isset( $this->meta['count_sent'] ) ) {
 233              $this->count_sent = absint( $this->meta['count_sent'] );
 234          } else {
 235              $this->count_sent = 0;
 236          }
 237      }
 238  
 239      /** Static Methods *******************************************************/
 240  
 241      /**
 242       * Fetch signups based on parameters.
 243       *
 244       * @since 2.0.0
 245       * @since 6.0.0 Added a list of allowed orderby parameters.
 246       *
 247       * @param array $args {
 248       *     The argument to retrieve desired signups.
 249       *     @type int         $offset         Offset amount. Default 0.
 250       *     @type int         $number         How many to fetch. Pass -1 to fetch all. Default 1.
 251       *     @type bool|string $usersearch     Whether or not to search for a username. Default false.
 252       *     @type string      $orderby        Order By parameter. Possible values are `signup_id`, `login`, `email`,
 253       *                                       `registered`, `activated`. Default `signup_id`.
 254       *     @type string      $order          Order direction. Default 'DESC'.
 255       *     @type bool        $include        Whether or not to include more specific query params.
 256       *     @type string      $activation_key Activation key to search for. If specified, all other
 257       *                                       parameters will be ignored.
 258       *     @type int|bool    $active         Pass 0 for inactive signups, 1 for active signups,
 259       *                                       and `false` to ignore.
 260       *     @type string      $user_login     Specific user login to return.
 261       *     @type string      $fields         Which fields to return. Specify 'ids' to fetch a list of signups IDs.
 262       *                                       Default: 'all' (return BP_Signup objects).
 263       * }
 264       * @return array {
 265       *     @type array $signups Located signups. (IDs only if `fields` is set to `ids`.)
 266       *     @type int   $total   Total number of signups matching params.
 267       * }
 268       */
 269  	public static function get( $args = array() ) {
 270          global $wpdb;
 271  
 272          $bp = buddypress();
 273          $r  = bp_parse_args(
 274              $args,
 275              array(
 276                  'offset'         => 0,
 277                  'number'         => 1,
 278                  'usersearch'     => false,
 279                  'orderby'        => 'signup_id',
 280                  'order'          => 'DESC',
 281                  'include'        => false,
 282                  'activation_key' => '',
 283                  'active'         => 0,
 284                  'user_email'     => '',
 285                  'user_login'     => '',
 286                  'fields'         => 'all',
 287              ),
 288              'bp_core_signups_get_args'
 289          );
 290  
 291          // Make sure the orderby clause is allowed.
 292          if ( ! in_array( $r['orderby'], array( 'login', 'email', 'registered', 'activated' ), true ) ) {
 293              $r['orderby'] = 'signup_id';
 294          }
 295  
 296          if ( 'login' === $r['orderby'] || 'email' === $r['orderby'] ) {
 297              $r['orderby'] = 'user_' . $r['orderby'];
 298          }
 299  
 300          $r['orderby'] = sanitize_title( $r['orderby'] );
 301  
 302          $sql = array(
 303              'select'     => "SELECT DISTINCT signup_id",
 304              'from'       => "{$bp->members->table_name_signups}",
 305              'where'      => array(),
 306              'orderby'    => '',
 307              'limit'      => '',
 308          );
 309  
 310          // Activation key trumps other parameters because it should be unique.
 311          if ( ! empty( $r['activation_key'] ) ) {
 312              $sql['where'][] = $wpdb->prepare( "activation_key = %s", $r['activation_key'] );
 313  
 314              // `Include` finds signups by ID.
 315          } else if ( ! empty( $r['include'] ) ) {
 316  
 317              $in             = implode( ',', wp_parse_id_list( $r['include'] ) );
 318              $sql['where'][] = "signup_id IN ({$in})";
 319  
 320              /**
 321               * Finally, the general case where a variety of parameters
 322               * can be used in combination to find signups.
 323               */
 324          } else {
 325              // Active.
 326              if ( false !== $r['active'] ) {
 327                  $sql['where'][] = $wpdb->prepare( "active = %d", absint( $r['active'] ) );
 328              }
 329  
 330              // Search terms.
 331              if ( ! empty( $r['usersearch'] ) ) {
 332                  $search_terms_like = '%' . bp_esc_like( $r['usersearch'] ) . '%';
 333                  $sql['where'][]    = $wpdb->prepare( "( user_login LIKE %s OR user_email LIKE %s OR meta LIKE %s )", $search_terms_like, $search_terms_like, $search_terms_like );
 334              }
 335  
 336              // User email.
 337              if ( ! empty( $r['user_email'] ) ) {
 338                  $sql['where'][] = $wpdb->prepare( "user_email = %s", $r['user_email'] );
 339              }
 340  
 341              // User login.
 342              if ( ! empty( $r['user_login'] ) ) {
 343                  $sql['where'][] = $wpdb->prepare( "user_login = %s", $r['user_login'] );
 344              }
 345  
 346              $order            = bp_esc_sql_order( $r['order'] );
 347              $sql['orderby'] = "ORDER BY {$r['orderby']} {$order}";
 348  
 349              $number = intval( $r['number'] );
 350              if ( -1 !== $number ) {
 351                  $sql['limit'] = $wpdb->prepare( "LIMIT %d, %d", absint( $r['offset'] ), $number );
 352              }
 353          }
 354  
 355          // Implode WHERE clauses.
 356          $sql['where'] = 'WHERE ' . implode( ' AND ', $sql['where'] );
 357  
 358          $paged_signups_sql = "{$sql['select']} FROM {$sql['from']} {$sql['where']} {$sql['orderby']} {$sql['limit']}";
 359  
 360          /**
 361           * Filters the Signups paged query.
 362           *
 363           * @since 2.0.0
 364           *
 365           * @param string $value SQL statement.
 366           * @param array  $sql   Array of SQL statement parts.
 367           * @param array  $args  Array of original arguments for get() method.
 368           * @param array  $r     Array of parsed arguments for get() method.
 369           */
 370          $paged_signups_sql = apply_filters( 'bp_members_signups_paged_query', $paged_signups_sql, $sql, $args, $r );
 371  
 372          $cached = bp_core_get_incremented_cache( $paged_signups_sql, 'bp_signups' );
 373          if ( false === $cached ) {
 374              $paged_signup_ids = $wpdb->get_col( $paged_signups_sql );
 375              bp_core_set_incremented_cache( $paged_signups_sql, 'bp_signups', $paged_signup_ids );
 376          } else {
 377              $paged_signup_ids = $cached;
 378          }
 379  
 380          // We only want the IDs.
 381          if ( 'ids' === $r['fields'] ) {
 382              $paged_signups = array_map( 'intval', $paged_signup_ids );
 383  
 384          } else {
 385              $uncached_signup_ids = bp_get_non_cached_ids( $paged_signup_ids, 'bp_signups' );
 386              if ( $uncached_signup_ids ) {
 387                  $signup_ids_sql      = implode( ',', array_map( 'intval', $uncached_signup_ids ) );
 388                  $signup_data_objects = $wpdb->get_results( "SELECT * FROM {$bp->members->table_name_signups} WHERE signup_id IN ({$signup_ids_sql})" );
 389                  foreach ( $signup_data_objects as $signup_data_object ) {
 390                      wp_cache_set( $signup_data_object->signup_id, $signup_data_object, 'bp_signups' );
 391                  }
 392              }
 393  
 394              $paged_signups = array();
 395              foreach ( $paged_signup_ids as $paged_signup_id ) {
 396                  $paged_signups[] = new BP_Signup( $paged_signup_id );
 397              }
 398          }
 399  
 400          // Find the total number of signups in the results set.
 401          $total_signups_sql = "SELECT COUNT(DISTINCT signup_id) FROM {$sql['from']} {$sql['where']}";
 402  
 403          /**
 404           * Filters the Signups count query.
 405           *
 406           * @since 2.0.0
 407           *
 408           * @param string $value SQL statement.
 409           * @param array  $sql   Array of SQL statement parts.
 410           * @param array  $args  Array of original arguments for get() method.
 411           * @param array  $r     Array of parsed arguments for get() method.
 412           */
 413          $total_signups_sql = apply_filters( 'bp_members_signups_count_query', $total_signups_sql, $sql, $args, $r );
 414  
 415          $cached = bp_core_get_incremented_cache( $total_signups_sql, 'bp_signups' );
 416          if ( false === $cached ) {
 417              $total_signups = (int) $wpdb->get_var( $total_signups_sql );
 418              bp_core_set_incremented_cache( $total_signups_sql, 'bp_signups', $total_signups );
 419          } else {
 420              $total_signups = (int) $cached;
 421          }
 422  
 423          return array( 'signups' => $paged_signups, 'total' => $total_signups );
 424      }
 425  
 426      /**
 427       * Add a signup.
 428       *
 429       * @since 2.0.0
 430       *
 431       * @param array $args {
 432       *     Array of arguments for signup addition.
 433       *     @type string     $domain         New user's domain.
 434       *     @type string     $path           New user's path.
 435       *     @type string     $title          New user's title.
 436       *     @type string     $user_login     New user's user_login.
 437       *     @type string     $user_email     New user's email address.
 438       *     @type int|string $registered     Time the user registered.
 439       *     @type string     $activation_key New user's activation key.
 440       *     @type string     $meta           New user's user meta.
 441       * }
 442       * @return int|bool ID of newly created signup on success, false on failure.
 443       */
 444  	public static function add( $args = array() ) {
 445          global $wpdb;
 446  
 447          $r = bp_parse_args(
 448              $args,
 449              array(
 450                  'domain'         => '',
 451                  'path'           => '',
 452                  'title'          => '',
 453                  'user_login'     => '',
 454                  'user_email'     => '',
 455                  'registered'     => current_time( 'mysql', true ),
 456                  'activation_key' => '',
 457                  'meta'           => array(),
 458              ),
 459              'bp_core_signups_add_args'
 460          );
 461  
 462          // Ensure that sent_date and count_sent are set in meta.
 463          if ( ! isset( $r['meta']['sent_date'] ) ) {
 464              $r['meta']['sent_date'] = '0000-00-00 00:00:00';
 465          }
 466          if ( ! isset( $r['meta']['count_sent'] ) ) {
 467              $r['meta']['count_sent'] = 0;
 468          }
 469  
 470          $r['meta'] = maybe_serialize( $r['meta'] );
 471  
 472          $inserted = $wpdb->insert(
 473              buddypress()->members->table_name_signups,
 474              $r,
 475              array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )
 476          );
 477  
 478          if ( $inserted ) {
 479              $retval = $wpdb->insert_id;
 480          } else {
 481              $retval = false;
 482          }
 483  
 484          /**
 485           * Fires after adding a new BP_Signup.
 486           *
 487           * @since 10.0.0
 488           *
 489           * @param int|bool $retval ID of the BP_Signup just added.
 490           * @param array    $r      Array of parsed arguments for add() method.
 491           * @param array    $args   Array of original arguments for add() method.
 492           */
 493          do_action( 'bp_core_signups_after_add', $retval, $r, $args );
 494  
 495          /**
 496           * Filters the result of a signup addition.
 497           *
 498           * @since 2.0.0
 499           *
 500           * @param int|bool $retval Newly added signup ID on success, false on failure.
 501           */
 502          return apply_filters( 'bp_core_signups_add', $retval );
 503      }
 504  
 505      /**
 506       * Create a WP user at signup.
 507       *
 508       * Since BP 2.0, non-multisite configurations have stored signups in
 509       * the same way as Multisite configs traditionally have: in the
 510       * wp_signups table. However, because some plugins may be looking
 511       * directly in the wp_users table for non-activated signups, we
 512       * mirror signups there by creating "phantom" users, mimicking WP's
 513       * default behavior.
 514       *
 515       * @since 2.0.0
 516       *
 517       * @param string $user_login    User login string.
 518       * @param string $user_password User password.
 519       * @param string $user_email    User email address.
 520       * @param array  $usermeta      Metadata associated with the signup.
 521       * @return int User id.
 522       */
 523  	public static function add_backcompat( $user_login = '', $user_password = '', $user_email = '', $usermeta = array() ) {
 524          global $wpdb;
 525  
 526          $user_id = wp_insert_user(
 527              array(
 528                  'user_login'   => $user_login,
 529                  'user_pass'    => $user_password,
 530                  'display_name' => sanitize_title( $user_login ),
 531                  'user_email'   => $user_email
 532              )
 533          );
 534  
 535          if ( is_wp_error( $user_id ) || empty( $user_id ) ) {
 536              return $user_id;
 537          }
 538  
 539          // Update the user status to '2', ie "not activated"
 540          // (0 = active, 1 = spam, 2 = not active).
 541          $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_status = 2 WHERE ID = %d", $user_id ) );
 542  
 543          // WordPress creates these options automatically on
 544          // wp_insert_user(), but we delete them so that inactive
 545          // signups don't appear in various user counts.
 546          delete_user_option( $user_id, 'capabilities' );
 547          delete_user_option( $user_id, 'user_level'   );
 548  
 549          // Set any profile data.
 550          if ( bp_is_active( 'xprofile' ) ) {
 551              if ( ! empty( $usermeta['profile_field_ids'] ) ) {
 552                  $profile_field_ids = explode( ',', $usermeta['profile_field_ids'] );
 553  
 554                  foreach ( (array) $profile_field_ids as $field_id ) {
 555                      if ( empty( $usermeta["field_{$field_id}"] ) ) {
 556                          continue;
 557                      }
 558  
 559                      $current_field = $usermeta["field_{$field_id}"];
 560                      xprofile_set_field_data( $field_id, $user_id, $current_field );
 561  
 562                      /*
 563                       * Save the visibility level.
 564                       *
 565                       * Use the field's default visibility if not present, and 'public' if a
 566                       * default visibility is not defined.
 567                       */
 568                      $key = "field_{$field_id}_visibility";
 569                      if ( isset( $usermeta[ $key ] ) ) {
 570                          $visibility_level = $usermeta[ $key ];
 571                      } else {
 572                          $vfield           = xprofile_get_field( $field_id, null, false );
 573                          $visibility_level = isset( $vfield->default_visibility ) ? $vfield->default_visibility : 'public';
 574                      }
 575                      xprofile_set_field_visibility_level( $field_id, $user_id, $visibility_level );
 576                  }
 577              }
 578          }
 579  
 580          /**
 581           * Fires after adding a new WP User (backcompat).
 582           *
 583           * @since 10.0.0
 584           *
 585           * @param int $user_id ID of the WP_User just added.
 586           */
 587          do_action( 'bp_core_signups_after_add_backcompat', $user_id );
 588  
 589          /**
 590           * Filters the user ID for the backcompat functionality.
 591           *
 592           * @since 2.0.0
 593           *
 594           * @param int $user_id User ID being registered.
 595           */
 596          return apply_filters( 'bp_core_signups_add_backcompat', $user_id );
 597      }
 598  
 599      /**
 600       * Check a user status (from wp_users) on a non-multisite config.
 601       *
 602       * @since 2.0.0
 603       *
 604       * @param int $user_id ID of the user being checked.
 605       * @return int|bool The status if found, otherwise false.
 606       */
 607  	public static function check_user_status( $user_id = 0 ) {
 608          global $wpdb;
 609  
 610          if ( empty( $user_id ) ) {
 611              return false;
 612          }
 613  
 614          $user        = get_user_by( 'id', $user_id );
 615          $user_status = $user->user_status;
 616  
 617          /**
 618           * Filters the user status of a provided user ID.
 619           *
 620           * @since 2.0.0
 621           *
 622           * @param int $value User status of the provided user ID.
 623           */
 624          return apply_filters( 'bp_core_signups_check_user_status', intval( $user_status ) );
 625      }
 626  
 627      /**
 628       * Activate a signup.
 629       *
 630       * @since 2.0.0
 631       *
 632       * @param string $key Activation key.
 633       * @return bool True on success, false on failure.
 634       */
 635  	public static function validate( $key = '' ) {
 636          global $wpdb;
 637  
 638          if ( empty( $key ) ) {
 639              return;
 640          }
 641  
 642          $activated = $wpdb->update(
 643              // Signups table.
 644              buddypress()->members->table_name_signups,
 645              array(
 646                  'active' => 1,
 647                  'activated' => current_time( 'mysql', true ),
 648              ),
 649              array(
 650                  'activation_key' => $key,
 651              ),
 652              // Data sanitization format.
 653              array(
 654                  '%d',
 655                  '%s',
 656              ),
 657              // WHERE sanitization format.
 658              array(
 659                  '%s',
 660              )
 661          );
 662  
 663          /**
 664           * Filters the status of the activated user.
 665           *
 666           * @since 2.0.0
 667           *
 668           * @param bool $activated Whether or not the activation was successful.
 669           */
 670          return apply_filters( 'bp_core_signups_validate', $activated );
 671      }
 672  
 673      /**
 674       * How many inactive signups do we have?
 675       *
 676       * @since 2.0.0
 677       *
 678       * @return int The number of signups.
 679       */
 680  	public static function count_signups() {
 681          $all_signups   = self::get(
 682              array(
 683                  'fields' => 'ids',
 684              )
 685          );
 686          $count_signups = $all_signups['total'];
 687  
 688          /**
 689           * Filters the total inactive signups.
 690           *
 691           * @since 2.0.0
 692           *
 693           * @param int $count_signups How many total signups there are.
 694           */
 695          return apply_filters( 'bp_core_signups_count', (int) $count_signups );
 696      }
 697  
 698      /**
 699       * Update the meta for a signup.
 700       *
 701       * This is the way we use to "trace" the last date an activation
 702       * email was sent and how many times activation was sent.
 703       *
 704       * @since 2.0.0
 705       *
 706       * @param array $args {
 707       *     Array of arguments for the signup update.
 708       *     @type int $signup_id User signup ID.
 709       *     @type array $meta Meta to update.
 710       * }
 711       * @return int The signup id.
 712       */
 713  	public static function update( $args = array() ) {
 714          global $wpdb;
 715  
 716          $r = bp_parse_args(
 717              $args,
 718              array(
 719                  'signup_id'  => 0,
 720                  'meta'       => array(),
 721              ),
 722              'bp_core_signups_update_args'
 723          );
 724  
 725          if ( empty( $r['signup_id'] ) || empty( $r['meta'] ) ) {
 726              return false;
 727          }
 728  
 729          $signup_id = absint( $r['signup_id'] );
 730  
 731          // Figure out which meta keys should be updated.
 732          $signup       = new BP_Signup( $signup_id );
 733          $blended_meta = bp_parse_args(
 734              $r['meta'],
 735              $signup->meta
 736          );
 737  
 738          $wpdb->update(
 739              // Signups table.
 740              buddypress()->members->table_name_signups,
 741              // Data to update.
 742              array(
 743                  'meta' => serialize( $blended_meta ),
 744              ),
 745              // WHERE.
 746              array(
 747                  'signup_id' => $signup_id,
 748              ),
 749              // Data sanitization format.
 750              array(
 751                  '%s',
 752              ),
 753              // WHERE sanitization format.
 754              array(
 755                  '%d',
 756              )
 757          );
 758  
 759          /**
 760           * Fires after updating the meta of a new BP_Signup.
 761           *
 762           * @since 10.0.0
 763           *
 764           * @param int|bool $signup_id    ID of the BP_Signup updated.
 765           * @param array    $r            Array of parsed arguments to update() method.
 766           * @param array    $args         Array of original arguments to update() method.
 767           * @param array    $blended_meta The complete set of meta to save.
 768           */
 769          do_action( 'bp_core_signups_after_update_meta', $signup_id, $r, $args, $blended_meta );
 770  
 771          /**
 772           * Filters the signup ID which received a meta update.
 773           *
 774           * @since 2.0.0
 775           *
 776           * @param int $value The signup ID.
 777           */
 778          return apply_filters( 'bp_core_signups_update', $r['signup_id'] );
 779      }
 780  
 781      /**
 782       * Resend an activation email.
 783       *
 784       * @since 2.0.0
 785       *
 786       * @param array $signup_ids Single ID or list of IDs to resend.
 787       * @return array
 788       */
 789  	public static function resend( $signup_ids = array() ) {
 790          if ( empty( $signup_ids ) || ! is_array( $signup_ids ) ) {
 791              return false;
 792          }
 793  
 794          $to_resend = self::get(
 795              array(
 796                  'include' => $signup_ids,
 797              )
 798          );
 799  
 800          if ( ! $signups = $to_resend['signups'] ) {
 801              return false;
 802          }
 803  
 804          $result = array();
 805  
 806          /**
 807           * Fires before activation emails are resent.
 808           *
 809           * @since 2.0.0
 810           *
 811           * @param array $signup_ids Array of IDs to resend activation emails to.
 812           */
 813          do_action( 'bp_core_signup_before_resend', $signup_ids );
 814  
 815          foreach ( $signups as $signup ) {
 816  
 817              $meta = array(
 818                  'sent_date'  => current_time( 'mysql', true ),
 819                  'count_sent' => $signup->count_sent + 1
 820              );
 821  
 822              // Send activation email.
 823              if ( is_multisite() ) {
 824                  wpmu_signup_user_notification( $signup->user_login, $signup->user_email, $signup->activation_key, serialize( $meta ) );
 825              } else {
 826  
 827                  // Check user status before sending email.
 828                  $user_id = email_exists( $signup->user_email );
 829  
 830                  if ( ! empty( $user_id ) && 2 != self::check_user_status( $user_id ) ) {
 831  
 832                      // Status is not 2, so user's account has been activated.
 833                      $result['errors'][ $signup->signup_id ] = array( $signup->user_login, esc_html__( 'the sign-up has already been activated.', 'buddypress' ) );
 834  
 835                      // Repair signups table.
 836                      self::validate( $signup->activation_key );
 837  
 838                      continue;
 839  
 840                  // Send the validation email.
 841                  } else {
 842                      $salutation = $signup->user_login;
 843                      if ( bp_is_active( 'xprofile' ) && isset( $meta[ 'field_' . bp_xprofile_fullname_field_id() ] ) ) {
 844                          $salutation = $meta[ 'field_' . bp_xprofile_fullname_field_id() ];
 845                      }
 846  
 847                      bp_core_signup_send_validation_email( false, $signup->user_email, $signup->activation_key, $salutation );
 848                  }
 849              }
 850  
 851              // Update metas.
 852              $result['resent'][] = self::update(
 853                  array(
 854                      'signup_id' => $signup->signup_id,
 855                      'meta'      => $meta,
 856                  )
 857              );
 858          }
 859  
 860          /**
 861           * Fires after activation emails are resent.
 862           *
 863           * @since 2.0.0
 864           *
 865           * @param array $signup_ids Array of IDs to resend activation emails to.
 866           * @param array $result     Updated metadata related to activation emails.
 867           */
 868          do_action( 'bp_core_signup_after_resend', $signup_ids, $result );
 869  
 870          /**
 871           * Filters the result of the metadata for signup activation email resends.
 872           *
 873           * @since 2.0.0
 874           *
 875           * @param array $result Updated metadata related to activation emails.
 876           */
 877          return apply_filters( 'bp_core_signup_resend', $result );
 878      }
 879  
 880      /**
 881       * Activate a pending account.
 882       *
 883       * @since 2.0.0
 884       *
 885       * @param array $signup_ids Single ID or list of IDs to activate.
 886       * @return array
 887       */
 888  	public static function activate( $signup_ids = array() ) {
 889          if ( empty( $signup_ids ) || ! is_array( $signup_ids ) ) {
 890              return false;
 891          }
 892  
 893          $to_activate = self::get(
 894              array(
 895                  'include' => $signup_ids,
 896              )
 897          );
 898  
 899          if ( ! $signups = $to_activate['signups'] ) {
 900              return false;
 901          }
 902  
 903          $result = array();
 904  
 905          /**
 906           * Fires before activation of user accounts.
 907           *
 908           * @since 2.0.0
 909           *
 910           * @param array $signup_ids Array of IDs to activate.
 911           */
 912          do_action( 'bp_core_signup_before_activate', $signup_ids );
 913  
 914          foreach ( $signups as $signup ) {
 915  
 916              $user = bp_core_activate_signup( $signup->activation_key );
 917  
 918              if ( ! empty( $user->errors ) ) {
 919  
 920                  $user_id = username_exists( $signup->user_login );
 921  
 922                  if ( 2 !== self::check_user_status( $user_id ) ) {
 923                      $user_id = false;
 924                  }
 925  
 926                  if ( empty( $user_id ) ) {
 927  
 928                      // Status is not 2, so user's account has been activated.
 929                      $result['errors'][ $signup->signup_id ] = array( $signup->user_login, esc_html__( 'the sign-up has already been activated.', 'buddypress' ) );
 930  
 931                      // Repair signups table.
 932                      self::validate( $signup->activation_key );
 933  
 934                  // We have a user id, account is not active, let's delete it.
 935                  } else {
 936                      $result['errors'][ $signup->signup_id ] = array( $signup->user_login, $user->get_error_message() );
 937                  }
 938  
 939              } else {
 940                  $result['activated'][] = $user;
 941              }
 942          }
 943  
 944          /**
 945           * Fires after activation of user accounts.
 946           *
 947           * @since 2.0.0
 948           *
 949           * @param array $signup_ids Array of IDs activated activate.
 950           * @param array $result     Array of data for activated accounts.
 951           */
 952          do_action( 'bp_core_signup_after_activate', $signup_ids, $result );
 953  
 954          /**
 955           * Filters the result of the metadata after user activation.
 956           *
 957           * @since 2.0.0
 958           *
 959           * @param array $result Updated metadata related to user activation.
 960           */
 961          return apply_filters( 'bp_core_signup_activate', $result );
 962      }
 963  
 964      /**
 965       * Delete a pending account.
 966       *
 967       * @since 2.0.0
 968       *
 969       * @param array $signup_ids Single ID or list of IDs to delete.
 970       * @return array
 971       */
 972  	public static function delete( $signup_ids = array() ) {
 973          global $wpdb;
 974  
 975          if ( empty( $signup_ids ) || ! is_array( $signup_ids ) ) {
 976              return false;
 977          }
 978  
 979          $to_delete = self::get(
 980              array(
 981                  'include' => $signup_ids,
 982              )
 983          );
 984  
 985          if ( ! $signups = $to_delete['signups'] ) {
 986              return false;
 987          }
 988  
 989          $result = array();
 990  
 991          /**
 992           * Fires before deletion of pending accounts.
 993           *
 994           * @since 2.0.0
 995           *
 996           * @param array $signup_ids Array of pending IDs to delete.
 997           */
 998          do_action( 'bp_core_signup_before_delete', $signup_ids );
 999  
1000          foreach ( $signups as $signup ) {
1001              $user_id = username_exists( $signup->user_login );
1002  
1003              if ( ! empty( $user_id ) && $signup->activation_key === bp_get_user_meta( $user_id, 'activation_key', true ) ) {
1004  
1005                  if ( 2 != self::check_user_status( $user_id ) ) {
1006  
1007                      // Status is not 2, so user's account has been activated.
1008                      $result['errors'][ $signup->signup_id ] = array( $signup->user_login, esc_html__( 'the sign-up has already been activated.', 'buddypress' ) );
1009  
1010                      // Repair signups table.
1011                      self::validate( $signup->activation_key );
1012  
1013                  // We have a user id, account is not active, let's delete it.
1014                  } else {
1015                      bp_core_delete_account( $user_id );
1016                  }
1017              }
1018  
1019              if ( empty( $result['errors'][ $signup->signup_id ] ) ) {
1020                  $wpdb->delete(
1021                      // Signups table.
1022                      buddypress()->members->table_name_signups,
1023                      // Where.
1024                      array( 'signup_id' => $signup->signup_id, ),
1025                      // WHERE sanitization format.
1026                      array( '%d', )
1027                  );
1028  
1029                  $result['deleted'][] = $signup->signup_id;
1030              }
1031          }
1032  
1033          /**
1034           * Fires after deletion of pending accounts.
1035           *
1036           * @since 2.0.0
1037           *
1038           * @param array $signup_ids Array of pending IDs to delete.
1039           * @param array $result     Array of data for deleted accounts.
1040           */
1041          do_action( 'bp_core_signup_after_delete', $signup_ids, $result );
1042  
1043          /**
1044           * Filters the result of the metadata for deleted pending accounts.
1045           *
1046           * @since 2.0.0
1047           *
1048           * @param array $result Updated metadata related to deleted pending accounts.
1049           */
1050          return apply_filters( 'bp_core_signup_delete', $result );
1051      }
1052  }


Generated: Tue Sep 21 01:01:37 2021 Cross-referenced by PHPXref 0.7.1