Skip to content

Commit

Permalink
Full Sync: Avoid sending actions when expanded objects create empty a…
Browse files Browse the repository at this point in the history
…rrays (#41183)

* Change to not send actions if get_next_chunk is overriden and objects are empty (only posts and comments for the moment)

* changelog

* Don't update chunks_sent if action is not sent

* Keep legacy implementation for comments

* Removed include_unapproved to true since it is not a boolean

* Used fetched comment ids to get the metadata from the fetched comments

Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/12924307484

Upstream-Ref: Automattic/jetpack@01e9cd1
  • Loading branch information
darssen authored and matticbot committed Jan 23, 2025
1 parent 26a6d73 commit 7397e3a
Show file tree
Hide file tree
Showing 13 changed files with 231 additions and 147 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"automattic/jetpack-config": "^3.0.0",
"automattic/jetpack-my-jetpack": "^5.4.0-alpha",
"automattic/jetpack-plugins-installer": "^0.5.0",
"automattic/jetpack-sync": "^4.4.1-alpha",
"automattic/jetpack-sync": "^4.5.0-alpha",
"automattic/jetpack-transport-helper": "^0.3.0",
"automattic/jetpack-plans": "^0.5.1",
"automattic/jetpack-waf": "^0.23.2",
Expand Down
2 changes: 1 addition & 1 deletion jetpack_vendor/automattic/jetpack-my-jetpack/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"automattic/jetpack-constants": "^3.0.1",
"automattic/jetpack-plans": "^0.5.1",
"automattic/jetpack-status": "^5.0.2",
"automattic/jetpack-sync": "^4.4.1-alpha",
"automattic/jetpack-sync": "^4.5.0-alpha",
"automattic/jetpack-protect-status": "^0.4.2"
},
"require-dev": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"php": ">=7.2",
"automattic/jetpack-connection": "^6.3.0",
"automattic/jetpack-plugins-installer": "^0.5.0",
"automattic/jetpack-sync": "^4.4.1-alpha",
"automattic/jetpack-sync": "^4.5.0-alpha",
"automattic/jetpack-protect-models": "^0.4.1",
"automattic/jetpack-plans": "^0.5.1"
},
Expand Down
7 changes: 5 additions & 2 deletions jetpack_vendor/automattic/jetpack-sync/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [4.4.1-alpha] - unreleased
## [4.5.0-alpha] - unreleased

This is an alpha version! The changes listed here are not final.

### Changed
- Sync: Full sync doesn't send actions for posts and comments with no items

## [4.4.0] - 2025-01-20
### Added
- Add context for full sync. [#40930]
Expand Down Expand Up @@ -1378,7 +1381,7 @@ This is an alpha version! The changes listed here are not final.

- Packages: Move sync to a classmapped package

[4.4.1-alpha]: https://github.com/Automattic/jetpack-sync/compare/v4.4.0...v4.4.1-alpha
[4.5.0-alpha]: https://github.com/Automattic/jetpack-sync/compare/v4.4.0...v4.5.0-alpha
[4.4.0]: https://github.com/Automattic/jetpack-sync/compare/v4.3.0...v4.4.0
[4.3.0]: https://github.com/Automattic/jetpack-sync/compare/v4.2.0...v4.3.0
[4.2.0]: https://github.com/Automattic/jetpack-sync/compare/v4.1.1...v4.2.0
Expand Down
2 changes: 1 addition & 1 deletion jetpack_vendor/automattic/jetpack-sync/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"link-template": "https://github.com/Automattic/jetpack-sync/compare/v${old}...v${new}"
},
"branch-alias": {
"dev-trunk": "4.4.x-dev"
"dev-trunk": "4.5.x-dev"
},
"dependencies": {
"test-only": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
class Package_Version {

const PACKAGE_VERSION = '4.4.1-alpha';
const PACKAGE_VERSION = '4.5.0-alpha';

const PACKAGE_SLUG = 'sync';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,14 @@ public function is_comment_type_allowed( $comment_id ) {
* @access public
*/
public function init_before_send() {

// Full sync.
add_filter( 'jetpack_sync_before_send_jetpack_full_sync_comments', array( $this, 'expand_comment_ids' ) );
$sync_module = Modules::get_module( 'full-sync' );
if ( $sync_module instanceof Full_Sync_Immediately ) {
add_filter( 'jetpack_sync_before_send_jetpack_full_sync_comments', array( $this, 'extract_comments_and_meta' ) );
} else {
add_filter( 'jetpack_sync_before_send_jetpack_full_sync_comments', array( $this, 'expand_comment_ids' ) );
}
}

/**
Expand Down Expand Up @@ -515,4 +521,74 @@ public function expand_comment_ids( $args ) {
$previous_interval_end,
);
}

/**
* Expand the comment IDs to comment objects and meta before being serialized and sent to the server.
*
* @access public
*
* @param array $args The hook parameters.
* @return array The expanded hook parameters.
*/
public function extract_comments_and_meta( $args ) {
list( $filtered_comments, $previous_end ) = $args;
return array(
$filtered_comments['objects'],
$filtered_comments['meta'],
$previous_end,
);
}

/**
* Given the Module Configuration and Status return the next chunk of items to send.
* This function also expands the posts and metadata and filters them based on the maximum size constraints.
*
* @param array $config This module Full Sync configuration.
* @param array $status This module Full Sync status.
* @param int $chunk_size Chunk size.
*
* @return array
*/
public function get_next_chunk( $config, $status, $chunk_size ) {

$comment_ids = parent::get_next_chunk( $config, $status, $chunk_size );
if ( empty( $comment_ids ) ) {
return array();
}
$comments = get_comments(
array(
'comment__in' => $comment_ids,
'orderby' => 'comment_ID',
'order' => 'DESC',
)
);
if ( empty( $comments ) ) {
return array();
}
// Get the comment IDs from the comments that were fetched.
$fetched_comment_ids = wp_list_pluck( $comments, 'comment_ID' );
return array(
'object_ids' => $comment_ids, // Still send the original comment IDs since we need them to update the status.
'objects' => $comments,
'meta' => $this->get_metadata( $fetched_comment_ids, 'comment', Settings::get_setting( 'comment_meta_whitelist' ) ),
);
}

/**
* Set the status of the full sync action based on the objects that were sent.
*
* @access public
*
* @param array $status This module Full Sync status.
* @param array $objects This module Full Sync objects.
*
* @return array The updated status.
*/
public function set_send_full_sync_actions_status( $status, $objects ) {

$object_ids = $objects['object_ids'];
$status['last_sent'] = end( $object_ids );
$status['sent'] += count( $object_ids );
return $status;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -405,20 +405,24 @@ public function send_full_sync_actions( $config, $status, $send_until ) {
$status['finished'] = true;
return $status;
}
$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;
// If we have objects as a key it means get_next_chunk is being overridden, we need to check for it being an empty array.
// In case it is an empty array, we should not send the action or increase the chunks_sent, we just need to update the status.
if ( ! isset( $objects['objects'] ) || array() !== $objects['objects'] ) {
$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;
}
++$chunks_sent;
}

// Updated the sent and last_sent status.
$status = $this->set_send_full_sync_actions_status( $status, $objects );
if ( $last_item === $status['last_sent'] ) {
$status['finished'] = true;
return $status;
}
++$chunks_sent;
}

return $status;
Expand Down
21 changes: 11 additions & 10 deletions jetpack_vendor/automattic/jetpack-sync/src/modules/class-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -809,13 +809,12 @@ public function send_published( $post_ID, $post ) {
* @return array $args The expanded hook parameters.
*/
public function add_term_relationships( $args ) {
list( $filtered_posts, $previous_interval_end ) = $args;
list( $filtered_post_ids, $filtered_posts, $filtered_posts_metadata ) = $filtered_posts;
list( $filtered_posts, $previous_interval_end ) = $args;

return array(
$filtered_posts,
$filtered_posts_metadata,
$this->get_term_relationships( $filtered_post_ids ),
$filtered_posts['objects'],
$filtered_posts['meta'],
$this->get_term_relationships( $filtered_posts['object_ids'] ),
$previous_interval_end,
);
}
Expand Down Expand Up @@ -882,9 +881,9 @@ public function get_next_chunk( $config, $status, $chunk_size ) {
// Filter posts and metadata based on maximum size constraints.
list( $filtered_post_ids, $filtered_posts, $filtered_posts_metadata ) = $this->filter_posts_and_metadata_max_size( $posts, $posts_metadata );
return array(
$filtered_post_ids,
$filtered_posts,
$filtered_posts_metadata,
'object_ids' => $filtered_post_ids,
'objects' => $filtered_posts,
'meta' => $filtered_posts_metadata,
);
}

Expand Down Expand Up @@ -964,8 +963,10 @@ public function filter_posts_and_metadata_max_size( $posts, $metadata ) {
* @return array The updated status.
*/
public function set_send_full_sync_actions_status( $status, $objects ) {
$status['last_sent'] = end( $objects[0] );
$status['sent'] += count( $objects[0] );

$object_ids = $objects['object_ids'];
$status['last_sent'] = end( $object_ids );
$status['sent'] += count( $object_ids );
return $status;
}
}
2 changes: 1 addition & 1 deletion jetpack_vendor/i18n-map.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
),
'jetpack-sync' => array(
'path' => 'jetpack_vendor/automattic/jetpack-sync',
'ver' => '4.4.1-alpha1737534132',
'ver' => '4.5.0-alpha1737617284',
),
'jetpack-transport-helper' => array(
'path' => 'jetpack_vendor/automattic/jetpack-transport-helper',
Expand Down
Loading

0 comments on commit 7397e3a

Please sign in to comment.