Skip to content

Commit 2f09814

Browse files
Copilotswissspidy
andcommitted
feat: improve flush command with clear all docs, fix double-call bug, add success messages
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent a2757db commit 2f09814

2 files changed

Lines changed: 49 additions & 8 deletions

File tree

features/manage-cache.feature

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,24 @@ Feature: Generate cache
2828
"""
2929
Success: The WP Super Cache is enabled.
3030
"""
31+
32+
When I run `wp super-cache flush`
33+
Then STDOUT should contain:
34+
"""
35+
Success: Cache cleared.
36+
"""
37+
38+
When I run `wp post create --post_title='Test post' --post_status=publish --porcelain`
39+
Then save STDOUT as {POST_ID}
40+
41+
When I run `wp super-cache flush --post_id={POST_ID}`
42+
Then STDOUT should contain:
43+
"""
44+
Success: Post cache cleared.
45+
"""
46+
47+
When I try `wp super-cache flush --post_id=invalid`
48+
Then STDERR should contain:
49+
"""
50+
Error: This is not a valid post id.
51+
"""

src/WP_Super_Cache_Command.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,29 @@ private function load() {
3232
}
3333

3434
/**
35-
* Clear something from the cache.
35+
* Clears the cache, or a specific post's cache.
3636
*
37-
* @synopsis [--post_id=<post-id>] [--permalink=<permalink>]
37+
* ## OPTIONS
38+
*
39+
* [--post_id=<post-id>]
40+
* : Clear the cache for the post with this ID.
41+
*
42+
* [--permalink=<permalink>]
43+
* : Clear the cache for the post with this permalink.
44+
*
45+
* ## EXAMPLES
46+
*
47+
* # Clear all cached pages.
48+
* $ wp super-cache flush
49+
* Success: Cache cleared.
50+
*
51+
* # Clear the cache for a specific post by ID.
52+
* $ wp super-cache flush --post_id=42
53+
* Success: Post cache cleared.
54+
*
55+
* # Clear the cache for a specific post by permalink.
56+
* $ wp super-cache flush --permalink=https://example.com/my-post/
57+
* Success: Post cache cleared.
3858
*
3959
* @when after_wp_load
4060
*/
@@ -44,21 +64,21 @@ public function flush( $args = array(), $assoc_args = array() ) {
4464
$this->load();
4565

4666
if ( isset( $assoc_args['post_id'] ) ) {
47-
if ( is_numeric( $assoc_args['post_id'] ) ) {
48-
wp_cache_post_change( $assoc_args['post_id'] );
49-
} else {
67+
if ( ! is_numeric( $assoc_args['post_id'] ) ) {
5068
WP_CLI::error( 'This is not a valid post id.' );
5169
}
5270

5371
wp_cache_post_change( $assoc_args['post_id'] );
72+
WP_CLI::success( 'Post cache cleared.' );
5473
} elseif ( isset( $assoc_args['permalink'] ) ) {
5574
$id = url_to_postid( $assoc_args['permalink'] );
5675

57-
if ( is_numeric( $id ) ) {
58-
wp_cache_post_change( $id );
59-
} else {
76+
if ( ! is_numeric( $id ) ) {
6077
WP_CLI::error( 'There is no post with this permalink.' );
6178
}
79+
80+
wp_cache_post_change( $id );
81+
WP_CLI::success( 'Post cache cleared.' );
6282
} else {
6383
wp_cache_clean_cache( $file_prefix, true );
6484
WP_CLI::success( 'Cache cleared.' );

0 commit comments

Comments
 (0)