Make WordPress Core


Ignore:
Timestamp:
10/23/2018 05:47:28 PM (6 years ago)
Author:
danielbachhuber
Message:

Embeds: Filter HTML response in oEmbed proxy controller.

Adapts the response from WP_oEmbed_Controller::get_proxy_item() so that the response is correctly filtered and embeds work properly in JavaSccript editors. Introduces new get_oembed_response_data_for_url() function for preparing internal oEmbed responses.

Props danielbachhuber, imath, swissspidy.
Fixes #45142.

File:
1 edited

Legend:

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

    r41634 r43810  
    555555    return apply_filters( 'oembed_response_data', $data, $post, $width, $height );
    556556}
     557
     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
    557622
    558623/**
     
    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         }
    1112 
    1113         return $result;
    1114     }
    1115 
    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;
    1130 }
     1139    $data = get_oembed_response_data_for_url( $url, $args );
     1140
     1141    if ( $data ) {
     1142        return _wp_oembed_get_object()->data2html( $data, $url );
     1143    }
     1144
     1145    return $result;
     1146}
Note: See TracChangeset for help on using the changeset viewer.