Skip to content

Commit a9849e4

Browse files
committed
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 #15909. Follow-up to [65226]. Props aryanjasala, audrasjb, mayankmajeji. Fixes #66663. See #42281. git-svn-id: https://develop.svn.wordpress.org/trunk@65844 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 8c464b2 commit a9849e4

3 files changed

Lines changed: 173 additions & 26 deletions

File tree

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

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,30 @@ function wp_get_translation_update_plugin_names() {
658658
$all_plugins = get_plugins();
659659
$plugin_update = get_site_transient( 'update_plugins' );
660660

661+
/*
662+
* Keys are added from the least to the most reliable, so a later key replaces an
663+
* earlier one: main file names, then plugin files and directories, then the slugs
664+
* from the plugin update data. Otherwise a plugin whose main file is named after
665+
* another plugin's slug would show that plugin's name.
666+
*/
667+
foreach ( $all_plugins as $plugin_file => $plugin_data ) {
668+
if ( isset( $plugin_data['Name'] ) ) {
669+
$plugin_names[ basename( $plugin_file, '.php' ) ] = $plugin_data['Name'];
670+
}
671+
}
672+
673+
foreach ( $all_plugins as $plugin_file => $plugin_data ) {
674+
$plugin_basename = dirname( $plugin_file );
675+
676+
if ( isset( $plugin_data['Name'] ) ) {
677+
$plugin_names[ $plugin_file ] = $plugin_data['Name'];
678+
679+
if ( '.' !== $plugin_basename ) {
680+
$plugin_names[ $plugin_basename ] = $plugin_data['Name'];
681+
}
682+
}
683+
}
684+
661685
if ( is_object( $plugin_update ) ) {
662686
$plugin_updates = array();
663687

@@ -678,19 +702,6 @@ function wp_get_translation_update_plugin_names() {
678702
}
679703
}
680704

681-
foreach ( $all_plugins as $plugin_file => $plugin_data ) {
682-
$plugin_basename = dirname( $plugin_file );
683-
684-
if ( isset( $plugin_data['Name'] ) ) {
685-
$plugin_names[ $plugin_file ] = $plugin_data['Name'];
686-
$plugin_names[ basename( $plugin_file, '.php' ) ] = $plugin_data['Name'];
687-
688-
if ( '.' !== $plugin_basename ) {
689-
$plugin_names[ $plugin_basename ] = $plugin_data['Name'];
690-
}
691-
}
692-
}
693-
694705
return $plugin_names;
695706
}
696707

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

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,18 +1358,35 @@ function do_undismiss_core_update() {
13581358
check_admin_referer( 'upgrade-translations' );
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 );
13751392

@@ -1386,7 +1403,7 @@ function do_undismiss_core_update() {
13861403
require_once ABSPATH . 'wp-admin/admin-header.php';
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' );
13921409
$context = WP_LANG_DIR;

‎tests/phpunit/tests/admin/includesUpdate.php‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function tear_down() {
3131
delete_site_transient( 'update_themes' );
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();
3637
}
@@ -624,4 +625,122 @@ public static function data_translation_update_selections() {
624625
),
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
}

0 commit comments

Comments
 (0)