From afd88c4ac75720caf685649d9a455e9998dcff8a Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 26 May 2023 17:57:23 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@55862 git-svn-id: http://core.svn.wordpress.org/trunk@55374 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/l10n.php | 36 ++++++++++++++++-------------------- wp-includes/version.php | 2 +- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/wp-includes/l10n.php b/wp-includes/l10n.php index 448a233fa8..080007b869 100644 --- a/wp-includes/l10n.php +++ b/wp-includes/l10n.php @@ -132,30 +132,26 @@ function determine_locale() { */ $determined_locale = apply_filters( 'pre_determine_locale', null ); - if ( ! empty( $determined_locale ) && is_string( $determined_locale ) ) { + if ( $determined_locale && is_string( $determined_locale ) ) { return $determined_locale; } - $determined_locale = get_locale(); - - if ( is_admin() ) { + if ( + isset( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] && + ( ! empty( $_GET['wp_lang'] ) || ! empty( $_COOKIE['wp_lang'] ) ) + ) { + if ( ! empty( $_GET['wp_lang'] ) ) { + $determined_locale = sanitize_locale_name( $_GET['wp_lang'] ); + } else { + $determined_locale = sanitize_locale_name( $_COOKIE['wp_lang'] ); + } + } else if ( + ( isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] && wp_is_json_request() ) || + is_admin() + ) { $determined_locale = get_user_locale(); - } - - if ( isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] && wp_is_json_request() ) { - $determined_locale = get_user_locale(); - } - - $wp_lang = ''; - - if ( ! empty( $_GET['wp_lang'] ) ) { - $wp_lang = sanitize_locale_name( wp_unslash( $_GET['wp_lang'] ) ); - } elseif ( ! empty( $_COOKIE['wp_lang'] ) ) { - $wp_lang = sanitize_locale_name( wp_unslash( $_COOKIE['wp_lang'] ) ); - } - - if ( ! empty( $wp_lang ) && ! empty( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] ) { - $determined_locale = $wp_lang; + } else { + $determined_locale = get_locale(); } /** diff --git a/wp-includes/version.php b/wp-includes/version.php index ebbdb952e7..1bffcbb766 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.3-alpha-55861'; +$wp_version = '6.3-alpha-55862'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.