From 9dac8e173f295c702dfa7d7cdb9341c21d1eb86c Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 24 Feb 2017 22:03:34 +0000 Subject: [PATCH] REST API: Shim `post_date_gmt` for drafts / empty dates in the REST API. Internally, WordPress uses a special `post_date_gmt` value of `0000-00-00 00:00:00` to indicate that a draft's date is "floating" and should be updated whenever the post is saved. This makes it much more difficult for API clients to know the correct date of a draft post. This commit provides a best guess at a `date_gmt` value for draft posts in this situation using the `date` field and the site's current timezone offset. Props joehoyle, jnylen0. Merges [40108] to the 4.7 branch. Fixes #38883. Built from https://develop.svn.wordpress.org/branches/4.7@40115 git-svn-id: http://core.svn.wordpress.org/branches/4.7@40052 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../class-wp-rest-posts-controller.php | 22 +++++++++++++++++-- wp-includes/version.php | 2 +- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index fe3c7f1210..9011f49cc7 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php @@ -1385,7 +1385,16 @@ class WP_REST_Posts_Controller extends WP_REST_Controller { } if ( ! empty( $schema['properties']['date_gmt'] ) ) { - $data['date_gmt'] = $this->prepare_date_response( $post->post_date_gmt ); + // For drafts, `post_date_gmt` may not be set, indicating that the + // date of the draft should be updated each time it is saved (see + // #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 ) ); + } else { + $post_date_gmt = $post->post_date_gmt; + } + $data['date_gmt'] = $this->prepare_date_response( $post_date_gmt ); } if ( ! empty( $schema['properties']['guid'] ) ) { @@ -1401,7 +1410,16 @@ class WP_REST_Posts_Controller extends WP_REST_Controller { } if ( ! empty( $schema['properties']['modified_gmt'] ) ) { - $data['modified_gmt'] = $this->prepare_date_response( $post->post_modified_gmt ); + // For drafts, `post_modified_gmt` may not be set (see + // `post_date_gmt` comments above). In this case, shim the value + // based on the `post_modified` field with the site's timezone + // offset applied. + if ( '0000-00-00 00:00:00' === $post->post_modified_gmt ) { + $post_modified_gmt = date( 'Y-m-d H:i:s', strtotime( $post->post_modified ) - ( get_option( 'gmt_offset' ) * 3600 ) ); + } else { + $post_modified_gmt = $post->post_modified_gmt; + } + $data['modified_gmt'] = $this->prepare_date_response( $post_modified_gmt ); } if ( ! empty( $schema['properties']['password'] ) ) { diff --git a/wp-includes/version.php b/wp-includes/version.php index 4ef4490f99..7658062dd4 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.7.3-alpha-40114'; +$wp_version = '4.7.3-alpha-40115'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.