Skip to content

Commit

Permalink
Upgrade/Install: Normalize major versions in `is_wp_version_compatibl…
Browse files Browse the repository at this point in the history
…e()`.

Modify `is_wp_version_compatible()` to return the expected result for major WordPress versions formatted as either `x.x` or `x.x.0` (for example `6.5` and `6.5.0`).

The WordPress project currently documents major version numbers in both formats leading to confusion for developers using the `is_wp_version_compatible()` function. As the PHP function `version_compare()` treats `x.x` and `x.x.0` as different version numbers this leads to unexpected results in the WP function.

This change removes a trailing `.0` from major version numbers to account for the WordPress project using the two formats interchangeably.

Props afragen, azaozz, costdev, joemcgill, jorbin, kkmuffme, sessioncookiemonster, swissspidy, wazeter.
Fixes #59448.


git-svn-id: https://develop.svn.wordpress.org/trunk@57707 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
peterwilsoncc committed Feb 25, 2024
1 parent 78b37f3 commit 2ba8f94
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 12 deletions.
8 changes: 8 additions & 0 deletions src/wp-includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8763,6 +8763,14 @@ function is_wp_version_compatible( $required ) {
// Strip off any -alpha, -RC, -beta, -src suffixes.
list( $version ) = explode( '-', $wp_version );

if ( is_string( $required ) ) {
$trimmed = trim( $required );

if ( substr_count( $trimmed, '.' ) > 1 && str_ends_with( $trimmed, '.0' ) ) {
$required = substr( $trimmed, 0, -2 );
}
}

return empty( $required ) || version_compare( $version, $required, '>=' );
}

Expand Down
109 changes: 99 additions & 10 deletions tests/phpunit/tests/functions/isWpVersionCompatible.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,51 +43,140 @@ public function data_is_wp_version_compatible() {

return array(
// Happy paths.
'the same version' => array(
'the same version' => array(
'required' => $wp_version,
'expected' => true,
),
'a lower required version' => array(
'a lower required version' => array(
'required' => $lower_version,
'expected' => true,
),
'a higher required version' => array(
'a higher required version' => array(
'required' => $higher_version,
'expected' => false,
),

// Acceptable versions containing '.0'.
'correct version ending with x.0' => array(
'required' => '5.0',
'expected' => true,
),
'correct version with x.0.x in middle of version' => array(
'required' => '5.0.1',
'expected' => true,
),

// Falsey values.
'false' => array(
'false' => array(
'required' => false,
'expected' => true,
),
'null' => array(
'null' => array(
'required' => null,
'expected' => true,
),
'0 int' => array(
'0 int' => array(
'required' => 0,
'expected' => true,
),
'0.0 float' => array(
'0.0 float' => array(
'required' => 0.0,
'expected' => true,
),
'0 string' => array(
'0 string' => array(
'required' => '0',
'expected' => true,
),
'empty string' => array(
'empty string' => array(
'required' => '',
'expected' => true,
),
'empty array' => array(
'empty array' => array(
'required' => array(),
'expected' => true,
),
);
}

/**
* Tests that is_wp_version_compatible() gracefully handles incorrect version numbering.
*
* @dataProvider data_is_wp_version_compatible_should_gracefully_handle_trailing_point_zero_version_numbers
*
* @ticket 59448
*
* @param mixed $required The minimum required WordPress version.
* @param string $wp The value for the $wp_version global variable.
* @param bool $expected The expected result.
*/
public function test_is_wp_version_compatible_should_gracefully_handle_trailing_point_zero_version_numbers( $required, $wp, $expected ) {
global $wp_version;
$original_version = $wp_version;
$wp_version = $wp;

$actual = is_wp_version_compatible( $required );

// Reset the version before the assertion in case of failure.
$wp_version = $original_version;

$this->assertSame( $expected, $actual, 'The expected result was not returned.' );
}

/**
* Data provider.
*
* @return array
*/
public function data_is_wp_version_compatible_should_gracefully_handle_trailing_point_zero_version_numbers() {
return array(
'an incorrect trailing .0 and the same version' => array(
'required' => '5.2.0',
'wp' => '5.2',
'expected' => true,
),
'an incorrect trailing .0 and the same x.0 version' => array(
'required' => '5.0.0',
'wp' => '5.0',
'expected' => true,
),
'an incorrect trailing .0 and space and same x.0 version' => array(
'required' => '5.0.0 ',
'wp' => '5.0',
'expected' => true,
),
'incorrect preceding and trailing spaces trailing .0' => array(
'required' => ' 5.0.0 ',
'wp' => '5.0',
'expected' => true,
),
'an incorrect trailing .0 on x.0.x version' => array(
'required' => '5.0.1.0',
'wp' => '5.0.1',
'expected' => true,
),
'an incorrect trailing .0 and an earlier version' => array(
'required' => '5.0.0',
'wp' => '4.0',
'expected' => false,
),
'an incorrect trailing .0 and an earlier x.0 version' => array(
'required' => '5.0.0',
'wp' => '4.0',
'expected' => false,
),
'an incorrect trailing .0 and a later version' => array(
'required' => '5.0.0',
'wp' => '6.0',
'expected' => true,
),
'an incorrect trailing .0 and a later x.0 version' => array(
'required' => '5.0.0',
'wp' => '6.0',
'expected' => true,
),
);
}

/**
* Tests is_wp_version_compatible() with development versions.
*
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/rest-api/rest-plugins-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ protected function check_get_plugin_data( $data, $network_only = false ) {
$this->assertSame( 'My &#8216;Cool&#8217; Plugin <cite>By <a href="https://wordpress.org/">WordPress.org</a>.</cite>', $data['description']['rendered'] );
$this->assertSame( $network_only, $data['network_only'] );
$this->assertSame( '5.6.0', $data['requires_php'] );
$this->assertSame( '5.4.0', $data['requires_wp'] );
$this->assertSame( '5.4', $data['requires_wp'] );
$this->assertSame( 'test-plugin', $data['textdomain'] );
}

Expand Down Expand Up @@ -1149,7 +1149,7 @@ private function create_test_plugin( $network_only = false ) {
* Author URI: https://wordpress.org/
* Text Domain: test-plugin
* Requires PHP: 5.6.0
* Requires at least: 5.4.0{$network}
* Requires at least: 5.4{$network}
*/
PHP;
wp_mkdir_p( WP_PLUGIN_DIR . '/test-plugin' );
Expand Down

0 comments on commit 2ba8f94

Please sign in to comment.