Quick/Bulk Edit: Fix inability to quick edit draft post date.

Follow up to [56022] to fix inability to set a date/time in quick editing. Allow a user to set a quick/edit date while preventing accidental date assignments per the original intent.

Props tristanleboss, ivanzhuck, tibbsa, sabernhardt, sergeybiryukov, oandregal, khokansardar, joedolson, shailu25.
Fixes #59125. See #19907.

git-svn-id: https://develop.svn.wordpress.org/trunk@56802 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Joe Dolson 2023-10-08 20:08:08 +00:00
parent 662e1c639f
commit eb0ae5679a
3 changed files with 72 additions and 14 deletions

View File

@ -491,13 +491,6 @@ window.wp = window.wp || {};
}; };
fields = $('#edit-'+id).find(':input').serialize(); 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); params = fields + '&' + $.param(params);
// Make Ajax request. // Make Ajax request.

View File

@ -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'] ) ) { if ( ! empty( $post_data['edit_date'] ) ) {
$aa = $post_data['aa']; $aa = $post_data['aa'];
$mm = $post_data['mm']; $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.' ) ); 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'] ) ) { if ( isset( $post_data['post_category'] ) ) {

View File

@ -89,7 +89,7 @@ class Tests_Ajax_wpAjaxInlineSave extends WP_Ajax_UnitTestCase {
} }
/** /**
* 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 * @ticket 19907
* *
@ -124,6 +124,63 @@ class Tests_Ajax_wpAjaxInlineSave extends WP_Ajax_UnitTestCase {
$_POST['screen'] = 'edit-post'; $_POST['screen'] = 'edit-post';
$_POST['post_view'] = 'list'; $_POST['post_view'] = 'list';
$_POST['edit_date'] = 'false'; $_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['mm'] = '09';
$_POST['jj'] = 11; $_POST['jj'] = 11;
$_POST['aa'] = 2020; $_POST['aa'] = 2020;
@ -140,6 +197,6 @@ class Tests_Ajax_wpAjaxInlineSave extends WP_Ajax_UnitTestCase {
$post = get_post( $post->ID ); $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 );
} }
} }