bp_core_activate_signup( string $key )

Activate a signup, as identified by an activation key.

Description

Parameters

$key

(Required) Activation key.

Return

(int|bool) User ID on success, false on failure.

Source

File: bp-members/bp-members-functions.php

1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
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.', 'buddyboss' ) );
        }
 
        $signup = $signups['signups'][0];
 
        if ( $signup->active ) {
            if ( empty( $signup->domain ) ) {
                return new WP_Error( 'already_active', __( 'The user is already active.', 'buddyboss' ), $signup );
            } else {
                return new WP_Error( 'already_active', __( 'The site is already active.', 'buddyboss' ), $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.', 'buddyboss' ) );
            }
 
            bp_delete_user_meta( $user_id, 'activation_key' );
 
            $user_already_created = true;
 
        } else {
            $user_already_exists = true;
        }
 
        if ( ! $user_id ) {
            return new WP_Error( 'create_user', __( 'Could not create user', 'buddyboss' ), $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.', 'buddyboss' ), $signup );
        }
 
        // Set up data to pass to the legacy filter.
        $user = array(
            'user_id'  => $user_id,
            'password' => $signup->meta['password'],
            'meta'     => $signup->meta,
        );
 
        /**
         * Maybe notify the site admin of a new user registration.
         *
         * @since BuddyPress 1.2.2
         *
         * @param bool $notification Whether to send the notification or not.
         */
        if ( apply_filters( 'bp_core_send_user_registration_admin_notification', true ) ) {
            wp_new_user_notification( $user_id );
        }
 
        if ( isset( $user_already_created ) ) {
 
            /**
             * Fires if the user has already been created.
             *
             * @since BuddyPress 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.
                 *
                 * Use the field's default visibility if not present, and 'public' if a
                 * default visibility is not defined.
                 */
                $key = "field_{$field_id}_visibility";
                if ( isset( $user['meta'][ $key ] ) ) {
                    $visibility_level = $user['meta'][ $key ];
                } else {
                    $vfield           = xprofile_get_field( $field_id );
                    $visibility_level = isset( $vfield->default_visibility ) ? $vfield->default_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 BuddyPress 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;
}

Changelog

Changelog
Version Description
BuddyPress 1.2.2 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.