diff --git a/packages/sync/src/class-functions.php b/packages/sync/src/class-functions.php index dc45c5c8c6414..cfdc2bd87c068 100644 --- a/packages/sync/src/class-functions.php +++ b/packages/sync/src/class-functions.php @@ -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 ) { + if ( Constants::is_defined( $constant ) ) { + if ( 'VIP_GO_ENV' === $constant && false === Constants::get_constant( 'VIP_GO_ENV' ) ) { + 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' ) ): + $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 ) { + case ( function_exists( 'is_wpe' ) || function_exists( 'is_wpe_snapshot' ) ): + $hosting_provider = 'wpe'; + break; } - return 'unknown'; + + return $hosting_provider; } /** diff --git a/tests/php/sync/test_class.jetpack-sync-callables.php b/tests/php/sync/test_class.jetpack-sync-callables.php index d0787281ca8b0..a60ac77088224 100644 --- a/tests/php/sync/test_class.jetpack-sync-callables.php +++ b/tests/php/sync/test_class.jetpack-sync-callables.php @@ -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() {