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
29 changes: 29 additions & 0 deletions features/transient.feature
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,32 @@ Feature: Manage WordPress transient cache
"""
Success: Transient deleted.
"""

Scenario: Transient delete and other flags
Given a WP install

When I try `wp transient delete`
Then STDERR should be:
"""
Error: Please specify transient key, or use --all or --expired.
"""

When I run `wp transient set foo bar`
And I run `wp transient set foo2 bar2`
And I run `wp transient delete --all`
Then STDOUT should contain:
"""
transients deleted from the database.
"""

When I try `wp transient get foo`
Then STDERR should be:
"""
Warning: Transient with key "foo" is not set.
"""

When I try `wp transient get foo2`
Then STDERR should be:
"""
Warning: Transient with key "foo2" is not set.
"""
12 changes: 12 additions & 0 deletions php/WP_CLI/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,18 @@ private static function back_compat_conversions( $args, $assoc_args ) {
$assoc_args['all'] = true;
}

// transient delete-expired -> transient delete --expired
if ( count( $args ) > 1 && 'transient' === $args[0] && 'delete-expired' === $args[1] ) {
$args[1] = 'delete';
$assoc_args['expired'] = true;
}

// transient delete-all -> transient delete --all
if ( count( $args ) > 1 && 'transient' === $args[0] && 'delete-all' === $args[1] ) {
$args[1] = 'delete';
$assoc_args['all'] = true;
}

// plugin scaffold -> scaffold plugin
if ( array( 'plugin', 'scaffold' ) == array_slice( $args, 0, 2 ) ) {
list( $args[0], $args[1] ) = array( $args[1], $args[0] );
Expand Down
68 changes: 43 additions & 25 deletions php/commands/transient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
*
* ## EXAMPLES
*
* # Set transient
* # Set transient.
* $ wp transient set sample_key "test data" 3600
* Success: Transient added.
*
* # Get transient
* # Get transient.
* $ wp transient get sample_key
* test data
*
* # Delete transient
* # Delete transient.
* $ wp transient delete sample_key
* Success: Transient deleted.
*
* # Delete expired transients
* $ wp transient delete-expired
* # Delete expired transients.
* $ wp transient delete --expired
* Success: 12 expired transients deleted from the database.
*
* # Delete all transients
* $ wp transient delete-all
* # Delete all transients.
* $ wp transient delete --all
* Success: 14 transients deleted from the database.
*/
class Transient_Command extends WP_CLI_Command {
Expand Down Expand Up @@ -111,21 +111,53 @@ public function set( $args, $assoc_args ) {
*
* ## OPTIONS
*
* <key>
* [<key>]
* : Key for the transient.
*
* [--network]
* : Delete the value of a network transient, instead of that on a single site.
*
* [--all]
* : Delete all transients.
*
* [--expired]
* : Delete all expired transients.
*
* ## EXAMPLES
*
* # Delete transient.
* $ wp transient delete sample_key
* Success: Transient deleted.
*
* # Delete expired transients.
* $ wp transient delete --expired
* Success: 12 expired transients deleted from the database.
*
* # Delete all transients.
* $ wp transient delete --all
* Success: 14 transients deleted from the database.
*/
public function delete( $args, $assoc_args ) {
list( $key ) = $args;
$key = ( ! empty( $args ) ) ? $args[0] : NULL;

$all = \WP_CLI\Utils\get_flag_value( $assoc_args, 'all' );
$expired = \WP_CLI\Utils\get_flag_value( $assoc_args, 'expired' );

if ( true === $all ) {
$this->delete_all();
return;
}
else if ( true === $expired ) {
$this->delete_expired();
return;
}

if ( ! $key ) {
WP_CLI::error( 'Please specify transient key, or use --all or --expired.' );
}

$func = \WP_CLI\Utils\get_flag_value( $assoc_args, 'network' ) ? 'delete_site_transient' : 'delete_transient';

if ( $func( $key ) ) {
WP_CLI::success( 'Transient deleted.' );
} else {
Expand Down Expand Up @@ -158,15 +190,8 @@ public function type() {

/**
* Delete all expired transients.
*
* ## EXAMPLES
*
* $ wp transient delete-expired
* Success: 12 expired transients deleted from the database.
*
* @subcommand delete-expired
*/
public function delete_expired() {
private function delete_expired() {
global $wpdb, $_wp_using_ext_object_cache;

// Always delete all transients from DB too.
Expand All @@ -192,15 +217,8 @@ public function delete_expired() {

/**
* Delete all transients.
*
* ## EXAMPLES
*
* $ wp transient delete-all
* Success: 14 transients deleted from the database.
*
* @subcommand delete-all
*/
public function delete_all() {
private function delete_all() {
global $wpdb, $_wp_using_ext_object_cache;

// Always delete all transients from DB too.
Expand Down