Skip to content

Commit 767e30d

Browse files
committed
Merge remote-tracking branch 'origin/main' into pluginDeactivation
2 parents 3aebb60 + 679bfeb commit 767e30d

11 files changed

+63
-185
lines changed

assets/scripts/onboarding.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ if ( window.vrts_admin_vars.onboarding ) {
6868
onCloseClick: () => {
6969
onboarding.destroy();
7070
},
71+
onDestroyed: () => {
72+
window.removeEventListener( 'keyup', onKeyup );
73+
},
7174
steps: window.vrts_admin_vars.onboarding.steps.map( ( step ) => {
7275
return {
7376
element: step.element,
@@ -81,6 +84,14 @@ if ( window.vrts_admin_vars.onboarding ) {
8184
} ),
8285
} );
8386

87+
const onKeyup = ( event ) => {
88+
if ( event.key === 'Escape' ) {
89+
onboarding.destroy();
90+
}
91+
};
92+
93+
window.addEventListener( 'keyup', onKeyup );
94+
8495
onboarding.drive();
8596
saveOnboarding();
8697
}

includes/features/class-enqueue-scripts.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,16 @@ public function enqueue_block_editor_assets() {
100100
wp_enqueue_script( 'vrts-editor' );
101101

102102
// Localize scripts.
103-
$test = (object) Test::get_item_by_post_id( $post->ID );
103+
$test_id = Test::get_item_id( $post->ID );
104+
$test = (object) Test::get_item( $test_id );
104105

105106
wp_localize_script(
106107
'vrts-editor',
107108
'vrts_editor_vars',
108109
[
109110
'plugin_name' => vrts()->get_plugin_info( 'name' ),
110111
'rest_url' => esc_url_raw( rest_url() ),
111-
'has_post_alert' => Test::has_post_alert( $post->ID ),
112+
'has_post_alert' => isset( $test->current_alert_id ) ? ! is_null( $test->current_alert_id ) : false,
112113
'base_screenshot_url' => Image_Helpers::get_screenshot_url( $test, 'base' ),
113114
'base_screenshot_date' => Date_Time_Helpers::get_formatted_date_time( $test->base_screenshot_date ?? null ),
114115
'remaining_tests' => Subscription::get_remaining_tests(),

includes/features/class-metaboxes.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,15 @@ public function add_rest_actions() {
109109
*/
110110
public function render_metabox() {
111111
global $post;
112+
112113
$post_id = $post->ID ? $post->ID : 0;
113-
$run_tests_checked = ! empty( Test::get_item_id( $post_id ) );
114+
$test_id = Test::get_item_id( $post_id );
115+
$test = (object) Test::get_item( $test_id );
114116

115-
$alert_id = Test::get_alert_id( $post_id );
117+
$run_tests_checked = ! is_null( $test_id );
118+
$alert_id = $test->current_alert_id ?? null;
116119
$testing_status_instructions = '';
120+
117121
if ( $alert_id ) {
118122
$alert_link = Url_Helpers::get_alert_page( $alert_id );
119123
$testing_status_instructions .= sprintf(
@@ -124,18 +128,15 @@ public function render_metabox() {
124128
);
125129
}
126130

127-
$test_id = Test::get_item_id( $post_id );
128-
$test = (object) Test::get_item( $test_id );
129-
130131
vrts()->component('metabox-classic-editor', [
131132
'post_id' => $post_id,
132133
'nonce' => $this->nonce,
133134
'plugin_url' => Url_Helpers::get_page_url( 'tests' ),
134135
'run_tests_checked' => $run_tests_checked,
135136
'field_test_status_key' => self::$field_test_status_key,
136-
'has_post_alert' => Test::has_post_alert( $post_id ),
137+
'has_post_alert' => isset( $test->current_alert_id ) ? ! is_null( $test->current_alert_id ) : false,
137138
'base_screenshot_url' => Image_Helpers::get_screenshot_url( $test, 'base' ),
138-
'base_screenshot_date' => Date_Time_Helpers::get_formatted_date_time( $test->base_screenshot_date ),
139+
'base_screenshot_date' => Date_Time_Helpers::get_formatted_date_time( $test->base_screenshot_date ?? null ),
139140
'testing_status_instructions' => $testing_status_instructions,
140141
'is_new_test' => self::is_new_test( $post_id ),
141142
'remaining_tests' => Subscription::get_remaining_tests(),

includes/features/class-test-runs-page.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,6 @@ public function render_page() {
9393
$run = Test_Run::get_item( $run_id );
9494

9595
if ( $run ) {
96-
$service = new Test_Run_Service();
97-
$service->update_latest_alert_for_all_tests( $run );
98-
9996
$tests = $this->prepare_tests( maybe_unserialize( $run->tests ) );
10097
$alerts = $this->prepare_alerts( $run_id, $tests );
10198

includes/models/class-alert.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -207,34 +207,6 @@ public static function get_items_by_test_run( $id = 0 ) {
207207
);
208208
}
209209

210-
/**
211-
* Get latest alert id by post id
212-
*
213-
* @param int $post_id the id of the post.
214-
* @param int $alert_state the state of the item.
215-
*
216-
* @return int
217-
*/
218-
public static function get_latest_alert_id_by_post_id( $post_id = 0, $alert_state = 0 ) {
219-
global $wpdb;
220-
221-
$alerts_table = Alerts_Table::get_table_name();
222-
223-
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- It's ok.
224-
return (int) $wpdb->get_var(
225-
$wpdb->prepare(
226-
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- It's ok.
227-
"SELECT id FROM $alerts_table
228-
WHERE alert_state = %d
229-
AND post_id = %d
230-
ORDER BY id DESC
231-
LIMIT 1",
232-
$alert_state,
233-
$post_id
234-
)
235-
);
236-
}
237-
238210
/**
239211
* Get latest alert ids by post ids
240212
*

includes/models/class-test.php

Lines changed: 32 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public static function get_items( $args = [], $return_count = false ) {
9999
tests.id,
100100
tests.status,
101101
tests.post_id,
102-
tests.current_alert_id,
102+
alerts.latest_id as current_alert_id,
103103
tests.service_test_id,
104104
tests.base_screenshot_url,
105105
tests.base_screenshot_date,
@@ -109,15 +109,15 @@ public static function get_items( $args = [], $return_count = false ) {
109109
tests.hide_css_selectors,
110110
posts.post_title,
111111
CASE
112-
WHEN tests.current_alert_id is not null THEN '6-has-alert'
112+
WHEN alerts.latest_id is not null THEN '6-has-alert'
113113
WHEN tests.service_test_id is null THEN '1-post-not-published'
114114
WHEN tests.base_screenshot_date is null THEN '2-waiting'
115115
WHEN tests.is_running > 0 THEN '3-running'
116116
WHEN tests.last_comparison_date is null THEN '4-scheduled'
117117
else '5-passed'
118118
END as calculated_status,
119119
CASE
120-
WHEN tests.current_alert_id is not null THEN alerts.target_screenshot_finish_date
120+
WHEN alerts.latest_id is not null THEN alerts.target_screenshot_finish_date
121121
WHEN tests.service_test_id is null THEN tests.base_screenshot_date
122122
WHEN tests.base_screenshot_date is null THEN tests.base_screenshot_date
123123
WHEN tests.is_running > 0 THEN tests.base_screenshot_date
@@ -126,7 +126,12 @@ public static function get_items( $args = [], $return_count = false ) {
126126
END as calculated_date
127127
FROM $tests_table as tests
128128
INNER JOIN $wpdb->posts as posts ON posts.id = tests.post_id
129-
LEFT JOIN $alerts_table as alerts ON alerts.id = tests.current_alert_id
129+
LEFT JOIN (
130+
SELECT MAX(id) as latest_id, post_id, target_screenshot_finish_date
131+
FROM $alerts_table
132+
WHERE alert_state = 0
133+
GROUP BY post_id
134+
) as alerts ON tests.post_id = alerts.post_id
130135
GROUP BY tests.id
131136
) tests
132137
$where
@@ -200,13 +205,34 @@ public static function get_item( $id = 0 ) {
200205
global $wpdb;
201206

202207
$tests_table = Tests_Table::get_table_name();
208+
$alerts_table = Alerts_Table::get_table_name();
203209

204210
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- It's ok.
205211
return $wpdb->get_row(
206212
$wpdb->prepare(
207-
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- It's ok.
208-
"SELECT * FROM $tests_table WHERE id = %d",
213+
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- It's ok.
214+
"SELECT
215+
test.id,
216+
test.status,
217+
test.post_id,
218+
alert.latest_id as current_alert_id,
219+
test.service_test_id,
220+
test.base_screenshot_url,
221+
test.base_screenshot_date,
222+
test.last_comparison_date,
223+
test.next_run_date,
224+
test.is_running,
225+
test.hide_css_selectors
226+
FROM $tests_table as test
227+
LEFT JOIN (
228+
SELECT MAX(id) as latest_id, post_id
229+
FROM $alerts_table
230+
WHERE alert_state = 0
231+
GROUP BY post_id
232+
) alert ON test.post_id = alert.post_id
233+
WHERE id = %d",
209234
$id
235+
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
210236
)
211237
);
212238
}
@@ -342,28 +368,6 @@ public static function exists_for_post( $post_id = 0 ) {
342368
);
343369
}
344370

345-
/**
346-
* Get the id of the alert
347-
*
348-
* @param int $post_id the id of the post.
349-
*
350-
* @return int
351-
*/
352-
public static function get_alert_id( $post_id = 0 ) {
353-
global $wpdb;
354-
355-
$tests_table = Tests_Table::get_table_name();
356-
357-
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- It's ok.
358-
return $wpdb->get_var(
359-
$wpdb->prepare(
360-
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- It's ok.
361-
"SELECT current_alert_id FROM $tests_table WHERE post_id = %d",
362-
$post_id
363-
)
364-
);
365-
}
366-
367371
/**
368372
* Get post id by test id
369373
*
@@ -432,30 +436,6 @@ public static function get_service_test_id_by_post_id( $post_id = 0 ) {
432436
return $service_test_id;
433437
}
434438

435-
/**
436-
* Does an alert exits?
437-
*
438-
* @param int $post_id the id of the post.
439-
*
440-
* @return boolean
441-
*/
442-
public static function has_post_alert( $post_id = 0 ) {
443-
global $wpdb;
444-
445-
$tests_table = Tests_Table::get_table_name();
446-
447-
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- It's ok.
448-
$current_alert_id = $wpdb->get_var(
449-
$wpdb->prepare(
450-
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- It's ok.
451-
"SELECT current_alert_id FROM $tests_table WHERE post_id = %d",
452-
$post_id
453-
)
454-
);
455-
456-
return null === $current_alert_id ? false : true;
457-
}
458-
459439
/**
460440
* Get total test items from database
461441
*
@@ -588,24 +568,6 @@ public static function get_all_service_test_ids() {
588568
return $wpdb->get_col( $query );
589569
}
590570

591-
/**
592-
* Set alert for a test.
593-
*
594-
* @param int $post_id The id of the post.
595-
* @param int $alert_id The id of the alert.
596-
*/
597-
public static function set_alert( $post_id = 0, $alert_id = 0 ) {
598-
global $wpdb;
599-
600-
$alert_id = 0 === $alert_id ? null : $alert_id;
601-
$tests_table = Tests_Table::get_table_name();
602-
$data = [ 'current_alert_id' => $alert_id ];
603-
$where = [ 'post_id' => $post_id ];
604-
605-
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- It's ok.
606-
return $wpdb->update( $tests_table, $data, $where );
607-
}
608-
609571
/**
610572
* Get post status
611573
*

includes/rest-api/class-rest-alerts-controller.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Vrts\Rest_Api;
44

5-
use Vrts\Services\Test_Service;
65
use WP_Error;
76
use WP_REST_Server;
87
use WP_REST_Request;
@@ -106,9 +105,6 @@ public function read_status_callback( WP_REST_Request $request ) {
106105

107106
Alert::set_alert_state( $id, $should_mark_as_read );
108107

109-
$test_service = new Test_Service();
110-
$test_service->update_latest_alert( $alert->post_id );
111-
112108
return new WP_REST_Response( true, 200 );
113109
}
114110
}

includes/rest-api/class-rest-test-runs-controller.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Vrts\Rest_Api;
44

5-
use Vrts\Services\Test_Run_Service;
65
use WP_Error;
76
use WP_REST_Server;
87
use WP_REST_Request;
@@ -72,9 +71,6 @@ public function read_status_callback( WP_REST_Request $request ) {
7271

7372
Alert::set_read_status_by_test_run( $id, $should_mark_as_read );
7473

75-
$service = new Test_Run_Service();
76-
$service->update_latest_alert_for_all_tests( $test_run );
77-
7874
return new WP_REST_Response( true, 200 );
7975
}
8076
}

includes/services/class-test-run-service.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
namespace Vrts\Services;
44

5-
use Vrts\Core\Utilities\Url_Helpers;
65
use Vrts\Features\Service;
76
use Vrts\Features\Subscription;
8-
use Vrts\Models\Alert;
97
use Vrts\Models\Test;
108
use Vrts\Models\Test_Run;
119
use Vrts\Services\Email_Service;
@@ -134,20 +132,4 @@ public function fetch_and_update_test_runs() {
134132
}
135133
}
136134
}
137-
138-
/**
139-
* Update latest alert for all tests.
140-
*
141-
* @param object $test_run Test run.
142-
*
143-
* @return void
144-
*/
145-
public function update_latest_alert_for_all_tests( $test_run ) {
146-
$tests = maybe_unserialize( $test_run->tests );
147-
148-
if ( ! empty( $tests ) ) {
149-
$service = new Test_Service();
150-
$service->update_latest_alerts( $tests );
151-
}
152-
}
153135
}

0 commit comments

Comments
 (0)