PageRenderTime 64ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/class-oembed.php

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