I18N: Check that $wp_locale global is set before calling its methods.

This avoids a fatal error if these functions are called in a mu-plugin before `$wp_locale` is set:
* `wp_get_list_item_separator()`
* `wp_get_word_count_type()`

Follow-up to [52929], [52933], [55279], [55295].

Props kraftbj.
Fixes #56698.

git-svn-id: https://develop.svn.wordpress.org/trunk@55351 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2023-02-15 23:35:33 +00:00
parent 7a7cdde6ea
commit 1e6ab88b6f
4 changed files with 68 additions and 1 deletions

View File

@ -235,7 +235,7 @@ class WP_Locale {
$this->number_format['decimal_point'] = ( 'number_format_decimal_point' === $decimal_point ) ? '.' : $decimal_point;
/* translators: used between list items, there is a space after the comma */
/* translators: Used between list items, there is a space after the comma. */
$this->list_item_separator = __( ', ' );
// Set text direction.

View File

@ -1807,6 +1807,12 @@ function translate_settings_using_i18n_schema( $i18n_schema, $settings, $textdom
function wp_get_list_item_separator() {
global $wp_locale;
if ( ! ( $wp_locale instanceof WP_Locale ) ) {
// Default value of WP_Locale::get_list_item_separator().
/* translators: Used between list items, there is a space after the comma. */
return __( ', ' );
}
return $wp_locale->get_list_item_separator();
}
@ -1823,5 +1829,10 @@ function wp_get_list_item_separator() {
function wp_get_word_count_type() {
global $wp_locale;
if ( ! ( $wp_locale instanceof WP_Locale ) ) {
// Default value of WP_Locale::get_word_count_type().
return 'words';
}
return $wp_locale->get_word_count_type();
}

View File

@ -0,0 +1,28 @@
<?php
/**
* @group l10n
* @group i18n
*
* @covers ::wp_get_list_item_separator
*/
class Tests_L10n_wpGetListItemSeparator extends WP_UnitTestCase {
/**
* Tests that the function returns a value when the $wp_locale global is not set.
*
* @ticket 56698
*/
public function test_should_return_default_value_if_wp_locale_is_not_set() {
global $wp_locale;
$original_locale = $wp_locale;
$wp_locale = null;
$actual = wp_get_list_item_separator();
$wp_locale = $original_locale;
$this->assertSame( __( ', ' ), $actual );
}
}

View File

@ -0,0 +1,28 @@
<?php
/**
* @group l10n
* @group i18n
*
* @covers ::wp_get_word_count_type
*/
class Tests_L10n_wpGetWordCountType extends WP_UnitTestCase {
/**
* Tests that the function returns a value when the $wp_locale global is not set.
*
* @ticket 56698
*/
public function test_should_return_default_value_if_wp_locale_is_not_set() {
global $wp_locale;
$original_locale = $wp_locale;
$wp_locale = null;
$actual = wp_get_word_count_type();
$wp_locale = $original_locale;
$this->assertSame( 'words', $actual );
}
}