diff --git a/wp-includes/class-wp-locale-switcher.php b/wp-includes/class-wp-locale-switcher.php index c6fef2c4a4..21881771d4 100644 --- a/wp-includes/class-wp-locale-switcher.php +++ b/wp-includes/class-wp-locale-switcher.php @@ -45,7 +45,7 @@ class WP_Locale_Switcher { * @since 4.7.0 */ public function __construct() { - $this->original_locale = is_admin() ? get_user_locale() : get_locale(); + $this->original_locale = determine_locale(); $this->available_languages = array_merge( array( 'en_US' ), get_available_languages() ); } @@ -69,7 +69,7 @@ class WP_Locale_Switcher { * @return bool True on success, false on failure. */ public function switch_to_locale( $locale ) { - $current_locale = is_admin() ? get_user_locale() : get_locale(); + $current_locale = determine_locale(); if ( $current_locale === $locale ) { return false; } diff --git a/wp-includes/general-template.php b/wp-includes/general-template.php index 856fbd657b..0c497f5212 100644 --- a/wp-includes/general-template.php +++ b/wp-includes/general-template.php @@ -718,7 +718,7 @@ function get_bloginfo( $show = '', $filter = 'raw' ) { */ $output = __( 'html_lang_attribute' ); if ( 'html_lang_attribute' === $output || preg_match( '/[^a-zA-Z0-9-]/', $output ) ) { - $output = is_admin() ? get_user_locale() : get_locale(); + $output = determine_locale(); $output = str_replace( '_', '-', $output ); } break; diff --git a/wp-includes/l10n.php b/wp-includes/l10n.php index 081f2cf7db..312ae31f90 100644 --- a/wp-includes/l10n.php +++ b/wp-includes/l10n.php @@ -104,6 +104,54 @@ function get_user_locale( $user_id = 0 ) { return $locale ? $locale : get_locale(); } +/** + * Determine the current locale desired for the request. + * + * @since 5.0.0 + * + * @global string $pagenow + * + * @return string The determined locale. + */ +function determine_locale() { + /** + * Filters the locale for the current request prior to the default determination process. + * + * Using this filter allows to override the default logic, effectively short-circuiting the function. + * + * @since 5.0.0 + * + * @param string|null The locale to return and short-circuit, or null as default. + */ + $determined_locale = apply_filters( 'pre_determine_locale', null ); + if ( ! empty( $determined_locale ) && is_string( $determined_locale ) ) { + return $determined_locale; + } + + $determined_locale = get_locale(); + + if ( is_admin() ) { + $determined_locale = get_user_locale(); + } + + if ( isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] && wp_is_json_request() && is_user_logged_in() ) { + $determined_locale = get_user_locale(); + } + + if ( ! empty( $_GET['wp_lang'] ) && 'wp-login.php' === $GLOBALS['pagenow'] ) { + $determined_locale = sanitize_text_field( $_GET['wp_lang'] ); + } + + /** + * Filters the locale for the current request. + * + * @since 5.0.0 + * + * @param string $locale The locale. + */ + return apply_filters( 'determine_locale', $determined_locale ); +} + /** * Retrieve the translation of $text. * @@ -685,7 +733,7 @@ function unload_textdomain( $domain ) { */ function load_default_textdomain( $locale = null ) { if ( null === $locale ) { - $locale = is_admin() ? get_user_locale() : get_locale(); + $locale = determine_locale(); } // Unload previously loaded strings so we can switch translations. @@ -734,7 +782,7 @@ function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path * @param string $locale The plugin's current locale. * @param string $domain Text domain. Unique identifier for retrieving translated strings. */ - $locale = apply_filters( 'plugin_locale', is_admin() ? get_user_locale() : get_locale(), $domain ); + $locale = apply_filters( 'plugin_locale', determine_locale(), $domain ); $mofile = $domain . '-' . $locale . '.mo'; @@ -768,7 +816,7 @@ function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path */ function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) { /** This filter is documented in wp-includes/l10n.php */ - $locale = apply_filters( 'plugin_locale', is_admin() ? get_user_locale() : get_locale(), $domain ); + $locale = apply_filters( 'plugin_locale', determine_locale(), $domain ); $mofile = $domain . '-' . $locale . '.mo'; @@ -807,7 +855,7 @@ function load_theme_textdomain( $domain, $path = false ) { * @param string $locale The theme's current locale. * @param string $domain Text domain. Unique identifier for retrieving translated strings. */ - $locale = apply_filters( 'theme_locale', is_admin() ? get_user_locale() : get_locale(), $domain ); + $locale = apply_filters( 'theme_locale', determine_locale(), $domain ); $mofile = $domain . '-' . $locale . '.mo'; @@ -941,7 +989,7 @@ function _get_path_to_translation_from_lang_dir( $domain ) { } } - $locale = is_admin() ? get_user_locale() : get_locale(); + $locale = determine_locale(); $mofile = "{$domain}-{$locale}.mo"; $path = WP_LANG_DIR . '/plugins/' . $mofile; diff --git a/wp-includes/script-loader.php b/wp-includes/script-loader.php index e3c4ef6543..81bb3e2073 100644 --- a/wp-includes/script-loader.php +++ b/wp-includes/script-loader.php @@ -954,7 +954,7 @@ function wp_default_scripts( &$scripts ) { 'var mejsL10n = %s;', wp_json_encode( array( - 'language' => strtolower( strtok( is_admin() ? get_user_locale() : get_locale(), '_-' ) ), + 'language' => strtolower( strtok( determine_locale(), '_-' ) ), 'strings' => array( 'mejs.install-flash' => __( 'You are using a browser that does not have Flash player enabled or installed. Please turn on your Flash player plugin or download the latest version from https://get.adobe.com/flashplayer/' ), 'mejs.fullscreen-off' => __( 'Turn off Fullscreen' ), diff --git a/wp-includes/version.php b/wp-includes/version.php index e5c4e1f220..d7f2fd1faf 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.1-alpha-44133'; +$wp_version = '5.1-alpha-44134'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. diff --git a/wp-login.php b/wp-login.php index df7cd17aa8..aee4fb0d83 100644 --- a/wp-login.php +++ b/wp-login.php @@ -464,9 +464,6 @@ if ( SITECOOKIEPATH != COOKIEPATH ) { setcookie( TEST_COOKIE, 'WP Cookie check', 0, SITECOOKIEPATH, COOKIE_DOMAIN, $secure ); } -$lang = ! empty( $_GET['wp_lang'] ) ? sanitize_text_field( $_GET['wp_lang'] ) : ''; -$switched_locale = switch_to_locale( $lang ); - /** * Fires when the login form is initialized. * @@ -527,10 +524,6 @@ switch ( $action ) { } setcookie( 'wp-postpass_' . COOKIEHASH, $hasher->HashPassword( wp_unslash( $_POST['post_password'] ) ), $expire, COOKIEPATH, COOKIE_DOMAIN, $secure ); - if ( $switched_locale ) { - restore_previous_locale(); - } - wp_safe_redirect( wp_get_referer() ); exit(); @@ -548,10 +541,6 @@ switch ( $action ) { $requested_redirect_to = ''; } - if ( $switched_locale ) { - restore_previous_locale(); - } - /** * Filters the log out redirect URL. * @@ -649,10 +638,6 @@ switch ( $action ) {