WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/24/2017 11:09:43 PM (23 months ago)
Author:
westonruter
Message:

Embeds: Improve consistency of update and refresh logic for oEmbed caching between oembed_cache and post meta.

  • Allow updating oEmbed cache during parse-embed requests for non-post editors (such as widgets).
  • Update any existing oembed_cache post when usecache and TTL has passed.
  • Do not overwrite a previously valid cache with {{unknown}}.

Props dlh.
See #34115.
Fixes #42310.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/oembed/WpEmbed.php

    r41651 r42009  
    273273    }
    274274
     275    /**
     276     * Test that parsing an embed shortcode should cause oembed_cache to be updated.
     277     *
     278     * @ticket 42310
     279     */
     280    public function test_shortcode_should_update_custom_post() {
     281        add_filter( 'oembed_ttl', '__return_zero' );
     282
     283        $url        = 'https://example.com/';
     284        $embedded   = '<b>Embedded content</b>';
     285        $key_suffix = md5( $url . serialize( wp_embed_defaults( $url ) ) );
     286
     287        add_filter( 'pre_oembed_result', '__return_empty_string' );
     288        $this->wp_embed->shortcode( array(), $url );
     289        remove_filter( 'pre_oembed_result', '__return_empty_string' );
     290
     291        $oembed_post_id = $this->wp_embed->find_oembed_post_id( $key_suffix );
     292
     293        $this->assertSame( '{{unknown}}', get_post( $oembed_post_id )->post_content );
     294
     295        $previous_usecache = $this->wp_embed->usecache;
     296        $this->wp_embed->usecache = false;
     297
     298        // The update cannot be empty because empty responses won't overwrite the cache.
     299        add_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
     300        $this->wp_embed->shortcode( array(), $url );
     301        remove_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
     302
     303        $this->assertSame( $embedded, get_post( $oembed_post_id )->post_content );
     304
     305        $this->wp_embed->usecache = $previous_usecache;
     306        remove_filter( 'oembed_ttl', '__return_zero' );
     307    }
     308
    275309    public function test_shortcode_should_get_url_from_src_attribute() {
    276310        $url    = 'http://example.com/embed/foo';
Note: See TracChangeset for help on using the changeset viewer.