From e635540d1c990fc9e49af92ad926a217e086418e Mon Sep 17 00:00:00 2001 From: Jb Audras Date: Sun, 2 Jul 2023 08:02:45 +0000 Subject: [PATCH] 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 --- src/wp-admin/includes/post.php | 9 +++ tests/phpunit/tests/admin/includesPost.php | 66 ++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index 37ab30721d..f8a478dfd9 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 ec72c68f6d..4ce59dbabe 100644 --- a/tests/phpunit/tests/admin/includesPost.php +++ b/tests/phpunit/tests/admin/includesPost.php @@ -293,6 +293,72 @@ class Tests_Admin_IncludesPost extends WP_UnitTestCase { $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 */