Skip to content

Commit f64fbce

Browse files
rithikb24meta-codesync[bot]
authored andcommitted
feat: making product update sync to fb async (#3645)
Summary: ## Description Adding schedule_product_sync that creates a transient job to sync product to commerce manager, and making it async to product update call on admin page & in turn much faster Helper Function Summaries ------------------------- **`store_sync_transient(int $product_id, array $sync_data): string`** Creates a unique transient key and stores sync data with 300-second expiration for async processing. **`process_sync_transient(string $transient_key): ?array`** Validates transient key format, retrieves and deletes sync data, and restores $_POST context for processing. **`handle_async_product_save(string $transient_key): void`** Processes WordPress cron async calls by handling transient data and calling the main product save method with clean product ID. ### Type of change Please delete options that are not relevant - Add (non-breaking change which adds functionality) ## Checklist - [yes] I have commented my code, particularly in hard-to-understand areas, if any. - [yes] I have confirmed that my changes do not introduce any new PHPCS warnings or errors. - [yes] I have checked plugin debug logs that my changes do not introduce any new PHP warnings or FATAL errors. - [yes] I followed general Pull Request best practices. Meta employees to follow this [wiki]([url](https://fburl.com/wiki/2cgfduwc)). - [yes] I have added tests (if necessary) and all the new and existing unit tests pass locally with my changes. - [yes] I have completed dogfooding and QA testing, or I have conducted thorough due diligence to ensure that it does not break existing functionality. - [yes] I have updated or requested update to plugin documentations (if necessary). Meta employees to follow this [wiki]([url](https://fburl.com/wiki/nhx73tgs)). ## Changelog entry Making product update changes sync to commerce manager async Pull Request resolved: #3645 Test Plan: Updated a product and verified sync to commerce manager Notice the difference in time for update to go through ### Before https://pxl.cl/8gHrM ### After https://pxl.cl/8gHrP Reviewed By: vinkmeta Differential Revision: D83573027 Pulled By: rithikb24 fbshipit-source-id: 7c20c9aab81bdff4261f4cc62b718427f303af42
1 parent 53a90f4 commit f64fbce

File tree

1 file changed

+102
-1
lines changed

1 file changed

+102
-1
lines changed

facebook-commerce.php

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@ public function __construct( WC_Facebookcommerce $facebook_for_woocommerce ) {
309309
update_option( self::SETTING_ENABLE_META_DIAGNOSIS, 'yes' );
310310
}
311311

312+
// Register the WordPress cron hook for async processing
313+
add_action( 'wc_facebook_async_sync', [ $this, 'handle_async_product_save' ] );
314+
312315
if ( is_admin() ) {
313316

314317
$this->init_pixel();
@@ -348,7 +351,7 @@ public function __construct( WC_Facebookcommerce $facebook_for_woocommerce ) {
348351
if ( $this->is_configured() && $this->get_product_catalog_id() ) {
349352

350353
// On_product_save() must run with priority larger than 20 to make sure WooCommerce has a chance to save the submitted product information.
351-
add_action( 'woocommerce_process_product_meta', [ $this, 'on_product_save' ], 40 );
354+
add_action( 'woocommerce_process_product_meta', [ $this, 'schedule_product_sync' ], 40 );
352355

353356
add_action(
354357
'woocommerce_product_quick_edit_save',
@@ -825,6 +828,104 @@ private function get_removed_from_sync_products_to_delete() {
825828
return array_map( 'absint', explode( ',', $posted_products ) );
826829
}
827830

831+
832+
/**
833+
* Stores sync data in a transient for async processing.
834+
*
835+
* @param int $wp_id The product ID
836+
* @param array $sync_data The sync data to store
837+
* @return string The transient key used for storage
838+
* @since 3.5.10
839+
*/
840+
private function get_store_sync_transient( int $wp_id, array $sync_data ): string {
841+
$sync_key = 'fb_sync_data_' . $wp_id . '_' . time();
842+
set_transient( $sync_key, $sync_data, 300 );
843+
return $sync_key;
844+
}
845+
846+
/**
847+
* Processes async sync data from transient and restores context.
848+
*
849+
* @param string $transient_key The transient key
850+
* @return array|null Array containing product_id and success flag, or null if failed
851+
* @since 3.5.10
852+
*/
853+
private function process_sync_transient( string $transient_key ): ?array {
854+
// Validate transient key format
855+
if ( strpos( $transient_key, 'fb_sync_data_' ) !== 0 ) {
856+
return null;
857+
}
858+
859+
// Retrieve sync data
860+
$sync_data = get_transient( $transient_key );
861+
if ( ! $sync_data ) {
862+
return null; // Data expired or doesn't exist
863+
}
864+
865+
// Clean up transient
866+
delete_transient( $transient_key );
867+
868+
// Restore context
869+
$product_id = $sync_data['product_id'];
870+
$_POST = $sync_data['post_data']; // Restore $_POST data
871+
872+
return [
873+
'product_id' => $product_id,
874+
'success' => true,
875+
];
876+
}
877+
878+
/**
879+
* Handles async product save from WordPress cron by processing transient data.
880+
*
881+
* @param string $transient_key The transient key containing sync data
882+
* @since 3.5.10
883+
* @internal
884+
*/
885+
public function handle_async_product_save( string $transient_key ) {
886+
$transient_result = $this->process_sync_transient( $transient_key );
887+
if ( ! $transient_result ) {
888+
return; // Invalid transient or data expired
889+
}
890+
891+
// Call the regular product save method with clean product ID
892+
$this->on_product_save( $transient_result['product_id'] );
893+
}
894+
895+
/**
896+
* Schedule product sync for async processing using WordPress cron.
897+
*
898+
* @param int $wp_id post ID
899+
*
900+
* @since 3.5.10
901+
*
902+
* @internal
903+
*/
904+
public function schedule_product_sync( int $wp_id ) {
905+
// Store sync data in transient for async processing
906+
// phpcs:disable WordPress.Security.NonceVerification.Missing
907+
$sync_data = [
908+
'product_id' => $wp_id,
909+
'post_data' => $_POST,
910+
];
911+
// phpcs:enable WordPress.Security.NonceVerification.Missing
912+
913+
$sync_key = $this->get_store_sync_transient( $wp_id, $sync_data );
914+
915+
// Schedule WordPress cron event
916+
$scheduled = wp_schedule_single_event(
917+
time() + 1,
918+
'wc_facebook_async_sync',
919+
[ $sync_key ]
920+
);
921+
922+
// Fallback to synchronous processing if scheduling fails
923+
if ( false === $scheduled ) {
924+
delete_transient( $sync_key );
925+
$this->on_product_save( $wp_id );
926+
}
927+
}
928+
828929
/**
829930
* Checks the product type and calls the corresponding on publish method.
830931
*

0 commit comments

Comments
 (0)