WordPress.org

Make WordPress Core

Ticket #26511: 26511.3.patch

File 26511.3.patch, 8.8 KB (added by tfrommen, 4 years ago)

Refreshed patch for 4.7

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

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
     1<?php
     2/**
     3 * Locale switcher object.
     4 *
     5 * @package WordPress
     6 * @subpackage i18n
     7 * @since 4.7.0
     8 */
     9
     10/**
     11 * Class for switching locales.
     12 *
     13 * @since 4.7.0
     14 */
     15class WP_Locale_Switcher {
     16
     17        /**
     18         * Filter callbacks.
     19         *
     20         * @since 4.7.0
     21         * @var callable[]
     22         */
     23        private $filters = array();
     24
     25        /**
     26         * Locale stack.
     27         *
     28         * @since 4.7.0
     29         * @var string[]
     30         */
     31        private $locales = array();
     32
     33        /**
     34         * Original locale.
     35         *
     36         * @since 4.7.0
     37         * @var string
     38         */
     39        private $original_locale;
     40
     41        /**
     42         * Translation objects.
     43         *
     44         * @since 4.7.0
     45         * @var NOOP_Translations[][]
     46         */
     47        private $translations = array();
     48
     49        /**
     50         * Constructor. Stores the original locale.
     51         *
     52         * @since 4.7.0
     53         *
     54         * @param string $original_locale Optional. The original locale. Defaults to result of get_locale().
     55         */
     56        public function __construct( $original_locale = '' ) {
     57
     58                $this->original_locale = $original_locale ? (string) $original_locale : get_locale();
     59        }
     60
     61        /**
     62         * Switches the translations according to the given locale.
     63         *
     64         * @since 4.7.0
     65         *
     66         * @param string $locale The locale.
     67         */
     68        public function switch_to_locale( $locale ) {
     69
     70                $this->locales[] = $locale;
     71
     72                $current_locale = get_locale();
     73                if ( $current_locale === $locale ) {
     74                        return;
     75                }
     76
     77                /**
     78                 * @global MO[] $l10n
     79                 */
     80                global $l10n;
     81
     82                $textdomains = array_keys( $l10n );
     83
     84                if ( ! $this->has_translations_for_locale( $current_locale ) ) {
     85                        foreach ( $textdomains as $textdomain ) {
     86                                $this->translations[ $current_locale ][ $textdomain ] = get_translations_for_domain( $textdomain );
     87                        }
     88                }
     89
     90                $this->remove_filters();
     91
     92                $this->add_filter_for_locale( $locale );
     93
     94                if ( $this->has_translations_for_locale( $locale ) ) {
     95                        foreach ( $textdomains as $textdomain ) {
     96                                if ( isset( $this->translations[ $locale ][ $textdomain ] ) ) {
     97                                        $l10n[ $textdomain ] = $this->translations[ $locale ][ $textdomain ];
     98                                }
     99                        }
     100                } else {
     101                        foreach ( $l10n as $textdomain => $mo ) {
     102                                if ( 'default' === $textdomain ) {
     103                                        load_default_textdomain();
     104
     105                                        continue;
     106                                }
     107
     108                                unload_textdomain( $textdomain );
     109
     110                                if ( $mofile = $mo->get_filename() ) {
     111                                        load_textdomain( $textdomain, $mofile );
     112                                }
     113
     114                                $this->translations[ $locale ][ $textdomain ] = get_translations_for_domain( $textdomain );
     115                        }
     116                }
     117
     118                /**
     119                 * @global WP_Locale $wp_locale
     120                 */
     121                $GLOBALS['wp_locale'] = new WP_Locale();
     122        }
     123
     124        /**
     125         * Restores the translations according to the previous locale.
     126         *
     127         * @since 4.7.0
     128         *
     129         * @return string|false Locale on success, false on error.
     130         */
     131        public function restore_locale() {
     132
     133                if ( ! array_pop( $this->locales ) ) {
     134                        // The stack is empty, bail.
     135                        return false;
     136                }
     137
     138                $this->remove_filters();
     139
     140                if ( $locale = end( $this->locales ) ) {
     141                        if ( isset( $filters[ $locale ] ) ) {
     142                                add_filter( 'locale', $filters[ $locale ] );
     143                        }
     144                } else {
     145                        // There's nothing left in the stack: go back to the original locale.
     146                        $locale = $this->original_locale;
     147                }
     148
     149                /**
     150                 * @global MO[] $l10n
     151                 */
     152                global $l10n;
     153
     154                foreach ( array_keys( $l10n ) as $textdomain ) {
     155                        if ( isset( $this->translations[ $locale ][ $textdomain ] ) ) {
     156                                $l10n[ $textdomain ] = $this->translations[ $locale ][ $textdomain ];
     157                        }
     158                }
     159
     160                /**
     161                 * @global WP_Locale $wp_locale
     162                 */
     163                $GLOBALS['wp_locale'] = new WP_Locale();
     164
     165                return $locale;
     166        }
     167
     168        /**
     169         * Checks if there are cached translations for the given locale.
     170         *
     171         * @since 4.7.0
     172         *
     173         * @param string $locale The locale.
     174         *
     175         * @return bool True if there are cached translations for the given locale, false otherwise.
     176         */
     177        private function has_translations_for_locale( $locale ) {
     178
     179                return ! empty( $this->translations[ $locale ] );
     180        }
     181
     182        /**
     183         * Removes all filter callbacks added before.
     184         *
     185         * @since 4.7.0
     186         */
     187        private function remove_filters() {
     188
     189                foreach ( $this->filters as $filter ) {
     190                        remove_filter( 'locale', $filter );
     191                }
     192        }
     193
     194        /**
     195         * Adds a filter callback returning the given locale.
     196         *
     197         * @since 4.7.0
     198         *
     199         * @param string $locale The locale.
     200         */
     201        private function add_filter_for_locale( $locale ) {
     202
     203                if ( ! isset( $this->filters[ $locale ] ) ) {
     204                        require_once ABSPATH . WPINC . '/class-wp-locale-storage.php';
     205
     206                        // This SHOULD be a closure.
     207                        $this->filters[ $locale ] = array( new WP_Locale_Storage( $locale ), 'get_locale' );
     208                }
     209
     210                add_filter( 'locale', $this->filters[ $locale ] );
     211        }
     212}
  • src/wp-includes/locale.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    1010/** WP_Locale class */
    1111require_once ABSPATH . WPINC . '/class-wp-locale.php';
    1212
     13/** WP_Locale_Switcher class */
     14require_once ABSPATH . WPINC . '/class-wp-locale-switcher.php';
     15
    1316/**
    1417 * Checks if current locale is RTL.
    1518 *
     
    2225function is_rtl() {
    2326        global $wp_locale;
    2427        return $wp_locale->is_rtl();
     28}
     29
     30/**
     31 * Switches the translations according to the given locale.
     32 *
     33 * @since 4.7.0
     34 *
     35 * @param string $locale The locale.
     36 */
     37function switch_to_locale( $locale ) {
     38
     39        /**
     40         * @global WP_Locale_Switcher $wp_locale_switcher
     41         */
     42        global $wp_locale_switcher;
     43
     44        $wp_locale_switcher->switch_to_locale( $locale );
     45}
     46
     47/**
     48 * Restores the translations according to the previous locale.
     49 *
     50 * @since 4.7.0
     51 *
     52 * @return string|false Locale on success, false on error.
     53 */
     54function restore_locale() {
     55
     56        /**
     57         * @global WP_Locale_Switcher $wp_locale_switcher
     58         */
     59        global $wp_locale_switcher;
     60
     61        return $wp_locale_switcher->restore_locale();
    2562}
  • src/wp-includes/pomo/mo.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    1616        var $_nplurals = 2;
    1717
    1818        /**
     19         * Loaded MO file.
     20         *
     21         * @since 4.7.0
     22         * @var string
     23         */
     24        private $filename = '';
     25
     26        /**
     27         * Returns the loaded MO file.
     28         *
     29         * @since 4.7.0
     30         *
     31         * @return string The loaded MO file.
     32         */
     33        public function get_filename() {
     34
     35                return $this->filename;
     36        }
     37
     38        /**
    1939         * Fills up with the entries from MO file $filename
    2040         *
    2141         * @param string $filename MO file to load
     
    2444                $reader = new POMO_FileReader($filename);
    2545                if (!$reader->is_resource())
    2646                        return false;
     47
     48                $this->filename = (string) $filename;
     49
    2750                return $this->import_from_reader($reader);
    2851        }
    2952
     53 No newline at end of file
  • src/wp-settings.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    379379 */
    380380$GLOBALS['wp_locale'] = new WP_Locale();
    381381
     382/**
     383 * WordPress Locale Switcher object for switching locales.
     384 * @global WP_Locale_Switcher $wp_locale_switcher
     385 * @since 4.7.0
     386 */
     387$GLOBALS['wp_locale_switcher'] = new WP_Locale_Switcher( $locale );
     388
    382389// Load the functions for the active theme, for both parent and child theme if applicable.
    383390if ( ! wp_installing() || 'wp-activate.php' === $pagenow ) {
    384391        if ( TEMPLATEPATH !== STYLESHEETPATH && file_exists( STYLESHEETPATH . '/functions.php' ) )
  • src/wp-includes/class-wp-locale-storage.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
     1<?php
     2/**
     3 * Simple locale storage.
     4 *
     5 * @package WordPress
     6 * @subpackage i18n
     7 * @since 4.7.0
     8 */
     9
     10/**
     11 * Class for locale storage objects with a getter.
     12 *
     13 * @since 4.7.0
     14 */
     15class WP_Locale_Storage {
     16
     17        /**
     18         * The stored locale.
     19         *
     20         * @since 4.7.0
     21         * @var string
     22         */
     23        private $locale;
     24
     25        /**
     26         * Constructor. Stores the given locale.
     27         *
     28         * @since 4.7.0
     29         *
     30         * @param string $locale A locale.
     31         */
     32        public function __construct( $locale ) {
     33
     34                $this->locale = (string) $locale;
     35        }
     36
     37        /**
     38         * Returns the stored locale.
     39         *
     40         * @since 4.7.0
     41         *
     42         * @return string The stored locale.
     43         */
     44        public function get_locale() {
     45
     46                return $this->locale;
     47        }
     48}