Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"wp-cli/wp-cli": "^2"
},
"require-dev": {
"wp-cli/entity-command": "^2.8",
"wp-cli/extension-command": "^2",
"wp-cli/wp-cli-tests": "^5"
},
Expand Down
21 changes: 21 additions & 0 deletions features/manage-cache.feature
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,24 @@ Feature: Generate cache
"""
Success: The WP Super Cache is enabled.
"""

When I run `wp super-cache flush`
Then STDOUT should contain:
"""
Success: Cache cleared.
"""

When I run `wp post create --post_title='Test post' --post_status=publish --porcelain`
Then save STDOUT as {POST_ID}

When I run `wp super-cache flush --post_id={POST_ID}`
Then STDOUT should contain:
"""
Success: Post cache cleared.
"""
Comment thread
swissspidy marked this conversation as resolved.

When I try `wp super-cache flush --post_id=invalid`
Then STDERR should contain:
"""
Error: This is not a valid post id.
"""
36 changes: 28 additions & 8 deletions src/WP_Super_Cache_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,29 @@ private function load() {
}

/**
* Clear something from the cache.
* Clears the cache, or a specific post's cache.
*
* @synopsis [--post_id=<post-id>] [--permalink=<permalink>]
* ## OPTIONS
*
* [--post_id=<post-id>]
* : Clear the cache for the post with this ID.
*
* [--permalink=<permalink>]
* : Clear the cache for the post with this permalink.
*
* ## EXAMPLES
*
* # Clear all cached pages.
* $ wp super-cache flush
* Success: Cache cleared.
*
* # Clear the cache for a specific post by ID.
* $ wp super-cache flush --post_id=42
* Success: Post cache cleared.
*
* # Clear the cache for a specific post by permalink.
* $ wp super-cache flush --permalink=https://example.com/my-post/
* Success: Post cache cleared.
*
* @when after_wp_load
*/
Expand All @@ -44,21 +64,21 @@ public function flush( $args = array(), $assoc_args = array() ) {
$this->load();

if ( isset( $assoc_args['post_id'] ) ) {
if ( is_numeric( $assoc_args['post_id'] ) ) {
wp_cache_post_change( $assoc_args['post_id'] );
} else {
if ( ! is_numeric( $assoc_args['post_id'] ) ) {
WP_CLI::error( 'This is not a valid post id.' );
}

wp_cache_post_change( $assoc_args['post_id'] );
WP_CLI::success( 'Post cache cleared.' );
Comment thread
swissspidy marked this conversation as resolved.
} elseif ( isset( $assoc_args['permalink'] ) ) {
$id = url_to_postid( $assoc_args['permalink'] );

if ( is_numeric( $id ) ) {
wp_cache_post_change( $id );
} else {
if ( ! is_numeric( $id ) ) {
WP_CLI::error( 'There is no post with this permalink.' );
}

wp_cache_post_change( $id );
WP_CLI::success( 'Post cache cleared.' );
Comment thread
swissspidy marked this conversation as resolved.
Outdated
} else {
wp_cache_clean_cache( $file_prefix, true );
WP_CLI::success( 'Cache cleared.' );
Expand Down
Loading