2016-05-10 07:45:42 -04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-05-10 07:52:28 -04:00
|
|
|
* Upgrade API: Language_Pack_Upgrader class
|
2016-05-10 07:45:42 -04:00
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @subpackage Upgrader
|
2016-05-10 07:52:28 -04:00
|
|
|
* @since 4.6.0
|
2016-05-10 07:45:42 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Core class used for updating/installing language packs (translations)
|
|
|
|
* for plugins, themes, and core.
|
|
|
|
*
|
|
|
|
* @since 3.7.0
|
2016-05-13 16:59:27 -04:00
|
|
|
* @since 4.6.0 Moved to its own file from wp-admin/includes/class-wp-upgrader.php.
|
2016-05-10 07:45:42 -04:00
|
|
|
*
|
|
|
|
* @see WP_Upgrader
|
|
|
|
*/
|
|
|
|
class Language_Pack_Upgrader extends WP_Upgrader {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Result of the language pack upgrade.
|
|
|
|
*
|
|
|
|
* @since 3.7.0
|
|
|
|
* @var array|WP_Error $result
|
|
|
|
* @see WP_Upgrader::$result
|
|
|
|
*/
|
|
|
|
public $result;
|
|
|
|
|
|
|
|
/**
|
2017-08-22 07:52:48 -04:00
|
|
|
* Whether a bulk upgrade/installation is being performed.
|
2016-05-10 07:45:42 -04:00
|
|
|
*
|
|
|
|
* @since 3.7.0
|
|
|
|
* @var bool $bulk
|
|
|
|
*/
|
|
|
|
public $bulk = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronously upgrades language packs after other upgrades have been made.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* avoid recursion. Otherwise unused. Default false.
|
|
|
|
*/
|
|
|
|
public static function async_upgrade( $upgrader = false ) {
|
|
|
|
// Avoid recursion.
|
|
|
|
if ( $upgrader && $upgrader instanceof Language_Pack_Upgrader ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to do?
|
|
|
|
$language_updates = wp_get_translation_updates();
|
|
|
|
if ( ! $language_updates ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-08-22 07:52:48 -04:00
|
|
|
* Avoid messing with VCS installations, at least for now.
|
2016-05-10 07:45:42 -04:00
|
|
|
* Noted: this is not the ideal way to accomplish this.
|
|
|
|
*/
|
|
|
|
$check_vcs = new WP_Automatic_Updater;
|
|
|
|
if ( $check_vcs->is_vcs_checkout( WP_CONTENT_DIR ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $language_updates as $key => $language_update ) {
|
|
|
|
$update = ! empty( $language_update->autoupdate );
|
|
|
|
|
|
|
|
/**
|
2016-05-22 14:01:30 -04:00
|
|
|
* Filters whether to asynchronously update translation for core, a plugin, or a theme.
|
2016-05-10 07:45:42 -04:00
|
|
|
*
|
|
|
|
* @since 4.0.0
|
|
|
|
*
|
|
|
|
* @param bool $update Whether to update.
|
|
|
|
* @param object $language_update The update offer.
|
|
|
|
*/
|
|
|
|
$update = apply_filters( 'async_update_translation', $update, $language_update );
|
|
|
|
|
|
|
|
if ( ! $update ) {
|
|
|
|
unset( $language_updates[ $key ] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( empty( $language_updates ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-use the automatic upgrader skin if the parent upgrader is using it.
|
|
|
|
if ( $upgrader && $upgrader->skin instanceof Automatic_Upgrader_Skin ) {
|
|
|
|
$skin = $upgrader->skin;
|
|
|
|
} else {
|
2017-11-30 18:11:00 -05:00
|
|
|
$skin = new Language_Pack_Upgrader_Skin(
|
|
|
|
array(
|
|
|
|
'skip_header_footer' => true,
|
|
|
|
)
|
|
|
|
);
|
2016-05-10 07:45:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$lp_upgrader = new Language_Pack_Upgrader( $skin );
|
|
|
|
$lp_upgrader->bulk_upgrade( $language_updates );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the upgrade strings.
|
|
|
|
*
|
|
|
|
* @since 3.7.0
|
|
|
|
*/
|
|
|
|
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.' );
|
2017-11-30 18:11:00 -05:00
|
|
|
$this->strings['up_to_date'] = __( 'The translations are up to date.' );
|
|
|
|
$this->strings['no_package'] = __( 'Update package not available.' );
|
2017-10-18 13:15:47 -04:00
|
|
|
/* translators: %s: package URL */
|
|
|
|
$this->strings['downloading_package'] = sprintf( __( 'Downloading translation from %s…' ), '<span class="code">%s</span>' );
|
2017-11-30 18:11:00 -05:00
|
|
|
$this->strings['unpack_package'] = __( 'Unpacking the update…' );
|
|
|
|
$this->strings['process_failed'] = __( 'Translation update failed.' );
|
|
|
|
$this->strings['process_success'] = __( 'Translation updated successfully.' );
|
2016-05-10 07:45:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Upgrade a language pack.
|
|
|
|
*
|
|
|
|
* @since 3.7.0
|
|
|
|
*
|
|
|
|
* @param string|false $update Optional. Whether an update offer is available. Default false.
|
|
|
|
* @param array $args Optional. Other optional arguments, see
|
|
|
|
* Language_Pack_Upgrader::bulk_upgrade(). Default empty array.
|
|
|
|
* @return array|bool|WP_Error The result of the upgrade, or a WP_Error object instead.
|
|
|
|
*/
|
|
|
|
public function upgrade( $update = false, $args = array() ) {
|
|
|
|
if ( $update ) {
|
|
|
|
$update = array( $update );
|
|
|
|
}
|
|
|
|
|
|
|
|
$results = $this->bulk_upgrade( $update, $args );
|
|
|
|
|
|
|
|
if ( ! is_array( $results ) ) {
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $results[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bulk upgrade language packs.
|
|
|
|
*
|
|
|
|
* @since 3.7.0
|
|
|
|
*
|
|
|
|
* @global WP_Filesystem_Base $wp_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
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
public function bulk_upgrade( $language_updates = array(), $args = array() ) {
|
|
|
|
global $wp_filesystem;
|
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
$defaults = array(
|
2016-05-10 07:45:42 -04:00
|
|
|
'clear_update_cache' => true,
|
|
|
|
);
|
|
|
|
$parsed_args = wp_parse_args( $args, $defaults );
|
|
|
|
|
|
|
|
$this->init();
|
|
|
|
$this->upgrade_strings();
|
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( ! $language_updates ) {
|
2016-05-10 07:45:42 -04:00
|
|
|
$language_updates = wp_get_translation_updates();
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2016-05-10 07:45:42 -04:00
|
|
|
|
|
|
|
if ( empty( $language_updates ) ) {
|
|
|
|
$this->skin->header();
|
|
|
|
$this->skin->set_result( true );
|
|
|
|
$this->skin->feedback( 'up_to_date' );
|
|
|
|
$this->skin->bulk_footer();
|
|
|
|
$this->skin->footer();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( 'upgrader_process_complete' == current_filter() ) {
|
2016-05-10 07:45:42 -04:00
|
|
|
$this->skin->feedback( 'starting_upgrade' );
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2016-05-10 07:45:42 -04:00
|
|
|
|
|
|
|
// Remove any existing upgrade filters from the plugin/theme upgraders #WP29425 & #WP29230
|
|
|
|
remove_all_filters( 'upgrader_pre_install' );
|
|
|
|
remove_all_filters( 'upgrader_clear_destination' );
|
|
|
|
remove_all_filters( 'upgrader_post_install' );
|
|
|
|
remove_all_filters( 'upgrader_source_selection' );
|
|
|
|
|
|
|
|
add_filter( 'upgrader_source_selection', array( $this, 'check_package' ), 10, 2 );
|
|
|
|
|
|
|
|
$this->skin->header();
|
|
|
|
|
|
|
|
// Connect to the Filesystem first.
|
|
|
|
$res = $this->fs_connect( array( WP_CONTENT_DIR, WP_LANG_DIR ) );
|
|
|
|
if ( ! $res ) {
|
|
|
|
$this->skin->footer();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$results = array();
|
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
$this->update_count = count( $language_updates );
|
2016-05-10 07:45:42 -04:00
|
|
|
$this->update_current = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The filesystem's mkdir() is not recursive. Make sure WP_LANG_DIR exists,
|
|
|
|
* as we then may need to create a /plugins or /themes directory inside of it.
|
|
|
|
*/
|
|
|
|
$remote_destination = $wp_filesystem->find_folder( WP_LANG_DIR );
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( ! $wp_filesystem->exists( $remote_destination ) ) {
|
|
|
|
if ( ! $wp_filesystem->mkdir( $remote_destination, FS_CHMOD_DIR ) ) {
|
2016-05-10 07:45:42 -04:00
|
|
|
return new WP_Error( 'mkdir_failed_lang_dir', $this->strings['mkdir_failed'], $remote_destination );
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
|
|
|
}
|
2016-05-10 07:45:42 -04:00
|
|
|
|
2016-06-11 13:44:28 -04:00
|
|
|
$language_updates_results = array();
|
|
|
|
|
2016-05-10 07:45:42 -04:00
|
|
|
foreach ( $language_updates as $language_update ) {
|
|
|
|
|
|
|
|
$this->skin->language_update = $language_update;
|
|
|
|
|
|
|
|
$destination = WP_LANG_DIR;
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( 'plugin' == $language_update->type ) {
|
2016-05-10 07:45:42 -04:00
|
|
|
$destination .= '/plugins';
|
2017-11-30 18:11:00 -05:00
|
|
|
} elseif ( 'theme' == $language_update->type ) {
|
2016-05-10 07:45:42 -04:00
|
|
|
$destination .= '/themes';
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2016-05-10 07:45:42 -04:00
|
|
|
|
|
|
|
$this->update_current++;
|
|
|
|
|
|
|
|
$options = array(
|
2017-11-30 18:11:00 -05:00
|
|
|
'package' => $language_update->package,
|
|
|
|
'destination' => $destination,
|
|
|
|
'clear_destination' => false,
|
2016-05-10 07:45:42 -04:00
|
|
|
'abort_if_destination_exists' => false, // We expect the destination to exist.
|
2017-11-30 18:11:00 -05:00
|
|
|
'clear_working' => true,
|
|
|
|
'is_multi' => true,
|
|
|
|
'hook_extra' => array(
|
2016-05-10 07:45:42 -04:00
|
|
|
'language_update_type' => $language_update->type,
|
2017-11-30 18:11:00 -05:00
|
|
|
'language_update' => $language_update,
|
|
|
|
),
|
2016-05-10 07:45:42 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
$result = $this->run( $options );
|
|
|
|
|
|
|
|
$results[] = $this->result;
|
|
|
|
|
|
|
|
// Prevent credentials auth screen from displaying multiple times.
|
2016-06-11 13:44:28 -04:00
|
|
|
if ( false === $result ) {
|
2016-05-10 07:45:42 -04:00
|
|
|
break;
|
2016-06-11 13:44:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$language_updates_results[] = array(
|
|
|
|
'language' => $language_update->language,
|
|
|
|
'type' => $language_update->type,
|
|
|
|
'slug' => isset( $language_update->slug ) ? $language_update->slug : 'default',
|
|
|
|
'version' => $language_update->version,
|
|
|
|
);
|
2016-05-10 07:45:42 -04:00
|
|
|
}
|
|
|
|
|
2016-06-11 13:44:28 -04:00
|
|
|
// Remove upgrade hooks which are not required for translation updates.
|
|
|
|
remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 );
|
|
|
|
remove_action( 'upgrader_process_complete', 'wp_version_check' );
|
|
|
|
remove_action( 'upgrader_process_complete', 'wp_update_plugins' );
|
|
|
|
remove_action( 'upgrader_process_complete', 'wp_update_themes' );
|
|
|
|
|
|
|
|
/** This action is documented in wp-admin/includes/class-wp-upgrader.php */
|
2017-11-30 18:11:00 -05:00
|
|
|
do_action(
|
|
|
|
'upgrader_process_complete', $this, array(
|
|
|
|
'action' => 'update',
|
|
|
|
'type' => 'translation',
|
|
|
|
'bulk' => true,
|
|
|
|
'translations' => $language_updates_results,
|
|
|
|
)
|
|
|
|
);
|
2016-06-11 13:44:28 -04:00
|
|
|
|
|
|
|
// Re-add upgrade hooks.
|
|
|
|
add_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 );
|
2016-08-28 06:04:30 -04:00
|
|
|
add_action( 'upgrader_process_complete', 'wp_version_check', 10, 0 );
|
|
|
|
add_action( 'upgrader_process_complete', 'wp_update_plugins', 10, 0 );
|
|
|
|
add_action( 'upgrader_process_complete', 'wp_update_themes', 10, 0 );
|
2016-06-11 13:44:28 -04:00
|
|
|
|
2016-05-10 07:45:42 -04:00
|
|
|
$this->skin->bulk_footer();
|
|
|
|
|
|
|
|
$this->skin->footer();
|
|
|
|
|
|
|
|
// Clean up our hooks, in case something else does an upgrade on this connection.
|
|
|
|
remove_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
|
|
|
|
|
|
|
|
if ( $parsed_args['clear_update_cache'] ) {
|
|
|
|
wp_clean_update_cache();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check the package source to make sure there are .mo and .po files.
|
|
|
|
*
|
|
|
|
* Hooked to the {@see 'upgrader_source_selection'} filter by
|
|
|
|
* Language_Pack_Upgrader::bulk_upgrade().
|
|
|
|
*
|
|
|
|
* @since 3.7.0
|
|
|
|
*
|
|
|
|
* @global WP_Filesystem_Base $wp_filesystem Subclass
|
|
|
|
*
|
|
|
|
* @param string|WP_Error $source
|
|
|
|
* @param string $remote_source
|
|
|
|
*/
|
|
|
|
public function check_package( $source, $remote_source ) {
|
|
|
|
global $wp_filesystem;
|
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( is_wp_error( $source ) ) {
|
2016-05-10 07:45:42 -04:00
|
|
|
return $source;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2016-05-10 07:45:42 -04:00
|
|
|
|
|
|
|
// Check that the folder contains a valid language.
|
|
|
|
$files = $wp_filesystem->dirlist( $remote_source );
|
|
|
|
|
|
|
|
// Check to see if a .po and .mo exist in the folder.
|
|
|
|
$po = $mo = false;
|
|
|
|
foreach ( (array) $files as $file => $filedata ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( '.po' == substr( $file, -3 ) ) {
|
2016-05-10 07:45:42 -04:00
|
|
|
$po = true;
|
2017-11-30 18:11:00 -05:00
|
|
|
} elseif ( '.mo' == substr( $file, -3 ) ) {
|
2016-05-10 07:45:42 -04:00
|
|
|
$mo = true;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2016-05-10 07:45:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $mo || ! $po ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
return new WP_Error(
|
|
|
|
'incompatible_archive_pomo', $this->strings['incompatible_archive'],
|
2016-05-10 07:45:42 -04:00
|
|
|
/* translators: 1: .po 2: .mo */
|
2017-11-30 18:11:00 -05:00
|
|
|
sprintf(
|
|
|
|
__( 'The language pack is missing either the %1$s or %2$s files.' ),
|
2016-05-10 07:45:42 -04:00
|
|
|
'<code>.po</code>',
|
|
|
|
'<code>.mo</code>'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $source;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the name of an item being updated.
|
|
|
|
*
|
|
|
|
* @since 3.7.0
|
|
|
|
*
|
|
|
|
* @param object $update The data for an update.
|
|
|
|
* @return string The name of the item being updated.
|
|
|
|
*/
|
|
|
|
public function get_name_for_update( $update ) {
|
|
|
|
switch ( $update->type ) {
|
|
|
|
case 'core':
|
|
|
|
return 'WordPress'; // Not translated
|
|
|
|
|
|
|
|
case 'theme':
|
|
|
|
$theme = wp_get_theme( $update->slug );
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( $theme->exists() ) {
|
2016-05-10 07:45:42 -04:00
|
|
|
return $theme->Get( 'Name' );
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2016-05-10 07:45:42 -04:00
|
|
|
break;
|
|
|
|
case 'plugin':
|
|
|
|
$plugin_data = get_plugins( '/' . $update->slug );
|
|
|
|
$plugin_data = reset( $plugin_data );
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( $plugin_data ) {
|
2016-05-10 07:45:42 -04:00
|
|
|
return $plugin_data['Name'];
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2016-05-10 07:45:42 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|