Make WordPress Core

Changeset 44134


Ignore:
Timestamp:
12/14/2018 01:31:27 AM (8 years ago)
Author:
jeremyfelt
Message:

REST API: Render response in user locale with ?_locale=user.

Introduces new determine_locale() function for deciding the proper locale to use for a response. Default value is get_user_locale() in the admin, and get_locale() on the frontend. Because REST API requests are considered frontend requests, ?_locale=user can be used to render the response in the user's locale.

Also updates wp-login.php?wp_lang implementation to benefit from this abstraction.

Merges [43776] from the 5.0 branch to trunk.

Props flixos90, mnelson4, swissspidy, TimothyBlynJacobs.
Fixes #44758.

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/wp-includes/class-wp-locale-switcher.php

    r42395 r44134  
    4646         */
    4747        public function __construct() {
    48                 $this->original_locale     = is_admin() ? get_user_locale() : get_locale();
     48                $this->original_locale     = determine_locale();
    4949                $this->available_languages = array_merge( array( 'en_US' ), get_available_languages() );
    5050        }
    … …  
    7070         */
    7171        public function switch_to_locale( $locale ) {
    72                 $current_locale = is_admin() ? get_user_locale() : get_locale();
     72                $current_locale = determine_locale();
    7373                if ( $current_locale === $locale ) {
    7474                        return false;
  • trunk/src/wp-includes/general-template.php

    r44121 r44134  
    719719                        $output = __( 'html_lang_attribute' );
    720720                        if ( 'html_lang_attribute' === $output || preg_match( '/[^a-zA-Z0-9-]/', $output ) ) {
    721                                 $output = is_admin() ? get_user_locale() : get_locale();
     721                                $output = determine_locale();
    722722                                $output = str_replace( '_', '-', $output );
    723723                        }
  • trunk/src/wp-includes/l10n.php

    r43571 r44134  
    103103        $locale = $user->locale;
    104104        return $locale ? $locale : get_locale();
     105}
     106
     107/**
     108 * Determine the current locale desired for the request.
     109 *
     110 * @since 5.0.0
     111 *
     112 * @global string $pagenow
     113 *
     114 * @return string The determined locale.
     115 */
     116function determine_locale() {
     117        /**
     118         * Filters the locale for the current request prior to the default determination process.
     119         *
     120         * Using this filter allows to override the default logic, effectively short-circuiting the function.
     121         *
     122         * @since 5.0.0
     123         *
     124         * @param string|null The locale to return and short-circuit, or null as default.
     125         */
     126        $determined_locale = apply_filters( 'pre_determine_locale', null );
     127        if ( ! empty( $determined_locale ) && is_string( $determined_locale ) ) {
     128                return $determined_locale;
     129        }
     130
     131        $determined_locale = get_locale();
     132
     133        if ( is_admin() ) {
     134                $determined_locale = get_user_locale();
     135        }
     136
     137        if ( isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] && wp_is_json_request() && is_user_logged_in() ) {
     138                $determined_locale = get_user_locale();
     139        }
     140
     141        if ( ! empty( $_GET['wp_lang'] ) && 'wp-login.php' === $GLOBALS['pagenow'] ) {
     142                $determined_locale = sanitize_text_field( $_GET['wp_lang'] );
     143        }
     144
     145        /**
     146         * Filters the locale for the current request.
     147         *
     148         * @since 5.0.0
     149         *
     150         * @param string $locale The locale.
     151         */
     152        return apply_filters( 'determine_locale', $determined_locale );
    105153}
    106154
    … …  
    686734function load_default_textdomain( $locale = null ) {
    687735        if ( null === $locale ) {
    688                 $locale = is_admin() ? get_user_locale() : get_locale();
     736                $locale = determine_locale();
    689737        }
    690738
    … …  
    735783         * @param string $domain Text domain. Unique identifier for retrieving translated strings.
    736784         */
    737         $locale = apply_filters( 'plugin_locale', is_admin() ? get_user_locale() : get_locale(), $domain );
     785        $locale = apply_filters( 'plugin_locale', determine_locale(), $domain );
    738786
    739787        $mofile = $domain . '-' . $locale . '.mo';
    … …  
    769817function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) {
    770818        /** This filter is documented in wp-includes/l10n.php */
    771         $locale = apply_filters( 'plugin_locale', is_admin() ? get_user_locale() : get_locale(), $domain );
     819        $locale = apply_filters( 'plugin_locale', determine_locale(), $domain );
    772820
    773821        $mofile = $domain . '-' . $locale . '.mo';
    … …  
    808856         * @param string $domain Text domain. Unique identifier for retrieving translated strings.
    809857         */
    810         $locale = apply_filters( 'theme_locale', is_admin() ? get_user_locale() : get_locale(), $domain );
     858        $locale = apply_filters( 'theme_locale', determine_locale(), $domain );
    811859
    812860        $mofile = $domain . '-' . $locale . '.mo';
    … …  
    942990        }
    943991
    944         $locale = is_admin() ? get_user_locale() : get_locale();
     992        $locale = determine_locale();
    945993        $mofile = "{$domain}-{$locale}.mo";
    946994
  • trunk/src/wp-includes/script-loader.php

    r44119 r44134  
    955955                        wp_json_encode(
    956956                                array(
    957                                         'language' => strtolower( strtok( is_admin() ? get_user_locale() : get_locale(), '_-' ) ),
     957                                        'language' => strtolower( strtok( determine_locale(), '_-' ) ),
    958958                                        'strings'  => array(
    959959                                                '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/' ),
  • trunk/src/wp-login.php

    r44021 r44134  
    465465}
    466466
    467 $lang            = ! empty( $_GET['wp_lang'] ) ? sanitize_text_field( $_GET['wp_lang'] ) : '';
    468 $switched_locale = switch_to_locale( $lang );
    469 
    470467/**
    471468 * Fires when the login form is initialized.
    … …  
    528525                setcookie( 'wp-postpass_' . COOKIEHASH, $hasher->HashPassword( wp_unslash( $_POST['post_password'] ) ), $expire, COOKIEPATH, COOKIE_DOMAIN, $secure );
    529526
    530                 if ( $switched_locale ) {
    531                         restore_previous_locale();
    532                 }
    533 
    534527                wp_safe_redirect( wp_get_referer() );
    535528                exit();
    … …  
    547540                        $redirect_to           = 'wp-login.php?loggedout=true';
    548541                        $requested_redirect_to = '';
    549                 }
    550 
    551                 if ( $switched_locale ) {
    552                         restore_previous_locale();
    553542                }
    554543
    … …  
    649638                <?php
    650639                login_footer( 'user_login' );
    651 
    652                 if ( $switched_locale ) {
    653                         restore_previous_locale();
    654                 }
    655640
    656641                break;
    … …  
    781766                login_footer( 'user_pass' );
    782767
    783                 if ( $switched_locale ) {
    784                         restore_previous_locale();
    785                 }
    786 
    787768                break;
    788769
    … …  
    867848                <?php
    868849                login_footer( 'user_login' );
    869 
    870                 if ( $switched_locale ) {
    871                         restore_previous_locale();
    872                 }
    873850
    874851                break;
    … …  
    11801157                login_footer();
    11811158
    1182                 if ( $switched_locale ) {
    1183                         restore_previous_locale();
    1184                 }
    1185 
    11861159                break;
    11871160} // End action switch.
Note: See TracChangeset for help on using the changeset viewer.