diff -r c7c34916027a -r 177826044cd9 wp/wp-admin/includes/class-language-pack-upgrader.php --- a/wp/wp-admin/includes/class-language-pack-upgrader.php Mon Oct 14 18:06:33 2019 +0200 +++ b/wp/wp-admin/includes/class-language-pack-upgrader.php Mon Oct 14 18:28:13 2019 +0200 @@ -41,7 +41,6 @@ * Hooked to the {@see 'upgrader_process_complete'} action by default. * * @since 3.7.0 - * @static * * @param false|WP_Upgrader $upgrader Optional. WP_Upgrader instance or false. If `$upgrader` is * a Language_Pack_Upgrader instance, the method will bail to @@ -94,9 +93,11 @@ if ( $upgrader && $upgrader->skin instanceof Automatic_Upgrader_Skin ) { $skin = $upgrader->skin; } else { - $skin = new Language_Pack_Upgrader_Skin( array( - 'skip_header_footer' => true, - ) ); + $skin = new Language_Pack_Upgrader_Skin( + array( + 'skip_header_footer' => true, + ) + ); } $lp_upgrader = new Language_Pack_Upgrader( $skin ); @@ -110,13 +111,15 @@ */ public function upgrade_strings() { $this->strings['starting_upgrade'] = __( 'Some of your translations need updating. Sit tight for a few more seconds while we update them as well.' ); - $this->strings['up_to_date'] = __( 'The translations are up to date.' ); - $this->strings['no_package'] = __( 'Update package not available.' ); + $this->strings['up_to_date'] = __( 'The translations are up to date.' ); + $this->strings['no_package'] = __( 'Update package not available.' ); /* translators: %s: package URL */ $this->strings['downloading_package'] = sprintf( __( 'Downloading translation from %s…' ), '%s' ); - $this->strings['unpack_package'] = __( 'Unpacking the update…' ); - $this->strings['process_failed'] = __( 'Translation update failed.' ); - $this->strings['process_success'] = __( 'Translation updated successfully.' ); + $this->strings['unpack_package'] = __( 'Unpacking the update…' ); + $this->strings['process_failed'] = __( 'Translation update failed.' ); + $this->strings['process_success'] = __( 'Translation updated successfully.' ); + $this->strings['remove_old'] = __( 'Removing the old version of the translation…' ); + $this->strings['remove_old_failed'] = __( 'Could not remove the old translation.' ); } /** @@ -148,22 +151,23 @@ * * @since 3.7.0 * - * @global WP_Filesystem_Base $wp_filesystem Subclass + * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. * - * @param array $language_updates Optional. Language pack updates. Default empty array. - * @param array $args { - * Optional. Other arguments for upgrading multiple language packs. Default empty array + * @param object[] $language_updates Optional. Array of language packs to update. @see wp_get_translation_updates(). + * Default empty array. + * @param array $args { + * Other arguments for upgrading multiple language packs. Default empty array. * * @type bool $clear_update_cache Whether to clear the update cache when done. * Default true. * } * @return array|bool|WP_Error Will return an array of results, or true if there are no updates, - * false or WP_Error for initial errors. + * false or WP_Error for initial errors. */ public function bulk_upgrade( $language_updates = array(), $args = array() ) { global $wp_filesystem; - $defaults = array( + $defaults = array( 'clear_update_cache' => true, ); $parsed_args = wp_parse_args( $args, $defaults ); @@ -171,8 +175,9 @@ $this->init(); $this->upgrade_strings(); - if ( ! $language_updates ) + if ( ! $language_updates ) { $language_updates = wp_get_translation_updates(); + } if ( empty( $language_updates ) ) { $this->skin->header(); @@ -183,8 +188,9 @@ return true; } - if ( 'upgrader_process_complete' == current_filter() ) + if ( 'upgrader_process_complete' == current_filter() ) { $this->skin->feedback( 'starting_upgrade' ); + } // Remove any existing upgrade filters from the plugin/theme upgraders #WP29425 & #WP29230 remove_all_filters( 'upgrader_pre_install' ); @@ -205,7 +211,7 @@ $results = array(); - $this->update_count = count( $language_updates ); + $this->update_count = count( $language_updates ); $this->update_current = 0; /* @@ -213,9 +219,11 @@ * as we then may need to create a /plugins or /themes directory inside of it. */ $remote_destination = $wp_filesystem->find_folder( WP_LANG_DIR ); - if ( ! $wp_filesystem->exists( $remote_destination ) ) - if ( ! $wp_filesystem->mkdir( $remote_destination, FS_CHMOD_DIR ) ) + if ( ! $wp_filesystem->exists( $remote_destination ) ) { + if ( ! $wp_filesystem->mkdir( $remote_destination, FS_CHMOD_DIR ) ) { return new WP_Error( 'mkdir_failed_lang_dir', $this->strings['mkdir_failed'], $remote_destination ); + } + } $language_updates_results = array(); @@ -224,24 +232,25 @@ $this->skin->language_update = $language_update; $destination = WP_LANG_DIR; - if ( 'plugin' == $language_update->type ) + if ( 'plugin' == $language_update->type ) { $destination .= '/plugins'; - elseif ( 'theme' == $language_update->type ) + } elseif ( 'theme' == $language_update->type ) { $destination .= '/themes'; + } $this->update_current++; $options = array( - 'package' => $language_update->package, - 'destination' => $destination, - 'clear_destination' => false, + 'package' => $language_update->package, + 'destination' => $destination, + 'clear_destination' => true, 'abort_if_destination_exists' => false, // We expect the destination to exist. - 'clear_working' => true, - 'is_multi' => true, - 'hook_extra' => array( + 'clear_working' => true, + 'is_multi' => true, + 'hook_extra' => array( 'language_update_type' => $language_update->type, - 'language_update' => $language_update, - ) + 'language_update' => $language_update, + ), ); $result = $this->run( $options ); @@ -268,12 +277,16 @@ remove_action( 'upgrader_process_complete', 'wp_update_themes' ); /** This action is documented in wp-admin/includes/class-wp-upgrader.php */ - do_action( 'upgrader_process_complete', $this, array( - 'action' => 'update', - 'type' => 'translation', - 'bulk' => true, - 'translations' => $language_updates_results - ) ); + do_action( + 'upgrader_process_complete', + $this, + array( + 'action' => 'update', + 'type' => 'translation', + 'bulk' => true, + 'translations' => $language_updates_results, + ) + ); // Re-add upgrade hooks. add_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 ); @@ -311,8 +324,9 @@ public function check_package( $source, $remote_source ) { global $wp_filesystem; - if ( is_wp_error( $source ) ) + if ( is_wp_error( $source ) ) { return $source; + } // Check that the folder contains a valid language. $files = $wp_filesystem->dirlist( $remote_source ); @@ -320,16 +334,20 @@ // Check to see if a .po and .mo exist in the folder. $po = $mo = false; foreach ( (array) $files as $file => $filedata ) { - if ( '.po' == substr( $file, -3 ) ) + if ( '.po' == substr( $file, -3 ) ) { $po = true; - elseif ( '.mo' == substr( $file, -3 ) ) + } elseif ( '.mo' == substr( $file, -3 ) ) { $mo = true; + } } if ( ! $mo || ! $po ) { - return new WP_Error( 'incompatible_archive_pomo', $this->strings['incompatible_archive'], - /* translators: 1: .po 2: .mo */ - sprintf( __( 'The language pack is missing either the %1$s or %2$s files.' ), + return new WP_Error( + 'incompatible_archive_pomo', + $this->strings['incompatible_archive'], + sprintf( + /* translators: 1: .po, 2: .mo */ + __( 'The language pack is missing either the %1$s or %2$s files.' ), '.po', '.mo' ) @@ -354,17 +372,101 @@ case 'theme': $theme = wp_get_theme( $update->slug ); - if ( $theme->exists() ) + if ( $theme->exists() ) { return $theme->Get( 'Name' ); + } break; case 'plugin': $plugin_data = get_plugins( '/' . $update->slug ); $plugin_data = reset( $plugin_data ); - if ( $plugin_data ) + if ( $plugin_data ) { return $plugin_data['Name']; + } break; } return ''; } + /** + * Clears existing translations where this item is going to be installed into. + * + * @since 5.1.0 + * + * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. + * + * @param string $remote_destination The location on the remote filesystem to be cleared. + * @return bool|WP_Error True upon success, WP_Error on failure. + */ + public function clear_destination( $remote_destination ) { + global $wp_filesystem; + + $language_update = $this->skin->language_update; + $language_directory = WP_LANG_DIR . '/'; // Local path for use with glob(). + + if ( 'core' === $language_update->type ) { + $files = array( + $remote_destination . $language_update->language . '.po', + $remote_destination . $language_update->language . '.mo', + $remote_destination . 'admin-' . $language_update->language . '.po', + $remote_destination . 'admin-' . $language_update->language . '.mo', + $remote_destination . 'admin-network-' . $language_update->language . '.po', + $remote_destination . 'admin-network-' . $language_update->language . '.mo', + $remote_destination . 'continents-cities-' . $language_update->language . '.po', + $remote_destination . 'continents-cities-' . $language_update->language . '.mo', + ); + + $json_translation_files = glob( $language_directory . $language_update->language . '-*.json' ); + if ( $json_translation_files ) { + foreach ( $json_translation_files as $json_translation_file ) { + $files[] = str_replace( $language_directory, $remote_destination, $json_translation_file ); + } + } + } else { + $files = array( + $remote_destination . $language_update->slug . '-' . $language_update->language . '.po', + $remote_destination . $language_update->slug . '-' . $language_update->language . '.mo', + ); + + $language_directory = $language_directory . $language_update->type . 's/'; + $json_translation_files = glob( $language_directory . $language_update->slug . '-' . $language_update->language . '-*.json' ); + if ( $json_translation_files ) { + foreach ( $json_translation_files as $json_translation_file ) { + $files[] = str_replace( $language_directory, $remote_destination, $json_translation_file ); + } + } + } + + $files = array_filter( $files, array( $wp_filesystem, 'exists' ) ); + + // No files to delete. + if ( ! $files ) { + return true; + } + + // Check all files are writable before attempting to clear the destination. + $unwritable_files = array(); + + // Check writability. + foreach ( $files as $file ) { + if ( ! $wp_filesystem->is_writable( $file ) ) { + // Attempt to alter permissions to allow writes and try again. + $wp_filesystem->chmod( $file, FS_CHMOD_FILE ); + if ( ! $wp_filesystem->is_writable( $file ) ) { + $unwritable_files[] = $file; + } + } + } + + if ( ! empty( $unwritable_files ) ) { + return new WP_Error( 'files_not_writable', $this->strings['files_not_writable'], implode( ', ', $unwritable_files ) ); + } + + foreach ( $files as $file ) { + if ( ! $wp_filesystem->delete( $file ) ) { + return new WP_Error( 'remove_old_failed', $this->strings['remove_old_failed'] ); + } + } + + return true; + } }