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 2471b8c60..3694ea721 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( '/^[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 numbers, letters, periods, and hyphens.', '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( 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..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 @@ -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/ * 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 d551c6c83..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 @@ -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,19 @@ 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' ) ) ); + } }