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() {
30 '#https?://(www\.)?youtube\.com/watch.*#i' => array( 'http://www.youtube.com/oembed', true ),
31 'http://youtu.be/*' => array( 'http://www.youtube.com/oembed', false ),
32 'http://blip.tv/*' => array( 'http://blip.tv/oembed/', false ),
33 '#https?://(www\.)?vimeo\.com/.*#i' => array( 'http://vimeo.com/api/oembed.{format}', true ),
34 '#https?://(www\.)?dailymotion\.com/.*#i' => array( 'http://www.dailymotion.com/services/oembed', true ),
35 'http://dai.ly/*' => array( 'http://www.dailymotion.com/services/oembed', false ),
36 '#https?://(www\.)?flickr\.com/.*#i' => array( 'http://www.flickr.com/services/oembed/', true ),
37 'http://flic.kr/*' => array( 'http://www.flickr.com/services/oembed/', false ),
38 '#https?://(.+\.)?smugmug\.com/.*#i' => array( 'http://api.smugmug.com/services/oembed/', true ),
39 '#https?://(www\.)?hulu\.com/watch/.*#i' => array( 'http://www.hulu.com/api/oembed.{format}', true ),
40 '#https?://(www\.)?viddler\.com/.*#i' => array( 'http://lab.viddler.com/services/oembed/', true ),
41 'http://qik.com/*' => array( 'http://qik.com/api/oembed.{format}', false ),
42 'http://revision3.com/*' => array( 'http://revision3.com/api/oembed/', false ),
43 'http://i*.photobucket.com/albums/*' => array( 'http://photobucket.com/oembed', false ),
44 'http://gi*.photobucket.com/groups/*' => array( 'http://photobucket.com/oembed', false ),
45 '#https?://(www\.)?scribd\.com/.*#i' => array( 'http://www.scribd.com/services/oembed', true ),
46 'http://wordpress.tv/*' => array( 'http://wordpress.tv/oembed/', false ),
47 '#https?://(.+\.)?polldaddy\.com/.*#i' => array( 'http://polldaddy.com/oembed/', true ),
48 '#https?://(www\.)?funnyordie\.com/videos/.*#i' => array( 'http://www.funnyordie.com/oembed', true ),
49 '#https?://(www\.)?twitter\.com/.+?/status(es)?/.*#i'=> array( 'http://api.twitter.com/1/statuses/oembed.{format}', true ),
50 '#https?://(www\.)?soundcloud\.com/.*#i' => array( 'http://soundcloud.com/oembed', true ),
51 '#https?://(www\.)?slideshare\.net/*#' => array( 'http://www.slideshare.net/api/oembed/2', true ),
52 '#http://instagr(\.am|am\.com)/p/.*#i' => array( 'http://api.instagram.com/oembed', true ),
53 '#https?://(www\.)?rdio\.com/.*#i' => array( 'http://www.rdio.com/api/oembed/', true ),
54 '#https?://rd\.io/x/.*#i' => array( 'http://www.rdio.com/api/oembed/', true ),
55 '#https?://(open|play)\.spotify\.com/.*#i' => array( 'https://embed.spotify.com/oembed/', true ),
58 * Filter the list of oEmbed providers.
60 * Discovery is disabled for users lacking the unfiltered_html capability.
61 * Only providers in this array will be used for those users.
63 * @see wp_oembed_add_provider()
67 * @param array $providers An array of popular oEmbed providers.
69 $this->providers = apply_filters( 'oembed_providers', $providers );
71 // Fix any embeds that contain new lines in the middle of the HTML which breaks wpautop().
72 add_filter( 'oembed_dataparse', array($this, '_strip_newlines'), 10, 3 );
76 * The do-it-all function that takes a URL and attempts to return the HTML.
78 * @see WP_oEmbed::discover()
79 * @see WP_oEmbed::fetch()
80 * @see WP_oEmbed::data2html()
82 * @param string $url The URL to the content that should be attempted to be embedded.
83 * @param array $args Optional arguments. Usually passed from a shortcode.
84 * @return bool|string False on failure, otherwise the UNSANITIZED (and potentially unsafe) HTML that should be used to embed.
86 function get_html( $url, $args = '' ) {
89 if ( !isset($args['discover']) )
90 $args['discover'] = true;
92 foreach ( $this->providers as $matchmask => $data ) {
93 list( $providerurl, $regex ) = $data;
95 // Turn the asterisk-type provider URLs into regex
97 $matchmask = '#' . str_replace( '___wildcard___', '(.+)', preg_quote( str_replace( '*', '___wildcard___', $matchmask ), '#' ) ) . '#i';
98 $matchmask = preg_replace( '|^#http\\\://|', '#https?\://', $matchmask );
101 if ( preg_match( $matchmask, $url ) ) {
102 $provider = str_replace( '{format}', 'json', $providerurl ); // JSON is easier to deal with than XML
107 if ( !$provider && $args['discover'] )
108 $provider = $this->discover( $url );
110 if ( !$provider || false === $data = $this->fetch( $provider, $url, $args ) )
114 * Filter the HTML returned by the oEmbed provider.
118 * @param string $data The returned oEmbed HTML.
119 * @param string $url URL of the content to be embedded.
120 * @param array $args Optional arguments, usually passed from a shortcode.
122 return apply_filters( 'oembed_result', $this->data2html( $data, $url ), $url, $args );
126 * Attempts to find oEmbed provider discovery <link> tags at the given URL.
128 * @param string $url The URL that should be inspected for discovery <link> tags.
129 * @return bool|string False on failure, otherwise the oEmbed provider URL.
131 function discover( $url ) {
132 $providers = array();
135 if ( $html = wp_remote_retrieve_body( wp_safe_remote_get( $url ) ) ) {
138 * Filter the link types that contain oEmbed provider URLs.
142 * @param array $format Array of oEmbed link types. Accepts 'application/json+oembed',
143 * 'text/xml+oembed', and 'application/xml+oembed' (incorrect,
144 * used by at least Vimeo).
146 $linktypes = apply_filters( 'oembed_linktypes', array(
147 'application/json+oembed' => 'json',
148 'text/xml+oembed' => 'xml',
149 'application/xml+oembed' => 'xml',
153 $html = substr( $html, 0, stripos( $html, '</head>' ) );
157 foreach ( $linktypes as $linktype => $format ) {
158 if ( stripos($html, $linktype) ) {
164 if ( $tagfound && preg_match_all( '/<link([^<>]+)>/i', $html, $links ) ) {
165 foreach ( $links[1] as $link ) {
166 $atts = shortcode_parse_atts( $link );
168 if ( !empty($atts['type']) && !empty($linktypes[$atts['type']]) && !empty($atts['href']) ) {
169 $providers[$linktypes[$atts['type']]] = $atts['href'];
171 // Stop here if it's JSON (that's all we need)
172 if ( 'json' == $linktypes[$atts['type']] )
179 // JSON is preferred to XML
180 if ( !empty($providers['json']) )
181 return $providers['json'];
182 elseif ( !empty($providers['xml']) )
183 return $providers['xml'];
189 * Connects to a oEmbed provider and returns the result.
191 * @param string $provider The URL to the oEmbed provider.
192 * @param string $url The URL to the content that is desired to be embedded.
193 * @param array $args Optional arguments. Usually passed from a shortcode.
194 * @return bool|object False on failure, otherwise the result in the form of an object.
196 function fetch( $provider, $url, $args = '' ) {
197 $args = wp_parse_args( $args, wp_embed_defaults() );
199 $provider = add_query_arg( 'maxwidth', (int) $args['width'], $provider );
200 $provider = add_query_arg( 'maxheight', (int) $args['height'], $provider );
201 $provider = add_query_arg( 'url', urlencode($url), $provider );
204 * Filter the oEmbed URL to be fetched.
208 * @param string $provider URL of the oEmbed provider.
209 * @param string $url URL of the content to be embedded.
210 * @param array $args Optional arguments, usually passed from a shortcode.
212 $provider = apply_filters( 'oembed_fetch_url', $provider, $url, $args );
214 foreach( array( 'json', 'xml' ) as $format ) {
215 $result = $this->_fetch_with_format( $provider, $format );
216 if ( is_wp_error( $result ) && 'not-implemented' == $result->get_error_code() )
218 return ( $result && ! is_wp_error( $result ) ) ? $result : false;
224 * Fetches result from an oEmbed provider for a specific format and complete provider URL
228 * @param string $provider_url_with_args URL to the provider with full arguments list (url, maxheight, etc.)
229 * @param string $format Format to use
230 * @return bool|object False on failure, otherwise the result in the form of an object.
232 function _fetch_with_format( $provider_url_with_args, $format ) {
233 $provider_url_with_args = add_query_arg( 'format', $format, $provider_url_with_args );
234 $response = wp_safe_remote_get( $provider_url_with_args );
235 if ( 501 == wp_remote_retrieve_response_code( $response ) )
236 return new WP_Error( 'not-implemented' );
237 if ( ! $body = wp_remote_retrieve_body( $response ) )
239 $parse_method = "_parse_$format";
240 return $this->$parse_method( $body );
244 * Parses a json response body.
249 function _parse_json( $response_body ) {
250 return ( ( $data = json_decode( trim( $response_body ) ) ) && is_object( $data ) ) ? $data : false;
254 * Parses an XML response body.
259 function _parse_xml( $response_body ) {
260 if ( ! function_exists( 'libxml_disable_entity_loader' ) )
263 $loader = libxml_disable_entity_loader( true );
264 $errors = libxml_use_internal_errors( true );
266 $return = $this->_parse_xml_body( $response_body );
268 libxml_use_internal_errors( $errors );
269 libxml_disable_entity_loader( $loader );
275 * Helper function for parsing an XML response body.
280 private function _parse_xml_body( $response_body ) {
281 if ( ! function_exists( 'simplexml_import_dom' ) || ! class_exists( 'DOMDocument' ) )
284 $dom = new DOMDocument;
285 $success = $dom->loadXML( $response_body );
289 if ( isset( $dom->doctype ) )
292 foreach ( $dom->childNodes as $child ) {
293 if ( XML_DOCUMENT_TYPE_NODE === $child->nodeType )
297 $xml = simplexml_import_dom( $dom );
301 $return = new stdClass;
302 foreach ( $xml as $key => $value ) {
303 $return->$key = (string) $value;
310 * Converts a data object from {@link WP_oEmbed::fetch()} and returns the HTML.
312 * @param object $data A data object result from an oEmbed provider.
313 * @param string $url The URL to the content that is desired to be embedded.
314 * @return bool|string False on error, otherwise the HTML needed to embed.
316 function data2html( $data, $url ) {
317 if ( ! is_object( $data ) || empty( $data->type ) )
322 switch ( $data->type ) {
324 if ( empty( $data->url ) || empty( $data->width ) || empty( $data->height ) )
326 if ( ! is_string( $data->url ) || ! is_numeric( $data->width ) || ! is_numeric( $data->height ) )
329 $title = ! empty( $data->title ) && is_string( $data->title ) ? $data->title : '';
330 $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>';
335 if ( ! empty( $data->html ) && is_string( $data->html ) )
336 $return = $data->html;
340 if ( ! empty( $data->title ) && is_string( $data->title ) )
341 $return = '<a href="' . esc_url( $url ) . '">' . esc_html( $data->title ) . '</a>';
349 * Filter the returned oEmbed HTML.
351 * Use this filter to add support for custom data types, or to filter the result.
355 * @param string $return The returned oEmbed HTML.
356 * @param object $data A data object result from an oEmbed provider.
357 * @param string $url The URL of the content to be embedded.
359 return apply_filters( 'oembed_dataparse', $return, $data, $url );
363 * Strip any new lines from the HTML.
366 * @param string $html Existing HTML.
367 * @param object $data Data object from WP_oEmbed::data2html()
368 * @param string $url The original URL passed to oEmbed.
369 * @return string Possibly modified $html
371 function _strip_newlines( $html, $data, $url ) {
372 if ( false !== strpos( $html, "\n" ) )
373 $html = str_replace( array( "\r\n", "\n" ), '', $html );
380 * Returns the initialized {@link WP_oEmbed} object
388 * @return WP_oEmbed object.
390 function _wp_oembed_get_object() {
393 if ( is_null($wp_oembed) )
394 $wp_oembed = new WP_oEmbed();