From 6f80489e95414b8858e23ee76bea43c8373c1393 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Thu, 19 Sep 2024 13:04:48 +0545 Subject: [PATCH 1/4] Introduce slug argument in CLI --- includes/CLI/Plugin_Check_Command.php | 5 ++ includes/Checker/AJAX_Runner.php | 11 ++++ includes/Checker/Abstract_Check_Runner.php | 52 ++++++++++++++++++- includes/Checker/CLI_Runner.php | 20 +++++++ .../Plugin_Header_Fields_Check.php | 4 +- includes/Plugin_Context.php | 28 +++++++++- tests/behat/features/plugin-check.feature | 42 +++++++++++++++ 7 files changed, 158 insertions(+), 4 deletions(-) diff --git a/includes/CLI/Plugin_Check_Command.php b/includes/CLI/Plugin_Check_Command.php index d975dba7e..1211517bc 100644 --- a/includes/CLI/Plugin_Check_Command.php +++ b/includes/CLI/Plugin_Check_Command.php @@ -113,6 +113,9 @@ public function __construct( Plugin_Context $plugin_context ) { * [--warning-severity=] * : Warning severity level. * + * [--slug=] + * : Slug to override the default. + * * ## EXAMPLES * * wp plugin check akismet @@ -145,6 +148,7 @@ public function check( $args, $assoc_args ) { 'severity' => '', 'error-severity' => '', 'warning-severity' => '', + 'slug' => '', ) ); @@ -194,6 +198,7 @@ static function ( $dirs ) use ( $excluded_files ) { $runner->set_check_slugs( $checks ); $runner->set_plugin( $plugin ); $runner->set_categories( $categories ); + $runner->set_slug( $options['slug'] ); $checks_to_run = $runner->get_checks_to_run(); } catch ( Exception $error ) { diff --git a/includes/Checker/AJAX_Runner.php b/includes/Checker/AJAX_Runner.php index 90613a934..874d41196 100644 --- a/includes/Checker/AJAX_Runner.php +++ b/includes/Checker/AJAX_Runner.php @@ -120,4 +120,15 @@ protected function get_categories_param() { return $categories; } + + /** + * Returns plugin slug parameter. + * + * @since 1.2.0 + * + * @return string Plugin slug parameter. + */ + protected function get_slug_param() { + return ''; + } } diff --git a/includes/Checker/Abstract_Check_Runner.php b/includes/Checker/Abstract_Check_Runner.php index 8cb612c5b..6e8d76b1f 100644 --- a/includes/Checker/Abstract_Check_Runner.php +++ b/includes/Checker/Abstract_Check_Runner.php @@ -18,6 +18,7 @@ * @since 1.0.0 * * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) + * @SuppressWarnings(PHPMD.TooManyPublicMethods) */ abstract class Abstract_Check_Runner implements Check_Runner { @@ -37,6 +38,14 @@ abstract class Abstract_Check_Runner implements Check_Runner { */ protected $check_slugs; + /** + * The plugin slug. + * + * @since 1.2.0 + * @var string + */ + protected $slug; + /** * The check slugs to exclude. * @@ -156,6 +165,15 @@ abstract protected function get_include_experimental_param(); */ abstract protected function get_categories_param(); + /** + * Returns plugin slug parameter. + * + * @since 1.0.0 + * + * @return string Plugin slug. + */ + abstract protected function get_slug_param(); + /** * Sets whether the runner class was initialized early. * @@ -590,6 +608,21 @@ final protected function get_categories() { return $this->get_categories_param(); } + /** + * Returns plugin slug. + * + * @since 1.2.0 + * + * @return string Plugin slug. + */ + final protected function get_slug() { + if ( null !== $this->slug ) { + return $this->slug; + } + + return $this->get_slug_param(); + } + /** Gets the Check_Context for the plugin. * * @since 1.0.0 @@ -599,7 +632,24 @@ final protected function get_categories() { private function get_check_context() { $plugin_basename = $this->get_plugin_basename(); $plugin_path = is_dir( $plugin_basename ) ? $plugin_basename : WP_PLUGIN_DIR . '/' . $plugin_basename; - return new Check_Context( $plugin_path ); + return new Check_Context( $plugin_path, $this->get_slug() ); + } + + /** + * Sets the plugin slug. + * + * @since 1.2.0 + * + * @param string $slug Plugin slug. + */ + final public function set_slug( $slug ) { + if ( ! empty( $slug ) ) { + $this->slug = $slug; + } else { + $basename = $this->get_plugin_basename(); + + $this->slug = ( '.' === pathinfo( $basename, PATHINFO_DIRNAME ) ) ? $basename : dirname( $basename ); + } } /** diff --git a/includes/Checker/CLI_Runner.php b/includes/Checker/CLI_Runner.php index f5743b16c..57bab8494 100644 --- a/includes/Checker/CLI_Runner.php +++ b/includes/Checker/CLI_Runner.php @@ -158,6 +158,26 @@ protected function get_categories_param() { return $categories; } + /** + * Returns plugin slug parameter. + * + * @since 1.2.0 + * + * @return string Plugin slug parameter. + */ + protected function get_slug_param() { + $slug = ''; + + foreach ( $_SERVER['argv'] as $value ) { + if ( false !== strpos( $value, '--slug=' ) ) { + $slug = str_replace( '--slug=', '', $value ); + break; + } + } + + return $slug; + } + /** * Checks whether the current environment allows for runtime checks to be used. * diff --git a/includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php b/includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php index af1496c0d..4c6ef2c1f 100644 --- a/includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php +++ b/includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php @@ -231,9 +231,9 @@ public function run( Check_Result $result ) { if ( ! $result->plugin()->is_single_file_plugin() ) { if ( ! empty( $plugin_header['TextDomain'] ) ) { - $plugin_slug = basename( $result->plugin()->path() ); + $plugin_slug = $result->plugin()->slug(); - if ( ! empty( $plugin_slug ) && $plugin_slug !== $plugin_header['TextDomain'] ) { + if ( $plugin_slug !== $plugin_header['TextDomain'] ) { $this->add_result_warning_for_file( $result, sprintf( diff --git a/includes/Plugin_Context.php b/includes/Plugin_Context.php index e0c4923a8..ea4ccf1b4 100644 --- a/includes/Plugin_Context.php +++ b/includes/Plugin_Context.php @@ -29,6 +29,14 @@ class Plugin_Context { */ protected $main_file; + /** + * Plugin slug. + * + * @since 1.0.0 + * @var string + */ + protected $slug; + /** * The minimum supported WordPress version of the plugin. * @@ -43,10 +51,11 @@ class Plugin_Context { * @since 1.0.0 * * @param string $main_file The absolute path to the plugin main file. + * @param string $slug The plugin slug. * * @throws Exception Throws exception if not called via regular WP-CLI or WordPress bootstrap order. */ - public function __construct( $main_file ) { + public function __construct( $main_file, $slug = '' ) { if ( function_exists( 'wp_normalize_path' ) ) { $this->main_file = wp_normalize_path( $main_file ); } elseif ( function_exists( '\WP_CLI\Utils\normalize_path' ) ) { @@ -67,6 +76,12 @@ public function __construct( $main_file ) { } } } + + if ( ! empty( $slug ) ) { + $this->slug = $slug; + } else { + $this->slug = dirname( $this->main_file ); + } } /** @@ -91,6 +106,17 @@ public function main_file() { return $this->main_file; } + /** + * Returns the plugin slug. + * + * @since 1.2.0 + * + * @return string Plugin slug. + */ + public function slug() { + return $this->slug; + } + /** * Returns the absolute path for a relative path to the plugin directory. * diff --git a/tests/behat/features/plugin-check.feature b/tests/behat/features/plugin-check.feature index 430c30cb0..b8358c055 100644 --- a/tests/behat/features/plugin-check.feature +++ b/tests/behat/features/plugin-check.feature @@ -364,6 +364,48 @@ Feature: Test that the WP-CLI command works. no_plugin_readme """ + Scenario: Check a plugin from external location with custom plugin slug + Given a WP install with the Plugin Check plugin + And an empty external-folder/pxzvccv345nhg directory + And a external-folder/pxzvccv345nhg/foo-sample.php file: + """ + Date: Fri, 20 Sep 2024 13:16:05 +0545 Subject: [PATCH 2/4] Find correct plugin slug if not given --- includes/Checker/Abstract_Check_Runner.php | 2 +- includes/Plugin_Context.php | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/includes/Checker/Abstract_Check_Runner.php b/includes/Checker/Abstract_Check_Runner.php index 6e8d76b1f..f90e95662 100644 --- a/includes/Checker/Abstract_Check_Runner.php +++ b/includes/Checker/Abstract_Check_Runner.php @@ -168,7 +168,7 @@ abstract protected function get_categories_param(); /** * Returns plugin slug parameter. * - * @since 1.0.0 + * @since 1.2.0 * * @return string Plugin slug. */ diff --git a/includes/Plugin_Context.php b/includes/Plugin_Context.php index ea4ccf1b4..9cdde25fc 100644 --- a/includes/Plugin_Context.php +++ b/includes/Plugin_Context.php @@ -32,7 +32,7 @@ class Plugin_Context { /** * Plugin slug. * - * @since 1.0.0 + * @since 1.2.0 * @var string */ protected $slug; @@ -49,6 +49,7 @@ class Plugin_Context { * Constructor. * * @since 1.0.0 + * @since 1.2.0 Second argument $slug was introduced. * * @param string $main_file The absolute path to the plugin main file. * @param string $slug The plugin slug. @@ -80,7 +81,7 @@ public function __construct( $main_file, $slug = '' ) { if ( ! empty( $slug ) ) { $this->slug = $slug; } else { - $this->slug = dirname( $this->main_file ); + $this->slug = basename( dirname( $this->main_file ) ); } } From 457fdca2738f98e2af40220b509e21fb64775e8f Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Fri, 20 Sep 2024 13:26:03 +0545 Subject: [PATCH 3/4] Add unit test for slug --- tests/phpunit/tests/Plugin_Context_Tests.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/phpunit/tests/Plugin_Context_Tests.php b/tests/phpunit/tests/Plugin_Context_Tests.php index e930aecb0..cdda423c0 100644 --- a/tests/phpunit/tests/Plugin_Context_Tests.php +++ b/tests/phpunit/tests/Plugin_Context_Tests.php @@ -49,6 +49,10 @@ public function test_location() { $this->assertSame( WP_PLUGIN_DIR . '/' . $this->plugin_name . '/', $this->plugin_context->location() ); } + public function test_slug() { + $this->assertSame( $this->plugin_name, $this->plugin_context->slug() ); + } + public function test_location_with_single_file_plugin() { $single_file = WP_PLUGIN_DIR . '/single-file-plugin.php'; $context = new Plugin_Context( $single_file ); From 1730708bb4cec15eb0fd71ce25e8c75661466bb6 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Fri, 20 Sep 2024 22:44:48 +0545 Subject: [PATCH 4/4] Update markdown docs for CLI --- docs/CLI.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/CLI.md b/docs/CLI.md index fe31e4640..7c5926dc5 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -56,6 +56,9 @@ By default, `.git`, `vendor` and `node_modules` directories are excluded. [--warning-severity=] : Warning severity level. + +[--slug=] +: Slug to override the default. ``` ## EXAMPLES ```