Skip to content

Commit bc41b9e

Browse files
committed
fix issues
1 parent 66daf71 commit bc41b9e

2 files changed

Lines changed: 96 additions & 21 deletions

File tree

‎features/cache-flush-granular.feature‎

Lines changed: 86 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ Feature: Granular cache flushing operations
77
"""
88
<?php
99
$cache_post = function(){
10-
wp_cache_set( 'post_123', array( 'ID' => 123, 'post_title' => 'Test' ), 'posts' );
11-
wp_cache_set( 'meta_123', array( 'key' => 'value' ), 'post_meta' );
10+
wp_cache_set( 123, array( 'ID' => 123, 'post_title' => 'Test' ), 'posts' );
11+
wp_cache_set( 123, array( 'key' => 'value' ), 'post_meta' );
1212
};
1313
$verify_cache_cleared = function(){
14-
$post = wp_cache_get( 'post_123', 'posts' );
15-
$meta = wp_cache_get( 'meta_123', 'post_meta' );
14+
$post = wp_cache_get( 123, 'posts' );
15+
$meta = wp_cache_get( 123, 'post_meta' );
1616
if ( false !== $post || false !== $meta ) {
1717
WP_CLI::error( 'Cache was not properly cleared.' );
1818
}
@@ -34,12 +34,12 @@ Feature: Granular cache flushing operations
3434
"""
3535
<?php
3636
$cache_term = function(){
37-
wp_cache_set( 'term_5', array( 'term_id' => 5 ), 'terms' );
38-
wp_cache_set( 'term_meta_5', array(), 'term_meta' );
37+
wp_cache_set( 5, array( 'term_id' => 5 ), 'terms' );
38+
wp_cache_set( 5, array(), 'term_meta' );
3939
};
4040
$verify_cache_cleared = function(){
41-
$term = wp_cache_get( 'term_5', 'terms' );
42-
$meta = wp_cache_get( 'term_meta_5', 'term_meta' );
41+
$term = wp_cache_get( 5, 'terms' );
42+
$meta = wp_cache_get( 5, 'term_meta' );
4343
if ( false !== $term || false !== $meta ) {
4444
WP_CLI::error( 'Cache was not properly cleared.' );
4545
}
@@ -54,19 +54,29 @@ Feature: Granular cache flushing operations
5454
Success: Term cache for ID 5 cleared.
5555
"""
5656

57+
@skip-object-cache
58+
Scenario: Flush cache for an existing term resolves its taxonomy
59+
Given a WP install
60+
61+
When I run `wp cache flush-term 1`
62+
Then STDOUT should contain:
63+
"""
64+
Success: Term cache for ID 1 cleared.
65+
"""
66+
5767
@skip-object-cache
5868
Scenario: Flush specific comment cache
5969
Given a WP install
6070
And a wp-content/mu-plugins/test-harness.php file:
6171
"""
6272
<?php
6373
$cache_comment = function(){
64-
wp_cache_set( 'comment_42', array(), 'comment' );
65-
wp_cache_set( 'comment_meta_42', array(), 'comment_meta' );
74+
wp_cache_set( 42, array(), 'comment' );
75+
wp_cache_set( 42, array(), 'comment_meta' );
6676
};
6777
$verify_cache_cleared = function(){
68-
$comment = wp_cache_get( 'comment_42', 'comment' );
69-
$meta = wp_cache_get( 'comment_meta_42', 'comment_meta' );
78+
$comment = wp_cache_get( 42, 'comment' );
79+
$meta = wp_cache_get( 42, 'comment_meta' );
7080
if ( false !== $comment || false !== $meta ) {
7181
WP_CLI::error( 'Cache was not properly cleared.' );
7282
}
@@ -88,12 +98,12 @@ Feature: Granular cache flushing operations
8898
"""
8999
<?php
90100
$cache_user = function(){
91-
wp_cache_set( 'user_1', array( 'ID' => 1 ), 'users' );
92-
wp_cache_set( 'user_meta_1', array(), 'user_meta' );
101+
wp_cache_set( 1, array( 'ID' => 1 ), 'users' );
102+
wp_cache_set( 1, array(), 'user_meta' );
93103
};
94104
$verify_cache_cleared = function(){
95-
$user = wp_cache_get( 'user_1', 'users' );
96-
$meta = wp_cache_get( 'user_meta_1', 'user_meta' );
105+
$user = wp_cache_get( 1, 'users' );
106+
$meta = wp_cache_get( 1, 'user_meta' );
97107
if ( false !== $user || false !== $meta ) {
98108
WP_CLI::error( 'Cache was not properly cleared.' );
99109
}
@@ -175,6 +185,66 @@ Feature: Granular cache flushing operations
175185
Please provide a valid user ID.
176186
"""
177187

188+
@skip-object-cache
189+
Scenario Outline: Non-positive post IDs fail gracefully
190+
Given a WP install
191+
192+
When I try `wp cache flush-post <id>`
193+
Then STDERR should contain:
194+
"""
195+
Please provide a valid post ID.
196+
"""
197+
198+
Examples:
199+
| id |
200+
| 0 |
201+
| -1 |
202+
203+
@skip-object-cache
204+
Scenario Outline: Non-positive term IDs fail gracefully
205+
Given a WP install
206+
207+
When I try `wp cache flush-term <id>`
208+
Then STDERR should contain:
209+
"""
210+
Please provide a valid term ID.
211+
"""
212+
213+
Examples:
214+
| id |
215+
| 0 |
216+
| -1 |
217+
218+
@skip-object-cache
219+
Scenario Outline: Non-positive comment IDs fail gracefully
220+
Given a WP install
221+
222+
When I try `wp cache flush-comment <id>`
223+
Then STDERR should contain:
224+
"""
225+
Please provide a valid comment ID.
226+
"""
227+
228+
Examples:
229+
| id |
230+
| 0 |
231+
| -1 |
232+
233+
@skip-object-cache
234+
Scenario Outline: Non-positive user IDs fail gracefully
235+
Given a WP install
236+
237+
When I try `wp cache flush-user <id>`
238+
Then STDERR should contain:
239+
"""
240+
Please provide a valid user ID.
241+
"""
242+
243+
Examples:
244+
| id |
245+
| 0 |
246+
| -1 |
247+
178248
@require-wp-6-1 @skip-object-cache
179249
Scenario: Flush all post caches on WordPress 6.1+
180250
Given a WP install

‎src/Cache_Command.php‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ function ( $key ) {
636636
*/
637637
public function flush_post( $args ) {
638638
if ( ! empty( $args ) ) {
639-
if ( ! is_numeric( $args[0] ) ) {
639+
if ( ! is_numeric( $args[0] ) || (int) $args[0] <= 0 ) {
640640
WP_CLI::error( 'Please provide a valid post ID.' );
641641
}
642642
$post_id = (int) $args[0];
@@ -679,11 +679,14 @@ public function flush_post( $args ) {
679679
*/
680680
public function flush_term( $args ) {
681681
if ( ! empty( $args ) ) {
682-
if ( ! is_numeric( $args[0] ) ) {
682+
if ( ! is_numeric( $args[0] ) || (int) $args[0] <= 0 ) {
683683
WP_CLI::error( 'Please provide a valid term ID.' );
684684
}
685685
$term_id = (int) $args[0];
686-
clean_term_cache( $term_id );
686+
$term = get_term( $term_id );
687+
$taxonomy = ( $term && ! is_wp_error( $term ) ) ? $term->taxonomy : '';
688+
clean_term_cache( $term_id, $taxonomy );
689+
wp_cache_delete( $term_id, 'term_meta' );
687690
WP_CLI::success( "Term cache for ID $term_id cleared." );
688691
} else {
689692
if ( ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) {
@@ -722,11 +725,12 @@ public function flush_term( $args ) {
722725
*/
723726
public function flush_comment( $args ) {
724727
if ( ! empty( $args ) ) {
725-
if ( ! is_numeric( $args[0] ) ) {
728+
if ( ! is_numeric( $args[0] ) || (int) $args[0] <= 0 ) {
726729
WP_CLI::error( 'Please provide a valid comment ID.' );
727730
}
728731
$comment_id = (int) $args[0];
729732
clean_comment_cache( $comment_id );
733+
wp_cache_delete( $comment_id, 'comment_meta' );
730734
WP_CLI::success( "Comment cache for ID $comment_id cleared." );
731735
} else {
732736
if ( ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) {
@@ -765,11 +769,12 @@ public function flush_comment( $args ) {
765769
*/
766770
public function flush_user( $args ) {
767771
if ( ! empty( $args ) ) {
768-
if ( ! is_numeric( $args[0] ) ) {
772+
if ( ! is_numeric( $args[0] ) || (int) $args[0] <= 0 ) {
769773
WP_CLI::error( 'Please provide a valid user ID.' );
770774
}
771775
$user_id = (int) $args[0];
772776
clean_user_cache( $user_id );
777+
wp_cache_delete( $user_id, 'user_meta' );
773778
WP_CLI::success( "User cache for ID $user_id cleared." );
774779
} else {
775780
if ( ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) {

0 commit comments

Comments
 (0)