]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/class-oembed.php
WordPress 4.7.2-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  *
5  * Used internally by the WP_Embed class, but is designed to be generic.
6  *
7  * @link https://codex.wordpress.org/oEmbed oEmbed Codex Article
8  * @link http://oembed.com/ oEmbed Homepage
9  *
10  * @package WordPress
11  * @subpackage oEmbed
12  */
13
14 /**
15  * Core class used to implement oEmbed functionality.
16  *
17  * @since 2.9.0
18  */
19 class WP_oEmbed {
20
21         /**
22          * A list of oEmbed providers.
23          *
24          * @since 2.9.0
25          * @access public
26          * @var array
27          */
28         public $providers = array();
29
30         /**
31          * A list of an early oEmbed providers.
32          *
33          * @since 4.0.0
34          * @access public
35          * @static
36          * @var array
37          */
38         public static $early_providers = array();
39
40         /**
41          * A list of private/protected methods, used for backward compatibility.
42          *
43          * @since 4.2.0
44          * @access private
45          * @var array
46          */
47         private $compat_methods = array( '_fetch_with_format', '_parse_json', '_parse_xml', '_parse_body' );
48
49         /**
50          * Constructor.
51          *
52          * @since 2.9.0
53          * @access public
54          */
55         public function __construct() {
56                 $host = urlencode( home_url() );
57                 $providers = array(
58                         '#https?://((m|www)\.)?youtube\.com/watch.*#i'             => array( 'https://www.youtube.com/oembed',                            true  ),
59                         '#https?://((m|www)\.)?youtube\.com/playlist.*#i'          => array( 'https://www.youtube.com/oembed',                            true  ),
60                         '#https?://youtu\.be/.*#i'                                 => array( 'https://www.youtube.com/oembed',                            true  ),
61                         '#https?://(.+\.)?vimeo\.com/.*#i'                         => array( 'https://vimeo.com/api/oembed.{format}',                     true  ),
62                         '#https?://(www\.)?dailymotion\.com/.*#i'                  => array( 'https://www.dailymotion.com/services/oembed',               true  ),
63                         '#https?://dai\.ly/.*#i'                                   => array( 'https://www.dailymotion.com/services/oembed',               true  ),
64                         '#https?://(www\.)?flickr\.com/.*#i'                       => array( 'https://www.flickr.com/services/oembed/',                   true  ),
65                         '#https?://flic\.kr/.*#i'                                  => array( 'https://www.flickr.com/services/oembed/',                   true  ),
66                         '#https?://(.+\.)?smugmug\.com/.*#i'                       => array( 'https://api.smugmug.com/services/oembed/',                  true  ),
67                         '#https?://(www\.)?hulu\.com/watch/.*#i'                   => array( 'http://www.hulu.com/api/oembed.{format}',                   true  ),
68                         'http://i*.photobucket.com/albums/*'                       => array( 'http://api.photobucket.com/oembed',                         false ),
69                         'http://gi*.photobucket.com/groups/*'                      => array( 'http://api.photobucket.com/oembed',                         false ),
70                         '#https?://(www\.)?scribd\.com/doc/.*#i'                   => array( 'https://www.scribd.com/services/oembed',                    true  ),
71                         '#https?://wordpress\.tv/.*#i'                             => array( 'https://wordpress.tv/oembed/',                              true  ),
72                         '#https?://(.+\.)?polldaddy\.com/.*#i'                     => array( 'https://polldaddy.com/oembed/',                             true  ),
73                         '#https?://poll\.fm/.*#i'                                  => array( 'https://polldaddy.com/oembed/',                             true  ),
74                         '#https?://(www\.)?funnyordie\.com/videos/.*#i'            => array( 'http://www.funnyordie.com/oembed',                          true  ),
75                         '#https?://(www\.)?twitter\.com/\w{1,15}/status(es)?/.*#i' => array( 'https://publish.twitter.com/oembed',                        true  ),
76                         '#https?://(www\.)?twitter\.com/\w{1,15}$#i'               => array( 'https://publish.twitter.com/oembed',                        true  ),
77                         '#https?://(www\.)?twitter\.com/\w{1,15}/likes$#i'         => array( 'https://publish.twitter.com/oembed',                        true  ),
78                         '#https?://(www\.)?twitter\.com/\w{1,15}/lists/.*#i'       => array( 'https://publish.twitter.com/oembed',                        true  ),
79                         '#https?://(www\.)?twitter\.com/\w{1,15}/timelines/.*#i'   => array( 'https://publish.twitter.com/oembed',                        true  ),
80                         '#https?://(www\.)?twitter\.com/i/moments/.*#i'            => array( 'https://publish.twitter.com/oembed',                        true  ),
81                         '#https?://vine\.co/v/.*#i'                                => array( 'https://vine.co/oembed.{format}',                           true  ),
82                         '#https?://(www\.)?soundcloud\.com/.*#i'                   => array( 'https://soundcloud.com/oembed',                             true  ),
83                         '#https?://(.+?\.)?slideshare\.net/.*#i'                   => array( 'https://www.slideshare.net/api/oembed/2',                   true  ),
84                         '#https?://(www\.)?instagr(\.am|am\.com)/p/.*#i'           => array( 'https://api.instagram.com/oembed',                          true  ),
85                         '#https?://(open|play)\.spotify\.com/.*#i'                 => array( 'https://embed.spotify.com/oembed/',                         true  ),
86                         '#https?://(.+\.)?imgur\.com/.*#i'                         => array( 'http://api.imgur.com/oembed',                               true  ),
87                         '#https?://(www\.)?meetu(\.ps|p\.com)/.*#i'                => array( 'https://api.meetup.com/oembed',                             true  ),
88                         '#https?://(www\.)?issuu\.com/.+/docs/.+#i'                => array( 'https://issuu.com/oembed_wp',                               true  ),
89                         '#https?://(www\.)?collegehumor\.com/video/.*#i'           => array( 'http://www.collegehumor.com/oembed.{format}',               true  ),
90                         '#https?://(www\.)?mixcloud\.com/.*#i'                     => array( 'https://www.mixcloud.com/oembed',                           true  ),
91                         '#https?://(www\.|embed\.)?ted\.com/talks/.*#i'            => array( 'https://www.ted.com/services/v1/oembed.{format}',           true  ),
92                         '#https?://(www\.)?(animoto|video214)\.com/play/.*#i'      => array( 'https://animoto.com/oembeds/create',                        true  ),
93                         '#https?://(.+)\.tumblr\.com/post/.*#i'                    => array( 'https://www.tumblr.com/oembed/1.0',                         true  ),
94                         '#https?://(www\.)?kickstarter\.com/projects/.*#i'         => array( 'https://www.kickstarter.com/services/oembed',               true  ),
95                         '#https?://kck\.st/.*#i'                                   => array( 'https://www.kickstarter.com/services/oembed',               true  ),
96                         '#https?://cloudup\.com/.*#i'                              => array( 'https://cloudup.com/oembed',                                true  ),
97                         '#https?://(www\.)?reverbnation\.com/.*#i'                 => array( 'https://www.reverbnation.com/oembed',                       true  ),
98                         '#https?://videopress\.com/v/.*#'                          => array( 'https://public-api.wordpress.com/oembed/1.0/?for=' . $host, true  ),
99                         '#https?://(www\.)?reddit\.com/r/[^/]+/comments/.*#i'      => array( 'https://www.reddit.com/oembed',                             true  ),
100                         '#https?://(www\.)?speakerdeck\.com/.*#i'                  => array( 'https://speakerdeck.com/oembed.{format}',                   true  ),
101                         '#https?://www\.facebook\.com/.*/posts/.*#i'               => array( 'https://www.facebook.com/plugins/post/oembed.json/',        true  ),
102                         '#https?://www\.facebook\.com/.*/activity/.*#i'            => array( 'https://www.facebook.com/plugins/post/oembed.json/',        true  ),
103                         '#https?://www\.facebook\.com/.*/photos/.*#i'              => array( 'https://www.facebook.com/plugins/post/oembed.json/',        true  ),
104                         '#https?://www\.facebook\.com/photo(s/|\.php).*#i'         => array( 'https://www.facebook.com/plugins/post/oembed.json/',        true  ),
105                         '#https?://www\.facebook\.com/permalink\.php.*#i'          => array( 'https://www.facebook.com/plugins/post/oembed.json/',        true  ),
106                         '#https?://www\.facebook\.com/media/.*#i'                  => array( 'https://www.facebook.com/plugins/post/oembed.json/',        true  ),
107                         '#https?://www\.facebook\.com/questions/.*#i'              => array( 'https://www.facebook.com/plugins/post/oembed.json/',        true  ),
108                         '#https?://www\.facebook\.com/notes/.*#i'                  => array( 'https://www.facebook.com/plugins/post/oembed.json/',        true  ),
109                         '#https?://www\.facebook\.com/.*/videos/.*#i'              => array( 'https://www.facebook.com/plugins/video/oembed.json/',       true  ),
110                         '#https?://www\.facebook\.com/video\.php.*#i'              => array( 'https://www.facebook.com/plugins/video/oembed.json/',       true  ),
111                 );
112
113                 if ( ! empty( self::$early_providers['add'] ) ) {
114                         foreach ( self::$early_providers['add'] as $format => $data ) {
115                                 $providers[ $format ] = $data;
116                         }
117                 }
118
119                 if ( ! empty( self::$early_providers['remove'] ) ) {
120                         foreach ( self::$early_providers['remove'] as $format ) {
121                                 unset( $providers[ $format ] );
122                         }
123                 }
124
125                 self::$early_providers = array();
126
127                 /**
128                  * Filters the list of whitelisted oEmbed providers.
129                  *
130                  * Since WordPress 4.4, oEmbed discovery is enabled for all users and allows embedding of sanitized
131                  * iframes. The providers in this list are whitelisted, meaning they are trusted and allowed to
132                  * embed any content, such as iframes, videos, JavaScript, and arbitrary HTML.
133                  *
134                  * Supported providers:
135                  *
136                  * |   Provider   |        Flavor         | Supports HTTPS |   Since   |
137                  * | ------------ | --------------------- | :------------: | --------- |
138                  * | Dailymotion  | dailymotion.com       |      Yes       | 2.9.0     |
139                  * | Flickr       | flickr.com            |      Yes       | 2.9.0     |
140                  * | Hulu         | hulu.com              |      Yes       | 2.9.0     |
141                  * | Photobucket  | photobucket.com       |      No        | 2.9.0     |
142                  * | Scribd       | scribd.com            |      Yes       | 2.9.0     |
143                  * | Vimeo        | vimeo.com             |      Yes       | 2.9.0     |
144                  * | WordPress.tv | wordpress.tv          |      Yes       | 2.9.0     |
145                  * | YouTube      | youtube.com/watch     |      Yes       | 2.9.0     |
146                  * | Funny or Die | funnyordie.com        |      Yes       | 3.0.0     |
147                  * | Polldaddy    | polldaddy.com         |      Yes       | 3.0.0     |
148                  * | SmugMug      | smugmug.com           |      Yes       | 3.0.0     |
149                  * | YouTube      | youtu.be              |      Yes       | 3.0.0     |
150                  * | Twitter      | twitter.com           |      Yes       | 3.4.0     |
151                  * | Instagram    | instagram.com         |      Yes       | 3.5.0     |
152                  * | Instagram    | instagr.am            |      Yes       | 3.5.0     |
153                  * | Slideshare   | slideshare.net        |      Yes       | 3.5.0     |
154                  * | SoundCloud   | soundcloud.com        |      Yes       | 3.5.0     |
155                  * | Dailymotion  | dai.ly                |      Yes       | 3.6.0     |
156                  * | Flickr       | flic.kr               |      Yes       | 3.6.0     |
157                  * | Spotify      | spotify.com           |      Yes       | 3.6.0     |
158                  * | Imgur        | imgur.com             |      Yes       | 3.9.0     |
159                  * | Meetup.com   | meetup.com            |      Yes       | 3.9.0     |
160                  * | Meetup.com   | meetu.ps              |      Yes       | 3.9.0     |
161                  * | Animoto      | animoto.com           |      Yes       | 4.0.0     |
162                  * | Animoto      | video214.com          |      Yes       | 4.0.0     |
163                  * | CollegeHumor | collegehumor.com      |      Yes       | 4.0.0     |
164                  * | Issuu        | issuu.com             |      Yes       | 4.0.0     |
165                  * | Mixcloud     | mixcloud.com          |      Yes       | 4.0.0     |
166                  * | Polldaddy    | poll.fm               |      Yes       | 4.0.0     |
167                  * | TED          | ted.com               |      Yes       | 4.0.0     |
168                  * | YouTube      | youtube.com/playlist  |      Yes       | 4.0.0     |
169                  * | Vine         | vine.co               |      Yes       | 4.1.0     |
170                  * | Tumblr       | tumblr.com            |      Yes       | 4.2.0     |
171                  * | Kickstarter  | kickstarter.com       |      Yes       | 4.2.0     |
172                  * | Kickstarter  | kck.st                |      Yes       | 4.2.0     |
173                  * | Cloudup      | cloudup.com           |      Yes       | 4.3.0     |
174                  * | ReverbNation | reverbnation.com      |      Yes       | 4.4.0     |
175                  * | VideoPress   | videopress.com        |      Yes       | 4.4.0     |
176                  * | Reddit       | reddit.com            |      Yes       | 4.4.0     |
177                  * | Speaker Deck | speakerdeck.com       |      Yes       | 4.4.0     |
178                  * | Twitter      | twitter.com/timelines |      Yes       | 4.5.0     |
179                  * | Twitter      | twitter.com/moments   |      Yes       | 4.5.0     |
180                  * | Facebook     | facebook.com          |      Yes       | 4.7.0     |
181                  * | Twitter      | twitter.com/user      |      Yes       | 4.7.0     |
182                  * | Twitter      | twitter.com/likes     |      Yes       | 4.7.0     |
183                  * | Twitter      | twitter.com/lists     |      Yes       | 4.7.0     |
184                  *
185                  * No longer supported providers:
186                  *
187                  * |   Provider   |        Flavor        | Supports HTTPS |   Since   |  Removed  |
188                  * | ------------ | -------------------- | :------------: | --------- | --------- |
189                  * | Qik          | qik.com              |      Yes       | 2.9.0     | 3.9.0     |
190                  * | Viddler      | viddler.com          |      Yes       | 2.9.0     | 4.0.0     |
191                  * | Revision3    | revision3.com        |      No        | 2.9.0     | 4.2.0     |
192                  * | Blip         | blip.tv              |      No        | 2.9.0     | 4.4.0     |
193                  * | Rdio         | rdio.com             |      Yes       | 3.6.0     | 4.4.1     |
194                  * | Rdio         | rd.io                |      Yes       | 3.6.0     | 4.4.1     |
195                  *
196                  * @see wp_oembed_add_provider()
197                  *
198                  * @since 2.9.0
199                  *
200                  * @param array $providers An array of popular oEmbed providers.
201                  */
202                 $this->providers = apply_filters( 'oembed_providers', $providers );
203
204                 // Fix any embeds that contain new lines in the middle of the HTML which breaks wpautop().
205                 add_filter( 'oembed_dataparse', array($this, '_strip_newlines'), 10, 3 );
206         }
207
208         /**
209          * Exposes private/protected methods for backward compatibility.
210          *
211          * @since 4.0.0
212          * @access public
213          *
214          * @param callable $name      Method to call.
215          * @param array    $arguments Arguments to pass when calling.
216          * @return mixed|bool Return value of the callback, false otherwise.
217          */
218         public function __call( $name, $arguments ) {
219                 if ( in_array( $name, $this->compat_methods ) ) {
220                         return call_user_func_array( array( $this, $name ), $arguments );
221                 }
222                 return false;
223         }
224
225         /**
226          * Takes a URL and returns the corresponding oEmbed provider's URL, if there is one.
227          *
228          * @since 4.0.0
229          * @access public
230          *
231          * @see WP_oEmbed::discover()
232          *
233          * @param string        $url  The URL to the content.
234          * @param string|array  $args Optional provider arguments.
235          * @return false|string False on failure, otherwise the oEmbed provider URL.
236          */
237         public function get_provider( $url, $args = '' ) {
238                 $args = wp_parse_args( $args );
239
240                 $provider = false;
241
242                 if ( !isset($args['discover']) )
243                         $args['discover'] = true;
244
245                 foreach ( $this->providers as $matchmask => $data ) {
246                         list( $providerurl, $regex ) = $data;
247
248                         // Turn the asterisk-type provider URLs into regex
249                         if ( !$regex ) {
250                                 $matchmask = '#' . str_replace( '___wildcard___', '(.+)', preg_quote( str_replace( '*', '___wildcard___', $matchmask ), '#' ) ) . '#i';
251                                 $matchmask = preg_replace( '|^#http\\\://|', '#https?\://', $matchmask );
252                         }
253
254                         if ( preg_match( $matchmask, $url ) ) {
255                                 $provider = str_replace( '{format}', 'json', $providerurl ); // JSON is easier to deal with than XML
256                                 break;
257                         }
258                 }
259
260                 if ( !$provider && $args['discover'] )
261                         $provider = $this->discover( $url );
262
263                 return $provider;
264         }
265
266         /**
267          * Adds an oEmbed provider.
268          *
269          * The provider is added just-in-time when wp_oembed_add_provider() is called before
270          * the {@see 'plugins_loaded'} hook.
271          *
272          * The just-in-time addition is for the benefit of the {@see 'oembed_providers'} filter.
273          *
274          * @static
275          * @since 4.0.0
276          * @access public
277          *
278          * @see wp_oembed_add_provider()
279          *
280          * @param string $format   Format of URL that this provider can handle. You can use
281          *                         asterisks as wildcards.
282          * @param string $provider The URL to the oEmbed provider..
283          * @param bool   $regex    Optional. Whether the $format parameter is in a regex format.
284          *                         Default false.
285          */
286         public static function _add_provider_early( $format, $provider, $regex = false ) {
287                 if ( empty( self::$early_providers['add'] ) ) {
288                         self::$early_providers['add'] = array();
289                 }
290
291                 self::$early_providers['add'][ $format ] = array( $provider, $regex );
292         }
293
294         /**
295          * Removes an oEmbed provider.
296          *
297          * The provider is removed just-in-time when wp_oembed_remove_provider() is called before
298          * the {@see 'plugins_loaded'} hook.
299          *
300          * The just-in-time removal is for the benefit of the {@see 'oembed_providers'} filter.
301          *
302          * @since 4.0.0
303          * @access public
304          * @static
305          *
306          * @see wp_oembed_remove_provider()
307          *
308          * @param string $format The format of URL that this provider can handle. You can use
309          *                       asterisks as wildcards.
310          */
311         public static function _remove_provider_early( $format ) {
312                 if ( empty( self::$early_providers['remove'] ) ) {
313                         self::$early_providers['remove'] = array();
314                 }
315
316                 self::$early_providers['remove'][] = $format;
317         }
318
319         /**
320          * The do-it-all function that takes a URL and attempts to return the HTML.
321          *
322          * @see WP_oEmbed::fetch()
323          * @see WP_oEmbed::data2html()
324          *
325          * @since 2.9.0
326          * @access public
327          *
328          * @param string       $url  The URL to the content that should be attempted to be embedded.
329          * @param array|string $args Optional. Arguments, usually passed from a shortcode. Default empty.
330          * @return false|string False on failure, otherwise the UNSANITIZED (and potentially unsafe) HTML that should be used to embed.
331          */
332         public function get_html( $url, $args = '' ) {
333                 $args = wp_parse_args( $args );
334
335                 /**
336                  * Filters the oEmbed result before any HTTP requests are made.
337                  *
338                  * This allows one to short-circuit the default logic, perhaps by
339                  * replacing it with a routine that is more optimal for your setup.
340                  *
341                  * Passing a non-null value to the filter will effectively short-circuit retrieval,
342                  * returning the passed value instead.
343                  *
344                  * @since 4.5.3
345                  *
346                  * @param null|string $result The UNSANITIZED (and potentially unsafe) HTML that should be used to embed. Default null.
347                  * @param string      $url    The URL to the content that should be attempted to be embedded.
348                  * @param array       $args   Optional. Arguments, usually passed from a shortcode. Default empty.
349                  */
350                 $pre = apply_filters( 'pre_oembed_result', null, $url, $args );
351
352                 if ( null !== $pre ) {
353                         return $pre;
354                 }
355
356                 $provider = $this->get_provider( $url, $args );
357
358                 if ( ! $provider || false === $data = $this->fetch( $provider, $url, $args ) ) {
359                         return false;
360                 }
361
362                 /**
363                  * Filters the HTML returned by the oEmbed provider.
364                  *
365                  * @since 2.9.0
366                  *
367                  * @param string $data The returned oEmbed HTML.
368                  * @param string $url  URL of the content to be embedded.
369                  * @param array  $args Optional arguments, usually passed from a shortcode.
370                  */
371                 return apply_filters( 'oembed_result', $this->data2html( $data, $url ), $url, $args );
372         }
373
374         /**
375          * Attempts to discover link tags at the given URL for an oEmbed provider.
376          *
377          * @since 2.9.0
378          * @access public
379          *
380          * @param string $url The URL that should be inspected for discovery `<link>` tags.
381          * @return false|string False on failure, otherwise the oEmbed provider URL.
382          */
383         public function discover( $url ) {
384                 $providers = array();
385                 $args = array(
386                         'limit_response_size' => 153600, // 150 KB
387                 );
388
389                 /**
390                  * Filters oEmbed remote get arguments.
391                  *
392                  * @since 4.0.0
393                  *
394                  * @see WP_Http::request()
395                  *
396                  * @param array  $args oEmbed remote get arguments.
397                  * @param string $url  URL to be inspected.
398                  */
399                 $args = apply_filters( 'oembed_remote_get_args', $args, $url );
400
401                 // Fetch URL content
402                 $request = wp_safe_remote_get( $url, $args );
403                 if ( $html = wp_remote_retrieve_body( $request ) ) {
404
405                         /**
406                          * Filters the link types that contain oEmbed provider URLs.
407                          *
408                          * @since 2.9.0
409                          *
410                          * @param array $format Array of oEmbed link types. Accepts 'application/json+oembed',
411                          *                      'text/xml+oembed', and 'application/xml+oembed' (incorrect,
412                          *                      used by at least Vimeo).
413                          */
414                         $linktypes = apply_filters( 'oembed_linktypes', array(
415                                 'application/json+oembed' => 'json',
416                                 'text/xml+oembed' => 'xml',
417                                 'application/xml+oembed' => 'xml',
418                         ) );
419
420                         // Strip <body>
421                         if ( $html_head_end = stripos( $html, '</head>' ) ) {
422                                 $html = substr( $html, 0, $html_head_end );
423                         }
424
425                         // Do a quick check
426                         $tagfound = false;
427                         foreach ( $linktypes as $linktype => $format ) {
428                                 if ( stripos($html, $linktype) ) {
429                                         $tagfound = true;
430                                         break;
431                                 }
432                         }
433
434                         if ( $tagfound && preg_match_all( '#<link([^<>]+)/?>#iU', $html, $links ) ) {
435                                 foreach ( $links[1] as $link ) {
436                                         $atts = shortcode_parse_atts( $link );
437
438                                         if ( !empty($atts['type']) && !empty($linktypes[$atts['type']]) && !empty($atts['href']) ) {
439                                                 $providers[$linktypes[$atts['type']]] = htmlspecialchars_decode( $atts['href'] );
440
441                                                 // Stop here if it's JSON (that's all we need)
442                                                 if ( 'json' == $linktypes[$atts['type']] )
443                                                         break;
444                                         }
445                                 }
446                         }
447                 }
448
449                 // JSON is preferred to XML
450                 if ( !empty($providers['json']) )
451                         return $providers['json'];
452                 elseif ( !empty($providers['xml']) )
453                         return $providers['xml'];
454                 else
455                         return false;
456         }
457
458         /**
459          * Connects to a oEmbed provider and returns the result.
460          *
461          * @since 2.9.0
462          * @access public
463          *
464          * @param string       $provider The URL to the oEmbed provider.
465          * @param string       $url      The URL to the content that is desired to be embedded.
466          * @param array|string $args     Optional. Arguments, usually passed from a shortcode. Default empty.
467          * @return false|object False on failure, otherwise the result in the form of an object.
468          */
469         public function fetch( $provider, $url, $args = '' ) {
470                 $args = wp_parse_args( $args, wp_embed_defaults( $url ) );
471
472                 $provider = add_query_arg( 'maxwidth', (int) $args['width'], $provider );
473                 $provider = add_query_arg( 'maxheight', (int) $args['height'], $provider );
474                 $provider = add_query_arg( 'url', urlencode($url), $provider );
475
476                 /**
477                  * Filters the oEmbed URL to be fetched.
478                  *
479                  * @since 2.9.0
480                  *
481                  * @param string $provider URL of the oEmbed provider.
482                  * @param string $url      URL of the content to be embedded.
483                  * @param array  $args     Optional arguments, usually passed from a shortcode.
484                  */
485                 $provider = apply_filters( 'oembed_fetch_url', $provider, $url, $args );
486
487                 foreach ( array( 'json', 'xml' ) as $format ) {
488                         $result = $this->_fetch_with_format( $provider, $format );
489                         if ( is_wp_error( $result ) && 'not-implemented' == $result->get_error_code() )
490                                 continue;
491                         return ( $result && ! is_wp_error( $result ) ) ? $result : false;
492                 }
493                 return false;
494         }
495
496         /**
497          * Fetches result from an oEmbed provider for a specific format and complete provider URL
498          *
499          * @since 3.0.0
500          * @access private
501          *
502          * @param string $provider_url_with_args URL to the provider with full arguments list (url, maxheight, etc.)
503          * @param string $format Format to use
504          * @return false|object|WP_Error False on failure, otherwise the result in the form of an object.
505          */
506         private function _fetch_with_format( $provider_url_with_args, $format ) {
507                 $provider_url_with_args = add_query_arg( 'format', $format, $provider_url_with_args );
508
509                 /** This filter is documented in wp-includes/class-oembed.php */
510                 $args = apply_filters( 'oembed_remote_get_args', array(), $provider_url_with_args );
511
512                 $response = wp_safe_remote_get( $provider_url_with_args, $args );
513                 if ( 501 == wp_remote_retrieve_response_code( $response ) )
514                         return new WP_Error( 'not-implemented' );
515                 if ( ! $body = wp_remote_retrieve_body( $response ) )
516                         return false;
517                 $parse_method = "_parse_$format";
518                 return $this->$parse_method( $body );
519         }
520
521         /**
522          * Parses a json response body.
523          *
524          * @since 3.0.0
525          * @access private
526          *
527          * @param string $response_body
528          * @return object|false
529          */
530         private function _parse_json( $response_body ) {
531                 $data = json_decode( trim( $response_body ) );
532                 return ( $data && is_object( $data ) ) ? $data : false;
533         }
534
535         /**
536          * Parses an XML response body.
537          *
538          * @since 3.0.0
539          * @access private
540          *
541          * @param string $response_body
542          * @return object|false
543          */
544         private function _parse_xml( $response_body ) {
545                 if ( ! function_exists( 'libxml_disable_entity_loader' ) )
546                         return false;
547
548                 $loader = libxml_disable_entity_loader( true );
549                 $errors = libxml_use_internal_errors( true );
550
551                 $return = $this->_parse_xml_body( $response_body );
552
553                 libxml_use_internal_errors( $errors );
554                 libxml_disable_entity_loader( $loader );
555
556                 return $return;
557         }
558
559         /**
560          * Serves as a helper function for parsing an XML response body.
561          *
562          * @since 3.6.0
563          * @access private
564          *
565          * @param string $response_body
566          * @return stdClass|false
567          */
568         private function _parse_xml_body( $response_body ) {
569                 if ( ! function_exists( 'simplexml_import_dom' ) || ! class_exists( 'DOMDocument', false ) )
570                         return false;
571
572                 $dom = new DOMDocument;
573                 $success = $dom->loadXML( $response_body );
574                 if ( ! $success )
575                         return false;
576
577                 if ( isset( $dom->doctype ) )
578                         return false;
579
580                 foreach ( $dom->childNodes as $child ) {
581                         if ( XML_DOCUMENT_TYPE_NODE === $child->nodeType )
582                                 return false;
583                 }
584
585                 $xml = simplexml_import_dom( $dom );
586                 if ( ! $xml )
587                         return false;
588
589                 $return = new stdClass;
590                 foreach ( $xml as $key => $value ) {
591                         $return->$key = (string) $value;
592                 }
593
594                 return $return;
595         }
596
597         /**
598          * Converts a data object from WP_oEmbed::fetch() and returns the HTML.
599          *
600          * @since 2.9.0
601          * @access public
602          *
603          * @param object $data A data object result from an oEmbed provider.
604          * @param string $url The URL to the content that is desired to be embedded.
605          * @return false|string False on error, otherwise the HTML needed to embed.
606          */
607         public function data2html( $data, $url ) {
608                 if ( ! is_object( $data ) || empty( $data->type ) )
609                         return false;
610
611                 $return = false;
612
613                 switch ( $data->type ) {
614                         case 'photo':
615                                 if ( empty( $data->url ) || empty( $data->width ) || empty( $data->height ) )
616                                         break;
617                                 if ( ! is_string( $data->url ) || ! is_numeric( $data->width ) || ! is_numeric( $data->height ) )
618                                         break;
619
620                                 $title = ! empty( $data->title ) && is_string( $data->title ) ? $data->title : '';
621                                 $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>';
622                                 break;
623
624                         case 'video':
625                         case 'rich':
626                                 if ( ! empty( $data->html ) && is_string( $data->html ) )
627                                         $return = $data->html;
628                                 break;
629
630                         case 'link':
631                                 if ( ! empty( $data->title ) && is_string( $data->title ) )
632                                         $return = '<a href="' . esc_url( $url ) . '">' . esc_html( $data->title ) . '</a>';
633                                 break;
634
635                         default:
636                                 $return = false;
637                 }
638
639                 /**
640                  * Filters the returned oEmbed HTML.
641                  *
642                  * Use this filter to add support for custom data types, or to filter the result.
643                  *
644                  * @since 2.9.0
645                  *
646                  * @param string $return The returned oEmbed HTML.
647                  * @param object $data   A data object result from an oEmbed provider.
648                  * @param string $url    The URL of the content to be embedded.
649                  */
650                 return apply_filters( 'oembed_dataparse', $return, $data, $url );
651         }
652
653         /**
654          * Strips any new lines from the HTML.
655          *
656          * @since 2.9.0 as strip_scribd_newlines()
657          * @since 3.0.0
658          * @access public
659          *
660          * @param string $html Existing HTML.
661          * @param object $data Data object from WP_oEmbed::data2html()
662          * @param string $url The original URL passed to oEmbed.
663          * @return string Possibly modified $html
664          */
665         public function _strip_newlines( $html, $data, $url ) {
666                 if ( false === strpos( $html, "\n" ) ) {
667                         return $html;
668                 }
669
670                 $count = 1;
671                 $found = array();
672                 $token = '__PRE__';
673                 $search = array( "\t", "\n", "\r", ' ' );
674                 $replace = array( '__TAB__', '__NL__', '__CR__', '__SPACE__' );
675                 $tokenized = str_replace( $search, $replace, $html );
676
677                 preg_match_all( '#(<pre[^>]*>.+?</pre>)#i', $tokenized, $matches, PREG_SET_ORDER );
678                 foreach ( $matches as $i => $match ) {
679                         $tag_html = str_replace( $replace, $search, $match[0] );
680                         $tag_token = $token . $i;
681
682                         $found[ $tag_token ] = $tag_html;
683                         $html = str_replace( $tag_html, $tag_token, $html, $count );
684                 }
685
686                 $replaced = str_replace( $replace, $search, $html );
687                 $stripped = str_replace( array( "\r\n", "\n" ), '', $replaced );
688                 $pre = array_values( $found );
689                 $tokens = array_keys( $found );
690
691                 return str_replace( $tokens, $pre, $stripped );
692         }
693 }