Skip to content

Commit

Permalink
Introduce 'relative' scheme to return only the paths for home_url, si…
Browse files Browse the repository at this point in the history
…te, admin, network_, and get_ variants. props SergeyBiryukov, see #18952.

git-svn-id: http://svn.automattic.com/wordpress/trunk@19870 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information
nacin committed Feb 8, 2012
1 parent 4c3c859 commit eef151f
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions wp-includes/link-template.php
Expand Up @@ -1834,7 +1834,7 @@ function get_shortcut_link() {
* @uses get_home_url()
*
* @param string $path (optional) Path relative to the home url.
* @param string $scheme (optional) Scheme to give the home url context. Currently 'http', 'https'.
* @param string $scheme (optional) Scheme to give the home url context. Currently 'http', 'https', or 'relative'.
* @return string Home url link with optional path appended.
*/
function home_url( $path = '', $scheme = null ) {
Expand All @@ -1853,21 +1853,23 @@ function home_url( $path = '', $scheme = null ) {
*
* @param int $blog_id (optional) Blog ID. Defaults to current blog.
* @param string $path (optional) Path relative to the home url.
* @param string $scheme (optional) Scheme to give the home url context. Currently 'http', 'https'.
* @param string $scheme (optional) Scheme to give the home url context. Currently 'http', 'https', or 'relative'.
* @return string Home url link with optional path appended.
*/
function get_home_url( $blog_id = null, $path = '', $scheme = null ) {
$orig_scheme = $scheme;

if ( !in_array( $scheme, array( 'http', 'https' ) ) )
if ( !in_array( $scheme, array( 'http', 'https', 'relative' ) ) )
$scheme = is_ssl() && !is_admin() ? 'https' : 'http';

if ( empty( $blog_id ) || !is_multisite() )
$url = get_option( 'home' );
else
$url = get_blog_option( $blog_id, 'home' );

if ( 'http' != $scheme )
if ( 'relative' == $scheme )
$url = preg_replace( '#^.+://[^/]*#', '', $url );
elseif ( 'http' != $scheme )
$url = str_replace( 'http://', "$scheme://", $url );

if ( !empty( $path ) && is_string( $path ) && strpos( $path, '..' ) === false )
Expand All @@ -1889,7 +1891,7 @@ function get_home_url( $blog_id = null, $path = '', $scheme = null ) {
* @uses get_site_url()
*
* @param string $path Optional. Path relative to the site url.
* @param string $scheme Optional. Scheme to give the site url context. Currently 'http', 'https', 'login', 'login_post', or 'admin'.
* @param string $scheme Optional. Scheme to give the site url context. Currently 'http', 'https', 'login', 'login_post', 'admin', or 'relative'.
* @return string Site url link with optional path appended.
*/
function site_url( $path = '', $scheme = null ) {
Expand All @@ -1908,13 +1910,13 @@ function site_url( $path = '', $scheme = null ) {
*
* @param int $blog_id (optional) Blog ID. Defaults to current blog.
* @param string $path Optional. Path relative to the site url.
* @param string $scheme Optional. Scheme to give the site url context. Currently 'http', 'https', 'login', 'login_post', or 'admin'.
* @param string $scheme Optional. Scheme to give the site url context. Currently 'http', 'https', 'login', 'login_post', 'admin', or 'relative'.
* @return string Site url link with optional path appended.
*/
function get_site_url( $blog_id = null, $path = '', $scheme = null ) {
// should the list of allowed schemes be maintained elsewhere?
$orig_scheme = $scheme;
if ( !in_array( $scheme, array( 'http', 'https' ) ) ) {
if ( !in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) {
if ( ( 'login_post' == $scheme || 'rpc' == $scheme ) && ( force_ssl_login() || force_ssl_admin() ) )
$scheme = 'https';
elseif ( ( 'login' == $scheme ) && force_ssl_admin() )
Expand All @@ -1930,7 +1932,9 @@ function get_site_url( $blog_id = null, $path = '', $scheme = null ) {
else
$url = get_blog_option( $blog_id, 'siteurl' );

if ( 'http' != $scheme )
if ( 'relative' == $scheme )
$url = preg_replace( '#^.+://[^/]*#', '', $url );
elseif ( 'http' != $scheme )
$url = str_replace( 'http://', "{$scheme}://", $url );

if ( !empty( $path ) && is_string( $path ) && strpos( $path, '..' ) === false )
Expand Down Expand Up @@ -2061,7 +2065,7 @@ function plugins_url($path = '', $plugin = '') {
* @since 3.0.0
*
* @param string $path Optional. Path relative to the site url.
* @param string $scheme Optional. Scheme to give the site url context. Currently 'http', 'https', 'login', 'login_post', or 'admin'.
* @param string $scheme Optional. Scheme to give the site url context. Currently 'http', 'https', 'login', 'login_post', 'admin', or 'relative'.
* @return string Site url link with optional path appended.
*/
function network_site_url( $path = '', $scheme = null ) {
Expand All @@ -2071,7 +2075,7 @@ function network_site_url( $path = '', $scheme = null ) {
return site_url($path, $scheme);

$orig_scheme = $scheme;
if ( !in_array($scheme, array('http', 'https')) ) {
if ( !in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) {
if ( ( 'login_post' == $scheme || 'rpc' == $scheme ) && ( force_ssl_login() || force_ssl_admin() ) )
$scheme = 'https';
elseif ( ('login' == $scheme) && ( force_ssl_admin() ) )
Expand All @@ -2082,7 +2086,10 @@ function network_site_url( $path = '', $scheme = null ) {
$scheme = ( is_ssl() ? 'https' : 'http' );
}

$url = $scheme . '://' . $current_site->domain . $current_site->path;
if ( 'relative' == $scheme )
$url = $current_site->path;
else
$url = $scheme . '://' . $current_site->domain . $current_site->path;

if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
$url .= ltrim($path, '/');
Expand All @@ -2101,7 +2108,7 @@ function network_site_url( $path = '', $scheme = null ) {
* @since 3.0.0
*
* @param string $path (optional) Path relative to the home url.
* @param string $scheme (optional) Scheme to give the home url context. Currently 'http', 'https'.
* @param string $scheme (optional) Scheme to give the home url context. Currently 'http', 'https', or 'relative'.
* @return string Home url link with optional path appended.
*/
function network_home_url( $path = '', $scheme = null ) {
Expand All @@ -2112,10 +2119,13 @@ function network_home_url( $path = '', $scheme = null ) {

$orig_scheme = $scheme;

if ( !in_array($scheme, array('http', 'https')) )
if ( !in_array( $scheme, array( 'http', 'https', 'relative' ) ) )
$scheme = is_ssl() && !is_admin() ? 'https' : 'http';

$url = $scheme . '://' . $current_site->domain . $current_site->path;
if ( 'relative' == $scheme )
$url = $current_site->path;
else
$url = $scheme . '://' . $current_site->domain . $current_site->path;

if ( !empty( $path ) && is_string( $path ) && strpos( $path, '..' ) === false )
$url .= ltrim( $path, '/' );
Expand Down

0 comments on commit eef151f

Please sign in to comment.