From 18a231bad660844f37c50edce8f970e0454f9f4a Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 24 Oct 2017 23:09:43 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@42009 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/ajax-actions.php | 9 +++++++ src/wp-includes/class-wp-embed.php | 34 +++++++++++++++++++++----- tests/phpunit/tests/oembed/WpEmbed.php | 34 ++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 6 deletions(-) diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php index 43f59b66ec..5985e3561a 100644 --- a/src/wp-admin/includes/ajax-actions.php +++ b/src/wp-admin/includes/ajax-actions.php @@ -3027,6 +3027,15 @@ function wp_ajax_parse_embed() { $parsed = false; $wp_embed->return_false_on_fail = true; + if ( 0 === $post_id ) { + /* + * Refresh oEmbeds cached outside of posts that are past their TTL. + * Posts are excluded because they have separate logic for refreshing + * their post meta caches. See WP_Embed::cache_oembed(). + */ + $wp_embed->usecache = false; + } + if ( is_ssl() && 0 === strpos( $url, 'http://' ) ) { // Admin is ssl and the user pasted non-ssl URL. // Check if the provider supports ssl embeds and use that for the preview. diff --git a/src/wp-includes/class-wp-embed.php b/src/wp-includes/class-wp-embed.php index c40ae2db59..4674caeb9f 100644 --- a/src/wp-includes/class-wp-embed.php +++ b/src/wp-includes/class-wp-embed.php @@ -284,12 +284,34 @@ class WP_Embed { kses_remove_filters(); } - wp_insert_post( wp_slash( array( - 'post_name' => $key_suffix, - 'post_content' => $html ? $html : '{{unknown}}', - 'post_status' => 'publish', - 'post_type' => 'oembed_cache', - ) ) ); + $insert_post_args = array( + 'post_name' => $key_suffix, + 'post_status' => 'publish', + 'post_type' => 'oembed_cache', + ); + + if ( $html ) { + if ( $cached_post_id ) { + wp_update_post( wp_slash( array( + 'ID' => $cached_post_id, + 'post_content' => $html, + ) ) ); + } else { + wp_insert_post( wp_slash( array_merge( + $insert_post_args, + array( + 'post_content' => $html, + ) + ) ) ); + } + } elseif ( ! $cache ) { + wp_insert_post( wp_slash( array_merge( + $insert_post_args, + array( + 'post_content' => '{{unknown}}', + ) + ) ) ); + } if ( $has_kses ) { kses_init_filters(); diff --git a/tests/phpunit/tests/oembed/WpEmbed.php b/tests/phpunit/tests/oembed/WpEmbed.php index 877a79e4b1..a62edc9a13 100644 --- a/tests/phpunit/tests/oembed/WpEmbed.php +++ b/tests/phpunit/tests/oembed/WpEmbed.php @@ -272,6 +272,40 @@ class Tests_WP_Embed extends WP_UnitTestCase { $this->assertEquals( '{{unknown}}', $post_content ); } + /** + * Test that parsing an embed shortcode should cause oembed_cache to be updated. + * + * @ticket 42310 + */ + public function test_shortcode_should_update_custom_post() { + add_filter( 'oembed_ttl', '__return_zero' ); + + $url = 'https://example.com/'; + $embedded = 'Embedded content'; + $key_suffix = md5( $url . serialize( wp_embed_defaults( $url ) ) ); + + add_filter( 'pre_oembed_result', '__return_empty_string' ); + $this->wp_embed->shortcode( array(), $url ); + remove_filter( 'pre_oembed_result', '__return_empty_string' ); + + $oembed_post_id = $this->wp_embed->find_oembed_post_id( $key_suffix ); + + $this->assertSame( '{{unknown}}', get_post( $oembed_post_id )->post_content ); + + $previous_usecache = $this->wp_embed->usecache; + $this->wp_embed->usecache = false; + + // The update cannot be empty because empty responses won't overwrite the cache. + add_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) ); + $this->wp_embed->shortcode( array(), $url ); + remove_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) ); + + $this->assertSame( $embedded, get_post( $oembed_post_id )->post_content ); + + $this->wp_embed->usecache = $previous_usecache; + remove_filter( 'oembed_ttl', '__return_zero' ); + } + public function test_shortcode_should_get_url_from_src_attribute() { $url = 'http://example.com/embed/foo'; $actual = $this->wp_embed->shortcode( array( 'src' => $url ) );