PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/class-oembed.php

https://github.com/yanickouellet/WordPress
PHP | 326 lines | 175 code | 44 blank | 107 comment | 44 complexity | 459936410c1098b35db0c514f0939e98 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  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. // List out some popular sites that support oEmbed.
  28. // The WP_Embed class disables discovery for non-unfiltered_html users, so only providers in this array will be used for them.
  29. // Add to this list using the wp_oembed_add_provider() function (see its PHPDoc for details).
  30. $this->providers = apply_filters( 'oembed_providers', array(
  31. '#https?://(www\.)?youtube\.com/watch.*#i' => array( 'http://www.youtube.com/oembed', true ),
  32. 'http://youtu.be/*' => array( 'http://www.youtube.com/oembed', false ),
  33. 'http://blip.tv/*' => array( 'http://blip.tv/oembed/', false ),
  34. '#https?://(www\.)?vimeo\.com/.*#i' => array( 'http://vimeo.com/api/oembed.{format}', true ),
  35. '#https?://(www\.)?dailymotion\.com/.*#i' => array( 'http://www.dailymotion.com/services/oembed', true ),
  36. 'http://dai.ly/*' => array( 'http://www.dailymotion.com/services/oembed', false ),
  37. '#https?://(www\.)?flickr\.com/.*#i' => array( 'http://www.flickr.com/services/oembed/', true ),
  38. 'http://flic.kr/*' => array( 'http://www.flickr.com/services/oembed/', false ),
  39. '#https?://(.+\.)?smugmug\.com/.*#i' => array( 'http://api.smugmug.com/services/oembed/', true ),
  40. '#https?://(www\.)?hulu\.com/watch/.*#i' => array( 'http://www.hulu.com/api/oembed.{format}', true ),
  41. '#https?://(www\.)?viddler\.com/.*#i' => array( 'http://lab.viddler.com/services/oembed/', true ),
  42. 'http://qik.com/*' => array( 'http://qik.com/api/oembed.{format}', false ),
  43. 'http://revision3.com/*' => array( 'http://revision3.com/api/oembed/', false ),
  44. 'http://i*.photobucket.com/albums/*' => array( 'http://photobucket.com/oembed', false ),
  45. 'http://gi*.photobucket.com/groups/*' => array( 'http://photobucket.com/oembed', false ),
  46. '#https?://(www\.)?scribd\.com/.*#i' => array( 'http://www.scribd.com/services/oembed', true ),
  47. 'http://wordpress.tv/*' => array( 'http://wordpress.tv/oembed/', false ),
  48. '#https?://(.+\.)?polldaddy\.com/.*#i' => array( 'http://polldaddy.com/oembed/', true ),
  49. '#https?://(www\.)?funnyordie\.com/videos/.*#i' => array( 'http://www.funnyordie.com/oembed', true ),
  50. '#https?://(www\.)?twitter\.com/.+?/status(es)?/.*#i'=> array( 'http://api.twitter.com/1/statuses/oembed.{format}', true ),
  51. '#https?://(www\.)?soundcloud\.com/.*#i' => array( 'http://soundcloud.com/oembed', true ),
  52. '#https?://(www\.)?slideshare\.net/*#' => array( 'http://www.slideshare.net/api/oembed/2', true ),
  53. '#http://instagr(\.am|am\.com)/p/.*#i' => array( 'http://api.instagram.com/oembed', true ),
  54. '#https?://(www\.)?rdio\.com/.*#i' => array( 'http://www.rdio.com/api/oembed/', true ),
  55. '#https?://rd\.io/x/.*#i' => array( 'http://www.rdio.com/api/oembed/', true ),
  56. '#https?://(open|play)\.spotify\.com/.*#i' => array( 'https://embed.spotify.com/oembed/', true ),
  57. ) );
  58. // Fix any embeds that contain new lines in the middle of the HTML which breaks wpautop().
  59. add_filter( 'oembed_dataparse', array($this, '_strip_newlines'), 10, 3 );
  60. }
  61. /**
  62. * The do-it-all function that takes a URL and attempts to return the HTML.
  63. *
  64. * @see WP_oEmbed::discover()
  65. * @see WP_oEmbed::fetch()
  66. * @see WP_oEmbed::data2html()
  67. *
  68. * @param string $url The URL to the content that should be attempted to be embedded.
  69. * @param array $args Optional arguments. Usually passed from a shortcode.
  70. * @return bool|string False on failure, otherwise the UNSANITIZED (and potentially unsafe) HTML that should be used to embed.
  71. */
  72. function get_html( $url, $args = '' ) {
  73. $provider = false;
  74. if ( !isset($args['discover']) )
  75. $args['discover'] = true;
  76. foreach ( $this->providers as $matchmask => $data ) {
  77. list( $providerurl, $regex ) = $data;
  78. // Turn the asterisk-type provider URLs into regex
  79. if ( !$regex ) {
  80. $matchmask = '#' . str_replace( '___wildcard___', '(.+)', preg_quote( str_replace( '*', '___wildcard___', $matchmask ), '#' ) ) . '#i';
  81. $matchmask = preg_replace( '|^#http\\\://|', '#https?\://', $matchmask );
  82. }
  83. if ( preg_match( $matchmask, $url ) ) {
  84. $provider = str_replace( '{format}', 'json', $providerurl ); // JSON is easier to deal with than XML
  85. break;
  86. }
  87. }
  88. if ( !$provider && $args['discover'] )
  89. $provider = $this->discover( $url );
  90. if ( !$provider || false === $data = $this->fetch( $provider, $url, $args ) )
  91. return false;
  92. return apply_filters( 'oembed_result', $this->data2html( $data, $url ), $url, $args );
  93. }
  94. /**
  95. * Attempts to find oEmbed provider discovery <link> tags at the given URL.
  96. *
  97. * @param string $url The URL that should be inspected for discovery <link> tags.
  98. * @return bool|string False on failure, otherwise the oEmbed provider URL.
  99. */
  100. function discover( $url ) {
  101. $providers = array();
  102. // Fetch URL content
  103. if ( $html = wp_remote_retrieve_body( wp_remote_get( $url, array( 'reject_unsafe_urls' => true ) ) ) ) {
  104. // <link> types that contain oEmbed provider URLs
  105. $linktypes = apply_filters( 'oembed_linktypes', array(
  106. 'application/json+oembed' => 'json',
  107. 'text/xml+oembed' => 'xml',
  108. 'application/xml+oembed' => 'xml', // Incorrect, but used by at least Vimeo
  109. ) );
  110. // Strip <body>
  111. $html = substr( $html, 0, stripos( $html, '</head>' ) );
  112. // Do a quick check
  113. $tagfound = false;
  114. foreach ( $linktypes as $linktype => $format ) {
  115. if ( stripos($html, $linktype) ) {
  116. $tagfound = true;
  117. break;
  118. }
  119. }
  120. if ( $tagfound && preg_match_all( '/<link([^<>]+)>/i', $html, $links ) ) {
  121. foreach ( $links[1] as $link ) {
  122. $atts = shortcode_parse_atts( $link );
  123. if ( !empty($atts['type']) && !empty($linktypes[$atts['type']]) && !empty($atts['href']) ) {
  124. $providers[$linktypes[$atts['type']]] = $atts['href'];
  125. // Stop here if it's JSON (that's all we need)
  126. if ( 'json' == $linktypes[$atts['type']] )
  127. break;
  128. }
  129. }
  130. }
  131. }
  132. // JSON is preferred to XML
  133. if ( !empty($providers['json']) )
  134. return $providers['json'];
  135. elseif ( !empty($providers['xml']) )
  136. return $providers['xml'];
  137. else
  138. return false;
  139. }
  140. /**
  141. * Connects to a oEmbed provider and returns the result.
  142. *
  143. * @param string $provider The URL to the oEmbed provider.
  144. * @param string $url The URL to the content that is desired to be embedded.
  145. * @param array $args Optional arguments. Usually passed from a shortcode.
  146. * @return bool|object False on failure, otherwise the result in the form of an object.
  147. */
  148. function fetch( $provider, $url, $args = '' ) {
  149. $args = wp_parse_args( $args, wp_embed_defaults() );
  150. $provider = add_query_arg( 'maxwidth', (int) $args['width'], $provider );
  151. $provider = add_query_arg( 'maxheight', (int) $args['height'], $provider );
  152. $provider = add_query_arg( 'url', urlencode($url), $provider );
  153. $provider = apply_filters( 'oembed_fetch_url', $provider, $url, $args );
  154. foreach( array( 'json', 'xml' ) as $format ) {
  155. $result = $this->_fetch_with_format( $provider, $format );
  156. if ( is_wp_error( $result ) && 'not-implemented' == $result->get_error_code() )
  157. continue;
  158. return ( $result && ! is_wp_error( $result ) ) ? $result : false;
  159. }
  160. return false;
  161. }
  162. /**
  163. * Fetches result from an oEmbed provider for a specific format and complete provider URL
  164. *
  165. * @since 3.0.0
  166. * @access private
  167. * @param string $provider_url_with_args URL to the provider with full arguments list (url, maxheight, etc.)
  168. * @param string $format Format to use
  169. * @return bool|object False on failure, otherwise the result in the form of an object.
  170. */
  171. function _fetch_with_format( $provider_url_with_args, $format ) {
  172. $provider_url_with_args = add_query_arg( 'format', $format, $provider_url_with_args );
  173. $response = wp_remote_get( $provider_url_with_args, array( 'reject_unsafe_urls' => true ) );
  174. if ( 501 == wp_remote_retrieve_response_code( $response ) )
  175. return new WP_Error( 'not-implemented' );
  176. if ( ! $body = wp_remote_retrieve_body( $response ) )
  177. return false;
  178. $parse_method = "_parse_$format";
  179. return $this->$parse_method( $body );
  180. }
  181. /**
  182. * Parses a json response body.
  183. *
  184. * @since 3.0.0
  185. * @access private
  186. */
  187. function _parse_json( $response_body ) {
  188. return ( ( $data = json_decode( trim( $response_body ) ) ) && is_object( $data ) ) ? $data : false;
  189. }
  190. /**
  191. * Parses an XML response body.
  192. *
  193. * @since 3.0.0
  194. * @access private
  195. */
  196. function _parse_xml( $response_body ) {
  197. if ( !function_exists('simplexml_load_string') ) {
  198. return false;
  199. }
  200. if ( ! function_exists( 'libxml_disable_entity_loader' ) )
  201. return false;
  202. $loader = libxml_disable_entity_loader( true );
  203. $errors = libxml_use_internal_errors( true );
  204. $data = simplexml_load_string( $response_body );
  205. libxml_use_internal_errors( $errors );
  206. $return = false;
  207. if ( is_object( $data ) ) {
  208. $return = new stdClass;
  209. foreach ( $data as $key => $value ) {
  210. $return->$key = (string) $value;
  211. }
  212. }
  213. libxml_disable_entity_loader( $loader );
  214. return $return;
  215. }
  216. /**
  217. * Converts a data object from {@link WP_oEmbed::fetch()} and returns the HTML.
  218. *
  219. * @param object $data A data object result from an oEmbed provider.
  220. * @param string $url The URL to the content that is desired to be embedded.
  221. * @return bool|string False on error, otherwise the HTML needed to embed.
  222. */
  223. function data2html( $data, $url ) {
  224. if ( ! is_object( $data ) || empty( $data->type ) )
  225. return false;
  226. $return = false;
  227. switch ( $data->type ) {
  228. case 'photo':
  229. if ( empty( $data->url ) || empty( $data->width ) || empty( $data->height ) )
  230. break;
  231. if ( ! is_string( $data->url ) || ! is_numeric( $data->width ) || ! is_numeric( $data->height ) )
  232. break;
  233. $title = ! empty( $data->title ) && is_string( $data->title ) ? $data->title : '';
  234. $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>';
  235. break;
  236. case 'video':
  237. case 'rich':
  238. if ( ! empty( $data->html ) && is_string( $data->html ) )
  239. $return = $data->html;
  240. break;
  241. case 'link':
  242. if ( ! empty( $data->title ) && is_string( $data->title ) )
  243. $return = '<a href="' . esc_url( $url ) . '">' . esc_html( $data->title ) . '</a>';
  244. break;
  245. default:
  246. $return = false;
  247. }
  248. // You can use this filter to add support for custom data types or to filter the result
  249. return apply_filters( 'oembed_dataparse', $return, $data, $url );
  250. }
  251. /**
  252. * Strip any new lines from the HTML.
  253. *
  254. * @access private
  255. * @param string $html Existing HTML.
  256. * @param object $data Data object from WP_oEmbed::data2html()
  257. * @param string $url The original URL passed to oEmbed.
  258. * @return string Possibly modified $html
  259. */
  260. function _strip_newlines( $html, $data, $url ) {
  261. if ( false !== strpos( $html, "\n" ) )
  262. $html = str_replace( array( "\r\n", "\n" ), '', $html );
  263. return $html;
  264. }
  265. }
  266. /**
  267. * Returns the initialized {@link WP_oEmbed} object
  268. *
  269. * @since 2.9.0
  270. * @access private
  271. *
  272. * @see WP_oEmbed
  273. * @uses WP_oEmbed
  274. *
  275. * @return WP_oEmbed object.
  276. */
  277. function _wp_oembed_get_object() {
  278. static $wp_oembed;
  279. if ( is_null($wp_oembed) )
  280. $wp_oembed = new WP_oEmbed();
  281. return $wp_oembed;
  282. }