diff --git a/features/theme-delete.feature b/features/theme-delete.feature index b130b11c5..58111ff41 100644 --- a/features/theme-delete.feature +++ b/features/theme-delete.feature @@ -13,6 +13,46 @@ Feature: Delete WordPress themes """ And the return code should be 0 + Scenario: Delete an active theme + When I run `wp theme activate p2` + Then STDOUT should not be empty + + When I try `wp theme delete p2` + Then STDERR should be: + """ + Warning: Can't delete the currently active theme: p2 + Error: No themes deleted. + """ + + When I try `wp theme delete p2 --force` + Then STDOUT should contain: + """ + Deleted 'p2' theme. + """ + + Scenario: Delete all installed themes + When I run `wp theme list --status=active --field=name --porcelain` + And save STDOUT as {ACTIVE_THEME} + + When I try `wp theme delete --all` + Then STDERR should contain: + """ + Warning: Can't delete the currently active theme: {ACTIVE_THEME} + """ + + When I run `wp theme delete --all --force` + Then STDOUT should be: + """ + Deleted '{ACTIVE_THEME}' theme. + Success: Deleted 1 of 1 themes. + """ + + When I try the previous command again + Then STDOUT should be: + """ + Success: No themes installed. + """ + Scenario: Attempting to delete a theme that doesn't exist When I run `wp theme delete p2` Then STDOUT should not be empty diff --git a/src/Theme_Command.php b/src/Theme_Command.php index 20339b4a0..62129c72f 100644 --- a/src/Theme_Command.php +++ b/src/Theme_Command.php @@ -753,9 +753,15 @@ public function is_active( $args, $assoc_args = array() ) { * * ## OPTIONS * - * ... + * [...] * : One or more themes to delete. * + * [--all] + * : If set, all themes will be deleted except active theme. + * + * [--force] + * : To delete active theme use this. + * * ## EXAMPLES * * $ wp theme delete twentytwelve @@ -764,12 +770,21 @@ public function is_active( $args, $assoc_args = array() ) { * * @alias uninstall */ - public function delete( $args ) { + public function delete( $args, $assoc_args ) { + + $all = Utils\get_flag_value( $assoc_args, 'all', false ); + + if ( ! ( $args = $this->check_optional_args_and_all( $args, $all, 'delete' ) ) ) { + return; + } + + $force = Utils\get_flag_value( $assoc_args, 'force', false ); + $successes = $errors = 0; foreach ( $this->fetcher->get_many( $args ) as $theme ) { $theme_slug = $theme->get_stylesheet(); - if ( $this->is_active_theme( $theme ) ) { + if ( $this->is_active_theme( $theme ) && ! $force ) { WP_CLI::warning( "Can't delete the currently active theme: $theme_slug" ); $errors++; continue; @@ -870,18 +885,18 @@ private function check_optional_args_and_all( $args, $all ) { return $args; } - + /** * Gets the template path based on installation type. */ private static function get_template_path( $template ) { $command_root = Utils\phar_safe_path( dirname( __DIR__ ) ); $template_path = "{$command_root}/templates/{$template}"; - + if ( ! file_exists( $template_path ) ) { WP_CLI::error( "Couldn't find {$template}" ); } - + return $template_path; } }