Multisite: Maintain switched state in site icon/logo functions.

Adjusts `get_custom_logo()`, `get_site_icon_url()`, and `has_custom_logo()` so that when called in a switched state, the original switched stack is not adjusted.

Props achbed, flixos90.
Fixes #38253.


git-svn-id: https://develop.svn.wordpress.org/trunk@38786 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jeremy Felt 2016-10-13 22:27:15 +00:00
parent 595c76de1a
commit ee259e073a
2 changed files with 80 additions and 6 deletions

View File

@ -783,8 +783,11 @@ function get_bloginfo( $show = '', $filter = 'raw' ) {
* @return string Site Icon URL.
*/
function get_site_icon_url( $size = 512, $url = '', $blog_id = 0 ) {
if ( is_multisite() && (int) $blog_id !== get_current_blog_id() ) {
$switched_blog = false;
if ( is_multisite() && ! empty( $blog_id ) && (int) $blog_id !== get_current_blog_id() ) {
switch_to_blog( $blog_id );
$switched_blog = true;
}
$site_icon_id = get_option( 'site_icon' );
@ -798,7 +801,7 @@ function get_site_icon_url( $size = 512, $url = '', $blog_id = 0 ) {
$url = wp_get_attachment_image_url( $site_icon_id, $size_data );
}
if ( is_multisite() && ms_is_switched() ) {
if ( $switched_blog ) {
restore_current_blog();
}
@ -848,13 +851,16 @@ function has_site_icon( $blog_id = 0 ) {
* @return bool Whether the site has a custom logo or not.
*/
function has_custom_logo( $blog_id = 0 ) {
if ( is_multisite() && (int) $blog_id !== get_current_blog_id() ) {
$switched_blog = false;
if ( is_multisite() && ! empty( $blog_id ) && (int) $blog_id !== get_current_blog_id() ) {
switch_to_blog( $blog_id );
$switched_blog = true;
}
$custom_logo_id = get_theme_mod( 'custom_logo' );
if ( is_multisite() && ms_is_switched() ) {
if ( $switched_blog ) {
restore_current_blog();
}
@ -871,9 +877,11 @@ function has_custom_logo( $blog_id = 0 ) {
*/
function get_custom_logo( $blog_id = 0 ) {
$html = '';
$switched_blog = false;
if ( is_multisite() && (int) $blog_id !== get_current_blog_id() ) {
if ( is_multisite() && ! empty( $blog_id ) && (int) $blog_id !== get_current_blog_id() ) {
switch_to_blog( $blog_id );
$switched_blog = true;
}
$custom_logo_id = get_theme_mod( 'custom_logo' );
@ -896,7 +904,7 @@ function get_custom_logo( $blog_id = 0 ) {
);
}
if ( is_multisite() && ms_is_switched() ) {
if ( $switched_blog ) {
restore_current_blog();
}

View File

@ -488,4 +488,70 @@ class Tests_General_Template extends WP_UnitTestCase {
$actual = get_the_modified_time( $d, $post_id );
$this->assertEquals( $expected, $actual );
}
/**
* @ticket 38253
*/
function test_get_site_icon_url_preserves_switched_state() {
if ( ! is_multisite() ) {
$this->markTestSkipped( 'This test requires multisite.' );
}
$blog_id = $this->factory->blog->create();
switch_to_blog( $blog_id );
$expected = $GLOBALS['_wp_switched_stack'];
get_site_icon_url( 512, '', $blog_id );
$result = $GLOBALS['_wp_switched_stack'];
restore_current_blog();
$this->assertSame( $expected, $result );
}
/**
* @ticket 38253
*/
function test_has_custom_logo_preserves_switched_state() {
if ( ! is_multisite() ) {
$this->markTestSkipped( 'This test requires multisite.' );
}
$blog_id = $this->factory->blog->create();
switch_to_blog( $blog_id );
$expected = $GLOBALS['_wp_switched_stack'];
has_custom_logo( $blog_id );
$result = $GLOBALS['_wp_switched_stack'];
restore_current_blog();
$this->assertSame( $expected, $result );
}
/**
* @ticket 38253
*/
function test_get_custom_logo_preserves_switched_state() {
if ( ! is_multisite() ) {
$this->markTestSkipped( 'This test requires multisite.' );
}
$blog_id = $this->factory->blog->create();
switch_to_blog( $blog_id );
$expected = $GLOBALS['_wp_switched_stack'];
get_custom_logo( $blog_id );
$result = $GLOBALS['_wp_switched_stack'];
restore_current_blog();
$this->assertSame( $expected, $result );
}
}