Ticket #39295: 39295.2.alt.diff
File 39295.2.alt.diff, 1.8 KB (added by , 4 years ago) |
---|
-
src/wp-includes/l10n.php
88 88 * 89 89 * @since 4.7.0 90 90 * 91 * @global string $locale The current locale. 92 * 91 93 * @param int|WP_User $user_id User's ID or a WP_User object. Defaults to current user. 92 94 * @return string The locale of the user. 93 95 */ 94 96 function get_user_locale( $user_id = 0 ) { 97 global $locale; 98 95 99 $user = false; 96 100 if ( 0 === $user_id && function_exists( 'wp_get_current_user' ) ) { 97 101 $user = wp_get_current_user(); … … 101 105 $user = get_user_by( 'id', $user_id ); 102 106 } 103 107 104 if ( ! $user ) { 105 return get_locale(); 108 if ( $user && $user->locale ) { 109 $user_locale = $user->locale; 110 } elseif ( ! doing_filter( 'locale' ) ) { 111 $user_locale = get_locale(); 112 } else { 113 $user_locale = $locale; 106 114 } 107 115 108 $locale = $user->locale; 109 return $locale ? $locale : get_locale(); 116 return $user_locale; 110 117 } 111 118 112 119 /** -
tests/phpunit/tests/l10n/getUserLocale.php
136 136 $user_locale = get_user_locale( 'string' ); 137 137 $this->assertSame( get_locale(), $user_locale ); 138 138 } 139 140 /** 141 * @ticket 39295 142 */ 143 public function test_get_user_locale_as_locale_filter_works_as_expected() { 144 wp_set_current_user( 0 ); 145 switch_to_locale( 'de_DE'); 146 147 $user_locale1 = get_user_locale(); 148 149 add_filter( 'locale', 'get_user_locale' ); 150 151 $user_locale2 = get_user_locale(); 152 153 remove_filter( 'locale', 'get_user_locale' ); 154 155 restore_current_locale(); 156 157 $this->assertSame( 'de_DE', $user_locale1 ); 158 $this->assertSame( 'en_US', $user_locale2 ); 159 } 139 160 }