[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> class-wp-oembed.php (source)

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


Generated : Sun Sep 20 08:20:30 2026 Cross-referenced by PHPXref