Skip to content

Commit

Permalink
Merge pull request #827 from WordPress/update-requires-wp-header-message
Browse files Browse the repository at this point in the history
Update error message for `plugin_header_invalid_requires_wp` code
  • Loading branch information
davidperezgar authored Dec 12, 2024
2 parents 8ec323f + 3c3bc3e commit d3fbbf6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use WordPress\Plugin_Check\Traits\Amend_Check_Result;
use WordPress\Plugin_Check\Traits\License_Utils;
use WordPress\Plugin_Check\Traits\Stable_Check;
use WordPress\Plugin_Check\Traits\Version_Utils;

/**
* Check for plugin header fields.
Expand All @@ -25,6 +26,7 @@ class Plugin_Header_Fields_Check implements Static_Check {
use Amend_Check_Result;
use License_Utils;
use Stable_Check;
use Version_Utils;

/**
* Gets the categories for the check.
Expand Down Expand Up @@ -245,14 +247,17 @@ public function run( Check_Result $result ) {

if ( ! empty( $plugin_header['RequiresWP'] ) ) {
if ( ! preg_match( '!^\d+\.\d(\.\d+)?$!', $plugin_header['RequiresWP'] ) ) {
$latest_wp_version = $this->get_wordpress_stable_version();
$previous_wp_version = $this->get_wordpress_relative_major_version( $latest_wp_version, -1 );

$this->add_result_error_for_file(
$result,
sprintf(
/* translators: 1: plugin header field; 2: Example version 6.5.1. 3: Example version 6.6. */
/* translators: 1: plugin header field, 2: Example version 6.7, 3: Example version 6.6 */
__( 'The "%1$s" header in the plugin file should only contain a WordPress version such as "%2$s" or "%3$s".', 'plugin-check' ),
esc_html( $labels['RequiresWP'] ),
'6.5.1',
'6.6'
esc_html( $latest_wp_version ),
esc_html( $previous_wp_version )
),
'plugin_header_invalid_requires_wp',
$plugin_main_file,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ public function test_run_with_errors() {
}
}

public function test_run_with_invalid_requires_wp_header() {
set_transient( 'wp_plugin_check_latest_version_info', array( 'current' => '6.5.1' ) );

$check = new Plugin_Header_Fields_Check();
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-header-fields-with-errors/load.php' );
$check_result = new Check_Result( $check_context );

$check->run( $check_result );

$errors = $check_result->get_errors();

$this->assertNotEmpty( $errors );

$error_items = wp_list_filter( $errors['load.php'][0][0], array( 'code' => 'plugin_header_invalid_requires_wp' ) );

$this->assertCount( 1, $error_items );
$this->assertStringContainsString( 'such as "6.5" or "6.4"', reset( $error_items )['message'] );

delete_transient( 'wp_plugin_check_latest_version_info' );
}

public function test_run_with_valid_requires_plugins_header() {
/*
* Test plugin has following valid header.
Expand Down

0 comments on commit d3fbbf6

Please sign in to comment.