WordPress.org

Make WordPress Core

Ticket #18146: 18146.2.diff

File 18146.2.diff, 7.4 KB (added by swissspidy, 6 years ago)
  • src/wp-admin/includes/user.php

    diff --git src/wp-admin/includes/user.php src/wp-admin/includes/user.php
    index 901f437..936ed8b 100644
    function edit_user( $user_id = 0 ) { 
    9494                $user->rich_editing = isset( $_POST['rich_editing'] ) && 'false' == $_POST['rich_editing'] ? 'false' : 'true';
    9595                $user->admin_color = isset( $_POST['admin_color'] ) ? sanitize_text_field( $_POST['admin_color'] ) : 'fresh';
    9696                $user->show_admin_bar_front = isset( $_POST['admin_bar_front'] ) ? 'true' : 'false';
     97
     98                // Map UTC+- timezones to gmt_offsets and set timezone_string to empty.
     99                if ( !empty($_POST['timezone_string']) && preg_match('/^UTC[+-]/', $_POST['timezone_string']) ) {
     100                        $_POST['gmt_offset'] = $_POST['timezone_string'];
     101                        $_POST['gmt_offset'] = preg_replace('/UTC\+?/', '', $_POST['gmt_offset']);
     102                        $_POST['timezone_string'] = '';
     103                }
     104
     105                $user->timezone_string = isset( $_POST['timezone_string'] ) ? sanitize_text_field( $_POST['timezone_string'] ) : '';
     106                $user->gmt_offset = isset( $_POST['gmt_offset'] ) ? sanitize_text_field( $_POST['gmt_offset'] ) : '';
    97107        }
    98108
    99109        $user->comment_shortcuts = isset( $_POST['comment_shortcuts'] ) && 'true' == $_POST['comment_shortcuts'] ? 'true' : '';
  • src/wp-admin/user-edit.php

    diff --git src/wp-admin/user-edit.php src/wp-admin/user-edit.php
    index 6ac4a28..43ef41f 100644
    elseif ( ! $user_id && ! IS_PROFILE_PAGE ) 
    2323elseif ( ! get_userdata( $user_id ) )
    2424        wp_die( __('Invalid user ID.') );
    2525
     26// Timezones
     27add_filter( 'pre_option_gmt_offset', 'wp_timezone_override_user_offset' );
     28
    2629wp_enqueue_script('user-profile');
    2730
    2831$title = IS_PROFILE_PAGE ? __('Profile') : __('Edit User');
    if ( !( IS_PROFILE_PAGE && !$user_can_edit ) ) : ?> 
    274277</fieldset>
    275278</td>
    276279</tr>
     280<tr class="user-timezone">
     281<?php
     282$current_offset = $profileuser->gmt_offset;
     283$tzstring = $profileuser->timezone_string;
     284
     285$check_zone_info = true;
     286
     287if ( empty( $tzstring ) ) { // Create a UTC+- zone if no timezone string exists
     288        $check_zone_info = false;
     289        if ( 0 == $current_offset ) {
     290                $tzstring = 'UTC+0';
     291        } elseif ( $current_offset < 0 ) {
     292                $tzstring = 'UTC' . $current_offset;
     293        } else {
     294                $tzstring = 'UTC+' . $current_offset;
     295        }
     296}
     297?>
     298<th scope="row"><label for="timezone"><?php _e( 'Timezone' ) ?></label
     299</th>
     300<td>
     301<select id="timezone_string" name="timezone_string">
     302        <?php echo wp_timezone_choice( $tzstring ); ?>
     303</select>
     304<span id="utc-time"><?php printf( __( '<abbr title="Coordinated Universal Time">UTC</abbr> time is <code>%s</code>' ), date_i18n( _x( 'Y-m-d G:i:s', 'timezone date format' ), false, 'gmt' ) ); ?></span>
     305<?php if ( $profileuser->timezone_string || ! empty( $current_offset ) ) : ?>
     306        <span id="local-time"><?php printf( __( 'Local time is <code>%1$s</code>' ), date_i18n( _x( 'Y-m-d G:i:s', 'timezone date format' ) ) ); ?></span>
     307<?php endif; ?>
     308<p class="description"><?php _e( 'Choose a city in the same timezone as you.' ); ?></p>
     309<?php if ( $check_zone_info && $tzstring ) : ?>
     310        <br />
     311        <span>
     312        <?php
     313        // Set TZ so localtime works.
     314        date_default_timezone_set( $tzstring );
     315        $now = localtime( time(), true );
     316        if ( $now['tm_isdst'] )
     317                _e('This timezone is currently in daylight saving time.');
     318        else
     319                _e('This timezone is currently in standard time.');
     320        ?>
     321        <br />
     322        <?php
     323        $allowed_zones = timezone_identifiers_list();
     324
     325        if ( in_array( $tzstring, $allowed_zones ) ) {
     326                $found = false;
     327                $date_time_zone_selected = new DateTimeZone($tzstring);
     328                $tz_offset = timezone_offset_get( $date_time_zone_selected, date_create() );
     329                $right_now = time();
     330                foreach ( timezone_transitions_get( $date_time_zone_selected ) as $tr ) {
     331                        if ( $tr['ts'] > $right_now ) {
     332                                $found = true;
     333                                break;
     334                        }
     335                }
     336
     337                if ( $found ) {
     338                        echo ' ';
     339                        $message = $tr['isdst'] ?
     340                                __('Daylight saving time begins on: <code>%s</code>.') :
     341                                __('Standard time begins on: <code>%s</code>.');
     342                        // Add the difference between the current offset and the new offset to ts to get the correct transition time from date_i18n().
     343                        printf( $message, date_i18n( get_option('date_format') . ' ' . get_option( 'time_format' ), $tr['ts'] + ($tz_offset - $tr['offset']) ) );
     344                } else {
     345                        _e( 'This timezone does not observe daylight saving time.' );
     346                }
     347        }
     348        // Set back to UTC.
     349        date_default_timezone_set( 'UTC' );
     350        ?>
     351        </span>
     352<?php endif; ?>
     353</td>
     354</tr>
    277355<?php
    278356/**
    279357 * Fires at the end of the 'Personal Options' settings table on the user editing screen.
  • src/wp-includes/default-filters.php

    diff --git src/wp-includes/default-filters.php src/wp-includes/default-filters.php
    index 7bf0388..8c20c0a 100644
    add_action( 'init', '_show_post_preview' ); 
    292292
    293293// Timezone
    294294add_filter( 'pre_option_gmt_offset','wp_timezone_override_offset' );
     295if ( is_admin() ) {
     296        add_filter( 'pre_option_gmt_offset', 'wp_timezone_override_user_offset' );
     297}
    295298
    296299// Admin Color Schemes
    297300add_action( 'admin_init', 'register_admin_color_schemes', 1);
  • src/wp-includes/functions.php

    diff --git src/wp-includes/functions.php src/wp-includes/functions.php
    index b09ed84..bcb8690 100644
    function wp_timezone_override_offset() { 
    39653965}
    39663966
    39673967/**
     3968 * Overrides the gmt_offset with the current user's offset, if available.
     3969 *
     3970 * @since 4.3.0
     3971 *
     3972 * @return float|false Timezone GMT offset, false otherwise.
     3973 */
     3974function wp_timezone_override_user_offset() {
     3975        global $user_id;
     3976
     3977        if ( ! $user_id ) {
     3978                $user_id = get_current_user_id();
     3979        }
     3980
     3981        $timezone_string = get_user_meta( $user_id, 'timezone_string', true );
     3982
     3983        if ( $timezone_string ) {
     3984                $timezone_object = timezone_open( $timezone_string );
     3985                $datetime_object = date_create();
     3986                if ( false !== $timezone_object || false !== $datetime_object ) {
     3987                        $offset = round( timezone_offset_get( $timezone_object, $datetime_object ) / HOUR_IN_SECONDS, 2 );
     3988                }
     3989        } else {
     3990                $offset = get_user_meta( $user_id, 'gmt_offset', true );
     3991        }
     3992
     3993        return $offset ? $offset : false;
     3994}
     3995
     3996/**
    39683997 * Sort-helper for timezones.
    39693998 *
    39703999 * @since 2.9.0
  • src/wp-includes/user.php

    diff --git src/wp-includes/user.php src/wp-includes/user.php
    index cb9695c..107421c 100644
    function wp_insert_user( $userdata ) { 
    20252025
    20262026        $meta['show_admin_bar_front'] = empty( $userdata['show_admin_bar_front'] ) ? 'true' : $userdata['show_admin_bar_front'];
    20272027
     2028        $meta['gmt_offset'] = empty( $userdata['gmt_offset'] ) ? '' : $userdata['gmt_offset'];
     2029        $meta['timezone_string'] = empty( $userdata['timezone_string'] ) ? '' : $userdata['timezone_string'];
     2030
    20282031        $user_nicename_check = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1" , $user_nicename, $user_login));
    20292032
    20302033        if ( $user_nicename_check ) {
    function wp_create_user($username, $password, $email = '') { 
    22052208 * @return array
    22062209 */
    22072210function _get_additional_user_keys( $user ) {
    2208         $keys = array( 'first_name', 'last_name', 'nickname', 'description', 'rich_editing', 'comment_shortcuts', 'admin_color', 'use_ssl', 'show_admin_bar_front' );
     2211        $keys = array( 'first_name', 'last_name', 'nickname', 'description', 'rich_editing', 'comment_shortcuts', 'admin_color', 'use_ssl', 'show_admin_bar_front', 'gmt_offset', 'timezone_string' );
    22092212        return array_merge( $keys, array_keys( wp_get_user_contact_methods( $user ) ) );
    22102213}
    22112214