mirror of
https://github.com/WordPress/WordPress.git
synced 2025-01-22 08:11:52 +01:00
rework syncing of global terms, see #12663
git-svn-id: http://svn.automattic.com/wordpress/trunk@13925 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
44ae35ec09
commit
1f7d47157d
@ -1205,23 +1205,44 @@ 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) );
|
||||
$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 );
|
||||
|
||||
@ -1230,6 +1251,9 @@ function global_terms( $term_id, $deprecated = '' ) {
|
||||
$wpdb->update( $wpdb->term_taxonomy, array('parent' => $global_id), array('parent' => $term_id) );
|
||||
|
||||
clean_term_cache($term_id);
|
||||
}
|
||||
if( $recurse_start )
|
||||
unset( $global_terms_recurse );
|
||||
|
||||
return $global_id;
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
} 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 ( empty($slug) ) {
|
||||
|
Loading…
Reference in New Issue
Block a user