Language_Pack_Upgrader::bulk_upgrade()
最后更新于:2021-11-25 19:53:22
Language_Pack_Upgrader::bulk_upgrade( object[]$language_updates=array(), array$args=array())Bulk upgrade language packs.
参数
- $language_updates
-
(object[]) (Optional) Array of language packs to update. @see gc_get_translation_updates().
Default value: array()
- $args
-
(array) (Optional) Other arguments for upgrading multiple language packs.
-
‘clear_update_cache’
(bool) Whether to clear the update cache when done. Default true.
Default value: array()
-
‘clear_update_cache’
响应
(array|bool|GC_Error) Will return an array of results, or true if there are no updates, false or GC_Error for initial errors.
源文件
文件: gc-admin/includes/class-language-pack-upgrader.php
public function bulk_upgrade( $language_updates = array(), $args = array() ) {
global $gc_filesystem;
$defaults = array(
'clear_update_cache' => true,
);
$parsed_args = gc_parse_args( $args, $defaults );
$this->init();
$this->upgrade_strings();
if ( ! $language_updates ) {
$language_updates = gc_get_translation_updates();
}
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;
}
if ( 'upgrader_process_complete' === current_filter() ) {
$this->skin->feedback( 'starting_upgrade' );
}
// Remove any existing upgrade filters from the plugin/theme upgraders #GC29425 & #GC29230.
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( GC_CONTENT_DIR, GC_LANG_DIR ) );
if ( ! $res ) {
$this->skin->footer();
return false;
}
$results = array();
$this->update_count = count( $language_updates );
$this->update_current = 0;
/*
* The filesystem's mkdir() is not recursive. Make sure GC_LANG_DIR exists,
* as we then may need to create a /plugins or /themes directory inside of it.
*/
$remote_destination = $gc_filesystem->find_folder( GC_LANG_DIR );
if ( ! $gc_filesystem->exists( $remote_destination ) ) {
if ( ! $gc_filesystem->mkdir( $remote_destination, FS_CHMOD_DIR ) ) {
return new GC_Error( 'mkdir_failed_lang_dir', $this->strings['mkdir_failed'], $remote_destination );
}
}
$language_updates_results = array();
foreach ( $language_updates as $language_update ) {
$this->skin->language_update = $language_update;
$destination = GC_LANG_DIR;
if ( 'plugin' === $language_update->type ) {
$destination .= '/plugins';
} elseif ( 'theme' === $language_update->type ) {
$destination .= '/themes';
}
$this->update_current++;
$options = array(
'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(
'language_update_type' => $language_update->type,
'language_update' => $language_update,
),
);
$result = $this->run( $options );
$results[] = $this->result;
// Prevent credentials auth screen from displaying multiple times.
if ( false === $result ) {
break;
}
$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,
);
}
// 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', 'gc_version_check' );
remove_action( 'upgrader_process_complete', 'gc_update_plugins' );
remove_action( 'upgrader_process_complete', 'gc_update_themes' );
/** This action is documented in gc-admin/includes/class-gc-upgrader.php */
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 );
add_action( 'upgrader_process_complete', 'gc_version_check', 10, 0 );
add_action( 'upgrader_process_complete', 'gc_update_plugins', 10, 0 );
add_action( 'upgrader_process_complete', 'gc_update_themes', 10, 0 );
$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'] ) {
gc_clean_update_cache();
}
return $results;
}