Make WordPress Core


Ignore:
Timestamp:
02/24/2017 10:02:48 PM (8 years ago)
Author:
SergeyBiryukov
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, jnylen0.
Merges [40108] to the 4.7 branch.
Fixes #38883.

Location:
branches/4.7
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.7

  • branches/4.7/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r40114 r40115  
    13861386
    13871387        if ( ! empty( $schema['properties']['date_gmt'] ) ) {
    1388             $data['date_gmt'] = $this->prepare_date_response( $post->post_date_gmt );
     1388            // For drafts, `post_date_gmt` may not be set, indicating that the
     1389            // date of the draft should be updated each time it is saved (see
     1390            // #38883).  In this case, shim the value based on the `post_date`
     1391            // field with the site's timezone offset applied.
     1392            if ( '0000-00-00 00:00:00' === $post->post_date_gmt ) {
     1393                $post_date_gmt = date( 'Y-m-d H:i:s', strtotime( $post->post_date ) - ( get_option( 'gmt_offset' ) * 3600 ) );
     1394            } else {
     1395                $post_date_gmt = $post->post_date_gmt;
     1396            }
     1397            $data['date_gmt'] = $this->prepare_date_response( $post_date_gmt );
    13891398        }
    13901399
     
    14021411
    14031412        if ( ! empty( $schema['properties']['modified_gmt'] ) ) {
    1404             $data['modified_gmt'] = $this->prepare_date_response( $post->post_modified_gmt );
     1413            // For drafts, `post_modified_gmt` may not be set (see
     1414            // `post_date_gmt` comments above).  In this case, shim the value
     1415            // based on the `post_modified` field with the site's timezone
     1416            // offset applied.
     1417            if ( '0000-00-00 00:00:00' === $post->post_modified_gmt ) {
     1418                $post_modified_gmt = date( 'Y-m-d H:i:s', strtotime( $post->post_modified ) - ( get_option( 'gmt_offset' ) * 3600 ) );
     1419            } else {
     1420                $post_modified_gmt = $post->post_modified_gmt;
     1421            }
     1422            $data['modified_gmt'] = $this->prepare_date_response( $post_modified_gmt );
    14051423        }
    14061424
Note: See TracChangeset for help on using the changeset viewer.