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

Add new checks for Plugin Header fields #786

Merged
merged 5 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,22 @@
}
}

if ( ! empty( $plugin_header['Description'] ) ) {
if ( empty( $plugin_header['Description'] ) ) {
$this->add_result_error_for_file(
$result,
sprintf(
/* translators: %s: plugin header field */
__( 'The "%s" header is missing in the plugin file.', 'plugin-check' ),
esc_html( $labels['Description'] )
),
'plugin_header_missing_plugin_description',
$plugin_main_file,
0,
0,
__( 'https://developer.wordpress.org/plugins/plugin-basics/header-requirements/', 'plugin-check' ),
7
);
} else {
if (
str_contains( $plugin_header['Description'], 'This is a short description of what the plugin does' )
|| str_contains( $plugin_header['Description'], 'Here is a short description of the plugin' )
Expand All @@ -155,6 +170,40 @@
}
}

if ( empty( $plugin_header['Version'] ) ) {
$this->add_result_error_for_file(
$result,
sprintf(
/* translators: %s: plugin header field */
__( 'The "%s" header is missing in the plugin file.', 'plugin-check' ),
esc_html( $labels['Version'] )
),
'plugin_header_missing_plugin_version',
$plugin_main_file,
0,
0,
__( 'https://developer.wordpress.org/plugins/plugin-basics/header-requirements/', 'plugin-check' ),
7
);
} else {
if ( preg_match( '|[^\d\.]|', $plugin_header['Version'] ) ) {
$this->add_result_error_for_file(
$result,
sprintf(

Check warning on line 192 in includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php#L190-L192

Added lines #L190 - L192 were not covered by tests
/* translators: %s: plugin header field */
__( 'The "%s" header in the plugin file should only contain numeric and period characters.', 'plugin-check' ),
ernilambar marked this conversation as resolved.
Show resolved Hide resolved
esc_html( $labels['Version'] )
),
'plugin_header_invalid_plugin_version',
$plugin_main_file,
0,
0,
__( 'https://developer.wordpress.org/plugins/plugin-basics/header-requirements/', 'plugin-check' ),
7
);

Check warning on line 203 in includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php#L194-L203

Added lines #L194 - L203 were not covered by tests
}
}

if ( ! empty( $plugin_header['AuthorURI'] ) ) {
if ( true !== $this->is_valid_url( $plugin_header['AuthorURI'] ) ) {
$this->add_result_warning_for_file(
Expand All @@ -174,6 +223,29 @@
}
}

if ( ! empty( $plugin_header['PluginURI'] ) && ! empty( $plugin_header['AuthorURI'] ) ) {
$plugin_uri = rtrim( strtolower( $plugin_header['PluginURI'] ), '/' );
$author_uri = rtrim( strtolower( $plugin_header['AuthorURI'] ), '/' );

if ( $plugin_uri === $author_uri ) {
$this->add_result_error_for_file(
$result,
sprintf(

Check warning on line 233 in includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php#L231-L233

Added lines #L231 - L233 were not covered by tests
/* translators: 1: plugin uri header field, 2: author uri header field */
__( 'The "%1$s" and "%2$s" header in the plugin file must be different. It is not required to provide both, so pick the one that best applies to your situation.', 'plugin-check' ),
esc_html( $labels['PluginURI'] ),
esc_html( $labels['AuthorURI'] )
),
'plugin_header_same_plugin_author_uri',
$plugin_main_file,
0,
0,
__( 'https://developer.wordpress.org/plugins/plugin-basics/header-requirements/', 'plugin-check' ),
7
davidperezgar marked this conversation as resolved.
Show resolved Hide resolved
);

Check warning on line 245 in includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php#L235-L245

Added lines #L235 - L245 were not covered by tests
}
}

if ( ! empty( $plugin_header['Network'] ) ) {
if ( 'true' !== strtolower( $plugin_header['Network'] ) ) {
$this->add_result_warning_for_file(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* Description: Here is a short description of the plugin.
* Requires at least: Recent version
* Requires PHP: Latest version
* Version: 1.0.0
* Author: WordPress Performance Team
* Author URI: This is not a valid URL
* Text Domain: test-mismathed-textdomain-here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
/**
* Plugin Name: Test Plugin escape output with Errors for Plugin Check
* Plugin URI: https://github.com/WordPress/plugin-check
* Description: Some plugin description.
* Requires at least: 6.0
* Requires PHP: 5.6
* Version: 1.0.0
* Version: 1.0.0-Beta
* Author: WordPress Performance Team
* Author URI: https://make.wordpress.org/performance/
* Author URI: https://github.com/wordpress/plugin-check/
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Text Domain: test-plugin-check-errors
* Domain Path: /languages
*
* @package test-plugin-check-errors
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function test_run_with_errors() {
$this->assertCount( 1, wp_list_filter( $errors['load.php'][0][0], array( 'code' => 'plugin_header_invalid_requires_wp' ) ) );
$this->assertCount( 1, wp_list_filter( $errors['load.php'][0][0], array( 'code' => 'plugin_header_invalid_requires_php' ) ) );
$this->assertCount( 1, wp_list_filter( $errors['load.php'][0][0], array( 'code' => 'plugin_header_no_license' ) ) );
$this->assertCount( 1, wp_list_filter( $errors['load.php'][0][0], array( 'code' => 'plugin_header_missing_plugin_version' ) ) );
$this->assertCount( 1, wp_list_filter( $warnings['load.php'][0][0], array( 'code' => 'plugin_header_invalid_plugin_uri_domain' ) ) );
$this->assertCount( 1, wp_list_filter( $warnings['load.php'][0][0], array( 'code' => 'plugin_header_invalid_plugin_description' ) ) );
$this->assertCount( 1, wp_list_filter( $warnings['load.php'][0][0], array( 'code' => 'plugin_header_invalid_author_uri' ) ) );
Expand Down Expand Up @@ -73,4 +74,20 @@ public function test_run_with_invalid_mpl1_license() {
// Check for invalid license.
$this->assertCount( 1, wp_list_filter( $errors['load.php'][0][0], array( 'code' => 'plugin_header_invalid_license' ) ) );
}

public function test_run_with_invalid_header_fields() {
$check = new Plugin_Header_Fields_Check();
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-late-escaping-errors/load.php' );
$check_result = new Check_Result( $check_context );

$check->run( $check_result );

$errors = $check_result->get_errors();

$this->assertNotEmpty( $errors );

$this->assertCount( 1, wp_list_filter( $errors['load.php'][0][0], array( 'code' => 'plugin_header_missing_plugin_description' ) ) );
$this->assertCount( 1, wp_list_filter( $errors['load.php'][0][0], array( 'code' => 'plugin_header_invalid_plugin_version' ) ) );
$this->assertCount( 1, wp_list_filter( $errors['load.php'][0][0], array( 'code' => 'plugin_header_same_plugin_author_uri' ) ) );
}
}