WordPress.org

Make WordPress Core

Changeset 42009


Ignore:
Timestamp:
10/24/2017 11:09:43 PM (22 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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/ajax-actions.php

    r41985 r42009  
    30283028    $wp_embed->return_false_on_fail = true;
    30293029
     3030    if ( 0 === $post_id ) {
     3031        /*
     3032         * Refresh oEmbeds cached outside of posts that are past their TTL.
     3033         * Posts are excluded because they have separate logic for refreshing
     3034         * their post meta caches. See WP_Embed::cache_oembed().
     3035         */
     3036        $wp_embed->usecache = false;
     3037    }
     3038
    30303039    if ( is_ssl() && 0 === strpos( $url, 'http://' ) ) {
    30313040        // Admin is ssl and the user pasted non-ssl URL.
  • trunk/src/wp-includes/class-wp-embed.php

    r41651 r42009  
    285285            }
    286286
    287             wp_insert_post( wp_slash( array(
    288                 'post_name'    => $key_suffix,
    289                 'post_content' => $html ? $html : '{{unknown}}',
    290                 'post_status'  => 'publish',
    291                 'post_type'    => 'oembed_cache',
    292             ) ) );
     287            $insert_post_args = array(
     288                'post_name' => $key_suffix,
     289                'post_status' => 'publish',
     290                'post_type' => 'oembed_cache',
     291            );
     292
     293            if ( $html ) {
     294                if ( $cached_post_id ) {
     295                    wp_update_post( wp_slash( array(
     296                        'ID' => $cached_post_id,
     297                        'post_content' => $html,
     298                    ) ) );
     299                } else {
     300                    wp_insert_post( wp_slash( array_merge(
     301                        $insert_post_args,
     302                        array(
     303                            'post_content' => $html,
     304                        )
     305                    ) ) );
     306                }
     307            } elseif ( ! $cache ) {
     308                wp_insert_post( wp_slash( array_merge(
     309                    $insert_post_args,
     310                    array(
     311                        'post_content' => '{{unknown}}',
     312                    )
     313                ) ) );
     314            }
    293315
    294316            if ( $has_kses ) {
  • 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.