From ee259e073ae7253090568c6e5d21a9c118de5206 Mon Sep 17 00:00:00 2001 From: Jeremy Felt Date: Thu, 13 Oct 2016 22:27:15 +0000 Subject: [PATCH] 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 --- src/wp-includes/general-template.php | 20 ++++--- tests/phpunit/tests/general/template.php | 66 ++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/general-template.php b/src/wp-includes/general-template.php index 75a4bdf5bd..23b2f1c5ab 100644 --- a/src/wp-includes/general-template.php +++ b/src/wp-includes/general-template.php @@ -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(); } diff --git a/tests/phpunit/tests/general/template.php b/tests/phpunit/tests/general/template.php index 275b61f759..d650d1ddf2 100644 --- a/tests/phpunit/tests/general/template.php +++ b/tests/phpunit/tests/general/template.php @@ -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 ); + } }