Changeset 55161 for trunk/src/wp-includes/class-wp-locale-switcher.php
- Timestamp:
- 01/30/2023 10:25:53 AM (22 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-locale-switcher.php
r55010 r55161 16 16 class WP_Locale_Switcher { 17 17 /** 18 * Locale s tack.19 * 20 * @since 4.7.0 21 * @var string[]22 */ 23 private $ locales= array();18 * Locale switching stack. 19 * 20 * @since 4.7.0 21 * @var array 22 */ 23 private $stack = array(); 24 24 25 25 /** … … 54 54 * Initializes the locale switcher. 55 55 * 56 * Hooks into the {@see 'locale'} filter to change the locale on the fly. 56 * Hooks into the {@see 'locale'} and {@see 'determine_locale'} filters 57 * to change the locale on the fly. 57 58 * 58 59 * @since 4.7.0 … … 60 61 public function init() { 61 62 add_filter( 'locale', array( $this, 'filter_locale' ) ); 63 add_filter( 'determine_locale', array( $this, 'filter_locale' ) ); 62 64 } 63 65 … … 67 69 * @since 4.7.0 68 70 * 69 * @param string $locale The locale to switch to. 71 * @param string $locale The locale to switch to. 72 * @param int|false $user_id Optional. User ID as context. Default false. 70 73 * @return bool True on success, false on failure. 71 74 */ 72 public function switch_to_locale( $locale ) {75 public function switch_to_locale( $locale, $user_id = false ) { 73 76 $current_locale = determine_locale(); 74 77 if ( $current_locale === $locale ) { … … 80 83 } 81 84 82 $this-> locales[] = $locale;85 $this->stack[] = array( $locale, $user_id ); 83 86 84 87 $this->change_locale( $locale ); … … 88 91 * 89 92 * @since 4.7.0 90 * 91 * @param string $locale The new locale. 93 * @since 6.2.0 The `$user_id` parameter was added. 94 * 95 * @param string $locale The new locale. 96 * @param null|int $user_id User ID for context if available. 92 97 */ 93 do_action( 'switch_locale', $locale );98 do_action( 'switch_locale', $locale, $user_id ); 94 99 95 100 return true; … … 97 102 98 103 /** 104 * Switches the translations according to the given user's locale. 105 * 106 * @since 6.2.0 107 * 108 * @param int $user_id User ID. 109 * @return bool True on success, false on failure. 110 */ 111 public function switch_to_user_locale( $user_id ) { 112 $locale = get_user_locale( $user_id ); 113 return $this->switch_to_locale( $locale, $user_id ); 114 } 115 116 /** 99 117 * Restores the translations according to the previous locale. 100 118 * … … 104 122 */ 105 123 public function restore_previous_locale() { 106 $previous_locale = array_pop( $this-> locales);124 $previous_locale = array_pop( $this->stack ); 107 125 108 126 if ( null === $previous_locale ) { … … 111 129 } 112 130 113 $locale = end( $this->locales ); 131 $entry = end( $this->stack ); 132 $locale = is_array( $entry ) ? $entry[0] : false; 114 133 115 134 if ( ! $locale ) { … … 128 147 * @param string $previous_locale The previous locale. 129 148 */ 130 do_action( 'restore_previous_locale', $locale, $previous_locale );149 do_action( 'restore_previous_locale', $locale, $previous_locale[0] ); 131 150 132 151 return $locale; … … 141 160 */ 142 161 public function restore_current_locale() { 143 if ( empty( $this-> locales) ) {144 return false; 145 } 146 147 $this-> locales = array( $this->original_locale);162 if ( empty( $this->stack ) ) { 163 return false; 164 } 165 166 $this->stack = array( array( $this->original_locale, false ) ); 148 167 149 168 return $this->restore_previous_locale(); … … 158 177 */ 159 178 public function is_switched() { 160 return ! empty( $this->locales ); 179 return ! empty( $this->stack ); 180 } 181 182 /** 183 * Returns the locale currently switched to. 184 * 185 * @since 6.2.0 186 * 187 * @return string|false Locale if the locale has been switched, false otherwise. 188 */ 189 public function get_current_locale() { 190 $entry = end( $this->stack ); 191 192 if ( $entry ) { 193 return $entry[0]; 194 } 195 196 return false; 197 } 198 199 /** 200 * Returns the user ID related to the currently switched locale. 201 * 202 * @since 6.2.0 203 * 204 * @return int|false User ID if set and if the locale has been switched, false otherwise. 205 */ 206 public function get_current_user_id() { 207 $entry = end( $this->stack ); 208 209 if ( $entry ) { 210 return $entry[1]; 211 } 212 213 return false; 161 214 } 162 215 … … 170 223 */ 171 224 public function filter_locale( $locale ) { 172 $switched_locale = end( $this->locales);225 $switched_locale = $this->get_current_locale(); 173 226 174 227 if ( $switched_locale ) {
Note: See TracChangeset
for help on using the changeset viewer.