Upgrade/Install: Fix JavaScript localization on install page.

Blocks registration causes scripts to be initialized and localized very early, before the current locale has been properly set on the installation page.

This changes `determine_locale()` so that the locale chosen during installation is recognized and loaded earlier, ensuring proper script localization.

Props sabernhardt, NekoJonez, jornp, costdev.
Fixes #58696

git-svn-id: https://develop.svn.wordpress.org/trunk@57286 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Pascal Birchler 2024-01-15 18:55:59 +00:00
parent 56e16bda31
commit 0d109bda84
3 changed files with 46 additions and 2 deletions

View File

@ -329,7 +329,7 @@ if ( defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) {
*/
$language = '';
if ( ! empty( $_REQUEST['language'] ) ) {
$language = preg_replace( '/[^a-zA-Z0-9_]/', '', $_REQUEST['language'] );
$language = sanitize_locale_name( $_REQUEST['language'] );
} elseif ( isset( $GLOBALS['wp_local_package'] ) ) {
$language = $GLOBALS['wp_local_package'];
}

View File

@ -150,6 +150,15 @@ function determine_locale() {
( isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] && wp_is_json_request() )
) {
$determined_locale = get_user_locale();
} elseif (
( ! empty( $_REQUEST['language'] ) || isset( $GLOBALS['wp_local_package'] ) )
&& wp_installing()
) {
if ( ! empty( $_REQUEST['language'] ) ) {
$determined_locale = sanitize_locale_name( $_REQUEST['language'] );
} else {
$determined_locale = $GLOBALS['wp_local_package'];
}
}
if ( ! $determined_locale ) {

View File

@ -20,7 +20,15 @@ class Tests_L10n_DetermineLocale extends WP_UnitTestCase {
}
public function tear_down() {
unset( $_SERVER['CONTENT_TYPE'], $_GET['_locale'], $_COOKIE['wp_lang'], $GLOBALS['pagenow'] );
unset(
$_SERVER['CONTENT_TYPE'],
$_GET['_locale'],
$_COOKIE['wp_lang'],
$GLOBALS['pagenow'],
$GLOBALS['wp_local_package'],
$_REQUEST['language']
);
wp_installing( false );
parent::tear_down();
}
@ -273,4 +281,31 @@ class Tests_L10n_DetermineLocale extends WP_UnitTestCase {
$this->assertSame( 'siteLocale', determine_locale() );
}
public function test_language_param_not_installing() {
$_REQUEST['language'] = 'de_DE';
$this->assertSame( 'en_US', determine_locale() );
}
public function test_language_param_installing() {
$_REQUEST['language'] = 'de_DE';
wp_installing( true );
$this->assertSame( 'de_DE', determine_locale() );
}
public function test_language_param_installing_incorrect_string() {
$_REQUEST['language'] = '####'; // Something sanitize_locale_name() strips away.
wp_installing( true );
$this->assertSame( 'en_US', determine_locale() );
}
public function test_wp_local_package_global_not_installing() {
$_REQUEST['language'] = 'de_DE';
$this->assertSame( 'en_US', determine_locale() );
}
public function test_wp_local_package_global_installing() {
$_REQUEST['language'] = 'de_DE';
wp_installing( true );
$this->assertSame( 'de_DE', determine_locale() );
}
}