diff --git a/includes/API/Plugin/Settings/Handler.php b/includes/API/Plugin/Settings/Handler.php index be6a759e4..90d48eab7 100644 --- a/includes/API/Plugin/Settings/Handler.php +++ b/includes/API/Plugin/Settings/Handler.php @@ -65,6 +65,7 @@ public function register_routes() { public function handle_update( \WP_REST_Request $wp_request ) { try { $request = new UpdateRequest( $wp_request ); + $request_data = $request->get_data(); $validation_result = $request->validate(); if ( is_wp_error( $validation_result ) ) { @@ -74,19 +75,15 @@ public function handle_update( \WP_REST_Request $wp_request ) { ); } + // Maybe trigger products sync and/or metadata feed uploads + $this->maybe_trigger_feed_uploads( $request_data ); + // Map parameters to options and update settings - $options = $this->map_params_to_options( $request->get_data() ); + $options = $this->map_params_to_options( $request_data ); $this->update_settings( $options ); // Update connection status flags - $this->update_connection_status( $request->get_data() ); - - // Allow opt-out of full batch-API sync, for example if store has a large number of products. - if ( facebook_for_woocommerce()->get_integration()->allow_full_batch_api_sync() ) { - facebook_for_woocommerce()->get_products_sync_handler()->create_or_update_all_products(); - } else { - \WC_Facebookcommerce_Utils::logToMeta( 'Initial full product sync disabled by filter hook `facebook_for_woocommerce_allow_full_batch_api_sync`' ); - } + $this->update_connection_status( $request_data ); return $this->success_response( [ @@ -253,4 +250,37 @@ private function clear_integration_options() { delete_option( $option ); } } + + /** + * Triggers products sync if catalog id is being set to a different value. + * + * @since 3.5.0 + * + * @param array $params Request parameters. + * @return void + */ + private function maybe_trigger_feed_uploads( $params ) { + // Only sync products if catalog id has been updated. + try { + if ( ! empty( $params['product_catalog_id'] ) && facebook_for_woocommerce()->get_integration()->get_product_catalog_id() !== $params['product_catalog_id'] ) { + // Allow opt-out of full batch-API sync, for example if store has a large number of products. + if ( facebook_for_woocommerce()->get_integration()->allow_full_batch_api_sync() ) { + facebook_for_woocommerce()->get_products_sync_handler()->create_or_update_all_products(); + } else { + \WC_Facebookcommerce_Utils::logToMeta( 'Initial full product sync disabled by filter hook `facebook_for_woocommerce_allow_full_batch_api_sync`' ); + } + } + } catch ( \Exception $exception ) { + \WC_Facebookcommerce_Utils::logExceptionImmediatelyToMeta( + $exception, + [ + 'event' => 'product_sync', + 'event_type' => 'sync_products_after_settings_update', + 'extra_data' => [ + 'message' => 'failed sync products during the update settings request.', + ], + ] + ); + } + } }