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. + """ diff --git a/php/WP_CLI/Runner.php b/php/WP_CLI/Runner.php index 031b1bd2ef..a292884a33 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 ( 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] ); diff --git a/php/commands/transient.php b/php/commands/transient.php index d68f4950ce..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 { @@ -111,21 +111,53 @@ 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 * + * # 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 { @@ -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. @@ -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.