From c55cf716da5145b758d5b7a228c2f441c1a35c7f Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Thu, 30 Aug 2012 13:33:00 +0000 Subject: [PATCH] Use set_url_scheme(). Props johnbillion, MarcusPope. see #19037 #20759 git-svn-id: http://core.svn.wordpress.org/trunk@21664 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/class-wp-list-table.php | 4 ++-- wp-admin/includes/meta-boxes.php | 4 +--- wp-admin/includes/plugin.php | 4 ++-- wp-includes/class-wp-editor.php | 2 +- wp-includes/class-wp-xmlrpc-server.php | 3 +-- wp-includes/feed.php | 7 +------ wp-includes/functions.php | 13 ++++++------- wp-includes/link-template.php | 8 +++----- wp-includes/ms-functions.php | 20 ++++++++++---------- wp-includes/nav-menu-template.php | 2 +- wp-includes/pluggable.php | 19 +++++++------------ wp-includes/theme.php | 7 +------ wp-login.php | 12 ++++++------ wp-signup.php | 6 +----- 14 files changed, 43 insertions(+), 68 deletions(-) diff --git a/wp-admin/includes/class-wp-list-table.php b/wp-admin/includes/class-wp-list-table.php index d05b7b29ff..030648fcfa 100644 --- a/wp-admin/includes/class-wp-list-table.php +++ b/wp-admin/includes/class-wp-list-table.php @@ -490,7 +490,7 @@ class WP_List_Table { $current = $this->get_pagenum(); - $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; + $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); $current_url = remove_query_arg( array( 'hotkeys_highlight_last', 'hotkeys_highlight_first' ), $current_url ); @@ -651,7 +651,7 @@ class WP_List_Table { list( $columns, $hidden, $sortable ) = $this->get_column_info(); - $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; + $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); $current_url = remove_query_arg( 'paged', $current_url ); if ( isset( $_GET['orderby'] ) ) diff --git a/wp-admin/includes/meta-boxes.php b/wp-admin/includes/meta-boxes.php index d5b0fbdf55..5c29931815 100644 --- a/wp-admin/includes/meta-boxes.php +++ b/wp-admin/includes/meta-boxes.php @@ -41,9 +41,7 @@ if ( 'publish' == $post->post_status ) { $preview_link = esc_url( get_permalink( $post->ID ) ); $preview_button = __( 'Preview Changes' ); } else { - $preview_link = get_permalink( $post->ID ); - if ( is_ssl() ) - $preview_link = str_replace( 'http://', 'https://', $preview_link ); + $preview_link = set_url_scheme( get_permalink( $post->ID ) ); $preview_link = esc_url( apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', $preview_link ) ) ); $preview_button = __( 'Preview' ); } diff --git a/wp-admin/includes/plugin.php b/wp-admin/includes/plugin.php index 941f545d54..a573599a5d 100644 --- a/wp-admin/includes/plugin.php +++ b/wp-admin/includes/plugin.php @@ -903,8 +903,8 @@ function add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $func if ( empty($icon_url) ) $icon_url = esc_url( admin_url( 'images/generic.png' ) ); - elseif ( is_ssl() && 0 === strpos($icon_url, 'http://') ) - $icon_url = 'https://' . substr($icon_url, 7); + else + $icon_url = set_url_scheme( $icon_url ); $new_menu = array( $menu_title, $capability, $menu_slug, $page_title, 'menu-top ' . $hookname, $hookname, $icon_url ); diff --git a/wp-includes/class-wp-editor.php b/wp-includes/class-wp-editor.php index e36b3fe583..f2f5689f86 100644 --- a/wp-includes/class-wp-editor.php +++ b/wp-includes/class-wp-editor.php @@ -211,7 +211,7 @@ final class _WP_Editors { foreach ( $mce_external_plugins as $name => $url ) { - if ( is_ssl() ) $url = str_replace('http://', 'https://', $url); + $url = set_url_scheme( $url ); $plugins[] = '-' . $name; diff --git a/wp-includes/class-wp-xmlrpc-server.php b/wp-includes/class-wp-xmlrpc-server.php index a5273f9f72..300f5f5ca2 100644 --- a/wp-includes/class-wp-xmlrpc-server.php +++ b/wp-includes/class-wp-xmlrpc-server.php @@ -3221,9 +3221,8 @@ class wp_xmlrpc_server extends IXR_Server { global $current_blog; $domain = $current_blog->domain; $path = $current_blog->path . 'xmlrpc.php'; - $protocol = is_ssl() ? 'https' : 'http'; - $rpc = new IXR_Client("$protocol://{$domain}{$path}"); + $rpc = new IXR_Client( set_url_scheme( "http://{$domain}{$path}" ) ); $rpc->query('wp.getUsersBlogs', $args[1], $args[2]); $blogs = $rpc->getResponse(); diff --git a/wp-includes/feed.php b/wp-includes/feed.php index e5b96ab122..899088ffdf 100644 --- a/wp-includes/feed.php +++ b/wp-includes/feed.php @@ -488,12 +488,7 @@ function prep_atom_text_construct($data) { */ function self_link() { $host = @parse_url(home_url()); - $host = $host['host']; - echo esc_url( - ( is_ssl() ? 'https' : 'http' ) . '://' - . $host - . stripslashes($_SERVER['REQUEST_URI']) - ); + echo esc_url( set_url_scheme( 'http://' . $host['host'] . stripslashes($_SERVER['REQUEST_URI']) ) ); } /** diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 242b0553bb..b6e9b7e0fc 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -2585,8 +2585,8 @@ function absint( $maybeint ) { */ function url_is_accessable_via_ssl($url) { - if (in_array('curl', get_loaded_extensions())) { - $ssl = preg_replace( '/^http:\/\//', 'https://', $url ); + if ( in_array( 'curl', get_loaded_extensions() ) ) { + $ssl = set_url_scheme( $url, 'https' ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $ssl); @@ -2933,12 +2933,11 @@ function force_ssl_admin( $force = null ) { * @return string */ function wp_guess_url() { - if ( defined('WP_SITEURL') && '' != WP_SITEURL ) { + if ( defined('WP_SITEURL') && '' != WP_SITEURL ) $url = WP_SITEURL; - } else { - $schema = is_ssl() ? 'https://' : 'http://'; - $url = preg_replace('#/(wp-admin/.*|wp-login.php)#i', '', $schema . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); - } + else + $url = set_url_scheme( preg_replace( '#/(wp-admin/.*|wp-login.php)#i', '', 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ) ); + return rtrim($url, '/'); } diff --git a/wp-includes/link-template.php b/wp-includes/link-template.php index 64fbd932a6..8d32707040 100644 --- a/wp-includes/link-template.php +++ b/wp-includes/link-template.php @@ -2049,9 +2049,7 @@ function includes_url($path = '') { * @return string Content url link with optional path appended. */ function content_url($path = '') { - $url = WP_CONTENT_URL; - if ( 0 === strpos($url, 'http') && is_ssl() ) - $url = str_replace( 'http://', 'https://', $url ); + $url = set_url_scheme( WP_CONTENT_URL ); if ( !empty($path) && is_string($path) && strpos($path, '..') === false ) $url .= '/' . ltrim($path, '/'); @@ -2083,8 +2081,8 @@ function plugins_url($path = '', $plugin = '') { else $url = WP_PLUGIN_URL; - if ( 0 === strpos($url, 'http') && is_ssl() ) - $url = str_replace( 'http://', 'https://', $url ); + + $url = set_url_scheme( $url ); if ( !empty($plugin) && is_string($plugin) ) { $folder = dirname(plugin_basename($plugin)); diff --git a/wp-includes/ms-functions.php b/wp-includes/ms-functions.php index 918465c541..dd70f9f9d3 100644 --- a/wp-includes/ms-functions.php +++ b/wp-includes/ms-functions.php @@ -1875,21 +1875,21 @@ function force_ssl_content( $force = '' ) { } /** - * Formats an String URL to use HTTPS if HTTP is found. + * Formats a URL to use https. + * * Useful as a filter. * * @since 2.8.5 - **/ + * + * @param string URL + * @return string URL with https as the scheme + */ function filter_SSL( $url ) { - if ( !is_string( $url ) ) - return get_bloginfo( 'url' ); //return home blog url with proper scheme + if ( ! is_string( $url ) ) + return get_bloginfo( 'url' ); // Return home blog url with proper scheme - $arrURL = parse_url( $url ); - - if ( force_ssl_content() && is_ssl() ) { - if ( 'http' === $arrURL['scheme'] ) - $url = str_replace( $arrURL['scheme'], 'https', $url ); - } + if ( force_ssl_content() && is_ssl() ) + $url = set_url_scheme( $url, 'https' ); return $url; } diff --git a/wp-includes/nav-menu-template.php b/wp-includes/nav-menu-template.php index 217308787c..115889ae8a 100644 --- a/wp-includes/nav-menu-template.php +++ b/wp-includes/nav-menu-template.php @@ -358,7 +358,7 @@ function _wp_menu_item_classes_by_context( &$menu_items ) { // if the menu item corresponds to the currently-requested URL } elseif ( 'custom' == $menu_item->object ) { $_root_relative_current = untrailingslashit( $_SERVER['REQUEST_URI'] ); - $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_root_relative_current; + $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_root_relative_current ); $raw_item_url = strpos( $menu_item->url, '#' ) ? substr( $menu_item->url, 0, strpos( $menu_item->url, '#' ) ) : $menu_item->url; $item_url = untrailingslashit( $raw_item_url ); $_indexless_current = untrailingslashit( preg_replace( '/index.php$/', '', $current_url ) ); diff --git a/wp-includes/pluggable.php b/wp-includes/pluggable.php index 2fc6bc9a1b..403a452901 100644 --- a/wp-includes/pluggable.php +++ b/wp-includes/pluggable.php @@ -748,11 +748,11 @@ function auth_redirect() { // If https is required and request is http, redirect if ( $secure && !is_ssl() && false !== strpos($_SERVER['REQUEST_URI'], 'wp-admin') ) { - if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) { - wp_redirect(preg_replace('|^http://|', 'https://', $_SERVER['REQUEST_URI'])); + if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) { + wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) ); exit(); } else { - wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); + wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); exit(); } } @@ -767,11 +767,11 @@ function auth_redirect() { // If the user wants ssl but the session is not ssl, redirect. if ( !$secure && get_user_option('use_ssl', $user_id) && false !== strpos($_SERVER['REQUEST_URI'], 'wp-admin') ) { - if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) { - wp_redirect(preg_replace('|^http://|', 'https://', $_SERVER['REQUEST_URI'])); + if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) { + wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) ); exit(); } else { - wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); + wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); exit(); } } @@ -782,12 +782,7 @@ function auth_redirect() { // The cookie is no good so force login nocache_headers(); - if ( is_ssl() ) - $proto = 'https://'; - else - $proto = 'http://'; - - $redirect = ( strpos($_SERVER['REQUEST_URI'], '/options.php') && wp_get_referer() ) ? wp_get_referer() : $proto . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; + $redirect = ( strpos( $_SERVER['REQUEST_URI'], '/options.php' ) && wp_get_referer() ) ? wp_get_referer() : set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); $login_url = wp_login_url($redirect, true); diff --git a/wp-includes/theme.php b/wp-includes/theme.php index b4e186de42..b7e0398e40 100644 --- a/wp-includes/theme.php +++ b/wp-includes/theme.php @@ -891,12 +891,7 @@ function get_header_image() { if ( is_random_header_image() ) $url = get_random_header_image(); - if ( is_ssl() ) - $url = str_replace( 'http://', 'https://', $url ); - else - $url = str_replace( 'https://', 'http://', $url ); - - return esc_url_raw( $url ); + return esc_url_raw( set_url_scheme( $url ) ); } /** diff --git a/wp-login.php b/wp-login.php index a442679f4d..f67122ac9e 100644 --- a/wp-login.php +++ b/wp-login.php @@ -12,12 +12,12 @@ require( dirname(__FILE__) . '/wp-load.php' ); // Redirect to https login if forced to use SSL -if ( force_ssl_admin() && !is_ssl() ) { +if ( force_ssl_admin() && ! is_ssl() ) { if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) { - wp_redirect(preg_replace('|^http://|', 'https://', $_SERVER['REQUEST_URI'])); + wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) ); exit(); } else { - wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); + wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); exit(); } } @@ -365,9 +365,9 @@ if ( defined( 'RELOCATE' ) && RELOCATE ) { // Move flag is set if ( isset( $_SERVER['PATH_INFO'] ) && ($_SERVER['PATH_INFO'] != $_SERVER['PHP_SELF']) ) $_SERVER['PHP_SELF'] = str_replace( $_SERVER['PATH_INFO'], '', $_SERVER['PHP_SELF'] ); - $schema = is_ssl() ? 'https://' : 'http://'; - if ( dirname($schema . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']) != get_option('siteurl') ) - update_option('siteurl', dirname($schema . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']) ); + $url = dirname( set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] ) ); + if ( $url != get_option( 'siteurl' ) ) + update_option( 'siteurl', $url ); } //Set a cookie now to see if they are supported by the browser. diff --git a/wp-signup.php b/wp-signup.php index 2cd93c8210..0563ef1731 100644 --- a/wp-signup.php +++ b/wp-signup.php @@ -390,11 +390,7 @@ $current_user = wp_get_current_user(); if ( $active_signup == 'none' ) { _e( 'Registration has been disabled.' ); } elseif ( $active_signup == 'blog' && !is_user_logged_in() ) { - if ( is_ssl() ) - $proto = 'https://'; - else - $proto = 'http://'; - $login_url = site_url( 'wp-login.php?redirect_to=' . urlencode($proto . $_SERVER['HTTP_HOST'] . '/wp-signup.php' )); + $login_url = site_url( 'wp-login.php?redirect_to=' . urlencode( set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . '/wp-signup.php' ) ) ); echo sprintf( __( 'You must first log in, and then you can create a new site.' ), $login_url ); } else { $stage = isset( $_POST['stage'] ) ? $_POST['stage'] : 'default';