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