From e1da095f38e1660ab73ef1374541f434a28d1697 Mon Sep 17 00:00:00 2001 From: jjgrainger Date: Fri, 17 Mar 2023 13:17:30 +0000 Subject: [PATCH 01/13] create setup environment ajax endpoint --- includes/Admin/Admin_AJAX.php | 62 +++++++++++++++++++++- includes/Checker/Abstract_Check_Runner.php | 11 ++-- object-cache.copy.php | 1 - 3 files changed, 65 insertions(+), 9 deletions(-) diff --git a/includes/Admin/Admin_AJAX.php b/includes/Admin/Admin_AJAX.php index a7c468d4d..401ce5828 100644 --- a/includes/Admin/Admin_AJAX.php +++ b/includes/Admin/Admin_AJAX.php @@ -9,8 +9,10 @@ use WP_Error; use Exception; +use WordPress\Plugin_Check\Checker\AJAX_Runner; use WordPress\Plugin_Check\Checker\Checks; use WordPress\Plugin_Check\Checker\Runtime_Check; +use WordPress\Plugin_Check\Checker\Runtime_Environment_Setup; use WordPress\Plugin_Check\Utilities\Plugin_Request_Utility; /** * Class to handle the Admin AJAX requests. @@ -33,6 +35,7 @@ class Admin_AJAX { * @since n.e.x.t */ public function add_hooks() { + add_action( 'wp_ajax_plugin_check_setup_environment', array( $this, 'setup_environment' ) ); add_action( 'wp_ajax_plugin_check_get_checks_to_run', array( $this, 'get_checks_to_run' ) ); add_action( 'wp_ajax_plugin_check_run_checks', array( $this, 'run_checks' ) ); } @@ -46,6 +49,61 @@ public function get_nonce() { return wp_create_nonce( self::NONCE_KEY ); } + /** + * Handles the AJAX request to setup the runtime environment if needed. + * + * @since n.e.x.t + */ + public function setup_environment() { + // Verify the nonce before continuing. + $valid_nonce = $this->verify_nonce( filter_input( INPUT_POST, 'nonce', FILTER_SANITIZE_STRING ) ); + + if ( is_wp_error( $valid_nonce ) ) { + wp_send_json_error( $valid_nonce, 403 ); + } + + $runner = Plugin_Request_Utility::get_runner(); + + if ( is_null( $runner ) ) { + $runner = new AJAX_Runner(); + } + + // Make sure we are using the correct runner instance. + if ( ! ( $runner instanceof AJAX_Runner ) ) { + wp_send_json_error( + new WP_Error( 'invalid-runner', __( 'AJAX Runner was not initialized correctly.', 'plugin-check' ) ), + 500 + ); + } + + $checks = filter_input( INPUT_POST, 'checks', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); + $plugin = filter_input( INPUT_POST, 'plugin', FILTER_SANITIZE_STRING ); + + try { + $runner->set_check_slugs( $checks ); + $runner->set_plugin( $plugin ); + } catch ( Exception $error ) { + wp_send_json_error( + new WP_Error( 'invalid-request', $error->getMessage() ), + 400 + ); + } + + $message = __( 'No runtime checks, runtime environment was not setup.', 'plugin-check' ); + + if ( $runner->has_runtime_check() ) { + $runtime = new Runtime_Environment_Setup(); + $runtime->setup(); + $message = __( 'Runtime environment setup successful.', 'plugin-check' ); + } + + wp_send_json_success( + array( + 'message' => $message, + ) + ); + } + /** * Handles the AJAX request that returns the checks to run. * @@ -68,7 +126,7 @@ public function get_checks_to_run() { } catch ( Exception $error ) { wp_send_json_error( new WP_Error( 'invalid-plugin', $error->getMessage() ), - 403 + 400 ); } @@ -90,7 +148,7 @@ public function get_checks_to_run() { 'inactive-plugin', __( 'Runtime checks cannot be run against inactive plugins.', 'plugin-check' ) ), - 403 + 400 ); } } else { diff --git a/includes/Checker/Abstract_Check_Runner.php b/includes/Checker/Abstract_Check_Runner.php index 2a24865df..897351b50 100644 --- a/includes/Checker/Abstract_Check_Runner.php +++ b/includes/Checker/Abstract_Check_Runner.php @@ -140,7 +140,7 @@ public function set_plugin( $plugin ) { * @throws Exception Thrown exception when preparation fails. */ public function prepare() { - if ( $this->requires_universal_preparations( $this->get_checks_to_run() ) ) { + if ( $this->has_runtime_check() ) { $preparation = new Universal_Runtime_Preparation( $this->get_checks_instance()->context() ); $cleanup = $preparation->prepare(); @@ -187,15 +187,14 @@ public function run() { } /** - * Determines if any of the checks requires the universal runtime preparation. + * Determines if any of the checks are a runtime check. * * @since n.e.x.t * - * @param array $checks An array of check instances to run. - * @return bool Returns true if one or more checks requires the universal runtime preparation. + * @return bool Returns true if one or more checks is a runtime check. */ - protected function requires_universal_preparations( array $checks ) { - foreach ( $checks as $check ) { + public function has_runtime_check() { + foreach ( $this->get_checks_to_run() as $check ) { if ( $check instanceof Runtime_Check ) { return true; } diff --git a/object-cache.copy.php b/object-cache.copy.php index 3684c7b1e..81c489d9a 100644 --- a/object-cache.copy.php +++ b/object-cache.copy.php @@ -1,4 +1,3 @@ - Date: Fri, 17 Mar 2023 15:39:49 +0000 Subject: [PATCH 02/13] add ajax request for setup environment endpoint --- assets/js/plugin-check-admin.js | 43 ++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/assets/js/plugin-check-admin.js b/assets/js/plugin-check-admin.js index 22ac0bdd1..d2af129c7 100644 --- a/assets/js/plugin-check-admin.js +++ b/assets/js/plugin-check-admin.js @@ -11,7 +11,8 @@ checkItButton.addEventListener( 'click', ( e ) => { e.preventDefault(); - getChecksToRun() + setupEnvironment() + .then( getChecksToRun ) .then( runChecks ) .then( ( data ) => { @@ -25,6 +26,46 @@ ); } ); + /** + * Setup the runtime environment if needed. + * + * @since n.e.x.t + */ + function setupEnvironment() { + // Setup the form data to post. + const pluginCheckData = new FormData(); + pluginCheckData.append( 'nonce', pluginCheck.nonce ); + pluginCheckData.append( 'plugin', pluginsList.value ); + pluginCheckData.append( 'checks', [] ); + pluginCheckData.append( 'action', 'plugin_check_setup_environment' ); + + return fetch( + ajaxurl, + { + method: 'POST', + credentials: 'same-origin', + body: pluginCheckData + } + ) + .then( + ( response ) => { + return response.json(); + } + ) + .then( handleDataErrors ) + .then( + ( data ) => { + if ( ! data.data || ! data.data.message ) { + throw new Error( 'Response contains no data.' ); + } + + console.log( data.data.message ); + + return data.data; + } + ); + } + /** * Get the Checks to run. * From eef17ebb500e19bfbec3499cade0ed4c5d9892c8 Mon Sep 17 00:00:00 2001 From: jjgrainger Date: Fri, 17 Mar 2023 17:06:56 +0000 Subject: [PATCH 03/13] add ajax endpoint for cleanup environment --- assets/js/plugin-check-admin.js | 49 +++++++++++++++++++++++++++++---- includes/Admin/Admin_AJAX.php | 45 ++++++++++++++++++++++++++++-- includes/Plugin_Main.php | 1 - 3 files changed, 86 insertions(+), 9 deletions(-) diff --git a/assets/js/plugin-check-admin.js b/assets/js/plugin-check-admin.js index d2af129c7..7a693cb4c 100644 --- a/assets/js/plugin-check-admin.js +++ b/assets/js/plugin-check-admin.js @@ -11,9 +11,10 @@ checkItButton.addEventListener( 'click', ( e ) => { e.preventDefault(); - setupEnvironment() - .then( getChecksToRun ) + getChecksToRun() + .then( setupEnvironment ) .then( runChecks ) + .then( cleanupEnvironment ) .then( ( data ) => { console.log( data.message ); @@ -31,12 +32,12 @@ * * @since n.e.x.t */ - function setupEnvironment() { + function setupEnvironment( data ) { // Setup the form data to post. const pluginCheckData = new FormData(); pluginCheckData.append( 'nonce', pluginCheck.nonce ); - pluginCheckData.append( 'plugin', pluginsList.value ); - pluginCheckData.append( 'checks', [] ); + pluginCheckData.append( 'plugin', data.plugin ); + pluginCheckData.append( 'checks', data.checks ); pluginCheckData.append( 'action', 'plugin_check_setup_environment' ); return fetch( @@ -66,6 +67,44 @@ ); } + /** + * Cleanup the runtime environment. + * + * @since n.e.x.t + */ + function cleanupEnvironment( data ) { + const pluginCheckData = new FormData(); + pluginCheckData.append( 'nonce', pluginCheck.nonce ); + pluginCheckData.append( 'action', 'plugin_check_cleanup_environment' ); + + return fetch( + ajaxurl, + { + method: 'POST', + credentials: 'same-origin', + body: pluginCheckData + } + ) + .then( + ( response ) => { + return response.json(); + } + ) + .then( handleDataErrors ) + .then( + ( data ) => { + if ( ! data.data || ! data.data.message ) { + throw new Error( 'Response contains no data.' ); + } + + console.log( data.data.message ); + + return data.data; + } + ); + } + + /** * Get the Checks to run. * diff --git a/includes/Admin/Admin_AJAX.php b/includes/Admin/Admin_AJAX.php index 401ce5828..642b6c4ab 100644 --- a/includes/Admin/Admin_AJAX.php +++ b/includes/Admin/Admin_AJAX.php @@ -35,6 +35,7 @@ class Admin_AJAX { * @since n.e.x.t */ public function add_hooks() { + add_action( 'wp_ajax_plugin_check_cleanup_environment', array( $this, 'cleanup_environment' ) ); add_action( 'wp_ajax_plugin_check_setup_environment', array( $this, 'setup_environment' ) ); add_action( 'wp_ajax_plugin_check_get_checks_to_run', array( $this, 'get_checks_to_run' ) ); add_action( 'wp_ajax_plugin_check_run_checks', array( $this, 'run_checks' ) ); @@ -61,7 +62,6 @@ public function setup_environment() { if ( is_wp_error( $valid_nonce ) ) { wp_send_json_error( $valid_nonce, 403 ); } - $runner = Plugin_Request_Utility::get_runner(); if ( is_null( $runner ) ) { @@ -76,7 +76,7 @@ public function setup_environment() { ); } - $checks = filter_input( INPUT_POST, 'checks', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); + $checks = wp_parse_list( filter_input( INPUT_POST, 'checks', FILTER_SANITIZE_STRING ) ); $plugin = filter_input( INPUT_POST, 'plugin', FILTER_SANITIZE_STRING ); try { @@ -97,6 +97,45 @@ public function setup_environment() { $message = __( 'Runtime environment setup successful.', 'plugin-check' ); } + wp_send_json_success( + array( + 'message' => $message, + 'plugin' => $plugin, + 'checks' => $checks, + ) + ); + } + + /** + * Handles the AJAX request to cleanup the runtime environment. + * + * @since n.e.x.t + */ + public function cleanup_environment() { + global $wpdb; + + // Verify the nonce before continuing. + $valid_nonce = $this->verify_nonce( filter_input( INPUT_POST, 'nonce', FILTER_SANITIZE_STRING ) ); + + if ( is_wp_error( $valid_nonce ) ) { + wp_send_json_error( $valid_nonce, 403 ); + } + + // Set the new prefix. + $old_prefix = $wpdb->set_prefix( 'wppc_' ); + + $message = __( 'Runtime environment was not prepared, cleanup was not run.', 'plugin-check' ); + + // Test if the runtime environment tables exist. + if ( 'wppc_posts' === $wpdb->get_var( "SHOW TABLES LIKE 'wppc_posts'" ) ) { + $runtime = new Runtime_Environment_Setup(); + $runtime->cleanup(); + $message = __( 'Runtime environment cleanup successful.', 'plugin-check' ); + } + + // Restore the old prefix. + $wpdb->set_prefix( $old_prefix ); + wp_send_json_success( array( 'message' => $message, @@ -117,7 +156,7 @@ public function get_checks_to_run() { wp_send_json_error( $valid_nonce, 403 ); } - $checks = filter_input( INPUT_POST, 'checks', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); + $checks = wp_parse_list( filter_input( INPUT_POST, 'checks', FILTER_SANITIZE_STRING ) ); $plugin = filter_input( INPUT_POST, 'plugin', FILTER_SANITIZE_STRING ); // Attempt to get the plugin basename based on the request. diff --git a/includes/Plugin_Main.php b/includes/Plugin_Main.php index 31d6eb299..83cb002db 100644 --- a/includes/Plugin_Main.php +++ b/includes/Plugin_Main.php @@ -54,7 +54,6 @@ public function context() { * @since n.e.x.t */ public function add_hooks() { - if ( defined( 'WP_CLI' ) && WP_CLI ) { $plugin_command = new Plugin_Check_Command( $this->context ); \WP_CLI::add_command( 'plugin', $plugin_command ); From c6fe064ca5bf59d717030763b6f9b83c98fa5bf6 Mon Sep 17 00:00:00 2001 From: jjgrainger Date: Mon, 20 Mar 2023 17:46:35 +0000 Subject: [PATCH 04/13] initialize runner in muplugins_loaded hook --- assets/js/plugin-check-admin.js | 56 +++++++++---------- includes/Utilities/Plugin_Request_Utility.php | 11 +++- .../Plugin_Request_Utility_Tests.php | 10 ++++ 3 files changed, 46 insertions(+), 31 deletions(-) diff --git a/assets/js/plugin-check-admin.js b/assets/js/plugin-check-admin.js index 7a693cb4c..d1084cbeb 100644 --- a/assets/js/plugin-check-admin.js +++ b/assets/js/plugin-check-admin.js @@ -72,37 +72,37 @@ * * @since n.e.x.t */ - function cleanupEnvironment( data ) { - const pluginCheckData = new FormData(); - pluginCheckData.append( 'nonce', pluginCheck.nonce ); - pluginCheckData.append( 'action', 'plugin_check_cleanup_environment' ); - - return fetch( - ajaxurl, - { - method: 'POST', - credentials: 'same-origin', - body: pluginCheckData - } - ) - .then( - ( response ) => { - return response.json(); + function cleanupEnvironment( data ) { + const pluginCheckData = new FormData(); + pluginCheckData.append( 'nonce', pluginCheck.nonce ); + pluginCheckData.append( 'action', 'plugin_check_cleanup_environment' ); + + return fetch( + ajaxurl, + { + method: 'POST', + credentials: 'same-origin', + body: pluginCheckData + } + ) + .then( + ( response ) => { + return response.json(); + } + ) + .then( handleDataErrors ) + .then( + ( data ) => { + if ( ! data.data || ! data.data.message ) { + throw new Error( 'Response contains no data.' ); } - ) - .then( handleDataErrors ) - .then( - ( data ) => { - if ( ! data.data || ! data.data.message ) { - throw new Error( 'Response contains no data.' ); - } - console.log( data.data.message ); + console.log( data.data.message ); - return data.data; - } - ); - } + return data.data; + } + ); + } /** diff --git a/includes/Utilities/Plugin_Request_Utility.php b/includes/Utilities/Plugin_Request_Utility.php index 0949f27ff..06c4f04a3 100644 --- a/includes/Utilities/Plugin_Request_Utility.php +++ b/includes/Utilities/Plugin_Request_Utility.php @@ -93,9 +93,14 @@ public static function initialize_runner() { foreach ( $runners as $runner ) { if ( $runner->is_plugin_check() ) { - // @TODO: Handle the cleanup function in later issue with shutdown action or method that returns cleanup functions. - static::$cleanup = $runner->prepare(); - static::$runner = $runner; + add_action( + 'muplugins_loaded', + function() use ( $runner ) { + static::$cleanup = $runner->prepare(); + static::$runner = $runner; + } + ); + break; } } diff --git a/tests/Utilities/Plugin_Request_Utility_Tests.php b/tests/Utilities/Plugin_Request_Utility_Tests.php index f6924ef9a..e21ec6777 100644 --- a/tests/Utilities/Plugin_Request_Utility_Tests.php +++ b/tests/Utilities/Plugin_Request_Utility_Tests.php @@ -47,6 +47,9 @@ public function test_initialize_runner_with_cli() { ); Plugin_Request_Utility::initialize_runner(); + + do_action( 'muplugins_loaded' ); + $runner = Plugin_Request_Utility::get_runner(); unset( $_SERVER['argv'] ); @@ -60,6 +63,9 @@ public function test_initialize_runner_with_ajax() { $_REQUEST['plugin'] = 'plugin-check'; Plugin_Request_Utility::initialize_runner(); + + do_action( 'muplugins_loaded' ); + $runner = Plugin_Request_Utility::get_runner(); $this->assertInstanceOf( AJAX_Runner::class, $runner ); @@ -87,6 +93,8 @@ function( $checks ) { Plugin_Request_Utility::initialize_runner(); + do_action( 'muplugins_loaded' ); + // Determine if one of the Universal_Runtume_Preparation was run. $prepared = has_filter( 'option_active_plugins' ); @@ -123,6 +131,8 @@ function( $checks ) { Plugin_Request_Utility::initialize_runner(); + do_action( 'muplugins_loaded' ); + // Determine if one of the Universal_Runtume_Preparation was run. $prepared = has_filter( 'option_active_plugins' ); From 2ad3ce5245f30f08282f706871817ec5c529a888 Mon Sep 17 00:00:00 2001 From: jjgrainger Date: Mon, 20 Mar 2023 19:04:24 +0000 Subject: [PATCH 05/13] use alternative when constants not available --- .../Preparations/Force_Single_Plugin_Preparation.php | 9 ++++++++- .../Preparations/Universal_Runtime_Preparation.php | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/includes/Checker/Preparations/Force_Single_Plugin_Preparation.php b/includes/Checker/Preparations/Force_Single_Plugin_Preparation.php index 5041b283d..dbc50fc5d 100644 --- a/includes/Checker/Preparations/Force_Single_Plugin_Preparation.php +++ b/includes/Checker/Preparations/Force_Single_Plugin_Preparation.php @@ -82,7 +82,14 @@ public function prepare() { public function filter_active_plugins( $active_plugins ) { if ( is_array( $active_plugins ) && in_array( $this->plugin_basename, $active_plugins, true ) ) { - $plugin_base_file = plugin_basename( WP_PLUGIN_CHECK_MAIN_FILE ); + if ( defined( 'WP_PLUGIN_CHECK_MAIN_FILE' ) ) { + $plugin_check_file = WP_PLUGIN_CHECK_MAIN_FILE; + } else { + $plugins_dir = defined( 'WP_PLUGIN_DIR' ) ? WP_PLUGIN_DIR : WP_CONTENT_DIR . '/plugins'; + $plugin_check_file = $plugins_dir . '/plugin-check/plugin-check.php'; + } + + $plugin_base_file = plugin_basename( $plugin_check_file ); // If the plugin-check is the only available plugin then return that one only. if ( $this->plugin_basename === $plugin_base_file ) { diff --git a/includes/Checker/Preparations/Universal_Runtime_Preparation.php b/includes/Checker/Preparations/Universal_Runtime_Preparation.php index cd4ab8793..633cc93fd 100644 --- a/includes/Checker/Preparations/Universal_Runtime_Preparation.php +++ b/includes/Checker/Preparations/Universal_Runtime_Preparation.php @@ -54,7 +54,14 @@ public function prepare() { $cleanup_functions = array(); - $use_minimal_theme_preparation = new Use_Minimal_Theme_Preparation( 'wp-empty-theme', WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . '/test-content/themes' ); + if ( ! defined( 'WP_PLUGIN_CHECK_PLUGIN_DIR_PATH' ) ) { + $plugins_dir = defined( 'WP_PLUGIN_DIR' ) ? WP_PLUGIN_DIR : WP_CONTENT_DIR . '/plugins'; + $theme_folder = $plugins_dir . '/plugin-check/test-content/themes'; + } else { + $theme_folder = WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'test-content/themes'; + } + + $use_minimal_theme_preparation = new Use_Minimal_Theme_Preparation( 'wp-empty-theme', $theme_folder ); $cleanup_functions[] = $use_minimal_theme_preparation->prepare(); $force_single_plugin_preparation = new Force_Single_Plugin_Preparation( $this->check_context->basename() ); From 7544e1b0d70a4fe04c0f9ddab1779dbed3f70ac9 Mon Sep 17 00:00:00 2001 From: jjgrainger Date: Mon, 20 Mar 2023 19:10:59 +0000 Subject: [PATCH 06/13] update has_runtime_check and get_checks_to_run methods --- includes/Admin/Admin_AJAX.php | 2 +- includes/Checker/Abstract_Check_Runner.php | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/includes/Admin/Admin_AJAX.php b/includes/Admin/Admin_AJAX.php index 642b6c4ab..0214d16c3 100644 --- a/includes/Admin/Admin_AJAX.php +++ b/includes/Admin/Admin_AJAX.php @@ -91,7 +91,7 @@ public function setup_environment() { $message = __( 'No runtime checks, runtime environment was not setup.', 'plugin-check' ); - if ( $runner->has_runtime_check() ) { + if ( $this->has_runtime_check( $runner->get_checks_to_run() ) ) { $runtime = new Runtime_Environment_Setup(); $runtime->setup(); $message = __( 'Runtime environment setup successful.', 'plugin-check' ); diff --git a/includes/Checker/Abstract_Check_Runner.php b/includes/Checker/Abstract_Check_Runner.php index 897351b50..c6f4a884d 100644 --- a/includes/Checker/Abstract_Check_Runner.php +++ b/includes/Checker/Abstract_Check_Runner.php @@ -140,7 +140,7 @@ public function set_plugin( $plugin ) { * @throws Exception Thrown exception when preparation fails. */ public function prepare() { - if ( $this->has_runtime_check() ) { + if ( $this->has_runtime_check( $this->get_checks_to_run() ) ) { $preparation = new Universal_Runtime_Preparation( $this->get_checks_instance()->context() ); $cleanup = $preparation->prepare(); @@ -191,10 +191,11 @@ public function run() { * * @since n.e.x.t * + * @param array $checks An array of check instances to run. * @return bool Returns true if one or more checks is a runtime check. */ - public function has_runtime_check() { - foreach ( $this->get_checks_to_run() as $check ) { + protected function has_runtime_check( array $checks ) { + foreach ( $checks as $check ) { if ( $check instanceof Runtime_Check ) { return true; } @@ -243,7 +244,7 @@ private function get_shared_preparations( array $checks ) { * * @return array An array map of check slugs to Check instances. */ - protected function get_checks_to_run() { + public function get_checks_to_run() { $check_slugs = $this->get_check_slugs(); $all_checks = $this->get_checks_instance()->get_checks(); From a17d71610d1b96681637db595560d9e6613546d4 Mon Sep 17 00:00:00 2001 From: jjgrainger Date: Mon, 20 Mar 2023 19:13:26 +0000 Subject: [PATCH 07/13] move get_checks_to_run call in try/catch block to capture exception thrown --- includes/Admin/Admin_AJAX.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/includes/Admin/Admin_AJAX.php b/includes/Admin/Admin_AJAX.php index 0214d16c3..a12c80499 100644 --- a/includes/Admin/Admin_AJAX.php +++ b/includes/Admin/Admin_AJAX.php @@ -82,6 +82,7 @@ public function setup_environment() { try { $runner->set_check_slugs( $checks ); $runner->set_plugin( $plugin ); + $checks_to_run = $runner->get_checks_to_run(); } catch ( Exception $error ) { wp_send_json_error( new WP_Error( 'invalid-request', $error->getMessage() ), @@ -91,7 +92,7 @@ public function setup_environment() { $message = __( 'No runtime checks, runtime environment was not setup.', 'plugin-check' ); - if ( $this->has_runtime_check( $runner->get_checks_to_run() ) ) { + if ( $this->has_runtime_check( $checks_to_run ) ) { $runtime = new Runtime_Environment_Setup(); $runtime->setup(); $message = __( 'Runtime environment setup successful.', 'plugin-check' ); From 7f122a0b3353b6d6d695670d02e9d9f36c378e82 Mon Sep 17 00:00:00 2001 From: jjgrainger Date: Tue, 21 Mar 2023 17:13:13 +0000 Subject: [PATCH 08/13] rename actions and methods --- assets/js/plugin-check-admin.js | 4 ++-- includes/Admin/Admin_AJAX.php | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/assets/js/plugin-check-admin.js b/assets/js/plugin-check-admin.js index d1084cbeb..50e427cb0 100644 --- a/assets/js/plugin-check-admin.js +++ b/assets/js/plugin-check-admin.js @@ -38,7 +38,7 @@ pluginCheckData.append( 'nonce', pluginCheck.nonce ); pluginCheckData.append( 'plugin', data.plugin ); pluginCheckData.append( 'checks', data.checks ); - pluginCheckData.append( 'action', 'plugin_check_setup_environment' ); + pluginCheckData.append( 'action', 'plugin_check_set_up_environment' ); return fetch( ajaxurl, @@ -75,7 +75,7 @@ function cleanupEnvironment( data ) { const pluginCheckData = new FormData(); pluginCheckData.append( 'nonce', pluginCheck.nonce ); - pluginCheckData.append( 'action', 'plugin_check_cleanup_environment' ); + pluginCheckData.append( 'action', 'plugin_check_clean_up_environment' ); return fetch( ajaxurl, diff --git a/includes/Admin/Admin_AJAX.php b/includes/Admin/Admin_AJAX.php index a12c80499..4d855df45 100644 --- a/includes/Admin/Admin_AJAX.php +++ b/includes/Admin/Admin_AJAX.php @@ -35,8 +35,8 @@ class Admin_AJAX { * @since n.e.x.t */ public function add_hooks() { - add_action( 'wp_ajax_plugin_check_cleanup_environment', array( $this, 'cleanup_environment' ) ); - add_action( 'wp_ajax_plugin_check_setup_environment', array( $this, 'setup_environment' ) ); + add_action( 'wp_ajax_plugin_check_clean_up_environment', array( $this, 'clean_up_environment' ) ); + add_action( 'wp_ajax_plugin_check_set_up_environment', array( $this, 'set_up_environment' ) ); add_action( 'wp_ajax_plugin_check_get_checks_to_run', array( $this, 'get_checks_to_run' ) ); add_action( 'wp_ajax_plugin_check_run_checks', array( $this, 'run_checks' ) ); } @@ -55,7 +55,7 @@ public function get_nonce() { * * @since n.e.x.t */ - public function setup_environment() { + public function set_up_environment() { // Verify the nonce before continuing. $valid_nonce = $this->verify_nonce( filter_input( INPUT_POST, 'nonce', FILTER_SANITIZE_STRING ) ); @@ -112,7 +112,7 @@ public function setup_environment() { * * @since n.e.x.t */ - public function cleanup_environment() { + public function clean_up_environment() { global $wpdb; // Verify the nonce before continuing. From 9719698c30f17fc76f044da067bf9ddfd9939b20 Mon Sep 17 00:00:00 2001 From: jjgrainger Date: Tue, 21 Mar 2023 17:14:50 +0000 Subject: [PATCH 09/13] add check for object-cache.php constant --- includes/Admin/Admin_AJAX.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/Admin/Admin_AJAX.php b/includes/Admin/Admin_AJAX.php index 4d855df45..13d738639 100644 --- a/includes/Admin/Admin_AJAX.php +++ b/includes/Admin/Admin_AJAX.php @@ -128,7 +128,7 @@ public function clean_up_environment() { $message = __( 'Runtime environment was not prepared, cleanup was not run.', 'plugin-check' ); // Test if the runtime environment tables exist. - if ( 'wppc_posts' === $wpdb->get_var( "SHOW TABLES LIKE 'wppc_posts'" ) ) { + if ( 'wppc_posts' === $wpdb->get_var( "SHOW TABLES LIKE 'wppc_posts'" ) || defined( 'WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION' ) ) { $runtime = new Runtime_Environment_Setup(); $runtime->cleanup(); $message = __( 'Runtime environment cleanup successful.', 'plugin-check' ); From dccdae63d0828d155a70e079d9d3cbb77c61adf6 Mon Sep 17 00:00:00 2001 From: jjgrainger Date: Tue, 21 Mar 2023 17:46:47 +0000 Subject: [PATCH 10/13] pass checks as array in AJAX requests --- assets/js/plugin-check-admin.js | 14 +++++++++----- includes/Admin/Admin_AJAX.php | 4 ++-- includes/Checker/AJAX_Runner.php | 2 +- tests/Checker/AJAX_Runner_Tests.php | 12 ++++++------ tests/Utilities/Plugin_Request_Utility_Tests.php | 2 +- 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/assets/js/plugin-check-admin.js b/assets/js/plugin-check-admin.js index 50e427cb0..146899579 100644 --- a/assets/js/plugin-check-admin.js +++ b/assets/js/plugin-check-admin.js @@ -33,13 +33,15 @@ * @since n.e.x.t */ function setupEnvironment( data ) { - // Setup the form data to post. const pluginCheckData = new FormData(); pluginCheckData.append( 'nonce', pluginCheck.nonce ); pluginCheckData.append( 'plugin', data.plugin ); - pluginCheckData.append( 'checks', data.checks ); pluginCheckData.append( 'action', 'plugin_check_set_up_environment' ); + for (var i = 0; i < data.checks.length; i++) { + pluginCheckData.append('checks[]', data.checks[i]); + } + return fetch( ajaxurl, { @@ -111,11 +113,10 @@ * @since n.e.x.t */ function getChecksToRun() { - // Collect the data to pass along for generating a check results. const pluginCheckData = new FormData(); pluginCheckData.append( 'nonce', pluginCheck.nonce ); pluginCheckData.append( 'plugin', pluginsList.value ); - pluginCheckData.append( 'checks', [] ); + pluginCheckData.append( 'checks[]', [] ); pluginCheckData.append( 'action', 'plugin_check_get_checks_to_run' ); return fetch( @@ -153,9 +154,12 @@ const pluginCheckData = new FormData(); pluginCheckData.append( 'nonce', pluginCheck.nonce ); pluginCheckData.append( 'plugin', data.plugin ); - pluginCheckData.append( 'checks', data.checks ); pluginCheckData.append( 'action', 'plugin_check_run_checks' ); + for (var i = 0; i < data.checks.length; i++) { + pluginCheckData.append('checks[]', data.checks[i]); + } + return fetch( ajaxurl, { diff --git a/includes/Admin/Admin_AJAX.php b/includes/Admin/Admin_AJAX.php index 13d738639..907084d81 100644 --- a/includes/Admin/Admin_AJAX.php +++ b/includes/Admin/Admin_AJAX.php @@ -76,7 +76,7 @@ public function set_up_environment() { ); } - $checks = wp_parse_list( filter_input( INPUT_POST, 'checks', FILTER_SANITIZE_STRING ) ); + $checks = array_filter( filter_input( INPUT_POST, 'checks', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ) ); $plugin = filter_input( INPUT_POST, 'plugin', FILTER_SANITIZE_STRING ); try { @@ -157,7 +157,7 @@ public function get_checks_to_run() { wp_send_json_error( $valid_nonce, 403 ); } - $checks = wp_parse_list( filter_input( INPUT_POST, 'checks', FILTER_SANITIZE_STRING ) ); + $checks = array_filter( filter_input( INPUT_POST, 'checks', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ) ); $plugin = filter_input( INPUT_POST, 'plugin', FILTER_SANITIZE_STRING ); // Attempt to get the plugin basename based on the request. diff --git a/includes/Checker/AJAX_Runner.php b/includes/Checker/AJAX_Runner.php index 77409b324..0144c7c4d 100644 --- a/includes/Checker/AJAX_Runner.php +++ b/includes/Checker/AJAX_Runner.php @@ -74,7 +74,7 @@ protected function get_check_slugs_param() { if ( isset( $_REQUEST['checks'] ) ) { // Checks are passed as a comma separated string. - $checks = wp_parse_list( $_REQUEST['checks'] ); + $checks = array_filter( $_REQUEST['checks'] ); } return $checks; diff --git a/tests/Checker/AJAX_Runner_Tests.php b/tests/Checker/AJAX_Runner_Tests.php index 6a8620fc3..5c7f97707 100644 --- a/tests/Checker/AJAX_Runner_Tests.php +++ b/tests/Checker/AJAX_Runner_Tests.php @@ -50,7 +50,7 @@ public function test_prepare_with_runtime_check() { add_filter( 'wp_doing_ajax', '__return_true' ); $_REQUEST['action'] = 'plugin_check_run_checks'; $_REQUEST['plugin'] = 'plugin-check'; - $_REQUEST['checks'] = 'runtime_check'; + $_REQUEST['checks'] = array( 'runtime_check' ); add_filter( 'wp_plugin_check_checks', @@ -82,7 +82,7 @@ public function test_prepare_with_static_check() { add_filter( 'wp_doing_ajax', '__return_true' ); $_REQUEST['action'] = 'plugin_check_run_checks'; $_REQUEST['plugin'] = 'plugin-check'; - $_REQUEST['checks'] = 'empty_check'; + $_REQUEST['checks'] = array( 'empty_check' ); add_filter( 'wp_plugin_check_checks', @@ -114,7 +114,7 @@ public function test_run() { add_filter( 'wp_doing_ajax', '__return_true' ); $_REQUEST['action'] = 'plugin_check_run_checks'; $_REQUEST['plugin'] = 'plugin-check'; - $_REQUEST['checks'] = 'empty_check'; + $_REQUEST['checks'] = array( 'empty_check' ); add_filter( 'wp_plugin_check_checks', @@ -138,7 +138,7 @@ public function test_run_with_errors() { add_filter( 'wp_doing_ajax', '__return_true' ); $_REQUEST['action'] = 'plugin_check_run_checks'; $_REQUEST['plugin'] = 'plugin-check'; - $_REQUEST['checks'] = 'error_check'; + $_REQUEST['checks'] = array( 'error_check' ); add_filter( 'wp_plugin_check_checks', @@ -167,7 +167,7 @@ public function test_runner_initialized_early_throws_plugin_basename_exception() add_filter( 'wp_doing_ajax', '__return_true' ); $_REQUEST['action'] = 'plugin_check_run_checks'; $_REQUEST['plugin'] = 'plugin-check'; - $_REQUEST['checks'] = 'empty_check'; + $_REQUEST['checks'] = array( 'empty_check' ); $muplugins_loaded = $wp_actions['muplugins_loaded']; unset( $wp_actions['muplugins_loaded'] ); @@ -191,7 +191,7 @@ public function test_runner_initialized_early_throws_checks_exception() { add_filter( 'wp_doing_ajax', '__return_true' ); $_REQUEST['action'] = 'plugin_check_run_checks'; $_REQUEST['plugin'] = 'plugin-check'; - $_REQUEST['checks'] = 'empty_check'; + $_REQUEST['checks'] = array( 'empty_check' ); $muplugins_loaded = $wp_actions['muplugins_loaded']; unset( $wp_actions['muplugins_loaded'] ); diff --git a/tests/Utilities/Plugin_Request_Utility_Tests.php b/tests/Utilities/Plugin_Request_Utility_Tests.php index e21ec6777..4952c79c6 100644 --- a/tests/Utilities/Plugin_Request_Utility_Tests.php +++ b/tests/Utilities/Plugin_Request_Utility_Tests.php @@ -118,7 +118,7 @@ public function test_destroy_runner_with_ajax() { add_filter( 'wp_doing_ajax', '__return_true' ); $_REQUEST['action'] = 'plugin_check_run_checks'; $_REQUEST['plugin'] = 'plugin-check'; - $_REQUEST['checks'] = 'runtime_check'; + $_REQUEST['checks'] = array( 'runtime_check' ); add_filter( 'wp_plugin_check_checks', From 86fa8d2e9a1fbd17f9550dae4cba469e176b52b2 Mon Sep 17 00:00:00 2001 From: jjgrainger Date: Wed, 22 Mar 2023 13:07:35 +0000 Subject: [PATCH 11/13] fix function names --- assets/js/plugin-check-admin.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/assets/js/plugin-check-admin.js b/assets/js/plugin-check-admin.js index 146899579..ea6b3f5b8 100644 --- a/assets/js/plugin-check-admin.js +++ b/assets/js/plugin-check-admin.js @@ -12,9 +12,9 @@ e.preventDefault(); getChecksToRun() - .then( setupEnvironment ) + .then( setUpEnvironment ) .then( runChecks ) - .then( cleanupEnvironment ) + .then( cleanUpEnvironment ) .then( ( data ) => { console.log( data.message ); @@ -32,7 +32,7 @@ * * @since n.e.x.t */ - function setupEnvironment( data ) { + function setUpEnvironment( data ) { const pluginCheckData = new FormData(); pluginCheckData.append( 'nonce', pluginCheck.nonce ); pluginCheckData.append( 'plugin', data.plugin ); @@ -74,7 +74,7 @@ * * @since n.e.x.t */ - function cleanupEnvironment( data ) { + function cleanUpEnvironment( data ) { const pluginCheckData = new FormData(); pluginCheckData.append( 'nonce', pluginCheck.nonce ); pluginCheckData.append( 'action', 'plugin_check_clean_up_environment' ); From eb9d97c4423fa0b95c4c0702f2412a425454b730 Mon Sep 17 00:00:00 2001 From: jjgrainger Date: Wed, 22 Mar 2023 13:07:48 +0000 Subject: [PATCH 12/13] make checks parameter optional --- assets/js/plugin-check-admin.js | 5 ++--- includes/Admin/Admin_AJAX.php | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/assets/js/plugin-check-admin.js b/assets/js/plugin-check-admin.js index ea6b3f5b8..9c2a3f13f 100644 --- a/assets/js/plugin-check-admin.js +++ b/assets/js/plugin-check-admin.js @@ -39,7 +39,7 @@ pluginCheckData.append( 'action', 'plugin_check_set_up_environment' ); for (var i = 0; i < data.checks.length; i++) { - pluginCheckData.append('checks[]', data.checks[i]); + pluginCheckData.append( 'checks[]', data.checks[ i ] ); } return fetch( @@ -116,7 +116,6 @@ const pluginCheckData = new FormData(); pluginCheckData.append( 'nonce', pluginCheck.nonce ); pluginCheckData.append( 'plugin', pluginsList.value ); - pluginCheckData.append( 'checks[]', [] ); pluginCheckData.append( 'action', 'plugin_check_get_checks_to_run' ); return fetch( @@ -157,7 +156,7 @@ pluginCheckData.append( 'action', 'plugin_check_run_checks' ); for (var i = 0; i < data.checks.length; i++) { - pluginCheckData.append('checks[]', data.checks[i]); + pluginCheckData.append( 'checks[]', data.checks[ i ] ); } return fetch( diff --git a/includes/Admin/Admin_AJAX.php b/includes/Admin/Admin_AJAX.php index 907084d81..b064ca3fd 100644 --- a/includes/Admin/Admin_AJAX.php +++ b/includes/Admin/Admin_AJAX.php @@ -76,7 +76,7 @@ public function set_up_environment() { ); } - $checks = array_filter( filter_input( INPUT_POST, 'checks', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ) ); + $checks = isset( $_REQUEST['checks'] ) ? array_filter( $_REQUEST['checks'] ) : array(); $plugin = filter_input( INPUT_POST, 'plugin', FILTER_SANITIZE_STRING ); try { @@ -157,7 +157,7 @@ public function get_checks_to_run() { wp_send_json_error( $valid_nonce, 403 ); } - $checks = array_filter( filter_input( INPUT_POST, 'checks', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ) ); + $checks = isset( $_REQUEST['checks'] ) ? array_filter( $_REQUEST['checks'] ) : array(); $plugin = filter_input( INPUT_POST, 'plugin', FILTER_SANITIZE_STRING ); // Attempt to get the plugin basename based on the request. From 7f8abd0aa44d3a8ac86d86e3dabae8ab325e87c2 Mon Sep 17 00:00:00 2001 From: jjgrainger Date: Wed, 22 Mar 2023 18:23:52 +0000 Subject: [PATCH 13/13] use filter_input and FILTER_FORCE_ARRAY for checks parameter --- includes/Admin/Admin_AJAX.php | 4 ++-- includes/Checker/AJAX_Runner.php | 9 +-------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/includes/Admin/Admin_AJAX.php b/includes/Admin/Admin_AJAX.php index b064ca3fd..9a1b3c611 100644 --- a/includes/Admin/Admin_AJAX.php +++ b/includes/Admin/Admin_AJAX.php @@ -76,7 +76,7 @@ public function set_up_environment() { ); } - $checks = isset( $_REQUEST['checks'] ) ? array_filter( $_REQUEST['checks'] ) : array(); + $checks = filter_input( INPUT_POST, 'checks', FILTER_DEFAULT, FILTER_FORCE_ARRAY ); $plugin = filter_input( INPUT_POST, 'plugin', FILTER_SANITIZE_STRING ); try { @@ -157,7 +157,7 @@ public function get_checks_to_run() { wp_send_json_error( $valid_nonce, 403 ); } - $checks = isset( $_REQUEST['checks'] ) ? array_filter( $_REQUEST['checks'] ) : array(); + $checks = filter_input( INPUT_POST, 'checks', FILTER_DEFAULT, FILTER_FORCE_ARRAY ); $plugin = filter_input( INPUT_POST, 'plugin', FILTER_SANITIZE_STRING ); // Attempt to get the plugin basename based on the request. diff --git a/includes/Checker/AJAX_Runner.php b/includes/Checker/AJAX_Runner.php index 0144c7c4d..7ebd3b8fe 100644 --- a/includes/Checker/AJAX_Runner.php +++ b/includes/Checker/AJAX_Runner.php @@ -70,13 +70,6 @@ protected function get_plugin_param() { * @return array An array of Check slugs to run. */ protected function get_check_slugs_param() { - $checks = array(); - - if ( isset( $_REQUEST['checks'] ) ) { - // Checks are passed as a comma separated string. - $checks = array_filter( $_REQUEST['checks'] ); - } - - return $checks; + return filter_input( INPUT_POST, 'checks', FILTER_DEFAULT, FILTER_FORCE_ARRAY ); } }