Make WordPress Core

Ticket #58986: 58986.diff

File 58986.diff, 8.7 KB (added by rleeson, 16 months ago)

Add common retrieval for GMT offset as a float with default of 0.

  • src/wp-admin/options-general.php

    diff --git src/wp-admin/options-general.php src/wp-admin/options-general.php
    index c79851badc..fe0f868dd8 100644
    if ( ! empty( $languages ) || ! empty( $translations ) ) { 
    230230?>
    231231<tr>
    232232<?php
    233 $current_offset = get_option( 'gmt_offset' );
     233$current_offset = current_gmt_offset();
    234234$tzstring       = get_option( 'timezone_string' );
    235235
    236236$check_zone_info = true;
  • src/wp-admin/update-core.php

    diff --git src/wp-admin/update-core.php src/wp-admin/update-core.php
    index 8949f39e33..719b89c290 100644
    if ( 'upgrade-core' === $action ) { 
    10791079        $current           = get_site_transient( 'update_core' );
    10801080
    10811081        if ( $current && isset( $current->last_checked ) ) {
    1082                 $last_update_check = $current->last_checked + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
     1082                $last_update_check = $current->last_checked + current_gmt_offset() * HOUR_IN_SECONDS;
    10831083        }
    10841084
    10851085        echo '<h2 class="wp-current-version">';
  • src/wp-includes/bookmark-template.php

    diff --git src/wp-includes/bookmark-template.php src/wp-includes/bookmark-template.php
    index d9bca2bbd7..9696d2b49d 100644
    function _walk_bookmarks( $bookmarks, $args = '' ) { 
    9090                                        __( 'Last updated: %s' ),
    9191                                        gmdate(
    9292                                                get_option( 'links_updated_date_format' ),
    93                                                 $bookmark->link_updated_f + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS )
     93                                                $bookmark->link_updated_f + ( current_gmt_offset() * HOUR_IN_SECONDS )
    9494                                        )
    9595                                );
    9696                                $title .= ')';
  • src/wp-includes/customize/class-wp-customize-date-time-control.php

    diff --git src/wp-includes/customize/class-wp-customize-date-time-control.php src/wp-includes/customize/class-wp-customize-date-time-control.php
    index 1b445af37f..5c12b3a4f0 100644
    class WP_Customize_Date_Time_Control extends WP_Customize_Control { 
    280280                                $timezone_info['description'] = '';
    281281                        }
    282282                } else {
    283                         $formatted_gmt_offset = $this->format_gmt_offset( (int) get_option( 'gmt_offset', 0 ) );
     283                        $formatted_gmt_offset = $this->format_gmt_offset( current_gmt_offset() );
    284284
    285285                        $timezone_info['description'] = sprintf(
    286286                                /* translators: 1: UTC abbreviation and offset, 2: UTC offset. */
  • src/wp-includes/deprecated.php

    diff --git src/wp-includes/deprecated.php src/wp-includes/deprecated.php
    index 72bf98d832..df44ef48bb 100644
    function get_links($category = -1, $before = '', $after = '<br />', $between = ' 
    981981
    982982                if ( $show_updated )
    983983                        if ( !str_starts_with($row->link_updated_f, '00') )
    984                                 $title .= ' ('.__('Last updated') . ' ' . gmdate(get_option('links_updated_date_format'), $row->link_updated_f + (get_option('gmt_offset') * HOUR_IN_SECONDS)) . ')';
     984                                $title .= ' ('.__('Last updated') . ' ' . gmdate(get_option('links_updated_date_format'), $row->link_updated_f + (current_gmt_offset() * HOUR_IN_SECONDS)) . ')';
    985985
    986986                if ( '' != $title )
    987987                        $title = ' title="' . $title . '"';
  • src/wp-includes/functions.php

    diff --git src/wp-includes/functions.php src/wp-includes/functions.php
    index 16a32dd7bf..0fbd93e201 100644
    function mysql2date( $format, $date, $translate = true ) { 
    7373function current_time( $type, $gmt = 0 ) {
    7474        // Don't use non-GMT timestamp, unless you know the difference and really need to.
    7575        if ( 'timestamp' === $type || 'U' === $type ) {
    76                 return $gmt ? time() : time() + (int) ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
     76                return $gmt ? time() : time() + (int) ( current_gmt_offset() * HOUR_IN_SECONDS );
    7777        }
    7878
    7979        if ( 'mysql' === $type ) {
    function current_datetime() { 
    9797        return new DateTimeImmutable( 'now', wp_timezone() );
    9898}
    9999
     100/**
     101 * Retrieves the current GMT offset set for the site.
     102 *
     103 *  - Default to 0 if the option is not set or invalid
     104 *
     105 * @since 6.4.0
     106 *
     107 * @return float
     108 */
     109function current_gmt_offset() {
     110        return (float) get_option( 'gmt_offset', 0 );
     111}
     112
    100113/**
    101114 * Retrieves the timezone of the site as a string.
    102115 *
    function wp_timezone_string() { 
    123136                return $timezone_string;
    124137        }
    125138
    126         $offset  = (float) get_option( 'gmt_offset' );
     139        $offset  = current_gmt_offset();
    127140        $hours   = (int) $offset;
    128141        $minutes = ( $offset - $hours );
    129142
  • src/wp-includes/rest-api/class-wp-rest-server.php

    diff --git src/wp-includes/rest-api/class-wp-rest-server.php src/wp-includes/rest-api/class-wp-rest-server.php
    index 9df4556ada..41ae6bb60c 100644
    class WP_REST_Server { 
    12651265                        'description'     => get_option( 'blogdescription' ),
    12661266                        'url'             => get_option( 'siteurl' ),
    12671267                        'home'            => home_url(),
    1268                         'gmt_offset'      => get_option( 'gmt_offset' ),
     1268                        'gmt_offset'      => current_gmt_offset(),
    12691269                        'timezone_string' => get_option( 'timezone_string' ),
    12701270                        'namespaces'      => array_keys( $this->namespaces ),
    12711271                        'authentication'  => array(),
  • src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
    index 356c321129..ab34a4c18b 100644
    class WP_REST_Posts_Controller extends WP_REST_Controller { 
    18021802                         * with the site's timezone offset applied.
    18031803                         */
    18041804                        if ( '0000-00-00 00:00:00' === $post->post_modified_gmt ) {
    1805                                 $post_modified_gmt = gmdate( 'Y-m-d H:i:s', strtotime( $post->post_modified ) - ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) );
     1805                                $post_modified_gmt = gmdate( 'Y-m-d H:i:s', strtotime( $post->post_modified ) - ( current_gmt_offset() * HOUR_IN_SECONDS ) );
    18061806                        } else {
    18071807                                $post_modified_gmt = $post->post_modified_gmt;
    18081808                        }
  • src/wp-includes/script-loader.php

    diff --git src/wp-includes/script-loader.php src/wp-includes/script-loader.php
    index 205cedda72..c32a60067d 100644
    function wp_default_packages_inline_scripts( $scripts ) { 
    444444                                                'datetimeAbbreviated' => __( 'M j, Y g:i a' ),
    445445                                        ),
    446446                                        'timezone' => array(
    447                                                 'offset' => (float) get_option( 'gmt_offset', 0 ),
     447                                                'offset' => current_gmt_offset(),
    448448                                                'string' => $timezone_string,
    449449                                                'abbr'   => $timezone_abbr,
    450450                                        ),
  • src/wp-mail.php

    diff --git src/wp-mail.php src/wp-mail.php
    index 1dd1e570d1..7f2921269e 100644
    if ( $last_checked ) { 
    4444
    4545set_transient( 'mailserver_last_checked', true, WP_MAIL_INTERVAL );
    4646
    47 $time_difference = get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
     47$time_difference = current_gmt_offset() * HOUR_IN_SECONDS;
    4848
    4949$phone_delim = '::';
    5050
  • new file tests/phpunit/tests/date/currentGmtOffset.php

    diff --git tests/phpunit/tests/date/currentGmtOffset.php tests/phpunit/tests/date/currentGmtOffset.php
    new file mode 100644
    index 0000000000..8a0805c1fd
    - +  
     1<?php
     2
     3/**
     4 * @group date
     5 * @group datetime
     6 * @covers ::current_gmt_offset()
     7 */
     8class Tests_Date_CurrentGmtOffset extends WP_UnitTestCase {
     9
     10        /**
     11         * Cleans up.
     12         */
     13        public function tear_down() {
     14                // Reset changed options to their default value.
     15                update_option( 'gmt_offset', 0 );
     16
     17                parent::tear_down();
     18        }
     19
     20        /**
     21         * @ticket 58986
     22         */
     23        public function test_current_gmt_offset_empty_is_default() {
     24                delete_option( 'gmt_offset' );
     25
     26                $this->assertIsFloat( current_gmt_offset(), 'Missing current GMT offset is default of 0');
     27        }
     28
     29        /**
     30         * @ticket 58986
     31         */
     32        public function test_current_gmt_offset_invalid_boolean_true() {
     33                update_option( 'gmt_offset', true );
     34
     35                $this->assertEquals( 0, current_gmt_offset(), 'Invalid current GMT offset of boolean true is 0');
     36        }
     37
     38        /**
     39         * @ticket 58986
     40         */
     41        public function test_current_gmt_offset_invalid_string() {
     42                update_option( 'gmt_offset', 'NinePointFive' );
     43
     44                $this->assertEquals( 0, current_gmt_offset(), 'Invalid current GMT offset of non-numeric text is 0');
     45        }
     46
     47        /**
     48         * @ticket 58986
     49         */
     50        public function test_current_gmt_offset_is_float() {
     51                update_option( 'gmt_offset', 9.5 );
     52
     53                $this->assertIsFloat( current_gmt_offset(), 'Current GMT offset is a float');
     54        }
     55}
  • tests/qunit/fixtures/wp-api-generated.js

    diff --git tests/qunit/fixtures/wp-api-generated.js tests/qunit/fixtures/wp-api-generated.js
    index 27aa52edcc..9f58d5e239 100644
    mockedApiResponse.Schema = { 
    1010    "description": "",
    1111    "url": "http://example.org",
    1212    "home": "http://example.org",
    13     "gmt_offset": "0",
     13    "gmt_offset": 0,
    1414    "timezone_string": "",
    1515    "namespaces": [
    1616        "oembed/1.0",