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/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r40101 r40108  
    12521252
    12531253        $this->assertEquals( $results['date_gmt'], $data['date_gmt'] );
    1254         // TODO expect null here for drafts (see https://core.trac.wordpress.org/ticket/5698#comment:14)
    12551254        $post_date_gmt = str_replace( 'T', ' ', $results['date_gmt'] );
    12561255        $this->assertEquals( $post_date_gmt, $post->post_date_gmt );
     
    13511350    public function test_create_post_as_contributor() {
    13521351        wp_set_current_user( self::$contributor_id );
    1353 
    1354         $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
    1355         $params = $this->set_post_data(array(
     1352        update_option( 'timezone_string', 'America/Chicago' );
     1353
     1354        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     1355        $params = $this->set_post_data( array(
     1356            // This results in a special `post_date_gmt` value of
     1357            // '0000-00-00 00:00:00'.  See #38883.
    13561358            'status' => 'pending',
    1357         ));
    1358 
    1359         $request->set_body_params( $params );
    1360         $response = $this->server->dispatch( $request );
     1359        ) );
     1360
     1361        $request->set_body_params( $params );
     1362        $response = $this->server->dispatch( $request );
     1363        $this->assertEquals( 201, $response->get_status() );
     1364
     1365        $data = $response->get_data();
     1366        $post = get_post( $data['id'] );
     1367        $this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt );
     1368        $this->assertNotEquals( '0000-00-00T00:00:00', $data['date_gmt'] );
     1369
    13611370        $this->check_create_post_response( $response );
     1371
     1372        update_option( 'timezone_string', '' );
    13621373    }
    13631374
     
    14321443        $this->assertEquals( 'draft', $data['status'] );
    14331444        $this->assertEquals( 'draft', $new_post->post_status );
    1434         // Confirm dates are null
    1435         $this->assertNull( $data['date_gmt'] );
    1436         $this->assertNull( $data['modified_gmt'] );
     1445        // Confirm dates are shimmed for gmt_offset
     1446        $post_modified_gmt = date( 'Y-m-d H:i:s', strtotime( $new_post->post_modified ) + ( get_option( 'gmt_offset' ) * 3600 ) );
     1447        $post_date_gmt = date( 'Y-m-d H:i:s', strtotime( $new_post->post_date ) + ( get_option( 'gmt_offset' ) * 3600 ) );
     1448
     1449        $this->assertEquals( mysql_to_rfc3339( $post_modified_gmt ), $data['modified_gmt'] );
     1450        $this->assertEquals( mysql_to_rfc3339( $post_date_gmt ), $data['date_gmt'] );
    14371451    }
    14381452
     
    21192133
    21202134        $this->assertEquals( $results['date_gmt'], $data['date_gmt'] );
    2121         // TODO expect null here for drafts (see https://core.trac.wordpress.org/ticket/5698#comment:14)
    21222135        $post_date_gmt = str_replace( 'T', ' ', $results['date_gmt'] );
    21232136        $this->assertEquals( $post_date_gmt, $post->post_date_gmt );
     
    21482161
    21492162        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
     2163    }
     2164
     2165    public function test_empty_post_date_gmt_shimmed_using_post_date() {
     2166        global $wpdb;
     2167
     2168        wp_set_current_user( self::$editor_id );
     2169        update_option( 'timezone_string', 'America/Chicago' );
     2170
     2171        // Need to set dates using wpdb directly because `wp_update_post` and
     2172        // `wp_insert_post` have additional validation on dates.
     2173        $post_id = $this->factory->post->create();
     2174        $wpdb->update(
     2175            $wpdb->posts,
     2176            array(
     2177                'post_date'     => '2016-02-23 12:00:00',
     2178                'post_date_gmt' => '0000-00-00 00:00:00',
     2179            ),
     2180            array(
     2181                'ID' => $post_id,
     2182            ),
     2183            array( '%s', '%s' ),
     2184            array( '%d' )
     2185        );
     2186        wp_cache_delete( $post_id, 'posts' );
     2187
     2188        $post = get_post( $post_id );
     2189        $this->assertEquals( $post->post_date,     '2016-02-23 12:00:00' );
     2190        $this->assertEquals( $post->post_date_gmt, '0000-00-00 00:00:00' );
     2191
     2192        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
     2193        $response = $this->server->dispatch( $request );
     2194        $this->assertEquals( 200, $response->get_status() );
     2195        $data = $response->get_data();
     2196
     2197        $this->assertEquals( '2016-02-23T12:00:00', $data['date'] );
     2198        $this->assertEquals( '2016-02-23T18:00:00', $data['date_gmt'] );
     2199
     2200        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $post_id ) );
     2201        $request->set_param( 'date', '2016-02-23T13:00:00' );
     2202        $response = $this->server->dispatch( $request );
     2203        $this->assertEquals( 200, $response->get_status() );
     2204        $data = $response->get_data();
     2205
     2206        $this->assertEquals( '2016-02-23T13:00:00', $data['date'] );
     2207        $this->assertEquals( '2016-02-23T19:00:00', $data['date_gmt'] );
     2208
     2209        $post = get_post( $post_id );
     2210        $this->assertEquals( $post->post_date,     '2016-02-23 13:00:00' );
     2211        $this->assertEquals( $post->post_date_gmt, '2016-02-23 19:00:00' );
     2212
     2213        update_option( 'timezone_string', '' );
    21502214    }
    21512215
Note: See TracChangeset for help on using the changeset viewer.