Repository navigation
Expand file tree
/
Copy pathCache_Command.php
More file actions
288 lines (245 loc) · 7.88 KB
/
Copy pathCache_Command.php
File metadata and controls
288 lines (245 loc) · 7.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
namespace WP_CLI\Embeds;
use WP_CLI;
use WP_CLI\Utils;
use WP_CLI_Command;
/**
* Finds, triggers, and deletes oEmbed caches.
*
* ## EXAMPLES
*
* # Find cache post ID for a given URL.
* $ wp embed cache find https://www.youtube.com/watch?v=dQw4w9WgXcQ --width=500
* 123
*
* # Clear cache for a post.
* $ wp embed cache clear 123
* Success: Cleared oEmbed cache.
*
* # Triggers cache for a post.
* $ wp embed cache trigger 456
* Success: Caching triggered!
*
* @package wp-cli
*/
class Cache_Command extends WP_CLI_Command {
/**
* Deletes all oEmbed caches for a given post.
*
* oEmbed caches for a post are stored in the post's metadata.
*
* ## OPTIONS
*
* [<post_id>]
* : ID of the post to clear the cache for.
*
* [--all]
* : Clear all oEmbed caches.
*
* ## EXAMPLES
*
* # Clear cache for a post
* $ wp embed cache clear 123
* Success: Cleared oEmbed cache.
*
* # Clear all caches
* $ wp embed cache clear --all
* Success: Cleared all oEmbed caches.
*/
public function clear( $args, $assoc_args ) {
/** @var \WP_Embed $wp_embed */
global $wp_embed, $wpdb;
$all = \WP_CLI\Utils\get_flag_value( $assoc_args, 'all' );
$post_id = isset( $args[0] ) ? $args[0] : null;
if ( null === $all && null === $post_id ) {
WP_CLI::error( 'You must specify at least one post ID or use --all.' );
}
if ( $post_id && $all ) {
WP_CLI::error( 'You cannot specify both a post ID and use --all.' );
}
// Delete all oEmbed caches.
if ( $all ) {
// Get post meta oEmbed caches
$oembed_post_meta_post_ids = (array) $wpdb->get_col(
"SELECT DISTINCT post_id FROM $wpdb->postmeta
WHERE meta_key REGEXP '^_oembed_[0-9a-f]{32}$'
OR meta_key REGEXP '^_oembed_time_[0-9a-f]{32}$'"
);
// Get posts oEmbed caches
$oembed_post_post_ids = (array) $wpdb->get_col(
"SELECT ID FROM $wpdb->posts
WHERE post_type = 'oembed_cache'
AND post_status = 'publish'
AND post_name REGEXP '^[0-9a-f]{32}$'"
);
// Get transient oEmbed caches
$oembed_transients = $wpdb->get_col(
"SELECT option_name FROM $wpdb->options
WHERE option_name REGEXP '^_transient_oembed_[0-9a-f]{32}$'"
);
$oembed_caches = array(
'post' => $oembed_post_meta_post_ids,
'oembed post' => $oembed_post_post_ids,
'transient' => $oembed_transients,
);
$total = array_sum(
array_map(
function ( $items ) {
return count( $items );
},
$oembed_caches
)
);
// Delete post meta oEmbed caches
foreach ( $oembed_post_meta_post_ids as $post_id ) {
$wp_embed->delete_oembed_caches( $post_id );
}
// Delete posts oEmbed caches
foreach ( $oembed_post_post_ids as $post_id ) {
wp_delete_post( $post_id, true );
}
// Delete transient oEmbed caches
foreach ( $oembed_transients as $option_name ) {
delete_transient( str_replace( '_transient_', '', $option_name ) );
}
if ( $total > 0 ) {
$details = array();
foreach ( $oembed_caches as $type => $items ) {
$count = count( $items );
$details[] = sprintf(
'%1$d %2$s %3$s',
$count,
$type,
Utils\pluralize( 'cache', $count )
);
}
$message = sprintf(
'Cleared %1$d oEmbed %2$s: %3$s.',
$total,
Utils\pluralize( 'cache', $total ),
implode( ', ', $details )
);
WP_CLI::success( $message );
} else {
WP_CLI::error( 'No oEmbed caches to clear!' );
}
if ( wp_using_ext_object_cache() ) {
WP_CLI::warning( 'Oembed transients are stored in an external object cache, and this command only deletes those stored in the database. You must flush the cache to delete all transients.' );
}
return;
}
// Delete oEmbed caches for a given post.
$count_metas = get_post_custom_keys( $post_id );
if ( $count_metas ) {
$count_metas = array_filter(
$count_metas,
function ( $v ) {
return '_oembed_' === substr( $v, 0, 8 );
}
);
}
if ( empty( $count_metas ) ) {
WP_CLI::error( 'No oEmbed cache to clear.' );
}
$wp_embed->delete_oembed_caches( $post_id );
WP_CLI::success( 'Cleared oEmbed cache.' );
}
/**
* Finds an oEmbed cache post ID for a given URL.
*
* Starting with WordPress 4.9, embeds that aren't associated with a specific post will be cached in
* a new oembed_cache post type. There can be more than one such entry for a url depending on attributes and context.
*
* Not to be confused with oEmbed caches for a given post which are stored in the post's metadata.
*
* ## OPTIONS
*
* <url>
* : URL to retrieve oEmbed data for.
*
* [--width=<width>]
* : Width of the embed in pixels. Part of cache key so must match. Defaults to `content_width` if set else 500px, so is theme and context dependent.
*
* [--height=<height>]
* : Height of the embed in pixels. Part of cache key so must match. Defaults to 1.5 * default width (`content_width` or 500px), to a maximum of 1000px.
*
* [--discover]
* : Whether to search with the discover attribute set or not. Part of cache key so must match. If not given, will search with attribute: unset, '1', '0', returning first.
*
* ## EXAMPLES
*
* # Find cache post ID for a given URL.
* $ wp embed cache find https://www.youtube.com/watch?v=dQw4w9WgXcQ --width=500
* 123
*/
public function find( $args, $assoc_args ) {
/** @var \WP_Embed $wp_embed */
global $wp_embed;
$url = $args[0];
$width = Utils\get_flag_value( $assoc_args, 'width' );
$height = Utils\get_flag_value( $assoc_args, 'height' );
$discover = Utils\get_flag_value( $assoc_args, 'discover' );
// The `$key_suffix` used for caching is part based on serializing the attributes array without normalizing it first so need to try to replicate that.
$oembed_args = array();
if ( null !== $width ) {
$oembed_args['width'] = $width; // Keep as string as if from a shortcode attribute.
}
if ( null !== $height ) {
$oembed_args['height'] = $height; // Keep as string as if from a shortcode attribute.
}
if ( null !== $discover ) {
$discovers = array( ( $discover ) ? '1' : '0' );
} else {
$discovers = array( null, '1', '0' );
}
$attr = wp_parse_args( $oembed_args, wp_embed_defaults( $url ) );
foreach ( $discovers as $discover ) {
if ( null !== $discover ) {
$attr['discover'] = $discover;
}
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize -- needed to mimic WP Core behavior. See: \WP_Embed::shortcode.
$key_suffix = md5( $url . serialize( $attr ) );
$cached_post_id = $wp_embed->find_oembed_post_id( $key_suffix );
if ( $cached_post_id ) {
WP_CLI::line( (string) $cached_post_id );
return;
}
}
WP_CLI::error( 'No cache post ID found!' );
}
/**
* Triggers the caching of all oEmbed results for a given post.
*
* oEmbed caches for a post are stored in the post's metadata.
*
* ## OPTIONS
*
* <post_id>
* : ID of the post to do the caching for.
*
* ## EXAMPLES
*
* # Triggers cache for a post
* $ wp embed cache trigger 456
* Success: Caching triggered!
*/
public function trigger( $args, $assoc_args ) {
/** @var \WP_Embed $wp_embed */
global $wp_embed;
$post_id = $args[0];
$post = get_post( $post_id );
$post_types = get_post_types( array( 'show_ui' => true ) );
if ( empty( $post->ID ) ) {
WP_CLI::warning( sprintf( "Post id '%s' not found.", $post_id ) );
return;
}
/** This filter is documented in wp-includes/class-wp-embed.php */
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Using WP Core hook.
if ( ! in_array( $post->post_type, apply_filters( 'embed_cache_oembed_types', $post_types ), true ) ) {
WP_CLI::warning( sprintf( "Cannot cache oEmbed results for '%s' post type", $post->post_type ) );
return;
}
$wp_embed->cache_oembed( $post_id );
WP_CLI::success( 'Caching triggered!' );
}
}