[wp-trac] [WordPress Trac] #62151: Allow Ignoring Patch Version in version_compare() for PHP Compatibility
WordPress Trac
noreply at wordpress.org
Tue Oct 1 19:09:46 UTC 2024
#62151: Allow Ignoring Patch Version in version_compare() for PHP Compatibility
---------------------------+------------------------------
Reporter: mostafa.s1990 | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Plugins | Version:
Severity: normal | Resolution:
Keywords: | Focuses: administration
---------------------------+------------------------------
Comment (by mostafa.s1990):
Maybe as a new WordPress function like:
{{{#!php
<?php
/**
* Compare two versions with the option to ignore the patch version.
*
* @param string $version1 First version to compare.
* @param string $version2 Second version to compare.
* @param string $operator The comparison operator (like '>', '<=', etc.).
* @param bool $ignore_patch Optional. Whether to ignore the patch
version. Default is false.
*
* @return bool Result of the version comparison.
*/
function wp_version_compare($version1, $version2, $operator, $ignore_patch
= false) {
if ($ignore_patch) {
// Remove the patch version (everything after the second dot) if
present
$version1 = implode('.', array_slice(explode('.', $version1), 0,
2));
$version2 = implode('.', array_slice(explode('.', $version2), 0,
2));
}
// Use version_compare with the modified or full versions
return version_compare($version1, $version2, $operator);
}
// Example usage
echo wp_version_compare('6.6.2', '6.6', '<=', true); // Outputs true
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/62151#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list