wp/wp-includes/class-wp-embed.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
--- a/wp/wp-includes/class-wp-embed.php	Tue Oct 22 16:11:46 2019 +0200
+++ b/wp/wp-includes/class-wp-embed.php	Tue Dec 15 13:49:49 2020 +0100
@@ -28,18 +28,18 @@
 	 * Constructor
 	 */
 	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( 'widget_text_content', array( $this, 'run_shortcode' ), 8 );
 
-		// Shortcode placeholder for strip_shortcodes()
+		// Shortcode placeholder for strip_shortcodes().
 		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( '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_page_form', array( $this, 'maybe_run_ajax_cache' ) );
 	}
@@ -59,16 +59,16 @@
 	public function run_shortcode( $content ) {
 		global $shortcode_tags;
 
-		// Back up current registered shortcodes and clear them all out
+		// Back up current registered shortcodes and clear them all out.
 		$orig_shortcode_tags = $shortcode_tags;
 		remove_all_shortcodes();
 
 		add_shortcode( 'embed', array( $this, 'shortcode' ) );
 
-		// Do the shortcode (only the [embed] one is registered)
+		// Do the shortcode (only the [embed] one is registered).
 		$content = do_shortcode( $content, true );
 
-		// Put the original shortcodes back
+		// Put the original shortcodes back.
 		$shortcode_tags = $orig_shortcode_tags;
 
 		return $content;
@@ -101,10 +101,12 @@
 	 *
 	 * This function should probably also only be used for sites that do not support oEmbed.
 	 *
-	 * @param string $id An internal ID/name for the handler. Needs to be unique.
-	 * @param string $regex The regex that will be used to see if this handler should be used for a URL.
+	 * @param string   $id       An internal ID/name for the handler. Needs to be unique.
+	 * @param string   $regex    The regex that will be used to see if this handler should be used for a URL.
 	 * @param callable $callback The callback function that will be called if the regex is matched.
-	 * @param int $priority Optional. Used to specify the order in which the registered handlers will be tested (default: 10). Lower numbers correspond with earlier testing, and handlers with the same priority are tested in the order in which they were added to the action.
+	 * @param int      $priority Optional. Used to specify the order in which the registered handlers will be tested.
+	 *                           Lower numbers correspond with earlier testing, and handlers with the same priority are
+	 *                           tested in the order in which they were added to the action. Default 10.
 	 */
 	public function register_handler( $id, $regex, $callback, $priority = 10 ) {
 		$this->handlers[ $priority ][ $id ] = array(
@@ -118,21 +120,68 @@
 	 *
 	 * Do not use this function directly, use wp_embed_unregister_handler() instead.
 	 *
-	 * @param string $id The handler ID that should be removed.
-	 * @param int $priority Optional. The priority of the handler to be removed (default: 10).
+	 * @param string $id       The handler ID that should be removed.
+	 * @param int    $priority Optional. The priority of the handler to be removed (default: 10).
 	 */
 	public function unregister_handler( $id, $priority = 10 ) {
 		unset( $this->handlers[ $priority ][ $id ] );
 	}
 
 	/**
+	 * Returns embed HTML for a given URL from embed handlers.
+	 *
+	 * Attempts to convert a URL into embed HTML by checking the URL
+	 * against the regex of the registered embed handlers.
+	 *
+	 * @since 5.5.0
+	 *
+	 * @param array  $attr {
+	 *     Shortcode attributes. Optional.
+	 *
+	 *     @type int $width  Width of the embed in pixels.
+	 *     @type int $height Height of the embed in pixels.
+	 * }
+	 * @param string $url The URL attempting to be embedded.
+	 * @return string|false The embed HTML on success, false otherwise.
+	 */
+	public function get_embed_handler_html( $attr, $url ) {
+		$rawattr = $attr;
+		$attr    = wp_parse_args( $attr, wp_embed_defaults( $url ) );
+
+		ksort( $this->handlers );
+		foreach ( $this->handlers as $priority => $handlers ) {
+			foreach ( $handlers as $id => $handler ) {
+				if ( preg_match( $handler['regex'], $url, $matches ) && is_callable( $handler['callback'] ) ) {
+					$return = call_user_func( $handler['callback'], $matches, $attr, $url, $rawattr );
+					if ( false !== $return ) {
+						/**
+						 * Filters the returned embed HTML.
+						 *
+						 * @since 2.9.0
+						 *
+						 * @see WP_Embed::shortcode()
+						 *
+						 * @param string|false $return The HTML result of the shortcode, or false on failure.
+						 * @param string       $url    The embed URL.
+						 * @param array        $attr   An array of shortcode attributes.
+						 */
+						return apply_filters( 'embed_handler_html', $return, $url, $attr );
+					}
+				}
+			}
+		}
+
+		return false;
+	}
+
+	/**
 	 * The do_shortcode() callback function.
 	 *
 	 * Attempts to convert a URL into embed HTML. Starts by checking the URL against the regex of
 	 * the registered embed handlers. If none of the regex matches and it's enabled, then the URL
 	 * will be given to the WP_oEmbed class.
 	 *
-	 * @param array $attr {
+	 * @param array  $attr {
 	 *     Shortcode attributes. Optional.
 	 *
 	 *     @type int $width  Width of the embed in pixels.
@@ -161,31 +210,14 @@
 
 		$this->last_attr = $attr;
 
-		// kses converts & into & and we need to undo this
+		// KSES converts & into & and we need to undo this.
 		// See https://core.trac.wordpress.org/ticket/11311
 		$url = str_replace( '&', '&', $url );
 
-		// Look for known internal handlers
-		ksort( $this->handlers );
-		foreach ( $this->handlers as $priority => $handlers ) {
-			foreach ( $handlers as $id => $handler ) {
-				if ( preg_match( $handler['regex'], $url, $matches ) && is_callable( $handler['callback'] ) ) {
-					if ( false !== $return = call_user_func( $handler['callback'], $matches, $attr, $url, $rawattr ) ) {
-						/**
-						 * Filters the returned embed handler.
-						 *
-						 * @since 2.9.0
-						 *
-						 * @see WP_Embed::shortcode()
-						 *
-						 * @param mixed  $return The shortcode callback function to call.
-						 * @param string $url    The attempted embed URL.
-						 * @param array  $attr   An array of shortcode attributes.
-						 */
-						return apply_filters( 'embed_handler_html', $return, $url, $attr );
-					}
-				}
-			}
+		// Look for known internal handlers.
+		$embed_handler_html = $this->get_embed_handler_html( $rawattr, $url );
+		if ( false !== $embed_handler_html ) {
+			return $embed_handler_html;
 		}
 
 		$post_ID = ( ! empty( $post->ID ) ) ? $post->ID : null;
@@ -247,10 +279,10 @@
 				 *
 				 * @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.
+				 * @param string|false $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 );
 			}
@@ -338,7 +370,7 @@
 			return apply_filters( 'embed_oembed_html', $html, $url, $attr, $post_ID );
 		}
 
-		// Still unknown
+		// Still unknown.
 		return $this->maybe_make_link( $url );
 	}
 
@@ -354,7 +386,7 @@
 		}
 
 		foreach ( $post_metas as $post_meta_key ) {
-			if ( '_oembed_' == substr( $post_meta_key, 0, 8 ) ) {
+			if ( '_oembed_' === substr( $post_meta_key, 0, 8 ) ) {
 				delete_post_meta( $post_ID, $post_meta_key );
 			}
 		}
@@ -369,6 +401,7 @@
 		$post = get_post( $post_ID );
 
 		$post_types = get_post_types( array( 'show_ui' => true ) );
+
 		/**
 		 * Filters the array of post types to cache oEmbed results for.
 		 *
@@ -376,11 +409,13 @@
 		 *
 		 * @param string[] $post_types Array of post type names to cache oEmbed results for. Defaults to post types with `show_ui` set to true.
 		 */
-		if ( empty( $post->ID ) || ! in_array( $post->post_type, apply_filters( 'embed_cache_oembed_types', $post_types ) ) ) {
+		$cache_oembed_types = apply_filters( 'embed_cache_oembed_types', $post_types );
+
+		if ( empty( $post->ID ) || ! in_array( $post->post_type, $cache_oembed_types, true ) ) {
 			return;
 		}
 
-		// Trigger a caching
+		// Trigger a caching.
 		if ( ! empty( $post->post_content ) ) {
 			$this->post_ID  = $post->ID;
 			$this->usecache = false;
@@ -434,7 +469,7 @@
 	 * Conditionally makes a hyperlink based on an internal class variable.
 	 *
 	 * @param string $url URL to potentially be linked.
-	 * @return false|string Linked URL or the original URL. False if 'return_false_on_fail' is true.
+	 * @return string|false Linked URL or the original URL. False if 'return_false_on_fail' is true.
 	 */
 	public function maybe_make_link( $url ) {
 		if ( $this->return_false_on_fail ) {
@@ -485,7 +520,7 @@
 		);
 
 		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.
+			// 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 );