Skip to content

Commit eb0ae56

Browse files
committed
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
1 parent 662e1c6 commit eb0ae56

File tree

3 files changed

+72
-14
lines changed

3 files changed

+72
-14
lines changed

src/js/_enqueues/admin/inline-edit-post.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -491,13 +491,6 @@ window.wp = window.wp || {};
491491
};
492492

493493
fields = $('#edit-'+id).find(':input').serialize();
494-
495-
var status = $(':input[name="_status"]').val();
496-
497-
if ( [ 'draft', 'pending', 'auto-draft' ].includes( status ) ) {
498-
params.edit_date = 'false';
499-
}
500-
501494
params = fields + '&' + $.param(params);
502495

503496
// Make Ajax request.

src/wp-admin/includes/post.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,6 @@ function _wp_translate_postdata( $update = false, $post_data = null ) {
171171
}
172172
}
173173

174-
if ( isset( $post_data['edit_date'] ) && 'false' === $post_data['edit_date'] ) {
175-
$post_data['edit_date'] = false;
176-
}
177-
178174
if ( ! empty( $post_data['edit_date'] ) ) {
179175
$aa = $post_data['aa'];
180176
$mm = $post_data['mm'];
@@ -197,7 +193,19 @@ function _wp_translate_postdata( $update = false, $post_data = null ) {
197193
return new WP_Error( 'invalid_date', __( 'Invalid date.' ) );
198194
}
199195

200-
$post_data['post_date_gmt'] = get_gmt_from_date( $post_data['post_date'] );
196+
/*
197+
* Only assign a post date if the user has explicitly set a new value.
198+
* See #59125 and #19907.
199+
*/
200+
$previous_date = $post_id ? get_post_field( 'post_date', $post_id ) : false;
201+
if ( $previous_date && $previous_date !== $post_data['post_date'] ) {
202+
$post_data['edit_date'] = true;
203+
$post_data['post_date_gmt'] = get_gmt_from_date( $post_data['post_date'] );
204+
} else {
205+
$post_data['edit_date'] = false;
206+
unset( $post_data['post_date'] );
207+
unset( $post_data['post_date_gmt'] );
208+
}
201209
}
202210

203211
if ( isset( $post_data['post_category'] ) ) {

tests/phpunit/tests/ajax/wpAjaxInlineSave.php

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function test_dont_process_terms_if_taxonomy_does_not_allow_show_on_quick
8989
}
9090

9191
/**
92-
* When updating a draft in quick edit mode, it should not set the publish date of the post when this one will be published.
92+
* When updating a draft in quick edit mode, it should not set the publish date of the post if the date passed is unchanged.
9393
*
9494
* @ticket 19907
9595
*
@@ -124,6 +124,63 @@ public function test_quick_edit_draft_should_not_set_publish_date() {
124124
$_POST['screen'] = 'edit-post';
125125
$_POST['post_view'] = 'list';
126126
$_POST['edit_date'] = 'false';
127+
$_POST['mm'] = get_the_date( 'm', $post );
128+
$_POST['jj'] = get_the_date( 'd', $post );
129+
$_POST['aa'] = get_the_date( 'Y', $post );
130+
$_POST['hh'] = get_the_date( 'H', $post );
131+
$_POST['mn'] = get_the_date( 'i', $post );
132+
$_POST['ss'] = get_the_date( 's', $post );
133+
134+
// Make the request.
135+
try {
136+
$this->_handleAjax( 'inline-save' );
137+
} catch ( WPAjaxDieContinueException $e ) {
138+
unset( $e );
139+
}
140+
141+
$post = get_post( $post->ID );
142+
143+
$post_date = sprintf( '%04d-%02d-%02d %02d:%02d:%02d', $_POST['aa'], $_POST['mm'], $_POST['jj'], $_POST['hh'], $_POST['mn'], $_POST['ss'] );
144+
145+
$this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt );
146+
}
147+
148+
/**
149+
* When updating a draft in quick edit mode, it should set the publish date of the post if there is a new date set.
150+
*
151+
* @ticket 59125
152+
*
153+
* @covers ::edit_post
154+
*/
155+
public function test_quick_edit_draft_should_set_publish_date() {
156+
// Become an administrator.
157+
$this->_setRole( 'administrator' );
158+
159+
$user = get_current_user_id();
160+
161+
$post = self::factory()->post->create_and_get(
162+
array(
163+
'post_status' => 'draft',
164+
'post_author' => $user,
165+
)
166+
);
167+
168+
$this->assertSame( 'draft', $post->post_status );
169+
170+
$this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt );
171+
172+
// Set up a request.
173+
$_POST['_inline_edit'] = wp_create_nonce( 'inlineeditnonce' );
174+
$_POST['post_ID'] = $post->ID;
175+
$_POST['post_type'] = 'post';
176+
$_POST['content'] = 'content test';
177+
$_POST['excerpt'] = 'excerpt test';
178+
$_POST['_status'] = $post->post_status;
179+
$_POST['post_status'] = $post->post_status;
180+
$_POST['post_author'] = $user;
181+
$_POST['screen'] = 'edit-post';
182+
$_POST['post_view'] = 'list';
183+
$_POST['edit_date'] = 'true';
127184
$_POST['mm'] = '09';
128185
$_POST['jj'] = 11;
129186
$_POST['aa'] = 2020;
@@ -140,6 +197,6 @@ public function test_quick_edit_draft_should_not_set_publish_date() {
140197

141198
$post = get_post( $post->ID );
142199

143-
$this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt );
200+
$this->assertEquals( '2020-09-11 19:20:11', $post->post_date_gmt );
144201
}
145202
}

0 commit comments

Comments
 (0)