diff options
Diffstat (limited to 'plugins/jetpack/sync/class.jetpack-sync-functions.php')
-rw-r--r-- | plugins/jetpack/sync/class.jetpack-sync-functions.php | 77 |
1 files changed, 44 insertions, 33 deletions
diff --git a/plugins/jetpack/sync/class.jetpack-sync-functions.php b/plugins/jetpack/sync/class.jetpack-sync-functions.php index 0037dd35..1f3bddd7 100644 --- a/plugins/jetpack/sync/class.jetpack-sync-functions.php +++ b/plugins/jetpack/sync/class.jetpack-sync-functions.php @@ -5,6 +5,8 @@ */ class Jetpack_Sync_Functions { + const HTTPS_CHECK_OPTION_PREFIX = 'jetpack_sync_https_history_'; + const HTTPS_CHECK_HISTORY = 5; public static function get_modules() { require_once( JETPACK__PLUGIN_DIR . 'class.jetpack-admin.php' ); @@ -97,54 +99,63 @@ class Jetpack_Sync_Functions { } public static function home_url() { - return self::preserve_scheme( 'home', 'home_url', true ); + return self::get_protocol_normalized_url( + 'home_url', + self::normalize_www_in_url( 'home', 'home_url' ) + ); } public static function site_url() { - return self::preserve_scheme( 'siteurl', 'site_url', true ); + return self::get_protocol_normalized_url( + 'site_url', + self::normalize_www_in_url( 'siteurl', 'site_url' ) + ); } public static function main_network_site_url() { - return self::preserve_scheme( 'siteurl', 'network_site_url', false ); - } - - public static function preserve_scheme( $option, $url_function, $normalize_www = false ) { - $previous_https_value = isset( $_SERVER['HTTPS'] ) ? $_SERVER['HTTPS'] : null; - $_SERVER['HTTPS'] = 'off'; - $url = call_user_func( $url_function ); - $option_url = get_option( $option ); - if ( $previous_https_value ) { - $_SERVER['HTTPS'] = $previous_https_value; - } else { - unset( $_SERVER['HTTPS'] ); + return self::get_protocol_normalized_url( 'main_network_site_url', network_site_url() ); + } + + public static function get_protocol_normalized_url( $callable, $new_value ) { + $option_key = self::HTTPS_CHECK_OPTION_PREFIX . $callable; + + $parsed_url = wp_parse_url( $new_value ); + if ( ! $parsed_url ) { + return $new_value; } - if ( $option_url === $url ) { + $scheme = $parsed_url['scheme']; + $scheme_history = get_option( $option_key, array() ); + $scheme_history[] = $scheme; + + // Limit length to self::HTTPS_CHECK_HISTORY + $scheme_history = array_slice( $scheme_history, ( self::HTTPS_CHECK_HISTORY * -1 ) ); + + update_option( $option_key, $scheme_history ); + + $forced_scheme = in_array( 'https', $scheme_history ) ? 'https' : 'http'; + + return set_url_scheme( $new_value, $forced_scheme ); + } + + 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 ) ); + + if ( ! $option_url || ! $url ) { return $url; } - // turn them both into parsed format - $option_url = parse_url( $option_url ); - $url = parse_url( $url ); - - if ( $normalize_www ) { - if ( $url['host'] === "www.{$option_url[ 'host' ]}" ) { - // remove www if not present in option URL - $url['host'] = $option_url['host']; - } - if ( $option_url['host'] === "www.{$url[ 'host' ]}" ) { - // add www if present in option URL - $url['host'] = $option_url['host']; - } + if ( $url[ 'host' ] === "www.{$option_url[ 'host' ]}" ) { + // remove www if not present in option URL + $url[ 'host' ] = $option_url[ 'host' ]; } - - if ( $url['host'] === $option_url['host'] ) { - $url['scheme'] = $option_url['scheme']; - // return set_url_scheme( $current_url, $option_url['scheme'] ); + if ( $option_url[ 'host' ] === "www.{$url[ 'host' ]}" ) { + // add www if present in option URL + $url[ 'host' ] = $option_url[ 'host' ]; } $normalized_url = "{$url['scheme']}://{$url['host']}"; - if ( isset( $url['path'] ) ) { $normalized_url .= "{$url['path']}"; } |