From 61cef85009949af9e2cf441c370ac2fe050d4a8e Mon Sep 17 00:00:00 2001 From: ernilambar Date: Fri, 9 Sep 2016 17:11:41 +0545 Subject: [PATCH 1/7] Acceping paramter in transient delete command --- php/commands/transient.php | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/php/commands/transient.php b/php/commands/transient.php index d68f4950ce..2e823be1ba 100644 --- a/php/commands/transient.php +++ b/php/commands/transient.php @@ -111,21 +111,36 @@ public function set( $args, $assoc_args ) { * * ## OPTIONS * - * + * [] * : 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 * * $ wp transient delete sample_key * Success: Transient deleted. */ 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 ( ! $key ) { + WP_CLI::error( 'Transient key is required.' ); + } $func = \WP_CLI\Utils\get_flag_value( $assoc_args, 'network' ) ? 'delete_site_transient' : 'delete_transient'; + var_dump( $args ); return; + if ( $func( $key ) ) { WP_CLI::success( 'Transient deleted.' ); } else { @@ -166,7 +181,7 @@ public function type() { * * @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. @@ -200,7 +215,7 @@ public function delete_expired() { * * @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. From d1aef6d0bc16e87b0843fa4befe9e4c99e375782 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Sat, 10 Sep 2016 09:42:13 +0545 Subject: [PATCH 2/7] Call function as per flag --- php/commands/transient.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/php/commands/transient.php b/php/commands/transient.php index 2e823be1ba..ed6ef4619e 100644 --- a/php/commands/transient.php +++ b/php/commands/transient.php @@ -134,12 +134,20 @@ public function delete( $args, $assoc_args ) { $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( 'Transient key is required.' ); + 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'; - var_dump( $args ); return; if ( $func( $key ) ) { WP_CLI::success( 'Transient deleted.' ); From 26f96bab60ce5d4bc3b8c4d30877141c33a23a03 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Sat, 10 Sep 2016 09:55:38 +0545 Subject: [PATCH 3/7] Add back compat shim in runner --- php/WP_CLI/Runner.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/php/WP_CLI/Runner.php b/php/WP_CLI/Runner.php index 031b1bd2ef..2c3aa2dc6b 100644 --- a/php/WP_CLI/Runner.php +++ b/php/WP_CLI/Runner.php @@ -485,6 +485,18 @@ private static function back_compat_conversions( $args, $assoc_args ) { $assoc_args['all'] = true; } + // transient delete-expired -> transient delete --expired + if ( 'transient' === $args[0] && 'delete-expired' === $args[1] ) { + $args[1] = 'delete'; + $assoc_args['expired'] = true; + } + + // transient delete-all -> transient delete --all + if ( '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] ); From ee50539b68696415465d10abd4cdd48ad5264b19 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Sat, 10 Sep 2016 10:05:34 +0545 Subject: [PATCH 4/7] Update examples for transient delete --- php/commands/transient.php | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/php/commands/transient.php b/php/commands/transient.php index ed6ef4619e..06cff3ea6c 100644 --- a/php/commands/transient.php +++ b/php/commands/transient.php @@ -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 { @@ -125,8 +125,17 @@ public function set( $args, $assoc_args ) { * * ## 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 ) { $key = ( ! empty( $args ) ) ? $args[0] : NULL; @@ -181,13 +190,6 @@ public function type() { /** * Delete all expired transients. - * - * ## EXAMPLES - * - * $ wp transient delete-expired - * Success: 12 expired transients deleted from the database. - * - * @subcommand delete-expired */ private function delete_expired() { global $wpdb, $_wp_using_ext_object_cache; @@ -215,13 +217,6 @@ private function delete_expired() { /** * Delete all transients. - * - * ## EXAMPLES - * - * $ wp transient delete-all - * Success: 14 transients deleted from the database. - * - * @subcommand delete-all */ private function delete_all() { global $wpdb, $_wp_using_ext_object_cache; From 12bdb3631a9522d0ccbdcfb26cc6c8abddb0769d Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Sat, 10 Sep 2016 14:20:43 +0545 Subject: [PATCH 5/7] Add check for args existance --- php/WP_CLI/Runner.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php/WP_CLI/Runner.php b/php/WP_CLI/Runner.php index 2c3aa2dc6b..ca0f32f795 100644 --- a/php/WP_CLI/Runner.php +++ b/php/WP_CLI/Runner.php @@ -486,7 +486,7 @@ private static function back_compat_conversions( $args, $assoc_args ) { } // transient delete-expired -> transient delete --expired - if ( 'transient' === $args[0] && 'delete-expired' === $args[1] ) { + if ( count( $args ) > 1 && 'transient' === $args[0] && 'delete-expired' === $args[1] ) { $args[1] = 'delete'; $assoc_args['expired'] = true; } From 582d9a0886f183e4a172bf8afb1be53beb235551 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Sat, 10 Sep 2016 14:59:28 +0545 Subject: [PATCH 6/7] Add missing args check --- php/WP_CLI/Runner.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php/WP_CLI/Runner.php b/php/WP_CLI/Runner.php index ca0f32f795..a292884a33 100644 --- a/php/WP_CLI/Runner.php +++ b/php/WP_CLI/Runner.php @@ -492,7 +492,7 @@ private static function back_compat_conversions( $args, $assoc_args ) { } // transient delete-all -> transient delete --all - if ( 'transient' === $args[0] && 'delete-all' === $args[1] ) { + if ( count( $args ) > 1 && 'transient' === $args[0] && 'delete-all' === $args[1] ) { $args[1] = 'delete'; $assoc_args['all'] = true; } From 26e2ed8525d0b7873da9894f2776fcfcd5274cc0 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Sat, 10 Sep 2016 20:31:12 +0545 Subject: [PATCH 7/7] Add more test for transient delete --- features/transient.feature | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/features/transient.feature b/features/transient.feature index fcbd9d8ead..064d4d8836 100644 --- a/features/transient.feature +++ b/features/transient.feature @@ -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. + """