Make WordPress Core

Ticket #14759: 14759.2.diff

File 14759.2.diff, 2.9 KB (added by markjaquith, 9 years ago)
  • wp-admin/includes/ajax-actions.php

    function wp_ajax_imgedit_preview() { 
    208208 * @since 3.1.0
    209209 */
    210210function wp_ajax_oembed_cache() {
    211         global $wp_embed;
    212 
    213         $return = ( $wp_embed->cache_oembed( $_GET['post'] ) ) ? '1' : '0';
    214         wp_die( $return );
     211        $GLOBALS['wp_embed']->cache_oembed( $_GET['post'] );
     212        wp_die( 0 );
    215213}
    216214
    217215/**
  • wp-includes/class-wp-embed.php

    class WP_Embed { 
    2525                // Attempts to embed all URLs in a post
    2626                add_filter( 'the_content', array( $this, 'autoembed' ), 8 );
    2727
    28                 // When a post is saved, invalidate the oEmbed cache
    29                 add_action( 'pre_post_update', array( $this, 'delete_oembed_caches' ) );
    30 
    3128                // After a post is saved, cache oEmbed items via AJAX
    3229                add_action( 'edit_form_advanced', array( $this, 'maybe_run_ajax_cache' ) );
    3330        }
    class WP_Embed { 
    186183                if ( $post_ID ) {
    187184
    188185                        // Check for a cached result (stored in the post meta)
    189                         $cachekey = '_oembed_' . md5( $url . serialize( $attr ) );
    190                         if ( $this->usecache ) {
    191                                 $cache = get_post_meta( $post_ID, $cachekey, true );
    192 
    193                                 // Failures are cached
     186                        $key_suffix = md5( $url . serialize( $attr ) );
     187                        $cachekey = '_oembed_' . $key_suffix;
     188                        $cachekey_time = '_oembed_time_' . $key_suffix;
     189                        $ttl = apply_filters( 'oembed_ttl', DAY_IN_SECONDS, $url, $attr, $post_ID );
     190                        $cache = get_post_meta( $post_ID, $cachekey, true );
     191                        $cache_time = get_post_meta( $post_ID, $cachekey_time, true );
     192                        if ( ! $cache_time ) {
     193                                $cache_time = 0;
     194                        }
     195                        $cached_recently = ( time() - $cache_time ) < $ttl;
     196                        if ( $this->usecache || $cached_recently ) {
     197                                // Failures are cached. Serve one if we're using the cache.
    194198                                if ( '{{unknown}}' === $cache )
    195199                                        return $this->maybe_make_link( $url );
    196200
    197                                 if ( ! empty( $cache ) )
     201                                if ( ! empty( $cache ) ) {
    198202                                        /**
    199203                                         * Filter the cached oEmbed HTML.
    200204                                         *
    class WP_Embed { 
    208212                                         * @param int    $post_ID Post ID.
    209213                                         */
    210214                                        return apply_filters( 'embed_oembed_html', $cache, $url, $attr, $post_ID );
     215                                }
    211216                        }
    212217
    213218                        /**
    class WP_Embed { 
    224229                        // Use oEmbed to get the HTML
    225230                        $html = wp_oembed_get( $url, $attr );
    226231
    227                         // Cache the result
    228                         $cache = ( $html ) ? $html : '{{unknown}}';
    229                         update_post_meta( $post_ID, $cachekey, $cache );
     232                        // Maybe cache the result
     233                        if ( $html ) {
     234                                update_post_meta( $post_ID, $cachekey, $html );
     235                                update_post_meta( $post_ID, $cachekey_time, time() );
     236                        } elseif ( ! $cache ) {
     237                                update_post_meta( $post_ID, $cachekey, '{{unknown}}' );
     238                        }
    230239
    231240                        // If there was a result, return it
    232241                        if ( $html ) {