From dd68c64a6985901aab6b12facd78908e6e9bf2ec Mon Sep 17 00:00:00 2001
From: Max Lyuchin <maxim.lyuchin@gmail.com>
Date: Wed, 15 Mar 2023 15:40:52 +0300
Subject: [PATCH 1/3] Force publish scheduled post in bulk edit

---
 src/wp-admin/includes/post.php | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php
index 824b96945075..1a0473c46dd2 100644
--- a/src/wp-admin/includes/post.php
+++ b/src/wp-admin/includes/post.php
@@ -664,6 +664,12 @@ 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 ( 'future' === $post->post_status && '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;

From 558e6e49a6dbc2703d4ebbdc4ae9f0c3cc4d4d6a Mon Sep 17 00:00:00 2001
From: Max Lyuchin <maxim.lyuchin@gmail.com>
Date: Wed, 15 Mar 2023 15:41:02 +0300
Subject: [PATCH 2/3] Unit tests

---
 tests/phpunit/tests/admin/includesPost.php | 31 ++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php
index 799d927dfa90..9a94d1e3d5f0 100644
--- a/tests/phpunit/tests/admin/includesPost.php
+++ b/tests/phpunit/tests/admin/includesPost.php
@@ -293,6 +293,37 @@ 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 41396
 	 */

From 4a520a124e8eac4149475d27c9e7c095f7b63042 Mon Sep 17 00:00:00 2001
From: Max Lyuchin <maxim.lyuchin@gmail.com>
Date: Wed, 15 Mar 2023 15:47:18 +0300
Subject: [PATCH 3/3] code styles

---
 src/wp-admin/includes/post.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php
index 1a0473c46dd2..0c9f9250ed9f 100644
--- a/src/wp-admin/includes/post.php
+++ b/src/wp-admin/includes/post.php
@@ -669,7 +669,7 @@ function bulk_edit_posts( $post_data = null ) {
 			$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;
