WordPress.org

Make WordPress Core


Ignore:
Timestamp:
02/24/2017 10:02:48 PM (5 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/tests/rest-api/rest-posts-controller.php

    r40114 r40115  
    11961196
    11971197        $this->assertEquals( $results['date_gmt'], $data['date_gmt'] );
    1198         // TODO expect null here for drafts (see https://core.trac.wordpress.org/ticket/5698#comment:14)
    11991198        $post_date_gmt = str_replace( 'T', ' ', $results['date_gmt'] );
    12001199        $this->assertEquals( $post_date_gmt, $post->post_date_gmt );
     
    12951294    public function test_create_post_as_contributor() {
    12961295        wp_set_current_user( self::$contributor_id );
    1297 
    1298         $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
    1299         $params = $this->set_post_data(array(
     1296        update_option( 'timezone_string', 'America/Chicago' );
     1297
     1298        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     1299        $params = $this->set_post_data( array(
     1300            // This results in a special `post_date_gmt` value of
     1301            // '0000-00-00 00:00:00'.  See #38883.
    13001302            'status' => 'pending',
    1301         ));
    1302 
    1303         $request->set_body_params( $params );
    1304         $response = $this->server->dispatch( $request );
     1303        ) );
     1304
     1305        $request->set_body_params( $params );
     1306        $response = $this->server->dispatch( $request );
     1307        $this->assertEquals( 201, $response->get_status() );
     1308
     1309        $data = $response->get_data();
     1310        $post = get_post( $data['id'] );
     1311        $this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt );
     1312        $this->assertNotEquals( '0000-00-00T00:00:00', $data['date_gmt'] );
     1313
    13051314        $this->check_create_post_response( $response );
     1315
     1316        update_option( 'timezone_string', '' );
    13061317    }
    13071318
     
    13761387        $this->assertEquals( 'draft', $data['status'] );
    13771388        $this->assertEquals( 'draft', $new_post->post_status );
    1378         // Confirm dates are null
    1379         $this->assertNull( $data['date_gmt'] );
    1380         $this->assertNull( $data['modified_gmt'] );
     1389        // Confirm dates are shimmed for gmt_offset
     1390        $post_modified_gmt = date( 'Y-m-d H:i:s', strtotime( $new_post->post_modified ) + ( get_option( 'gmt_offset' ) * 3600 ) );
     1391        $post_date_gmt = date( 'Y-m-d H:i:s', strtotime( $new_post->post_date ) + ( get_option( 'gmt_offset' ) * 3600 ) );
     1392
     1393        $this->assertEquals( mysql_to_rfc3339( $post_modified_gmt ), $data['modified_gmt'] );
     1394        $this->assertEquals( mysql_to_rfc3339( $post_date_gmt ), $data['date_gmt'] );
    13811395    }
    13821396
     
    20632077
    20642078        $this->assertEquals( $results['date_gmt'], $data['date_gmt'] );
    2065         // TODO expect null here for drafts (see https://core.trac.wordpress.org/ticket/5698#comment:14)
    20662079        $post_date_gmt = str_replace( 'T', ' ', $results['date_gmt'] );
    20672080        $this->assertEquals( $post_date_gmt, $post->post_date_gmt );
     
    20922105
    20932106        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
     2107    }
     2108
     2109    public function test_empty_post_date_gmt_shimmed_using_post_date() {
     2110        global $wpdb;
     2111
     2112        wp_set_current_user( self::$editor_id );
     2113        update_option( 'timezone_string', 'America/Chicago' );
     2114
     2115        // Need to set dates using wpdb directly because `wp_update_post` and
     2116        // `wp_insert_post` have additional validation on dates.
     2117        $post_id = $this->factory->post->create();
     2118        $wpdb->update(
     2119            $wpdb->posts,
     2120            array(
     2121                'post_date'     => '2016-02-23 12:00:00',
     2122                'post_date_gmt' => '0000-00-00 00:00:00',
     2123            ),
     2124            array(
     2125                'ID' => $post_id,
     2126            ),
     2127            array( '%s', '%s' ),
     2128            array( '%d' )
     2129        );
     2130        wp_cache_delete( $post_id, 'posts' );
     2131
     2132        $post = get_post( $post_id );
     2133        $this->assertEquals( $post->post_date,     '2016-02-23 12:00:00' );
     2134        $this->assertEquals( $post->post_date_gmt, '0000-00-00 00:00:00' );
     2135
     2136        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
     2137        $response = $this->server->dispatch( $request );
     2138        $this->assertEquals( 200, $response->get_status() );
     2139        $data = $response->get_data();
     2140
     2141        $this->assertEquals( '2016-02-23T12:00:00', $data['date'] );
     2142        $this->assertEquals( '2016-02-23T18:00:00', $data['date_gmt'] );
     2143
     2144        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $post_id ) );
     2145        $request->set_param( 'date', '2016-02-23T13:00:00' );
     2146        $response = $this->server->dispatch( $request );
     2147        $this->assertEquals( 200, $response->get_status() );
     2148        $data = $response->get_data();
     2149
     2150        $this->assertEquals( '2016-02-23T13:00:00', $data['date'] );
     2151        $this->assertEquals( '2016-02-23T19:00:00', $data['date_gmt'] );
     2152
     2153        $post = get_post( $post_id );
     2154        $this->assertEquals( $post->post_date,     '2016-02-23 13:00:00' );
     2155        $this->assertEquals( $post->post_date_gmt, '2016-02-23 19:00:00' );
     2156
     2157        update_option( 'timezone_string', '' );
    20942158    }
    20952159
Note: See TracChangeset for help on using the changeset viewer.