Skip to content

Commit 66d85f3

Browse files
committed
I18N: Refactor determine_locale() for performance and readability.
Refactors the function to avoid unnecessary `get_locale()` calls and slightly improve performance, while keeping it readable. Adds tests. Props Cybr, spacedmonkey, swissspidy. Fixes #58317. git-svn-id: https://develop.svn.wordpress.org/trunk@55862 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 404fd63 commit 66d85f3

File tree

2 files changed

+276
-20
lines changed

2 files changed

+276
-20
lines changed

src/wp-includes/l10n.php

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -132,30 +132,26 @@ function determine_locale() {
132132
*/
133133
$determined_locale = apply_filters( 'pre_determine_locale', null );
134134

135-
if ( ! empty( $determined_locale ) && is_string( $determined_locale ) ) {
135+
if ( $determined_locale && is_string( $determined_locale ) ) {
136136
return $determined_locale;
137137
}
138138

139-
$determined_locale = get_locale();
140-
141-
if ( is_admin() ) {
142-
$determined_locale = get_user_locale();
143-
}
144-
145-
if ( isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] && wp_is_json_request() ) {
139+
if (
140+
isset( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] &&
141+
( ! empty( $_GET['wp_lang'] ) || ! empty( $_COOKIE['wp_lang'] ) )
142+
) {
143+
if ( ! empty( $_GET['wp_lang'] ) ) {
144+
$determined_locale = sanitize_locale_name( $_GET['wp_lang'] );
145+
} else {
146+
$determined_locale = sanitize_locale_name( $_COOKIE['wp_lang'] );
147+
}
148+
} else if (
149+
( isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] && wp_is_json_request() ) ||
150+
is_admin()
151+
) {
146152
$determined_locale = get_user_locale();
147-
}
148-
149-
$wp_lang = '';
150-
151-
if ( ! empty( $_GET['wp_lang'] ) ) {
152-
$wp_lang = sanitize_locale_name( wp_unslash( $_GET['wp_lang'] ) );
153-
} elseif ( ! empty( $_COOKIE['wp_lang'] ) ) {
154-
$wp_lang = sanitize_locale_name( wp_unslash( $_COOKIE['wp_lang'] ) );
155-
}
156-
157-
if ( ! empty( $wp_lang ) && ! empty( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] ) {
158-
$determined_locale = $wp_lang;
153+
} else {
154+
$determined_locale = get_locale();
159155
}
160156

161157
/**
Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
<?php
2+
3+
/**
4+
* @group l10n
5+
* @group i18n
6+
*
7+
* @covers ::determine_locale
8+
*/
9+
class Tests_L10n_DetermineLocale extends WP_UnitTestCase {
10+
protected $locale;
11+
protected static $user_id;
12+
13+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
14+
self::$user_id = $factory->user->create(
15+
array(
16+
'role' => 'administrator',
17+
'locale' => 'userLocale',
18+
)
19+
);
20+
}
21+
22+
public function tear_down() {
23+
unset( $_SERVER['CONTENT_TYPE'], $_GET['_locale'], $_COOKIE['wp_lang'], $GLOBALS['pagenow'] );
24+
25+
parent::tear_down();
26+
}
27+
28+
public function test_short_circuit_empty() {
29+
add_filter( 'pre_determine_locale', '__return_false' );
30+
$this->assertNotFalse( determine_locale() );
31+
}
32+
33+
public function test_short_circuit_no_string() {
34+
add_filter(
35+
'pre_determine_locale',
36+
static function() {
37+
return 1234;
38+
}
39+
);
40+
$this->assertNotFalse( determine_locale() );
41+
}
42+
43+
public function test_short_circuit_string() {
44+
add_filter(
45+
'pre_determine_locale',
46+
static function() {
47+
return 'myNewLocale';
48+
}
49+
);
50+
$this->assertSame( 'myNewLocale', determine_locale() );
51+
}
52+
53+
public function test_defaults_to_site_locale() {
54+
add_filter(
55+
'locale',
56+
static function() {
57+
return 'siteLocale';
58+
}
59+
);
60+
61+
$this->assertSame( get_locale(), determine_locale() );
62+
}
63+
64+
public function test_is_admin_no_user() {
65+
add_filter(
66+
'locale',
67+
static function() {
68+
return 'siteLocale';
69+
}
70+
);
71+
72+
set_current_screen( 'dashboard' );
73+
74+
$this->assertSame( 'siteLocale', determine_locale() );
75+
}
76+
77+
public function test_is_admin_user_locale() {
78+
add_filter(
79+
'locale',
80+
static function() {
81+
return 'siteLocale';
82+
}
83+
);
84+
85+
set_current_screen( 'dashboard' );
86+
wp_set_current_user( self::$user_id );
87+
88+
$this->assertSame( 'userLocale', determine_locale() );
89+
}
90+
91+
public function test_json_request_user_locale() {
92+
add_filter(
93+
'locale',
94+
static function() {
95+
return 'siteLocale';
96+
}
97+
);
98+
99+
wp_set_current_user( self::$user_id );
100+
101+
$_SERVER['CONTENT_TYPE'] = 'application/json';
102+
$_GET['_locale'] = 'user';
103+
104+
$this->assertSame( 'userLocale', determine_locale() );
105+
}
106+
107+
public function test_json_request_user_locale_no_user() {
108+
add_filter(
109+
'locale',
110+
static function() {
111+
return 'siteLocale';
112+
}
113+
);
114+
115+
$_SERVER['CONTENT_TYPE'] = 'application/json';
116+
$_GET['_locale'] = 'user';
117+
118+
$this->assertSame( 'siteLocale', determine_locale() );
119+
}
120+
121+
public function test_json_request_missing_get_param() {
122+
add_filter(
123+
'locale',
124+
static function() {
125+
return 'siteLocale';
126+
}
127+
);
128+
129+
wp_set_current_user( self::$user_id );
130+
131+
$_SERVER['CONTENT_TYPE'] = 'application/json';
132+
133+
$this->assertSame( 'siteLocale', determine_locale() );
134+
}
135+
136+
public function test_json_request_incorrect_get_param() {
137+
add_filter(
138+
'locale',
139+
static function() {
140+
return 'siteLocale';
141+
}
142+
);
143+
144+
wp_set_current_user( self::$user_id );
145+
146+
$_SERVER['CONTENT_TYPE'] = 'application/json';
147+
$_GET['_locale'] = 'foo';
148+
149+
$this->assertSame( 'siteLocale', determine_locale() );
150+
}
151+
152+
public function test_get_param_but_no_json_request() {
153+
add_filter(
154+
'locale',
155+
static function() {
156+
return 'siteLocale';
157+
}
158+
);
159+
160+
wp_set_current_user( self::$user_id );
161+
162+
$_GET['_locale'] = 'user';
163+
164+
$this->assertSame( 'siteLocale', determine_locale() );
165+
}
166+
167+
public function test_wp_login_get_param_not_on_login_page() {
168+
add_filter(
169+
'locale',
170+
static function() {
171+
return 'siteLocale';
172+
}
173+
);
174+
175+
wp_set_current_user( self::$user_id );
176+
177+
$_GET['wp_lang'] = 'de_DE';
178+
179+
$this->assertSame( 'siteLocale', determine_locale() );
180+
}
181+
182+
public function test_wp_login_get_param_on_login_page() {
183+
add_filter(
184+
'locale',
185+
static function() {
186+
return 'siteLocale';
187+
}
188+
);
189+
190+
wp_set_current_user( self::$user_id );
191+
192+
$GLOBALS['pagenow'] = 'wp-login.php';
193+
$_GET['wp_lang'] = 'de_DE';
194+
195+
$this->assertSame( 'de_DE', determine_locale() );
196+
}
197+
198+
public function test_wp_login_get_param_on_login_page_empty_string() {
199+
add_filter(
200+
'locale',
201+
static function() {
202+
return 'siteLocale';
203+
}
204+
);
205+
206+
wp_set_current_user( self::$user_id );
207+
208+
$GLOBALS['pagenow'] = 'wp-login.php';
209+
$_GET['wp_lang'] = '';
210+
211+
$this->assertSame( 'siteLocale', determine_locale() );
212+
}
213+
214+
public function test_wp_login_cookie_not_on_login_page() {
215+
add_filter(
216+
'locale',
217+
static function() {
218+
return 'siteLocale';
219+
}
220+
);
221+
222+
wp_set_current_user( self::$user_id );
223+
224+
$_COOKIE['wp_lang'] = 'de_DE';
225+
226+
$this->assertSame( 'siteLocale', determine_locale() );
227+
}
228+
229+
public function test_wp_login_cookie_on_login_page() {
230+
add_filter(
231+
'locale',
232+
static function() {
233+
return 'siteLocale';
234+
}
235+
);
236+
237+
wp_set_current_user( self::$user_id );
238+
239+
$GLOBALS['pagenow'] = 'wp-login.php';
240+
$_COOKIE['wp_lang'] = 'de_DE';
241+
242+
$this->assertSame( 'de_DE', determine_locale() );
243+
}
244+
245+
public function test_wp_login_cookie_on_login_page_empty_string() {
246+
add_filter(
247+
'locale',
248+
static function() {
249+
return 'siteLocale';
250+
}
251+
);
252+
253+
wp_set_current_user( self::$user_id );
254+
255+
$GLOBALS['pagenow'] = 'wp-login.php';
256+
$_COOKIE['wp_lang'] = '';
257+
258+
$this->assertSame( 'siteLocale', determine_locale() );
259+
}
260+
}

0 commit comments

Comments
 (0)