Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/wp-includes/class-oembed.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -405,9 +405,9 @@ public function get_html( $url, $args = '' ) {
*
* @since 2.9.0
*
* @param string $data The returned oEmbed HTML.
* @param string $url URL of the content to be embedded.
* @param array $args Optional arguments, usually passed from a shortcode.
* @param string|false $data The returned oEmbed HTML (false if unsafe).
* @param string $url URL of the content to be embedded.
* @param array $args Optional arguments, usually passed from a shortcode.
*/
return apply_filters( 'oembed_result', $this->data2html( $data, $url ), $url, $args );
}
Expand Down
10 changes: 10 additions & 0 deletions src/wp-includes/class-wp-oembed-controller.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -173,12 +173,22 @@ public function get_proxy_item( $request ) {
$args['height'] = $args['maxheight'];
}

// Short-circuit process for URLs belonging to the current site.
$data = get_oembed_response_data_for_url( $url, $args );

if ( $data ) {
return $data;
}

$data = _wp_oembed_get_object()->get_data( $url, $args );

if ( false === $data ) {
return new WP_Error( 'oembed_invalid_url', get_status_header_desc( 404 ), array( 'status' => 404 ) );
}

/** This filter is documented in wp-includes/class-oembed.php */
$data->html = apply_filters( 'oembed_result', _wp_oembed_get_object()->data2html( (object) $data, $url ), $url, $args );

/**
* Filters the oEmbed TTL value (time to live).
*
Expand Down
122 changes: 69 additions & 53 deletions src/wp-includes/embed.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -555,6 +555,71 @@ function get_oembed_response_data( $post, $width ) {
return apply_filters( 'oembed_response_data', $data, $post, $width, $height );
}


/**
* Retrieves the oEmbed response data for a given URL.
*
* @since 5.0.0
*
* @param string $url The URL that should be inspected for discovery `<link>` tags.
* @param array $args oEmbed remote get arguments.
* @return object|false oEmbed response data if the URL does belong to the current site. False otherwise.
*/
function get_oembed_response_data_for_url( $url, $args ) {
$switched_blog = false;

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 );
$switched_blog = true;
}
}

$post_id = url_to_postid( $url );

/** This filter is documented in wp-includes/class-wp-oembed-controller.php */
$post_id = apply_filters( 'oembed_request_post_id', $post_id, $url );

if ( ! $post_id ) {
if ( $switched_blog ) {
restore_current_blog();
}

return false;
}

$width = isset( $args['width'] ) ? $args['width'] : 0;

$data = get_oembed_response_data( $post_id, $width );

if ( $switched_blog ) {
restore_current_blog();
}

return $data ? (object) $data : false;
}


/**
* Filters the oEmbed response data to return an iframe embed code.
*
Expand DownExpand Up@@ -1071,60 +1136,11 @@ 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 ) {
$switched_blog = false;

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 );
$switched_blog = true;
}
}

$post_id = url_to_postid( $url );

/** This filter is documented in wp-includes/class-wp-oembed-controller.php */
$post_id = apply_filters( 'oembed_request_post_id', $post_id, $url );

if ( ! $post_id ) {
if ( $switched_blog ) {
restore_current_blog();
}
$data = get_oembed_response_data_for_url( $url, $args );

return $result;
if ( $data ) {
return _wp_oembed_get_object()->data2html( $data, $url );
}

$width = isset( $args['width'] ) ? $args['width'] : 0;

$data = get_oembed_response_data( $post_id, $width );
$data = _wp_oembed_get_object()->data2html( (object) $data, $url );

if ( $switched_blog ) {
restore_current_blog();
}

if ( ! $data ) {
return $result;
}

return $data;
return $result;
}
Loading