class-wp-oembed.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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?://youtu\.be/.*#i' => array( 'https://www.youtube.com/oembed', true ),
  51. '#https?://(.+\.)?vimeo\.com/.*#i' => array( 'https://vimeo.com/api/oembed.{format}', true ),
  52. '#https?://(www\.)?dailymotion\.com/.*#i' => array( 'https://www.dailymotion.com/services/oembed', true ),
  53. '#https?://dai\.ly/.*#i' => array( 'https://www.dailymotion.com/services/oembed', true ),
  54. '#https?://(www\.)?flickr\.com/.*#i' => array( 'https://www.flickr.com/services/oembed/', true ),
  55. '#https?://flic\.kr/.*#i' => array( 'https://www.flickr.com/services/oembed/', true ),
  56. '#https?://(.+\.)?smugmug\.com/.*#i' => array( 'https://api.smugmug.com/services/oembed/', true ),
  57. '#https?://(www\.)?hulu\.com/watch/.*#i' => array( 'https://www.hulu.com/api/oembed.{format}', 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?://(www\.)?instagr(\.am|am\.com)/(p|tv)/.*#i' => array( 'https://api.instagram.com/oembed', true ),
  72. '#https?://(open|play)\.spotify\.com/.*#i' => array( 'https://embed.spotify.com/oembed/', true ),
  73. '#https?://(.+\.)?imgur\.com/.*#i' => array( 'https://api.imgur.com/oembed', true ),
  74. '#https?://(www\.)?meetu(\.ps|p\.com)/.*#i' => array( 'https://api.meetup.com/oembed', true ),
  75. '#https?://(www\.)?issuu\.com/.+/docs/.+#i' => array( 'https://issuu.com/oembed_wp', true ),
  76. '#https?://(www\.)?collegehumor\.com/video/.*#i' => array( 'https://www.collegehumor.com/oembed.{format}', true ),
  77. '#https?://(www\.)?mixcloud\.com/.*#i' => array( 'https://www.mixcloud.com/oembed', true ),
  78. '#https?://(www\.|embed\.)?ted\.com/talks/.*#i' => array( 'https://www.ted.com/services/v1/oembed.{format}', true ),
  79. '#https?://(www\.)?(animoto|video214)\.com/play/.*#i' => array( 'https://animoto.com/oembeds/create', true ),
  80. '#https?://(.+)\.tumblr\.com/post/.*#i' => array( 'https://www.tumblr.com/oembed/1.0', true ),
  81. '#https?://(www\.)?kickstarter\.com/projects/.*#i' => array( 'https://www.kickstarter.com/services/oembed', true ),
  82. '#https?://kck\.st/.*#i' => array( 'https://www.kickstarter.com/services/oembed', true ),
  83. '#https?://cloudup\.com/.*#i' => array( 'https://cloudup.com/oembed', true ),
  84. '#https?://(www\.)?reverbnation\.com/.*#i' => array( 'https://www.reverbnation.com/oembed', true ),
  85. '#https?://videopress\.com/v/.*#' => array( 'https://public-api.wordpress.com/oembed/?for=' . $host, true ),
  86. '#https?://(www\.)?reddit\.com/r/[^/]+/comments/.*#i' => array( 'https://www.reddit.com/oembed', true ),
  87. '#https?://(www\.)?speakerdeck\.com/.*#i' => array( 'https://speakerdeck.com/oembed.{format}', true ),
  88. '#https?://www\.facebook\.com/.*/posts/.*#i' => array( 'https://www.facebook.com/plugins/post/oembed.json/', true ),
  89. '#https?://www\.facebook\.com/.*/activity/.*#i' => array( 'https://www.facebook.com/plugins/post/oembed.json/', true ),
  90. '#https?://www\.facebook\.com/.*/photos/.*#i' => array( 'https://www.facebook.com/plugins/post/oembed.json/', true ),
  91. '#https?://www\.facebook\.com/photo(s/|\.php).*#i' => array( 'https://www.facebook.com/plugins/post/oembed.json/', true ),
  92. '#https?://www\.facebook\.com/permalink\.php.*#i' => array( 'https://www.facebook.com/plugins/post/oembed.json/', true ),
  93. '#https?://www\.facebook\.com/media/.*#i' => array( 'https://www.facebook.com/plugins/post/oembed.json/', true ),
  94. '#https?://www\.facebook\.com/questions/.*#i' => array( 'https://www.facebook.com/plugins/post/oembed.json/', true ),
  95. '#https?://www\.facebook\.com/notes/.*#i' => array( 'https://www.facebook.com/plugins/post/oembed.json/', true ),
  96. '#https?://www\.facebook\.com/.*/videos/.*#i' => array( 'https://www.facebook.com/plugins/video/oembed.json/', true ),
  97. '#https?://www\.facebook\.com/video\.php.*#i' => array( 'https://www.facebook.com/plugins/video/oembed.json/', true ),
  98. '#https?://(www\.)?screencast\.com/.*#i' => array( 'https://api.screencast.com/external/oembed', true ),
  99. '#https?://([a-z0-9-]+\.)?amazon\.(com|com\.mx|com\.br|ca)/.*#i' => array( 'https://read.amazon.com/kp/api/oembed', true ),
  100. '#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 ),
  101. '#https?://([a-z0-9-]+\.)?amazon\.(co\.jp|com\.au)/.*#i' => array( 'https://read.amazon.com.au/kp/api/oembed', true ),
  102. '#https?://([a-z0-9-]+\.)?amazon\.cn/.*#i' => array( 'https://read.amazon.cn/kp/api/oembed', true ),
  103. '#https?://(www\.)?a\.co/.*#i' => array( 'https://read.amazon.com/kp/api/oembed', true ),
  104. '#https?://(www\.)?amzn\.to/.*#i' => array( 'https://read.amazon.com/kp/api/oembed', true ),
  105. '#https?://(www\.)?amzn\.eu/.*#i' => array( 'https://read.amazon.co.uk/kp/api/oembed', true ),
  106. '#https?://(www\.)?amzn\.in/.*#i' => array( 'https://read.amazon.in/kp/api/oembed', true ),
  107. '#https?://(www\.)?amzn\.asia/.*#i' => array( 'https://read.amazon.com.au/kp/api/oembed', true ),
  108. '#https?://(www\.)?z\.cn/.*#i' => array( 'https://read.amazon.cn/kp/api/oembed', true ),
  109. '#https?://www\.someecards\.com/.+-cards/.+#i' => array( 'https://www.someecards.com/v2/oembed/', true ),
  110. '#https?://www\.someecards\.com/usercards/viewcard/.+#i' => array( 'https://www.someecards.com/v2/oembed/', true ),
  111. '#https?://some\.ly\/.+#i' => array( 'https://www.someecards.com/v2/oembed/', true ),
  112. );
  113. if ( ! empty( self::$early_providers['add'] ) ) {
  114. foreach ( self::$early_providers['add'] as $format => $data ) {
  115. $providers[ $format ] = $data;
  116. }
  117. }
  118. if ( ! empty( self::$early_providers['remove'] ) ) {
  119. foreach ( self::$early_providers['remove'] as $format ) {
  120. unset( $providers[ $format ] );
  121. }
  122. }
  123. self::$early_providers = array();
  124. /**
  125. * Filters the list of whitelisted oEmbed providers.
  126. *
  127. * Since WordPress 4.4, oEmbed discovery is enabled for all users and allows embedding of sanitized
  128. * iframes. The providers in this list are whitelisted, meaning they are trusted and allowed to
  129. * embed any content, such as iframes, videos, JavaScript, and arbitrary HTML.
  130. *
  131. * Supported providers:
  132. *
  133. * | Provider | Flavor | Since |
  134. * | ------------ | ----------------------------------------- | ------- |
  135. * | Dailymotion | dailymotion.com | 2.9.0 |
  136. * | Flickr | flickr.com | 2.9.0 |
  137. * | Hulu | hulu.com | 2.9.0 |
  138. * | Scribd | scribd.com | 2.9.0 |
  139. * | Vimeo | vimeo.com | 2.9.0 |
  140. * | WordPress.tv | wordpress.tv | 2.9.0 |
  141. * | YouTube | youtube.com/watch | 2.9.0 |
  142. * | Crowdsignal | polldaddy.com | 3.0.0 |
  143. * | SmugMug | smugmug.com | 3.0.0 |
  144. * | YouTube | youtu.be | 3.0.0 |
  145. * | Twitter | twitter.com | 3.4.0 |
  146. * | Instagram | instagram.com | 3.5.0 |
  147. * | Instagram | instagr.am | 3.5.0 |
  148. * | Slideshare | slideshare.net | 3.5.0 |
  149. * | SoundCloud | soundcloud.com | 3.5.0 |
  150. * | Dailymotion | dai.ly | 3.6.0 |
  151. * | Flickr | flic.kr | 3.6.0 |
  152. * | Spotify | spotify.com | 3.6.0 |
  153. * | Imgur | imgur.com | 3.9.0 |
  154. * | Meetup.com | meetup.com | 3.9.0 |
  155. * | Meetup.com | meetu.ps | 3.9.0 |
  156. * | Animoto | animoto.com | 4.0.0 |
  157. * | Animoto | video214.com | 4.0.0 |
  158. * | CollegeHumor | collegehumor.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. * | Facebook | facebook.com | 4.7.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. * | Screencast | screencast.com | 4.8.0 |
  179. * | Amazon | amazon.com (com.mx, com.br, ca) | 4.9.0 |
  180. * | Amazon | amazon.de (fr, it, es, in, nl, ru, co.uk) | 4.9.0 |
  181. * | Amazon | amazon.co.jp (com.au) | 4.9.0 |
  182. * | Amazon | amazon.cn | 4.9.0 |
  183. * | Amazon | a.co | 4.9.0 |
  184. * | Amazon | amzn.to (eu, in, asia) | 4.9.0 |
  185. * | Amazon | z.cn | 4.9.0 |
  186. * | Someecards | someecards.com | 4.9.0 |
  187. * | Someecards | some.ly | 4.9.0 |
  188. * | Crowdsignal | survey.fm | 5.1.0 |
  189. * | Instagram TV | instagram.com | 5.1.0 |
  190. * | Instagram TV | instagr.am | 5.1.0 |
  191. *
  192. * No longer supported providers:
  193. *
  194. * | Provider | Flavor | Since | Removed |
  195. * | ------------ | -------------------- | --------- | --------- |
  196. * | Qik | qik.com | 2.9.0 | 3.9.0 |
  197. * | Viddler | viddler.com | 2.9.0 | 4.0.0 |
  198. * | Revision3 | revision3.com | 2.9.0 | 4.2.0 |
  199. * | Blip | blip.tv | 2.9.0 | 4.4.0 |
  200. * | Rdio | rdio.com | 3.6.0 | 4.4.1 |
  201. * | Rdio | rd.io | 3.6.0 | 4.4.1 |
  202. * | Vine | vine.co | 4.1.0 | 4.9.0 |
  203. * | Photobucket | photobucket.com | 2.9.0 | 5.1.0 |
  204. * | Funny or Die | funnyordie.com | 3.0.0 | 5.1.0 |
  205. *
  206. * @see wp_oembed_add_provider()
  207. *
  208. * @since 2.9.0
  209. *
  210. * @param array[] $providers An array of arrays containing data about popular oEmbed providers.
  211. */
  212. $this->providers = apply_filters( 'oembed_providers', $providers );
  213. // Fix any embeds that contain new lines in the middle of the HTML which breaks wpautop().
  214. add_filter( 'oembed_dataparse', array( $this, '_strip_newlines' ), 10, 3 );
  215. }
  216. /**
  217. * Exposes private/protected methods for backward compatibility.
  218. *
  219. * @since 4.0.0
  220. *
  221. * @param string $name Method to call.
  222. * @param array $arguments Arguments to pass when calling.
  223. * @return mixed|bool Return value of the callback, false otherwise.
  224. */
  225. public function __call( $name, $arguments ) {
  226. if ( in_array( $name, $this->compat_methods ) ) {
  227. return $this->$name( ...$arguments );
  228. }
  229. return false;
  230. }
  231. /**
  232. * Takes a URL and returns the corresponding oEmbed provider's URL, if there is one.
  233. *
  234. * @since 4.0.0
  235. *
  236. * @see WP_oEmbed::discover()
  237. *
  238. * @param string $url The URL to the content.
  239. * @param string|array $args Optional provider arguments.
  240. * @return false|string False on failure, otherwise the oEmbed provider URL.
  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 array|string $args Optional. Arguments, usually passed from a shortcode. Default empty.
  319. * @return false|object False on failure, otherwise the result in the form of an object.
  320. */
  321. public function get_data( $url, $args = '' ) {
  322. $args = wp_parse_args( $args );
  323. $provider = $this->get_provider( $url, $args );
  324. if ( ! $provider ) {
  325. return false;
  326. }
  327. $data = $this->fetch( $provider, $url, $args );
  328. if ( false === $data ) {
  329. return false;
  330. }
  331. return $data;
  332. }
  333. /**
  334. * The do-it-all function that takes a URL and attempts to return the HTML.
  335. *
  336. * @see WP_oEmbed::fetch()
  337. * @see WP_oEmbed::data2html()
  338. *
  339. * @since 2.9.0
  340. *
  341. * @param string $url The URL to the content that should be attempted to be embedded.
  342. * @param array|string $args Optional. Arguments, usually passed from a shortcode. Default empty.
  343. * @return false|string False on failure, otherwise the UNSANITIZED (and potentially unsafe) HTML that should be used to embed.
  344. */
  345. public function get_html( $url, $args = '' ) {
  346. /**
  347. * Filters the oEmbed result before any HTTP requests are made.
  348. *
  349. * This allows one to short-circuit the default logic, perhaps by
  350. * replacing it with a routine that is more optimal for your setup.
  351. *
  352. * Passing a non-null value to the filter will effectively short-circuit retrieval,
  353. * returning the passed value instead.
  354. *
  355. * @since 4.5.3
  356. *
  357. * @param null|string $result The UNSANITIZED (and potentially unsafe) HTML that should be used to embed. Default null.
  358. * @param string $url The URL to the content that should be attempted to be embedded.
  359. * @param array $args Optional. Arguments, usually passed from a shortcode. Default empty.
  360. */
  361. $pre = apply_filters( 'pre_oembed_result', null, $url, $args );
  362. if ( null !== $pre ) {
  363. return $pre;
  364. }
  365. $data = $this->get_data( $url, $args );
  366. if ( false === $data ) {
  367. return false;
  368. }
  369. /**
  370. * Filters the HTML returned by the oEmbed provider.
  371. *
  372. * @since 2.9.0
  373. *
  374. * @param string|false $data The returned oEmbed HTML (false if unsafe).
  375. * @param string $url URL of the content to be embedded.
  376. * @param array $args Optional arguments, usually passed from a shortcode.
  377. */
  378. return apply_filters( 'oembed_result', $this->data2html( $data, $url ), $url, $args );
  379. }
  380. /**
  381. * Attempts to discover link tags at the given URL for an oEmbed provider.
  382. *
  383. * @since 2.9.0
  384. *
  385. * @param string $url The URL that should be inspected for discovery `<link>` tags.
  386. * @return false|string False on failure, otherwise the oEmbed provider URL.
  387. */
  388. public function discover( $url ) {
  389. $providers = array();
  390. $args = array(
  391. 'limit_response_size' => 153600, // 150 KB
  392. );
  393. /**
  394. * Filters oEmbed remote get arguments.
  395. *
  396. * @since 4.0.0
  397. *
  398. * @see WP_Http::request()
  399. *
  400. * @param array $args oEmbed remote get arguments.
  401. * @param string $url URL to be inspected.
  402. */
  403. $args = apply_filters( 'oembed_remote_get_args', $args, $url );
  404. // Fetch URL content
  405. $request = wp_safe_remote_get( $url, $args );
  406. $html = wp_remote_retrieve_body( $request );
  407. if ( $html ) {
  408. /**
  409. * Filters the link types that contain oEmbed provider URLs.
  410. *
  411. * @since 2.9.0
  412. *
  413. * @param string[] $format Array of oEmbed link types. Accepts 'application/json+oembed',
  414. * 'text/xml+oembed', and 'application/xml+oembed' (incorrect,
  415. * used by at least Vimeo).
  416. */
  417. $linktypes = apply_filters(
  418. 'oembed_linktypes',
  419. array(
  420. 'application/json+oembed' => 'json',
  421. 'text/xml+oembed' => 'xml',
  422. 'application/xml+oembed' => 'xml',
  423. )
  424. );
  425. // Strip <body>
  426. $html_head_end = stripos( $html, '</head>' );
  427. if ( $html_head_end ) {
  428. $html = substr( $html, 0, $html_head_end );
  429. }
  430. // Do a quick check
  431. $tagfound = false;
  432. foreach ( $linktypes as $linktype => $format ) {
  433. if ( stripos( $html, $linktype ) ) {
  434. $tagfound = true;
  435. break;
  436. }
  437. }
  438. if ( $tagfound && preg_match_all( '#<link([^<>]+)/?>#iU', $html, $links ) ) {
  439. foreach ( $links[1] as $link ) {
  440. $atts = shortcode_parse_atts( $link );
  441. if ( ! empty( $atts['type'] ) && ! empty( $linktypes[ $atts['type'] ] ) && ! empty( $atts['href'] ) ) {
  442. $providers[ $linktypes[ $atts['type'] ] ] = htmlspecialchars_decode( $atts['href'] );
  443. // Stop here if it's JSON (that's all we need)
  444. if ( 'json' == $linktypes[ $atts['type'] ] ) {
  445. break;
  446. }
  447. }
  448. }
  449. }
  450. }
  451. // JSON is preferred to XML
  452. if ( ! empty( $providers['json'] ) ) {
  453. return $providers['json'];
  454. } elseif ( ! empty( $providers['xml'] ) ) {
  455. return $providers['xml'];
  456. } else {
  457. return false;
  458. }
  459. }
  460. /**
  461. * Connects to a oEmbed provider and returns the result.
  462. *
  463. * @since 2.9.0
  464. *
  465. * @param string $provider The URL to the oEmbed provider.
  466. * @param string $url The URL to the content that is desired to be embedded.
  467. * @param array|string $args Optional. Arguments, usually passed from a shortcode. Default empty.
  468. * @return false|object False on failure, otherwise the result in the form of an object.
  469. */
  470. public function fetch( $provider, $url, $args = '' ) {
  471. $args = wp_parse_args( $args, wp_embed_defaults( $url ) );
  472. $provider = add_query_arg( 'maxwidth', (int) $args['width'], $provider );
  473. $provider = add_query_arg( 'maxheight', (int) $args['height'], $provider );
  474. $provider = add_query_arg( 'url', urlencode( $url ), $provider );
  475. $provider = add_query_arg( 'dnt', 1, $provider );
  476. /**
  477. * Filters the oEmbed URL to be fetched.
  478. *
  479. * @since 2.9.0
  480. * @since 4.9.0 The `dnt` (Do Not Track) query parameter was added to all oEmbed provider URLs.
  481. *
  482. * @param string $provider URL of the oEmbed provider.
  483. * @param string $url URL of the content to be embedded.
  484. * @param array $args Optional arguments, usually passed from a shortcode.
  485. */
  486. $provider = apply_filters( 'oembed_fetch_url', $provider, $url, $args );
  487. foreach ( array( 'json', 'xml' ) as $format ) {
  488. $result = $this->_fetch_with_format( $provider, $format );
  489. if ( is_wp_error( $result ) && 'not-implemented' == $result->get_error_code() ) {
  490. continue;
  491. }
  492. return ( $result && ! is_wp_error( $result ) ) ? $result : false;
  493. }
  494. return false;
  495. }
  496. /**
  497. * Fetches result from an oEmbed provider for a specific format and complete provider URL
  498. *
  499. * @since 3.0.0
  500. *
  501. * @param string $provider_url_with_args URL to the provider with full arguments list (url, maxheight, etc.)
  502. * @param string $format Format to use
  503. * @return false|object|WP_Error False on failure, otherwise the result in the form of an object.
  504. */
  505. private function _fetch_with_format( $provider_url_with_args, $format ) {
  506. $provider_url_with_args = add_query_arg( 'format', $format, $provider_url_with_args );
  507. /** This filter is documented in wp-includes/class-wp-oembed.php */
  508. $args = apply_filters( 'oembed_remote_get_args', array(), $provider_url_with_args );
  509. $response = wp_safe_remote_get( $provider_url_with_args, $args );
  510. if ( 501 == wp_remote_retrieve_response_code( $response ) ) {
  511. return new WP_Error( 'not-implemented' );
  512. }
  513. $body = wp_remote_retrieve_body( $response );
  514. if ( ! $body ) {
  515. return false;
  516. }
  517. $parse_method = "_parse_$format";
  518. return $this->$parse_method( $body );
  519. }
  520. /**
  521. * Parses a json response body.
  522. *
  523. * @since 3.0.0
  524. *
  525. * @param string $response_body
  526. * @return object|false
  527. */
  528. private function _parse_json( $response_body ) {
  529. $data = json_decode( trim( $response_body ) );
  530. return ( $data && is_object( $data ) ) ? $data : false;
  531. }
  532. /**
  533. * Parses an XML response body.
  534. *
  535. * @since 3.0.0
  536. *
  537. * @param string $response_body
  538. * @return object|false
  539. */
  540. private function _parse_xml( $response_body ) {
  541. if ( ! function_exists( 'libxml_disable_entity_loader' ) ) {
  542. return false;
  543. }
  544. $loader = libxml_disable_entity_loader( true );
  545. $errors = libxml_use_internal_errors( true );
  546. $return = $this->_parse_xml_body( $response_body );
  547. libxml_use_internal_errors( $errors );
  548. libxml_disable_entity_loader( $loader );
  549. return $return;
  550. }
  551. /**
  552. * Serves as a helper function for parsing an XML response body.
  553. *
  554. * @since 3.6.0
  555. *
  556. * @param string $response_body
  557. * @return stdClass|false
  558. */
  559. private function _parse_xml_body( $response_body ) {
  560. if ( ! function_exists( 'simplexml_import_dom' ) || ! class_exists( 'DOMDocument', false ) ) {
  561. return false;
  562. }
  563. $dom = new DOMDocument;
  564. $success = $dom->loadXML( $response_body );
  565. if ( ! $success ) {
  566. return false;
  567. }
  568. if ( isset( $dom->doctype ) ) {
  569. return false;
  570. }
  571. foreach ( $dom->childNodes as $child ) {
  572. if ( XML_DOCUMENT_TYPE_NODE === $child->nodeType ) {
  573. return false;
  574. }
  575. }
  576. $xml = simplexml_import_dom( $dom );
  577. if ( ! $xml ) {
  578. return false;
  579. }
  580. $return = new stdClass;
  581. foreach ( $xml as $key => $value ) {
  582. $return->$key = (string) $value;
  583. }
  584. return $return;
  585. }
  586. /**
  587. * Converts a data object from WP_oEmbed::fetch() and returns the HTML.
  588. *
  589. * @since 2.9.0
  590. *
  591. * @param object $data A data object result from an oEmbed provider.
  592. * @param string $url The URL to the content that is desired to be embedded.
  593. * @return false|string False on error, otherwise the HTML needed to embed.
  594. */
  595. public function data2html( $data, $url ) {
  596. if ( ! is_object( $data ) || empty( $data->type ) ) {
  597. return false;
  598. }
  599. $return = false;
  600. switch ( $data->type ) {
  601. case 'photo':
  602. if ( empty( $data->url ) || empty( $data->width ) || empty( $data->height ) ) {
  603. break;
  604. }
  605. if ( ! is_string( $data->url ) || ! is_numeric( $data->width ) || ! is_numeric( $data->height ) ) {
  606. break;
  607. }
  608. $title = ! empty( $data->title ) && is_string( $data->title ) ? $data->title : '';
  609. $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>';
  610. break;
  611. case 'video':
  612. case 'rich':
  613. if ( ! empty( $data->html ) && is_string( $data->html ) ) {
  614. $return = $data->html;
  615. }
  616. break;
  617. case 'link':
  618. if ( ! empty( $data->title ) && is_string( $data->title ) ) {
  619. $return = '<a href="' . esc_url( $url ) . '">' . esc_html( $data->title ) . '</a>';
  620. }
  621. break;
  622. default:
  623. $return = false;
  624. }
  625. /**
  626. * Filters the returned oEmbed HTML.
  627. *
  628. * Use this filter to add support for custom data types, or to filter the result.
  629. *
  630. * @since 2.9.0
  631. *
  632. * @param string $return The returned oEmbed HTML.
  633. * @param object $data A data object result from an oEmbed provider.
  634. * @param string $url The URL of the content to be embedded.
  635. */
  636. return apply_filters( 'oembed_dataparse', $return, $data, $url );
  637. }
  638. /**
  639. * Strips any new lines from the HTML.
  640. *
  641. * @since 2.9.0 as strip_scribd_newlines()
  642. * @since 3.0.0
  643. *
  644. * @param string $html Existing HTML.
  645. * @param object $data Data object from WP_oEmbed::data2html()
  646. * @param string $url The original URL passed to oEmbed.
  647. * @return string Possibly modified $html
  648. */
  649. public function _strip_newlines( $html, $data, $url ) {
  650. if ( false === strpos( $html, "\n" ) ) {
  651. return $html;
  652. }
  653. $count = 1;
  654. $found = array();
  655. $token = '__PRE__';
  656. $search = array( "\t", "\n", "\r", ' ' );
  657. $replace = array( '__TAB__', '__NL__', '__CR__', '__SPACE__' );
  658. $tokenized = str_replace( $search, $replace, $html );
  659. preg_match_all( '#(<pre[^>]*>.+?</pre>)#i', $tokenized, $matches, PREG_SET_ORDER );
  660. foreach ( $matches as $i => $match ) {
  661. $tag_html = str_replace( $replace, $search, $match[0] );
  662. $tag_token = $token . $i;
  663. $found[ $tag_token ] = $tag_html;
  664. $html = str_replace( $tag_html, $tag_token, $html, $count );
  665. }
  666. $replaced = str_replace( $replace, $search, $html );
  667. $stripped = str_replace( array( "\r\n", "\n" ), '', $replaced );
  668. $pre = array_values( $found );
  669. $tokens = array_keys( $found );
  670. return str_replace( $tokens, $pre, $stripped );
  671. }
  672. }