changeset 5 | 5e2f62d02dcd |
parent 0 | d970ebf37754 |
child 7 | cf61fcea0001 |
4:346c88efed21 | 5:5e2f62d02dcd |
---|---|
1 <?php |
1 <?php |
2 /** |
2 /** |
3 * API for fetching the HTML to embed remote content based on a provided URL. |
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. |
4 * Used internally by the {@link WP_Embed} class, but is designed to be generic. |
5 * |
5 * |
6 * @link http://codex.wordpress.org/oEmbed oEmbed Codex Article |
6 * @link https://codex.wordpress.org/oEmbed oEmbed Codex Article |
7 * @link http://oembed.com/ oEmbed Homepage |
7 * @link http://oembed.com/ oEmbed Homepage |
8 * |
8 * |
9 * @package WordPress |
9 * @package WordPress |
10 * @subpackage oEmbed |
10 * @subpackage oEmbed |
11 */ |
11 */ |
16 * @package WordPress |
16 * @package WordPress |
17 * @subpackage oEmbed |
17 * @subpackage oEmbed |
18 * @since 2.9.0 |
18 * @since 2.9.0 |
19 */ |
19 */ |
20 class WP_oEmbed { |
20 class WP_oEmbed { |
21 var $providers = array(); |
21 public $providers = array(); |
22 public static $early_providers = array(); |
|
23 |
|
24 private $compat_methods = array( '_fetch_with_format', '_parse_json', '_parse_xml', '_parse_body' ); |
|
22 |
25 |
23 /** |
26 /** |
24 * Constructor |
27 * Constructor |
25 * |
28 * |
26 * @uses apply_filters() Filters a list of pre-defined oEmbed providers. |
29 * @since 2.9.0 |
27 */ |
30 */ |
28 function __construct() { |
31 public function __construct() { |
29 $providers = array( |
32 $providers = array( |
30 '#https?://(www\.)?youtube\.com/watch.*#i' => array( 'http://www.youtube.com/oembed', true ), |
33 '#http://(www\.)?youtube\.com/watch.*#i' => array( 'http://www.youtube.com/oembed', true ), |
31 'http://youtu.be/*' => array( 'http://www.youtube.com/oembed', false ), |
34 '#https://(www\.)?youtube\.com/watch.*#i' => array( 'http://www.youtube.com/oembed?scheme=https', true ), |
32 'http://blip.tv/*' => array( 'http://blip.tv/oembed/', false ), |
35 '#http://(www\.)?youtube\.com/playlist.*#i' => array( 'http://www.youtube.com/oembed', true ), |
33 '#https?://(www\.)?vimeo\.com/.*#i' => array( 'http://vimeo.com/api/oembed.{format}', true ), |
36 '#https://(www\.)?youtube\.com/playlist.*#i' => array( 'http://www.youtube.com/oembed?scheme=https', true ), |
34 '#https?://(www\.)?dailymotion\.com/.*#i' => array( 'http://www.dailymotion.com/services/oembed', true ), |
37 '#http://youtu\.be/.*#i' => array( 'http://www.youtube.com/oembed', true ), |
35 'http://dai.ly/*' => array( 'http://www.dailymotion.com/services/oembed', false ), |
38 '#https://youtu\.be/.*#i' => array( 'http://www.youtube.com/oembed?scheme=https', true ), |
36 '#https?://(www\.)?flickr\.com/.*#i' => array( 'http://www.flickr.com/services/oembed/', true ), |
39 'http://blip.tv/*' => array( 'http://blip.tv/oembed/', false ), |
37 'http://flic.kr/*' => array( 'http://www.flickr.com/services/oembed/', false ), |
40 '#https?://(.+\.)?vimeo\.com/.*#i' => array( 'http://vimeo.com/api/oembed.{format}', true ), |
38 '#https?://(.+\.)?smugmug\.com/.*#i' => array( 'http://api.smugmug.com/services/oembed/', true ), |
41 '#https?://(www\.)?dailymotion\.com/.*#i' => array( 'http://www.dailymotion.com/services/oembed', true ), |
39 '#https?://(www\.)?hulu\.com/watch/.*#i' => array( 'http://www.hulu.com/api/oembed.{format}', true ), |
42 'http://dai.ly/*' => array( 'http://www.dailymotion.com/services/oembed', false ), |
40 '#https?://(www\.)?viddler\.com/.*#i' => array( 'http://lab.viddler.com/services/oembed/', true ), |
43 '#https?://(www\.)?flickr\.com/.*#i' => array( 'https://www.flickr.com/services/oembed/', true ), |
41 'http://qik.com/*' => array( 'http://qik.com/api/oembed.{format}', false ), |
44 '#https?://flic\.kr/.*#i' => array( 'https://www.flickr.com/services/oembed/', true ), |
42 'http://revision3.com/*' => array( 'http://revision3.com/api/oembed/', false ), |
45 '#https?://(.+\.)?smugmug\.com/.*#i' => array( 'http://api.smugmug.com/services/oembed/', true ), |
43 'http://i*.photobucket.com/albums/*' => array( 'http://photobucket.com/oembed', false ), |
46 '#https?://(www\.)?hulu\.com/watch/.*#i' => array( 'http://www.hulu.com/api/oembed.{format}', true ), |
44 'http://gi*.photobucket.com/groups/*' => array( 'http://photobucket.com/oembed', false ), |
47 'http://i*.photobucket.com/albums/*' => array( 'http://photobucket.com/oembed', false ), |
45 '#https?://(www\.)?scribd\.com/.*#i' => array( 'http://www.scribd.com/services/oembed', true ), |
48 'http://gi*.photobucket.com/groups/*' => array( 'http://photobucket.com/oembed', false ), |
46 'http://wordpress.tv/*' => array( 'http://wordpress.tv/oembed/', false ), |
49 '#https?://(www\.)?scribd\.com/doc/.*#i' => array( 'http://www.scribd.com/services/oembed', true ), |
47 '#https?://(.+\.)?polldaddy\.com/.*#i' => array( 'http://polldaddy.com/oembed/', true ), |
50 '#https?://wordpress.tv/.*#i' => array( 'http://wordpress.tv/oembed/', true ), |
48 '#https?://(www\.)?funnyordie\.com/videos/.*#i' => array( 'http://www.funnyordie.com/oembed', true ), |
51 '#https?://(.+\.)?polldaddy\.com/.*#i' => array( 'https://polldaddy.com/oembed/', true ), |
49 '#https?://(www\.)?twitter\.com/.+?/status(es)?/.*#i'=> array( 'http://api.twitter.com/1/statuses/oembed.{format}', true ), |
52 '#https?://poll\.fm/.*#i' => array( 'https://polldaddy.com/oembed/', true ), |
50 '#https?://(www\.)?soundcloud\.com/.*#i' => array( 'http://soundcloud.com/oembed', true ), |
53 '#https?://(www\.)?funnyordie\.com/videos/.*#i' => array( 'http://www.funnyordie.com/oembed', true ), |
51 '#https?://(www\.)?slideshare\.net/*#' => array( 'http://www.slideshare.net/api/oembed/2', true ), |
54 '#https?://(www\.)?twitter\.com/.+?/status(es)?/.*#i' => array( 'https://api.twitter.com/1/statuses/oembed.{format}', true ), |
52 '#http://instagr(\.am|am\.com)/p/.*#i' => array( 'http://api.instagram.com/oembed', true ), |
55 '#https?://vine.co/v/.*#i' => array( 'https://vine.co/oembed.{format}', true ), |
53 '#https?://(www\.)?rdio\.com/.*#i' => array( 'http://www.rdio.com/api/oembed/', true ), |
56 '#https?://(www\.)?soundcloud\.com/.*#i' => array( 'http://soundcloud.com/oembed', true ), |
54 '#https?://rd\.io/x/.*#i' => array( 'http://www.rdio.com/api/oembed/', true ), |
57 '#https?://(.+?\.)?slideshare\.net/.*#i' => array( 'https://www.slideshare.net/api/oembed/2', true ), |
55 '#https?://(open|play)\.spotify\.com/.*#i' => array( 'https://embed.spotify.com/oembed/', true ), |
58 '#https?://instagr(\.am|am\.com)/p/.*#i' => array( 'https://api.instagram.com/oembed', true ), |
59 '#https?://(www\.)?rdio\.com/.*#i' => array( 'http://www.rdio.com/api/oembed/', true ), |
|
60 '#https?://rd\.io/x/.*#i' => array( 'http://www.rdio.com/api/oembed/', true ), |
|
61 '#https?://(open|play)\.spotify\.com/.*#i' => array( 'https://embed.spotify.com/oembed/', true ), |
|
62 '#https?://(.+\.)?imgur\.com/.*#i' => array( 'http://api.imgur.com/oembed', true ), |
|
63 '#https?://(www\.)?meetu(\.ps|p\.com)/.*#i' => array( 'http://api.meetup.com/oembed', true ), |
|
64 '#https?://(www\.)?issuu\.com/.+/docs/.+#i' => array( 'http://issuu.com/oembed_wp', true ), |
|
65 '#https?://(www\.)?collegehumor\.com/video/.*#i' => array( 'http://www.collegehumor.com/oembed.{format}', true ), |
|
66 '#https?://(www\.)?mixcloud\.com/.*#i' => array( 'http://www.mixcloud.com/oembed', true ), |
|
67 '#https?://(www\.|embed\.)?ted\.com/talks/.*#i' => array( 'http://www.ted.com/talks/oembed.{format}', true ), |
|
68 '#https?://(www\.)?(animoto|video214)\.com/play/.*#i' => array( 'http://animoto.com/oembeds/create', true ), |
|
69 '#https?://(.+)\.tumblr\.com/post/.*#i' => array( 'https://www.tumblr.com/oembed/1.0', true ), |
|
70 '#https?://(www\.)?kickstarter\.com/projects/.*#i' => array( 'https://www.kickstarter.com/services/oembed', true ), |
|
71 '#https?://kck\.st/.*#i' => array( 'https://www.kickstarter.com/services/oembed', true ), |
|
56 ); |
72 ); |
73 |
|
74 if ( ! empty( self::$early_providers['add'] ) ) { |
|
75 foreach ( self::$early_providers['add'] as $format => $data ) { |
|
76 $providers[ $format ] = $data; |
|
77 } |
|
78 } |
|
79 |
|
80 if ( ! empty( self::$early_providers['remove'] ) ) { |
|
81 foreach ( self::$early_providers['remove'] as $format ) { |
|
82 unset( $providers[ $format ] ); |
|
83 } |
|
84 } |
|
85 |
|
86 self::$early_providers = array(); |
|
87 |
|
57 /** |
88 /** |
58 * Filter the list of oEmbed providers. |
89 * Filter the list of oEmbed providers. |
59 * |
90 * |
60 * Discovery is disabled for users lacking the unfiltered_html capability. |
91 * Discovery is disabled for users lacking the unfiltered_html capability. |
61 * Only providers in this array will be used for those users. |
92 * Only providers in this array will be used for those users. |
93 * |
|
94 * Supported providers: |
|
95 * |
|
96 * | ------------ | -------------------- | ----- | --------- | |
|
97 * | Provider | Flavor | SSL | Since | |
|
98 * | ------------ | -------------------- | ----- | --------- | |
|
99 * | Blip | blip.tv | ! | 2.9.0 | |
|
100 * | Dailymotion | dailymotion.com | Yes | 2.9.0 | |
|
101 * | Flickr | flickr.com | Yes | 2.9.0 | |
|
102 * | Hulu | hulu.com | Yes | 2.9.0 | |
|
103 * | Photobucket | photobucket.com | ! | 2.9.0 | |
|
104 * | Scribd | scribd.com | Yes | 2.9.0 | |
|
105 * | Vimeo | vimeo.com | Yes | 2.9.0 | |
|
106 * | WordPress.tv | wordpress.tv | Yes | 2.9.0 | |
|
107 * | YouTube | youtube.com/watch | Yes | 2.9.0 | |
|
108 * | ------------ | -------------------- | ----- | --------- | |
|
109 * | Funny or Die | funnyordie.com | Yes | 3.0.0 | |
|
110 * | Polldaddy | polldaddy.com | Yes | 3.0.0 | |
|
111 * | SmugMug | smugmug.com | Yes | 3.0.0 | |
|
112 * | YouTube | youtu.be | Yes | 3.0.0 | |
|
113 * | ------------ | -------------------- | ----- | --------- | |
|
114 * | Twitter | twitter.com | Yes | 3.4.0 | |
|
115 * | ------------ | -------------------- | ----- | --------- | |
|
116 * | Instagram | instagram.com | Yes | 3.5.0 | |
|
117 * | Instagram | instagr.am | Yes | 3.5.0 | |
|
118 * | Slideshare | slideshare.net | Yes | 3.5.0 | |
|
119 * | SoundCloud | soundcloud.com | Yes | 3.5.0 | |
|
120 * | ------------ | -------------------- | ----- | --------- | |
|
121 * | Dailymotion | dai.ly | ! | 3.6.0 | |
|
122 * | Flickr | flic.kr | Yes | 3.6.0 | |
|
123 * | Rdio | rdio.com | Yes | 3.6.0 | |
|
124 * | Rdio | rd.io | Yes | 3.6.0 | |
|
125 * | Spotify | spotify.com | Yes | 3.6.0 | |
|
126 * | ------------ | -------------------- | ----- | --------- | |
|
127 * | Imgur | imgur.com | Yes | 3.9.0 | |
|
128 * | Meetup.com | meetup.com | Yes | 3.9.0 | |
|
129 * | Meetup.com | meetu.ps | Yes | 3.9.0 | |
|
130 * | ------------ | -------------------- | ----- | --------- | |
|
131 * | Animoto | animoto.com | Yes | 4.0.0 | |
|
132 * | Animoto | video214.com | Yes | 4.0.0 | |
|
133 * | CollegeHumor | collegehumor.com | Yes | 4.0.0 | |
|
134 * | Issuu | issuu.com | Yes | 4.0.0 | |
|
135 * | Mixcloud | mixcloud.com | Yes | 4.0.0 | |
|
136 * | Polldaddy | poll.fm | Yes | 4.0.0 | |
|
137 * | TED | ted.com | Yes | 4.0.0 | |
|
138 * | YouTube | youtube.com/playlist | Yes | 4.0.0 | |
|
139 * | ------------ | -------------------- | ----- | --------- | |
|
140 * | Vine | vine.co | Yes | 4.1.0 | |
|
141 * | ------------ | -------------------- | ----- | --------- | |
|
142 * | Tumblr | tumblr.com | Yes | 4.2.0 | |
|
143 * | Kickstarter | kickstarter.com | Yes | 4.2.0 | |
|
144 * | Kickstarter | kck.st | Yes | 4.2.0 | |
|
145 * | ------------ | -------------------- | ----- | --------- | |
|
146 * |
|
147 * No longer supported providers: |
|
148 * |
|
149 * | ------------ | -------------------- | ----- | --------- | --------- | |
|
150 * | Provider | Flavor | SSL | Since | Removed | |
|
151 * | ------------ | -------------------- | ----- | --------- | --------- | |
|
152 * | Qik | qik.com | Yes | 2.9.0 | 3.9.0 | |
|
153 * | ------------ | -------------------- | ----- | --------- | --------- | |
|
154 * | Viddler | viddler.com | Yes | 2.9.0 | 4.0.0 | |
|
155 * | ------------ | -------------------- | ----- | --------- | --------- | |
|
156 * | Revision3 | revision3.com | ! | 2.9.0 | 4.2.0 | |
|
157 * | ------------ | -------------------- | ----- | --------- | --------- | |
|
62 * |
158 * |
63 * @see wp_oembed_add_provider() |
159 * @see wp_oembed_add_provider() |
64 * |
160 * |
65 * @since 2.9.0 |
161 * @since 2.9.0 |
66 * |
162 * |
71 // Fix any embeds that contain new lines in the middle of the HTML which breaks wpautop(). |
167 // Fix any embeds that contain new lines in the middle of the HTML which breaks wpautop(). |
72 add_filter( 'oembed_dataparse', array($this, '_strip_newlines'), 10, 3 ); |
168 add_filter( 'oembed_dataparse', array($this, '_strip_newlines'), 10, 3 ); |
73 } |
169 } |
74 |
170 |
75 /** |
171 /** |
76 * The do-it-all function that takes a URL and attempts to return the HTML. |
172 * Make private/protected methods readable for backwards compatibility. |
173 * |
|
174 * @since 4.0.0 |
|
175 * @access public |
|
176 * |
|
177 * @param callable $name Method to call. |
|
178 * @param array $arguments Arguments to pass when calling. |
|
179 * @return mixed|bool Return value of the callback, false otherwise. |
|
180 */ |
|
181 public function __call( $name, $arguments ) { |
|
182 if ( in_array( $name, $this->compat_methods ) ) { |
|
183 return call_user_func_array( array( $this, $name ), $arguments ); |
|
184 } |
|
185 return false; |
|
186 } |
|
187 |
|
188 /** |
|
189 * Takes a URL and returns the corresponding oEmbed provider's URL, if there is one. |
|
190 * |
|
191 * @since 4.0.0 |
|
192 * @access public |
|
77 * |
193 * |
78 * @see WP_oEmbed::discover() |
194 * @see WP_oEmbed::discover() |
79 * @see WP_oEmbed::fetch() |
195 * |
80 * @see WP_oEmbed::data2html() |
196 * @param string $url The URL to the content. |
81 * |
197 * @param string|array $args Optional provider arguments. |
82 * @param string $url The URL to the content that should be attempted to be embedded. |
198 * @return bool|string False on failure, otherwise the oEmbed provider URL. |
83 * @param array $args Optional arguments. Usually passed from a shortcode. |
199 */ |
84 * @return bool|string False on failure, otherwise the UNSANITIZED (and potentially unsafe) HTML that should be used to embed. |
200 public function get_provider( $url, $args = '' ) { |
85 */ |
201 |
86 function get_html( $url, $args = '' ) { |
|
87 $provider = false; |
202 $provider = false; |
88 |
203 |
89 if ( !isset($args['discover']) ) |
204 if ( !isset($args['discover']) ) |
90 $args['discover'] = true; |
205 $args['discover'] = true; |
91 |
206 |
105 } |
220 } |
106 |
221 |
107 if ( !$provider && $args['discover'] ) |
222 if ( !$provider && $args['discover'] ) |
108 $provider = $this->discover( $url ); |
223 $provider = $this->discover( $url ); |
109 |
224 |
225 return $provider; |
|
226 } |
|
227 |
|
228 /** |
|
229 * Add an oEmbed provider just-in-time when wp_oembed_add_provider() is called |
|
230 * before the 'plugins_loaded' hook. |
|
231 * |
|
232 * The just-in-time addition is for the benefit of the 'oembed_providers' filter. |
|
233 * |
|
234 * @since 4.0.0 |
|
235 * @access public |
|
236 * @static |
|
237 * |
|
238 * @see wp_oembed_add_provider() |
|
239 * |
|
240 * @param string $format Format of URL that this provider can handle. You can use |
|
241 * asterisks as wildcards. |
|
242 * @param string $provider The URL to the oEmbed provider.. |
|
243 * @param bool $regex Optional. Whether the $format parameter is in a regex format. |
|
244 * Default false. |
|
245 */ |
|
246 public static function _add_provider_early( $format, $provider, $regex = false ) { |
|
247 if ( empty( self::$early_providers['add'] ) ) { |
|
248 self::$early_providers['add'] = array(); |
|
249 } |
|
250 |
|
251 self::$early_providers['add'][ $format ] = array( $provider, $regex ); |
|
252 } |
|
253 |
|
254 /** |
|
255 * Remove an oEmbed provider just-in-time when wp_oembed_remove_provider() is called |
|
256 * before the 'plugins_loaded' hook. |
|
257 * |
|
258 * The just-in-time removal is for the benefit of the 'oembed_providers' filter. |
|
259 * |
|
260 * @since 4.0.0 |
|
261 * @access public |
|
262 * @static |
|
263 * |
|
264 * @see wp_oembed_remove_provider() |
|
265 * |
|
266 * @param string $format The format of URL that this provider can handle. You can use |
|
267 * asterisks as wildcards. |
|
268 */ |
|
269 public static function _remove_provider_early( $format ) { |
|
270 if ( empty( self::$early_providers['remove'] ) ) { |
|
271 self::$early_providers['remove'] = array(); |
|
272 } |
|
273 |
|
274 self::$early_providers['remove'][] = $format; |
|
275 } |
|
276 |
|
277 /** |
|
278 * The do-it-all function that takes a URL and attempts to return the HTML. |
|
279 * |
|
280 * @see WP_oEmbed::fetch() |
|
281 * @see WP_oEmbed::data2html() |
|
282 * |
|
283 * @param string $url The URL to the content that should be attempted to be embedded. |
|
284 * @param array $args Optional arguments. Usually passed from a shortcode. |
|
285 * @return false|string False on failure, otherwise the UNSANITIZED (and potentially unsafe) HTML that should be used to embed. |
|
286 */ |
|
287 public function get_html( $url, $args = '' ) { |
|
288 $provider = $this->get_provider( $url, $args ); |
|
289 |
|
110 if ( !$provider || false === $data = $this->fetch( $provider, $url, $args ) ) |
290 if ( !$provider || false === $data = $this->fetch( $provider, $url, $args ) ) |
111 return false; |
291 return false; |
112 |
292 |
113 /** |
293 /** |
114 * Filter the HTML returned by the oEmbed provider. |
294 * Filter the HTML returned by the oEmbed provider. |
121 */ |
301 */ |
122 return apply_filters( 'oembed_result', $this->data2html( $data, $url ), $url, $args ); |
302 return apply_filters( 'oembed_result', $this->data2html( $data, $url ), $url, $args ); |
123 } |
303 } |
124 |
304 |
125 /** |
305 /** |
126 * Attempts to find oEmbed provider discovery <link> tags at the given URL. |
306 * Attempts to discover link tags at the given URL for an oEmbed provider. |
127 * |
307 * |
128 * @param string $url The URL that should be inspected for discovery <link> tags. |
308 * @param string $url The URL that should be inspected for discovery `<link>` tags. |
129 * @return bool|string False on failure, otherwise the oEmbed provider URL. |
309 * @return bool|string False on failure, otherwise the oEmbed provider URL. |
130 */ |
310 */ |
131 function discover( $url ) { |
311 public function discover( $url ) { |
132 $providers = array(); |
312 $providers = array(); |
133 |
313 |
314 /** |
|
315 * Filter oEmbed remote get arguments. |
|
316 * |
|
317 * @since 4.0.0 |
|
318 * |
|
319 * @see WP_Http::request() |
|
320 * |
|
321 * @param array $args oEmbed remote get arguments. |
|
322 * @param string $url URL to be inspected. |
|
323 */ |
|
324 $args = apply_filters( 'oembed_remote_get_args', array(), $url ); |
|
325 |
|
134 // Fetch URL content |
326 // Fetch URL content |
135 if ( $html = wp_remote_retrieve_body( wp_safe_remote_get( $url ) ) ) { |
327 $request = wp_safe_remote_get( $url, $args ); |
328 if ( $html = wp_remote_retrieve_body( $request ) ) { |
|
136 |
329 |
137 /** |
330 /** |
138 * Filter the link types that contain oEmbed provider URLs. |
331 * Filter the link types that contain oEmbed provider URLs. |
139 * |
332 * |
140 * @since 2.9.0 |
333 * @since 2.9.0 |
159 $tagfound = true; |
352 $tagfound = true; |
160 break; |
353 break; |
161 } |
354 } |
162 } |
355 } |
163 |
356 |
164 if ( $tagfound && preg_match_all( '/<link([^<>]+)>/i', $html, $links ) ) { |
357 if ( $tagfound && preg_match_all( '#<link([^<>]+)/?>#iU', $html, $links ) ) { |
165 foreach ( $links[1] as $link ) { |
358 foreach ( $links[1] as $link ) { |
166 $atts = shortcode_parse_atts( $link ); |
359 $atts = shortcode_parse_atts( $link ); |
167 |
360 |
168 if ( !empty($atts['type']) && !empty($linktypes[$atts['type']]) && !empty($atts['href']) ) { |
361 if ( !empty($atts['type']) && !empty($linktypes[$atts['type']]) && !empty($atts['href']) ) { |
169 $providers[$linktypes[$atts['type']]] = $atts['href']; |
362 $providers[$linktypes[$atts['type']]] = htmlspecialchars_decode( $atts['href'] ); |
170 |
363 |
171 // Stop here if it's JSON (that's all we need) |
364 // Stop here if it's JSON (that's all we need) |
172 if ( 'json' == $linktypes[$atts['type']] ) |
365 if ( 'json' == $linktypes[$atts['type']] ) |
173 break; |
366 break; |
174 } |
367 } |
191 * @param string $provider The URL to the oEmbed provider. |
384 * @param string $provider The URL to the oEmbed provider. |
192 * @param string $url The URL to the content that is desired to be embedded. |
385 * @param string $url The URL to the content that is desired to be embedded. |
193 * @param array $args Optional arguments. Usually passed from a shortcode. |
386 * @param array $args Optional arguments. Usually passed from a shortcode. |
194 * @return bool|object False on failure, otherwise the result in the form of an object. |
387 * @return bool|object False on failure, otherwise the result in the form of an object. |
195 */ |
388 */ |
196 function fetch( $provider, $url, $args = '' ) { |
389 public function fetch( $provider, $url, $args = '' ) { |
197 $args = wp_parse_args( $args, wp_embed_defaults() ); |
390 $args = wp_parse_args( $args, wp_embed_defaults( $url ) ); |
198 |
391 |
199 $provider = add_query_arg( 'maxwidth', (int) $args['width'], $provider ); |
392 $provider = add_query_arg( 'maxwidth', (int) $args['width'], $provider ); |
200 $provider = add_query_arg( 'maxheight', (int) $args['height'], $provider ); |
393 $provider = add_query_arg( 'maxheight', (int) $args['height'], $provider ); |
201 $provider = add_query_arg( 'url', urlencode($url), $provider ); |
394 $provider = add_query_arg( 'url', urlencode($url), $provider ); |
202 |
395 |
227 * @access private |
420 * @access private |
228 * @param string $provider_url_with_args URL to the provider with full arguments list (url, maxheight, etc.) |
421 * @param string $provider_url_with_args URL to the provider with full arguments list (url, maxheight, etc.) |
229 * @param string $format Format to use |
422 * @param string $format Format to use |
230 * @return bool|object False on failure, otherwise the result in the form of an object. |
423 * @return bool|object False on failure, otherwise the result in the form of an object. |
231 */ |
424 */ |
232 function _fetch_with_format( $provider_url_with_args, $format ) { |
425 private function _fetch_with_format( $provider_url_with_args, $format ) { |
233 $provider_url_with_args = add_query_arg( 'format', $format, $provider_url_with_args ); |
426 $provider_url_with_args = add_query_arg( 'format', $format, $provider_url_with_args ); |
234 $response = wp_safe_remote_get( $provider_url_with_args ); |
427 |
428 /** This filter is documented in wp-includes/class-oembed.php */ |
|
429 $args = apply_filters( 'oembed_remote_get_args', array(), $provider_url_with_args ); |
|
430 |
|
431 $response = wp_safe_remote_get( $provider_url_with_args, $args ); |
|
235 if ( 501 == wp_remote_retrieve_response_code( $response ) ) |
432 if ( 501 == wp_remote_retrieve_response_code( $response ) ) |
236 return new WP_Error( 'not-implemented' ); |
433 return new WP_Error( 'not-implemented' ); |
237 if ( ! $body = wp_remote_retrieve_body( $response ) ) |
434 if ( ! $body = wp_remote_retrieve_body( $response ) ) |
238 return false; |
435 return false; |
239 $parse_method = "_parse_$format"; |
436 $parse_method = "_parse_$format"; |
244 * Parses a json response body. |
441 * Parses a json response body. |
245 * |
442 * |
246 * @since 3.0.0 |
443 * @since 3.0.0 |
247 * @access private |
444 * @access private |
248 */ |
445 */ |
249 function _parse_json( $response_body ) { |
446 private function _parse_json( $response_body ) { |
250 return ( ( $data = json_decode( trim( $response_body ) ) ) && is_object( $data ) ) ? $data : false; |
447 return ( ( $data = json_decode( trim( $response_body ) ) ) && is_object( $data ) ) ? $data : false; |
251 } |
448 } |
252 |
449 |
253 /** |
450 /** |
254 * Parses an XML response body. |
451 * Parses an XML response body. |
255 * |
452 * |
256 * @since 3.0.0 |
453 * @since 3.0.0 |
257 * @access private |
454 * @access private |
258 */ |
455 */ |
259 function _parse_xml( $response_body ) { |
456 private function _parse_xml( $response_body ) { |
260 if ( ! function_exists( 'libxml_disable_entity_loader' ) ) |
457 if ( ! function_exists( 'libxml_disable_entity_loader' ) ) |
261 return false; |
458 return false; |
262 |
459 |
263 $loader = libxml_disable_entity_loader( true ); |
460 $loader = libxml_disable_entity_loader( true ); |
264 $errors = libxml_use_internal_errors( true ); |
461 $errors = libxml_use_internal_errors( true ); |
309 /** |
506 /** |
310 * Converts a data object from {@link WP_oEmbed::fetch()} and returns the HTML. |
507 * Converts a data object from {@link WP_oEmbed::fetch()} and returns the HTML. |
311 * |
508 * |
312 * @param object $data A data object result from an oEmbed provider. |
509 * @param object $data A data object result from an oEmbed provider. |
313 * @param string $url The URL to the content that is desired to be embedded. |
510 * @param string $url The URL to the content that is desired to be embedded. |
314 * @return bool|string False on error, otherwise the HTML needed to embed. |
511 * @return false|string False on error, otherwise the HTML needed to embed. |
315 */ |
512 */ |
316 function data2html( $data, $url ) { |
513 public function data2html( $data, $url ) { |
317 if ( ! is_object( $data ) || empty( $data->type ) ) |
514 if ( ! is_object( $data ) || empty( $data->type ) ) |
318 return false; |
515 return false; |
319 |
516 |
320 $return = false; |
517 $return = false; |
321 |
518 |
360 } |
557 } |
361 |
558 |
362 /** |
559 /** |
363 * Strip any new lines from the HTML. |
560 * Strip any new lines from the HTML. |
364 * |
561 * |
365 * @access private |
562 * @access public |
366 * @param string $html Existing HTML. |
563 * @param string $html Existing HTML. |
367 * @param object $data Data object from WP_oEmbed::data2html() |
564 * @param object $data Data object from WP_oEmbed::data2html() |
368 * @param string $url The original URL passed to oEmbed. |
565 * @param string $url The original URL passed to oEmbed. |
369 * @return string Possibly modified $html |
566 * @return string Possibly modified $html |
370 */ |
567 */ |
371 function _strip_newlines( $html, $data, $url ) { |
568 public function _strip_newlines( $html, $data, $url ) { |
372 if ( false !== strpos( $html, "\n" ) ) |
569 if ( false === strpos( $html, "\n" ) ) { |
373 $html = str_replace( array( "\r\n", "\n" ), '', $html ); |
570 return $html; |
374 |
571 } |
375 return $html; |
572 |
573 $count = 1; |
|
574 $found = array(); |
|
575 $token = '__PRE__'; |
|
576 $search = array( "\t", "\n", "\r", ' ' ); |
|
577 $replace = array( '__TAB__', '__NL__', '__CR__', '__SPACE__' ); |
|
578 $tokenized = str_replace( $search, $replace, $html ); |
|
579 |
|
580 preg_match_all( '#(<pre[^>]*>.+?</pre>)#i', $tokenized, $matches, PREG_SET_ORDER ); |
|
581 foreach ( $matches as $i => $match ) { |
|
582 $tag_html = str_replace( $replace, $search, $match[0] ); |
|
583 $tag_token = $token . $i; |
|
584 |
|
585 $found[ $tag_token ] = $tag_html; |
|
586 $html = str_replace( $tag_html, $tag_token, $html, $count ); |
|
587 } |
|
588 |
|
589 $replaced = str_replace( $replace, $search, $html ); |
|
590 $stripped = str_replace( array( "\r\n", "\n" ), '', $replaced ); |
|
591 $pre = array_values( $found ); |
|
592 $tokens = array_keys( $found ); |
|
593 |
|
594 return str_replace( $tokens, $pre, $stripped ); |
|
376 } |
595 } |
377 } |
596 } |
378 |
597 |
379 /** |
598 /** |
380 * Returns the initialized {@link WP_oEmbed} object |
599 * Returns the initialized {@link WP_oEmbed} object |
381 * |
600 * |
382 * @since 2.9.0 |
601 * @since 2.9.0 |
383 * @access private |
602 * @access private |
384 * |
603 * |
385 * @see WP_oEmbed |
604 * @see WP_oEmbed |
386 * @uses WP_oEmbed |
|
387 * |
605 * |
388 * @return WP_oEmbed object. |
606 * @return WP_oEmbed object. |
389 */ |
607 */ |
390 function _wp_oembed_get_object() { |
608 function _wp_oembed_get_object() { |
391 static $wp_oembed; |
609 static $wp_oembed; |