upgrade_230

Execute changes made in WordPress 2.3.

Description

upgrade_230(); 

Usage

  1. if ( !function_exists( 'upgrade_230' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/upgrade.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = upgrade_230(); 
  7.  

Defined (1)

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

/wp-admin/includes/upgrade.php  
  1. function upgrade_230() { 
  2. global $wp_current_db_version, $wpdb; 
  3.  
  4. if ( $wp_current_db_version < 5200 ) { 
  5.  
  6. // Convert categories to terms. 
  7. $tt_ids = array(); 
  8. $have_tags = false; 
  9. $categories = $wpdb->get_results("SELECT * FROM $wpdb->categories ORDER BY cat_ID"); 
  10. foreach ($categories as $category) { 
  11. $term_id = (int) $category->cat_ID; 
  12. $name = $category->cat_name; 
  13. $description = $category->category_description; 
  14. $slug = $category->category_nicename; 
  15. $parent = $category->category_parent; 
  16. $term_group = 0; 
  17.  
  18. // Associate terms with the same slug in a term group and make slugs unique. 
  19. if ( $exists = $wpdb->get_results( $wpdb->prepare("SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $slug) ) ) { 
  20. $term_group = $exists[0]->term_group; 
  21. $id = $exists[0]->term_id; 
  22. $num = 2; 
  23. do { 
  24. $alt_slug = $slug . "-$num"; 
  25. $num++; 
  26. $slug_check = $wpdb->get_var( $wpdb->prepare("SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug) ); 
  27. } while ( $slug_check ); 
  28.  
  29. $slug = $alt_slug; 
  30.  
  31. if ( empty( $term_group ) ) { 
  32. $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms GROUP BY term_group") + 1; 
  33. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->terms SET term_group = %d WHERE term_id = %d", $term_group, $id) ); 
  34.  
  35. $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->terms (term_id, name, slug, term_group) VALUES 
  36. (%d, %s, %s, %d)", $term_id, $name, $slug, $term_group) ); 
  37.  
  38. $count = 0; 
  39. if ( !empty($category->category_count) ) { 
  40. $count = (int) $category->category_count; 
  41. $taxonomy = 'category'; 
  42. $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ( %d, %s, %s, %d, %d)", $term_id, $taxonomy, $description, $parent, $count) ); 
  43. $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id; 
  44.  
  45. if ( !empty($category->link_count) ) { 
  46. $count = (int) $category->link_count; 
  47. $taxonomy = 'link_category'; 
  48. $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ( %d, %s, %s, %d, %d)", $term_id, $taxonomy, $description, $parent, $count) ); 
  49. $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id; 
  50.  
  51. if ( !empty($category->tag_count) ) { 
  52. $have_tags = true; 
  53. $count = (int) $category->tag_count; 
  54. $taxonomy = 'post_tag'; 
  55. $wpdb->insert( $wpdb->term_taxonomy, compact('term_id', 'taxonomy', 'description', 'parent', 'count') ); 
  56. $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id; 
  57.  
  58. if ( empty($count) ) { 
  59. $count = 0; 
  60. $taxonomy = 'category'; 
  61. $wpdb->insert( $wpdb->term_taxonomy, compact('term_id', 'taxonomy', 'description', 'parent', 'count') ); 
  62. $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id; 
  63.  
  64. $select = 'post_id, category_id'; 
  65. if ( $have_tags ) 
  66. $select .= ', rel_type'; 
  67.  
  68. $posts = $wpdb->get_results("SELECT $select FROM $wpdb->post2cat GROUP BY post_id, category_id"); 
  69. foreach ( $posts as $post ) { 
  70. $post_id = (int) $post->post_id; 
  71. $term_id = (int) $post->category_id; 
  72. $taxonomy = 'category'; 
  73. if ( !empty($post->rel_type) && 'tag' == $post->rel_type) 
  74. $taxonomy = 'tag'; 
  75. $tt_id = $tt_ids[$term_id][$taxonomy]; 
  76. if ( empty($tt_id) ) 
  77. continue; 
  78.  
  79. $wpdb->insert( $wpdb->term_relationships, array('object_id' => $post_id, 'term_taxonomy_id' => $tt_id) ); 
  80.  
  81. // < 3570 we used linkcategories. >= 3570 we used categories and link2cat. 
  82. if ( $wp_current_db_version < 3570 ) { 
  83. /** 
  84. * Create link_category terms for link categories. Create a map of link 
  85. * cat IDs to link_category terms. 
  86. */ 
  87. $link_cat_id_map = array(); 
  88. $default_link_cat = 0; 
  89. $tt_ids = array(); 
  90. $link_cats = $wpdb->get_results("SELECT cat_id, cat_name FROM " . $wpdb->prefix . 'linkcategories'); 
  91. foreach ( $link_cats as $category) { 
  92. $cat_id = (int) $category->cat_id; 
  93. $term_id = 0; 
  94. $name = wp_slash($category->cat_name); 
  95. $slug = sanitize_title($name); 
  96. $term_group = 0; 
  97.  
  98. // Associate terms with the same slug in a term group and make slugs unique. 
  99. if ( $exists = $wpdb->get_results( $wpdb->prepare("SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $slug) ) ) { 
  100. $term_group = $exists[0]->term_group; 
  101. $term_id = $exists[0]->term_id; 
  102.  
  103. if ( empty($term_id) ) { 
  104. $wpdb->insert( $wpdb->terms, compact('name', 'slug', 'term_group') ); 
  105. $term_id = (int) $wpdb->insert_id; 
  106.  
  107. $link_cat_id_map[$cat_id] = $term_id; 
  108. $default_link_cat = $term_id; 
  109.  
  110. $wpdb->insert( $wpdb->term_taxonomy, array('term_id' => $term_id, 'taxonomy' => 'link_category', 'description' => '', 'parent' => 0, 'count' => 0) ); 
  111. $tt_ids[$term_id] = (int) $wpdb->insert_id; 
  112.  
  113. // Associate links to cats. 
  114. $links = $wpdb->get_results("SELECT link_id, link_category FROM $wpdb->links"); 
  115. if ( !empty($links) ) foreach ( $links as $link ) { 
  116. if ( 0 == $link->link_category ) 
  117. continue; 
  118. if ( ! isset($link_cat_id_map[$link->link_category]) ) 
  119. continue; 
  120. $term_id = $link_cat_id_map[$link->link_category]; 
  121. $tt_id = $tt_ids[$term_id]; 
  122. if ( empty($tt_id) ) 
  123. continue; 
  124.  
  125. $wpdb->insert( $wpdb->term_relationships, array('object_id' => $link->link_id, 'term_taxonomy_id' => $tt_id) ); 
  126.  
  127. // Set default to the last category we grabbed during the upgrade loop. 
  128. update_option('default_link_category', $default_link_cat); 
  129. } else { 
  130. $links = $wpdb->get_results("SELECT link_id, category_id FROM $wpdb->link2cat GROUP BY link_id, category_id"); 
  131. foreach ( $links as $link ) { 
  132. $link_id = (int) $link->link_id; 
  133. $term_id = (int) $link->category_id; 
  134. $taxonomy = 'link_category'; 
  135. $tt_id = $tt_ids[$term_id][$taxonomy]; 
  136. if ( empty($tt_id) ) 
  137. continue; 
  138. $wpdb->insert( $wpdb->term_relationships, array('object_id' => $link_id, 'term_taxonomy_id' => $tt_id) ); 
  139.  
  140. if ( $wp_current_db_version < 4772 ) { 
  141. // Obsolete linkcategories table 
  142. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'linkcategories'); 
  143.  
  144. // Recalculate all counts 
  145. $terms = $wpdb->get_results("SELECT term_taxonomy_id, taxonomy FROM $wpdb->term_taxonomy"); 
  146. foreach ( (array) $terms as $term ) { 
  147. if ( ('post_tag' == $term->taxonomy) || ('category' == $term->taxonomy) ) 
  148. $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type = 'post' AND term_taxonomy_id = %d", $term->term_taxonomy_id) ); 
  149. else 
  150. $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term->term_taxonomy_id) ); 
  151. $wpdb->update( $wpdb->term_taxonomy, array('count' => $count), array('term_taxonomy_id' => $term->term_taxonomy_id) );