Skip to content

Commit

Permalink
Jetpack Sync: Add key for full sync actions (#40566)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
darssen authored Dec 17, 2024
1 parent 5b564ca commit f16ae49
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Sync:Add specific key for full sync actions
10 changes: 6 additions & 4 deletions projects/packages/sync/src/class-sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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(),
Expand Down
9 changes: 6 additions & 3 deletions projects/packages/sync/src/modules/class-module.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
}

/**
Expand Down

0 comments on commit f16ae49

Please sign in to comment.