Embeds: Improve performance when embedding a post on Multisite.

After [37798], this fixes embeds coming from a different site in the network.

Props imath.
Fixes #40673. See #36767. 


git-svn-id: https://develop.svn.wordpress.org/trunk@41600 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Pascal Birchler 2017-09-26 08:39:57 +00:00
parent dd065164f4
commit 14b593d669
2 changed files with 132 additions and 0 deletions

View File

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

View File

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