diff --git a/cli.php b/cli.php index 7d45f9776..fe52e6fdd 100644 --- a/cli.php +++ b/cli.php @@ -11,7 +11,7 @@ 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\CLI\Plugin_Check_Categories_Command; +use WordPress\Plugin_Check\CLI\Plugin_List_Check_Categories_Command; use WordPress\Plugin_Check\CLI\Plugin_Check_Checks_Command; if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) { @@ -37,8 +37,8 @@ // Create the CLI command instance and add to WP CLI. $plugin_command = new Plugin_Check_Command( $context ); WP_CLI::add_command( 'plugin', $plugin_command ); -$plugin_categories_command = new Plugin_Check_Categories_Command( $context ); -WP_CLI::add_command( 'plugin', $plugin_categories_command ); +$plugin_list_check_categories_command = new Plugin_List_Check_Categories_Command( $context ); +WP_CLI::add_command( 'plugin', $plugin_list_check_categories_command ); $plugin_checks_command = new Plugin_Check_Checks_Command( $context ); WP_CLI::add_command( 'plugin', $plugin_checks_command ); diff --git a/includes/CLI/Plugin_Check_Checks_Command.php b/includes/CLI/Plugin_Check_Checks_Command.php index 038abab19..2c2bd33ea 100644 --- a/includes/CLI/Plugin_Check_Checks_Command.php +++ b/includes/CLI/Plugin_Check_Checks_Command.php @@ -7,7 +7,7 @@ namespace WordPress\Plugin_Check\CLI; -use WordPress\Plugin_Check\Checker\CLI_Checks_Runner; +use WordPress\Plugin_Check\Checker\Default_Check_Repository; use WordPress\Plugin_Check\Plugin_Context; use WordPress\Plugin_Check\Utilities\Plugin_Request_Utility; use WP_CLI; @@ -79,27 +79,14 @@ public function __construct( Plugin_Context $plugin_context ) { * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function check_checks( $args, $assoc_args ) { - // Get the Runner. - $runner = Plugin_Request_Utility::get_runner(); - - // Create the runner if not already initialized. - if ( is_null( $runner ) ) { - $runner = new CLI_Checks_Runner(); - } - - // Make sure we are using the correct runner instance. - if ( ! ( $runner instanceof CLI_Checks_Runner ) ) { - WP_CLI::error( - __( 'CLI Checks Runner was not initialized correctly.', 'plugin-check' ) - ); - } + $check_repo = new Default_Check_Repository(); $all_checks = array(); - $checks_to_run = $runner->get_checks_to_run(); + $checks_items = $check_repo->get_checks()->to_map(); - if ( is_array( $checks_to_run ) && count( $checks_to_run ) > 0 ) { - foreach ( $checks_to_run as $key => $check ) { + if ( is_array( $checks_items ) && count( $checks_items ) > 0 ) { + foreach ( $checks_items as $key => $check ) { $item = array(); $item['slug'] = $key; diff --git a/includes/CLI/Plugin_Check_Categories_Command.php b/includes/CLI/Plugin_List_Check_Categories_Command.php similarity index 84% rename from includes/CLI/Plugin_Check_Categories_Command.php rename to includes/CLI/Plugin_List_Check_Categories_Command.php index 81643b4ef..ed962aa38 100644 --- a/includes/CLI/Plugin_Check_Categories_Command.php +++ b/includes/CLI/Plugin_List_Check_Categories_Command.php @@ -1,6 +1,6 @@ plugin_context->basename() ) ) { - WP_CLI::error( - __( 'Plugin Checker is not active.', 'plugin-check' ) - ); - } - + public function list_check_categories( $args, $assoc_args ) { // Get options based on the CLI arguments. $options = $this->get_options( $assoc_args ); diff --git a/includes/Checker/Abstract_Check_Runner.php b/includes/Checker/Abstract_Check_Runner.php index 7aa5df10e..de4ddae79 100644 --- a/includes/Checker/Abstract_Check_Runner.php +++ b/includes/Checker/Abstract_Check_Runner.php @@ -151,8 +151,8 @@ abstract protected function get_categories_param(); * @since n.e.x.t */ final public function __construct() { - $this->initialized_early = ! did_action( 'muplugins_loaded' ); - $this->register_checks(); + $this->initialized_early = ! did_action( 'muplugins_loaded' ); + $this->check_repository = new Default_Check_Repository(); $this->runtime_environment = new Runtime_Environment_Setup(); } @@ -529,47 +529,6 @@ private function get_check_context() { return new Check_Context( WP_PLUGIN_DIR . '/' . $this->get_plugin_basename() ); } - /** - * Registers Checks to the Check_Repository. - * - * @since n.e.x.t - */ - private function register_checks() { - $this->check_repository = new Default_Check_Repository(); - - /** - * Filters the available plugin check classes. - * - * @since n.e.x.t - * - * @param array $checks An array map of check slugs to Check instances. - */ - $checks = apply_filters( - 'wp_plugin_check_checks', - array( - 'i18n_usage' => new Checks\I18n_Usage_Check(), - 'enqueued_scripts_size' => new Checks\Enqueued_Scripts_Size_Check(), - 'code_obfuscation' => new Checks\Code_Obfuscation_Check(), - 'file_type' => new Checks\File_Type_Check(), - 'plugin_header_text_domain' => new Checks\Plugin_Header_Text_Domain_Check(), - 'late_escaping' => new Checks\Late_Escaping_Check(), - 'plugin_updater' => new Checks\Plugin_Updater_Check(), - 'plugin_review_phpcs' => new Checks\Plugin_Review_PHPCS_Check(), - 'performant_wp_query_params' => new Checks\Performant_WP_Query_Params_Check(), - 'enqueued_scripts_in_footer' => new Checks\Enqueued_Scripts_In_Footer_Check(), - 'plugin_readme' => new Checks\Plugin_Readme_Check(), - 'enqueued_styles_scope' => new Checks\Enqueued_Styles_Scope_Check(), - 'localhost' => new Checks\Localhost_Check(), - 'no_unfiltered_uploads' => new Checks\No_Unfiltered_Uploads_Check(), - 'trademarks' => new Checks\Trademarks_Check(), - ) - ); - - foreach ( $checks as $slug => $check ) { - $this->check_repository->register_check( $slug, $check ); - } - } - /** * Sets the runtime environment setup. * diff --git a/includes/Checker/CLI_Checks_Runner.php b/includes/Checker/CLI_Checks_Runner.php deleted file mode 100644 index 54d1eb60c..000000000 --- a/includes/Checker/CLI_Checks_Runner.php +++ /dev/null @@ -1,106 +0,0 @@ - count( $_SERVER['argv'] ) ) { - return false; - } - - if ( - 'wp' === substr( $_SERVER['argv'][0], -2 ) && - 'plugin' === $_SERVER['argv'][1] && - 'check-checks' === $_SERVER['argv'][2] - ) { - return true; - } - - return false; - } - - /** - * Returns the plugin parameter based on the request. - * - * @since n.e.x.t - * - * @return string The plugin parameter. - * - * @throws Exception Thrown if the plugin parameter is empty. - */ - protected function get_plugin_param() { - return 'plugin-check'; - } - - /** - * Returns an array of Check slugs to run based on the request. - * - * @since n.e.x.t - * - * @return array An array of Check slugs to run. - */ - protected function get_check_slugs_param() { - return array(); - } - - /** - * Returns an array of Check slugs to exclude based on the request. - * - * @since n.e.x.t - * - * @return array An array of Check slugs to run. - */ - protected function get_check_exclude_slugs_param() { - return array(); - } - - /** - * Returns the include experimental parameter based on the request. - * - * @since n.e.x.t - * - * @return bool Returns true to include experimental checks else false. - */ - protected function get_include_experimental_param() { - return false; - } - - /** - * Returns an array of categories for filtering the checks. - * - * @since n.e.x.t - * - * @return array An array of categories. - */ - protected function get_categories_param() { - return array(); - } -} diff --git a/includes/Checker/Default_Check_Repository.php b/includes/Checker/Default_Check_Repository.php index 3199d41fa..139c1baa1 100644 --- a/includes/Checker/Default_Check_Repository.php +++ b/includes/Checker/Default_Check_Repository.php @@ -32,6 +32,15 @@ class Default_Check_Repository implements Check_Repository { */ protected $static_checks = array(); + /** + * Initializes checks. + * + * @since n.e.x.t + */ + public function __construct() { + $this->register_default_checks(); + } + /** * Registers a check to the repository. * @@ -108,4 +117,43 @@ static function ( Check $check ) { } ); } + + /** + * Registers Checks. + * + * @since n.e.x.t + */ + private function register_default_checks() { + /** + * Filters the available plugin check classes. + * + * @since n.e.x.t + * + * @param array $checks An array map of check slugs to Check instances. + */ + $checks = apply_filters( + 'wp_plugin_check_checks', + array( + 'i18n_usage' => new Checks\I18n_Usage_Check(), + 'enqueued_scripts_size' => new Checks\Enqueued_Scripts_Size_Check(), + 'code_obfuscation' => new Checks\Code_Obfuscation_Check(), + 'file_type' => new Checks\File_Type_Check(), + 'plugin_header_text_domain' => new Checks\Plugin_Header_Text_Domain_Check(), + 'late_escaping' => new Checks\Late_Escaping_Check(), + 'plugin_updater' => new Checks\Plugin_Updater_Check(), + 'plugin_review_phpcs' => new Checks\Plugin_Review_PHPCS_Check(), + 'performant_wp_query_params' => new Checks\Performant_WP_Query_Params_Check(), + 'enqueued_scripts_in_footer' => new Checks\Enqueued_Scripts_In_Footer_Check(), + 'plugin_readme' => new Checks\Plugin_Readme_Check(), + 'enqueued_styles_scope' => new Checks\Enqueued_Styles_Scope_Check(), + 'localhost' => new Checks\Localhost_Check(), + 'no_unfiltered_uploads' => new Checks\No_Unfiltered_Uploads_Check(), + 'trademarks' => new Checks\Trademarks_Check(), + ) + ); + + foreach ( $checks as $slug => $check ) { + $this->register_check( $slug, $check ); + } + } }