WordPress.org

Make WordPress Core

Ticket #49263: 49263.1.diff

File 49263.1.diff, 6.6 KB (added by iandunn, 8 months ago)

rough WIP exploring approaches

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

    diff --git src/wp-includes/class-wp-locale-switcher.php src/wp-includes/class-wp-locale-switcher.php
    index 7ec7622161..a62aa7a7f5 100644
    class WP_Locale_Switcher { 
    7171        public function switch_to_locale( $locale ) {
    7272                $current_locale = determine_locale();
    7373                if ( $current_locale === $locale ) {
     74                        var_dump( compact( 'current_locale', 'locale' ) );
     75                        echo 'ret early equal <br>';
    7476                        return false;
    7577                }
    7678
    7779                if ( ! in_array( $locale, $this->available_languages, true ) ) {
     80                        echo 'ret early not avail <br>';
    7881                        return false;
    7982                }
    8083
  • src/wp-includes/l10n.php

    diff --git src/wp-includes/l10n.php src/wp-includes/l10n.php
    index 9f211d4dc6..e1bdb73c26 100644
     
    2525 * @global string $locale
    2626 * @global string $wp_local_package
    2727 *
     28 * @param
    2829 * @return string The locale of the blog or from the {@see 'locale'} hook.
    2930 */
    30 function get_locale() {
     31function get_locale( $use_cache = true ) {
    3132        global $locale, $wp_local_package;
    3233
    33         if ( isset( $locale ) ) {
     34        if ( isset( $locale ) && $use_cache ) {
    3435                /**
    3536                 * Filters the locale ID of the WordPress installation.
    3637                 *
    function get_locale() { 
    4142                return apply_filters( 'locale', $locale );
    4243        }
    4344
     45        // start modularize ?
    4446        if ( isset( $wp_local_package ) ) {
    4547                $locale = $wp_local_package;
    4648        }
    function get_locale() { 
    7577        if ( empty( $locale ) ) {
    7678                $locale = 'en_US';
    7779        }
     80        // end modularize ?
    7881
    7982        /** This filter is documented in wp-includes/l10n.php */
    8083        return apply_filters( 'locale', $locale );
  • src/wp-includes/ms-blogs.php

    diff --git src/wp-includes/ms-blogs.php src/wp-includes/ms-blogs.php
    index 25dbe4b29f..992aceb69b 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        // 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?
     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;
    function switch_to_blog( $new_blog_id, $deprecated = null ) { 
    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;
     541//              var_dump( $new_blog_id, $prev_blog_id );
     542//              die('early');
    520543                return true;
    521544        }
    522545
    function switch_to_blog( $new_blog_id, $deprecated = null ) { 
    546569                }
    547570        }
    548571
     572        if ( true === $options['locale'] ) {
     573                $new_locale = get_locale( false );
     574                // doing this resets global $locale, so determine_locale() thinks its equal and switch_to_locale() returns early.
     575                // could add a $force param to switch_locale(), or pass param to get_locale() to tell it to not update global $locale, but not very elegant
     576                // maybe modularize the get_locale logic so you can reuse the inner stuff w/out setting the global var?
     577
     578                // could also add a `locale` filter and then remove it, but not very elegant
     579
     580                // modularizing get_locale seems like might be better approach - _get_fresh_locale() or _get_raw_locale()
     581                        // should be internal?
     582
     583
     584
     585
     586                switch_to_locale( $new_locale );
     587        }
     588
    549589        /** This filter is documented in wp-includes/ms-blogs.php */
    550         do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' );
     590        do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch', $options );
    551591        $GLOBALS['switched'] = true;
    552592
    553593        return true;
    function switch_to_blog( $new_blog_id, $deprecated = null ) { 
    571611function restore_current_blog() {
    572612        global $wpdb;
    573613
     614        // need a param here to determine what to restore?
     615        // or just restore everything? - can't b/c
     616        // or just automatically restonre what the last switch enabled, but do automatically w/out asking ?
     617
    574618        if ( empty( $GLOBALS['_wp_switched_stack'] ) ) {
    575619                return false;
    576620        }
    function restore_current_blog() { 
    613657                }
    614658        }
    615659
     660        if ( true ) { // how to determine?
     661                restore_current_locale();
     662        }
     663
    616664        /** This filter is documented in wp-includes/ms-blogs.php */
    617665        do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' );
    618666
  • tests/phpunit/tests/multisite/site.php

    diff --git tests/phpunit/tests/multisite/site.php tests/phpunit/tests/multisite/site.php
    index f77d425470..77d7ef7f95 100644
    if ( is_multisite() ) : 
    133133                        $this->assertFalse( restore_current_blog() );
    134134                }
    135135
     136                // document
     137
     138                /**
     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
     154                        switch_to_blog( $blog_id, null, array( 'locale' => true ) );
     155
     156                        $this->assertEquals( 'es_MX', get_locale() );
     157                        $this->assertEquals( 'es_MX', get_user_locale() );
     158                        // test actually translating a string?
     159
     160                        // handle plugin/theme textdomains
     161
     162                        restore_current_blog();
     163
     164
     165                        // test w/ user_locale on/off?
     166                        // test during installation? at least manually
     167                }
     168
    136169                /**
    137170                 * Test the cache keys and database tables setup through the creation of a site.
    138171                 */