From 4414e3c20ac9ed66a1d6fad7d5a5e1b18e7596e9 Mon Sep 17 00:00:00 2001 From: James Nylen Date: Fri, 24 Feb 2017 18:14:21 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@40108 602fd350-edb4-49c9-b593-d223f7449a82 --- .../class-wp-rest-posts-controller.php | 22 +++++- .../testcase-rest-post-type-controller.php | 22 ++---- .../tests/rest-api/rest-posts-controller.php | 78 +++++++++++++++++-- 3 files changed, 99 insertions(+), 23 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index 3648d9fa07..5b6cafdbc4 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php @@ -1391,7 +1391,16 @@ class WP_REST_Posts_Controller extends WP_REST_Controller { } if ( ! empty( $schema['properties']['date_gmt'] ) ) { - $data['date_gmt'] = $this->prepare_date_response( $post->post_date_gmt ); + // For drafts, `post_date_gmt` may not be set, indicating that the + // date of the draft should be updated each time it is saved (see + // #38883). In this case, shim the value based on the `post_date` + // field with the site's timezone offset applied. + if ( '0000-00-00 00:00:00' === $post->post_date_gmt ) { + $post_date_gmt = date( 'Y-m-d H:i:s', strtotime( $post->post_date ) - ( get_option( 'gmt_offset' ) * 3600 ) ); + } else { + $post_date_gmt = $post->post_date_gmt; + } + $data['date_gmt'] = $this->prepare_date_response( $post_date_gmt ); } if ( ! empty( $schema['properties']['guid'] ) ) { @@ -1407,7 +1416,16 @@ class WP_REST_Posts_Controller extends WP_REST_Controller { } if ( ! empty( $schema['properties']['modified_gmt'] ) ) { - $data['modified_gmt'] = $this->prepare_date_response( $post->post_modified_gmt ); + // For drafts, `post_modified_gmt` may not be set (see + // `post_date_gmt` comments above). In this case, shim the value + // based on the `post_modified` field with the site's timezone + // offset applied. + if ( '0000-00-00 00:00:00' === $post->post_modified_gmt ) { + $post_modified_gmt = date( 'Y-m-d H:i:s', strtotime( $post->post_modified ) - ( get_option( 'gmt_offset' ) * 3600 ) ); + } else { + $post_modified_gmt = $post->post_modified_gmt; + } + $data['modified_gmt'] = $this->prepare_date_response( $post_modified_gmt ); } if ( ! empty( $schema['properties']['password'] ) ) { diff --git a/tests/phpunit/includes/testcase-rest-post-type-controller.php b/tests/phpunit/includes/testcase-rest-post-type-controller.php index 387d359459..8c30d5ba93 100644 --- a/tests/phpunit/includes/testcase-rest-post-type-controller.php +++ b/tests/phpunit/includes/testcase-rest-post-type-controller.php @@ -10,12 +10,18 @@ abstract class WP_Test_REST_Post_Type_Controller_Testcase extends WP_Test_REST_C $this->assertEquals( $post->post_name, $data['slug'] ); $this->assertEquals( get_permalink( $post->ID ), $data['link'] ); if ( '0000-00-00 00:00:00' === $post->post_date_gmt ) { - $this->assertNull( $data['date_gmt'] ); + $post_date_gmt = date( 'Y-m-d H:i:s', strtotime( $post->post_date ) - ( get_option( 'gmt_offset' ) * 3600 ) ); + $this->assertEquals( mysql_to_rfc3339( $post_date_gmt ), $data['date_gmt'] ); + } else { + $this->assertEquals( mysql_to_rfc3339( $post->post_date_gmt ), $data['date_gmt'] ); } $this->assertEquals( mysql_to_rfc3339( $post->post_date ), $data['date'] ); if ( '0000-00-00 00:00:00' === $post->post_modified_gmt ) { - $this->assertNull( $data['modified_gmt'] ); + $post_modified_gmt = date( 'Y-m-d H:i:s', strtotime( $post->post_modified ) - ( get_option( 'gmt_offset' ) * 3600 ) ); + $this->assertEquals( mysql_to_rfc3339( $post_modified_gmt ), $data['modified_gmt'] ); + } else { + $this->assertEquals( mysql_to_rfc3339( $post->post_modified_gmt ), $data['modified_gmt'] ); } $this->assertEquals( mysql_to_rfc3339( $post->post_modified ), $data['modified'] ); @@ -139,18 +145,6 @@ abstract class WP_Test_REST_Post_Type_Controller_Testcase extends WP_Test_REST_C if ( 'edit' === $context ) { $this->assertEquals( $post->guid, $data['guid']['raw'] ); - - if ( '0000-00-00 00:00:00' === $post->post_date_gmt ) { - $this->assertNull( $data['date_gmt'] ); - } else { - $this->assertEquals( mysql_to_rfc3339( $post->post_date_gmt ), $data['date_gmt'] ); - } - - if ( '0000-00-00 00:00:00' === $post->post_modified_gmt ) { - $this->assertNull( $data['modified_gmt'] ); - } else { - $this->assertEquals( mysql_to_rfc3339( $post->post_modified_gmt ), $data['modified_gmt'] ); - } } $taxonomies = wp_list_filter( get_object_taxonomies( $post->post_type, 'objects' ), array( 'show_in_rest' => true ) ); diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index 6943329577..b5c18a0ade 100644 --- a/tests/phpunit/tests/rest-api/rest-posts-controller.php +++ b/tests/phpunit/tests/rest-api/rest-posts-controller.php @@ -1251,7 +1251,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $this->assertEquals( $post_date, $post->post_date ); $this->assertEquals( $results['date_gmt'], $data['date_gmt'] ); - // TODO expect null here for drafts (see https://core.trac.wordpress.org/ticket/5698#comment:14) $post_date_gmt = str_replace( 'T', ' ', $results['date_gmt'] ); $this->assertEquals( $post_date_gmt, $post->post_date_gmt ); } @@ -1350,15 +1349,27 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_create_post_as_contributor() { wp_set_current_user( self::$contributor_id ); + update_option( 'timezone_string', 'America/Chicago' ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); - $params = $this->set_post_data(array( + $params = $this->set_post_data( array( + // This results in a special `post_date_gmt` value of + // '0000-00-00 00:00:00'. See #38883. 'status' => 'pending', - )); + ) ); $request->set_body_params( $params ); $response = $this->server->dispatch( $request ); + $this->assertEquals( 201, $response->get_status() ); + + $data = $response->get_data(); + $post = get_post( $data['id'] ); + $this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt ); + $this->assertNotEquals( '0000-00-00T00:00:00', $data['date_gmt'] ); + $this->check_create_post_response( $response ); + + update_option( 'timezone_string', '' ); } public function test_create_post_sticky() { @@ -1431,9 +1442,12 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $new_post = get_post( $data['id'] ); $this->assertEquals( 'draft', $data['status'] ); $this->assertEquals( 'draft', $new_post->post_status ); - // Confirm dates are null - $this->assertNull( $data['date_gmt'] ); - $this->assertNull( $data['modified_gmt'] ); + // Confirm dates are shimmed for gmt_offset + $post_modified_gmt = date( 'Y-m-d H:i:s', strtotime( $new_post->post_modified ) + ( get_option( 'gmt_offset' ) * 3600 ) ); + $post_date_gmt = date( 'Y-m-d H:i:s', strtotime( $new_post->post_date ) + ( get_option( 'gmt_offset' ) * 3600 ) ); + + $this->assertEquals( mysql_to_rfc3339( $post_modified_gmt ), $data['modified_gmt'] ); + $this->assertEquals( mysql_to_rfc3339( $post_date_gmt ), $data['date_gmt'] ); } public function test_create_post_private() { @@ -2118,7 +2132,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $this->assertEquals( $post_date, $post->post_date ); $this->assertEquals( $results['date_gmt'], $data['date_gmt'] ); - // TODO expect null here for drafts (see https://core.trac.wordpress.org/ticket/5698#comment:14) $post_date_gmt = str_replace( 'T', ' ', $results['date_gmt'] ); $this->assertEquals( $post_date_gmt, $post->post_date_gmt ); } @@ -2149,6 +2162,57 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); } + public function test_empty_post_date_gmt_shimmed_using_post_date() { + global $wpdb; + + wp_set_current_user( self::$editor_id ); + update_option( 'timezone_string', 'America/Chicago' ); + + // Need to set dates using wpdb directly because `wp_update_post` and + // `wp_insert_post` have additional validation on dates. + $post_id = $this->factory->post->create(); + $wpdb->update( + $wpdb->posts, + array( + 'post_date' => '2016-02-23 12:00:00', + 'post_date_gmt' => '0000-00-00 00:00:00', + ), + array( + 'ID' => $post_id, + ), + array( '%s', '%s' ), + array( '%d' ) + ); + wp_cache_delete( $post_id, 'posts' ); + + $post = get_post( $post_id ); + $this->assertEquals( $post->post_date, '2016-02-23 12:00:00' ); + $this->assertEquals( $post->post_date_gmt, '0000-00-00 00:00:00' ); + + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) ); + $response = $this->server->dispatch( $request ); + $this->assertEquals( 200, $response->get_status() ); + $data = $response->get_data(); + + $this->assertEquals( '2016-02-23T12:00:00', $data['date'] ); + $this->assertEquals( '2016-02-23T18:00:00', $data['date_gmt'] ); + + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $post_id ) ); + $request->set_param( 'date', '2016-02-23T13:00:00' ); + $response = $this->server->dispatch( $request ); + $this->assertEquals( 200, $response->get_status() ); + $data = $response->get_data(); + + $this->assertEquals( '2016-02-23T13:00:00', $data['date'] ); + $this->assertEquals( '2016-02-23T19:00:00', $data['date_gmt'] ); + + $post = get_post( $post_id ); + $this->assertEquals( $post->post_date, '2016-02-23 13:00:00' ); + $this->assertEquals( $post->post_date_gmt, '2016-02-23 19:00:00' ); + + update_option( 'timezone_string', '' ); + } + public function test_update_post_slug() { wp_set_current_user( self::$editor_id );