Skip to content

Commit

Permalink
Merge branch 'trunk' into 486-check-calling-remote-files-js-css-image…
Browse files Browse the repository at this point in the history
…s-etc-offloading-external
  • Loading branch information
ernilambar authored Sep 22, 2024
2 parents 497de88 + aaad52b commit 629ba2c
Show file tree
Hide file tree
Showing 33 changed files with 400 additions and 133 deletions.
2 changes: 1 addition & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ comment: false
# See https://docs.codecov.com/docs/ignoring-paths
ignore:
- drop-ins
- test-content
- runtime-content
- tests
3 changes: 3 additions & 0 deletions docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ By default, `.git`, `vendor` and `node_modules` directories are excluded.
[--warning-severity=<warning-severity>]
: Warning severity level.
[--slug=<slug>]
: Slug to override the default.
```
## EXAMPLES
```
Expand Down
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 @@ 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function () use ( $result ) {
if ( isset( $plugin_script['count'] ) && ( $url_count === $plugin_script['count'] ) ) {
$this->add_result_warning_for_file(
$result,
__( 'This script is being loaded in all contexts.', 'plugin-check' ),
__( 'This script is being loaded in all frontend contexts.', 'plugin-check' ),
'EnqueuedScriptsScope',
$plugin_script['path']
);
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
63 changes: 60 additions & 3 deletions includes/Checker/Checks/Plugin_Repo/Plugin_Readme_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ protected function check_files( Check_Result $result, array $files ) {

// Check the readme file for warnings.
$this->check_for_warnings( $result, $readme_file, $parser );

// Check the readme file for contributors.
$this->check_for_contributors( $result, $readme_file );
}

/**
Expand Down Expand Up @@ -146,14 +149,17 @@ private function check_name( Check_Result $result, string $readme_file, Parser $
} else {
$plugin_data = get_plugin_data( $result->plugin()->main_file() );

if ( $parser->name !== $plugin_data['Name'] ) {
$plugin_readme_name = html_entity_decode( $parser->name, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 );
$plugin_header_name = html_entity_decode( $plugin_data['Name'], ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 );

if ( $plugin_readme_name !== $plugin_header_name ) {
$this->add_result_warning_for_file(
$result,
sprintf(
/* translators: 1: Plugin name, 2: Name in plugin header */
__( 'Plugin name "%1$s" is different from the name declared in plugin header "%2$s".', 'plugin-check' ),
$parser->name,
$plugin_data['Name']
$plugin_readme_name,
$plugin_header_name
),
'mismatched_plugin_name',
$readme_file,
Expand Down Expand Up @@ -652,6 +658,57 @@ private function check_for_warnings( Check_Result $result, string $readme_file,
}
}

/**
* Checks the readme file for contributors.
*
* @since 1.2.0
*
* @param Check_Result $result The Check Result to amend.
* @param string $readme_file Readme file.
*/
private function check_for_contributors( Check_Result $result, string $readme_file ) {
$regex = '/Contributors\s?:(.*?)\R/';

$matches = array();

self::file_preg_match( $regex, array( $readme_file ), $matches );

// Bail if no "Contributors" found.
if ( empty( $matches ) ) {
return;
}

$usernames = explode( ',', $matches[1] );

$usernames = array_map( 'trim', $usernames );

$valid = true;

foreach ( $usernames as $username ) {
if ( 1 !== preg_match( '/^[a-z0-9\s_.\-@]+$/', $username ) ) {
$valid = false;
break;
}
}

if ( ! $valid ) {
$this->add_result_warning_for_file(
$result,
sprintf(
/* translators: %s: plugin header field */
__( 'The "%s" header in the readme file must be a comma-separated list of WordPress.org-formatted usernames.', 'plugin-check' ),
'Contributors'
),
'readme_invalid_contributors',
$readme_file,
0,
0,
'',
6
);
}
}

/**
* Returns current major WordPress version.
*
Expand Down
2 changes: 1 addition & 1 deletion includes/Checker/Checks/Plugin_Repo/Trademarks_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ private function check_for_slug( Check_Result $result ) {
return;
}

$plugin_slug = basename( $result->plugin()->path() );
$plugin_slug = $result->plugin()->slug();

try {
$this->validate_slug_has_no_trademarks( $plugin_slug );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public function prepare() {

if ( ! defined( 'WP_PLUGIN_CHECK_PLUGIN_DIR_PATH' ) ) {
$plugins_dir = defined( 'WP_PLUGIN_DIR' ) ? WP_PLUGIN_DIR : WP_CONTENT_DIR . '/plugins';
$theme_folder = $plugins_dir . '/plugin-check/test-content/themes';
$theme_folder = $plugins_dir . '/plugin-check/runtime-content/themes';
} else {
$theme_folder = WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'test-content/themes';
$theme_folder = WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'runtime-content/themes';
}

$use_minimal_theme_preparation = new Use_Minimal_Theme_Preparation( 'wp-empty-theme', $theme_folder );
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
2 changes: 1 addition & 1 deletion includes/Traits/File_Editor_URL.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function get_file_editor_url( Check_Result $result, $filename, $line =
$edit_url = null;

$plugin_path = $result->plugin()->path( '/' );
$plugin_slug = basename( $plugin_path );
$plugin_slug = $result->plugin()->slug();
$filename = str_replace( $plugin_path, '', $filename );
/**
* Filters the template for the URL for linking to an external editor to open a file for editing.
Expand Down
Loading

0 comments on commit 629ba2c

Please sign in to comment.