WordPress.org

Make WordPress Core

Changeset 13925


Ignore:
Timestamp:
04/02/2010 01:26:44 AM (11 years ago)
Author:
wpmuguru
Message:

rework syncing of global terms, see #12663

Location:
trunk/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/ms-functions.php

    r13884 r13925  
    12061206 */
    12071207function global_terms( $term_id, $deprecated = '' ) {
    1208     global $wpdb;
     1208    global $wpdb, $global_terms_recurse;
    12091209
    12101210    if ( !global_terms_enabled() )
    12111211        return $term_id;
    12121212
     1213    // prevent a race condition
     1214    if ( !isset( $global_terms_recurse ) ) {
     1215        $recurse_start = true;
     1216        $global_terms_recurse = 1;
     1217    } elseif ( 10 < $global_terms_recurse++ ) {
     1218        return $term_id;
     1219        $recurse_start = false;
     1220    }
     1221
    12131222    $term_id = intval( $term_id );
    12141223    $c = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->terms WHERE term_id = %d", $term_id ) );
     
    12161225    $global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE category_nicename = %s", $c->slug ) );
    12171226    if ( $global_id == null ) {
    1218         $wpdb->insert( $wpdb->sitecategories, array('cat_name' => $c->name, 'category_nicename' => $c->slug) );
    1219         $global_id = $wpdb->insert_id;
    1220     }
    1221 
    1222     if ( $global_id == $term_id )
    1223         return $global_id;
    1224 
    1225     if ( get_option( 'default_category' ) == $term_id )
    1226         update_option( 'default_category', $global_id );
    1227 
    1228     $wpdb->update( $wpdb->terms, array('term_id' => $global_id), array('term_id' => $term_id) );
    1229     $wpdb->update( $wpdb->term_taxonomy, array('term_id' => $global_id), array('term_id' => $term_id) );
    1230     $wpdb->update( $wpdb->term_taxonomy, array('parent' => $global_id), array('parent' => $term_id) );
    1231 
    1232     clean_term_cache($term_id);
     1227        $used_global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE cat_ID = %d", $c->term_id ) );
     1228        if ( null == $used_global_id ) {
     1229            $wpdb->insert( $wpdb->sitecategories, array( 'cat_ID' => $term_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) );
     1230            $global_id = $wpdb->insert_id;
     1231        } else {
     1232            $max_global_id = $wpdb->get_var( "SELECT MAX(cat_ID) FROM $wpdb->sitecategories" );
     1233            $max_global_id += mt_rand( 100, 400 );
     1234            $wpdb->insert( $wpdb->sitecategories, array( 'cat_ID' => $global_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) );
     1235            $global_id = $wpdb->insert_id;
     1236        }
     1237    } elseif ( $global_id != $term_id ) {
     1238        $local_id = $wpdb->get_row( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE term_id = %d", $global_id ) );
     1239        if ( null != $local_id )
     1240            $local_id = global_terms( $local_id );
     1241            if ( 10 < $global_terms_recurse )
     1242                $global_id = $term_id;
     1243    }
     1244
     1245    if ( $global_id != $term_id ) {
     1246        if ( get_option( 'default_category' ) == $term_id )
     1247            update_option( 'default_category', $global_id );
     1248
     1249        $wpdb->update( $wpdb->terms, array('term_id' => $global_id), array('term_id' => $term_id) );
     1250        $wpdb->update( $wpdb->term_taxonomy, array('term_id' => $global_id), array('term_id' => $term_id) );
     1251        $wpdb->update( $wpdb->term_taxonomy, array('parent' => $global_id), array('parent' => $term_id) );
     1252
     1253        clean_term_cache($term_id);
     1254    }
     1255    if( $recurse_start )
     1256        unset( $global_terms_recurse );
    12331257
    12341258    return $global_id;
  • trunk/wp-includes/taxonomy.php

    r13924 r13925  
    15461546        // Make sure the slug is unique accross all taxonomies.
    15471547        $slug = wp_unique_term_slug($slug, (object) $args);
    1548         if ( !is_multisite() ) {
    1549             if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) )
    1550                 return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);
    1551             $term_id = (int) $wpdb->insert_id;
    1552         } else {
    1553             $maxterm = $wpdb->get_var( "SELECT max(term_id) FROM {$wpdb->terms}" );
    1554             $term_id = mt_rand( $maxterm+100, $maxterm+4000 );
    1555             if ( false === $wpdb->insert( $wpdb->terms, compact( 'term_id', 'name', 'slug', 'term_group' ) ) )
    1556                 return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);
    1557         }
     1548        if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) )
     1549            return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);
     1550        $term_id = (int) $wpdb->insert_id;
    15581551    } else if ( is_taxonomy_hierarchical($taxonomy) && !empty($parent) ) {
    15591552        // If the taxonomy supports hierarchy and the term has a parent, make the slug unique
    15601553        // by incorporating parent slugs.
    15611554        $slug = wp_unique_term_slug($slug, (object) $args);
    1562         if ( !is_multisite() ) {
    1563             if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) )
    1564                 return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);
    1565             $term_id = (int) $wpdb->insert_id;
    1566         } else {
    1567             $maxterm = $wpdb->get_var( "SELECT max(term_id) FROM {$wpdb->terms}" );
    1568             $term_id = mt_rand( $maxterm+100, $maxterm+4000 );
    1569             if ( false === $wpdb->insert( $wpdb->terms, compact( 'term_id','name', 'slug', 'term_group' ) ) )
    1570                 return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);
    1571         }
     1555        if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) )
     1556            return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);
     1557        $term_id = (int) $wpdb->insert_id;
    15721558    }
    15731559
Note: See TracChangeset for help on using the changeset viewer.