[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:06:51 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          |   Keywords:
  Focuses:  administration  |
----------------------------+-----------------------------
 Currently, `version_compare()` in PHP compares the full version number,
 including the patch (third number). However, in many cases, only the major
 and minor versions are relevant, such as when comparing WordPress or
 plugin versions for compatibility.

 For example, if a plugin is tested up to WordPress version `6.6` in
 `readme.txt`, but the current version is `6.6.2`, the comparison returns
 false because of the patch difference. The WordPress API adjusts this by
 adding the patch version (`6.6.2`), but it would be more efficient to
 handle this locally with `version_compare()` and ignore the patch version.

 **Proposal**:
 Add a simple option to `version_compare()` that allows ignoring the patch
 version in comparisons. This would prevent the need to modify API data for
 version compatibility checks.

 Here is an example of how this could be handled locally:

 {{{#!php
 <?php
 function version_compare_ignore_patch($version1, $version2, $operator) {
     // 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 versions
     return version_compare($version1, $version2, $operator);
 }

 // Example usage
 echo version_compare_ignore_patch('6.6.2', '6.6', '<=');  // Outputs true
 }}}



 This would streamline plugin and WordPress version compatibility checks
 without requiring adjustments on the API side, improving efficiency for
 developers.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/62151>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list