Make WordPress Core

Changeset 65849


Ignore:
Timestamp:
12/01/2026 01:21:01 PM (3 hours ago)
Author:
audrasjb
Message:

Upgrade/Install: Keep the translation update selection after asking for credentials.

When WordPress needs FTP or SSH credentials, the "Connection Information" form posts back to update-core.php?action=do-translation-upgrade without translations[] and checked[]. Since [65226], wp_process_translation_update_selection() returns false for that request and the Updates screen reloads, so the selected translations were never installed.

This changeset:

  • Carries the selected update IDs in the upgrader URL (&translations=<id>,<id>), the same way plugin and theme updates do. The request after the credentials form reads them from there. Deferrals made by the first request are kept.
  • Builds the plugin names in wp_get_translation_update_plugin_names() from the least to the most reliable key: main file names, then plugin files and directories, then slugs from the plugin update data. Before, a plugin whose main file was named after another plugin's slug showed that plugin's name.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/15909.

Follow-up to [65226].

Reviewed by mamaduka, audrasjb.
Merges [65844] to the 7.2 branch.
Props aryanjasala, audrasjb, mayankmajeji.
Fixes #66663.
See #42281.

Location:
branches/7.2
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/7.2

  • branches/7.2/src/wp-admin/includes/update.php

    r65410 r65849  
    656656        $plugin_update = get_site_transient( 'update_plugins' );
    657657
     658        /*
     659         * Keys are added from the least to the most reliable, so a later key replaces an
     660         * earlier one: main file names, then plugin files and directories, then the slugs
     661         * from the plugin update data. Otherwise a plugin whose main file is named after
     662         * another plugin's slug would show that plugin's name.
     663         */
     664        foreach ( $all_plugins as $plugin_file => $plugin_data ) {
     665                if ( isset( $plugin_data['Name'] ) ) {
     666                        $plugin_names[ basename( $plugin_file, '.php' ) ] = $plugin_data['Name'];
     667                }
     668        }
     669
     670        foreach ( $all_plugins as $plugin_file => $plugin_data ) {
     671                $plugin_basename = dirname( $plugin_file );
     672
     673                if ( isset( $plugin_data['Name'] ) ) {
     674                        $plugin_names[ $plugin_file ] = $plugin_data['Name'];
     675
     676                        if ( '.' !== $plugin_basename ) {
     677                                $plugin_names[ $plugin_basename ] = $plugin_data['Name'];
     678                        }
     679                }
     680        }
     681
    658682        if ( is_object( $plugin_update ) ) {
    659683                $plugin_updates = array();
    … …  
    672696                        if ( ! empty( $plugin_data->slug ) && ! empty( $all_plugins[ $plugin_file ]['Name'] ) ) {
    673697                                $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'];
    687698                        }
    688699                }
  • branches/7.2/src/wp-admin/update-core.php

    r65226 r65849  
    13591359
    13601360        // Requests without the form's update identifiers must not install all translations.
    1361         $translation_update_ids          = array_unique(
    1362                 array_map(
    1363                         'sanitize_text_field',
    1364                         wp_unslash( (array) ( $_POST['translations'] ?? array() ) )
    1365                 )
    1366         );
    1367         $selected_translation_update_ids = array_unique(
    1368                 array_map(
    1369                         'sanitize_text_field',
    1370                         wp_unslash( (array) ( $_POST['checked'] ?? array() ) )
    1371                 )
    1372         );
     1361        if ( isset( $_POST['translations'] ) ) {
     1362                $translation_update_ids          = array_unique(
     1363                        array_map(
     1364                                'sanitize_text_field',
     1365                                wp_unslash( (array) $_POST['translations'] )
     1366                        )
     1367                );
     1368                $selected_translation_update_ids = array_unique(
     1369                        array_map(
     1370                                'sanitize_text_field',
     1371                                wp_unslash( (array) ( $_POST['checked'] ?? array() ) )
     1372                        )
     1373                );
     1374        } elseif ( isset( $_GET['translations'] ) && is_string( $_GET['translations'] ) ) {
     1375                /*
     1376                 * The filesystem credentials form posts back to the upgrader URL without the
     1377                 * fields of the Updates screen form, so the URL carries the selected updates.
     1378                 */
     1379                $translation_update_ids          = array_unique(
     1380                        array_map(
     1381                                'sanitize_text_field',
     1382                                explode( ',', wp_unslash( $_GET['translations'] ) )
     1383                        )
     1384                );
     1385                $selected_translation_update_ids = $translation_update_ids;
     1386        } else {
     1387                $translation_update_ids          = array();
     1388                $selected_translation_update_ids = array();
     1389        }
    13731390
    13741391        $selected_translation_updates = wp_process_translation_update_selection( $translation_update_ids, $selected_translation_update_ids );
    … …  
    13871404        require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
    13881405
    1389         $url     = 'update-core.php?action=do-translation-upgrade';
     1406        $url     = 'update-core.php?action=do-translation-upgrade&translations=' . urlencode( implode( ',', array_keys( $selected_translation_updates ) ) );
    13901407        $nonce   = 'upgrade-translations';
    13911408        $title   = __( 'Update Translations' );
  • branches/7.2/tests/phpunit/tests/admin/includesUpdate.php

    r65442 r65849  
    3232                remove_all_filters( 'all_plugins' );
    3333                remove_all_filters( 'pre_site_transient_update_plugins' );
     34                wp_cache_delete( 'plugins', 'plugins' );
    3435
    3536                parent::tear_down();
    … …  
    625626                );
    626627        }
     628
     629        /**
     630         * Ensure that a plugin translation update is named after the plugin with that slug in
     631         * the plugin update data, not after a plugin whose main file has the same name.
     632         *
     633         * @ticket 66663
     634         */
     635        public function test_wp_get_translation_update_plugin_names_prefers_slugs_from_plugin_update_data() {
     636                // get_plugins() reads installed plugins from this cache entry.
     637                wp_cache_set(
     638                        'plugins',
     639                        array(
     640                                '' => array(
     641                                        'bar/bar.php'  => array(
     642                                                'Name' => 'Bar',
     643                                        ),
     644                                        'zeta/bar.php' => array(
     645                                                'Name' => 'Zeta',
     646                                        ),
     647                                ),
     648                        ),
     649                        'plugins'
     650                );
     651
     652                set_site_transient(
     653                        'update_plugins',
     654                        (object) array(
     655                                'no_update' => array(
     656                                        'bar/bar.php'  => (object) array(
     657                                                'slug' => 'bar',
     658                                        ),
     659                                        'zeta/bar.php' => (object) array(
     660                                                'slug' => 'zeta',
     661                                        ),
     662                                ),
     663                        )
     664                );
     665
     666                $plugin_names = wp_get_translation_update_plugin_names();
     667
     668                $this->assertSame( 'Bar', $plugin_names['bar'], 'The slug from the plugin update data should name the plugin that has it.' );
     669                $this->assertSame( 'Zeta', $plugin_names['zeta'], 'The other plugin should keep its own slug.' );
     670        }
     671
     672        /**
     673         * Ensure that a plugin directory name takes precedence over another plugin's main file name.
     674         *
     675         * @ticket 66663
     676         */
     677        public function test_wp_get_translation_update_plugin_names_prefers_directories_over_main_file_names() {
     678                // get_plugins() reads installed plugins from this cache entry.
     679                wp_cache_set(
     680                        'plugins',
     681                        array(
     682                                '' => array(
     683                                        'bar/plugin.php' => array(
     684                                                'Name' => 'Bar',
     685                                        ),
     686                                        'zeta/bar.php'   => array(
     687                                                'Name' => 'Zeta',
     688                                        ),
     689                                ),
     690                        ),
     691                        'plugins'
     692                );
     693
     694                $this->assertSame(
     695                        'Bar',
     696                        wp_get_translation_update_name(
     697                                (object) array(
     698                                        'type' => 'plugin',
     699                                        'slug' => 'bar',
     700                                )
     701                        ),
     702                        'The plugin in the bar directory should name the update for the bar slug.'
     703                );
     704        }
     705
     706        /**
     707         * Ensure that processing the selection again with only the selected updates, as the
     708         * Updates screen does after asking for filesystem credentials, keeps the deferrals
     709         * made by the first request.
     710         *
     711         * @ticket 66663
     712         */
     713        public function test_wp_process_translation_update_selection_after_filesystem_credentials_keeps_deferrals() {
     714                $updates = array();
     715
     716                foreach ( array( 'first-plugin', 'second-plugin' ) as $slug ) {
     717                        $updates[ $slug ] = (object) array(
     718                                'type'     => 'plugin',
     719                                'slug'     => $slug,
     720                                'language' => 'de_DE',
     721                                'version'  => '1.0.0',
     722                        );
     723                }
     724
     725                set_site_transient(
     726                        'update_plugins',
     727                        (object) array(
     728                                'translations' => array_map( 'get_object_vars', array_values( $updates ) ),
     729                        )
     730                );
     731
     732                $first_id  = wp_get_translation_update_id( $updates['first-plugin'] );
     733                $second_id = wp_get_translation_update_id( $updates['second-plugin'] );
     734
     735                // The Updates screen form, with only the first update selected.
     736                $selected = wp_process_translation_update_selection( array( $first_id, $second_id ), array( $first_id ) );
     737
     738                $this->assertSame( array( $first_id ), array_keys( $selected ), 'The first request should select the checked update.' );
     739
     740                // The filesystem credentials form posts back only the selected updates.
     741                $selected = wp_process_translation_update_selection( array( $first_id ), array( $first_id ) );
     742
     743                $this->assertSame( array( $first_id ), array_keys( $selected ), 'The request after the credentials form should select the same update.' );
     744                $this->assertSame( array( $second_id ), array_keys( wp_get_deferred_translation_updates() ), 'The update deferred by the first request should stay deferred.' );
     745        }
    627746}
Note: See TracChangeset for help on using the changeset viewer.