WordPress.org

Make WordPress Core

Changeset 41600


Ignore:
Timestamp:
09/26/2017 08:39:57 AM (10 months ago)
Author:
swissspidy
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/embed.php

    r41448 r41600  
    10721072 */
    10731073function wp_filter_pre_oembed_result( $result, $url, $args ) {
     1074    if ( is_multisite() ) {
     1075        $url_parts = wp_parse_args( wp_parse_url( $url ), array(
     1076            'host'   => '',
     1077            'path'   => '/',
     1078        ) );
     1079
     1080        $qv = array( 'domain' => $url_parts['host'], 'path' => '/' );
     1081
     1082        // In case of subdirectory configs, set the path.
     1083        if ( ! is_subdomain_install() ) {
     1084            $path = explode( '/', ltrim( $url_parts['path'], '/' ) );
     1085            $path = reset( $path );
     1086
     1087            if ( $path ) {
     1088                $qv['path'] = get_network()->path . $path . '/';
     1089            }
     1090        }
     1091
     1092        $sites = get_sites( $qv );
     1093        $site  = reset( $sites );
     1094
     1095        if ( $site && (int) $site->blog_id !== get_current_blog_id() ) {
     1096            switch_to_blog( $site->blog_id );
     1097        }
     1098    }
     1099
    10741100    $post_id = url_to_postid( $url );
    10751101
     
    10861112    $data = _wp_oembed_get_object()->data2html( (object) $data, $url );
    10871113
     1114    if ( is_multisite() && ms_is_switched() ) {
     1115        restore_current_blog();
     1116    }
     1117
    10881118    if ( ! $data ) {
    10891119        return $result;
  • trunk/tests/phpunit/tests/oembed/wpOembed.php

    r39919 r41600  
    7070        $this->assertFalse( $actual );
    7171    }
     72
     73    /**
     74     * @ticket 40673
     75     * @group multisite
     76     * @group ms-required
     77     */
     78    public function test_wp_filter_pre_oembed_result_multisite_root_root() {
     79        $post_id   = self::factory()->post->create();
     80        $permalink = get_permalink( $post_id );
     81
     82        add_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) );
     83        $actual = $this->oembed->get_html( $permalink );
     84        remove_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) );
     85
     86        $this->assertNotNull( $this->pre_oembed_result_filtered );
     87        $this->assertEquals( $this->pre_oembed_result_filtered, $actual );
     88    }
     89
     90    /**
     91     * @ticket 40673
     92     * @group multisite
     93     * @group ms-required
     94     */
     95    public function test_wp_filter_pre_oembed_result_multisite_sub_samesub() {
     96        $user_id = self::factory()->user->create();
     97
     98        $blog_id = self::factory()->blog->create( array(
     99            'user_id' => $user_id,
     100        ) );
     101
     102        switch_to_blog( $blog_id );
     103
     104        $post_id   = self::factory()->post->create();
     105        $permalink = get_permalink( $post_id );
     106
     107        add_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) );
     108        $actual = $this->oembed->get_html( $permalink );
     109        remove_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) );
     110
     111        restore_current_blog();
     112
     113        $this->assertNotNull( $this->pre_oembed_result_filtered );
     114        $this->assertEquals( $this->pre_oembed_result_filtered, $actual );
     115    }
     116
     117    /**
     118     * @ticket 40673
     119     * @group multisite
     120     * @group ms-required
     121     */
     122    public function test_wp_filter_pre_oembed_result_multisite_sub_othersub() {
     123        $user_id = self::factory()->user->create();
     124
     125        $blog_id = self::factory()->blog->create( array(
     126            'user_id' => $user_id,
     127        ) );
     128
     129        switch_to_blog( $blog_id );
     130
     131        $post_id   = self::factory()->post->create();
     132        $permalink = get_permalink( $post_id );
     133
     134        $blog_id = self::factory()->blog->create( array(
     135            'user_id' => $user_id,
     136        ) );
     137
     138        switch_to_blog( $blog_id );
     139
     140        add_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) );
     141        $actual = $this->oembed->get_html( $permalink );
     142        remove_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) );
     143
     144        restore_current_blog();
     145
     146        $this->assertNotNull( $this->pre_oembed_result_filtered );
     147        $this->assertEquals( $this->pre_oembed_result_filtered, $actual );
     148    }
     149
     150    /**
     151     * @ticket 40673
     152     * @group multisite
     153     * @group ms-required
     154     */
     155    public function test_wp_filter_pre_oembed_result_multisite_sub_main() {
     156        $post_id   = self::factory()->post->create();
     157        $permalink = get_permalink( $post_id );
     158        $user_id   = self::factory()->user->create();
     159        $blog_id   = self::factory()->blog->create( array(
     160            'user_id' => $user_id,
     161        ) );
     162
     163        switch_to_blog( $blog_id );
     164
     165        add_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) );
     166        $actual = $this->oembed->get_html( $permalink );
     167        remove_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) );
     168
     169        restore_current_blog();
     170
     171        $this->assertNotNull( $this->pre_oembed_result_filtered );
     172        $this->assertEquals( $this->pre_oembed_result_filtered, $actual );
     173    }
    72174}
Note: See TracChangeset for help on using the changeset viewer.