Skip to content

Commit 1e6ab88

Browse files
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
1 parent 7a7cdde commit 1e6ab88

4 files changed

Lines changed: 68 additions & 1 deletion

File tree

src/wp-includes/class-wp-locale.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public function init() {
235235

236236
$this->number_format['decimal_point'] = ( 'number_format_decimal_point' === $decimal_point ) ? '.' : $decimal_point;
237237

238-
/* translators: used between list items, there is a space after the comma */
238+
/* translators: Used between list items, there is a space after the comma. */
239239
$this->list_item_separator = __( ', ' );
240240

241241
// Set text direction.

src/wp-includes/l10n.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,6 +1807,12 @@ function translate_settings_using_i18n_schema( $i18n_schema, $settings, $textdom
18071807
function wp_get_list_item_separator() {
18081808
global $wp_locale;
18091809

1810+
if ( ! ( $wp_locale instanceof WP_Locale ) ) {
1811+
// Default value of WP_Locale::get_list_item_separator().
1812+
/* translators: Used between list items, there is a space after the comma. */
1813+
return __( ', ' );
1814+
}
1815+
18101816
return $wp_locale->get_list_item_separator();
18111817
}
18121818

@@ -1823,5 +1829,10 @@ function wp_get_list_item_separator() {
18231829
function wp_get_word_count_type() {
18241830
global $wp_locale;
18251831

1832+
if ( ! ( $wp_locale instanceof WP_Locale ) ) {
1833+
// Default value of WP_Locale::get_word_count_type().
1834+
return 'words';
1835+
}
1836+
18261837
return $wp_locale->get_word_count_type();
18271838
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* @group l10n
5+
* @group i18n
6+
*
7+
* @covers ::wp_get_list_item_separator
8+
*/
9+
class Tests_L10n_wpGetListItemSeparator extends WP_UnitTestCase {
10+
11+
/**
12+
* Tests that the function returns a value when the $wp_locale global is not set.
13+
*
14+
* @ticket 56698
15+
*/
16+
public function test_should_return_default_value_if_wp_locale_is_not_set() {
17+
global $wp_locale;
18+
19+
$original_locale = $wp_locale;
20+
$wp_locale = null;
21+
22+
$actual = wp_get_list_item_separator();
23+
24+
$wp_locale = $original_locale;
25+
26+
$this->assertSame( __( ', ' ), $actual );
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* @group l10n
5+
* @group i18n
6+
*
7+
* @covers ::wp_get_word_count_type
8+
*/
9+
class Tests_L10n_wpGetWordCountType extends WP_UnitTestCase {
10+
11+
/**
12+
* Tests that the function returns a value when the $wp_locale global is not set.
13+
*
14+
* @ticket 56698
15+
*/
16+
public function test_should_return_default_value_if_wp_locale_is_not_set() {
17+
global $wp_locale;
18+
19+
$original_locale = $wp_locale;
20+
$wp_locale = null;
21+
22+
$actual = wp_get_word_count_type();
23+
24+
$wp_locale = $original_locale;
25+
26+
$this->assertSame( 'words', $actual );
27+
}
28+
}

0 commit comments

Comments
 (0)