Make WordPress Core


Ignore:
Timestamp:
06/18/2009 05:40:40 PM (17 years ago)
Author:
ryan
Message:

Optimize timezone dropdown creation. Props sambauers. fixes #10125 for trunk

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/functions.php

    r11544 r11597  
    31503150 */
    31513151function wp_timezone_override_offset() {
    3152         if (!wp_timezone_supported()) return false;
    3153 
    3154         $tz = get_option('timezone_string');
    3155         if (empty($tz)) return false;
    3156 
    3157         @date_default_timezone_set($tz);
    3158 
    3159         $dateTimeZoneSelected = timezone_open($tz);
    3160         $dateTimeServer = date_create();
    3161         if ($dateTimeZoneSelected === false || $dateTimeServer === false) return false;
    3162 
    3163         $timeOffset = timezone_offset_get($dateTimeZoneSelected, $dateTimeServer);
    3164         $timeOffset = $timeOffset / 3600;
    3165 
    3166         return $timeOffset;
     3152        if ( !wp_timezone_supported() ) {
     3153                return false;
     3154        }
     3155        if ( !$timezone_string = get_option( 'timezone_string' ) ) {
     3156                return false;
     3157        }
     3158
     3159        @date_default_timezone_set( $timezone_string );
     3160        $timezone_object = timezone_open( $timezone_string );
     3161        $datetime_object = date_create();
     3162        if ( false === $timezone_object || false === $datetime_object ) {
     3163                return false;
     3164        }
     3165        return round( timezone_offset_get( $timezone_object, $datetime_object ) / 3600, 2 );
    31673166}
    31683167
    31693168/**
    31703169 * Check for PHP timezone support
    3171  *
    31723170 */
    31733171function wp_timezone_supported() {
    3174         if (function_exists('date_default_timezone_set')
    3175                 && function_exists('timezone_identifiers_list')
    3176                 && function_exists('timezone_open')
    3177                 && function_exists('timezone_offset_get')
    3178                 )
    3179                 return apply_filters('timezone_support',true);
    3180 
    3181         return apply_filters('timezone_support',false);
     3172        $support = false;
     3173        if (
     3174                function_exists( 'date_default_timezone_set' ) &&
     3175                function_exists( 'timezone_identifiers_list' ) &&
     3176                function_exists( 'timezone_open' ) &&
     3177                function_exists( 'timezone_offset_get' )
     3178        ) {
     3179                $support = true;
     3180        }
     3181        return apply_filters( 'timezone_support', $support );
     3182}
     3183
     3184function _wp_timezone_choice_usort_callback( $a, $b ) {
     3185        // Don't use translated versions of Etc
     3186        if ( 'Etc' === $a['continent'] && 'Etc' === $b['continent'] ) {
     3187                // Make the order of these more like the old dropdown
     3188                if ( 'GMT+' === substr( $a['city'], 0, 4 ) && 'GMT+' === substr( $b['city'], 0, 4 ) ) {
     3189                        return -1 * ( strnatcasecmp( $a['city'], $b['city'] ) );
     3190                }
     3191                if ( 'UTC' === $a['city'] ) {
     3192                        if ( 'GMT+' === substr( $b['city'], 0, 4 ) ) {
     3193                                return 1;
     3194                        }
     3195                        return -1;
     3196                }
     3197                if ( 'UTC' === $b['city'] ) {
     3198                        if ( 'GMT+' === substr( $a['city'], 0, 4 ) ) {
     3199                                return -1;
     3200                        }
     3201                        return 1;
     3202                }
     3203                return strnatcasecmp( $a['city'], $b['city'] );
     3204        }
     3205        if ( $a['t_continent'] == $b['t_continent'] ) {
     3206                if ( $a['t_city'] == $b['t_city'] ) {
     3207                        return strnatcasecmp( $a['t_subcity'], $b['t_subcity'] );
     3208                }
     3209                return strnatcasecmp( $a['t_city'], $b['t_city'] );
     3210        } else {
     3211                // Force Etc to the bottom of the list
     3212                if ( 'Etc' === $a['continent'] ) {
     3213                        return 1;
     3214                }
     3215                if ( 'Etc' === $b['continent'] ) {
     3216                        return -1;
     3217                }
     3218                return strnatcasecmp( $a['t_continent'], $b['t_continent'] );
     3219        }
    31823220}
    31833221
     
    31883226 *
    31893227 */
    3190 function wp_timezone_choice($selectedzone) {
     3228function wp_timezone_choice( $selected_zone ) {
    31913229        static $mo_loaded = false;
    31923230
    3193         $continents = array('Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific', 'Etc');
     3231        $continents = array( 'Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific', 'Etc' );
    31943232
    31953233        // Load translations for continents and cities
    3196         if ( ! $mo_loaded ) {
     3234        if ( !$mo_loaded ) {
    31973235                $locale = get_locale();
    3198                 $mofile = WP_LANG_DIR . "/continents-cities-$locale.mo";
    3199                 load_textdomain('continents-cities', $mofile);
     3236                $mofile = WP_LANG_DIR . '/continents-cities-' . $locale . '.mo';
     3237                load_textdomain( 'continents-cities', $mofile );
    32003238                $mo_loaded = true;
    32013239        }
    32023240
    3203         $all = timezone_identifiers_list();
    3204 
    3205         $i = 0;
    3206         foreach ( $all as $zone ) {
    3207                 $zone = explode('/',$zone);
    3208                 if ( ! in_array($zone[0], $continents) )
     3241        $zonen = array();
     3242        foreach ( timezone_identifiers_list() as $zone ) {
     3243                $zone = explode( '/', $zone );
     3244                if ( !in_array( $zone[0], $continents ) ) {
    32093245                        continue;
    3210                 if ( $zone[0] == 'Etc' && in_array($zone[1], array('UCT', 'GMT', 'GMT0', 'GMT+0', 'GMT-0', 'Greenwich', 'Universal', 'Zulu')) )
     3246                }
     3247                if ( 'Etc' === $zone[0] && in_array( $zone[1], array( 'UCT', 'GMT', 'GMT0', 'GMT+0', 'GMT-0', 'Greenwich', 'Universal', 'Zulu' ) ) ) {
    32113248                        continue;
    3212                 $zonen[$i]['continent'] = isset($zone[0]) ? $zone[0] : '';
    3213                 $zonen[$i]['city'] = isset($zone[1]) ? $zone[1] : '';
    3214                 $zonen[$i]['subcity'] = isset($zone[2]) ? $zone[2] : '';
    3215                 $i++;
    3216         }
    3217 
    3218         usort($zonen, create_function(
    3219                 '$a, $b', '
    3220                 $t = create_function(\'$s\', \'return translate(str_replace("_", " ", $s), "continents-cities");\');
    3221                 $a_continent = $t($a["continent"]);
    3222                 $b_continent = $t($b["continent"]);
    3223                 $a_city = $t($a["city"]);
    3224                 $b_city = $t($b["city"]);
    3225                 $a_subcity = $t($a["subcity"]);
    3226                 $b_subcity = $t($b["subcity"]);
    3227                 if ( $a_continent == $b_continent && $a_city == $b_city )
    3228                         return strnatcasecmp($a_subcity, $b_subcity);
    3229                 elseif ( $a_continent == $b_continent )
    3230                         return strnatcasecmp($a_city, $b_city);
    3231                 else
    3232                         return strnatcasecmp($a_continent, $b_continent);
    3233                 '));
    3234 
    3235         $structure = '';
    3236         $pad = '   ';
    3237 
    3238         if ( empty($selectedzone) )
    3239                 $structure .= '<option selected="selected" value="">' . __('Select a city') . "</option>\n";
    3240         foreach ( $zonen as $zone ) {
    3241                 extract($zone);
    3242                 if ( empty($selectcontinent) && !empty($city) ) {
    3243                         $selectcontinent = $continent;
    3244                         $structure .= '<optgroup label="'. esc_attr( translate( $continent, "continents-cities" ) ) .'">' . "\n"; // continent
    3245                 } elseif ( !empty($selectcontinent) && $selectcontinent != $continent ) {
    3246                         $structure .= "</optgroup>\n";
    3247                         $selectcontinent = '';
    3248                         if ( !empty($city) ) {
    3249                                 $selectcontinent = $continent;
    3250                                 $structure .= '<optgroup label="'. esc_attr( translate( $continent, "continents-cities" ) ) .'">' . "\n"; // continent
     3249                }
     3250
     3251                // This determines what gets set and translated - we don't translate Etc/* strings here, they are done later
     3252                $exists = array(
     3253                        0 => ( isset( $zone[0] ) && $zone[0] ) ? true : false,
     3254                        1 => ( isset( $zone[1] ) && $zone[1] ) ? true : false,
     3255                        2 => ( isset( $zone[2] ) && $zone[2] ) ? true : false
     3256                );
     3257                $exists[3] = ( $exists[0] && 'Etc' !== $zone[0] ) ? true : false;
     3258                $exists[4] = ( $exists[1] && $exists[3] ) ? true : false;
     3259                $exists[5] = ( $exists[2] && $exists[3] ) ? true : false;
     3260
     3261                $zonen[] = array(
     3262                        'continent'   => ( $exists[0] ? $zone[0] : '' ),
     3263                        'city'        => ( $exists[1] ? $zone[1] : '' ),
     3264                        'subcity'     => ( $exists[2] ? $zone[2] : '' ),
     3265                        't_continent' => ( $exists[3] ? translate( str_replace( '_', ' ', $zone[0] ), 'continents-cities' ) : '' ),
     3266                        't_city'      => ( $exists[4] ? translate( str_replace( '_', ' ', $zone[1] ), 'continents-cities' ) : '' ),
     3267                        't_subcity'   => ( $exists[5] ? translate( str_replace( '_', ' ', $zone[2] ), 'continents-cities' ) : '' )
     3268                );
     3269        }
     3270        usort( $zonen, '_wp_timezone_choice_usort_callback' );
     3271
     3272        $structure = array();
     3273
     3274        if ( empty( $selected_zone ) ) {
     3275                $structure[] = '<option selected="selected" value="">' . __( 'Select a city' ) . '</option>';
     3276        }
     3277
     3278        foreach ( $zonen as $key => $zone ) {
     3279                // Build value in an array to join later
     3280                $value = array( $zone['continent'] );
     3281
     3282                if ( empty( $zone['city'] ) ) {
     3283                        // It's at the continent level (generally won't happen)
     3284                        $display = $zone['t_continent'];
     3285                } else {
     3286                        // It's inside a continent group
     3287                       
     3288                        // Continent optgroup
     3289                        if ( !isset( $zonen[$key - 1] ) || $zonen[$key - 1]['continent'] !== $zone['continent'] ) {
     3290                                $label = ( 'Etc' === $zone['continent'] ) ? __( 'Manual offsets' ) : $zone['t_continent'];
     3291                                $structure[] = '<optgroup label="'. esc_attr( $label ) .'">';
     3292                        }
     3293                       
     3294                        // Add the city to the value
     3295                        $value[] = $zone['city'];
     3296                        if ( 'Etc' === $zone['continent'] ) {
     3297                                if ( 'UTC' === $zone['city'] ) {
     3298                                        $display = '';
     3299                                } else {
     3300                                        $display = str_replace( 'GMT', '', $zone['city'] );
     3301                                        $display = strtr( $display, '+-', '-+' ) . ':00';
     3302                                }
     3303                                $display = '&nbsp;&nbsp;&nbsp;' . sprintf( __( 'UTC %s' ), $display );
     3304                        } else {
     3305                                $display = '&nbsp;&nbsp;&nbsp;' . $zone['t_city'];
     3306                                if ( !empty( $zone['subcity'] ) ) {
     3307                                        // Add the subcity to the value
     3308                                        $value[] = $zone['subcity'];
     3309                                        $display .= ' - ' . $zone['t_subcity'];
     3310                                }
    32513311                        }
    32523312                }
    32533313
    3254                 if ( !empty($city) ) {
    3255                         $display = str_replace('_',' ',$city);
    3256                         $display = translate($display, "continents-cities");
    3257                         if ( !empty($subcity) ) {
    3258                                 $display_subcity = str_replace('_', ' ', $subcity);
    3259                                 $display_subcity = translate($display_subcity, "continents-cities");
    3260                                 $city = $city . '/'. $subcity;
    3261                                 $display = $display . '/' . $display_subcity;
    3262                         }
    3263                         if ( $continent == 'Etc' )
    3264                                 $display = strtr($display, '+-', '-+');
    3265                         $structure .= "\t<option ".((($continent.'/'.$city)==$selectedzone)?'selected="selected"':'')." value=\"".($continent.'/'.$city)."\">$pad".$display."</option>\n"; //Timezone
    3266                 } else {
    3267                         $structure .= "<option ".(($continent==$selectedzone)?'selected="selected"':'')." value=\"".$continent."\">" . translate($continent, "continents-cities") . "</option>\n"; //Timezone
     3314                // Build the value
     3315                $value = join( '/', $value );
     3316                $selected = '';
     3317                if ( $value === $selected_zone ) {
     3318                        $selected = 'selected="selected" ';
    32683319                }
    3269         }
    3270 
    3271         if ( !empty($selectcontinent) )
    3272                 $structure .= "</optgroup>\n";
    3273         return $structure;
     3320                $structure[] = '<option ' . $selected . 'value="' . esc_attr( $value ) . '">' . esc_html( $display ) . "</option>";
     3321               
     3322                // Close continent optgroup
     3323                if ( !empty( $zone['city'] ) && isset( $zonen[$key + 1] ) && $zonen[$key + 1]['continent'] !== $zone['continent'] ) {
     3324                        $structure[] = '</optgroup>';
     3325                }
     3326        }
     3327
     3328        return join( "\n", $structure );
    32743329}
    32753330
Note: See TracChangeset for help on using the changeset viewer.