Commit 03748b6
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
- includes
- wp-includes
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
191 | 191 | | |
192 | 192 | | |
193 | 193 | | |
194 | | - | |
| 194 | + | |
195 | 195 | | |
196 | 196 | | |
197 | 197 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
279 | 279 | | |
280 | 280 | | |
281 | 281 | | |
282 | | - | |
283 | | - | |
| 282 | + | |
| 283 | + | |
284 | 284 | | |
285 | 285 | | |
286 | 286 | | |
| |||
Lines changed: 3 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
352 | 352 | | |
353 | 353 | | |
354 | 354 | | |
355 | | - | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
356 | 358 | | |
357 | 359 | | |
358 | 360 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
116 | | - | |
| 116 | + | |
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
504 | 504 | | |
505 | 505 | | |
506 | 506 | | |
507 | | - | |
| 507 | + | |
508 | 508 | | |
509 | 509 | | |
510 | 510 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1553 | 1553 | | |
1554 | 1554 | | |
1555 | 1555 | | |
1556 | | - | |
| 1556 | + | |
1557 | 1557 | | |
1558 | 1558 | | |
1559 | 1559 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8977 | 8977 | | |
8978 | 8978 | | |
8979 | 8979 | | |
| 8980 | + | |
| 8981 | + | |
| 8982 | + | |
| 8983 | + | |
| 8984 | + | |
| 8985 | + | |
| 8986 | + | |
| 8987 | + | |
| 8988 | + | |
| 8989 | + | |
| 8990 | + | |
| 8991 | + | |
| 8992 | + | |
| 8993 | + | |
| 8994 | + | |
| 8995 | + | |
| 8996 | + | |
| 8997 | + | |
| 8998 | + | |
| 8999 | + | |
| 9000 | + | |
| 9001 | + | |
8980 | 9002 | | |
8981 | 9003 | | |
8982 | 9004 | | |
| |||
0 commit comments