Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
36 changes: 36 additions & 0 deletions features/manage-cache.feature
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,39 @@ 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.
"""

When I try `wp super-cache flush --post_id=invalid`
Then STDERR should contain:
"""
Error: This is not a valid post id.
"""

When I run `wp post get {POST_ID} --field=url`
Then save STDOUT as {POST_PERMALINK}

When I run `wp super-cache flush --permalink={POST_PERMALINK}`
Then STDOUT should contain:
"""
Success: Post cache cleared.
"""

When I try `wp super-cache flush --permalink=https://example.com/no-such-post/`
Then STDERR should contain:
"""
Error: There is no post with this permalink.
"""
41 changes: 31 additions & 10 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,22 @@ 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 {
$post_id = absint( $assoc_args['post_id'] );
if ( $post_id <= 0 ) {
WP_CLI::error( 'This is not a valid post id.' );
}

wp_cache_post_change( $assoc_args['post_id'] );
wp_cache_post_change( $post_id );
WP_CLI::success( 'Post cache cleared.' );
} elseif ( isset( $assoc_args['permalink'] ) ) {
$id = url_to_postid( $assoc_args['permalink'] );
$id = absint( url_to_postid( $assoc_args['permalink'] ) );

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

wp_cache_post_change( $id );
WP_CLI::success( 'Post cache cleared.' );
} else {
wp_cache_clean_cache( $file_prefix, true );
WP_CLI::success( 'Cache cleared.' );
Expand Down
Loading