Skip to content

Commit fe5e609

Browse files
committed
fix: Add feature detection for group flushing
Add wp_cache_supports() checks before calling wp_cache_flush_group(). Requires WordPress 6.1+ or persistent object cache with group flushing support. Specific item flushing works on all WordPress versions. Update tests to validate specific item flushing and expect errors when group flushing not supported.
1 parent fdc5c32 commit fe5e609

2 files changed

Lines changed: 66 additions & 30 deletions

File tree

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Feature: Granular cache flushing operations
22

33
@skip-object-cache
4-
Scenario: Flush post cache
4+
Scenario: Flush specific post cache
55
Given a WP install
66
And a wp-content/mu-plugins/test-harness.php file:
77
"""
@@ -13,20 +13,24 @@ Feature: Granular cache flushing operations
1313
WP_CLI::add_hook( 'before_invoke:cache flush-post', $cache_post );
1414
"""
1515

16-
When I run `wp cache flush-post`
16+
When I run `wp cache flush-post 123`
1717
Then STDOUT should contain:
1818
"""
19-
Success: Post caches cleared.
19+
Success: Post cache for ID 123 cleared.
2020
"""
2121

22-
When I run `wp cache flush-post 123`
23-
Then STDOUT should contain:
22+
@skip-object-cache
23+
Scenario: Flush all posts cache requires support
24+
Given a WP install
25+
26+
When I try `wp cache flush-post`
27+
Then STDERR should contain:
2428
"""
25-
Success: Post cache for ID 123 cleared.
29+
Flushing all post caches requires WordPress 6.1+
2630
"""
2731

2832
@skip-object-cache
29-
Scenario: Flush term cache
33+
Scenario: Flush specific term cache
3034
Given a WP install
3135
And a wp-content/mu-plugins/test-harness.php file:
3236
"""
@@ -38,20 +42,24 @@ Feature: Granular cache flushing operations
3842
WP_CLI::add_hook( 'before_invoke:cache flush-term', $cache_term );
3943
"""
4044

41-
When I run `wp cache flush-term`
45+
When I run `wp cache flush-term 5`
4246
Then STDOUT should contain:
4347
"""
44-
Success: Term caches cleared.
48+
Success: Term cache for ID 5 cleared.
4549
"""
4650

47-
When I run `wp cache flush-term 5`
48-
Then STDOUT should contain:
51+
@skip-object-cache
52+
Scenario: Flush all terms cache requires support
53+
Given a WP install
54+
55+
When I try `wp cache flush-term`
56+
Then STDERR should contain:
4957
"""
50-
Success: Term cache for ID 5 cleared.
58+
Flushing all term caches requires WordPress 6.1+
5159
"""
5260

5361
@skip-object-cache
54-
Scenario: Flush comment cache
62+
Scenario: Flush specific comment cache
5563
Given a WP install
5664
And a wp-content/mu-plugins/test-harness.php file:
5765
"""
@@ -63,20 +71,24 @@ Feature: Granular cache flushing operations
6371
WP_CLI::add_hook( 'before_invoke:cache flush-comment', $cache_comment );
6472
"""
6573

66-
When I run `wp cache flush-comment`
74+
When I run `wp cache flush-comment 42`
6775
Then STDOUT should contain:
6876
"""
69-
Success: Comment caches cleared.
77+
Success: Comment cache for ID 42 cleared.
7078
"""
7179

72-
When I run `wp cache flush-comment 42`
73-
Then STDOUT should contain:
80+
@skip-object-cache
81+
Scenario: Flush all comments cache requires support
82+
Given a WP install
83+
84+
When I try `wp cache flush-comment`
85+
Then STDERR should contain:
7486
"""
75-
Success: Comment cache for ID 42 cleared.
87+
Flushing all comment caches requires WordPress 6.1+
7688
"""
7789

7890
@skip-object-cache
79-
Scenario: Flush user cache
91+
Scenario: Flush specific user cache
8092
Given a WP install
8193
And a wp-content/mu-plugins/test-harness.php file:
8294
"""
@@ -88,20 +100,24 @@ Feature: Granular cache flushing operations
88100
WP_CLI::add_hook( 'before_invoke:cache flush-user', $cache_user );
89101
"""
90102

91-
When I run `wp cache flush-user`
103+
When I run `wp cache flush-user 1`
92104
Then STDOUT should contain:
93105
"""
94-
Success: User caches cleared.
106+
Success: User cache for ID 1 cleared.
95107
"""
96108

97-
When I run `wp cache flush-user 1`
98-
Then STDOUT should contain:
109+
@skip-object-cache
110+
Scenario: Flush all users cache requires support
111+
Given a WP install
112+
113+
When I try `wp cache flush-user`
114+
Then STDERR should contain:
99115
"""
100-
Success: User cache for ID 1 cleared.
116+
Flushing all user caches requires WordPress 6.1+
101117
"""
102118

103119
@skip-object-cache
104-
Scenario: Flush option cache
120+
Scenario: Flush specific option cache
105121
Given a WP install
106122
And a wp-content/mu-plugins/test-harness.php file:
107123
"""
@@ -113,14 +129,19 @@ Feature: Granular cache flushing operations
113129
WP_CLI::add_hook( 'before_invoke:cache flush-option', $cache_option );
114130
"""
115131

116-
When I run `wp cache flush-option`
132+
When I run `wp cache flush-option my_option`
117133
Then STDOUT should contain:
118134
"""
119-
Success: Option caches cleared.
135+
Success: Option cache for 'my_option' cleared.
120136
"""
121137

122-
When I run `wp cache flush-option my_option`
123-
Then STDOUT should contain:
138+
@skip-object-cache
139+
Scenario: Flush all options cache requires support
140+
Given a WP install
141+
142+
When I try `wp cache flush-option`
143+
Then STDERR should contain:
124144
"""
125-
Success: Option cache for 'my_option' cleared.
145+
Flushing all option caches requires WordPress 6.1+
126146
"""
147+

‎src/Cache_Command.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,9 @@ public function flush_post( $args ) {
641641
clean_post_cache( $post_id );
642642
WP_CLI::success( "Post cache for ID $post_id cleared." );
643643
} else {
644+
if ( ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) {
645+
WP_CLI::error( 'Flushing all post caches requires WordPress 6.1+ or a persistent object cache with group flushing support.' );
646+
}
644647
wp_cache_flush_group( 'posts' );
645648
wp_cache_flush_group( 'post_meta' );
646649
WP_CLI::success( 'Post caches cleared.' );
@@ -676,6 +679,9 @@ public function flush_term( $args ) {
676679
clean_term_cache( $term_id );
677680
WP_CLI::success( "Term cache for ID $term_id cleared." );
678681
} else {
682+
if ( ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) {
683+
WP_CLI::error( 'Flushing all term caches requires WordPress 6.1+ or a persistent object cache with group flushing support.' );
684+
}
679685
wp_cache_flush_group( 'terms' );
680686
wp_cache_flush_group( 'term_meta' );
681687
WP_CLI::success( 'Term caches cleared.' );
@@ -711,6 +717,9 @@ public function flush_comment( $args ) {
711717
clean_comment_cache( $comment_id );
712718
WP_CLI::success( "Comment cache for ID $comment_id cleared." );
713719
} else {
720+
if ( ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) {
721+
WP_CLI::error( 'Flushing all comment caches requires WordPress 6.1+ or a persistent object cache with group flushing support.' );
722+
}
714723
wp_cache_flush_group( 'comment' );
715724
wp_cache_flush_group( 'comment_meta' );
716725
WP_CLI::success( 'Comment caches cleared.' );
@@ -746,6 +755,9 @@ public function flush_user( $args ) {
746755
clean_user_cache( $user_id );
747756
WP_CLI::success( "User cache for ID $user_id cleared." );
748757
} else {
758+
if ( ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) {
759+
WP_CLI::error( 'Flushing all user caches requires WordPress 6.1+ or a persistent object cache with group flushing support.' );
760+
}
749761
wp_cache_flush_group( 'users' );
750762
wp_cache_flush_group( 'user_meta' );
751763
WP_CLI::success( 'User caches cleared.' );
@@ -782,6 +794,9 @@ public function flush_option( $args ) {
782794
wp_cache_delete( $option_name, 'options' );
783795
WP_CLI::success( "Option cache for '$option_name' cleared." );
784796
} else {
797+
if ( ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) {
798+
WP_CLI::error( 'Flushing all option caches requires WordPress 6.1+ or a persistent object cache with group flushing support.' );
799+
}
785800
wp_cache_flush_group( 'options' );
786801
WP_CLI::success( 'Option caches cleared.' );
787802
}

0 commit comments

Comments
 (0)