diff --git a/projects/packages/sync/changelog/pr-47303 b/projects/packages/sync/changelog/pr-47303 new file mode 100644 index 000000000000..bbf768b4aff0 --- /dev/null +++ b/projects/packages/sync/changelog/pr-47303 @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +REST_Endpoints: Add clear-queue endpoint to allow clearing a Sync queue via REST API. diff --git a/projects/packages/sync/src/class-rest-endpoints.php b/projects/packages/sync/src/class-rest-endpoints.php index 1bc84b08240b..10ca265f9e87 100644 --- a/projects/packages/sync/src/class-rest-endpoints.php +++ b/projects/packages/sync/src/class-rest-endpoints.php @@ -332,6 +332,17 @@ public static function initialize_rest_api() { 'permission_callback' => __CLASS__ . '::verify_default_permissions', ) ); + + // Clear Sync queue. + register_rest_route( + 'jetpack/v4', + '/sync/clear-queue', + array( + 'methods' => WP_REST_Server::EDITABLE, + 'callback' => __CLASS__ . '::clear_queue', + 'permission_callback' => __CLASS__ . '::verify_default_permissions', + ) + ); } /** @@ -810,6 +821,27 @@ public static function reset_locks() { ); } + /** + * Clear the Sync queue. + * + * @since $$next-version$$ + * + * @return \WP_REST_Response + */ + public static function clear_queue() { + $queue = new Queue( 'sync' ); + $queue->reset(); + + // Re-enable sending in case it was temporarily disabled during a pull. + delete_transient( Sender::TEMP_SYNC_DISABLE_TRANSIENT_NAME ); + + return rest_ensure_response( + array( + 'success' => true, + ) + ); + } + /** * Verify that request has default permissions to perform sync actions. * diff --git a/projects/packages/sync/tests/php/REST_Endpoints_Test.php b/projects/packages/sync/tests/php/REST_Endpoints_Test.php index 0624087224c1..079f82b594fb 100644 --- a/projects/packages/sync/tests/php/REST_Endpoints_Test.php +++ b/projects/packages/sync/tests/php/REST_Endpoints_Test.php @@ -262,6 +262,27 @@ public function test_sync_reset_locks() { $this->assertEquals( 200, $response->get_status() ); } + /** + * Testing the `POST /jetpack/v4/sync/clear-queue` endpoint clears the sync queue. + */ + public function test_sync_clear_queue() { + $user = wp_get_current_user(); + $user->add_cap( 'manage_options' ); + + set_transient( Sender::TEMP_SYNC_DISABLE_TRANSIENT_NAME, time() ); + + $request = new WP_REST_Request( 'POST', '/jetpack/v4/sync/clear-queue' ); + $request->set_header( 'Content-Type', 'application/json' ); + $response = $this->server->dispatch( $request ); + $data = $response->get_data(); + + $user->remove_cap( 'manage_options' ); + + $this->assertEquals( 200, $response->get_status() ); + $this->assertTrue( $data['success'] ); + $this->assertFalse( get_transient( Sender::TEMP_SYNC_DISABLE_TRANSIENT_NAME ) ); + } + /** * Array of Sync Endpoints and method. * @@ -283,6 +304,7 @@ public static function endpoint_provider() { array( 'sync/data-check', 'GET', null ), array( 'sync/data-histogram', 'POST', null ), array( 'sync/locks', 'DELETE', null ), + array( 'sync/clear-queue', 'POST', null ), ); } diff --git a/projects/plugins/jetpack/changelog/pr-47303 b/projects/plugins/jetpack/changelog/pr-47303 new file mode 100644 index 000000000000..ebfae8c170f2 --- /dev/null +++ b/projects/plugins/jetpack/changelog/pr-47303 @@ -0,0 +1,4 @@ +Significance: minor +Type: enhancement + +Sync: Add clear-queue REST endpoint to allow clearing a Sync queue.