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.
6 * @link http://codex.wordpress.org/oEmbed oEmbed Codex Article
7 * @link http://oembed.com/ oEmbed Homepage
21 var $providers = array();
26 * @uses apply_filters() Filters a list of pre-defined oEmbed providers.
28 function __construct() {
29 // List out some popular sites that support oEmbed.
30 // The WP_Embed class disables discovery for non-unfiltered_html users, so only providers in this array will be used for them.
31 // Add to this list using the wp_oembed_add_provider() function (see its PHPDoc for details).
32 $this->providers = apply_filters( 'oembed_providers', array(
33 '#https?://(www\.)?youtube\.com/watch.*#i' => array( 'http://www.youtube.com/oembed', true ),
34 'http://youtu.be/*' => array( 'http://www.youtube.com/oembed', false ),
35 'http://blip.tv/*' => array( 'http://blip.tv/oembed/', false ),
36 '#https?://(www\.)?vimeo\.com/.*#i' => array( 'http://vimeo.com/api/oembed.{format}', true ),
37 '#https?://(www\.)?dailymotion\.com/.*#i' => array( 'http://www.dailymotion.com/services/oembed', true ),
38 'http://dai.ly/*' => array( 'http://www.dailymotion.com/services/oembed', false ),
39 '#https?://(www\.)?flickr\.com/.*#i' => array( 'http://www.flickr.com/services/oembed/', true ),
40 'http://flic.kr/*' => array( 'http://www.flickr.com/services/oembed/', false ),
41 '#https?://(.+\.)?smugmug\.com/.*#i' => array( 'http://api.smugmug.com/services/oembed/', true ),
42 '#https?://(www\.)?hulu\.com/watch/.*#i' => array( 'http://www.hulu.com/api/oembed.{format}', true ),
43 '#https?://(www\.)?viddler\.com/.*#i' => array( 'http://lab.viddler.com/services/oembed/', true ),
44 'http://qik.com/*' => array( 'http://qik.com/api/oembed.{format}', false ),
45 'http://revision3.com/*' => array( 'http://revision3.com/api/oembed/', false ),
46 'http://i*.photobucket.com/albums/*' => array( 'http://photobucket.com/oembed', false ),
47 'http://gi*.photobucket.com/groups/*' => array( 'http://photobucket.com/oembed', false ),
48 '#https?://(www\.)?scribd\.com/.*#i' => array( 'http://www.scribd.com/services/oembed', true ),
49 'http://wordpress.tv/*' => array( 'http://wordpress.tv/oembed/', false ),
50 '#https?://(.+\.)?polldaddy\.com/.*#i' => array( 'http://polldaddy.com/oembed/', true ),
51 '#https?://(www\.)?funnyordie\.com/videos/.*#i' => array( 'http://www.funnyordie.com/oembed', true ),
52 '#https?://(www\.)?twitter\.com/.+?/status(es)?/.*#i'=> array( 'http://api.twitter.com/1/statuses/oembed.{format}', true ),
53 '#https?://(www\.)?soundcloud\.com/.*#i' => array( 'http://soundcloud.com/oembed', true ),
54 '#https?://(www\.)?slideshare\.net/*#' => array( 'http://www.slideshare.net/api/oembed/2', true ),
55 '#http://instagr(\.am|am\.com)/p/.*#i' => array( 'http://api.instagram.com/oembed', true ),
56 '#https?://(www\.)?rdio\.com/.*#i' => array( 'http://www.rdio.com/api/oembed/', true ),
57 '#https?://rd\.io/x/.*#i' => array( 'http://www.rdio.com/api/oembed/', true ),
58 '#https?://(open|play)\.spotify\.com/.*#i' => array( 'https://embed.spotify.com/oembed/', true ),
61 // Fix any embeds that contain new lines in the middle of the HTML which breaks wpautop().
62 add_filter( 'oembed_dataparse', array($this, '_strip_newlines'), 10, 3 );
66 * The do-it-all function that takes a URL and attempts to return the HTML.
68 * @see WP_oEmbed::discover()
69 * @see WP_oEmbed::fetch()
70 * @see WP_oEmbed::data2html()
72 * @param string $url The URL to the content that should be attempted to be embedded.
73 * @param array $args Optional arguments. Usually passed from a shortcode.
74 * @return bool|string False on failure, otherwise the UNSANITIZED (and potentially unsafe) HTML that should be used to embed.
76 function get_html( $url, $args = '' ) {
79 if ( !isset($args['discover']) )
80 $args['discover'] = true;
82 foreach ( $this->providers as $matchmask => $data ) {
83 list( $providerurl, $regex ) = $data;
85 // Turn the asterisk-type provider URLs into regex
87 $matchmask = '#' . str_replace( '___wildcard___', '(.+)', preg_quote( str_replace( '*', '___wildcard___', $matchmask ), '#' ) ) . '#i';
88 $matchmask = preg_replace( '|^#http\\\://|', '#https?\://', $matchmask );
91 if ( preg_match( $matchmask, $url ) ) {
92 $provider = str_replace( '{format}', 'json', $providerurl ); // JSON is easier to deal with than XML
97 if ( !$provider && $args['discover'] )
98 $provider = $this->discover( $url );
100 if ( !$provider || false === $data = $this->fetch( $provider, $url, $args ) )
103 return apply_filters( 'oembed_result', $this->data2html( $data, $url ), $url, $args );
107 * Attempts to find oEmbed provider discovery <link> tags at the given URL.
109 * @param string $url The URL that should be inspected for discovery <link> tags.
110 * @return bool|string False on failure, otherwise the oEmbed provider URL.
112 function discover( $url ) {
113 $providers = array();
116 if ( $html = wp_remote_retrieve_body( wp_safe_remote_get( $url ) ) ) {
118 // <link> types that contain oEmbed provider URLs
119 $linktypes = apply_filters( 'oembed_linktypes', array(
120 'application/json+oembed' => 'json',
121 'text/xml+oembed' => 'xml',
122 'application/xml+oembed' => 'xml', // Incorrect, but used by at least Vimeo
126 $html = substr( $html, 0, stripos( $html, '</head>' ) );
130 foreach ( $linktypes as $linktype => $format ) {
131 if ( stripos($html, $linktype) ) {
137 if ( $tagfound && preg_match_all( '/<link([^<>]+)>/i', $html, $links ) ) {
138 foreach ( $links[1] as $link ) {
139 $atts = shortcode_parse_atts( $link );
141 if ( !empty($atts['type']) && !empty($linktypes[$atts['type']]) && !empty($atts['href']) ) {
142 $providers[$linktypes[$atts['type']]] = $atts['href'];
144 // Stop here if it's JSON (that's all we need)
145 if ( 'json' == $linktypes[$atts['type']] )
152 // JSON is preferred to XML
153 if ( !empty($providers['json']) )
154 return $providers['json'];
155 elseif ( !empty($providers['xml']) )
156 return $providers['xml'];
162 * Connects to a oEmbed provider and returns the result.
164 * @param string $provider The URL to the oEmbed provider.
165 * @param string $url The URL to the content that is desired to be embedded.
166 * @param array $args Optional arguments. Usually passed from a shortcode.
167 * @return bool|object False on failure, otherwise the result in the form of an object.
169 function fetch( $provider, $url, $args = '' ) {
170 $args = wp_parse_args( $args, wp_embed_defaults() );
172 $provider = add_query_arg( 'maxwidth', (int) $args['width'], $provider );
173 $provider = add_query_arg( 'maxheight', (int) $args['height'], $provider );
174 $provider = add_query_arg( 'url', urlencode($url), $provider );
176 $provider = apply_filters( 'oembed_fetch_url', $provider, $url, $args );
178 foreach( array( 'json', 'xml' ) as $format ) {
179 $result = $this->_fetch_with_format( $provider, $format );
180 if ( is_wp_error( $result ) && 'not-implemented' == $result->get_error_code() )
182 return ( $result && ! is_wp_error( $result ) ) ? $result : false;
188 * Fetches result from an oEmbed provider for a specific format and complete provider URL
192 * @param string $provider_url_with_args URL to the provider with full arguments list (url, maxheight, etc.)
193 * @param string $format Format to use
194 * @return bool|object False on failure, otherwise the result in the form of an object.
196 function _fetch_with_format( $provider_url_with_args, $format ) {
197 $provider_url_with_args = add_query_arg( 'format', $format, $provider_url_with_args );
198 $response = wp_safe_remote_get( $provider_url_with_args );
199 if ( 501 == wp_remote_retrieve_response_code( $response ) )
200 return new WP_Error( 'not-implemented' );
201 if ( ! $body = wp_remote_retrieve_body( $response ) )
203 $parse_method = "_parse_$format";
204 return $this->$parse_method( $body );
208 * Parses a json response body.
213 function _parse_json( $response_body ) {
214 return ( ( $data = json_decode( trim( $response_body ) ) ) && is_object( $data ) ) ? $data : false;
218 * Parses an XML response body.
223 function _parse_xml( $response_body ) {
224 if ( ! function_exists( 'libxml_disable_entity_loader' ) )
227 $loader = libxml_disable_entity_loader( true );
228 $errors = libxml_use_internal_errors( true );
230 $return = $this->_parse_xml_body( $response_body );
232 libxml_use_internal_errors( $errors );
233 libxml_disable_entity_loader( $loader );
239 * Helper function for parsing an XML response body.
244 private function _parse_xml_body( $response_body ) {
245 if ( ! function_exists( 'simplexml_import_dom' ) || ! class_exists( 'DOMDocument' ) )
248 $dom = new DOMDocument;
249 $success = $dom->loadXML( $response_body );
253 if ( isset( $dom->doctype ) )
256 foreach ( $dom->childNodes as $child ) {
257 if ( XML_DOCUMENT_TYPE_NODE === $child->nodeType )
261 $xml = simplexml_import_dom( $dom );
265 $return = new stdClass;
266 foreach ( $xml as $key => $value ) {
267 $return->$key = (string) $value;
274 * Converts a data object from {@link WP_oEmbed::fetch()} and returns the HTML.
276 * @param object $data A data object result from an oEmbed provider.
277 * @param string $url The URL to the content that is desired to be embedded.
278 * @return bool|string False on error, otherwise the HTML needed to embed.
280 function data2html( $data, $url ) {
281 if ( ! is_object( $data ) || empty( $data->type ) )
286 switch ( $data->type ) {
288 if ( empty( $data->url ) || empty( $data->width ) || empty( $data->height ) )
290 if ( ! is_string( $data->url ) || ! is_numeric( $data->width ) || ! is_numeric( $data->height ) )
293 $title = ! empty( $data->title ) && is_string( $data->title ) ? $data->title : '';
294 $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>';
299 if ( ! empty( $data->html ) && is_string( $data->html ) )
300 $return = $data->html;
304 if ( ! empty( $data->title ) && is_string( $data->title ) )
305 $return = '<a href="' . esc_url( $url ) . '">' . esc_html( $data->title ) . '</a>';
312 // You can use this filter to add support for custom data types or to filter the result
313 return apply_filters( 'oembed_dataparse', $return, $data, $url );
317 * Strip any new lines from the HTML.
320 * @param string $html Existing HTML.
321 * @param object $data Data object from WP_oEmbed::data2html()
322 * @param string $url The original URL passed to oEmbed.
323 * @return string Possibly modified $html
325 function _strip_newlines( $html, $data, $url ) {
326 if ( false !== strpos( $html, "\n" ) )
327 $html = str_replace( array( "\r\n", "\n" ), '', $html );
334 * Returns the initialized {@link WP_oEmbed} object
342 * @return WP_oEmbed object.
344 function _wp_oembed_get_object() {
347 if ( is_null($wp_oembed) )
348 $wp_oembed = new WP_oEmbed();