<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[31792] trunk: In `wp_insert_term()`, allow a term with an existing name if a unique `$slug` has been provided.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/31792">31792</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/31792","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>boonebgorges</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2015-03-16 11:15:34 +0000 (Mon, 16 Mar 2015)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>In `wp_insert_term()`, allow a term with an existing name if a unique `$slug` has been provided.

`wp_insert_term()` protects against the creation of terms with duplicate names
at the same level of a taxonomy hierarchy. However, it's historically been
possible to override this protection by explicitly providing a value of `$slug`
that is unique at the hierarchy tier. This ability was broken in <a href="https://core.trac.wordpress.org/changeset/31734">[31734]</a>, and
the current changeset restores the original behavior.

A number of unit tests are added and refactored in support of these changes.

See <a href="https://core.trac.wordpress.org/ticket/17689">#17689</a> for discussion of a fix that was superceded by <a href="https://core.trac.wordpress.org/changeset/31734">[31734]</a>. This commit
retains the fix for the underlying bug described in that ticket.

See <a href="https://core.trac.wordpress.org/ticket/31328">#31328</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludestaxonomyphp">trunk/src/wp-includes/taxonomy.php</a></li>
<li><a href="#trunktestsphpunitteststermphp">trunk/tests/phpunit/tests/term.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludestaxonomyphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/taxonomy.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/taxonomy.php        2015-03-16 10:45:29 UTC (rev 31791)
+++ trunk/src/wp-includes/taxonomy.php  2015-03-16 11:15:34 UTC (rev 31792)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2903,15 +2903,29 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        // Terms with duplicate names are not allowed at the same level of a taxonomy hierarchy.
-       if ( $existing_term = get_term_by( 'name', $name, $taxonomy ) ) {
-               if ( is_taxonomy_hierarchical( $taxonomy ) ) {
-                       $siblings = get_terms( $taxonomy, array( 'fields' => 'names', 'get' => 'all', 'parent' => $parent ) );
-                       if ( in_array( $name, $siblings ) ) {
-                               return new WP_Error( 'term_exists', __( 'A term with the name already exists with this parent.' ), $existing_term->term_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /*
+        * Prevent the creation of terms with duplicate names at the same level of a taxonomy hierarchy,
+        * unless a unique slug has been explicitly provided.
+        */
+       if ( $name_match = get_term_by( 'name', $name, $taxonomy ) ) {
+               $slug_match = get_term_by( 'slug', $slug, $taxonomy );
+               if ( ! $slug_provided || $name_match->slug === $slug || $slug_match ) {
+                       if ( is_taxonomy_hierarchical( $taxonomy ) ) {
+                               $siblings = get_terms( $taxonomy, array( 'get' => 'all', 'parent' => $parent ) );
+
+                               $existing_term = null;
+                               if ( $name_match->slug === $slug && in_array( $name, wp_list_pluck( $siblings, 'name' ) ) ) {
+                                       $existing_term = $name_match;
+                               } elseif ( $slug_match && in_array( $slug, wp_list_pluck( $siblings, 'slug' ) ) ) {
+                                       $existing_term = $slug_match;
+                               }
+
+                               if ( $existing_term ) {
+                                       return new WP_Error( 'term_exists', __( 'A term with the name already exists with this parent.' ), $existing_term->term_id );
+                               }
+                       } else {
+                               return new WP_Error( 'term_exists', __( 'A term with the name already exists in this taxonomy.' ), $name_match->term_id );
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                } else {
-                       return new WP_Error( 'term_exists', __( 'A term with the name already exists in this taxonomy.' ), $existing_term->term_id );
</del><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunktestsphpunitteststermphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/term.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/term.php        2015-03-16 10:45:29 UTC (rev 31791)
+++ trunk/tests/phpunit/tests/term.php  2015-03-16 11:15:34 UTC (rev 31792)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -174,9 +174,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Test existing term name with unique slug
</span><span class="cx" style="display: block; padding: 0 10px">                $term1 = $this->factory->tag->create( array( 'name' => 'Bozo', 'slug' => 'bozo1' ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertTrue( is_wp_error( $term1 ) );
-               $this->assertSame( 'term_exists', $term1->get_error_code() );
-               $this->assertEquals( $term->term_id, $term1->get_error_data() );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertFalse( is_wp_error( $term1 ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Test an existing term name
</span><span class="cx" style="display: block; padding: 0 10px">                $term2 = $this->factory->tag->create( array( 'name' => 'Bozo' ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -228,6 +226,203 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @ticket 31328
+        */
+       public function test_wp_insert_term_should_not_allow_duplicate_names_when_slug_is_a_duplicate_of_the_same_term_in_non_hierarchical_taxonomy() {
+               register_taxonomy( 'wptests_tax', 'post' );
+               $t1 = $this->factory->term->create( array(
+                       'name' => 'Foo',
+                       'slug' => 'foo',
+                       'taxonomy' => 'wptests_tax',
+               ) );
+
+               $t2 = wp_insert_term( 'Foo', 'wptests_tax', array(
+                       'slug' => 'foo',
+               ) );
+
+               $this->assertWPError( $t2 );
+               $this->assertSame( 'term_exists', $t2->get_error_code() );
+       }
+
+       /**
+        * @ticket 31328
+        */
+       public function test_wp_insert_term_should_not_allow_duplicate_names_when_slug_is_a_duplicate_of_a_different_term_in_non_hierarchical_taxonomy() {
+               register_taxonomy( 'wptests_tax', 'post' );
+               $t1 = $this->factory->term->create( array(
+                       'name' => 'Foo',
+                       'slug' => 'foo',
+                       'taxonomy' => 'wptests_tax',
+               ) );
+
+               $t2 = $this->factory->term->create( array(
+                       'name' => 'Bar',
+                       'slug' => 'bar',
+                       'taxonomy' => 'wptests_tax',
+               ) );
+
+               $t3 = wp_insert_term( 'Foo', 'wptests_tax', array(
+                       'slug' => 'bar',
+               ) );
+
+               $this->assertWPError( $t3 );
+               $this->assertSame( 'term_exists', $t3->get_error_code() );
+       }
+
+       /**
+        * @ticket 31328
+        */
+       public function test_wp_insert_term_should_allow_duplicate_names_when_a_unique_slug_has_been_provided_in_non_hierarchical_taxonomy() {
+               register_taxonomy( 'wptests_tax', 'post' );
+               $t1 = $this->factory->term->create( array(
+                       'name' => 'Foo',
+                       'slug' => 'foo',
+                       'taxonomy' => 'wptests_tax',
+               ) );
+
+               $t2 = wp_insert_term( 'Foo', 'wptests_tax', array(
+                       'slug' => 'foo-unique',
+               ) );
+
+               $this->assertFalse( is_wp_error( $t2 ) );
+
+               $t2_term = get_term( $t2['term_id'], 'wptests_tax' );
+               $this->assertSame( 'foo-unique', $t2_term->slug );
+               $this->assertSame( 'Foo', $t2_term->name );
+       }
+
+       /**
+        * @ticket 31328
+        */
+       public function test_wp_insert_term_should_not_allow_duplicate_names_when_the_slug_is_not_provided_in_non_hierarchical_taxonomy() {
+               register_taxonomy( 'wptests_tax', 'post' );
+               $t1 = $this->factory->term->create( array(
+                       'name' => 'Foo',
+                       'slug' => 'foo',
+                       'taxonomy' => 'wptests_tax',
+               ) );
+
+               $t2 = wp_insert_term( 'Foo', 'wptests_tax' );
+
+               $this->assertWPError( $t2 );
+               $this->assertSame( 'term_exists', $t2->get_error_code() );
+       }
+
+       /**
+        * @ticket 31328
+        */
+       public function test_wp_insert_term_should_not_allow_duplicate_names_when_slug_is_a_duplicate_of_the_same_term_in_hierarchical_taxonomy() {
+               register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
+               $t1 = $this->factory->term->create( array(
+                       'name' => 'Foo',
+                       'slug' => 'foo',
+                       'taxonomy' => 'wptests_tax',
+               ) );
+
+               $t2 = wp_insert_term( 'Foo', 'wptests_tax', array(
+                       'slug' => 'foo',
+               ) );
+
+               $this->assertWPError( $t2 );
+               $this->assertSame( 'term_exists', $t2->get_error_code() );
+       }
+
+       /**
+        * @ticket 31328
+        */
+       public function test_wp_insert_term_should_not_allow_duplicate_names_when_slug_is_a_duplicate_of_a_different_term_at_same_hierarchy_level_in_hierarchical_taxonomy() {
+               register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
+               $t1 = $this->factory->term->create( array(
+                       'name' => 'Foo',
+                       'slug' => 'foo',
+                       'taxonomy' => 'wptests_tax',
+               ) );
+
+               $t2 = $this->factory->term->create( array(
+                       'name' => 'Bar',
+                       'slug' => 'bar',
+                       'taxonomy' => 'wptests_tax',
+               ) );
+
+               $t3 = wp_insert_term( 'Foo', 'wptests_tax', array(
+                       'slug' => 'bar',
+               ) );
+
+               $this->assertWPError( $t3 );
+               $this->assertSame( 'term_exists', $t3->get_error_code() );
+       }
+
+       /**
+        * @ticket 31328
+        */
+       public function test_wp_insert_term_should_allow_duplicate_names_when_slug_is_a_duplicate_of_a_term_at_different_hierarchy_level_in_hierarchical_taxonomy() {
+               register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
+               $t1 = $this->factory->term->create( array(
+                       'name' => 'Foo',
+                       'slug' => 'foo',
+                       'taxonomy' => 'wptests_tax',
+               ) );
+
+               $t2 = $this->factory->term->create();
+
+               $t3 = $this->factory->term->create( array(
+                       'name' => 'Bar',
+                       'slug' => 'bar',
+                       'parent' => $t2,
+                       'taxonomy' => 'wptests_tax',
+               ) );
+
+               $t4 = wp_insert_term( 'Foo', 'wptests_tax', array(
+                       'slug' => 'bar',
+               ) );
+
+               $this->assertFalse( is_wp_error( $t4 ) );
+               $t4_term = get_term( $t4['term_id'], 'wptests_tax' );
+
+               // `wp_unique_term_slug()` allows term creation but iterates the slug.
+               $this->assertSame( 'bar-2', $t4_term->slug );
+               $this->assertSame( 'Foo', $t4_term->name );
+       }
+
+       /**
+        * @ticket 31328
+        */
+       public function test_wp_insert_term_should_allow_duplicate_names_when_a_unique_slug_has_been_provided_in_hierarchical_taxonomy() {
+               register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
+               $t1 = $this->factory->term->create( array(
+                       'name' => 'Foo',
+                       'slug' => 'foo',
+                       'taxonomy' => 'wptests_tax',
+               ) );
+
+               $t2 = wp_insert_term( 'Foo', 'wptests_tax', array(
+                       'slug' => 'foo-unique',
+               ) );
+
+               $this->assertFalse( is_wp_error( $t2 ) );
+
+               $t2_term = get_term( $t2['term_id'], 'wptests_tax' );
+               $this->assertSame( 'foo-unique', $t2_term->slug );
+               $this->assertSame( 'Foo', $t2_term->name );
+       }
+
+       /**
+        * @ticket 31328
+        */
+       public function test_wp_insert_term_should_not_allow_duplicate_names_when_the_slug_is_not_provided_in_hierarchical_taxonomy() {
+               register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
+               $t1 = $this->factory->term->create( array(
+                       'name' => 'Foo',
+                       'slug' => 'foo',
+                       'taxonomy' => 'wptests_tax',
+               ) );
+
+               $t2 = wp_insert_term( 'Foo', 'wptests_tax' );
+
+               $this->assertWPError( $t2 );
+               $this->assertSame( 'term_exists', $t2->get_error_code() );
+       }
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * @ticket 5809
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_wp_insert_term_duplicate_slug_same_taxonomy() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -370,77 +565,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( 0, $created_term->term_group );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function test_wp_insert_term_duplicate_name_slug_non_hierarchical() {
-               register_taxonomy( 'foo', 'post', array() );
-
-               $existing_term = $this->factory->term->create( array(
-                       'slug' => 'new-term',
-                       'name' => 'New Term',
-                       'taxonomy' => 'foo',
-               ) );
-
-               $found = wp_insert_term( 'New Term', 'foo', array(
-                       'slug' => 'new-term',
-               ) );
-
-               _unregister_taxonomy( 'foo' );
-
-               $this->assertTrue( is_wp_error( $found ) );
-               $this->assertEquals( $existing_term, $found->get_error_data() );
-       }
-
-       public function test_wp_insert_term_duplicate_name_hierarchical() {
-               register_taxonomy( 'foo', 'post', array(
-                       'hierarchical' => true,
-               ) );
-
-               $parent_term = $this->factory->term->create( array(
-                       'taxonomy' => 'foo',
-               ) );
-
-               $existing_term = $this->factory->term->create( array(
-                       'name' => 'New Term',
-                       'taxonomy' => 'foo',
-                       'parent' => $parent_term,
-               ) );
-
-               $found = wp_insert_term( 'New Term', 'foo', array(
-                       'parent' => $parent_term,
-               ) );
-
-               _unregister_taxonomy( 'foo' );
-
-               $this->assertTrue( is_wp_error( $found ) );
-               $this->assertEquals( $existing_term, $found->get_error_data() );
-       }
-
-       public function test_wp_insert_term_duplicate_name_slug_hierarchical() {
-               register_taxonomy( 'foo', 'post', array(
-                       'hierarchical' => true,
-               ) );
-
-               $parent_term = $this->factory->term->create( array(
-                       'taxonomy' => 'foo',
-               ) );
-
-               $existing_term = $this->factory->term->create( array(
-                       'name' => 'New Term',
-                       'slug' => 'new-term-slug',
-                       'taxonomy' => 'foo',
-                       'parent' => $parent_term,
-               ) );
-
-               $found = wp_insert_term( 'New Term', 'foo', array(
-                       'parent' => $parent_term,
-                       'slug' => 'new-term-slug',
-               ) );
-
-               _unregister_taxonomy( 'foo' );
-
-               $this->assertTrue( is_wp_error( $found ) );
-               $this->assertEquals( $existing_term, $found->get_error_data() );
-       }
-
</del><span class="cx" style="display: block; padding: 0 10px">         /**
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 5809
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span></span></pre>
</div>
</div>

</body>
</html>