From 21f1342bc78c4346c058e2ec67fef6a73b0192e1 Mon Sep 17 00:00:00 2001
From: Danny de Haan <d.dehaan@haboes.nl>
Date: Thu, 1 Oct 2015 22:54:15 +0200
Subject: [PATCH] Added filters for term and author descriptions

---
 wp-includes/class-wp-embed.php | 131 +++++++++++++++++++----------------------
 1 file changed, 60 insertions(+), 71 deletions(-)

diff --git a/wp-includes/class-wp-embed.php b/wp-includes/class-wp-embed.php
index d4b1ded..38ba47b 100644
--- a/wp-includes/class-wp-embed.php
+++ b/wp-includes/class-wp-embed.php
@@ -27,6 +27,10 @@ class WP_Embed {
 		// Hack to get the [embed] shortcode to run before wpautop()
 		add_filter( 'the_content', array( $this, 'run_shortcode' ), 8 );
 
+		add_filter( 'term_description', array( $this, 'autoembed' ), 8 );
+
+		add_filter( 'get_the_author_description', array( $this, 'autoembed' ), 8 );
+
 		// Shortcode placeholder for strip_shortcodes()
 		add_shortcode( 'embed', '__return_false' );
 
@@ -35,7 +39,6 @@ class WP_Embed {
 
 		// After a post is saved, cache oEmbed items via AJAX
 		add_action( 'edit_form_advanced', array( $this, 'maybe_run_ajax_cache' ) );
-		add_action( 'edit_page_form', array( $this, 'maybe_run_ajax_cache' ) );
 	}
 
 	/**
@@ -132,6 +135,10 @@ class WP_Embed {
 	public function shortcode( $attr, $url = '' ) {
 		$post = get_post();
 
+		if ( null !== $post ) {
+			$post_ID = $post->ID;
+		}
+
 		if ( empty( $url ) && ! empty( $attr['src'] ) ) {
 			$url = $attr['src'];
 		}
@@ -174,61 +181,10 @@ class WP_Embed {
 			}
 		}
 
-		$post_ID = ( ! empty( $post->ID ) ) ? $post->ID : null;
-		if ( ! empty( $this->post_ID ) ) // Potentially set by WP_Embed::cache_oembed()
-			$post_ID = $this->post_ID;
-
-		// Unknown URL format. Let oEmbed have a go.
-		if ( $post_ID ) {
-
-			// 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;
-
-			/**
-			 * Filter 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 );
-
-			$cache = get_post_meta( $post_ID, $cachekey, true );
-			$cache_time = get_post_meta( $post_ID, $cachekey_time, true );
-
-			if ( ! $cache_time ) {
-				$cache_time = 0;
-			}
-
-			$cached_recently = ( time() - $cache_time ) < $ttl;
-
-			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 );
-
-				if ( ! empty( $cache ) ) {
-					/**
-					 * Filter 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 );
-				}
-			}
+		// Check for a cached result (stored in the post meta)
+		$cachekey = '_oembed_' . md5( $url . serialize( $attr ) );
 
+		if ( false === ( $cache = get_transient( $cachekey ) ) ) {
 			/**
 			 * Filter whether to inspect the given URL for discoverable link tags.
 			 *
@@ -238,24 +194,57 @@ class WP_Embed {
 			 *
 			 * @param bool $enable Whether to enable `<link>` tag discovery. Default false.
 			 */
-			$attr['discover'] = ( apply_filters( 'embed_oembed_discover', false ) && author_can( $post_ID, 'unfiltered_html' ) );
+			$attr['discover'] = ( apply_filters( 'embed_oembed_discover', false ) && author_can( ( isset( $post_ID ) ? $post_ID : 0 ), 'unfiltered_html' ) );
 
 			// Use oEmbed to get the HTML
-			$html = wp_oembed_get( $url, $attr );
-
-			// Maybe cache the result
-			if ( $html ) {
-				update_post_meta( $post_ID, $cachekey, $html );
-				update_post_meta( $post_ID, $cachekey_time, time() );
-			} elseif ( ! $cache ) {
-				update_post_meta( $post_ID, $cachekey, '{{unknown}}' );
-			}
+			$cache = wp_oembed_get( $url, $attr );
+		}
 
-			// 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 );
+		if ( $this->usecache || false !== $cache ) {
+			// Failures are cached. Serve one if we're using the cache.
+			if ( '{{unknown}}' === $cache )
+				return $this->maybe_make_link( $url );
+
+			if ( ! empty( $cache ) ) {
+				/**
+				 * Filter 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 when set, otherwise 0.
+				 */
+				$ttl = apply_filters( 'oembed_ttl', DAY_IN_SECONDS, $url, $attr, ( isset( $post_ID ) ? $post_ID : 0 ) );
+
+				/**
+				 * Set the oEmbed cache with the generated html
+				 * 
+				 * @since 4.4
+				 *
+				 * @param string $cachekey  The generated cachekey based on $url and $atts
+				 * @param string $cache     The generated oEmbed HTML
+				 * @param int    $ttl       The time to live for the transient
+				 */
+				set_transient( $cachekey, $cache, $ttl );
+
+				/**
+				 * Filter 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, ( isset( $post_ID ) ? $post_ID : 0 ) );
 			}
+		} else {
+			set_transient( $cachekey, '{{unknown}}', $ttl );
 		}
 
 		// Still unknown
@@ -272,7 +261,7 @@ class WP_Embed {
 		if ( empty($post_metas) )
 			return;
 
-		foreach ( $post_metas as $post_meta_key ) {
+		foreach( $post_metas as $post_meta_key ) {
 			if ( '_oembed_' == substr( $post_meta_key, 0, 8 ) )
 				delete_post_meta( $post_ID, $post_meta_key );
 		}
@@ -321,7 +310,7 @@ class WP_Embed {
 	public function autoembed( $content ) {
 		// Replace line breaks from all HTML elements with placeholders.
 		$content = wp_replace_in_html_tags( $content, array( "\n" => '<!-- wp-line-break -->' ) );
-
+		
 		// Find URLs that are on their own line.
 		$content = preg_replace_callback( '|^(\s*)(https?://[^\s"]+)(\s*)$|im', array( $this, 'autoembed_callback' ), $content );
 
-- 
2.3.2 (Apple Git-55)

