WordPress.org

Make WordPress Core

Changeset 38955


Ignore:
Timestamp:
10/26/2016 02:14:44 PM (19 months 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.

Location:
trunk
Files:
2 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
  • trunk/tests/phpunit/tests/l10n/getUserLocale.php

    r38705 r38955  
    4444    }
    4545
     46    public function test_returns_site_locale_if_no_user() {
     47        wp_set_current_user( 0 );
     48        $this->assertSame( get_locale(), get_user_locale() );
     49    }
     50
    4651    public function test_returns_correct_user_locale() {
    4752        set_current_screen( 'dashboard' );
     
    7681        $this->assertSame( $user_locale, $user_locale_2 );
    7782    }
     83
     84    public function test_user_id_argument_with_id() {
     85        $user_id = $this->factory()->user->create( array(
     86            'locale' => 'es_ES',
     87        ) );
     88
     89        $user_locale1 = get_user_locale( $user_id );
     90
     91        delete_user_meta( $user_id, 'locale' );
     92
     93        $user_locale2 = get_user_locale( $user_id );
     94
     95        $this->assertSame( 'es_ES', $user_locale1 );
     96        $this->assertSame( get_locale(), $user_locale2 );
     97    }
     98
     99    public function test_user_id_argument_with_wp_user_object() {
     100        $user_id = $this->factory()->user->create( array(
     101            'locale' => 'es_ES',
     102        ) );
     103
     104        $user = get_user_by( 'id', $user_id );
     105
     106        $user_locale1 = get_user_locale( $user );
     107
     108        delete_user_meta( $user_id, 'locale' );
     109
     110        $user_locale2 = get_user_locale( $user );
     111
     112        $this->assertSame( 'es_ES', $user_locale1 );
     113        $this->assertSame( get_locale(), $user_locale2 );
     114    }
     115
     116    public function test_user_id_argument_with_nonexistent_user() {
     117        global $wpdb;
     118
     119        $user_id = $wpdb->get_var( "SELECT MAX(ID) FROM $wpdb->users" ) + 1;
     120
     121        $user_locale = get_user_locale( $user_id );
     122
     123        $this->assertSame( get_locale(), $user_locale );
     124    }
     125
     126    public function test_user_id_argument_with_invalid_type() {
     127        $user_locale = get_user_locale( 'string' );
     128        $this->assertSame( get_locale(), $user_locale );
     129    }
    78130}
Note: See TracChangeset for help on using the changeset viewer.