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