Skip to content

Commit 2ba8f94

Browse files
committed
Upgrade/Install: Normalize major versions in is_wp_version_compatible().
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
1 parent 78b37f3 commit 2ba8f94

3 files changed

Lines changed: 109 additions & 12 deletions

File tree

src/wp-includes/functions.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8763,6 +8763,14 @@ function is_wp_version_compatible( $required ) {
87638763
// Strip off any -alpha, -RC, -beta, -src suffixes.
87648764
list( $version ) = explode( '-', $wp_version );
87658765

8766+
if ( is_string( $required ) ) {
8767+
$trimmed = trim( $required );
8768+
8769+
if ( substr_count( $trimmed, '.' ) > 1 && str_ends_with( $trimmed, '.0' ) ) {
8770+
$required = substr( $trimmed, 0, -2 );
8771+
}
8772+
}
8773+
87668774
return empty( $required ) || version_compare( $version, $required, '>=' );
87678775
}
87688776

tests/phpunit/tests/functions/isWpVersionCompatible.php

Lines changed: 99 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,51 +43,140 @@ public function data_is_wp_version_compatible() {
4343

4444
return array(
4545
// Happy paths.
46-
'the same version' => array(
46+
'the same version' => array(
4747
'required' => $wp_version,
4848
'expected' => true,
4949
),
50-
'a lower required version' => array(
50+
'a lower required version' => array(
5151
'required' => $lower_version,
5252
'expected' => true,
5353
),
54-
'a higher required version' => array(
54+
'a higher required version' => array(
5555
'required' => $higher_version,
5656
'expected' => false,
5757
),
5858

59+
// Acceptable versions containing '.0'.
60+
'correct version ending with x.0' => array(
61+
'required' => '5.0',
62+
'expected' => true,
63+
),
64+
'correct version with x.0.x in middle of version' => array(
65+
'required' => '5.0.1',
66+
'expected' => true,
67+
),
68+
5969
// Falsey values.
60-
'false' => array(
70+
'false' => array(
6171
'required' => false,
6272
'expected' => true,
6373
),
64-
'null' => array(
74+
'null' => array(
6575
'required' => null,
6676
'expected' => true,
6777
),
68-
'0 int' => array(
78+
'0 int' => array(
6979
'required' => 0,
7080
'expected' => true,
7181
),
72-
'0.0 float' => array(
82+
'0.0 float' => array(
7383
'required' => 0.0,
7484
'expected' => true,
7585
),
76-
'0 string' => array(
86+
'0 string' => array(
7787
'required' => '0',
7888
'expected' => true,
7989
),
80-
'empty string' => array(
90+
'empty string' => array(
8191
'required' => '',
8292
'expected' => true,
8393
),
84-
'empty array' => array(
94+
'empty array' => array(
8595
'required' => array(),
8696
'expected' => true,
8797
),
8898
);
8999
}
90100

101+
/**
102+
* Tests that is_wp_version_compatible() gracefully handles incorrect version numbering.
103+
*
104+
* @dataProvider data_is_wp_version_compatible_should_gracefully_handle_trailing_point_zero_version_numbers
105+
*
106+
* @ticket 59448
107+
*
108+
* @param mixed $required The minimum required WordPress version.
109+
* @param string $wp The value for the $wp_version global variable.
110+
* @param bool $expected The expected result.
111+
*/
112+
public function test_is_wp_version_compatible_should_gracefully_handle_trailing_point_zero_version_numbers( $required, $wp, $expected ) {
113+
global $wp_version;
114+
$original_version = $wp_version;
115+
$wp_version = $wp;
116+
117+
$actual = is_wp_version_compatible( $required );
118+
119+
// Reset the version before the assertion in case of failure.
120+
$wp_version = $original_version;
121+
122+
$this->assertSame( $expected, $actual, 'The expected result was not returned.' );
123+
}
124+
125+
/**
126+
* Data provider.
127+
*
128+
* @return array
129+
*/
130+
public function data_is_wp_version_compatible_should_gracefully_handle_trailing_point_zero_version_numbers() {
131+
return array(
132+
'an incorrect trailing .0 and the same version' => array(
133+
'required' => '5.2.0',
134+
'wp' => '5.2',
135+
'expected' => true,
136+
),
137+
'an incorrect trailing .0 and the same x.0 version' => array(
138+
'required' => '5.0.0',
139+
'wp' => '5.0',
140+
'expected' => true,
141+
),
142+
'an incorrect trailing .0 and space and same x.0 version' => array(
143+
'required' => '5.0.0 ',
144+
'wp' => '5.0',
145+
'expected' => true,
146+
),
147+
'incorrect preceding and trailing spaces trailing .0' => array(
148+
'required' => ' 5.0.0 ',
149+
'wp' => '5.0',
150+
'expected' => true,
151+
),
152+
'an incorrect trailing .0 on x.0.x version' => array(
153+
'required' => '5.0.1.0',
154+
'wp' => '5.0.1',
155+
'expected' => true,
156+
),
157+
'an incorrect trailing .0 and an earlier version' => array(
158+
'required' => '5.0.0',
159+
'wp' => '4.0',
160+
'expected' => false,
161+
),
162+
'an incorrect trailing .0 and an earlier x.0 version' => array(
163+
'required' => '5.0.0',
164+
'wp' => '4.0',
165+
'expected' => false,
166+
),
167+
'an incorrect trailing .0 and a later version' => array(
168+
'required' => '5.0.0',
169+
'wp' => '6.0',
170+
'expected' => true,
171+
),
172+
'an incorrect trailing .0 and a later x.0 version' => array(
173+
'required' => '5.0.0',
174+
'wp' => '6.0',
175+
'expected' => true,
176+
),
177+
);
178+
}
179+
91180
/**
92181
* Tests is_wp_version_compatible() with development versions.
93182
*

tests/phpunit/tests/rest-api/rest-plugins-controller.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ protected function check_get_plugin_data( $data, $network_only = false ) {
10211021
$this->assertSame( 'My &#8216;Cool&#8217; Plugin <cite>By <a href="https://wordpress.org/">WordPress.org</a>.</cite>', $data['description']['rendered'] );
10221022
$this->assertSame( $network_only, $data['network_only'] );
10231023
$this->assertSame( '5.6.0', $data['requires_php'] );
1024-
$this->assertSame( '5.4.0', $data['requires_wp'] );
1024+
$this->assertSame( '5.4', $data['requires_wp'] );
10251025
$this->assertSame( 'test-plugin', $data['textdomain'] );
10261026
}
10271027

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

0 commit comments

Comments
 (0)