class-oembed.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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://codex.wordpress.org/oEmbed oEmbed Codex Article
  8. * @link http://oembed.com/ oEmbed Homepage
  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. * @static
  31. * @var array
  32. */
  33. public static $early_providers = array();
  34. /**
  35. * A list of private/protected methods, used for backward compatibility.
  36. *
  37. * @since 4.2.0
  38. * @var array
  39. */
  40. private $compat_methods = array( '_fetch_with_format', '_parse_json', '_parse_xml', '_parse_xml_body' );
  41. /**
  42. * Constructor.
  43. *
  44. * @since 2.9.0
  45. */
  46. public function __construct() {
  47. $host = urlencode( home_url() );
  48. $providers = array(
  49. '#https?://((m|www)\.)?youtube\.com/watch.*#i' => array( 'https://www.youtube.com/oembed', true ),
  50. '#https?://((m|www)\.)?youtube\.com/playlist.*#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\.)?hulu\.com/watch/.*#i' => array( 'http://www.hulu.com/api/oembed.{format}', true ),
  59. 'http://i*.photobucket.com/albums/*' => array( 'http://api.photobucket.com/oembed', false ),
  60. 'http://gi*.photobucket.com/groups/*' => array( 'http://api.photobucket.com/oembed', false ),
  61. '#https?://(www\.)?scribd\.com/doc/.*#i' => array( 'https://www.scribd.com/services/oembed', true ),
  62. '#https?://wordpress\.tv/.*#i' => array( 'https://wordpress.tv/oembed/', true ),
  63. '#https?://(.+\.)?polldaddy\.com/.*#i' => array( 'https://polldaddy.com/oembed/', true ),
  64. '#https?://poll\.fm/.*#i' => array( 'https://polldaddy.com/oembed/', true ),
  65. '#https?://(www\.)?funnyordie\.com/videos/.*#i' => array( 'http://www.funnyordie.com/oembed', true ),
  66. '#https?://(www\.)?twitter\.com/\w{1,15}/status(es)?/.*#i' => array( 'https://publish.twitter.com/oembed', true ),
  67. '#https?://(www\.)?twitter\.com/\w{1,15}$#i' => array( 'https://publish.twitter.com/oembed', true ),
  68. '#https?://(www\.)?twitter\.com/\w{1,15}/likes$#i' => array( 'https://publish.twitter.com/oembed', true ),
  69. '#https?://(www\.)?twitter\.com/\w{1,15}/lists/.*#i' => array( 'https://publish.twitter.com/oembed', true ),
  70. '#https?://(www\.)?twitter\.com/\w{1,15}/timelines/.*#i' => array( 'https://publish.twitter.com/oembed', true ),
  71. '#https?://(www\.)?twitter\.com/i/moments/.*#i' => array( 'https://publish.twitter.com/oembed', true ),
  72. '#https?://(www\.)?soundcloud\.com/.*#i' => array( 'https://soundcloud.com/oembed', true ),
  73. '#https?://(.+?\.)?slideshare\.net/.*#i' => array( 'https://www.slideshare.net/api/oembed/2', true ),
  74. '#https?://(www\.)?instagr(\.am|am\.com)/p/.*#i' => array( 'https://api.instagram.com/oembed', true ),
  75. '#https?://(open|play)\.spotify\.com/.*#i' => array( 'https://embed.spotify.com/oembed/', true ),
  76. '#https?://(.+\.)?imgur\.com/.*#i' => array( 'https://api.imgur.com/oembed', true ),
  77. '#https?://(www\.)?meetu(\.ps|p\.com)/.*#i' => array( 'https://api.meetup.com/oembed', true ),
  78. '#https?://(www\.)?issuu\.com/.+/docs/.+#i' => array( 'https://issuu.com/oembed_wp', true ),
  79. '#https?://(www\.)?collegehumor\.com/video/.*#i' => array( 'https://www.collegehumor.com/oembed.{format}', true ),
  80. '#https?://(www\.)?mixcloud\.com/.*#i' => array( 'https://www.mixcloud.com/oembed', true ),
  81. '#https?://(www\.|embed\.)?ted\.com/talks/.*#i' => array( 'https://www.ted.com/services/v1/oembed.{format}', true ),
  82. '#https?://(www\.)?(animoto|video214)\.com/play/.*#i' => array( 'https://animoto.com/oembeds/create', true ),
  83. '#https?://(.+)\.tumblr\.com/post/.*#i' => array( 'https://www.tumblr.com/oembed/1.0', true ),
  84. '#https?://(www\.)?kickstarter\.com/projects/.*#i' => array( 'https://www.kickstarter.com/services/oembed', true ),
  85. '#https?://kck\.st/.*#i' => array( 'https://www.kickstarter.com/services/oembed', true ),
  86. '#https?://cloudup\.com/.*#i' => array( 'https://cloudup.com/oembed', true ),
  87. '#https?://(www\.)?reverbnation\.com/.*#i' => array( 'https://www.reverbnation.com/oembed', true ),
  88. '#https?://videopress\.com/v/.*#' => array( 'https://public-api.wordpress.com/oembed/?for=' . $host, true ),
  89. '#https?://(www\.)?reddit\.com/r/[^/]+/comments/.*#i' => array( 'https://www.reddit.com/oembed', true ),
  90. '#https?://(www\.)?speakerdeck\.com/.*#i' => array( 'https://speakerdeck.com/oembed.{format}', true ),
  91. '#https?://www\.facebook\.com/.*/posts/.*#i' => array( 'https://www.facebook.com/plugins/post/oembed.json/', true ),
  92. '#https?://www\.facebook\.com/.*/activity/.*#i' => array( 'https://www.facebook.com/plugins/post/oembed.json/', true ),
  93. '#https?://www\.facebook\.com/.*/photos/.*#i' => array( 'https://www.facebook.com/plugins/post/oembed.json/', true ),
  94. '#https?://www\.facebook\.com/photo(s/|\.php).*#i' => array( 'https://www.facebook.com/plugins/post/oembed.json/', true ),
  95. '#https?://www\.facebook\.com/permalink\.php.*#i' => array( 'https://www.facebook.com/plugins/post/oembed.json/', true ),
  96. '#https?://www\.facebook\.com/media/.*#i' => array( 'https://www.facebook.com/plugins/post/oembed.json/', true ),
  97. '#https?://www\.facebook\.com/questions/.*#i' => array( 'https://www.facebook.com/plugins/post/oembed.json/', true ),
  98. '#https?://www\.facebook\.com/notes/.*#i' => array( 'https://www.facebook.com/plugins/post/oembed.json/', true ),
  99. '#https?://www\.facebook\.com/.*/videos/.*#i' => array( 'https://www.facebook.com/plugins/video/oembed.json/', true ),
  100. '#https?://www\.facebook\.com/video\.php.*#i' => array( 'https://www.facebook.com/plugins/video/oembed.json/', true ),
  101. '#https?://(www\.)?screencast\.com/.*#i' => array( 'https://api.screencast.com/external/oembed', true ),
  102. '#https?://([a-z0-9-]+\.)?amazon\.(com|com\.mx|com\.br|ca)/.*#i' => array( 'https://read.amazon.com/kp/api/oembed', true ),
  103. '#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 ),
  104. '#https?://([a-z0-9-]+\.)?amazon\.(co\.jp|com\.au)/.*#i' => array( 'https://read.amazon.com.au/kp/api/oembed', true ),
  105. '#https?://([a-z0-9-]+\.)?amazon\.cn/.*#i' => array( 'https://read.amazon.cn/kp/api/oembed', true ),
  106. '#https?://(www\.)?a\.co/.*#i' => array( 'https://read.amazon.com/kp/api/oembed', true ),
  107. '#https?://(www\.)?amzn\.to/.*#i' => array( 'https://read.amazon.com/kp/api/oembed', true ),
  108. '#https?://(www\.)?amzn\.eu/.*#i' => array( 'https://read.amazon.co.uk/kp/api/oembed', true ),
  109. '#https?://(www\.)?amzn\.in/.*#i' => array( 'https://read.amazon.in/kp/api/oembed', true ),
  110. '#https?://(www\.)?amzn\.asia/.*#i' => array( 'https://read.amazon.com.au/kp/api/oembed', true ),
  111. '#https?://(www\.)?z\.cn/.*#i' => array( 'https://read.amazon.cn/kp/api/oembed', true ),
  112. '#https?://www\.someecards\.com/.+-cards/.+#i' => array( 'https://www.someecards.com/v2/oembed/', true ),
  113. '#https?://www\.someecards\.com/usercards/viewcard/.+#i' => array( 'https://www.someecards.com/v2/oembed/', true ),
  114. '#https?://some\.ly\/.+#i' => array( 'https://www.someecards.com/v2/oembed/', true ),
  115. );
  116. if ( ! empty( self::$early_providers['add'] ) ) {
  117. foreach ( self::$early_providers['add'] as $format => $data ) {
  118. $providers[ $format ] = $data;
  119. }
  120. }
  121. if ( ! empty( self::$early_providers['remove'] ) ) {
  122. foreach ( self::$early_providers['remove'] as $format ) {
  123. unset( $providers[ $format ] );
  124. }
  125. }
  126. self::$early_providers = array();
  127. /**
  128. * Filters the list of whitelisted oEmbed providers.
  129. *
  130. * Since WordPress 4.4, oEmbed discovery is enabled for all users and allows embedding of sanitized
  131. * iframes. The providers in this list are whitelisted, meaning they are trusted and allowed to
  132. * embed any content, such as iframes, videos, JavaScript, and arbitrary HTML.
  133. *
  134. * Supported providers:
  135. *
  136. * | Provider | Flavor | Supports HTTPS | Since |
  137. * | ------------ | --------------------------------- | :------------: | ------- |
  138. * | Dailymotion | dailymotion.com | Yes | 2.9.0 |
  139. * | Flickr | flickr.com | Yes | 2.9.0 |
  140. * | Hulu | hulu.com | Yes | 2.9.0 |
  141. * | Photobucket | photobucket.com | No | 2.9.0 |
  142. * | Scribd | scribd.com | Yes | 2.9.0 |
  143. * | Vimeo | vimeo.com | Yes | 2.9.0 |
  144. * | WordPress.tv | wordpress.tv | Yes | 2.9.0 |
  145. * | YouTube | youtube.com/watch | Yes | 2.9.0 |
  146. * | Funny or Die | funnyordie.com | Yes | 3.0.0 |
  147. * | Polldaddy | polldaddy.com | Yes | 3.0.0 |
  148. * | SmugMug | smugmug.com | Yes | 3.0.0 |
  149. * | YouTube | youtu.be | Yes | 3.0.0 |
  150. * | Twitter | twitter.com | Yes | 3.4.0 |
  151. * | Instagram | instagram.com | Yes | 3.5.0 |
  152. * | Instagram | instagr.am | Yes | 3.5.0 |
  153. * | Slideshare | slideshare.net | Yes | 3.5.0 |
  154. * | SoundCloud | soundcloud.com | Yes | 3.5.0 |
  155. * | Dailymotion | dai.ly | Yes | 3.6.0 |
  156. * | Flickr | flic.kr | Yes | 3.6.0 |
  157. * | Spotify | spotify.com | Yes | 3.6.0 |
  158. * | Imgur | imgur.com | Yes | 3.9.0 |
  159. * | Meetup.com | meetup.com | Yes | 3.9.0 |
  160. * | Meetup.com | meetu.ps | Yes | 3.9.0 |
  161. * | Animoto | animoto.com | Yes | 4.0.0 |
  162. * | Animoto | video214.com | Yes | 4.0.0 |
  163. * | CollegeHumor | collegehumor.com | Yes | 4.0.0 |
  164. * | Issuu | issuu.com | Yes | 4.0.0 |
  165. * | Mixcloud | mixcloud.com | Yes | 4.0.0 |
  166. * | Polldaddy | poll.fm | Yes | 4.0.0 |
  167. * | TED | ted.com | Yes | 4.0.0 |
  168. * | YouTube | youtube.com/playlist | Yes | 4.0.0 |
  169. * | Tumblr | tumblr.com | Yes | 4.2.0 |
  170. * | Kickstarter | kickstarter.com | Yes | 4.2.0 |
  171. * | Kickstarter | kck.st | Yes | 4.2.0 |
  172. * | Cloudup | cloudup.com | Yes | 4.3.0 |
  173. * | ReverbNation | reverbnation.com | Yes | 4.4.0 |
  174. * | VideoPress | videopress.com | Yes | 4.4.0 |
  175. * | Reddit | reddit.com | Yes | 4.4.0 |
  176. * | Speaker Deck | speakerdeck.com | Yes | 4.4.0 |
  177. * | Twitter | twitter.com/timelines | Yes | 4.5.0 |
  178. * | Twitter | twitter.com/moments | Yes | 4.5.0 |
  179. * | Facebook | facebook.com | Yes | 4.7.0 |
  180. * | Twitter | twitter.com/user | Yes | 4.7.0 |
  181. * | Twitter | twitter.com/likes | Yes | 4.7.0 |
  182. * | Twitter | twitter.com/lists | Yes | 4.7.0 |
  183. * | Screencast | screencast.com | Yes | 4.8.0 |
  184. * | Amazon | amazon.com|com.mx|com.br|ca | Yes | 4.9.0 |
  185. * | Amazon | amazon.de|fr|it|es|in|nl|ru|co.uk | Yes | 4.9.0 |
  186. * | Amazon | amazon.co.jp|com.au | Yes | 4.9.0 |
  187. * | Amazon | amazon.cn | Yes | 4.9.0 |
  188. * | Amazon | a.co | Yes | 4.9.0 |
  189. * | Amazon | amzn.to|eu|in|asia | Yes | 4.9.0 |
  190. * | Amazon | z.cn | Yes | 4.9.0 |
  191. * | Someecards | someecards.com | Yes | 4.9.0 |
  192. * | Someecards | some.ly | Yes | 4.9.0 |
  193. *
  194. * No longer supported providers:
  195. *
  196. * | Provider | Flavor | Supports HTTPS | Since | Removed |
  197. * | ------------ | -------------------- | :------------: | --------- | --------- |
  198. * | Qik | qik.com | Yes | 2.9.0 | 3.9.0 |
  199. * | Viddler | viddler.com | Yes | 2.9.0 | 4.0.0 |
  200. * | Revision3 | revision3.com | No | 2.9.0 | 4.2.0 |
  201. * | Blip | blip.tv | No | 2.9.0 | 4.4.0 |
  202. * | Rdio | rdio.com | Yes | 3.6.0 | 4.4.1 |
  203. * | Rdio | rd.io | Yes | 3.6.0 | 4.4.1 |
  204. * | Vine | vine.co | Yes | 4.1.0 | 4.9.0 |
  205. *
  206. * @see wp_oembed_add_provider()
  207. *
  208. * @since 2.9.0
  209. *
  210. * @param array $providers An array of 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 callable $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 call_user_func_array( array( $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. foreach ( $this->providers as $matchmask => $data ) {
  248. list( $providerurl, $regex ) = $data;
  249. // Turn the asterisk-type provider URLs into regex
  250. if ( !$regex ) {
  251. $matchmask = '#' . str_replace( '___wildcard___', '(.+)', preg_quote( str_replace( '*', '___wildcard___', $matchmask ), '#' ) ) . '#i';
  252. $matchmask = preg_replace( '|^#http\\\://|', '#https?\://', $matchmask );
  253. }
  254. if ( preg_match( $matchmask, $url ) ) {
  255. $provider = str_replace( '{format}', 'json', $providerurl ); // JSON is easier to deal with than XML
  256. break;
  257. }
  258. }
  259. if ( !$provider && $args['discover'] )
  260. $provider = $this->discover( $url );
  261. return $provider;
  262. }
  263. /**
  264. * Adds an oEmbed provider.
  265. *
  266. * The provider is added just-in-time when wp_oembed_add_provider() is called before
  267. * the {@see 'plugins_loaded'} hook.
  268. *
  269. * The just-in-time addition is for the benefit of the {@see 'oembed_providers'} filter.
  270. *
  271. * @static
  272. * @since 4.0.0
  273. *
  274. * @see wp_oembed_add_provider()
  275. *
  276. * @param string $format Format of URL that this provider can handle. You can use
  277. * asterisks as wildcards.
  278. * @param string $provider The URL to the oEmbed provider..
  279. * @param bool $regex Optional. Whether the $format parameter is in a regex format.
  280. * Default false.
  281. */
  282. public static function _add_provider_early( $format, $provider, $regex = false ) {
  283. if ( empty( self::$early_providers['add'] ) ) {
  284. self::$early_providers['add'] = array();
  285. }
  286. self::$early_providers['add'][ $format ] = array( $provider, $regex );
  287. }
  288. /**
  289. * Removes an oEmbed provider.
  290. *
  291. * The provider is removed just-in-time when wp_oembed_remove_provider() is called before
  292. * the {@see 'plugins_loaded'} hook.
  293. *
  294. * The just-in-time removal is for the benefit of the {@see 'oembed_providers'} filter.
  295. *
  296. * @since 4.0.0
  297. * @static
  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 $data The returned oEmbed HTML.
  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. if ( $html = wp_remote_retrieve_body( $request ) ) {
  407. /**
  408. * Filters the link types that contain oEmbed provider URLs.
  409. *
  410. * @since 2.9.0
  411. *
  412. * @param array $format Array of oEmbed link types. Accepts 'application/json+oembed',
  413. * 'text/xml+oembed', and 'application/xml+oembed' (incorrect,
  414. * used by at least Vimeo).
  415. */
  416. $linktypes = apply_filters( 'oembed_linktypes', array(
  417. 'application/json+oembed' => 'json',
  418. 'text/xml+oembed' => 'xml',
  419. 'application/xml+oembed' => 'xml',
  420. ) );
  421. // Strip <body>
  422. if ( $html_head_end = stripos( $html, '</head>' ) ) {
  423. $html = substr( $html, 0, $html_head_end );
  424. }
  425. // Do a quick check
  426. $tagfound = false;
  427. foreach ( $linktypes as $linktype => $format ) {
  428. if ( stripos($html, $linktype) ) {
  429. $tagfound = true;
  430. break;
  431. }
  432. }
  433. if ( $tagfound && preg_match_all( '#<link([^<>]+)/?>#iU', $html, $links ) ) {
  434. foreach ( $links[1] as $link ) {
  435. $atts = shortcode_parse_atts( $link );
  436. if ( !empty($atts['type']) && !empty($linktypes[$atts['type']]) && !empty($atts['href']) ) {
  437. $providers[$linktypes[$atts['type']]] = htmlspecialchars_decode( $atts['href'] );
  438. // Stop here if it's JSON (that's all we need)
  439. if ( 'json' == $linktypes[$atts['type']] )
  440. break;
  441. }
  442. }
  443. }
  444. }
  445. // JSON is preferred to XML
  446. if ( !empty($providers['json']) )
  447. return $providers['json'];
  448. elseif ( !empty($providers['xml']) )
  449. return $providers['xml'];
  450. else
  451. return false;
  452. }
  453. /**
  454. * Connects to a oEmbed provider and returns the result.
  455. *
  456. * @since 2.9.0
  457. *
  458. * @param string $provider The URL to the oEmbed provider.
  459. * @param string $url The URL to the content that is desired to be embedded.
  460. * @param array|string $args Optional. Arguments, usually passed from a shortcode. Default empty.
  461. * @return false|object False on failure, otherwise the result in the form of an object.
  462. */
  463. public function fetch( $provider, $url, $args = '' ) {
  464. $args = wp_parse_args( $args, wp_embed_defaults( $url ) );
  465. $provider = add_query_arg( 'maxwidth', (int) $args['width'], $provider );
  466. $provider = add_query_arg( 'maxheight', (int) $args['height'], $provider );
  467. $provider = add_query_arg( 'url', urlencode($url), $provider );
  468. $provider = add_query_arg( 'dnt', 1, $provider );
  469. /**
  470. * Filters the oEmbed URL to be fetched.
  471. *
  472. * @since 2.9.0
  473. * @since 4.9.0 The `dnt` (Do Not Track) query parameter was added to all oEmbed provider URLs.
  474. *
  475. * @param string $provider URL of the oEmbed provider.
  476. * @param string $url URL of the content to be embedded.
  477. * @param array $args Optional arguments, usually passed from a shortcode.
  478. */
  479. $provider = apply_filters( 'oembed_fetch_url', $provider, $url, $args );
  480. foreach ( array( 'json', 'xml' ) as $format ) {
  481. $result = $this->_fetch_with_format( $provider, $format );
  482. if ( is_wp_error( $result ) && 'not-implemented' == $result->get_error_code() )
  483. continue;
  484. return ( $result && ! is_wp_error( $result ) ) ? $result : false;
  485. }
  486. return false;
  487. }
  488. /**
  489. * Fetches result from an oEmbed provider for a specific format and complete provider URL
  490. *
  491. * @since 3.0.0
  492. *
  493. * @param string $provider_url_with_args URL to the provider with full arguments list (url, maxheight, etc.)
  494. * @param string $format Format to use
  495. * @return false|object|WP_Error False on failure, otherwise the result in the form of an object.
  496. */
  497. private function _fetch_with_format( $provider_url_with_args, $format ) {
  498. $provider_url_with_args = add_query_arg( 'format', $format, $provider_url_with_args );
  499. /** This filter is documented in wp-includes/class-oembed.php */
  500. $args = apply_filters( 'oembed_remote_get_args', array(), $provider_url_with_args );
  501. $response = wp_safe_remote_get( $provider_url_with_args, $args );
  502. if ( 501 == wp_remote_retrieve_response_code( $response ) )
  503. return new WP_Error( 'not-implemented' );
  504. if ( ! $body = wp_remote_retrieve_body( $response ) )
  505. return false;
  506. $parse_method = "_parse_$format";
  507. return $this->$parse_method( $body );
  508. }
  509. /**
  510. * Parses a json response body.
  511. *
  512. * @since 3.0.0
  513. *
  514. * @param string $response_body
  515. * @return object|false
  516. */
  517. private function _parse_json( $response_body ) {
  518. $data = json_decode( trim( $response_body ) );
  519. return ( $data && is_object( $data ) ) ? $data : false;
  520. }
  521. /**
  522. * Parses an XML response body.
  523. *
  524. * @since 3.0.0
  525. *
  526. * @param string $response_body
  527. * @return object|false
  528. */
  529. private function _parse_xml( $response_body ) {
  530. if ( ! function_exists( 'libxml_disable_entity_loader' ) )
  531. return false;
  532. $loader = libxml_disable_entity_loader( true );
  533. $errors = libxml_use_internal_errors( true );
  534. $return = $this->_parse_xml_body( $response_body );
  535. libxml_use_internal_errors( $errors );
  536. libxml_disable_entity_loader( $loader );
  537. return $return;
  538. }
  539. /**
  540. * Serves as a helper function for parsing an XML response body.
  541. *
  542. * @since 3.6.0
  543. *
  544. * @param string $response_body
  545. * @return stdClass|false
  546. */
  547. private function _parse_xml_body( $response_body ) {
  548. if ( ! function_exists( 'simplexml_import_dom' ) || ! class_exists( 'DOMDocument', false ) )
  549. return false;
  550. $dom = new DOMDocument;
  551. $success = $dom->loadXML( $response_body );
  552. if ( ! $success )
  553. return false;
  554. if ( isset( $dom->doctype ) )
  555. return false;
  556. foreach ( $dom->childNodes as $child ) {
  557. if ( XML_DOCUMENT_TYPE_NODE === $child->nodeType )
  558. return false;
  559. }
  560. $xml = simplexml_import_dom( $dom );
  561. if ( ! $xml )
  562. return false;
  563. $return = new stdClass;
  564. foreach ( $xml as $key => $value ) {
  565. $return->$key = (string) $value;
  566. }
  567. return $return;
  568. }
  569. /**
  570. * Converts a data object from WP_oEmbed::fetch() and returns the HTML.
  571. *
  572. * @since 2.9.0
  573. *
  574. * @param object $data A data object result from an oEmbed provider.
  575. * @param string $url The URL to the content that is desired to be embedded.
  576. * @return false|string False on error, otherwise the HTML needed to embed.
  577. */
  578. public function data2html( $data, $url ) {
  579. if ( ! is_object( $data ) || empty( $data->type ) )
  580. return false;
  581. $return = false;
  582. switch ( $data->type ) {
  583. case 'photo':
  584. if ( empty( $data->url ) || empty( $data->width ) || empty( $data->height ) )
  585. break;
  586. if ( ! is_string( $data->url ) || ! is_numeric( $data->width ) || ! is_numeric( $data->height ) )
  587. break;
  588. $title = ! empty( $data->title ) && is_string( $data->title ) ? $data->title : '';
  589. $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>';
  590. break;
  591. case 'video':
  592. case 'rich':
  593. if ( ! empty( $data->html ) && is_string( $data->html ) )
  594. $return = $data->html;
  595. break;
  596. case 'link':
  597. if ( ! empty( $data->title ) && is_string( $data->title ) )
  598. $return = '<a href="' . esc_url( $url ) . '">' . esc_html( $data->title ) . '</a>';
  599. break;
  600. default:
  601. $return = false;
  602. }
  603. /**
  604. * Filters the returned oEmbed HTML.
  605. *
  606. * Use this filter to add support for custom data types, or to filter the result.
  607. *
  608. * @since 2.9.0
  609. *
  610. * @param string $return The returned oEmbed HTML.
  611. * @param object $data A data object result from an oEmbed provider.
  612. * @param string $url The URL of the content to be embedded.
  613. */
  614. return apply_filters( 'oembed_dataparse', $return, $data, $url );
  615. }
  616. /**
  617. * Strips any new lines from the HTML.
  618. *
  619. * @since 2.9.0 as strip_scribd_newlines()
  620. * @since 3.0.0
  621. *
  622. * @param string $html Existing HTML.
  623. * @param object $data Data object from WP_oEmbed::data2html()
  624. * @param string $url The original URL passed to oEmbed.
  625. * @return string Possibly modified $html
  626. */
  627. public function _strip_newlines( $html, $data, $url ) {
  628. if ( false === strpos( $html, "\n" ) ) {
  629. return $html;
  630. }
  631. $count = 1;
  632. $found = array();
  633. $token = '__PRE__';
  634. $search = array( "\t", "\n", "\r", ' ' );
  635. $replace = array( '__TAB__', '__NL__', '__CR__', '__SPACE__' );
  636. $tokenized = str_replace( $search, $replace, $html );
  637. preg_match_all( '#(<pre[^>]*>.+?</pre>)#i', $tokenized, $matches, PREG_SET_ORDER );
  638. foreach ( $matches as $i => $match ) {
  639. $tag_html = str_replace( $replace, $search, $match[0] );
  640. $tag_token = $token . $i;
  641. $found[ $tag_token ] = $tag_html;
  642. $html = str_replace( $tag_html, $tag_token, $html, $count );
  643. }
  644. $replaced = str_replace( $replace, $search, $html );
  645. $stripped = str_replace( array( "\r\n", "\n" ), '', $replaced );
  646. $pre = array_values( $found );
  647. $tokens = array_keys( $found );
  648. return str_replace( $tokens, $pre, $stripped );
  649. }
  650. }