<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[41651] trunk: Embeds: Cache oEmbeds in an `oembed_cache` custom post type instead of postmeta when there is no global `$post`.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/41651">41651</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/41651","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>westonruter</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2017-09-30 01:14:34 +0000 (Sat, 30 Sep 2017)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/40854">#40854</a>, <a href="https://core.trac.wordpress.org/ticket/39994">#39994</a>, <a href="https://core.trac.wordpress.org/ticket/40935">#40935</a>.
Fixes <a href="https://core.trac.wordpress.org/ticket/34115">#34115</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpembedphp">trunk/src/wp-includes/class-wp-embed.php</a></li>
<li><a href="#trunksrcwpincludespostphp">trunk/src/wp-includes/post.php</a></li>
<li><a href="#trunktestsphpunittestsexternalhttpoembedphp">trunk/tests/phpunit/tests/external-http/oembed.php</a></li>
<li><a href="#trunktestsphpunittestsoembedWpEmbedphp">trunk/tests/phpunit/tests/oembed/WpEmbed.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpembedphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-embed.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-embed.php 2017-09-29 22:03:15 UTC (rev 41650)
+++ trunk/src/wp-includes/class-wp-embed.php 2017-09-30 01:14:34 UTC (rev 41651)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -30,12 +30,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> public function __construct() {
</span><span class="cx" style="display: block; padding: 0 10px"> // Hack to get the [embed] shortcode to run before wpautop()
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'the_content', array( $this, 'run_shortcode' ), 8 );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ add_filter( 'widget_text_content', array( $this, 'run_shortcode' ), 8 );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Shortcode placeholder for strip_shortcodes()
</span><span class="cx" style="display: block; padding: 0 10px"> add_shortcode( 'embed', '__return_false' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Attempts to embed all URLs in a post
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'the_content', array( $this, 'autoembed' ), 8 );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ add_filter( 'widget_text_content', array( $this, 'autoembed' ), 8 );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // After a post is saved, cache oEmbed items via Ajax
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'edit_form_advanced', array( $this, 'maybe_run_ajax_cache' ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -185,90 +187,121 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $post_ID = ( ! empty( $post->ID ) ) ? $post->ID : null;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! empty( $this->post_ID ) ) // Potentially set by WP_Embed::cache_oembed()
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ // Potentially set by WP_Embed::cache_oembed().
+ if ( ! empty( $this->post_ID ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $post_ID = $this->post_ID;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Unknown URL format. Let oEmbed have a go.
- if ( $post_ID ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Check for a cached result (stored as custom post or in the post meta).
+ $key_suffix = md5( $url . serialize( $attr ) );
+ $cachekey = '_oembed_' . $key_suffix;
+ $cachekey_time = '_oembed_time_' . $key_suffix;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Check for a cached result (stored in the post meta)
- $key_suffix = md5( $url . serialize( $attr ) );
- $cachekey = '_oembed_' . $key_suffix;
- $cachekey_time = '_oembed_time_' . $key_suffix;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Filters the oEmbed TTL value (time to live).
+ *
+ * @since 4.0.0
+ *
+ * @param int $time Time to live (in seconds).
+ * @param string $url The attempted embed URL.
+ * @param array $attr An array of shortcode attributes.
+ * @param int $post_ID Post ID.
+ */
+ $ttl = apply_filters( 'oembed_ttl', DAY_IN_SECONDS, $url, $attr, $post_ID );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- /**
- * Filters the oEmbed TTL value (time to live).
- *
- * @since 4.0.0
- *
- * @param int $time Time to live (in seconds).
- * @param string $url The attempted embed URL.
- * @param array $attr An array of shortcode attributes.
- * @param int $post_ID Post ID.
- */
- $ttl = apply_filters( 'oembed_ttl', DAY_IN_SECONDS, $url, $attr, $post_ID );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $cache = '';
+ $cache_time = 0;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $cached_post_id = $this->find_oembed_post_id( $key_suffix );
+
+ if ( $post_ID ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $cache = get_post_meta( $post_ID, $cachekey, true );
</span><span class="cx" style="display: block; padding: 0 10px"> $cache_time = get_post_meta( $post_ID, $cachekey_time, true );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! $cache_time ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $cache_time = 0;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ } elseif ( $cached_post_id ) {
+ $cached_post = get_post( $cached_post_id );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $cached_recently = ( time() - $cache_time ) < $ttl;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $cache = $cached_post->post_content;
+ $cache_time = strtotime( $cached_post->post_modified_gmt );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( $this->usecache || $cached_recently ) {
- // Failures are cached. Serve one if we're using the cache.
- if ( '{{unknown}}' === $cache )
- return $this->maybe_make_link( $url );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $cached_recently = ( time() - $cache_time ) < $ttl;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! empty( $cache ) ) {
- /**
- * Filters the cached oEmbed HTML.
- *
- * @since 2.9.0
- *
- * @see WP_Embed::shortcode()
- *
- * @param mixed $cache The cached HTML result, stored in post meta.
- * @param string $url The attempted embed URL.
- * @param array $attr An array of shortcode attributes.
- * @param int $post_ID Post ID.
- */
- return apply_filters( 'embed_oembed_html', $cache, $url, $attr, $post_ID );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $this->usecache || $cached_recently ) {
+ // Failures are cached. Serve one if we're using the cache.
+ if ( '{{unknown}}' === $cache ) {
+ return $this->maybe_make_link( $url );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- /**
- * Filters whether to inspect the given URL for discoverable link tags.
- *
- * @since 2.9.0
- * @since 4.4.0 The default value changed to true.
- *
- * @see WP_oEmbed::discover()
- *
- * @param bool $enable Whether to enable `<link>` tag discovery. Default true.
- */
- $attr['discover'] = ( apply_filters( 'embed_oembed_discover', true ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! empty( $cache ) ) {
+ /**
+ * Filters the cached oEmbed HTML.
+ *
+ * @since 2.9.0
+ *
+ * @see WP_Embed::shortcode()
+ *
+ * @param mixed $cache The cached HTML result, stored in post meta.
+ * @param string $url The attempted embed URL.
+ * @param array $attr An array of shortcode attributes.
+ * @param int $post_ID Post ID.
+ */
+ return apply_filters( 'embed_oembed_html', $cache, $url, $attr, $post_ID );
+ }
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Use oEmbed to get the HTML
- $html = wp_oembed_get( $url, $attr );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Filters whether to inspect the given URL for discoverable link tags.
+ *
+ * @since 2.9.0
+ * @since 4.4.0 The default value changed to true.
+ *
+ * @see WP_oEmbed::discover()
+ *
+ * @param bool $enable Whether to enable `<link>` tag discovery. Default true.
+ */
+ $attr['discover'] = apply_filters( 'embed_oembed_discover', true );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Maybe cache the result
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Use oEmbed to get the HTML.
+ $html = wp_oembed_get( $url, $attr );
+
+ if ( $post_ID ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( $html ) {
</span><span class="cx" style="display: block; padding: 0 10px"> update_post_meta( $post_ID, $cachekey, $html );
</span><span class="cx" style="display: block; padding: 0 10px"> update_post_meta( $post_ID, $cachekey_time, time() );
</span><span class="cx" style="display: block; padding: 0 10px"> } elseif ( ! $cache ) {
</span><span class="cx" style="display: block; padding: 0 10px"> update_post_meta( $post_ID, $cachekey, '{{unknown}}' );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ } else {
+ $has_kses = false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // If there was a result, return it
- if ( $html ) {
- /** This filter is documented in wp-includes/class-wp-embed.php */
- return apply_filters( 'embed_oembed_html', $html, $url, $attr, $post_ID );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $has_kses ) {
+ // Prevent KSES from corrupting JSON in post_content.
+ kses_remove_filters();
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ 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();
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // If there was a result, return it.
+ if ( $html ) {
+ /** This filter is documented in wp-includes/class-wp-embed.php */
+ return apply_filters( 'embed_oembed_html', $html, $url, $attr, $post_ID );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Still unknown
</span><span class="cx" style="display: block; padding: 0 10px"> return $this->maybe_make_link( $url );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -382,4 +415,43 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> return apply_filters( 'embed_maybe_make_link', $output, $url );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * 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;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunksrcwpincludespostphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/post.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/post.php 2017-09-29 22:03:15 UTC (rev 41650)
+++ trunk/src/wp-includes/post.php 2017-09-30 01:14:34 UTC (rev 41651)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -195,6 +195,21 @@
</span><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="cx" style="display: block; padding: 0 10px"> ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 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(),
+ ) );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> register_post_status( 'publish', array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'label' => _x( 'Published', 'post status' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'public' => true,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3003,7 +3018,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @type string $guid Global Unique ID for referencing the post. Default empty.
</span><span class="cx" style="display: block; padding: 0 10px"> * @type array $post_category Array of category names, slugs, or IDs.
</span><span class="cx" style="display: block; padding: 0 10px"> * Defaults to value of the 'default_category' option.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @type array $tags_input Array of tag names, slugs, or IDs. Default empty.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @type array $tags_input Array of tag names, slugs, or IDs. Default empty.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @type array $tax_input Array of taxonomy terms keyed by their taxonomy name. Default empty.
</span><span class="cx" style="display: block; padding: 0 10px"> * @type array $meta_input Array of post meta values keyed by their post meta key. Default empty.
</span><span class="cx" style="display: block; padding: 0 10px"> * }
</span></span></pre></div>
<a id="trunktestsphpunittestsexternalhttpoembedphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/external-http/oembed.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/external-http/oembed.php 2017-09-29 22:03:15 UTC (rev 41650)
+++ trunk/tests/phpunit/tests/external-http/oembed.php 2017-09-30 01:14:34 UTC (rev 41651)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -35,12 +35,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> function test_youtube_embed_url() {
</span><span class="cx" style="display: block; padding: 0 10px"> global $wp_embed;
</span><span class="cx" style="display: block; padding: 0 10px"> $out = $wp_embed->autoembed( 'https://www.youtube.com/embed/QcIy9NiNbmo' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertContains( 'https://youtube.com/watch?v=QcIy9NiNbmo', $out );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertContains( 'https://www.youtube.com/embed/QcIy9NiNbmo?feature=oembed', $out );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> function test_youtube_v_url() {
</span><span class="cx" style="display: block; padding: 0 10px"> global $wp_embed;
</span><span class="cx" style="display: block; padding: 0 10px"> $out = $wp_embed->autoembed( 'https://www.youtube.com/v/QcIy9NiNbmo' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertContains( 'https://youtube.com/watch?v=QcIy9NiNbmo', $out );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertContains( 'https://www.youtube.com/embed/QcIy9NiNbmo?feature=oembed', $out );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-}
</del><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of file
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+}
</ins></span></pre></div>
<a id="trunktestsphpunittestsoembedWpEmbedphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/oembed/WpEmbed.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/oembed/WpEmbed.php 2017-09-29 22:03:15 UTC (rev 41650)
+++ trunk/tests/phpunit/tests/oembed/WpEmbed.php 2017-09-30 01:14:34 UTC (rev 41651)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -161,48 +161,115 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertNotEmpty( get_post_meta( $post_id, $cachekey_time, true ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_shortcode_should_cache_data_in_post_meta_for_known_post() {
- $GLOBALS['post'] = $this->factory()->post->create_and_get();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_shortcode_should_get_cached_data_from_post_meta_for_known_post() {
+ global $post;
+
+ $post = $this->factory()->post->create_and_get();
</ins><span class="cx" style="display: block; padding: 0 10px"> $url = 'https://example.com/';
</span><span class="cx" style="display: block; padding: 0 10px"> $expected = '<b>Embedded content</b>';
</span><span class="cx" style="display: block; padding: 0 10px"> $key_suffix = md5( $url . serialize( wp_embed_defaults( $url ) ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $cachekey = '_oembed_' . $key_suffix;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $cachekey_time = '_oembed_time_' . $key_suffix;
</del><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ add_post_meta( $post->ID, $cachekey, $expected );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $actual = $this->wp_embed->shortcode( array(), $url );
</span><span class="cx" style="display: block; padding: 0 10px"> remove_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertEquals( $expected, $actual );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $actual_2 = $this->wp_embed->shortcode( array(), $url );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertEquals( $expected, get_post_meta( $GLOBALS['post']->ID, $cachekey, true ) );
- $this->assertNotEmpty( get_post_meta( $GLOBALS['post']->ID, $cachekey_time, true ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $cached = get_post_meta( $post->ID, $cachekey, true );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Result should be cached.
- $actual = $this->wp_embed->shortcode( array(), $url );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Cleanup.
+ unset( $post );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( $expected, $actual );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( $expected, $actual_2 );
+ $this->assertEquals( $expected, $cached );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_shortcode_should_cache_failure_in_post_meta_for_known_post() {
- $GLOBALS['post'] = $this->factory()->post->create_and_get();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_shortcode_should_get_cached_failure_from_post_meta_for_known_post() {
+ global $post;
+
+ $post = $this->factory()->post->create_and_get();
</ins><span class="cx" style="display: block; padding: 0 10px"> $url = 'https://example.com/';
</span><span class="cx" style="display: block; padding: 0 10px"> $expected = '<a href="' . esc_url( $url ) . '">' . esc_html( $url ) . '</a>';
</span><span class="cx" style="display: block; padding: 0 10px"> $key_suffix = md5( $url . serialize( wp_embed_defaults( $url ) ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $cachekey = '_oembed_' . $key_suffix;
</span><span class="cx" style="display: block; padding: 0 10px"> $cachekey_time = '_oembed_time_' . $key_suffix;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ add_post_meta( $post->ID, $cachekey, '{{unknown}}' );
+ add_post_meta( $post->ID, $cachekey_time, 0 );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'pre_oembed_result', '__return_empty_string' );
</span><span class="cx" style="display: block; padding: 0 10px"> $actual = $this->wp_embed->shortcode( array(), $url );
</span><span class="cx" style="display: block; padding: 0 10px"> remove_filter( 'pre_oembed_result', '__return_empty_string' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Result should be cached.
+ $actual_2 = $this->wp_embed->shortcode( array(), $url );
+
+ $cached = get_post_meta( $post->ID, $cachekey, true );
+ $cached_time = get_post_meta( $post->ID, $cachekey_time, true );
+
+ // Cleanup.
+ unset( $post );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( $expected, $actual );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( '{{unknown}}', $cached );
+ $this->assertEmpty( $cached_time );
+ $this->assertEquals( $expected, $actual_2 );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertEquals( '{{unknown}}', get_post_meta( $GLOBALS['post']->ID, $cachekey, true ) );
- $this->assertEmpty( get_post_meta( $GLOBALS['post']->ID, $cachekey_time, true ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * @ticket 34115
+ */
+ public function test_shortcode_should_cache_data_in_custom_post() {
+ $url = 'https://example.com/';
+ $expected = '<b>Embedded content</b>';
+ $key_suffix = md5( $url . serialize( wp_embed_defaults( $url ) ) );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ add_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
+ $actual = $this->wp_embed->shortcode( array(), $url );
+ remove_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
+
+ $oembed_post_id = $this->wp_embed->find_oembed_post_id( $key_suffix );
+ $post_content = get_post( $oembed_post_id )->post_content;
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Result should be cached.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $actual_2 = $this->wp_embed->shortcode( array(), $url );
+
+ wp_delete_post( $oembed_post_id );
+
+ $this->assertNotNull( $oembed_post_id );
+ $this->assertEquals( $expected, $post_content );
+ $this->assertEquals( $expected, $actual );
+ $this->assertEquals( $expected, $actual_2 );
+ }
+
+ /**
+ * @ticket 34115
+ */
+ public function test_shortcode_should_cache_failure_in_custom_post() {
+ $url = 'https://example.com/';
+ $expected = '<a href="' . esc_url( $url ) . '">' . esc_html( $url ) . '</a>';
+ $key_suffix = md5( $url . serialize( wp_embed_defaults( $url ) ) );
+
+ add_filter( 'pre_oembed_result', '__return_empty_string' );
</ins><span class="cx" style="display: block; padding: 0 10px"> $actual = $this->wp_embed->shortcode( array(), $url );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ remove_filter( 'pre_oembed_result', '__return_empty_string' );
+
+ $oembed_post_id = $this->wp_embed->find_oembed_post_id( $key_suffix );
+ $post_content = get_post( $oembed_post_id )->post_content;
+
+ // Result should be cached.
+ $actual_2 = $this->wp_embed->shortcode( array(), $url );
+
+ wp_delete_post( $oembed_post_id );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( $expected, $actual );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( $expected, $actual_2 );
+ $this->assertNotNull( $oembed_post_id );
+ $this->assertEquals( '{{unknown}}', $post_content );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_shortcode_should_get_url_from_src_attribute() {
</span></span></pre>
</div>
</div>
</body>
</html>