From 3eb0e3a4bb102df6cff6cc67678218fdfa32bc80 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Fri, 30 Oct 2015 02:01:32 +0000 Subject: [PATCH] Ensure that the scheme used in the URL returned by `get_blogaddress_by_id()` always reflects the blog's URL, instead of using `http`. Props thomaswm Fixes #14867 git-svn-id: https://develop.svn.wordpress.org/trunk@35446 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/ms-blogs.php | 19 +++++++++++---- tests/phpunit/tests/multisite/site.php | 33 ++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/ms-blogs.php b/src/wp-includes/ms-blogs.php index 5418ce0be0..84f63ed71d 100644 --- a/src/wp-includes/ms-blogs.php +++ b/src/wp-includes/ms-blogs.php @@ -38,8 +38,16 @@ function wpmu_update_blogs_date() { * @return string Full URL of the blog if found. Empty string if not. */ function get_blogaddress_by_id( $blog_id ) { - $bloginfo = get_blog_details( (int) $blog_id, false ); // only get bare details! - return ( $bloginfo ) ? esc_url( 'http://' . $bloginfo->domain . $bloginfo->path ) : ''; + $bloginfo = get_blog_details( (int) $blog_id ); + + if ( empty( $bloginfo ) ) { + return ''; + } + + $scheme = parse_url( $bloginfo->home, PHP_URL_SCHEME ); + $scheme = empty( $scheme ) ? 'http' : $scheme; + + return esc_url( $scheme . '://' . $bloginfo->domain . $bloginfo->path ); } /** @@ -216,9 +224,10 @@ function get_blog_details( $fields = null, $get_all = true ) { } switch_to_blog( $blog_id ); - $details->blogname = get_option( 'blogname' ); - $details->siteurl = get_option( 'siteurl' ); - $details->post_count = get_option( 'post_count' ); + $details->blogname = get_option( 'blogname' ); + $details->siteurl = get_option( 'siteurl' ); + $details->post_count = get_option( 'post_count' ); + $details->home = get_option( 'home' ); restore_current_blog(); /** diff --git a/tests/phpunit/tests/multisite/site.php b/tests/phpunit/tests/multisite/site.php index 8afdbe8fb6..d00147b051 100644 --- a/tests/phpunit/tests/multisite/site.php +++ b/tests/phpunit/tests/multisite/site.php @@ -1001,6 +1001,39 @@ class Tests_Multisite_Site extends WP_UnitTestCase { $this->assertEquals( '', $blogaddress ); } + /** + * @ticket 14867 + */ + function test_get_blogaddress_by_id_scheme_reflects_blog_scheme() { + $blog = self::factory()->blog->create(); + + $this->assertSame( 'http', parse_url( get_blogaddress_by_id( $blog ), PHP_URL_SCHEME ) ); + + update_blog_option( $blog, 'home', set_url_scheme( get_blog_option( $blog, 'home' ), 'https' ) ); + + $this->assertSame( 'https', parse_url( get_blogaddress_by_id( $blog ), PHP_URL_SCHEME ) ); + } + + /** + * @ticket 14867 + */ + function test_get_blogaddress_by_id_scheme_is_unaffected_by_request() { + $blog = self::factory()->blog->create(); + + $this->assertFalse( is_ssl() ); + $this->assertSame( 'http', parse_url( get_blogaddress_by_id( $blog ), PHP_URL_SCHEME ) ); + + $_SERVER['HTTPS'] = 'on'; + + $is_ssl = is_ssl(); + $address = parse_url( get_blogaddress_by_id( $blog ), PHP_URL_SCHEME ); + + unset( $_SERVER['HTTPS'] ); + + $this->assertTrue( $is_ssl ); + $this->assertSame( 'http', $address ); + } + /** * @ticket 33620 * @dataProvider data_new_blog_url_schemes