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.
Fixes #38883.

Built from https://develop.svn.wordpress.org/trunk@40108


git-svn-id: http://core.svn.wordpress.org/trunk@40045 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
James Nylen 2017-02-24 18:15:44 +00:00
parent 33286df4c1
commit 78b298f0f3
2 changed files with 21 additions and 3 deletions

View File

@ -1391,7 +1391,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'] ) ) {
@ -1407,7 +1416,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'] ) ) {

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.8-alpha-40107';
$wp_version = '4.8-alpha-40108';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.