Make WordPress Core

Ticket #45142: 45142.diff

File 45142.diff, 4.6 KB (added by swissspidy, 7 years ago)
  • src/wp-includes/class-wp-oembed-controller.php

    diff --git src/wp-includes/class-wp-oembed-controller.php src/wp-includes/class-wp-oembed-controller.php
    index 7ee7950b07..a4055d13d1 100644
    final class WP_oEmbed_Controller { 
    173173                        $args['height'] = $args['maxheight'];
    174174                }
    175175
    176                 $data = _wp_oembed_get_object()->get_data( $url, $args );
     176                // Short-circuit process for URLs belonging to the current site.
     177                $data = get_oembed_response_data_for_url( $url, $args );
     178
     179                if ( ! $data ) {
     180                        $data = _wp_oembed_get_object()->get_data( $url, $args );
     181                }
    177182
    178183                if ( false === $data ) {
    179184                        return new WP_Error( 'oembed_invalid_url', get_status_header_desc( 404 ), array( 'status' => 404 ) );
    180185                }
    181186
     187                /** This filter is documented in wp-includes/class-oembed.php */
     188                $data->html = apply_filters( 'oembed_result', _wp_oembed_get_object()->data2html( $data, $url ), $url, $args );
     189
    182190                /**
    183191                 * Filters the oEmbed TTL value (time to live).
    184192                 *
  • src/wp-includes/embed.php

    diff --git src/wp-includes/embed.php src/wp-includes/embed.php
    index 176988057b..efd991104a 100644
    function get_oembed_response_data( $post, $width ) { 
    555555        return apply_filters( 'oembed_response_data', $data, $post, $width, $height );
    556556}
    557557
     558
     559/**
     560 * Retrieves the oEmbed response data for a given URL.
     561 *
     562 * @since 5.0.0
     563 *
     564 * @param string $url  The URL that should be inspected for discovery `<link>` tags.
     565 * @param array  $args oEmbed remote get arguments.
     566 * @return object|false oEmbed response data if the URL does belong to the current site. False otherwise.
     567 */
     568function get_oembed_response_data_for_url( $url, $args ) {
     569        $switched_blog = false;
     570
     571        if ( is_multisite() ) {
     572                $url_parts = wp_parse_args( wp_parse_url( $url ), array(
     573                        'host'   => '',
     574                        'path'   => '/',
     575                ) );
     576
     577                $qv = array( 'domain' => $url_parts['host'], 'path' => '/' );
     578
     579                // In case of subdirectory configs, set the path.
     580                if ( ! is_subdomain_install() ) {
     581                        $path = explode( '/', ltrim( $url_parts['path'], '/' ) );
     582                        $path = reset( $path );
     583
     584                        if ( $path ) {
     585                                $qv['path'] = get_network()->path . $path . '/';
     586                        }
     587                }
     588
     589                $sites = get_sites( $qv );
     590                $site  = reset( $sites );
     591
     592                if ( $site && (int) $site->blog_id !== get_current_blog_id() ) {
     593                        switch_to_blog( $site->blog_id );
     594                        $switched_blog = true;
     595                }
     596        }
     597
     598        $post_id = url_to_postid( $url );
     599
     600        /** This filter is documented in wp-includes/class-wp-oembed-controller.php */
     601        $post_id = apply_filters( 'oembed_request_post_id', $post_id, $url );
     602
     603        if ( ! $post_id ) {
     604                if ( $switched_blog ) {
     605                        restore_current_blog();
     606                }
     607
     608                return false;
     609        }
     610
     611        $width = isset( $args['width'] ) ? $args['width'] : 0;
     612
     613        $data = get_oembed_response_data( $post_id, $width );
     614
     615        if ( $switched_blog ) {
     616                restore_current_blog();
     617        }
     618
     619        return $data ? (object) $data : false;
     620}
     621
     622
    558623/**
    559624 * Filters the oEmbed response data to return an iframe embed code.
    560625 *
    function the_embed_site_title() { 
    10711136 *                     Null if the URL does not belong to the current site.
    10721137 */
    10731138function wp_filter_pre_oembed_result( $result, $url, $args ) {
    1074         $switched_blog = false;
    1075 
    1076         if ( is_multisite() ) {
    1077                 $url_parts = wp_parse_args( wp_parse_url( $url ), array(
    1078                         'host'   => '',
    1079                         'path'   => '/',
    1080                 ) );
    1081 
    1082                 $qv = array( 'domain' => $url_parts['host'], 'path' => '/' );
    1083 
    1084                 // In case of subdirectory configs, set the path.
    1085                 if ( ! is_subdomain_install() ) {
    1086                         $path = explode( '/', ltrim( $url_parts['path'], '/' ) );
    1087                         $path = reset( $path );
    1088 
    1089                         if ( $path ) {
    1090                                 $qv['path'] = get_network()->path . $path . '/';
    1091                         }
    1092                 }
    1093 
    1094                 $sites = get_sites( $qv );
    1095                 $site  = reset( $sites );
    1096 
    1097                 if ( $site && (int) $site->blog_id !== get_current_blog_id() ) {
    1098                         switch_to_blog( $site->blog_id );
    1099                         $switched_blog = true;
    1100                 }
    1101         }
    1102 
    1103         $post_id = url_to_postid( $url );
    1104 
    1105         /** This filter is documented in wp-includes/class-wp-oembed-controller.php */
    1106         $post_id = apply_filters( 'oembed_request_post_id', $post_id, $url );
    1107 
    1108         if ( ! $post_id ) {
    1109                 if ( $switched_blog ) {
    1110                         restore_current_blog();
    1111                 }
     1139        $data = get_oembed_response_data_for_url( $url, $args );
    11121140
    1113                 return $result;
     1141        if ( $data ) {
     1142                return _wp_oembed_get_object()->data2html( $data, $url );
    11141143        }
    11151144
    1116         $width = isset( $args['width'] ) ? $args['width'] : 0;
    1117 
    1118         $data = get_oembed_response_data( $post_id, $width );
    1119         $data = _wp_oembed_get_object()->data2html( (object) $data, $url );
    1120 
    1121         if ( $switched_blog ) {
    1122                 restore_current_blog();
    1123         }
    1124 
    1125         if ( ! $data ) {
    1126                 return $result;
    1127         }
    1128 
    1129         return $data;
     1145        return $result;
    11301146}