Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into testStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
domtra committed Dec 12, 2023
2 parents 41eca0d + afe1a30 commit 6eb41a6
Show file tree
Hide file tree
Showing 17 changed files with 374 additions and 41 deletions.
4 changes: 4 additions & 0 deletions components/admin-notification/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
$template = dirname( __FILE__ ) . '/views/admin-notification-new-test-failed.php';
break;

case 'new_tests_added':
$template = dirname( __FILE__ ) . '/views/admin-notification-new-tests-added.php';
break;

case 'plugin_activated':
$template = dirname( __FILE__ ) . '/views/admin-notification-plugin-activated.php';
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div class="vrts-notice notice notice-success" data-view="<?php echo esc_attr( $data['view'] ); ?>">
<?php wp_nonce_field( 'vrts_admin_notice_nonce' ); ?>
<h3><?php esc_html_e( 'Tests added successfully', 'visual-regression-tests' ); ?></h3>
<p>
<?php
printf(
/* translators: %s: the title of the page. */
esc_html__( 'Tests will be run for the following pages: %s', 'visual-regression-tests' ),
esc_html( $data['page_titles'] )
);
?>
</p>
</div>
2 changes: 1 addition & 1 deletion components/tests-page/views/tests-page-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<li>
<form method="post" id="form-run-manual-tests">
<?php wp_nonce_field( 'submit_run_manual_tests', '_wpnonce' ); ?>
<input type="submit" name="submit_run_manual_tests" value="<?php esc_attr_e( 'Run Manual Tests', 'visual-regression-tests' ); ?>"
<input type="submit" name="submit_run_manual_tests" value="<?php esc_attr_e( 'Run Tests', 'visual-regression-tests' ); ?>"
class="page-title-action button-secondary"
id="<?php echo ( ! $data['is_connected'] || ! $data['running_tests_count'] ) ? 'run-manual-tests-disabled' : 'run-manual-tests'; ?>"
<?php echo ( ! $data['is_connected'] || ! $data['running_tests_count'] ) ? ' disabled' : ''; ?>
Expand Down
15 changes: 15 additions & 0 deletions includes/core/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,21 @@ public function get_plugin_logo_icon( $base64 = true ) {
return $svg;
}

/**
* Get public post types.
*
* @return array public post types.
*/
public function get_public_post_types() {
$default_post_types = [ 'post', 'page' ];
$custom_post_types = get_post_types( [
'public' => true,
'_builtin' => false,
] );

return array_merge( $default_post_types, $custom_post_types );
}

/**
* Get snapshot placeholder image.
*
Expand Down
66 changes: 66 additions & 0 deletions includes/features/class-bulk-actions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Vrts\Features;

use Vrts\Services\Test_Service;

class Bulk_Actions {

/**
* Constructor.
*/
public function __construct() {
add_action( 'admin_init', function() {
$post_types = vrts()->get_public_post_types();

foreach ( $post_types as $post_type ) {
add_filter( 'bulk_actions-edit-' . $post_type, [ $this, 'register_bulk_test_option' ] );
add_filter( 'handle_bulk_actions-edit-' . $post_type, [ $this, 'handle_bulk_optimize_action' ], 10, 3 );
}
} );
}

/**
* Register bulk optimize option.
*
* @param array $bulk_actions Bulk actions.
* @return array
*/
public function register_bulk_test_option( $bulk_actions ) {
$bulk_actions['add-to-vrts'] = __( 'Add to VRTs', 'visual-regression-tests-service' );

return $bulk_actions;
}

/**
* Handle bulk optimize action.
*
* @param string $redirect_to Redirect to.
* @param string $doaction Action.
* @param array $post_ids Post ids.
* @return string
*/
public function handle_bulk_optimize_action( $redirect_to, $doaction, $post_ids ) {
if ( 'add-to-vrts' !== $doaction ) {
return $redirect_to;
}

$service = new Test_Service();
$created_tests = $service->create_tests( $post_ids );
$vrts_url = admin_url( 'admin.php?page=vrts' );

if ( is_wp_error( $created_tests ) ) {
$redirect_to = add_query_arg([
'new-test-failed' => true,
], $vrts_url);
} else {
$redirect_to = add_query_arg([
'message' => 'success',
'new-tests-added' => true,
'post_ids' => wp_list_pluck( $created_tests, 'post_id' ),
], $vrts_url);
}

return $redirect_to;
}
}
8 changes: 7 additions & 1 deletion includes/features/class-email-notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,15 @@ public function send_email( $differences, $post_id, $alert_id ) {
esc_url( get_the_permalink( $post_id ) )
);

/**
* It replaces common plain text characters with formatted entities
* so we remove it as in plain text emails it displays it as e.g. &#8217;s
*/
remove_filter( 'the_title', 'wptexturize' );

$message = esc_html_x( 'Howdy,', 'notification email', 'visual-regression-tests' ) . "\n\n" .
esc_html_x( 'New visual differences have been detected on the following page:', 'notification email', 'visual-regression-tests' ) . "\n\n" .
esc_html( get_the_title( $post_id ) ) . "\n\n" .
get_the_title( $post_id ) . "\n\n" .
esc_html_x( 'Review and resolve the alert to resume testing:', 'notification email', 'visual-regression-tests' ) . "\n" .
esc_url( $admin_url ) . 'admin.php?page=vrts-alerts&action=edit&alert_id=' . $alert_id . "\n\n" .
sprintf(
Expand Down
16 changes: 10 additions & 6 deletions includes/features/class-subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,31 @@ public static function get_subscription_tier_id() {

/**
* Increase number of tests until server updates the number of available tests.
*
* @param int $number Number of tests to increase.
*/
public static function increase_tests_count() {
public static function increase_tests_count( $number = 1 ) {
$remaining_tests = get_option( 'vrts_remaining_tests' );
$total_tests = get_option( 'vrts_total_tests' );

if ( $remaining_tests < $total_tests ) {
$remaining_tests++;
update_option( 'vrts_remaining_tests', $remaining_tests );
$remaining_tests += $number;
update_option( 'vrts_remaining_tests', min( $total_tests, $remaining_tests ) );
}
return true;
}

/**
* Decrease number of tests until server updates the number of available tests.
*
* @param int $number Number of tests to decrease.
*/
public static function decrease_tests_count() {
public static function decrease_tests_count( $number = 1 ) {
$remaining_tests = get_option( 'vrts_remaining_tests' );

if ( $remaining_tests >= 1 ) {
$remaining_tests--;
update_option( 'vrts_remaining_tests', $remaining_tests );
$remaining_tests -= $number;
update_option( 'vrts_remaining_tests', max( 0, $remaining_tests ) );
}
}

Expand Down
29 changes: 25 additions & 4 deletions includes/features/class-tests-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,12 @@ public function init_notifications() {
add_action( 'admin_notices', [ $this, 'render_notification_new_test_added' ] );
}

// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- It should be ok here.
$is_new_tests_added = isset( $_GET['new-tests-added'] ) ? sanitize_text_field( wp_unslash( $_GET['new-tests-added'] ) ) : false;
if ( $is_new_tests_added ) {
add_action( 'admin_notices', [ $this, 'render_notification_new_tests_added' ] );
}

// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- It should be ok here.
$is_testing_disabled = isset( $_GET['testing-disabled'] ) ? sanitize_text_field( wp_unslash( $_GET['testing-disabled'] ) ) : false;
if ( $is_testing_disabled ) {
Expand Down Expand Up @@ -425,18 +431,33 @@ public function render_notification_get_started() {
public function render_notification_new_test_added() {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- It should be ok here.
$post_id = isset( $_GET['post_id'] ) ? intval( $_GET['post_id'] ) : false;
Admin_Notices::render_notification('new_test_added', false, [
Admin_Notices::render_notification( 'new_test_added', false, [
'page_title' => get_the_title( $post_id ),
]);
}

/**
* Render new_tests_added Notification.
*/
public function render_notification_new_tests_added() {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- It should be ok here.
$post_ids = isset( $_GET['post_ids'] ) ? array_map( 'intval', $_GET['post_ids'] ) : false;
if ( $post_ids ) {
Admin_Notices::render_notification( 'new_tests_added', false, [
'page_titles' => implode( ', ', array_reverse( array_map( function( $post_id ) {
return get_the_title( $post_id );
}, $post_ids ) ) ),
]);
}
}

/**
* Render new_test_failed Notification.
*/
public function render_notification_new_test_failed() {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- It should be ok here.
$post_id = isset( $_GET['post_id'] ) ? intval( $_GET['post_id'] ) : false;
Admin_Notices::render_notification('new_test_failed', false, [
Admin_Notices::render_notification( 'new_test_failed', false, [
'page_title' => get_the_title( $post_id ),
]);
}
Expand All @@ -447,7 +468,7 @@ public function render_notification_new_test_failed() {
public function render_notification_test_disabled() {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- It should be ok here.
$post_id = isset( $_GET['post_id'] ) ? intval( $_GET['post_id'] ) : false;
Admin_Notices::render_notification('test_disabled', false, [
Admin_Notices::render_notification( 'test_disabled', false, [
'page_title' => get_the_title( $post_id ),
'post_id' => intval( $post_id ),
]);
Expand All @@ -458,7 +479,7 @@ public function render_notification_test_disabled() {
*/
public function render_notification_unlock_more_tests() {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- It should be ok here.
Admin_Notices::render_notification('unlock_more_tests', false, [
Admin_Notices::render_notification( 'unlock_more_tests', false, [
'total_tests' => Subscription::get_total_tests(),
'remaining_tests' => Subscription::get_remaining_tests(),
]);
Expand Down
8 changes: 4 additions & 4 deletions includes/features/class-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ class Tests {
* Constructor.
*/
public function __construct() {
// Allows developers to rerun tests by calling `do_action( 'vrts_rerun_tests' )`.
add_action( 'vrts_rerun_tests', [ $this, 'rerun_tests' ] );
// Allows developers to run tests by calling `do_action( 'vrts_run_tests' )`.
add_action( 'vrts_run_tests', [ $this, 'run_tests' ] );
}

/**
* Rerun all running tests if a subscription is active.
* Run all tests if a subscription is active.
*/
public static function rerun_tests() {
public static function run_tests() {
$has_subscription = Subscription::get_subscription_status();

if ( ! $has_subscription ) {
Expand Down
2 changes: 1 addition & 1 deletion includes/list-tables/class-tests-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public function get_bulk_actions() {
];
if ( Subscription::get_subscription_status() && count( Test::get_all_running() ) > 0 ) {
$actions = array_merge(
[ 'run-manual-test' => esc_html__( 'Run Manual Test', 'visual-regression-tests' ) ],
[ 'run-manual-test' => esc_html__( 'Run Test', 'visual-regression-tests' ) ],
$actions
);
}
Expand Down
63 changes: 63 additions & 0 deletions includes/models/class-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,28 @@ public static function get_item_by_post_id( $post_id = 0 ) {
);
}

/**
* Get multiple tests from database by post ids
*
* @param array $post_ids Post ids.
*
* @return object
*/
public static function get_items_by_post_ids( $post_ids = [] ) {
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_results(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- It's ok.
"SELECT * FROM $tests_table WHERE post_id IN (" . implode( ',', array_fill( 0, count( $post_ids ), '%d' ) ) . ')',
$post_ids
)
);
}

/**
* Get a single test from database
*
Expand Down Expand Up @@ -450,6 +472,47 @@ public static function save( $args = [], $row_id = null ) {
}
}

/**
* Insert multiple test data
*
* @param array $data Data to update (in multi array column => value pairs).
*
* @return int|false The number of rows affected, or false on error.
*/
public static function save_multiple( $data = [] ) {
global $wpdb;

$tests_table = Tests_Table::get_table_name();

// If there is no data, return false.
if ( ! isset( $data[0] ) ) {
return false;
}

$fields = '`' . implode( '`, `', array_keys( $data[0] ) ) . '`';
$formats = implode( ', ', array_map(function( $row ) {
return '(' . implode( ', ', array_fill( 0, count( $row ), '%s' ) ) . ')';
}, $data ) );
$values = [];

foreach ( $data as $row ) {
foreach ( $row as $value ) {
$values[] = $value;
}
}

// TODO: add support for update.

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- It's ok.
return $wpdb->query(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- It's ok.
"INSERT INTO `$tests_table` ($fields) VALUES $formats",
$values
)
);
}

/**
* Save hide CSS selectors for a test.
*
Expand Down
Loading

0 comments on commit 6eb41a6

Please sign in to comment.