PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/class-oembed.php

https://gitlab.com/Blueprint-Marketing/WordPress-1
PHP | 397 lines | 188 code | 51 blank | 158 comment | 48 complexity | a14f889955c89723a942740c82d47767 MD5 | raw file
  1. <?php
  2. /**
  3. * API for fetching the HTML to embed remote content based on a provided URL.
  4. * Used internally by the {@link WP_Embed} class, but is designed to be generic.
  5. *
  6. * @link http://codex.wordpress.org/oEmbed oEmbed Codex Article
  7. * @link http://oembed.com/ oEmbed Homepage
  8. *
  9. * @package WordPress
  10. * @subpackage oEmbed
  11. */
  12. /**
  13. * oEmbed class.
  14. *
  15. * @package WordPress
  16. * @subpackage oEmbed
  17. * @since 2.9.0
  18. */
  19. class WP_oEmbed {
  20. var $providers = array();
  21. /**
  22. * Constructor
  23. *
  24. * @uses apply_filters() Filters a list of pre-defined oEmbed providers.
  25. */
  26. function __construct() {
  27. $providers = array(
  28. '#https?://(www\.)?youtube\.com/watch.*#i' => array( 'http://www.youtube.com/oembed', true ),
  29. 'http://youtu.be/*' => array( 'http://www.youtube.com/oembed', false ),
  30. 'http://blip.tv/*' => array( 'http://blip.tv/oembed/', false ),
  31. '#https?://(www\.)?vimeo\.com/.*#i' => array( 'http://vimeo.com/api/oembed.{format}', true ),
  32. '#https?://(www\.)?dailymotion\.com/.*#i' => array( 'http://www.dailymotion.com/services/oembed', true ),
  33. 'http://dai.ly/*' => array( 'http://www.dailymotion.com/services/oembed', false ),
  34. '#https?://(www\.)?flickr\.com/.*#i' => array( 'http://www.flickr.com/services/oembed/', true ),
  35. 'http://flic.kr/*' => array( 'http://www.flickr.com/services/oembed/', false ),
  36. '#https?://(.+\.)?smugmug\.com/.*#i' => array( 'http://api.smugmug.com/services/oembed/', true ),
  37. '#https?://(www\.)?hulu\.com/watch/.*#i' => array( 'http://www.hulu.com/api/oembed.{format}', true ),
  38. '#https?://(www\.)?viddler\.com/.*#i' => array( 'http://lab.viddler.com/services/oembed/', true ),
  39. 'http://qik.com/*' => array( 'http://qik.com/api/oembed.{format}', false ),
  40. 'http://revision3.com/*' => array( 'http://revision3.com/api/oembed/', false ),
  41. 'http://i*.photobucket.com/albums/*' => array( 'http://photobucket.com/oembed', false ),
  42. 'http://gi*.photobucket.com/groups/*' => array( 'http://photobucket.com/oembed', false ),
  43. '#https?://(www\.)?scribd\.com/.*#i' => array( 'http://www.scribd.com/services/oembed', true ),
  44. 'http://wordpress.tv/*' => array( 'http://wordpress.tv/oembed/', false ),
  45. '#https?://(.+\.)?polldaddy\.com/.*#i' => array( 'http://polldaddy.com/oembed/', true ),
  46. '#https?://(www\.)?funnyordie\.com/videos/.*#i' => array( 'http://www.funnyordie.com/oembed', true ),
  47. '#https?://(www\.)?twitter\.com/.+?/status(es)?/.*#i'=> array( 'https://api.twitter.com/1/statuses/oembed.{format}', true ),
  48. '#https?://(www\.)?soundcloud\.com/.*#i' => array( 'http://soundcloud.com/oembed', true ),
  49. '#https?://(www\.)?slideshare\.net/*#' => array( 'http://www.slideshare.net/api/oembed/2', true ),
  50. '#http://instagr(\.am|am\.com)/p/.*#i' => array( 'http://api.instagram.com/oembed', true ),
  51. '#https?://(www\.)?rdio\.com/.*#i' => array( 'http://www.rdio.com/api/oembed/', true ),
  52. '#https?://rd\.io/x/.*#i' => array( 'http://www.rdio.com/api/oembed/', true ),
  53. '#https?://(open|play)\.spotify\.com/.*#i' => array( 'https://embed.spotify.com/oembed/', true ),
  54. );
  55. /**
  56. * Filter the list of oEmbed providers.
  57. *
  58. * Discovery is disabled for users lacking the unfiltered_html capability.
  59. * Only providers in this array will be used for those users.
  60. *
  61. * @see wp_oembed_add_provider()
  62. *
  63. * @since 2.9.0
  64. *
  65. * @param array $providers An array of popular oEmbed providers.
  66. */
  67. $this->providers = apply_filters( 'oembed_providers', $providers );
  68. // Fix any embeds that contain new lines in the middle of the HTML which breaks wpautop().
  69. add_filter( 'oembed_dataparse', array($this, '_strip_newlines'), 10, 3 );
  70. }
  71. /**
  72. * The do-it-all function that takes a URL and attempts to return the HTML.
  73. *
  74. * @see WP_oEmbed::discover()
  75. * @see WP_oEmbed::fetch()
  76. * @see WP_oEmbed::data2html()
  77. *
  78. * @param string $url The URL to the content that should be attempted to be embedded.
  79. * @param array $args Optional arguments. Usually passed from a shortcode.
  80. * @return bool|string False on failure, otherwise the UNSANITIZED (and potentially unsafe) HTML that should be used to embed.
  81. */
  82. function get_html( $url, $args = '' ) {
  83. $provider = false;
  84. if ( !isset($args['discover']) )
  85. $args['discover'] = true;
  86. foreach ( $this->providers as $matchmask => $data ) {
  87. list( $providerurl, $regex ) = $data;
  88. // Turn the asterisk-type provider URLs into regex
  89. if ( !$regex ) {
  90. $matchmask = '#' . str_replace( '___wildcard___', '(.+)', preg_quote( str_replace( '*', '___wildcard___', $matchmask ), '#' ) ) . '#i';
  91. $matchmask = preg_replace( '|^#http\\\://|', '#https?\://', $matchmask );
  92. }
  93. if ( preg_match( $matchmask, $url ) ) {
  94. $provider = str_replace( '{format}', 'json', $providerurl ); // JSON is easier to deal with than XML
  95. break;
  96. }
  97. }
  98. if ( !$provider && $args['discover'] )
  99. $provider = $this->discover( $url );
  100. if ( !$provider || false === $data = $this->fetch( $provider, $url, $args ) )
  101. return false;
  102. /**
  103. * Filter the HTML returned by the oEmbed provider.
  104. *
  105. * @since 2.9.0
  106. *
  107. * @param string $data The returned oEmbed HTML.
  108. * @param string $url URL of the content to be embedded.
  109. * @param array $args Optional arguments, usually passed from a shortcode.
  110. */
  111. return apply_filters( 'oembed_result', $this->data2html( $data, $url ), $url, $args );
  112. }
  113. /**
  114. * Attempts to find oEmbed provider discovery <link> tags at the given URL.
  115. *
  116. * @param string $url The URL that should be inspected for discovery <link> tags.
  117. * @return bool|string False on failure, otherwise the oEmbed provider URL.
  118. */
  119. function discover( $url ) {
  120. $providers = array();
  121. // Fetch URL content
  122. if ( $html = wp_remote_retrieve_body( wp_safe_remote_get( $url ) ) ) {
  123. /**
  124. * Filter the link types that contain oEmbed provider URLs.
  125. *
  126. * @since 2.9.0
  127. *
  128. * @param array $format Array of oEmbed link types. Accepts 'application/json+oembed',
  129. * 'text/xml+oembed', and 'application/xml+oembed' (incorrect,
  130. * used by at least Vimeo).
  131. */
  132. $linktypes = apply_filters( 'oembed_linktypes', array(
  133. 'application/json+oembed' => 'json',
  134. 'text/xml+oembed' => 'xml',
  135. 'application/xml+oembed' => 'xml',
  136. ) );
  137. // Strip <body>
  138. $html = substr( $html, 0, stripos( $html, '</head>' ) );
  139. // Do a quick check
  140. $tagfound = false;
  141. foreach ( $linktypes as $linktype => $format ) {
  142. if ( stripos($html, $linktype) ) {
  143. $tagfound = true;
  144. break;
  145. }
  146. }
  147. if ( $tagfound && preg_match_all( '/<link([^<>]+)>/i', $html, $links ) ) {
  148. foreach ( $links[1] as $link ) {
  149. $atts = shortcode_parse_atts( $link );
  150. if ( !empty($atts['type']) && !empty($linktypes[$atts['type']]) && !empty($atts['href']) ) {
  151. $providers[$linktypes[$atts['type']]] = $atts['href'];
  152. // Stop here if it's JSON (that's all we need)
  153. if ( 'json' == $linktypes[$atts['type']] )
  154. break;
  155. }
  156. }
  157. }
  158. }
  159. // JSON is preferred to XML
  160. if ( !empty($providers['json']) )
  161. return $providers['json'];
  162. elseif ( !empty($providers['xml']) )
  163. return $providers['xml'];
  164. else
  165. return false;
  166. }
  167. /**
  168. * Connects to a oEmbed provider and returns the result.
  169. *
  170. * @param string $provider The URL to the oEmbed provider.
  171. * @param string $url The URL to the content that is desired to be embedded.
  172. * @param array $args Optional arguments. Usually passed from a shortcode.
  173. * @return bool|object False on failure, otherwise the result in the form of an object.
  174. */
  175. function fetch( $provider, $url, $args = '' ) {
  176. $args = wp_parse_args( $args, wp_embed_defaults() );
  177. $provider = add_query_arg( 'maxwidth', (int) $args['width'], $provider );
  178. $provider = add_query_arg( 'maxheight', (int) $args['height'], $provider );
  179. $provider = add_query_arg( 'url', urlencode($url), $provider );
  180. /**
  181. * Filter the oEmbed URL to be fetched.
  182. *
  183. * @since 2.9.0
  184. *
  185. * @param string $provider URL of the oEmbed provider.
  186. * @param string $url URL of the content to be embedded.
  187. * @param array $args Optional arguments, usually passed from a shortcode.
  188. */
  189. $provider = apply_filters( 'oembed_fetch_url', $provider, $url, $args );
  190. foreach( array( 'json', 'xml' ) as $format ) {
  191. $result = $this->_fetch_with_format( $provider, $format );
  192. if ( is_wp_error( $result ) && 'not-implemented' == $result->get_error_code() )
  193. continue;
  194. return ( $result && ! is_wp_error( $result ) ) ? $result : false;
  195. }
  196. return false;
  197. }
  198. /**
  199. * Fetches result from an oEmbed provider for a specific format and complete provider URL
  200. *
  201. * @since 3.0.0
  202. * @access private
  203. * @param string $provider_url_with_args URL to the provider with full arguments list (url, maxheight, etc.)
  204. * @param string $format Format to use
  205. * @return bool|object False on failure, otherwise the result in the form of an object.
  206. */
  207. function _fetch_with_format( $provider_url_with_args, $format ) {
  208. $provider_url_with_args = add_query_arg( 'format', $format, $provider_url_with_args );
  209. $response = wp_safe_remote_get( $provider_url_with_args );
  210. if ( 501 == wp_remote_retrieve_response_code( $response ) )
  211. return new WP_Error( 'not-implemented' );
  212. if ( ! $body = wp_remote_retrieve_body( $response ) )
  213. return false;
  214. $parse_method = "_parse_$format";
  215. return $this->$parse_method( $body );
  216. }
  217. /**
  218. * Parses a json response body.
  219. *
  220. * @since 3.0.0
  221. * @access private
  222. */
  223. function _parse_json( $response_body ) {
  224. return ( ( $data = json_decode( trim( $response_body ) ) ) && is_object( $data ) ) ? $data : false;
  225. }
  226. /**
  227. * Parses an XML response body.
  228. *
  229. * @since 3.0.0
  230. * @access private
  231. */
  232. function _parse_xml( $response_body ) {
  233. if ( ! function_exists( 'libxml_disable_entity_loader' ) )
  234. return false;
  235. $loader = libxml_disable_entity_loader( true );
  236. $errors = libxml_use_internal_errors( true );
  237. $return = $this->_parse_xml_body( $response_body );
  238. libxml_use_internal_errors( $errors );
  239. libxml_disable_entity_loader( $loader );
  240. return $return;
  241. }
  242. /**
  243. * Helper function for parsing an XML response body.
  244. *
  245. * @since 3.6.0
  246. * @access private
  247. */
  248. private function _parse_xml_body( $response_body ) {
  249. if ( ! function_exists( 'simplexml_import_dom' ) || ! class_exists( 'DOMDocument' ) )
  250. return false;
  251. $dom = new DOMDocument;
  252. $success = $dom->loadXML( $response_body );
  253. if ( ! $success )
  254. return false;
  255. if ( isset( $dom->doctype ) )
  256. return false;
  257. foreach ( $dom->childNodes as $child ) {
  258. if ( XML_DOCUMENT_TYPE_NODE === $child->nodeType )
  259. return false;
  260. }
  261. $xml = simplexml_import_dom( $dom );
  262. if ( ! $xml )
  263. return false;
  264. $return = new stdClass;
  265. foreach ( $xml as $key => $value ) {
  266. $return->$key = (string) $value;
  267. }
  268. return $return;
  269. }
  270. /**
  271. * Converts a data object from {@link WP_oEmbed::fetch()} and returns the HTML.
  272. *
  273. * @param object $data A data object result from an oEmbed provider.
  274. * @param string $url The URL to the content that is desired to be embedded.
  275. * @return bool|string False on error, otherwise the HTML needed to embed.
  276. */
  277. function data2html( $data, $url ) {
  278. if ( ! is_object( $data ) || empty( $data->type ) )
  279. return false;
  280. $return = false;
  281. switch ( $data->type ) {
  282. case 'photo':
  283. if ( empty( $data->url ) || empty( $data->width ) || empty( $data->height ) )
  284. break;
  285. if ( ! is_string( $data->url ) || ! is_numeric( $data->width ) || ! is_numeric( $data->height ) )
  286. break;
  287. $title = ! empty( $data->title ) && is_string( $data->title ) ? $data->title : '';
  288. $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>';
  289. break;
  290. case 'video':
  291. case 'rich':
  292. if ( ! empty( $data->html ) && is_string( $data->html ) )
  293. $return = $data->html;
  294. break;
  295. case 'link':
  296. if ( ! empty( $data->title ) && is_string( $data->title ) )
  297. $return = '<a href="' . esc_url( $url ) . '">' . esc_html( $data->title ) . '</a>';
  298. break;
  299. default:
  300. $return = false;
  301. }
  302. /**
  303. * Filter the returned oEmbed HTML.
  304. *
  305. * Use this filter to add support for custom data types, or to filter the result.
  306. *
  307. * @since 2.9.0
  308. *
  309. * @param string $return The returned oEmbed HTML.
  310. * @param object $data A data object result from an oEmbed provider.
  311. * @param string $url The URL of the content to be embedded.
  312. */
  313. return apply_filters( 'oembed_dataparse', $return, $data, $url );
  314. }
  315. /**
  316. * Strip any new lines from the HTML.
  317. *
  318. * @access private
  319. * @param string $html Existing HTML.
  320. * @param object $data Data object from WP_oEmbed::data2html()
  321. * @param string $url The original URL passed to oEmbed.
  322. * @return string Possibly modified $html
  323. */
  324. function _strip_newlines( $html, $data, $url ) {
  325. if ( false !== strpos( $html, "\n" ) )
  326. $html = str_replace( array( "\r\n", "\n" ), '', $html );
  327. return $html;
  328. }
  329. }
  330. /**
  331. * Returns the initialized {@link WP_oEmbed} object
  332. *
  333. * @since 2.9.0
  334. * @access private
  335. *
  336. * @see WP_oEmbed
  337. * @uses WP_oEmbed
  338. *
  339. * @return WP_oEmbed object.
  340. */
  341. function _wp_oembed_get_object() {
  342. static $wp_oembed;
  343. if ( is_null($wp_oembed) )
  344. $wp_oembed = new WP_oEmbed();
  345. return $wp_oembed;
  346. }