| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Upgrade API: Language_Pack_Upgrader class 4 * 5 * @package WordPress 6 * @subpackage Upgrader 7 * @since 4.6.0 8 */ 9 10 /** 11 * Core class used for updating/installing language packs (translations) 12 * for plugins, themes, and core. 13 * 14 * @since 3.7.0 15 * @since 4.6.0 Moved to its own file from wp-admin/includes/class-wp-upgrader.php. 16 * 17 * @see WP_Upgrader 18 */ 19 class Language_Pack_Upgrader extends WP_Upgrader { 20 21 /** 22 * Result of the language pack upgrade. 23 * 24 * @since 3.7.0 25 * @var array|WP_Error|null $result 26 * @phpstan-var non-empty-array<string, mixed>|WP_Error|null 27 * @see WP_Upgrader::$result 28 */ 29 public $result; 30 31 /** 32 * Whether a bulk upgrade/installation is being performed. 33 * 34 * @since 3.7.0 35 * @var bool $bulk 36 */ 37 public $bulk = true; 38 39 /** 40 * Asynchronously upgrades language packs after other upgrades have been made. 41 * 42 * Hooked to the {@see 'upgrader_process_complete'} action by default. 43 * 44 * @since 3.7.0 45 * 46 * @param false|WP_Upgrader $upgrader Optional. WP_Upgrader instance or false. If `$upgrader` is 47 * a Language_Pack_Upgrader instance, the method will bail to 48 * avoid recursion. Otherwise unused. Default false. 49 */ 50 public static function async_upgrade( $upgrader = false ) { 51 // Avoid recursion. 52 if ( $upgrader && $upgrader instanceof Language_Pack_Upgrader ) { 53 return; 54 } 55 56 // Nothing to do? 57 $language_updates = wp_get_translation_updates(); 58 if ( ! $language_updates ) { 59 return; 60 } 61 62 /* 63 * Avoid messing with VCS installations, at least for now. 64 * Noted: this is not the ideal way to accomplish this. 65 */ 66 $check_vcs = new WP_Automatic_Updater(); 67 if ( $check_vcs->is_vcs_checkout( WP_CONTENT_DIR ) ) { 68 return; 69 } 70 71 $deferred_updates = wp_get_deferred_translation_updates( $language_updates ); 72 73 foreach ( $language_updates as $key => $language_update ) { 74 $update = ! empty( $language_update->autoupdate ); 75 76 if ( $update && wp_is_translation_update_deferred( $language_update, $deferred_updates ) ) { 77 $update = false; 78 } 79 80 /** 81 * Filters whether to asynchronously update translation for core, a plugin, or a theme. 82 * 83 * @since 4.0.0 84 * 85 * @param bool $update Whether to update. 86 * @param object $language_update The update offer. 87 */ 88 $update = apply_filters( 'async_update_translation', $update, $language_update ); 89 90 if ( ! $update ) { 91 unset( $language_updates[ $key ] ); 92 } 93 } 94 95 if ( empty( $language_updates ) ) { 96 return; 97 } 98 99 // Re-use the automatic upgrader skin if the parent upgrader is using it. 100 if ( $upgrader && $upgrader->skin instanceof Automatic_Upgrader_Skin ) { 101 $skin = $upgrader->skin; 102 } else { 103 $skin = new Language_Pack_Upgrader_Skin( 104 array( 105 'skip_header_footer' => true, 106 ) 107 ); 108 } 109 110 $lp_upgrader = new Language_Pack_Upgrader( $skin ); 111 $lp_upgrader->bulk_upgrade( $language_updates ); 112 } 113 114 /** 115 * Initializes the upgrade strings. 116 * 117 * @since 3.7.0 118 */ 119 public function upgrade_strings() { 120 $this->strings['starting_upgrade'] = __( 'Some of your translations need updating. Sit tight for a few more seconds while they are updated as well.' ); 121 $this->strings['up_to_date'] = __( 'Your translations are all up to date.' ); 122 $this->strings['no_package'] = __( 'Update package not available.' ); 123 /* translators: %s: Package URL. */ 124 $this->strings['downloading_package'] = sprintf( __( 'Downloading translation from %s…' ), '<span class="code pre">%s</span>' ); 125 $this->strings['unpack_package'] = __( 'Unpacking the update…' ); 126 $this->strings['process_failed'] = __( 'Translation update failed.' ); 127 $this->strings['process_success'] = __( 'Translation updated successfully.' ); 128 $this->strings['remove_old'] = __( 'Removing the old version of the translation…' ); 129 $this->strings['remove_old_failed'] = __( 'Could not remove the old translation.' ); 130 } 131 132 /** 133 * Upgrades a language pack. 134 * 135 * @since 3.7.0 136 * 137 * @param object|false $update Optional. The language pack update offer, or false to upgrade all 138 * available language packs. Default false. 139 * @param array $args Optional. Other optional arguments, see 140 * Language_Pack_Upgrader::bulk_upgrade(). Default empty array. 141 * @return array|bool|WP_Error|null The result of the upgrade, or a WP_Error object instead. Null if the 142 * upgrade failed before the package was installed, for example when 143 * the package could not be downloaded. 144 */ 145 public function upgrade( $update = false, $args = array() ) { 146 if ( $update ) { 147 $update = array( $update ); 148 } 149 150 $results = $this->bulk_upgrade( $update, $args ); 151 152 if ( ! is_array( $results ) ) { 153 return $results; 154 } 155 156 return $results[0]; 157 } 158 159 /** 160 * Upgrades several language packs at once. 161 * 162 * @since 3.7.0 163 * 164 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. 165 * 166 * @param object[] $language_updates Optional. Array of language packs to update. See {@see wp_get_translation_updates()}. 167 * Default empty array. 168 * @param array $args { 169 * Other arguments for upgrading multiple language packs. Default empty array. 170 * 171 * @type bool $clear_update_cache Whether to clear the update cache when done. 172 * Default true. 173 * } 174 * @return array|bool|WP_Error Will return an array of results, or true if there are no updates, 175 * false or WP_Error for initial errors. 176 */ 177 public function bulk_upgrade( $language_updates = array(), $args = array() ) { 178 global $wp_filesystem; 179 180 $defaults = array( 181 'clear_update_cache' => true, 182 ); 183 $parsed_args = wp_parse_args( $args, $defaults ); 184 185 $this->init(); 186 $this->upgrade_strings(); 187 188 if ( ! $language_updates ) { 189 $language_updates = wp_get_translation_updates(); 190 } 191 192 if ( empty( $language_updates ) ) { 193 $this->skin->header(); 194 $this->skin->set_result( true ); 195 $this->skin->feedback( 'up_to_date' ); 196 $this->skin->bulk_footer(); 197 $this->skin->footer(); 198 return true; 199 } 200 201 if ( 'upgrader_process_complete' === current_filter() ) { 202 $this->skin->feedback( 'starting_upgrade' ); 203 } 204 205 // Remove any existing upgrade filters from the plugin/theme upgraders #WP29425 & #WP29230. 206 remove_all_filters( 'upgrader_pre_install' ); 207 remove_all_filters( 'upgrader_clear_destination' ); 208 remove_all_filters( 'upgrader_post_install' ); 209 remove_all_filters( 'upgrader_source_selection' ); 210 211 add_filter( 'upgrader_source_selection', array( $this, 'check_package' ), 10, 2 ); 212 213 $this->skin->header(); 214 215 // Connect to the filesystem first. 216 $res = $this->fs_connect( array( WP_CONTENT_DIR, WP_LANG_DIR ) ); 217 if ( ! $res ) { 218 $this->skin->footer(); 219 return false; 220 } 221 222 $results = array(); 223 224 $this->update_count = count( $language_updates ); 225 $this->update_current = 0; 226 227 /* 228 * The filesystem's mkdir() is not recursive. Make sure WP_LANG_DIR exists, 229 * as we then may need to create a /plugins or /themes directory inside of it. 230 */ 231 $remote_destination = $wp_filesystem->find_folder( WP_LANG_DIR ); 232 if ( ! $wp_filesystem->exists( $remote_destination ) ) { 233 if ( ! $wp_filesystem->mkdir( $remote_destination, FS_CHMOD_DIR ) ) { 234 return new WP_Error( 'mkdir_failed_lang_dir', $this->strings['mkdir_failed'], $remote_destination ); 235 } 236 } 237 238 $language_updates_results = array(); 239 $successful_update_ids = array(); 240 241 foreach ( $language_updates as $language_update ) { 242 243 $this->skin->language_update = $language_update; 244 245 $destination = WP_LANG_DIR; 246 if ( 'plugin' === $language_update->type ) { 247 $destination .= '/plugins'; 248 } elseif ( 'theme' === $language_update->type ) { 249 $destination .= '/themes'; 250 } 251 252 ++$this->update_current; 253 254 $options = array( 255 'package' => $language_update->package, 256 'destination' => $destination, 257 'clear_destination' => true, 258 'abort_if_destination_exists' => false, // We expect the destination to exist. 259 'clear_working' => true, 260 'is_multi' => true, 261 'hook_extra' => array( 262 'language_update_type' => $language_update->type, 263 'language_update' => $language_update, 264 ), 265 ); 266 267 $result = $this->run( $options ); 268 269 $results[] = $this->result; 270 271 // Prevent credentials auth screen from displaying multiple times. 272 if ( false === $result ) { 273 break; 274 } 275 276 if ( is_array( $result ) ) { 277 $successful_update_ids[ wp_get_translation_update_id( $language_update ) ] = true; 278 } 279 280 $language_updates_results[] = array( 281 'language' => $language_update->language, 282 'type' => $language_update->type, 283 'slug' => $language_update->slug ?? 'default', 284 'version' => $language_update->version, 285 ); 286 } 287 288 if ( $successful_update_ids ) { 289 $deferred_updates = wp_get_deferred_translation_updates(); 290 $remaining = array_diff_key( $deferred_updates, $successful_update_ids ); 291 292 if ( $remaining !== $deferred_updates ) { 293 wp_set_deferred_translation_updates( $remaining ); 294 } 295 } 296 297 // Remove upgrade hooks which are not required for translation updates. 298 remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 ); 299 remove_action( 'upgrader_process_complete', 'wp_version_check' ); 300 remove_action( 'upgrader_process_complete', 'wp_update_plugins' ); 301 remove_action( 'upgrader_process_complete', 'wp_update_themes' ); 302 303 /** This action is documented in wp-admin/includes/class-wp-upgrader.php */ 304 do_action( 305 'upgrader_process_complete', 306 $this, 307 array( 308 'action' => 'update', 309 'type' => 'translation', 310 'bulk' => true, 311 'translations' => $language_updates_results, 312 ) 313 ); 314 315 // Re-add upgrade hooks. 316 add_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 ); 317 add_action( 'upgrader_process_complete', 'wp_version_check', 10, 0 ); 318 add_action( 'upgrader_process_complete', 'wp_update_plugins', 10, 0 ); 319 add_action( 'upgrader_process_complete', 'wp_update_themes', 10, 0 ); 320 321 $this->skin->bulk_footer(); 322 323 $this->skin->footer(); 324 325 // Clean up our hooks, in case something else does an upgrade on this connection. 326 remove_filter( 'upgrader_source_selection', array( $this, 'check_package' ) ); 327 328 if ( $parsed_args['clear_update_cache'] ) { 329 wp_clean_update_cache(); 330 } 331 332 return $results; 333 } 334 335 /** 336 * Checks that the package source contains .mo and .po files. 337 * 338 * Hooked to the {@see 'upgrader_source_selection'} filter by 339 * Language_Pack_Upgrader::bulk_upgrade(). 340 * 341 * @since 3.7.0 342 * 343 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. 344 * 345 * @param string|WP_Error $source The path to the downloaded package source. 346 * @param string $remote_source Remote file source location. 347 * @return string|WP_Error The source as passed, or a WP_Error object on failure. 348 */ 349 public function check_package( $source, $remote_source ) { 350 global $wp_filesystem; 351 352 if ( is_wp_error( $source ) ) { 353 return $source; 354 } 355 356 // Check that the folder contains a valid language. 357 $files = $wp_filesystem->dirlist( $remote_source ); 358 359 // Check to see if the expected files exist in the folder. 360 $po = false; 361 $mo = false; 362 $php = false; 363 foreach ( (array) $files as $file => $filedata ) { 364 if ( str_ends_with( $file, '.po' ) ) { 365 $po = true; 366 } elseif ( str_ends_with( $file, '.mo' ) ) { 367 $mo = true; 368 } elseif ( str_ends_with( $file, '.l10n.php' ) ) { 369 $php = true; 370 } 371 } 372 373 if ( $php ) { 374 return $source; 375 } 376 377 if ( ! $mo || ! $po ) { 378 return new WP_Error( 379 'incompatible_archive_pomo', 380 $this->strings['incompatible_archive'], 381 sprintf( 382 /* translators: 1: .po, 2: .mo, 3: .l10n.php */ 383 __( 'The language pack is missing either the %1$s, %2$s, or %3$s files.' ), 384 '<code>.po</code>', 385 '<code>.mo</code>', 386 '<code>.l10n.php</code>' 387 ) 388 ); 389 } 390 391 return $source; 392 } 393 394 /** 395 * Gets the name of an item being updated. 396 * 397 * @since 3.7.0 398 * 399 * @param object $update The data for an update. 400 * @return string The name of the item being updated. 401 */ 402 public function get_name_for_update( $update ) { 403 switch ( $update->type ) { 404 case 'core': 405 return 'WordPress'; // Not translated. 406 407 case 'theme': 408 $theme = wp_get_theme( $update->slug ); 409 if ( $theme->exists() ) { 410 return $theme->get( 'Name' ); 411 } 412 break; 413 case 'plugin': 414 $plugin_data = get_plugins( '/' . $update->slug ); 415 $plugin_data = reset( $plugin_data ); 416 if ( $plugin_data ) { 417 return $plugin_data['Name']; 418 } 419 break; 420 } 421 return ''; 422 } 423 424 /** 425 * Clears existing translations where this item is going to be installed into. 426 * 427 * @since 5.1.0 428 * 429 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. 430 * 431 * @param string $remote_destination The location on the remote filesystem to be cleared. 432 * @return bool|WP_Error True upon success, WP_Error on failure. 433 */ 434 public function clear_destination( $remote_destination ) { 435 global $wp_filesystem; 436 437 $language_update = $this->skin->language_update; 438 $language_directory = WP_LANG_DIR . '/'; // Local path for use with glob(). 439 440 if ( 'core' === $language_update->type ) { 441 $files = array( 442 $remote_destination . $language_update->language . '.po', 443 $remote_destination . $language_update->language . '.mo', 444 $remote_destination . $language_update->language . '.l10n.php', 445 $remote_destination . 'admin-' . $language_update->language . '.po', 446 $remote_destination . 'admin-' . $language_update->language . '.mo', 447 $remote_destination . 'admin-' . $language_update->language . '.l10n.php', 448 $remote_destination . 'admin-network-' . $language_update->language . '.po', 449 $remote_destination . 'admin-network-' . $language_update->language . '.mo', 450 $remote_destination . 'admin-network-' . $language_update->language . '.l10n.php', 451 $remote_destination . 'continents-cities-' . $language_update->language . '.po', 452 $remote_destination . 'continents-cities-' . $language_update->language . '.mo', 453 $remote_destination . 'continents-cities-' . $language_update->language . '.l10n.php', 454 ); 455 456 $json_translation_files = glob( $language_directory . $language_update->language . '-*.json' ); 457 if ( $json_translation_files ) { 458 foreach ( $json_translation_files as $json_translation_file ) { 459 $files[] = str_replace( $language_directory, $remote_destination, $json_translation_file ); 460 } 461 } 462 } else { 463 $files = array( 464 $remote_destination . $language_update->slug . '-' . $language_update->language . '.po', 465 $remote_destination . $language_update->slug . '-' . $language_update->language . '.mo', 466 $remote_destination . $language_update->slug . '-' . $language_update->language . '.l10n.php', 467 ); 468 469 $language_directory = $language_directory . $language_update->type . 's/'; 470 $json_translation_files = glob( $language_directory . $language_update->slug . '-' . $language_update->language . '-*.json' ); 471 if ( $json_translation_files ) { 472 foreach ( $json_translation_files as $json_translation_file ) { 473 $files[] = str_replace( $language_directory, $remote_destination, $json_translation_file ); 474 } 475 } 476 } 477 478 $files = array_filter( $files, array( $wp_filesystem, 'exists' ) ); 479 480 // No files to delete. 481 if ( ! $files ) { 482 return true; 483 } 484 485 // Check all files are writable before attempting to clear the destination. 486 $unwritable_files = array(); 487 488 // Check writability. 489 foreach ( $files as $file ) { 490 if ( ! $wp_filesystem->is_writable( $file ) ) { 491 // Attempt to alter permissions to allow writes and try again. 492 $wp_filesystem->chmod( $file, FS_CHMOD_FILE ); 493 if ( ! $wp_filesystem->is_writable( $file ) ) { 494 $unwritable_files[] = $file; 495 } 496 } 497 } 498 499 if ( ! empty( $unwritable_files ) ) { 500 return new WP_Error( 'files_not_writable', $this->strings['files_not_writable'], implode( ', ', $unwritable_files ) ); 501 } 502 503 foreach ( $files as $file ) { 504 if ( ! $wp_filesystem->delete( $file ) ) { 505 return new WP_Error( 'remove_old_failed', $this->strings['remove_old_failed'] ); 506 } 507 } 508 509 return true; 510 } 511 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Thu Nov 19 08:20:01 2026 | Cross-referenced by PHPXref |