| [ Index ] |
PHP Cross Reference of Wordpress 2.7.1 |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * WordPress Categories to Tags Converter. 4 * 5 * @package WordPress 6 * @subpackage Importer 7 */ 8 9 /** 10 * WordPress categories to tags converter class. 11 * 12 * Will convert WordPress categories to tags, removing the category after the 13 * process is complete and updating all posts to switch to the tag. 14 * 15 * @since unknown 16 */ 17 class WP_Categories_to_Tags { 18 var $categories_to_convert = array(); 19 var $all_categories = array(); 20 var $tags_to_convert = array(); 21 var $all_tags = array(); 22 var $hybrids_ids = array(); 23 24 function header() { 25 echo '<div class="wrap">'; 26 if ( ! current_user_can('manage_categories') ) { 27 echo '<div class="narrow">'; 28 echo '<p>' . __('Cheatin’ uh?') . '</p>'; 29 echo '</div>'; 30 } else { ?> 31 <div class="tablenav"><p style="margin:4px"><a style="display:inline;" class="button-secondary" href="admin.php?import=wp-cat2tag"><?php _e( "Categories to Tags" ); ?></a> 32 <a style="display:inline;" class="button-secondary" href="admin.php?import=wp-cat2tag&step=3"><?php _e( "Tags to Categories" ); ?></a></p></div> 33 <?php } 34 } 35 36 function footer() { 37 echo '</div>'; 38 } 39 40 function populate_cats() { 41 42 $categories = get_categories('get=all'); 43 foreach ( $categories as $category ) { 44 $this->all_categories[] = $category; 45 if ( is_term( $category->slug, 'post_tag' ) ) 46 $this->hybrids_ids[] = $category->term_id; 47 } 48 } 49 50 function populate_tags() { 51 52 $tags = get_terms( array('post_tag'), 'get=all' ); 53 foreach ( $tags as $tag ) { 54 $this->all_tags[] = $tag; 55 if ( is_term( $tag->slug, 'category' ) ) 56 $this->hybrids_ids[] = $tag->term_id; 57 } 58 } 59 60 function categories_tab() { 61 $this->populate_cats(); 62 $cat_num = count($this->all_categories); 63 64 echo '<br class="clear" />'; 65 66 if ( $cat_num > 0 ) { 67 screen_icon(); 68 echo '<h2>' . sprintf( __ngettext( 'Convert Category to Tag.', 'Convert Categories (%d) to Tags.', $cat_num ), $cat_num ) . '</h2>'; 69 echo '<div class="narrow">'; 70 echo '<p>' . __('Hey there. Here you can selectively convert existing categories to tags. To get started, check the categories you wish to be converted, then click the Convert button.') . '</p>'; 71 echo '<p>' . __('Keep in mind that if you convert a category with child categories, the children become top-level orphans.') . '</p></div>'; 72 73 $this->categories_form(); 74 } else { 75 echo '<p>'.__('You have no categories to convert!').'</p>'; 76 } 77 } 78 79 function categories_form() { ?> 80 81 <script type="text/javascript"> 82 /* <![CDATA[ */ 83 var checkflag = "false"; 84 function check_all_rows() { 85 field = document.catlist; 86 if ( 'false' == checkflag ) { 87 for ( i = 0; i < field.length; i++ ) { 88 if ( 'cats_to_convert[]' == field[i].name ) 89 field[i].checked = true; 90 } 91 checkflag = 'true'; 92 return '<?php _e('Uncheck All') ?>'; 93 } else { 94 for ( i = 0; i < field.length; i++ ) { 95 if ( 'cats_to_convert[]' == field[i].name ) 96 field[i].checked = false; 97 } 98 checkflag = 'false'; 99 return '<?php _e('Check All') ?>'; 100 } 101 } 102 /* ]]> */ 103 </script> 104 105 <form name="catlist" id="catlist" action="admin.php?import=wp-cat2tag&step=2" method="post"> 106 <p><input type="button" class="button-secondary" value="<?php _e('Check All'); ?>" onclick="this.value=check_all_rows()" /> 107 <?php wp_nonce_field('import-cat2tag'); ?></p> 108 <ul style="list-style:none"> 109 110 <?php $hier = _get_term_hierarchy('category'); 111 112 foreach ($this->all_categories as $category) { 113 $category = sanitize_term( $category, 'category', 'display' ); 114 115 if ( (int) $category->parent == 0 ) { ?> 116 117 <li><label><input type="checkbox" name="cats_to_convert[]" value="<?php echo intval($category->term_id); ?>" /> <?php echo $category->name . ' (' . $category->count . ')'; ?></label><?php 118 119 if ( in_array( intval($category->term_id), $this->hybrids_ids ) ) 120 echo ' <a href="#note"> * </a>'; 121 122 if ( isset($hier[$category->term_id]) ) 123 $this->_category_children($category, $hier); ?></li> 124 <?php } 125 } ?> 126 </ul> 127 128 <?php if ( ! empty($this->hybrids_ids) ) 129 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>'; ?> 130 131 <p class="submit"><input type="submit" name="submit" class="button" value="<?php _e('Convert Categories to Tags'); ?>" /></p> 132 </form> 133 134 <?php } 135 136 function tags_tab() { 137 $this->populate_tags(); 138 $tags_num = count($this->all_tags); 139 140 echo '<br class="clear" />'; 141 142 if ( $tags_num > 0 ) { 143 screen_icon(); 144 echo '<h2>' . sprintf( __ngettext( 'Convert Tag to Category.', 'Convert Tags (%d) to Categories.', $tags_num ), $tags_num ) . '</h2>'; 145 echo '<div class="narrow">'; 146 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>'; 147 echo '<p>' . __('The newly created categories will still be associated with the same posts.') . '</p></div>'; 148 149 $this->tags_form(); 150 } else { 151 echo '<p>'.__('You have no tags to convert!').'</p>'; 152 } 153 } 154 155 function tags_form() { ?> 156 157 <script type="text/javascript"> 158 /* <![CDATA[ */ 159 var checktags = "false"; 160 function check_all_tagrows() { 161 field = document.taglist; 162 if ( 'false' == checktags ) { 163 for ( i = 0; i < field.length; i++ ) { 164 if ( 'tags_to_convert[]' == field[i].name ) 165 field[i].checked = true; 166 } 167 checktags = 'true'; 168 return '<?php _e('Uncheck All') ?>'; 169 } else { 170 for ( i = 0; i < field.length; i++ ) { 171 if ( 'tags_to_convert[]' == field[i].name ) 172 field[i].checked = false; 173 } 174 checktags = 'false'; 175 return '<?php _e('Check All') ?>'; 176 } 177 } 178 /* ]]> */ 179 </script> 180 181 <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()" /> 183 <?php wp_nonce_field('import-cat2tag'); ?></p> 184 <ul style="list-style:none"> 185 186 <?php foreach ( $this->all_tags as $tag ) { ?> 187 <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> 188 189 <?php } ?> 190 </ul> 191 192 <?php if ( ! empty($this->hybrids_ids) ) 193 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>'; ?> 194 195 <p class="submit"><input type="submit" name="submit_tags" class="button" value="<?php _e('Convert Tags to Categories'); ?>" /></p> 196 </form> 197 198 <?php } 199 200 function _category_children($parent, $hier) { ?> 201 202 <ul style="list-style:none"> 203 <?php foreach ($hier[$parent->term_id] as $child_id) { 204 $child =& get_category($child_id); ?> 205 <li><label><input type="checkbox" name="cats_to_convert[]" value="<?php echo intval($child->term_id); ?>" /> <?php echo $child->name . ' (' . $child->count . ')'; ?></label><?php 206 207 if ( in_array( intval($child->term_id), $this->hybrids_ids ) ) 208 echo ' <a href="#note"> * </a>'; 209 210 if ( isset($hier[$child->term_id]) ) 211 $this->_category_children($child, $hier); ?></li> 212 <?php } ?> 213 </ul><?php 214 } 215 216 function _category_exists($cat_id) { 217 $cat_id = (int) $cat_id; 218 219 $maybe_exists = category_exists($cat_id); 220 221 if ( $maybe_exists ) { 222 return true; 223 } else { 224 return false; 225 } 226 } 227 228 function convert_categories() { 229 global $wpdb; 230 231 if ( (!isset($_POST['cats_to_convert']) || !is_array($_POST['cats_to_convert'])) && empty($this->categories_to_convert)) { ?> 232 <div class="narrow"> 233 <p><?php printf(__('Uh, oh. Something didn’t work. Please <a href="%s">try again</a>.'), 'admin.php?import=wp-cat2tag'); ?></p> 234 </div> 235 <?php return; 236 } 237 238 if ( empty($this->categories_to_convert) ) 239 $this->categories_to_convert = $_POST['cats_to_convert']; 240 241 $hier = _get_term_hierarchy('category'); 242 $hybrid_cats = $clear_parents = $parents = false; 243 $clean_term_cache = $clean_cat_cache = array(); 244 $default_cat = get_option('default_category'); 245 246 echo '<ul>'; 247 248 foreach ( (array) $this->categories_to_convert as $cat_id) { 249 $cat_id = (int) $cat_id; 250 251 if ( ! $this->_category_exists($cat_id) ) { 252 echo '<li>' . sprintf( __('Category %s doesn\'t exist!'), $cat_id ) . "</li>\n"; 253 } else { 254 $category =& get_category($cat_id); 255 echo '<li>' . sprintf(__('Converting category <strong>%s</strong> ... '), $category->name); 256 257 // If the category is the default, leave category in place and create tag. 258 if ( $default_cat == $category->term_id ) { 259 260 if ( ! ($id = is_term( $category->slug, 'post_tag' ) ) ) 261 $id = wp_insert_term($category->name, 'post_tag', array('slug' => $category->slug)); 262 263 $id = $id['term_taxonomy_id']; 264 $posts = get_objects_in_term($category->term_id, 'category'); 265 $term_order = 0; 266 267 foreach ( $posts as $post ) { 268 $values[] = $wpdb->prepare( "(%d, %d, %d)", $post, $id, $term_order); 269 clean_post_cache($post); 270 } 271 272 if ( $values ) { 273 $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)"); 274 275 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET count = %d WHERE term_id = %d AND taxonomy = 'post_tag'", $category->count, $category->term_id) ); 276 } 277 278 echo __('Converted successfully.') . "</li>\n"; 279 continue; 280 } 281 282 // if tag already exists, add it to all posts in the category 283 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) ) ) { 284 $objects_ids = get_objects_in_term($category->term_id, 'category'); 285 $tag_ttid = (int) $tag_ttid; 286 $term_order = 0; 287 288 foreach ( $objects_ids as $object_id ) 289 $values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $tag_ttid, $term_order); 290 291 if ( $values ) { 292 $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)"); 293 294 $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tag_ttid) ); 295 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET count = %d WHERE term_id = %d AND taxonomy = 'post_tag'", $count, $category->term_id) ); 296 } 297 echo __('Tag added to all posts in this category.') . " *</li>\n"; 298 299 $hybrid_cats = true; 300 $clean_term_cache[] = $category->term_id; 301 $clean_cat_cache[] = $category->term_id; 302 303 continue; 304 } 305 306 $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) ); 307 if ( $tt_ids ) { 308 $posts = $wpdb->get_col("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN (" . join(',', $tt_ids) . ") GROUP BY object_id"); 309 foreach ( (array) $posts as $post ) 310 clean_post_cache($post); 311 } 312 313 // Change the category to a tag. 314 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET taxonomy = 'post_tag' WHERE term_id = %d AND taxonomy = 'category'", $category->term_id) ); 315 316 // Set all parents to 0 (root-level) if their parent was the converted tag 317 $parents = $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET parent = 0 WHERE parent = %d AND taxonomy = 'category'", $category->term_id) ); 318 319 if ( $parents ) $clear_parents = true; 320 $clean_cat_cache[] = $category->term_id; 321 echo __('Converted successfully.') . "</li>\n"; 322 } 323 } 324 echo '</ul>'; 325 326 if ( ! empty($clean_term_cache) ) { 327 $clean_term_cache = array_unique(array_values($clean_term_cache)); 328 foreach ( $clean_term_cache as $id ) 329 wp_cache_delete($id, 'post_tag'); 330 } 331 332 if ( ! empty($clean_cat_cache) ) { 333 $clean_cat_cache = array_unique(array_values($clean_cat_cache)); 334 foreach ( $clean_cat_cache as $id ) 335 wp_cache_delete($id, 'category'); 336 } 337 338 if ( $clear_parents ) delete_option('category_children'); 339 340 if ( $hybrid_cats ) 341 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>'; 342 echo '<p>' . sprintf( __('We’re all done here, but you can always <a href="%s">convert more</a>.'), 'admin.php?import=wp-cat2tag' ) . '</p>'; 343 } 344 345 function convert_tags() { 346 global $wpdb; 347 348 if ( (!isset($_POST['tags_to_convert']) || !is_array($_POST['tags_to_convert'])) && empty($this->tags_to_convert)) { 349 echo '<div class="narrow">'; 350 echo '<p>' . sprintf(__('Uh, oh. Something didn’t work. Please <a href="%s">try again</a>.'), 'admin.php?import=wp-cat2tag&step=3') . '</p>'; 351 echo '</div>'; 352 return; 353 } 354 355 if ( empty($this->tags_to_convert) ) 356 $this->tags_to_convert = $_POST['tags_to_convert']; 357 358 $hybrid_tags = $clear_parents = false; 359 $clean_cat_cache = $clean_term_cache = array(); 360 $default_cat = get_option('default_category'); 361 echo '<ul>'; 362 363 foreach ( (array) $this->tags_to_convert as $tag_id) { 364 $tag_id = (int) $tag_id; 365 366 if ( $tag = get_term( $tag_id, 'post_tag' ) ) { 367 printf('<li>' . __('Converting tag <strong>%s</strong> ... '), $tag->name); 368 369 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) ) ) { 370 $objects_ids = get_objects_in_term($tag->term_id, 'post_tag'); 371 $cat_ttid = (int) $cat_ttid; 372 $term_order = 0; 373 374 foreach ( $objects_ids as $object_id ) { 375 $values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $cat_ttid, $term_order); 376 clean_post_cache($object_id); 377 } 378 379 if ( $values ) { 380 $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)"); 381 382 if ( $default_cat != $tag->term_id ) { 383 $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tag->term_id) ); 384 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET count = %d WHERE term_id = %d AND taxonomy = 'category'", $count, $tag->term_id) ); 385 } 386 } 387 388 $hybrid_tags = true; 389 $clean_term_cache[] = $tag->term_id; 390 $clean_cat_cache[] = $tag->term_id; 391 echo __('All posts were added to the category with the same name.') . " *</li>\n"; 392 393 continue; 394 } 395 396 // Change the tag to a category. 397 $parent = $wpdb->get_var( $wpdb->prepare("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'post_tag'", $tag->term_id) ); 398 if ( 0 == $parent || (0 < (int) $parent && $this->_category_exists($parent)) ) { 399 $reset_parent = ''; 400 $clear_parents = true; 401 } else 402 $reset_parent = ", parent = '0'"; 403 404 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET taxonomy = 'category' $reset_parent WHERE term_id = %d AND taxonomy = 'post_tag'", $tag->term_id) ); 405 406 $clean_term_cache[] = $tag->term_id; 407 $clean_cat_cache[] = $cat['term_id']; 408 echo __('Converted successfully.') . "</li>\n"; 409 410 } else { 411 printf( '<li>' . __('Tag #%s doesn\'t exist!') . "</li>\n", $tag_id ); 412 } 413 } 414 415 if ( ! empty($clean_term_cache) ) { 416 $clean_term_cache = array_unique(array_values($clean_term_cache)); 417 foreach ( $clean_term_cache as $id ) 418 wp_cache_delete($id, 'post_tag'); 419 } 420 421 if ( ! empty($clean_cat_cache) ) { 422 $clean_cat_cache = array_unique(array_values($clean_cat_cache)); 423 foreach ( $clean_cat_cache as $id ) 424 wp_cache_delete($id, 'category'); 425 } 426 427 if ( $clear_parents ) delete_option('category_children'); 428 429 echo '</ul>'; 430 if ( $hybrid_tags ) 431 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>'; 432 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>'; 433 } 434 435 function init() { 436 437 $step = (isset($_GET['step'])) ? (int) $_GET['step'] : 1; 438 439 $this->header(); 440 441 if ( current_user_can('manage_categories') ) { 442 443 switch ($step) { 444 case 1 : 445 $this->categories_tab(); 446 break; 447 448 case 2 : 449 check_admin_referer('import-cat2tag'); 450 $this->convert_categories(); 451 break; 452 453 case 3 : 454 $this->tags_tab(); 455 break; 456 457 case 4 : 458 check_admin_referer('import-cat2tag'); 459 $this->convert_tags(); 460 break; 461 } 462 } 463 464 $this->footer(); 465 } 466 467 function WP_Categories_to_Tags() { 468 // Do nothing. 469 } 470 } 471 472 $wp_cat2tag_importer = new WP_Categories_to_Tags(); 473 474 register_importer('wp-cat2tag', __('Categories and Tags Converter'), __('Convert existing categories to tags or tags to categories, selectively.'), array(&$wp_cat2tag_importer, 'init')); 475 476 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Mar 23 16:23:02 2009 | Cross-referenced by PHPXref 0.7 |