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 { |
| 71 | 71 | public function switch_to_locale( $locale ) { |
| 72 | 72 | $current_locale = determine_locale(); |
| 73 | 73 | if ( $current_locale === $locale ) { |
| | 74 | var_dump( compact( 'current_locale', 'locale' ) ); |
| | 75 | echo 'ret early equal <br>'; |
| 74 | 76 | return false; |
| 75 | 77 | } |
| 76 | 78 | |
| 77 | 79 | if ( ! in_array( $locale, $this->available_languages, true ) ) { |
| | 80 | echo 'ret early not avail <br>'; |
| 78 | 81 | return false; |
| 79 | 82 | } |
| 80 | 83 | |
diff --git src/wp-includes/l10n.php src/wp-includes/l10n.php
index 9f211d4dc6..e1bdb73c26 100644
|
|
|
|
| 25 | 25 | * @global string $locale |
| 26 | 26 | * @global string $wp_local_package |
| 27 | 27 | * |
| | 28 | * @param |
| 28 | 29 | * @return string The locale of the blog or from the {@see 'locale'} hook. |
| 29 | 30 | */ |
| 30 | | function get_locale() { |
| | 31 | function get_locale( $use_cache = true ) { |
| 31 | 32 | global $locale, $wp_local_package; |
| 32 | 33 | |
| 33 | | if ( isset( $locale ) ) { |
| | 34 | if ( isset( $locale ) && $use_cache ) { |
| 34 | 35 | /** |
| 35 | 36 | * Filters the locale ID of the WordPress installation. |
| 36 | 37 | * |
| … |
… |
function get_locale() { |
| 41 | 42 | return apply_filters( 'locale', $locale ); |
| 42 | 43 | } |
| 43 | 44 | |
| | 45 | // start modularize ? |
| 44 | 46 | if ( isset( $wp_local_package ) ) { |
| 45 | 47 | $locale = $wp_local_package; |
| 46 | 48 | } |
| … |
… |
function get_locale() { |
| 75 | 77 | if ( empty( $locale ) ) { |
| 76 | 78 | $locale = 'en_US'; |
| 77 | 79 | } |
| | 80 | // end modularize ? |
| 78 | 81 | |
| 79 | 82 | /** This filter is documented in wp-includes/l10n.php */ |
| 80 | 83 | return apply_filters( 'locale', $locale ); |
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 ) { |
| 486 | 486 | * |
| 487 | 487 | * @param int $new_blog_id The ID of the blog to switch to. Default: current blog. |
| 488 | 488 | * @param bool $deprecated Not used. |
| | 489 | * @param what to name it? |
| 489 | 490 | * @return true Always returns true. |
| 490 | 491 | */ |
| 491 | | function switch_to_blog( $new_blog_id, $deprecated = null ) { |
| | 492 | function switch_to_blog( $new_blog_id, $deprecated = null, $options = array() ) { |
| 492 | 493 | global $wpdb; |
| 493 | 494 | |
| | 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 | |
| 494 | 514 | $prev_blog_id = get_current_blog_id(); |
| 495 | 515 | if ( empty( $new_blog_id ) ) { |
| 496 | 516 | $new_blog_id = $prev_blog_id; |
| … |
… |
function switch_to_blog( $new_blog_id, $deprecated = null ) { |
| 514 | 534 | * @param int $prev_blog_id Previous blog ID. |
| 515 | 535 | * @param string $context Additional context. Accepts 'switch' when called from switch_to_blog() |
| 516 | 536 | * or 'restore' when called from restore_current_blog(). |
| | 537 | * @param array $optons explain |
| 517 | 538 | */ |
| 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 ); |
| 519 | 540 | $GLOBALS['switched'] = true; |
| | 541 | // var_dump( $new_blog_id, $prev_blog_id ); |
| | 542 | // die('early'); |
| 520 | 543 | return true; |
| 521 | 544 | } |
| 522 | 545 | |
| … |
… |
function switch_to_blog( $new_blog_id, $deprecated = null ) { |
| 546 | 569 | } |
| 547 | 570 | } |
| 548 | 571 | |
| | 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 | |
| 549 | 589 | /** 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 ); |
| 551 | 591 | $GLOBALS['switched'] = true; |
| 552 | 592 | |
| 553 | 593 | return true; |
| … |
… |
function switch_to_blog( $new_blog_id, $deprecated = null ) { |
| 571 | 611 | function restore_current_blog() { |
| 572 | 612 | global $wpdb; |
| 573 | 613 | |
| | 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 | |
| 574 | 618 | if ( empty( $GLOBALS['_wp_switched_stack'] ) ) { |
| 575 | 619 | return false; |
| 576 | 620 | } |
| … |
… |
function restore_current_blog() { |
| 613 | 657 | } |
| 614 | 658 | } |
| 615 | 659 | |
| | 660 | if ( true ) { // how to determine? |
| | 661 | restore_current_locale(); |
| | 662 | } |
| | 663 | |
| 616 | 664 | /** This filter is documented in wp-includes/ms-blogs.php */ |
| 617 | 665 | do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' ); |
| 618 | 666 | |
diff --git tests/phpunit/tests/multisite/site.php tests/phpunit/tests/multisite/site.php
index f77d425470..77d7ef7f95 100644
|
|
|
if ( is_multisite() ) : |
| 133 | 133 | $this->assertFalse( restore_current_blog() ); |
| 134 | 134 | } |
| 135 | 135 | |
| | 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 | |
| 136 | 169 | /** |
| 137 | 170 | * Test the cache keys and database tables setup through the creation of a site. |
| 138 | 171 | */ |