From 0df4e1f0269248acb932232e0870358b5b0b7a4e Mon Sep 17 00:00:00 2001 From: Jeremy Felt Date: Mon, 8 Aug 2016 22:32:47 +0000 Subject: [PATCH] Multisite: Use `get_current_blog_id()` in `get_site()` for current site. The global `$current_blog` is not switched in `switch_to_blog()` and can not be used to properly retrieve current switched site information. See #37607. git-svn-id: https://develop.svn.wordpress.org/trunk@38217 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/ms-blogs.php | 7 ++-- tests/phpunit/tests/multisite/getSite.php | 44 +++++++++++++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 tests/phpunit/tests/multisite/getSite.php diff --git a/src/wp-includes/ms-blogs.php b/src/wp-includes/ms-blogs.php index d1e9aa00e6..ddd21e4097 100644 --- a/src/wp-includes/ms-blogs.php +++ b/src/wp-includes/ms-blogs.php @@ -478,15 +478,12 @@ function clean_blog_cache( $blog ) { * * @since 4.6.0 * - * @global WP_Site $current_blog The current site. - * * @param WP_Site|int|null $site Optional. Site to retrieve. Default is the current site. * @return WP_Site|null The site object or null if not found. */ function get_site( &$site = null ) { - global $current_blog; - if ( empty( $site ) && isset( $current_blog ) ) { - $site = $current_blog; + if ( empty( $site ) ) { + $site = get_current_blog_id(); } if ( $site instanceof WP_Site ) { diff --git a/tests/phpunit/tests/multisite/getSite.php b/tests/phpunit/tests/multisite/getSite.php new file mode 100644 index 0000000000..f42ec43ff8 --- /dev/null +++ b/tests/phpunit/tests/multisite/getSite.php @@ -0,0 +1,44 @@ + array( 'domain' => 'wordpress.org', 'path' => '/' ), + 'wordpress.org/foo/' => array( 'domain' => 'wordpress.org', 'path' => '/foo/' ), + 'wordpress.org/foo/bar/' => array( 'domain' => 'wordpress.org', 'path' => '/foo/bar/' ), + ); + + foreach ( self::$site_ids as &$id ) { + $id = $factory->blog->create( $id ); + } + unset( $id ); + } + + public static function wpTearDownAfterClass() { + foreach( self::$site_ids as $id ) { + wpmu_delete_blog( $id, true ); + } + + wp_update_network_site_counts(); + } + + public function test_get_site_in_switched_state_returns_switched_site() { + switch_to_blog( self::$site_ids[ 'wordpress.org/foo/' ] ); + $site = get_site(); + restore_current_blog(); + + $this->assertEquals( self::$site_ids[ 'wordpress.org/foo/'], $site->id ); + } + +} + +endif;