From 368d6ba1909abe2b83e7df29cccd1efa02e71218 Mon Sep 17 00:00:00 2001 From: "Dominik Schilling (ocean90)" Date: Wed, 26 Oct 2016 14:14:44 +0000 Subject: [PATCH] I18N: Add `$user_id` argument to `get_user_locale()`. This allows to retrieve the locale of any user with the additional fallback to the site locale. Fixes #38512. See #29783, #26511. git-svn-id: https://develop.svn.wordpress.org/trunk@38955 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/l10n.php | 22 ++++++--- tests/phpunit/tests/l10n/getUserLocale.php | 52 ++++++++++++++++++++++ 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/l10n.php b/src/wp-includes/l10n.php index 03a7a11bec..ba696ded93 100644 --- a/src/wp-includes/l10n.php +++ b/src/wp-includes/l10n.php @@ -76,20 +76,32 @@ function get_locale() { } /** - * Retrieves the locale of the current user. + * Retrieves the locale of a user. * * If the user has a locale set to a non-empty string then it will be * returned. Otherwise it returns the locale of get_locale(). * * @since 4.7.0 * - * @return string The locale of the current user. + * @param int|WP_User $user_id User's ID or a WP_User object. Defaults to current user. + * @return string The locale of the user. */ -function get_user_locale() { - $user = wp_get_current_user(); +function get_user_locale( $user_id = 0 ) { + $user = false; + if ( 0 === $user_id ) { + $user = wp_get_current_user(); + } elseif ( $user_id instanceof WP_User ) { + $user = $user_id; + } elseif ( is_numeric( $user_id ) ) { + $user = get_user_by( 'id', $user_id ); + } + + if ( ! $user ) { + return get_locale(); + } $locale = $user->locale; - return ( '' === $locale ) ? get_locale() : $locale; + return $locale ? $locale : get_locale(); } /** diff --git a/tests/phpunit/tests/l10n/getUserLocale.php b/tests/phpunit/tests/l10n/getUserLocale.php index 1ea3d4b89a..c55642622c 100644 --- a/tests/phpunit/tests/l10n/getUserLocale.php +++ b/tests/phpunit/tests/l10n/getUserLocale.php @@ -43,6 +43,11 @@ class Tests_Get_User_Locale extends WP_UnitTestCase { $this->assertSame( get_locale(), get_user_locale() ); } + public function test_returns_site_locale_if_no_user() { + wp_set_current_user( 0 ); + $this->assertSame( get_locale(), get_user_locale() ); + } + public function test_returns_correct_user_locale() { set_current_screen( 'dashboard' ); $this->assertSame( 'de_DE', get_user_locale() ); @@ -75,4 +80,51 @@ class Tests_Get_User_Locale extends WP_UnitTestCase { $this->assertSame( 'de_DE', $user_locale ); $this->assertSame( $user_locale, $user_locale_2 ); } + + public function test_user_id_argument_with_id() { + $user_id = $this->factory()->user->create( array( + 'locale' => 'es_ES', + ) ); + + $user_locale1 = get_user_locale( $user_id ); + + delete_user_meta( $user_id, 'locale' ); + + $user_locale2 = get_user_locale( $user_id ); + + $this->assertSame( 'es_ES', $user_locale1 ); + $this->assertSame( get_locale(), $user_locale2 ); + } + + public function test_user_id_argument_with_wp_user_object() { + $user_id = $this->factory()->user->create( array( + 'locale' => 'es_ES', + ) ); + + $user = get_user_by( 'id', $user_id ); + + $user_locale1 = get_user_locale( $user ); + + delete_user_meta( $user_id, 'locale' ); + + $user_locale2 = get_user_locale( $user ); + + $this->assertSame( 'es_ES', $user_locale1 ); + $this->assertSame( get_locale(), $user_locale2 ); + } + + public function test_user_id_argument_with_nonexistent_user() { + global $wpdb; + + $user_id = $wpdb->get_var( "SELECT MAX(ID) FROM $wpdb->users" ) + 1; + + $user_locale = get_user_locale( $user_id ); + + $this->assertSame( get_locale(), $user_locale ); + } + + public function test_user_id_argument_with_invalid_type() { + $user_locale = get_user_locale( 'string' ); + $this->assertSame( get_locale(), $user_locale ); + } }