Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
04/24/2014 12:22:29 AM (10 years ago)
Author:
boonebgorges
Message:

Improve the migration tool that converts old non-multisite signups to post-2.0 system

This refactoring involves a number of improvements:

  • Old signups are identified as those users with user_status=2, instead of those users with an 'activation_key' usermeta value. Use of the latter technique caused problems with upgrades when plugins had interfered in the normal BuddyPress registration flow in such a way as to retain the activation key on activated accounts. user_status=2 is a more reliable technique.
  • Querying by user_status=2 means that the get_users() query does not have to include all usermeta, which makes for a more performant query on large sites.
  • The migration routine has been broken out into a separate function bp_members_migrate_signups(), and unit tests have been written for it.
  • Activation keys are now regenerated for migrated users who did not have an existing key in usermeta for some reason.

Fixes #5553

Props imath

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-members/bp-members-functions.php

    r8272 r8315  
    16381638}
    16391639
     1640/**
     1641 * Migrate signups from pre-2.0 configuration to wp_signups.
     1642 *
     1643 * @since BuddyPress (2.0.1)
     1644 */
     1645function bp_members_migrate_signups() {
     1646    global $wpdb;
     1647
     1648    $status_2_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->users} WHERE user_status = '2'" );
     1649
     1650    if ( ! empty( $status_2_ids ) ) {
     1651        $signups = get_users( array(
     1652            'fields'  => array(
     1653                'ID',
     1654                'user_login',
     1655                'user_pass',
     1656                'user_registered',
     1657                'user_email',
     1658                'display_name',
     1659            ),
     1660            'include' => $status_2_ids,
     1661        ) );
     1662
     1663        // Fetch activation keys separately, to avoid the all_with_meta
     1664        // overhead
     1665        $status_2_ids_sql = implode( ',', $status_2_ids );
     1666        $ak_data = $wpdb->get_results( "SELECT user_id, meta_value FROM {$wpdb->usermeta} WHERE meta_key = 'activation_key' AND user_id IN ({$status_2_ids_sql})" );
     1667
     1668        // Rekey
     1669        $activation_keys = array();
     1670        foreach ( $ak_data as $ak_datum ) {
     1671            $activation_keys[ intval( $ak_datum->user_id ) ] = $ak_datum->meta_value;
     1672        }
     1673
     1674        unset( $status_2_ids_sql, $status_2_ids, $ak_data );
     1675
     1676        // Merge
     1677        foreach ( $signups as &$signup ) {
     1678            if ( isset( $activation_keys[ $signup->ID ] ) ) {
     1679                $signup->activation_key = $activation_keys[ $signup->ID ];
     1680            }
     1681        }
     1682
     1683        // Reset the signup var as we're using it to process the migration
     1684        unset( $signup );
     1685
     1686    } else {
     1687        return;
     1688    }
     1689
     1690    foreach ( $signups as $signup ) {
     1691        $meta = array();
     1692
     1693        // Rebuild the activation key, if missing
     1694        if ( empty( $signup->activation_key ) ) {
     1695            $signup->activation_key = wp_hash( $signup->ID );
     1696        }
     1697
     1698        if ( bp_is_active( 'xprofile' ) ) {
     1699            $meta['field_1'] = $signup->display_name;
     1700        }
     1701
     1702        $meta['password'] = $signup->user_pass;
     1703
     1704        $user_login = preg_replace( '/\s+/', '', sanitize_user( $signup->user_login, true ) );
     1705        $user_email = sanitize_email( $signup->user_email );
     1706
     1707        BP_Signup::add( array(
     1708            'user_login'     => $user_login,
     1709            'user_email'     => $user_email,
     1710            'registered'     => $signup->user_registered,
     1711            'activation_key' => $signup->activation_key,
     1712            'meta'           => $meta
     1713        ) );
     1714
     1715        // Deleting these options will remove signups from users count
     1716        delete_user_option( $signup->ID, 'capabilities' );
     1717        delete_user_option( $signup->ID, 'user_level'   );
     1718    }
     1719}
     1720
    16401721function bp_core_new_user_activity( $user ) {
    16411722    if ( empty( $user ) || !bp_is_active( 'activity' ) )
Note: See TracChangeset for help on using the changeset viewer.