-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathCommandWithTranslation.php
More file actions
383 lines (305 loc) · 10.1 KB
/
CommandWithTranslation.php
File metadata and controls
383 lines (305 loc) · 10.1 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
<?php
namespace WP_CLI;
use WP_CLI;
use WP_CLI_Command;
/**
* Base class for WP-CLI commands that deal with translations
*
* @package wp-cli
*/
abstract class CommandWithTranslation extends WP_CLI_Command {
protected $obj_type;
protected $obj_fields;
/**
* Callback to sort array by a 'language' key.
*/
protected function sort_translations_callback( $a, $b ) {
return strnatcasecmp( $a['language'], $b['language'] );
}
/**
* Updates installed languages for the current object type.
*
* @param string[] $args Positional arguments.
* @param array{'dry-run'?: bool, all?: bool} $assoc_args Associative arguments.
*/
public function update( $args, $assoc_args ) {
$updates = $this->get_translation_updates();
if ( empty( $updates ) ) {
WP_CLI::success( 'Translations are up to date.' );
return;
}
if ( empty( $args ) ) {
$args = array( null ); // Used for core.
}
$upgrader = 'WP_CLI\\LanguagePackUpgrader';
$results = array();
$num_to_update = 0;
foreach ( $args as $slug ) {
// Gets a list of all languages.
$all_languages = $this->get_all_languages( $slug );
$updates_per_type = array();
// Formats the updates list.
foreach ( $updates as $update ) {
if ( null !== $slug && $update->slug !== $slug ) {
continue;
}
$name = 'WordPress'; // Core.
if ( 'plugin' === $update->type ) {
/**
* @var array<array{Name: string}> $plugins
*/
$plugins = get_plugins( '/' . $update->slug );
/**
* @var array{Name: string}> $plugin_data
*/
$plugin_data = array_shift( $plugins );
$name = $plugin_data['Name'];
} elseif ( 'theme' === $update->type ) {
$theme_data = wp_get_theme( $update->slug );
/**
* @var string $name
*/
$name = $theme_data['Name'];
}
// Gets the translation data.
$translation = wp_list_filter( $all_languages, array( 'language' => $update->language ) );
$translation = (object) reset( $translation );
$update->Type = ucfirst( $update->type );
$update->Name = $name;
$update->Version = $update->version;
if ( isset( $translation->english_name ) ) {
$update->Language = $translation->english_name;
}
if ( ! isset( $updates_per_type[ $update->type ] ) ) {
$updates_per_type[ $update->type ] = array();
}
$updates_per_type[ $update->type ][] = $update;
}
$obj_type = rtrim( $this->obj_type, 's' );
$available_updates = isset( $updates_per_type[ $obj_type ] ) ? $updates_per_type[ $obj_type ] : null;
if ( ! is_array( $available_updates ) ) {
continue;
}
$num_to_update += count( $available_updates );
if ( ! Utils\get_flag_value( $assoc_args, 'dry-run' ) ) {
// Update translations.
foreach ( $available_updates as $update ) {
WP_CLI::line( "Updating '{$update->Language}' translation for {$update->Name} {$update->Version}..." );
/**
* @var \WP_CLI\LanguagePackUpgrader $upgrader_instance
*/
$upgrader_instance = Utils\get_upgrader( $upgrader );
$result = $upgrader_instance->upgrade( $update );
$results[] = $result;
}
}
}
// Only preview which translations would be updated.
if ( Utils\get_flag_value( $assoc_args, 'dry-run' ) ) {
$update_count = count( $updates );
WP_CLI::line(
sprintf(
'Found %d translation %s that would be processed:',
$update_count,
WP_CLI\Utils\pluralize( 'update', $update_count )
)
);
Utils\format_items( 'table', $updates, array( 'Type', 'Name', 'Version', 'Language' ) );
return;
}
$num_updated = count( array_filter( $results ) );
$line = sprintf( "Updated $num_updated/$num_to_update %s.", WP_CLI\Utils\pluralize( 'translation', $num_updated ) );
if ( $num_to_update === $num_updated ) {
WP_CLI::success( $line );
} elseif ( $num_updated > 0 ) {
WP_CLI::warning( $line );
} else {
WP_CLI::error( $line );
}
}
/**
* Get all updates available for all translations.
*
* @see wp_get_translation_updates()
*
* @return array
*/
protected function get_translation_updates() {
$available = $this->get_installed_languages();
$func = function () use ( $available ) {
return $available;
};
switch ( $this->obj_type ) {
case 'plugins':
add_filter( 'plugins_update_check_locales', $func );
wp_clean_plugins_cache();
// Check for Plugin translation updates.
wp_update_plugins();
remove_filter( 'plugins_update_check_locales', $func );
$transient = 'update_plugins';
break;
case 'themes':
add_filter( 'themes_update_check_locales', $func );
wp_clean_themes_cache();
// Check for Theme translation updates.
wp_update_themes();
remove_filter( 'themes_update_check_locales', $func );
$transient = 'update_themes';
break;
default:
delete_site_transient( 'update_core' );
// Check for Core translation updates.
wp_version_check();
$transient = 'update_core';
break;
}
$updates = array();
/**
* @var object{translations: array} $transient
*/
$transient = get_site_transient( $transient );
if ( empty( $transient->translations ) ) {
return $updates;
}
foreach ( $transient->translations as $translation ) {
$updates[] = (object) $translation;
}
return $updates;
}
/**
* Download a language pack.
*
* @see wp_download_language_pack()
*
* @param string $download Language code to download.
* @param string $slug Plugin or theme slug. Not used for core.
* @return string|\WP_Error Returns the language code if successfully downloaded, or a WP_Error object on failure.
*/
protected function download_language_pack( $download, $slug = null ) {
$translations = $this->get_all_languages( $slug );
$translation_to_load = null;
foreach ( $translations as $translation ) {
if ( $translation['language'] === $download ) {
$translation_to_load = $translation;
break;
}
}
if ( ! $translation_to_load ) {
return new \WP_Error( 'not_found', $slug ? "Language '{$download}' for '{$slug}' not available." : "Language '{$download}' not available." );
}
$translation = (object) $translation_to_load;
$translation->type = rtrim( $this->obj_type, 's' );
// Make sure caching in LanguagePackUpgrader works.
if ( ! isset( $translation->slug ) ) {
$translation->slug = $slug;
}
$upgrader = 'WP_CLI\\LanguagePackUpgrader';
/**
* @var \WP_CLI\LanguagePackUpgrader $upgrader_instance
*/
$upgrader_instance = Utils\get_upgrader( $upgrader );
// Incorrect docblock in WordPress core.
// @phpstan-ignore argument.type
$result = $upgrader_instance->upgrade( $translation, array( 'clear_update_cache' => false ) );
if ( is_wp_error( $result ) ) {
return $result;
}
if ( ! $result ) {
return new \WP_Error( 'not_installed', $slug ? "Could not install language '{$download}' for '{$slug}'." : "Could not install language '{$download}'." );
}
return $translation->language;
}
/**
* Return a list of installed languages.
*
* @param string $slug Optional. Plugin or theme slug. Defaults to 'default' for core.
*
* @return string[]
*/
protected function get_installed_languages( $slug = 'default' ) {
/**
* @var array<string, array<string, array<string, mixed>>> $available
*/
$available = wp_get_installed_translations( $this->obj_type );
// For plugins and themes, check if the text domain differs from the slug.
$text_domain = $slug;
if ( 'default' !== $slug ) {
if ( 'plugins' === $this->obj_type ) {
$plugins = get_plugins( '/' . $slug );
if ( ! empty( $plugins ) ) {
$plugin_data = array_shift( $plugins );
// Use the TextDomain header if available, otherwise fall back to slug.
if ( ! empty( $plugin_data['TextDomain'] ) ) {
$text_domain = $plugin_data['TextDomain'];
}
}
} elseif ( 'themes' === $this->obj_type ) {
$theme_data = wp_get_theme( $slug );
if ( $theme_data->exists() ) {
// Use the TextDomain property if available, otherwise fall back to slug.
$theme_text_domain = $theme_data->get( 'TextDomain' );
if ( ! empty( $theme_text_domain ) ) {
$text_domain = $theme_text_domain;
}
}
}
}
$available = ! empty( $available[ $text_domain ] ) ? array_keys( $available[ $text_domain ] ) : array();
$available[] = 'en_US';
return $available;
}
/**
* Return a list of all languages.
*
* @param string $slug Optional. Plugin or theme slug. Not used for core.
*
* @return array<array{language: string, english_name: string, native_name: string, updated: string}>
*/
protected function get_all_languages( $slug = null ) {
require_once ABSPATH . '/wp-admin/includes/translation-install.php';
require ABSPATH . WPINC . '/version.php'; // Include an unmodified $wp_version
$args = array(
// False positive, because it's defined in version.php
// @phpstan-ignore variable.undefined
'version' => $wp_version,
);
if ( $slug ) {
$args['slug'] = $slug;
if ( 'plugins' === $this->obj_type ) {
$plugins = get_plugins( '/' . $slug );
$plugin_data = array_shift( $plugins );
if ( isset( $plugin_data['Version'] ) ) {
$args['version'] = $plugin_data['Version'];
}
} elseif ( 'themes' === $this->obj_type ) {
$theme_data = wp_get_theme( $slug );
if ( isset( $theme_data['Version'] ) ) {
$args['version'] = $theme_data['Version'];
}
}
}
$response = translations_api( $this->obj_type, $args );
if ( is_wp_error( $response ) ) {
WP_CLI::error( $response );
}
$translations = ! empty( $response['translations'] ) ? $response['translations'] : array();
$en_us = array(
'language' => 'en_US',
'english_name' => 'English (United States)',
'native_name' => 'English (United States)',
'updated' => '',
);
$translations[] = $en_us;
uasort( $translations, array( $this, 'sort_translations_callback' ) );
return $translations;
}
/**
* Get Formatter object based on supplied parameters.
*
* @param array $assoc_args Parameters passed to command. Determines formatting.
* @return Formatter
*/
protected function get_formatter( &$assoc_args ) {
return new Formatter( $assoc_args, $this->obj_fields, $this->obj_type );
}
}