-
Notifications
You must be signed in to change notification settings - Fork 3.3k
General: Introduce wp_get_branch_version() helper to extract major.minor version safely. #11211
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
base: trunk
Are you sure you want to change the base?
Changes from all commits
7e45289
fedcc76
4100bb2
d584d96
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8958,26 +8958,65 @@ function clean_dirsize_cache( $path ) { | |
| } | ||
|
|
||
| /** | ||
| * Returns the current WordPress version. | ||
| * Returns the WordPress version string in the requested format. | ||
| * | ||
| * Returns an unmodified value of `$wp_version`. Some plugins modify the global | ||
| * in an attempt to improve security through obscurity. This practice can cause | ||
| * errors in WordPress, so the ability to get an unmodified version is needed. | ||
| * Returns an unmodified value of `$wp_version` by default. Some plugins modify | ||
| * the global in an attempt to improve security through obscurity. This practice | ||
| * can cause errors in WordPress, so the ability to get an unmodified version is | ||
| * needed. | ||
| * | ||
| * WordPress version numbering: x.y is the "major" release (e.g. 7.0, 6.9), | ||
| * x.y.z is the "minor" release (e.g. 7.0.1). See | ||
| * https://make.wordpress.org/core/handbook/about/release-cycle/version-numbering/ | ||
| * | ||
| * @since 6.7.0 | ||
| * @since 7.1.0 Added the `$part` parameter to support 'major' and 'minor' formats. | ||
| * | ||
| * @return string The current WordPress version. | ||
| * @param string $part Optional. The version part to return. Accepts 'major' for the | ||
| * x.y version (e.g. '6.9'), 'minor' for the x.y.z version | ||
| * (e.g. '6.9.1'), or 'full' for the complete version string | ||
| * (e.g. '7.0-beta3-61849'). Default 'full'. | ||
| * @return string The WordPress version string in the requested format. | ||
| */ | ||
| function wp_get_wp_version() { | ||
| function wp_get_wp_version( $part = 'full' ) { | ||
| static $wp_version; | ||
|
|
||
| if ( ! isset( $wp_version ) ) { | ||
| require ABSPATH . WPINC . '/version.php'; | ||
| } | ||
|
|
||
| if ( 'major' === $part ) { | ||
| return wp_get_branch_version( $wp_version ); | ||
| } elseif ( 'minor' === $part ) { | ||
| $parts = preg_split( '/[.-]/', $wp_version, 4 ); | ||
| return $parts[0] . '.' . ( $parts[1] ?? '0' ) . '.' . ( $parts[2] ?? '0' ); | ||
| } | ||
|
|
||
| return $wp_version; | ||
| } | ||
|
|
||
| /** | ||
| * Extracts the major version (x.y) from a WordPress version string. | ||
| * | ||
| * WordPress version numbering: x.y is the "major" release (e.g. 7.0, 6.9). | ||
| * This function strips the minor release number and any pre-release suffix. | ||
| * | ||
| * @since 7.1.0 | ||
| * | ||
| * @param string $version Optional. A WordPress version string. Defaults to the | ||
| * current WordPress version from wp_get_wp_version(). | ||
| * @return string The major version string in "x.y" format (e.g. "7.0"). | ||
| */ | ||
| function wp_get_branch_version( $version = '' ) { | ||
tejas0306 marked this conversation as resolved.
Show resolved
Hide resolved
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is required, the need in core isn't wide enough for what is relatively simple code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem is that it's needed in about half a dozen different places in core. Currently these are using a mixture of different ad-hoc methods to find the version number, and some of these are actually wrong/buggy.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the review. The intention of At the moment, different files use different methods such as This PR updates the current call sites that need the branch version to use a shared helper instead:
The goal is mainly to keep the behavior consistent and avoid repeating slightly different parsing logic across these files. Happy to adjust the implementation or limit the scope if a smaller change would be preferred.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tejas0306 something went wrong last time trunk was merged so the PR is showing 95 changed files instead of the few that have actually changed. Are you able to remerge / rebase so I can follow this up? |
||
| if ( '' === $version ) { | ||
| $version = wp_get_wp_version(); | ||
| } | ||
|
|
||
| $parts = preg_split( '/[.-]/', $version, 3 ); | ||
|
|
||
| return $parts[0] . '.' . ( $parts[1] ?? '0' ); | ||
| } | ||
|
|
||
| /** | ||
| * Checks compatibility with the current WordPress version. | ||
| * | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Tests for wp_get_branch_version() and wp_get_wp_version() $part parameter. | ||
| * | ||
| * @group functions | ||
| * | ||
| * @covers ::wp_get_branch_version | ||
| * @covers ::wp_get_wp_version | ||
| */ | ||
| class Tests_Functions_WpGetBranchVersion extends WP_UnitTestCase { | ||
|
|
||
| /** | ||
| * Tests extracting the major (x.y) version from a WordPress version string. | ||
| * | ||
| * @dataProvider data_major_version | ||
| * | ||
| * @ticket 64830 | ||
| * | ||
| * @param string $version The input version string. | ||
| * @param string $expected The expected major version string. | ||
| */ | ||
| public function test_major_version( $version, $expected ) { | ||
| $this->assertSame( $expected, wp_get_branch_version( $version ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Data provider for major version extraction. | ||
| * | ||
| * @return array[] | ||
| */ | ||
| public function data_major_version() { | ||
| return array( | ||
| 'major ending with 0 and no minor' => array( '7.0', '7.0' ), | ||
| 'minor number zero' => array( '7.0.0', '7.0' ), | ||
| 'minor with a major that ends in zero' => array( '7.0.1', '7.0' ), | ||
| 'double digit minor with trailing zero' => array( '7.0.10', '7.0' ), | ||
| 'double digit first part of major having zero' => array( '10.0.0', '10.0' ), | ||
| 'triple digit major' => array( '100.1.0', '100.1' ), | ||
| 'typical release' => array( '6.9', '6.9' ), | ||
| 'typical minor release' => array( '6.9.1', '6.9' ), | ||
| 'alpha suffix' => array( '7.0-alpha-61215', '7.0' ), | ||
| 'beta suffix' => array( '7.0-beta3-61849', '7.0' ), | ||
| 'RC suffix' => array( '7.0-RC1', '7.0' ), | ||
| 'src suffix' => array( '7.0-alpha-61215-src', '7.0' ), | ||
| 'single component' => array( '7', '7.0' ), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that wp_get_wp_version( 'major' ) returns the expected major version. | ||
| * | ||
| * @ticket 64830 | ||
| */ | ||
| public function test_wp_get_wp_version_major() { | ||
| $expected = wp_get_branch_version( wp_get_wp_version() ); | ||
| $this->assertSame( $expected, wp_get_wp_version( 'major' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that wp_get_wp_version( 'minor' ) returns the expected minor version. | ||
| * | ||
| * @ticket 64830 | ||
| */ | ||
| public function test_wp_get_wp_version_minor() { | ||
| $full = wp_get_wp_version(); | ||
| $parts = preg_split( '/[.-]/', $full, 4 ); | ||
| $expected = $parts[0] . '.' . ( $parts[1] ?? '0' ) . '.' . ( $parts[2] ?? '0' ); | ||
| $this->assertSame( $expected, wp_get_wp_version( 'minor' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that wp_get_wp_version() with no argument still returns the full version. | ||
| * | ||
| * @ticket 64830 | ||
| */ | ||
| public function test_wp_get_wp_version_full_default() { | ||
| $this->assertSame( $GLOBALS['wp_version'], wp_get_wp_version() ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that wp_get_wp_version( 'full' ) returns the full version. | ||
| * | ||
| * @ticket 64830 | ||
| */ | ||
| public function test_wp_get_wp_version_full_explicit() { | ||
| $this->assertSame( $GLOBALS['wp_version'], wp_get_wp_version( 'full' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that wp_get_branch_version() with no argument returns the current major version. | ||
| * | ||
| * @ticket 64830 | ||
| */ | ||
| public function test_wp_get_branch_version_defaults_to_current() { | ||
| $this->assertSame( wp_get_wp_version( 'major' ), wp_get_branch_version() ); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.