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