From 80bd8ac220ab0ac10afc2e19fcbab1ba2368ec7c Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 27 Sep 2017 08:35:16 +0000 Subject: [PATCH] Embeds: Maintain switched state when embedding a post on Multisite. Props bor0. Fixes #40673. git-svn-id: https://develop.svn.wordpress.org/trunk@41606 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/embed.php | 5 ++++- tests/phpunit/tests/oembed/wpOembed.php | 29 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/embed.php b/src/wp-includes/embed.php index d83e8f510d..6040004bac 100644 --- a/src/wp-includes/embed.php +++ b/src/wp-includes/embed.php @@ -1071,6 +1071,8 @@ function the_embed_site_title() { * Null if the URL does not belong to the current site. */ function wp_filter_pre_oembed_result( $result, $url, $args ) { + $switched_blog = false; + if ( is_multisite() ) { $url_parts = wp_parse_args( wp_parse_url( $url ), array( 'host' => '', @@ -1094,6 +1096,7 @@ function wp_filter_pre_oembed_result( $result, $url, $args ) { if ( $site && (int) $site->blog_id !== get_current_blog_id() ) { switch_to_blog( $site->blog_id ); + $switched_blog = true; } } @@ -1111,7 +1114,7 @@ function wp_filter_pre_oembed_result( $result, $url, $args ) { $data = get_oembed_response_data( $post_id, $width ); $data = _wp_oembed_get_object()->data2html( (object) $data, $url ); - if ( is_multisite() && ms_is_switched() ) { + if ( $switched_blog ) { restore_current_blog(); } diff --git a/tests/phpunit/tests/oembed/wpOembed.php b/tests/phpunit/tests/oembed/wpOembed.php index 767655c873..d8d346de11 100644 --- a/tests/phpunit/tests/oembed/wpOembed.php +++ b/tests/phpunit/tests/oembed/wpOembed.php @@ -171,4 +171,33 @@ class Tests_WP_oEmbed extends WP_UnitTestCase { $this->assertNotNull( $this->pre_oembed_result_filtered ); $this->assertEquals( $this->pre_oembed_result_filtered, $actual ); } + + /** + * @ticket 40673 + * @group multisite + * @group ms-required + */ + public function test_wp_filter_pre_oembed_result_multisite_preserves_switched_state() { + $user_id = self::factory()->user->create(); + + $blog_id = self::factory()->blog->create( array( 'user_id' => $user_id ) ); + switch_to_blog( $blog_id ); + + $expected_stack = $GLOBALS['_wp_switched_stack']; + + $post_id = self::factory()->post->create(); + $permalink = get_permalink( $post_id ); + + add_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) ); + $actual = $this->oembed->get_html( $permalink ); + remove_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) ); + + $actual_stack = $GLOBALS['_wp_switched_stack']; + + restore_current_blog(); + + $this->assertNotNull( $this->pre_oembed_result_filtered ); + $this->assertEquals( $this->pre_oembed_result_filtered, $actual ); + $this->assertSame( $expected_stack, $actual_stack ); + } }