-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathTheme_Language_Command.php
More file actions
659 lines (576 loc) · 18.2 KB
/
Theme_Language_Command.php
File metadata and controls
659 lines (576 loc) · 18.2 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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
<?php
/**
* Installs, activates, and manages theme language packs.
*
* ## EXAMPLES
*
* # Install the Dutch theme language pack for Twenty Ten.
* $ wp language theme install twentyten nl_NL
* Downloading translation from https://downloads.wordpress.org/translation/theme/twentyten/4.0/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.
*
* # Uninstall the Dutch theme language pack for Twenty Ten.
* $ wp language theme uninstall twentyten nl_NL
* Language 'nl_NL' for 'twentyten' uninstalled.
* +-----------+--------+-------------+
* | name | locale | status |
* +-----------+--------+-------------+
* | twentyten | nl_NL | uninstalled |
* +-----------+--------+-------------+
* Success: Uninstalled 1 of 1 languages.
*
* # List installed theme language packs for Twenty Ten.
* $ wp language theme list twentyten --status=installed
* +-----------+----------+--------------+-------------+-----------+--------+---------------------+
* | theme | language | english_name | native_name | status | update | updated |
* +-----------+----------+--------------+-------------+-----------+--------+---------------------+
* | twentyten | nl_NL | Dutch | Nederlands | installed | none | 2023-12-29 21:21:39 |
* +-----------+----------+--------------+-------------+-----------+--------+---------------------+
*
* @package wp-cli
*/
class Theme_Language_Command extends WP_CLI\CommandWithTranslation {
protected $obj_type = 'themes';
protected $obj_fields = array(
'theme',
'language',
'english_name',
'native_name',
'status',
'update',
'updated',
);
/**
* Lists all available languages for one or more themes.
*
* ## OPTIONS
*
* [<theme>...]
* : One or more themes to list languages for.
*
* [--all]
* : If set, available languages for all themes will be listed.
*
* [--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:
*
* * theme
* * language
* * english_name
* * native_name
* * status
* * update
* * updated
*
* ## EXAMPLES
*
* # List available language packs for the theme.
* $ wp language theme list twentyten --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.
* @param array{all?: bool, field?: string, format: string, theme?: 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 ) {
$all = \WP_CLI\Utils\get_flag_value( $assoc_args, 'all', false );
if ( ! $all && empty( $args ) ) {
WP_CLI::error( 'Please specify one or more themes, or use --all.' );
}
if ( $all ) {
$args = array_map(
function ( $file ) {
return \WP_CLI\Utils\get_theme_name( $file );
},
array_keys( wp_get_themes() )
);
if ( empty( $args ) ) {
WP_CLI::success( 'No themes installed.' );
return;
}
}
$updates = $this->get_translation_updates();
$current_locale = get_locale();
$translations = array();
$themes = new \WP_CLI\Fetchers\Theme();
foreach ( $args as $theme ) {
if ( ! $themes->get( $theme ) ) {
WP_CLI::warning( "Theme '{$theme}' not found." );
continue;
}
$installed_translations = $this->get_installed_languages( $theme );
$available_translations = $this->get_all_languages( $theme );
foreach ( $available_translations as $translation ) {
$translation['theme'] = $theme;
$translation['status'] = in_array( $translation['language'], $installed_translations, true ) ? 'installed' : 'uninstalled';
if ( $current_locale === $translation['language'] ) {
$translation['status'] = 'active';
}
$filter_args = array(
'language' => $translation['language'],
'type' => 'theme',
'slug' => $theme,
);
$update = wp_list_filter( $updates, $filter_args );
$translation['update'] = $update ? 'available' : 'none';
// Support features like --status=active.
foreach ( array_keys( $translation ) as $field ) {
if ( isset( $assoc_args[ $field ] ) && ! in_array( $translation[ $field ], array_map( 'trim', explode( ',', $assoc_args[ $field ] ) ), true ) ) {
continue 2;
}
}
$translations[] = $translation;
}
}
$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
*
* <theme>
* : Theme to check for.
*
* <language>...
* : The language code to check.
*
* ## EXAMPLES
*
* # Check whether the German language is installed for Twenty Seventeen; exit status 0 if installed, otherwise 1.
* $ wp language theme is-installed twentyseventeen de_DE
* $ echo $?
* 1
*
* @subcommand is-installed
*
* @param non-empty-array<string> $args Positional arguments.
*/
public function is_installed( $args ) {
$theme = array_shift( $args );
$language_codes = (array) $args;
$available = $this->get_installed_languages( $theme );
foreach ( $language_codes as $language_code ) {
if ( ! in_array( $language_code, $available, true ) ) {
\WP_CLI::halt( 1 );
}
}
\WP_CLI::halt( 0 );
}
/**
* Installs a given language for a theme.
*
* Downloads the language pack from WordPress.org.
*
* ## OPTIONS
*
* [<theme>]
* : Theme to install language for.
*
* [--all]
* : If set, languages for all themes will be installed.
*
* <language>...
* : Language code to install.
*
* [--format=<format>]
* : Render output in a particular format. Used when installing languages for all themes.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - summary
* ---
*
* ## EXAMPLES
*
* # Install the Japanese language for Twenty Seventeen.
* $ wp language theme install twentyseventeen ja
* Downloading translation from https://downloads.wordpress.org/translation/theme/twentyseventeen/1.3/ja.zip...
* Unpacking the update...
* Installing the latest version...
* Translation updated successfully.
* Language 'ja' installed.
* Success: Installed 1 of 1 languages.
*
* @subcommand install
*
* @param string[] $args Positional arguments.
* @param array{all?: bool, format: string} $assoc_args Associative arguments.
*/
public function install( $args, $assoc_args ) {
$all = \WP_CLI\Utils\get_flag_value( $assoc_args, 'all', false );
if ( ! $all && count( $args ) < 2 ) {
\WP_CLI::error( 'Please specify a theme, or use --all.' );
}
if ( $all ) {
$this->install_many( $args, $assoc_args );
} else {
/**
* @var non-empty-list<string> $args
*/
$this->install_one( $args, $assoc_args );
}
}
/**
* Installs translations for a theme.
*
* @param non-empty-list<string> $args Positional arguments.
* @param array{all?: bool, format: string} $assoc_args Associative arguments.
*/
private function install_one( $args, $assoc_args ) {
$theme = array_shift( $args );
$language_codes = $args;
$count = count( $language_codes );
$available = $this->get_installed_languages( $theme );
$successes = 0;
$errors = 0;
$skips = 0;
foreach ( $language_codes as $language_code ) {
if ( in_array( $language_code, $available, true ) ) {
\WP_CLI::log( "Language '{$language_code}' already installed." );
++$skips;
} else {
$response = $this->download_language_pack( $language_code, $theme );
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() ) {
++$skips;
} else {
++$errors;
}
} else {
\WP_CLI::log( "Language '{$language_code}' installed." );
++$successes;
}
}
}
\WP_CLI\Utils\report_batch_operation_results( 'language', 'install', $count, $successes, $errors, $skips );
}
/**
* Installs translations for all installed themes.
*
* @param string[] $args Positional arguments.
* @param array{all?: bool, format: string} $assoc_args Associative arguments.
*/
private function install_many( $args, $assoc_args ) {
$language_codes = (array) $args;
/**
* @var \WP_Theme[] $themes
*/
$themes = wp_get_themes();
if ( empty( $assoc_args['format'] ) ) {
$assoc_args['format'] = 'table';
}
if ( in_array( $assoc_args['format'], array( 'json', 'csv' ), true ) ) {
$logger = new \WP_CLI\Loggers\Quiet();
\WP_CLI::set_logger( $logger );
}
if ( empty( $themes ) ) {
\WP_CLI::success( 'No themes installed.' );
return;
}
$count = count( $themes ) * count( $language_codes );
$results = array();
$successes = 0;
$errors = 0;
$skips = 0;
foreach ( $themes as $theme_path => $theme_details ) {
$theme_name = \WP_CLI\Utils\get_theme_name( $theme_path );
$available = $this->get_installed_languages( $theme_name );
/**
* @var string $display_name
*/
$display_name = $theme_details['Name'];
foreach ( $language_codes as $language_code ) {
$result = [
'name' => $theme_name,
'locale' => $language_code,
];
if ( in_array( $language_code, $available, true ) ) {
\WP_CLI::log( "Language '{$language_code}' for '{$display_name}' already installed." );
$result['status'] = 'already installed';
++$skips;
} else {
$response = $this->download_language_pack( $language_code, $theme_name );
if ( is_wp_error( $response ) ) {
\WP_CLI::warning( $response );
\WP_CLI::log( "Language '{$language_code}' for '{$display_name}' not installed." );
if ( 'not_found' === $response->get_error_code() ) {
$result['status'] = 'not available';
++$skips;
} else {
$result['status'] = 'not installed';
++$errors;
}
} else {
\WP_CLI::log( "Language '{$language_code}' for '{$display_name}' installed." );
$result['status'] = 'installed';
++$successes;
}
}
$results[] = (object) $result;
}
}
if ( 'summary' !== $assoc_args['format'] ) {
\WP_CLI\Utils\format_items( $assoc_args['format'], $results, array( 'name', 'locale', 'status' ) );
}
\WP_CLI\Utils\report_batch_operation_results( 'language', 'install', $count, $successes, $errors, $skips );
}
/**
* Uninstalls a given language for a theme.
*
* ## OPTIONS
*
* [<theme>]
* : Theme to uninstall language for.
*
* [--all]
* : If set, languages for all themes will be uninstalled.
*
* <language>...
* : Language code to uninstall.
*
* [--format=<format>]
* : Render output in a particular format. Used when installing languages for all themes.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - summary
* ---
*
* ## EXAMPLES
*
* # Uninstall the Japanese theme language pack for Twenty Ten.
* $ wp language theme uninstall twentyten ja
* Language 'ja' for 'twentyten' uninstalled.
* +-----------+--------+-------------+
* | name | locale | status |
* +-----------+--------+-------------+
* | twentyten | ja | uninstalled |
* +-----------+--------+-------------+
* Success: Uninstalled 1 of 1 languages.
*
* @subcommand uninstall
*
* @param string[] $args Positional arguments.
* @param array{all?: bool, format: string} $assoc_args Associative arguments.
*/
public function uninstall( $args, $assoc_args ) {
/** @var WP_Filesystem_Base $wp_filesystem */
global $wp_filesystem;
if ( empty( $assoc_args['format'] ) ) {
$assoc_args['format'] = 'table';
}
if ( in_array( $assoc_args['format'], array( 'json', 'csv' ), true ) ) {
$logger = new \WP_CLI\Loggers\Quiet();
\WP_CLI::set_logger( $logger );
}
$all = \WP_CLI\Utils\get_flag_value( $assoc_args, 'all', false );
if ( ! $all && count( $args ) < 2 ) {
\WP_CLI::error( 'Please specify one or more themes, or use --all.' );
}
if ( $all ) {
$themes = wp_get_themes();
if ( empty( $themes ) ) {
\WP_CLI::success( 'No themes installed.' );
return;
}
$process_themes = array();
foreach ( $themes as $theme_path => $theme_details ) {
$theme_name = \WP_CLI\Utils\get_theme_name( $theme_path );
array_push( $process_themes, $theme_name );
}
} else {
$process_themes = array( array_shift( $args ) );
}
$language_codes = (array) $args;
$current_locale = get_locale();
$dir = WP_LANG_DIR . "/$this->obj_type";
$files = scandir( $dir );
if ( ! $files ) {
\WP_CLI::error( 'No files found in language directory.' );
}
$count = count( $process_themes ) * count( $language_codes );
$results = array();
$successes = 0;
$errors = 0;
$skips = 0;
// As of WP 4.0, no API for deleting a language pack
WP_Filesystem();
/**
* @var string $theme
*/
foreach ( $process_themes as $theme ) {
$available_languages = $this->get_installed_languages( $theme );
foreach ( $language_codes as $language_code ) {
$result = [
'name' => $theme,
'locale' => $language_code,
'status' => 'not available',
];
if ( ! in_array( $language_code, $available_languages, true ) ) {
$result['status'] = 'not installed';
\WP_CLI::warning( "Language '{$language_code}' not installed." );
if ( $all ) {
++$skips;
} else {
++$errors;
}
$results[] = (object) $result;
continue;
}
if ( $language_code === $current_locale ) {
\WP_CLI::warning( "The '{$language_code}' language is active." );
exit;
}
$files_to_remove = array(
"$theme-$language_code.po",
"$theme-$language_code.mo",
"$theme-$language_code.l10n.php",
);
$count_files_to_remove = 0;
$count_files_removed = 0;
$had_one_file = false;
foreach ( $files as $file ) {
if ( '.' === $file[0] || is_dir( $file ) ) {
continue;
}
if (
! in_array( $file, $files_to_remove, true ) &&
! preg_match( "/$theme-$language_code-\w{32}\.json/", $file )
) {
continue;
}
$had_one_file = true;
++$count_files_to_remove;
if ( $wp_filesystem->delete( $dir . '/' . $file ) ) {
++$count_files_removed;
} else {
\WP_CLI::error( "Couldn't uninstall language: $language_code from theme $theme." );
}
}
if ( $count_files_to_remove === $count_files_removed ) {
$result['status'] = 'uninstalled';
++$successes;
$this->clear_translation_files_cache( $dir );
\WP_CLI::log( "Language '{$language_code}' for '{$theme}' uninstalled." );
} elseif ( $count_files_removed ) {
\WP_CLI::log( "Language '{$language_code}' for '{$theme}' partially uninstalled." );
$result['status'] = 'partial uninstall';
++$errors;
$this->clear_translation_files_cache( $dir );
} elseif ( $had_one_file ) { /* $count_files_removed == 0 */
\WP_CLI::log( "Couldn't uninstall language '{$language_code}' from theme {$theme}." );
$result['status'] = 'failed to uninstall';
++$errors;
} else {
\WP_CLI::log( "Language '{$language_code}' for '{$theme}' already uninstalled." );
$result['status'] = 'already uninstalled';
++$skips;
}
$results[] = (object) $result;
}
}
if ( 'summary' !== $assoc_args['format'] ) {
\WP_CLI\Utils\format_items( $assoc_args['format'], $results, array( 'name', 'locale', 'status' ) );
}
\WP_CLI\Utils\report_batch_operation_results( 'language', 'uninstall', $count, $successes, $errors, $skips );
}
/**
* Updates installed languages for one or more themes.
*
* ## OPTIONS
*
* [<theme>...]
* : One or more themes to update languages for.
*
* [--all]
* : If set, languages for all themes will be updated.
*
* [--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 all installed language packs for all themes.
* $ wp language theme update --all
* Updating 'Japanese' translation for Twenty Fifteen 1.5...
* Downloading translation from https://downloads.wordpress.org/translation/theme/twentyfifteen/1.5/ja.zip...
* Translation updated successfully.
* Success: Updated 1/1 translation.
*
* @subcommand update
*
* @param string[] $args Positional arguments.
* @param array{'dry-run'?: bool, all?: bool, format?: string} $assoc_args Associative arguments.
*/
public function update( $args, $assoc_args ) {
$all = \WP_CLI\Utils\get_flag_value( $assoc_args, 'all', false );
if ( ! $all && empty( $args ) ) {
WP_CLI::error( 'Please specify one or more themes, or use --all.' );
}
if ( $all ) {
$args = array_map( '\WP_CLI\Utils\get_theme_name', array_keys( wp_get_themes() ) );
if ( empty( $args ) ) {
WP_CLI::success( 'No themes installed.' );
return;
}
}
parent::update( $args, $assoc_args );
}
}