diff --git a/wp-includes/class-wp-locale-switcher.php b/wp-includes/class-wp-locale-switcher.php index e111892871..aa74fb4f83 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() ); } @@ -67,7 +67,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 63b85378e2..c3eca6df89 100644 --- a/wp-includes/general-template.php +++ b/wp-includes/general-template.php @@ -703,7 +703,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 0ee9c46f2e..b1aa6c553c 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. * @@ -663,7 +711,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. @@ -711,7 +759,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'; @@ -745,7 +793,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'; @@ -784,7 +832,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'; @@ -915,7 +963,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; @@ -1210,7 +1258,7 @@ function wp_dropdown_languages( $args = array() ) { selected( '', $parsed_args['selected'], false ) ); - // List installed languages. + // List installed languages. foreach ( $languages as $language ) { $structure[] = sprintf( '', @@ -1347,7 +1395,7 @@ function wp_get_jed_locale_data( $domain ) { $locale = array( '' => array( 'domain' => $domain, - 'lang' => is_admin() ? get_user_locale() : get_locale(), + 'lang' => determine_locale(), ), ); diff --git a/wp-includes/script-loader.php b/wp-includes/script-loader.php index dcc167e19f..598e6dfac3 100644 --- a/wp-includes/script-loader.php +++ b/wp-includes/script-loader.php @@ -905,7 +905,7 @@ function wp_default_scripts( &$scripts ) { $scripts->add( 'mediaelement-migrate', "/wp-includes/js/mediaelement/mediaelement-migrate$suffix.js", array(), false, 1); did_action( 'init' ) && $scripts->add_inline_script( 'mediaelement-core', sprintf( '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 c8eabad5ed..609344e718 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '5.0-alpha-43775'; +$wp_version = '5.0-alpha-43776'; /** * 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 ffd86c1e20..fe9df8704b 100644 --- a/wp-login.php +++ b/wp-login.php @@ -437,9 +437,6 @@ setcookie( TEST_COOKIE, 'WP Cookie check', 0, COOKIEPATH, COOKIE_DOMAIN, $secure 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. * @@ -500,10 +497,6 @@ case 'postpass' : } 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(); @@ -521,10 +514,6 @@ case 'logout' : $requested_redirect_to = ''; } - if ( $switched_locale ) { - restore_previous_locale(); - } - /** * Filters the log out redirect URL. * @@ -618,10 +607,6 @@ endif;