Skip to content

Commit efe1656

Browse files
committed
Properly delete JSON & PHP translation files
1 parent 24f76e3 commit efe1656

File tree

6 files changed

+92
-40
lines changed

6 files changed

+92
-40
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: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -246,38 +246,59 @@ 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 ) ) {
256-
WP_CLI::error( 'Language not installed.' );
257-
}
258-
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+
if ( count( $language_codes ) === 1 ) {
264+
WP_CLI::error( 'Language not installed.' );
265+
} else {
266+
WP_CLI::warning( 'Language not installed.' );
267+
}
263268
}
264269

265-
$current_locale = get_locale();
266270
if ( $language_code === $current_locale ) {
267271
WP_CLI::warning( "The '{$language_code}' language is active." );
268-
exit;
272+
continue;
269273
}
270274

275+
$files_to_remove = array(
276+
"$language_code.po",
277+
"$language_code.mo",
278+
"$language_code.l10n.php",
279+
"admin-$language_code.po",
280+
"admin-$language_code.mo",
281+
"admin-$language_code.l10n.php",
282+
"admin-network-$language_code.po",
283+
"admin-network-$language_code.mo",
284+
"admin-network-$language_code.l10n.php",
285+
"continents-cities-$language_code.po",
286+
"continents-cities-$language_code.mo",
287+
"continents-cities-$language_code.l10n.php",
288+
);
289+
271290
// As of WP 4.0, no API for deleting a language pack
272291
WP_Filesystem();
273292
$deleted = false;
274293
foreach ( $files as $file ) {
275294
if ( '.' === $file[0] || is_dir( $file ) ) {
276295
continue;
277296
}
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 ) ) {
297+
298+
if (
299+
! in_array( $file, $files_to_remove, true ) &&
300+
! preg_match( "$language_code-\w{32}\.json", $file )
301+
) {
281302
continue;
282303
}
283304

src/Plugin_Language_Command.php

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -472,24 +472,36 @@ public function uninstall( $args, $assoc_args ) {
472472

473473
if ( $language_code === $current_locale ) {
474474
\WP_CLI::warning( "The '{$language_code}' language is active." );
475-
exit;
475+
continue;
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 );
478+
$files_to_remove = array(
479+
"$plugin-$language_code.po",
480+
"$plugin-$language_code.mo",
481+
"$plugin-$language_code.l10n.php",
482+
);
482483

483484
$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-
}
485+
$had_one_file = false;
486+
487+
foreach ( $files as $file ) {
488+
if ( '.' === $file[0] || is_dir( $file ) ) {
489+
continue;
490+
}
491+
492+
if (
493+
! in_array( $file, $files_to_remove, true ) &&
494+
! preg_match( "$plugin-$language_code-\w{32}\.json", $file )
495+
) {
496+
continue;
497+
}
498+
499+
$had_one_file = true;
500+
501+
if ( $wp_filesystem->delete( WP_LANG_DIR . $dir . '/' . $file ) ) {
502+
++$count_files_removed;
503+
} else {
504+
\WP_CLI::error( "Couldn't uninstall language: $language_code from plugin $plugin." );
493505
}
494506
}
495507

src/Theme_Language_Command.php

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -486,21 +486,33 @@ 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 );
489+
$files_to_remove = array(
490+
"$theme-$language_code.po",
491+
"$theme-$language_code.mo",
492+
"$theme-$language_code.l10n.php",
493+
);
493494

494495
$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-
}
496+
$had_one_file = false;
497+
498+
foreach ( $files as $file ) {
499+
if ( '.' === $file[0] || is_dir( $file ) ) {
500+
continue;
501+
}
502+
503+
if (
504+
! in_array( $file, $files_to_remove, true ) &&
505+
! preg_match( "$theme-$language_code-\w{32}\.json", $file )
506+
) {
507+
continue;
508+
}
509+
510+
$had_one_file = true;
511+
512+
if ( $wp_filesystem->delete( WP_LANG_DIR . $dir . '/' . $file ) ) {
513+
++$count_files_removed;
514+
} else {
515+
\WP_CLI::error( "Couldn't uninstall language: $language_code from theme $theme." );
504516
}
505517
}
506518

0 commit comments

Comments
 (0)