From 54baae39b2101c15621be3e9e8b56d73f82f60b6 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 24 Mar 2017 17:31:54 +0000 Subject: [PATCH] REST API: Use `get_gmt_from_date()` when preparing a draft post for response. This prevents wrong dates when dealing with DST, see [40115] and [40284]. Props nerrad. Fixes #40136. git-svn-id: https://develop.svn.wordpress.org/trunk@40324 602fd350-edb4-49c9-b593-d223f7449a82 --- .../rest-api/endpoints/class-wp-rest-posts-controller.php | 2 +- tests/phpunit/tests/rest-api/rest-posts-controller.php | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index 26b78c5e35..89b4ccb3e2 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php @@ -1401,7 +1401,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller { // #38883). In this case, shim the value based on the `post_date` // field with the site's timezone offset applied. if ( '0000-00-00 00:00:00' === $post->post_date_gmt ) { - $post_date_gmt = date( 'Y-m-d H:i:s', strtotime( $post->post_date ) - ( get_option( 'gmt_offset' ) * 3600 ) ); + $post_date_gmt = get_gmt_from_date( $post->post_date ); } else { $post_date_gmt = $post->post_date_gmt; } diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index abc30d40ce..409982eed8 100644 --- a/tests/phpunit/tests/rest-api/rest-posts-controller.php +++ b/tests/phpunit/tests/rest-api/rest-posts-controller.php @@ -2242,8 +2242,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te global $wpdb; wp_set_current_user( self::$editor_id ); - update_option( 'timezone_string', '' ); - update_option( 'gmt_offset', -6 ); + update_option( 'timezone_string', 'America/Chicago' ); // Need to set dates using wpdb directly because `wp_update_post` and // `wp_insert_post` have additional validation on dates. @@ -2288,7 +2287,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $this->assertEquals( $post->post_date_gmt, '2016-02-23 19:00:00' ); update_option( 'timezone_string', '' ); - update_option( 'gmt_offset', 0 ); } public function test_update_post_slug() {