<!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" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { 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 #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#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>
<title>[13925] trunk/wp-includes: rework syncing of global terms, see #12663</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/13925">13925</a></dd>
<dt>Author</dt> <dd>wpmuguru</dd>
<dt>Date</dt> <dd>2010-04-02 01:26:44 +0000 (Fri, 02 Apr 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>rework syncing of global terms, see <a href="http://trac.wordpress.org/ticket/12663">#12663</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesmsfunctionsphp">trunk/wp-includes/ms-functions.php</a></li>
<li><a href="#trunkwpincludestaxonomyphp">trunk/wp-includes/taxonomy.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesmsfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/ms-functions.php (13924 => 13925)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/ms-functions.php        2010-04-02 01:23:58 UTC (rev 13924)
+++ trunk/wp-includes/ms-functions.php        2010-04-02 01:26:44 UTC (rev 13925)
</span><span class="lines">@@ -1205,32 +1205,56 @@
</span><span class="cx">  * @return int An ID from the global terms table mapped from $term_id.
</span><span class="cx">  */
</span><span class="cx"> function global_terms( $term_id, $deprecated = '' ) {
</span><del>-        global $wpdb;
</del><ins>+        global $wpdb, $global_terms_recurse;
</ins><span class="cx"> 
</span><span class="cx">         if ( !global_terms_enabled() )
</span><span class="cx">                 return $term_id;
</span><span class="cx"> 
</span><ins>+        // prevent a race condition
+        if ( !isset( $global_terms_recurse ) ) {
+                $recurse_start = true;
+                $global_terms_recurse = 1;
+        } elseif ( 10 &lt; $global_terms_recurse++ ) {
+                return $term_id;
+                $recurse_start = false;
+        }
+
</ins><span class="cx">         $term_id = intval( $term_id );
</span><span class="cx">         $c = $wpdb-&gt;get_row( $wpdb-&gt;prepare( &quot;SELECT * FROM $wpdb-&gt;terms WHERE term_id = %d&quot;, $term_id ) );
</span><span class="cx"> 
</span><span class="cx">         $global_id = $wpdb-&gt;get_var( $wpdb-&gt;prepare( &quot;SELECT cat_ID FROM $wpdb-&gt;sitecategories WHERE category_nicename = %s&quot;, $c-&gt;slug ) );
</span><span class="cx">         if ( $global_id == null ) {
</span><del>-                $wpdb-&gt;insert( $wpdb-&gt;sitecategories, array('cat_name' =&gt; $c-&gt;name, 'category_nicename' =&gt; $c-&gt;slug) );
-                $global_id = $wpdb-&gt;insert_id;
</del><ins>+                $used_global_id = $wpdb-&gt;get_var( $wpdb-&gt;prepare( &quot;SELECT cat_ID FROM $wpdb-&gt;sitecategories WHERE cat_ID = %d&quot;, $c-&gt;term_id ) );
+                if ( null == $used_global_id ) {
+                        $wpdb-&gt;insert( $wpdb-&gt;sitecategories, array( 'cat_ID' =&gt; $term_id, 'cat_name' =&gt; $c-&gt;name, 'category_nicename' =&gt; $c-&gt;slug ) );
+                        $global_id = $wpdb-&gt;insert_id;
+                } else {
+                        $max_global_id = $wpdb-&gt;get_var( &quot;SELECT MAX(cat_ID) FROM $wpdb-&gt;sitecategories&quot; );
+                        $max_global_id += mt_rand( 100, 400 );
+                        $wpdb-&gt;insert( $wpdb-&gt;sitecategories, array( 'cat_ID' =&gt; $global_id, 'cat_name' =&gt; $c-&gt;name, 'category_nicename' =&gt; $c-&gt;slug ) );
+                        $global_id = $wpdb-&gt;insert_id;
+                }
+        } elseif ( $global_id != $term_id ) {
+                $local_id = $wpdb-&gt;get_row( $wpdb-&gt;prepare( &quot;SELECT term_id FROM $wpdb-&gt;terms WHERE term_id = %d&quot;, $global_id ) );
+                if ( null != $local_id )
+                        $local_id = global_terms( $local_id );
+                        if ( 10 &lt; $global_terms_recurse )
+                                $global_id = $term_id;
</ins><span class="cx">         }
</span><span class="cx"> 
</span><del>-        if ( $global_id == $term_id )
-                return $global_id;
</del><ins>+        if ( $global_id != $term_id ) {
+                if ( get_option( 'default_category' ) == $term_id )
+                        update_option( 'default_category', $global_id );
</ins><span class="cx"> 
</span><del>-        if ( get_option( 'default_category' ) == $term_id )
-                update_option( 'default_category', $global_id );
</del><ins>+                $wpdb-&gt;update( $wpdb-&gt;terms, array('term_id' =&gt; $global_id), array('term_id' =&gt; $term_id) );
+                $wpdb-&gt;update( $wpdb-&gt;term_taxonomy, array('term_id' =&gt; $global_id), array('term_id' =&gt; $term_id) );
+                $wpdb-&gt;update( $wpdb-&gt;term_taxonomy, array('parent' =&gt; $global_id), array('parent' =&gt; $term_id) );
</ins><span class="cx"> 
</span><del>-        $wpdb-&gt;update( $wpdb-&gt;terms, array('term_id' =&gt; $global_id), array('term_id' =&gt; $term_id) );
-        $wpdb-&gt;update( $wpdb-&gt;term_taxonomy, array('term_id' =&gt; $global_id), array('term_id' =&gt; $term_id) );
-        $wpdb-&gt;update( $wpdb-&gt;term_taxonomy, array('parent' =&gt; $global_id), array('parent' =&gt; $term_id) );
</del><ins>+                clean_term_cache($term_id);
+        }
+        if( $recurse_start )
+                unset( $global_terms_recurse );
</ins><span class="cx"> 
</span><del>-        clean_term_cache($term_id);
-
</del><span class="cx">         return $global_id;
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkwpincludestaxonomyphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/taxonomy.php (13924 => 13925)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/taxonomy.php        2010-04-02 01:23:58 UTC (rev 13924)
+++ trunk/wp-includes/taxonomy.php        2010-04-02 01:26:44 UTC (rev 13925)
</span><span class="lines">@@ -1545,30 +1545,16 @@
</span><span class="cx">         if ( ! $term_id = is_term($slug, $taxonomy) ) {
</span><span class="cx">                 // Make sure the slug is unique accross all taxonomies.
</span><span class="cx">                 $slug = wp_unique_term_slug($slug, (object) $args);
</span><del>-                if ( !is_multisite() ) {
-                        if ( false === $wpdb-&gt;insert( $wpdb-&gt;terms, compact( 'name', 'slug', 'term_group' ) ) )
-                                return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb-&gt;last_error);
-                        $term_id = (int) $wpdb-&gt;insert_id;
-                } else {
-                        $maxterm = $wpdb-&gt;get_var( &quot;SELECT max(term_id) FROM {$wpdb-&gt;terms}&quot; );
-                        $term_id = mt_rand( $maxterm+100, $maxterm+4000 );
-                        if ( false === $wpdb-&gt;insert( $wpdb-&gt;terms, compact( 'term_id', 'name', 'slug', 'term_group' ) ) )
-                                return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb-&gt;last_error);
-                }
</del><ins>+                if ( false === $wpdb-&gt;insert( $wpdb-&gt;terms, compact( 'name', 'slug', 'term_group' ) ) )
+                        return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb-&gt;last_error);
+                $term_id = (int) $wpdb-&gt;insert_id;
</ins><span class="cx">         } else if ( is_taxonomy_hierarchical($taxonomy) &amp;&amp; !empty($parent) ) {
</span><span class="cx">                 // If the taxonomy supports hierarchy and the term has a parent, make the slug unique
</span><span class="cx">                 // by incorporating parent slugs.
</span><span class="cx">                 $slug = wp_unique_term_slug($slug, (object) $args);
</span><del>-                if ( !is_multisite() ) {
-                        if ( false === $wpdb-&gt;insert( $wpdb-&gt;terms, compact( 'name', 'slug', 'term_group' ) ) )
-                                return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb-&gt;last_error);
-                        $term_id = (int) $wpdb-&gt;insert_id;
-                } else {
-                        $maxterm = $wpdb-&gt;get_var( &quot;SELECT max(term_id) FROM {$wpdb-&gt;terms}&quot; );
-                        $term_id = mt_rand( $maxterm+100, $maxterm+4000 );
-                        if ( false === $wpdb-&gt;insert( $wpdb-&gt;terms, compact( 'term_id','name', 'slug', 'term_group' ) ) )
-                                return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb-&gt;last_error);
-                }
</del><ins>+                if ( false === $wpdb-&gt;insert( $wpdb-&gt;terms, compact( 'name', 'slug', 'term_group' ) ) )
+                        return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb-&gt;last_error);
+                $term_id = (int) $wpdb-&gt;insert_id;
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         if ( empty($slug) ) {
</span></span></pre>
</div>
</div>

</body>
</html>