Site Icon: Wrap site icon retrieval with `switch_to_blog()` as needed.

When the site icon for another site is requested, retrieving its ID via `get_blog_option()` is not enough. `switch_to_blog()` is used to set proper context when required.

Adds multsite tests for `has_site_icon()`.

Props imath.
Fixes #34312.


git-svn-id: https://develop.svn.wordpress.org/trunk@35572 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jeremy Felt 2015-11-08 02:03:34 +00:00
parent 99c7b9b881
commit 9d48f6c7c3
2 changed files with 39 additions and 4 deletions

View File

@ -758,12 +758,12 @@ function get_bloginfo( $show = '', $filter = 'raw' ) {
* @return string Site Icon URL.
*/
function get_site_icon_url( $size = 512, $url = '', $blog_id = 0 ) {
if ( $blog_id && is_multisite() ) {
$site_icon_id = get_blog_option( $blog_id, 'site_icon' );
} else {
$site_icon_id = get_option( 'site_icon' );
if ( is_multisite() && (int) $blog_id !== get_current_blog_id() ) {
switch_to_blog( $blog_id );
}
$site_icon_id = get_option( 'site_icon' );
if ( $site_icon_id ) {
if ( $size >= 512 ) {
$size_data = 'full';
@ -773,6 +773,10 @@ 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() ) {
restore_current_blog();
}
/**
* Filter the site icon URL.
*

View File

@ -57,6 +57,37 @@ class Tests_General_Template extends WP_UnitTestCase {
$this->assertFalse( has_site_icon() );
}
/**
* @group site_icon
* @group multisite
*/
function test_has_site_icon_returns_true_when_called_for_other_site_with_site_icon_set() {
if ( ! is_multisite() ) {
$this->markTestSkipped( 'This test requires multisite.' );
}
$blog_id = $this->factory->blog->create();
switch_to_blog( $blog_id );
$this->_set_site_icon();
restore_current_blog();
$this->assertTrue( has_site_icon( $blog_id ) );
}
/**
* @group site_icon
* @group multisite
*/
function test_has_site_icon_returns_false_when_called_for_other_site_without_site_icon_set() {
if ( ! is_multisite() ) {
$this->markTestSkipped( 'This test requires multisite.' );
}
$blog_id = $this->factory->blog->create();
$this->assertFalse( has_site_icon( $blog_id ) );
}
/**
* @group site_icon
*/