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.
Built from https://develop.svn.wordpress.org/trunk@38955


git-svn-id: http://core.svn.wordpress.org/trunk@38898 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Dominik Schilling 2016-10-26 14:15:32 +00:00
parent 0c2a6184eb
commit 49c5fb895d
2 changed files with 18 additions and 6 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
* 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();
}
/**

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.7-alpha-38954';
$wp_version = '4.7-alpha-38955';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.