2022-12-21 00:58:10 +00:00
|
|
|
<?php // @codingStandardsIgnoreLine
|
2022-11-30 01:17:27 +00:00
|
|
|
/**
|
|
|
|
* Register REST endpoint for fetching live branches manifest.
|
|
|
|
*
|
|
|
|
* @package WC_Beta_Tester
|
|
|
|
*/
|
|
|
|
|
2022-12-21 00:58:10 +00:00
|
|
|
require_once __DIR__ . '/../../includes/class-wc-beta-tester-live-branches-installer.php';
|
|
|
|
|
2022-11-30 01:17:27 +00:00
|
|
|
register_woocommerce_admin_test_helper_rest_route(
|
|
|
|
'/live-branches/manifest/v1',
|
|
|
|
'fetch_live_branches_manifest',
|
|
|
|
array(
|
|
|
|
'methods' => 'GET',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* API endpoint to fetch the manifest of live branches.
|
|
|
|
*/
|
|
|
|
function fetch_live_branches_manifest() {
|
2023-06-20 06:54:49 +00:00
|
|
|
$response = wp_remote_get( 'https://betadownload.jetpack.me/woocommerce-branches.json' );
|
|
|
|
|
|
|
|
if ( is_wp_error( $response ) ) {
|
|
|
|
// Handle the error case.
|
|
|
|
$error_message = $response->get_error_message();
|
|
|
|
return new WP_REST_Response( array( 'error' => $error_message ), 500 );
|
|
|
|
}
|
|
|
|
|
2022-12-21 00:58:10 +00:00
|
|
|
$body = wp_remote_retrieve_body( $response );
|
|
|
|
$installer = new WC_Beta_Tester_Live_Branches_Installer();
|
|
|
|
|
|
|
|
$obj = json_decode( $body );
|
|
|
|
|
2023-06-20 06:54:49 +00:00
|
|
|
if ( json_last_error() !== JSON_ERROR_NONE ) {
|
|
|
|
// Handle JSON decoding error.
|
|
|
|
return new WP_REST_Response( array( 'error' => 'Error decoding JSON' ), 500 );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the expected properties exist in the JSON.
|
|
|
|
if ( ! isset( $obj->pr ) || ! isset( $obj->master ) ) {
|
|
|
|
return new WP_REST_Response( array( 'error' => 'Missing properties in JSON' ), 500 );
|
|
|
|
}
|
|
|
|
|
2022-12-21 00:58:10 +00:00
|
|
|
foreach ( $obj->pr as $key => $value ) {
|
|
|
|
$value->install_status = $installer->check_install_status( $value->version );
|
|
|
|
}
|
2022-11-30 01:17:27 +00:00
|
|
|
|
2023-06-20 06:54:49 +00:00
|
|
|
$obj->master->install_status = $installer->check_install_status( $obj->master->version );
|
|
|
|
|
2022-12-21 00:58:10 +00:00
|
|
|
return new WP_REST_Response( $obj, 200 );
|
2022-11-30 01:17:27 +00:00
|
|
|
}
|