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
This commit is contained in:
Pascal Birchler 2017-09-27 08:35:16 +00:00
parent 2ff224a044
commit 80bd8ac220
2 changed files with 33 additions and 1 deletions

View File

@ -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();
}

View File

@ -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 );
}
}