Skip to content

Commit

Permalink
Quick/Bulk Edit: Ensure scheduled posts are published when using Bulk…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
audrasjb committed Jul 2, 2023
1 parent 7f0eb07 commit e635540
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/wp-admin/includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,15 @@ function bulk_edit_posts( $post_data = null ) {
// Prevent wp_insert_post() from overwriting post format with the old data.
unset( $post_data['tax_input']['post_format'] );

// Reset post date of scheduled post to be published.
if (
in_array( $post->post_status, array( 'future', 'draft' ), true ) &&
'publish' === $post_data['post_status']
) {
$post_data['post_date'] = current_time( 'mysql' );
$post_data['post_date_gmt'] = '';
}

$post_id = wp_update_post( $post_data );
update_post_meta( $post_id, '_edit_last', get_current_user_id() );
$updated[] = $post_id;
Expand Down
66 changes: 66 additions & 0 deletions tests/phpunit/tests/admin/includesPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,72 @@ public function test_bulk_edit_posts_should_preserve_post_format_when_unchanged(
$this->assertFalse( get_post_format( $post_ids[2] ) );
}

/**
* @ticket 31635
*/
public function test_bulk_edit_posts_should_publish_scheduled_post() {
wp_set_current_user( self::$admin_id );

$post = self::factory()->post->create(
array(
'post_author' => self::$author_ids[0],
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_status' => 'future',
'post_date' => gmdate( 'Y-m-d H:i:s', strtotime( '+1 month' ) ),
)
);

$request = array(
'post_type' => 'post',
'post_author' => -1,
'ping_status' => -1,
'comment_status' => -1,
'_status' => 'publish',
'post' => array( $post ),
);

bulk_edit_posts( $request );

$this->assertSame( 'publish', get_post_status( $post ) );
$this->assertLessThanOrEqual( gmdate( 'Y-m-d H:i:s' ), get_post_time( 'Y-m-d H:i:s', false, $post ) );
}
/**
* @ticket 31635
*/
public function test_bulk_edit_posts_should_publish_draft_immediately() {
wp_set_current_user( self::$admin_id );

// Create draft last edited a month ago
$post = self::factory()->post->create(
array(
'post_author' => self::$author_ids[0],
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_status' => 'draft',
'post_date' => gmdate( 'Y-m-d H:i:s', strtotime( '-1 month' ) ),
)
);

$request = array(
'post_type' => 'post',
'post_author' => -1,
'ping_status' => -1,
'comment_status' => -1,
'_status' => 'publish',
'post' => array( $post ),
);

bulk_edit_posts( $request );

$this->assertSame( 'publish', get_post_status( $post ) );

// Expect to be published within the last minute (to consider slow testing environment).
$minute_before = gmdate( 'Y-m-d H:i:s', strtotime( '-1 minute' ) );
$this->assertGreaterThanOrEqual( $minute_before, get_post_time( 'Y-m-d H:i:s', false, $post ) );
$this->assertLessThanOrEqual( gmdate( 'Y-m-d H:i:s' ), get_post_time( 'Y-m-d H:i:s', false, $post ) );
}

/**
* @ticket 41396
*/
Expand Down

0 comments on commit e635540

Please sign in to comment.