wp/wp-includes/class-wp-embed.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
     4  *
     4  *
     5  * @package WordPress
     5  * @package WordPress
     6  * @subpackage Embed
     6  * @subpackage Embed
     7  * @since 2.9.0
     7  * @since 2.9.0
     8  */
     8  */
       
     9 #[AllowDynamicProperties]
     9 class WP_Embed {
    10 class WP_Embed {
    10 	public $handlers = array();
    11 	public $handlers = array();
    11 	public $post_ID;
    12 	public $post_ID;
    12 	public $usecache      = true;
    13 	public $usecache      = true;
    13 	public $linkifunknown = true;
    14 	public $linkifunknown = true;
   209 		$rawattr = $attr;
   210 		$rawattr = $attr;
   210 		$attr    = wp_parse_args( $attr, wp_embed_defaults( $url ) );
   211 		$attr    = wp_parse_args( $attr, wp_embed_defaults( $url ) );
   211 
   212 
   212 		$this->last_attr = $attr;
   213 		$this->last_attr = $attr;
   213 
   214 
   214 		// KSES converts & into & and we need to undo this.
   215 		/*
   215 		// See https://core.trac.wordpress.org/ticket/11311
   216 		 * KSES converts & into & and we need to undo this.
       
   217 		 * See https://core.trac.wordpress.org/ticket/11311
       
   218 		 */
   216 		$url = str_replace( '&', '&', $url );
   219 		$url = str_replace( '&', '&', $url );
   217 
   220 
   218 		// Look for known internal handlers.
   221 		// Look for known internal handlers.
   219 		$embed_handler_html = $this->get_embed_handler_html( $rawattr, $url );
   222 		$embed_handler_html = $this->get_embed_handler_html( $rawattr, $url );
   220 		if ( false !== $embed_handler_html ) {
   223 		if ( false !== $embed_handler_html ) {
   221 			return $embed_handler_html;
   224 			return $embed_handler_html;
   222 		}
   225 		}
   223 
   226 
   224 		$post_ID = ( ! empty( $post->ID ) ) ? $post->ID : null;
   227 		$post_id = ( ! empty( $post->ID ) ) ? $post->ID : null;
   225 
   228 
   226 		// Potentially set by WP_Embed::cache_oembed().
   229 		// Potentially set by WP_Embed::cache_oembed().
   227 		if ( ! empty( $this->post_ID ) ) {
   230 		if ( ! empty( $this->post_ID ) ) {
   228 			$post_ID = $this->post_ID;
   231 			$post_id = $this->post_ID;
   229 		}
   232 		}
   230 
   233 
   231 		// Check for a cached result (stored as custom post or in the post meta).
   234 		// Check for a cached result (stored as custom post or in the post meta).
   232 		$key_suffix    = md5( $url . serialize( $attr ) );
   235 		$key_suffix    = md5( $url . serialize( $attr ) );
   233 		$cachekey      = '_oembed_' . $key_suffix;
   236 		$cachekey      = '_oembed_' . $key_suffix;
   239 		 * @since 4.0.0
   242 		 * @since 4.0.0
   240 		 *
   243 		 *
   241 		 * @param int    $time    Time to live (in seconds).
   244 		 * @param int    $time    Time to live (in seconds).
   242 		 * @param string $url     The attempted embed URL.
   245 		 * @param string $url     The attempted embed URL.
   243 		 * @param array  $attr    An array of shortcode attributes.
   246 		 * @param array  $attr    An array of shortcode attributes.
   244 		 * @param int    $post_ID Post ID.
   247 		 * @param int    $post_id Post ID.
   245 		 */
   248 		 */
   246 		$ttl = apply_filters( 'oembed_ttl', DAY_IN_SECONDS, $url, $attr, $post_ID );
   249 		$ttl = apply_filters( 'oembed_ttl', DAY_IN_SECONDS, $url, $attr, $post_id );
   247 
   250 
   248 		$cache      = '';
   251 		$cache      = '';
   249 		$cache_time = 0;
   252 		$cache_time = 0;
   250 
   253 
   251 		$cached_post_id = $this->find_oembed_post_id( $key_suffix );
   254 		$cached_post_id = $this->find_oembed_post_id( $key_suffix );
   252 
   255 
   253 		if ( $post_ID ) {
   256 		if ( $post_id ) {
   254 			$cache      = get_post_meta( $post_ID, $cachekey, true );
   257 			$cache      = get_post_meta( $post_id, $cachekey, true );
   255 			$cache_time = get_post_meta( $post_ID, $cachekey_time, true );
   258 			$cache_time = get_post_meta( $post_id, $cachekey_time, true );
   256 
   259 
   257 			if ( ! $cache_time ) {
   260 			if ( ! $cache_time ) {
   258 				$cache_time = 0;
   261 				$cache_time = 0;
   259 			}
   262 			}
   260 		} elseif ( $cached_post_id ) {
   263 		} elseif ( $cached_post_id ) {
   281 				 * @see WP_Embed::shortcode()
   284 				 * @see WP_Embed::shortcode()
   282 				 *
   285 				 *
   283 				 * @param string|false $cache   The cached HTML result, stored in post meta.
   286 				 * @param string|false $cache   The cached HTML result, stored in post meta.
   284 				 * @param string       $url     The attempted embed URL.
   287 				 * @param string       $url     The attempted embed URL.
   285 				 * @param array        $attr    An array of shortcode attributes.
   288 				 * @param array        $attr    An array of shortcode attributes.
   286 				 * @param int          $post_ID Post ID.
   289 				 * @param int          $post_id Post ID.
   287 				 */
   290 				 */
   288 				return apply_filters( 'embed_oembed_html', $cache, $url, $attr, $post_ID );
   291 				return apply_filters( 'embed_oembed_html', $cache, $url, $attr, $post_id );
   289 			}
   292 			}
   290 		}
   293 		}
   291 
   294 
   292 		/**
   295 		/**
   293 		 * Filters whether to inspect the given URL for discoverable link tags.
   296 		 * Filters whether to inspect the given URL for discoverable link tags.
   302 		$attr['discover'] = apply_filters( 'embed_oembed_discover', true );
   305 		$attr['discover'] = apply_filters( 'embed_oembed_discover', true );
   303 
   306 
   304 		// Use oEmbed to get the HTML.
   307 		// Use oEmbed to get the HTML.
   305 		$html = wp_oembed_get( $url, $attr );
   308 		$html = wp_oembed_get( $url, $attr );
   306 
   309 
   307 		if ( $post_ID ) {
   310 		if ( $post_id ) {
   308 			if ( $html ) {
   311 			if ( $html ) {
   309 				update_post_meta( $post_ID, $cachekey, $html );
   312 				update_post_meta( $post_id, $cachekey, $html );
   310 				update_post_meta( $post_ID, $cachekey_time, time() );
   313 				update_post_meta( $post_id, $cachekey_time, time() );
   311 			} elseif ( ! $cache ) {
   314 			} elseif ( ! $cache ) {
   312 				update_post_meta( $post_ID, $cachekey, '{{unknown}}' );
   315 				update_post_meta( $post_id, $cachekey, '{{unknown}}' );
   313 			}
   316 			}
   314 		} else {
   317 		} else {
   315 			$has_kses = false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' );
   318 			$has_kses = false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' );
   316 
   319 
   317 			if ( $has_kses ) {
   320 			if ( $has_kses ) {
   366 		}
   369 		}
   367 
   370 
   368 		// If there was a result, return it.
   371 		// If there was a result, return it.
   369 		if ( $html ) {
   372 		if ( $html ) {
   370 			/** This filter is documented in wp-includes/class-wp-embed.php */
   373 			/** This filter is documented in wp-includes/class-wp-embed.php */
   371 			return apply_filters( 'embed_oembed_html', $html, $url, $attr, $post_ID );
   374 			return apply_filters( 'embed_oembed_html', $html, $url, $attr, $post_id );
   372 		}
   375 		}
   373 
   376 
   374 		// Still unknown.
   377 		// Still unknown.
   375 		return $this->maybe_make_link( $url );
   378 		return $this->maybe_make_link( $url );
   376 	}
   379 	}
   377 
   380 
   378 	/**
   381 	/**
   379 	 * Deletes all oEmbed caches. Unused by core as of 4.0.0.
   382 	 * Deletes all oEmbed caches. Unused by core as of 4.0.0.
   380 	 *
   383 	 *
   381 	 * @param int $post_ID Post ID to delete the caches for.
   384 	 * @param int $post_id Post ID to delete the caches for.
   382 	 */
   385 	 */
   383 	public function delete_oembed_caches( $post_ID ) {
   386 	public function delete_oembed_caches( $post_id ) {
   384 		$post_metas = get_post_custom_keys( $post_ID );
   387 		$post_metas = get_post_custom_keys( $post_id );
   385 		if ( empty( $post_metas ) ) {
   388 		if ( empty( $post_metas ) ) {
   386 			return;
   389 			return;
   387 		}
   390 		}
   388 
   391 
   389 		foreach ( $post_metas as $post_meta_key ) {
   392 		foreach ( $post_metas as $post_meta_key ) {
   390 			if ( '_oembed_' === substr( $post_meta_key, 0, 8 ) ) {
   393 			if ( str_starts_with( $post_meta_key, '_oembed_' ) ) {
   391 				delete_post_meta( $post_ID, $post_meta_key );
   394 				delete_post_meta( $post_id, $post_meta_key );
   392 			}
   395 			}
   393 		}
   396 		}
   394 	}
   397 	}
   395 
   398 
   396 	/**
   399 	/**
   397 	 * Triggers a caching of all oEmbed results.
   400 	 * Triggers a caching of all oEmbed results.
   398 	 *
   401 	 *
   399 	 * @param int $post_ID Post ID to do the caching for.
   402 	 * @param int $post_id Post ID to do the caching for.
   400 	 */
   403 	 */
   401 	public function cache_oembed( $post_ID ) {
   404 	public function cache_oembed( $post_id ) {
   402 		$post = get_post( $post_ID );
   405 		$post = get_post( $post_id );
   403 
   406 
   404 		$post_types = get_post_types( array( 'show_ui' => true ) );
   407 		$post_types = get_post_types( array( 'show_ui' => true ) );
   405 
   408 
   406 		/**
   409 		/**
   407 		 * Filters the array of post types to cache oEmbed results for.
   410 		 * Filters the array of post types to cache oEmbed results for.