Repository navigation
Expand file tree
/
Copy pathCache_Command.php
More file actions
241 lines (206 loc) · 6.84 KB
/
Copy pathCache_Command.php
File metadata and controls
241 lines (206 loc) · 6.84 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
<?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 or all posts.
*
* oEmbed caches for a post are stored in the post's metadata.
* Starting with WordPress 4.9, embeds that aren't associated with a specific post will be cached in
* a new oembed_cache post type.
*
* ## OPTIONS
*
* [<post_id>...]
* : One or more IDs of the posts 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 cache for multiple posts.
* $ wp embed cache clear 123 456 789
* Success: Cleared oEmbed cache for post 123.
* Success: Cleared oEmbed cache for post 456.
* Success: Cleared oEmbed cache for post 789.
*
* # Clear all oEmbed caches.
* $ wp embed cache clear --all
* Success: Cleared all oEmbed caches.
*/
public function clear( $args, $assoc_args ) {
$all = Utils\get_flag_value( $assoc_args, 'all', false );
if ( empty( $args ) && ! $all ) {
WP_CLI::error( 'Please specify one or more post IDs, or use --all.' );
}
if ( $all ) {
/** @var \wpdb $wpdb */
global $wpdb;
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query( "DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE '_oembed_%'" );
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query( "DELETE FROM {$wpdb->posts} WHERE post_type = 'oembed_cache'" );
if ( function_exists( 'wp_cache_flush' ) ) {
wp_cache_flush();
}
WP_CLI::success( 'Cleared all oEmbed caches.' );
return;
}
/** @var \WP_Embed $wp_embed */
global $wp_embed;
$errors = 0;
foreach ( $args as $post_id ) {
$post_metas = get_post_custom_keys( $post_id );
if ( $post_metas ) {
$post_metas = array_filter(
$post_metas,
function ( $v ) {
return '_oembed_' === substr( $v, 0, 8 );
}
);
}
if ( empty( $post_metas ) ) {
if ( 1 === count( $args ) ) {
WP_CLI::error( 'No cache to clear!' );
} else {
WP_CLI::warning( "No cache to clear for post {$post_id}!" );
++$errors;
continue;
}
}
$wp_embed->delete_oembed_caches( $post_id );
if ( count( $args ) > 1 ) {
WP_CLI::success( "Cleared oEmbed cache for post {$post_id}." );
}
}
if ( 1 === count( $args ) ) {
WP_CLI::success( 'Cleared oEmbed cache.' );
} elseif ( $errors > 0 ) {
WP_CLI::halt( 1 );
}
}
/**
* 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!' );
}
}