Changeset 9485 for trunk/src/bp-members/bp-members-classes.php
- Timestamp:
- 02/15/2015 12:48:56 AM (11 years ago)
- File:
-
- 1 edited
-
trunk/src/bp-members/bp-members-classes.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-members/bp-members-classes.php
r9308 r9485 1 1 <?php 2 3 2 /** 4 * Signups Management class.3 * BuddyPress Members Classes 5 4 * 6 5 * @package BuddyPress 7 * @subpackage coreClasses 8 * 9 * @since BuddyPress (2.0.0) 6 * @subpackage MembersClasses 10 7 */ 11 class BP_Signup {12 8 13 /** 14 * ID of the signup which the object relates to. 15 * 16 * @var integer 17 */ 18 public $id; 9 // Exit if accessed directly 10 defined( 'ABSPATH' ) || exit; 19 11 20 /** 21 * The URL to the full size of the avatar for the user. 22 * 23 * @var string 24 */ 25 public $avatar; 26 27 /** 28 * The username for the user. 29 * 30 * @var string 31 */ 32 public $user_login; 33 34 /** 35 * The email for the user. 36 * 37 * @var string 38 */ 39 public $user_email; 40 41 /** 42 * The full name of the user. 43 * 44 * @var string 45 */ 46 public $user_name; 47 48 /** 49 * Metadata associated with the signup. 50 * 51 * @var array 52 */ 53 public $meta; 54 55 /** 56 * The registered date for the user. 57 * 58 * @var string 59 */ 60 public $registered; 61 62 /** 63 * The activation key for the user. 64 * 65 * @var string 66 */ 67 public $activation_key; 68 69 70 /** Public Methods *******************************************************/ 71 72 /** 73 * Class constructor. 74 * 75 * @since BuddyPress (2.0.0) 76 * 77 * @param integer $signup_id The ID for the signup being queried. 78 */ 79 public function __construct( $signup_id = 0 ) { 80 if ( !empty( $signup_id ) ) { 81 $this->id = $signup_id; 82 $this->populate(); 83 } 84 } 85 86 /** 87 * Populate the instantiated class with data based on the signup_id provided. 88 * 89 * @since BuddyPress (2.0.0) 90 */ 91 public function populate() { 92 global $wpdb; 93 94 $signups_table = buddypress()->members->table_name_signups; 95 $signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$signups_table} WHERE signup_id = %d AND active = 0", $this->id ) ); 96 97 $this->avatar = get_avatar( $signup->user_email, 32 ); 98 $this->user_login = $signup->user_login; 99 $this->user_email = $signup->user_email; 100 $this->meta = maybe_unserialize( $signup->meta ); 101 $this->user_name = ! empty( $this->meta['field_1'] ) ? wp_unslash( $this->meta['field_1'] ) : ''; 102 $this->registered = $signup->registered; 103 $this->activation_key = $signup->activation_key; 104 } 105 106 /** Static Methods *******************************************************/ 107 108 /** 109 * Fetch signups based on parameters. 110 * 111 * @since BuddyPress (2.0.0) 112 * 113 * @param array $args the argument to retrieve desired signups 114 * @return array { 115 * @type array $signups Located signups. 116 * @type int $total Total number of signups matching params. 117 * } 118 */ 119 public static function get( $args = array() ) { 120 global $wpdb; 121 122 $r = bp_parse_args( $args, 123 array( 124 'offset' => 0, 125 'number' => 1, 126 'usersearch' => false, 127 'orderby' => 'signup_id', 128 'order' => 'DESC', 129 'include' => false, 130 'activation_key' => '', 131 'user_login' => '', 132 ), 133 'bp_core_signups_get_args' 134 ); 135 136 // @todo whitelist sanitization 137 if ( $r['orderby'] !== 'signup_id' ) { 138 $r['orderby'] = 'user_' . $r['orderby']; 139 } 140 141 $r['orderby'] = sanitize_title( $r['orderby'] ); 142 143 $sql = array(); 144 $signups_table = buddypress()->members->table_name_signups; 145 $sql['select'] = "SELECT * FROM {$signups_table}"; 146 $sql['where'] = array(); 147 $sql['where'][] = "active = 0"; 148 149 if ( empty( $r['include'] ) ) { 150 151 // Search terms 152 if ( ! empty( $r['usersearch'] ) ) { 153 $search_terms_like = '%' . bp_esc_like( $r['usersearch'] ) . '%'; 154 $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 ); 155 } 156 157 // Activation key 158 if ( ! empty( $r['activation_key'] ) ) { 159 $sql['where'][] = $wpdb->prepare( "activation_key = %s", $r['activation_key'] ); 160 } 161 162 // User login 163 if ( ! empty( $r['user_login'] ) ) { 164 $sql['where'][] = $wpdb->prepare( "user_login = %s", $r['user_login'] ); 165 } 166 167 $sql['orderby'] = "ORDER BY {$r['orderby']}"; 168 $sql['order'] = bp_esc_sql_order( $r['order'] ); 169 $sql['limit'] = $wpdb->prepare( "LIMIT %d, %d", $r['offset'], $r['number'] ); 170 } else { 171 $in = implode( ',', wp_parse_id_list( $r['include'] ) ); 172 $sql['in'] = "AND signup_id IN ({$in})"; 173 } 174 175 // Implode WHERE clauses 176 $sql['where'] = 'WHERE ' . implode( ' AND ', $sql['where'] ); 177 178 /** 179 * Filters the Signups paged query. 180 * 181 * @since BuddyPress (2.0.0) 182 * 183 * @param string $value SQL statement. 184 * @param array $sql Array of SQL statement parts. 185 * @param array $args Array of original arguments for get() method. 186 * @param array $r Array of parsed arguments for get() method. 187 */ 188 $paged_signups = $wpdb->get_results( apply_filters( 'bp_members_signups_paged_query', join( ' ', $sql ), $sql, $args, $r ) ); 189 190 if ( empty( $paged_signups ) ) { 191 return array( 'signups' => false, 'total' => false ); 192 } 193 194 // Used to calculate a diff between now & last 195 // time an activation link has been resent 196 $now = current_time( 'timestamp', true ); 197 198 foreach ( (array) $paged_signups as $key => $signup ) { 199 200 $signup->id = intval( $signup->signup_id ); 201 202 $signup->meta = ! empty( $signup->meta ) ? maybe_unserialize( $signup->meta ) : false; 203 204 $signup->user_name = ''; 205 if ( ! empty( $signup->meta['field_1'] ) ) { 206 $signup->user_name = wp_unslash( $signup->meta['field_1'] ); 207 } 208 209 // Sent date defaults to date of registration 210 if ( ! empty( $signup->meta['sent_date'] ) ) { 211 $signup->date_sent = $signup->meta['sent_date']; 212 } else { 213 $signup->date_sent = $signup->registered; 214 } 215 216 $sent_at = mysql2date('U', $signup->date_sent ); 217 $diff = $now - $sent_at; 218 219 /** 220 * add a boolean in case the last time an activation link 221 * has been sent happened less than a day ago 222 */ 223 if ( $diff < 1 * DAY_IN_SECONDS ) { 224 $signup->recently_sent = true; 225 } 226 227 if ( ! empty( $signup->meta['count_sent'] ) ) { 228 $signup->count_sent = absint( $signup->meta['count_sent'] ); 229 } else { 230 $signup->count_sent = 1; 231 } 232 233 $paged_signups[ $key ] = $signup; 234 } 235 236 unset( $sql['limit'] ); 237 $sql['select'] = preg_replace( "/SELECT.*?FROM/", "SELECT COUNT(*) FROM", $sql['select'] ); 238 239 /** 240 * Filters the Signups count query. 241 * 242 * @since BuddyPress (2.0.0) 243 * 244 * @param string $value SQL statement. 245 * @param array $sql Array of SQL statement parts. 246 * @param array $args Array of original arguments for get() method. 247 * @param array $r Array of parsed arguments for get() method. 248 */ 249 $total_signups = $wpdb->get_var( apply_filters( 'bp_members_signups_count_query', join( ' ', $sql ), $sql, $args, $r ) ); 250 251 return array( 'signups' => $paged_signups, 'total' => $total_signups ); 252 } 253 254 /** 255 * Add a signup. 256 * 257 * @since BuddyPress (2.0.0) 258 * 259 * @param array $args 260 * @return int|bool ID of newly created signup on success, false on 261 * failure. 262 */ 263 public static function add( $args = array() ) { 264 global $wpdb; 265 266 $r = bp_parse_args( $args, 267 array( 268 'domain' => '', 269 'path' => '', 270 'title' => '', 271 'user_login' => '', 272 'user_email' => '', 273 'registered' => current_time( 'mysql', true ), 274 'activation_key' => '', 275 'meta' => '', 276 ), 277 'bp_core_signups_add_args' 278 ); 279 280 $r['meta'] = maybe_serialize( $r['meta'] ); 281 282 $inserted = $wpdb->insert( 283 buddypress()->members->table_name_signups, 284 $r, 285 array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) 286 ); 287 288 if ( $inserted ) { 289 $retval = $wpdb->insert_id; 290 } else { 291 $retval = false; 292 } 293 294 /** 295 * Filters the result of a signup addition. 296 * 297 * @since BuddyPress (2.0.0) 298 * 299 * @param int|bool $retval Newly added user ID on success, false on failure. 300 */ 301 return apply_filters( 'bp_core_signups_add', $retval ); 302 } 303 304 /** 305 * Create a WP user at signup. 306 * 307 * Since BP 2.0, non-multisite configurations have stored signups in 308 * the same way as Multisite configs traditionally have: in the 309 * wp_signups table. However, because some plugins may be looking 310 * directly in the wp_users table for non-activated signups, we 311 * mirror signups there by creating "phantom" users, mimicking WP's 312 * default behavior. 313 * 314 * @since BuddyPress (2.0.0) 315 * 316 * @param string $user_login User login string. 317 * @param string $user_password User password. 318 * @param string $user_email User email address. 319 * @param array $usermeta Metadata associated with the signup. 320 * @return int User id. 321 */ 322 public static function add_backcompat( $user_login = '', $user_password = '', $user_email = '', $usermeta = array() ) { 323 global $wpdb; 324 325 $user_id = wp_insert_user( array( 326 'user_login' => $user_login, 327 'user_pass' => $user_password, 328 'display_name' => sanitize_title( $user_login ), 329 'user_email' => $user_email 330 ) ); 331 332 if ( is_wp_error( $user_id ) || empty( $user_id ) ) { 333 return $user_id; 334 } 335 336 // Update the user status to '2', ie "not activated" 337 // (0 = active, 1 = spam, 2 = not active) 338 $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_status = 2 WHERE ID = %d", $user_id ) ); 339 340 // WordPress creates these options automatically on 341 // wp_insert_user(), but we delete them so that inactive 342 // signups don't appear in various user counts. 343 delete_user_option( $user_id, 'capabilities' ); 344 delete_user_option( $user_id, 'user_level' ); 345 346 // Set any profile data 347 if ( bp_is_active( 'xprofile' ) ) { 348 if ( ! empty( $usermeta['profile_field_ids'] ) ) { 349 $profile_field_ids = explode( ',', $usermeta['profile_field_ids'] ); 350 351 foreach ( (array) $profile_field_ids as $field_id ) { 352 if ( empty( $usermeta["field_{$field_id}"] ) ) { 353 continue; 354 } 355 356 $current_field = $usermeta["field_{$field_id}"]; 357 xprofile_set_field_data( $field_id, $user_id, $current_field ); 358 359 // Save the visibility level 360 $visibility_level = ! empty( $usermeta['field_' . $field_id . '_visibility'] ) ? $usermeta['field_' . $field_id . '_visibility'] : 'public'; 361 xprofile_set_field_visibility_level( $field_id, $user_id, $visibility_level ); 362 } 363 } 364 } 365 366 /** 367 * Filters the user ID for the backcompat functionality. 368 * 369 * @since BuddyPress (2.0.0) 370 * 371 * @param int $user_id User ID being registered. 372 */ 373 return apply_filters( 'bp_core_signups_add_backcompat', $user_id ); 374 } 375 376 /** 377 * Check a user status (from wp_users) on a non-multisite config. 378 * 379 * @since BuddyPress (2.0.0) 380 * 381 * @param int $user_id ID of the user being checked. 382 * @return int|bool The status if found, otherwise false. 383 */ 384 public static function check_user_status( $user_id = 0 ) { 385 global $wpdb; 386 387 if ( empty( $user_id ) ) { 388 return false; 389 } 390 391 $user_status = $wpdb->get_var( $wpdb->prepare( "SELECT user_status FROM {$wpdb->users} WHERE ID = %d", $user_id ) ); 392 393 /** 394 * Filters the user status of a provided user ID. 395 * 396 * @since BuddyPress (2.0.0) 397 * 398 * @param int $value User status of the provided user ID. 399 */ 400 return apply_filters( 'bp_core_signups_check_user_status', intval( $user_status ) ); 401 } 402 403 /** 404 * Activate a signup. 405 * 406 * @since BuddyPress (2.0.0) 407 * 408 * @param string $key Activation key. 409 * @return bool True on success, false on failure. 410 */ 411 public static function validate( $key = '' ) { 412 global $wpdb; 413 414 if ( empty( $key ) ) { 415 return; 416 } 417 418 $activated = $wpdb->update( 419 // Signups table 420 buddypress()->members->table_name_signups, 421 array( 422 'active' => 1, 423 'activated' => current_time( 'mysql', true ), 424 ), 425 array( 426 'activation_key' => $key, 427 ), 428 // Data sanitization format 429 array( 430 '%d', 431 '%s', 432 ), 433 // WHERE sanitization format 434 array( 435 '%s', 436 ) 437 ); 438 439 /** 440 * Filters the status of the activated user. 441 * 442 * @since BuddyPress (2.0.0) 443 * 444 * @param bool $activated Whether or not the activation was successful. 445 */ 446 return apply_filters( 'bp_core_signups_validate', $activated ); 447 } 448 449 /** 450 * How many inactive signups do we have? 451 * 452 * @since BuddyPress (2.0.0) 453 * 454 * @return int the number of signups 455 */ 456 public static function count_signups() { 457 global $wpdb; 458 459 $signups_table = buddypress()->members->table_name_signups; 460 $count_signups = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) AS total FROM {$signups_table} WHERE active = %d", 0 ) ); 461 462 /** 463 * Filters the total inactive signups. 464 * 465 * @since BuddyPress (2.0.0) 466 * 467 * @param int $count_signups How many total signups there are. 468 */ 469 return apply_filters( 'bp_core_signups_count', (int) $count_signups ); 470 } 471 472 /** 473 * Update the meta for a signup. 474 * 475 * This is the way we use to "trace" the last date an activation 476 * email was sent and how many times activation was sent. 477 * 478 * @since BuddyPress (2.0.0) 479 * 480 * @param array $args 481 * @return int the signup id 482 */ 483 public static function update( $args = array() ) { 484 global $wpdb; 485 486 $r = bp_parse_args( $args, 487 array( 488 'signup_id' => 0, 489 'meta' => array(), 490 ), 491 'bp_core_signups_update_args' 492 ); 493 494 if ( empty( $r['signup_id'] ) || empty( $r['meta'] ) ) { 495 return false; 496 } 497 498 $wpdb->update( 499 // Signups table 500 buddypress()->members->table_name_signups, 501 // Data to update 502 array( 503 'meta' => serialize( $r['meta'] ), 504 ), 505 // WHERE 506 array( 507 'signup_id' => $r['signup_id'], 508 ), 509 // Data sanitization format 510 array( 511 '%s', 512 ), 513 // WHERE sanitization format 514 array( 515 '%d', 516 ) 517 ); 518 519 /** 520 * Filters the signup ID which received a meta update. 521 * 522 * @since BuddyPress (2.0.0) 523 * 524 * @param int $value The signup ID. 525 */ 526 return apply_filters( 'bp_core_signups_update', $r['signup_id'] ); 527 } 528 529 /** 530 * Resend an activation email. 531 * 532 * @since BuddyPress (2.0.0) 533 * 534 * @param array $signup_ids Single ID or list of IDs to resend. 535 * @return array 536 */ 537 public static function resend( $signup_ids = array() ) { 538 if ( empty( $signup_ids ) || ! is_array( $signup_ids ) ) { 539 return false; 540 } 541 542 $to_resend = self::get( array( 543 'include' => $signup_ids, 544 ) ); 545 546 if ( ! $signups = $to_resend['signups'] ) { 547 return false; 548 } 549 550 $result = array(); 551 552 /** 553 * Fires before activation emails are resent. 554 * 555 * @since BuddyPress (2.0.0) 556 * 557 * @param array $signup_ids Array of IDs to resend activation emails to. 558 */ 559 do_action( 'bp_core_signup_before_resend', $signup_ids ); 560 561 foreach ( $signups as $signup ) { 562 563 $meta = $signup->meta; 564 $meta['sent_date'] = current_time( 'mysql', true ); 565 $meta['count_sent'] = $signup->count_sent + 1; 566 567 // Send activation email 568 if ( is_multisite() ) { 569 wpmu_signup_user_notification( $signup->user_login, $signup->user_email, $signup->activation_key, serialize( $meta ) ); 570 } else { 571 572 // Check user status before sending email 573 $user_id = email_exists( $signup->user_email ); 574 575 if ( ! empty( $user_id ) && 2 != self::check_user_status( $user_id ) ) { 576 577 // Status is not 2, so user's account has been activated 578 $result['errors'][ $signup->signup_id ] = array( $signup->user_login, esc_html__( 'the sign-up has already been activated.', 'buddypress' ) ); 579 580 // repair signups table 581 self::validate( $signup->activation_key ); 582 583 continue; 584 585 // Send the validation email 586 } else { 587 bp_core_signup_send_validation_email( false, $signup->user_email, $signup->activation_key ); 588 } 589 } 590 591 // Update metas 592 $result['resent'][] = self::update( array( 593 'signup_id' => $signup->signup_id, 594 'meta' => $meta, 595 ) ); 596 } 597 598 /** 599 * Fires after activation emails are resent. 600 * 601 * @since BuddyPress (2.0.0) 602 * 603 * @param array $signup_ids Array of IDs to resend activation emails to. 604 * @param array $result Updated metadata related to activation emails. 605 */ 606 do_action( 'bp_core_signup_after_resend', $signup_ids, $result ); 607 608 /** 609 * Filters the result of the metadata for signup activation email resends. 610 * 611 * @since BuddyPress (2.0.0) 612 * 613 * @param array $result Updated metadata related to activation emails. 614 */ 615 return apply_filters( 'bp_core_signup_resend', $result ); 616 } 617 618 /** 619 * Activate a pending account. 620 * 621 * @since BuddyPress (2.0.0) 622 * 623 * @param array $signup_ids Single ID or list of IDs to activate. 624 * @return array 625 */ 626 public static function activate( $signup_ids = array() ) { 627 if ( empty( $signup_ids ) || ! is_array( $signup_ids ) ) { 628 return false; 629 } 630 631 $to_activate = self::get( array( 632 'include' => $signup_ids, 633 ) ); 634 635 if ( ! $signups = $to_activate['signups'] ) { 636 return false; 637 } 638 639 $result = array(); 640 641 /** 642 * Fires before activation of user accounts. 643 * 644 * @since BuddyPress (2.0.0) 645 * 646 * @param array $signup_ids Array of IDs to activate. 647 */ 648 do_action( 'bp_core_signup_before_activate', $signup_ids ); 649 650 foreach ( $signups as $signup ) { 651 652 $user = bp_core_activate_signup( $signup->activation_key ); 653 654 if ( ! empty( $user->errors ) ) { 655 656 $user_id = username_exists( $signup->user_login ); 657 658 if ( 2 !== self::check_user_status( $user_id ) ) { 659 $user_id = false; 660 } 661 662 if ( empty( $user_id ) ) { 663 664 // Status is not 2, so user's account has been activated 665 $result['errors'][ $signup->signup_id ] = array( $signup->user_login, esc_html__( 'the sign-up has already been activated.', 'buddypress' ) ); 666 667 // repair signups table 668 self::validate( $signup->activation_key ); 669 670 // we have a user id, account is not active, let's delete it 671 } else { 672 $result['errors'][ $signup->signup_id ] = array( $signup->user_login, $user->get_error_message() ); 673 } 674 675 } else { 676 $result['activated'][] = $user; 677 } 678 } 679 680 /** 681 * Fires after activation of user accounts. 682 * 683 * @since BuddyPress (2.0.0) 684 * 685 * @param array $signup_ids Array of IDs activated activate. 686 * @param array $result Array of data for activated accounts. 687 */ 688 do_action( 'bp_core_signup_after_activate', $signup_ids, $result ); 689 690 /** 691 * Filters the result of the metadata after user activation. 692 * 693 * @since BuddyPress (2.0.0) 694 * 695 * @param array $result Updated metadata related to user activation. 696 */ 697 return apply_filters( 'bp_core_signup_activate', $result ); 698 } 699 700 /** 701 * Delete a pending account. 702 * 703 * @since BuddyPress (2.0.0) 704 * 705 * @param array $signup_ids Single ID or list of IDs to delete. 706 * @return array 707 */ 708 public static function delete( $signup_ids = array() ) { 709 global $wpdb; 710 711 if ( empty( $signup_ids ) || ! is_array( $signup_ids ) ) { 712 return false; 713 } 714 715 $to_delete = self::get( array( 716 'include' => $signup_ids, 717 ) ); 718 719 if ( ! $signups = $to_delete['signups'] ) { 720 return false; 721 } 722 723 $result = array(); 724 725 /** 726 * Fires before deletion of pending accounts. 727 * 728 * @since BuddyPress (2.0.0) 729 * 730 * @param array $signup_ids Array of pending IDs to delete. 731 */ 732 do_action( 'bp_core_signup_before_delete', $signup_ids ); 733 734 foreach ( $signups as $signup ) { 735 $user_id = username_exists( $signup->user_login ); 736 737 if ( ! empty( $user_id ) && $signup->activation_key == wp_hash( $user_id ) ) { 738 739 if ( 2 != self::check_user_status( $user_id ) ) { 740 741 // Status is not 2, so user's account has been activated 742 $result['errors'][ $signup->signup_id ] = array( $signup->user_login, esc_html__( 'the sign-up has already been activated.', 'buddypress' ) ); 743 744 // repair signups table 745 self::validate( $signup->activation_key ); 746 747 // we have a user id, account is not active, let's delete it 748 } else { 749 bp_core_delete_account( $user_id ); 750 } 751 } 752 753 if ( empty( $result['errors'][ $signup->signup_id ] ) ) { 754 $wpdb->delete( 755 // Signups table 756 buddypress()->members->table_name_signups, 757 // Where 758 array( 'signup_id' => $signup->signup_id, ), 759 // WHERE sanitization format 760 array( '%d', ) 761 ); 762 763 $result['deleted'][] = $signup->signup_id; 764 } 765 } 766 767 /** 768 * Fires after deletion of pending accounts. 769 * 770 * @since BuddyPress (2.0.0) 771 * 772 * @param array $signup_ids Array of pending IDs to delete. 773 * @param array $result Array of data for deleted accounts. 774 */ 775 do_action( 'bp_core_signup_after_delete', $signup_ids, $result ); 776 777 /** 778 * Filters the result of the metadata for deleted pending accounts. 779 * 780 * @since BuddyPress (2.0.0) 781 * 782 * @param array $result Updated metadata related to deleted pending accounts. 783 */ 784 return apply_filters( 'bp_core_signup_delete', $result ); 785 } 786 } 12 require __DIR__ . '/classes/class-bp-signup.php';
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)