Login::authenticate_new_user( WP_User|WP_Error|null $user, string $username, string $password )
Summary
If a user logs in with credentials for a user in BigCommerce that do not match a user in WordPress, create a user account and log the user in.
Parameters
- $user
-
(WP_User|WP_Error|null) (Required)
- $username
-
(string) (Required)
- $password
-
(string) (Required)
Return
(WP_User|WP_Error|null)
Source
File: src/BigCommerce/Accounts/Login.php
public function authenticate_new_user( $user, $username, $password ) { if ( $user instanceof WP_User ) { return $user; } if ( ! is_email( $username ) ) { return $user; } $matching_user = get_user_by( 'email', $username ); if ( $matching_user ) { return $user; // don't try to create a new user if we already have one with that email } $api = $this->api_factory->customer(); try { $matches = $api->getCustomers( [ 'email' => $username, ] ); if ( empty( $matches ) ) { return $user; } /** @var Api\Resources\Customer $customer */ $found_customer = reset( $matches ); $valid = $api->validatePassword( $found_customer->id, $password ); if ( ! $valid ) { return new \WP_Error( 'incorrect_password', sprintf( __( 'The password you entered for the email address %s is incorrect.', 'bigcommerce' ), $username ) ); } // we have a valid user, so create a WP account $user_id = wp_create_user( $username, $password, $username ); if ( is_wp_error( $user_id ) ) { return $user_id; } $user = new \WP_User( $user_id ); /** * Filter the default role given to new users * * @param string $role */ $role = apply_filters( 'bigcommerce/user/default_role', Customer_Role::NAME ); $user->set_role( $role ); // all future password validation will be against the API for this user update_user_meta( $user_id, User_Profile_Settings::SYNC_PASSWORD, true ); $customer = new Customer( $user_id ); $customer->set_customer_id( $found_customer->id ); return new \WP_User( $user_id ); } catch ( \Exception $e ) { return $user; } }