-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathCore_Language_Command.php
More file actions
478 lines (429 loc) · 12.9 KB
/
Core_Language_Command.php
File metadata and controls
478 lines (429 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
<?php
/**
* Installs, activates, and manages core language packs.
*
* ## EXAMPLES
*
* # Install the Dutch core language pack.
* $ wp language core install nl_NL
* Downloading translation from https://downloads.wordpress.org/translation/core/6.4.3/nl_NL.zip...
* Unpacking the update...
* Installing the latest version...
* Removing the old version of the translation...
* Translation updated successfully.
* Language 'nl_NL' installed.
* Success: Installed 1 of 1 languages.
*
* # Activate the Dutch core language pack.
* $ wp site switch-language nl_NL
* Success: Language activated.
*
* # Uninstall the Dutch core language pack.
* $ wp language core uninstall nl_NL
* Success: Language uninstalled.
*
* # List installed core language packs.
* $ wp language core list --status=installed
* +----------+--------------+-------------+-----------+-----------+---------------------+
* | language | english_name | native_name | status | update | updated |
* +----------+--------------+-------------+-----------+-----------+---------------------+
* | nl_NL | Dutch | Nederlands | installed | available | 2024-01-31 10:24:06 |
* +----------+--------------+-------------+-----------+-----------+---------------------+
*
* @package wp-cli
*/
class Core_Language_Command extends WP_CLI\CommandWithTranslation {
protected $obj_type = 'core';
protected $obj_fields = array(
'language',
'english_name',
'native_name',
'status',
'update',
'updated',
);
/**
* Lists all available languages.
*
* ## OPTIONS
*
* [--field=<field>]
* : Display the value of a single field
*
* [--<field>=<value>]
* : Filter results by key=value pairs.
*
* [--fields=<fields>]
* : Limit the output to specific fields.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - count
* ---
*
* ## AVAILABLE FIELDS
*
* These fields will be displayed by default for each translation:
*
* * language
* * english_name
* * native_name
* * status
* * update
* * updated
*
* ## EXAMPLES
*
* # List language,english_name,status fields of available languages.
* $ wp language core list --fields=language,english_name,status
* +----------------+-------------------------+-------------+
* | language | english_name | status |
* +----------------+-------------------------+-------------+
* | ar | Arabic | uninstalled |
* | ary | Moroccan Arabic | uninstalled |
* | az | Azerbaijani | uninstalled |
*
* @subcommand list
*
* @param string[] $args Positional arguments. Unused.
* @param array{field?: string, format: string, language?: string, english_name?: string, native_name?: string, status?: string, update?: string, updated?: string} $assoc_args Associative arguments.
*/
public function list_( $args, $assoc_args ) {
$translations = $this->get_all_languages();
$available = $this->get_installed_languages();
$updates = $this->get_translation_updates();
$current_locale = get_locale();
$translations = array_map(
function ( $translation ) use ( $available, $current_locale, $updates ) {
$translation['status'] = 'uninstalled';
if ( in_array( $translation['language'], $available, true ) ) {
$translation['status'] = 'installed';
}
if ( $current_locale === $translation['language'] ) {
$translation['status'] = 'active';
}
$update = wp_list_filter( $updates, array( 'language' => $translation['language'] ) );
if ( $update ) {
$translation['update'] = 'available';
} else {
$translation['update'] = 'none';
}
return $translation;
},
$translations
);
foreach ( $translations as $key => $translation ) {
foreach ( array_keys( $translation ) as $field ) {
if ( isset( $assoc_args[ $field ] ) && ! in_array( $translation[ $field ], array_map( 'trim', explode( ',', $assoc_args[ $field ] ) ), true ) ) {
unset( $translations[ $key ] );
}
}
}
$formatter = $this->get_formatter( $assoc_args );
$formatter->display_items( $translations );
}
/**
* Checks if a given language is installed.
*
* Returns exit code 0 when installed, 1 when uninstalled.
*
* ## OPTIONS
*
* <language>
* : The language code to check.
*
* ## EXAMPLES
*
* # Check whether the German language is installed; exit status 0 if installed, otherwise 1.
* $ wp language core is-installed de_DE
* $ echo $?
* 1
*
* @subcommand is-installed
*
* @param array{string} $args Positional arguments.
*/
public function is_installed( $args ) {
list( $language_code ) = $args;
$available = $this->get_installed_languages();
if ( in_array( $language_code, $available, true ) ) {
\WP_CLI::halt( 0 );
} else {
\WP_CLI::halt( 1 );
}
}
/**
* Installs a given language.
*
* Downloads the language pack from WordPress.org. Find your language code at: https://translate.wordpress.org/
*
* ## OPTIONS
*
* <language>...
* : Language code to install.
*
* [--activate]
* : If set, the language will be activated immediately after install.
*
* [--format=<format>]
* : Render output in a particular format (for one or more languages).
* ---
* options:
* - table
* - csv
* - json
* - summary
* ---
*
* ## EXAMPLES
*
* # Install the Brazilian Portuguese language.
* $ wp language core install pt_BR
* Downloading translation from https://downloads.wordpress.org/translation/core/6.5/pt_BR.zip...
* Unpacking the update...
* Installing the latest version...
* Removing the old version of the translation...
* Translation updated successfully.
* Language 'pt_BR' installed.
* Success: Installed 1 of 1 languages.
*
* @subcommand install
*
* @param string[] $args Positional arguments.
* @param array{activate?: bool, format?: string} $assoc_args Associative arguments.
*/
public function install( $args, $assoc_args ) {
$language_codes = (array) $args;
$count = count( $language_codes );
if ( $count > 1 && in_array( true, $assoc_args, true ) ) {
WP_CLI::error( 'Only a single language can be active.' );
}
$has_format_flag = isset( $assoc_args['format'] );
if ( $has_format_flag ) {
if ( in_array( $assoc_args['format'], array( 'json', 'csv' ), true ) ) {
$logger = new \WP_CLI\Loggers\Quiet();
\WP_CLI::set_logger( $logger );
}
}
$available = $this->get_installed_languages();
$results = array();
$successes = 0;
$errors = 0;
$skips = 0;
foreach ( $language_codes as $language_code ) {
$result = [];
if ( $has_format_flag ) {
$result = [
'locale' => $language_code,
];
}
if ( in_array( $language_code, $available, true ) ) {
\WP_CLI::log( "Language '{$language_code}' already installed." );
if ( $has_format_flag ) {
$result['status'] = 'already installed';
}
++$skips;
} else {
$response = $this->download_language_pack( $language_code );
if ( is_wp_error( $response ) ) {
\WP_CLI::warning( $response );
\WP_CLI::log( "Language '{$language_code}' not installed." );
// Skip if translation is not yet available.
if ( 'not_found' === $response->get_error_code() ) {
if ( $has_format_flag ) {
$result['status'] = 'not available';
}
++$skips;
} else {
if ( $has_format_flag ) {
$result['status'] = 'not installed';
}
++$errors;
}
} else {
\WP_CLI::log( "Language '{$language_code}' installed." );
if ( $has_format_flag ) {
$result['status'] = 'installed';
}
++$successes;
}
}
if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'activate' ) ) {
$this->activate_language( $language_code );
}
if ( $has_format_flag ) {
$results[] = (object) $result;
}
}
if ( $has_format_flag ) {
if ( 'summary' !== $assoc_args['format'] ) {
\WP_CLI\Utils\format_items( $assoc_args['format'], $results, array( 'locale', 'status' ) );
}
}
\WP_CLI\Utils\report_batch_operation_results( 'language', 'install', $count, $successes, $errors, $skips );
}
/**
* Uninstalls a given language.
*
* ## OPTIONS
*
* <language>...
* : Language code to uninstall.
*
* ## EXAMPLES
*
* # Uninstall the Japanese core language pack.
* $ wp language core uninstall ja
* Success: Language uninstalled.
*
* @subcommand uninstall
* @throws WP_CLI\ExitException
*
* @param string[] $args Positional arguments.
*/
public function uninstall( $args ) {
global $wp_filesystem;
$dir = 'core' === $this->obj_type ? '' : "/$this->obj_type";
$files = scandir( WP_LANG_DIR . $dir );
if ( ! $files ) {
WP_CLI::error( 'No files found in language directory.' );
}
$language_codes = (array) $args;
$available = $this->get_installed_languages();
$current_locale = get_locale();
foreach ( $language_codes as $language_code ) {
if ( ! in_array( $language_code, $available, true ) ) {
WP_CLI::error( 'Language not installed.' );
}
if ( $language_code === $current_locale ) {
WP_CLI::warning( "The '{$language_code}' language is active." );
exit;
}
$files_to_remove = array(
"$language_code.po",
"$language_code.mo",
"$language_code.l10n.php",
"admin-$language_code.po",
"admin-$language_code.mo",
"admin-$language_code.l10n.php",
"admin-network-$language_code.po",
"admin-network-$language_code.mo",
"admin-network-$language_code.l10n.php",
"continents-cities-$language_code.po",
"continents-cities-$language_code.mo",
"continents-cities-$language_code.l10n.php",
);
// As of WP 4.0, no API for deleting a language pack
WP_Filesystem();
$deleted = false;
foreach ( $files as $file ) {
if ( '.' === $file[0] || is_dir( $file ) ) {
continue;
}
if (
! in_array( $file, $files_to_remove, true ) &&
! preg_match( "/$language_code-\w{32}\.json/", $file )
) {
continue;
}
/** @var WP_Filesystem_Base $wp_filesystem */
if ( $wp_filesystem->delete( WP_LANG_DIR . $dir . '/' . $file ) ) {
$deleted = true;
}
}
if ( $deleted ) {
$this->clear_translation_files_cache( WP_LANG_DIR . $dir );
WP_CLI::success( 'Language uninstalled.' );
} else {
WP_CLI::error( "Couldn't uninstall language." );
}
}
}
/**
* Updates installed languages for core.
*
* ## OPTIONS
*
* [--dry-run]
* : Preview which translations would be updated.
*
* [--format=<format>]
* : Render output in a particular format. When not specified, updates show
* progress messages and success/warning/error messages. When specified,
* the output format changes based on the selected format.
* ---
* options:
* - table
* - csv
* - json
* - summary
* ---
*
* ## EXAMPLES
*
* # Update installed core languages packs.
* $ wp language core update
* Updating 'Japanese' translation for WordPress 6.4.3...
* Downloading translation from https://downloads.wordpress.org/translation/core/6.4.3/ja.zip...
* Translation updated successfully.
* Success: Updated 1/1 translation.
*
* @subcommand update
*
* @param string[] $args Positional arguments.
* @param array{'dry-run'?: bool, format?: string} $assoc_args Associative arguments.
*/
public function update( $args, $assoc_args ) { // phpcs:ignore Generic.CodeAnalysis.UselessOverridingMethod.Found -- Overruling the documentation, so not useless ;-).
parent::update( $args, $assoc_args );
}
/**
* Activates a given language.
*
* **Warning: `wp language core activate` is deprecated. Use `wp site switch-language` instead.**
*
* ## OPTIONS
*
* <language>
* : Language code to activate.
*
* ## EXAMPLES
*
* # Activate the given language.
* $ wp language core activate ja
* Success: Language activated.
*
* @subcommand activate
* @throws WP_CLI\ExitException
*
* @param array{string} $args Positional arguments.
*/
public function activate( $args ) {
\WP_CLI::warning( 'This command is deprecated. use wp site switch-language instead' );
list( $language_code ) = $args;
$this->activate_language( $language_code );
}
/**
* @param string $language_code
*/
private function activate_language( $language_code ) {
$available = $this->get_installed_languages();
if ( ! in_array( $language_code, $available, true ) ) {
WP_CLI::error( 'Language not installed.' );
}
if ( 'en_US' === $language_code ) {
$language_code = '';
}
if ( get_locale() === $language_code ) {
WP_CLI::warning( "Language '{$language_code}' already active." );
return;
}
update_option( 'WPLANG', $language_code );
WP_CLI::success( 'Language activated.' );
}
}