From f16ae49427fa4f8676aebcf2c6d6f3de8bdf40bb Mon Sep 17 00:00:00 2001 From: Juanma Rodriguez Escriche Date: Tue, 17 Dec 2024 10:28:05 +0100 Subject: [PATCH] Jetpack Sync: Add key for full sync actions (#40566) * Added key so it is the same when sending full sync actions * changelog * Switch to wp_json_encode instead of (string) since terms will show an array in status * Use chunks_sent instead of last sent to avoid sending weird strings, espceially for term_relationships * Using last sent again but with crc32 * Not sending specific key for modules that don't send multiple full sync actions --- .../update-sync-add-key-for-full-sync-actions | 4 ++++ projects/packages/sync/src/class-sender.php | 10 ++++++---- projects/packages/sync/src/modules/class-module.php | 9 ++++++--- 3 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 projects/packages/sync/changelog/update-sync-add-key-for-full-sync-actions diff --git a/projects/packages/sync/changelog/update-sync-add-key-for-full-sync-actions b/projects/packages/sync/changelog/update-sync-add-key-for-full-sync-actions new file mode 100644 index 0000000000000..2a67637236472 --- /dev/null +++ b/projects/packages/sync/changelog/update-sync-add-key-for-full-sync-actions @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Sync:Add specific key for full sync actions diff --git a/projects/packages/sync/src/class-sender.php b/projects/packages/sync/src/class-sender.php index d86b2bce8ef97..05dbb2e4575ce 100644 --- a/projects/packages/sync/src/class-sender.php +++ b/projects/packages/sync/src/class-sender.php @@ -704,16 +704,17 @@ public function do_sync_for_queue( $queue ) { * * @param string $action_name The action. * @param array $data The data associated with the action. + * @param string $key The key to use for the action. * * @return array Items processed. TODO: this doesn't make much sense anymore, it should probably be just a bool. */ - public function send_action( $action_name, $data = null ) { + public function send_action( $action_name, $data = null, $key = null ) { if ( ! Settings::is_sender_enabled( 'full_sync' ) ) { return array(); } // Compose the data to be sent. - $action_to_send = $this->create_action_to_send( $action_name, $data ); + $action_to_send = $this->create_action_to_send( $action_name, $data, $key ); list( $items_to_send, $skipped_items_ids, $items, $preprocess_duration ) = $this->get_items_to_send( $action_to_send, true ); // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable Settings::set_is_sending( true ); @@ -741,11 +742,12 @@ public function send_action( $action_name, $data = null ) { * * @param string $action_name The action. * @param array $data The data associated with the action. + * @param string $key The key to use for the action. * @return array An array of synthetic sync actions keyed by current microtime(true) */ - private function create_action_to_send( $action_name, $data ) { + private function create_action_to_send( $action_name, $data, $key = null ) { return array( - (string) microtime( true ) => array( + $key ?? (string) microtime( true ) => array( $action_name, $data, get_current_user_id(), diff --git a/projects/packages/sync/src/modules/class-module.php b/projects/packages/sync/src/modules/class-module.php index ea419b15a7626..af3ca93207047 100644 --- a/projects/packages/sync/src/modules/class-module.php +++ b/projects/packages/sync/src/modules/class-module.php @@ -405,7 +405,9 @@ public function send_full_sync_actions( $config, $status, $send_until ) { $status['finished'] = true; return $status; } - $result = $this->send_action( $this->full_sync_action_name(), array( $objects, $status['last_sent'] ) ); + $key = $this->full_sync_action_name() . '_' . crc32( wp_json_encode( $status['last_sent'] ) ); + + $result = $this->send_action( $this->full_sync_action_name(), array( $objects, $status['last_sent'] ), $key ); if ( is_wp_error( $result ) || $wpdb->last_error ) { $status['error'] = true; return $status; @@ -443,10 +445,11 @@ protected function set_send_full_sync_actions_status( $status, $objects ) { * * @param string $action_name The action. * @param array $data The data associated with the action. + * @param string $key The key to use for the action. */ - public function send_action( $action_name, $data = null ) { + public function send_action( $action_name, $data = null, $key = null ) { $sender = Sender::get_instance(); - return $sender->send_action( $action_name, $data ); + return $sender->send_action( $action_name, $data, $key ); } /**