Timezone support. Props Otto42. see #3962

git-svn-id: https://develop.svn.wordpress.org/trunk@10753 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan Boren 2009-03-10 00:50:00 +00:00
parent 00c4cb1765
commit 2e9a9d1426
4 changed files with 118 additions and 2 deletions

View File

@ -93,6 +93,9 @@ include('./admin-header.php');
</td>
</tr>
<tr>
<?php
if (!wp_timezone_supported()) : // no magic timezone support here
?>
<th scope="row"><label for="gmt_offset"><?php _e('Timezone') ?> </label></th>
<td>
<select name="gmt_offset" id="gmt_offset">
@ -127,6 +130,26 @@ foreach ( $offset_range as $offset ) {
<br/>
<span class="setting-description"><?php _e('Unfortunately, you have to manually update this for Daylight Savings Time. Lame, we know, but will be fixed in the future.'); ?></span>
</td>
<?php
else: // looks like we can do nice timezone selection!
$current_offset = get_option('gmt_offset');
?>
<th scope="row"><label for="timezone_string"><?php _e('Timezone') ?></label></th>
<td>
<select id="timezone_string" name="timezone_string">
<?php echo wp_timezone_choice(get_option('timezone_string')); ?>
</select>
<span id="utc-time"><?php printf(__('<abbr title="Coordinated Universal Time">UTC</abbr> time is <code>%s</code>'), date_i18n(__('Y-m-d G:i:s'), false, 'gmt')); ?></span>
<?php if (get_option('timezone_string')) : ?>
<span id="local-time"><?php printf(__('Current time in %1$s is <code>%2$s</code>'), get_option('timezone_string'), date_i18n(__('Y-m-d G:i:s'))); ?></span>
<?php endif; ?>
<br/>
<span class="setting-description"><?php _e('Choose a city in the same timezone as you.'); ?></span>
</td>
<?php endif; ?>
</tr>
<tr>
<th scope="row"><?php _e('Date Format') ?></th>

View File

@ -22,7 +22,7 @@ $parent_file = 'options-general.php';
wp_reset_vars(array('action'));
$whitelist_options = array(
'general' => array( 'blogname', 'blogdescription', 'admin_email', 'users_can_register', 'gmt_offset', 'date_format', 'time_format', 'start_of_week', 'default_role' ),
'general' => array( 'blogname', 'blogdescription', 'admin_email', 'users_can_register', 'gmt_offset', 'date_format', 'time_format', 'start_of_week', 'default_role', 'timezone_string' ),
'discussion' => array( 'default_pingback_flag', 'default_ping_status', 'default_comment_status', 'comments_notify', 'moderation_notify', 'comment_moderation', 'require_name_email', 'comment_whitelist', 'comment_max_links', 'moderation_keys', 'blacklist_keys', 'show_avatars', 'avatar_rating', 'avatar_default', 'close_comments_for_old_posts', 'close_comments_days_old', 'thread_comments', 'thread_comments_depth', 'page_comments', 'comments_per_page', 'default_comments_page', 'comment_order', 'comment_registration' ),
'misc' => array( 'hack_file', 'use_linksupdate', 'uploads_use_yearmonth_folders', 'upload_path', 'upload_url_path' ),
'media' => array( 'thumbnail_size_w', 'thumbnail_size_h', 'thumbnail_crop', 'medium_size_w', 'medium_size_h', 'large_size_w', 'large_size_h', 'image_default_size', 'image_default_align', 'image_default_link_type' ),

View File

@ -203,4 +203,6 @@ add_action('edit_post', 'wp_check_for_changed_slugs');
add_action('edit_form_advanced', 'wp_remember_old_slug');
add_action('init', '_show_post_preview');
add_filter('pre_option_gmt_offset','wp_timezone_override_offset');
?>

View File

@ -3049,5 +3049,96 @@ function update_site_option( $key, $value ) {
return update_option($key, $value);
}
/**
* gmt_offset modification for smart timezone handling
*
* Overrides the gmt_offset option if we have a timezone_string available
*/
function wp_timezone_override_offset() {
if (!wp_timezone_supported()) return false;
?>
$tz = get_option('timezone_string');
if (empty($tz)) return false;
@date_default_timezone_set($tz);
$dateTimeZoneSelected = timezone_open($tz);
$dateTimeServer = date_create();
if ($dateTimeZoneSelected === false || $dateTimeServer === false) return false;
$timeOffset = timezone_offset_get($dateTimeZoneSelected, $dateTimeServer);
$timeOffset = $timeOffset / 3600;
return $timeOffset;
}
/**
* Check for PHP timezone support
*
*/
function wp_timezone_supported() {
if (function_exists('date_default_timezone_set')
&& function_exists('timezone_identifiers_list')
&& function_exists('timezone_open')
&& function_exists('timezone_offset_get')
)
return true;
return false;
}
/**
* Gives a nicely formatted list of timezone strings // temporary! Not in final
*
* @param string $selectedzone - which zone should be the selected one
*
*/
function wp_timezone_choice($selectedzone) {
$all = timezone_identifiers_list();
$i = 0;
foreach ( $all as $zone ) {
$zone = explode('/',$zone);
$zonen[$i]['continent'] = isset($zone[0]) ? $zone[0] : '';
$zonen[$i]['city'] = isset($zone[1]) ? $zone[1] : '';
$zonen[$i]['subcity'] = isset($zone[2]) ? $zone[2] : '';
$i++;
}
asort($zonen);
$structure = '';
$pad = '&nbsp;&nbsp;&nbsp;';
if ( empty($selectedzone) )
$structure .= '<option selected="selected" value="">' . __('Select a city') . "</option>\n";
foreach ( $zonen as $zone ) {
extract($zone);
if ( empty($selectcontinent) && !empty($city) ) {
$selectcontinent = $continent;
$structure .= '<optgroup label="'.$continent.'">' . "\n"; // continent
} elseif ( !empty($selectcontinent) && $selectcontinent != $continent ) {
$structure .= "</optgroup>\n";
$selectcontinent = '';
if ( !empty($city) ) {
$selectcontinent = $continent;
$structure .= '<optgroup label="'.$continent.'">' . "\n"; // continent
}
}
if ( !empty($city) ) {
if ( !empty($subcity) ) {
$city = $city . '/'. $subcity;
}
$structure .= "\t<option ".((($continent.'/'.$city)==$selectedzone)?'selected="selected "':'')." value=\"".($continent.'/'.$city)."\">$pad".str_replace('_',' ',$city)."</option>\n"; //Timezone
} else {
$structure .= "<option ".(($continent==$selectedzone)?'selected="selected "':'')." value=\"".$continent."\">".$continent."</option>\n"; //Timezone
}
}
if ( !empty($selectcontinent) )
$structure .= "</optgroup>\n";
return $structure;
}
?>