PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/class-oembed.php

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