Skip to content

Commit 9e104f5

Browse files
committed
Support --all and multiple post IDs for embed cache clear (#67)
1 parent 990b411 commit 9e104f5

2 files changed

Lines changed: 164 additions & 19 deletions

File tree

‎features/cache.feature‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,101 @@ Feature: Manage oEmbed cache.
4747
Success: Cleared oEmbed cache.
4848
"""
4949

50+
Scenario: Clear oEmbed cache with missing arguments
51+
When I try `wp embed cache clear`
52+
Then STDERR should be:
53+
"""
54+
Error: Please specify one or more post IDs, or use --all.
55+
"""
56+
And the return code should be 1
57+
58+
Scenario: Clear oEmbed cache for multiple posts
59+
When I run `wp post create --post_title="Post 1" --porcelain`
60+
Then STDOUT should be a number
61+
And save STDOUT as {POST_ID_1}
62+
63+
When I run `wp post create --post_title="Post 2" --porcelain`
64+
Then STDOUT should be a number
65+
And save STDOUT as {POST_ID_2}
66+
67+
When I run `wp post meta add {POST_ID_1} _oembed_foo 'bar'`
68+
Then STDOUT should not be empty
69+
70+
When I run `wp post meta add {POST_ID_2} _oembed_baz 'qux'`
71+
Then STDOUT should not be empty
72+
73+
When I run `wp embed cache clear {POST_ID_1} {POST_ID_2}`
74+
Then STDOUT should be:
75+
"""
76+
Success: Cleared oEmbed cache for post {POST_ID_1}.
77+
Success: Cleared oEmbed cache for post {POST_ID_2}.
78+
"""
79+
And the return code should be 0
80+
81+
When I try `wp post meta get {POST_ID_1} _oembed_foo`
82+
Then the return code should be 1
83+
84+
When I try `wp post meta get {POST_ID_2} _oembed_baz`
85+
Then the return code should be 1
86+
87+
Scenario: Clear oEmbed cache for multiple posts with some empty
88+
When I run `wp post create --post_title="Post 1" --porcelain`
89+
Then STDOUT should be a number
90+
And save STDOUT as {POST_ID_1}
91+
92+
When I run `wp post create --post_title="Post 2" --porcelain`
93+
Then STDOUT should be a number
94+
And save STDOUT as {POST_ID_2}
95+
96+
When I run `wp post meta add {POST_ID_1} _oembed_foo 'bar'`
97+
Then STDOUT should not be empty
98+
99+
When I try `wp embed cache clear {POST_ID_1} {POST_ID_2}`
100+
Then STDOUT should be:
101+
"""
102+
Success: Cleared oEmbed cache for post {POST_ID_1}.
103+
"""
104+
And STDERR should be:
105+
"""
106+
Warning: No cache to clear for post {POST_ID_2}!
107+
"""
108+
And the return code should be 1
109+
110+
Scenario: Clear all oEmbed caches
111+
When I run `wp post create --post_title="Post 1" --porcelain`
112+
Then STDOUT should be a number
113+
And save STDOUT as {POST_ID_1}
114+
115+
When I run `wp post create --post_title="Post 2" --porcelain`
116+
Then STDOUT should be a number
117+
And save STDOUT as {POST_ID_2}
118+
119+
When I run `wp post meta add {POST_ID_1} _oembed_foo 'bar'`
120+
Then STDOUT should not be empty
121+
122+
When I run `wp post meta add {POST_ID_2} _oembed_baz 'qux'`
123+
Then STDOUT should not be empty
124+
125+
When I run `wp post create --post_title=EmbedCache --post_type=oembed_cache --post_status=publish --porcelain`
126+
Then STDOUT should be a number
127+
And save STDOUT as {OEMBED_POST_ID}
128+
129+
When I run `wp embed cache clear --all`
130+
Then STDOUT should be:
131+
"""
132+
Success: Cleared all oEmbed caches.
133+
"""
134+
And the return code should be 0
135+
136+
When I try `wp post meta get {POST_ID_1} _oembed_foo`
137+
Then the return code should be 1
138+
139+
When I try `wp post meta get {POST_ID_2} _oembed_baz`
140+
Then the return code should be 1
141+
142+
When I try `wp post get {OEMBED_POST_ID}`
143+
Then the return code should be 1
144+
50145
Scenario: Trigger oEmbed cache for a non-existent post
51146
When I try `wp embed cache trigger 123456`
52147
Then STDERR should contain:

‎src/Cache_Command.php‎

Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,45 +28,95 @@
2828
class Cache_Command extends WP_CLI_Command {
2929

3030
/**
31-
* Deletes all oEmbed caches for a given post.
31+
* Deletes all oEmbed caches for a given post or all posts.
3232
*
3333
* oEmbed caches for a post are stored in the post's metadata.
34+
* Starting with WordPress 4.9, embeds that aren't associated with a specific post will be cached in
35+
* a new oembed_cache post type.
3436
*
3537
* ## OPTIONS
3638
*
37-
* <post_id>
38-
* : ID of the post to clear the cache for.
39+
* [<post_id>...]
40+
* : One or more IDs of the posts to clear the cache for.
41+
*
42+
* [--all]
43+
* : Clear all oEmbed caches.
3944
*
4045
* ## EXAMPLES
4146
*
42-
* # Clear cache for a post
47+
* # Clear cache for a post.
4348
* $ wp embed cache clear 123
4449
* Success: Cleared oEmbed cache.
50+
*
51+
* # Clear cache for multiple posts.
52+
* $ wp embed cache clear 123 456 789
53+
* Success: Cleared oEmbed cache for post 123.
54+
* Success: Cleared oEmbed cache for post 456.
55+
* Success: Cleared oEmbed cache for post 789.
56+
*
57+
* # Clear all oEmbed caches.
58+
* $ wp embed cache clear --all
59+
* Success: Cleared all oEmbed caches.
4560
*/
4661
public function clear( $args, $assoc_args ) {
62+
$all = Utils\get_flag_value( $assoc_args, 'all', false );
63+
64+
if ( empty( $args ) && ! $all ) {
65+
WP_CLI::error( 'Please specify one or more post IDs, or use --all.' );
66+
}
67+
68+
if ( $all ) {
69+
/** @var \wpdb $wpdb */
70+
global $wpdb;
71+
72+
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
73+
$wpdb->query( "DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE '_oembed_%'" );
74+
75+
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
76+
$wpdb->query( "DELETE FROM {$wpdb->posts} WHERE post_type = 'oembed_cache'" );
77+
78+
WP_CLI::success( 'Cleared all oEmbed caches.' );
79+
return;
80+
}
81+
4782
/** @var \WP_Embed $wp_embed */
4883
global $wp_embed;
4984

50-
$post_id = $args[0];
51-
52-
$post_metas = get_post_custom_keys( $post_id );
85+
$errors = 0;
86+
foreach ( $args as $post_id ) {
87+
$post_metas = get_post_custom_keys( $post_id );
88+
89+
if ( $post_metas ) {
90+
$post_metas = array_filter(
91+
$post_metas,
92+
function ( $v ) {
93+
return '_oembed_' === substr( $v, 0, 8 );
94+
}
95+
);
96+
}
5397

54-
if ( $post_metas ) {
55-
$post_metas = array_filter(
56-
$post_metas,
57-
function ( $v ) {
58-
return '_oembed_' === substr( $v, 0, 8 );
98+
if ( empty( $post_metas ) ) {
99+
if ( 1 === count( $args ) ) {
100+
WP_CLI::error( 'No cache to clear!' );
101+
} else {
102+
WP_CLI::warning( "No cache to clear for post {$post_id}!" );
103+
++$errors;
104+
continue;
59105
}
60-
);
61-
}
106+
}
62107

63-
if ( empty( $post_metas ) ) {
64-
WP_CLI::error( 'No cache to clear!' );
65-
}
108+
$wp_embed->delete_oembed_caches( $post_id );
66109

67-
$wp_embed->delete_oembed_caches( $post_id );
110+
if ( count( $args ) > 1 ) {
111+
WP_CLI::success( "Cleared oEmbed cache for post {$post_id}." );
112+
}
113+
}
68114

69-
WP_CLI::success( 'Cleared oEmbed cache.' );
115+
if ( 1 === count( $args ) ) {
116+
WP_CLI::success( 'Cleared oEmbed cache.' );
117+
} elseif ( $errors > 0 ) {
118+
WP_CLI::halt( 1 );
119+
}
70120
}
71121

72122
/**

0 commit comments

Comments
 (0)