Skip to content

Commit e635540

Browse files
committed
Quick/Bulk Edit: Ensure scheduled posts are published when using Bulk Edit.
This changeset ensures scheduled posts are actually published when changing their status to "Published" using bulk edit. Also adds related unit tests. Props siobhan, Clorith, webcommsat, cadic, oglekler, audrasjb, pavanpatil1. Fixes #31635. git-svn-id: https://develop.svn.wordpress.org/trunk@56123 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 7f0eb07 commit e635540

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

src/wp-admin/includes/post.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,15 @@ function bulk_edit_posts( $post_data = null ) {
668668
// Prevent wp_insert_post() from overwriting post format with the old data.
669669
unset( $post_data['tax_input']['post_format'] );
670670

671+
// Reset post date of scheduled post to be published.
672+
if (
673+
in_array( $post->post_status, array( 'future', 'draft' ), true ) &&
674+
'publish' === $post_data['post_status']
675+
) {
676+
$post_data['post_date'] = current_time( 'mysql' );
677+
$post_data['post_date_gmt'] = '';
678+
}
679+
671680
$post_id = wp_update_post( $post_data );
672681
update_post_meta( $post_id, '_edit_last', get_current_user_id() );
673682
$updated[] = $post_id;

tests/phpunit/tests/admin/includesPost.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,72 @@ public function test_bulk_edit_posts_should_preserve_post_format_when_unchanged(
293293
$this->assertFalse( get_post_format( $post_ids[2] ) );
294294
}
295295

296+
/**
297+
* @ticket 31635
298+
*/
299+
public function test_bulk_edit_posts_should_publish_scheduled_post() {
300+
wp_set_current_user( self::$admin_id );
301+
302+
$post = self::factory()->post->create(
303+
array(
304+
'post_author' => self::$author_ids[0],
305+
'comment_status' => 'closed',
306+
'ping_status' => 'closed',
307+
'post_status' => 'future',
308+
'post_date' => gmdate( 'Y-m-d H:i:s', strtotime( '+1 month' ) ),
309+
)
310+
);
311+
312+
$request = array(
313+
'post_type' => 'post',
314+
'post_author' => -1,
315+
'ping_status' => -1,
316+
'comment_status' => -1,
317+
'_status' => 'publish',
318+
'post' => array( $post ),
319+
);
320+
321+
bulk_edit_posts( $request );
322+
323+
$this->assertSame( 'publish', get_post_status( $post ) );
324+
$this->assertLessThanOrEqual( gmdate( 'Y-m-d H:i:s' ), get_post_time( 'Y-m-d H:i:s', false, $post ) );
325+
}
326+
/**
327+
* @ticket 31635
328+
*/
329+
public function test_bulk_edit_posts_should_publish_draft_immediately() {
330+
wp_set_current_user( self::$admin_id );
331+
332+
// Create draft last edited a month ago
333+
$post = self::factory()->post->create(
334+
array(
335+
'post_author' => self::$author_ids[0],
336+
'comment_status' => 'closed',
337+
'ping_status' => 'closed',
338+
'post_status' => 'draft',
339+
'post_date' => gmdate( 'Y-m-d H:i:s', strtotime( '-1 month' ) ),
340+
)
341+
);
342+
343+
$request = array(
344+
'post_type' => 'post',
345+
'post_author' => -1,
346+
'ping_status' => -1,
347+
'comment_status' => -1,
348+
'_status' => 'publish',
349+
'post' => array( $post ),
350+
);
351+
352+
bulk_edit_posts( $request );
353+
354+
$this->assertSame( 'publish', get_post_status( $post ) );
355+
356+
// Expect to be published within the last minute (to consider slow testing environment).
357+
$minute_before = gmdate( 'Y-m-d H:i:s', strtotime( '-1 minute' ) );
358+
$this->assertGreaterThanOrEqual( $minute_before, get_post_time( 'Y-m-d H:i:s', false, $post ) );
359+
$this->assertLessThanOrEqual( gmdate( 'Y-m-d H:i:s' ), get_post_time( 'Y-m-d H:i:s', false, $post ) );
360+
}
361+
296362
/**
297363
* @ticket 41396
298364
*/

0 commit comments

Comments
 (0)