Make WordPress Core

Changeset 58843


Ignore:
Timestamp:
08/02/2024 10:36:27 PM (2 years ago)
Author:
peterwilsoncc
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r58827 r58843  
    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.
     
    88408840 */
    88418841function 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.
  • trunk/tests/phpunit/tests/functions/isWpVersionCompatible.php

    r57735 r58843  
    1010class Tests_Functions_IsWpVersionCompatible extends WP_UnitTestCase {
    1111        /**
     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
     43        /**
    1244         * Tests is_wp_version_compatible().
    1345         *
     
    1547         *
    1648         * @ticket 54257
     49         * @ticket 61781
    1750         *
    1851         * @param mixed $required The minimum required WordPress version.
     
    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;
     
    105137         *
    106138         * @ticket 59448
     139         * @ticket 61781
    107140         *
    108141         * @param mixed  $required The minimum required WordPress version.
     
    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
     
    184209         *
    185210         * @ticket 54257
     211         * @ticket 61781
    186212         *
    187213         * @param string $required  The minimum required WordPress version.
     
    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
     
    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 );
Note: See TracChangeset for help on using the changeset viewer.