Skip to content

Commit f104682

Browse files
Merge pull request #786 from WordPress/783-checks-for-plugin-header-fields
Add new checks for Plugin Header fields
2 parents a79a2e8 + e45151b commit f104682

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed

includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,22 @@ public function run( Check_Result $result ) {
132132
}
133133
}
134134

135-
if ( ! empty( $plugin_header['Description'] ) ) {
135+
if ( empty( $plugin_header['Description'] ) ) {
136+
$this->add_result_error_for_file(
137+
$result,
138+
sprintf(
139+
/* translators: %s: plugin header field */
140+
__( 'The "%s" header is missing in the plugin file.', 'plugin-check' ),
141+
esc_html( $labels['Description'] )
142+
),
143+
'plugin_header_missing_plugin_description',
144+
$plugin_main_file,
145+
0,
146+
0,
147+
__( 'https://developer.wordpress.org/plugins/plugin-basics/header-requirements/', 'plugin-check' ),
148+
7
149+
);
150+
} else {
136151
if (
137152
str_contains( $plugin_header['Description'], 'This is a short description of what the plugin does' )
138153
|| str_contains( $plugin_header['Description'], 'Here is a short description of the plugin' )
@@ -155,6 +170,40 @@ public function run( Check_Result $result ) {
155170
}
156171
}
157172

173+
if ( empty( $plugin_header['Version'] ) ) {
174+
$this->add_result_error_for_file(
175+
$result,
176+
sprintf(
177+
/* translators: %s: plugin header field */
178+
__( 'The "%s" header is missing in the plugin file.', 'plugin-check' ),
179+
esc_html( $labels['Version'] )
180+
),
181+
'plugin_header_missing_plugin_version',
182+
$plugin_main_file,
183+
0,
184+
0,
185+
__( 'https://developer.wordpress.org/plugins/plugin-basics/header-requirements/', 'plugin-check' ),
186+
7
187+
);
188+
} else {
189+
if ( ! preg_match( '/^[a-z0-9.-]+$/i', $plugin_header['Version'] ) ) {
190+
$this->add_result_error_for_file(
191+
$result,
192+
sprintf(
193+
/* translators: %s: plugin header field */
194+
__( 'The "%s" header in the plugin file should only contain numbers, letters, periods, and hyphens.', 'plugin-check' ),
195+
esc_html( $labels['Version'] )
196+
),
197+
'plugin_header_invalid_plugin_version',
198+
$plugin_main_file,
199+
0,
200+
0,
201+
__( 'https://developer.wordpress.org/plugins/plugin-basics/header-requirements/', 'plugin-check' ),
202+
7
203+
);
204+
}
205+
}
206+
158207
if ( ! empty( $plugin_header['AuthorURI'] ) ) {
159208
if ( true !== $this->is_valid_url( $plugin_header['AuthorURI'] ) ) {
160209
$this->add_result_warning_for_file(

tests/phpunit/testdata/plugins/test-plugin-header-fields-with-errors/load.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* Description: Here is a short description of the plugin.
66
* Requires at least: Recent version
77
* Requires PHP: Latest version
8-
* Version: 1.0.0
98
* Author: WordPress Performance Team
109
* Author URI: This is not a valid URL
1110
* Text Domain: test-mismathed-textdomain-here

tests/phpunit/testdata/plugins/test-plugin-late-escaping-errors/load.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
/**
33
* Plugin Name: Test Plugin escape output with Errors for Plugin Check
44
* Plugin URI: https://github.com/WordPress/plugin-check
5-
* Description: Some plugin description.
65
* Requires at least: 6.0
76
* Requires PHP: 5.6
8-
* Version: 1.0.0
7+
* Version: 1.0.0 Beta
98
* Author: WordPress Performance Team
109
* Author URI: https://make.wordpress.org/performance/
1110
* License: GPLv2 or later
1211
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
1312
* Text Domain: test-plugin-check-errors
13+
* Domain Path: /languages
1414
*
1515
* @package test-plugin-check-errors
1616
*/

tests/phpunit/tests/Checker/Checks/Plugin_Header_Fields_Check_Tests.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function test_run_with_errors() {
2828
$this->assertCount( 1, wp_list_filter( $errors['load.php'][0][0], array( 'code' => 'plugin_header_invalid_requires_wp' ) ) );
2929
$this->assertCount( 1, wp_list_filter( $errors['load.php'][0][0], array( 'code' => 'plugin_header_invalid_requires_php' ) ) );
3030
$this->assertCount( 1, wp_list_filter( $errors['load.php'][0][0], array( 'code' => 'plugin_header_no_license' ) ) );
31+
$this->assertCount( 1, wp_list_filter( $errors['load.php'][0][0], array( 'code' => 'plugin_header_missing_plugin_version' ) ) );
3132
$this->assertCount( 1, wp_list_filter( $warnings['load.php'][0][0], array( 'code' => 'plugin_header_invalid_plugin_uri_domain' ) ) );
3233
$this->assertCount( 1, wp_list_filter( $warnings['load.php'][0][0], array( 'code' => 'plugin_header_invalid_plugin_description' ) ) );
3334
$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() {
7374
// Check for invalid license.
7475
$this->assertCount( 1, wp_list_filter( $errors['load.php'][0][0], array( 'code' => 'plugin_header_invalid_license' ) ) );
7576
}
77+
78+
public function test_run_with_invalid_header_fields() {
79+
$check = new Plugin_Header_Fields_Check();
80+
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-late-escaping-errors/load.php' );
81+
$check_result = new Check_Result( $check_context );
82+
83+
$check->run( $check_result );
84+
85+
$errors = $check_result->get_errors();
86+
87+
$this->assertNotEmpty( $errors );
88+
89+
$this->assertCount( 1, wp_list_filter( $errors['load.php'][0][0], array( 'code' => 'plugin_header_missing_plugin_description' ) ) );
90+
$this->assertCount( 1, wp_list_filter( $errors['load.php'][0][0], array( 'code' => 'plugin_header_invalid_plugin_version' ) ) );
91+
}
7692
}

0 commit comments

Comments
 (0)