diff --git a/src/wp-includes/embed.php b/src/wp-includes/embed.php index 7e5bb374b9..d83e8f510d 100644 --- a/src/wp-includes/embed.php +++ b/src/wp-includes/embed.php @@ -1071,6 +1071,32 @@ 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 ) { + if ( is_multisite() ) { + $url_parts = wp_parse_args( wp_parse_url( $url ), array( + 'host' => '', + 'path' => '/', + ) ); + + $qv = array( 'domain' => $url_parts['host'], 'path' => '/' ); + + // In case of subdirectory configs, set the path. + if ( ! is_subdomain_install() ) { + $path = explode( '/', ltrim( $url_parts['path'], '/' ) ); + $path = reset( $path ); + + if ( $path ) { + $qv['path'] = get_network()->path . $path . '/'; + } + } + + $sites = get_sites( $qv ); + $site = reset( $sites ); + + if ( $site && (int) $site->blog_id !== get_current_blog_id() ) { + switch_to_blog( $site->blog_id ); + } + } + $post_id = url_to_postid( $url ); /** This filter is documented in wp-includes/class-wp-oembed-controller.php */ @@ -1085,6 +1111,10 @@ 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() ) { + restore_current_blog(); + } + if ( ! $data ) { return $result; } diff --git a/tests/phpunit/tests/oembed/wpOembed.php b/tests/phpunit/tests/oembed/wpOembed.php index ade91df95e..767655c873 100644 --- a/tests/phpunit/tests/oembed/wpOembed.php +++ b/tests/phpunit/tests/oembed/wpOembed.php @@ -69,4 +69,106 @@ class Tests_WP_oEmbed extends WP_UnitTestCase { $this->assertNotFalse( $this->pre_oembed_result_filtered ); $this->assertFalse( $actual ); } + + /** + * @ticket 40673 + * @group multisite + * @group ms-required + */ + public function test_wp_filter_pre_oembed_result_multisite_root_root() { + $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' ) ); + + $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_sub_samesub() { + $user_id = self::factory()->user->create(); + + $blog_id = self::factory()->blog->create( array( + 'user_id' => $user_id, + ) ); + + switch_to_blog( $blog_id ); + + $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' ) ); + + restore_current_blog(); + + $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_sub_othersub() { + $user_id = self::factory()->user->create(); + + $blog_id = self::factory()->blog->create( array( + 'user_id' => $user_id, + ) ); + + switch_to_blog( $blog_id ); + + $post_id = self::factory()->post->create(); + $permalink = get_permalink( $post_id ); + + $blog_id = self::factory()->blog->create( array( + 'user_id' => $user_id, + ) ); + + switch_to_blog( $blog_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' ) ); + + restore_current_blog(); + + $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_sub_main() { + $post_id = self::factory()->post->create(); + $permalink = get_permalink( $post_id ); + $user_id = self::factory()->user->create(); + $blog_id = self::factory()->blog->create( array( + 'user_id' => $user_id, + ) ); + + switch_to_blog( $blog_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' ) ); + + restore_current_blog(); + + $this->assertNotNull( $this->pre_oembed_result_filtered ); + $this->assertEquals( $this->pre_oembed_result_filtered, $actual ); + } }