Skip to content

Commit 5f1ad2d

Browse files
committed
In wp_update_term(), limit duplicate slug checks to the same taxonomy as the updated term.
In 4.1 [30240], `wp_insert_term()` was modified to allow the creation of terms with duplicate slugs, as long as the terms are in different taxonomies. `wp_update_term()` didn't get the corresponding modification, with the result that term updates fail when the term being updated shares a slug with an older term, regardless of that older term's taxonomy. Props ipm-frommen. Fixes #30780 for trunk. git-svn-id: https://develop.svn.wordpress.org/trunk@30985 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 061bfdd commit 5f1ad2d

2 files changed

Lines changed: 147 additions & 7 deletions

File tree

src/wp-includes/taxonomy.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3353,8 +3353,8 @@ function wp_update_term( $term_id, $taxonomy, $args = array() ) {
33533353
$parent = apply_filters( 'wp_update_term_parent', $args['parent'], $term_id, $taxonomy, $parsed_args, $args );
33543354

33553355
// Check for duplicate slug
3356-
$id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE slug = %s", $slug ) );
3357-
if ( $id && ($id != $term_id) ) {
3356+
$duplicate = get_term_by( 'slug', $slug, $taxonomy );
3357+
if ( $duplicate && $duplicate->term_id != $term_id ) {
33583358
// If an empty slug was passed or the parent changed, reset the slug to something unique.
33593359
// Otherwise, bail.
33603360
if ( $empty_slug || ( $parent != $term['parent']) )

tests/phpunit/tests/term.php

Lines changed: 145 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ public function test_wp_update_term_slug_set_slug() {
640640
/**
641641
* @ticket 5809
642642
*/
643-
public function test_wp_update_term_duplicate_slug_same_taxonomy() {
643+
public function test_wp_update_term_should_not_create_duplicate_slugs_within_the_same_taxonomy() {
644644
register_taxonomy( 'wptests_tax', 'post' );
645645

646646
$t1 = $this->factory->term->create( array(
@@ -650,7 +650,7 @@ public function test_wp_update_term_duplicate_slug_same_taxonomy() {
650650
) );
651651

652652
$t2 = $this->factory->term->create( array(
653-
'name' => 'Foo',
653+
'name' => 'Bar',
654654
'slug' => 'bar',
655655
'taxonomy' => 'wptests_tax',
656656
) );
@@ -666,7 +666,7 @@ public function test_wp_update_term_duplicate_slug_same_taxonomy() {
666666
/**
667667
* @ticket 5809
668668
*/
669-
public function test_wp_update_term_duplicate_slug_different_taxonomy() {
669+
public function test_wp_update_term_should_allow_duplicate_slugs_in_different_taxonomy() {
670670
register_taxonomy( 'wptests_tax', 'post' );
671671
register_taxonomy( 'wptests_tax_2', 'post' );
672672

@@ -686,8 +686,78 @@ public function test_wp_update_term_duplicate_slug_different_taxonomy() {
686686
'slug' => 'foo',
687687
) );
688688

689-
$this->assertWPError( $updated );
690-
$this->assertSame( 'duplicate_term_slug', $updated->get_error_code() );
689+
$this->assertFalse( is_wp_error( $updated ) );
690+
691+
$t1_term = get_term( $t1, 'wptests_tax' );
692+
$t2_term = get_term( $t2, 'wptests_tax_2' );
693+
$this->assertSame( $t1_term->slug, $t2_term->slug );
694+
}
695+
696+
/**
697+
* @ticket 30780
698+
*/
699+
public function test_wp_update_term_should_allow_duplicate_names_in_different_taxonomies() {
700+
register_taxonomy( 'wptests_tax', 'post' );
701+
register_taxonomy( 'wptests_tax_2', 'post' );
702+
703+
$t1 = $this->factory->term->create( array(
704+
'name' => 'Foo',
705+
'slug' => 'foo',
706+
'taxonomy' => 'wptests_tax',
707+
) );
708+
709+
$t2 = $this->factory->term->create( array(
710+
'name' => 'Bar',
711+
'slug' => 'bar',
712+
'taxonomy' => 'wptests_tax_2',
713+
) );
714+
715+
$updated = wp_update_term( $t2, 'wptests_tax_2', array(
716+
'name' => 'Foo',
717+
) );
718+
719+
$this->assertFalse( is_wp_error( $updated ) );
720+
721+
$t2_term = get_term( $t2, 'wptests_tax_2' );
722+
$this->assertSame( 'Foo', $t2_term->name );
723+
}
724+
725+
/**
726+
* @ticket 30780
727+
*/
728+
public function test_wp_update_term_should_allow_duplicate_names_at_different_levels_of_the_same_taxonomy() {
729+
register_taxonomy( 'wptests_tax', 'post', array(
730+
'hierarchical' => true,
731+
) );
732+
733+
$t1 = $this->factory->term->create( array(
734+
'name' => 'Foo',
735+
'slug' => 'foo',
736+
'taxonomy' => 'wptests_tax',
737+
) );
738+
739+
$t2 = $this->factory->term->create( array(
740+
'name' => 'Bar',
741+
'slug' => 'bar',
742+
'taxonomy' => 'wptests_tax',
743+
'parent' => $t1,
744+
) );
745+
746+
$t3 = $this->factory->term->create( array(
747+
'name' => 'Bar Child',
748+
'slug' => 'bar-child',
749+
'taxonomy' => 'wptests_tax',
750+
'parent' => $t2,
751+
) );
752+
753+
$updated = wp_update_term( $t3, 'wptests_tax', array(
754+
'name' => 'Bar',
755+
) );
756+
757+
$this->assertFalse( is_wp_error( $updated ) );
758+
759+
$t3_term = get_term( $t3, 'wptests_tax' );
760+
$this->assertSame( 'Bar', $t3_term->name );
691761
}
692762

693763
public function test_wp_update_term_alias_of_no_term_group() {
@@ -1605,6 +1675,76 @@ function test_orphan_category() {
16051675
$this->assertWPError( $cat_id2 );
16061676
}
16071677

1678+
/**
1679+
* @ticket 30780
1680+
*/
1681+
public function test_wp_update_term_should_assign_new_slug_when_reassigning_parent_as_long_as_there_is_no_other_term_with_the_same_slug() {
1682+
register_taxonomy( 'wptests_tax', 'post', array(
1683+
'hierarchical' => true,
1684+
) );
1685+
register_taxonomy( 'wptests_tax_2', 'post', array(
1686+
'hierarchical' => true,
1687+
) );
1688+
1689+
$t1 = $this->factory->term->create( array(
1690+
'taxonomy' => 'wptests_tax',
1691+
'slug' => 'parent-term',
1692+
) );
1693+
1694+
$t2 = $this->factory->term->create( array(
1695+
'taxonomy' => 'wptests_tax',
1696+
'slug' => 'foo',
1697+
) );
1698+
1699+
wp_update_term( $t2, 'wptests_tax', array(
1700+
'parent' => $t1,
1701+
) );
1702+
1703+
$t2_term = get_term( $t2, 'wptests_tax' );
1704+
1705+
$this->assertSame( 'foo', $t2_term->slug );
1706+
1707+
_unregister_taxonomy( 'wptests_tax' );
1708+
}
1709+
1710+
/**
1711+
* @ticket 30780
1712+
*/
1713+
public function test_wp_update_term_should_not_assign_new_slug_when_reassigning_parent_as_long_as_there_is_no_other_slug_conflict_within_the_taxonomy() {
1714+
register_taxonomy( 'wptests_tax', 'post', array(
1715+
'hierarchical' => true,
1716+
) );
1717+
register_taxonomy( 'wptests_tax_2', 'post', array(
1718+
'hierarchical' => true,
1719+
) );
1720+
1721+
$t1 = $this->factory->term->create( array(
1722+
'taxonomy' => 'wptests_tax',
1723+
'slug' => 'parent-term',
1724+
) );
1725+
1726+
// Same slug but in a different tax.
1727+
$t2 = $this->factory->term->create( array(
1728+
'taxonomy' => 'wptests_tax_2',
1729+
'slug' => 'foo',
1730+
) );
1731+
1732+
$t3 = $this->factory->term->create( array(
1733+
'taxonomy' => 'wptests_tax',
1734+
'slug' => 'foo',
1735+
) );
1736+
1737+
wp_update_term( $t3, 'wptests_tax', array(
1738+
'parent' => $t1,
1739+
) );
1740+
1741+
$t3_term = get_term( $t3, 'wptests_tax' );
1742+
1743+
$this->assertSame( 'foo', $t3_term->slug );
1744+
1745+
_unregister_taxonomy( 'wptests_tax' );
1746+
}
1747+
16081748
/** Helpers **********************************************************/
16091749

16101750
public function _pre_insert_term_callback() {

0 commit comments

Comments
 (0)