From 8e0bbbe9a3232836cd70ee9f620bb8d63c5b38d5 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Tue, 12 Nov 2024 12:37:22 +0545 Subject: [PATCH 1/4] Add more checks for Plugin Header fields --- .../Plugin_Header_Fields_Check.php | 74 ++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) 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 363ec5d60..f184f251f 100644 --- a/includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php +++ b/includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php @@ -132,7 +132,22 @@ public function run( Check_Result $result ) { } } - 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' ) @@ -155,6 +170,40 @@ public function run( Check_Result $result ) { } } + 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( + /* translators: %s: plugin header field */ + __( 'The "%s" header in the plugin file should only contain numeric and period characters.', 'plugin-check' ), + 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 + ); + } + } + if ( ! empty( $plugin_header['AuthorURI'] ) ) { if ( true !== $this->is_valid_url( $plugin_header['AuthorURI'] ) ) { $this->add_result_warning_for_file( @@ -174,6 +223,29 @@ public function run( Check_Result $result ) { } } + 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( + /* 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 + ); + } + } + if ( ! empty( $plugin_header['Network'] ) ) { if ( 'true' !== strtolower( $plugin_header['Network'] ) ) { $this->add_result_warning_for_file( From 44934320213993172bf28689ff9721864f93d380 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Tue, 12 Nov 2024 14:15:54 +0545 Subject: [PATCH 2/4] Add unit test for added checks --- .../load.php | 1 - .../test-plugin-late-escaping-errors/load.php | 6 +++--- .../Checks/Plugin_Header_Fields_Check_Tests.php | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/testdata/plugins/test-plugin-header-fields-with-errors/load.php b/tests/phpunit/testdata/plugins/test-plugin-header-fields-with-errors/load.php index d2cf4b93e..6cd19caac 100644 --- a/tests/phpunit/testdata/plugins/test-plugin-header-fields-with-errors/load.php +++ b/tests/phpunit/testdata/plugins/test-plugin-header-fields-with-errors/load.php @@ -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 diff --git a/tests/phpunit/testdata/plugins/test-plugin-late-escaping-errors/load.php b/tests/phpunit/testdata/plugins/test-plugin-late-escaping-errors/load.php index cef8b3a59..3f3a05b1f 100644 --- a/tests/phpunit/testdata/plugins/test-plugin-late-escaping-errors/load.php +++ b/tests/phpunit/testdata/plugins/test-plugin-late-escaping-errors/load.php @@ -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 */ diff --git a/tests/phpunit/tests/Checker/Checks/Plugin_Header_Fields_Check_Tests.php b/tests/phpunit/tests/Checker/Checks/Plugin_Header_Fields_Check_Tests.php index 54ef73e85..6c4d8af8d 100644 --- a/tests/phpunit/tests/Checker/Checks/Plugin_Header_Fields_Check_Tests.php +++ b/tests/phpunit/tests/Checker/Checks/Plugin_Header_Fields_Check_Tests.php @@ -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' ) ) ); @@ -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' ) ) ); + } } From 21755bf5bec2ef6bf52eaab23218aada48335950 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Wed, 27 Nov 2024 10:27:59 +0545 Subject: [PATCH 3/4] Update regex for version validation --- .../Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php | 4 ++-- .../plugins/test-plugin-late-escaping-errors/load.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) 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 591683ba4..2ec651898 100644 --- a/includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php +++ b/includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php @@ -186,12 +186,12 @@ public function run( Check_Result $result ) { 7 ); } else { - if ( preg_match( '|[^\d\.]|', $plugin_header['Version'] ) ) { + if ( ! preg_match( '/^[a-z0-9.-]+$/i', $plugin_header['Version'] ) ) { $this->add_result_error_for_file( $result, sprintf( /* translators: %s: plugin header field */ - __( 'The "%s" header in the plugin file should only contain numeric and period characters.', 'plugin-check' ), + __( 'The "%s" header in the plugin file should only contain numbers, letters, periods, and hyphens.', 'plugin-check' ), esc_html( $labels['Version'] ) ), 'plugin_header_invalid_plugin_version', diff --git a/tests/phpunit/testdata/plugins/test-plugin-late-escaping-errors/load.php b/tests/phpunit/testdata/plugins/test-plugin-late-escaping-errors/load.php index 3f3a05b1f..d524bcddc 100644 --- a/tests/phpunit/testdata/plugins/test-plugin-late-escaping-errors/load.php +++ b/tests/phpunit/testdata/plugins/test-plugin-late-escaping-errors/load.php @@ -4,7 +4,7 @@ * Plugin URI: https://github.com/WordPress/plugin-check * Requires at least: 6.0 * Requires PHP: 5.6 - * Version: 1.0.0-Beta + * Version: 1.0.0 Beta * Author: WordPress Performance Team * Author URI: https://github.com/wordpress/plugin-check/ * License: GPLv2 or later From e45151beb6847eaca0d8caf891ab906e57e6fabd Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Thu, 28 Nov 2024 10:49:29 +0545 Subject: [PATCH 4/4] Remove plugin_header_same_plugin_author_uri check --- .../Plugin_Header_Fields_Check.php | 23 ------------------- .../test-plugin-late-escaping-errors/load.php | 2 +- .../Plugin_Header_Fields_Check_Tests.php | 1 - 3 files changed, 1 insertion(+), 25 deletions(-) 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 2ec651898..3694ea721 100644 --- a/includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php +++ b/includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php @@ -223,29 +223,6 @@ public function run( Check_Result $result ) { } } - 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( - /* 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 - ); - } - } - if ( ! empty( $plugin_header['Network'] ) ) { if ( 'true' !== strtolower( $plugin_header['Network'] ) ) { $this->add_result_warning_for_file( diff --git a/tests/phpunit/testdata/plugins/test-plugin-late-escaping-errors/load.php b/tests/phpunit/testdata/plugins/test-plugin-late-escaping-errors/load.php index d524bcddc..a777dd97f 100644 --- a/tests/phpunit/testdata/plugins/test-plugin-late-escaping-errors/load.php +++ b/tests/phpunit/testdata/plugins/test-plugin-late-escaping-errors/load.php @@ -6,7 +6,7 @@ * Requires PHP: 5.6 * Version: 1.0.0 Beta * Author: WordPress Performance Team - * Author URI: https://github.com/wordpress/plugin-check/ + * Author URI: https://make.wordpress.org/performance/ * License: GPLv2 or later * License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html * Text Domain: test-plugin-check-errors diff --git a/tests/phpunit/tests/Checker/Checks/Plugin_Header_Fields_Check_Tests.php b/tests/phpunit/tests/Checker/Checks/Plugin_Header_Fields_Check_Tests.php index b82067972..b55799b10 100644 --- a/tests/phpunit/tests/Checker/Checks/Plugin_Header_Fields_Check_Tests.php +++ b/tests/phpunit/tests/Checker/Checks/Plugin_Header_Fields_Check_Tests.php @@ -88,6 +88,5 @@ public function test_run_with_invalid_header_fields() { $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' ) ) ); } }