Make WordPress Core

Ticket #14759: 14759.patch

File 14759.patch, 4.0 KB (added by leewillis77, 10 years ago)

Initial patch for discusssion

  • media.php

     
    10241024 *
    10251025 * @param string $url The URL that should be embedded.
    10261026 * @param array $args Additional arguments and parameters.
     1027 * @param bool $use_cache Use a cached value if available.
    10271028 * @return bool|string False on failure or the embed HTML on success.
    10281029 */
    1029 function wp_oembed_get( $url, $args = '' ) {
     1030function wp_oembed_get( $url, $args = '', $use_cache = true ) {
    10301031        require_once( ABSPATH . WPINC . '/class-oembed.php' );
    10311032        $oembed = _wp_oembed_get_object();
    1032         return $oembed->get_html( $url, $args );
     1033        return $oembed->get_html( $url, $args, $use_cache );
    10331034}
    10341035
    10351036/**
  • class-oembed.php

     
    6666         *
    6767         * @param string $url The URL to the content that should be attempted to be embedded.
    6868         * @param array $args Optional arguments. Usually passed from a shortcode.
     69     * @param bool $use_cache Use a cached value if available.
    6970         * @return bool|string False on failure, otherwise the UNSANITIZED (and potentially unsafe) HTML that should be used to embed.
    7071         */
    71         function get_html( $url, $args = '' ) {
     72        function get_html( $url, $args = '', $use_cache = true ) {
     73
     74        // Check for a cached result (stored in the post meta)
     75        $cachekey = '_oembed_' . md5( $url . serialize( $attr ) );
     76
     77        if ( $use_cache ) {
     78            $cache = get_transient( $cachekey );
     79
     80            if ( ! empty( $cache ) )
     81                return $cache;
     82
     83        }
     84
    7285                $provider = false;
    7386
    7487                if ( !isset($args['discover']) )
     
    92105                if ( !$provider && $args['discover'] )
    93106                        $provider = $this->discover( $url );
    94107
    95                 if ( !$provider || false === $data = $this->fetch( $provider, $url, $args ) )
    96                         return false;
     108                if ( !$provider || false === $data = $this->fetch( $provider, $url, $args ) ) {
     109            // Cache failures.
     110            set_transient( $cachekey, '{{unknown}}', 12 * HOUR_IN_SECONDS );
     111            return '{{unknown}}';
     112        }
    97113
    98                 return apply_filters( 'oembed_result', $this->data2html( $data, $url ), $url, $args );
     114        $html = apply_filters( 'oembed_result', $this->data2html( $data, $url ), $url, $args );
     115        $expiry_time = ! empty ( $data->cache_age ) ? (int) $data->cache_age : 30 * DAY_IN_SECONDS;
     116        set_transient( $cachekey, $html, $expiry_time );
     117
     118                return $html;
    99119        }
    100120
    101121        /**
  • class-wp-embed.php

     
    165165                // Unknown URL format. Let oEmbed have a go.
    166166                if ( $post_ID ) {
    167167
    168                         // Check for a cached result (stored in the post meta)
    169                         $cachekey = '_oembed_' . md5( $url . serialize( $attr ) );
    170                         if ( $this->usecache ) {
    171                                 $cache = get_post_meta( $post_ID, $cachekey, true );
    172 
    173                                 // Failures are cached
    174                                 if ( '{{unknown}}' === $cache )
    175                                         return $this->maybe_make_link( $url );
    176 
    177                                 if ( ! empty( $cache ) )
    178                                         return apply_filters( 'embed_oembed_html', $cache, $url, $attr, $post_ID );
    179                         }
    180 
    181168                        // Use oEmbed to get the HTML
    182169                        $attr['discover'] = ( apply_filters('embed_oembed_discover', false) && author_can( $post_ID, 'unfiltered_html' ) );
    183                         $html = wp_oembed_get( $url, $attr );
     170                        $html = wp_oembed_get( $url, $attr, $this->usecache );
    184171
    185                         // Cache the result
    186                         $cache = ( $html ) ? $html : '{{unknown}}';
    187                         update_post_meta( $post_ID, $cachekey, $cache );
     172                        // If there was a result, return it
     173                        if ( $html ) {
     174                // Failures
     175                if ( '{{unknown}}' === $html )
     176                    return $this->maybe_make_link( $url );
    188177
    189                         // If there was a result, return it
    190                         if ( $html )
    191178                                return apply_filters( 'embed_oembed_html', $html, $url, $attr, $post_ID );
     179            }
    192180                }
    193181
    194182                // Still unknown