/**
 * Activate a signup, as identified by an activation key.
 *
 * @param string $key Activation key.
 * @return int|bool User ID on success, false on failure.
 */
function bp_core_activate_signup($key)
{
    global $wpdb;
    $user = false;
    // Multisite installs have their own activation routine.
    if (is_multisite()) {
        $user = wpmu_activate_signup($key);
        // If there were errors, add a message and redirect.
        if (!empty($user->errors)) {
            return $user;
        }
        $user_id = $user['user_id'];
    } else {
        $signups = BP_Signup::get(array('activation_key' => $key));
        if (empty($signups['signups'])) {
            return new WP_Error('invalid_key', __('Invalid activation key.', 'buddypress'));
        }
        $signup = $signups['signups'][0];
        if ($signup->active) {
            if (empty($signup->domain)) {
                return new WP_Error('already_active', __('The user is already active.', 'buddypress'), $signup);
            } else {
                return new WP_Error('already_active', __('The site is already active.', 'buddypress'), $signup);
            }
        }
        // Password is hashed again in wp_insert_user.
        $password = wp_generate_password(12, false);
        $user_id = username_exists($signup->user_login);
        // Create the user.
        if (!$user_id) {
            $user_id = wp_create_user($signup->user_login, $password, $signup->user_email);
            // If a user ID is found, this may be a legacy signup, or one
            // created locally for backward compatibility. Process it.
        } elseif ($key == wp_hash($user_id)) {
            // Change the user's status so they become active.
            if (!$wpdb->query($wpdb->prepare("UPDATE {$wpdb->users} SET user_status = 0 WHERE ID = %d", $user_id))) {
                return new WP_Error('invalid_key', __('Invalid activation key.', 'buddypress'));
            }
            bp_delete_user_meta($user_id, 'activation_key');
            $member = get_userdata($user_id);
            $member->set_role(get_option('default_role'));
            $user_already_created = true;
        } else {
            $user_already_exists = true;
        }
        if (!$user_id) {
            return new WP_Error('create_user', __('Could not create user', 'buddypress'), $signup);
        }
        // Fetch the signup so we have the data later on.
        $signups = BP_Signup::get(array('activation_key' => $key));
        $signup = isset($signups['signups']) && !empty($signups['signups'][0]) ? $signups['signups'][0] : false;
        // Activate the signup.
        BP_Signup::validate($key);
        if (isset($user_already_exists)) {
            return new WP_Error('user_already_exists', __('That username is already activated.', 'buddypress'), $signup);
        }
        // Set up data to pass to the legacy filter.
        $user = array('user_id' => $user_id, 'password' => $signup->meta['password'], 'meta' => $signup->meta);
        // Notify the site admin of a new user registration.
        wp_new_user_notification($user_id);
        if (isset($user_already_created)) {
            /**
             * Fires if the user has already been created.
             *
             * @since 1.2.2
             *
             * @param int    $user_id ID of the user being checked.
             * @param string $key     Activation key.
             * @param array  $user    Array of user data.
             */
            do_action('bp_core_activated_user', $user_id, $key, $user);
            return $user_id;
        }
    }
    // Set any profile data.
    if (bp_is_active('xprofile')) {
        if (!empty($user['meta']['profile_field_ids'])) {
            $profile_field_ids = explode(',', $user['meta']['profile_field_ids']);
            foreach ((array) $profile_field_ids as $field_id) {
                $current_field = isset($user['meta']["field_{$field_id}"]) ? $user['meta']["field_{$field_id}"] : false;
                if (!empty($current_field)) {
                    xprofile_set_field_data($field_id, $user_id, $current_field);
                }
                // Save the visibility level.
                $visibility_level = !empty($user['meta']['field_' . $field_id . '_visibility']) ? $user['meta']['field_' . $field_id . '_visibility'] : 'public';
                xprofile_set_field_visibility_level($field_id, $user_id, $visibility_level);
            }
        }
    }
    // Update the display_name.
    wp_update_user(array('ID' => $user_id, 'display_name' => bp_core_get_user_displayname($user_id)));
    // Set the password on multisite installs.
    if (!empty($user['meta']['password'])) {
        $wpdb->query($wpdb->prepare("UPDATE {$wpdb->users} SET user_pass = %s WHERE ID = %d", $user['meta']['password'], $user_id));
    }
    /**
     * Fires at the end of the user activation process.
     *
     * @since 1.2.2
     *
     * @param int    $user_id ID of the user being checked.
     * @param string $key     Activation key.
     * @param array  $user    Array of user data.
     */
    do_action('bp_core_activated_user', $user_id, $key, $user);
    return $user_id;
}
/**
 * Activate a signup, as identified by an activation key.
 *
 * @since 1.2.2
 *
 * @param string $key Activation key.
 * @return int|bool User ID on success, false on failure.
 */
function bp_core_activate_signup($key)
{
    global $wpdb;
    $user = false;
    // Multisite installs have their own activation routine.
    if (is_multisite()) {
        $user = wpmu_activate_signup($key);
        // If there were errors, add a message and redirect.
        if (!empty($user->errors)) {
            return $user;
        }
        $user_id = $user['user_id'];
    } else {
        $signups = BP_Signup::get(array('activation_key' => $key));
        if (empty($signups['signups'])) {
            return new WP_Error('invalid_key', __('Invalid activation key.', 'buddypress'));
        }
        $signup = $signups['signups'][0];
        if ($signup->active) {
            if (empty($signup->domain)) {
                return new WP_Error('already_active', __('The user is already active.', 'buddypress'), $signup);
            } else {
                return new WP_Error('already_active', __('The site is already active.', 'buddypress'), $signup);
            }
        }
        // Password is hashed again in wp_insert_user.
        $password = wp_generate_password(12, false);
        $user_id = username_exists($signup->user_login);
        // Create the user. This should only be necessary if BP_SIGNUPS_SKIP_USER_CREATION is true.
        if (!$user_id) {
            $user_id = wp_create_user($signup->user_login, $password, $signup->user_email);
            // Otherwise, update the existing user's status.
        } elseif ($key === bp_get_user_meta($user_id, 'activation_key', true) || $key === wp_hash($user_id)) {
            // Change the user's status so they become active.
            if (!$wpdb->query($wpdb->prepare("UPDATE {$wpdb->users} SET user_status = 0 WHERE ID = %d", $user_id))) {
                return new WP_Error('invalid_key', __('Invalid activation key.', 'buddypress'));
            }
            bp_delete_user_meta($user_id, 'activation_key');
            $member = get_userdata($user_id);
            $member->set_role(get_option('default_role'));
            $user_already_created = true;
        } else {
            $user_already_exists = true;
        }
        if (!$user_id) {
            return new WP_Error('create_user', __('Could not create user', 'buddypress'), $signup);
        }
        // Fetch the signup so we have the data later on.
        $signups = BP_Signup::get(array('activation_key' => $key));
        $signup = isset($signups['signups']) && !empty($signups['signups'][0]) ? $signups['signups'][0] : false;
        // Activate the signup.
        BP_Signup::validate($key);
        if (isset($user_already_exists)) {
            return new WP_Error('user_already_exists', __('That username is already activated.', 'buddypress'), $signup);
        }
        // Set up data to pass to the legacy filter.
        $user = array('user_id' => $user_id, 'password' => $signup->meta['password'], 'meta' => $signup->meta);
        // Notify the site admin of a new user registration.
        wp_new_user_notification($user_id);
        if (isset($user_already_created)) {
            /**
             * Fires if the user has already been created.
             *
             * @since 1.2.2
             *
             * @param int    $user_id ID of the user being checked.
             * @param string $key     Activation key.
             * @param array  $user    Array of user data.
             */
            do_action('bp_core_activated_user', $user_id, $key, $user);
            return $user_id;
        }
    }
    // Set any profile data.
    if (bp_is_active('xprofile')) {
        if (!empty($user['meta']['profile_field_ids'])) {
            $profile_field_ids = explode(',', $user['meta']['profile_field_ids']);
            foreach ((array) $profile_field_ids as $field_id) {
                $current_field = isset($user['meta']["field_{$field_id}"]) ? $user['meta']["field_{$field_id}"] : false;
                if (!empty($current_field)) {
                    xprofile_set_field_data($field_id, $user_id, $current_field);
                }
                // Save the visibility level.
                $visibility_level = !empty($user['meta']['field_' . $field_id . '_visibility']) ? $user['meta']['field_' . $field_id . '_visibility'] : 'public';
                xprofile_set_field_visibility_level($field_id, $user_id, $visibility_level);
            }
        }
    }
    // Replace the password automatically generated by WordPress by the one the user chose.
    if (!empty($user['meta']['password'])) {
        $wpdb->query($wpdb->prepare("UPDATE {$wpdb->users} SET user_pass = %s WHERE ID = %d", $user['meta']['password'], $user_id));
        /**
         * Make sure to clean the user's cache as we've
         * directly edited the password without using
         * wp_update_user().
         *
         * If we can't use wp_update_user() that's because
         * we already hashed the password at the signup step.
         */
        $uc = wp_cache_get($user_id, 'users');
        if (!empty($uc->ID)) {
            clean_user_cache($uc->ID);
        }
    }
    /**
     * Fires at the end of the user activation process.
     *
     * @since 1.2.2
     *
     * @param int    $user_id ID of the user being checked.
     * @param string $key     Activation key.
     * @param array  $user    Array of user data.
     */
    do_action('bp_core_activated_user', $user_id, $key, $user);
    return $user_id;
}
 function active_user_for_wps($user_id, $user_login, $user_password, $user_email, $usermeta)
 {
     global $bp, $wpdb;
     $user = null;
     if (defined('DOING_AJAX')) {
         return $user_id;
     }
     if (is_multisite()) {
         return $user_id;
     }
     //do not proceed for mu
     $signups = BP_Signup::get(array('user_login' => $user_login));
     $signups = $signups['signups'];
     if (!$signups) {
         return false;
     }
     //if we are here, just popout the array
     $signup = array_pop($signups);
     // password is hashed again in wp_insert_user
     $password = wp_generate_password(12, false);
     $user_id = username_exists($signup->user_login);
     $key = $signup->activation_key;
     if (!$key) {
         $key = bp_get_user_meta($user_id, 'activation_key', true);
     }
     // Create the user
     if (!$user_id) {
         //this should almost never happen
         $user_id = wp_create_user($signup->user_login, $password, $signup->user_email);
         // If a user ID is found, this may be a legacy signup, or one
         // created locally for backward compatibility. Process it.
     } elseif ($key == wp_hash($user_id)) {
         // Change the user's status so they become active
         if (!$wpdb->query($wpdb->prepare("UPDATE {$wpdb->users} SET user_status = 0 WHERE ID = %d", $user_id))) {
             return new WP_Error('invalid_key', __('Invalid activation key', 'buddypress'));
         }
         bp_delete_user_meta($user_id, 'activation_key');
         $member = get_userdata($user_id);
         $member->set_role(get_option('default_role'));
         $user_already_created = true;
     } else {
         $user_already_exists = true;
     }
     if (!$user_id) {
         return new WP_Error('create_user', __('Could not create user', 'buddypress'), $signup);
     }
     // Fetch the signup so we have the data later on
     $signups = BP_Signup::get(array('activation_key' => $key));
     $signup = isset($signups['signups']) && !empty($signups['signups'][0]) ? $signups['signups'][0] : false;
     // Activate the signup
     BP_Signup::validate($key);
     if (isset($user_already_exists)) {
         return new WP_Error('user_already_exists', __('That username is already activated.', 'buddypress'), $signup);
     }
     // Set up data to pass to the legacy filter
     $user = array('user_id' => $user_id, 'password' => $signup->meta['password'], 'meta' => $signup->meta);
     // Notify the site admin of a new user registration
     wp_new_user_notification($user_id);
     wp_cache_delete('bp_total_member_count', 'bp');
     /* Add a last active entry */
     bp_update_user_last_activity($user_id);
     do_action('bp_core_activated_user', $user_id, $key, $user);
     bp_core_add_message(__('Your account is now active!'));
     $bp->activation_complete = true;
     xprofile_sync_wp_profile();
     //$ud = get_userdata($signup['user_id']);
     self::login_redirect($user_login, $user_password);
     //will never reach here anyway
     return $user_id;
 }