Make WordPress Core

Ticket #49263: 49263.2.diff

File 49263.2.diff, 6.1 KB (added by iandunn, 5 years ago)

Modularized get_locale() into _get_raw_site_locale()

  • src/wp-includes/l10n.php

     
    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                /**
     
    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        }
     
    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                        }
     
    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                }
     
    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

     
    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        // better name - flags? switches? what_to_switch? there's gotta be something more descriptive
     496
     497        $default_options = array(
     498                'blog_id'      => true, // any valid use case for this being false?
     499                'table_prefix' => true, // any valid use case for this being false?
     500                'object_cache' => true,
     501                'locale'       => false,
     502        );
     503
     504        // add filter for default options too?
     505
     506        // document filter
     507        $options = apply_filters(
     508                'switch_blog_options',
     509                wp_parse_args( $options, $default_options )
     510        );
     511
     512        // add conditions around everything based on existing options
     513
    494514        $prev_blog_id = get_current_blog_id();
    495515        if ( empty( $new_blog_id ) ) {
    496516                $new_blog_id = $prev_blog_id;
     
    514534                 * @param int    $prev_blog_id Previous blog ID.
    515535                 * @param string $context      Additional context. Accepts 'switch' when called from switch_to_blog()
    516536                 *                             or 'restore' when called from restore_current_blog().
     537                 * @param array $optons        explain
    517538                 */
    518                 do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' );
     539                do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch', $options );
    519540                $GLOBALS['switched'] = true;
    520541                return true;
    521542        }
     
    546567                }
    547568        }
    548569
     570        $switcher_initialized = isset( $GLOBALS['wp_locale_switcher'] );
     571                // also check $GLOBALS['wp_locale_switcher'] instanceof WP_Locale_Switcher ?
     572                // make DRY w/ restore_current_blog()
     573
     574        if ( true === $options['locale'] && $switcher_initialized ) {
     575                $new_locale = _get_raw_site_locale();
     576                switch_to_locale( $new_locale );
     577        }
     578
    549579        /** This filter is documented in wp-includes/ms-blogs.php */
    550         do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' );
     580        do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch', $options );
    551581        $GLOBALS['switched'] = true;
    552582
    553583        return true;
     
    613643                }
    614644        }
    615645
     646        $should_restore = true;
     647                // how to determine? always true? only if the corresponding switch_to_blog() call passed locale=>true ? any other concerns?
     648                // or just automatically restonre what the last switch enabled, but do automatically w/out asking ?
     649        $switcher_initialized = isset( $GLOBALS['wp_locale_switcher'] );
     650                // also check $GLOBALS['wp_locale_switcher'] instanceof WP_Locale_Switcher ?
     651
     652        if ( $should_restore && $switcher_initialized ) {
     653                // should check is_locale_switched() as well?
     654                restore_current_locale();
     655        }
     656
    616657        /** This filter is documented in wp-includes/ms-blogs.php */
    617658        do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' );
    618659
  • tests/phpunit/tests/multisite/site.php

     
    133133                        $this->assertFalse( restore_current_blog() );
    134134                }
    135135
     136                // document
     137
    136138                /**
     139                 *
     140                 * @group switch_to_blog
     141                 */
     142                function test_switch_blog_locale() {
     143                        // maybe need data provider to toggle all the individual flags
     144                        $this->assertTrue( is_main_site() );
     145                        $this->assertEquals( 'en_US', get_locale() );
     146                        $this->assertEquals( 'en_US', get_user_locale() );
     147
     148                        $blog_id = self::factory()->blog->create();
     149                        // set locale to es_MX ; need to modify factory to allow passing that to constructor, b/c doing switch_to_blog and update_option() would kinda default the point?
     150
     151                        // tes probably failing b/c not auto copying files over
     152
     153                        switch_to_blog( $blog_id, null, array( 'locale' => false ) );
     154
     155                        $this->assertEquals( 'en_US', get_locale() );
     156                        // test actually translating a string?
     157
     158                        // handle plugin/theme textdomains
     159
     160                        restore_current_blog();
     161
     162                        switch_to_blog( $blog_id, null, array( 'locale' => true ) );
     163
     164                        $this->assertEquals( 'es_MX', get_locale() );
     165                        $this->assertEquals( 'es_MX', get_user_locale() );
     166                        // test actually translating a string?
     167
     168                        // handle plugin/theme textdomains
     169
     170                        restore_current_blog();
     171
     172
     173                        // test w/ user_locale on/off?
     174                        // test during installation? at least manually
     175                        // test plugin textdomain?
     176                }
     177
     178                /**
    137179                 * Test the cache keys and database tables setup through the creation of a site.
    138180                 */
    139181                function test_created_site_details() {