Administrator
- Join Date:
- Sep 2009
- Location:
- Stuart, FL
- Posts:
- 14,142
- Plugin Contributions:
- 56
New function: zen_version_compare
With all the versions floating around in the Zen Cart universe, plugins need the capability to see if that version is, for instance, greater than or equal to some reference version.
As I found with the Edit Orders changes going from Zen Cart 1.5.5 to 1.5.5a, the built-in PHP version_compare function doesn't cut it when there's an alphabetic suffix thrown in the mix. In that case, the version_compare ('1.5.5a', '1.5.5') return value will be -1 -- indicating that 1.5.5a is less than 1.5.5.
Here's a handy-dandy little function that makes use of the PHP version_compare, but adds that alpha-suffix into the mix:
<?php
// -----
// This function "extends" the PHP version_compare function to be usable in checking Zen Cart versions; I placed it into /includes/functions/extra_functions/zen_version_compare.php:
//
// $version1 ... (string) The version to compare, e.g. '1.5.5a' or '1.5.4'
// $version2 ... (mixed) Identifies he Zen Cart version to compare against, either the explicit (string) version to use in the compare
// or (boolean) false, in which case the current Zen Cart version is used in the compare.
// $operator ... (mixed) Identifies the (optional) operator to use -- in a manner similar to the PHP version_compare function.
//
// Returns values as identified in the PHP version_compare function.
//
function zen_version_compare ($version1, $version2 = false, $operator = false)
{
if ($version2 === false) {
$version2 = PROJECT_VERSION_MAJOR . '.' . PROJECT_VERSION_MINOR;
}
$version1_suffix = substr ($version1, -1, 1);
$version2_suffix = substr ($version2, -1, 1);
$v1_alpha_suffix = ctype_alpha ($version1_suffix);
$v2_alpha_suffix = ctype_alpha ($version2_suffix);
if ($v1_alpha_suffix || $v2_alpha_suffix ) {
if ($v1_alpha_suffix) {
$version1 = substr ($version1, 0, -1) . '.' . (ord (strtoupper ($version1_suffix)) - ord ('A') + 1);
} else {
$version1 .= '.0';
}
if ($v2_alpha_suffix) {
$version2 = substr ($version2, 0, -1) . '.' . (ord (strtoupper ($version2_suffix)) - ord ('A') + 1);
} else {
$version2 .= '.0';
}
}
return ($operator === false) ? version_compare ($version1, $version2) : version_compare ($version1, $version2, $operator);
}
Here's also a debug-script that I used to test/validate the output; this one I placed into /test_version_compare.php:
<?php
require ('includes/application_top.php');
$check_array = array (
array ('1.3.9h', '1.3.9a', false, 1),
array ('1.5.5a', '1.5.5', false, 1),
array ('1.5.5', '1.5.5a', false, -1),
array ('1.5.4', '1.5.5', false, -1),
array ('1.5.4', '1.5.4', false, 0),
array ('1.5.5a', '1.5.5', '>=', true),
array ('1.5.5a', false, false, 1) //-Assumes running on Zen Cart 1.5.5!
);
foreach ($check_array as $current_values) {
$result = zen_version_compare ($current_values[0], $current_values[1], $current_values[2]);
echo "Comparing " . $current_values[0] . (($current_values[2] === false) ? ' to ' : (' ' . $current_values[2] . ' ')) . $current_values[1] . (($result === $current_values[3]) ? ' ✓' : (' ✗ (' . $result . ' != ' . $current_values[3])) . '<br />';
}