Changeset 6677 for sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-login/functions.php
- Timestamp:
- 02/17/2018 01:40:56 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-login/functions.php
r6645 r6677 147 147 } 148 148 add_filter( 'insert_user_meta', 'wporg_login_limit_user_meta', 1 ); 149 150 /** 151 * Retreives all avaiable locales with their native names. 152 * 153 * @return array Locales with their native names. 154 */ 155 function wporg_login_get_locales() { 156 wp_cache_add_global_groups( [ 'locale-associations' ] ); 157 158 $wp_locales = wp_cache_get( 'locale-list', 'locale-associations' ); 159 if ( false === $wp_locales ) { 160 $wp_locales = (array) $GLOBALS['wpdb']->get_col( 'SELECT locale FROM wporg_locales' ); 161 wp_cache_set( 'locale-list', $wp_locales, 'locale-associations' ); 162 } 163 164 $wp_locales[] = 'en_US'; 165 166 require_once GLOTPRESS_LOCALES_PATH; 167 168 $locales = []; 169 170 foreach ( $wp_locales as $locale ) { 171 $gp_locale = GP_Locales::by_field( 'wp_locale', $locale ); 172 if ( ! $gp_locale ) { 173 continue; 174 } 175 176 $locales[ $locale ] = $gp_locale->native_name; 177 } 178 179 natsort( $locales ); 180 181 return $locales; 182 } 183 184 /** 185 * Prints markup for a simple language switcher. 186 */ 187 function wporg_login_language_switcher() { 188 $current_locale = get_locale(); 189 190 ?> 191 <div class="language-switcher"> 192 <form id="language-switcher" action="" method="GET"> 193 <label for="language-switcher-locales"> 194 <span aria-hidden="true" class="dashicons dashicons-translation"></span> 195 <span class="screen-reader-text"><?php _e( 'Select the language:', 'wporg' ); ?></span> 196 </label> 197 <select id="language-switcher-locales" name="locale"> 198 <?php 199 foreach ( wporg_login_get_locales() as $locale => $locale_name ) { 200 printf( 201 '<option value="%s"%s>%s</option>', 202 esc_attr( $locale ), 203 selected( $locale, $current_locale, false ), 204 esc_html( $locale_name ) 205 ); 206 } 207 ?> 208 </select> 209 </form> 210 </div> 211 <script> 212 var switcherForm = document.getElementById( 'language-switcher' ); 213 var localesSelect = document.getElementById( 'language-switcher-locales' ); 214 localesSelect.addEventListener( 'change', function() { 215 switcherForm.submit() 216 } ); 217 </script> 218 <?php 219 } 220 add_action( 'login_footer', 'wporg_login_language_switcher' );
Note: See TracChangeset
for help on using the changeset viewer.