Ticket #26511: 26511.18.1.diff
File 26511.18.1.diff, 35.8 KB (added by , 8 years ago) |
---|
-
src/wp-admin/includes/ms.php
272 272 ); 273 273 update_option( 'adminhash', $new_admin_email ); 274 274 275 $switched_locale = switch_to_locale( get_user_locale() ); 276 275 277 /* translators: Do not translate USERNAME, ADMIN_URL, EMAIL, SITENAME, SITEURL: those are placeholders. */ 276 278 $email_text = __( 'Howdy ###USERNAME###, 277 279 … … 315 317 $content = str_replace( '###SITEURL###', network_home_url(), $content ); 316 318 317 319 wp_mail( $value, sprintf( __( '[%s] New Admin Email Address' ), wp_specialchars_decode( get_option( 'blogname' ) ) ), $content ); 320 321 if ( $switched_locale ) { 322 restore_previous_locale(); 323 } 318 324 } 319 325 320 326 /** … … 353 359 ); 354 360 update_user_meta( $current_user->ID, '_new_email', $new_user_email ); 355 361 362 $switched_locale = switch_to_locale( get_user_locale() ); 363 356 364 /* translators: Do not translate USERNAME, ADMIN_URL, EMAIL, SITENAME, SITEURL: those are placeholders. */ 357 365 $email_text = __( 'Howdy ###USERNAME###, 358 366 … … 395 403 396 404 wp_mail( $_POST['email'], sprintf( __( '[%s] New Email Address' ), wp_specialchars_decode( get_option( 'blogname' ) ) ), $content ); 397 405 $_POST['email'] = $current_user->user_email; 406 407 if ( $switched_locale ) { 408 restore_previous_locale(); 409 } 398 410 } 399 411 } 400 412 -
src/wp-admin/includes/upgrade.php
372 372 $email = $user->user_email; 373 373 $name = $user->user_login; 374 374 $login_url = wp_login_url(); 375 376 $switched_locale = switch_to_locale( $user->locale ); 377 375 378 $message = sprintf( __( "Your new WordPress site has been successfully set up at: 376 379 377 380 %1\$s … … 389 392 "), $blog_url, $name, $password, $login_url ); 390 393 391 394 @wp_mail($email, __('New WordPress Site'), $message); 395 396 if ( $switched_locale ) { 397 restore_previous_locale(); 398 } 392 399 } 393 400 endif; 394 401 -
src/wp-admin/ms-delete-site.php
42 42 43 43 $url_delete = esc_url( admin_url( 'ms-delete-site.php?h=' . $hash ) ); 44 44 45 $switched_locale = switch_to_locale( get_locale() ); 46 45 47 /* translators: Do not translate USERNAME, URL_DELETE, SITE_NAME: those are placeholders. */ 46 48 $content = __( "Howdy ###USERNAME###, 47 49 … … 73 75 $content = str_replace( '###SITE_NAME###', get_network()->site_name, $content ); 74 76 75 77 wp_mail( get_option( 'admin_email' ), "[ " . wp_specialchars_decode( get_option( 'blogname' ) ) . " ] ".__( 'Delete My Site' ), $content ); 78 79 if ( $switched_locale ) { 80 restore_previous_locale(); 81 } 76 82 ?> 77 83 78 84 <p><?php _e( 'Thank you. Please check your email for a link to confirm your action. Your site will not be deleted until this link is clicked.' ) ?></p> -
src/wp-admin/user-new.php
87 87 */ 88 88 do_action( 'invite_user', $user_id, $role, $newuser_key ); 89 89 90 $switched_locale = switch_to_locale( $user_details->locale ); 91 90 92 /* translators: 1: Site name, 2: site URL, 3: role, 4: activation URL */ 91 93 $message = __( 'Hi, 92 94 … … 96 98 Please click the following link to confirm the invite: 97 99 %4$s' ); 98 100 wp_mail( $new_user_email, sprintf( __( '[%s] Joining confirmation' ), wp_specialchars_decode( get_option( 'blogname' ) ) ), sprintf( $message, get_option( 'blogname' ), home_url(), wp_specialchars_decode( translate_user_role( $role['name'] ) ), home_url( "/newbloguser/$newuser_key/" ) ) ); 101 102 if ( $switched_locale ) { 103 restore_previous_locale(); 104 } 105 99 106 $redirect = add_query_arg( array('update' => 'add'), 'user-new.php' ); 100 107 } 101 108 } -
src/wp-includes/class-wp-locale-switcher.php
1 <?php 2 /** 3 * Locale API: WP_Locale_Switcher class 4 * 5 * @package WordPress 6 * @subpackage i18n 7 * @since 4.7.0 8 */ 9 10 /** 11 * Core class used for switching locales. 12 * 13 * @since 4.7.0 14 */ 15 class WP_Locale_Switcher { 16 /** 17 * Locale stack. 18 * 19 * @since 4.7.0 20 * @access private 21 * @var string[] 22 */ 23 private $locales = array(); 24 25 /** 26 * Original locale. 27 * 28 * @since 4.7.0 29 * @access private 30 * @var string 31 */ 32 private $original_locale; 33 34 /** 35 * Holds all available languages. 36 * 37 * @since 4.7.0 38 * @access private 39 * @var array An array of language codes (file names without the .mo extension). 40 */ 41 private $available_languages = array(); 42 43 /** 44 * Constructor. 45 * 46 * Stores the original locale as well as a list of all available languages. 47 * 48 * @since 4.7.0 49 */ 50 public function __construct() { 51 $this->original_locale = is_admin() ? get_user_locale() : get_locale(); 52 $this->available_languages = array_merge( array( 'en_US' ), get_available_languages() ); 53 } 54 55 /** 56 * Initializes the locale switcher. 57 * 58 * Hooks into the {@see 'locale'} filter to change the locale on the fly. 59 */ 60 public function init() { 61 add_filter( 'locale', array( $this, 'filter_locale' ) ); 62 } 63 64 /** 65 * Switches the translations according to the given locale. 66 * 67 * @since 4.7.0 68 * 69 * @param string $locale The locale to switch to. 70 * @return bool True on success, false on failure. 71 */ 72 public function switch_to_locale( $locale ) { 73 $current_locale = is_admin() ? get_user_locale() : get_locale(); 74 if ( $current_locale === $locale ) { 75 return false; 76 } 77 78 if ( ! in_array( $locale, $this->available_languages, true ) ) { 79 return false; 80 } 81 82 $this->locales[] = $locale; 83 84 $this->change_locale( $locale ); 85 86 /** 87 * Fires when the locale is switched. 88 * 89 * @since 4.7.0 90 * 91 * @param string $locale The new locale. 92 */ 93 do_action( 'switch_locale', $locale ); 94 95 return true; 96 } 97 98 /** 99 * Restores the translations according to the previous locale. 100 * 101 * @since 4.7.0 102 * 103 * @return string|false Locale on success, false on failure. 104 */ 105 public function restore_previous_locale() { 106 $previous_locale = array_pop( $this->locales ); 107 108 if ( null === $previous_locale ) { 109 // The stack is empty, bail. 110 return false; 111 } 112 113 $locale = end( $this->locales ); 114 115 if ( ! $locale ) { 116 // There's nothing left in the stack: go back to the original locale. 117 $locale = $this->original_locale; 118 } 119 120 $this->change_locale( $locale ); 121 122 /** 123 * Fires when the locale is restored to the previous one. 124 * 125 * @since 4.7.0 126 * 127 * @param string $locale The new locale. 128 * @param string $previous_locale The previous locale. 129 */ 130 do_action( 'restore_previous_locale', $locale, $previous_locale ); 131 132 return $locale; 133 } 134 135 /** 136 * Restores the translations according to the original locale. 137 * 138 * @since 4.7.0 139 * 140 * @return string|false Locale on success, false on failure. 141 */ 142 public function restore_current_locale() { 143 if ( empty( $this->locales ) ) { 144 return false; 145 } 146 147 $this->locales = array( $this->original_locale ); 148 149 return $this->restore_previous_locale(); 150 } 151 152 /** 153 * Whether switch_to_locale() is in effect. 154 * 155 * @since 4.7.0 156 * 157 * @return bool True if the locale has been switched, false otherwise. 158 */ 159 public function is_switched() { 160 return ! empty( $this->locales ); 161 } 162 163 /** 164 * Filters the WordPress install's locale. 165 * 166 * @since 4.7.0 167 * 168 * @param string $locale The WordPress install's locale. 169 * @return string The locale currently being switched to. 170 */ 171 public function filter_locale( $locale ) { 172 $switched_locale = end( $this->locales ); 173 174 if ( $switched_locale ) { 175 return $switched_locale; 176 } 177 178 return $locale; 179 } 180 181 /** 182 * Load translations for a given locale. 183 * 184 * When switching to a locale, translations for this locale must be loaded from scratch. 185 * 186 * @since 4.7.0 187 * @access private 188 * 189 * @global Mo[] $l10n An array of all currently loaded text domains. 190 * 191 * @param string $locale The locale to load translations for. 192 */ 193 private function load_translations( $locale ) { 194 global $l10n; 195 196 $domains = $l10n ? array_keys( $l10n ) : array(); 197 198 load_default_textdomain( $locale ); 199 200 foreach ( $domains as $domain ) { 201 if ( 'default' === $domain ) { 202 continue; 203 } 204 205 $mofile = $l10n[ $domain ]->get_filename(); 206 207 unload_textdomain( $domain ); 208 209 if ( $mofile ) { 210 load_textdomain( $domain, $mofile ); 211 } 212 213 get_translations_for_domain( $domain ); 214 } 215 } 216 217 /** 218 * Changes the site's locale to the given one. 219 * 220 * Loads the translations, changes the global `$wp_locale` object and updates 221 * all post type labels. 222 * 223 * @since 4.7.0 224 * @access private 225 * 226 * @global WP_Locale $wp_locale The WordPress date and time locale object. 227 * 228 * @param string $locale The locale to change to. 229 */ 230 private function change_locale( $locale ) { 231 $this->load_translations( $locale ); 232 233 $GLOBALS['wp_locale'] = new WP_Locale(); 234 235 /** 236 * Fires when the locale is switched to or restored. 237 * 238 * @since 4.7.0 239 * 240 * @param string $locale The new locale. 241 */ 242 do_action( 'change_locale', $locale ); 243 } 244 } -
src/wp-includes/default-filters.php
406 406 add_action( 'admin_menu', '_add_post_type_submenus' ); 407 407 add_action( 'before_delete_post', '_reset_front_page_settings_for_post' ); 408 408 add_action( 'wp_trash_post', '_reset_front_page_settings_for_post' ); 409 add_action( 'change_locale', 'create_initial_post_types' ); 409 410 410 411 // Post Formats 411 412 add_filter( 'request', '_post_format_request' ); … … 431 432 432 433 // Taxonomy 433 434 add_action( 'init', 'create_initial_taxonomies', 0 ); // highest priority 435 add_action( 'change_locale', 'create_initial_taxonomies' ); 434 436 435 437 // Canonical 436 438 add_action( 'template_redirect', 'redirect_canonical' ); -
src/wp-includes/l10n.php
1166 1166 } 1167 1167 return $wp_locale->is_rtl(); 1168 1168 } 1169 1170 /** 1171 * Switches the translations according to the given locale. 1172 * 1173 * @since 4.7.0 1174 * 1175 * @global WP_Locale_Switcher $wp_locale_switcher 1176 * 1177 * @param string $locale The locale. 1178 * @return bool True on success, false on failure. 1179 */ 1180 function switch_to_locale( $locale ) { 1181 /* @var WP_Locale_Switcher $wp_locale_switcher */ 1182 global $wp_locale_switcher; 1183 1184 return $wp_locale_switcher->switch_to_locale( $locale ); 1185 } 1186 1187 /** 1188 * Restores the translations according to the previous locale. 1189 * 1190 * @since 4.7.0 1191 * 1192 * @global WP_Locale_Switcher $wp_locale_switcher 1193 * 1194 * @return string|false Locale on success, false on error. 1195 */ 1196 function restore_previous_locale() { 1197 /* @var WP_Locale_Switcher $wp_locale_switcher */ 1198 global $wp_locale_switcher; 1199 1200 return $wp_locale_switcher->restore_previous_locale(); 1201 } 1202 1203 /** 1204 * Restores the translations according to the original locale. 1205 * 1206 * @since 4.7.0 1207 * 1208 * @global WP_Locale_Switcher $wp_locale_switcher 1209 * 1210 * @return string|false Locale on success, false on error. 1211 */ 1212 function restore_current_locale() { 1213 /* @var WP_Locale_Switcher $wp_locale_switcher */ 1214 global $wp_locale_switcher; 1215 1216 return $wp_locale_switcher->restore_current_locale(); 1217 } 1218 1219 /** 1220 * Whether switch_to_locale() is in effect. 1221 * 1222 * @since 4.7.0 1223 * 1224 * @global WP_Locale_Switcher $wp_locale_switcher 1225 * 1226 * @return bool True if the locale has been switched, false otherwise. 1227 */ 1228 function is_locale_switched() { 1229 /* @var WP_Locale_Switcher $wp_locale_switcher */ 1230 global $wp_locale_switcher; 1231 1232 return $wp_locale_switcher->is_switched(); 1233 } -
src/wp-includes/load.php
841 841 * @since 3.4.0 842 842 * @access private 843 843 * 844 * @global string $text_direction 845 * @global WP_Locale $wp_locale The WordPress date and time locale object. 844 * @global string $text_direction 845 * @global WP_Locale $wp_locale The WordPress date and time locale object. 846 * @global WP_Locale_Switcher $wp_locale_switcher WordPress locale switcher object. 846 847 * 847 848 * @staticvar bool $loaded 848 849 */ 849 850 function wp_load_translations_early() { 850 global $text_direction, $wp_locale ;851 global $text_direction, $wp_locale, $wp_locale_switcher; 851 852 852 853 static $loaded = false; 853 854 if ( $loaded ) … … 864 865 require_once ABSPATH . WPINC . '/pomo/mo.php'; 865 866 require_once ABSPATH . WPINC . '/l10n.php'; 866 867 require_once ABSPATH . WPINC . '/class-wp-locale.php'; 868 require_once ABSPATH . WPINC . '/class-wp-locale-switcher.php'; 867 869 868 870 // General libraries 869 871 require_once ABSPATH . WPINC . '/plugin.php'; … … 915 917 } 916 918 917 919 $wp_locale = new WP_Locale(); 920 $wp_locale_switcher = new WP_Locale_Switcher(); 921 $wp_locale_switcher->init(); 918 922 } 919 923 920 924 /** -
src/wp-includes/ms-functions.php
800 800 $admin_email = 'support@' . $_SERVER['SERVER_NAME']; 801 801 $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) ); 802 802 $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n"; 803 804 $user = get_user_by( 'login', $user ); 805 $switched_locale = switch_to_locale( $user && $user->locale ? $user->locale : get_locale() ); 806 803 807 $message = sprintf( 804 808 /** 805 809 * Filters the message content of the new blog notification email. … … 849 853 esc_url( 'http://' . $domain . $path ) 850 854 ); 851 855 wp_mail( $user_email, wp_specialchars_decode( $subject ), $message, $message_headers ); 856 857 if ( $switched_locale ) { 858 restore_previous_locale(); 859 } 860 852 861 return true; 853 862 } 854 863 … … 887 896 if ( ! apply_filters( 'wpmu_signup_user_notification', $user, $user_email, $key, $meta ) ) 888 897 return false; 889 898 899 $user = get_user_by( 'login', $user ); 900 $switched_locale = switch_to_locale( $user && $user->locale ? $user->locale : get_locale() ); 901 890 902 // Send email with activation link. 891 903 $admin_email = get_site_option( 'admin_email' ); 892 904 if ( $admin_email == '' ) … … 934 946 $user 935 947 ); 936 948 wp_mail( $user_email, wp_specialchars_decode( $subject ), $message, $message_headers ); 949 950 if ( $switched_locale ) { 951 restore_previous_locale(); 952 } 953 937 954 return true; 938 955 } 939 956 … … 1448 1465 if ( ! apply_filters( 'wpmu_welcome_notification', $blog_id, $user_id, $password, $title, $meta ) ) 1449 1466 return false; 1450 1467 1468 $user = get_userdata( $user_id ); 1469 1470 $switched_locale = switch_to_locale( $user->locale ); 1471 1451 1472 $welcome_email = get_site_option( 'welcome_email' ); 1452 1473 if ( $welcome_email == false ) { 1453 1474 /* translators: Do not translate USERNAME, SITE_NAME, BLOG_URL, PASSWORD: those are placeholders. */ … … 1468 1489 } 1469 1490 1470 1491 $url = get_blogaddress_by_id($blog_id); 1471 $user = get_userdata( $user_id );1472 1492 1473 1493 $welcome_email = str_replace( 'SITE_NAME', $current_network->site_name, $welcome_email ); 1474 1494 $welcome_email = str_replace( 'BLOG_TITLE', $title, $welcome_email ); … … 1512 1532 */ 1513 1533 $subject = apply_filters( 'update_welcome_subject', sprintf( __( 'New %1$s Site: %2$s' ), $current_network->site_name, wp_unslash( $title ) ) ); 1514 1534 wp_mail( $user->user_email, wp_specialchars_decode( $subject ), $message, $message_headers ); 1535 1536 if ( $switched_locale ) { 1537 restore_previous_locale(); 1538 } 1539 1515 1540 return true; 1516 1541 } 1517 1542 … … 1551 1576 1552 1577 $user = get_userdata( $user_id ); 1553 1578 1579 $switched_locale = switch_to_locale( $user->locale ); 1580 1554 1581 /** 1555 1582 * Filters the content of the welcome email after user activation. 1556 1583 * … … 1590 1617 */ 1591 1618 $subject = apply_filters( 'update_welcome_user_subject', sprintf( __( 'New %1$s User: %2$s' ), $current_network->site_name, $user->user_login) ); 1592 1619 wp_mail( $user->user_email, wp_specialchars_decode( $subject ), $message, $message_headers ); 1620 1621 if ( $switched_locale ) { 1622 restore_previous_locale(); 1623 } 1624 1593 1625 return true; 1594 1626 } 1595 1627 -
src/wp-includes/pluggable.php
211 211 // (Re)create it, if it's gone missing 212 212 if ( ! ( $phpmailer instanceof PHPMailer ) ) { 213 213 require_once ABSPATH . WPINC . '/class-phpmailer.php'; 214 require_once ABSPATH . WPINC . '/class-smtp.php'; 214 require_once ABSPATH . WPINC . '/class-smtp.php'; 215 215 $phpmailer = new PHPMailer( true ); 216 216 } 217 217 … … 1418 1418 $emails = array_flip( $emails ); 1419 1419 } 1420 1420 1421 $switched_locale = switch_to_locale( get_locale() ); 1422 1421 1423 $comment_author_domain = @gethostbyaddr($comment->comment_author_IP); 1422 1424 1423 1425 // The blogname option is escaped with esc_html on the way into the database in sanitize_option … … 1522 1524 @wp_mail( $email, wp_specialchars_decode( $subject ), $notify_message, $message_headers ); 1523 1525 } 1524 1526 1527 if ( $switched_locale ) { 1528 restore_previous_locale(); 1529 } 1530 1525 1531 return true; 1526 1532 } 1527 1533 endif; … … 1569 1575 $emails[] = $user->user_email; 1570 1576 } 1571 1577 1578 $switched_locale = switch_to_locale( get_locale() ); 1579 1572 1580 $comment_author_domain = @gethostbyaddr($comment->comment_author_IP); 1573 1581 $comments_waiting = $wpdb->get_var("SELECT count(comment_ID) FROM $wpdb->comments WHERE comment_approved = '0'"); 1574 1582 … … 1664 1672 @wp_mail( $email, wp_specialchars_decode( $subject ), $notify_message, $message_headers ); 1665 1673 } 1666 1674 1675 if ( $switched_locale ) { 1676 restore_previous_locale(); 1677 } 1678 1667 1679 return true; 1668 1680 } 1669 1681 endif; … … 1723 1735 $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); 1724 1736 1725 1737 if ( 'user' !== $notify ) { 1738 $switched_locale = switch_to_locale( get_locale() ); 1726 1739 $message = sprintf( __( 'New user registration on your site %s:' ), $blogname ) . "\r\n\r\n"; 1727 1740 $message .= sprintf( __( 'Username: %s' ), $user->user_login ) . "\r\n\r\n"; 1728 1741 $message .= sprintf( __( 'Email: %s' ), $user->user_email ) . "\r\n"; 1729 1742 1730 1743 @wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] New User Registration' ), $blogname ), $message ); 1744 1745 if ( $switched_locale ) { 1746 restore_previous_locale(); 1747 } 1731 1748 } 1732 1749 1733 1750 // `$deprecated was pre-4.3 `$plaintext_pass`. An empty `$plaintext_pass` didn't sent a user notifcation. … … 1748 1765 $hashed = time() . ':' . $wp_hasher->HashPassword( $key ); 1749 1766 $wpdb->update( $wpdb->users, array( 'user_activation_key' => $hashed ), array( 'user_login' => $user->user_login ) ); 1750 1767 1768 $switched_locale = switch_to_locale( $user->locale ); 1769 1751 1770 $message = sprintf(__('Username: %s'), $user->user_login) . "\r\n\r\n"; 1752 1771 $message .= __('To set your password, visit the following address:') . "\r\n\r\n"; 1753 1772 $message .= '<' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user->user_login), 'login') . ">\r\n\r\n"; … … 1755 1774 $message .= wp_login_url() . "\r\n"; 1756 1775 1757 1776 wp_mail($user->user_email, sprintf(__('[%s] Your username and password info'), $blogname), $message); 1777 1778 if ( $switched_locale ) { 1779 restore_previous_locale(); 1780 } 1758 1781 } 1759 1782 endif; 1760 1783 -
src/wp-includes/pomo/mo.php
16 16 var $_nplurals = 2; 17 17 18 18 /** 19 * Loaded MO file. 20 * 21 * @var string 22 */ 23 private $filename = ''; 24 25 /** 26 * Returns the loaded MO file. 27 * 28 * @return string The loaded MO file. 29 */ 30 public function get_filename() { 31 return $this->filename; 32 } 33 34 /** 19 35 * Fills up with the entries from MO file $filename 20 36 * 21 37 * @param string $filename MO file to load 22 38 */ 23 39 function import_from_file($filename) { 24 $reader = new POMO_FileReader($filename); 25 if (!$reader->is_resource()) 40 $reader = new POMO_FileReader( $filename ); 41 42 if ( ! $reader->is_resource() ) { 26 43 return false; 27 return $this->import_from_reader($reader); 44 } 45 46 $this->filename = (string) $filename; 47 48 return $this->import_from_reader( $reader ); 28 49 } 29 50 30 51 /** … … 299 320 return $this->_nplurals; 300 321 } 301 322 } 302 endif; 303 No newline at end of file 323 endif; -
src/wp-includes/user.php
1801 1801 1802 1802 $blog_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); 1803 1803 1804 $switched_locale = false; 1805 if ( ! empty( $send_password_change_email ) || ! empty( $send_email_change_email ) ) { 1806 $switched_locale = switch_to_locale( isset( $user['locale'] ) ? $user['locale'] : get_locale() ); 1807 } 1808 1804 1809 if ( ! empty( $send_password_change_email ) ) { 1805 1806 1810 /* translators: Do not translate USERNAME, ADMIN_EMAIL, EMAIL, SITENAME, SITEURL: those are placeholders. */ 1807 1811 $pass_change_text = __( 'Hi ###USERNAME###, 1808 1812 … … 1910 1914 1911 1915 wp_mail( $email_change_email['to'], sprintf( $email_change_email['subject'], $blog_name ), $email_change_email['message'], $email_change_email['headers'] ); 1912 1916 } 1917 1918 if ( $switched_locale ) { 1919 restore_previous_locale(); 1920 } 1913 1921 } 1914 1922 1915 1923 // Update the cookies if the password changed. -
src/wp-settings.php
130 130 // Load the L10n library. 131 131 require_once( ABSPATH . WPINC . '/l10n.php' ); 132 132 require_once( ABSPATH . WPINC . '/class-wp-locale.php' ); 133 require_once( ABSPATH . WPINC . '/class-wp-locale-switcher.php' ); 133 134 134 135 // Run the installer if WordPress is not installed. 135 136 wp_not_installed(); … … 400 401 */ 401 402 $GLOBALS['wp_locale'] = new WP_Locale(); 402 403 404 /** 405 * WordPress Locale Switcher object for switching locales. 406 * 407 * @since 4.7.0 408 * 409 * @global WP_Locale_Switcher $wp_locale_switcher WordPress locale switcher object. 410 */ 411 $GLOBALS['wp_locale_switcher'] = new WP_Locale_Switcher(); 412 $GLOBALS['wp_locale_switcher']->init(); 413 403 414 // Load the functions for the active theme, for both parent and child theme if applicable. 404 415 if ( ! wp_installing() || 'wp-activate.php' === $pagenow ) { 405 416 if ( TEMPLATEPATH !== STYLESHEETPATH && file_exists( STYLESHEETPATH . '/functions.php' ) ) -
tests/phpunit/tests/l10n/loadTextdomainJustInTime.php
9 9 private $orig_theme_dir; 10 10 private $theme_root; 11 11 12 function setUp() {12 public function setUp() { 13 13 parent::setUp(); 14 14 15 15 $this->theme_root = DIR_TESTDATA . '/themedir1'; … … 22 22 add_filter( 'template_root', array( $this, 'filter_theme_root' ) ); 23 23 wp_clean_themes_cache(); 24 24 unset( $GLOBALS['wp_themes'] ); 25 25 unset( $GLOBALS['l10n'] ); 26 26 unset( $GLOBALS['l10n_unloaded'] ); 27 27 } 28 28 29 function tearDown() {29 public function tearDown() { 30 30 $GLOBALS['wp_theme_directories'] = $this->orig_theme_dir; 31 31 remove_filter( 'theme_root', array( $this, 'filter_theme_root' ) ); 32 32 remove_filter( 'stylesheet_root', array( $this, 'filter_theme_root' ) ); … … 33 33 remove_filter( 'template_root', array( $this, 'filter_theme_root' ) ); 34 34 wp_clean_themes_cache(); 35 35 unset( $GLOBALS['wp_themes'] ); 36 unset( $GLOBALS['l10n'] ); 37 unset( $GLOBALS['l10n_unloaded'] ); 36 38 37 39 parent::tearDown(); 38 40 } … … 60 62 $expected_output = i18n_plugin_test(); 61 63 $is_textdomain_loaded_after = is_textdomain_loaded( 'internationalized-plugin' ); 62 64 63 unload_textdomain( 'internationalized-plugin' );64 65 remove_filter( 'locale', array( $this, 'filter_set_locale_to_german' ) ); 65 66 66 67 $this->assertFalse( $is_textdomain_loaded_before ); … … 82 83 $expected_output = i18n_theme_test(); 83 84 $is_textdomain_loaded_after = is_textdomain_loaded( 'internationalized-theme' ); 84 85 85 unload_textdomain( 'internationalized-theme' );86 86 remove_filter( 'locale', array( $this, 'filter_set_locale_to_german' ) ); 87 87 88 88 $this->assertFalse( $is_textdomain_loaded_before ); … … 141 141 $this->assertSame( 'Das ist ein Dummy Plugin', $expected_output_final ); 142 142 $this->assertTrue( $is_textdomain_loaded_final ); 143 143 } 144 145 /** 146 * @ticket 26511 147 */ 148 public function test_plugin_translation_after_switching_locale() { 149 require_once DIR_TESTDATA . '/plugins/internationalized-plugin.php'; 150 151 switch_to_locale( 'de_DE' ); 152 $expected = i18n_plugin_test(); 153 restore_previous_locale(); 154 155 $this->assertSame( 'Das ist ein Dummy Plugin', $expected ); 156 } 157 158 /** 159 * @ticket 26511 160 */ 161 public function test_theme_translation_after_switching_locale() { 162 switch_theme( 'internationalized-theme' ); 163 164 require_once get_stylesheet_directory() . '/functions.php'; 165 166 switch_to_locale( 'de_DE' ); 167 $expected = i18n_theme_test(); 168 restore_previous_locale(); 169 170 switch_theme( WP_DEFAULT_THEME ); 171 172 $this->assertSame( 'Das ist ein Dummy Theme', $expected ); 173 } 144 174 } -
tests/phpunit/tests/l10n/localeSwitcher.php
1 <?php 2 3 /** 4 * @group l10n 5 * @group i18n 6 * @group 26511 7 */ 8 class Tests_Locale_Switcher extends WP_UnitTestCase { 9 /** 10 * @var string 11 */ 12 protected $locale = ''; 13 14 /** 15 * @var string 16 */ 17 protected $previous_locale = ''; 18 19 public function setUp() { 20 parent::setUp(); 21 22 $this->locale = ''; 23 $this->previous_locale = ''; 24 25 unset( $GLOBALS['l10n'] ); 26 unset( $GLOBALS['l10n_unloaded'] ); 27 } 28 29 public function tearDown() { 30 unset( $GLOBALS['l10n'] ); 31 unset( $GLOBALS['l10n_unloaded'] ); 32 33 parent::tearDown(); 34 } 35 36 public function test_switch_to_non_existent_locale_returns_false() { 37 $this->assertFalse( switch_to_locale( 'foo_BAR' ) ); 38 } 39 40 public function test_switch_to_non_existent_locale_does_not_change_locale() { 41 switch_to_locale( 'foo_BAR' ); 42 43 $this->assertSame( 'en_US', get_locale() ); 44 } 45 46 public function test_switch_to_locale_returns_true() { 47 $expected = switch_to_locale( 'en_GB' ); 48 49 // Cleanup. 50 restore_previous_locale(); 51 52 $this->assertTrue( $expected ); 53 } 54 55 public function test_switch_to_locale_changes_the_locale() { 56 switch_to_locale( 'en_GB' ); 57 58 $locale = get_locale(); 59 60 // Cleanup. 61 restore_previous_locale(); 62 63 $this->assertSame( 'en_GB', $locale ); 64 } 65 66 public function test_switch_to_locale_loads_translation() { 67 switch_to_locale( 'es_ES' ); 68 69 $actual = __( 'Invalid parameter.' ); 70 71 // Cleanup. 72 restore_previous_locale(); 73 74 $this->assertSame( 'Parámetro no válido. ', $actual ); 75 } 76 77 public function test_switch_to_locale_changes_wp_locale_global() { 78 global $wp_locale; 79 80 $expected = array( 81 'thousands_sep' => '.', 82 'decimal_point' => ',', 83 ); 84 85 switch_to_locale( 'de_DE' ); 86 87 $wp_locale_de_DE = clone $wp_locale; 88 89 // Cleanup. 90 restore_previous_locale(); 91 92 $this->assertEqualSetsWithIndex( $expected, $wp_locale_de_DE->number_format ); 93 } 94 95 public function test_switch_to_locale_en_US() { 96 switch_to_locale( 'en_GB' ); 97 $locale_en_GB = get_locale(); 98 switch_to_locale( 'en_US' ); 99 $locale_en_US = get_locale(); 100 101 // Cleanup. 102 restore_current_locale(); 103 104 $this->assertSame( 'en_GB', $locale_en_GB ); 105 $this->assertSame( 'en_US', $locale_en_US ); 106 } 107 108 public function test_switch_to_locale_multiple_times() { 109 switch_to_locale( 'en_GB' ); 110 switch_to_locale( 'es_ES' ); 111 $locale = get_locale(); 112 113 // Cleanup. 114 restore_previous_locale(); 115 restore_previous_locale(); 116 117 $this->assertSame( 'es_ES', $locale ); 118 } 119 120 public function test_switch_to_locale_multiple_times_loads_translation() { 121 switch_to_locale( 'en_GB' ); 122 switch_to_locale( 'de_DE' ); 123 switch_to_locale( 'es_ES' ); 124 125 $actual = __( 'Invalid parameter.' ); 126 127 // Cleanup. 128 restore_previous_locale(); 129 restore_previous_locale(); 130 restore_previous_locale(); 131 132 $this->assertSame( 'Parámetro no válido. ', $actual ); 133 } 134 135 public function test_restore_previous_locale_without_switching() { 136 $this->assertFalse( restore_previous_locale() ); 137 } 138 139 public function test_restore_previous_locale_changes_the_locale_back() { 140 switch_to_locale( 'en_GB' ); 141 142 // Cleanup. 143 restore_previous_locale(); 144 145 $this->assertSame( 'en_US', get_locale() ); 146 } 147 148 public function test_restore_previous_locale_after_switching_multiple_times() { 149 switch_to_locale( 'en_GB' ); 150 switch_to_locale( 'es_ES' ); 151 restore_previous_locale(); 152 153 $locale = get_locale(); 154 155 // Cleanup. 156 restore_previous_locale(); 157 158 $this->assertSame( 'en_GB', $locale ); 159 } 160 161 public function test_restore_previous_locale_restores_translation() { 162 switch_to_locale( 'es_ES' ); 163 restore_previous_locale(); 164 165 $actual = __( 'Invalid parameter.' ); 166 167 $this->assertSame( 'Invalid parameter.', $actual ); 168 } 169 170 public function test_restore_previous_locale_action_passes_previous_locale() { 171 switch_to_locale( 'en_GB' ); 172 switch_to_locale( 'es_ES' ); 173 174 add_action( 'restore_previous_locale', array( $this, 'store_locale' ), 10, 2 ); 175 176 restore_previous_locale(); 177 178 $previous_locale = $this->previous_locale; 179 180 // Cleanup. 181 restore_previous_locale(); 182 183 $this->assertSame( 'es_ES', $previous_locale ); 184 } 185 186 public function test_restore_previous_locale_restores_wp_locale_global() { 187 global $wp_locale; 188 189 $expected = array( 190 'thousands_sep' => ',', 191 'decimal_point' => '.', 192 ); 193 194 switch_to_locale( 'de_DE' ); 195 restore_previous_locale(); 196 197 $this->assertEqualSetsWithIndex( $expected, $wp_locale->number_format ); 198 } 199 200 public function test_restore_current_locale_without_switching() { 201 $this->assertFalse( restore_current_locale() ); 202 } 203 204 public function test_restore_current_locale_after_switching_multiple_times() { 205 switch_to_locale( 'en_GB' ); 206 switch_to_locale( 'nl_NL' ); 207 switch_to_locale( 'es_ES' ); 208 209 restore_current_locale(); 210 211 $this->assertSame( 'en_US', get_locale() ); 212 } 213 214 public function store_locale( $locale, $previous_locale ) { 215 $this->locale = $locale; 216 $this->previous_locale = $previous_locale; 217 } 218 219 public function test_is_locale_switched_if_not_switched() { 220 $this->assertFalse( is_locale_switched() ); 221 } 222 223 public function test_is_locale_switched_original_locale() { 224 $original_locale = get_locale(); 225 226 switch_to_locale( 'en_GB' ); 227 switch_to_locale( $original_locale ); 228 229 $is_locale_switched = is_locale_switched(); 230 231 restore_current_locale(); 232 233 $this->assertTrue( $is_locale_switched ); 234 } 235 236 public function test_is_locale_switched() { 237 switch_to_locale( 'en_GB' ); 238 switch_to_locale( 'nl_NL' ); 239 240 $is_locale_switched = is_locale_switched(); 241 242 restore_current_locale(); 243 244 $this->assertTrue( $is_locale_switched ); 245 } 246 247 public function test_switch_to_site_locale_if_user_locale_is_set() { 248 global $l10n, $wp_locale_switcher; 249 250 $site_locale = get_locale(); 251 252 $user_id = $this->factory()->user->create( array( 253 'role' => 'administrator', 254 'locale' => 'de_DE', 255 ) ); 256 257 wp_set_current_user( $user_id ); 258 set_current_screen( 'dashboard' ); 259 260 $locale_switcher = clone $wp_locale_switcher; 261 262 $wp_locale_switcher = new WP_Locale_Switcher(); 263 $wp_locale_switcher->init(); 264 265 $user_locale = get_user_locale(); 266 267 $this->assertSame( 'de_DE', $user_locale ); 268 269 load_default_textdomain( $user_locale ); 270 $language_header_before_switch = $l10n['default']->headers['Language']; // de_DE 271 272 $locale_switched_user_locale = switch_to_locale( $user_locale ); // False. 273 $locale_switched_site_locale = switch_to_locale( $site_locale ); // True. 274 $site_locale_after_switch = get_locale(); 275 $language_header_after_switch = isset( $l10n['default'] ); // en_US 276 277 restore_current_locale(); 278 279 $language_header_after_restore = $l10n['default']->headers['Language']; // de_DE 280 281 $wp_locale_switcher = $locale_switcher; 282 283 set_current_screen( 'front' ); 284 285 $this->assertFalse( $locale_switched_user_locale ); 286 $this->assertTrue( $locale_switched_site_locale ); 287 $this->assertSame( $site_locale, $site_locale_after_switch ); 288 $this->assertSame( 'de_DE', $language_header_before_switch ); 289 $this->assertFalse( $language_header_after_switch ); 290 $this->assertSame( 'de_DE', $language_header_after_restore ); 291 } 292 293 public function test_switch_to_different_site_locale_if_user_locale_is_set() { 294 global $l10n, $wp_locale_switcher; 295 296 // Change site locale to es_ES. 297 add_filter( 'locale', array( $this, 'filter_locale' ) ); 298 299 $site_locale = get_locale(); 300 301 $user_id = $this->factory()->user->create( array( 302 'role' => 'administrator', 303 'locale' => 'de_DE', 304 ) ); 305 306 wp_set_current_user( $user_id ); 307 set_current_screen( 'dashboard' ); 308 309 $locale_switcher = clone $wp_locale_switcher; 310 311 $wp_locale_switcher = new WP_Locale_Switcher(); 312 $wp_locale_switcher->init(); 313 314 $user_locale = get_user_locale(); 315 316 $this->assertSame( 'de_DE', $user_locale ); 317 318 load_default_textdomain( $user_locale ); 319 $language_header_before_switch = $l10n['default']->headers['Language']; // de_DE 320 321 $locale_switched_user_locale = switch_to_locale( $user_locale ); // False. 322 $locale_switched_site_locale = switch_to_locale( $site_locale ); // True. 323 $site_locale_after_switch = get_locale(); 324 $language_header_after_switch = $l10n['default']->headers['Language']; // es_ES 325 326 restore_current_locale(); 327 328 $language_header_after_restore = $l10n['default']->headers['Language']; // de_DE 329 330 $wp_locale_switcher = $locale_switcher; 331 332 set_current_screen( 'front' ); 333 334 remove_filter( 'locale', array( $this, 'filter_locale' ) ); 335 336 $this->assertFalse( $locale_switched_user_locale ); 337 $this->assertTrue( $locale_switched_site_locale ); 338 $this->assertSame( $site_locale, $site_locale_after_switch ); 339 $this->assertSame( 'de_DE', $language_header_before_switch ); 340 $this->assertSame( 'es_ES', $language_header_after_switch ); 341 $this->assertSame( 'de_DE', $language_header_after_restore ); 342 } 343 344 public function test_multiple_switches_to_site_locale_and_user_locale() { 345 global $wp_locale_switcher; 346 347 $site_locale = get_locale(); 348 349 $user_id = $this->factory()->user->create( array( 350 'role' => 'administrator', 351 'locale' => 'en_GB', 352 ) ); 353 354 wp_set_current_user( $user_id ); 355 set_current_screen( 'dashboard' ); 356 357 $locale_switcher = clone $wp_locale_switcher; 358 359 $wp_locale_switcher = new WP_Locale_Switcher(); 360 $wp_locale_switcher->init(); 361 362 $user_locale = get_user_locale(); 363 364 load_default_textdomain( $user_locale ); 365 366 require_once DIR_TESTDATA . '/plugins/internationalized-plugin.php'; 367 368 switch_to_locale( 'de_DE' ); 369 switch_to_locale( $site_locale ); 370 371 $expected = i18n_plugin_test(); 372 373 restore_current_locale(); 374 375 $wp_locale_switcher = $locale_switcher; 376 377 set_current_screen( 'front' ); 378 379 $this->assertSame( 'en_US', get_locale() ); 380 $this->assertSame( 'This is a dummy plugin', $expected ); 381 } 382 383 public function filter_locale() { 384 return 'es_ES'; 385 } 386 } -
tests/phpunit/tests/locale.php
1 1 <?php 2 2 3 3 /** 4 * @group l ocale4 * @group l10n 5 5 * @group i18n 6 6 */ 7 7 class Tests_Locale extends WP_UnitTestCase {