Embeds: Cache oEmbeds in an `oembed_cache` custom post type instead of postmeta when there is no global `$post`.

Add processing of embeds to rich Text widget.

Props swissspidy, westonruter, ocean90, johnbillion.
See #40854, #39994, #40935.
Fixes #34115.

Built from https://develop.svn.wordpress.org/trunk@41651


git-svn-id: http://core.svn.wordpress.org/trunk@41485 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Weston Ruter 2017-09-30 01:15:48 +00:00
parent e90b5ee669
commit e0c3951b08
3 changed files with 147 additions and 60 deletions

View File

@ -30,12 +30,14 @@ class WP_Embed {
public function __construct() { public function __construct() {
// Hack to get the [embed] shortcode to run before wpautop() // Hack to get the [embed] shortcode to run before wpautop()
add_filter( 'the_content', array( $this, 'run_shortcode' ), 8 ); add_filter( 'the_content', array( $this, 'run_shortcode' ), 8 );
add_filter( 'widget_text_content', array( $this, 'run_shortcode' ), 8 );
// Shortcode placeholder for strip_shortcodes() // Shortcode placeholder for strip_shortcodes()
add_shortcode( 'embed', '__return_false' ); add_shortcode( 'embed', '__return_false' );
// Attempts to embed all URLs in a post // Attempts to embed all URLs in a post
add_filter( 'the_content', array( $this, 'autoembed' ), 8 ); add_filter( 'the_content', array( $this, 'autoembed' ), 8 );
add_filter( 'widget_text_content', array( $this, 'autoembed' ), 8 );
// After a post is saved, cache oEmbed items via Ajax // After a post is saved, cache oEmbed items via Ajax
add_action( 'edit_form_advanced', array( $this, 'maybe_run_ajax_cache' ) ); add_action( 'edit_form_advanced', array( $this, 'maybe_run_ajax_cache' ) );
@ -185,13 +187,13 @@ class WP_Embed {
} }
$post_ID = ( ! empty( $post->ID ) ) ? $post->ID : null; $post_ID = ( ! empty( $post->ID ) ) ? $post->ID : null;
if ( ! empty( $this->post_ID ) ) // Potentially set by WP_Embed::cache_oembed()
// Potentially set by WP_Embed::cache_oembed().
if ( ! empty( $this->post_ID ) ) {
$post_ID = $this->post_ID; $post_ID = $this->post_ID;
}
// Unknown URL format. Let oEmbed have a go. // Check for a cached result (stored as custom post or in the post meta).
if ( $post_ID ) {
// Check for a cached result (stored in the post meta)
$key_suffix = md5( $url . serialize( $attr ) ); $key_suffix = md5( $url . serialize( $attr ) );
$cachekey = '_oembed_' . $key_suffix; $cachekey = '_oembed_' . $key_suffix;
$cachekey_time = '_oembed_time_' . $key_suffix; $cachekey_time = '_oembed_time_' . $key_suffix;
@ -208,19 +210,32 @@ class WP_Embed {
*/ */
$ttl = apply_filters( 'oembed_ttl', DAY_IN_SECONDS, $url, $attr, $post_ID ); $ttl = apply_filters( 'oembed_ttl', DAY_IN_SECONDS, $url, $attr, $post_ID );
$cache = '';
$cache_time = 0;
$cached_post_id = $this->find_oembed_post_id( $key_suffix );
if ( $post_ID ) {
$cache = get_post_meta( $post_ID, $cachekey, true ); $cache = get_post_meta( $post_ID, $cachekey, true );
$cache_time = get_post_meta( $post_ID, $cachekey_time, true ); $cache_time = get_post_meta( $post_ID, $cachekey_time, true );
if ( ! $cache_time ) { if ( ! $cache_time ) {
$cache_time = 0; $cache_time = 0;
} }
} elseif ( $cached_post_id ) {
$cached_post = get_post( $cached_post_id );
$cache = $cached_post->post_content;
$cache_time = strtotime( $cached_post->post_modified_gmt );
}
$cached_recently = ( time() - $cache_time ) < $ttl; $cached_recently = ( time() - $cache_time ) < $ttl;
if ( $this->usecache || $cached_recently ) { if ( $this->usecache || $cached_recently ) {
// Failures are cached. Serve one if we're using the cache. // Failures are cached. Serve one if we're using the cache.
if ( '{{unknown}}' === $cache ) if ( '{{unknown}}' === $cache ) {
return $this->maybe_make_link( $url ); return $this->maybe_make_link( $url );
}
if ( ! empty( $cache ) ) { if ( ! empty( $cache ) ) {
/** /**
@ -249,25 +264,43 @@ class WP_Embed {
* *
* @param bool $enable Whether to enable `<link>` tag discovery. Default true. * @param bool $enable Whether to enable `<link>` tag discovery. Default true.
*/ */
$attr['discover'] = ( apply_filters( 'embed_oembed_discover', true ) ); $attr['discover'] = apply_filters( 'embed_oembed_discover', true );
// Use oEmbed to get the HTML // Use oEmbed to get the HTML.
$html = wp_oembed_get( $url, $attr ); $html = wp_oembed_get( $url, $attr );
// Maybe cache the result if ( $post_ID ) {
if ( $html ) { if ( $html ) {
update_post_meta( $post_ID, $cachekey, $html ); update_post_meta( $post_ID, $cachekey, $html );
update_post_meta( $post_ID, $cachekey_time, time() ); update_post_meta( $post_ID, $cachekey_time, time() );
} elseif ( ! $cache ) { } elseif ( ! $cache ) {
update_post_meta( $post_ID, $cachekey, '{{unknown}}' ); update_post_meta( $post_ID, $cachekey, '{{unknown}}' );
} }
} else {
$has_kses = false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' );
// If there was a result, return it if ( $has_kses ) {
// Prevent KSES from corrupting JSON in post_content.
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',
) ) );
if ( $has_kses ) {
kses_init_filters();
}
}
// If there was a result, return it.
if ( $html ) { if ( $html ) {
/** This filter is documented in wp-includes/class-wp-embed.php */ /** This filter is documented in wp-includes/class-wp-embed.php */
return apply_filters( 'embed_oembed_html', $html, $url, $attr, $post_ID ); return apply_filters( 'embed_oembed_html', $html, $url, $attr, $post_ID );
} }
}
// Still unknown // Still unknown
return $this->maybe_make_link( $url ); return $this->maybe_make_link( $url );
@ -382,4 +415,43 @@ class WP_Embed {
*/ */
return apply_filters( 'embed_maybe_make_link', $output, $url ); return apply_filters( 'embed_maybe_make_link', $output, $url );
} }
/**
* Find the oEmbed cache post ID for a given cache key.
*
* @since 4.9.0
*
* @param string $cache_key oEmbed cache key.
* @return int|null Post ID on success, null on failure.
*/
public function find_oembed_post_id( $cache_key ) {
$cache_group = 'oembed_cache_post';
$oembed_post_id = wp_cache_get( $cache_key, $cache_group );
if ( $oembed_post_id && 'oembed_cache' === get_post_type( $oembed_post_id ) ) {
return $oembed_post_id;
}
$oembed_post_query = new WP_Query( array(
'post_type' => 'oembed_cache',
'post_status' => 'publish',
'name' => $cache_key,
'posts_per_page' => 1,
'no_found_rows' => true,
'cache_results' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'lazy_load_term_meta' => false,
) );
if ( ! empty( $oembed_post_query->posts ) ) {
// Note: 'fields'=>'ids' is not being used in order to cache the post object as it will be needed.
$oembed_post_id = $oembed_post_query->posts[0]->ID;
wp_cache_set( $cache_key, $oembed_post_id, $cache_group );
return $oembed_post_id;
}
return null;
}
} }

View File

@ -195,6 +195,21 @@ function create_initial_post_types() {
), ),
) ); ) );
register_post_type( 'oembed_cache', array(
'labels' => array(
'name' => __( 'oEmbed Responses' ),
'singular_name' => __( 'oEmbed Response' ),
),
'public' => false,
'hierarchical' => false,
'rewrite' => false,
'query_var' => false,
'delete_with_user' => false,
'can_export' => false,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'supports' => array(),
) );
register_post_status( 'publish', array( register_post_status( 'publish', array(
'label' => _x( 'Published', 'post status' ), 'label' => _x( 'Published', 'post status' ),
'public' => true, 'public' => true,

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.9-alpha-41650'; $wp_version = '4.9-alpha-41651';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.