0
|
1 |
<?php |
|
2 |
/** |
|
3 |
* API for easily embedding rich media such as videos and images into content. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Embed |
|
7 |
* @since 2.9.0 |
|
8 |
*/ |
|
9 |
class WP_Embed { |
5
|
10 |
public $handlers = array(); |
|
11 |
public $post_ID; |
|
12 |
public $usecache = true; |
|
13 |
public $linkifunknown = true; |
|
14 |
|
|
15 |
/** |
|
16 |
* When an URL cannot be embedded, return false instead of returning a link |
|
17 |
* or the URL. Bypasses the 'embed_maybe_make_link' filter. |
|
18 |
*/ |
|
19 |
public $return_false_on_fail = false; |
0
|
20 |
|
|
21 |
/** |
|
22 |
* Constructor |
|
23 |
*/ |
5
|
24 |
public function __construct() { |
0
|
25 |
// Hack to get the [embed] shortcode to run before wpautop() |
|
26 |
add_filter( 'the_content', array( $this, 'run_shortcode' ), 8 ); |
|
27 |
|
|
28 |
// Shortcode placeholder for strip_shortcodes() |
|
29 |
add_shortcode( 'embed', '__return_false' ); |
|
30 |
|
|
31 |
// Attempts to embed all URLs in a post |
|
32 |
add_filter( 'the_content', array( $this, 'autoembed' ), 8 ); |
|
33 |
|
|
34 |
// After a post is saved, cache oEmbed items via AJAX |
|
35 |
add_action( 'edit_form_advanced', array( $this, 'maybe_run_ajax_cache' ) ); |
|
36 |
} |
|
37 |
|
|
38 |
/** |
|
39 |
* Process the [embed] shortcode. |
|
40 |
* |
|
41 |
* Since the [embed] shortcode needs to be run earlier than other shortcodes, |
|
42 |
* this function removes all existing shortcodes, registers the [embed] shortcode, |
|
43 |
* calls {@link do_shortcode()}, and then re-registers the old shortcodes. |
|
44 |
* |
|
45 |
* @uses $shortcode_tags |
|
46 |
* |
|
47 |
* @param string $content Content to parse |
|
48 |
* @return string Content with shortcode parsed |
|
49 |
*/ |
5
|
50 |
public function run_shortcode( $content ) { |
0
|
51 |
global $shortcode_tags; |
|
52 |
|
|
53 |
// Back up current registered shortcodes and clear them all out |
|
54 |
$orig_shortcode_tags = $shortcode_tags; |
|
55 |
remove_all_shortcodes(); |
|
56 |
|
|
57 |
add_shortcode( 'embed', array( $this, 'shortcode' ) ); |
|
58 |
|
|
59 |
// Do the shortcode (only the [embed] one is registered) |
|
60 |
$content = do_shortcode( $content ); |
|
61 |
|
|
62 |
// Put the original shortcodes back |
|
63 |
$shortcode_tags = $orig_shortcode_tags; |
|
64 |
|
|
65 |
return $content; |
|
66 |
} |
|
67 |
|
|
68 |
/** |
|
69 |
* If a post/page was saved, then output JavaScript to make |
|
70 |
* an AJAX request that will call WP_Embed::cache_oembed(). |
|
71 |
*/ |
5
|
72 |
public function maybe_run_ajax_cache() { |
0
|
73 |
$post = get_post(); |
|
74 |
|
5
|
75 |
if ( ! $post || empty( $_GET['message'] ) ) |
0
|
76 |
return; |
|
77 |
|
|
78 |
?> |
|
79 |
<script type="text/javascript"> |
|
80 |
jQuery(document).ready(function($){ |
|
81 |
$.get("<?php echo admin_url( 'admin-ajax.php?action=oembed-cache&post=' . $post->ID, 'relative' ); ?>"); |
|
82 |
}); |
|
83 |
</script> |
|
84 |
<?php |
|
85 |
} |
|
86 |
|
|
87 |
/** |
|
88 |
* Register an embed handler. Do not use this function directly, use {@link wp_embed_register_handler()} instead. |
|
89 |
* This function should probably also only be used for sites that do not support oEmbed. |
|
90 |
* |
|
91 |
* @param string $id An internal ID/name for the handler. Needs to be unique. |
|
92 |
* @param string $regex The regex that will be used to see if this handler should be used for a URL. |
|
93 |
* @param callback $callback The callback function that will be called if the regex is matched. |
|
94 |
* @param int $priority Optional. Used to specify the order in which the registered handlers will be tested (default: 10). Lower numbers correspond with earlier testing, and handlers with the same priority are tested in the order in which they were added to the action. |
|
95 |
*/ |
5
|
96 |
public function register_handler( $id, $regex, $callback, $priority = 10 ) { |
0
|
97 |
$this->handlers[$priority][$id] = array( |
|
98 |
'regex' => $regex, |
|
99 |
'callback' => $callback, |
|
100 |
); |
|
101 |
} |
|
102 |
|
|
103 |
/** |
|
104 |
* Unregister a previously registered embed handler. Do not use this function directly, use {@link wp_embed_unregister_handler()} instead. |
|
105 |
* |
|
106 |
* @param string $id The handler ID that should be removed. |
|
107 |
* @param int $priority Optional. The priority of the handler to be removed (default: 10). |
|
108 |
*/ |
5
|
109 |
public function unregister_handler( $id, $priority = 10 ) { |
0
|
110 |
if ( isset($this->handlers[$priority][$id]) ) |
|
111 |
unset($this->handlers[$priority][$id]); |
|
112 |
} |
|
113 |
|
|
114 |
/** |
|
115 |
* The {@link do_shortcode()} callback function. |
|
116 |
* |
|
117 |
* Attempts to convert a URL into embed HTML. Starts by checking the URL against the regex of the registered embed handlers. |
|
118 |
* If none of the regex matches and it's enabled, then the URL will be given to the {@link WP_oEmbed} class. |
|
119 |
* |
5
|
120 |
* @param array $attr { |
|
121 |
* Shortcode attributes. Optional. |
0
|
122 |
* |
5
|
123 |
* @type int $width Width of the embed in pixels. |
|
124 |
* @type int $height Height of the embed in pixels. |
|
125 |
* } |
0
|
126 |
* @param string $url The URL attempting to be embedded. |
5
|
127 |
* @return string|false The embed HTML on success, otherwise the original URL. |
|
128 |
* `->maybe_make_link()` can return false on failure. |
0
|
129 |
*/ |
5
|
130 |
public function shortcode( $attr, $url = '' ) { |
0
|
131 |
$post = get_post(); |
|
132 |
|
5
|
133 |
if ( empty( $url ) && ! empty( $attr['src'] ) ) { |
|
134 |
$url = $attr['src']; |
|
135 |
} |
|
136 |
|
|
137 |
|
0
|
138 |
if ( empty( $url ) ) |
|
139 |
return ''; |
|
140 |
|
|
141 |
$rawattr = $attr; |
5
|
142 |
$attr = wp_parse_args( $attr, wp_embed_defaults( $url ) ); |
0
|
143 |
|
|
144 |
// kses converts & into & and we need to undo this |
5
|
145 |
// See https://core.trac.wordpress.org/ticket/11311 |
0
|
146 |
$url = str_replace( '&', '&', $url ); |
|
147 |
|
|
148 |
// Look for known internal handlers |
|
149 |
ksort( $this->handlers ); |
|
150 |
foreach ( $this->handlers as $priority => $handlers ) { |
|
151 |
foreach ( $handlers as $id => $handler ) { |
|
152 |
if ( preg_match( $handler['regex'], $url, $matches ) && is_callable( $handler['callback'] ) ) { |
|
153 |
if ( false !== $return = call_user_func( $handler['callback'], $matches, $attr, $url, $rawattr ) ) |
|
154 |
/** |
|
155 |
* Filter the returned embed handler. |
|
156 |
* |
|
157 |
* @since 2.9.0 |
|
158 |
* |
5
|
159 |
* @see WP_Embed::shortcode() |
|
160 |
* |
0
|
161 |
* @param mixed $return The shortcode callback function to call. |
|
162 |
* @param string $url The attempted embed URL. |
|
163 |
* @param array $attr An array of shortcode attributes. |
|
164 |
*/ |
|
165 |
return apply_filters( 'embed_handler_html', $return, $url, $attr ); |
|
166 |
} |
|
167 |
} |
|
168 |
} |
|
169 |
|
|
170 |
$post_ID = ( ! empty( $post->ID ) ) ? $post->ID : null; |
|
171 |
if ( ! empty( $this->post_ID ) ) // Potentially set by WP_Embed::cache_oembed() |
|
172 |
$post_ID = $this->post_ID; |
|
173 |
|
|
174 |
// Unknown URL format. Let oEmbed have a go. |
|
175 |
if ( $post_ID ) { |
|
176 |
|
|
177 |
// Check for a cached result (stored in the post meta) |
5
|
178 |
$key_suffix = md5( $url . serialize( $attr ) ); |
|
179 |
$cachekey = '_oembed_' . $key_suffix; |
|
180 |
$cachekey_time = '_oembed_time_' . $key_suffix; |
0
|
181 |
|
5
|
182 |
/** |
|
183 |
* Filter the oEmbed TTL value (time to live). |
|
184 |
* |
|
185 |
* @since 4.0.0 |
|
186 |
* |
|
187 |
* @param int $time Time to live (in seconds). |
|
188 |
* @param string $url The attempted embed URL. |
|
189 |
* @param array $attr An array of shortcode attributes. |
|
190 |
* @param int $post_ID Post ID. |
|
191 |
*/ |
|
192 |
$ttl = apply_filters( 'oembed_ttl', DAY_IN_SECONDS, $url, $attr, $post_ID ); |
|
193 |
|
|
194 |
$cache = get_post_meta( $post_ID, $cachekey, true ); |
|
195 |
$cache_time = get_post_meta( $post_ID, $cachekey_time, true ); |
|
196 |
|
|
197 |
if ( ! $cache_time ) { |
|
198 |
$cache_time = 0; |
|
199 |
} |
|
200 |
|
|
201 |
$cached_recently = ( time() - $cache_time ) < $ttl; |
|
202 |
|
|
203 |
if ( $this->usecache || $cached_recently ) { |
|
204 |
// Failures are cached. Serve one if we're using the cache. |
0
|
205 |
if ( '{{unknown}}' === $cache ) |
|
206 |
return $this->maybe_make_link( $url ); |
|
207 |
|
5
|
208 |
if ( ! empty( $cache ) ) { |
0
|
209 |
/** |
|
210 |
* Filter the cached oEmbed HTML. |
|
211 |
* |
|
212 |
* @since 2.9.0 |
|
213 |
* |
5
|
214 |
* @see WP_Embed::shortcode() |
|
215 |
* |
0
|
216 |
* @param mixed $cache The cached HTML result, stored in post meta. |
|
217 |
* @param string $url The attempted embed URL. |
|
218 |
* @param array $attr An array of shortcode attributes. |
|
219 |
* @param int $post_ID Post ID. |
|
220 |
*/ |
|
221 |
return apply_filters( 'embed_oembed_html', $cache, $url, $attr, $post_ID ); |
5
|
222 |
} |
0
|
223 |
} |
|
224 |
|
|
225 |
/** |
5
|
226 |
* Filter whether to inspect the given URL for discoverable link tags. |
|
227 |
* |
|
228 |
* @since 2.9.0 |
0
|
229 |
* |
|
230 |
* @see WP_oEmbed::discover() |
|
231 |
* |
5
|
232 |
* @param bool $enable Whether to enable `<link>` tag discovery. Default false. |
0
|
233 |
*/ |
|
234 |
$attr['discover'] = ( apply_filters( 'embed_oembed_discover', false ) && author_can( $post_ID, 'unfiltered_html' ) ); |
|
235 |
|
|
236 |
// Use oEmbed to get the HTML |
|
237 |
$html = wp_oembed_get( $url, $attr ); |
|
238 |
|
5
|
239 |
// Maybe cache the result |
|
240 |
if ( $html ) { |
|
241 |
update_post_meta( $post_ID, $cachekey, $html ); |
|
242 |
update_post_meta( $post_ID, $cachekey_time, time() ); |
|
243 |
} elseif ( ! $cache ) { |
|
244 |
update_post_meta( $post_ID, $cachekey, '{{unknown}}' ); |
|
245 |
} |
0
|
246 |
|
|
247 |
// If there was a result, return it |
|
248 |
if ( $html ) { |
|
249 |
/** This filter is documented in wp-includes/class-wp-embed.php */ |
|
250 |
return apply_filters( 'embed_oembed_html', $html, $url, $attr, $post_ID ); |
|
251 |
} |
|
252 |
} |
|
253 |
|
|
254 |
// Still unknown |
|
255 |
return $this->maybe_make_link( $url ); |
|
256 |
} |
|
257 |
|
|
258 |
/** |
5
|
259 |
* Delete all oEmbed caches. Unused by core as of 4.0.0. |
0
|
260 |
* |
|
261 |
* @param int $post_ID Post ID to delete the caches for. |
|
262 |
*/ |
5
|
263 |
public function delete_oembed_caches( $post_ID ) { |
0
|
264 |
$post_metas = get_post_custom_keys( $post_ID ); |
|
265 |
if ( empty($post_metas) ) |
|
266 |
return; |
|
267 |
|
|
268 |
foreach( $post_metas as $post_meta_key ) { |
|
269 |
if ( '_oembed_' == substr( $post_meta_key, 0, 8 ) ) |
|
270 |
delete_post_meta( $post_ID, $post_meta_key ); |
|
271 |
} |
|
272 |
} |
|
273 |
|
|
274 |
/** |
|
275 |
* Triggers a caching of all oEmbed results. |
|
276 |
* |
|
277 |
* @param int $post_ID Post ID to do the caching for. |
|
278 |
*/ |
5
|
279 |
public function cache_oembed( $post_ID ) { |
0
|
280 |
$post = get_post( $post_ID ); |
|
281 |
|
5
|
282 |
$post_types = get_post_types( array( 'show_ui' => true ) ); |
0
|
283 |
/** |
|
284 |
* Filter the array of post types to cache oEmbed results for. |
|
285 |
* |
|
286 |
* @since 2.9.0 |
|
287 |
* |
5
|
288 |
* @param array $post_types Array of post types to cache oEmbed results for. Defaults to post types with `show_ui` set to true. |
0
|
289 |
*/ |
5
|
290 |
if ( empty( $post->ID ) || ! in_array( $post->post_type, apply_filters( 'embed_cache_oembed_types', $post_types ) ) ){ |
0
|
291 |
return; |
5
|
292 |
} |
0
|
293 |
|
|
294 |
// Trigger a caching |
5
|
295 |
if ( ! empty( $post->post_content ) ) { |
0
|
296 |
$this->post_ID = $post->ID; |
|
297 |
$this->usecache = false; |
|
298 |
|
|
299 |
$content = $this->run_shortcode( $post->post_content ); |
|
300 |
$this->autoembed( $content ); |
|
301 |
|
|
302 |
$this->usecache = true; |
|
303 |
} |
|
304 |
} |
|
305 |
|
|
306 |
/** |
|
307 |
* Passes any unlinked URLs that are on their own line to {@link WP_Embed::shortcode()} for potential embedding. |
|
308 |
* |
|
309 |
* @uses WP_Embed::autoembed_callback() |
|
310 |
* |
|
311 |
* @param string $content The content to be searched. |
|
312 |
* @return string Potentially modified $content. |
|
313 |
*/ |
5
|
314 |
public function autoembed( $content ) { |
|
315 |
return preg_replace_callback( '|^(\s*)(https?://[^\s"]+)(\s*)$|im', array( $this, 'autoembed_callback' ), $content ); |
0
|
316 |
} |
|
317 |
|
|
318 |
/** |
|
319 |
* Callback function for {@link WP_Embed::autoembed()}. |
|
320 |
* |
|
321 |
* @param array $match A regex match array. |
|
322 |
* @return string The embed HTML on success, otherwise the original URL. |
|
323 |
*/ |
5
|
324 |
public function autoembed_callback( $match ) { |
0
|
325 |
$oldval = $this->linkifunknown; |
|
326 |
$this->linkifunknown = false; |
5
|
327 |
$return = $this->shortcode( array(), $match[2] ); |
0
|
328 |
$this->linkifunknown = $oldval; |
|
329 |
|
5
|
330 |
return $match[1] . $return . $match[3]; |
0
|
331 |
} |
|
332 |
|
|
333 |
/** |
|
334 |
* Conditionally makes a hyperlink based on an internal class variable. |
|
335 |
* |
|
336 |
* @param string $url URL to potentially be linked. |
5
|
337 |
* @return false|string Linked URL or the original URL. False if 'return_false_on_fail' is true. |
0
|
338 |
*/ |
5
|
339 |
public function maybe_make_link( $url ) { |
|
340 |
if ( $this->return_false_on_fail ) { |
|
341 |
return false; |
|
342 |
} |
|
343 |
|
0
|
344 |
$output = ( $this->linkifunknown ) ? '<a href="' . esc_url($url) . '">' . esc_html($url) . '</a>' : $url; |
|
345 |
|
|
346 |
/** |
|
347 |
* Filter the returned, maybe-linked embed URL. |
|
348 |
* |
|
349 |
* @since 2.9.0 |
|
350 |
* |
|
351 |
* @param string $output The linked or original URL. |
|
352 |
* @param string $url The original URL. |
|
353 |
*/ |
|
354 |
return apply_filters( 'embed_maybe_make_link', $output, $url ); |
|
355 |
} |
|
356 |
} |
|
357 |
$GLOBALS['wp_embed'] = new WP_Embed(); |