From 8fe01c0621a824b4b996e23208a37a1a6d690b7b Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 21 Jan 2019 18:04:55 +0000 Subject: [PATCH] Post Formats: Prevent Bulk Edit from unintentionally changing post format to Standard even if set to "No change". Correct the logic in [41187]. Props birgire, mukesh27, lanche86. Fixes #44914. See #41396. git-svn-id: https://develop.svn.wordpress.org/trunk@44670 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/post.php | 4 ++- tests/phpunit/tests/admin/includesPost.php | 35 ++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index 7a1e2a81b4..0e19876e51 100644 --- a/src/wp-admin/includes/post.php +++ b/src/wp-admin/includes/post.php @@ -610,9 +610,11 @@ function bulk_edit_posts( $post_data = null ) { if ( isset( $shared_post_data['post_format'] ) ) { set_post_format( $post_ID, $shared_post_data['post_format'] ); - unset( $post_data['tax_input']['post_format'] ); } + // Prevent wp_insert_post() from overwriting post format with the old data. + unset( $post_data['tax_input']['post_format'] ); + $updated[] = wp_update_post( $post_data ); if ( isset( $post_data['sticky'] ) && current_user_can( $ptype->cap->edit_others_posts ) ) { diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php index ee3edb8aa0..76802b4fa1 100644 --- a/tests/phpunit/tests/admin/includesPost.php +++ b/tests/phpunit/tests/admin/includesPost.php @@ -254,6 +254,41 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase { $this->assertEquals( 'closed', $post->ping_status ); } + /** + * The bulk_edit_posts() function should preserve the post format + * when it's unchanged. + * + * @ticket 44914 + */ + public function test_bulk_edit_posts_should_preserve_post_format_when_unchanged() { + wp_set_current_user( self::$admin_id ); + + $post_ids = self::factory()->post->create_many( 3 ); + + set_post_format( $post_ids[0], 'image' ); + set_post_format( $post_ids[1], 'aside' ); + + $request = array( + 'post_format' => -1, // Don't change the post format. + '_status' => -1, + 'post' => $post_ids, + ); + + bulk_edit_posts( $request ); + + $terms1 = get_the_terms( $post_ids[0], 'post_format' ); + $terms2 = get_the_terms( $post_ids[1], 'post_format' ); + $terms3 = get_the_terms( $post_ids[2], 'post_format' ); + + $this->assertSame( 'post-format-image', $terms1[0]->slug ); + $this->assertSame( 'post-format-aside', $terms2[0]->slug ); + $this->assertFalse( $terms3 ); + + $this->assertSame( 'image', get_post_format( $post_ids[0] ) ); + $this->assertSame( 'aside', get_post_format( $post_ids[1] ) ); + $this->assertFalse( get_post_format( $post_ids[2] ) ); + } + /** * @ticket 41396 */