Make WordPress Core

Opened 2 months ago

Last modified 2 months ago

#62151 new enhancement

Allow Ignoring Patch Version in version_compare() for PHP Compatibility

Reported by: mostafas1990's profile mostafa.s1990 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: General Keywords: has-patch
Focuses: Cc:

Description

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
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.

Change History (4)

#1 @mostafa.s1990
2 months ago

Maybe as a new WordPress function like:

<?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

#2 @jorbin
2 months ago

  • Component changed from Plugins to General
  • Focuses administration removed

is_wp_version_compatible is already available if you want to compare with just a major version. Can you explain some use cases that would be solved by this proposal that aren't currently feasible?

#3 @mostafa.s1990
2 months ago

Thanks for the feedback, While is_wp_version_compatible helps in some cases, this proposal addresses a specific issue for self-hosted plugin update systems (e.g., example).

Currently, plugin developers need to update the "tested" version on the server with every minor WordPress release (e.g., 6.6.1, 6.6.2), even if the plugin remains compatible across all minor versions. We’ve already handled this locally in our plugin here and here, but it would be more efficient if WordPress core provided native support for this functionality.

This would simplify the process for self-hosted plugin developers and reduce the need for constant server-side metadata updates for every WordPress patch release.

This ticket was mentioned in PR #7476 on WordPress/wordpress-develop by @debarghyabanerjee.


2 months ago
#4

  • Keywords has-patch added

Trac Ticket: Core-62151

## Summary

  • This pull request introduces the wp_version_compare function to enhance version comparison capabilities in WordPress by allowing the option to ignore the patch version. This is particularly useful for compatibility checks where only major and minor versions are relevant, simplifying the process of validating plugin and theme compatibility with WordPress core.

## Problem Statement

  • The existing version_compare() function in PHP compares the full version number, including the patch (third number). In many scenarios, such as plugin development, only the major and minor versions are necessary for determining compatibility. For example, a plugin tested with WordPress version 6.6 should also be compatible with 6.6.2, but the standard comparison would return false due to the patch difference.

## Proposed Solution

  • The new wp_version_compare function provides:
  • Customizable Version Comparison: An option to ignore the patch version during comparisons, allowing developers to manage compatibility without altering API data.
  • Input Validation: Ensures that the provided versions are strings and that the comparison operator is valid, returning false for invalid parameters.

## Benefits

  • Simplifies Compatibility Checks: By allowing developers to focus on major and minor version differences, the function enhances usability in compatibility scenarios.
  • Improved Validation: Robust input validation helps prevent errors, ensuring reliable comparisons.

## Conclusion

  • This enhancement aligns with WordPress's goal of providing developers with powerful and flexible tools for managing version compatibility. By integrating the wp_version_compare function, we empower developers to streamline their workflows and reduce potential compatibility issues.
Note: See TracTickets for help on using tickets.