Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions projects/packages/sync/changelog/pr-47303
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

REST_Endpoints: Add clear-queue endpoint to allow clearing a Sync queue via REST API.
32 changes: 32 additions & 0 deletions projects/packages/sync/src/class-rest-endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)
);
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down
22 changes: 22 additions & 0 deletions projects/packages/sync/tests/php/REST_Endpoints_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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 ),
);
}

Expand Down
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/changelog/pr-47303
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: enhancement

Sync: Add clear-queue REST endpoint to allow clearing a Sync queue.
Loading