balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

class-wp-oembed.php (30321B)


      1 <?php
      2 /**
      3  * API for fetching the HTML to embed remote content based on a provided URL
      4  *
      5  * Used internally by the WP_Embed class, but is designed to be generic.
      6  *
      7  * @link https://wordpress.org/support/article/embeds/
      8  * @link http://oembed.com/
      9  *
     10  * @package WordPress
     11  * @subpackage oEmbed
     12  */
     13 
     14 /**
     15  * Core class used to implement oEmbed functionality.
     16  *
     17  * @since 2.9.0
     18  */
     19 class WP_oEmbed {
     20 
     21 	/**
     22 	 * A list of oEmbed providers.
     23 	 *
     24 	 * @since 2.9.0
     25 	 * @var array
     26 	 */
     27 	public $providers = array();
     28 
     29 	/**
     30 	 * A list of an early oEmbed providers.
     31 	 *
     32 	 * @since 4.0.0
     33 	 * @var array
     34 	 */
     35 	public static $early_providers = array();
     36 
     37 	/**
     38 	 * A list of private/protected methods, used for backward compatibility.
     39 	 *
     40 	 * @since 4.2.0
     41 	 * @var array
     42 	 */
     43 	private $compat_methods = array( '_fetch_with_format', '_parse_json', '_parse_xml', '_parse_xml_body' );
     44 
     45 	/**
     46 	 * Constructor.
     47 	 *
     48 	 * @since 2.9.0
     49 	 */
     50 	public function __construct() {
     51 		$host      = urlencode( home_url() );
     52 		$providers = array(
     53 			'#https?://((m|www)\.)?youtube\.com/watch.*#i' => array( 'https://www.youtube.com/oembed', true ),
     54 			'#https?://((m|www)\.)?youtube\.com/playlist.*#i' => array( 'https://www.youtube.com/oembed', true ),
     55 			'#https?://youtu\.be/.*#i'                     => array( 'https://www.youtube.com/oembed', true ),
     56 			'#https?://(.+\.)?vimeo\.com/.*#i'             => array( 'https://vimeo.com/api/oembed.{format}', true ),
     57 			'#https?://(www\.)?dailymotion\.com/.*#i'      => array( 'https://www.dailymotion.com/services/oembed', true ),
     58 			'#https?://dai\.ly/.*#i'                       => array( 'https://www.dailymotion.com/services/oembed', true ),
     59 			'#https?://(www\.)?flickr\.com/.*#i'           => array( 'https://www.flickr.com/services/oembed/', true ),
     60 			'#https?://flic\.kr/.*#i'                      => array( 'https://www.flickr.com/services/oembed/', true ),
     61 			'#https?://(.+\.)?smugmug\.com/.*#i'           => array( 'https://api.smugmug.com/services/oembed/', true ),
     62 			'#https?://(www\.)?scribd\.com/(doc|document)/.*#i' => array( 'https://www.scribd.com/services/oembed', true ),
     63 			'#https?://wordpress\.tv/.*#i'                 => array( 'https://wordpress.tv/oembed/', true ),
     64 			'#https?://(.+\.)?polldaddy\.com/.*#i'         => array( 'https://api.crowdsignal.com/oembed', true ),
     65 			'#https?://poll\.fm/.*#i'                      => array( 'https://api.crowdsignal.com/oembed', true ),
     66 			'#https?://(.+\.)?survey\.fm/.*#i'             => array( 'https://api.crowdsignal.com/oembed', true ),
     67 			'#https?://(www\.)?twitter\.com/\w{1,15}/status(es)?/.*#i' => array( 'https://publish.twitter.com/oembed', true ),
     68 			'#https?://(www\.)?twitter\.com/\w{1,15}$#i'   => array( 'https://publish.twitter.com/oembed', true ),
     69 			'#https?://(www\.)?twitter\.com/\w{1,15}/likes$#i' => array( 'https://publish.twitter.com/oembed', true ),
     70 			'#https?://(www\.)?twitter\.com/\w{1,15}/lists/.*#i' => array( 'https://publish.twitter.com/oembed', true ),
     71 			'#https?://(www\.)?twitter\.com/\w{1,15}/timelines/.*#i' => array( 'https://publish.twitter.com/oembed', true ),
     72 			'#https?://(www\.)?twitter\.com/i/moments/.*#i' => array( 'https://publish.twitter.com/oembed', true ),
     73 			'#https?://(www\.)?soundcloud\.com/.*#i'       => array( 'https://soundcloud.com/oembed', true ),
     74 			'#https?://(.+?\.)?slideshare\.net/.*#i'       => array( 'https://www.slideshare.net/api/oembed/2', true ),
     75 			'#https?://(open|play)\.spotify\.com/.*#i'     => array( 'https://embed.spotify.com/oembed/', true ),
     76 			'#https?://(.+\.)?imgur\.com/.*#i'             => array( 'https://api.imgur.com/oembed', true ),
     77 			'#https?://(www\.)?meetu(\.ps|p\.com)/.*#i'    => array( 'https://api.meetup.com/oembed', true ),
     78 			'#https?://(www\.)?issuu\.com/.+/docs/.+#i'    => array( 'https://issuu.com/oembed_wp', true ),
     79 			'#https?://(www\.)?mixcloud\.com/.*#i'         => array( 'https://www.mixcloud.com/oembed', true ),
     80 			'#https?://(www\.|embed\.)?ted\.com/talks/.*#i' => array( 'https://www.ted.com/services/v1/oembed.{format}', true ),
     81 			'#https?://(www\.)?(animoto|video214)\.com/play/.*#i' => array( 'https://animoto.com/oembeds/create', true ),
     82 			'#https?://(.+)\.tumblr\.com/post/.*#i'        => array( 'https://www.tumblr.com/oembed/1.0', true ),
     83 			'#https?://(www\.)?kickstarter\.com/projects/.*#i' => array( 'https://www.kickstarter.com/services/oembed', true ),
     84 			'#https?://kck\.st/.*#i'                       => array( 'https://www.kickstarter.com/services/oembed', true ),
     85 			'#https?://cloudup\.com/.*#i'                  => array( 'https://cloudup.com/oembed', true ),
     86 			'#https?://(www\.)?reverbnation\.com/.*#i'     => array( 'https://www.reverbnation.com/oembed', true ),
     87 			'#https?://videopress\.com/v/.*#'              => array( 'https://public-api.wordpress.com/oembed/?for=' . $host, true ),
     88 			'#https?://(www\.)?reddit\.com/r/[^/]+/comments/.*#i' => array( 'https://www.reddit.com/oembed', true ),
     89 			'#https?://(www\.)?speakerdeck\.com/.*#i'      => array( 'https://speakerdeck.com/oembed.{format}', true ),
     90 			'#https?://(www\.)?screencast\.com/.*#i'       => array( 'https://api.screencast.com/external/oembed', true ),
     91 			'#https?://([a-z0-9-]+\.)?amazon\.(com|com\.mx|com\.br|ca)/.*#i' => array( 'https://read.amazon.com/kp/api/oembed', true ),
     92 			'#https?://([a-z0-9-]+\.)?amazon\.(co\.uk|de|fr|it|es|in|nl|ru)/.*#i' => array( 'https://read.amazon.co.uk/kp/api/oembed', true ),
     93 			'#https?://([a-z0-9-]+\.)?amazon\.(co\.jp|com\.au)/.*#i' => array( 'https://read.amazon.com.au/kp/api/oembed', true ),
     94 			'#https?://([a-z0-9-]+\.)?amazon\.cn/.*#i'     => array( 'https://read.amazon.cn/kp/api/oembed', true ),
     95 			'#https?://(www\.)?a\.co/.*#i'                 => array( 'https://read.amazon.com/kp/api/oembed', true ),
     96 			'#https?://(www\.)?amzn\.to/.*#i'              => array( 'https://read.amazon.com/kp/api/oembed', true ),
     97 			'#https?://(www\.)?amzn\.eu/.*#i'              => array( 'https://read.amazon.co.uk/kp/api/oembed', true ),
     98 			'#https?://(www\.)?amzn\.in/.*#i'              => array( 'https://read.amazon.in/kp/api/oembed', true ),
     99 			'#https?://(www\.)?amzn\.asia/.*#i'            => array( 'https://read.amazon.com.au/kp/api/oembed', true ),
    100 			'#https?://(www\.)?z\.cn/.*#i'                 => array( 'https://read.amazon.cn/kp/api/oembed', true ),
    101 			'#https?://www\.someecards\.com/.+-cards/.+#i' => array( 'https://www.someecards.com/v2/oembed/', true ),
    102 			'#https?://www\.someecards\.com/usercards/viewcard/.+#i' => array( 'https://www.someecards.com/v2/oembed/', true ),
    103 			'#https?://some\.ly\/.+#i'                     => array( 'https://www.someecards.com/v2/oembed/', true ),
    104 			'#https?://(www\.)?tiktok\.com/.*/video/.*#i'  => array( 'https://www.tiktok.com/oembed', true ),
    105 		);
    106 
    107 		if ( ! empty( self::$early_providers['add'] ) ) {
    108 			foreach ( self::$early_providers['add'] as $format => $data ) {
    109 				$providers[ $format ] = $data;
    110 			}
    111 		}
    112 
    113 		if ( ! empty( self::$early_providers['remove'] ) ) {
    114 			foreach ( self::$early_providers['remove'] as $format ) {
    115 				unset( $providers[ $format ] );
    116 			}
    117 		}
    118 
    119 		self::$early_providers = array();
    120 
    121 		/**
    122 		 * Filters the list of sanctioned oEmbed providers.
    123 		 *
    124 		 * Since WordPress 4.4, oEmbed discovery is enabled for all users and allows embedding of sanitized
    125 		 * iframes. The providers in this list are sanctioned, meaning they are trusted and allowed to
    126 		 * embed any content, such as iframes, videos, JavaScript, and arbitrary HTML.
    127 		 *
    128 		 * Supported providers:
    129 		 *
    130 		 * |   Provider   |                     Flavor                |  Since  |
    131 		 * | ------------ | ----------------------------------------- | ------- |
    132 		 * | Dailymotion  | dailymotion.com                           | 2.9.0   |
    133 		 * | Flickr       | flickr.com                                | 2.9.0   |
    134 		 * | Scribd       | scribd.com                                | 2.9.0   |
    135 		 * | Vimeo        | vimeo.com                                 | 2.9.0   |
    136 		 * | WordPress.tv | wordpress.tv                              | 2.9.0   |
    137 		 * | YouTube      | youtube.com/watch                         | 2.9.0   |
    138 		 * | Crowdsignal  | polldaddy.com                             | 3.0.0   |
    139 		 * | SmugMug      | smugmug.com                               | 3.0.0   |
    140 		 * | YouTube      | youtu.be                                  | 3.0.0   |
    141 		 * | Twitter      | twitter.com                               | 3.4.0   |
    142 		 * | Slideshare   | slideshare.net                            | 3.5.0   |
    143 		 * | SoundCloud   | soundcloud.com                            | 3.5.0   |
    144 		 * | Dailymotion  | dai.ly                                    | 3.6.0   |
    145 		 * | Flickr       | flic.kr                                   | 3.6.0   |
    146 		 * | Spotify      | spotify.com                               | 3.6.0   |
    147 		 * | Imgur        | imgur.com                                 | 3.9.0   |
    148 		 * | Meetup.com   | meetup.com                                | 3.9.0   |
    149 		 * | Meetup.com   | meetu.ps                                  | 3.9.0   |
    150 		 * | Animoto      | animoto.com                               | 4.0.0   |
    151 		 * | Animoto      | video214.com                              | 4.0.0   |
    152 		 * | Issuu        | issuu.com                                 | 4.0.0   |
    153 		 * | Mixcloud     | mixcloud.com                              | 4.0.0   |
    154 		 * | Crowdsignal  | poll.fm                                   | 4.0.0   |
    155 		 * | TED          | ted.com                                   | 4.0.0   |
    156 		 * | YouTube      | youtube.com/playlist                      | 4.0.0   |
    157 		 * | Tumblr       | tumblr.com                                | 4.2.0   |
    158 		 * | Kickstarter  | kickstarter.com                           | 4.2.0   |
    159 		 * | Kickstarter  | kck.st                                    | 4.2.0   |
    160 		 * | Cloudup      | cloudup.com                               | 4.3.0   |
    161 		 * | ReverbNation | reverbnation.com                          | 4.4.0   |
    162 		 * | VideoPress   | videopress.com                            | 4.4.0   |
    163 		 * | Reddit       | reddit.com                                | 4.4.0   |
    164 		 * | Speaker Deck | speakerdeck.com                           | 4.4.0   |
    165 		 * | Twitter      | twitter.com/timelines                     | 4.5.0   |
    166 		 * | Twitter      | twitter.com/moments                       | 4.5.0   |
    167 		 * | Twitter      | twitter.com/user                          | 4.7.0   |
    168 		 * | Twitter      | twitter.com/likes                         | 4.7.0   |
    169 		 * | Twitter      | twitter.com/lists                         | 4.7.0   |
    170 		 * | Screencast   | screencast.com                            | 4.8.0   |
    171 		 * | Amazon       | amazon.com (com.mx, com.br, ca)           | 4.9.0   |
    172 		 * | Amazon       | amazon.de (fr, it, es, in, nl, ru, co.uk) | 4.9.0   |
    173 		 * | Amazon       | amazon.co.jp (com.au)                     | 4.9.0   |
    174 		 * | Amazon       | amazon.cn                                 | 4.9.0   |
    175 		 * | Amazon       | a.co                                      | 4.9.0   |
    176 		 * | Amazon       | amzn.to (eu, in, asia)                    | 4.9.0   |
    177 		 * | Amazon       | z.cn                                      | 4.9.0   |
    178 		 * | Someecards   | someecards.com                            | 4.9.0   |
    179 		 * | Someecards   | some.ly                                   | 4.9.0   |
    180 		 * | Crowdsignal  | survey.fm                                 | 5.1.0   |
    181 		 * | TikTok       | tiktok.com                                | 5.4.0   |
    182 		 *
    183 		 * No longer supported providers:
    184 		 *
    185 		 * |   Provider   |        Flavor        |   Since   |  Removed  |
    186 		 * | ------------ | -------------------- | --------- | --------- |
    187 		 * | Qik          | qik.com              | 2.9.0     | 3.9.0     |
    188 		 * | Viddler      | viddler.com          | 2.9.0     | 4.0.0     |
    189 		 * | Revision3    | revision3.com        | 2.9.0     | 4.2.0     |
    190 		 * | Blip         | blip.tv              | 2.9.0     | 4.4.0     |
    191 		 * | Rdio         | rdio.com             | 3.6.0     | 4.4.1     |
    192 		 * | Rdio         | rd.io                | 3.6.0     | 4.4.1     |
    193 		 * | Vine         | vine.co              | 4.1.0     | 4.9.0     |
    194 		 * | Photobucket  | photobucket.com      | 2.9.0     | 5.1.0     |
    195 		 * | Funny or Die | funnyordie.com       | 3.0.0     | 5.1.0     |
    196 		 * | CollegeHumor | collegehumor.com     | 4.0.0     | 5.3.1     |
    197 		 * | Hulu         | hulu.com             | 2.9.0     | 5.5.0     |
    198 		 * | Instagram    | instagram.com        | 3.5.0     | 5.5.2     |
    199 		 * | Instagram    | instagr.am           | 3.5.0     | 5.5.2     |
    200 		 * | Instagram TV | instagram.com        | 5.1.0     | 5.5.2     |
    201 		 * | Instagram TV | instagr.am           | 5.1.0     | 5.5.2     |
    202 		 * | Facebook     | facebook.com         | 4.7.0     | 5.5.2     |
    203 		 *
    204 		 * @see wp_oembed_add_provider()
    205 		 *
    206 		 * @since 2.9.0
    207 		 *
    208 		 * @param array[] $providers An array of arrays containing data about popular oEmbed providers.
    209 		 */
    210 		$this->providers = apply_filters( 'oembed_providers', $providers );
    211 
    212 		// Fix any embeds that contain new lines in the middle of the HTML which breaks wpautop().
    213 		add_filter( 'oembed_dataparse', array( $this, '_strip_newlines' ), 10, 3 );
    214 	}
    215 
    216 	/**
    217 	 * Exposes private/protected methods for backward compatibility.
    218 	 *
    219 	 * @since 4.0.0
    220 	 *
    221 	 * @param string $name      Method to call.
    222 	 * @param array  $arguments Arguments to pass when calling.
    223 	 * @return mixed|false Return value of the callback, false otherwise.
    224 	 */
    225 	public function __call( $name, $arguments ) {
    226 		if ( in_array( $name, $this->compat_methods, true ) ) {
    227 			return $this->$name( ...$arguments );
    228 		}
    229 		return false;
    230 	}
    231 
    232 	/**
    233 	 * Takes a URL and returns the corresponding oEmbed provider's URL, if there is one.
    234 	 *
    235 	 * @since 4.0.0
    236 	 *
    237 	 * @see WP_oEmbed::discover()
    238 	 *
    239 	 * @param string       $url  The URL to the content.
    240 	 * @param string|array $args {
    241 	 *     Optional. Additional provider arguments. Default empty.
    242 	 *
    243 	 *     @type bool $discover Optional. Determines whether to attempt to discover link tags
    244 	 *                          at the given URL for an oEmbed provider when the provider URL
    245 	 *                          is not found in the built-in providers list. Default true.
    246 	 * }
    247 	 * @return string|false The oEmbed provider URL on success, false on failure.
    248 	 */
    249 	public function get_provider( $url, $args = '' ) {
    250 		$args = wp_parse_args( $args );
    251 
    252 		$provider = false;
    253 
    254 		if ( ! isset( $args['discover'] ) ) {
    255 			$args['discover'] = true;
    256 		}
    257 
    258 		foreach ( $this->providers as $matchmask => $data ) {
    259 			list( $providerurl, $regex ) = $data;
    260 
    261 			// Turn the asterisk-type provider URLs into regex.
    262 			if ( ! $regex ) {
    263 				$matchmask = '#' . str_replace( '___wildcard___', '(.+)', preg_quote( str_replace( '*', '___wildcard___', $matchmask ), '#' ) ) . '#i';
    264 				$matchmask = preg_replace( '|^#http\\\://|', '#https?\://', $matchmask );
    265 			}
    266 
    267 			if ( preg_match( $matchmask, $url ) ) {
    268 				$provider = str_replace( '{format}', 'json', $providerurl ); // JSON is easier to deal with than XML.
    269 				break;
    270 			}
    271 		}
    272 
    273 		if ( ! $provider && $args['discover'] ) {
    274 			$provider = $this->discover( $url );
    275 		}
    276 
    277 		return $provider;
    278 	}
    279 
    280 	/**
    281 	 * Adds an oEmbed provider.
    282 	 *
    283 	 * The provider is added just-in-time when wp_oembed_add_provider() is called before
    284 	 * the {@see 'plugins_loaded'} hook.
    285 	 *
    286 	 * The just-in-time addition is for the benefit of the {@see 'oembed_providers'} filter.
    287 	 *
    288 	 * @since 4.0.0
    289 	 *
    290 	 * @see wp_oembed_add_provider()
    291 	 *
    292 	 * @param string $format   Format of URL that this provider can handle. You can use
    293 	 *                         asterisks as wildcards.
    294 	 * @param string $provider The URL to the oEmbed provider..
    295 	 * @param bool   $regex    Optional. Whether the $format parameter is in a regex format.
    296 	 *                         Default false.
    297 	 */
    298 	public static function _add_provider_early( $format, $provider, $regex = false ) {
    299 		if ( empty( self::$early_providers['add'] ) ) {
    300 			self::$early_providers['add'] = array();
    301 		}
    302 
    303 		self::$early_providers['add'][ $format ] = array( $provider, $regex );
    304 	}
    305 
    306 	/**
    307 	 * Removes an oEmbed provider.
    308 	 *
    309 	 * The provider is removed just-in-time when wp_oembed_remove_provider() is called before
    310 	 * the {@see 'plugins_loaded'} hook.
    311 	 *
    312 	 * The just-in-time removal is for the benefit of the {@see 'oembed_providers'} filter.
    313 	 *
    314 	 * @since 4.0.0
    315 	 *
    316 	 * @see wp_oembed_remove_provider()
    317 	 *
    318 	 * @param string $format The format of URL that this provider can handle. You can use
    319 	 *                       asterisks as wildcards.
    320 	 */
    321 	public static function _remove_provider_early( $format ) {
    322 		if ( empty( self::$early_providers['remove'] ) ) {
    323 			self::$early_providers['remove'] = array();
    324 		}
    325 
    326 		self::$early_providers['remove'][] = $format;
    327 	}
    328 
    329 	/**
    330 	 * Takes a URL and attempts to return the oEmbed data.
    331 	 *
    332 	 * @see WP_oEmbed::fetch()
    333 	 *
    334 	 * @since 4.8.0
    335 	 *
    336 	 * @param string       $url  The URL to the content that should be attempted to be embedded.
    337 	 * @param string|array $args Optional. Additional arguments for retrieving embed HTML.
    338 	 *                           See wp_oembed_get() for accepted arguments. Default empty.
    339 	 * @return object|false The result in the form of an object on success, false on failure.
    340 	 */
    341 	public function get_data( $url, $args = '' ) {
    342 		$args = wp_parse_args( $args );
    343 
    344 		$provider = $this->get_provider( $url, $args );
    345 
    346 		if ( ! $provider ) {
    347 			return false;
    348 		}
    349 
    350 		$data = $this->fetch( $provider, $url, $args );
    351 
    352 		if ( false === $data ) {
    353 			return false;
    354 		}
    355 
    356 		return $data;
    357 	}
    358 
    359 	/**
    360 	 * The do-it-all function that takes a URL and attempts to return the HTML.
    361 	 *
    362 	 * @see WP_oEmbed::fetch()
    363 	 * @see WP_oEmbed::data2html()
    364 	 *
    365 	 * @since 2.9.0
    366 	 *
    367 	 * @param string       $url  The URL to the content that should be attempted to be embedded.
    368 	 * @param string|array $args Optional. Additional arguments for retrieving embed HTML.
    369 	 *                           See wp_oembed_get() for accepted arguments. Default empty.
    370 	 * @return string|false The UNSANITIZED (and potentially unsafe) HTML that should be used to embed
    371 	 *                      on success, false on failure.
    372 	 */
    373 	public function get_html( $url, $args = '' ) {
    374 		/**
    375 		 * Filters the oEmbed result before any HTTP requests are made.
    376 		 *
    377 		 * This allows one to short-circuit the default logic, perhaps by
    378 		 * replacing it with a routine that is more optimal for your setup.
    379 		 *
    380 		 * Returning a non-null value from the filter will effectively short-circuit retrieval
    381 		 * and return the passed value instead.
    382 		 *
    383 		 * @since 4.5.3
    384 		 *
    385 		 * @param null|string  $result The UNSANITIZED (and potentially unsafe) HTML that should be used to embed.
    386 		 *                             Default null to continue retrieving the result.
    387 		 * @param string       $url    The URL to the content that should be attempted to be embedded.
    388 		 * @param string|array $args   Optional. Additional arguments for retrieving embed HTML.
    389 		 *                             See wp_oembed_get() for accepted arguments. Default empty.
    390 		 */
    391 		$pre = apply_filters( 'pre_oembed_result', null, $url, $args );
    392 
    393 		if ( null !== $pre ) {
    394 			return $pre;
    395 		}
    396 
    397 		$data = $this->get_data( $url, $args );
    398 
    399 		if ( false === $data ) {
    400 			return false;
    401 		}
    402 
    403 		/**
    404 		 * Filters the HTML returned by the oEmbed provider.
    405 		 *
    406 		 * @since 2.9.0
    407 		 *
    408 		 * @param string|false $data The returned oEmbed HTML (false if unsafe).
    409 		 * @param string       $url  URL of the content to be embedded.
    410 		 * @param string|array $args Optional. Additional arguments for retrieving embed HTML.
    411 		 *                           See wp_oembed_get() for accepted arguments. Default empty.
    412 		 */
    413 		return apply_filters( 'oembed_result', $this->data2html( $data, $url ), $url, $args );
    414 	}
    415 
    416 	/**
    417 	 * Attempts to discover link tags at the given URL for an oEmbed provider.
    418 	 *
    419 	 * @since 2.9.0
    420 	 *
    421 	 * @param string $url The URL that should be inspected for discovery `<link>` tags.
    422 	 * @return string|false The oEmbed provider URL on success, false on failure.
    423 	 */
    424 	public function discover( $url ) {
    425 		$providers = array();
    426 		$args      = array(
    427 			'limit_response_size' => 153600, // 150 KB
    428 		);
    429 
    430 		/**
    431 		 * Filters oEmbed remote get arguments.
    432 		 *
    433 		 * @since 4.0.0
    434 		 *
    435 		 * @see WP_Http::request()
    436 		 *
    437 		 * @param array  $args oEmbed remote get arguments.
    438 		 * @param string $url  URL to be inspected.
    439 		 */
    440 		$args = apply_filters( 'oembed_remote_get_args', $args, $url );
    441 
    442 		// Fetch URL content.
    443 		$request = wp_safe_remote_get( $url, $args );
    444 		$html    = wp_remote_retrieve_body( $request );
    445 		if ( $html ) {
    446 
    447 			/**
    448 			 * Filters the link types that contain oEmbed provider URLs.
    449 			 *
    450 			 * @since 2.9.0
    451 			 *
    452 			 * @param string[] $format Array of oEmbed link types. Accepts 'application/json+oembed',
    453 			 *                         'text/xml+oembed', and 'application/xml+oembed' (incorrect,
    454 			 *                         used by at least Vimeo).
    455 			 */
    456 			$linktypes = apply_filters(
    457 				'oembed_linktypes',
    458 				array(
    459 					'application/json+oembed' => 'json',
    460 					'text/xml+oembed'         => 'xml',
    461 					'application/xml+oembed'  => 'xml',
    462 				)
    463 			);
    464 
    465 			// Strip <body>.
    466 			$html_head_end = stripos( $html, '</head>' );
    467 			if ( $html_head_end ) {
    468 				$html = substr( $html, 0, $html_head_end );
    469 			}
    470 
    471 			// Do a quick check.
    472 			$tagfound = false;
    473 			foreach ( $linktypes as $linktype => $format ) {
    474 				if ( stripos( $html, $linktype ) ) {
    475 					$tagfound = true;
    476 					break;
    477 				}
    478 			}
    479 
    480 			if ( $tagfound && preg_match_all( '#<link([^<>]+)/?>#iU', $html, $links ) ) {
    481 				foreach ( $links[1] as $link ) {
    482 					$atts = shortcode_parse_atts( $link );
    483 
    484 					if ( ! empty( $atts['type'] ) && ! empty( $linktypes[ $atts['type'] ] ) && ! empty( $atts['href'] ) ) {
    485 						$providers[ $linktypes[ $atts['type'] ] ] = htmlspecialchars_decode( $atts['href'] );
    486 
    487 						// Stop here if it's JSON (that's all we need).
    488 						if ( 'json' === $linktypes[ $atts['type'] ] ) {
    489 							break;
    490 						}
    491 					}
    492 				}
    493 			}
    494 		}
    495 
    496 		// JSON is preferred to XML.
    497 		if ( ! empty( $providers['json'] ) ) {
    498 			return $providers['json'];
    499 		} elseif ( ! empty( $providers['xml'] ) ) {
    500 			return $providers['xml'];
    501 		} else {
    502 			return false;
    503 		}
    504 	}
    505 
    506 	/**
    507 	 * Connects to a oEmbed provider and returns the result.
    508 	 *
    509 	 * @since 2.9.0
    510 	 *
    511 	 * @param string       $provider The URL to the oEmbed provider.
    512 	 * @param string       $url      The URL to the content that is desired to be embedded.
    513 	 * @param string|array $args     Optional. Additional arguments for retrieving embed HTML.
    514 	 *                               See wp_oembed_get() for accepted arguments. Default empty.
    515 	 * @return object|false The result in the form of an object on success, false on failure.
    516 	 */
    517 	public function fetch( $provider, $url, $args = '' ) {
    518 		$args = wp_parse_args( $args, wp_embed_defaults( $url ) );
    519 
    520 		$provider = add_query_arg( 'maxwidth', (int) $args['width'], $provider );
    521 		$provider = add_query_arg( 'maxheight', (int) $args['height'], $provider );
    522 		$provider = add_query_arg( 'url', urlencode( $url ), $provider );
    523 		$provider = add_query_arg( 'dnt', 1, $provider );
    524 
    525 		/**
    526 		 * Filters the oEmbed URL to be fetched.
    527 		 *
    528 		 * @since 2.9.0
    529 		 * @since 4.9.0 The `dnt` (Do Not Track) query parameter was added to all oEmbed provider URLs.
    530 		 *
    531 		 * @param string $provider URL of the oEmbed provider.
    532 		 * @param string $url      URL of the content to be embedded.
    533 		 * @param array  $args     Optional. Additional arguments for retrieving embed HTML.
    534 		 *                         See wp_oembed_get() for accepted arguments. Default empty.
    535 		 */
    536 		$provider = apply_filters( 'oembed_fetch_url', $provider, $url, $args );
    537 
    538 		foreach ( array( 'json', 'xml' ) as $format ) {
    539 			$result = $this->_fetch_with_format( $provider, $format );
    540 			if ( is_wp_error( $result ) && 'not-implemented' === $result->get_error_code() ) {
    541 				continue;
    542 			}
    543 			return ( $result && ! is_wp_error( $result ) ) ? $result : false;
    544 		}
    545 		return false;
    546 	}
    547 
    548 	/**
    549 	 * Fetches result from an oEmbed provider for a specific format and complete provider URL
    550 	 *
    551 	 * @since 3.0.0
    552 	 *
    553 	 * @param string $provider_url_with_args URL to the provider with full arguments list (url, maxheight, etc.)
    554 	 * @param string $format                 Format to use.
    555 	 * @return object|false|WP_Error The result in the form of an object on success, false on failure.
    556 	 */
    557 	private function _fetch_with_format( $provider_url_with_args, $format ) {
    558 		$provider_url_with_args = add_query_arg( 'format', $format, $provider_url_with_args );
    559 
    560 		/** This filter is documented in wp-includes/class-wp-oembed.php */
    561 		$args = apply_filters( 'oembed_remote_get_args', array(), $provider_url_with_args );
    562 
    563 		$response = wp_safe_remote_get( $provider_url_with_args, $args );
    564 		if ( 501 == wp_remote_retrieve_response_code( $response ) ) {
    565 			return new WP_Error( 'not-implemented' );
    566 		}
    567 		$body = wp_remote_retrieve_body( $response );
    568 		if ( ! $body ) {
    569 			return false;
    570 		}
    571 		$parse_method = "_parse_$format";
    572 		return $this->$parse_method( $body );
    573 	}
    574 
    575 	/**
    576 	 * Parses a json response body.
    577 	 *
    578 	 * @since 3.0.0
    579 	 *
    580 	 * @param string $response_body
    581 	 * @return object|false
    582 	 */
    583 	private function _parse_json( $response_body ) {
    584 		$data = json_decode( trim( $response_body ) );
    585 		return ( $data && is_object( $data ) ) ? $data : false;
    586 	}
    587 
    588 	/**
    589 	 * Parses an XML response body.
    590 	 *
    591 	 * @since 3.0.0
    592 	 *
    593 	 * @param string $response_body
    594 	 * @return object|false
    595 	 */
    596 	private function _parse_xml( $response_body ) {
    597 		if ( ! function_exists( 'libxml_disable_entity_loader' ) ) {
    598 			return false;
    599 		}
    600 
    601 		if ( PHP_VERSION_ID < 80000 ) {
    602 			// This function has been deprecated in PHP 8.0 because in libxml 2.9.0, external entity loading
    603 			// is disabled by default, so this function is no longer needed to protect against XXE attacks.
    604 			// phpcs:ignore PHPCompatibility.FunctionUse.RemovedFunctions.libxml_disable_entity_loaderDeprecated
    605 			$loader = libxml_disable_entity_loader( true );
    606 		}
    607 
    608 		$errors = libxml_use_internal_errors( true );
    609 
    610 		$return = $this->_parse_xml_body( $response_body );
    611 
    612 		libxml_use_internal_errors( $errors );
    613 
    614 		if ( PHP_VERSION_ID < 80000 && isset( $loader ) ) {
    615 			// phpcs:ignore PHPCompatibility.FunctionUse.RemovedFunctions.libxml_disable_entity_loaderDeprecated
    616 			libxml_disable_entity_loader( $loader );
    617 		}
    618 
    619 		return $return;
    620 	}
    621 
    622 	/**
    623 	 * Serves as a helper function for parsing an XML response body.
    624 	 *
    625 	 * @since 3.6.0
    626 	 *
    627 	 * @param string $response_body
    628 	 * @return stdClass|false
    629 	 */
    630 	private function _parse_xml_body( $response_body ) {
    631 		if ( ! function_exists( 'simplexml_import_dom' ) || ! class_exists( 'DOMDocument', false ) ) {
    632 			return false;
    633 		}
    634 
    635 		$dom     = new DOMDocument;
    636 		$success = $dom->loadXML( $response_body );
    637 		if ( ! $success ) {
    638 			return false;
    639 		}
    640 
    641 		if ( isset( $dom->doctype ) ) {
    642 			return false;
    643 		}
    644 
    645 		foreach ( $dom->childNodes as $child ) {
    646 			if ( XML_DOCUMENT_TYPE_NODE === $child->nodeType ) {
    647 				return false;
    648 			}
    649 		}
    650 
    651 		$xml = simplexml_import_dom( $dom );
    652 		if ( ! $xml ) {
    653 			return false;
    654 		}
    655 
    656 		$return = new stdClass;
    657 		foreach ( $xml as $key => $value ) {
    658 			$return->$key = (string) $value;
    659 		}
    660 
    661 		return $return;
    662 	}
    663 
    664 	/**
    665 	 * Converts a data object from WP_oEmbed::fetch() and returns the HTML.
    666 	 *
    667 	 * @since 2.9.0
    668 	 *
    669 	 * @param object $data A data object result from an oEmbed provider.
    670 	 * @param string $url  The URL to the content that is desired to be embedded.
    671 	 * @return string|false The HTML needed to embed on success, false on failure.
    672 	 */
    673 	public function data2html( $data, $url ) {
    674 		if ( ! is_object( $data ) || empty( $data->type ) ) {
    675 			return false;
    676 		}
    677 
    678 		$return = false;
    679 
    680 		switch ( $data->type ) {
    681 			case 'photo':
    682 				if ( empty( $data->url ) || empty( $data->width ) || empty( $data->height ) ) {
    683 					break;
    684 				}
    685 				if ( ! is_string( $data->url ) || ! is_numeric( $data->width ) || ! is_numeric( $data->height ) ) {
    686 					break;
    687 				}
    688 
    689 				$title  = ! empty( $data->title ) && is_string( $data->title ) ? $data->title : '';
    690 				$return = '<a href="' . esc_url( $url ) . '"><img src="' . esc_url( $data->url ) . '" alt="' . esc_attr( $title ) . '" width="' . esc_attr( $data->width ) . '" height="' . esc_attr( $data->height ) . '" /></a>';
    691 				break;
    692 
    693 			case 'video':
    694 			case 'rich':
    695 				if ( ! empty( $data->html ) && is_string( $data->html ) ) {
    696 					$return = $data->html;
    697 				}
    698 				break;
    699 
    700 			case 'link':
    701 				if ( ! empty( $data->title ) && is_string( $data->title ) ) {
    702 					$return = '<a href="' . esc_url( $url ) . '">' . esc_html( $data->title ) . '</a>';
    703 				}
    704 				break;
    705 
    706 			default:
    707 				$return = false;
    708 		}
    709 
    710 		/**
    711 		 * Filters the returned oEmbed HTML.
    712 		 *
    713 		 * Use this filter to add support for custom data types, or to filter the result.
    714 		 *
    715 		 * @since 2.9.0
    716 		 *
    717 		 * @param string $return The returned oEmbed HTML.
    718 		 * @param object $data   A data object result from an oEmbed provider.
    719 		 * @param string $url    The URL of the content to be embedded.
    720 		 */
    721 		return apply_filters( 'oembed_dataparse', $return, $data, $url );
    722 	}
    723 
    724 	/**
    725 	 * Strips any new lines from the HTML.
    726 	 *
    727 	 * @since 2.9.0 as strip_scribd_newlines()
    728 	 * @since 3.0.0
    729 	 *
    730 	 * @param string $html Existing HTML.
    731 	 * @param object $data Data object from WP_oEmbed::data2html()
    732 	 * @param string $url The original URL passed to oEmbed.
    733 	 * @return string Possibly modified $html
    734 	 */
    735 	public function _strip_newlines( $html, $data, $url ) {
    736 		if ( false === strpos( $html, "\n" ) ) {
    737 			return $html;
    738 		}
    739 
    740 		$count     = 1;
    741 		$found     = array();
    742 		$token     = '__PRE__';
    743 		$search    = array( "\t", "\n", "\r", ' ' );
    744 		$replace   = array( '__TAB__', '__NL__', '__CR__', '__SPACE__' );
    745 		$tokenized = str_replace( $search, $replace, $html );
    746 
    747 		preg_match_all( '#(<pre[^>]*>.+?</pre>)#i', $tokenized, $matches, PREG_SET_ORDER );
    748 		foreach ( $matches as $i => $match ) {
    749 			$tag_html  = str_replace( $replace, $search, $match[0] );
    750 			$tag_token = $token . $i;
    751 
    752 			$found[ $tag_token ] = $tag_html;
    753 			$html                = str_replace( $tag_html, $tag_token, $html, $count );
    754 		}
    755 
    756 		$replaced = str_replace( $replace, $search, $html );
    757 		$stripped = str_replace( array( "\r\n", "\n" ), '', $replaced );
    758 		$pre      = array_values( $found );
    759 		$tokens   = array_keys( $found );
    760 
    761 		return str_replace( $tokens, $pre, $stripped );
    762 	}
    763 }