-
Notifications
You must be signed in to change notification settings - Fork 882
Sync WooCommerce module: Start syncing customer account detail changes stored in user meta #49551
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fgiannar
wants to merge
5
commits into
trunk
Choose a base branch
from
add/sync-woo-customer-props-updated
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0a787f9
Sync WooCommerce module: Start syncing 'woocommerce_customer_object_u…
fgiannar a1aa3cd
changelog
fgiannar 3c1e46b
Alternate approach to ensure all meta changes are picked
fgiannar 5ab8fcf
Enforce minimal WP_User shaped payload and update docblocks
fgiannar 2eb4b82
Prevent 'jetpack_updated_woo_customer_meta' action if user is deleted
fgiannar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
projects/packages/sync/changelog/add-woocommerce-customer-updated-props-sync
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: minor | ||
| Type: added | ||
|
|
||
| Sync: Send WooCommerce customer account detail updates. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,6 +55,38 @@ class WooCommerce extends Module { | |
| 'discount_amount_tax', | ||
| ); | ||
|
|
||
| /** | ||
| * Mapping between WooCommerce customer detail user meta keys and customer prop names. | ||
| * | ||
| * @access private | ||
| * | ||
| * @var array | ||
| */ | ||
| private static $customer_detail_meta_key_to_prop = array( | ||
| 'paying_customer' => 'is_paying_customer', | ||
| 'billing_first_name' => 'billing_first_name', | ||
| 'billing_last_name' => 'billing_last_name', | ||
| 'billing_company' => 'billing_company', | ||
| 'billing_address_1' => 'billing_address_1', | ||
| 'billing_address_2' => 'billing_address_2', | ||
| 'billing_city' => 'billing_city', | ||
| 'billing_state' => 'billing_state', | ||
| 'billing_postcode' => 'billing_postcode', | ||
| 'billing_country' => 'billing_country', | ||
| 'billing_email' => 'billing_email', | ||
| 'billing_phone' => 'billing_phone', | ||
| 'shipping_first_name' => 'shipping_first_name', | ||
| 'shipping_last_name' => 'shipping_last_name', | ||
| 'shipping_company' => 'shipping_company', | ||
| 'shipping_address_1' => 'shipping_address_1', | ||
| 'shipping_address_2' => 'shipping_address_2', | ||
| 'shipping_city' => 'shipping_city', | ||
| 'shipping_state' => 'shipping_state', | ||
| 'shipping_postcode' => 'shipping_postcode', | ||
| 'shipping_country' => 'shipping_country', | ||
| 'shipping_phone' => 'shipping_phone', | ||
| ); | ||
|
|
||
| /** | ||
| * Name of the order item database table. | ||
| * | ||
|
|
@@ -64,6 +96,13 @@ class WooCommerce extends Module { | |
| */ | ||
| private $order_item_table_name; | ||
|
|
||
| /** | ||
| * Customer detail meta changes to sync at the end of the request. | ||
| * | ||
| * @var array | ||
| */ | ||
| private $customer_meta_updates = array(); | ||
|
|
||
| /** | ||
| * The table name. | ||
| * | ||
|
|
@@ -129,6 +168,7 @@ public function __construct() { | |
| add_filter( 'jetpack_sync_comment_meta_whitelist', array( $this, 'add_woocommerce_comment_meta_whitelist' ), 10 ); | ||
|
|
||
| add_filter( 'jetpack_sync_before_enqueue_woocommerce_new_order_item', array( $this, 'filter_order_item' ) ); | ||
| add_filter( 'jetpack_sync_before_enqueue_jetpack_updated_woo_customer_meta', array( $this, 'filter_customer_updated_meta' ) ); | ||
| add_filter( 'jetpack_sync_whitelisted_comment_types', array( $this, 'add_review_comment_types' ) ); | ||
|
|
||
| // Blacklist Action Scheduler comment types. | ||
|
|
@@ -194,6 +234,13 @@ public function init_listeners( $callable ) { | |
| add_action( 'woocommerce_new_webhook', $callable, 10, 1 ); | ||
| add_action( 'woocommerce_webhook_deleted', $callable, 10, 2 ); | ||
| add_action( 'woocommerce_webhook_updated', $callable, 10, 1 ); | ||
|
|
||
| // Customers. | ||
| add_action( 'added_user_meta', array( $this, 'maybe_sync_customer_meta_update' ), 10, 4 ); | ||
| add_action( 'updated_user_meta', array( $this, 'maybe_sync_customer_meta_update' ), 10, 4 ); | ||
| add_action( 'deleted_user_meta', array( $this, 'maybe_sync_customer_meta_update' ), 10, 4 ); | ||
| add_action( 'shutdown', array( $this, 'action_customer_meta_updates' ) ); | ||
| add_action( 'jetpack_updated_woo_customer_meta', $callable, 10, 2 ); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -242,6 +289,151 @@ public function filter_order_item( $args ) { | |
| return $args; | ||
| } | ||
|
|
||
| /** | ||
| * Validate the minimal customer meta update payload before enqueueing. | ||
| * | ||
| * @param array $args Hook arguments. | ||
| * @return array|false Minimal user object and changed prop names, or false when invalid. | ||
| */ | ||
| public function filter_customer_updated_meta( $args ) { | ||
| if ( | ||
| ! is_array( $args ) | ||
| || ! isset( $args[0] ) | ||
| || ! isset( $args[1] ) | ||
| || ! is_object( $args[0] ) | ||
| || ! isset( $args[0]->data ) | ||
| || ! is_object( $args[0]->data ) | ||
| || ! isset( $args[0]->data->ID ) | ||
| || ! is_numeric( $args[0]->data->ID ) | ||
| || ! is_array( $args[1] ) | ||
| ) { | ||
| return false; | ||
| } | ||
|
|
||
| $customer_id = (int) $args[0]->data->ID; | ||
| if ( $customer_id <= 0 ) { | ||
| return false; | ||
| } | ||
|
|
||
| $args[0]->ID = $customer_id; | ||
| $args[0]->data->ID = $customer_id; | ||
|
|
||
| $updated_props = $this->get_customer_detail_props( $args[1] ); | ||
| if ( empty( $updated_props ) ) { | ||
| return false; | ||
| } | ||
|
|
||
| return array( $args[0], $updated_props ); | ||
| } | ||
|
|
||
| /** | ||
| * Track updated WooCommerce customer meta props for syncing. | ||
| * | ||
| * @param int $meta_id ID of the meta object. | ||
|
fgiannar marked this conversation as resolved.
Outdated
|
||
| * @param int $user_id User ID. | ||
| * @param string $meta_key Meta key. | ||
| * @param mixed $value Meta value. | ||
| */ | ||
| public function maybe_sync_customer_meta_update( $meta_id, $user_id, $meta_key, $value ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | ||
| $customer_id = (int) $user_id; | ||
| if ( $customer_id <= 0 ) { | ||
| return; | ||
| } | ||
|
|
||
| $updated_props = $this->get_customer_detail_props( array( $meta_key ) ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if ( empty( $updated_props ) ) { | ||
| return; | ||
| } | ||
|
|
||
| if ( ! isset( $this->customer_meta_updates[ $customer_id ] ) ) { | ||
| $this->customer_meta_updates[ $customer_id ] = array(); | ||
| } | ||
|
|
||
| foreach ( $updated_props as $prop ) { | ||
| $this->customer_meta_updates[ $customer_id ][ $prop ] = true; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Send batched WooCommerce customer meta updates. | ||
| */ | ||
| public function action_customer_meta_updates() { | ||
| if ( empty( $this->customer_meta_updates ) ) { | ||
| return; | ||
| } | ||
|
|
||
| $customer_meta_updates = $this->customer_meta_updates; | ||
| $this->customer_meta_updates = array(); | ||
|
|
||
| foreach ( $customer_meta_updates as $customer_id => $updated_props ) { | ||
| /** | ||
| * Fires when WooCommerce customer details stored in user meta are updated. | ||
| * | ||
| * @param object $customer Minimal WP_User-shaped customer object. | ||
| * @param array $updated_props Updated customer detail prop names. | ||
| */ | ||
| do_action( | ||
| 'jetpack_updated_woo_customer_meta', | ||
| $this->build_minimal_customer_user_object( (int) $customer_id ), | ||
| array_keys( $updated_props ) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Retrieve whitelisted WooCommerce customer detail props. | ||
| * | ||
| * @param array $props Customer detail meta keys or prop names. | ||
| * @return array Customer detail prop names. | ||
| */ | ||
| private function get_customer_detail_props( $props ) { | ||
| $updated_props = array(); | ||
| foreach ( $props as $prop ) { | ||
| if ( ! is_string( $prop ) && ! is_numeric( $prop ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| $prop = sanitize_key( (string) $prop ); | ||
| if ( isset( self::$customer_detail_meta_key_to_prop[ $prop ] ) ) { | ||
| $updated_props[] = self::$customer_detail_meta_key_to_prop[ $prop ]; | ||
| continue; | ||
| } | ||
|
|
||
| if ( in_array( $prop, self::$customer_detail_meta_key_to_prop, true ) ) { | ||
| $updated_props[] = $prop; | ||
| } | ||
| } | ||
|
|
||
| return array_values( array_unique( $updated_props ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Build a minimal WP_User-shaped object for Activity Log. | ||
| * | ||
| * @param int $customer_id Customer user ID. | ||
| * @return object Minimal user object. | ||
| */ | ||
| private function build_minimal_customer_user_object( $customer_id ) { | ||
| $user_data = (object) array( | ||
| 'ID' => $customer_id, | ||
| 'display_name' => '', | ||
| 'user_login' => '', | ||
| 'user_email' => '', | ||
| ); | ||
|
|
||
| $user = get_userdata( $customer_id ); | ||
| if ( $user ) { | ||
| $user_data->display_name = (string) $user->display_name; | ||
| $user_data->user_login = (string) $user->user_login; | ||
| $user_data->user_email = (string) $user->user_email; | ||
| } | ||
|
|
||
| return (object) array( | ||
| 'ID' => $customer_id, | ||
| 'data' => $user_data, | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Handler for filtering out non-whitelisted order item meta. | ||
| * | ||
|
|
||
5 changes: 5 additions & 0 deletions
5
projects/plugins/jetpack/changelog/add-sync-woo-customer-props-updated
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| Significance: patch | ||
| Type: other | ||
| Comment: Updated Sync related unit tests | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.