From d4fbf0467162092fecf5f432a034633de61be559 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Mon, 9 Feb 2015 22:53:40 +0000 Subject: [PATCH] Don't parse empty 'tax_input' keys in `edit_post()`. This fixes a bug introduced in [31359] where saving a tax_input that contained only whitespace would result in a random tag being erroneously added to the post. See #30615. git-svn-id: https://develop.svn.wordpress.org/trunk@31392 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/post.php | 5 ++++ tests/phpunit/tests/admin/includesPost.php | 30 ++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index 13d9d44bd8..1e6d9fa3bc 100644 --- a/src/wp-admin/includes/post.php +++ b/src/wp-admin/includes/post.php @@ -337,6 +337,11 @@ function edit_post( $post_data = null ) { $clean_terms = array(); foreach ( $terms as $term ) { + // Empty terms are invalid input. + if ( empty( $term ) ) { + continue; + } + $_term = get_terms( $taxonomy, array( 'name' => $term, 'fields' => 'ids', diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php index 647ff61ecd..0388a82a09 100644 --- a/tests/phpunit/tests/admin/includesPost.php +++ b/tests/phpunit/tests/admin/includesPost.php @@ -175,6 +175,36 @@ class Tests_Admin_includesPost extends WP_UnitTestCase { $this->assertContains( 'baz', wp_list_pluck( $found, 'name' ) ); } + /** + * @ticket 30615 + */ + public function test_edit_post_should_not_create_terms_for_an_empty_tag_input_field() { + $u = $this->factory->user->create( array( 'role' => 'editor' ) ); + wp_set_current_user( $u ); + + register_taxonomy( 'wptests_tax', array( 'post' ) ); + $t1 = $this->factory->term->create( array( + 'taxonomy' => 'wptests_tax', + 'name' => 'foo', + 'slug' => 'bar', + ) ); + + $p = $this->factory->post->create(); + + $post_data = array( + 'post_ID' => $p, + 'tax_input' => array( + 'wptests_tax' => ' ', + ), + ); + + edit_post( $post_data ); + + $found = wp_get_post_terms( $p, 'wptests_tax' ); + + $this->assertEmpty( $found ); + } + /** * @ticket 27792 */