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

Implement Version Utils in readme check #855

Merged
merged 2 commits into from
Jan 13, 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
43 changes: 2 additions & 41 deletions includes/Checker/Checks/Plugin_Repo/Plugin_Readme_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use WordPress\Plugin_Check\Traits\Find_Readme;
use WordPress\Plugin_Check\Traits\License_Utils;
use WordPress\Plugin_Check\Traits\Stable_Check;
use WordPress\Plugin_Check\Traits\Version_Utils;
use WordPressdotorg\Plugin_Directory\Readme\Parser;

/**
Expand All @@ -29,6 +30,7 @@ class Plugin_Readme_Check extends Abstract_File_Check {
use Find_Readme;
use Stable_Check;
use License_Utils;
use Version_Utils;

/**
* Gets the categories for the check.
Expand Down Expand Up @@ -786,47 +788,6 @@ function ( $value ) {
}
}

/**
* Returns current major WordPress version.
*
* @since 1.0.0
*
* @return string Stable WordPress version.
*/
private function get_wordpress_stable_version() {
$version = get_transient( 'wp_plugin_check_latest_wp_version' );

if ( false === $version ) {
$response = wp_remote_get( 'https://api.wordpress.org/core/version-check/1.7/' );

if ( ! is_wp_error( $response ) && 200 === wp_remote_retrieve_response_code( $response ) ) {
$body = json_decode( wp_remote_retrieve_body( $response ), true );

if ( isset( $body['offers'] ) && ! empty( $body['offers'] ) ) {
$latest_release = reset( $body['offers'] );

$version = $latest_release['current'];

set_transient( 'wp_plugin_check_latest_wp_version', $version, DAY_IN_SECONDS );
}
}
}

// If $version is still false at this point, use current installed WordPress version.
if ( false === $version ) {
$version = get_bloginfo( 'version' );

// Strip off any -alpha, -RC, -beta suffixes.
list( $version, ) = explode( '-', $version );
}

if ( preg_match( '#^\d.\d#', $version, $matches ) ) {
$version = $matches[0];
}

return $version;
}

/**
* Returns ignored warnings.
*
Expand Down
25 changes: 11 additions & 14 deletions tests/phpunit/tests/Checker/Checks/Plugin_Readme_Check_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ public function test_run_with_errors_multiple_parser_warnings() {
public function test_run_with_errors_parser_warnings_with_custom_set_transient_version() {
$version = '5.0';

set_transient( 'wp_plugin_check_latest_wp_version', $version );
set_transient( 'wp_plugin_check_latest_version_info', array( 'current' => '5.0.1' ) );

$readme_check = new Plugin_Readme_Check();
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-plugin-readme-parser-warnings/load.php' );
Expand All @@ -349,7 +349,7 @@ public function test_run_with_errors_parser_warnings_with_custom_set_transient_v

$warnings = $check_result->get_warnings();

delete_transient( 'wp_plugin_check_latest_wp_version' );
delete_transient( 'wp_plugin_check_latest_version_info' );

$this->assertNotEmpty( $warnings );
$this->assertArrayHasKey( 'readme.txt', $warnings );
Expand Down Expand Up @@ -473,9 +473,8 @@ public function test_run_with_errors_upgrade_notice() {
}

public function test_run_with_errors_tested_up_to_latest_plus_two_version() {
$version = '5.9'; // Target plugin has "6.1" is readme.

set_transient( 'wp_plugin_check_latest_wp_version', $version );
// Target plugin has "6.1" is readme.
set_transient( 'wp_plugin_check_latest_version_info', array( 'current' => '5.9.1' ) );

$readme_check = new Plugin_Readme_Check();
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-plugin-readme-md-with-errors/load.php' );
Expand All @@ -485,7 +484,7 @@ public function test_run_with_errors_tested_up_to_latest_plus_two_version() {

$errors = $check_result->get_errors();

delete_transient( 'wp_plugin_check_latest_wp_version' );
delete_transient( 'wp_plugin_check_latest_version_info' );

$this->assertNotEmpty( $errors );

Expand All @@ -497,9 +496,8 @@ public function test_run_with_errors_tested_up_to_latest_plus_two_version() {
}

public function test_run_without_errors_tested_up_to_latest_plus_one_version() {
$version = '6.0'; // Target plugin has "6.1" is readme.

set_transient( 'wp_plugin_check_latest_wp_version', $version );
// Target plugin has "6.1" is readme.
set_transient( 'wp_plugin_check_latest_version_info', array( 'current' => '6.0.1' ) );

$readme_check = new Plugin_Readme_Check();
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-plugin-readme-md-with-errors/load.php' );
Expand All @@ -509,15 +507,14 @@ public function test_run_without_errors_tested_up_to_latest_plus_one_version() {

$errors = $check_result->get_errors();

delete_transient( 'wp_plugin_check_latest_wp_version' );
delete_transient( 'wp_plugin_check_latest_version_info' );

$this->assertCount( 0, wp_list_filter( $errors['readme.md'][0][0], array( 'code' => 'nonexistent_tested_upto_header' ) ) );
}

public function test_run_without_errors_tested_up_to_latest_stable_version() {
$version = '6.1'; // Target plugin has "6.1" is readme.

set_transient( 'wp_plugin_check_latest_wp_version', $version );
// Target plugin has "6.1" is readme.
set_transient( 'wp_plugin_check_latest_version_info', array( 'current' => '6.1.1' ) );

$readme_check = new Plugin_Readme_Check();
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-plugin-readme-md-with-errors/load.php' );
Expand All @@ -527,7 +524,7 @@ public function test_run_without_errors_tested_up_to_latest_stable_version() {

$errors = $check_result->get_errors();

delete_transient( 'wp_plugin_check_latest_wp_version' );
delete_transient( 'wp_plugin_check_latest_version_info' );

$this->assertCount( 0, wp_list_filter( $errors['readme.md'][0][0], array( 'code' => 'nonexistent_tested_upto_header' ) ) );
}
Expand Down
Loading