Ticket #49263: 49263.4.diff
File 49263.4.diff, 9.1 KB (added by , 5 years ago) |
---|
-
src/wp-includes/l10n.php
diff --git src/wp-includes/l10n.php src/wp-includes/l10n.php index 9f211d4dc6..28e6e5774d 100644
28 28 * @return string The locale of the blog or from the {@see 'locale'} hook. 29 29 */ 30 30 function get_locale() { 31 global $locale , $wp_local_package;31 global $locale; 32 32 33 33 if ( isset( $locale ) ) { 34 34 /** … … function get_locale() { 41 41 return apply_filters( 'locale', $locale ); 42 42 } 43 43 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? 52 function _get_raw_site_locale() { 53 global $wp_local_package; 54 44 55 if ( isset( $wp_local_package ) ) { 45 56 $locale = $wp_local_package; 46 57 } … … function get_locale() { 57 68 $ms_locale = get_site_option( 'WPLANG' ); 58 69 } else { 59 70 $ms_locale = get_option( 'WPLANG' ); 71 60 72 if ( false === $ms_locale ) { 61 73 $ms_locale = get_site_option( 'WPLANG' ); 62 74 } … … function get_locale() { 65 77 if ( $ms_locale !== false ) { 66 78 $locale = $ms_locale; 67 79 } 80 68 81 } else { 69 82 $db_locale = get_option( 'WPLANG' ); 83 70 84 if ( $db_locale !== false ) { 71 85 $locale = $db_locale; 72 86 } … … function get_locale() { 76 90 $locale = 'en_US'; 77 91 } 78 92 79 /** This filter is documented in wp-includes/l10n.php */ 80 return apply_filters( 'locale', $locale ); 93 return $locale; 81 94 } 82 95 83 96 /** -
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 ) { 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 // 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 494 528 $prev_blog_id = get_current_blog_id(); 495 529 if ( empty( $new_blog_id ) ) { 496 530 $new_blog_id = $prev_blog_id; … … function switch_to_blog( $new_blog_id, $deprecated = null ) { 514 548 * @param int $prev_blog_id Previous blog ID. 515 549 * @param string $context Additional context. Accepts 'switch' when called from switch_to_blog() 516 550 * or 'restore' when called from restore_current_blog(). 551 * @param array $optons explain 517 552 */ 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 ); 519 554 $GLOBALS['switched'] = true; 520 555 return true; 521 556 } … … function switch_to_blog( $new_blog_id, $deprecated = null ) { 546 581 } 547 582 } 548 583 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 549 593 /** 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 ); 551 595 $GLOBALS['switched'] = true; 552 596 553 597 return true; … … function restore_current_blog() { 613 657 } 614 658 } 615 659 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 616 678 /** This filter is documented in wp-includes/ms-blogs.php */ 617 679 do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' ); 618 680 -
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() ) : 133 133 $this->assertFalse( restore_current_blog() ); 134 134 } 135 135 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 136 218 /** 137 219 * Test the cache keys and database tables setup through the creation of a site. 138 220 */