Skip to content

Commit 03748b6

Browse files
committed
General: Introduce wp_get_branch_version() helper to extract major.minor version safely.
This introduces a new helper function `wp_get_branch_version()` for extracting the WordPress branch version (major.minor) from a version string. Currently, several different approaches are used across core to determine the branch version, including: - `(float)` casting of `get_bloginfo( 'version' )` - `substr( $ver, 0, 3 )` - `explode( '.' )` based parsing - `preg_split()` in `class-core-upgrader.php` Some of these approaches are fragile or incorrect: - `(float)` casting can produce incorrect values due to floating-point precision issues. - `substr( $ver, 0, 3 )` breaks for versions >= 10. - Multiple inconsistent approaches make maintenance harder. This patch introduces `wp_get_branch_version()` using a string-based approach: ```php function wp_get_branch_version( $version = '' ) { if ( '' === $version ) { $version = wp_get_wp_version(); } $parts = preg_split( '/[.-]/', $version, 3 ); return $parts[0] . '.' . ( $parts[1] ?? '0' ); }
1 parent 56a6768 commit 03748b6

7 files changed

Lines changed: 31 additions & 7 deletions

File tree

src/wp-admin/admin-header.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@
191191
$admin_body_class .= ' taxonomy-' . $current_screen->taxonomy;
192192
}
193193

194-
$admin_body_class .= ' branch-' . str_replace( array( '.', ',' ), '-', (float) get_bloginfo( 'version' ) );
194+
$admin_body_class .= ' branch-' . str_replace( '.', '-', preg_replace( '/\.0$/', '', wp_get_branch_version() ) );
195195
$admin_body_class .= ' version-' . str_replace( '.', '-', preg_replace( '/^([.0-9]+).*/', '$1', get_bloginfo( 'version' ) ) );
196196
$admin_body_class .= ' admin-color-' . sanitize_html_class( get_user_option( 'admin_color' ), 'modern' );
197197
$admin_body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_user_locale() ) ) );

src/wp-admin/includes/class-core-upgrader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ public function upgrade( $current, $args = array() ) {
279279
public static function should_update_to_version( $offered_ver ) {
280280
require ABSPATH . WPINC . '/version.php'; // $wp_version; // x.y.z
281281

282-
$current_branch = implode( '.', array_slice( preg_split( '/[.-]/', $wp_version ), 0, 2 ) ); // x.y
283-
$new_branch = implode( '.', array_slice( preg_split( '/[.-]/', $offered_ver ), 0, 2 ) ); // x.y
282+
$current_branch = wp_get_branch_version( $wp_version ); // x.y
283+
$new_branch = wp_get_branch_version( $offered_ver ); // x.y
284284

285285
$current_is_development_version = (bool) strpos( $wp_version, '-' );
286286

src/wp-admin/includes/class-wp-site-health-auto-updates.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,9 @@ public function test_all_files_writable() {
352352
$dev = ( str_contains( $wp_version, '-' ) );
353353
// Get the last stable version's files and test against that.
354354
if ( ! $checksums && $dev ) {
355-
$checksums = get_core_checksums( (float) $wp_version - 0.1, 'en_US' );
355+
$parts = preg_split( '/[.-]/', $wp_version, 3 );
356+
$prev_ver = $parts[0] . '.' . max( 0, (int) ( $parts[1] ?? 0 ) - 1 );
357+
$checksums = get_core_checksums( $prev_ver, 'en_US' );
356358
}
357359

358360
// There aren't always checksums for development releases, so just skip the test if we still can't find any.

src/wp-admin/includes/plugin-install.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ function plugins_api( $action, $args = array() ) {
113113
}
114114

115115
if ( ! isset( $args->wp_version ) ) {
116-
$args->wp_version = substr( wp_get_wp_version(), 0, 3 ); // x.y
116+
$args->wp_version = wp_get_branch_version(); // x.y
117117
}
118118

119119
/**

src/wp-admin/includes/theme.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ function themes_api( $action, $args = array() ) {
504504
}
505505

506506
if ( ! isset( $args->wp_version ) ) {
507-
$args->wp_version = substr( wp_get_wp_version(), 0, 3 ); // x.y
507+
$args->wp_version = wp_get_branch_version(); // x.y
508508
}
509509

510510
/**

src/wp-includes/block-template-utils.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1553,7 +1553,7 @@ function wp_generate_block_templates_export_file() {
15531553
$theme_json_raw = $tree->get_data();
15541554
// If a version is defined, add a schema.
15551555
if ( $theme_json_raw['version'] ) {
1556-
$theme_json_version = 'wp/' . substr( $wp_version, 0, 3 );
1556+
$theme_json_version = 'wp/' . wp_get_branch_version( $wp_version );
15571557
$schema = array( '$schema' => 'https://schemas.wp.org/' . $theme_json_version . '/theme.json' );
15581558
$theme_json_raw = array_merge( $schema, $theme_json_raw );
15591559
}

src/wp-includes/functions.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8977,6 +8977,28 @@ function wp_get_wp_version() {
89778977
return $wp_version;
89788978
}
89798979

8980+
/**
8981+
* Returns the major.minor "branch" version for a given WordPress version string.
8982+
*
8983+
* Extracts the first two version components (major and minor) from a WordPress
8984+
* version string, stripping any patch level and pre-release suffix.
8985+
*
8986+
* @since 7.1.0
8987+
*
8988+
* @param string $version Optional. A WordPress version string. Defaults to the
8989+
* current WordPress version from wp_get_wp_version().
8990+
* @return string The branch version string in "major.minor" format (e.g. "6.9").
8991+
*/
8992+
function wp_get_branch_version( $version = '' ) {
8993+
if ( '' === $version ) {
8994+
$version = wp_get_wp_version();
8995+
}
8996+
8997+
$parts = preg_split( '/[.-]/', $version, 3 );
8998+
8999+
return $parts[0] . '.' . ( $parts[1] ?? '0' );
9000+
}
9001+
89809002
/**
89819003
* Checks compatibility with the current WordPress version.
89829004
*

0 commit comments

Comments
 (0)