]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/class-oembed.php
WordPress 3.9
[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                 $providers = array(
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  ),
59                 );
60
61                 /**
62                  * Filter the list of oEmbed providers.
63                  *
64                  * Discovery is disabled for users lacking the unfiltered_html capability.
65                  * Only providers in this array will be used for those users.
66                  *
67                  * @see wp_oembed_add_provider()
68                  *
69                  * @since 2.9.0
70                  *
71                  * @param array $providers An array of popular oEmbed providers.
72                  */
73                 $this->providers = apply_filters( 'oembed_providers', $providers );
74
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 );
77         }
78
79         /**
80          * The do-it-all function that takes a URL and attempts to return the HTML.
81          *
82          * @see WP_oEmbed::discover()
83          * @see WP_oEmbed::fetch()
84          * @see WP_oEmbed::data2html()
85          *
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.
89          */
90         function get_html( $url, $args = '' ) {
91                 $provider = false;
92
93                 if ( !isset($args['discover']) )
94                         $args['discover'] = true;
95
96                 foreach ( $this->providers as $matchmask => $data ) {
97                         list( $providerurl, $regex ) = $data;
98
99                         // Turn the asterisk-type provider URLs into regex
100                         if ( !$regex ) {
101                                 $matchmask = '#' . str_replace( '___wildcard___', '(.+)', preg_quote( str_replace( '*', '___wildcard___', $matchmask ), '#' ) ) . '#i';
102                                 $matchmask = preg_replace( '|^#http\\\://|', '#https?\://', $matchmask );
103                         }
104
105                         if ( preg_match( $matchmask, $url ) ) {
106                                 $provider = str_replace( '{format}', 'json', $providerurl ); // JSON is easier to deal with than XML
107                                 break;
108                         }
109                 }
110
111                 if ( !$provider && $args['discover'] )
112                         $provider = $this->discover( $url );
113
114                 if ( !$provider || false === $data = $this->fetch( $provider, $url, $args ) )
115                         return false;
116
117                 /**
118                  * Filter the HTML returned by the oEmbed provider.
119                  *
120                  * @since 2.9.0
121                  *
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.
125                  */
126                 return apply_filters( 'oembed_result', $this->data2html( $data, $url ), $url, $args );
127         }
128
129         /**
130          * Attempts to find oEmbed provider discovery <link> tags at the given URL.
131          *
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.
134          */
135         function discover( $url ) {
136                 $providers = array();
137
138                 // Fetch URL content
139                 $request = wp_safe_remote_get( $url );
140                 if ( $html = wp_remote_retrieve_body( $request ) ) {
141
142                         /**
143                          * Filter the link types that contain oEmbed provider URLs.
144                          *
145                          * @since 2.9.0
146                          *
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).
150                          */
151                         $linktypes = apply_filters( 'oembed_linktypes', array(
152                                 'application/json+oembed' => 'json',
153                                 'text/xml+oembed' => 'xml',
154                                 'application/xml+oembed' => 'xml',
155                         ) );
156
157                         // Strip <body>
158                         $html = substr( $html, 0, stripos( $html, '</head>' ) );
159
160                         // Do a quick check
161                         $tagfound = false;
162                         foreach ( $linktypes as $linktype => $format ) {
163                                 if ( stripos($html, $linktype) ) {
164                                         $tagfound = true;
165                                         break;
166                                 }
167                         }
168
169                         if ( $tagfound && preg_match_all( '/<link([^<>]+)>/i', $html, $links ) ) {
170                                 foreach ( $links[1] as $link ) {
171                                         $atts = shortcode_parse_atts( $link );
172
173                                         if ( !empty($atts['type']) && !empty($linktypes[$atts['type']]) && !empty($atts['href']) ) {
174                                                 $providers[$linktypes[$atts['type']]] = $atts['href'];
175
176                                                 // Stop here if it's JSON (that's all we need)
177                                                 if ( 'json' == $linktypes[$atts['type']] )
178                                                         break;
179                                         }
180                                 }
181                         }
182                 }
183
184                 // JSON is preferred to XML
185                 if ( !empty($providers['json']) )
186                         return $providers['json'];
187                 elseif ( !empty($providers['xml']) )
188                         return $providers['xml'];
189                 else
190                         return false;
191         }
192
193         /**
194          * Connects to a oEmbed provider and returns the result.
195          *
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.
200          */
201         function fetch( $provider, $url, $args = '' ) {
202                 $args = wp_parse_args( $args, wp_embed_defaults() );
203
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 );
207
208                 /**
209                  * Filter the oEmbed URL to be fetched.
210                  *
211                  * @since 2.9.0
212                  *
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.
216                  */
217                 $provider = apply_filters( 'oembed_fetch_url', $provider, $url, $args );
218
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() )
222                                 continue;
223                         return ( $result && ! is_wp_error( $result ) ) ? $result : false;
224                 }
225                 return false;
226         }
227
228         /**
229          * Fetches result from an oEmbed provider for a specific format and complete provider URL
230          *
231          * @since 3.0.0
232          * @access private
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.
236          */
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 ) )
243                         return false;
244                 $parse_method = "_parse_$format";
245                 return $this->$parse_method( $body );
246         }
247
248         /**
249          * Parses a json response body.
250          *
251          * @since 3.0.0
252          * @access private
253          */
254         function _parse_json( $response_body ) {
255                 return ( ( $data = json_decode( trim( $response_body ) ) ) && is_object( $data ) ) ? $data : false;
256         }
257
258         /**
259          * Parses an XML response body.
260          *
261          * @since 3.0.0
262          * @access private
263          */
264         function _parse_xml( $response_body ) {
265                 if ( ! function_exists( 'libxml_disable_entity_loader' ) )
266                         return false;
267
268                 $loader = libxml_disable_entity_loader( true );
269                 $errors = libxml_use_internal_errors( true );
270
271                 $return = $this->_parse_xml_body( $response_body );
272
273                 libxml_use_internal_errors( $errors );
274                 libxml_disable_entity_loader( $loader );
275
276                 return $return;
277         }
278
279         /**
280          * Helper function for parsing an XML response body.
281          *
282          * @since 3.6.0
283          * @access private
284          */
285         private function _parse_xml_body( $response_body ) {
286                 if ( ! function_exists( 'simplexml_import_dom' ) || ! class_exists( 'DOMDocument' ) )
287                         return false;
288
289                 $dom = new DOMDocument;
290                 $success = $dom->loadXML( $response_body );
291                 if ( ! $success )
292                         return false;
293
294                 if ( isset( $dom->doctype ) )
295                         return false;
296
297                 foreach ( $dom->childNodes as $child ) {
298                         if ( XML_DOCUMENT_TYPE_NODE === $child->nodeType )
299                                 return false;
300                 }
301
302                 $xml = simplexml_import_dom( $dom );
303                 if ( ! $xml )
304                         return false;
305
306                 $return = new stdClass;
307                 foreach ( $xml as $key => $value ) {
308                         $return->$key = (string) $value;
309                 }
310
311                 return $return;
312         }
313
314         /**
315          * Converts a data object from {@link WP_oEmbed::fetch()} and returns the HTML.
316          *
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.
320          */
321         function data2html( $data, $url ) {
322                 if ( ! is_object( $data ) || empty( $data->type ) )
323                         return false;
324
325                 $return = false;
326
327                 switch ( $data->type ) {
328                         case 'photo':
329                                 if ( empty( $data->url ) || empty( $data->width ) || empty( $data->height ) )
330                                         break;
331                                 if ( ! is_string( $data->url ) || ! is_numeric( $data->width ) || ! is_numeric( $data->height ) )
332                                         break;
333
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>';
336                                 break;
337
338                         case 'video':
339                         case 'rich':
340                                 if ( ! empty( $data->html ) && is_string( $data->html ) )
341                                         $return = $data->html;
342                                 break;
343
344                         case 'link':
345                                 if ( ! empty( $data->title ) && is_string( $data->title ) )
346                                         $return = '<a href="' . esc_url( $url ) . '">' . esc_html( $data->title ) . '</a>';
347                                 break;
348
349                         default:
350                                 $return = false;
351                 }
352
353                 /**
354                  * Filter the returned oEmbed HTML.
355                  *
356                  * Use this filter to add support for custom data types, or to filter the result.
357                  *
358                  * @since 2.9.0
359                  *
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.
363                  */
364                 return apply_filters( 'oembed_dataparse', $return, $data, $url );
365         }
366
367         /**
368          * Strip any new lines from the HTML.
369          *
370          * @access private
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
375          */
376         function _strip_newlines( $html, $data, $url ) {
377                 if ( false !== strpos( $html, "\n" ) )
378                         $html = str_replace( array( "\r\n", "\n" ), '', $html );
379
380                 return $html;
381         }
382 }
383
384 /**
385  * Returns the initialized {@link WP_oEmbed} object
386  *
387  * @since 2.9.0
388  * @access private
389  *
390  * @see WP_oEmbed
391  * @uses WP_oEmbed
392  *
393  * @return WP_oEmbed object.
394  */
395 function _wp_oembed_get_object() {
396         static $wp_oembed;
397
398         if ( is_null($wp_oembed) )
399                 $wp_oembed = new WP_oEmbed();
400
401         return $wp_oembed;
402 }