Skip to content

Commit 99b0144

Browse files
committed
Upgrade/Install: Show and allow to select translation updates on the Updates screen.
This changeset introduces a way to see each available translation update on the Updates screen, and allows users to update them selectively. With this change, translation updates management is more consistent with how plugin and theme updates are managed. Developed in #11610. Props stk_jj, Presskopp, jyria, tfrommen, audrasjb, ozgursar, im3dabasia1. Fixes #42281. git-svn-id: https://develop.svn.wordpress.org/trunk@65226 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 6e85a75 commit 99b0144

8 files changed

Lines changed: 1439 additions & 8 deletions

File tree

‎src/wp-admin/includes/class-language-pack-upgrader.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,15 @@ public static function async_upgrade( $upgrader = false ) {
6868
return;
6969
}
7070

71+
$deferred_updates = wp_get_deferred_translation_updates( $language_updates );
72+
7173
foreach ( $language_updates as $key => $language_update ) {
7274
$update = ! empty( $language_update->autoupdate );
7375

76+
if ( $update && wp_is_translation_update_deferred( $language_update, $deferred_updates ) ) {
77+
$update = false;
78+
}
79+
7480
/**
7581
* Filters whether to asynchronously update translation for core, a plugin, or a theme.
7682
*
@@ -230,6 +236,7 @@ public function bulk_upgrade( $language_updates = array(), $args = array() ) {
230236
}
231237

232238
$language_updates_results = array();
239+
$successful_update_ids = array();
233240

234241
foreach ( $language_updates as $language_update ) {
235242

@@ -266,6 +273,10 @@ public function bulk_upgrade( $language_updates = array(), $args = array() ) {
266273
break;
267274
}
268275

276+
if ( is_array( $result ) ) {
277+
$successful_update_ids[ wp_get_translation_update_id( $language_update ) ] = true;
278+
}
279+
269280
$language_updates_results[] = array(
270281
'language' => $language_update->language,
271282
'type' => $language_update->type,
@@ -274,6 +285,15 @@ public function bulk_upgrade( $language_updates = array(), $args = array() ) {
274285
);
275286
}
276287

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+
277297
// Remove upgrade hooks which are not required for translation updates.
278298
remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 );
279299
remove_action( 'upgrader_process_complete', 'wp_version_check' );

‎src/wp-admin/includes/class-wp-automatic-updater.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,11 @@ public function should_update( $type, $item, $context ) {
312312
}
313313
} else {
314314
$update = ! empty( $item->autoupdate );
315+
316+
if ( $update && wp_is_translation_update_deferred( $item ) ) {
317+
$update = false;
318+
}
319+
315320
}
316321

317322
// If the `disable_autoupdate` flag is set, override any user-choice, but allow filters.

‎src/wp-admin/includes/update.php‎

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,211 @@ function get_theme_updates() {
641641
return $update_themes;
642642
}
643643

644+
/**
645+
* Gets plugin names for translation updates.
646+
*
647+
* @since 7.2.0
648+
*
649+
* @return string[] Plugin names keyed by plugin file, directory, basename, and slug.
650+
*/
651+
function wp_get_translation_update_plugin_names() {
652+
require_once ABSPATH . 'wp-admin/includes/plugin.php';
653+
654+
$plugin_names = array();
655+
$all_plugins = get_plugins();
656+
$plugin_update = get_site_transient( 'update_plugins' );
657+
658+
if ( is_object( $plugin_update ) ) {
659+
$plugin_updates = array();
660+
661+
if ( ! empty( $plugin_update->response ) && is_array( $plugin_update->response ) ) {
662+
$plugin_updates = array_merge( $plugin_updates, $plugin_update->response );
663+
}
664+
665+
if ( ! empty( $plugin_update->no_update ) && is_array( $plugin_update->no_update ) ) {
666+
$plugin_updates = array_merge( $plugin_updates, $plugin_update->no_update );
667+
}
668+
669+
foreach ( $plugin_updates as $plugin_file => $plugin_data ) {
670+
$plugin_data = (object) $plugin_data;
671+
672+
if ( ! empty( $plugin_data->slug ) && ! empty( $all_plugins[ $plugin_file ]['Name'] ) ) {
673+
$plugin_names[ $plugin_data->slug ] = $all_plugins[ $plugin_file ]['Name'];
674+
}
675+
}
676+
}
677+
678+
foreach ( $all_plugins as $plugin_file => $plugin_data ) {
679+
$plugin_basename = dirname( $plugin_file );
680+
681+
if ( isset( $plugin_data['Name'] ) ) {
682+
$plugin_names[ $plugin_file ] = $plugin_data['Name'];
683+
$plugin_names[ basename( $plugin_file, '.php' ) ] = $plugin_data['Name'];
684+
685+
if ( '.' !== $plugin_basename ) {
686+
$plugin_names[ $plugin_basename ] = $plugin_data['Name'];
687+
}
688+
}
689+
}
690+
691+
return $plugin_names;
692+
}
693+
694+
/**
695+
* Gets the display name for a translation update.
696+
*
697+
* @since 7.2.0
698+
*
699+
* @param object $update Translation update object.
700+
* @param string[]|null $plugin_names Optional. Plugin names keyed by plugin file, directory,
701+
* basename, and slug. Default null.
702+
* @return string The translation update name.
703+
*/
704+
function wp_get_translation_update_name( $update, $plugin_names = null ) {
705+
$type = isset( $update->type ) ? $update->type : '';
706+
$slug = isset( $update->slug ) ? $update->slug : '';
707+
708+
switch ( $type ) {
709+
case 'core':
710+
return 'WordPress'; // Not translated.
711+
712+
case 'theme':
713+
$theme = wp_get_theme( $slug );
714+
if ( $theme->exists() ) {
715+
return $theme->get( 'Name' );
716+
}
717+
break;
718+
719+
case 'plugin':
720+
if ( null === $plugin_names ) {
721+
$plugin_names = wp_get_translation_update_plugin_names();
722+
}
723+
724+
if ( ! empty( $plugin_names[ $slug ] ) ) {
725+
return $plugin_names[ $slug ];
726+
}
727+
break;
728+
}
729+
730+
return $slug;
731+
}
732+
733+
/**
734+
* Gets the display language for a translation update.
735+
*
736+
* @since 7.2.0
737+
*
738+
* @param string $locale Translation update locale.
739+
* @return string The translation update language.
740+
*/
741+
function wp_get_translation_update_language( $locale ) {
742+
$translations = get_site_transient( 'available_translations' );
743+
744+
if ( is_array( $translations ) && ! empty( $translations[ $locale ]['native_name'] ) ) {
745+
return sprintf(
746+
/* translators: 1: Native language name, 2: Locale. */
747+
__( '%1$s (%2$s)' ),
748+
$translations[ $locale ]['native_name'],
749+
$locale
750+
);
751+
}
752+
753+
return $locale;
754+
}
755+
756+
/**
757+
* Gets display data for available translation updates.
758+
*
759+
* @since 7.2.0
760+
*
761+
* @return array[] {
762+
* An array of translation update display data.
763+
*
764+
* @type bool $checked Whether the translation update is selected for installation.
765+
* @type bool $deferred Whether the translation update has been deferred.
766+
* @type string $id Translation update identifier.
767+
* @type string $language Translation update language.
768+
* @type string $language_code Translation update locale.
769+
* @type string $name Translation update name.
770+
* @type string $slug Translation update slug.
771+
* @type string $type Translation update type.
772+
* @type string $version Translation update version.
773+
* }
774+
*/
775+
function wp_get_translation_update_data() {
776+
$available_updates = wp_get_translation_updates();
777+
$deferred_translation_updates = wp_get_deferred_translation_updates( $available_updates );
778+
$plugin_names = null;
779+
$translation_updates = array();
780+
781+
foreach ( $available_updates as $update ) {
782+
$translation_update_id = wp_get_translation_update_id( $update );
783+
$language = isset( $update->language ) ? $update->language : '';
784+
$type = isset( $update->type ) ? $update->type : '';
785+
$deferred = isset( $deferred_translation_updates[ $translation_update_id ] );
786+
787+
if ( 'plugin' === $type && null === $plugin_names ) {
788+
$plugin_names = wp_get_translation_update_plugin_names();
789+
}
790+
791+
$translation_updates[] = array(
792+
'checked' => ! $deferred,
793+
'deferred' => $deferred,
794+
'id' => $translation_update_id,
795+
'language' => wp_get_translation_update_language( $language ),
796+
'language_code' => $language,
797+
'name' => wp_get_translation_update_name( $update, $plugin_names ),
798+
'slug' => isset( $update->slug ) ? $update->slug : '',
799+
'type' => $type,
800+
'version' => isset( $update->version ) ? $update->version : '',
801+
);
802+
}
803+
804+
return $translation_updates;
805+
}
806+
807+
/**
808+
* Processes a translation update selection and stores the deferred updates.
809+
*
810+
* Only currently available updates included in the submitted form can be selected
811+
* or deferred. Previously deferred updates outside the form remain deferred, while
812+
* newly available updates outside the form are left unchanged.
813+
*
814+
* The caller must check capabilities and the nonce before calling this function.
815+
*
816+
* @since 7.2.0
817+
*
818+
* @param string[] $translation_update_ids Sanitized identifiers of updates included in the form.
819+
* @param string[] $selected_translation_update_ids Sanitized identifiers of selected updates.
820+
* @return object[]|false Selected current translation updates keyed by identifier, an empty array
821+
* if all updates in the form were deferred, or false if the form contains
822+
* no currently available updates. On false, stored deferrals are unchanged.
823+
*/
824+
function wp_process_translation_update_selection( $translation_update_ids, $selected_translation_update_ids ) {
825+
if ( empty( $translation_update_ids ) ) {
826+
return false;
827+
}
828+
829+
$current_translation_updates = wp_get_translation_updates_by_id();
830+
$translation_updates = array_intersect_key( $current_translation_updates, array_flip( $translation_update_ids ) );
831+
832+
if ( empty( $translation_updates ) ) {
833+
return false;
834+
}
835+
836+
$selected_translation_updates = array_intersect_key( $translation_updates, array_flip( $selected_translation_update_ids ) );
837+
$deferred_translation_updates = array_intersect_key(
838+
$current_translation_updates,
839+
wp_get_deferred_translation_updates()
840+
);
841+
$deferred_translation_updates = array_diff_key( $deferred_translation_updates, $translation_updates );
842+
$deferred_translation_updates += array_diff_key( $translation_updates, $selected_translation_updates );
843+
844+
wp_set_deferred_translation_updates( $deferred_translation_updates );
845+
846+
return $selected_translation_updates;
847+
}
848+
644849
/**
645850
* Adds a callback to display update information for themes with updates available.
646851
*

0 commit comments

Comments
 (0)