Make WordPress Core

Ticket #49263: 49263.4.diff

File 49263.4.diff, 9.1 KB (added by iandunn, 5 years ago)

Minor notes

  • src/wp-includes/l10n.php

    diff --git src/wp-includes/l10n.php src/wp-includes/l10n.php
    index 9f211d4dc6..28e6e5774d 100644
     
    2828 * @return string The locale of the blog or from the {@see 'locale'} hook.
    2929 */
    3030function get_locale() {
    31         global $locale, $wp_local_package;
     31        global $locale;
    3232
    3333        if ( isset( $locale ) ) {
    3434                /**
    function get_locale() { 
    4141                return apply_filters( 'locale', $locale );
    4242        }
    4343
     44        $locale = _get_raw_site_locale();
     45
     46        /** This filter is documented in wp-includes/l10n.php */
     47        return apply_filters( 'locale', $locale );
     48}
     49
     50// todo explain internal and should not be used by plugins?
     51// or let it be external? no harm?
     52function _get_raw_site_locale() {
     53        global $wp_local_package;
     54
    4455        if ( isset( $wp_local_package ) ) {
    4556                $locale = $wp_local_package;
    4657        }
    function get_locale() { 
    5768                        $ms_locale = get_site_option( 'WPLANG' );
    5869                } else {
    5970                        $ms_locale = get_option( 'WPLANG' );
     71
    6072                        if ( false === $ms_locale ) {
    6173                                $ms_locale = get_site_option( 'WPLANG' );
    6274                        }
    function get_locale() { 
    6577                if ( $ms_locale !== false ) {
    6678                        $locale = $ms_locale;
    6779                }
     80
    6881        } else {
    6982                $db_locale = get_option( 'WPLANG' );
     83
    7084                if ( $db_locale !== false ) {
    7185                        $locale = $db_locale;
    7286                }
    function get_locale() { 
    7690                $locale = 'en_US';
    7791        }
    7892
    79         /** This filter is documented in wp-includes/l10n.php */
    80         return apply_filters( 'locale', $locale );
     93        return $locale;
    8194}
    8295
    8396/**
  • src/wp-includes/ms-blogs.php

    diff --git src/wp-includes/ms-blogs.php src/wp-includes/ms-blogs.php
    index 184953b4a8..90d39f7d52 100644
    function update_blog_option( $id, $option, $value, $deprecated = null ) { 
    486486 *
    487487 * @param int  $new_blog_id The ID of the blog to switch to. Default: current blog.
    488488 * @param bool $deprecated  Not used.
     489 * @param what to name it?
    489490 * @return true Always returns true.
    490491 */
    491 function switch_to_blog( $new_blog_id, $deprecated = null ) {
     492function switch_to_blog( $new_blog_id, $deprecated = null, $options = array() ) {
    492493        global $wpdb;
    493494
     495        // what did deprecated arg use to be? why isn't there a _deprecated_parametr() call here?
     496                // https://core.trac.wordpress.org/changeset/21485
     497        // if it used to be an int or something, then could do something like
     498        // if ! array then deprecated_paramters()
     499
     500        // if pass in bool, then depreated warning
     501        // else, convert to array to make sure
     502        // or maybe just if they pass in anything besides an array?
     503        // search plugin dir to get rough idea of current usage
     504                // ag "switch_to_blog\(\s?(.*)," --php ./plugins/ | tee scans/switch-to-blog.txt
     505                //> cat scans/switch-to-blog.txt |grep -v "freemius" > scans/switch-to-blog-not-freemius.txt
     506
     507        // better name - flags? switches? what_to_switch? there's gotta be something more descriptive
     508
     509        $default_options = array(
     510                'blog_id'      => true, // any valid use case for this being false?
     511                'table_prefix' => true, // any valid use case for this being false?
     512                'object_cache' => true,
     513                'locale'       => false,
     514        );
     515
     516        // add filter for default options too?
     517
     518        // document filter
     519        $options = apply_filters(
     520                'switch_blog_options',
     521                wp_parse_args( $options, $default_options )
     522        );
     523
     524        // add conditions around everything based on existing options
     525                // er, that may be getting out of scope for this ticket
     526                // maybe just do locale for this ticket, and then open new ticket to add additional options
     527
    494528        $prev_blog_id = get_current_blog_id();
    495529        if ( empty( $new_blog_id ) ) {
    496530                $new_blog_id = $prev_blog_id;
    function switch_to_blog( $new_blog_id, $deprecated = null ) { 
    514548                 * @param int    $prev_blog_id Previous blog ID.
    515549                 * @param string $context      Additional context. Accepts 'switch' when called from switch_to_blog()
    516550                 *                             or 'restore' when called from restore_current_blog().
     551                 * @param array $optons        explain
    517552                 */
    518                 do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' );
     553                do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch', $options );
    519554                $GLOBALS['switched'] = true;
    520555                return true;
    521556        }
    function switch_to_blog( $new_blog_id, $deprecated = null ) { 
    546581                }
    547582        }
    548583
     584        $switcher_initialized = isset( $GLOBALS['wp_locale_switcher'] );
     585                // also check $GLOBALS['wp_locale_switcher'] instanceof WP_Locale_Switcher ?
     586                // make DRY w/ restore_current_blog()
     587
     588        if ( true === $options['locale'] && $switcher_initialized ) {
     589                $new_locale = _get_raw_site_locale();
     590                switch_to_locale( $new_locale );
     591        }
     592
    549593        /** This filter is documented in wp-includes/ms-blogs.php */
    550         do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' );
     594        do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch', $options );
    551595        $GLOBALS['switched'] = true;
    552596
    553597        return true;
    function restore_current_blog() { 
    613657                }
    614658        }
    615659
     660        $should_restore = true;
     661                // how to determine? always true? only if the corresponding switch_to_blog() call passed locale=>true ? any other concerns?
     662                // or just automatically restonre what the last switch enabled, but do automatically w/out asking ?
     663        $switcher_initialized = isset( $GLOBALS['wp_locale_switcher'] );
     664                // also check $GLOBALS['wp_locale_switcher'] instanceof WP_Locale_Switcher ?
     665
     666        if ( $should_restore && $switcher_initialized ) {
     667                if ( is_locale_switched() ) {
     668                        // ^ probably isn't good enough, b/c that only checks if the locale has been switched, NOT if it was switched by the corresponding switch_to_blog() call
     669                                // could be situations where there are nested blog switches, and sometimes locale is switched and sometimes it's not
     670                                // so i think this could pop a locale off the stack when it shouldn't
     671                                // add a unit test for that situation
     672                        // what's an elegant solution to that?
     673                                // maybe need to track it in a global or something? hopefully there's a better way
     674                        restore_current_locale();
     675                }
     676        }
     677
    616678        /** This filter is documented in wp-includes/ms-blogs.php */
    617679        do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' );
    618680
  • tests/phpunit/tests/multisite/site.php

    diff --git tests/phpunit/tests/multisite/site.php tests/phpunit/tests/multisite/site.php
    index 5afc0adf73..7864cc3c6f 100644
    if ( is_multisite() ) : 
    133133                        $this->assertFalse( restore_current_blog() );
    134134                }
    135135
     136                /**
     137                 *
     138                 * look up guidelines for phpdoc etc
     139                 *
     140                 * @dataProvider data_switch_blog_locale
     141                 *
     142                 * @covers switch_to_blog
     143                 *
     144                 *                       'blog_id'                     => $japanese_blog_id,
     145                                        'get_locale_options'          => array(),
     146                                        'expected_raw_site_locale'    => 'ja_JP',
     147                                        'expected_cached_site_locale' => 'en_US',
     148                 */
     149                function test_switch_blog_locale( $switch_blog_id, $switch_blog_arguments, $expected_raw_locale, $expected_cached_locale ) {
     150                        // Make sure starting assumptions are correct.
     151                        $this->assertTrue( is_main_site() );
     152                        $this->assertEquals( 'en_US', _get_raw_site_locale() );
     153                        $this->assertEquals( 'en_US', get_locale() );
     154
     155                        // Make sure switched properly.
     156                        switch_to_blog( $switch_blog_id, null, $switch_blog_arguments );
     157                        $this->assertEquals( $expected_raw_locale, _get_raw_site_locale() );
     158                        $this->assertEquals( $expected_cached_locale, get_locale() );
     159                        restore_current_blog();
     160
     161                        // Make sure restored properly.
     162                                // thss is probably redundant b/c will be checked on the next call, so can probably remove this
     163                        $this->assertTrue( is_main_site() );
     164                        $this->assertEquals( 'en_US', _get_raw_site_locale() );
     165                        $this->assertEquals( 'en_US', get_locale() );
     166
     167
     168                        // test actually translating a string everywhere?
     169                        // test during installation? at least manually
     170                }
     171
     172                /**
     173                 * Data provider for
     174                 *
     175                 *
     176                 * list args here maybe
     177                 */
     178                function data_switch_blog_locale() {
     179                        $japanese_blog_id = self::factory()->blog->create(
     180                                array(
     181                                        'options' => array( 'WPLANG' => 'ja_JP' ),
     182                                )
     183                        );
     184                        // should create ^ in class setUp()? but only used for this one function. function gets called many times though
     185                        // maybe create in test_switch_blog_locale, but then how to put id in provided data down here?
     186
     187                        // also create a german blog so can test non-english -> non-english
     188
     189
     190                        // identify the bug you're seeing in mu-plugin tests, and test for that here
     191
     192
     193                        $cases = array(
     194                                // Default behavior, locale not switched
     195                                array(
     196                                        'blog_id'                     => $japanese_blog_id,
     197                                        'get_locale_options'          => array(),
     198                                        'expected_raw_site_locale'    => 'ja_JP',
     199                                        'expected_cached_site_locale' => 'en_US',
     200                                ),
     201
     202                                // Switch the locale
     203                                array(
     204                                        'blog_id'                     => $japanese_blog_id,
     205                                        'get_locale_options'          => array( 'locale' => true ),
     206                                        'expected_raw_site_locale'    => 'ja_JP',
     207                                        'expected_cached_site_locale' => 'ja_JP',
     208                                ),
     209                        );
     210
     211                        // test switching english -> nonenglish, non-english->english, nonenglish -> nonenglish
     212
     213                        // programatically copy all those tests and repeat them w/ user locale enabled
     214
     215                        return $cases;
     216                }
     217
    136218                /**
    137219                 * Test the cache keys and database tables setup through the creation of a site.
    138220                 */