diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index 37ab30721d9f3..f8a478dfd94e6 100644 --- a/src/wp-admin/includes/post.php +++ b/src/wp-admin/includes/post.php @@ -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; diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php index ec72c68f6daf9..4ce59dbabe177 100644 --- a/tests/phpunit/tests/admin/includesPost.php +++ b/tests/phpunit/tests/admin/includesPost.php @@ -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 */