Make WordPress Core

Changeset 40115


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:
4 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
  • 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
  • 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.