From 1e6ab88b6f83830f480c33b3306c7e2df20912f4 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Wed, 15 Feb 2023 23:35:33 +0000 Subject: [PATCH] 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 --- src/wp-includes/class-wp-locale.php | 2 +- src/wp-includes/l10n.php | 11 ++++++++ .../tests/l10n/wpGetListItemSeparator.php | 28 +++++++++++++++++++ .../phpunit/tests/l10n/wpGetWordCountType.php | 28 +++++++++++++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/tests/l10n/wpGetListItemSeparator.php create mode 100644 tests/phpunit/tests/l10n/wpGetWordCountType.php diff --git a/src/wp-includes/class-wp-locale.php b/src/wp-includes/class-wp-locale.php index 38b1860b89..140b93b482 100644 --- a/src/wp-includes/class-wp-locale.php +++ b/src/wp-includes/class-wp-locale.php @@ -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. diff --git a/src/wp-includes/l10n.php b/src/wp-includes/l10n.php index 52a51d5147..eb1a2cc5a9 100644 --- a/src/wp-includes/l10n.php +++ b/src/wp-includes/l10n.php @@ -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(); } diff --git a/tests/phpunit/tests/l10n/wpGetListItemSeparator.php b/tests/phpunit/tests/l10n/wpGetListItemSeparator.php new file mode 100644 index 0000000000..5fc3d9e6f3 --- /dev/null +++ b/tests/phpunit/tests/l10n/wpGetListItemSeparator.php @@ -0,0 +1,28 @@ +assertSame( __( ', ' ), $actual ); + } +} diff --git a/tests/phpunit/tests/l10n/wpGetWordCountType.php b/tests/phpunit/tests/l10n/wpGetWordCountType.php new file mode 100644 index 0000000000..3a4b032205 --- /dev/null +++ b/tests/phpunit/tests/l10n/wpGetWordCountType.php @@ -0,0 +1,28 @@ +assertSame( 'words', $actual ); + } +}