author | ymh <ymh.work@gmail.com> |
Tue, 15 Dec 2020 13:49:49 +0100 | |
changeset 16 | a86126ab1dd4 |
parent 9 | 177826044cd9 |
child 18 | be944660c56a |
permissions | -rw-r--r-- |
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; |
|
9 | 12 |
public $usecache = true; |
5 | 13 |
public $linkifunknown = true; |
9 | 14 |
public $last_attr = array(); |
15 |
public $last_url = ''; |
|
5 | 16 |
|
17 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
18 |
* When a URL cannot be embedded, return false instead of returning a link |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
19 |
* or the URL. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
20 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
21 |
* Bypasses the {@see 'embed_maybe_make_link'} filter. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
22 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
23 |
* @var bool |
5 | 24 |
*/ |
25 |
public $return_false_on_fail = false; |
|
0 | 26 |
|
27 |
/** |
|
28 |
* Constructor |
|
29 |
*/ |
|
5 | 30 |
public function __construct() { |
16 | 31 |
// Hack to get the [embed] shortcode to run before wpautop(). |
0 | 32 |
add_filter( 'the_content', array( $this, 'run_shortcode' ), 8 ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
33 |
add_filter( 'widget_text_content', array( $this, 'run_shortcode' ), 8 ); |
0 | 34 |
|
16 | 35 |
// Shortcode placeholder for strip_shortcodes(). |
0 | 36 |
add_shortcode( 'embed', '__return_false' ); |
37 |
||
16 | 38 |
// Attempts to embed all URLs in a post. |
0 | 39 |
add_filter( 'the_content', array( $this, 'autoembed' ), 8 ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
40 |
add_filter( 'widget_text_content', array( $this, 'autoembed' ), 8 ); |
0 | 41 |
|
16 | 42 |
// After a post is saved, cache oEmbed items via Ajax. |
0 | 43 |
add_action( 'edit_form_advanced', array( $this, 'maybe_run_ajax_cache' ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
44 |
add_action( 'edit_page_form', array( $this, 'maybe_run_ajax_cache' ) ); |
0 | 45 |
} |
46 |
||
47 |
/** |
|
48 |
* Process the [embed] shortcode. |
|
49 |
* |
|
50 |
* Since the [embed] shortcode needs to be run earlier than other shortcodes, |
|
51 |
* this function removes all existing shortcodes, registers the [embed] shortcode, |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
52 |
* calls do_shortcode(), and then re-registers the old shortcodes. |
0 | 53 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
54 |
* @global array $shortcode_tags |
0 | 55 |
* |
56 |
* @param string $content Content to parse |
|
57 |
* @return string Content with shortcode parsed |
|
58 |
*/ |
|
5 | 59 |
public function run_shortcode( $content ) { |
0 | 60 |
global $shortcode_tags; |
61 |
||
16 | 62 |
// Back up current registered shortcodes and clear them all out. |
0 | 63 |
$orig_shortcode_tags = $shortcode_tags; |
64 |
remove_all_shortcodes(); |
|
65 |
||
66 |
add_shortcode( 'embed', array( $this, 'shortcode' ) ); |
|
67 |
||
16 | 68 |
// Do the shortcode (only the [embed] one is registered). |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
69 |
$content = do_shortcode( $content, true ); |
0 | 70 |
|
16 | 71 |
// Put the original shortcodes back. |
0 | 72 |
$shortcode_tags = $orig_shortcode_tags; |
73 |
||
74 |
return $content; |
|
75 |
} |
|
76 |
||
77 |
/** |
|
78 |
* If a post/page was saved, then output JavaScript to make |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
79 |
* an Ajax request that will call WP_Embed::cache_oembed(). |
0 | 80 |
*/ |
5 | 81 |
public function maybe_run_ajax_cache() { |
0 | 82 |
$post = get_post(); |
83 |
||
9 | 84 |
if ( ! $post || empty( $_GET['message'] ) ) { |
0 | 85 |
return; |
9 | 86 |
} |
0 | 87 |
|
9 | 88 |
?> |
0 | 89 |
<script type="text/javascript"> |
90 |
jQuery(document).ready(function($){ |
|
91 |
$.get("<?php echo admin_url( 'admin-ajax.php?action=oembed-cache&post=' . $post->ID, 'relative' ); ?>"); |
|
92 |
}); |
|
93 |
</script> |
|
9 | 94 |
<?php |
0 | 95 |
} |
96 |
||
97 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
98 |
* Registers an embed handler. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
99 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
100 |
* Do not use this function directly, use wp_embed_register_handler() instead. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
101 |
* |
0 | 102 |
* This function should probably also only be used for sites that do not support oEmbed. |
103 |
* |
|
16 | 104 |
* @param string $id An internal ID/name for the handler. Needs to be unique. |
105 |
* @param string $regex The regex that will be used to see if this handler should be used for a URL. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
106 |
* @param callable $callback The callback function that will be called if the regex is matched. |
16 | 107 |
* @param int $priority Optional. Used to specify the order in which the registered handlers will be tested. |
108 |
* Lower numbers correspond with earlier testing, and handlers with the same priority are |
|
109 |
* tested in the order in which they were added to the action. Default 10. |
|
0 | 110 |
*/ |
5 | 111 |
public function register_handler( $id, $regex, $callback, $priority = 10 ) { |
9 | 112 |
$this->handlers[ $priority ][ $id ] = array( |
0 | 113 |
'regex' => $regex, |
114 |
'callback' => $callback, |
|
115 |
); |
|
116 |
} |
|
117 |
||
118 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
119 |
* Unregisters a previously-registered embed handler. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
120 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
121 |
* Do not use this function directly, use wp_embed_unregister_handler() instead. |
0 | 122 |
* |
16 | 123 |
* @param string $id The handler ID that should be removed. |
124 |
* @param int $priority Optional. The priority of the handler to be removed (default: 10). |
|
0 | 125 |
*/ |
5 | 126 |
public function unregister_handler( $id, $priority = 10 ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
127 |
unset( $this->handlers[ $priority ][ $id ] ); |
0 | 128 |
} |
129 |
||
130 |
/** |
|
16 | 131 |
* Returns embed HTML for a given URL from embed handlers. |
132 |
* |
|
133 |
* Attempts to convert a URL into embed HTML by checking the URL |
|
134 |
* against the regex of the registered embed handlers. |
|
135 |
* |
|
136 |
* @since 5.5.0 |
|
137 |
* |
|
138 |
* @param array $attr { |
|
139 |
* Shortcode attributes. Optional. |
|
140 |
* |
|
141 |
* @type int $width Width of the embed in pixels. |
|
142 |
* @type int $height Height of the embed in pixels. |
|
143 |
* } |
|
144 |
* @param string $url The URL attempting to be embedded. |
|
145 |
* @return string|false The embed HTML on success, false otherwise. |
|
146 |
*/ |
|
147 |
public function get_embed_handler_html( $attr, $url ) { |
|
148 |
$rawattr = $attr; |
|
149 |
$attr = wp_parse_args( $attr, wp_embed_defaults( $url ) ); |
|
150 |
||
151 |
ksort( $this->handlers ); |
|
152 |
foreach ( $this->handlers as $priority => $handlers ) { |
|
153 |
foreach ( $handlers as $id => $handler ) { |
|
154 |
if ( preg_match( $handler['regex'], $url, $matches ) && is_callable( $handler['callback'] ) ) { |
|
155 |
$return = call_user_func( $handler['callback'], $matches, $attr, $url, $rawattr ); |
|
156 |
if ( false !== $return ) { |
|
157 |
/** |
|
158 |
* Filters the returned embed HTML. |
|
159 |
* |
|
160 |
* @since 2.9.0 |
|
161 |
* |
|
162 |
* @see WP_Embed::shortcode() |
|
163 |
* |
|
164 |
* @param string|false $return The HTML result of the shortcode, or false on failure. |
|
165 |
* @param string $url The embed URL. |
|
166 |
* @param array $attr An array of shortcode attributes. |
|
167 |
*/ |
|
168 |
return apply_filters( 'embed_handler_html', $return, $url, $attr ); |
|
169 |
} |
|
170 |
} |
|
171 |
} |
|
172 |
} |
|
173 |
||
174 |
return false; |
|
175 |
} |
|
176 |
||
177 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
178 |
* The do_shortcode() callback function. |
0 | 179 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
180 |
* Attempts to convert a URL into embed HTML. Starts by checking the URL against the regex of |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
181 |
* the registered embed handlers. If none of the regex matches and it's enabled, then the URL |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
182 |
* will be given to the WP_oEmbed class. |
0 | 183 |
* |
16 | 184 |
* @param array $attr { |
5 | 185 |
* Shortcode attributes. Optional. |
0 | 186 |
* |
5 | 187 |
* @type int $width Width of the embed in pixels. |
188 |
* @type int $height Height of the embed in pixels. |
|
189 |
* } |
|
0 | 190 |
* @param string $url The URL attempting to be embedded. |
5 | 191 |
* @return string|false The embed HTML on success, otherwise the original URL. |
192 |
* `->maybe_make_link()` can return false on failure. |
|
0 | 193 |
*/ |
5 | 194 |
public function shortcode( $attr, $url = '' ) { |
0 | 195 |
$post = get_post(); |
196 |
||
5 | 197 |
if ( empty( $url ) && ! empty( $attr['src'] ) ) { |
198 |
$url = $attr['src']; |
|
199 |
} |
|
200 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
201 |
$this->last_url = $url; |
5 | 202 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
203 |
if ( empty( $url ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
204 |
$this->last_attr = $attr; |
0 | 205 |
return ''; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
206 |
} |
0 | 207 |
|
208 |
$rawattr = $attr; |
|
9 | 209 |
$attr = wp_parse_args( $attr, wp_embed_defaults( $url ) ); |
0 | 210 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
211 |
$this->last_attr = $attr; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
212 |
|
16 | 213 |
// KSES converts & into & and we need to undo this. |
5 | 214 |
// See https://core.trac.wordpress.org/ticket/11311 |
0 | 215 |
$url = str_replace( '&', '&', $url ); |
216 |
||
16 | 217 |
// Look for known internal handlers. |
218 |
$embed_handler_html = $this->get_embed_handler_html( $rawattr, $url ); |
|
219 |
if ( false !== $embed_handler_html ) { |
|
220 |
return $embed_handler_html; |
|
0 | 221 |
} |
222 |
||
223 |
$post_ID = ( ! empty( $post->ID ) ) ? $post->ID : null; |
|
224 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
225 |
// Potentially set by WP_Embed::cache_oembed(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
226 |
if ( ! empty( $this->post_ID ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
227 |
$post_ID = $this->post_ID; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
228 |
} |
0 | 229 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
230 |
// Check for a cached result (stored as custom post or in the post meta). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
231 |
$key_suffix = md5( $url . serialize( $attr ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
232 |
$cachekey = '_oembed_' . $key_suffix; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
233 |
$cachekey_time = '_oembed_time_' . $key_suffix; |
0 | 234 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
235 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
236 |
* Filters the oEmbed TTL value (time to live). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
237 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
238 |
* @since 4.0.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
239 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
240 |
* @param int $time Time to live (in seconds). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
241 |
* @param string $url The attempted embed URL. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
242 |
* @param array $attr An array of shortcode attributes. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
243 |
* @param int $post_ID Post ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
244 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
245 |
$ttl = apply_filters( 'oembed_ttl', DAY_IN_SECONDS, $url, $attr, $post_ID ); |
5 | 246 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
247 |
$cache = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
248 |
$cache_time = 0; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
249 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
250 |
$cached_post_id = $this->find_oembed_post_id( $key_suffix ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
251 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
252 |
if ( $post_ID ) { |
9 | 253 |
$cache = get_post_meta( $post_ID, $cachekey, true ); |
5 | 254 |
$cache_time = get_post_meta( $post_ID, $cachekey_time, true ); |
255 |
||
256 |
if ( ! $cache_time ) { |
|
257 |
$cache_time = 0; |
|
258 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
259 |
} elseif ( $cached_post_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
260 |
$cached_post = get_post( $cached_post_id ); |
5 | 261 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
262 |
$cache = $cached_post->post_content; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
263 |
$cache_time = strtotime( $cached_post->post_modified_gmt ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
264 |
} |
0 | 265 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
266 |
$cached_recently = ( time() - $cache_time ) < $ttl; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
267 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
268 |
if ( $this->usecache || $cached_recently ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
269 |
// Failures are cached. Serve one if we're using the cache. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
270 |
if ( '{{unknown}}' === $cache ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
271 |
return $this->maybe_make_link( $url ); |
0 | 272 |
} |
273 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
274 |
if ( ! empty( $cache ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
275 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
276 |
* Filters the cached oEmbed HTML. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
277 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
278 |
* @since 2.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
279 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
280 |
* @see WP_Embed::shortcode() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
281 |
* |
16 | 282 |
* @param string|false $cache The cached HTML result, stored in post meta. |
283 |
* @param string $url The attempted embed URL. |
|
284 |
* @param array $attr An array of shortcode attributes. |
|
285 |
* @param int $post_ID Post ID. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
286 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
287 |
return apply_filters( 'embed_oembed_html', $cache, $url, $attr, $post_ID ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
288 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
289 |
} |
0 | 290 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
291 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
292 |
* Filters whether to inspect the given URL for discoverable link tags. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
293 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
294 |
* @since 2.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
295 |
* @since 4.4.0 The default value changed to true. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
296 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
297 |
* @see WP_oEmbed::discover() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
298 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
299 |
* @param bool $enable Whether to enable `<link>` tag discovery. Default true. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
300 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
301 |
$attr['discover'] = apply_filters( 'embed_oembed_discover', true ); |
0 | 302 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
303 |
// Use oEmbed to get the HTML. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
304 |
$html = wp_oembed_get( $url, $attr ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
305 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
306 |
if ( $post_ID ) { |
5 | 307 |
if ( $html ) { |
308 |
update_post_meta( $post_ID, $cachekey, $html ); |
|
309 |
update_post_meta( $post_ID, $cachekey_time, time() ); |
|
310 |
} elseif ( ! $cache ) { |
|
311 |
update_post_meta( $post_ID, $cachekey, '{{unknown}}' ); |
|
312 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
313 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
314 |
$has_kses = false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ); |
0 | 315 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
316 |
if ( $has_kses ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
317 |
// Prevent KSES from corrupting JSON in post_content. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
318 |
kses_remove_filters(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
319 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
320 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
321 |
$insert_post_args = array( |
9 | 322 |
'post_name' => $key_suffix, |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
323 |
'post_status' => 'publish', |
9 | 324 |
'post_type' => 'oembed_cache', |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
325 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
326 |
|
0 | 327 |
if ( $html ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
328 |
if ( $cached_post_id ) { |
9 | 329 |
wp_update_post( |
330 |
wp_slash( |
|
331 |
array( |
|
332 |
'ID' => $cached_post_id, |
|
333 |
'post_content' => $html, |
|
334 |
) |
|
335 |
) |
|
336 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
337 |
} else { |
9 | 338 |
wp_insert_post( |
339 |
wp_slash( |
|
340 |
array_merge( |
|
341 |
$insert_post_args, |
|
342 |
array( |
|
343 |
'post_content' => $html, |
|
344 |
) |
|
345 |
) |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
346 |
) |
9 | 347 |
); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
348 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
349 |
} elseif ( ! $cache ) { |
9 | 350 |
wp_insert_post( |
351 |
wp_slash( |
|
352 |
array_merge( |
|
353 |
$insert_post_args, |
|
354 |
array( |
|
355 |
'post_content' => '{{unknown}}', |
|
356 |
) |
|
357 |
) |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
358 |
) |
9 | 359 |
); |
0 | 360 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
361 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
362 |
if ( $has_kses ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
363 |
kses_init_filters(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
364 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
365 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
366 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
367 |
// If there was a result, return it. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
368 |
if ( $html ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
369 |
/** This filter is documented in wp-includes/class-wp-embed.php */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
370 |
return apply_filters( 'embed_oembed_html', $html, $url, $attr, $post_ID ); |
0 | 371 |
} |
372 |
||
16 | 373 |
// Still unknown. |
0 | 374 |
return $this->maybe_make_link( $url ); |
375 |
} |
|
376 |
||
377 |
/** |
|
5 | 378 |
* Delete all oEmbed caches. Unused by core as of 4.0.0. |
0 | 379 |
* |
380 |
* @param int $post_ID Post ID to delete the caches for. |
|
381 |
*/ |
|
5 | 382 |
public function delete_oembed_caches( $post_ID ) { |
0 | 383 |
$post_metas = get_post_custom_keys( $post_ID ); |
9 | 384 |
if ( empty( $post_metas ) ) { |
0 | 385 |
return; |
9 | 386 |
} |
0 | 387 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
388 |
foreach ( $post_metas as $post_meta_key ) { |
16 | 389 |
if ( '_oembed_' === substr( $post_meta_key, 0, 8 ) ) { |
0 | 390 |
delete_post_meta( $post_ID, $post_meta_key ); |
9 | 391 |
} |
0 | 392 |
} |
393 |
} |
|
394 |
||
395 |
/** |
|
396 |
* Triggers a caching of all oEmbed results. |
|
397 |
* |
|
398 |
* @param int $post_ID Post ID to do the caching for. |
|
399 |
*/ |
|
5 | 400 |
public function cache_oembed( $post_ID ) { |
0 | 401 |
$post = get_post( $post_ID ); |
402 |
||
5 | 403 |
$post_types = get_post_types( array( 'show_ui' => true ) ); |
16 | 404 |
|
0 | 405 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
406 |
* Filters the array of post types to cache oEmbed results for. |
0 | 407 |
* |
408 |
* @since 2.9.0 |
|
409 |
* |
|
9 | 410 |
* @param string[] $post_types Array of post type names to cache oEmbed results for. Defaults to post types with `show_ui` set to true. |
0 | 411 |
*/ |
16 | 412 |
$cache_oembed_types = apply_filters( 'embed_cache_oembed_types', $post_types ); |
413 |
||
414 |
if ( empty( $post->ID ) || ! in_array( $post->post_type, $cache_oembed_types, true ) ) { |
|
0 | 415 |
return; |
5 | 416 |
} |
0 | 417 |
|
16 | 418 |
// Trigger a caching. |
5 | 419 |
if ( ! empty( $post->post_content ) ) { |
9 | 420 |
$this->post_ID = $post->ID; |
0 | 421 |
$this->usecache = false; |
422 |
||
423 |
$content = $this->run_shortcode( $post->post_content ); |
|
424 |
$this->autoembed( $content ); |
|
425 |
||
426 |
$this->usecache = true; |
|
427 |
} |
|
428 |
} |
|
429 |
||
430 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
431 |
* Passes any unlinked URLs that are on their own line to WP_Embed::shortcode() for potential embedding. |
0 | 432 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
433 |
* @see WP_Embed::autoembed_callback() |
0 | 434 |
* |
435 |
* @param string $content The content to be searched. |
|
436 |
* @return string Potentially modified $content. |
|
437 |
*/ |
|
5 | 438 |
public function autoembed( $content ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
439 |
// Replace line breaks from all HTML elements with placeholders. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
440 |
$content = wp_replace_in_html_tags( $content, array( "\n" => '<!-- wp-line-break -->' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
441 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
442 |
if ( preg_match( '#(^|\s|>)https?://#i', $content ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
443 |
// Find URLs on their own line. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
444 |
$content = preg_replace_callback( '|^(\s*)(https?://[^\s<>"]+)(\s*)$|im', array( $this, 'autoembed_callback' ), $content ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
445 |
// Find URLs in their own paragraph. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
446 |
$content = preg_replace_callback( '|(<p(?: [^>]*)?>\s*)(https?://[^\s<>"]+)(\s*<\/p>)|i', array( $this, 'autoembed_callback' ), $content ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
447 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
448 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
449 |
// Put the line breaks back. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
450 |
return str_replace( '<!-- wp-line-break -->', "\n", $content ); |
0 | 451 |
} |
452 |
||
453 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
454 |
* Callback function for WP_Embed::autoembed(). |
0 | 455 |
* |
456 |
* @param array $match A regex match array. |
|
457 |
* @return string The embed HTML on success, otherwise the original URL. |
|
458 |
*/ |
|
5 | 459 |
public function autoembed_callback( $match ) { |
9 | 460 |
$oldval = $this->linkifunknown; |
0 | 461 |
$this->linkifunknown = false; |
9 | 462 |
$return = $this->shortcode( array(), $match[2] ); |
0 | 463 |
$this->linkifunknown = $oldval; |
464 |
||
5 | 465 |
return $match[1] . $return . $match[3]; |
0 | 466 |
} |
467 |
||
468 |
/** |
|
469 |
* Conditionally makes a hyperlink based on an internal class variable. |
|
470 |
* |
|
471 |
* @param string $url URL to potentially be linked. |
|
16 | 472 |
* @return string|false Linked URL or the original URL. False if 'return_false_on_fail' is true. |
0 | 473 |
*/ |
5 | 474 |
public function maybe_make_link( $url ) { |
475 |
if ( $this->return_false_on_fail ) { |
|
476 |
return false; |
|
477 |
} |
|
478 |
||
9 | 479 |
$output = ( $this->linkifunknown ) ? '<a href="' . esc_url( $url ) . '">' . esc_html( $url ) . '</a>' : $url; |
0 | 480 |
|
481 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
482 |
* Filters the returned, maybe-linked embed URL. |
0 | 483 |
* |
484 |
* @since 2.9.0 |
|
485 |
* |
|
486 |
* @param string $output The linked or original URL. |
|
487 |
* @param string $url The original URL. |
|
488 |
*/ |
|
489 |
return apply_filters( 'embed_maybe_make_link', $output, $url ); |
|
490 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
491 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
492 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
493 |
* Find the oEmbed cache post ID for a given cache key. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
494 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
495 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
496 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
497 |
* @param string $cache_key oEmbed cache key. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
498 |
* @return int|null Post ID on success, null on failure. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
499 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
500 |
public function find_oembed_post_id( $cache_key ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
501 |
$cache_group = 'oembed_cache_post'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
502 |
$oembed_post_id = wp_cache_get( $cache_key, $cache_group ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
503 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
504 |
if ( $oembed_post_id && 'oembed_cache' === get_post_type( $oembed_post_id ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
505 |
return $oembed_post_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
506 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
507 |
|
9 | 508 |
$oembed_post_query = new WP_Query( |
509 |
array( |
|
510 |
'post_type' => 'oembed_cache', |
|
511 |
'post_status' => 'publish', |
|
512 |
'name' => $cache_key, |
|
513 |
'posts_per_page' => 1, |
|
514 |
'no_found_rows' => true, |
|
515 |
'cache_results' => true, |
|
516 |
'update_post_meta_cache' => false, |
|
517 |
'update_post_term_cache' => false, |
|
518 |
'lazy_load_term_meta' => false, |
|
519 |
) |
|
520 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
521 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
522 |
if ( ! empty( $oembed_post_query->posts ) ) { |
16 | 523 |
// Note: 'fields' => 'ids' is not being used in order to cache the post object as it will be needed. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
524 |
$oembed_post_id = $oembed_post_query->posts[0]->ID; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
525 |
wp_cache_set( $cache_key, $oembed_post_id, $cache_group ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
526 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
527 |
return $oembed_post_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
528 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
529 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
530 |
return null; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
531 |
} |
0 | 532 |
} |