Skip to content

Commit 0e06e2b

Browse files
committed
General: Use clean WordPress version in is_wp_version_compatible().
Update `is_wp_version_compatible()` to use `wp_get_wp_version()` introduced in [58813] to ensure the value of `$wp_version` has not been modified by a theme or plugin. Props costdev, mukesh27, Cybr, sergeybiryukov. Fixes #61781. git-svn-id: https://develop.svn.wordpress.org/trunk@58843 602fd350-edb4-49c9-b593-d223f7449a82
1 parent da0f44f commit 0e06e2b

File tree

2 files changed

+51
-27
lines changed

2 files changed

+51
-27
lines changed

src/wp-includes/functions.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8833,13 +8833,21 @@ function wp_get_wp_version() {
88338833
*
88348834
* @since 5.2.0
88358835
*
8836-
* @global string $wp_version The WordPress version string.
8836+
* @global string $_wp_tests_wp_version The WordPress version string. Used only in Core tests.
88378837
*
88388838
* @param string $required Minimum required WordPress version.
88398839
* @return bool True if required version is compatible or empty, false if not.
88408840
*/
88418841
function is_wp_version_compatible( $required ) {
8842-
global $wp_version;
8842+
if (
8843+
defined( 'WP_RUN_CORE_TESTS' )
8844+
&& WP_RUN_CORE_TESTS
8845+
&& isset( $GLOBALS['_wp_tests_wp_version'] )
8846+
) {
8847+
$wp_version = $GLOBALS['_wp_tests_wp_version'];
8848+
} else {
8849+
$wp_version = wp_get_wp_version();
8850+
}
88438851

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

tests/phpunit/tests/functions/isWpVersionCompatible.php

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,45 @@
88
* @covers ::is_wp_version_compatible
99
*/
1010
class Tests_Functions_IsWpVersionCompatible extends WP_UnitTestCase {
11+
/**
12+
* The current WordPress version.
13+
*
14+
* @var string
15+
*/
16+
private static $wp_version;
17+
18+
/**
19+
* Sets the test WordPress version property and global before any tests run.
20+
*/
21+
public static function set_up_before_class() {
22+
parent::set_up_before_class();
23+
self::$wp_version = wp_get_wp_version();
24+
$GLOBALS['_wp_tests_wp_version'] = self::$wp_version;
25+
}
26+
27+
/**
28+
* Resets the test WordPress version global after each test runs.
29+
*/
30+
public function tear_down() {
31+
$GLOBALS['_wp_tests_wp_version'] = self::$wp_version;
32+
parent::tear_down();
33+
}
34+
35+
/**
36+
* Unsets the test WordPress version global after all tests run.
37+
*/
38+
public static function tear_down_after_class() {
39+
unset( $GLOBALS['_wp_tests_wp_version'] );
40+
parent::tear_down_after_class();
41+
}
42+
1143
/**
1244
* Tests is_wp_version_compatible().
1345
*
1446
* @dataProvider data_is_wp_version_compatible
1547
*
1648
* @ticket 54257
49+
* @ticket 61781
1750
*
1851
* @param mixed $required The minimum required WordPress version.
1952
* @param bool $expected The expected result.
@@ -28,8 +61,7 @@ public function test_is_wp_version_compatible( $required, $expected ) {
2861
* @return array[]
2962
*/
3063
public function data_is_wp_version_compatible() {
31-
global $wp_version;
32-
64+
$wp_version = wp_get_wp_version();
3365
$version_parts = explode( '.', $wp_version );
3466
$lower_version = $version_parts;
3567
$higher_version = $version_parts;
@@ -104,22 +136,15 @@ public function data_is_wp_version_compatible() {
104136
* @dataProvider data_is_wp_version_compatible_should_gracefully_handle_trailing_point_zero_version_numbers
105137
*
106138
* @ticket 59448
139+
* @ticket 61781
107140
*
108141
* @param mixed $required The minimum required WordPress version.
109142
* @param string $wp The value for the $wp_version global variable.
110143
* @param bool $expected The expected result.
111144
*/
112145
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.' );
146+
$GLOBALS['_wp_tests_wp_version'] = $wp;
147+
$this->assertSame( $expected, is_wp_version_compatible( $required ), 'The expected result was not returned.' );
123148
}
124149

125150
/**
@@ -183,22 +208,15 @@ public function data_is_wp_version_compatible_should_gracefully_handle_trailing_
183208
* @dataProvider data_is_wp_version_compatible_with_development_versions
184209
*
185210
* @ticket 54257
211+
* @ticket 61781
186212
*
187213
* @param string $required The minimum required WordPress version.
188214
* @param string $wp The value for the $wp_version global variable.
189215
* @param bool $expected The expected result.
190216
*/
191217
public function test_is_wp_version_compatible_with_development_versions( $required, $wp, $expected ) {
192-
global $wp_version;
193-
194-
$original_version = $wp_version;
195-
$wp_version = $wp;
196-
$actual = is_wp_version_compatible( $required );
197-
198-
// Reset the version before the assertion in case of failure.
199-
$wp_version = $original_version;
200-
201-
$this->assertSame( $expected, $actual );
218+
$GLOBALS['_wp_tests_wp_version'] = $wp;
219+
$this->assertSame( $expected, is_wp_version_compatible( $required ) );
202220
}
203221

204222
/**
@@ -207,10 +225,8 @@ public function test_is_wp_version_compatible_with_development_versions( $requir
207225
* @return array[]
208226
*/
209227
public function data_is_wp_version_compatible_with_development_versions() {
210-
global $wp_version;
211-
212228
// For consistent results, remove possible suffixes.
213-
list( $version ) = explode( '-', $wp_version );
229+
list( $version ) = explode( '-', wp_get_wp_version() );
214230

215231
$version_parts = explode( '.', $version );
216232
$lower_version = $version_parts;

0 commit comments

Comments
 (0)