From 50c4abe81b014b769c813b91f772dd326d030a77 Mon Sep 17 00:00:00 2001 From: Brandon Kraft Date: Mon, 23 Sep 2019 12:35:16 -0500 Subject: [PATCH 1/4] WP 5.3: Utilize new wp_timezone() function while providing a back-compat shim. --- _inc/lib/icalendar-reader.php | 17 +---------------- class.json-api-endpoints.php | 19 ++----------------- functions.global.php | 32 ++++++++++++++++++++++++++++++++ packages/sync/src/Functions.php | 13 +++++++++++-- 4 files changed, 46 insertions(+), 35 deletions(-) diff --git a/_inc/lib/icalendar-reader.php b/_inc/lib/icalendar-reader.php index 306f9fffcaef6..998f4c139c9c1 100644 --- a/_inc/lib/icalendar-reader.php +++ b/_inc/lib/icalendar-reader.php @@ -90,17 +90,7 @@ function apply_timezone_offset( $events ) { } // get timezone offset from the timezone name. - $timezone_name = get_option( 'timezone_string' ); - if ( $timezone_name ) { - $timezone = new DateTimeZone( $timezone_name ); - $timezone_offset_interval = false; - } else { - // If the timezone isn't set then the GMT offset must be set. - // generate a DateInterval object from the timezone offset - $gmt_offset = get_option( 'gmt_offset' ) * HOUR_IN_SECONDS; - $timezone_offset_interval = date_interval_create_from_date_string( "{$gmt_offset} seconds" ); - $timezone = new DateTimeZone( 'UTC' ); - } + $timezone = wp_timezone(); $offsetted_events = array(); @@ -115,11 +105,6 @@ function apply_timezone_offset( $events ) { $end_time = new DateTime( $end_time, $this->timezone ); $end_time->setTimeZone( $timezone ); - if ( $timezone_offset_interval ) { - $start_time->add( $timezone_offset_interval ); - $end_time->add( $timezone_offset_interval ); - } - $event['DTSTART'] = $start_time->format( 'YmdHis\Z' ); $event['DTEND'] = $end_time->format( 'YmdHis\Z' ); } diff --git a/class.json-api-endpoints.php b/class.json-api-endpoints.php index 65bdc10335ff0..2819de593720c 100644 --- a/class.json-api-endpoints.php +++ b/class.json-api-endpoints.php @@ -1557,27 +1557,12 @@ function parse_date( $date_string ) { $dt_local = clone $dt_utc = new DateTime( 'now', new DateTimeZone( 'UTC' ) ); } - // First try to use timezone as it's daylight savings aware. - $timezone_string = get_option( 'timezone_string' ); - if ( $timezone_string ) { - $tz = timezone_open( $timezone_string ); - if ( $tz ) { - $dt_local->setTimezone( $tz ); - return array( - (string) $dt_local->format( 'Y-m-d H:i:s' ), - (string) $dt_utc->format( 'Y-m-d H:i:s' ), - ); - } - } + $dt_local->setTimezone( wp_timezone() ); - // Fallback to GMT offset (in hours) - // NOTE: TZ of $dt_local is still UTC, we simply modified the timestamp with an offset. - $gmt_offset_seconds = intval( get_option( 'gmt_offset' ) * 3600 ); - $dt_local->modify( "+{$gmt_offset_seconds} seconds" ); return array( (string) $dt_local->format( 'Y-m-d H:i:s' ), (string) $dt_utc->format( 'Y-m-d H:i:s' ), - ); + ); } // Load the functions.php file for the current theme to get its post formats, CPTs, etc. diff --git a/functions.global.php b/functions.global.php index e61733209ea2b..ca6b8132821d7 100644 --- a/functions.global.php +++ b/functions.global.php @@ -19,6 +19,38 @@ exit; } +if ( ! function_exists( wp_timezone ) ) { + /** + * Shim for WordPress 5.3's wp_timezone() function. + * + * This is a mix of wp_timezone(), which calls wp_timezone_string(). + * We don't need both in Jetpack, so providing only one function. + * + * @since 7.9.0 + * @todo Remove when WP 5.3 is Jetpack's minimum + * + * @return DateTimeZone Site's DateTimeZone + */ + function wp_timezone() { + $timezone_string = get_option( 'timezone_string' ); + + if ( $timezone_string ) { + return new DateTimeZone( $timezone_string ); + } + + $offset = (float) get_option( 'gmt_offset' ); + $hours = (int) $offset; + $minutes = ( $offset - $hours ); + + $sign = ( $offset < 0 ) ? '-' : '+'; + $abs_hour = abs( $hours ); + $abs_mins = abs( $minutes * 60 ); + $tz_offset = sprintf( '%s%02d:%02d', $sign, $abs_hour, $abs_mins ); + + return new DateTimeZone( $tz_offset ); + } +} + /** * Set the admin language, based on user language. * diff --git a/packages/sync/src/Functions.php b/packages/sync/src/Functions.php index 82bf8b0f6caf3..f7f8adcd6dfac 100644 --- a/packages/sync/src/Functions.php +++ b/packages/sync/src/Functions.php @@ -1,12 +1,17 @@ Date: Mon, 23 Sep 2019 14:00:38 -0500 Subject: [PATCH 2/4] PHPCS updates --- class.json-api-endpoints.php | 19 +++++++++++-------- functions.global.php | 2 +- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/class.json-api-endpoints.php b/class.json-api-endpoints.php index 2819de593720c..8fd4e65fe3a95 100644 --- a/class.json-api-endpoints.php +++ b/class.json-api-endpoints.php @@ -1533,16 +1533,17 @@ function format_date( $date_gmt, $date = null ) { * relative to now and will convert it to local time using either the * timezone set in the options table for the blog or the GMT offset. * - * @param datetime string + * @param datetime string $date_string Date to parse. * * @return array( $local_time_string, $gmt_time_string ) */ - function parse_date( $date_string ) { + public function parse_date( $date_string ) { $date_string_info = date_parse( $date_string ); if ( is_array( $date_string_info ) && 0 === $date_string_info['error_count'] ) { // Check if it's already localized. Can't just check is_localtime because date_parse('oppossum') returns true; WTF, PHP. if ( isset( $date_string_info['zone'] ) && true === $date_string_info['is_localtime'] ) { - $dt_local = clone $dt_utc = new DateTime( $date_string ); + $dt_utc = new DateTime( $date_string ); + $dt_local = $dt_utc; $dt_utc->setTimezone( new DateTimeZone( 'UTC' ) ); return array( (string) $dt_local->format( 'Y-m-d H:i:s' ), @@ -1550,11 +1551,13 @@ function parse_date( $date_string ) { ); } - // It's parseable but no TZ info so assume UTC - $dt_local = clone $dt_utc = new DateTime( $date_string, new DateTimeZone( 'UTC' ) ); + // It's parseable but no TZ info so assume UTC. + $dt_utc = new DateTime( $date_string, new DateTimeZone( 'UTC' ) ); + $dt_local = new DateTime( $date_string, new DateTimeZone( 'UTC' ) ); } else { - // Could not parse time, use now in UTC - $dt_local = clone $dt_utc = new DateTime( 'now', new DateTimeZone( 'UTC' ) ); + // Could not parse time, use now in UTC. + $dt_utc = new DateTime( 'now', new DateTimeZone( 'UTC' ) ); + $dt_local = $dt_utc; } $dt_local->setTimezone( wp_timezone() ); @@ -1562,7 +1565,7 @@ function parse_date( $date_string ) { return array( (string) $dt_local->format( 'Y-m-d H:i:s' ), (string) $dt_utc->format( 'Y-m-d H:i:s' ), - ); + ); } // Load the functions.php file for the current theme to get its post formats, CPTs, etc. diff --git a/functions.global.php b/functions.global.php index ca6b8132821d7..9d983fd99ffe0 100644 --- a/functions.global.php +++ b/functions.global.php @@ -19,7 +19,7 @@ exit; } -if ( ! function_exists( wp_timezone ) ) { +if ( ! function_exists( 'wp_timezone' ) ) { /** * Shim for WordPress 5.3's wp_timezone() function. * From 5ef7d672e4f1fb3c595afe7edfd1f95a0d20bc2a Mon Sep 17 00:00:00 2001 From: Brandon Kraft Date: Fri, 27 Sep 2019 09:36:29 -0500 Subject: [PATCH 3/4] [not verified] Revert out change to Sync package and add note --- packages/sync/src/Functions.php | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/packages/sync/src/Functions.php b/packages/sync/src/Functions.php index f7f8adcd6dfac..70319ad737f41 100644 --- a/packages/sync/src/Functions.php +++ b/packages/sync/src/Functions.php @@ -1,17 +1,12 @@ Date: Fri, 27 Sep 2019 10:35:06 -0500 Subject: [PATCH 4/4] Re-add cloning to API endpoint Resolves unit test failure on wp.com. --- class.json-api-endpoints.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/class.json-api-endpoints.php b/class.json-api-endpoints.php index 8fd4e65fe3a95..e24813ea0ec78 100644 --- a/class.json-api-endpoints.php +++ b/class.json-api-endpoints.php @@ -1543,7 +1543,7 @@ public function parse_date( $date_string ) { // Check if it's already localized. Can't just check is_localtime because date_parse('oppossum') returns true; WTF, PHP. if ( isset( $date_string_info['zone'] ) && true === $date_string_info['is_localtime'] ) { $dt_utc = new DateTime( $date_string ); - $dt_local = $dt_utc; + $dt_local = clone $dt_utc; $dt_utc->setTimezone( new DateTimeZone( 'UTC' ) ); return array( (string) $dt_local->format( 'Y-m-d H:i:s' ), @@ -1553,11 +1553,11 @@ public function parse_date( $date_string ) { // It's parseable but no TZ info so assume UTC. $dt_utc = new DateTime( $date_string, new DateTimeZone( 'UTC' ) ); - $dt_local = new DateTime( $date_string, new DateTimeZone( 'UTC' ) ); + $dt_local = clone $dt_utc; } else { // Could not parse time, use now in UTC. $dt_utc = new DateTime( 'now', new DateTimeZone( 'UTC' ) ); - $dt_local = $dt_utc; + $dt_local = clone $dt_utc; } $dt_local->setTimezone( wp_timezone() );