Changeset 40115
- Timestamp:
- 02/24/2017 10:02:48 PM (8 years ago)
- 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 1386 1386 1387 1387 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 ); 1389 1398 } 1390 1399 … … 1402 1411 1403 1412 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 ); 1405 1423 } 1406 1424 -
branches/4.7/tests/phpunit/includes/testcase-rest-post-type-controller.php
r40081 r40115 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 -
branches/4.7/tests/phpunit/tests/rest-api/rest-posts-controller.php
r40114 r40115 1196 1196 1197 1197 $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)1199 1198 $post_date_gmt = str_replace( 'T', ' ', $results['date_gmt'] ); 1200 1199 $this->assertEquals( $post_date_gmt, $post->post_date_gmt ); … … 1295 1294 public function test_create_post_as_contributor() { 1296 1295 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. 1300 1302 '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 1305 1314 $this->check_create_post_response( $response ); 1315 1316 update_option( 'timezone_string', '' ); 1306 1317 } 1307 1318 … … 1376 1387 $this->assertEquals( 'draft', $data['status'] ); 1377 1388 $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'] ); 1381 1395 } 1382 1396 … … 2063 2077 2064 2078 $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)2066 2079 $post_date_gmt = str_replace( 'T', ' ', $results['date_gmt'] ); 2067 2080 $this->assertEquals( $post_date_gmt, $post->post_date_gmt ); … … 2092 2105 2093 2106 $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', '' ); 2094 2158 } 2095 2159
Note: See TracChangeset
for help on using the changeset viewer.