Skip to content

Commit

Permalink
Quick/Bulk Edit: Fix inability to quick edit draft post date.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
joedolson committed Oct 8, 2023
1 parent 662e1c6 commit eb0ae56
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 14 deletions.
7 changes: 0 additions & 7 deletions src/js/_enqueues/admin/inline-edit-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 13 additions & 5 deletions src/wp-admin/includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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'] ) ) {
Expand Down
61 changes: 59 additions & 2 deletions tests/phpunit/tests/ajax/wpAjaxInlineSave.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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;
Expand All @@ -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 );
}
}

0 comments on commit eb0ae56

Please sign in to comment.