Ticket #44975: 44975.4.diff
File 44975.4.diff, 5.1 KB (added by , 5 years ago) |
---|
-
src/wp-includes/rest-api.php
diff --git src/wp-includes/rest-api.php src/wp-includes/rest-api.php index 4b49ebfdcb..cf05448781 100644
function rest_validate_value_from_schema( $value, $args, $param = '' ) { 1226 1226 if ( isset( $args['format'] ) ) { 1227 1227 switch ( $args['format'] ) { 1228 1228 case 'date-time': 1229 if ( ! rest_parse_date( $value ) ) {1229 if ( ! empty( $value ) && ! rest_parse_date( $value ) ) { 1230 1230 return new WP_Error( 'rest_invalid_date', __( 'Invalid date.' ) ); 1231 1231 } 1232 1232 break; -
src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index e53ff4b910..4ca94dd83f 100644
class WP_REST_Posts_Controller extends WP_REST_Controller { 1036 1036 } 1037 1037 } 1038 1038 1039 // Sending a blank (null or empty string) date or date_gmt value resets date and date_gmt to their 1040 // default values (`0000-00-00 00:00:00`). 1041 if ( 1042 ( isset( $request['date_gmt'] ) && '' === $request['date_gmt'] ) || 1043 ( isset( $request['date'] ) && '' === $request['date'] ) 1044 ) { 1045 $prepared_post->post_date_gmt = null; 1046 $prepared_post->post_date = null; 1047 } 1048 1039 1049 // Post slug. 1040 1050 if ( ! empty( $schema['properties']['slug'] ) && isset( $request['slug'] ) ) { 1041 1051 $prepared_post->post_name = $request['slug']; … … class WP_REST_Posts_Controller extends WP_REST_Controller { 1881 1891 'properties' => array( 1882 1892 'date' => array( 1883 1893 'description' => __( "The date the object was published, in the site's timezone." ), 1884 'type' => 'string',1894 'type' => [ 'string', 'null' ], 1885 1895 'format' => 'date-time', 1886 1896 'context' => array( 'view', 'edit', 'embed' ), 1887 1897 ), 1888 1898 'date_gmt' => array( 1889 1899 'description' => __( 'The date the object was published, as GMT.' ), 1890 'type' => 'string',1900 'type' => [ 'string', 'null' ], 1891 1901 'format' => 'date-time', 1892 1902 'context' => array( 'view', 'edit' ), 1893 1903 ), -
tests/phpunit/tests/rest-api/rest-posts-controller.php
diff --git tests/phpunit/tests/rest-api/rest-posts-controller.php tests/phpunit/tests/rest-api/rest-posts-controller.php index ba174c448e..9d0a97f324 100644
class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 2555 2555 $this->assertEquals( $params['excerpt'], $post->post_excerpt ); 2556 2556 } 2557 2557 2558 /** 2559 * Verify that updating a post with a `null` date or date_gmt results in a reset post, where all 2560 * date values are equal (date, date_gmt, date_modified and date_modofied_gmt) in the API response. 2561 * In the database, the post_date_gmt field is reset to the default `0000-00-00 00:00:00`. 2562 * 2563 * @ticket 44975 2564 */ 2565 public function test_rest_update_post_with_empty_date() { 2566 // Create a new test post. 2567 $post_id = $this->factory->post->create(); 2568 wp_set_current_user( self::$editor_id ); 2569 2570 // Set the post date to the future. 2571 $future_date = '2919-07-29T18:00:00'; 2572 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $post_id ) ); 2573 $request->add_header( 'content-type', 'application/json' ); 2574 $params = $this->set_post_data( 2575 array( 2576 'date_gmt' => $future_date, 2577 'date' => $future_date, 2578 'title' => 'update', 2579 'status' => 'draft', 2580 ) 2581 ); 2582 $request->set_body( wp_json_encode( $params ) ); 2583 $response = rest_get_server()->dispatch( $request ); 2584 $this->check_update_post_response( $response ); 2585 $new_data = $response->get_data(); 2586 2587 // Verify the post is set to the future date. 2588 $this->assertEquals( $new_data['date_gmt'], $future_date ); 2589 $this->assertEquals( $new_data['date'], $future_date ); 2590 $this->assertNotEquals( $new_data['date_gmt'], $new_data['modified_gmt'] ); 2591 $this->assertNotEquals( $new_data['date'], $new_data['modified'] ); 2592 2593 // Update post with a blank field (date or date_gmt). 2594 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $post_id ) ); 2595 $request->add_header( 'content-type', 'application/json' ); 2596 $params = $this->set_post_data( 2597 array( 2598 'date_gmt' => null, 2599 'title' => 'test', 2600 'status' => 'draft', 2601 ) 2602 ); 2603 $request->set_body( wp_json_encode( $params ) ); 2604 $response = rest_get_server()->dispatch( $request ); 2605 2606 // Verify the date field values are reset in the API response. 2607 $this->check_update_post_response( $response ); 2608 $new_data = $response->get_data(); 2609 $this->assertEquals( $new_data['date_gmt'], $new_data['date'] ); 2610 $this->assertNotEquals( $new_data['date_gmt'], $future_date ); 2611 2612 $post = get_post( $post_id, 'ARRAY_A' ); 2613 $this->assertEquals( $post['post_date_gmt'], '0000-00-00 00:00:00' ); 2614 $this->assertNotEquals( $new_data['date_gmt'], $future_date ); 2615 $this->assertNotEquals( $new_data['date'], $future_date ); 2616 } 2617 2558 2618 public function test_rest_update_post_raw() { 2559 2619 wp_set_current_user( self::$editor_id ); 2560 2620