Make WordPress Core

Ticket #44975: 44975.2.diff

File 44975.2.diff, 4.3 KB (added by adamsilverstein, 5 years ago)
  • src/wp-includes/rest-api.php

    diff --git src/wp-includes/rest-api.php src/wp-includes/rest-api.php
    index 31223c6f30..92e6af0d41 100644
    function rest_validate_value_from_schema( $value, $args, $param = '' ) { 
    12061206        if ( isset( $args['format'] ) ) {
    12071207                switch ( $args['format'] ) {
    12081208                        case 'date-time':
    1209                                 if ( ! rest_parse_date( $value ) ) {
     1209                                if ( '' !== $value && ! rest_parse_date( $value ) ) {
    12101210                                        return new WP_Error( 'rest_invalid_date', __( 'Invalid date.' ) );
    12111211                                }
    12121212                                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 eff50e8e75..bb78d0587c 100644
    class WP_REST_Posts_Controller extends WP_REST_Controller { 
    10361036                        }
    10371037                }
    10381038
     1039                // Sending a blank date or date_gmt value resets date and date_gmt to their default values (`0000-00-00 00:00:00`).
     1040                if (
     1041                        ( isset( $request['date_gmt'] ) && '' === $request['date_gmt'] ) ||
     1042                        ( isset( $request['date'] ) && '' === $request['date'] )
     1043                ) {
     1044                        $prepared_post->post_date_gmt = null;
     1045                        $prepared_post->post_date     = null;
     1046                }
     1047
    10391048                // Post slug.
    10401049                if ( ! empty( $schema['properties']['slug'] ) && isset( $request['slug'] ) ) {
    10411050                        $prepared_post->post_name = $request['slug'];
  • 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 f03eee8e83..e328a8875f 100644
    class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    25552555                $this->assertEquals( $params['excerpt'], $post->post_excerpt );
    25562556        }
    25572557
     2558        /**
     2559         * Verify that updating a post with an empty 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( array( 'date_gmt' => $future_date, 'date' => $future_date,'title' => 'update', 'status' => 'draft' ) );
     2575                $request->set_body( wp_json_encode( $params ) );
     2576                $response = rest_get_server()->dispatch( $request );
     2577                $this->check_update_post_response( $response );
     2578                $new_data = $response->get_data();
     2579
     2580                // Verify the post is set to the future date.
     2581                $this->assertEquals( $new_data['date_gmt'], $future_date );
     2582                $this->assertEquals( $new_data['date'], $future_date );
     2583                $this->assertNotEquals( $new_data['date_gmt'], $new_data['modified_gmt'] );
     2584                $this->assertNotEquals( $new_data['date'], $new_data['modified'] );
     2585
     2586                // Update post with a blank field (date or date_gmt).
     2587                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $post_id ) );
     2588                $request->add_header( 'content-type', 'application/json' );
     2589                $params = $this->set_post_data( array( 'date_gmt' => '', 'title' => 'test', 'status' => 'draft' ) );
     2590                $request->set_body( wp_json_encode( $params ) );
     2591                $response = rest_get_server()->dispatch( $request );
     2592
     2593                // Verify the date field values are reset in the API response.
     2594                $this->check_update_post_response( $response );
     2595                $new_data = $response->get_data();
     2596                $this->assertEquals( $new_data['date_gmt'], $new_data['date'] );
     2597                $this->assertNotEquals( $new_data['date_gmt'], $future_date );
     2598
     2599                $post = get_post( $post_id, 'ARRAY_A' );
     2600                $this->assertEquals( $post['post_date_gmt'], '0000-00-00 00:00:00' );
     2601                $this->assertNotEquals( $new_data['date_gmt'], $future_date );
     2602                $this->assertNotEquals( $new_data['date'], $future_date );
     2603        }
     2604
    25582605        public function test_rest_update_post_raw() {
    25592606                wp_set_current_user( self::$editor_id );
    25602607