Skip to content

Commit a9dbf2e

Browse files
Copilotswissspidy
andauthored
Fix flush command bugs and expose clear-all cache for deployments (#35)
* Initial plan * 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> * Add entity command for testing * fix: use absint() for post_id/permalink validation and add permalink Behat tests Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> * Apply suggestion from @swissspidy --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: Pascal Birchler <pascalb@google.com>
1 parent 649acdd commit a9dbf2e

3 files changed

Lines changed: 68 additions & 10 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"wp-cli/wp-cli": "^2"
2222
},
2323
"require-dev": {
24+
"wp-cli/entity-command": "^2.8",
2425
"wp-cli/extension-command": "^2",
2526
"wp-cli/wp-cli-tests": "^5"
2627
},

features/manage-cache.feature

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,39 @@ 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+
"""
52+
53+
When I run `wp post get {POST_ID} --field=url`
54+
Then save STDOUT as {POST_PERMALINK}
55+
56+
When I run `wp super-cache flush --permalink={POST_PERMALINK}`
57+
Then STDOUT should contain:
58+
"""
59+
Success: Post cache cleared.
60+
"""
61+
62+
When I try `wp super-cache flush --permalink=https://example.com/no-such-post/`
63+
Then STDERR should contain:
64+
"""
65+
Error: There is no post with this permalink.
66+
"""

src/WP_Super_Cache_Command.php

Lines changed: 31 additions & 10 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,22 @@ 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+
$post_id = absint( $assoc_args['post_id'] );
68+
if ( $post_id <= 0 ) {
5069
WP_CLI::error( 'This is not a valid post id.' );
5170
}
5271

53-
wp_cache_post_change( $assoc_args['post_id'] );
72+
wp_cache_post_change( $post_id );
73+
WP_CLI::success( 'Post cache cleared.' );
5474
} elseif ( isset( $assoc_args['permalink'] ) ) {
55-
$id = url_to_postid( $assoc_args['permalink'] );
75+
$id = absint( url_to_postid( $assoc_args['permalink'] ) );
5676

57-
if ( is_numeric( $id ) ) {
58-
wp_cache_post_change( $id );
59-
} else {
77+
if ( $id <= 0 ) {
6078
WP_CLI::error( 'There is no post with this permalink.' );
6179
}
80+
81+
wp_cache_post_change( $id );
82+
WP_CLI::success( 'Post cache cleared.' );
6283
} else {
6384
wp_cache_clean_cache( $file_prefix, true );
6485
WP_CLI::success( 'Cache cleared.' );

0 commit comments

Comments
 (0)