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