WordPress.org

Make WordPress Core

Changeset 40108


Ignore:
Timestamp:
02/24/2017 06:14:21 PM (16 months 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.

Location:
trunk
Files:
3 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
  • trunk/tests/phpunit/includes/testcase-rest-post-type-controller.php

    r40080 r40108  
    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
  • 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.