From c9662378cd7ba6550811d94a6196b2ea56511d2c Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Wed, 10 May 2023 17:34:02 +0530 Subject: [PATCH 1/4] Add util function --- cli.php | 13 ++++------ includes/Checker/CLI_Runner.php | 11 ++------- includes/Utilities/Plugin_Request_Utility.php | 24 +++++++++++++++++++ 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/cli.php b/cli.php index c8aa56e62..dee366f83 100644 --- a/cli.php +++ b/cli.php @@ -10,6 +10,7 @@ use WordPress\Plugin_Check\Plugin_Context; use WordPress\Plugin_Check\CLI\Plugin_Check_Command; +use WordPress\Plugin_Check\Utilities\Plugin_Request_Utility; if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) { return; @@ -44,20 +45,14 @@ WP_CLI::add_hook( 'before_wp_load', function() { - if ( empty( $_SERVER['argv'] ) || 3 > count( $_SERVER['argv'] ) ) { - return; - } - - if ( - 'wp' === substr( $_SERVER['argv'][0], -2 ) && - 'plugin' === $_SERVER['argv'][1] && - 'check' === $_SERVER['argv'][2] - ) { + if ( Plugin_Request_Utility::is_plugin_check() ) { if ( ! file_exists( ABSPATH . 'wp-content/object-cache.php' ) ) { if ( ! copy( __DIR__ . '/object-cache.copy.php', ABSPATH . 'wp-content/object-cache.php' ) ) { WP_CLI::error( 'Unable to copy object-cache.php file.' ); } } + } else { + return; } } ); diff --git a/includes/Checker/CLI_Runner.php b/includes/Checker/CLI_Runner.php index 50b6a2a43..f7125f3a3 100644 --- a/includes/Checker/CLI_Runner.php +++ b/includes/Checker/CLI_Runner.php @@ -8,6 +8,7 @@ namespace WordPress\Plugin_Check\Checker; use Exception; +use WordPress\Plugin_Check\Utilities\Plugin_Request_Utility; /** * CLI Runner class. @@ -32,15 +33,7 @@ class CLI_Runner extends Abstract_Check_Runner { * @return bool Returns true if is an CLI request for the plugin check else false. */ public function is_plugin_check() { - if ( empty( $_SERVER['argv'] ) || 3 > count( $_SERVER['argv'] ) ) { - return false; - } - - if ( - 'wp' === substr( $_SERVER['argv'][0], -2 ) && - 'plugin' === $_SERVER['argv'][1] && - 'check' === $_SERVER['argv'][2] - ) { + if ( Plugin_Request_Utility::is_plugin_check() ) { return true; } diff --git a/includes/Utilities/Plugin_Request_Utility.php b/includes/Utilities/Plugin_Request_Utility.php index d5e69a31d..a7cca6285 100644 --- a/includes/Utilities/Plugin_Request_Utility.php +++ b/includes/Utilities/Plugin_Request_Utility.php @@ -134,4 +134,28 @@ public static function destroy_runner() { static::$runner = null; } + + /** + * Checks if the current request is a CLI request for the Plugin Checker. + * + * @since n.e.x.t + * + * @return bool Returns true if is a CLI request for the plugin check else false. + */ + public static function is_plugin_check() { + if ( empty( $_SERVER['argv'] ) || 3 > count( $_SERVER['argv'] ) ) { + return false; + } + + if ( + 'wp' === $_SERVER['argv'][0] && + 'plugin' === $_SERVER['argv'][1] && + 'check' === $_SERVER['argv'][2] + ) { + return true; + } + + return false; + } + } From 0ff406cc2d5fafc18e513e5bbe786393860606d4 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Mon, 15 May 2023 19:38:23 +0530 Subject: [PATCH 2/4] Address review feedback --- cli.php | 6 ++--- includes/Checker/AJAX_Runner.php | 2 +- includes/Checker/Abstract_Check_Runner.php | 2 +- includes/Checker/CLI_Runner.php | 13 +++++++--- includes/Checker/Check_Runner.php | 2 +- includes/Utilities/Plugin_Request_Utility.php | 24 ------------------- 6 files changed, 15 insertions(+), 34 deletions(-) diff --git a/cli.php b/cli.php index dee366f83..fcd9cdb4b 100644 --- a/cli.php +++ b/cli.php @@ -8,9 +8,9 @@ * @since n.e.x.t */ +use WordPress\Plugin_Check\Checker\CLI_Runner; use WordPress\Plugin_Check\Plugin_Context; use WordPress\Plugin_Check\CLI\Plugin_Check_Command; -use WordPress\Plugin_Check\Utilities\Plugin_Request_Utility; if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) { return; @@ -45,14 +45,12 @@ WP_CLI::add_hook( 'before_wp_load', function() { - if ( Plugin_Request_Utility::is_plugin_check() ) { + if ( CLI_Runner::is_plugin_check() ) { if ( ! file_exists( ABSPATH . 'wp-content/object-cache.php' ) ) { if ( ! copy( __DIR__ . '/object-cache.copy.php', ABSPATH . 'wp-content/object-cache.php' ) ) { WP_CLI::error( 'Unable to copy object-cache.php file.' ); } } - } else { - return; } } ); diff --git a/includes/Checker/AJAX_Runner.php b/includes/Checker/AJAX_Runner.php index 7ebd3b8fe..3ef4ab40f 100644 --- a/includes/Checker/AJAX_Runner.php +++ b/includes/Checker/AJAX_Runner.php @@ -31,7 +31,7 @@ class AJAX_Runner extends Abstract_Check_Runner { * * @return bool Returns true if is an AJAX request for the plugin check else false. */ - public function is_plugin_check() { + static function is_plugin_check() { if ( ! wp_doing_ajax() ) { return false; } diff --git a/includes/Checker/Abstract_Check_Runner.php b/includes/Checker/Abstract_Check_Runner.php index b225588b6..965417414 100644 --- a/includes/Checker/Abstract_Check_Runner.php +++ b/includes/Checker/Abstract_Check_Runner.php @@ -65,7 +65,7 @@ abstract class Abstract_Check_Runner implements Check_Runner { * * @return bool Returns true if the check is for plugin else false. */ - abstract public function is_plugin_check(); + abstract static function is_plugin_check(); /** * Returns the plugin parameter based on the request. diff --git a/includes/Checker/CLI_Runner.php b/includes/Checker/CLI_Runner.php index f7125f3a3..3e277be07 100644 --- a/includes/Checker/CLI_Runner.php +++ b/includes/Checker/CLI_Runner.php @@ -8,7 +8,6 @@ namespace WordPress\Plugin_Check\Checker; use Exception; -use WordPress\Plugin_Check\Utilities\Plugin_Request_Utility; /** * CLI Runner class. @@ -32,8 +31,16 @@ class CLI_Runner extends Abstract_Check_Runner { * * @return bool Returns true if is an CLI request for the plugin check else false. */ - public function is_plugin_check() { - if ( Plugin_Request_Utility::is_plugin_check() ) { + static function is_plugin_check() { + if ( empty( $_SERVER['argv'] ) || 3 > count( $_SERVER['argv'] ) ) { + return false; + } + + if ( + 'wp' === substr( $_SERVER['argv'][0], -2 ) && + 'plugin' === $_SERVER['argv'][1] && + 'check' === $_SERVER['argv'][2] + ) { return true; } diff --git a/includes/Checker/Check_Runner.php b/includes/Checker/Check_Runner.php index bec52f227..4cc6d0c3a 100644 --- a/includes/Checker/Check_Runner.php +++ b/includes/Checker/Check_Runner.php @@ -23,7 +23,7 @@ interface Check_Runner { * * @return boolean Returns true if the check is for plugin else false. */ - public function is_plugin_check(); + static function is_plugin_check(); /** * Prepares the environment for running the requested checks. diff --git a/includes/Utilities/Plugin_Request_Utility.php b/includes/Utilities/Plugin_Request_Utility.php index a7cca6285..d5e69a31d 100644 --- a/includes/Utilities/Plugin_Request_Utility.php +++ b/includes/Utilities/Plugin_Request_Utility.php @@ -134,28 +134,4 @@ public static function destroy_runner() { static::$runner = null; } - - /** - * Checks if the current request is a CLI request for the Plugin Checker. - * - * @since n.e.x.t - * - * @return bool Returns true if is a CLI request for the plugin check else false. - */ - public static function is_plugin_check() { - if ( empty( $_SERVER['argv'] ) || 3 > count( $_SERVER['argv'] ) ) { - return false; - } - - if ( - 'wp' === $_SERVER['argv'][0] && - 'plugin' === $_SERVER['argv'][1] && - 'check' === $_SERVER['argv'][2] - ) { - return true; - } - - return false; - } - } From ba05b3357e95b48ff2e8ab0f7533ce7153b1ea28 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Mon, 15 May 2023 19:53:57 +0530 Subject: [PATCH 3/4] Fix lint issue --- includes/Checker/AJAX_Runner.php | 2 +- includes/Checker/CLI_Runner.php | 2 +- includes/Checker/Check_Runner.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/Checker/AJAX_Runner.php b/includes/Checker/AJAX_Runner.php index 3ef4ab40f..69b4daa08 100644 --- a/includes/Checker/AJAX_Runner.php +++ b/includes/Checker/AJAX_Runner.php @@ -31,7 +31,7 @@ class AJAX_Runner extends Abstract_Check_Runner { * * @return bool Returns true if is an AJAX request for the plugin check else false. */ - static function is_plugin_check() { + public static function is_plugin_check() { if ( ! wp_doing_ajax() ) { return false; } diff --git a/includes/Checker/CLI_Runner.php b/includes/Checker/CLI_Runner.php index 3e277be07..debfe557c 100644 --- a/includes/Checker/CLI_Runner.php +++ b/includes/Checker/CLI_Runner.php @@ -31,7 +31,7 @@ class CLI_Runner extends Abstract_Check_Runner { * * @return bool Returns true if is an CLI request for the plugin check else false. */ - static function is_plugin_check() { + public static function is_plugin_check() { if ( empty( $_SERVER['argv'] ) || 3 > count( $_SERVER['argv'] ) ) { return false; } diff --git a/includes/Checker/Check_Runner.php b/includes/Checker/Check_Runner.php index 4cc6d0c3a..27390283e 100644 --- a/includes/Checker/Check_Runner.php +++ b/includes/Checker/Check_Runner.php @@ -23,7 +23,7 @@ interface Check_Runner { * * @return boolean Returns true if the check is for plugin else false. */ - static function is_plugin_check(); + public static function is_plugin_check(); /** * Prepares the environment for running the requested checks. From b50a5657f3b7cfd11eee8dd47c96c384dae9330d Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Mon, 15 May 2023 20:01:55 +0530 Subject: [PATCH 4/4] Fix lint issue --- includes/Checker/Abstract_Check_Runner.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/Checker/Abstract_Check_Runner.php b/includes/Checker/Abstract_Check_Runner.php index 965417414..58f6c9045 100644 --- a/includes/Checker/Abstract_Check_Runner.php +++ b/includes/Checker/Abstract_Check_Runner.php @@ -65,7 +65,7 @@ abstract class Abstract_Check_Runner implements Check_Runner { * * @return bool Returns true if the check is for plugin else false. */ - abstract static function is_plugin_check(); + abstract public static function is_plugin_check(); /** * Returns the plugin parameter based on the request.