From d317c5c5edc66ef689c68f8a4438d479a8a0474a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Tr=C3=A4nklein?= Date: Thu, 28 Nov 2024 11:41:31 +0100 Subject: [PATCH] feat: disconnect service on deactivation and reconnect on activation --- includes/features/class-deactivate.php | 1 + includes/features/class-service.php | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/includes/features/class-deactivate.php b/includes/features/class-deactivate.php index f2a235c..ff319b3 100644 --- a/includes/features/class-deactivate.php +++ b/includes/features/class-deactivate.php @@ -18,5 +18,6 @@ public function __construct() { */ public function deactivate() { Test_Run::delete_all_not_finished(); + Service::disconnect_service(); } } diff --git a/includes/features/class-service.php b/includes/features/class-service.php index bdbc79f..952c2fb 100644 --- a/includes/features/class-service.php +++ b/includes/features/class-service.php @@ -48,7 +48,7 @@ public static function retry_connection() { * @return bool */ private static function create_site() { - if ( ! empty( get_option( 'vrts_project_id' ) ) || ! empty( get_option( 'vrts_project_token' ) ) ) { + if ( static::is_connected() ) { return; } $time = current_time( 'mysql' ); @@ -63,9 +63,17 @@ private static function create_site() { 'requested_at' => $time, ]; + if ( ! empty( get_option( 'vrts_project_id' ) ) && ! empty( get_option( 'vrts_project_token' ) ) ) { + $parameters['project_id'] = get_option( 'vrts_project_id' ); + $parameters['project_token'] = get_option( 'vrts_project_token' ); + $parameters['project_secret'] = get_option( 'vrts_project_secret' ); + $parameters['tests'] = Test::get_all_service_test_ids(); + } + $service_request = self::rest_service_request( $service_api_route, $parameters, 'post' ); - if ( 201 === $service_request['status_code'] ) { + delete_option( 'vrts_disconnected' ); + if ( 201 === $service_request['status_code'] || 200 === $service_request['status_code'] ) { $data = $service_request['response']; update_option( 'vrts_project_id', $data['id'] ); @@ -77,6 +85,8 @@ private static function create_site() { self::add_homepage_test(); return true; + } else { + update_option( 'vrts_disconnected', 1 ); } return false; } @@ -311,7 +321,10 @@ public static function fetch_updates() { public static function disconnect_service() { $service_project_id = get_option( 'vrts_project_id' ); $service_api_route = 'sites/' . $service_project_id; - self::rest_service_request( $service_api_route, [], 'delete' ); + $response = self::rest_service_request( $service_api_route, [], 'delete' ); + if ( 200 === $response['status_code'] ) { + update_option( 'vrts_disconnected', 1 ); + } } /** @@ -333,7 +346,7 @@ public static function delete_option() { * Check if external service was able to connect */ public static function is_connected() { - return (bool) get_option( 'vrts_project_id' ) && (bool) get_option( 'vrts_project_token' ); + return ! (bool) get_option( 'vrts_disconnected' ) && (bool) get_option( 'vrts_project_id' ) && (bool) get_option( 'vrts_project_token' ); } /**