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

Check valid WP version for Requires at least plugin header #852

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -265,8 +265,9 @@
}

if ( ! empty( $plugin_header['RequiresWP'] ) ) {
$latest_wp_version = $this->get_wordpress_stable_version();

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(
Expand All @@ -285,6 +286,26 @@
'',
7
);
} else {
$acceptable_min_wp_version = $this->get_wordpress_relative_major_version( $latest_wp_version, 1 );

if ( version_compare( $plugin_header['RequiresWP'], $acceptable_min_wp_version, '>' ) ) {
$this->add_result_error_for_file(
$result,
sprintf(

Check warning on line 295 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#L293-L295

Added lines #L293 - L295 were not covered by tests
/* translators: 1: plugin header field, 2: currently used version */
__( '<strong>%1$s: %2$s.</strong><br>The "%1$s" value in your plugin header is not valid. This version of WordPress does not exist (yet).', 'plugin-check' ),
esc_html( $labels['RequiresWP'] ),
esc_html( $plugin_header['RequiresWP'] )
),
'plugin_header_nonexistent_requires_wp',
$plugin_main_file,
0,
0,
'https://developer.wordpress.org/plugins/plugin-basics/header-requirements/#header-fields',
7
);

Check warning on line 307 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#L297-L307

Added lines #L297 - L307 were not covered by tests
}
}
}
if ( ! empty( $plugin_header['RequiresPHP'] ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,61 @@ 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' ) ) );
}

public function test_run_with_errors_requires_at_least_latest_plus_two_version() {
// Target plugin has "6.0" in plugin header.
set_transient( 'wp_plugin_check_latest_version_info', array( 'current' => '5.8.1' ) );

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

$readme_check->run( $check_result );

$errors = $check_result->get_errors();

delete_transient( 'wp_plugin_check_latest_version_info' );

$this->assertNotEmpty( $errors );

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

$this->assertCount( 1, $filtered_items );
$this->assertStringContainsString( 'Requires at least: 6.0', $filtered_items[0]['message'] );
$this->assertStringContainsString( 'This version of WordPress does not exist (yet).', $filtered_items[0]['message'] );
}

public function test_run_without_errors_requires_at_least_latest_plus_one_version() {
// Target plugin has "6.0" in plugin header.
set_transient( 'wp_plugin_check_latest_version_info', array( 'current' => '5.9.1' ) );

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

$readme_check->run( $check_result );

$errors = $check_result->get_errors();

delete_transient( 'wp_plugin_check_latest_version_info' );

$this->assertEmpty( $errors );
}

public function test_run_without_errors_requires_at_least_latest_version() {
// Target plugin has "6.0" in plugin header.
set_transient( 'wp_plugin_check_latest_version_info', array( 'current' => '6.0.1' ) );

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

$readme_check->run( $check_result );

$errors = $check_result->get_errors();

delete_transient( 'wp_plugin_check_latest_version_info' );
ernilambar marked this conversation as resolved.
Show resolved Hide resolved

$this->assertEmpty( $errors );
}
}
Loading