<!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>[35537] trunk: Make `get_term()` behave more consistently in the context of shared terms.</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/35537">35537</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/35537","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-11-05 16:44:59 +0000 (Thu, 05 Nov 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'>Make `get_term()` behave more consistently in the context of shared terms.

When `WP_Term` was introduced in <a href="https://core.trac.wordpress.org/changeset/34997">[34997]</a>, the `$taxonomy` parameter for
`get_term()` was made optional. This meant that, when the optional param was
omitted, `get_term()` had no way of determining which term was intended when
the term_id was shared between multiple taxonomies. As a (somewhat sneaky) way
of fixing things, `get_term()` split any shared terms it found. But this could
cause problems with developer expectations: it's not clear why requesting a
term should result in a database update, much less a potential change in the
ID of a term.

In place of this technique, this changeset introduces a number of changes that
make the handling of shared terms a bit less insane:

* When a taxonomy is provided to `get_term()`, and a cached term is found matching the term_id, make sure the taxonomy also matches before returning it.
* When a taxonomy is not provided, ensure that the term is not shared before adding it to the cache.
* When a term is shared between taxonomies and no taxonomy is provided, return a `WP_Error` rather than splitting the term.
* When a term is shared between taxonomies, only one of which is valid, return the term from that taxonomy.

Props boonebgorges, dlh.
Fixes <a href="https://core.trac.wordpress.org/ticket/34533">#34533</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswptermphp">trunk/src/wp-includes/class-wp-term.php</a></li>
<li><a href="#trunksrcwpincludestaxonomyfunctionsphp">trunk/src/wp-includes/taxonomy-functions.php</a></li>
<li><a href="#trunktestsphpunitteststermgetTermphp">trunk/tests/phpunit/tests/term/getTerm.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswptermphp"></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/class-wp-term.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-term.php   2015-11-05 15:35:06 UTC (rev 35536)
+++ trunk/src/wp-includes/class-wp-term.php     2015-11-05 16:44:59 UTC (rev 35537)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -115,10 +115,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @global wpdb $wpdb WordPress database abstraction object.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param int $term_id Term ID.
-        * @return WP_Term|false Term object, false otherwise.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param int    $term_id  Term ID.
+        * @param string $taxonomy Optional. Limit matched terms to those matching `$taxonomy`. Only used for
+        *                         disambiguating potentially shared terms.
+        * @return WP_Term|WP_Error|false Term object, if found. WP_Error if `$term_id` is shared between taxonomies and
+        *                                there's insufficient data to distinguish which term is intended.
+        *                                False for other failures.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public static function get_instance( $term_id ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public static function get_instance( $term_id, $taxonomy = null ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 global $wpdb;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $term_id = (int) $term_id;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -129,14 +133,58 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $_term = wp_cache_get( $term_id, 'terms' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // If there isn't a cached version, hit the database.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! $_term ) {
-                       $_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE t.term_id = %d LIMIT 1", $term_id ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( ! $_term || ( $taxonomy && $taxonomy !== $_term->taxonomy ) ) {
+                       // Grab all matching terms, in case any are shared between taxonomies.
+                       $terms = $wpdb->get_results( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE t.term_id = %d", $term_id ) );
+                       if ( ! $terms ) {
+                               return false;
+                       }
+
+                       // If a taxonomy was specified, find a match.
+                       if ( $taxonomy ) {
+                               foreach ( $terms as $match ) {
+                                       if ( $taxonomy === $match->taxonomy ) {
+                                               $_term = $match;
+                                               break;
+                                       }
+                               }
+
+                       // If only one match was found, it's the one we want.
+                       } elseif ( 1 === count( $terms ) ) {
+                               $_term = reset( $terms );
+
+                       // Otherwise, the term must be shared between taxonomies.
+                       } else {
+                               // If the term is shared only with invalid taxonomies, return the one valid term.
+                               foreach ( $terms as $t ) {
+                                       if ( ! taxonomy_exists( $t->taxonomy ) ) {
+                                               continue;
+                                       }
+
+                                       // Only hit if we've already identified a term in a valid taxonomy.
+                                       if ( $_term ) {
+                                               return new WP_Error( 'ambiguous_term_id', __( 'Term ID is shared between multiple taxonomies' ), $term_id );
+                                       }
+
+                                       $_term = $t;
+                               }
+                       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( ! $_term ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                return false;
</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">+                        // Don't return terms from invalid taxonomies.
+                       if ( ! taxonomy_exists( $_term->taxonomy ) ) {
+                               return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) );
+                       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         $_term = sanitize_term( $_term, $_term->taxonomy, 'raw' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        wp_cache_add( $term_id, $_term, 'terms' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       // Don't cache terms that are shared between taxonomies.
+                       if ( 1 === count( $terms ) ) {
+                               wp_cache_add( $term_id, $_term, 'terms' );
+                       }
</ins><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">                $term_obj = new WP_Term( $_term );
</span></span></pre></div>
<a id="trunksrcwpincludestaxonomyfunctionsphp"></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-functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/taxonomy-functions.php      2015-11-05 15:35:06 UTC (rev 35536)
+++ trunk/src/wp-includes/taxonomy-functions.php        2015-11-05 16:44:59 UTC (rev 35537)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -764,25 +764,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $_term = WP_Term::get_instance( $term->term_id );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">        } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $_term = WP_Term::get_instance( $term );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $_term = WP_Term::get_instance( $term, $taxonomy );
</ins><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">-        // If `$taxonomy` was provided, make sure it matches the taxonomy of the located term.
-       if ( $_term && $taxonomy && $taxonomy !== $_term->taxonomy ) {
-               // If there are two terms with the same ID, split the other one to a new term.
-               $new_term_id = _split_shared_term( $_term->term_id, $_term->term_taxonomy_id );
-
-               // If no split occurred, this is an invalid request. Return null (not WP_Error) for back compat.
-               if ( $new_term_id === $_term->term_id ) {
-                       return null;
-
-               // The term has been split. Refetch the term from the proper taxonomy.
-               } else {
-                       return get_term( $_term->term_id, $taxonomy, $output, $filter );
-               }
-       }
-
-       if ( ! $_term ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( is_wp_error( $_term ) ) {
+               return $_term;
+       } elseif ( ! $_term ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 return null;
</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="trunktestsphpunitteststermgetTermphp"></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/getTerm.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/term/getTerm.php        2015-11-05 15:35:06 UTC (rev 35536)
+++ trunk/tests/phpunit/tests/term/getTerm.php  2015-11-05 16:44:59 UTC (rev 35537)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -9,6 +9,41 @@
</span><span class="cx" style="display: block; padding: 0 10px">                register_taxonomy( 'wptests_tax', 'post' );
</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">+        /**
+        * Utility function for generating two shared terms, in the 'wptests_tax' and 'wptests_tax_2' taxonomies.
+        *
+        * @return array Array of term_id/old_term_id/term_taxonomy_id triplets.
+        */
+       protected function generate_shared_terms() {
+               global $wpdb;
+
+               register_taxonomy( 'wptests_tax_2', 'post' );
+
+               $t1 = wp_insert_term( 'Foo', 'wptests_tax' );
+               $t2 = wp_insert_term( 'Foo', 'wptests_tax_2' );
+
+               // Manually modify because shared terms shouldn't naturally occur.
+               $wpdb->update( $wpdb->term_taxonomy,
+                       array( 'term_id' => $t1['term_id'] ),
+                       array( 'term_taxonomy_id' => $t2['term_taxonomy_id'] ),
+                       array( '%d' ),
+                       array( '%d' )
+               );
+
+               return array(
+                       array(
+                               'term_id' => $t1['term_id'],
+                               'old_term_id' => $t1['term_id'],
+                               'term_taxonomy_id' => $t1['term_taxonomy_id'],
+                       ),
+                       array(
+                               'term_id' => $t1['term_id'],
+                               'old_term_id' => $t2['term_id'],
+                               'term_taxonomy_id' => $t2['term_taxonomy_id'],
+                       ),
+               );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         public function test_should_return_error_for_empty_term() {
</span><span class="cx" style="display: block; padding: 0 10px">                $found = get_term( '', 'wptests_tax' );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertWPError( $found );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -114,4 +149,86 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $term_id = self::factory()->term->create( array( 'taxonomy' => 'post_tag' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertNull( get_term( $term_id, 'category' ) );
</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 34533
+        */
+       public function test_should_return_wp_error_when_term_is_shared_and_no_taxonomy_is_specified() {
+               $terms = $this->generate_shared_terms();
+
+               $found = get_term( $terms[0]['term_id'] );
+
+               $this->assertWPError( $found );
+       }
+
+       /**
+        * @ticket 34533
+        */
+       public function test_should_return_term_when_term_is_shared_and_correct_taxonomy_is_specified() {
+               $terms = $this->generate_shared_terms();
+
+               $found = get_term( $terms[0]['term_id'], 'wptests_tax' );
+
+               $this->assertInstanceOf( 'WP_Term', $found );
+               $this->assertSame( $terms[0]['term_id'], $found->term_id );
+       }
+
+       /**
+        * @ticket 34533
+        */
+       public function test_should_return_null_when_term_is_shared_and_incorrect_taxonomy_is_specified() {
+               $terms = $this->generate_shared_terms();
+
+               $found = get_term( $terms[0]['term_id'], 'post_tag' );
+
+               $this->assertNull( $found );
+       }
+
+       /**
+        * @ticket 34533
+        */
+       public function test_shared_term_in_cache_should_be_ignored_when_specifying_a_different_taxonomy() {
+               global $wpdb;
+
+               $terms = $this->generate_shared_terms();
+
+               // Prime cache for 'wptests_tax'.
+               get_term( $terms[0]['term_id'], 'wptests_tax' );
+               $num_queries = $wpdb->num_queries;
+
+               // Database should be hit again.
+               $found = get_term( $terms[1]['term_id'], 'wptests_tax_2' );
+               $num_queries++;
+
+               $this->assertSame( $num_queries, $wpdb->num_queries );
+               $this->assertInstanceOf( 'WP_Term', $found );
+               $this->assertSame( 'wptests_tax_2', $found->taxonomy );
+       }
+
+       /**
+        * @ticket 34533
+        */
+       public function test_should_return_error_when_only_matching_term_is_in_an_invalid_taxonomy() {
+               $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
+
+               _unregister_taxonomy( 'wptests_tax' );
+
+               $found = get_term( $t );
+               $this->assertWPError( $found );
+               $this->assertSame( 'invalid_taxonomy', $found->get_error_code() );
+       }
+
+       /**
+        * @ticket 34533
+        */
+       public function test_term_should_be_returned_when_id_is_shared_only_with_invalid_taxonomies() {
+               $terms = $this->generate_shared_terms();
+
+               _unregister_taxonomy( 'wptests_tax' );
+
+               $found = get_term( $terms[1]['term_id'] );
+               $this->assertInstanceOf( 'WP_Term', $found );
+               $this->assertSame( 'wptests_tax_2', $found->taxonomy );
+               $this->assertSame( $terms[1]['term_id'], $found->term_id );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>