WordPress.org

Make WordPress Core


Ignore:
Timestamp:
02/24/2017 10:02:48 PM (4 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/tests/phpunit/includes/testcase-rest-post-type-controller.php

    r40081 r40115  
    1111        $this->assertEquals( get_permalink( $post->ID ), $data['link'] );
    1212        if ( '0000-00-00 00:00:00' === $post->post_date_gmt ) {
    13             $this->assertNull( $data['date_gmt'] );
     13            $post_date_gmt = date( 'Y-m-d H:i:s', strtotime( $post->post_date ) - ( get_option( 'gmt_offset' ) * 3600 ) );
     14            $this->assertEquals( mysql_to_rfc3339( $post_date_gmt ), $data['date_gmt'] );
     15        } else {
     16            $this->assertEquals( mysql_to_rfc3339( $post->post_date_gmt ), $data['date_gmt'] );
    1417        }
    1518        $this->assertEquals( mysql_to_rfc3339( $post->post_date ), $data['date'] );
    1619
    1720        if ( '0000-00-00 00:00:00' === $post->post_modified_gmt ) {
    18             $this->assertNull( $data['modified_gmt'] );
     21            $post_modified_gmt = date( 'Y-m-d H:i:s', strtotime( $post->post_modified ) - ( get_option( 'gmt_offset' ) * 3600 ) );
     22            $this->assertEquals( mysql_to_rfc3339( $post_modified_gmt ), $data['modified_gmt'] );
     23        } else {
     24            $this->assertEquals( mysql_to_rfc3339( $post->post_modified_gmt ), $data['modified_gmt'] );
    1925        }
    2026        $this->assertEquals( mysql_to_rfc3339( $post->post_modified ), $data['modified'] );
     
    140146        if ( 'edit' === $context ) {
    141147            $this->assertEquals( $post->guid, $data['guid']['raw'] );
    142 
    143             if ( '0000-00-00 00:00:00' === $post->post_date_gmt ) {
    144                 $this->assertNull( $data['date_gmt'] );
    145             } else {
    146                 $this->assertEquals( mysql_to_rfc3339( $post->post_date_gmt ), $data['date_gmt'] );
    147             }
    148 
    149             if ( '0000-00-00 00:00:00' === $post->post_modified_gmt ) {
    150                 $this->assertNull( $data['modified_gmt'] );
    151             } else {
    152                 $this->assertEquals( mysql_to_rfc3339( $post->post_modified_gmt ), $data['modified_gmt'] );
    153             }
    154148        }
    155149
Note: See TracChangeset for help on using the changeset viewer.