Ticket #6909: tag-to-cat.patch
File tag-to-cat.patch, 13.0 KB (added by , 17 years ago) |
---|
-
wp-admin/import/wp-cat2tag.php
3 3 class WP_Categories_to_Tags { 4 4 var $categories_to_convert = array(); 5 5 var $all_categories = array(); 6 var $tags_to_convert = array(); 7 var $all_tags = array(); 8 var $hybrids_ids = array(); 6 9 7 10 function header() { 8 11 echo '<div class="wrap">'; 9 echo '<h2>' . __('Convert Categories to Tags') . '</h2>'; 12 if ( ! current_user_can('manage_categories') ) { 13 echo '<div class="narrow">'; 14 echo '<p>' . __('Cheatin’ uh?') . '</p>'; 15 echo '</div>'; 16 } else { ?> 17 <div class="tablenav"><p style="margin:4px"><a style="display:inline;" class="button-secondary" href="admin.php?import=wp-cat2tag">Categories to Tags</a> 18 <a style="display:inline;" class="button-secondary" href="admin.php?import=wp-cat2tag&step=3">Tags to Categories</a></p></div> 19 <?php } 10 20 } 11 21 12 22 function footer() { 13 23 echo '</div>'; 14 24 } 15 25 16 function populate_ all_categories() {26 function populate_cats() { 17 27 global $wpdb; 18 28 19 29 $categories = get_categories('get=all'); 20 30 foreach ( $categories as $category ) { 21 if ( !tag_exists($wpdb->escape($category->name)) ) 22 $this->all_categories[] = $category; 31 $this->all_categories[] = $category; 32 if ( tag_exists( $wpdb->escape($category->name) ) ) 33 $this->hybrids_ids[] = $category->term_id; 23 34 } 24 35 } 25 36 26 function welcome() {27 $this->populate_all_categories();37 function populate_tags() { 38 global $wpdb; 28 39 29 echo '<div class="narrow">'; 40 $tags = get_terms( array('post_tag'), 'get=all' ); 41 foreach ( $tags as $tag ) { 42 $this->all_tags[] = $tag; 43 if ( $this->_category_exists($tag->term_id) ) 44 $this->hybrids_ids[] = $tag->term_id; 45 } 46 } 30 47 31 if (count($this->all_categories) > 0) { 48 function categories_tab() { 49 $this->populate_cats(); 50 $cat_num = count($this->all_categories); 51 52 echo '<br class="clear" />'; 53 54 if ( $cat_num > 0 ) { 55 echo '<h2>Convert Categories (' . $cat_num . ') to Tags.</h2>'; 56 echo '<div class="narrow">'; 32 57 echo '<p>' . __('Hey there. Here you can selectively converts existing categories to tags. To get started, check the categories you wish to be converted, then click the Convert button.') . '</p>'; 33 echo '<p>' . __('Keep in mind that if you convert a category with child categories, the children become top-level orphans.') . '</p> ';58 echo '<p>' . __('Keep in mind that if you convert a category with child categories, the children become top-level orphans.') . '</p></div>'; 34 59 35 60 $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>'; 36 63 } else { 37 64 echo '<p>'.__('You have no categories to convert!').'</p>'; 38 65 } 39 40 echo '</div>';41 66 } 42 67 43 function categories_form() { 44 ?> 68 function categories_form() { ?> 69 45 70 <script type="text/javascript"> 46 <!-- 71 /* <![CDATA[ */ 47 72 var checkflag = "false"; 48 73 function check_all_rows() { 49 field = document. formlist;74 field = document.catlist; 50 75 if ( 'false' == checkflag ) { 51 76 for ( i = 0; i < field.length; i++ ) { 52 77 if ( 'cats_to_convert[]' == field[i].name ) … … 63 88 return '<?php _e('Check All') ?>'; 64 89 } 65 90 } 91 /* ]]> */ 92 </script> 66 93 67 // --> 68 </script> 94 <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()"> 96 <?php wp_nonce_field('import-cat2tag'); ?></p> 97 <ul style="list-style:none"> 69 98 <?php 70 echo '<form name="formlist" id="formlist" action="admin.php?import=wp-cat2tag&step=2" method="post">71 <p><input type="button" class="button-secondary" value="' . __('Check All') . '"' . ' onClick="this.value=check_all_rows()"></p>';72 wp_nonce_field('import-cat2tag');73 echo '<ul style="list-style:none">';74 75 99 $hier = _get_term_hierarchy('category'); 76 100 77 101 foreach ($this->all_categories as $category) { 78 102 $category = sanitize_term( $category, 'category', 'display' ); 79 103 80 if ((int) $category->parent == 0) { 81 echo '<li><label><input type="checkbox" name="cats_to_convert[]" value="' . intval($category->term_id) . '" /> ' . $category->name . ' (' . $category->count . ')</label>'; 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 ?> 111 <li><label><input type="checkbox" name="cats_to_convert[]" value="<?php echo intval($category->term_id); ?>" /> <?php echo $category->name . ' (' . $category->count . ')'; ?></label><?php 82 112 83 if (isset($hier[$category->term_id])) { 84 $this->_category_children($category, $hier); 113 if ( isset($hier[$category->term_id]) ) 114 $this->_category_children($category, $hier); 115 ?></li> 116 <?php 85 117 } 118 } 119 } 120 ?> 121 </ul> 86 122 87 echo '</li>'; 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 ?> 128 <p class="submit"><input type="submit" name="submit" class="button" value="<?php _e('Convert Categories to Tags'); ?>" /></p> 129 </form> 130 131 <?php 132 } 133 134 function tags_tab() { 135 $this->populate_tags(); 136 $tags_num = count($this->all_tags); 137 138 echo '<br class="clear" />'; 139 140 if ( $tags_num > 0 ) { 141 echo '<h2>Convert Tags (' . $tags_num . ') to Categories.</h2>'; 142 echo '<div class="narrow">'; 143 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 echo '<p>' . __('The newly created categories will still be associated with the same posts.') . '</p></div>'; 145 146 147 $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 } 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 <?php 186 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 <?php 191 } 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 195 <?php 88 196 } 89 197 } 198 ?> 199 </ul> 90 200 91 echo '</ul>'; 201 <?php 202 if ( ! empty($this->hybrids_ids) ) 203 echo '<p>' . __('* This tag is also a category. It cannot be converted again.') . '</p>'; 204 ?> 92 205 93 echo '<p class="submit"><input type="submit" name="submit" class="button" value="' . __('Convert Tags') . '" /></p>'; 206 <p class="submit"><input type="submit" name="submit_tags" class="button" value="<?php _e('Convert Tags to Categories'); ?>" /></p> 207 </form> 94 208 95 echo '</form>'; 209 <?php 96 210 } 97 211 98 212 function _category_children($parent, $hier) { 99 echo '<ul style="list-style:none">'; 213 ?> 100 214 215 <ul style="list-style:none"> 216 <?php 101 217 foreach ($hier[$parent->term_id] as $child_id) { 102 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 103 228 104 echo '<li><label><input type="checkbox" name="cats_to_convert[]" value="' . intval($child->term_id) . '" /> ' . $child->name . ' (' . $child->count . ')</label>'; 105 106 if (isset($hier[$child->term_id])) { 229 if ( isset($hier[$child->term_id]) ) 107 230 $this->_category_children($child, $hier); 231 ?> </li> 232 <?php 108 233 } 109 110 echo '</li>';111 234 } 112 113 echo '</ul>'; 235 ?> 236 </ul> 237 <?php 114 238 } 115 239 116 240 function _category_exists($cat_id) { … … 125 249 } 126 250 } 127 251 128 function convert_ them() {252 function convert_categories() { 129 253 global $wpdb; 130 254 131 255 if ( (!isset($_POST['cats_to_convert']) || !is_array($_POST['cats_to_convert'])) && empty($this->categories_to_convert)) { … … 135 259 return; 136 260 } 137 261 138 139 262 if ( empty($this->categories_to_convert) ) 140 263 $this->categories_to_convert = $_POST['cats_to_convert']; 141 264 $hier = _get_term_hierarchy('category'); … … 199 322 echo '<p>' . sprintf( __('We’re all done here, but you can always <a href="%s">convert more</a>.'), 'admin.php?import=wp-cat2tag' ) . '</p>'; 200 323 } 201 324 325 function convert_tags() { 326 global $wpdb; 327 328 if ( (!isset($_POST['tags_to_convert']) || !is_array($_POST['tags_to_convert'])) && empty($this->tags_to_convert)) { 329 echo '<div class="narrow">'; 330 echo '<p>' . sprintf(__('Uh, oh. Something didn’t work. Please <a href="%s">try again</a>.'), 'admin.php?import=wp-cat2tag&step=3') . '</p>'; 331 echo '</div>'; 332 return; 333 } 334 335 if ( empty($this->categories_to_convert) ) 336 $this->tags_to_convert = $_POST['tags_to_convert']; 337 338 $clean_cache = array(); 339 echo '<ul>'; 340 341 foreach ( (array) $this->tags_to_convert as $tag_id) { 342 $tag_id = (int) $tag_id; 343 344 echo '<li>' . sprintf(__('Converting tag #%s ... '), $tag_id); 345 346 if ( ! is_term($tag_id, 'post_tag') ) { 347 _e('Tag doesn\'t exist!'); 348 } else { 349 350 if ( is_term($tag_id, 'category') ) { 351 _e('This Tag is already a Category.'); 352 echo '</li>'; 353 continue; 354 } 355 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 // 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)) ) 366 $reset_parent = ''; 367 else $reset_parent = ", parent = '0'"; 368 369 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET taxonomy = 'category' $reset_parent WHERE term_id = %d AND taxonomy = 'post_tag'", $tag_id) ); 370 371 // Clean the cache 372 $clean_cache[] = $tag_id; 373 374 _e('Converted successfully.'); 375 } 376 377 echo '</li>'; 378 } 379 380 clean_term_cache( $clean_cache, 'post_tag' ); 381 delete_option('category_children'); 382 383 echo '</ul>'; 384 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 } 386 202 387 function init() { 203 388 204 389 $step = (isset($_GET['step'])) ? (int) $_GET['step'] : 1; 205 390 206 391 $this->header(); 207 392 208 if (!current_user_can('manage_categories')) { 209 echo '<div class="narrow">'; 210 echo '<p>' . __('Cheatin’ uh?') . '</p>'; 211 echo '</div>'; 212 } else { 213 if ( $step > 1 ) 214 check_admin_referer('import-cat2tag'); 393 if ( current_user_can('manage_categories') ) { 215 394 216 395 switch ($step) { 217 396 case 1 : 218 $this-> welcome();397 $this->categories_tab(); 219 398 break; 220 399 221 400 case 2 : 222 $this->convert_them(); 401 check_admin_referer('import-cat2tag'); 402 $this->convert_categories(); 223 403 break; 404 405 case 3 : 406 $this->tags_tab(); 407 break; 408 409 case 4 : 410 check_admin_referer('import-cat2tag'); 411 $this->convert_tags(); 412 break; 224 413 } 225 414 } 226 415 … … 234 423 235 424 $wp_cat2tag_importer = new WP_Categories_to_Tags(); 236 425 237 register_importer('wp-cat2tag', __('Categories to Tags Converter'), __('Convert existing categories to tags, selectively.'), array(&$wp_cat2tag_importer, 'init'));426 register_importer('wp-cat2tag', __('Categories and Tags Converter'), __('Convert existing categories to tags or tags to categories, selectively.'), array(&$wp_cat2tag_importer, 'init')); 238 427 239 428 ?> 429 No newline at end of file