From 629b43769be4bc7e2e7661b1ccf7f925fa94adcd Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 15 Jun 2016 11:23:28 +0000 Subject: [PATCH] Embeds: Improve performance when embedding a post from the current site. When the post being embedded is from the same site, there's no reason to do an HTTP request for it. The data can be fetched directly using `get_oembed_response_data()`. Fixes #36767 for trunk. Built from https://develop.svn.wordpress.org/trunk@37708 git-svn-id: http://core.svn.wordpress.org/trunk@37674 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-oembed.php | 24 +++++++++++++++++++++++- wp-includes/default-filters.php | 1 + wp-includes/embed.php | 32 ++++++++++++++++++++++++++++++++ wp-includes/version.php | 2 +- 4 files changed, 57 insertions(+), 2 deletions(-) diff --git a/wp-includes/class-oembed.php b/wp-includes/class-oembed.php index ef36683bc0..6bdc925e63 100644 --- a/wp-includes/class-oembed.php +++ b/wp-includes/class-oembed.php @@ -315,10 +315,32 @@ class WP_oEmbed { * @return false|string False on failure, otherwise the UNSANITIZED (and potentially unsafe) HTML that should be used to embed. */ public function get_html( $url, $args = '' ) { + /** + * Filters the oEmbed result before any HTTP requests are made. + * + * This allows one to short-circuit the default logic, perhaps by + * replacing it with a routine that is more optimal for your setup. + * + * Passing a non-null value to the filter will effectively short-circuit retrieval, + * returning the passed value instead. + * + * @since 4.5.3 + * + * @param null|string $result The UNSANITIZED (and potentially unsafe) HTML that should be used to embed. Default null. + * @param string $url The URL to the content that should be attempted to be embedded. + * @param array $args Optional. Arguments, usually passed from a shortcode. Default empty. + */ + $pre = apply_filters( 'pre_oembed_result', null, $url, $args ); + + if ( null !== $pre ) { + return $pre; + } + $provider = $this->get_provider( $url, $args ); - if ( !$provider || false === $data = $this->fetch( $provider, $url, $args ) ) + if ( ! $provider || false === $data = $this->fetch( $provider, $url, $args ) ) { return false; + } /** * Filters the HTML returned by the oEmbed provider. diff --git a/wp-includes/default-filters.php b/wp-includes/default-filters.php index f7bfeb52fb..ddaa5efe24 100644 --- a/wp-includes/default-filters.php +++ b/wp-includes/default-filters.php @@ -476,5 +476,6 @@ add_filter( 'the_excerpt_embed', 'wp_embed_excerpt_attachment' ); add_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10, 3 ); add_filter( 'oembed_response_data', 'get_oembed_response_data_rich', 10, 4 ); +add_filter( 'pre_oembed_result', 'wp_filter_pre_oembed_result', 10, 3 ); unset( $filter, $action ); diff --git a/wp-includes/embed.php b/wp-includes/embed.php index 91e42691cc..68536434cb 100644 --- a/wp-includes/embed.php +++ b/wp-includes/embed.php @@ -1079,3 +1079,35 @@ function the_embed_site_title() { */ echo apply_filters( 'embed_site_title_html', $site_title ); } + +/** + * Filters the oEmbed result before any HTTP requests are made. + * + * If the URL belongs to the current site, the result is fetched directly instead of + * going through the oEmbed discovery process. + * + * @since 4.5.3 + * + * @param null|string $result The UNSANITIZED (and potentially unsafe) HTML that should be used to embed. Default null. + * @param string $url The URL that should be inspected for discovery `` tags. + * @param array $args oEmbed remote get arguments. + * @return null|string The UNSANITIZED (and potentially unsafe) HTML that should be used to embed. + * Null if the URL does not belong to the current site. + */ +function wp_filter_pre_oembed_result( $result, $url, $args ) { + $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 ); + + $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 ( ! $data ) { + return $result; + } + + return $data; +} diff --git a/wp-includes/version.php b/wp-includes/version.php index f7543e03e8..51e8c7a01f 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.6-alpha-37707'; +$wp_version = '4.6-alpha-37708'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.