Ticket #6909: tag-to-cat2.patch
File tag-to-cat2.patch, 20.2 KB (added by , 17 years ago) |
---|
-
wp-admin/import/wp-cat2tag.php
24 24 } 25 25 26 26 function populate_cats() { 27 global $wpdb;28 27 29 28 $categories = get_categories('get=all'); 30 29 foreach ( $categories as $category ) { 31 30 $this->all_categories[] = $category; 32 if ( tag_exists( $wpdb->escape($category->name)) )31 if ( is_term( $category->slug, 'post_tag' ) ) 33 32 $this->hybrids_ids[] = $category->term_id; 34 33 } 35 34 } 36 35 37 36 function populate_tags() { 38 global $wpdb;39 37 40 38 $tags = get_terms( array('post_tag'), 'get=all' ); 41 39 foreach ( $tags as $tag ) { 42 40 $this->all_tags[] = $tag; 43 if ( $this->_category_exists($tag->term_id) )41 if ( is_term( $tag->slug, 'category' ) ) 44 42 $this->hybrids_ids[] = $tag->term_id; 45 43 } 46 44 } … … 58 56 echo '<p>' . __('Keep in mind that if you convert a category with child categories, the children become top-level orphans.') . '</p></div>'; 59 57 60 58 $this->categories_form(); 61 } elseif ( $hyb_num > 0 ) {62 echo '<p>' . __('You have no categories that can be converted. However some of your categories are both a tag and a category.') . '</p>';63 59 } else { 64 60 echo '<p>'.__('You have no categories to convert!').'</p>'; 65 61 } … … 92 88 </script> 93 89 94 90 <form name="catlist" id="catlist" action="admin.php?import=wp-cat2tag&step=2" method="post"> 95 <p><input type="button" class="button-secondary" value="<?php _e('Check All'); ?>" onclick="this.value=check_all_rows()" >91 <p><input type="button" class="button-secondary" value="<?php _e('Check All'); ?>" onclick="this.value=check_all_rows()" /> 96 92 <?php wp_nonce_field('import-cat2tag'); ?></p> 97 93 <ul style="list-style:none"> 98 <?php99 $hier = _get_term_hierarchy('category');100 94 95 <?php $hier = _get_term_hierarchy('category'); 96 101 97 foreach ($this->all_categories as $category) { 102 98 $category = sanitize_term( $category, 'category', 'display' ); 103 99 104 if ( (int) $category->parent == 0 ) { 105 if ( in_array( intval($category->term_id), $this->hybrids_ids ) ) { 106 ?> 107 <li style="color:#777;padding-left:1.3em;"><?php echo $category->name . ' (' . $category->count . ') *'; ?></li> 108 <?php 109 } else { 110 ?> 100 if ( (int) $category->parent == 0 ) { ?> 101 111 102 <li><label><input type="checkbox" name="cats_to_convert[]" value="<?php echo intval($category->term_id); ?>" /> <?php echo $category->name . ' (' . $category->count . ')'; ?></label><?php 112 103 113 if ( isset($hier[$category->term_id]) ) 114 $this->_category_children($category, $hier); 115 ?></li> 116 <?php 117 } 118 } 119 } 120 ?> 104 if ( in_array( intval($category->term_id), $this->hybrids_ids ) ) 105 echo ' <a href="#note"> * </a>'; 106 107 if ( isset($hier[$category->term_id]) ) 108 $this->_category_children($category, $hier); ?></li> 109 <?php } 110 } ?> 121 111 </ul> 122 112 123 <?php 124 if ( ! empty($this->hybrids_ids) ) { 125 echo '<p>' . __('* This category is also a tag. It cannot be convert again.') . '</p>'; 126 } 127 ?> 113 <?php if ( ! empty($this->hybrids_ids) ) 114 echo '<p><a name="note"></a>' . __('* This category is also a tag. Converting it will add that tag to all posts that are currently in the category.') . '</p>'; ?> 115 128 116 <p class="submit"><input type="submit" name="submit" class="button" value="<?php _e('Convert Categories to Tags'); ?>" /></p> 129 117 </form> 130 118 131 <?php 132 } 119 <?php } 133 120 134 121 function tags_tab() { 135 122 $this->populate_tags(); … … 142 129 echo '<div class="narrow">'; 143 130 echo '<p>' . __('Here you can selectively converts existing tags to categories. To get started, check the tags you wish to be converted, then click the Convert button.') . '</p>'; 144 131 echo '<p>' . __('The newly created categories will still be associated with the same posts.') . '</p></div>'; 145 146 132 147 133 $this->tags_form(); 148 } elseif ( $hyb_num > 0 ) {149 echo '<p>' . __('You have no tags that can be converted. However some of your tags are both a tag and a category.') . '</p>';150 134 } else { 151 135 echo '<p>'.__('You have no tags to convert!').'</p>'; 152 136 } … … 179 163 </script> 180 164 181 165 <form name="taglist" id="taglist" action="admin.php?import=wp-cat2tag&step=4" method="post"> 182 <p><input type="button" class="button-secondary" value="<?php _e('Check All'); ?>" onclick="this.value=check_all_tagrows()" >166 <p><input type="button" class="button-secondary" value="<?php _e('Check All'); ?>" onclick="this.value=check_all_tagrows()" /> 183 167 <?php wp_nonce_field('import-cat2tag'); ?></p> 184 168 <ul style="list-style:none"> 185 <?php186 foreach ( $this->all_tags as $tag ) {187 if ( in_array( intval($tag->term_id), $this->hybrids_ids ) ) {188 ?>189 <li style="color:#777;padding-left:1.3em;"><?php echo attribute_escape($tag->name) . ' (' . $tag->count . ') *'; ?></li>190 <?php191 } else {192 ?>193 <li><label><input type="checkbox" name="tags_to_convert[]" value="<?php echo intval($tag->term_id); ?>" /> <?php echo attribute_escape($tag->name) . ' (' . $tag->count . ')'; ?></label></li>194 169 195 <?php 196 }197 } 198 ?>170 <?php foreach ( $this->all_tags as $tag ) { ?> 171 <li><label><input type="checkbox" name="tags_to_convert[]" value="<?php echo intval($tag->term_id); ?>" /> <?php echo attribute_escape($tag->name) . ' (' . $tag->count . ')'; ?></label><?php if ( in_array( intval($tag->term_id), $this->hybrids_ids ) ) echo ' <a href="#note"> * </a>'; ?></li> 172 173 <?php } ?> 199 174 </ul> 200 175 201 <?php 202 if ( ! empty($this->hybrids_ids) ) 203 echo '<p>' . __('* This tag is also a category. It cannot be converted again.') . '</p>'; 204 ?> 176 <?php if ( ! empty($this->hybrids_ids) ) 177 echo '<p><a name="note"></a>' . __('* This tag is also a category. When converted, all posts associated with the tag will also be in the category.') . '</p>'; ?> 205 178 206 179 <p class="submit"><input type="submit" name="submit_tags" class="button" value="<?php _e('Convert Tags to Categories'); ?>" /></p> 207 180 </form> 208 181 209 <?php 210 } 182 <?php } 211 183 212 function _category_children($parent, $hier) { 213 ?> 184 function _category_children($parent, $hier) { ?> 214 185 215 186 <ul style="list-style:none"> 216 <?php 217 foreach ($hier[$parent->term_id] as $child_id) { 218 $child =& get_category($child_id); 219 220 if ( in_array( intval($child->term_id), $this->hybrids_ids ) ) { 221 ?> 222 <li style="color:#777;padding-left:1.3em;"><?php echo $child->name . ' (' . $child->count . ') *'; ?></li> 223 <?php 224 } else { 225 ?> 226 <li><label><input type="checkbox" name="cats_to_convert[]" value="<?php echo intval($child->term_id); ?>" /> <?php echo $child->name . ' (' . $child->count . ')'; ?></label> 227 <?php 187 <?php foreach ($hier[$parent->term_id] as $child_id) { 188 $child =& get_category($child_id); ?> 189 <li><label><input type="checkbox" name="cats_to_convert[]" value="<?php echo intval($child->term_id); ?>" /> <?php echo $child->name . ' (' . $child->count . ')'; ?></label><?php 228 190 191 if ( in_array( intval($child->term_id), $this->hybrids_ids ) ) 192 echo ' <a href="#note"> * </a>'; 193 229 194 if ( isset($hier[$child->term_id]) ) 230 $this->_category_children($child, $hier); 231 ?> </li> 232 <?php 233 } 234 } 235 ?> 236 </ul> 237 <?php 195 $this->_category_children($child, $hier); ?></li> 196 <?php } ?> 197 </ul><?php 238 198 } 239 199 240 200 function _category_exists($cat_id) { … … 252 212 function convert_categories() { 253 213 global $wpdb; 254 214 255 if ( (!isset($_POST['cats_to_convert']) || !is_array($_POST['cats_to_convert'])) && empty($this->categories_to_convert)) { 256 echo '<div class="narrow">';257 echo '<p>' . sprintf(__('Uh, oh. Something didn’t work. Please <a href="%s">try again</a>.'), 'admin.php?import=wp-cat2tag') . '</p>';258 echo '</div>';259 215 if ( (!isset($_POST['cats_to_convert']) || !is_array($_POST['cats_to_convert'])) && empty($this->categories_to_convert)) { ?> 216 <div class="narrow"> 217 <p><?php printf(__('Uh, oh. Something didn’t work. Please <a href="%s">try again</a>.'), 'admin.php?import=wp-cat2tag'); ?></p> 218 </div> 219 <?php return; 260 220 } 261 221 262 222 if ( empty($this->categories_to_convert) ) 263 223 $this->categories_to_convert = $_POST['cats_to_convert']; 224 264 225 $hier = _get_term_hierarchy('category'); 226 $hybrid_cats = $clear_parents = $parents = false; 227 $clean_term_cache = $clean_cat_cache = array(); 228 $default_cat = get_option('default_category'); 265 229 266 230 echo '<ul>'; 267 231 268 232 foreach ( (array) $this->categories_to_convert as $cat_id) { 269 233 $cat_id = (int) $cat_id; 270 234 271 echo '<li>' . sprintf(__('Converting category #%s ... '), $cat_id); 272 273 if (!$this->_category_exists($cat_id)) { 274 _e('Category doesn\'t exist!'); 235 if ( ! $this->_category_exists($cat_id) ) { 236 echo '<li>' . sprintf( __('Category %s doesn\'t exist!'), $cat_id ) . "</li>\n"; 275 237 } else { 276 238 $category =& get_category($cat_id); 239 echo '<li>' . sprintf(__('Converting category <strong>%s</strong> ... '), $category->name); 277 240 278 if ( tag_exists($wpdb->escape($category->name)) ) { 279 _e('Category is already a tag.'); 280 echo '</li>'; 281 continue; 282 } 241 // If the category is the default, leave category in place and create tag. 242 if ( $default_cat == $category->term_id ) { 283 243 284 // If the category is the default, leave category in place and create tag.285 if ( get_option('default_category') == $category->term_id ) {286 $id = wp_insert_term($category->name, 'post_tag', array('slug' => $category->slug));244 if ( ! ($id = is_term( $category->slug, 'post_tag' ) ) ) 245 $id = wp_insert_term($category->name, 'post_tag', array('slug' => $category->slug)); 246 287 247 $id = $id['term_taxonomy_id']; 288 248 $posts = get_objects_in_term($category->term_id, 'category'); 249 $term_order = 0; 250 289 251 foreach ( $posts as $post ) { 290 if ( !$wpdb->get_var( $wpdb->prepare("SELECT object_id FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id = %d", $post, $id) ) ) 291 $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES (%d, %d)", $post, $id) ); 252 $values[] = $wpdb->prepare( "(%d, %d, %d)", $post, $id, $term_order); 292 253 clean_post_cache($post); 293 254 } 294 } else { 295 $tt_ids = $wpdb->get_col( $wpdb->prepare("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'category'", $category->term_id) ); 296 if ( $tt_ids ) { 297 $posts = $wpdb->get_col("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN (" . join(',', $tt_ids) . ") GROUP BY object_id"); 298 foreach ( (array) $posts as $post ) 299 clean_post_cache($post); 255 256 if ( $values ) { 257 $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . join(',', $values) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)"); 258 259 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET count = %d WHERE term_id = %d AND taxonomy = 'post_tag'", $category->count, $category->term_id) ); 300 260 } 301 261 302 // Change the category to a tag. 303 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET taxonomy = 'post_tag' WHERE term_id = %d AND taxonomy = 'category'", $category->term_id) ); 262 echo __('Converted successfully.') . "</li>\n"; 263 continue; 264 } 265 266 // if tag already exists, add it to all posts in the category 267 if ( $tag_ttid = $wpdb->get_var( $wpdb->prepare("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'post_tag'", $category->term_id) ) ) { 268 $objects_ids = get_objects_in_term($category->term_id, 'category'); 269 $tag_ttid = (int) $tag_ttid; 270 $term_order = 0; 304 271 305 $terms = $wpdb->get_col( $wpdb->prepare("SELECT term_id FROM $wpdb->term_taxonomy WHERE parent = %d AND taxonomy = 'category'", $category->term_id) ); 306 foreach ( (array) $terms as $term ) 307 clean_category_cache($term); 272 foreach ( $objects_ids as $object_id ) 273 $values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $tag_ttid, $term_order); 308 274 309 // Set all parents to 0 (root-level) if their parent was the converted tag 310 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET parent = 0 WHERE parent = %d AND taxonomy = 'category'", $category->term_id) ); 275 if ( $values ) { 276 $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . join(',', $values) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)"); 277 278 $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tag_ttid) ); 279 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET count = %d WHERE term_id = %d AND taxonomy = 'post_tag'", $count, $category->term_id) ); 280 } 281 echo __('Tag added to all posts in this category.') . " *</li>\n"; 282 283 $hybrid_cats = true; 284 $clean_term_cache[] = $category->term_id; 285 $clean_cat_cache[] = $category->term_id; 286 287 continue; 311 288 } 312 // Clean the cache313 clean_category_cache($category->term_id);314 289 315 _e('Converted successfully.'); 290 $tt_ids = $wpdb->get_col( $wpdb->prepare("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'category'", $category->term_id) ); 291 if ( $tt_ids ) { 292 $posts = $wpdb->get_col("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN (" . join(',', $tt_ids) . ") GROUP BY object_id"); 293 foreach ( (array) $posts as $post ) 294 clean_post_cache($post); 295 } 296 297 // Change the category to a tag. 298 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET taxonomy = 'post_tag' WHERE term_id = %d AND taxonomy = 'category'", $category->term_id) ); 299 300 // Set all parents to 0 (root-level) if their parent was the converted tag 301 $parents = $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET parent = 0 WHERE parent = %d AND taxonomy = 'category'", $category->term_id) ); 302 303 if ( $parents ) $clear_parents = true; 304 $clean_cat_cache[] = $category->term_id; 305 echo __('Converted successfully.') . "</li>\n"; 316 306 } 307 } 308 echo '</ul>'; 317 309 318 echo '</li>'; 310 if ( ! empty($clean_term_cache) ) { 311 $clean_term_cache = array_unique(array_values($clean_term_cache)); 312 foreach ( $clean_term_cache as $id ) 313 wp_cache_delete($id, 'post_tag'); 319 314 } 320 315 321 echo '</ul>'; 316 if ( ! empty($clean_cat_cache) ) { 317 $clean_cat_cache = array_unique(array_values($clean_cat_cache)); 318 foreach ( $clean_cat_cache as $id ) 319 wp_cache_delete($id, 'category'); 320 } 321 322 if ( $clear_parents ) delete_option('category_children'); 323 324 if ( $hybrid_cats ) 325 echo '<p>' . sprintf( __('* This category is also a tag. The converter has added that tag to all posts currently in the category. If you want to remove it, please confirm that all tags were added successfully, then delete it from the <a href="%s">Manage Categories</a> page.'), 'categories.php') . '</p>'; 322 326 echo '<p>' . sprintf( __('We’re all done here, but you can always <a href="%s">convert more</a>.'), 'admin.php?import=wp-cat2tag' ) . '</p>'; 323 327 } 324 328 … … 332 336 return; 333 337 } 334 338 335 if ( empty($this-> categories_to_convert) )339 if ( empty($this->tags_to_convert) ) 336 340 $this->tags_to_convert = $_POST['tags_to_convert']; 337 341 338 $clean_cache = array(); 342 $hybrid_tags = $clear_parents = false; 343 $clean_cat_cache = $clean_term_cache = array(); 344 $default_cat = get_option('default_category'); 339 345 echo '<ul>'; 340 346 341 347 foreach ( (array) $this->tags_to_convert as $tag_id) { 342 348 $tag_id = (int) $tag_id; 343 349 344 echo '<li>' . sprintf(__('Converting tag #%s ... '), $tag_id); 350 if ( $tag = get_term( $tag_id, 'post_tag' ) ) { 351 printf('<li>' . __('Converting tag <strong>%s</strong> ... '), $tag->name); 345 352 346 if ( ! is_term($tag_id, 'post_tag') ) { 347 _e('Tag doesn\'t exist!'); 348 } else { 353 if ( $cat_ttid = $wpdb->get_var( $wpdb->prepare("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'category'", $tag->term_id) ) ) { 354 $objects_ids = get_objects_in_term($tag->term_id, 'post_tag'); 355 $cat_ttid = (int) $cat_ttid; 356 $term_order = 0; 349 357 350 if ( is_term($tag_id, 'category') ) { 351 _e('This Tag is already a Category.'); 352 echo '</li>'; 358 foreach ( $objects_ids as $object_id ) { 359 $values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $cat_ttid, $term_order); 360 clean_post_cache($object_id); 361 } 362 363 if ( $values ) { 364 $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . join(',', $values) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)"); 365 366 if ( $default_cat != $tag->term_id ) { 367 $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tag->term_id) ); 368 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET count = %d WHERE term_id = %d AND taxonomy = 'category'", $count, $tag->term_id) ); 369 } 370 } 371 372 $hybrid_tags = true; 373 $clean_term_cache[] = $tag->term_id; 374 $clean_cat_cache[] = $tag->term_id; 375 echo __('All posts were added to the category with the same name.') . " *</li>\n"; 376 353 377 continue; 354 378 } 355 379 356 $tt_ids = $wpdb->get_col( $wpdb->prepare("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'post_tag'", $tag_id) );357 if ( $tt_ids ) {358 $posts = $wpdb->get_col("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN (" . join(',', $tt_ids) . ") GROUP BY object_id");359 foreach ( (array) $posts as $post )360 clean_post_cache($post);361 }362 363 380 // Change the tag to a category. 364 $parent = $wpdb->get_var( $wpdb->prepare("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'post_tag'", $tag _id) );365 if ( 0 == $parent || (0 < (int) $parent && $this->_category_exists($parent)) ) 381 $parent = $wpdb->get_var( $wpdb->prepare("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'post_tag'", $tag->term_id) ); 382 if ( 0 == $parent || (0 < (int) $parent && $this->_category_exists($parent)) ) { 366 383 $reset_parent = ''; 367 else $reset_parent = ", parent = '0'"; 384 $clear_parents = true; 385 } else 386 $reset_parent = ", parent = '0'"; 368 387 369 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET taxonomy = 'category' $reset_parent WHERE term_id = %d AND taxonomy = 'post_tag'", $tag _id) );388 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET taxonomy = 'category' $reset_parent WHERE term_id = %d AND taxonomy = 'post_tag'", $tag->term_id) ); 370 389 371 // Clean the cache 372 $clean_cache[] = $tag_id; 390 $clean_term_cache[] = $tag->term_id; 391 $clean_cat_cache[] = $cat['term_id']; 392 echo __('Converted successfully.') . "</li>\n"; 373 393 374 _e('Converted successfully.'); 394 } else { 395 printf( '<li>' . __('Tag #%s doesn\'t exist!') . "</li>\n", $tag_id ); 375 396 } 397 } 376 398 377 echo '</li>'; 399 if ( ! empty($clean_term_cache) ) { 400 $clean_term_cache = array_unique(array_values($clean_term_cache)); 401 foreach ( $clean_term_cache as $id ) 402 wp_cache_delete($id, 'post_tag'); 378 403 } 379 404 380 clean_term_cache( $clean_cache, 'post_tag' ); 381 delete_option('category_children'); 382 405 if ( ! empty($clean_cat_cache) ) { 406 $clean_cat_cache = array_unique(array_values($clean_cat_cache)); 407 foreach ( $clean_cat_cache as $id ) 408 wp_cache_delete($id, 'category'); 409 } 410 411 if ( $clear_parents ) delete_option('category_children'); 412 383 413 echo '</ul>'; 414 if ( $hybrid_tags ) 415 echo '<p>' . sprintf( __('* This tag is also a category. The converter has added all posts from it to the category. If you want to remove it, please confirm that all posts were added successfully, then delete it from the <a href="%s">Manage Tags</a> page.'), 'edit-tags.php') . '</p>'; 384 416 echo '<p>' . sprintf( __('We’re all done here, but you can always <a href="%s">convert more</a>.'), 'admin.php?import=wp-cat2tag&step=3' ) . '</p>'; 385 417 } 386 418 … … 401 433 check_admin_referer('import-cat2tag'); 402 434 $this->convert_categories(); 403 435 break; 404 436 405 437 case 3 : 406 438 $this->tags_tab(); 407 439 break; 408 440 409 441 case 4 : 410 442 check_admin_referer('import-cat2tag'); 411 443 $this->convert_tags(); -
wp-admin/wp-admin.css
547 547 border-top-style: solid; 548 548 } 549 549 550 #wphead a, #dashmenu a, #adminmenu a, #submenu a, #sidemenu a {550 #wphead a, #dashmenu a, #adminmenu a, #submenu a, #sidemenu a, #taglist a, #catlist a { 551 551 text-decoration: none; 552 552 } 553 553