Keine Beschreibung

class-wp-oembed.php 30KB

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