Skip to content

Commit

Permalink
feat: disconnect service on deactivation and reconnect on activation
Browse files Browse the repository at this point in the history
  • Loading branch information
domtra committed Nov 28, 2024
1 parent caa51d2 commit d317c5c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions includes/features/class-deactivate.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ public function __construct() {
*/
public function deactivate() {
Test_Run::delete_all_not_finished();
Service::disconnect_service();
}
}
21 changes: 17 additions & 4 deletions includes/features/class-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand All @@ -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'] );
Expand All @@ -77,6 +85,8 @@ private static function create_site() {
self::add_homepage_test();

return true;
} else {
update_option( 'vrts_disconnected', 1 );
}
return false;
}
Expand Down Expand Up @@ -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 );
}
}

/**
Expand All @@ -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' );
}

/**
Expand Down

0 comments on commit d317c5c

Please sign in to comment.