diff --git a/wp-includes/ms-functions.php b/wp-includes/ms-functions.php index 1c53ff74b..6b5e0d4e7 100644 --- a/wp-includes/ms-functions.php +++ b/wp-includes/ms-functions.php @@ -1205,31 +1205,55 @@ function fix_import_form_size( $size ) { * @return int An ID from the global terms table mapped from $term_id. */ function global_terms( $term_id, $deprecated = '' ) { - global $wpdb; + global $wpdb, $global_terms_recurse; if ( !global_terms_enabled() ) return $term_id; + // prevent a race condition + if ( !isset( $global_terms_recurse ) ) { + $recurse_start = true; + $global_terms_recurse = 1; + } elseif ( 10 < $global_terms_recurse++ ) { + return $term_id; + $recurse_start = false; + } + $term_id = intval( $term_id ); $c = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->terms WHERE term_id = %d", $term_id ) ); $global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE category_nicename = %s", $c->slug ) ); if ( $global_id == null ) { - $wpdb->insert( $wpdb->sitecategories, array('cat_name' => $c->name, 'category_nicename' => $c->slug) ); - $global_id = $wpdb->insert_id; + $used_global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE cat_ID = %d", $c->term_id ) ); + if ( null == $used_global_id ) { + $wpdb->insert( $wpdb->sitecategories, array( 'cat_ID' => $term_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) ); + $global_id = $wpdb->insert_id; + } else { + $max_global_id = $wpdb->get_var( "SELECT MAX(cat_ID) FROM $wpdb->sitecategories" ); + $max_global_id += mt_rand( 100, 400 ); + $wpdb->insert( $wpdb->sitecategories, array( 'cat_ID' => $global_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) ); + $global_id = $wpdb->insert_id; + } + } elseif ( $global_id != $term_id ) { + $local_id = $wpdb->get_row( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE term_id = %d", $global_id ) ); + if ( null != $local_id ) + $local_id = global_terms( $local_id ); + if ( 10 < $global_terms_recurse ) + $global_id = $term_id; } - if ( $global_id == $term_id ) - return $global_id; + if ( $global_id != $term_id ) { + if ( get_option( 'default_category' ) == $term_id ) + update_option( 'default_category', $global_id ); - if ( get_option( 'default_category' ) == $term_id ) - update_option( 'default_category', $global_id ); + $wpdb->update( $wpdb->terms, array('term_id' => $global_id), array('term_id' => $term_id) ); + $wpdb->update( $wpdb->term_taxonomy, array('term_id' => $global_id), array('term_id' => $term_id) ); + $wpdb->update( $wpdb->term_taxonomy, array('parent' => $global_id), array('parent' => $term_id) ); - $wpdb->update( $wpdb->terms, array('term_id' => $global_id), array('term_id' => $term_id) ); - $wpdb->update( $wpdb->term_taxonomy, array('term_id' => $global_id), array('term_id' => $term_id) ); - $wpdb->update( $wpdb->term_taxonomy, array('parent' => $global_id), array('parent' => $term_id) ); - - clean_term_cache($term_id); + clean_term_cache($term_id); + } + if( $recurse_start ) + unset( $global_terms_recurse ); return $global_id; } diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index 36b037eca..7e24ba52b 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -1545,30 +1545,16 @@ function wp_insert_term( $term, $taxonomy, $args = array() ) { if ( ! $term_id = is_term($slug, $taxonomy) ) { // Make sure the slug is unique accross all taxonomies. $slug = wp_unique_term_slug($slug, (object) $args); - if ( !is_multisite() ) { - if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) ) - return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error); - $term_id = (int) $wpdb->insert_id; - } else { - $maxterm = $wpdb->get_var( "SELECT max(term_id) FROM {$wpdb->terms}" ); - $term_id = mt_rand( $maxterm+100, $maxterm+4000 ); - if ( false === $wpdb->insert( $wpdb->terms, compact( 'term_id', 'name', 'slug', 'term_group' ) ) ) - return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error); - } + if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) ) + return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error); + $term_id = (int) $wpdb->insert_id; } else if ( is_taxonomy_hierarchical($taxonomy) && !empty($parent) ) { // If the taxonomy supports hierarchy and the term has a parent, make the slug unique // by incorporating parent slugs. $slug = wp_unique_term_slug($slug, (object) $args); - if ( !is_multisite() ) { - if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) ) - return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error); - $term_id = (int) $wpdb->insert_id; - } else { - $maxterm = $wpdb->get_var( "SELECT max(term_id) FROM {$wpdb->terms}" ); - $term_id = mt_rand( $maxterm+100, $maxterm+4000 ); - if ( false === $wpdb->insert( $wpdb->terms, compact( 'term_id','name', 'slug', 'term_group' ) ) ) - return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error); - } + if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) ) + return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error); + $term_id = (int) $wpdb->insert_id; } if ( empty($slug) ) {