Make WordPress Core

Ticket #39953: 39953.1.patch

File 39953.1.patch, 9.5 KB (added by mnelson4, 4 years ago)

Includes 39953.patch's changes, and adds date_floating to register_post_type and the REST API post statuses responses

  • src/wp-includes/post.php

    diff --git src/wp-includes/post.php src/wp-includes/post.php
    index 4876f40f0d..06aaf19d31 100644
    function create_initial_post_types() { 
    345345                                'Draft <span class="count">(%s)</span>',
    346346                                'Drafts <span class="count">(%s)</span>'
    347347                        ),
     348                        'date_floating' => true,
    348349                )
    349350        );
    350351
    function create_initial_post_types() { 
    397398                        'label'    => 'auto-draft',
    398399                        'internal' => true,
    399400                        '_builtin' => true, /* internal use only. */
     401                        'date_floating' => true
    400402                )
    401403        );
    402404
    function _wp_privacy_statuses() { 
    10181020 *                                                  the top of the edit listings,
    10191021 *                                                  e.g. All (12) | Published (9) | My Custom Status (2)
    10201022 *                                                  Default is value of $internal.
     1023 *     @type bool        $date_floating             Whether the post has a floating creation date.
     1024 *                                                  Default to false.
    10211025 * }
    10221026 * @return object
    10231027 */
    function register_post_status( $post_status, $args = array() ) { 
    10411045                'publicly_queryable'        => null,
    10421046                'show_in_admin_status_list' => null,
    10431047                'show_in_admin_all_list'    => null,
     1048                'date_floating'             => null,
    10441049        );
    10451050        $args     = wp_parse_args( $args, $defaults );
    10461051        $args     = (object) $args;
    function register_post_status( $post_status, $args = array() ) { 
    10851090                $args->show_in_admin_status_list = ! $args->internal;
    10861091        }
    10871092
     1093        if ( null === $args->date_floating ) {
     1094                $args->date_floating = false;
     1095        }
     1096
    10881097        if ( false === $args->label ) {
    10891098                $args->label = $post_status;
    10901099        }
  • src/wp-includes/rest-api/endpoints/class-wp-rest-post-statuses-controller.php

    diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-post-statuses-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-post-statuses-controller.php
    index 155c8aaa96..23ff60e90d 100644
    class WP_REST_Post_Statuses_Controller extends WP_REST_Controller { 
    234234                        $data['slug'] = $status->name;
    235235                }
    236236
     237                if ( in_array('date_floating', $fields, true ) ) {
     238                        $data['date_floating'] = $status->date_floating;
     239                }
     240
    237241                $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
    238242                $data    = $this->add_additional_fields_to_object( $data, $request );
    239243                $data    = $this->filter_response_by_context( $data, $context );
    class WP_REST_Post_Statuses_Controller extends WP_REST_Controller { 
    319323                                        'context'     => array( 'embed', 'view', 'edit' ),
    320324                                        'readonly'    => true,
    321325                                ),
     326                                'date_floating'=> array(
     327                                        'description' => __( 'Whether posts of this status may have floating published dates.' ),
     328                                        'type'        => 'boolean',
     329                                        'context'     => array( 'view', 'edit' ),
     330                                        'readonly'    => true
     331                                )
    322332                        ),
    323333                );
    324334
  • 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 8b4dfef229..9d3b489488 100644
    class WP_REST_Posts_Controller extends WP_REST_Controller { 
    10211021
    10221022                // Post date.
    10231023                if ( ! empty( $schema['properties']['date'] ) && ! empty( $request['date'] ) ) {
     1024                        $current_date = isset( $prepared_post->ID ) ? get_post( $prepared_post->ID )->post_date : false;
    10241025                        $date_data = rest_get_date_with_gmt( $request['date'] );
    10251026
    1026                         if ( ! empty( $date_data ) ) {
     1027                        if ( ! empty( $date_data ) && $current_date !== $date_data[0] ) {
    10271028                                list( $prepared_post->post_date, $prepared_post->post_date_gmt ) = $date_data;
    10281029                                $prepared_post->edit_date                                        = true;
    10291030                        }
    10301031                } elseif ( ! empty( $schema['properties']['date_gmt'] ) && ! empty( $request['date_gmt'] ) ) {
     1032                        $current_date = isset( $prepared_post->ID ) ? get_post( $prepared_post->ID )->post_date_gmt : false;
    10311033                        $date_data = rest_get_date_with_gmt( $request['date_gmt'], true );
    10321034
    1033                         if ( ! empty( $date_data ) ) {
     1035                        if ( ! empty( $date_data ) && $current_date !== $date_data[1] ) {
    10341036                                list( $prepared_post->post_date, $prepared_post->post_date_gmt ) = $date_data;
    10351037                                $prepared_post->edit_date                                        = true;
    10361038                        }
  • tests/phpunit/tests/rest-api/rest-post-statuses-controller.php

    diff --git tests/phpunit/tests/rest-api/rest-post-statuses-controller.php tests/phpunit/tests/rest-api/rest-post-statuses-controller.php
    index 1fe933b008..1a32293c88 100644
    class WP_Test_REST_Post_Statuses_Controller extends WP_Test_REST_Controller_Test 
    153153                $response   = rest_get_server()->dispatch( $request );
    154154                $data       = $response->get_data();
    155155                $properties = $data['schema']['properties'];
    156                 $this->assertEquals( 7, count( $properties ) );
     156                $this->assertEquals( 8, count( $properties ) );
    157157                $this->assertArrayHasKey( 'name', $properties );
    158158                $this->assertArrayHasKey( 'private', $properties );
    159159                $this->assertArrayHasKey( 'protected', $properties );
    class WP_Test_REST_Post_Statuses_Controller extends WP_Test_REST_Controller_Test 
    161161                $this->assertArrayHasKey( 'queryable', $properties );
    162162                $this->assertArrayHasKey( 'show_in_list', $properties );
    163163                $this->assertArrayHasKey( 'slug', $properties );
     164                $this->assertArrayhasKey( 'date_floating', $properties );
    164165        }
    165166
    166167        public function test_get_additional_field_registration() {
    class WP_Test_REST_Post_Statuses_Controller extends WP_Test_REST_Controller_Test 
    217218                        ),
    218219                        array_keys( $links )
    219220                );
     221                $this->assertEquals( $status_obj->date_floating, $data['date_floating']);
    220222        }
    221223
    222224        protected function check_post_status_object_response( $response ) {
  • 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..422eb79cf2 100644
    class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    42834283
    42844284        }
    42854285
     4286        public function test_putting_same_publish_date_does_not_remove_floating_date() {
     4287
     4288                wp_set_current_user( self::$superadmin_id );
     4289
     4290                $time = date( 'Y-m-d H:i:s' );
     4291
     4292                $post = self::factory()->post->create_and_get( array(
     4293                        'post_status' => 'draft',
     4294                        'post_date'   => $time,
     4295                ) );
     4296
     4297                $this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt );
     4298
     4299                $get = new WP_REST_Request( 'GET', "/wp/v2/posts/{$post->ID}" );
     4300                $get->set_query_params( array( 'context' => 'edit' ) );
     4301
     4302                $get      = rest_get_server()->dispatch( $get );
     4303                $get_body = $get->get_data();
     4304
     4305                $put = new WP_REST_Request( 'PUT', "/wp/v2/posts/{$post->ID}" );
     4306                $put->set_body_params( $get_body );
     4307
     4308                $response = rest_get_server()->dispatch( $put );
     4309                $body     = $response->get_data();
     4310
     4311                $this->assertEquals( $get_body['date'], $body['date'] );
     4312                $this->assertEquals( $get_body['date_gmt'], $body['date_gmt'] );
     4313
     4314                $this->assertEquals( '0000-00-00 00:00:00', get_post( $post->ID )->post_date_gmt );
     4315        }
     4316
     4317        public function test_putting_different_publish_date_removes_floating_date() {
     4318
     4319                wp_set_current_user( self::$superadmin_id );
     4320
     4321                $time     = date( 'Y-m-d H:i:s' );
     4322                $new_time = date( 'Y-m-d H:i:s', strtotime( '+1 week' ) );
     4323
     4324                $post = self::factory()->post->create_and_get( array(
     4325                        'post_status' => 'draft',
     4326                        'post_date'   => $time,
     4327                ) );
     4328
     4329                $this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt );
     4330
     4331                $get = new WP_REST_Request( 'GET', "/wp/v2/posts/{$post->ID}" );
     4332                $get->set_query_params( array( 'context' => 'edit' ) );
     4333
     4334                $get      = rest_get_server()->dispatch( $get );
     4335                $get_body = $get->get_data();
     4336
     4337                $put = new WP_REST_Request( 'PUT', "/wp/v2/posts/{$post->ID}" );
     4338                $put->set_body_params( array_merge( $get_body, array(
     4339                        'date' => mysql_to_rfc3339( $new_time ),
     4340                ) ) );
     4341
     4342                $response = rest_get_server()->dispatch( $put );
     4343                $body     = $response->get_data();
     4344
     4345                $this->assertEquals( mysql_to_rfc3339( $new_time ), $body['date'] );
     4346
     4347                $this->assertNotEquals( '0000-00-00 00:00:00', get_post( $post->ID )->post_date_gmt );
     4348        }
     4349
     4350        public function test_publishing_post_with_same_date_removes_floating_date() {
     4351
     4352                wp_set_current_user( self::$superadmin_id );
     4353
     4354                $time = date( 'Y-m-d H:i:s' );
     4355
     4356                $post = self::factory()->post->create_and_get( array(
     4357                        'post_status' => 'draft',
     4358                        'post_date'   => $time,
     4359                ) );
     4360
     4361                $this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt );
     4362
     4363                $get = new WP_REST_Request( 'GET', "/wp/v2/posts/{$post->ID}" );
     4364                $get->set_query_params( array( 'context' => 'edit' ) );
     4365
     4366                $get      = rest_get_server()->dispatch( $get );
     4367                $get_body = $get->get_data();
     4368
     4369                $put = new WP_REST_Request( 'PUT', "/wp/v2/posts/{$post->ID}" );
     4370                $put->set_body_params( array_merge( $get_body, array(
     4371                        'status' => 'publish',
     4372                ) ) );
     4373
     4374                $response = rest_get_server()->dispatch( $put );
     4375                $body     = $response->get_data();
     4376
     4377                $this->assertEquals( $get_body['date'], $body['date'] );
     4378                $this->assertEquals( $get_body['date_gmt'], $body['date_gmt'] );
     4379
     4380                $this->assertNotEquals( '0000-00-00 00:00:00', get_post( $post->ID )->post_date_gmt );
     4381        }
     4382
    42864383        public function tearDown() {
    42874384                _unregister_post_type( 'private-post' );
    42884385                _unregister_post_type( 'youseeme' );