Create VersionUtil class
This commit is contained in:
parent
8997db5f98
commit
cbf0da970a
|
@ -6,6 +6,7 @@ use Automattic\WooCommerce\Internal\DependencyManagement\AbstractServiceProvider
|
||||||
use Automattic\WooCommerce\Internal\Features\FeaturesController;
|
use Automattic\WooCommerce\Internal\Features\FeaturesController;
|
||||||
use Automattic\WooCommerce\Proxies\LegacyProxy;
|
use Automattic\WooCommerce\Proxies\LegacyProxy;
|
||||||
use Automattic\WooCommerce\Utilities\PluginUtil;
|
use Automattic\WooCommerce\Utilities\PluginUtil;
|
||||||
|
use Automattic\WooCommerce\Utilities\VersionUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Service provider for the features enabling/disabling/compatibility engine.
|
* Service provider for the features enabling/disabling/compatibility engine.
|
||||||
|
@ -19,6 +20,7 @@ class FeaturesServiceProvider extends AbstractServiceProvider {
|
||||||
*/
|
*/
|
||||||
protected $provides = array(
|
protected $provides = array(
|
||||||
FeaturesController::class,
|
FeaturesController::class,
|
||||||
|
VersionUtil::class,
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -27,5 +29,7 @@ class FeaturesServiceProvider extends AbstractServiceProvider {
|
||||||
public function register() {
|
public function register() {
|
||||||
$this->share( FeaturesController::class )
|
$this->share( FeaturesController::class )
|
||||||
->addArguments( array( LegacyProxy::class, PluginUtil::class ) );
|
->addArguments( array( LegacyProxy::class, PluginUtil::class ) );
|
||||||
|
$this->share( VersionUtil::class )
|
||||||
|
->addArguments( array( LegacyProxy::class ) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,72 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Automattic\WooCommerce\Utilities;
|
||||||
|
|
||||||
|
use Automattic\Jetpack\Constants;
|
||||||
|
use Automattic\WooCommerce\Proxies\LegacyProxy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class VersionUtil
|
||||||
|
*
|
||||||
|
* @since x.x.x
|
||||||
|
*/
|
||||||
|
class VersionUtil {
|
||||||
|
|
||||||
|
/** @var LegacyProxy */
|
||||||
|
protected $proxy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Init this class instance.
|
||||||
|
*
|
||||||
|
* @since x.x.x
|
||||||
|
*
|
||||||
|
* @param LegacyProxy $proxy
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
final public function init( LegacyProxy $proxy ) {
|
||||||
|
$this->proxy = $proxy;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the current WordPress version is at least the given version.
|
||||||
|
*
|
||||||
|
* @since x.x.x
|
||||||
|
*
|
||||||
|
* @param string $version The version to check against.
|
||||||
|
* @param bool $use_unmodified_version Whether to use the unmodified WordPress version from
|
||||||
|
* the wp-includes/version.php file.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function wp_version_at_least( string $version, bool $use_unmodified_version = false ): bool {
|
||||||
|
if ( $use_unmodified_version ) {
|
||||||
|
$wp_version = $this->get_unmodified_wp_version();
|
||||||
|
} else {
|
||||||
|
$wp_version = $this->proxy->get_global( 'wp_version' );
|
||||||
|
}
|
||||||
|
|
||||||
|
return version_compare( $wp_version, $version, '>=' );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the unmodified WordPress version from the wp-includes/version.php file.
|
||||||
|
*
|
||||||
|
* @since x.x.x
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function get_unmodified_wp_version() {
|
||||||
|
$abspath = Constants::get_constant( 'ABSPATH' );
|
||||||
|
$wpinc = Constants::get_constant( 'WPINC' );
|
||||||
|
|
||||||
|
include "{$abspath}{$wpinc}/version.php";
|
||||||
|
|
||||||
|
// Possibly strip the hyphen and any text after it from the version.
|
||||||
|
$hyphen_position = strpos( $wp_version, '-' );
|
||||||
|
if ( false !== $hyphen_position ) {
|
||||||
|
$wp_version = substr( $wp_version, 0, $hyphen_position );
|
||||||
|
}
|
||||||
|
|
||||||
|
return $wp_version;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Automattic\WooCommerce\Tests\Utilities;
|
||||||
|
|
||||||
|
use Automattic\WooCommerce\Utilities\VersionUtil;
|
||||||
|
use WC_Unit_Test_Case;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class VersionUtiilTest
|
||||||
|
*
|
||||||
|
* @since x.x.x
|
||||||
|
*/
|
||||||
|
class VersionUtiilTest extends WC_Unit_Test_Case {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The system under test.
|
||||||
|
*
|
||||||
|
* @var VersionUtil
|
||||||
|
*/
|
||||||
|
private $sut;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Runs before each test.
|
||||||
|
*/
|
||||||
|
public function setUp(): void {
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->reset_container_resolutions();
|
||||||
|
$this->reset_legacy_proxy_mocks();
|
||||||
|
|
||||||
|
$this->sut = $this->get_instance_of( VersionUtil::class );
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_wp_version_at_least() {
|
||||||
|
// Store the current wp_version
|
||||||
|
$original_wp_version = $GLOBALS['wp_version'];
|
||||||
|
|
||||||
|
// Set a fake wp_version to test.
|
||||||
|
$GLOBALS['wp_version'] = '5.6.0';
|
||||||
|
|
||||||
|
$this->assertFalse( $this->sut->wp_version_at_least( '5.6.1' ) );
|
||||||
|
$this->assertFalse( $this->sut->wp_version_at_least( '6.3' ) );
|
||||||
|
$this->assertTrue( $this->sut->wp_version_at_least( '5.6' ) );
|
||||||
|
$this->assertTrue( $this->sut->wp_version_at_least( '5.5' ) );
|
||||||
|
|
||||||
|
// Restore the original wp_version
|
||||||
|
$GLOBALS['wp_version'] = $original_wp_version;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue