Skip to content

Commit 4d0a2e5

Browse files
authored
Merge pull request #137 from wp-cli/fix/129-uninstall
2 parents 24f76e3 + f7efd49 commit 4d0a2e5

File tree

6 files changed

+97
-43
lines changed

6 files changed

+97
-43
lines changed

features/language-core.feature

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ Feature: Manage translation files for a WordPress install
165165
When I run `wp language core uninstall en_GB`
166166
Then the wp-content/languages/admin-en_GB.po file should not exist
167167
And the wp-content/languages/en_GB.po file should not exist
168+
And the wp-content/languages/en_GB.l10n.php file should not exist
168169
And STDOUT should be:
169170
"""
170171
Success: Language uninstalled.
@@ -173,8 +174,10 @@ Feature: Manage translation files for a WordPress install
173174
When I run `wp language core uninstall en_CA ja`
174175
Then the wp-content/languages/admin-en_CA.po file should not exist
175176
And the wp-content/languages/en_CA.po file should not exist
177+
And the wp-content/languages/en_CA.l10n.php file should not exist
176178
And the wp-content/languages/admin-ja.po file should not exist
177179
And the wp-content/languages/ja.po file should not exist
180+
And the wp-content/languages/ja.l10n.php file should not exist
178181
And STDOUT should be:
179182
"""
180183
Success: Language uninstalled.

features/language-plugin.feature

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,10 @@ Feature: Manage translation files for a WordPress install
125125
When I run `wp language plugin uninstall hello-dolly cs_CZ de_DE`
126126
Then the wp-content/languages/plugins/hello-dolly-cs_CZ.po file should not exist
127127
And the wp-content/languages/plugins/hello-dolly-cs_CZ.mo file should not exist
128+
And the wp-content/languages/plugins/hello-dolly-cs_CZ.l10n.php file should not exist
128129
And the wp-content/languages/plugins/hello-dolly-de_DE.po file should not exist
129130
And the wp-content/languages/plugins/hello-dolly-de_DE.mo file should not exist
131+
And the wp-content/languages/plugins/hello-dolly-de_DE.l10n.php file should not exist
130132
And STDOUT should contain:
131133
"""
132134
Language 'cs_CZ' for 'hello-dolly' uninstalled.

features/language-theme.feature

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,10 @@ Feature: Manage translation files for a WordPress install
119119
When I run `wp language theme uninstall twentyten cs_CZ de_DE`
120120
Then the wp-content/languages/themes/twentyten-cs_CZ.po file should not exist
121121
And the wp-content/languages/themes/twentyten-cs_CZ.mo file should not exist
122+
And the wp-content/languages/themes/twentyten-cs_CZ.l10n.php file should not exist
122123
And the wp-content/languages/themes/twentyten-de_DE.po file should not exist
123124
And the wp-content/languages/themes/twentyten-de_DE.mo file should not exist
125+
And the wp-content/languages/themes/twentyten-de_DE.l10n.php file should not exist
124126
"""
125127
Success: Language 'cs_CZ' for 'twentyten' uninstalled.
126128
Success: Language 'de_DE' for 'twentyten' uninstalled.

src/Core_Language_Command.php

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -246,38 +246,55 @@ public function install( $args, $assoc_args ) {
246246
public function uninstall( $args, $assoc_args ) {
247247
global $wp_filesystem;
248248

249+
$dir = 'core' === $this->obj_type ? '' : "/$this->obj_type";
250+
$files = scandir( WP_LANG_DIR . $dir );
251+
if ( ! $files ) {
252+
WP_CLI::error( 'No files found in language directory.' );
253+
}
254+
249255
$language_codes = (array) $args;
250256

251257
$available = $this->get_installed_languages();
252258

253-
foreach ( $language_codes as $language_code ) {
259+
$current_locale = get_locale();
254260

261+
foreach ( $language_codes as $language_code ) {
255262
if ( ! in_array( $language_code, $available, true ) ) {
256263
WP_CLI::error( 'Language not installed.' );
257264
}
258265

259-
$dir = 'core' === $this->obj_type ? '' : "/$this->obj_type";
260-
$files = scandir( WP_LANG_DIR . $dir );
261-
if ( ! $files ) {
262-
WP_CLI::error( 'No files found in language directory.' );
263-
}
264-
265-
$current_locale = get_locale();
266266
if ( $language_code === $current_locale ) {
267267
WP_CLI::warning( "The '{$language_code}' language is active." );
268268
exit;
269269
}
270270

271+
$files_to_remove = array(
272+
"$language_code.po",
273+
"$language_code.mo",
274+
"$language_code.l10n.php",
275+
"admin-$language_code.po",
276+
"admin-$language_code.mo",
277+
"admin-$language_code.l10n.php",
278+
"admin-network-$language_code.po",
279+
"admin-network-$language_code.mo",
280+
"admin-network-$language_code.l10n.php",
281+
"continents-cities-$language_code.po",
282+
"continents-cities-$language_code.mo",
283+
"continents-cities-$language_code.l10n.php",
284+
);
285+
271286
// As of WP 4.0, no API for deleting a language pack
272287
WP_Filesystem();
273288
$deleted = false;
274289
foreach ( $files as $file ) {
275290
if ( '.' === $file[0] || is_dir( $file ) ) {
276291
continue;
277292
}
278-
$extension_length = strlen( $language_code ) + 4;
279-
$ending = substr( $file, -$extension_length );
280-
if ( ! in_array( $file, array( $language_code . '.po', $language_code . '.mo' ), true ) && ! in_array( $ending, array( '-' . $language_code . '.po', '-' . $language_code . '.mo' ), true ) ) {
293+
294+
if (
295+
! in_array( $file, $files_to_remove, true ) &&
296+
! preg_match( "/$language_code-\w{32}\.json/", $file )
297+
) {
281298
continue;
282299
}
283300

src/Plugin_Language_Command.php

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -475,25 +475,40 @@ public function uninstall( $args, $assoc_args ) {
475475
exit;
476476
}
477477

478-
$po_file = "{$dir}/{$plugin}-{$language_code}.po";
479-
$mo_file = "{$dir}/{$plugin}-{$language_code}.mo";
480-
481-
$files_to_remove = array( $po_file, $mo_file );
482-
483-
$count_files_removed = 0;
484-
$had_one_file = 0;
485-
foreach ( $files_to_remove as $file ) {
486-
if ( $wp_filesystem->exists( $file ) ) {
487-
$had_one_file = 1;
488-
if ( $wp_filesystem->delete( $file ) ) {
489-
++$count_files_removed;
490-
} else {
491-
\WP_CLI::error( "Couldn't uninstall language: $language_code from plugin $plugin." );
492-
}
478+
$files_to_remove = array(
479+
"$plugin-$language_code.po",
480+
"$plugin-$language_code.mo",
481+
"$plugin-$language_code.l10n.php",
482+
);
483+
484+
$count_files_to_remove = 0;
485+
$count_files_removed = 0;
486+
$had_one_file = false;
487+
488+
foreach ( $files as $file ) {
489+
if ( '.' === $file[0] || is_dir( $file ) ) {
490+
continue;
491+
}
492+
493+
if (
494+
! in_array( $file, $files_to_remove, true ) &&
495+
! preg_match( "/$plugin-$language_code-\w{32}\.json/", $file )
496+
) {
497+
continue;
498+
}
499+
500+
$had_one_file = true;
501+
502+
++$count_files_to_remove;
503+
504+
if ( $wp_filesystem->delete( $dir . '/' . $file ) ) {
505+
++$count_files_removed;
506+
} else {
507+
\WP_CLI::error( "Couldn't uninstall language: $language_code from plugin $plugin." );
493508
}
494509
}
495510

496-
if ( count( $files_to_remove ) === $count_files_removed ) {
511+
if ( $count_files_to_remove === $count_files_removed ) {
497512
$result['status'] = 'uninstalled';
498513
++$successes;
499514
\WP_CLI::log( "Language '{$language_code}' for '{$plugin}' uninstalled." );

src/Theme_Language_Command.php

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -486,25 +486,40 @@ public function uninstall( $args, $assoc_args ) {
486486
exit;
487487
}
488488

489-
$po_file = "{$dir}/{$theme}-{$language_code}.po";
490-
$mo_file = "{$dir}/{$theme}-{$language_code}.mo";
491-
492-
$files_to_remove = array( $po_file, $mo_file );
493-
494-
$count_files_removed = 0;
495-
$had_one_file = 0;
496-
foreach ( $files_to_remove as $file ) {
497-
if ( $wp_filesystem->exists( $file ) ) {
498-
$had_one_file = 1;
499-
if ( $wp_filesystem->delete( $file ) ) {
500-
++$count_files_removed;
501-
} else {
502-
\WP_CLI::error( "Couldn't uninstall language: $language_code from theme $theme." );
503-
}
489+
$files_to_remove = array(
490+
"$theme-$language_code.po",
491+
"$theme-$language_code.mo",
492+
"$theme-$language_code.l10n.php",
493+
);
494+
495+
$count_files_to_remove = 0;
496+
$count_files_removed = 0;
497+
$had_one_file = false;
498+
499+
foreach ( $files as $file ) {
500+
if ( '.' === $file[0] || is_dir( $file ) ) {
501+
continue;
502+
}
503+
504+
if (
505+
! in_array( $file, $files_to_remove, true ) &&
506+
! preg_match( "/$theme-$language_code-\w{32}\.json/", $file )
507+
) {
508+
continue;
509+
}
510+
511+
$had_one_file = true;
512+
513+
++$count_files_to_remove;
514+
515+
if ( $wp_filesystem->delete( $dir . '/' . $file ) ) {
516+
++$count_files_removed;
517+
} else {
518+
\WP_CLI::error( "Couldn't uninstall language: $language_code from theme $theme." );
504519
}
505520
}
506521

507-
if ( count( $files_to_remove ) === $count_files_removed ) {
522+
if ( $count_files_to_remove === $count_files_removed ) {
508523
$result['status'] = 'uninstalled';
509524
++$successes;
510525
\WP_CLI::log( "Language '{$language_code}' for '{$theme}' uninstalled." );

0 commit comments

Comments
 (0)