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