From c411790e7e6dc23908ab4827a3e6b5193aaffefc Mon Sep 17 00:00:00 2001 From: Eric Binnion Date: Mon, 12 Dec 2016 12:17:04 -0600 Subject: [PATCH 01/20] Sync: IDC: Add get_raw_url method to Jetpack_Sync_Functions --- sync/class.jetpack-sync-functions.php | 22 +++++++++++++++++++ .../test_class.jetpack-sync-callables.php | 22 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/sync/class.jetpack-sync-functions.php b/sync/class.jetpack-sync-functions.php index 596af3573976..603c0018fc20 100644 --- a/sync/class.jetpack-sync-functions.php +++ b/sync/class.jetpack-sync-functions.php @@ -184,6 +184,28 @@ public static function get_protocol_normalized_url( $callable, $new_value ) { return set_url_scheme( $new_value, $forced_scheme ); } + public static function get_raw_url( $option_name ) { + global $wpdb; + + $value = null; + if ( 'home' == $option_name && Jetpack_Constants::is_defined( 'WP_HOME' ) ) { + $value = Jetpack_Constants::get_constant( 'WP_HOME' ); + } else if ( 'siteurl' == $option_name && Jetpack_Constants::is_defined( 'WP_SITEURL' ) ) { + $value = Jetpack_Constants::get_constant( 'WP_SITEURL' ); + } else { + // Let's get the option from the database so that we can bypass filters. This will help + // ensure that we get more uniform values. + $value = $wpdb->get_var( + $wpdb->prepare( + "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", + $option_name + ) + ); + } + + return $value; + } + public static function normalize_www_in_url( $option, $url_function ) { $url = wp_parse_url( call_user_func( $url_function ) ); $option_url = wp_parse_url( get_option( $option ) ); diff --git a/tests/php/sync/test_class.jetpack-sync-callables.php b/tests/php/sync/test_class.jetpack-sync-callables.php index 95694bdc9f76..adbd89b8f510 100644 --- a/tests/php/sync/test_class.jetpack-sync-callables.php +++ b/tests/php/sync/test_class.jetpack-sync-callables.php @@ -517,7 +517,29 @@ function test_sanitize_sync_taxonomies_method() { $sanitized = Jetpack_Sync_Functions::sanitize_taxonomy( (object) array( 'rest_controller_class' => 'WP_REST_Terms_Controller' ) ); $this->assertEquals( $sanitized->rest_controller_class, 'WP_REST_Terms_Controller' ); +} + function test_get_raw_url_by_option_bypasses_filters() { + add_filter( 'option_home', array( $this, '__return_filtered_url' ) ); + $this->assertTrue( 'http://filteredurl.com' !== Jetpack_Sync_Functions::get_raw_url( 'home' ) ); + remove_filter( 'option_home', array( $this, '__return_filtered_url' ) ); + } + + function test_get_raw_url_by_constant_bypasses_filters() { + Jetpack_Constants::set_constant( 'WP_HOME', 'http://constanturl.com' ); + Jetpack_Constants::set_constant( 'WP_SITEURL', 'http://constanturl.com' ); + add_filter( 'option_home', array( $this, '__return_filtered_url' ) ); + add_filter( 'option_siteurl', array( $this, '__return_filtered_url' ) ); + + $this->assertEquals( 'http://constanturl.com', Jetpack_Sync_Functions::get_raw_url( 'home' ) ); + $this->assertEquals( 'http://constanturl.com', Jetpack_Sync_Functions::get_raw_url( 'siteurl' ) ); + + remove_filter( 'option_home', array( $this, '__return_filtered_url' ) ); + remove_filter( 'option_siteurl', array( $this, '__return_filtered_url' ) ); + Jetpack_Constants::clear_constants(); + } + function __return_filtered_url() { + return 'http://filteredurl.com'; } function add_www_subdomain_to_siteurl( $url ) { From 95144163e8a6448f5a2800a3b78d8733e3d61039 Mon Sep 17 00:00:00 2001 From: Eric Binnion Date: Mon, 12 Dec 2016 13:36:40 -0600 Subject: [PATCH 02/20] Sync: IDC: Add SSL support to Jetpack_Sync_Functions::get_raw_url() --- sync/class.jetpack-sync-functions.php | 8 ++++++++ .../sync/test_class.jetpack-sync-callables.php | 15 +++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/sync/class.jetpack-sync-functions.php b/sync/class.jetpack-sync-functions.php index 603c0018fc20..3ea71b16acd0 100644 --- a/sync/class.jetpack-sync-functions.php +++ b/sync/class.jetpack-sync-functions.php @@ -203,6 +203,14 @@ public static function get_raw_url( $option_name ) { ); } + if ( is_ssl() ) { + $scheme = 'https'; + } else { + $scheme = parse_url( $value, PHP_URL_SCHEME ); + } + + $value = set_url_scheme( $value, $scheme ); + return $value; } diff --git a/tests/php/sync/test_class.jetpack-sync-callables.php b/tests/php/sync/test_class.jetpack-sync-callables.php index adbd89b8f510..e1be03d07635 100644 --- a/tests/php/sync/test_class.jetpack-sync-callables.php +++ b/tests/php/sync/test_class.jetpack-sync-callables.php @@ -538,6 +538,21 @@ function test_get_raw_url_by_constant_bypasses_filters() { Jetpack_Constants::clear_constants(); } + function test_get_raw_url_returns_with_https_if_is_ssl() { + $home_option = get_option( 'home' ); + + // Test without https first + $this->assertEquals( $home_option, Jetpack_Sync_Functions::get_raw_url( 'home' ) ); + + // Now, with https + $_SERVER['HTTPS'] = 'on'; + $this->assertEquals( + set_url_scheme( $home_option, 'https' ), + Jetpack_Sync_Functions::get_raw_url( 'home' ) + ); + unset( $_SERVER['HTTPS'] ); + } + function __return_filtered_url() { return 'http://filteredurl.com'; } From b2895f935ccb318f5fa678333030a7d91e3c5e9f Mon Sep 17 00:00:00 2001 From: Eric Binnion Date: Mon, 12 Dec 2016 13:37:07 -0600 Subject: [PATCH 03/20] Sync: IDC: Allow using raw URL with constant --- sync/class.jetpack-sync-functions.php | 14 ++++++++++++++ .../sync/test_class.jetpack-sync-callables.php | 16 ++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/sync/class.jetpack-sync-functions.php b/sync/class.jetpack-sync-functions.php index 3ea71b16acd0..f8a109ffd09f 100644 --- a/sync/class.jetpack-sync-functions.php +++ b/sync/class.jetpack-sync-functions.php @@ -145,6 +145,13 @@ public static function file_system_write_access() { } public static function home_url() { + if ( + Jetpack_Constants::is_defined( 'JETPACK_SYNC_USE_RAW_URL' ) && + Jetpack_Constants::get_constant( 'JETPACK_SYNC_USE_RAW_URL' ) + ) { + return self::get_raw_url( 'home' ); + } + return self::get_protocol_normalized_url( 'home_url', self::normalize_www_in_url( 'home', 'home_url' ) @@ -152,6 +159,13 @@ public static function home_url() { } public static function site_url() { + if ( + Jetpack_Constants::is_defined( 'JETPACK_SYNC_USE_RAW_URL' ) && + Jetpack_Constants::get_constant( 'JETPACK_SYNC_USE_RAW_URL' ) + ) { + return self::get_raw_url( 'siteurl' ); + } + return self::get_protocol_normalized_url( 'site_url', self::normalize_www_in_url( 'siteurl', 'site_url' ) diff --git a/tests/php/sync/test_class.jetpack-sync-callables.php b/tests/php/sync/test_class.jetpack-sync-callables.php index e1be03d07635..d75d5466262b 100644 --- a/tests/php/sync/test_class.jetpack-sync-callables.php +++ b/tests/php/sync/test_class.jetpack-sync-callables.php @@ -553,6 +553,22 @@ function test_get_raw_url_returns_with_https_if_is_ssl() { unset( $_SERVER['HTTPS'] ); } + function test_urls_are_raw_with_use_raw_url_constant() { + add_filter( 'option_home', array( $this, '__return_filtered_url' ) ); + add_filter( 'option_siteurl', array( $this, '__return_filtered_url' ) ); + + // Test with constant first + Jetpack_Constants::set_constant( 'JETPACK_SYNC_USE_RAW_URL', true ); + $this->assertTrue( 'http://filteredurl.com' !== Jetpack_Sync_Functions::home_url() ); + Jetpack_Constants::clear_constants(); + + // Now, without, which should return the filtered URL + $this->assertEquals( $this->__return_filtered_url(), Jetpack_Sync_Functions::home_url() ); + + remove_filter( 'option_home', array( $this, '__return_filtered_url' ) ); + remove_filter( 'option_siteurl', array( $this, '__return_filtered_url' ) ); + } + function __return_filtered_url() { return 'http://filteredurl.com'; } From 0bb2aeb9c420ab4c76a84e87f221c1c8c3ee7363 Mon Sep 17 00:00:00 2001 From: Eric Binnion Date: Mon, 12 Dec 2016 16:20:04 -0600 Subject: [PATCH 04/20] Sync: IDC: Add filters to the home_url and site_url callables --- sync/class.jetpack-sync-functions.php | 36 +++++++++++++++++++-------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/sync/class.jetpack-sync-functions.php b/sync/class.jetpack-sync-functions.php index f8a109ffd09f..c4ec42bc5c49 100644 --- a/sync/class.jetpack-sync-functions.php +++ b/sync/class.jetpack-sync-functions.php @@ -149,13 +149,21 @@ public static function home_url() { Jetpack_Constants::is_defined( 'JETPACK_SYNC_USE_RAW_URL' ) && Jetpack_Constants::get_constant( 'JETPACK_SYNC_USE_RAW_URL' ) ) { - return self::get_raw_url( 'home' ); + $home_url = self::get_raw_url( 'home' ); + } else { + $home_url = self::normalize_www_in_url( 'home', 'home_url' ); } - return self::get_protocol_normalized_url( - 'home_url', - self::normalize_www_in_url( 'home', 'home_url' ) - ); + $home_url = self::get_protocol_normalized_url( 'home_url', $home_url ); + + /** + * Allows overriding of the home_url value that is synced back to WordPress.com. + * + * @since 4.6 + * + * @param string $home_url + */ + return esc_url_raw( apply_filters( 'jetpack_sync_home_url', $home_url ) ); } public static function site_url() { @@ -163,13 +171,21 @@ public static function site_url() { Jetpack_Constants::is_defined( 'JETPACK_SYNC_USE_RAW_URL' ) && Jetpack_Constants::get_constant( 'JETPACK_SYNC_USE_RAW_URL' ) ) { - return self::get_raw_url( 'siteurl' ); + $site_url = self::get_raw_url( 'siteurl' ); + } else { + $site_url = self::normalize_www_in_url( 'siteurl', 'site_url' ); } - return self::get_protocol_normalized_url( - 'site_url', - self::normalize_www_in_url( 'siteurl', 'site_url' ) - ); + $site_url = self::get_protocol_normalized_url( 'site_url', $site_url ); + + /** + * Allows overriding of the site_url value that is synced back to WordPress.com. + * + * @since 4.6 + * + * @param string $site_url + */ + return esc_url_raw( apply_filters( 'jetpack_sync_site_url', $site_url ) ); } public static function main_network_site_url() { From 07df0da77fa70aa934efc2314aa75c169738bc81 Mon Sep 17 00:00:00 2001 From: Eric Binnion Date: Mon, 12 Dec 2016 17:21:58 -0600 Subject: [PATCH 05/20] Sync: IDC: Add support for domain mapping --- 3rd-party/3rd-party.php | 1 + 3rd-party/domain-mapping.php | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 3rd-party/domain-mapping.php diff --git a/3rd-party/3rd-party.php b/3rd-party/3rd-party.php index 9d5462747315..cdf08f273e27 100644 --- a/3rd-party/3rd-party.php +++ b/3rd-party/3rd-party.php @@ -10,6 +10,7 @@ require_once( JETPACK__PLUGIN_DIR . '3rd-party/bitly.php' ); require_once( JETPACK__PLUGIN_DIR . '3rd-party/bbpress.php' ); require_once( JETPACK__PLUGIN_DIR . '3rd-party/woocommerce.php' ); +require_once( JETPACK__PLUGIN_DIR . '3rd-party/domain-mapping.php' ); // We can't load this conditionally since polldaddy add the call in class constuctor. require_once( JETPACK__PLUGIN_DIR . '3rd-party/polldaddy.php' ); diff --git a/3rd-party/domain-mapping.php b/3rd-party/domain-mapping.php new file mode 100644 index 000000000000..bd7446365415 --- /dev/null +++ b/3rd-party/domain-mapping.php @@ -0,0 +1,37 @@ + Date: Mon, 12 Dec 2016 20:51:33 -0600 Subject: [PATCH 06/20] Sync: IDC: 3rd Party: Hook domain mapping plugins to sync filters --- 3rd-party/domain-mapping.php | 79 ++++++++++++++++++++++++------------ 1 file changed, 52 insertions(+), 27 deletions(-) diff --git a/3rd-party/domain-mapping.php b/3rd-party/domain-mapping.php index bd7446365415..655928322f4e 100644 --- a/3rd-party/domain-mapping.php +++ b/3rd-party/domain-mapping.php @@ -1,37 +1,62 @@ Date: Tue, 13 Dec 2016 11:36:54 -0600 Subject: [PATCH 07/20] Sync: IDC: Add tests for 3rd party domain mapping support --- 3rd-party/domain-mapping.php | 149 +++++++++++------ phpunit.xml.dist | 3 + .../test_class.jetpack-domain-mapping.php | 152 ++++++++++++++++++ 3 files changed, 255 insertions(+), 49 deletions(-) create mode 100644 tests/php/3rd-party/test_class.jetpack-domain-mapping.php diff --git a/3rd-party/domain-mapping.php b/3rd-party/domain-mapping.php index 655928322f4e..6742651b4f75 100644 --- a/3rd-party/domain-mapping.php +++ b/3rd-party/domain-mapping.php @@ -1,62 +1,113 @@ function_exists( 'domain_mapping_siteurl' ) ) { + return false; + } + + add_filter( 'jetpack_sync_home_url', 'domain_mapping_siteurl' ); + add_filter( 'jetpack_sync_site_url', 'domain_mapping_siteurl' ); + + return true; + } + + /** + * This method will test for a class and method known to be used in WPMU Dev's domain mapping plugin. If the + * method exists, then we'll hook the swap_to_mapped_url() to our Jetpack sync filters for home_url and site_url. + * + * @return bool + */ + function hook_wpmu_dev_domain_mapping() { + if ( ! $this->class_exists( 'domain_map' ) || ! $this->method_exists( 'domain_map', 'utils' ) ) { + return false; + } + + $utils = $this->get_domain_mapping_utils_instance(); + add_filter( 'jetpack_sync_home_url', array( $utils, 'swap_to_mapped_url' ) ); + add_filter( 'jetpack_sync_site_url', array( $utils, 'swap_to_mapped_url' ) ); + + return true; + } + + /* + * Utility Methods + * + * These methods are very minimal, and in most cases, simply pass on arguments. Why create them you ask? + * So that we can test. */ - function jetpack_domain_mapping_hook_compatible_plugins() { - $methods = get_class_methods( 'Jetpack_3rd_Party_Domain_Mapping' ); - do { - $method = array_pop( $methods ); - $hooked = call_user_func( 'Jetpack_3rd_Party_Domain_Mapping', $method ); - } while( ! $hooked && ! empty( $methods ) ); - } - add_action( 'plugins_loaded', 'jetpack_domain_mapping_hook_compatible_plugins' ); -endif; + + public function method_exists( $class, $method ) { + return method_exists( $class, $method ); + } + + public function class_exists( $class ) { + return class_exists( $class ); + } + + public function function_exists( $function ) { + return function_exists( $function ); + } + + public function get_domain_mapping_utils_instance() { + return domain_map::utils(); + } +} + +Jetpack_3rd_Party_Domain_Mapping::init(); diff --git a/phpunit.xml.dist b/phpunit.xml.dist index c25dbf8962a4..2f27db1b85b2 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -80,6 +80,9 @@ tests/php/test_class.jetpack-jitm.php + + tests/php/3rd-party + diff --git a/tests/php/3rd-party/test_class.jetpack-domain-mapping.php b/tests/php/3rd-party/test_class.jetpack-domain-mapping.php new file mode 100644 index 000000000000..caeb06887f35 --- /dev/null +++ b/tests/php/3rd-party/test_class.jetpack-domain-mapping.php @@ -0,0 +1,152 @@ +get_jetpack_sync_filters() as $filter ) { + remove_all_filters( $filter ); + } + } + + function test_domain_mapping_should_not_try_to_hook_when_sunrise_disable() { + $stub = $this->getMockBuilder( 'MockDomainMapping' ) + ->setMethods( array( 'hook_wordpress_mu_domain_mapping', 'hook_wpmu_dev_domain_mapping' ) ) + ->disableOriginalConstructor() + ->getMock(); + + // Both of these methods should not be called + $stub->expects( $this->exactly( 0 ) ) + ->method( 'hook_wordpress_mu_domain_mapping' ) + ->will( $this->returnValue( false ) ); + + $stub->expects( $this->exactly( 0 ) ) + ->method( 'hook_wpmu_dev_domain_mapping' ) + ->will( $this->returnValue( false ) ); + + $stub->attempt_to_hook_domain_mapping_plugins(); + } + + function test_domain_mapping_should_stop_search_after_hooking_once() { + Jetpack_Constants::set_constant( 'SUNRISE', true ); + + $stub = $this->getMockBuilder( 'MockDomainMapping' ) + ->setMethods( array( 'hook_wordpress_mu_domain_mapping', 'hook_wpmu_dev_domain_mapping' ) ) + ->disableOriginalConstructor() + ->getMock(); + + // The first method in the array should be the only one called. + $stub->expects( $this->exactly( 1 ) ) + ->method( 'hook_wordpress_mu_domain_mapping' ) + ->will( $this->returnValue( true ) ); + + $stub->expects( $this->exactly( 0 ) ) + ->method( 'hook_wpmu_dev_domain_mapping' ) + ->will( $this->returnValue( false ) ); + + $stub->attempt_to_hook_domain_mapping_plugins(); + } + + function test_domain_mapping_mu_domain_mapping_not_hooked_when_function_not_exists() { + Jetpack_Constants::set_constant( 'DOMAIN_MAPPING', true ); + + $stub = $this->getMockBuilder( 'MockDomainMapping' ) + ->setMethods( array( 'function_exists' ) ) + ->disableOriginalConstructor() + ->getMock(); + + $stub->expects( $this->once() ) + ->method( 'function_exists' ) + ->will( $this->returnValue( false ) ); + + $this->assertFalse( $stub->hook_wordpress_mu_domain_mapping() ); + + foreach ( $this->get_jetpack_sync_filters() as $filter ) { + $this->assertFalse( $this->filter_has_hook( $filter ) ); + } + } + + function test_domain_mapping_mu_domain_mapping_hooked_when_function_exists() { + Jetpack_Constants::set_constant( 'DOMAIN_MAPPING', true ); + + $stub = $this->getMockBuilder( 'MockDomainMapping' ) + ->setMethods( array( 'function_exists' ) ) + ->disableOriginalConstructor() + ->getMock(); + + $stub->expects( $this->once() ) + ->method( 'function_exists' ) + ->will( $this->returnValue( true ) ); + + $this->assertTrue( $stub->hook_wordpress_mu_domain_mapping() ); + + foreach ( $this->get_jetpack_sync_filters() as $filter ) { + $this->assertTrue( $this->filter_has_hook( $filter ) ); + } + } + + function test_domain_mapping_wpmu_dev_domain_mapping_not_hooked_when_functions_not_exist() { + $stub = $this->getMockBuilder( 'MockDomainMapping' ) + ->setMethods( array( 'class_exists', 'method_exists' ) ) + ->disableOriginalConstructor() + ->getMock(); + + $stub->expects( $this->once() ) + ->method( 'class_exists' ) + ->will( $this->returnValue( false ) ); + + $stub->expects( $this->exactly( 0 ) ) + ->method( 'method_exists' ) + ->will( $this->returnValue( false ) ); + + $this->assertFalse( $stub->hook_wpmu_dev_domain_mapping() ); + + foreach ( $this->get_jetpack_sync_filters() as $filter ) { + $this->assertFalse( $this->filter_has_hook( $filter ) ); + } + } + + function test_domain_mapping_wpmu_dev_domain_mapping_hooked_when_functions_exist() { + $stub = $this->getMockBuilder( 'MockDomainMapping' ) + ->setMethods( array( 'class_exists', 'method_exists', 'get_domain_mapping_utils_instance' ) ) + ->disableOriginalConstructor() + ->getMock(); + + $stub->expects( $this->once() ) + ->method( 'class_exists' ) + ->will( $this->returnValue( true ) ); + + $stub->expects( $this->once() ) + ->method( 'method_exists' ) + ->will( $this->returnValue( true ) ); + + $stub->expects( $this->once() ) + ->method( 'get_domain_mapping_utils_instance' ) + ->will( $this->returnValue( new stdClass() ) ); + + $this->assertTrue( $stub->hook_wpmu_dev_domain_mapping() ); + + foreach ( $this->get_jetpack_sync_filters() as $filter ) { + $this->assertTrue( $this->filter_has_hook( $filter ) ); + } + } + + function filter_has_hook( $hook ) { + global $wp_filter; + return isset( $wp_filter[ $hook ] ) && ! empty( $wp_filter[ $hook ] ); + } + + function get_jetpack_sync_filters() { + return array( + 'jetpack_sync_home_url', + 'jetpack_sync_site_url', + ); + } +} From 8d3a7fb79076bc46284f66202fbed534e47ad323 Mon Sep 17 00:00:00 2001 From: Eric Binnion Date: Tue, 13 Dec 2016 14:15:20 -0600 Subject: [PATCH 08/20] Sync: IDC: Fix broken test by changing constant to static variable --- 3rd-party/domain-mapping.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/3rd-party/domain-mapping.php b/3rd-party/domain-mapping.php index 6742651b4f75..82769b2d9458 100644 --- a/3rd-party/domain-mapping.php +++ b/3rd-party/domain-mapping.php @@ -17,7 +17,7 @@ class Jetpack_3rd_Party_Domain_Mapping { * * @var array */ - const TEST_METHODS = array( + static $test_methods = array( 'hook_wordpress_mu_domain_mapping', 'hook_wpmu_dev_domain_mapping' ); @@ -31,11 +31,12 @@ static function init() { } private function __construct() { + error_log( 'domain mapping construct called' ); add_action( 'plugins_loaded', array( $this, 'attempt_to_hook_domain_mapping_plugins' ) ); } /** - * This function is called on the plugins_loaded action and will loop through the TEST_METHODS + * This function is called on the plugins_loaded action and will loop through the $test_methods * to try and hook a domain mapping plugin to the Jetpack sync filters for the home_url and site_url callables. */ function attempt_to_hook_domain_mapping_plugins() { @@ -44,9 +45,9 @@ function attempt_to_hook_domain_mapping_plugins() { } $hooked = false; - $count = count( self::TEST_METHODS ); + $count = count( self::$test_methods ); for ( $i = 0; $i < $count && ! $hooked; $i++ ) { - $hooked = call_user_func( array( $this, self::TEST_METHODS[ $i ] ) ); + $hooked = call_user_func( array( $this, self::$test_methods[ $i ] ) ); } } From b9a8df6fe36b550267c60f892f244e97ccd2d0ec Mon Sep 17 00:00:00 2001 From: Eric Binnion Date: Tue, 13 Dec 2016 14:33:01 -0600 Subject: [PATCH 09/20] Sync: IDC: Use Jetpack_Sync_Options to bypass potential caching issues --- sync/class.jetpack-sync-functions.php | 7 +------ sync/class.jetpack-sync-module-callables.php | 4 ++-- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/sync/class.jetpack-sync-functions.php b/sync/class.jetpack-sync-functions.php index c4ec42bc5c49..64deac9538d4 100644 --- a/sync/class.jetpack-sync-functions.php +++ b/sync/class.jetpack-sync-functions.php @@ -225,12 +225,7 @@ public static function get_raw_url( $option_name ) { } else { // Let's get the option from the database so that we can bypass filters. This will help // ensure that we get more uniform values. - $value = $wpdb->get_var( - $wpdb->prepare( - "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", - $option_name - ) - ); + $value = Jetpack_Sync_Options::get_option( $option_name ); } if ( is_ssl() ) { diff --git a/sync/class.jetpack-sync-module-callables.php b/sync/class.jetpack-sync-module-callables.php index b056521332b0..08828924868e 100644 --- a/sync/class.jetpack-sync-module-callables.php +++ b/sync/class.jetpack-sync-module-callables.php @@ -145,7 +145,7 @@ public function maybe_sync_callables() { return; } - $callable_checksums = (array) get_option( self::CALLABLES_CHECKSUM_OPTION_NAME, array() ); + $callable_checksums = (array) Jetpack_Sync_Options::get_option( self::CALLABLES_CHECKSUM_OPTION_NAME, array() ); // only send the callables that have changed foreach ( $callables as $name => $value ) { @@ -166,7 +166,7 @@ public function maybe_sync_callables() { $callable_checksums[ $name ] = $checksum; } } - update_option( self::CALLABLES_CHECKSUM_OPTION_NAME, $callable_checksums ); + Jetpack_Sync_Options::update_option( self::CALLABLES_CHECKSUM_OPTION_NAME, $callable_checksums ); } public function expand_callables( $args ) { From 7214ce765c9a6d620b1c7275c06f3c4cd3e8a7ed Mon Sep 17 00:00:00 2001 From: Eric Binnion Date: Tue, 13 Dec 2016 15:36:34 -0600 Subject: [PATCH 10/20] Sync: IDC: Remove error_log that was causing tests to fail --- 3rd-party/domain-mapping.php | 1 - 1 file changed, 1 deletion(-) diff --git a/3rd-party/domain-mapping.php b/3rd-party/domain-mapping.php index 82769b2d9458..638298bed21b 100644 --- a/3rd-party/domain-mapping.php +++ b/3rd-party/domain-mapping.php @@ -31,7 +31,6 @@ static function init() { } private function __construct() { - error_log( 'domain mapping construct called' ); add_action( 'plugins_loaded', array( $this, 'attempt_to_hook_domain_mapping_plugins' ) ); } From 8be4bea99530f0f981c968f23331bcb1c5a31555 Mon Sep 17 00:00:00 2001 From: Eric Binnion Date: Wed, 14 Dec 2016 11:01:27 -0600 Subject: [PATCH 11/20] Sync: IDC: Swap DOMAIN_MAPPING constant check to SUNRISE_LOADED In the WordPress MU Domain Mapping plugin, the DOMAIN_MAPPING constant is only set if we're actively mapping a domain. So, if thisdomain.com points to somesubsite.network.com. This led to the 3rd party domain mapping logic to fail for site where the admin was not using the mapped domain. The SUNRISE_LOADED constant is loaded on every request with the WordPress MU Domain Mapping plugin. --- 3rd-party/domain-mapping.php | 2 +- tests/php/3rd-party/test_class.jetpack-domain-mapping.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/3rd-party/domain-mapping.php b/3rd-party/domain-mapping.php index 638298bed21b..6079ac325908 100644 --- a/3rd-party/domain-mapping.php +++ b/3rd-party/domain-mapping.php @@ -58,7 +58,7 @@ function attempt_to_hook_domain_mapping_plugins() { * @return bool */ function hook_wordpress_mu_domain_mapping() { - if ( ! Jetpack_Constants::is_defined( 'DOMAIN_MAPPING' ) || ! $this->function_exists( 'domain_mapping_siteurl' ) ) { + if ( ! Jetpack_Constants::is_defined( 'SUNRISE_LOADED' ) || ! $this->function_exists( 'domain_mapping_siteurl' ) ) { return false; } diff --git a/tests/php/3rd-party/test_class.jetpack-domain-mapping.php b/tests/php/3rd-party/test_class.jetpack-domain-mapping.php index caeb06887f35..3e53b86331ba 100644 --- a/tests/php/3rd-party/test_class.jetpack-domain-mapping.php +++ b/tests/php/3rd-party/test_class.jetpack-domain-mapping.php @@ -55,7 +55,7 @@ function test_domain_mapping_should_stop_search_after_hooking_once() { } function test_domain_mapping_mu_domain_mapping_not_hooked_when_function_not_exists() { - Jetpack_Constants::set_constant( 'DOMAIN_MAPPING', true ); + Jetpack_Constants::set_constant( 'SUNRISE_LOADED', true ); $stub = $this->getMockBuilder( 'MockDomainMapping' ) ->setMethods( array( 'function_exists' ) ) @@ -74,7 +74,7 @@ function test_domain_mapping_mu_domain_mapping_not_hooked_when_function_not_exis } function test_domain_mapping_mu_domain_mapping_hooked_when_function_exists() { - Jetpack_Constants::set_constant( 'DOMAIN_MAPPING', true ); + Jetpack_Constants::set_constant( 'SUNRISE_LOADED', true ); $stub = $this->getMockBuilder( 'MockDomainMapping' ) ->setMethods( array( 'function_exists' ) ) From e54dbebe2347a2dc4ae95c9250adabed8d62504a Mon Sep 17 00:00:00 2001 From: Eric Binnion Date: Mon, 30 Jan 2017 16:02:24 -0600 Subject: [PATCH 12/20] Sync: Default to using raw home and siteurl values --- sync/class.jetpack-sync-functions.php | 4 +-- .../test_class.jetpack-sync-callables.php | 35 +++++-------------- 2 files changed, 10 insertions(+), 29 deletions(-) diff --git a/sync/class.jetpack-sync-functions.php b/sync/class.jetpack-sync-functions.php index 64deac9538d4..8345c56340dd 100644 --- a/sync/class.jetpack-sync-functions.php +++ b/sync/class.jetpack-sync-functions.php @@ -146,7 +146,7 @@ public static function file_system_write_access() { public static function home_url() { if ( - Jetpack_Constants::is_defined( 'JETPACK_SYNC_USE_RAW_URL' ) && + ! Jetpack_Constants::is_defined( 'JETPACK_SYNC_USE_RAW_URL' ) || Jetpack_Constants::get_constant( 'JETPACK_SYNC_USE_RAW_URL' ) ) { $home_url = self::get_raw_url( 'home' ); @@ -168,7 +168,7 @@ public static function home_url() { public static function site_url() { if ( - Jetpack_Constants::is_defined( 'JETPACK_SYNC_USE_RAW_URL' ) && + ! Jetpack_Constants::is_defined( 'JETPACK_SYNC_USE_RAW_URL' ) || Jetpack_Constants::get_constant( 'JETPACK_SYNC_USE_RAW_URL' ) ) { $site_url = self::get_raw_url( 'siteurl' ); diff --git a/tests/php/sync/test_class.jetpack-sync-callables.php b/tests/php/sync/test_class.jetpack-sync-callables.php index d75d5466262b..09276341f799 100644 --- a/tests/php/sync/test_class.jetpack-sync-callables.php +++ b/tests/php/sync/test_class.jetpack-sync-callables.php @@ -204,9 +204,8 @@ function test_sync_jetpack_sync_unlock_sync_callable_action_allows_syncing_siteu $this->server_replica_storage->reset(); - // We set the filters here to simulate how setting the WP_HOME and WP_SITEURL constant works. - add_filter( 'option_home', array( $this, 'return_https_site_com_blog' ) ); - add_filter( 'option_siteurl', array( $this, 'return_https_site_com_blog' ) ); + update_option( 'home', $this->return_https_site_com_blog() ); + update_option( 'siteurl', $this->return_https_site_com_blog() ); /** * Used to signal that the callables await transient should be cleared. Clearing the await transient is useful @@ -228,8 +227,9 @@ function test_sync_jetpack_sync_unlock_sync_callable_action_allows_syncing_siteu // Cleanup unset( $_SERVER['HTTPS'] ); - remove_filter( 'option_home', array( $this, 'return_https_site_com_blog' ) ); - remove_filter( 'option_siteurl', array( $this, 'return_https_site_com_blog' ) ); + + update_option( 'home', $original_home_option ); + update_option( 'siteurl', $original_siteurl_option ); } function test_home_site_urls_synced_while_migrate_for_idc_set() { @@ -270,25 +270,6 @@ function test_home_site_urls_synced_while_migrate_for_idc_set() { Jetpack_Options::delete_option( 'migrate_for_idc' ); } - function test_scheme_switching_does_not_cause_sync() { - $this->setSyncClientDefaults(); - delete_transient( Jetpack_Sync_Module_Callables::CALLABLES_AWAIT_TRANSIENT_NAME ); - delete_option( Jetpack_Sync_Module_Callables::CALLABLES_CHECKSUM_OPTION_NAME ); - $_SERVER['HTTPS'] = 'off'; - $home_url = home_url(); - $this->sender->do_sync(); - - $this->assertEquals( $home_url, $this->server_replica_storage->get_callable( 'home_url' ) ); - - // this sets is_ssl() to return true. - $_SERVER['HTTPS'] = 'on'; - delete_transient( Jetpack_Sync_Module_Callables::CALLABLES_AWAIT_TRANSIENT_NAME ); - $this->sender->do_sync(); - - unset( $_SERVER['HTTPS'] ); - $this->assertEquals( $home_url, $this->server_replica_storage->get_callable( 'home_url' ) ); - } - function return_example_com() { return 'http://example.com'; } @@ -553,17 +534,17 @@ function test_get_raw_url_returns_with_https_if_is_ssl() { unset( $_SERVER['HTTPS'] ); } - function test_urls_are_raw_with_use_raw_url_constant() { + function test_user_can_stop_raw_urls() { add_filter( 'option_home', array( $this, '__return_filtered_url' ) ); add_filter( 'option_siteurl', array( $this, '__return_filtered_url' ) ); // Test with constant first - Jetpack_Constants::set_constant( 'JETPACK_SYNC_USE_RAW_URL', true ); $this->assertTrue( 'http://filteredurl.com' !== Jetpack_Sync_Functions::home_url() ); - Jetpack_Constants::clear_constants(); // Now, without, which should return the filtered URL + Jetpack_Constants::set_constant( 'JETPACK_SYNC_USE_RAW_URL', false ); $this->assertEquals( $this->__return_filtered_url(), Jetpack_Sync_Functions::home_url() ); + Jetpack_Constants::clear_constants(); remove_filter( 'option_home', array( $this, '__return_filtered_url' ) ); remove_filter( 'option_siteurl', array( $this, '__return_filtered_url' ) ); From f6e22f5da6cec4210f9a8e7789d7cbe7810d09d4 Mon Sep 17 00:00:00 2001 From: Eric Binnion Date: Tue, 18 Jul 2017 21:49:30 -0500 Subject: [PATCH 13/20] Sync: Stop using removed Jetpack_Sync_Options class --- sync/class.jetpack-sync-functions.php | 2 +- sync/class.jetpack-sync-module-callables.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sync/class.jetpack-sync-functions.php b/sync/class.jetpack-sync-functions.php index 8345c56340dd..ddaeea702faa 100644 --- a/sync/class.jetpack-sync-functions.php +++ b/sync/class.jetpack-sync-functions.php @@ -225,7 +225,7 @@ public static function get_raw_url( $option_name ) { } else { // Let's get the option from the database so that we can bypass filters. This will help // ensure that we get more uniform values. - $value = Jetpack_Sync_Options::get_option( $option_name ); + $value = Jetpack_Options::get_raw_option( $option_name ); } if ( is_ssl() ) { diff --git a/sync/class.jetpack-sync-module-callables.php b/sync/class.jetpack-sync-module-callables.php index 08828924868e..917345b3f61d 100644 --- a/sync/class.jetpack-sync-module-callables.php +++ b/sync/class.jetpack-sync-module-callables.php @@ -145,7 +145,7 @@ public function maybe_sync_callables() { return; } - $callable_checksums = (array) Jetpack_Sync_Options::get_option( self::CALLABLES_CHECKSUM_OPTION_NAME, array() ); + $callable_checksums = (array) Jetpack_Options::get_raw_option( self::CALLABLES_CHECKSUM_OPTION_NAME, array() ); // only send the callables that have changed foreach ( $callables as $name => $value ) { @@ -166,7 +166,7 @@ public function maybe_sync_callables() { $callable_checksums[ $name ] = $checksum; } } - Jetpack_Sync_Options::update_option( self::CALLABLES_CHECKSUM_OPTION_NAME, $callable_checksums ); + Jetpack_Options::update_raw_option( self::CALLABLES_CHECKSUM_OPTION_NAME, $callable_checksums ); } public function expand_callables( $args ) { From e244ddeee8dd2d33a47a100f0384dbbced492940 Mon Sep 17 00:00:00 2001 From: Eric Binnion Date: Fri, 21 Jul 2017 19:13:07 -0500 Subject: [PATCH 14/20] Sync: IDC: Get home/siteurl with sync functions method --- sync/class.jetpack-sync-actions.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sync/class.jetpack-sync-actions.php b/sync/class.jetpack-sync-actions.php index 05a507783448..a01d819abdd0 100644 --- a/sync/class.jetpack-sync-actions.php +++ b/sync/class.jetpack-sync-actions.php @@ -113,6 +113,7 @@ static function set_is_importing_true() { } static function send_data( $data, $codec_name, $sent_timestamp, $queue_id, $checkout_duration, $preprocess_duration ) { + require_once dirname( __FILE__ ) . '/class.jetpack-sync-functions.php'; Jetpack::load_xml_rpc_client(); $query_args = array( @@ -120,8 +121,8 @@ static function send_data( $data, $codec_name, $sent_timestamp, $queue_id, $chec 'codec' => $codec_name, // send the name of the codec used to encode the data 'timestamp' => $sent_timestamp, // send current server time so we can compensate for clock differences 'queue' => $queue_id, // sync or full_sync - 'home' => get_home_url(), // Send home url option to check for Identity Crisis server-side - 'siteurl' => get_site_url(), // Send siteurl option to check for Identity Crisis server-side + 'home' => Jetpack_Sync_Functions::home_url(), // Send home url option to check for Identity Crisis server-side + 'siteurl' => Jetpack_Sync_Functions::site_url(), // Send siteurl option to check for Identity Crisis server-side 'cd' => sprintf( '%.4f', $checkout_duration), // Time spent retrieving queue items from the DB 'pd' => sprintf( '%.4f', $preprocess_duration), // Time spent converting queue items into data to send ); From bfd6b4615593b30c8f4fc20b61ec6c093abbd184 Mon Sep 17 00:00:00 2001 From: Eric Binnion Date: Fri, 21 Jul 2017 20:38:46 -0500 Subject: [PATCH 15/20] IDC: Remove broken test that is no longer applicable --- tests/php/test_class.jetpack.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/tests/php/test_class.jetpack.php b/tests/php/test_class.jetpack.php index bdf5590d69c9..bf364c15d732 100644 --- a/tests/php/test_class.jetpack.php +++ b/tests/php/test_class.jetpack.php @@ -323,14 +323,6 @@ function test_idc_optin_default() { } } - function test_idc_optin_false_when_sunrise() { - Jetpack_Constants::set_constant( 'SUNRISE', true ); - - $this->assertFalse( Jetpack::sync_idc_optin() ); - - Jetpack_Constants::clear_constants(); - } - function test_idc_optin_filter_overrides_development_version() { add_filter( 'jetpack_development_version', '__return_true' ); add_filter( 'jetpack_sync_idc_optin', '__return_false' ); From 1824c78c8c492ff265bd5d188a6b2ede3c69e00f Mon Sep 17 00:00:00 2001 From: Eric Binnion Date: Sat, 22 Jul 2017 12:30:33 -0500 Subject: [PATCH 16/20] Sync: Factor out logic to get raw URL; Do not set scheme --- sync/class.jetpack-sync-functions.php | 44 +++++++++++---------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/sync/class.jetpack-sync-functions.php b/sync/class.jetpack-sync-functions.php index ddaeea702faa..07790649a910 100644 --- a/sync/class.jetpack-sync-functions.php +++ b/sync/class.jetpack-sync-functions.php @@ -144,17 +144,28 @@ public static function file_system_write_access() { return false; } - public static function home_url() { + /** + * Helper function that is used when getting home or siteurl values. Decides + * whether to get the raw or filtered value. + * + * @return string + */ + public static function get_raw_or_filtered_url( $url_type, $url_function) { if ( ! Jetpack_Constants::is_defined( 'JETPACK_SYNC_USE_RAW_URL' ) || Jetpack_Constants::get_constant( 'JETPACK_SYNC_USE_RAW_URL' ) ) { - $home_url = self::get_raw_url( 'home' ); + $url = self::get_raw_url( $url_type ); } else { - $home_url = self::normalize_www_in_url( 'home', 'home_url' ); + $url = self::normalize_www_in_url( $url_type, $url_function ); + $url = self::get_protocol_normalized_url( $url_function, $url ); } - $home_url = self::get_protocol_normalized_url( 'home_url', $home_url ); + return $url; + } + + public static function home_url() { + $url = self::get_raw_or_filtered_url( 'home', 'home_url' ); /** * Allows overriding of the home_url value that is synced back to WordPress.com. @@ -163,20 +174,11 @@ public static function home_url() { * * @param string $home_url */ - return esc_url_raw( apply_filters( 'jetpack_sync_home_url', $home_url ) ); + return esc_url_raw( apply_filters( 'jetpack_sync_home_url', $url ) ); } public static function site_url() { - if ( - ! Jetpack_Constants::is_defined( 'JETPACK_SYNC_USE_RAW_URL' ) || - Jetpack_Constants::get_constant( 'JETPACK_SYNC_USE_RAW_URL' ) - ) { - $site_url = self::get_raw_url( 'siteurl' ); - } else { - $site_url = self::normalize_www_in_url( 'siteurl', 'site_url' ); - } - - $site_url = self::get_protocol_normalized_url( 'site_url', $site_url ); + $url = self::get_raw_or_filtered_url( 'siteurl', 'site_url' ); /** * Allows overriding of the site_url value that is synced back to WordPress.com. @@ -185,7 +187,7 @@ public static function site_url() { * * @param string $site_url */ - return esc_url_raw( apply_filters( 'jetpack_sync_site_url', $site_url ) ); + return esc_url_raw( apply_filters( 'jetpack_sync_site_url', $url ) ); } public static function main_network_site_url() { @@ -215,8 +217,6 @@ public static function get_protocol_normalized_url( $callable, $new_value ) { } public static function get_raw_url( $option_name ) { - global $wpdb; - $value = null; if ( 'home' == $option_name && Jetpack_Constants::is_defined( 'WP_HOME' ) ) { $value = Jetpack_Constants::get_constant( 'WP_HOME' ); @@ -228,14 +228,6 @@ public static function get_raw_url( $option_name ) { $value = Jetpack_Options::get_raw_option( $option_name ); } - if ( is_ssl() ) { - $scheme = 'https'; - } else { - $scheme = parse_url( $value, PHP_URL_SCHEME ); - } - - $value = set_url_scheme( $value, $scheme ); - return $value; } From 0404b78afe1122b1e9c140db4a42823b28fe0a6b Mon Sep 17 00:00:00 2001 From: Eric Binnion Date: Sat, 22 Jul 2017 12:40:59 -0500 Subject: [PATCH 17/20] Sync: Update broken test after last commit to not modify protocol on raw url --- tests/php/sync/test_class.jetpack-sync-callables.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/php/sync/test_class.jetpack-sync-callables.php b/tests/php/sync/test_class.jetpack-sync-callables.php index 09276341f799..55e38362a531 100644 --- a/tests/php/sync/test_class.jetpack-sync-callables.php +++ b/tests/php/sync/test_class.jetpack-sync-callables.php @@ -519,7 +519,7 @@ function test_get_raw_url_by_constant_bypasses_filters() { Jetpack_Constants::clear_constants(); } - function test_get_raw_url_returns_with_https_if_is_ssl() { + function test_get_raw_url_returns_with_http_if_is_ssl() { $home_option = get_option( 'home' ); // Test without https first @@ -528,7 +528,7 @@ function test_get_raw_url_returns_with_https_if_is_ssl() { // Now, with https $_SERVER['HTTPS'] = 'on'; $this->assertEquals( - set_url_scheme( $home_option, 'https' ), + set_url_scheme( $home_option, 'http' ), Jetpack_Sync_Functions::get_raw_url( 'home' ) ); unset( $_SERVER['HTTPS'] ); From 15f0555ca364cc4cfc60478037f4117e4f248bc0 Mon Sep 17 00:00:00 2001 From: Eric Binnion Date: Mon, 24 Jul 2017 12:41:52 -0500 Subject: [PATCH 18/20] Sync: IDC: Switch IDC methods to conditonally use raw URL as well --- class.jetpack-xmlrpc-server.php | 5 +++-- class.jetpack.php | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/class.jetpack-xmlrpc-server.php b/class.jetpack-xmlrpc-server.php index 61f1be7c3556..54e311b8da21 100644 --- a/class.jetpack-xmlrpc-server.php +++ b/class.jetpack-xmlrpc-server.php @@ -340,9 +340,10 @@ function sync_object( $args ) { * @return array */ function validate_urls_for_idc_mitigation() { + require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-functions.php'; return array( - 'home' => get_home_url(), - 'siteurl' => get_site_url(), + 'home' => Jetpack_Sync_Functions::home_url(), + 'siteurl' => Jetpack_Sync_Functions::site_url(), ); } diff --git a/class.jetpack.php b/class.jetpack.php index 2544348a5ae1..6a7a497b2d64 100644 --- a/class.jetpack.php +++ b/class.jetpack.php @@ -5797,9 +5797,10 @@ public static function normalize_url_protocol_agnostic( $url ) { * @return array Array of the local urls, wpcom urls, and error code */ public static function get_sync_error_idc_option( $response = array() ) { + require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-functions.php'; $local_options = array( - 'home' => get_home_url(), - 'siteurl' => get_site_url(), + 'home' => Jetpack_Sync_Functions::home_url(), + 'siteurl' => Jetpack_Sync_Functions::site_url(), ); $options = array_merge( $local_options, $response ); From c448404f5d7bc72899981e8c3dbe71f781b6cabd Mon Sep 17 00:00:00 2001 From: Eric Binnion Date: Mon, 24 Jul 2017 14:52:56 -0500 Subject: [PATCH 19/20] Sync: IDC: Update docblocks to 5.2 for raw URL logic --- sync/class.jetpack-sync-functions.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sync/class.jetpack-sync-functions.php b/sync/class.jetpack-sync-functions.php index 07790649a910..b27fb2a5f727 100644 --- a/sync/class.jetpack-sync-functions.php +++ b/sync/class.jetpack-sync-functions.php @@ -170,7 +170,7 @@ public static function home_url() { /** * Allows overriding of the home_url value that is synced back to WordPress.com. * - * @since 4.6 + * @since 5.2 * * @param string $home_url */ @@ -183,7 +183,7 @@ public static function site_url() { /** * Allows overriding of the site_url value that is synced back to WordPress.com. * - * @since 4.6 + * @since 5.2 * * @param string $site_url */ From 1e4a204ac4a91d78917ebbb039db8111fbc4cc2a Mon Sep 17 00:00:00 2001 From: Eric Binnion Date: Mon, 24 Jul 2017 15:21:28 -0500 Subject: [PATCH 20/20] Sync: IDC: Simplify some logic around getting URLs --- sync/class.jetpack-sync-functions.php | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/sync/class.jetpack-sync-functions.php b/sync/class.jetpack-sync-functions.php index b27fb2a5f727..f8d76def4e75 100644 --- a/sync/class.jetpack-sync-functions.php +++ b/sync/class.jetpack-sync-functions.php @@ -150,13 +150,16 @@ public static function file_system_write_access() { * * @return string */ - public static function get_raw_or_filtered_url( $url_type, $url_function) { + public static function get_raw_or_filtered_url( $url_type ) { if ( ! Jetpack_Constants::is_defined( 'JETPACK_SYNC_USE_RAW_URL' ) || Jetpack_Constants::get_constant( 'JETPACK_SYNC_USE_RAW_URL' ) ) { $url = self::get_raw_url( $url_type ); } else { + $url_function = ( 'home' == $url_type ) + ? 'home_url' + : 'site_url'; $url = self::normalize_www_in_url( $url_type, $url_function ); $url = self::get_protocol_normalized_url( $url_function, $url ); } @@ -165,7 +168,7 @@ public static function get_raw_or_filtered_url( $url_type, $url_function) { } public static function home_url() { - $url = self::get_raw_or_filtered_url( 'home', 'home_url' ); + $url = self::get_raw_or_filtered_url( 'home' ); /** * Allows overriding of the home_url value that is synced back to WordPress.com. @@ -178,7 +181,7 @@ public static function home_url() { } public static function site_url() { - $url = self::get_raw_or_filtered_url( 'siteurl', 'site_url' ); + $url = self::get_raw_or_filtered_url( 'siteurl' ); /** * Allows overriding of the site_url value that is synced back to WordPress.com. @@ -218,10 +221,12 @@ public static function get_protocol_normalized_url( $callable, $new_value ) { public static function get_raw_url( $option_name ) { $value = null; - if ( 'home' == $option_name && Jetpack_Constants::is_defined( 'WP_HOME' ) ) { - $value = Jetpack_Constants::get_constant( 'WP_HOME' ); - } else if ( 'siteurl' == $option_name && Jetpack_Constants::is_defined( 'WP_SITEURL' ) ) { - $value = Jetpack_Constants::get_constant( 'WP_SITEURL' ); + $constant = ( 'home' == $option_name ) + ? 'WP_HOME' + : 'WP_SITEURL'; + + if ( Jetpack_Constants::is_defined( $constant ) ) { + $value = Jetpack_Constants::get_constant( $constant ); } else { // Let's get the option from the database so that we can bypass filters. This will help // ensure that we get more uniform values.