Make WordPress Core

Changeset 37709


Ignore:
Timestamp:
06/15/2016 11:31:12 AM (8 years ago)
Author:
swissspidy
Message:

Embeds: Improve performance when embedding a post of 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().

Merge of [37708] to the 4.5 branch.

Fixes #36767.

Location:
branches/4.5
Files:
4 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/4.5

  • branches/4.5/src/wp-includes/class-oembed.php

    r37069 r37709  
    316316     */
    317317    public function get_html( $url, $args = '' ) {
     318        /**
     319         * Filters the oEmbed result before any HTTP requests are made.
     320         *
     321         * This allows one to short-circuit the default logic, perhaps by
     322         * replacing it with a routine that is more optimal for your setup.
     323         *
     324         * Passing a non-null value to the filter will effectively short-circuit retrieval,
     325         * returning the passed value instead.
     326         *
     327         * @since 4.5.3
     328         *
     329         * @param null|string $result The UNSANITIZED (and potentially unsafe) HTML that should be used to embed. Default null.
     330         * @param string      $url    The URL to the content that should be attempted to be embedded.
     331         * @param array       $args   Optional. Arguments, usually passed from a shortcode. Default empty.
     332         */
     333        $pre = apply_filters( 'pre_oembed_result', null, $url, $args );
     334
     335        if ( null !== $pre ) {
     336            return $pre;
     337        }
     338
    318339        $provider = $this->get_provider( $url, $args );
    319340
    320         if ( !$provider || false === $data = $this->fetch( $provider, $url, $args ) )
    321             return false;
     341        if ( ! $provider || false === $data = $this->fetch( $provider, $url, $args ) ) {
     342            return false;
     343        }
    322344
    323345        /**
  • branches/4.5/src/wp-includes/default-filters.php

    r36915 r37709  
    477477add_filter( 'oembed_dataparse',       'wp_filter_oembed_result',        10, 3 );
    478478add_filter( 'oembed_response_data',   'get_oembed_response_data_rich',  10, 4 );
     479add_filter( 'pre_oembed_result',      'wp_filter_pre_oembed_result',    10, 3 );
    479480
    480481unset( $filter, $action );
  • branches/4.5/src/wp-includes/embed.php

    r36923 r37709  
    10801080    echo apply_filters( 'embed_site_title_html', $site_title );
    10811081}
     1082
     1083/**
     1084 * Filters the oEmbed result before any HTTP requests are made.
     1085 *
     1086 * If the URL belongs to the current site, the result is fetched directly instead of
     1087 * going through the oEmbed discovery process.
     1088 *
     1089 * @since 4.5.3
     1090 *
     1091 * @param null|string $result The UNSANITIZED (and potentially unsafe) HTML that should be used to embed. Default null.
     1092 * @param string      $url    The URL that should be inspected for discovery `<link>` tags.
     1093 * @param array       $args   oEmbed remote get arguments.
     1094 * @return null|string The UNSANITIZED (and potentially unsafe) HTML that should be used to embed.
     1095 *                     Null if the URL does not belong to the current site.
     1096 */
     1097function wp_filter_pre_oembed_result( $result, $url, $args ) {
     1098    $post_id = url_to_postid( $url );
     1099
     1100    /** This filter is documented in wp-includes/class-wp-oembed-controller.php */
     1101    $post_id = apply_filters( 'oembed_request_post_id', $post_id, $url );
     1102
     1103    $width = isset( $args['width'] ) ? $args['width'] : 0;
     1104
     1105    $data = get_oembed_response_data( $post_id, $width );
     1106    $data = _wp_oembed_get_object()->data2html( (object) $data, $url );
     1107
     1108    if ( ! $data ) {
     1109        return $result;
     1110    }
     1111
     1112    return $data;
     1113}
Note: See TracChangeset for help on using the changeset viewer.