Making WordPress.org


Ignore:
Timestamp:
02/15/2018 10:26:52 AM (7 years ago)
Author:
obenland
Message:

Locale detector: Save locale in cookie

Also switches to using the actual list of available locales.

Props ocean90.
See #1673.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/locale-detection/class-detector.php

    r6638 r6640  
    4242     */
    4343    public function __construct() {
    44         $this->active_locales = array_merge(
    45             $this->active_locales,
    46             (array) glob( get_template_directory() . '/languages/*.mo' ),
    47             (array) glob( get_stylesheet_directory() . '/languages/*.mo' )
    48         );
    49         foreach ( $this->active_locales as &$mo_file ) {
    50             $mo_file = basename( $mo_file, '.mo' );
     44        $this->active_locales = $this->get_active_locales();
     45
     46        if ( ! empty( $_COOKIE[ 'wporg_locale' ] ) ) {
     47            $locale = $this->sanitize_locale( $_COOKIE['wporg_locale'] );
     48
     49            if ( in_array( $locale, $this->active_locales, true ) ) {
     50                $this->locale = $locale;
     51                return;
     52            }
    5153        }
    52         unset( $mo_file );
    5354
    5455        if ( isset( $_GET['locale'] ) ) {
    55             $get_locale = preg_replace( '/[^A-Z_-]/i', '', $_GET['locale'] );
     56            $get_locale = $this->sanitize_locale( $_GET['locale'] );
    5657
    5758            $this->locale = $this->check_variants( $get_locale ) ?: $this->locale;
     
    5960            $this->locale = $this->guess_locale() ?: $this->locale;
    6061        }
     62
     63        setcookie( 'wporg_locale', $this->locale, time() + YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN, is_ssl() );
    6164    }
    6265
     
    6871    public function get_locale() {
    6972        return $this->locale;
     73    }
     74
     75    /**
     76     * Returns the list of available locales.
     77     *
     78     * @return array
     79     */
     80    public function get_active_locales() {
     81        wp_cache_add_global_groups( [ 'locale-associations' ] );
     82
     83        $locales = wp_cache_get( 'locale-list', 'locale-associations' );
     84        if ( false === $locales ) {
     85            $locales = (array) $GLOBALS['wpdb']->get_col( 'SELECT locale FROM wporg_locales' );
     86            wp_cache_set( 'locale-list', $locales, 'locale-associations' );
     87        }
     88
     89        return $locales;
    7090    }
    7191
     
    165185        return '';
    166186    }
     187
     188    /**
     189     * Returns a valid locale string.
     190     *
     191     * @param string $locale Locale string to be sanitized.
     192     * @return string
     193     */
     194    protected function sanitize_locale( $locale ) {
     195        return preg_replace( '/[^a-zA-Z0-9_]/', '', $locale );
     196    }
    167197}
Note: See TracChangeset for help on using the changeset viewer.