Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding new managed WordPress hosts to be identified in class-functions.php. #14213

Merged
merged 9 commits into from
Jan 15, 2020
85 changes: 73 additions & 12 deletions packages/sync/src/class-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,22 +161,83 @@ public static function get_post_type_features() {
* @return string Hosting provider.
*/
public static function get_hosting_provider() {
if ( defined( 'GD_SYSTEM_PLUGIN_DIR' ) || class_exists( '\\WPaaS\\Plugin' ) ) {
return 'gd-managed-wp';
}
if ( defined( 'MM_BASE_DIR' ) ) {
return 'bh';
$hosting_provider_detection_methods = array(
'get_hosting_provider_by_known_constant',
'get_hosting_provider_by_known_class',
'get_hosting_provider_by_known_function',
);

$functions = new Functions();
foreach ( $hosting_provider_detection_methods as $method ) {
$hosting_provider = call_user_func( array( $functions, $method ) );
if ( false !== $hosting_provider ) {
return $hosting_provider;
}
}
if ( defined( 'IS_PRESSABLE' ) ) {
return 'pressable';

return 'unknown';
}

/**
* Return a hosting provider using a set of known constants.
*
* @return mixed A host identifier string or false.
*/
public function get_hosting_provider_by_known_constant() {
$hosting_provider_constants = array(
'GD_SYSTEM_PLUGIN_DIR' => 'gd-managed-wp',
'MM_BASE_DIR' => 'bh',
'PAGELYBIN' => 'pagely',
'KINSTAMU_VERSION' => 'kinsta',
'FLYWHEEL_CONFIG_DIR' => 'flywheel',
'IS_PRESSABLE' => 'pressable',
'VIP_GO_ENV' => 'vip-go',
);

foreach ( $hosting_provider_constants as $constant => $constant_value ) {
oikeme marked this conversation as resolved.
Show resolved Hide resolved
if ( Constants::is_defined( $constant ) ) {
if ( 'VIP_GO_ENV' === $constant && false === Constants::get_constant( 'VIP_GO_ENV' ) ) {
oikeme marked this conversation as resolved.
Show resolved Hide resolved
continue;
}
return $constant_value;
}
}
if ( function_exists( 'is_wpe' ) || function_exists( 'is_wpe_snapshot' ) ) {
return 'wpe';

return false;
}

/**
* Return a hosting provider using a set of known classes.
*
* @return mixed A host identifier string or false.
*/
public function get_hosting_provider_by_known_class() {
$hosting_provider = false;

switch ( true ) {
case ( class_exists( '\\WPaaS\\Plugin' ) ):
oikeme marked this conversation as resolved.
Show resolved Hide resolved
$hosting_provider = 'gd-managed-wp';
break;
}
if ( defined( 'VIP_GO_ENV' ) && false !== VIP_GO_ENV ) {
return 'vip-go';

return $hosting_provider;
}

/**
* Return a hosting provider using a set of known functions.
*
* @return mixed A host identifier string or false.
*/
public function get_hosting_provider_by_known_function() {
$hosting_provider = false;

switch ( true ) {
oikeme marked this conversation as resolved.
Show resolved Hide resolved
case ( function_exists( 'is_wpe' ) || function_exists( 'is_wpe_snapshot' ) ):
oikeme marked this conversation as resolved.
Show resolved Hide resolved
$hosting_provider = 'wpe';
break;
}
return 'unknown';

return $hosting_provider;
}

/**
Expand Down
65 changes: 65 additions & 0 deletions tests/php/sync/test_class.jetpack-sync-callables.php
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,71 @@ public function test_sync_callable_recursive_gets_checksum() {
$this->assertTrue( ! empty( $synced_value ), 'We couldn\'t synced a value!' );
}

/**
* Test get_hosting_provider() callable to ensure that known hosts have the
* right hosting provider returned.
*
* @return void
*/
public function test_get_hosting_provider_callable_with_unknown_host() {
$this->assertEquals( Functions::get_hosting_provider(), 'unknown' );
}

/**
* Test getting a hosting provider by a known constant
*
* @return void
*/
public function test_get_hosting_provider_by_known_constant() {
$functions = new Functions();
Constants::set_constant( 'GD_SYSTEM_PLUGIN_DIR', 'set' );
$this->assertEquals( $functions->get_hosting_provider_by_known_constant(), 'gd-managed-wp' );
Constants::clear_constants();

Constants::set_constant( 'UNKNOWN', 'set' );
$this->assertFalse( $functions->get_hosting_provider_by_known_constant() );
Constants::clear_constants();
}

/**
* Test getting a hosting provider by a known class
*
* @return void
*/
public function test_get_hosting_provider_by_known_class() {
$functions = new Functions();

$this->assertFalse( $functions->get_hosting_provider_by_known_class() );

$class_mock = $this->getMockBuilder( '\\WPaaS\\Plugin' )
->getMock(); // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable

$this->assertEquals( $functions->get_hosting_provider_by_known_class(), 'gd-managed-wp' );

}

/**
* Test getting a hosting provider by a known function
*
* @return bool
*/
public function test_get_hosting_provider_by_known_function() {

/**
* Stub is_wpe for testing function exists
*
* @return boolean
*/
function is_wpe() {
return true;
}

$functions = new Functions();

// Get hosting provider by known function.
$this->assertEquals( $functions->get_hosting_provider_by_known_function(), 'wpe' );
}

}

function jetpack_recursive_banana() {
Expand Down