@@ -45,21 +45,35 @@ public function connect_customer_id( $username, $user ) {
4545 return ; // already connected
4646 }
4747
48- $ api = $ this ->api_factory ->customer ();
48+ $ customer_id = $ this ->find_customer_id_by_email ( $ user ->user_email );
49+ if ( $ customer_id ) {
50+ $ customer ->set_customer_id ( $ customer_id );
4951
50- try {
51- $ matches = $ api ->getCustomers ( [
52- 'email ' => $ user ->user_email ,
53- ] );
52+ return ;
53+ }
5454
55- if ( ! empty ( $ matches ) ) {
56- /** @var Api\Resources\Customer $customer */
57- $ found_customer = reset ( $ matches );
58- $ customer ->set_customer_id ( $ found_customer ->id );
55+ $ this ->create_customer_from_user ( $ user );
56+ }
5957
60- return ;
61- }
58+ /**
59+ * Find the customer ID associated with the given email address
60+ *
61+ * @param string $email
62+ *
63+ * @return int The customer ID, 0 if not found
64+ */
65+ private function find_customer_id_by_email ( $ email ) {
66+ return $ this ->api_factory ->customer ()->find_customer_id_by_email ( $ email );
67+ }
6268
69+ /**
70+ * @param \WP_User $user
71+ *
72+ * @return int The new customer's ID, 0 on failure
73+ */
74+ private function create_customer_from_user ( $ user ) {
75+ try {
76+ $ api = $ this ->api_factory ->customer ();
6377 $ new_customer_data = [
6478 'first_name ' => $ user ->first_name ?: $ user ->user_login ,
6579 'last_name ' => $ user ->last_name ?: __ ( 'User ' , 'bigcommerce ' ),
@@ -70,14 +84,16 @@ public function connect_customer_id( $username, $user ) {
7084 $ response = $ api ->createCustomer ( $ new_customer_data );
7185
7286 if ( $ response && ! empty ( $ response ->id ) ) {
87+ $ customer = new Customer ( $ user ->ID );
7388 $ customer ->set_customer_id ( $ response ->id );
7489
75- return ;
90+ return $ response -> id ;
7691 }
77-
7892 } catch ( \Exception $ e ) {
79- return ;
93+ return 0 ;
8094 }
95+
96+ return 0 ;
8197 }
8298
8399 /**
@@ -277,6 +293,11 @@ public function authenticate_new_user( $user, $username, $password ) {
277293 return $ user ;
278294 }
279295
296+ $ matching_user = get_user_by ( 'email ' , $ username );
297+ if ( $ matching_user ) {
298+ return $ user ; // don't try to create a new user if we already have one with that email
299+ }
300+
280301 $ api = $ this ->api_factory ->customer ();
281302
282303 try {
@@ -340,16 +361,29 @@ public function authenticate_new_user( $user, $username, $password ) {
340361 * @filter check_password
341362 */
342363 public function check_password_for_linked_accounts ( $ match , $ password , $ hash , $ user_id ) {
343- $ customer = new Customer ( $ user_id );
344- $ customer_id = $ customer ->get_customer_id ();
345- if ( ! $ customer_id ) {
346- return $ match ;
347- }
348364 $ sync = get_user_meta ( $ user_id , User_Profile_Settings::SYNC_PASSWORD , true );
349365 if ( ! $ sync ) {
350366 return $ match ;
351367 }
352368
369+ $ customer = new Customer ( $ user_id );
370+ $ customer_id = $ customer ->get_customer_id ();
371+ if ( ! $ customer_id ) {
372+ /*
373+ * If an account is set to sync with BigCommerce, but we don't know
374+ * the customer ID, we'll look it up here. Presuming we find it,
375+ * we can validate the password against that ID.
376+ *
377+ * After a successful login, the customer ID will be set in
378+ * self::connect_customer_id() on the wp_login action.
379+ */
380+ $ user = new \WP_User ( $ user_id );
381+ $ customer_id = $ this ->find_customer_id_by_email ( $ user ->user_email );
382+ if ( ! $ customer_id ) {
383+ return $ match ;
384+ }
385+ }
386+
353387 $ api = $ this ->api_factory ->customer ();
354388 try {
355389 return $ api ->validatePassword ( $ customer_id , $ password );
@@ -379,4 +413,4 @@ private function delete_user( $user_id, $customer_id ) {
379413 wp_delete_user ( $ user_id );
380414 }
381415
382- }
416+ }
0 commit comments