Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce slug argument in CLI #650

Merged
merged 4 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' => '',
)
);

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'] );

$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 '';
}
}
52 changes: 51 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.2.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;
}

return $this->get_slug_param();
}

/** Gets the Check_Context for the plugin.
*
* @since 1.0.0
Expand All @@ -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 );
}
}

/**
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 @@
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;

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
29 changes: 28 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.2.0
* @var string
*/
protected $slug;

/**
* The minimum supported WordPress version of the plugin.
*
Expand All @@ -41,12 +49,14 @@ 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.
*
* @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 +77,12 @@ public function __construct( $main_file ) {
}
}
}

if ( ! empty( $slug ) ) {
$this->slug = $slug;
} else {
$this->slug = basename( dirname( $this->main_file ) );
}
}

/**
Expand All @@ -91,6 +107,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
4 changes: 4 additions & 0 deletions tests/phpunit/tests/Plugin_Context_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down