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


Generated: Thu Nov 21 01:00:57 2024 Cross-referenced by PHPXref 0.7.1