Avoid updating customer info synchronously when "last active" is modified. (https://github.com/woocommerce/woocommerce-admin/pull/6765)

* Schedule (async) customer update when last active date is modified.

* Add test case for scheduled customer update.

* Add changelog entry.
This commit is contained in:
Jeff Stieler 2021-04-12 08:17:02 -06:00 committed by GitHub
parent 157625018d
commit d819373057
4 changed files with 52 additions and 15 deletions

View File

@ -87,6 +87,7 @@ Release and roadmap notes are available on the [WooCommerce Developers Blog](htt
- Fix: Check active plugins before getting the PayPal onboarding status #6625 - Fix: Check active plugins before getting the PayPal onboarding status #6625
- Dev: Add support for running php unit tests in PHP 8. #6678 - Dev: Add support for running php unit tests in PHP 8. #6678
- Fix: Remove no-reply from inbox notification emails #6644 - Fix: Remove no-reply from inbox notification emails #6644
- Performance: Avoid updating customer info synchronously from the front end. #6765
== 2.2.0 3/30/2021 == == 2.2.0 3/30/2021 ==

View File

@ -84,24 +84,9 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
*/ */
public static function init() { public static function init() {
add_action( 'edit_user_profile_update', array( __CLASS__, 'update_registered_customer' ) ); add_action( 'edit_user_profile_update', array( __CLASS__, 'update_registered_customer' ) );
add_action( 'updated_user_meta', array( __CLASS__, 'update_registered_customer_via_last_active' ), 10, 3 );
add_action( 'woocommerce_analytics_delete_order_stats', array( __CLASS__, 'sync_on_order_delete' ), 15, 2 ); add_action( 'woocommerce_analytics_delete_order_stats', array( __CLASS__, 'sync_on_order_delete' ), 15, 2 );
} }
/**
* Trigger a customer update if their "last active" meta value was changed.
* Function expects to be hooked into the `updated_user_meta` action.
*
* @param int $meta_id ID of updated metadata entry.
* @param int $user_id ID of the user being updated.
* @param string $meta_key Meta key being updated.
*/
public static function update_registered_customer_via_last_active( $meta_id, $user_id, $meta_key ) {
if ( 'wc_last_active' === $meta_key ) {
self::update_registered_customer( $user_id );
}
}
/** /**
* Sync customers data after an order was deleted. * Sync customers data after an order was deleted.
* *

View File

@ -28,6 +28,7 @@ class CustomersScheduler extends ImportScheduler {
public static function init() { public static function init() {
add_action( 'woocommerce_new_customer', array( __CLASS__, 'schedule_import' ) ); add_action( 'woocommerce_new_customer', array( __CLASS__, 'schedule_import' ) );
add_action( 'woocommerce_update_customer', array( __CLASS__, 'schedule_import' ) ); add_action( 'woocommerce_update_customer', array( __CLASS__, 'schedule_import' ) );
add_action( 'updated_user_meta', array( __CLASS__, 'schedule_import_via_last_active' ), 10, 3 );
add_action( 'woocommerce_privacy_remove_order_personal_data', array( __CLASS__, 'schedule_anonymize' ) ); add_action( 'woocommerce_privacy_remove_order_personal_data', array( __CLASS__, 'schedule_anonymize' ) );
add_action( 'delete_user', array( __CLASS__, 'schedule_user_delete' ) ); add_action( 'delete_user', array( __CLASS__, 'schedule_user_delete' ) );
add_action( 'remove_user_from_blog', array( __CLASS__, 'schedule_user_delete' ) ); add_action( 'remove_user_from_blog', array( __CLASS__, 'schedule_user_delete' ) );
@ -137,6 +138,20 @@ class CustomersScheduler extends ImportScheduler {
self::schedule_action( 'import', array( $user_id ) ); self::schedule_action( 'import', array( $user_id ) );
} }
/**
* Schedule an import if the "last active" meta value was changed.
* Function expects to be hooked into the `updated_user_meta` action.
*
* @param int $meta_id ID of updated metadata entry.
* @param int $user_id ID of the user being updated.
* @param string $meta_key Meta key being updated.
*/
public static function schedule_import_via_last_active( $meta_id, $user_id, $meta_key ) {
if ( 'wc_last_active' === $meta_key ) {
self::schedule_import( $user_id );
}
}
/** /**
* Schedule an action to anonymize a single Order. * Schedule an action to anonymize a single Order.
* *

View File

@ -6,6 +6,7 @@
* @since 3.5.0 * @since 3.5.0
*/ */
use Automattic\WooCommerce\Admin\Schedulers\CustomersScheduler;
use Automattic\WooCommerce\Admin\Schedulers\OrdersScheduler; use Automattic\WooCommerce\Admin\Schedulers\OrdersScheduler;
use \Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore as OrdersStatsDataStore; use \Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore as OrdersStatsDataStore;
@ -19,6 +20,7 @@ class WC_Tests_API_Init extends WC_REST_Unit_Test_Case {
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
$this->queue = new WC_Admin_Test_Action_Queue(); $this->queue = new WC_Admin_Test_Action_Queue();
CustomersScheduler::set_queue( $this->queue );
OrdersScheduler::set_queue( $this->queue ); OrdersScheduler::set_queue( $this->queue );
} }
@ -27,6 +29,7 @@ class WC_Tests_API_Init extends WC_REST_Unit_Test_Case {
*/ */
public function tearDown() { public function tearDown() {
parent::tearDown(); parent::tearDown();
CustomersScheduler::set_queue( null );
OrdersScheduler::set_queue( null ); OrdersScheduler::set_queue( null );
$this->queue->actions = array(); $this->queue->actions = array();
} }
@ -87,4 +90,37 @@ class WC_Tests_API_Init extends WC_REST_Unit_Test_Case {
$this->queue->actions[0] $this->queue->actions[0]
); );
} }
/**
* Test that updating user meta other than wc_last_active doesn't trigger a customer sync.
*
* @return void
*/
public function test_other_user_meta_update_no_customer_sync() {
update_user_meta( 1, 'nickname', 'test' );
$this->assertEmpty( $this->queue->actions );
}
/**
* Test that updating wc_last_active triggers a customer sync.
*
* @return void
*/
public function test_other_last_active_update_customer_sync() {
// First call creates the meta key.
// These don't use wc_update_user_last_active() because the timestamps will be the same.
update_user_meta( 1, 'wc_last_active', time() - 10 );
// Second call updates it which triggers the sync.
update_user_meta( 1, 'wc_last_active', time() );
$this->assertCount( 1, $this->queue->actions );
$this->assertArraySubset(
array(
'hook' => CustomersScheduler::get_action( 'import' ),
'args' => array( 1 ),
),
$this->queue->actions[0]
);
}
} }