Skip to content

Commit

Permalink
Backup: Add backup preflight endpoint in backup package and js-package (
Browse files Browse the repository at this point in the history
#36032)

* Add rewind/preflight endpoint on backup package

* Map backup/preflight API on js-packages API

* Add fetchBackupPreflightStatus test

* changelog

* Update jetpack-api package version

* Return more error details
  • Loading branch information
Initsogar committed Feb 28, 2024
1 parent a833fbb commit b4a2e25
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Add endpoint to query backup preflight checks
4 changes: 4 additions & 0 deletions projects/js-packages/api/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,10 @@ function JetpackRestApiClient( root, nonce ) {
getRequest( `${ apiRoot }jetpack/v4/site/backup/undo-event`, getParams )
.then( checkStatus )
.then( parseJsonResponse ),
fetchBackupPreflightStatus: () =>
getRequest( `${ apiRoot }jetpack/v4/site/backup/preflight`, getParams )
.then( checkStatus )
.then( parseJsonResponse ),
};

/**
Expand Down
2 changes: 1 addition & 1 deletion projects/js-packages/api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@automattic/jetpack-api",
"version": "0.16.10",
"version": "0.17.0-alpha",
"description": "Jetpack Api Package",
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/js-packages/api/#readme",
"bugs": {
Expand Down
11 changes: 11 additions & 0 deletions projects/js-packages/api/test/rest-api.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,16 @@ describe( 'restApi', () => {
method: 'post',
} );
} );

it( 'can fetchBackupPreflightStatus', async () => {
fetch.mockFetchResponse( { ok: true, tests: [] } );
const preflightStatus = await restApi.fetchBackupPreflightStatus();
expect( preflightStatus ).toEqual( { ok: true, tests: [] } );
expect( fetch ).toHaveBeenCalledTimes( 1 );
expect( fetch ).toHaveBeenCalledWith(
'/fakeApiRoot/jetpack/v4/site/backup/preflight?_cacheBuster=1234',
{ credentials: 'same-origin', headers: { 'X-WP-Nonce': undefined } }
);
} );
} );
} );
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Add endpoint to query backup preflight checks
48 changes: 48 additions & 0 deletions projects/packages/backup/src/class-rest-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,17 @@ public static function register_rest_routes() {
'permission_callback' => __CLASS__ . '::backup_permissions_callback',
)
);

// Fetch backup preflight status
register_rest_route(
'jetpack/v4',
'/site/backup/preflight',
array(
'methods' => WP_REST_Server::READABLE,
'callback' => __CLASS__ . '::get_site_backup_preflight',
'permission_callback' => __NAMESPACE__ . '\Jetpack_Backup::backups_permissions_callback',
)
);
}

/**
Expand Down Expand Up @@ -688,6 +699,43 @@ public static function fetch_wc_orders_backup( $request ) {
);
}

/**
* Fetch backup preflight status
*
* @return array
*/
public static function get_site_backup_preflight() {
$blog_id = Jetpack_Options::get_option( 'id' );

$response = Client::wpcom_json_api_request_as_user(
'/sites/' . $blog_id . '/rewind/preflight?force=wpcom',
'v2',
array(),
null,
'wpcom'
);

if ( is_wp_error( $response ) ) {
return new WP_Error(
'wp_error_fetch_preflight',
$response->get_error_message(),
array( 'status' => 500 )
);
}

$response_code = wp_remote_retrieve_response_code( $response );
if ( 200 !== $response_code ) {
return new WP_Error(
'http_error_fetch_preflight',
wp_remote_retrieve_response_message( $response ),
array( 'status' => $response_code )
);
}

$body = json_decode( $response['body'], true );
return rest_ensure_response( $body );
}

/**
* Fetch option row by option name.
*
Expand Down

0 comments on commit b4a2e25

Please sign in to comment.