Skip to content

Commit

Permalink
Introduce slug argument in CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
ernilambar committed Sep 19, 2024
1 parent c187c17 commit ad7b8d2
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 4 deletions.
5 changes: 5 additions & 0 deletions includes/CLI/Plugin_Check_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ public function __construct( Plugin_Context $plugin_context ) {
* [--warning-severity=<warning-severity>]
* : Warning severity level.
*
* [--slug=<slug>]
* : Slug to override the default.
*
* ## EXAMPLES
*
* wp plugin check akismet
Expand Down Expand Up @@ -145,6 +148,7 @@ public function check( $args, $assoc_args ) {
'severity' => '',
'error-severity' => '',
'warning-severity' => '',
'slug' => '',

Check warning on line 151 in includes/CLI/Plugin_Check_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Command.php#L151

Added line #L151 was not covered by tests
)
);

Expand Down Expand Up @@ -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'] );

Check warning on line 201 in includes/CLI/Plugin_Check_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Command.php#L201

Added line #L201 was not covered by tests

$checks_to_run = $runner->get_checks_to_run();
} catch ( Exception $error ) {
Expand Down
11 changes: 11 additions & 0 deletions includes/Checker/AJAX_Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
}
}
50 changes: 49 additions & 1 deletion includes/Checker/Abstract_Check_Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* @since 1.0.0
*
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
*/
abstract class Abstract_Check_Runner implements Check_Runner {

Expand All @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;

Check warning on line 620 in includes/Checker/Abstract_Check_Runner.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Abstract_Check_Runner.php#L620

Added line #L620 was not covered by tests
}

return $this->get_slug_param();
}

/** Gets the Check_Context for the plugin.
*
* @since 1.0.0
Expand All @@ -599,7 +632,22 @@ 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;

Check warning on line 647 in includes/Checker/Abstract_Check_Runner.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Abstract_Check_Runner.php#L645-L647

Added lines #L645 - L647 were not covered by tests
} else {
$this->slug = $this->get_plugin_basename();

Check warning on line 649 in includes/Checker/Abstract_Check_Runner.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Abstract_Check_Runner.php#L649

Added line #L649 was not covered by tests
}
}

/**
Expand Down
20 changes: 20 additions & 0 deletions includes/Checker/CLI_Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = array();

foreach ( $_SERVER['argv'] as $value ) {
if ( false !== strpos( $value, '--slug=' ) ) {
$slug = str_replace( '--slug=', '', $value );
break;

Check warning on line 174 in includes/Checker/CLI_Runner.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/CLI_Runner.php#L173-L174

Added lines #L173 - L174 were not covered by tests
}
}

return $slug;
}

/**
* Checks whether the current environment allows for runtime checks to be used.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
28 changes: 27 additions & 1 deletion includes/Plugin_Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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' ) ) {
Expand All @@ -67,6 +76,12 @@ public function __construct( $main_file ) {
}
}
}

if ( ! empty( $slug ) ) {
$this->slug = $slug;

Check warning on line 81 in includes/Plugin_Context.php

View check run for this annotation

Codecov / codecov/patch

includes/Plugin_Context.php#L81

Added line #L81 was not covered by tests
} else {
$this->slug = basename( $this->path() );
}
}

/**
Expand All @@ -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.
*
Expand Down
42 changes: 42 additions & 0 deletions tests/behat/features/plugin-check.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
<?php
/**
* Plugin Name: Foo Sample
* Plugin URI: https://foo-sample.com
* Description:
* Version: 0.1.0
* Author:
* Author URI:
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: foo-sample
*/
"""

When I run the WP-CLI command `plugin check {RUN_DIR}/external-folder/pxzvccv345nhg/ --format=csv --fields=code,type`
Then STDERR should be empty
And STDOUT should contain:
"""
textdomain_mismatch,WARNING
"""
And STDOUT should contain:
"""
no_plugin_readme,WARNING
"""

When I run the WP-CLI command `plugin check {RUN_DIR}/external-folder/pxzvccv345nhg/ --format=csv --fields=code,type --slug=foo-sample`
Then STDERR should be empty
And STDOUT should not contain:
"""
textdomain_mismatch,WARNING
"""
And STDOUT should contain:
"""
no_plugin_readme,WARNING
"""

Scenario: Check a plugin from external location but with invalid plugin
Given a WP install with the Plugin Check plugin
And an empty external-folder/foo-plugin directory
Expand Down

0 comments on commit ad7b8d2

Please sign in to comment.