]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/class-oembed.php
Wordpress 3.6-scripts
[autoinstalls/wordpress.git] / wp-includes / class-oembed.php
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 /**
14  * oEmbed class.
15  *
16  * @package WordPress
17  * @subpackage oEmbed
18  * @since 2.9.0
19  */
20 class WP_oEmbed {
21         var $providers = array();
22
23         /**
24          * Constructor
25          *
26          * @uses apply_filters() Filters a list of pre-defined oEmbed providers.
27          */
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  ),
59                 ) );
60
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 );
63         }
64
65         /**
66          * The do-it-all function that takes a URL and attempts to return the HTML.
67          *
68          * @see WP_oEmbed::discover()
69          * @see WP_oEmbed::fetch()
70          * @see WP_oEmbed::data2html()
71          *
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.
75          */
76         function get_html( $url, $args = '' ) {
77                 $provider = false;
78
79                 if ( !isset($args['discover']) )
80                         $args['discover'] = true;
81
82                 foreach ( $this->providers as $matchmask => $data ) {
83                         list( $providerurl, $regex ) = $data;
84
85                         // Turn the asterisk-type provider URLs into regex
86                         if ( !$regex ) {
87                                 $matchmask = '#' . str_replace( '___wildcard___', '(.+)', preg_quote( str_replace( '*', '___wildcard___', $matchmask ), '#' ) ) . '#i';
88                                 $matchmask = preg_replace( '|^#http\\\://|', '#https?\://', $matchmask );
89                         }
90
91                         if ( preg_match( $matchmask, $url ) ) {
92                                 $provider = str_replace( '{format}', 'json', $providerurl ); // JSON is easier to deal with than XML
93                                 break;
94                         }
95                 }
96
97                 if ( !$provider && $args['discover'] )
98                         $provider = $this->discover( $url );
99
100                 if ( !$provider || false === $data = $this->fetch( $provider, $url, $args ) )
101                         return false;
102
103                 return apply_filters( 'oembed_result', $this->data2html( $data, $url ), $url, $args );
104         }
105
106         /**
107          * Attempts to find oEmbed provider discovery <link> tags at the given URL.
108          *
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.
111          */
112         function discover( $url ) {
113                 $providers = array();
114
115                 // Fetch URL content
116                 if ( $html = wp_remote_retrieve_body( wp_safe_remote_get( $url ) ) ) {
117
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
123                         ) );
124
125                         // Strip <body>
126                         $html = substr( $html, 0, stripos( $html, '</head>' ) );
127
128                         // Do a quick check
129                         $tagfound = false;
130                         foreach ( $linktypes as $linktype => $format ) {
131                                 if ( stripos($html, $linktype) ) {
132                                         $tagfound = true;
133                                         break;
134                                 }
135                         }
136
137                         if ( $tagfound && preg_match_all( '/<link([^<>]+)>/i', $html, $links ) ) {
138                                 foreach ( $links[1] as $link ) {
139                                         $atts = shortcode_parse_atts( $link );
140
141                                         if ( !empty($atts['type']) && !empty($linktypes[$atts['type']]) && !empty($atts['href']) ) {
142                                                 $providers[$linktypes[$atts['type']]] = $atts['href'];
143
144                                                 // Stop here if it's JSON (that's all we need)
145                                                 if ( 'json' == $linktypes[$atts['type']] )
146                                                         break;
147                                         }
148                                 }
149                         }
150                 }
151
152                 // JSON is preferred to XML
153                 if ( !empty($providers['json']) )
154                         return $providers['json'];
155                 elseif ( !empty($providers['xml']) )
156                         return $providers['xml'];
157                 else
158                         return false;
159         }
160
161         /**
162          * Connects to a oEmbed provider and returns the result.
163          *
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.
168          */
169         function fetch( $provider, $url, $args = '' ) {
170                 $args = wp_parse_args( $args, wp_embed_defaults() );
171
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 );
175
176                 $provider = apply_filters( 'oembed_fetch_url', $provider, $url, $args );
177
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() )
181                                 continue;
182                         return ( $result && ! is_wp_error( $result ) ) ? $result : false;
183                 }
184                 return false;
185         }
186
187         /**
188          * Fetches result from an oEmbed provider for a specific format and complete provider URL
189          *
190          * @since 3.0.0
191          * @access private
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.
195          */
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 ) )
202                         return false;
203                 $parse_method = "_parse_$format";
204                 return $this->$parse_method( $body );
205         }
206
207         /**
208          * Parses a json response body.
209          *
210          * @since 3.0.0
211          * @access private
212          */
213         function _parse_json( $response_body ) {
214                 return ( ( $data = json_decode( trim( $response_body ) ) ) && is_object( $data ) ) ? $data : false;
215         }
216
217         /**
218          * Parses an XML response body.
219          *
220          * @since 3.0.0
221          * @access private
222          */
223         function _parse_xml( $response_body ) {
224                 if ( ! function_exists( 'libxml_disable_entity_loader' ) )
225                         return false;
226
227                 $loader = libxml_disable_entity_loader( true );
228                 $errors = libxml_use_internal_errors( true );
229
230                 $return = $this->_parse_xml_body( $response_body );
231
232                 libxml_use_internal_errors( $errors );
233                 libxml_disable_entity_loader( $loader );
234
235                 return $return;
236         }
237
238         /**
239          * Helper function for parsing an XML response body.
240          *
241          * @since 3.6.0
242          * @access private
243          */
244         private function _parse_xml_body( $response_body ) {
245                 if ( ! function_exists( 'simplexml_import_dom' ) || ! class_exists( 'DOMDocument' ) )
246                         return false;
247
248                 $dom = new DOMDocument;
249                 $success = $dom->loadXML( $response_body );
250                 if ( ! $success )
251                         return false;
252
253                 if ( isset( $dom->doctype ) )
254                         return false;
255
256                 foreach ( $dom->childNodes as $child ) {
257                         if ( XML_DOCUMENT_TYPE_NODE === $child->nodeType )
258                                 return false;
259                 }
260
261                 $xml = simplexml_import_dom( $dom );
262                 if ( ! $xml )
263                         return false;
264
265                 $return = new stdClass;
266                 foreach ( $xml as $key => $value ) {
267                         $return->$key = (string) $value;
268                 }
269
270                 return $return;
271         }
272
273         /**
274          * Converts a data object from {@link WP_oEmbed::fetch()} and returns the HTML.
275          *
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.
279          */
280         function data2html( $data, $url ) {
281                 if ( ! is_object( $data ) || empty( $data->type ) )
282                         return false;
283
284                 $return = false;
285
286                 switch ( $data->type ) {
287                         case 'photo':
288                                 if ( empty( $data->url ) || empty( $data->width ) || empty( $data->height ) )
289                                         break;
290                                 if ( ! is_string( $data->url ) || ! is_numeric( $data->width ) || ! is_numeric( $data->height ) )
291                                         break;
292
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>';
295                                 break;
296
297                         case 'video':
298                         case 'rich':
299                                 if ( ! empty( $data->html ) && is_string( $data->html ) )
300                                         $return = $data->html;
301                                 break;
302
303                         case 'link':
304                                 if ( ! empty( $data->title ) && is_string( $data->title ) )
305                                         $return = '<a href="' . esc_url( $url ) . '">' . esc_html( $data->title ) . '</a>';
306                                 break;
307
308                         default:
309                                 $return = false;
310                 }
311
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 );
314         }
315
316         /**
317          * Strip any new lines from the HTML.
318          *
319          * @access private
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
324          */
325         function _strip_newlines( $html, $data, $url ) {
326                 if ( false !== strpos( $html, "\n" ) )
327                         $html = str_replace( array( "\r\n", "\n" ), '', $html );
328
329                 return $html;
330         }
331 }
332
333 /**
334  * Returns the initialized {@link WP_oEmbed} object
335  *
336  * @since 2.9.0
337  * @access private
338  *
339  * @see WP_oEmbed
340  * @uses WP_oEmbed
341  *
342  * @return WP_oEmbed object.
343  */
344 function _wp_oembed_get_object() {
345         static $wp_oembed;
346
347         if ( is_null($wp_oembed) )
348                 $wp_oembed = new WP_oEmbed();
349
350         return $wp_oembed;
351 }