global_terms

Maintains a canonical list of terms by syncing terms created for each blog with the global terms table.

Description

(int) global_terms( (int) $term_id, (string) $deprecated = '' ); 

Returns (int)

An ID from the global terms table mapped from $term_id.

Parameters (2)

0. $term_id (int)
An ID for a term on the current blog.
1. $deprecated — Optional. (string) => ''
The deprecated.

Usage

  1. if ( !function_exists( 'global_terms' ) ) { 
  2. require_once ABSPATH . WPINC . '/ms-functions.php'; 
  3.  
  4. // An ID for a term on the current blog. 
  5. $term_id = -1; 
  6.  
  7. // The deprecated. 
  8. $deprecated = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = global_terms($term_id, $deprecated); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/ms-functions.php  
  1. function global_terms( $term_id, $deprecated = '' ) { 
  2. global $wpdb; 
  3. static $global_terms_recurse = null; 
  4.  
  5. if ( !global_terms_enabled() ) 
  6. return $term_id; 
  7.  
  8. // prevent a race condition 
  9. $recurse_start = false; 
  10. if ( $global_terms_recurse === null ) { 
  11. $recurse_start = true; 
  12. $global_terms_recurse = 1; 
  13. } elseif ( 10 < $global_terms_recurse++ ) { 
  14. return $term_id; 
  15.  
  16. $term_id = intval( $term_id ); 
  17. $c = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->terms WHERE term_id = %d", $term_id ) ); 
  18.  
  19. $global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE category_nicename = %s", $c->slug ) ); 
  20. if ( $global_id == null ) { 
  21. $used_global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE cat_ID = %d", $c->term_id ) ); 
  22. if ( null == $used_global_id ) { 
  23. $wpdb->insert( $wpdb->sitecategories, array( 'cat_ID' => $term_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) ); 
  24. $global_id = $wpdb->insert_id; 
  25. if ( empty( $global_id ) ) 
  26. return $term_id; 
  27. } else { 
  28. $max_global_id = $wpdb->get_var( "SELECT MAX(cat_ID) FROM $wpdb->sitecategories" ); 
  29. $max_local_id = $wpdb->get_var( "SELECT MAX(term_id) FROM $wpdb->terms" ); 
  30. $new_global_id = max( $max_global_id, $max_local_id ) + mt_rand( 100, 400 ); 
  31. $wpdb->insert( $wpdb->sitecategories, array( 'cat_ID' => $new_global_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) ); 
  32. $global_id = $wpdb->insert_id; 
  33. } elseif ( $global_id != $term_id ) { 
  34. $local_id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE term_id = %d", $global_id ) ); 
  35. if ( null != $local_id ) { 
  36. global_terms( $local_id ); 
  37. if ( 10 < $global_terms_recurse ) { 
  38. $global_id = $term_id; 
  39.  
  40. if ( $global_id != $term_id ) { 
  41. if ( get_option( 'default_category' ) == $term_id ) 
  42. update_option( 'default_category', $global_id ); 
  43.  
  44. $wpdb->update( $wpdb->terms, array('term_id' => $global_id), array('term_id' => $term_id) ); 
  45. $wpdb->update( $wpdb->term_taxonomy, array('term_id' => $global_id), array('term_id' => $term_id) ); 
  46. $wpdb->update( $wpdb->term_taxonomy, array('parent' => $global_id), array('parent' => $term_id) ); 
  47.  
  48. clean_term_cache($term_id); 
  49. if ( $recurse_start ) 
  50. $global_terms_recurse = null; 
  51.  
  52. return $global_id;