Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into pluginDeactivation
Browse files Browse the repository at this point in the history
  • Loading branch information
harunbleech committed Nov 28, 2024
2 parents 3aebb60 + 679bfeb commit 767e30d
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 185 deletions.
11 changes: 11 additions & 0 deletions assets/scripts/onboarding.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ if ( window.vrts_admin_vars.onboarding ) {
onCloseClick: () => {
onboarding.destroy();
},
onDestroyed: () => {
window.removeEventListener( 'keyup', onKeyup );
},
steps: window.vrts_admin_vars.onboarding.steps.map( ( step ) => {
return {
element: step.element,
Expand All @@ -81,6 +84,14 @@ if ( window.vrts_admin_vars.onboarding ) {
} ),
} );

const onKeyup = ( event ) => {
if ( event.key === 'Escape' ) {
onboarding.destroy();
}
};

window.addEventListener( 'keyup', onKeyup );

onboarding.drive();
saveOnboarding();
}
Expand Down
5 changes: 3 additions & 2 deletions includes/features/class-enqueue-scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,16 @@ public function enqueue_block_editor_assets() {
wp_enqueue_script( 'vrts-editor' );

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

wp_localize_script(
'vrts-editor',
'vrts_editor_vars',
[
'plugin_name' => vrts()->get_plugin_info( 'name' ),
'rest_url' => esc_url_raw( rest_url() ),
'has_post_alert' => Test::has_post_alert( $post->ID ),
'has_post_alert' => isset( $test->current_alert_id ) ? ! is_null( $test->current_alert_id ) : false,
'base_screenshot_url' => Image_Helpers::get_screenshot_url( $test, 'base' ),
'base_screenshot_date' => Date_Time_Helpers::get_formatted_date_time( $test->base_screenshot_date ?? null ),
'remaining_tests' => Subscription::get_remaining_tests(),
Expand Down
15 changes: 8 additions & 7 deletions includes/features/class-metaboxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,15 @@ public function add_rest_actions() {
*/
public function render_metabox() {
global $post;

$post_id = $post->ID ? $post->ID : 0;
$run_tests_checked = ! empty( Test::get_item_id( $post_id ) );
$test_id = Test::get_item_id( $post_id );
$test = (object) Test::get_item( $test_id );

$alert_id = Test::get_alert_id( $post_id );
$run_tests_checked = ! is_null( $test_id );
$alert_id = $test->current_alert_id ?? null;
$testing_status_instructions = '';

if ( $alert_id ) {
$alert_link = Url_Helpers::get_alert_page( $alert_id );
$testing_status_instructions .= sprintf(
Expand All @@ -124,18 +128,15 @@ public function render_metabox() {
);
}

$test_id = Test::get_item_id( $post_id );
$test = (object) Test::get_item( $test_id );

vrts()->component('metabox-classic-editor', [
'post_id' => $post_id,
'nonce' => $this->nonce,
'plugin_url' => Url_Helpers::get_page_url( 'tests' ),
'run_tests_checked' => $run_tests_checked,
'field_test_status_key' => self::$field_test_status_key,
'has_post_alert' => Test::has_post_alert( $post_id ),
'has_post_alert' => isset( $test->current_alert_id ) ? ! is_null( $test->current_alert_id ) : false,
'base_screenshot_url' => Image_Helpers::get_screenshot_url( $test, 'base' ),
'base_screenshot_date' => Date_Time_Helpers::get_formatted_date_time( $test->base_screenshot_date ),
'base_screenshot_date' => Date_Time_Helpers::get_formatted_date_time( $test->base_screenshot_date ?? null ),
'testing_status_instructions' => $testing_status_instructions,
'is_new_test' => self::is_new_test( $post_id ),
'remaining_tests' => Subscription::get_remaining_tests(),
Expand Down
3 changes: 0 additions & 3 deletions includes/features/class-test-runs-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,6 @@ public function render_page() {
$run = Test_Run::get_item( $run_id );

if ( $run ) {
$service = new Test_Run_Service();
$service->update_latest_alert_for_all_tests( $run );

$tests = $this->prepare_tests( maybe_unserialize( $run->tests ) );
$alerts = $this->prepare_alerts( $run_id, $tests );

Expand Down
28 changes: 0 additions & 28 deletions includes/models/class-alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,34 +207,6 @@ public static function get_items_by_test_run( $id = 0 ) {
);
}

/**
* Get latest alert id by post id
*
* @param int $post_id the id of the post.
* @param int $alert_state the state of the item.
*
* @return int
*/
public static function get_latest_alert_id_by_post_id( $post_id = 0, $alert_state = 0 ) {
global $wpdb;

$alerts_table = Alerts_Table::get_table_name();

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- It's ok.
return (int) $wpdb->get_var(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- It's ok.
"SELECT id FROM $alerts_table
WHERE alert_state = %d
AND post_id = %d
ORDER BY id DESC
LIMIT 1",
$alert_state,
$post_id
)
);
}

/**
* Get latest alert ids by post ids
*
Expand Down
102 changes: 32 additions & 70 deletions includes/models/class-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static function get_items( $args = [], $return_count = false ) {
tests.id,
tests.status,
tests.post_id,
tests.current_alert_id,
alerts.latest_id as current_alert_id,
tests.service_test_id,
tests.base_screenshot_url,
tests.base_screenshot_date,
Expand All @@ -109,15 +109,15 @@ public static function get_items( $args = [], $return_count = false ) {
tests.hide_css_selectors,
posts.post_title,
CASE
WHEN tests.current_alert_id is not null THEN '6-has-alert'
WHEN alerts.latest_id is not null THEN '6-has-alert'
WHEN tests.service_test_id is null THEN '1-post-not-published'
WHEN tests.base_screenshot_date is null THEN '2-waiting'
WHEN tests.is_running > 0 THEN '3-running'
WHEN tests.last_comparison_date is null THEN '4-scheduled'
else '5-passed'
END as calculated_status,
CASE
WHEN tests.current_alert_id is not null THEN alerts.target_screenshot_finish_date
WHEN alerts.latest_id is not null THEN alerts.target_screenshot_finish_date
WHEN tests.service_test_id is null THEN tests.base_screenshot_date
WHEN tests.base_screenshot_date is null THEN tests.base_screenshot_date
WHEN tests.is_running > 0 THEN tests.base_screenshot_date
Expand All @@ -126,7 +126,12 @@ public static function get_items( $args = [], $return_count = false ) {
END as calculated_date
FROM $tests_table as tests
INNER JOIN $wpdb->posts as posts ON posts.id = tests.post_id
LEFT JOIN $alerts_table as alerts ON alerts.id = tests.current_alert_id
LEFT JOIN (
SELECT MAX(id) as latest_id, post_id, target_screenshot_finish_date
FROM $alerts_table
WHERE alert_state = 0
GROUP BY post_id
) as alerts ON tests.post_id = alerts.post_id
GROUP BY tests.id
) tests
$where
Expand Down Expand Up @@ -200,13 +205,34 @@ public static function get_item( $id = 0 ) {
global $wpdb;

$tests_table = Tests_Table::get_table_name();
$alerts_table = Alerts_Table::get_table_name();

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- It's ok.
return $wpdb->get_row(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- It's ok.
"SELECT * FROM $tests_table WHERE id = %d",
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- It's ok.
"SELECT
test.id,
test.status,
test.post_id,
alert.latest_id as current_alert_id,
test.service_test_id,
test.base_screenshot_url,
test.base_screenshot_date,
test.last_comparison_date,
test.next_run_date,
test.is_running,
test.hide_css_selectors
FROM $tests_table as test
LEFT JOIN (
SELECT MAX(id) as latest_id, post_id
FROM $alerts_table
WHERE alert_state = 0
GROUP BY post_id
) alert ON test.post_id = alert.post_id
WHERE id = %d",
$id
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
)
);
}
Expand Down Expand Up @@ -342,28 +368,6 @@ public static function exists_for_post( $post_id = 0 ) {
);
}

/**
* Get the id of the alert
*
* @param int $post_id the id of the post.
*
* @return int
*/
public static function get_alert_id( $post_id = 0 ) {
global $wpdb;

$tests_table = Tests_Table::get_table_name();

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- It's ok.
return $wpdb->get_var(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- It's ok.
"SELECT current_alert_id FROM $tests_table WHERE post_id = %d",
$post_id
)
);
}

/**
* Get post id by test id
*
Expand Down Expand Up @@ -432,30 +436,6 @@ public static function get_service_test_id_by_post_id( $post_id = 0 ) {
return $service_test_id;
}

/**
* Does an alert exits?
*
* @param int $post_id the id of the post.
*
* @return boolean
*/
public static function has_post_alert( $post_id = 0 ) {
global $wpdb;

$tests_table = Tests_Table::get_table_name();

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- It's ok.
$current_alert_id = $wpdb->get_var(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- It's ok.
"SELECT current_alert_id FROM $tests_table WHERE post_id = %d",
$post_id
)
);

return null === $current_alert_id ? false : true;
}

/**
* Get total test items from database
*
Expand Down Expand Up @@ -588,24 +568,6 @@ public static function get_all_service_test_ids() {
return $wpdb->get_col( $query );
}

/**
* Set alert for a test.
*
* @param int $post_id The id of the post.
* @param int $alert_id The id of the alert.
*/
public static function set_alert( $post_id = 0, $alert_id = 0 ) {
global $wpdb;

$alert_id = 0 === $alert_id ? null : $alert_id;
$tests_table = Tests_Table::get_table_name();
$data = [ 'current_alert_id' => $alert_id ];
$where = [ 'post_id' => $post_id ];

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- It's ok.
return $wpdb->update( $tests_table, $data, $where );
}

/**
* Get post status
*
Expand Down
4 changes: 0 additions & 4 deletions includes/rest-api/class-rest-alerts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Vrts\Rest_Api;

use Vrts\Services\Test_Service;
use WP_Error;
use WP_REST_Server;
use WP_REST_Request;
Expand Down Expand Up @@ -106,9 +105,6 @@ public function read_status_callback( WP_REST_Request $request ) {

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

$test_service = new Test_Service();
$test_service->update_latest_alert( $alert->post_id );

return new WP_REST_Response( true, 200 );
}
}
4 changes: 0 additions & 4 deletions includes/rest-api/class-rest-test-runs-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Vrts\Rest_Api;

use Vrts\Services\Test_Run_Service;
use WP_Error;
use WP_REST_Server;
use WP_REST_Request;
Expand Down Expand Up @@ -72,9 +71,6 @@ public function read_status_callback( WP_REST_Request $request ) {

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

$service = new Test_Run_Service();
$service->update_latest_alert_for_all_tests( $test_run );

return new WP_REST_Response( true, 200 );
}
}
18 changes: 0 additions & 18 deletions includes/services/class-test-run-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

namespace Vrts\Services;

use Vrts\Core\Utilities\Url_Helpers;
use Vrts\Features\Service;
use Vrts\Features\Subscription;
use Vrts\Models\Alert;
use Vrts\Models\Test;
use Vrts\Models\Test_Run;
use Vrts\Services\Email_Service;
Expand Down Expand Up @@ -134,20 +132,4 @@ public function fetch_and_update_test_runs() {
}
}
}

/**
* Update latest alert for all tests.
*
* @param object $test_run Test run.
*
* @return void
*/
public function update_latest_alert_for_all_tests( $test_run ) {
$tests = maybe_unserialize( $test_run->tests );

if ( ! empty( $tests ) ) {
$service = new Test_Service();
$service->update_latest_alerts( $tests );
}
}
}
Loading

0 comments on commit 767e30d

Please sign in to comment.