Skip to content

Commit

Permalink
refactor function for trailing whitespace add more datasets
Browse files Browse the repository at this point in the history
  • Loading branch information
afragen committed Jan 29, 2024
1 parent 0cfcdeb commit bccd14f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/wp-includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8735,14 +8735,18 @@ function is_wp_version_compatible( $required ) {
// Strip off any -alpha, -RC, -beta, -src suffixes.
list( $version ) = explode( '-', $wp_version );

if ( is_string( $required ) && substr_count( $required, '.' ) > 1 && str_ends_with( $required, '.0' ) ) {
$passed = $required;
$required = preg_replace( '/\.0/', '', $passed, 1 );
wp_trigger_error(
__FUNCTION__,
/* translators: 1: Version string sent to function, 2: Version string returned from function. */
sprintf( __( '`%1$s` Not a valid WordPress version string. `%2$s` is assumed.' ), $passed, $required )
);
if ( is_string( $required ) ) {
$passed = $required;
$trimmed = trim( $required );

if ( substr_count( $trimmed, '.' ) > 1 && str_ends_with( $trimmed, '.0' ) ) {
$required = preg_replace( '/\.0$/', '', $trimmed, 1 );
wp_trigger_error(
__FUNCTION__,
/* translators: 1: Version string sent to function, 2: Version string returned from function. */
sprintf( __( '`%1$s` Not a valid WordPress version string. `%2$s` is assumed.' ), $passed, $required )
);
}
}

return empty( $required ) || version_compare( $version, $required, '>=' );
Expand Down
15 changes: 15 additions & 0 deletions tests/phpunit/tests/functions/isWpVersionCompatible.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,21 @@ public function data_is_wp_version_compatible_should_throw_a_notice() {
'wp' => '5.0',
'expected' => true,
),
'an incorrect trailing .0 and space and same x.0 version' => array(
'required' => '5.0.0 ',
'wp' => '5.0',
'expected' => true,
),
'incorrect preceding and trailing spaces trailing .0' => array(
'required' => ' 5.0.0 ',
'wp' => '5.0',
'expected' => true,
),
'an incorrect trailing .0 on x.0.x version' => array(
'required' => '5.0.1.0',
'wp' => '5.0.1',
'expected' => true,
),
'an incorrect trailing .0 and an earlier version' => array(
'required' => '5.0.0',
'wp' => '4.0',
Expand Down

0 comments on commit bccd14f

Please sign in to comment.