PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/blog/wp-includes/class-oembed.php

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