Changeset 40108
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
r40101 r40108 1392 1392 1393 1393 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 ); 1395 1404 } 1396 1405 … … 1408 1417 1409 1418 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 ); 1411 1429 } 1412 1430 -
trunk/tests/phpunit/includes/testcase-rest-post-type-controller.php
r40080 r40108 11 11 $this->assertEquals( get_permalink( $post->ID ), $data['link'] ); 12 12 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'] ); 14 17 } 15 18 $this->assertEquals( mysql_to_rfc3339( $post->post_date ), $data['date'] ); 16 19 17 20 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'] ); 19 25 } 20 26 $this->assertEquals( mysql_to_rfc3339( $post->post_modified ), $data['modified'] ); … … 140 146 if ( 'edit' === $context ) { 141 147 $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 }154 148 } 155 149 -
trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php
r40101 r40108 1252 1252 1253 1253 $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)1255 1254 $post_date_gmt = str_replace( 'T', ' ', $results['date_gmt'] ); 1256 1255 $this->assertEquals( $post_date_gmt, $post->post_date_gmt ); … … 1351 1350 public function test_create_post_as_contributor() { 1352 1351 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. 1356 1358 '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 1361 1370 $this->check_create_post_response( $response ); 1371 1372 update_option( 'timezone_string', '' ); 1362 1373 } 1363 1374 … … 1432 1443 $this->assertEquals( 'draft', $data['status'] ); 1433 1444 $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'] ); 1437 1451 } 1438 1452 … … 2119 2133 2120 2134 $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)2122 2135 $post_date_gmt = str_replace( 'T', ' ', $results['date_gmt'] ); 2123 2136 $this->assertEquals( $post_date_gmt, $post->post_date_gmt ); … … 2148 2161 2149 2162 $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', '' ); 2150 2214 } 2151 2215
Note: See TracChangeset
for help on using the changeset viewer.