Make WordPress Core

Ticket #29783: 29783.6.diff

File 29783.6.diff, 17.8 KB (added by swissspidy, 8 years ago)
  • src/wp-admin/admin-header.php

    diff --git src/wp-admin/admin-header.php src/wp-admin/admin-header.php
    index 9ec3999..b5bbec0 100644
    if ( $current_screen->taxonomy ) 
    156156$admin_body_class .= ' branch-' . str_replace( array( '.', ',' ), '-', floatval( get_bloginfo( 'version' ) ) );
    157157$admin_body_class .= ' version-' . str_replace( '.', '-', preg_replace( '/^([.0-9]+).*/', '$1', get_bloginfo( 'version' ) ) );
    158158$admin_body_class .= ' admin-color-' . sanitize_html_class( get_user_option( 'admin_color' ), 'fresh' );
    159 $admin_body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_locale() ) ) );
     159$admin_body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_user_locale() ) ) );
    160160
    161161if ( wp_is_mobile() )
    162162        $admin_body_class .= ' mobile';
  • src/wp-admin/includes/class-wp-plugin-install-list-table.php

    diff --git src/wp-admin/includes/class-wp-plugin-install-list-table.php src/wp-admin/includes/class-wp-plugin-install-list-table.php
    index 73df40f..db6c86d 100644
    class WP_Plugin_Install_List_Table extends WP_List_Table { 
    134134                                'active_installs' => true
    135135                        ),
    136136                        // Send the locale and installed plugin slugs to the API so it can provide context-sensitive results.
    137                         'locale' => get_locale(),
     137                        'locale' => get_user_locale(),
    138138                        'installed_plugins' => $this->get_installed_plugin_slugs(),
    139139                );
    140140
  • src/wp-admin/includes/class-wp-press-this.php

    diff --git src/wp-admin/includes/class-wp-press-this.php src/wp-admin/includes/class-wp-press-this.php
    index 3ff242b..e275b16 100644
    class WP_Press_This { 
    13121312        $admin_body_class .= ' branch-' . str_replace( array( '.', ',' ), '-', floatval( $wp_version ) );
    13131313        $admin_body_class .= ' version-' . str_replace( '.', '-', preg_replace( '/^([.0-9]+).*/', '$1', $wp_version ) );
    13141314        $admin_body_class .= ' admin-color-' . sanitize_html_class( get_user_option( 'admin_color' ), 'fresh' );
    1315         $admin_body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_locale() ) ) );
     1315        $admin_body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_user_locale() ) ) );
    13161316
    13171317        /** This filter is documented in wp-admin/admin-header.php */
    13181318        $admin_body_classes = apply_filters( 'admin_body_class', '' );
  • src/wp-admin/includes/credits.php

    diff --git src/wp-admin/includes/credits.php src/wp-admin/includes/credits.php
    index 6634724..005858c 100644
     
    1616 */
    1717function wp_credits() {
    1818        $wp_version = get_bloginfo( 'version' );
    19         $locale = get_locale();
     19        $locale = get_user_locale();
    2020
    2121        $results = get_site_transient( 'wordpress_credits_' . $locale );
    2222
  • src/wp-admin/includes/dashboard.php

    diff --git src/wp-admin/includes/dashboard.php src/wp-admin/includes/dashboard.php
    index f3d2f79..98dfc85 100644
    function wp_dashboard_browser_nag() { 
    13531353                $notice .= "<p class='browser-update-nag{$browser_nag_class}'>{$msg}</p>";
    13541354
    13551355                $browsehappy = 'http://browsehappy.com/';
    1356                 $locale = get_locale();
     1356                $locale = get_user_locale();
    13571357                if ( 'en_US' !== $locale )
    13581358                        $browsehappy = add_query_arg( 'locale', $locale, $browsehappy );
    13591359
  • src/wp-admin/includes/import.php

    diff --git src/wp-admin/includes/import.php src/wp-admin/includes/import.php
    index 4f0b785..0d53442 100644
    function wp_import_handle_upload() { 
    125125function wp_get_popular_importers() {
    126126        include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
    127127
    128         $locale = get_locale();
     128        $locale = get_user_locale();
    129129        $cache_key = 'popular_importers_' . md5( $locale . $wp_version );
    130130        $popular_importers = get_site_transient( $cache_key );
    131131
    132132        if ( ! $popular_importers ) {
    133133                $url = add_query_arg( array(
    134                         'locale'  => get_locale(),
     134                        'locale'  => get_user_locale(),
    135135                        'version' => $wp_version,
    136136                ), 'http://api.wordpress.org/core/importers/1.1/' );
    137137                $options = array( 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url() );
  • src/wp-admin/includes/plugin-install.php

    diff --git src/wp-admin/includes/plugin-install.php src/wp-admin/includes/plugin-install.php
    index a33797e..b0c2e39 100644
    function plugins_api( $action, $args = array() ) { 
    109109        }
    110110
    111111        if ( ! isset( $args->locale ) ) {
    112                 $args->locale = get_locale();
     112                $args->locale = get_user_locale();
    113113        }
    114114
    115115        /**
  • src/wp-admin/includes/template.php

    diff --git src/wp-admin/includes/template.php src/wp-admin/includes/template.php
    index 4eac951..01cb080 100644
    do_action( "admin_head-$hook_suffix" ); 
    16221622/** This action is documented in wp-admin/admin-header.php */
    16231623do_action( 'admin_head' );
    16241624
    1625 $admin_body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_locale() ) ) );
     1625$admin_body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_user_locale() ) ) );
    16261626
    16271627if ( is_rtl() )
    16281628        $admin_body_class .= ' rtl';
  • src/wp-admin/includes/theme.php

    diff --git src/wp-admin/includes/theme.php src/wp-admin/includes/theme.php
    index 95aaec1..b5fc980 100644
    function themes_api( $action, $args = array() ) { 
    412412        }
    413413
    414414        if ( ! isset( $args->locale ) ) {
    415                 $args->locale = get_locale();
     415                $args->locale = get_user_locale();
    416416        }
    417417
    418418        /**
  • src/wp-admin/includes/user.php

    diff --git src/wp-admin/includes/user.php src/wp-admin/includes/user.php
    index fc46dd3..6bf826b 100644
    function edit_user( $user_id = 0 ) { 
    9494                $user->rich_editing = isset( $_POST['rich_editing'] ) && 'false' == $_POST['rich_editing'] ? 'false' : 'true';
    9595                $user->admin_color = isset( $_POST['admin_color'] ) ? sanitize_text_field( $_POST['admin_color'] ) : 'fresh';
    9696                $user->show_admin_bar_front = isset( $_POST['admin_bar_front'] ) ? 'true' : 'false';
     97                $user->locale = '';
     98
     99                if ( isset( $_POST['locale'] ) ) {
     100                        $locale = sanitize_text_field( $_POST['locale'] );
     101                        if ( ! in_array( $locale, get_available_languages(), true ) ) {
     102                                $locale = '';
     103                        }
     104
     105                        $user->locale = ( '' === $locale ) ? 'en_US' : $locale;
     106                }
    97107        }
    98108
    99109        $user->comment_shortcuts = isset( $_POST['comment_shortcuts'] ) && 'true' == $_POST['comment_shortcuts'] ? 'true' : '';
  • src/wp-admin/options.php

    diff --git src/wp-admin/options.php src/wp-admin/options.php
    index f319d52..93c69bc 100644
    if ( 'update' == $action ) { 
    209209                        $value = null;
    210210                        if ( isset( $_POST[ $option ] ) ) {
    211211                                $value = $_POST[ $option ];
    212                                 if ( ! is_array( $value ) )
     212                                if ( ! is_array( $value ) ) {
    213213                                        $value = trim( $value );
     214                                }
    214215                                $value = wp_unslash( $value );
    215216                        }
    216217                        update_option( $option, $value );
    217218                }
    218219
    219220                // Switch translation in case WPLANG was changed.
    220                 $language = get_option( 'WPLANG' );
    221                 if ( $language ) {
    222                         load_default_textdomain( $language );
    223                 } else {
    224                         unload_textdomain( 'default' );
     221                $language      = get_option( 'WPLANG' );
     222                $user_language = get_user_locale();
     223                if ( $language === $user_language ) {
     224                        if ( $language ) {
     225                                load_default_textdomain( $language );
     226                        } else {
     227                                unload_textdomain( 'default' );
     228                        }
    225229                }
    226230        }
    227231
  • src/wp-admin/plugin-editor.php

    diff --git src/wp-admin/plugin-editor.php src/wp-admin/plugin-editor.php
    index 9919005..6225c72 100644
    foreach ( $plugin_files as $plugin_file ) : 
    260260                <input type="hidden" name="scrollto" id="scrollto" value="<?php echo $scrollto; ?>" />
    261261                </div>
    262262                <?php if ( !empty( $docs_select ) ) : ?>
    263                 <div id="documentation" class="hide-if-no-js"><label for="docs-list"><?php _e('Documentation:') ?></label> <?php echo $docs_select ?> <input type="button" class="button" value="<?php esc_attr_e( 'Look Up' ) ?> " onclick="if ( '' != jQuery('#docs-list').val() ) { window.open( 'https://api.wordpress.org/core/handbook/1.0/?function=' + escape( jQuery( '#docs-list' ).val() ) + '&amp;locale=<?php echo urlencode( get_locale() ) ?>&amp;version=<?php echo urlencode( get_bloginfo( 'version' ) ) ?>&amp;redirect=true'); }" /></div>
     263                <div id="documentation" class="hide-if-no-js"><label for="docs-list"><?php _e('Documentation:') ?></label> <?php echo $docs_select ?> <input type="button" class="button" value="<?php esc_attr_e( 'Look Up' ) ?> " onclick="if ( '' != jQuery('#docs-list').val() ) { window.open( 'https://api.wordpress.org/core/handbook/1.0/?function=' + escape( jQuery( '#docs-list' ).val() ) + '&amp;locale=<?php echo urlencode( get_user_locale() ) ?>&amp;version=<?php echo urlencode( get_bloginfo( 'version' ) ) ?>&amp;redirect=true'); }" /></div>
    264264                <?php endif; ?>
    265265<?php if ( is_writeable($real_file) ) : ?>
    266266        <?php if ( in_array( $file, (array) get_option( 'active_plugins', array() ) ) ) { ?>
  • src/wp-admin/theme-editor.php

    diff --git src/wp-admin/theme-editor.php src/wp-admin/theme-editor.php
    index 0b100cf..c4a6207 100644
    else : ?> 
    263263                <div id="documentation" class="hide-if-no-js">
    264264                <label for="docs-list"><?php _e('Documentation:') ?></label>
    265265                <?php echo $docs_select; ?>
    266                 <input type="button" class="button" value="<?php esc_attr_e( 'Look Up' ); ?>" onclick="if ( '' != jQuery('#docs-list').val() ) { window.open( 'https://api.wordpress.org/core/handbook/1.0/?function=' + escape( jQuery( '#docs-list' ).val() ) + '&amp;locale=<?php echo urlencode( get_locale() ) ?>&amp;version=<?php echo urlencode( get_bloginfo( 'version' ) ) ?>&amp;redirect=true'); }" />
     266                <input type="button" class="button" value="<?php esc_attr_e( 'Look Up' ); ?>" onclick="if ( '' != jQuery('#docs-list').val() ) { window.open( 'https://api.wordpress.org/core/handbook/1.0/?function=' + escape( jQuery( '#docs-list' ).val() ) + '&amp;locale=<?php echo urlencode( get_user_locale() ) ?>&amp;version=<?php echo urlencode( get_bloginfo( 'version' ) ) ?>&amp;redirect=true'); }" />
    267267                </div>
    268268        <?php endif; ?>
    269269
  • src/wp-admin/user-edit.php

    diff --git src/wp-admin/user-edit.php src/wp-admin/user-edit.php
    index bb3c62e..3b49a4b 100644
    if ( !( IS_PROFILE_PAGE && !$user_can_edit ) ) : ?> 
    269269</fieldset>
    270270</td>
    271271</tr>
     272
     273<?php
     274$languages = get_available_languages();
     275if ( $languages ) : ?>
     276<tr class="user-language-wrap">
     277        <th scope="row">
     278                <label for="site_language"><?php _e( 'Site Language' ); ?></label>
     279        </th>
     280        <td>
     281                <?php
     282                $user_locale = get_user_option( 'locale', $profileuser->ID );
     283
     284                if ( 'en_US' === $user_locale ) { // en_US
     285                        $user_locale = false;
     286                } elseif ( ! in_array( $user_locale, $languages, true ) ) {
     287                        $user_locale = get_locale();
     288                }
     289
     290                wp_dropdown_languages( array(
     291                        'name'                        => 'locale',
     292                        'id'                          => 'locale',
     293                        'selected'                    => $user_locale,
     294                        'languages'                   => $languages,
     295                        'show_available_translations' => false
     296                ) );
     297                ?>
     298        </td>
     299</tr>
     300<?php
     301endif;
     302?>
     303
    272304<?php
    273305/**
    274306 * Fires at the end of the 'Personal Options' settings table on the user editing screen.
  • src/wp-includes/class-wp-editor.php

    diff --git src/wp-includes/class-wp-editor.php src/wp-includes/class-wp-editor.php
    index 6e6fecc..11075ef 100644
    final class _WP_Editors { 
    351351                        if ( empty( self::$first_init ) ) {
    352352                                self::$baseurl = includes_url( 'js/tinymce' );
    353353
    354                                 $mce_locale = get_locale();
     354                                $mce_locale = get_user_locale();
    355355                                self::$mce_locale = $mce_locale = empty( $mce_locale ) ? 'en' : strtolower( substr( $mce_locale, 0, 2 ) ); // ISO 639-1
    356356
    357357                                /** This filter is documented in wp-admin/includes/media.php */
    final class _WP_Editors { 
    672672                                }
    673673                        }
    674674
    675                         $body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_locale() ) ) );
     675                        $body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_user_locale() ) ) );
    676676
    677677                        if ( !empty($set['tinymce']['body_class']) ) {
    678678                                $body_class .= ' ' . $set['tinymce']['body_class'];
  • src/wp-includes/class-wp-theme.php

    diff --git src/wp-includes/class-wp-theme.php src/wp-includes/class-wp-theme.php
    index 40af39d..8136956 100644
    final class WP_Theme implements ArrayAccess { 
    13951395         * @param array $themes Array of themes to sort, passed by reference.
    13961396         */
    13971397        public static function sort_by_name( &$themes ) {
    1398                 if ( 0 === strpos( get_locale(), 'en_' ) ) {
     1398                if ( 0 === strpos( get_user_locale(), 'en_' ) ) {
    13991399                        uasort( $themes, array( 'WP_Theme', '_name_sort' ) );
    14001400                } else {
    14011401                        uasort( $themes, array( 'WP_Theme', '_name_sort_i18n' ) );
  • src/wp-includes/class-wp-user.php

    diff --git src/wp-includes/class-wp-user.php src/wp-includes/class-wp-user.php
    index bcb82eb..e3c2608 100644
     
    3131 * @property string $display_name
    3232 * @property string $spam
    3333 * @property string $deleted
     34 * @property string $locale
    3435 */
    3536class WP_User {
    3637        /**
  • src/wp-includes/l10n.php

    diff --git src/wp-includes/l10n.php src/wp-includes/l10n.php
    index aaa7611..03a7a11 100644
    function get_locale() { 
    7676}
    7777
    7878/**
     79 * Retrieves the locale of the current user.
     80 *
     81 * If the user has a locale set to a non-empty string then it will be
     82 * returned. Otherwise it returns the locale of get_locale().
     83 *
     84 * @since 4.7.0
     85 *
     86 * @return string The locale of the current user.
     87 */
     88function get_user_locale() {
     89        $user = wp_get_current_user();
     90
     91        $locale = $user->locale;
     92        return ( '' === $locale ) ? get_locale() : $locale;
     93}
     94
     95/**
    7996 * Retrieve the translation of $text.
    8097 *
    8198 * If there is no translation, or the text domain isn't loaded, the original text is returned.
    function unload_textdomain( $domain ) { 
    633650 */
    634651function load_default_textdomain( $locale = null ) {
    635652        if ( null === $locale ) {
    636                 $locale = get_locale();
     653                $locale = is_admin() ? get_user_locale() : get_locale();
    637654        }
    638655
    639656        // Unload previously loaded strings so we can switch translations.
    function is_rtl() { 
    11481165                return false;
    11491166        }
    11501167        return $wp_locale->is_rtl();
    1151 }
    1152  No newline at end of file
     1168}
  • src/wp-includes/user.php

    diff --git src/wp-includes/user.php src/wp-includes/user.php
    index 9ff6455..5641695 100644
    function wp_insert_user( $userdata ) { 
    15921592
    15931593        $meta['show_admin_bar_front'] = empty( $userdata['show_admin_bar_front'] ) ? 'true' : $userdata['show_admin_bar_front'];
    15941594
     1595        $meta['locale'] = isset( $userdata['locale'] ) ? $userdata['locale'] : '';
     1596
    15951597        $user_nicename_check = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1" , $user_nicename, $user_login));
    15961598
    15971599        if ( $user_nicename_check ) {
    function wp_create_user($username, $password, $email = '') { 
    19511953 * @return array List of user keys to be populated in wp_update_user().
    19521954 */
    19531955function _get_additional_user_keys( $user ) {
    1954         $keys = array( 'first_name', 'last_name', 'nickname', 'description', 'rich_editing', 'comment_shortcuts', 'admin_color', 'use_ssl', 'show_admin_bar_front' );
     1956        $keys = array( 'first_name', 'last_name', 'nickname', 'description', 'rich_editing', 'comment_shortcuts', 'admin_color', 'use_ssl', 'show_admin_bar_front', 'locale' );
    19551957        return array_merge( $keys, array_keys( wp_get_user_contact_methods( $user ) ) );
    19561958}
    19571959
  • new file tests/phpunit/tests/l10n/getUserLocale.php

    diff --git tests/phpunit/tests/l10n/getUserLocale.php tests/phpunit/tests/l10n/getUserLocale.php
    new file mode 100644
    index 0000000..1ea3d4b
    - +  
     1<?php
     2
     3/**
     4 * @group l10n
     5 * @group i18n
     6 */
     7class Tests_Get_User_Locale extends WP_UnitTestCase {
     8        protected $user_id;
     9
     10        public function setUp() {
     11                parent::setUp();
     12
     13                $this->user_id = $this->factory()->user->create( array(
     14                        'role'   => 'administrator',
     15                        'locale' => 'de_DE',
     16                ) );
     17
     18                wp_set_current_user( $this->user_id );
     19        }
     20
     21        public function tearDown() {
     22                delete_user_meta( $this->user_id, 'locale' );
     23                set_current_screen( 'front' );
     24
     25                parent::tearDown();
     26        }
     27
     28        public function test_user_locale_property() {
     29                set_current_screen( 'dashboard' );
     30                $this->assertSame( 'de_DE', get_user_locale() );
     31                $this->assertSame( get_user_by( 'id', $this->user_id )->locale, get_user_locale() );
     32        }
     33
     34        public function test_update_user_locale() {
     35                set_current_screen( 'dashboard' );
     36                update_user_meta( $this->user_id, 'locale', 'fr_FR' );
     37                $this->assertSame( 'fr_FR', get_user_locale() );
     38        }
     39
     40        public function test_returns_site_locale_if_empty() {
     41                set_current_screen( 'dashboard' );
     42                update_user_meta( $this->user_id, 'locale', '' );
     43                $this->assertSame( get_locale(), get_user_locale() );
     44        }
     45
     46        public function test_returns_correct_user_locale() {
     47                set_current_screen( 'dashboard' );
     48                $this->assertSame( 'de_DE', get_user_locale() );
     49        }
     50
     51        public function test_returns_correct_user_locale_on_frontend() {
     52                $this->assertSame( 'de_DE', get_user_locale() );
     53        }
     54
     55        public function test_site_locale_is_not_affected() {
     56                set_current_screen( 'dashboard' );
     57                $this->assertSame( 'en_US', get_locale() );
     58        }
     59
     60        public function test_site_locale_is_not_affected_on_frontend() {
     61                $this->assertSame( 'en_US', get_locale() );
     62        }
     63
     64        public function test_user_locale_is_same_across_network() {
     65                if ( ! is_multisite() ) {
     66                        $this->markTestSkipped( __METHOD__ . ' requires multisite' );
     67                }
     68
     69                $user_locale = get_user_locale();
     70
     71                switch_to_blog( self::factory()->blog->create() );
     72                $user_locale_2 = get_user_locale();
     73                restore_current_blog();
     74
     75                $this->assertSame( 'de_DE', $user_locale );
     76                $this->assertSame( $user_locale, $user_locale_2 );
     77        }
     78}