| [ Index ] |
PHP Cross Reference of Wordpress 2.9.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( _n( '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 esc_attr_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 esc_attr_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( _n( '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 convert 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 esc_attr_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 esc_attr($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 esc_attr_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 clean_term_cache($clean_term_cache, 'post_tag'); 329 } 330 331 if ( ! empty($clean_cat_cache) ) { 332 $clean_cat_cache = array_unique(array_values($clean_cat_cache)); 333 clean_term_cache($clean_cat_cache, 'category'); 334 } 335 336 if ( $clear_parents ) delete_option('category_children'); 337 338 if ( $hybrid_cats ) 339 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>'; 340 echo '<p>' . sprintf( __('We’re all done here, but you can always <a href="%s">convert more</a>.'), 'admin.php?import=wp-cat2tag' ) . '</p>'; 341 } 342 343 function convert_tags() { 344 global $wpdb; 345 346 if ( (!isset($_POST['tags_to_convert']) || !is_array($_POST['tags_to_convert'])) && empty($this->tags_to_convert)) { 347 echo '<div class="narrow">'; 348 echo '<p>' . sprintf(__('Uh, oh. Something didn’t work. Please <a href="%s">try again</a>.'), 'admin.php?import=wp-cat2tag&step=3') . '</p>'; 349 echo '</div>'; 350 return; 351 } 352 353 if ( empty($this->tags_to_convert) ) 354 $this->tags_to_convert = $_POST['tags_to_convert']; 355 356 $hybrid_tags = $clear_parents = false; 357 $clean_cat_cache = $clean_term_cache = array(); 358 $default_cat = get_option('default_category'); 359 echo '<ul>'; 360 361 foreach ( (array) $this->tags_to_convert as $tag_id) { 362 $tag_id = (int) $tag_id; 363 364 if ( $tag = get_term( $tag_id, 'post_tag' ) ) { 365 printf('<li>' . __('Converting tag <strong>%s</strong> ... '), $tag->name); 366 367 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) ) ) { 368 $objects_ids = get_objects_in_term($tag->term_id, 'post_tag'); 369 $cat_ttid = (int) $cat_ttid; 370 $term_order = 0; 371 372 foreach ( $objects_ids as $object_id ) { 373 $values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $cat_ttid, $term_order); 374 clean_post_cache($object_id); 375 } 376 377 if ( $values ) { 378 $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)"); 379 380 if ( $default_cat != $tag->term_id ) { 381 $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tag->term_id) ); 382 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET count = %d WHERE term_id = %d AND taxonomy = 'category'", $count, $tag->term_id) ); 383 } 384 } 385 386 $hybrid_tags = true; 387 $clean_term_cache[] = $tag->term_id; 388 $clean_cat_cache[] = $tag->term_id; 389 echo __('All posts were added to the category with the same name.') . " *</li>\n"; 390 391 continue; 392 } 393 394 // Change the tag to a category. 395 $parent = $wpdb->get_var( $wpdb->prepare("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'post_tag'", $tag->term_id) ); 396 if ( 0 == $parent || (0 < (int) $parent && $this->_category_exists($parent)) ) { 397 $reset_parent = ''; 398 $clear_parents = true; 399 } else 400 $reset_parent = ", parent = '0'"; 401 402 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET taxonomy = 'category' $reset_parent WHERE term_id = %d AND taxonomy = 'post_tag'", $tag->term_id) ); 403 404 $clean_term_cache[] = $tag->term_id; 405 $clean_cat_cache[] = $cat['term_id']; 406 echo __('Converted successfully.') . "</li>\n"; 407 408 } else { 409 printf( '<li>' . __('Tag #%s doesn’t exist!') . "</li>\n", $tag_id ); 410 } 411 } 412 413 if ( ! empty($clean_term_cache) ) { 414 $clean_term_cache = array_unique(array_values($clean_term_cache)); 415 clean_term_cache($clean_term_cache, 'post_tag'); 416 } 417 418 if ( ! empty($clean_cat_cache) ) { 419 $clean_cat_cache = array_unique(array_values($clean_cat_cache)); 420 clean_term_cache($clean_term_cache, 'category'); 421 } 422 423 if ( $clear_parents ) delete_option('category_children'); 424 425 echo '</ul>'; 426 if ( $hybrid_tags ) 427 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>'; 428 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>'; 429 } 430 431 function init() { 432 433 $step = (isset($_GET['step'])) ? (int) $_GET['step'] : 1; 434 435 $this->header(); 436 437 if ( current_user_can('manage_categories') ) { 438 439 switch ($step) { 440 case 1 : 441 $this->categories_tab(); 442 break; 443 444 case 2 : 445 check_admin_referer('import-cat2tag'); 446 $this->convert_categories(); 447 break; 448 449 case 3 : 450 $this->tags_tab(); 451 break; 452 453 case 4 : 454 check_admin_referer('import-cat2tag'); 455 $this->convert_tags(); 456 break; 457 } 458 } 459 460 $this->footer(); 461 } 462 463 function WP_Categories_to_Tags() { 464 // Do nothing. 465 } 466 } 467 468 $wp_cat2tag_importer = new WP_Categories_to_Tags(); 469 470 register_importer('wp-cat2tag', __('Categories and Tags Converter'), __('Convert existing categories to tags or tags to categories, selectively.'), array(&$wp_cat2tag_importer, 'init')); 471 472 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Fri Jan 8 00:19:48 2010 | Cross-referenced by PHPXref 0.7 |