diff --git a/json-endpoints/jetpack/class.jetpack-json-api-sync-endpoint.php b/json-endpoints/jetpack/class.jetpack-json-api-sync-endpoint.php index 3ba54c4bf4350..6863ae927e002 100644 --- a/json-endpoints/jetpack/class.jetpack-json-api-sync-endpoint.php +++ b/json-endpoints/jetpack/class.jetpack-json-api-sync-endpoint.php @@ -52,7 +52,9 @@ protected function validate_queue( $query ) { // GET /sites/%s/sync/status class Jetpack_JSON_API_Sync_Status_Endpoint extends Jetpack_JSON_API_Sync_Endpoint { protected function result() { - return Jetpack_Sync_Actions::get_sync_status(); + $args = $this->query_args(); + $fields = isset( $args['fields'] ) ? $args['fields'] : array(); + return Jetpack_Sync_Actions::get_sync_status( $fields ); } } @@ -101,7 +103,7 @@ protected function result() { if ( is_numeric( $value ) ) { $value = (int) $value; } - + // special case for sending empty arrays - a string with value 'empty' if ( $value === 'empty' ) { $value = array(); @@ -209,12 +211,12 @@ protected function result() { } $buffer = $this->get_buffer( $queue, $args[ 'number_of_items' ] ); - + // Check that the $buffer is not checkout out already if ( is_wp_error( $buffer ) ) { return new WP_Error( 'buffer_open', "We couldn't get the buffer it is currently checked out", 400 ); } - + if ( ! is_object( $buffer ) ) { return new WP_Error( 'buffer_non-object', 'Buffer is not an object', 400 ); } diff --git a/json-endpoints/jetpack/json-api-jetpack-endpoints.php b/json-endpoints/jetpack/json-api-jetpack-endpoints.php index 0714052afc221..442e1bec72258 100644 --- a/json-endpoints/jetpack/json-api-jetpack-endpoints.php +++ b/json-endpoints/jetpack/json-api-jetpack-endpoints.php @@ -455,34 +455,43 @@ ) ); // GET /sites/%s/sync/status -new Jetpack_JSON_API_Sync_Status_Endpoint( array( - 'description' => 'Status of the current full sync or the previous full sync', - 'method' => 'GET', - 'path' => '/sites/%s/sync/status', - 'stat' => 'sync-status', - 'path_labels' => array( - '$site' => '(int|string) The site ID, The site domain' - ), - 'response_format' => array( - 'started' => '(int|null) The unix timestamp when the last sync started', - 'queue_finished' => '(int|null) The unix timestamp when the enqueuing was done for the last sync', - 'send_started' => '(int|null) The unix timestamp when the last sent process started', - 'finished' => '(int|null) The unix timestamp when the last sync finished', - 'total' => '(array) Count of actions that could be sent', - 'queue' => '(array) Count of actions that have been added to the queue', - 'sent' => '(array) Count of actions that have been sent', - 'config' => '(array) Configuration of the last full sync', - 'queue_size' => '(int) Number of items in the sync queue', - 'queue_lag' => '(float) Time delay of the oldest item in the sync queue', - 'queue_next_sync' => '(float) Time in seconds before trying to sync again', - 'full_queue_size' => '(int) Number of items in the full sync queue', - 'full_queue_lag' => '(float) Time delay of the oldest item in the full sync queue', - 'full_queue_next_sync' => '(float) Time in seconds before trying to sync the full sync queue again', - 'cron_size' => '(int) Size of the current cron array', - 'next_cron' => '(int) The number of seconds till the next item in cron.', - ), - 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/example.wordpress.org/sync/status' -) ); +new Jetpack_JSON_API_Sync_Status_Endpoint( + array( + 'description' => 'Status of the current full sync or the previous full sync', + 'method' => 'GET', + 'path' => '/sites/%s/sync/status', + 'stat' => 'sync-status', + 'path_labels' => array( + '$site' => '(int|string) The site ID, The site domain', + ), + 'query_parameters' => array( + 'fields' => '(string|null) List of comma-separated fields to return (see `response_format`).', + ), + 'response_format' => array( + 'posts_checksum' => '(string|null) Posts checksum. Needs to be requested using the filter parameter.', + 'comments_checksum' => '(string|null) Comments checksum. Needs to be requested using the filter parameter.', + 'post_meta_checksum' => '(string|null) Post Meta checksum. Needs to be requested using the filter parameter.', + 'comment_meta_checksum' => '(string|null) Comment Meta checksum. Needs to be requested using the filter parameter.', + 'started' => '(int|null) The unix timestamp when the last sync started', + 'queue_finished' => '(int|null) The unix timestamp when the enqueuing was done for the last sync', + 'send_started' => '(int|null) The unix timestamp when the last send process started', + 'finished' => '(int|null) The unix timestamp when the last sync finished', + 'total' => '(array) Count of actions that could be sent', + 'queue' => '(array) Count of actions that have been added to the queue', + 'sent' => '(array) Count of actions that have been sent', + 'config' => '(array) Configuration of the last full sync', + 'queue_size' => '(int) Number of items in the sync queue', + 'queue_lag' => '(float) Time delay of the oldest item in the sync queue', + 'queue_next_sync' => '(float) Time in seconds before trying to sync again', + 'full_queue_size' => '(int) Number of items in the full sync queue', + 'full_queue_lag' => '(float) Time delay of the oldest item in the full sync queue', + 'full_queue_next_sync' => '(float) Time in seconds before trying to sync the full sync queue again', + 'cron_size' => '(int) Size of the current cron array', + 'next_cron' => '(int) The number of seconds till the next item in cron.', + ), + 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/example.wordpress.org/sync/status', + ) +); // GET /sites/%s/data-checksums diff --git a/sync/class.jetpack-sync-actions.php b/sync/class.jetpack-sync-actions.php index 9268f78e594c5..79f14615d5ded 100644 --- a/sync/class.jetpack-sync-actions.php +++ b/sync/class.jetpack-sync-actions.php @@ -439,7 +439,13 @@ static function cleanup_on_upgrade( $new_version = null, $old_version = null ) { } } - static function get_sync_status() { + /** + * Get the sync status + * + * @param string|null $fields A comma-separated string of the fields to include in the array from the JSON response. + * @return array + */ + static function get_sync_status( $fields = null ) { self::initialize_sender(); $sync_module = Jetpack_Sync_Modules::get_module( 'full-sync' ); @@ -448,9 +454,32 @@ static function get_sync_status() { $cron_timestamps = array_keys( _get_cron_array() ); $next_cron = $cron_timestamps[0] - time(); + $checksums = array(); + + if ( ! empty( $fields ) ) { + require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-wp-replicastore.php'; + $store = new Jetpack_Sync_WP_Replicastore(); + $fields_params = array_map( 'trim', explode( ',', $fields ) ); + + if ( in_array( 'posts_checksum', $fields_params, true ) ) { + $checksums['posts_checksum'] = $store->posts_checksum(); + } + if ( in_array( 'comments_checksum', $fields_params, true ) ) { + $checksums['comments_checksum'] = $store->comments_checksum(); + } + if ( in_array( 'post_meta_checksum', $fields_params, true ) ) { + $checksums['post_meta_checksum'] = $store->post_meta_checksum(); + } + if ( in_array( 'comment_meta_checksum', $fields_params, true ) ) { + $checksums['comment_meta_checksum'] = $store->comment_meta_checksum(); + } + } + $full_sync_status = ( $sync_module ) ? $sync_module->get_status() : array(); + return array_merge( $full_sync_status, + $checksums, array( 'cron_size' => count( $cron_timestamps ), 'next_cron' => $next_cron, @@ -482,4 +511,3 @@ static function get_sync_status() { // We need to define this here so that it's hooked before `updating_jetpack_version` is called add_action( 'updating_jetpack_version', array( 'Jetpack_Sync_Actions', 'cleanup_on_upgrade' ), 10, 2 ); add_action( 'jetpack_user_authorized', array( 'Jetpack_Sync_Actions', 'do_initial_sync' ), 10, 0 ); - diff --git a/tests/php/sync/test_class.jetpack-sync-actions.php b/tests/php/sync/test_class.jetpack-sync-actions.php new file mode 100644 index 0000000000000..7f6eee25f3f79 --- /dev/null +++ b/tests/php/sync/test_class.jetpack-sync-actions.php @@ -0,0 +1,49 @@ +assertArrayNotHasKey( 'posts_checksum', $no_checksum ); + $this->assertArrayNotHasKey( 'comments_checksum', $no_checksum ); + $this->assertArrayNotHasKey( 'post_meta_checksum', $no_checksum ); + $this->assertArrayNotHasKey( 'comment_meta_checksum', $no_checksum ); + + $kitchen_sink_checksum = Jetpack_Sync_Actions::get_sync_status( + 'posts_checksum,comments_checksum,post_meta_checksum,comment_meta_checksum' + ); + $this->assertArrayHasKey( 'posts_checksum', $kitchen_sink_checksum ); + $this->assertArrayHasKey( 'comments_checksum', $kitchen_sink_checksum ); + $this->assertArrayHasKey( 'post_meta_checksum', $kitchen_sink_checksum ); + $this->assertArrayHasKey( 'comment_meta_checksum', $kitchen_sink_checksum ); + + $posts = Jetpack_Sync_Actions::get_sync_status( 'posts_checksum' ); + $this->assertArrayHasKey( 'posts_checksum', $posts ); + $this->assertArrayNotHasKey( 'comments_checksum', $posts ); + $this->assertArrayNotHasKey( 'post_meta_checksum', $posts ); + $this->assertArrayNotHasKey( 'comment_meta_checksum', $posts ); + + $comments = Jetpack_Sync_Actions::get_sync_status( + 'comments_checksum' + ); + $this->assertArrayNotHasKey( 'posts_checksum', $comments ); + $this->assertArrayHasKey( 'comments_checksum', $comments ); + $this->assertArrayNotHasKey( 'post_meta_checksum', $comments ); + $this->assertArrayNotHasKey( 'comment_meta_checksum', $comments ); + + $post_meta = Jetpack_Sync_Actions::get_sync_status( + 'post_meta_checksum' + ); + $this->assertArrayNotHasKey( 'posts_checksum', $post_meta ); + $this->assertArrayNotHasKey( 'comments_checksum', $post_meta ); + $this->assertArrayHasKey( 'post_meta_checksum', $post_meta ); + $this->assertArrayNotHasKey( 'comment_meta_checksum', $post_meta ); + + $comment_meta = Jetpack_Sync_Actions::get_sync_status( + 'comment_meta_checksum' + ); + $this->assertArrayNotHasKey( 'posts_checksum', $comment_meta ); + $this->assertArrayNotHasKey( 'comments_checksum', $comment_meta ); + $this->assertArrayNotHasKey( 'post_meta_checksum', $comment_meta ); + $this->assertArrayHasKey( 'comment_meta_checksum', $comment_meta ); + } +} diff --git a/tests/php/sync/test_class.jetpack-sync-integration.php b/tests/php/sync/test_class.jetpack-sync-integration.php index 10bb07e01f72d..f217172799c8c 100644 --- a/tests/php/sync/test_class.jetpack-sync-integration.php +++ b/tests/php/sync/test_class.jetpack-sync-integration.php @@ -1,7 +1,6 @@ factory->post->create(); $this->assertNotEmpty( $this->sender->get_sync_queue()->get_all() );