class-oembed.php 29 KB

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