Make WordPress Core

Changeset 31378


Ignore:
Timestamp:
02/09/2015 03:58:35 AM (12 years ago)
Author:
dd32
Message:

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.
Merges [30985] to the 4.1 branch.
Fixes #30780.

Location:
branches/4.1
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/4.1

  • branches/4.1/src/wp-includes/taxonomy.php

    r31084 r31378  
    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.
  • branches/4.1/tests/phpunit/tests/term.php

    r30585 r31378  
    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
     
    651651
    652652                $t2 = $this->factory->term->create( array(
    653                         'name' => 'Foo',
     653                        'name' => 'Bar',
    654654                        'slug' => 'bar',
    655655                        'taxonomy' => 'wptests_tax',
     
    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' );
     
    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
     
    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
Note: See TracChangeset for help on using the changeset viewer.