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