Make WordPress Core


Ignore:
Timestamp:
02/24/2017 06:14:21 PM (8 years ago)
Author:
jnylen0
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r40101 r40108  
    13921392
    13931393        if ( ! empty( $schema['properties']['date_gmt'] ) ) {
    1394             $data['date_gmt'] = $this->prepare_date_response( $post->post_date_gmt );
     1394            // For drafts, `post_date_gmt` may not be set, indicating that the
     1395            // date of the draft should be updated each time it is saved (see
     1396            // #38883).  In this case, shim the value based on the `post_date`
     1397            // field with the site's timezone offset applied.
     1398            if ( '0000-00-00 00:00:00' === $post->post_date_gmt ) {
     1399                $post_date_gmt = date( 'Y-m-d H:i:s', strtotime( $post->post_date ) - ( get_option( 'gmt_offset' ) * 3600 ) );
     1400            } else {
     1401                $post_date_gmt = $post->post_date_gmt;
     1402            }
     1403            $data['date_gmt'] = $this->prepare_date_response( $post_date_gmt );
    13951404        }
    13961405
     
    14081417
    14091418        if ( ! empty( $schema['properties']['modified_gmt'] ) ) {
    1410             $data['modified_gmt'] = $this->prepare_date_response( $post->post_modified_gmt );
     1419            // For drafts, `post_modified_gmt` may not be set (see
     1420            // `post_date_gmt` comments above).  In this case, shim the value
     1421            // based on the `post_modified` field with the site's timezone
     1422            // offset applied.
     1423            if ( '0000-00-00 00:00:00' === $post->post_modified_gmt ) {
     1424                $post_modified_gmt = date( 'Y-m-d H:i:s', strtotime( $post->post_modified ) - ( get_option( 'gmt_offset' ) * 3600 ) );
     1425            } else {
     1426                $post_modified_gmt = $post->post_modified_gmt;
     1427            }
     1428            $data['modified_gmt'] = $this->prepare_date_response( $post_modified_gmt );
    14111429        }
    14121430
Note: See TracChangeset for help on using the changeset viewer.