[wp-testers] Issue with version_compare()
Frank Bueltge
frank at bueltge.de
Mon Jun 14 08:12:51 UTC 2010
if ( version_compare($wp_version, "3.0alpha", "<") ) {
this works fine greater than all 3.* version.
Its great documantation on php.net
You can test this example:
<?php
$tests = array(
array('2.7.1' => '2.7.1'),
array('2.7.1' => '2.7.2'),
array('2.7.1' => '2.8-bleeding-edge'),
array('2.8' => '2.8-bleeding-edge'),
array('2.8alpha' => '2.8-bleeding-edge'),
array('2.8-bleeding-edge' => '2.8.1'),
array('2.8-bleeding-edge' => '2.7'),
array('2.7-bleeding-edge' => '2.8-bleeding-edge'),
array('2.8' => '2.8alpha'),
array('2.8' => '2.8RC1-123456'),
array('2.7' => '2.8alpha'),
array('2.7.1' => '2.8alpha'),
array('2.7.2' => '2.8alpha'),
);
function test_version_compare($first, $second) {
$result = '-n.a.-';
if (version_compare($first, $second, '<')) $result = 'is smaller';
if (version_compare($first, $second, '>')) $result = 'is greater than';
if (version_compare($first, $second, '==')) $result = 'is equal';
return $result;
}
?>
<table width="100%" style="text-align:center;">
<?php
foreach($tests as $test) {
foreach($test as $first => $second) {
?>
<tr>
<td><?php echo $first; ?></td>
<td><?php echo test_version_compare($first, $second); ?></td>
<td><?php echo $second; ?></td>
</tr>
<?php
}
}
results:
2.7.1 is smaller 2.7.2
2.7.1 is smaller 2.8-bleeding-edge
2.8 is greater than 2.8-bleeding-edge
2.8alpha is smaller 2.8-bleeding-edge
2.8-bleeding-edge is smaller 2.8.1
2.8-bleeding-edge is greater than 2.7
2.7-bleeding-edge is smaller 2.8-bleeding-edge
2.8 is greater than 2.8alpha
2.8 is greater than 2.8RC1-123456
2.7 is smaller 2.8alpha
2.7.1 is smaller 2.8alpha
2.7.2 is smaller 2.8alpha
Best regards
Frank
On Mon, Jun 14, 2010 at 9:54 AM, Dion Hulse (dd32) <wordpress at dd32.id.au>wrote:
> On Mon, 14 Jun 2010 17:48:52 +1000, Kim Parsell <kparsell-wp at kpdesign.net>
> wrote:
>
> I'm reworking my plugin, and need to use version_compare() in order to
>> serve up the correct code to users of either 2.9 or 3.0.
>>
>> A sample of the code I'm using is:
>>
>> if (version_compare($wp_version, '2.9', '=')) {
>> // do something here
>> } else if (version_compare($wp_version, '3.0', '=')) {
>> // do something else
>> }
>>
>> In testing on my 3.0 dev install (single user mode), the code to be
>> served to the 3.0 users wasn't executing unless I changed the last part
>> from '=' to '<='.
>>
>> I did a search through Trac and came upon a comment in a ticket
>> (http://core.trac.wordpress.org/ticket/13566#comment:12) where someone
>> else was having the same issue with the new importers.
>>
>> My understanding of comment 13 from mdawaffe is that api.wordpress.org
>> should now be using everything before the first "-" when comparing
>> version strings, so the code I have above *should* work; however, it
>> isn't.
>>
>> Is the change to the API only available for checking if a new version of
>> a plugin is available for download, or should it also work in a plugin
>> when doing version_compare(), such as what I'm trying to do?
>>
>
> It wont affect version_compare() since thats a PHP function, The code that
> mdawaffe has changed is only affecting the result from the WordPress API
> service.
>
> The reason its affecting you, is because, obviously, 3.0-alpha is NOT 3.0
> stable. doing 2.9 + = is also bad, as it wont affect 2.9.1, 2.9.2 etc.
>
> Instead of doing version_compare() you should be checking to see if a
> function exists, for example:
>
> if ( function_exists('some_WordPress3_functionality') ) {
> some_WordPress3_functionality(array(...));
> } else {
> // Fall back to doing it the 2.9 way
> register_something();
> add_rewrite_rule(..)
> }
>
> Cheers
>
> Dion Hulse / dd32
>
> Contact:
> e: contact at dd32.id.au
> Web: http://dd32.id.au/
> _______________________________________________
> wp-testers mailing list
> wp-testers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-testers
>
More information about the wp-testers
mailing list