WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/26/2016 02:14:44 PM (3 years ago)
Author:
ocean90
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/l10n.php

    r38705 r38955  
    7777
    7878/**
    79  * Retrieves the locale of the current user.
     79 * Retrieves the locale of a user.
    8080 *
    8181 * If the user has a locale set to a non-empty string then it will be
     
    8484 * @since 4.7.0
    8585 *
    86  * @return string The locale of the current user.
    87  */
    88 function get_user_locale() {
    89     $user = wp_get_current_user();
     86 * @param int|WP_User $user_id User's ID or a WP_User object. Defaults to current user.
     87 * @return string The locale of the user.
     88 */
     89function get_user_locale( $user_id = 0 ) {
     90    $user = false;
     91    if ( 0 === $user_id ) {
     92        $user = wp_get_current_user();
     93    } elseif ( $user_id instanceof WP_User ) {
     94        $user = $user_id;
     95    } elseif ( is_numeric( $user_id ) ) {
     96        $user = get_user_by( 'id', $user_id );
     97    }
     98
     99    if ( ! $user ) {
     100        return get_locale();
     101    }
    90102
    91103    $locale = $user->locale;
    92     return ( '' === $locale ) ? get_locale() : $locale;
     104    return $locale ? $locale : get_locale();
    93105}
    94106
Note: See TracChangeset for help on using the changeset viewer.