Skip to content

Commit b3da0ae

Browse files
authored
Merge pull request #184 from moderntribe/release/3.7.0
Package version 3.7.0
2 parents 575ed12 + 2317ad4 commit b3da0ae

26 files changed

Lines changed: 441 additions & 76 deletions

CHANGELOG.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
# Changelog
22

3+
## [3.7.0]
4+
5+
### Added
6+
7+
- When using the WordPress password reset form or updated a user's password from the
8+
WordPress admin, the new password will be synchronized with the BigCommerce customer
9+
account for users who's accounts are configured to sync with BigCommerce.
10+
- Added an endpoint to handle abandoned cart recovery. Visitors who abandon their
11+
carts will receive an email with a link that includes a token to recover the cart.
12+
On clicking the link, the cart will be restored in the user's new browser session.
13+
- Added new route configurations to point BigCommerce-generated links to pages on the
14+
WordPress site:
15+
- `create_account` points to the user registration page
16+
- `forgot_password` points to the lost password page
17+
- `account_order_status` points to the order history page
18+
- `account_new_return` points to the shipping & returns page
19+
- `recover_abandoned_cart` points to the cart recovery endpoint
20+
21+
### Changed
22+
23+
- If a user account is configured to sync with BigCommerce but the customer ID user
24+
meta is missing (as may happen if the BigCommerce plugin has been uninstalled and
25+
reinstalled), a matching customer account may be found by email and used for
26+
password validation.
27+
328
## [3.6.0]
429

530
### Added
@@ -891,6 +916,7 @@
891916

892917

893918
[Unreleased]: https://github.com/moderntribe/bigcommerce/compare/master...develop
919+
[3.7.0]: https://github.com/bigcommerce/bigcommerce-for-wordpress/compare/3.6.0...3.7.0
894920
[3.6.0]: https://github.com/bigcommerce/bigcommerce-for-wordpress/compare/3.5.0...3.6.0
895921
[3.5.0]: https://github.com/bigcommerce/bigcommerce-for-wordpress/compare/3.4.1...3.5.0
896922
[3.4.1]: https://github.com/bigcommerce/bigcommerce-for-wordpress/compare/3.4.0...3.4.1

bigcommerce.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Plugin Name: BigCommerce for WordPress
44
Description: Scale your ecommerce business with WordPress on the front-end and BigCommerce on the back end. Free up server resources from things like catalog management, processing payments, and managing fulfillment logistics.
55
Author: BigCommerce
6-
Version: 3.6.0
6+
Version: 3.7.0
77
Author URI: https://www.bigcommerce.com/wordpress
88
Requires PHP: 5.6.24
99
Text Domain: bigcommerce

build-timestamp.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<?php
2-
define('BIGCOMMERCE_ASSETS_BUILD_TIMESTAMP', '5.51.09.06.2019');
2+
define('BIGCOMMERCE_ASSETS_BUILD_TIMESTAMP', '3.24.09.24.2019');

readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Contributors: bigcommerce, moderntribe, jbrinley, becomevocal, vincentlistrani,
33
Tags: ecommerce, online store, sell online, storefront, retail, online shop, bigcommerce, big commerce, e-commerce, physical products, buy buttons, commerce, shopping cart, checkout, cart, shop, headless commerce, shipping, payments, fulfillment
44
Requires at least: 4.6
55
Tested up to: 5.2.2
6-
Stable tag: 3.6.0
6+
Stable tag: 3.7.0
77
Requires PHP: 5.6.24
88
License: GPLv2 or later
99
License URI: https://www.gnu.org/licenses/gpl-2.0.html

src/BigCommerce/Accounts/Login.php

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace BigCommerce\Accounts;
4+
5+
use BigCommerce\Api\Customer_Api;
6+
7+
class Password_Reset {
8+
9+
/** @var Customer_Api */
10+
private $customer_api;
11+
12+
public function __construct( Customer_Api $customer_api ) {
13+
$this->customer_api = $customer_api;
14+
}
15+
16+
/**
17+
* When a user submits the reset password form on the front end,
18+
* sync with BigCommerce.
19+
*
20+
* @param \WP_User $user The user.
21+
* @param string $new_pass New user password.
22+
*
23+
* @return void
24+
* @action after_password_reset
25+
*/
26+
public function sync_reset_password_with_bigcommerce( $user, $new_pass ) {
27+
$sync = get_user_meta( $user->ID, User_Profile_Settings::SYNC_PASSWORD, true );
28+
if ( ! $sync ) {
29+
return;
30+
}
31+
32+
$this->set_password( $user, $new_pass );
33+
}
34+
35+
/**
36+
* When a user's password is updated from the admin, sync with BigCommerce.
37+
*
38+
* @param int $user_id User ID.
39+
* @param \WP_User $old_user_data Object containing user's data prior to update.
40+
*
41+
* @return void
42+
* @action profile_update
43+
*/
44+
public function sync_password_change_with_bigcommerce( $user_id, $old_user_data ) {
45+
if ( empty( $_POST['pass1'] ) ) { // $_POST is the only place we can find the plain text password
46+
return; // not a request to update a user's password
47+
}
48+
$sync = get_user_meta( $user_id, User_Profile_Settings::SYNC_PASSWORD, true );
49+
if ( ! $sync ) {
50+
return;
51+
}
52+
$current_user = new \WP_User( $user_id );
53+
54+
if ( $current_user->user_pass === $old_user_data->user_pass ) {
55+
return; // nothing changes
56+
}
57+
58+
$this->set_password( $current_user, $_POST['pass1'] );
59+
}
60+
61+
/**
62+
* @param \WP_User $user
63+
* @param string $password
64+
*
65+
* @return bool Whether the password was updated
66+
*/
67+
private function set_password( $user, $password ) {
68+
$customer = new Customer( $user->ID );
69+
$customer_id = $customer->get_customer_id();
70+
if ( ! $customer_id ) {
71+
/*
72+
* If an account is set to sync with BigCommerce, but we don't know
73+
* the customer ID, we'll look it up here.
74+
*/
75+
$customer_id = $this->customer_api->find_customer_id_by_email( $user->user_email );
76+
}
77+
if ( ! $customer_id ) {
78+
return false;
79+
}
80+
81+
$profile = [
82+
'_authentication' => [
83+
'password' => $password,
84+
],
85+
];
86+
87+
try {
88+
$this->customer_api->updateCustomer( $customer_id, $profile );
89+
90+
return true;
91+
} catch ( \Exception $e ) {
92+
return false;
93+
}
94+
}
95+
}

src/BigCommerce/Api/Customer_Api.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33

44
namespace BigCommerce\Api;
55

6-
6+
/**
7+
* Class Customer_Api
8+
*
9+
* @method mixed updateCustomer( $customer_id, $profile )
10+
*/
711
class Customer_Api extends v2ApiAdapter {
812
/**
913
* @param int $customer_id
@@ -29,4 +33,27 @@ public function validatePassword( $customer_id, $password ) {
2933

3034
return ! empty( $response->success );
3135
}
32-
}
36+
37+
/**
38+
* Find the customer ID associated with the given email address
39+
*
40+
* @param string $email
41+
*
42+
* @return int The customer ID, 0 if not found
43+
*/
44+
public function find_customer_id_by_email( $email ) {
45+
try {
46+
$matches = $this->getCustomers( [
47+
'email' => $email,
48+
] );
49+
50+
if ( empty( $matches ) ) {
51+
return 0;
52+
}
53+
54+
return reset( $matches )->id;
55+
} catch ( \Exception $e ) {
56+
return 0;
57+
}
58+
}
59+
}

src/BigCommerce/Api_Factory.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use BigCommerce\Api\v3\Api\WishlistsApi;
1717
use BigCommerce\Api\Webhooks_Api;
1818
use BigCommerce\Api\v3\Api\CartApi;
19+
use BigCommerce\Api\v3\Api\AbandonedCartApi;
1920
use BigCommerce\Api\v3\Api\CatalogApi;
2021
use BigCommerce\Api\v3\Api\ChannelsApi;
2122
use BigCommerce\Api\v3\Api\CustomersApi;
@@ -47,6 +48,13 @@ public function cart() {
4748
return new CartApi( $this->api_client );
4849
}
4950

51+
/**
52+
* @return AbandonedCartApi
53+
*/
54+
public function abandonedCart() {
55+
return new AbandonedCartApi( $this->api_client );
56+
}
57+
5058
/**
5159
* @return CatalogApi
5260
*/

src/BigCommerce/Cart/Cart.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function get_cart_id() {
5656
*
5757
* @return void
5858
*/
59-
private function set_cart_id( $cart_id ) {
59+
public function set_cart_id( $cart_id ) {
6060
/**
6161
* Filter how long the cart cookie should persist
6262
*

0 commit comments

Comments
 (0)