<!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>[30494] trunk: Improve cleanup of cached term_ids after shared terms are split.</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/30494">30494</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/30494","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>2014-11-21 03:16:08 +0000 (Fri, 21 Nov 2014)</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'>Improve cleanup of cached term_ids after shared terms are split.

* If the split term ID is stored as 'default_category', 'default_link_category', or 'default_email_category', update it to the new ID.
* If the split term ID is associated with a nav menu item, update that piece of postmeta to the new ID.

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

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

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesdefaultfiltersphp"></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/default-filters.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/default-filters.php 2014-11-21 03:13:20 UTC (rev 30493)
+++ trunk/src/wp-includes/default-filters.php   2014-11-21 03:16:08 UTC (rev 30494)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -306,4 +306,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'determine_current_user', 'wp_validate_auth_cookie'          );
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'determine_current_user', 'wp_validate_logged_in_cookie', 20 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+// Split term updates
+add_filter( 'split_shared_term', '_wp_check_split_default_terms',  10, 4 );
+add_filter( 'split_shared_term', '_wp_check_split_terms_in_menus', 10, 4 );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> unset($filter, $action);
</span></span></pre></div>
<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        2014-11-21 03:13:20 UTC (rev 30493)
+++ trunk/src/wp-includes/taxonomy.php  2014-11-21 03:16:08 UTC (rev 30494)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4136,6 +4136,61 @@
</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">+ * Check default categories when a term gets split to see if any of them need
+ * to be updated.
+ *
+ * @since 4.1.0
+ * @access private
+ *
+ * @param int    $term_id          ID of the formerly shared term.
+ * @param int    $new_term_id      ID of the new term created for the $term_taxonomy_id.
+ * @param int    $term_taxonomy_id ID for the term_taxonomy row affected by the split.
+ * @param string $taxonomy         Taxonomy for the split term.
+ */
+function _wp_check_split_default_terms( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
+       if ( 'category' == $taxonomy ) {
+               foreach ( array( 'default_category', 'default_link_category', 'default_email_category' ) as $option ) {
+                       if ( $term_id == get_option( $option, -1 ) ) {
+                               update_option( $option, $new_term_id );
+                       }
+               }
+       }
+}
+
+/**
+ * Check menu items when a term gets split to see if any of them need to be
+ * updated.
+ *
+ * @since 4.1.0
+ * @access private
+ *
+ * @param int    $term_id          ID of the formerly shared term.
+ * @param int    $new_term_id      ID of the new term created for the $term_taxonomy_id.
+ * @param int    $term_taxonomy_id ID for the term_taxonomy row affected by the split.
+ * @param string $taxonomy         Taxonomy for the split term.
+ */
+function _wp_check_split_terms_in_menus( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
+       global $wpdb;
+       $post_ids = $wpdb->get_col( $wpdb->prepare(
+               "SELECT m1.post_id
+               FROM {$wpdb->postmeta} AS m1
+                       INNER JOIN {$wpdb->postmeta} AS m2 ON m2.post_id=m1.post_id
+                       INNER JOIN {$wpdb->postmeta} AS m3 ON m3.post_id=m1.post_id
+               WHERE ( m1.meta_key = '_menu_item_type' AND m1.meta_value = 'taxonomy' )
+                       AND ( m2.meta_key = '_menu_item_object' AND m2.meta_value = '%s' )
+                       AND ( m3.meta_key = '_menu_item_object_id' AND m3.meta_value = %d )",
+               $taxonomy,
+               $term_id
+       ) );
+
+       if ( $post_ids ) {
+               foreach ( $post_ids as $post_id ) {
+                       update_post_meta( $post_id, '_menu_item_object_id', $new_term_id, $term_id );
+               }
+       }
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px">  * Generate a permalink for a taxonomy term archive.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 2.5.0
</span></span></pre></div>
<a id="trunktestsphpunitteststermsplitSharedTermphp"></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/splitSharedTerm.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/term/splitSharedTerm.php        2014-11-21 03:13:20 UTC (rev 30493)
+++ trunk/tests/phpunit/tests/term/splitSharedTerm.php  2014-11-21 03:16:08 UTC (rev 30494)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4,7 +4,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @group taxonomy
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> class Tests_Term_SplitSharedTerm extends WP_UnitTestCase {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        protected $tt_ids = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected $terms = array();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        public function setUp() {
</span><span class="cx" style="display: block; padding: 0 10px">                global $wpdb;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -40,11 +40,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'parent' => $t1['term_id'],
</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">-                $this->tt_ids = array(
-                       't1' => $t1['term_taxonomy_id'],
-                       't2' => $t2['term_taxonomy_id'],
-                       't3' => $t3['term_taxonomy_id'],
-                       't2_child' => $t2_child['term_taxonomy_id'],
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->terms = array(
+                       't1' => $t1,
+                       't2' => $t2,
+                       't3' => $t3,
+                       't2_child' => $t2_child,
</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">                _split_shared_term( $t1['term_id'], $t2['term_taxonomy_id'] );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -55,9 +55,9 @@
</span><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_should_create_new_term_ids() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $t1_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t1'], 'wptests_tax' );
-               $t2_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t2'], 'wptests_tax_2' );
-               $t3_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t3'], 'wptests_tax_3' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $t1_term = get_term_by( 'term_taxonomy_id', $this->terms['t1']['term_taxonomy_id'], 'wptests_tax' );
+               $t2_term = get_term_by( 'term_taxonomy_id', $this->terms['t2']['term_taxonomy_id'], 'wptests_tax_2' );
+               $t3_term = get_term_by( 'term_taxonomy_id', $this->terms['t3']['term_taxonomy_id'], 'wptests_tax_3' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertNotEquals( $t1_term->term_id, $t2_term->term_id );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertNotEquals( $t1_term->term_id, $t3_term->term_id );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -68,26 +68,26 @@
</span><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_should_retain_child_terms_when_using_get_terms_parent() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $t2_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t2'], 'wptests_tax_2' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $t2_term = get_term_by( 'term_taxonomy_id', $this->terms['t2']['term_taxonomy_id'], 'wptests_tax_2' );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $children = get_terms( 'wptests_tax_2', array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'parent' => $t2_term->term_id,
</span><span class="cx" style="display: block; padding: 0 10px">                        'hide_empty' => false,
</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">-                $this->assertEquals( $this->tt_ids['t2_child'], $children[0]->term_taxonomy_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertEquals( $this->terms['t2_child']['term_taxonomy_id'], $children[0]->term_taxonomy_id );
</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">        /**
</span><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_should_retain_child_terms_when_using_get_terms_child_of() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $t2_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t2'], 'wptests_tax_2' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $t2_term = get_term_by( 'term_taxonomy_id', $this->terms['t2']['term_taxonomy_id'], 'wptests_tax_2' );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $children = get_terms( 'wptests_tax_2', array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'child_of' => $t2_term->term_id,
</span><span class="cx" style="display: block; padding: 0 10px">                        'hide_empty' => false,
</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">-                $this->assertEquals( $this->tt_ids['t2_child'], $children[0]->term_taxonomy_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertEquals( $this->terms['t2_child']['term_taxonomy_id'], $children[0]->term_taxonomy_id );
</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">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -121,4 +121,65 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $t2_children = get_term_children( $t2['term_id'], 'wptests_tax_4' );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( array( $new_term_id ), $t2_children );
</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 30335
+        */
+       public function test_should_update_default_category_on_term_split() {
+               global $wpdb;
+               $t1 = wp_insert_term( 'Foo Default', 'category' );
+
+               update_option( 'default_category', $t1['term_id'] );
+
+               register_taxonomy( 'wptests_tax_5', 'post' );
+               $t2 = wp_insert_term( 'Foo Default', 'wptests_tax_5' );
+
+               // Manually modify because split 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' )
+               );
+
+               $this->assertEquals( $t1['term_id'], get_option( 'default_category', -1 ) );
+
+               $new_term_id = _split_shared_term( $t1['term_id'], $t1['term_taxonomy_id'] );
+
+               $this->assertNotEquals( $new_term_id, $t1['term_id'] );
+               $this->assertEquals( $new_term_id, get_option( 'default_category', -1 ) );
+       }
+
+       /**
+        * @ticket 30335
+        */
+       public function test_should_update_menus_on_term_split() {
+               global $wpdb;
+
+               $t1 = wp_insert_term( 'Foo Menu', 'category' );
+
+               register_taxonomy( 'wptests_tax_6', 'post' );
+               $t2 = wp_insert_term( 'Foo Menu', 'wptests_tax_6' );
+
+               // Manually modify because split 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' )
+               );
+
+               $menu_id = wp_create_nav_menu( rand_str() );
+               $cat_menu_item = wp_update_nav_menu_item( $menu_id, 0, array(
+                       'menu-item-type' => 'taxonomy',
+                       'menu-item-object' => 'category',
+                       'menu-item-object-id' => $t1['term_id'],
+                       'menu-item-status' => 'publish'
+               ) );
+               $this->assertEquals( $t1['term_id'], get_post_meta( $cat_menu_item, '_menu_item_object_id', true ) );
+
+               $new_term_id = _split_shared_term( $t1['term_id'], $t1['term_taxonomy_id'] );
+               $this->assertNotEquals( $new_term_id, $t1['term_id'] );
+               $this->assertEquals( $new_term_id, get_post_meta( $cat_menu_item, '_menu_item_object_id', true ) );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>