@@ -48,7 +48,7 @@ public static function retry_connection() {
48
48
* @return bool
49
49
*/
50
50
private static function create_site () {
51
- if ( ! empty ( get_option ( ' vrts_project_id ' ) ) || ! empty ( get_option ( ' vrts_project_token ' ) ) ) {
51
+ if ( static :: is_connected ( ) ) {
52
52
return ;
53
53
}
54
54
$ time = current_time ( 'mysql ' );
@@ -63,9 +63,17 @@ private static function create_site() {
63
63
'requested_at ' => $ time ,
64
64
];
65
65
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
+
66
73
$ service_request = self ::rest_service_request ( $ service_api_route , $ parameters , 'post ' );
67
74
68
- if ( 201 === $ service_request ['status_code ' ] ) {
75
+ delete_option ( 'vrts_disconnected ' );
76
+ if ( 201 === $ service_request ['status_code ' ] || 200 === $ service_request ['status_code ' ] ) {
69
77
$ data = $ service_request ['response ' ];
70
78
71
79
update_option ( 'vrts_project_id ' , $ data ['id ' ] );
@@ -77,6 +85,8 @@ private static function create_site() {
77
85
self ::add_homepage_test ();
78
86
79
87
return true ;
88
+ } else {
89
+ update_option ( 'vrts_disconnected ' , 1 );
80
90
}
81
91
return false ;
82
92
}
@@ -204,6 +214,15 @@ public static function update_test( $service_test_id, $data ) {
204
214
return 200 === $ response ['status_code ' ];
205
215
}
206
216
217
+ /**
218
+ * Send request to server to resume tests.
219
+ */
220
+ public static function resume_tests () {
221
+ $ service_project_id = get_option ( 'vrts_project_id ' );
222
+ $ service_api_route = 'sites/ ' . $ service_project_id ;
223
+ self ::rest_service_request ( $ service_api_route . '/resume ' , [], 'post ' );
224
+ }
225
+
207
226
/**
208
227
* Add homepage as a default test.
209
228
*/
@@ -296,13 +315,28 @@ public static function fetch_updates() {
296
315
return self ::rest_service_request ( $ service_api_route , [], 'get ' );
297
316
}
298
317
318
+ /**
319
+ * Get test runs from the service.
320
+ *
321
+ * @param string[] $test_run_ids the test run ids.
322
+ */
323
+ public static function fetch_test_runs ( $ test_run_ids ) {
324
+ $ service_project_id = get_option ( 'vrts_project_id ' );
325
+ $ service_api_route = 'sites/ ' . $ service_project_id . '/runs ' ;
326
+ $ service_api_route = add_query_arg ( 'ids ' , implode ( ', ' , $ test_run_ids ), $ service_api_route );
327
+ return self ::rest_service_request ( $ service_api_route , [], 'get ' );
328
+ }
329
+
299
330
/**
300
331
* Delete project from the service.
301
332
*/
302
333
public static function disconnect_service () {
303
334
$ service_project_id = get_option ( 'vrts_project_id ' );
304
335
$ service_api_route = 'sites/ ' . $ service_project_id ;
305
- self ::rest_service_request ( $ service_api_route , [], 'delete ' );
336
+ $ response = self ::rest_service_request ( $ service_api_route , [], 'delete ' );
337
+ if ( 200 === $ response ['status_code ' ] ) {
338
+ update_option ( 'vrts_disconnected ' , 1 );
339
+ }
306
340
}
307
341
308
342
/**
@@ -324,7 +358,7 @@ public static function delete_option() {
324
358
* Check if external service was able to connect
325
359
*/
326
360
public static function is_connected () {
327
- return (bool ) get_option ( 'vrts_project_id ' ) && (bool ) get_option ( 'vrts_project_token ' );
361
+ return ! ( bool ) get_option ( ' vrts_disconnected ' ) && (bool ) get_option ( 'vrts_project_id ' ) && (bool ) get_option ( 'vrts_project_token ' );
328
362
}
329
363
330
364
/**
0 commit comments