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 '#http://(www\.)?youtube\.com/watch.*#i' => array( 'http://www.youtube.com/oembed', true ),
31 '#https://(www\.)?youtube\.com/watch.*#i' => array( 'http://www.youtube.com/oembed?scheme=https', true ),
32 '#http://youtu\.be/.*#i' => array( 'http://www.youtube.com/oembed', true ),
33 '#https://youtu\.be/.*#i' => array( 'http://www.youtube.com/oembed?scheme=https', true ),
34 'http://blip.tv/*' => array( 'http://blip.tv/oembed/', false ),
35 '#https?://(.+\.)?vimeo\.com/.*#i' => array( 'http://vimeo.com/api/oembed.{format}', true ),
36 '#https?://(www\.)?dailymotion\.com/.*#i' => array( 'http://www.dailymotion.com/services/oembed', true ),
37 'http://dai.ly/*' => array( 'http://www.dailymotion.com/services/oembed', false ),
38 '#https?://(www\.)?flickr\.com/.*#i' => array( 'http://www.flickr.com/services/oembed/', true ),
39 'http://flic.kr/*' => array( 'http://www.flickr.com/services/oembed/', false ),
40 '#https?://(.+\.)?smugmug\.com/.*#i' => array( 'http://api.smugmug.com/services/oembed/', true ),
41 '#https?://(www\.)?hulu\.com/watch/.*#i' => array( 'http://www.hulu.com/api/oembed.{format}', true ),
42 '#https?://(www\.)?viddler\.com/.*#i' => array( 'http://lab.viddler.com/services/oembed/', true ),
43 'http://revision3.com/*' => array( 'http://revision3.com/api/oembed/', false ),
44 'http://i*.photobucket.com/albums/*' => array( 'http://photobucket.com/oembed', false ),
45 'http://gi*.photobucket.com/groups/*' => array( 'http://photobucket.com/oembed', false ),
46 '#https?://(www\.)?scribd\.com/.*#i' => array( 'http://www.scribd.com/services/oembed', true ),
47 'http://wordpress.tv/*' => array( 'http://wordpress.tv/oembed/', false ),
48 '#https?://(.+\.)?polldaddy\.com/.*#i' => array( 'http://polldaddy.com/oembed/', true ),
49 '#https?://(www\.)?funnyordie\.com/videos/.*#i' => array( 'http://www.funnyordie.com/oembed', true ),
50 '#https?://(www\.)?twitter\.com/.+?/status(es)?/.*#i' => array( 'https://api.twitter.com/1/statuses/oembed.{format}', true ),
51 '#https?://(www\.)?soundcloud\.com/.*#i' => array( 'http://soundcloud.com/oembed', true ),
52 '#https?://(www\.)?slideshare\.net/*#' => array( 'http://www.slideshare.net/api/oembed/2', true ),
53 '#http://instagr(\.am|am\.com)/p/.*#i' => array( 'http://api.instagram.com/oembed', true ),
54 '#https?://(www\.)?rdio\.com/.*#i' => array( 'http://www.rdio.com/api/oembed/', true ),
55 '#https?://rd\.io/x/.*#i' => array( 'http://www.rdio.com/api/oembed/', true ),
56 '#https?://(open|play)\.spotify\.com/.*#i' => array( 'https://embed.spotify.com/oembed/', true ),
57 '#https?://(.+\.)?imgur\.com/.*#i' => array( 'http://api.imgur.com/oembed', true ),
58 '#https?://(www\.)?meetu(\.ps|p\.com)/.*#i' => array( 'http://api.meetup.com/oembed', true ),
62 * Filter the list of oEmbed providers.
64 * Discovery is disabled for users lacking the unfiltered_html capability.
65 * Only providers in this array will be used for those users.
67 * @see wp_oembed_add_provider()
71 * @param array $providers An array of popular oEmbed providers.
73 $this->providers = apply_filters( 'oembed_providers', $providers );
75 // Fix any embeds that contain new lines in the middle of the HTML which breaks wpautop().
76 add_filter( 'oembed_dataparse', array($this, '_strip_newlines'), 10, 3 );
80 * The do-it-all function that takes a URL and attempts to return the HTML.
82 * @see WP_oEmbed::discover()
83 * @see WP_oEmbed::fetch()
84 * @see WP_oEmbed::data2html()
86 * @param string $url The URL to the content that should be attempted to be embedded.
87 * @param array $args Optional arguments. Usually passed from a shortcode.
88 * @return bool|string False on failure, otherwise the UNSANITIZED (and potentially unsafe) HTML that should be used to embed.
90 function get_html( $url, $args = '' ) {
93 if ( !isset($args['discover']) )
94 $args['discover'] = true;
96 foreach ( $this->providers as $matchmask => $data ) {
97 list( $providerurl, $regex ) = $data;
99 // Turn the asterisk-type provider URLs into regex
101 $matchmask = '#' . str_replace( '___wildcard___', '(.+)', preg_quote( str_replace( '*', '___wildcard___', $matchmask ), '#' ) ) . '#i';
102 $matchmask = preg_replace( '|^#http\\\://|', '#https?\://', $matchmask );
105 if ( preg_match( $matchmask, $url ) ) {
106 $provider = str_replace( '{format}', 'json', $providerurl ); // JSON is easier to deal with than XML
111 if ( !$provider && $args['discover'] )
112 $provider = $this->discover( $url );
114 if ( !$provider || false === $data = $this->fetch( $provider, $url, $args ) )
118 * Filter the HTML returned by the oEmbed provider.
122 * @param string $data The returned oEmbed HTML.
123 * @param string $url URL of the content to be embedded.
124 * @param array $args Optional arguments, usually passed from a shortcode.
126 return apply_filters( 'oembed_result', $this->data2html( $data, $url ), $url, $args );
130 * Attempts to find oEmbed provider discovery <link> tags at the given URL.
132 * @param string $url The URL that should be inspected for discovery <link> tags.
133 * @return bool|string False on failure, otherwise the oEmbed provider URL.
135 function discover( $url ) {
136 $providers = array();
139 $request = wp_safe_remote_get( $url );
140 if ( $html = wp_remote_retrieve_body( $request ) ) {
143 * Filter the link types that contain oEmbed provider URLs.
147 * @param array $format Array of oEmbed link types. Accepts 'application/json+oembed',
148 * 'text/xml+oembed', and 'application/xml+oembed' (incorrect,
149 * used by at least Vimeo).
151 $linktypes = apply_filters( 'oembed_linktypes', array(
152 'application/json+oembed' => 'json',
153 'text/xml+oembed' => 'xml',
154 'application/xml+oembed' => 'xml',
158 $html = substr( $html, 0, stripos( $html, '</head>' ) );
162 foreach ( $linktypes as $linktype => $format ) {
163 if ( stripos($html, $linktype) ) {
169 if ( $tagfound && preg_match_all( '/<link([^<>]+)>/i', $html, $links ) ) {
170 foreach ( $links[1] as $link ) {
171 $atts = shortcode_parse_atts( $link );
173 if ( !empty($atts['type']) && !empty($linktypes[$atts['type']]) && !empty($atts['href']) ) {
174 $providers[$linktypes[$atts['type']]] = $atts['href'];
176 // Stop here if it's JSON (that's all we need)
177 if ( 'json' == $linktypes[$atts['type']] )
184 // JSON is preferred to XML
185 if ( !empty($providers['json']) )
186 return $providers['json'];
187 elseif ( !empty($providers['xml']) )
188 return $providers['xml'];
194 * Connects to a oEmbed provider and returns the result.
196 * @param string $provider The URL to the oEmbed provider.
197 * @param string $url The URL to the content that is desired to be embedded.
198 * @param array $args Optional arguments. Usually passed from a shortcode.
199 * @return bool|object False on failure, otherwise the result in the form of an object.
201 function fetch( $provider, $url, $args = '' ) {
202 $args = wp_parse_args( $args, wp_embed_defaults() );
204 $provider = add_query_arg( 'maxwidth', (int) $args['width'], $provider );
205 $provider = add_query_arg( 'maxheight', (int) $args['height'], $provider );
206 $provider = add_query_arg( 'url', urlencode($url), $provider );
209 * Filter the oEmbed URL to be fetched.
213 * @param string $provider URL of the oEmbed provider.
214 * @param string $url URL of the content to be embedded.
215 * @param array $args Optional arguments, usually passed from a shortcode.
217 $provider = apply_filters( 'oembed_fetch_url', $provider, $url, $args );
219 foreach( array( 'json', 'xml' ) as $format ) {
220 $result = $this->_fetch_with_format( $provider, $format );
221 if ( is_wp_error( $result ) && 'not-implemented' == $result->get_error_code() )
223 return ( $result && ! is_wp_error( $result ) ) ? $result : false;
229 * Fetches result from an oEmbed provider for a specific format and complete provider URL
233 * @param string $provider_url_with_args URL to the provider with full arguments list (url, maxheight, etc.)
234 * @param string $format Format to use
235 * @return bool|object False on failure, otherwise the result in the form of an object.
237 function _fetch_with_format( $provider_url_with_args, $format ) {
238 $provider_url_with_args = add_query_arg( 'format', $format, $provider_url_with_args );
239 $response = wp_safe_remote_get( $provider_url_with_args );
240 if ( 501 == wp_remote_retrieve_response_code( $response ) )
241 return new WP_Error( 'not-implemented' );
242 if ( ! $body = wp_remote_retrieve_body( $response ) )
244 $parse_method = "_parse_$format";
245 return $this->$parse_method( $body );
249 * Parses a json response body.
254 function _parse_json( $response_body ) {
255 return ( ( $data = json_decode( trim( $response_body ) ) ) && is_object( $data ) ) ? $data : false;
259 * Parses an XML response body.
264 function _parse_xml( $response_body ) {
265 if ( ! function_exists( 'libxml_disable_entity_loader' ) )
268 $loader = libxml_disable_entity_loader( true );
269 $errors = libxml_use_internal_errors( true );
271 $return = $this->_parse_xml_body( $response_body );
273 libxml_use_internal_errors( $errors );
274 libxml_disable_entity_loader( $loader );
280 * Helper function for parsing an XML response body.
285 private function _parse_xml_body( $response_body ) {
286 if ( ! function_exists( 'simplexml_import_dom' ) || ! class_exists( 'DOMDocument' ) )
289 $dom = new DOMDocument;
290 $success = $dom->loadXML( $response_body );
294 if ( isset( $dom->doctype ) )
297 foreach ( $dom->childNodes as $child ) {
298 if ( XML_DOCUMENT_TYPE_NODE === $child->nodeType )
302 $xml = simplexml_import_dom( $dom );
306 $return = new stdClass;
307 foreach ( $xml as $key => $value ) {
308 $return->$key = (string) $value;
315 * Converts a data object from {@link WP_oEmbed::fetch()} and returns the HTML.
317 * @param object $data A data object result from an oEmbed provider.
318 * @param string $url The URL to the content that is desired to be embedded.
319 * @return bool|string False on error, otherwise the HTML needed to embed.
321 function data2html( $data, $url ) {
322 if ( ! is_object( $data ) || empty( $data->type ) )
327 switch ( $data->type ) {
329 if ( empty( $data->url ) || empty( $data->width ) || empty( $data->height ) )
331 if ( ! is_string( $data->url ) || ! is_numeric( $data->width ) || ! is_numeric( $data->height ) )
334 $title = ! empty( $data->title ) && is_string( $data->title ) ? $data->title : '';
335 $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>';
340 if ( ! empty( $data->html ) && is_string( $data->html ) )
341 $return = $data->html;
345 if ( ! empty( $data->title ) && is_string( $data->title ) )
346 $return = '<a href="' . esc_url( $url ) . '">' . esc_html( $data->title ) . '</a>';
354 * Filter the returned oEmbed HTML.
356 * Use this filter to add support for custom data types, or to filter the result.
360 * @param string $return The returned oEmbed HTML.
361 * @param object $data A data object result from an oEmbed provider.
362 * @param string $url The URL of the content to be embedded.
364 return apply_filters( 'oembed_dataparse', $return, $data, $url );
368 * Strip any new lines from the HTML.
371 * @param string $html Existing HTML.
372 * @param object $data Data object from WP_oEmbed::data2html()
373 * @param string $url The original URL passed to oEmbed.
374 * @return string Possibly modified $html
376 function _strip_newlines( $html, $data, $url ) {
377 if ( false !== strpos( $html, "\n" ) )
378 $html = str_replace( array( "\r\n", "\n" ), '', $html );
385 * Returns the initialized {@link WP_oEmbed} object
393 * @return WP_oEmbed object.
395 function _wp_oembed_get_object() {
398 if ( is_null($wp_oembed) )
399 $wp_oembed = new WP_oEmbed();