Taxonomy: Take 'parent' into account when checking for terms with duplicate names.

Terms with duplicate names are not allowed at the same level of a
taxonomy hierarchy. The name lookup introduced in [34809] did not
properly account for the 'parent' parameter, with the result that
the duplicate-name restriction was tighter than intended (terms
with duplicate names could not be created at different levels of
a single hierarchy).

Props mikejolley.
Fixes #39984.

git-svn-id: https://develop.svn.wordpress.org/trunk@40145 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2017-03-03 02:49:13 +00:00
parent 8a2341335b
commit d3e463cb84
2 changed files with 36 additions and 0 deletions

View File

@ -2040,6 +2040,7 @@ function wp_insert_term( $term, $taxonomy, $args = array() ) {
$name_matches = get_terms( $taxonomy, array( $name_matches = get_terms( $taxonomy, array(
'name' => $name, 'name' => $name,
'hide_empty' => false, 'hide_empty' => false,
'parent' => $args['parent'],
) ); ) );
/* /*

View File

@ -393,6 +393,41 @@ class Tests_Term_WpInsertTerm extends WP_UnitTestCase {
$this->assertSame( 'term_exists', $error->get_error_code() ); $this->assertSame( 'term_exists', $error->get_error_code() );
$this->assertSame( $t1, $error->get_error_data() ); $this->assertSame( $t1, $error->get_error_data() );
} }
/**
* @ticket 39984
*/
public function test_error_should_reference_correct_term_when_rejected_as_duplicate() {
register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
$t1 = self::factory()->term->create( array(
'name' => 'Foo',
'slug' => 'foo',
'taxonomy' => 'wptests_tax',
) );
$t2 = self::factory()->term->create( array(
'name' => 'Bar',
'slug' => 'bar',
'taxonomy' => 'wptests_tax',
) );
$t1_child = wp_insert_term( 'Child', 'wptests_tax', array(
'parent' => $t1,
) );
$t2_child = wp_insert_term( 'Child', 'wptests_tax', array(
'parent' => $t2,
) );
$error = wp_insert_term( 'Child', 'wptests_tax', array(
'parent' => $t2,
) );
$this->assertWPError( $error );
$this->assertSame( 'term_exists', $error->get_error_code() );
$this->assertSame( $t2_child['term_id'], $error->get_error_data() );
}
/** /**
* @ticket 31328 * @ticket 31328
*/ */