Skip to content

Commit 5fc2c84

Browse files
Avoid deleting parent of active theme unless using --force (#324)
1 parent 362a142 commit 5fc2c84

File tree

3 files changed

+85
-13
lines changed

3 files changed

+85
-13
lines changed

features/theme-delete.feature

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,26 @@ Feature: Delete WordPress themes
1919

2020
When I try `wp theme delete p2`
2121
Then STDERR should be:
22-
"""
23-
Warning: Can't delete the currently active theme: p2
24-
Error: No themes deleted.
25-
"""
22+
"""
23+
Warning: Can't delete the currently active theme: p2
24+
Error: No themes deleted.
25+
"""
2626

2727
When I try `wp theme delete p2 --force`
2828
Then STDOUT should contain:
29-
"""
30-
Deleted 'p2' theme.
31-
"""
29+
"""
30+
Deleted 'p2' theme.
31+
"""
3232

3333
Scenario: Delete all installed themes
3434
When I run `wp theme list --status=active --field=name --porcelain`
3535
And save STDOUT as {ACTIVE_THEME}
3636

3737
When I try `wp theme delete --all`
3838
Then STDOUT should contain:
39-
"""
40-
Success: Deleted
41-
"""
39+
"""
40+
Success: Deleted
41+
"""
4242
And STDERR should be empty
4343

4444
When I run `wp theme delete --all --force`
@@ -50,9 +50,62 @@ Feature: Delete WordPress themes
5050

5151
When I try the previous command again
5252
Then STDOUT should be:
53-
"""
54-
Success: No themes deleted.
55-
"""
53+
"""
54+
Success: No themes deleted.
55+
"""
56+
57+
Scenario: Delete all installed themes when active theme has a parent
58+
Given a WP install
59+
And I run `wp theme install moina-blog --activate`
60+
61+
When I run `wp theme list --field=name`
62+
Then STDOUT should contain:
63+
"""
64+
moina-blog
65+
moina
66+
"""
67+
68+
When I try `wp theme delete moina-blog`
69+
Then STDERR should contain:
70+
"""
71+
Can't delete the currently active theme
72+
"""
73+
And STDERR should contain:
74+
"""
75+
Error: No themes deleted.
76+
"""
77+
78+
When I try `wp theme delete moina`
79+
Then STDERR should contain:
80+
"""
81+
Can't delete the parent of the currently active theme
82+
"""
83+
And STDERR should contain:
84+
"""
85+
Error: No themes deleted.
86+
"""
87+
88+
When I run `wp theme delete --all`
89+
Then STDOUT should contain:
90+
"""
91+
Success: Deleted
92+
"""
93+
94+
When I run `wp theme list --field=name`
95+
Then STDOUT should contain:
96+
"""
97+
moina-blog
98+
moina
99+
"""
100+
101+
When I run `wp theme delete --all --force`
102+
Then STDOUT should contain:
103+
"""
104+
Success: Deleted
105+
"""
106+
107+
When I run `wp theme list --field=name`
108+
Then STDOUT should be empty
56109

57110
Scenario: Attempting to delete a theme that doesn't exist
58111
When I run `wp theme delete p2`

src/Theme_Command.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,14 @@ public function delete( $args, $assoc_args ) {
782782
continue;
783783
}
784784

785+
if ( $this->is_active_parent_theme( $theme ) && ! $force ) {
786+
if ( ! $all ) {
787+
WP_CLI::warning( "Can't delete the parent of the currently active theme: $theme_slug" );
788+
$errors++;
789+
}
790+
continue;
791+
}
792+
785793
$r = delete_theme( $theme_slug );
786794

787795
if ( is_wp_error( $r ) ) {

src/WP_CLI/ParseThemeNameInput.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,17 @@ protected function is_active_theme( $theme ) {
158158
return $theme->get_stylesheet_directory() === get_stylesheet_directory();
159159
}
160160

161+
/**
162+
* Check whether a given theme is the active theme parent.
163+
*
164+
* @param string $theme Theme to check.
165+
*
166+
* @return bool Whether the provided theme is the active theme.
167+
*/
168+
protected function is_active_parent_theme( $theme ) {
169+
return $theme->get_stylesheet_directory() === get_template_directory();
170+
}
171+
161172
/**
162173
* Get the available update info.
163174
*

0 commit comments

Comments
 (0)