@@ -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 *
0 commit comments