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
This commit is contained in:
Dominik Schilling (ocean90) 2016-10-26 14:14:44 +00:00
parent 3e3ac4d727
commit 368d6ba190
2 changed files with 69 additions and 5 deletions

View File

@ -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 * 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(). * returned. Otherwise it returns the locale of get_locale().
* *
* @since 4.7.0 * @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() { function get_user_locale( $user_id = 0 ) {
$user = wp_get_current_user(); $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; $locale = $user->locale;
return ( '' === $locale ) ? get_locale() : $locale; return $locale ? $locale : get_locale();
} }
/** /**

View File

@ -43,6 +43,11 @@ class Tests_Get_User_Locale extends WP_UnitTestCase {
$this->assertSame( get_locale(), get_user_locale() ); $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() { public function test_returns_correct_user_locale() {
set_current_screen( 'dashboard' ); set_current_screen( 'dashboard' );
$this->assertSame( 'de_DE', get_user_locale() ); $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( 'de_DE', $user_locale );
$this->assertSame( $user_locale, $user_locale_2 ); $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 );
}
} }