@@ -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