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
This commit is contained in:
parent
8d56eff073
commit
18a231bad6
|
@ -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.
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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 = '<b>Embedded content</b>';
|
||||
$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 ) );
|
||||
|
|
Loading…
Reference in New Issue