Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

694 linhas
28 KiB

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