Skip to content

Commit 94a2cb1

Browse files
swissspidyclaude
andcommitted
Fix granular cache flush CI and clear related caches
- Fix `@require-wp-6-1` tag typo so the bulk-flush scenarios are skipped on WordPress < 6.1, and add a scenario for the < 6.1 error path. - Move the bulk group flushing into a helper that also checks `function_exists( 'wp_cache_flush_group' )`, fixing PHPStan. - Bulk post flush also clears `{$taxonomy}_relationships` groups. - Bulk term flush also clears taxonomy hierarchy caches. - Bulk user flush also clears the userlogins, userslugs and useremail groups. - Single option flush also clears `notoptions`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
1 parent f196fef commit 94a2cb1

2 files changed

Lines changed: 61 additions & 45 deletions

File tree

‎features/cache-flush-granular.feature‎

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,13 @@ Feature: Granular cache flushing operations
144144
$cache_option = function(){
145145
wp_cache_set( 'my_option', 'value', 'options' );
146146
wp_cache_set( 'alloptions', array( 'my_option' => 'value' ), 'options' );
147+
wp_cache_set( 'notoptions', array( 'my_option' => true ), 'options' );
147148
};
148149
$verify_cache_cleared = function(){
149150
$option = wp_cache_get( 'my_option', 'options' );
150151
$alloptions = wp_cache_get( 'alloptions', 'options' );
151-
if ( false !== $option || false !== $alloptions ) {
152+
$notoptions = wp_cache_get( 'notoptions', 'options' );
153+
if ( false !== $option || false !== $alloptions || false !== $notoptions ) {
152154
WP_CLI::error( 'Cache was not properly cleared.' );
153155
}
154156
};
@@ -262,7 +264,7 @@ Feature: Granular cache flushing operations
262264
| 0 |
263265
| -1 |
264266

265-
@require-wp-6-1 @skip-object-cache
267+
@require-wp-6.1 @skip-object-cache
266268
Scenario: Flush all post caches on WordPress 6.1+
267269
Given a WP install
268270
And a wp-content/mu-plugins/test-harness.php file:
@@ -272,13 +274,15 @@ Feature: Granular cache flushing operations
272274
wp_cache_set( 'post_1', array( 'ID' => 1 ), 'posts' );
273275
wp_cache_set( 'post_2', array( 'ID' => 2 ), 'posts' );
274276
wp_cache_set( 'meta_1', array( 'key' => 'value' ), 'post_meta' );
277+
wp_cache_set( 1, array( 1 ), 'category_relationships' );
275278
};
276279
$verify_group_cleared = function(){
277280
if ( function_exists( 'wp_cache_supports' ) && wp_cache_supports( 'flush_group' ) ) {
278281
$post1 = wp_cache_get( 'post_1', 'posts' );
279282
$post2 = wp_cache_get( 'post_2', 'posts' );
280283
$meta = wp_cache_get( 'meta_1', 'post_meta' );
281-
if ( false !== $post1 || false !== $post2 || false !== $meta ) {
284+
$rel = wp_cache_get( 1, 'category_relationships' );
285+
if ( false !== $post1 || false !== $post2 || false !== $meta || false !== $rel ) {
282286
WP_CLI::error( 'Group cache was not properly cleared.' );
283287
}
284288
}
@@ -293,7 +297,7 @@ Feature: Granular cache flushing operations
293297
Success: Post caches cleared.
294298
"""
295299

296-
@require-wp-6-1 @skip-object-cache
300+
@require-wp-6.1 @skip-object-cache
297301
Scenario: Flush all term caches on WordPress 6.1+
298302
Given a WP install
299303
And a wp-content/mu-plugins/test-harness.php file:
@@ -324,7 +328,7 @@ Feature: Granular cache flushing operations
324328
Success: Term caches cleared.
325329
"""
326330

327-
@require-wp-6-1 @skip-object-cache
331+
@require-wp-6.1 @skip-object-cache
328332
Scenario: Flush all comment caches on WordPress 6.1+
329333
Given a WP install
330334
And a wp-content/mu-plugins/test-harness.php file:
@@ -355,7 +359,7 @@ Feature: Granular cache flushing operations
355359
Success: Comment caches cleared.
356360
"""
357361

358-
@require-wp-6-1 @skip-object-cache
362+
@require-wp-6.1 @skip-object-cache
359363
Scenario: Flush all user caches on WordPress 6.1+
360364
Given a WP install
361365
And a wp-content/mu-plugins/test-harness.php file:
@@ -365,13 +369,21 @@ Feature: Granular cache flushing operations
365369
wp_cache_set( 'user_1', array( 'ID' => 1 ), 'users' );
366370
wp_cache_set( 'user_2', array( 'ID' => 2 ), 'users' );
367371
wp_cache_set( 'meta_1', array(), 'user_meta' );
372+
wp_cache_set( 'old@example.com', 1, 'useremail' );
373+
wp_cache_set( 'oldlogin', 1, 'userlogins' );
374+
wp_cache_set( 'oldslug', 1, 'userslugs' );
368375
};
369376
$verify_group_cleared = function(){
370377
if ( function_exists( 'wp_cache_supports' ) && wp_cache_supports( 'flush_group' ) ) {
371378
$user1 = wp_cache_get( 'user_1', 'users' );
372379
$user2 = wp_cache_get( 'user_2', 'users' );
373380
$meta = wp_cache_get( 'meta_1', 'user_meta' );
374-
if ( false !== $user1 || false !== $user2 || false !== $meta ) {
381+
$lookups = array(
382+
wp_cache_get( 'old@example.com', 'useremail' ),
383+
wp_cache_get( 'oldlogin', 'userlogins' ),
384+
wp_cache_get( 'oldslug', 'userslugs' ),
385+
);
386+
if ( false !== $user1 || false !== $user2 || false !== $meta || array( false, false, false ) !== $lookups ) {
375387
WP_CLI::error( 'Group cache was not properly cleared.' );
376388
}
377389
}
@@ -386,7 +398,7 @@ Feature: Granular cache flushing operations
386398
Success: User caches cleared.
387399
"""
388400

389-
@require-wp-6-1 @skip-object-cache
401+
@require-wp-6.1 @skip-object-cache
390402
Scenario: Flush all option caches on WordPress 6.1+
391403
Given a WP install
392404
And a wp-content/mu-plugins/test-harness.php file:
@@ -414,3 +426,14 @@ Feature: Granular cache flushing operations
414426
"""
415427
Success: Option caches cleared.
416428
"""
429+
430+
@less-than-wp-6.1
431+
Scenario: Flushing all post caches requires WordPress 6.1+
432+
Given a WP install
433+
434+
When I try `wp cache flush-post`
435+
Then STDERR should be:
436+
"""
437+
Error: Flushing all post caches requires WordPress 6.1+
438+
"""
439+
And the return code should be 1

‎src/Cache_Command.php‎

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -643,14 +643,11 @@ public function flush_post( $args ) {
643643
clean_post_cache( $post_id );
644644
WP_CLI::success( "Post cache for ID $post_id cleared." );
645645
} else {
646-
if ( ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) {
647-
WP_CLI::error( 'Flushing all post caches requires WordPress 6.1+' );
648-
}
649-
$posts_flushed = wp_cache_flush_group( 'posts' );
650-
$post_meta_flushed = wp_cache_flush_group( 'post_meta' );
651-
if ( ! $posts_flushed || ! $post_meta_flushed ) {
652-
WP_CLI::error( 'Failed to flush post caches.' );
646+
$groups = array( 'posts', 'post_meta' );
647+
foreach ( get_taxonomies() as $taxonomy ) {
648+
$groups[] = "{$taxonomy}_relationships";
653649
}
650+
$this->flush_cache_groups( 'post', $groups );
654651
WP_CLI::success( 'Post caches cleared.' );
655652
}
656653
}
@@ -689,13 +686,10 @@ public function flush_term( $args ) {
689686
wp_cache_delete( $term_id, 'term_meta' );
690687
WP_CLI::success( "Term cache for ID $term_id cleared." );
691688
} else {
692-
if ( ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) {
693-
WP_CLI::error( 'Flushing all term caches requires WordPress 6.1+' );
694-
}
695-
$terms_flushed = wp_cache_flush_group( 'terms' );
696-
$term_meta_flushed = wp_cache_flush_group( 'term_meta' );
697-
if ( ! $terms_flushed || ! $term_meta_flushed ) {
698-
WP_CLI::error( 'Failed to flush term caches.' );
689+
$this->flush_cache_groups( 'term', array( 'terms', 'term_meta' ) );
690+
// Hierarchies are stored in the `{$taxonomy}_children` option, not in a term cache group.
691+
foreach ( get_taxonomies() as $taxonomy ) {
692+
clean_taxonomy_cache( $taxonomy );
699693
}
700694
WP_CLI::success( 'Term caches cleared.' );
701695
}
@@ -733,14 +727,7 @@ public function flush_comment( $args ) {
733727
wp_cache_delete( $comment_id, 'comment_meta' );
734728
WP_CLI::success( "Comment cache for ID $comment_id cleared." );
735729
} else {
736-
if ( ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) {
737-
WP_CLI::error( 'Flushing all comment caches requires WordPress 6.1+' );
738-
}
739-
$comment_flushed = wp_cache_flush_group( 'comment' );
740-
$comment_meta_flushed = wp_cache_flush_group( 'comment_meta' );
741-
if ( ! $comment_flushed || ! $comment_meta_flushed ) {
742-
WP_CLI::error( 'Failed to flush comment caches.' );
743-
}
730+
$this->flush_cache_groups( 'comment', array( 'comment', 'comment_meta' ) );
744731
WP_CLI::success( 'Comment caches cleared.' );
745732
}
746733
}
@@ -777,14 +764,7 @@ public function flush_user( $args ) {
777764
wp_cache_delete( $user_id, 'user_meta' );
778765
WP_CLI::success( "User cache for ID $user_id cleared." );
779766
} else {
780-
if ( ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) {
781-
WP_CLI::error( 'Flushing all user caches requires WordPress 6.1+' );
782-
}
783-
$users_flushed = wp_cache_flush_group( 'users' );
784-
$user_meta_flushed = wp_cache_flush_group( 'user_meta' );
785-
if ( ! $users_flushed || ! $user_meta_flushed ) {
786-
WP_CLI::error( 'Failed to flush user caches.' );
787-
}
767+
$this->flush_cache_groups( 'user', array( 'users', 'user_meta', 'userlogins', 'userslugs', 'useremail' ) );
788768
WP_CLI::success( 'User caches cleared.' );
789769
}
790770
}
@@ -815,17 +795,30 @@ public function flush_option( $args ) {
815795
if ( ! empty( $args ) ) {
816796
$option_name = $args[0];
817797
wp_cache_delete( 'alloptions', 'options' );
798+
wp_cache_delete( 'notoptions', 'options' );
818799
wp_cache_delete( $option_name, 'options' );
819800
WP_CLI::success( "Option cache for '$option_name' cleared." );
820801
} else {
821-
if ( ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) {
822-
WP_CLI::error( 'Flushing all option caches requires WordPress 6.1+' );
823-
}
824-
$options_flushed = wp_cache_flush_group( 'options' );
825-
if ( ! $options_flushed ) {
826-
WP_CLI::error( 'Failed to flush option caches.' );
827-
}
802+
$this->flush_cache_groups( 'option', array( 'options' ) );
828803
WP_CLI::success( 'Option caches cleared.' );
829804
}
830805
}
806+
807+
/**
808+
* Flushes the given cache groups, erroring out if group flushing is unavailable or fails.
809+
*
810+
* @param string $type Object type, used in error messages.
811+
* @param array<string> $groups Cache groups to flush.
812+
*/
813+
private function flush_cache_groups( $type, $groups ) {
814+
if ( ! function_exists( 'wp_cache_supports' ) || ! function_exists( 'wp_cache_flush_group' ) || ! wp_cache_supports( 'flush_group' ) ) {
815+
WP_CLI::error( "Flushing all {$type} caches requires WordPress 6.1+" );
816+
}
817+
818+
foreach ( $groups as $group ) {
819+
if ( ! wp_cache_flush_group( $group ) ) {
820+
WP_CLI::error( "Failed to flush {$type} caches." );
821+
}
822+
}
823+
}
831824
}

0 commit comments

Comments
 (0)