diff --git a/src/js/_enqueues/admin/inline-edit-post.js b/src/js/_enqueues/admin/inline-edit-post.js index 5fff8c3f28825..1f572353ff00c 100644 --- a/src/js/_enqueues/admin/inline-edit-post.js +++ b/src/js/_enqueues/admin/inline-edit-post.js @@ -491,13 +491,6 @@ window.wp = window.wp || {}; }; fields = $('#edit-'+id).find(':input').serialize(); - - var status = $(':input[name="_status"]').val(); - - if ( [ 'draft', 'pending', 'auto-draft' ].includes( status ) ) { - params.edit_date = 'false'; - } - params = fields + '&' + $.param(params); // Make Ajax request. diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index cac831cbd9468..3b1603a38cdcc 100644 --- a/src/wp-admin/includes/post.php +++ b/src/wp-admin/includes/post.php @@ -171,10 +171,6 @@ function _wp_translate_postdata( $update = false, $post_data = null ) { } } - if ( isset( $post_data['edit_date'] ) && 'false' === $post_data['edit_date'] ) { - $post_data['edit_date'] = false; - } - if ( ! empty( $post_data['edit_date'] ) ) { $aa = $post_data['aa']; $mm = $post_data['mm']; @@ -197,7 +193,19 @@ function _wp_translate_postdata( $update = false, $post_data = null ) { return new WP_Error( 'invalid_date', __( 'Invalid date.' ) ); } - $post_data['post_date_gmt'] = get_gmt_from_date( $post_data['post_date'] ); + /* + * Only assign a post date if the user has explicitly set a new value. + * See #59125 and #19907. + */ + $previous_date = $post_id ? get_post_field( 'post_date', $post_id ) : false; + if ( $previous_date && $previous_date !== $post_data['post_date'] ) { + $post_data['edit_date'] = true; + $post_data['post_date_gmt'] = get_gmt_from_date( $post_data['post_date'] ); + } else { + $post_data['edit_date'] = false; + unset( $post_data['post_date'] ); + unset( $post_data['post_date_gmt'] ); + } } if ( isset( $post_data['post_category'] ) ) { diff --git a/tests/phpunit/tests/ajax/wpAjaxInlineSave.php b/tests/phpunit/tests/ajax/wpAjaxInlineSave.php index 3537def1dcce2..2edd630de8c64 100644 --- a/tests/phpunit/tests/ajax/wpAjaxInlineSave.php +++ b/tests/phpunit/tests/ajax/wpAjaxInlineSave.php @@ -89,7 +89,7 @@ public function test_dont_process_terms_if_taxonomy_does_not_allow_show_on_quick } /** - * When updating a draft in quick edit mode, it should not set the publish date of the post when this one will be published. + * When updating a draft in quick edit mode, it should not set the publish date of the post if the date passed is unchanged. * * @ticket 19907 * @@ -124,6 +124,63 @@ public function test_quick_edit_draft_should_not_set_publish_date() { $_POST['screen'] = 'edit-post'; $_POST['post_view'] = 'list'; $_POST['edit_date'] = 'false'; + $_POST['mm'] = get_the_date( 'm', $post ); + $_POST['jj'] = get_the_date( 'd', $post ); + $_POST['aa'] = get_the_date( 'Y', $post ); + $_POST['hh'] = get_the_date( 'H', $post ); + $_POST['mn'] = get_the_date( 'i', $post ); + $_POST['ss'] = get_the_date( 's', $post ); + + // Make the request. + try { + $this->_handleAjax( 'inline-save' ); + } catch ( WPAjaxDieContinueException $e ) { + unset( $e ); + } + + $post = get_post( $post->ID ); + + $post_date = sprintf( '%04d-%02d-%02d %02d:%02d:%02d', $_POST['aa'], $_POST['mm'], $_POST['jj'], $_POST['hh'], $_POST['mn'], $_POST['ss'] ); + + $this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt ); + } + + /** + * When updating a draft in quick edit mode, it should set the publish date of the post if there is a new date set. + * + * @ticket 59125 + * + * @covers ::edit_post + */ + public function test_quick_edit_draft_should_set_publish_date() { + // Become an administrator. + $this->_setRole( 'administrator' ); + + $user = get_current_user_id(); + + $post = self::factory()->post->create_and_get( + array( + 'post_status' => 'draft', + 'post_author' => $user, + ) + ); + + $this->assertSame( 'draft', $post->post_status ); + + $this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt ); + + // Set up a request. + $_POST['_inline_edit'] = wp_create_nonce( 'inlineeditnonce' ); + $_POST['post_ID'] = $post->ID; + $_POST['post_type'] = 'post'; + $_POST['content'] = 'content test'; + $_POST['excerpt'] = 'excerpt test'; + $_POST['_status'] = $post->post_status; + $_POST['post_status'] = $post->post_status; + $_POST['post_author'] = $user; + $_POST['screen'] = 'edit-post'; + $_POST['post_view'] = 'list'; + $_POST['edit_date'] = 'true'; $_POST['mm'] = '09'; $_POST['jj'] = 11; $_POST['aa'] = 2020; @@ -140,6 +197,6 @@ public function test_quick_edit_draft_should_not_set_publish_date() { $post = get_post( $post->ID ); - $this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt ); + $this->assertEquals( '2020-09-11 19:20:11', $post->post_date_gmt ); } }