Skip to content

Commit c666062

Browse files
authored
Merge pull request #74 from bleech/pluginDeactivation
feat: disconnect service on deactivation and reconnect on activation
2 parents caa51d2 + d317c5c commit c666062

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

includes/features/class-deactivate.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ public function __construct() {
1818
*/
1919
public function deactivate() {
2020
Test_Run::delete_all_not_finished();
21+
Service::disconnect_service();
2122
}
2223
}

includes/features/class-service.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static function retry_connection() {
4848
* @return bool
4949
*/
5050
private static function create_site() {
51-
if ( ! empty( get_option( 'vrts_project_id' ) ) || ! empty( get_option( 'vrts_project_token' ) ) ) {
51+
if ( static::is_connected() ) {
5252
return;
5353
}
5454
$time = current_time( 'mysql' );
@@ -63,9 +63,17 @@ private static function create_site() {
6363
'requested_at' => $time,
6464
];
6565

66+
if ( ! empty( get_option( 'vrts_project_id' ) ) && ! empty( get_option( 'vrts_project_token' ) ) ) {
67+
$parameters['project_id'] = get_option( 'vrts_project_id' );
68+
$parameters['project_token'] = get_option( 'vrts_project_token' );
69+
$parameters['project_secret'] = get_option( 'vrts_project_secret' );
70+
$parameters['tests'] = Test::get_all_service_test_ids();
71+
}
72+
6673
$service_request = self::rest_service_request( $service_api_route, $parameters, 'post' );
6774

68-
if ( 201 === $service_request['status_code'] ) {
75+
delete_option( 'vrts_disconnected' );
76+
if ( 201 === $service_request['status_code'] || 200 === $service_request['status_code'] ) {
6977
$data = $service_request['response'];
7078

7179
update_option( 'vrts_project_id', $data['id'] );
@@ -77,6 +85,8 @@ private static function create_site() {
7785
self::add_homepage_test();
7886

7987
return true;
88+
} else {
89+
update_option( 'vrts_disconnected', 1 );
8090
}
8191
return false;
8292
}
@@ -311,7 +321,10 @@ public static function fetch_updates() {
311321
public static function disconnect_service() {
312322
$service_project_id = get_option( 'vrts_project_id' );
313323
$service_api_route = 'sites/' . $service_project_id;
314-
self::rest_service_request( $service_api_route, [], 'delete' );
324+
$response = self::rest_service_request( $service_api_route, [], 'delete' );
325+
if ( 200 === $response['status_code'] ) {
326+
update_option( 'vrts_disconnected', 1 );
327+
}
315328
}
316329

317330
/**
@@ -333,7 +346,7 @@ public static function delete_option() {
333346
* Check if external service was able to connect
334347
*/
335348
public static function is_connected() {
336-
return (bool) get_option( 'vrts_project_id' ) && (bool) get_option( 'vrts_project_token' );
349+
return ! (bool) get_option( 'vrts_disconnected' ) && (bool) get_option( 'vrts_project_id' ) && (bool) get_option( 'vrts_project_token' );
337350
}
338351

339352
/**

0 commit comments

Comments
 (0)