-
+ 8EFAFE653749909462BBDCF77F3A4CED3DD6AED05E15BBD4757603E8F74D393F0CB5DD74E4867C3360ED38BB44458836C0A692E8D5E98DE8A2FF44EFADCF0069
mp-wp/wp-admin/import/wp-cat2tag.php
(0 . 0)(1 . 476)
22162 <?php
22163 /**
22164 * WordPress Categories to Tags Converter.
22165 *
22166 * @package WordPress
22167 * @subpackage Importer
22168 */
22169
22170 /**
22171 * WordPress categories to tags converter class.
22172 *
22173 * Will convert WordPress categories to tags, removing the category after the
22174 * process is complete and updating all posts to switch to the tag.
22175 *
22176 * @since unknown
22177 */
22178 class WP_Categories_to_Tags {
22179 var $categories_to_convert = array();
22180 var $all_categories = array();
22181 var $tags_to_convert = array();
22182 var $all_tags = array();
22183 var $hybrids_ids = array();
22184
22185 function header() {
22186 echo '<div class="wrap">';
22187 if ( ! current_user_can('manage_categories') ) {
22188 echo '<div class="narrow">';
22189 echo '<p>' . __('Cheatin’ uh?') . '</p>';
22190 echo '</div>';
22191 } else { ?>
22192 <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>
22193 <a style="display:inline;" class="button-secondary" href="admin.php?import=wp-cat2tag&step=3"><?php _e( "Tags to Categories" ); ?></a></p></div>
22194 <?php }
22195 }
22196
22197 function footer() {
22198 echo '</div>';
22199 }
22200
22201 function populate_cats() {
22202
22203 $categories = get_categories('get=all');
22204 foreach ( $categories as $category ) {
22205 $this->all_categories[] = $category;
22206 if ( is_term( $category->slug, 'post_tag' ) )
22207 $this->hybrids_ids[] = $category->term_id;
22208 }
22209 }
22210
22211 function populate_tags() {
22212
22213 $tags = get_terms( array('post_tag'), 'get=all' );
22214 foreach ( $tags as $tag ) {
22215 $this->all_tags[] = $tag;
22216 if ( is_term( $tag->slug, 'category' ) )
22217 $this->hybrids_ids[] = $tag->term_id;
22218 }
22219 }
22220
22221 function categories_tab() {
22222 $this->populate_cats();
22223 $cat_num = count($this->all_categories);
22224
22225 echo '<br class="clear" />';
22226
22227 if ( $cat_num > 0 ) {
22228 screen_icon();
22229 echo '<h2>' . sprintf( __ngettext( 'Convert Category to Tag.', 'Convert Categories (%d) to Tags.', $cat_num ), $cat_num ) . '</h2>';
22230 echo '<div class="narrow">';
22231 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>';
22232 echo '<p>' . __('Keep in mind that if you convert a category with child categories, the children become top-level orphans.') . '</p></div>';
22233
22234 $this->categories_form();
22235 } else {
22236 echo '<p>'.__('You have no categories to convert!').'</p>';
22237 }
22238 }
22239
22240 function categories_form() { ?>
22241
22242 <script type="text/javascript">
22243 /* <![CDATA[ */
22244 var checkflag = "false";
22245 function check_all_rows() {
22246 field = document.catlist;
22247 if ( 'false' == checkflag ) {
22248 for ( i = 0; i < field.length; i++ ) {
22249 if ( 'cats_to_convert[]' == field[i].name )
22250 field[i].checked = true;
22251 }
22252 checkflag = 'true';
22253 return '<?php _e('Uncheck All') ?>';
22254 } else {
22255 for ( i = 0; i < field.length; i++ ) {
22256 if ( 'cats_to_convert[]' == field[i].name )
22257 field[i].checked = false;
22258 }
22259 checkflag = 'false';
22260 return '<?php _e('Check All') ?>';
22261 }
22262 }
22263 /* ]]> */
22264 </script>
22265
22266 <form name="catlist" id="catlist" action="admin.php?import=wp-cat2tag&step=2" method="post">
22267 <p><input type="button" class="button-secondary" value="<?php _e('Check All'); ?>" onclick="this.value=check_all_rows()" />
22268 <?php wp_nonce_field('import-cat2tag'); ?></p>
22269 <ul style="list-style:none">
22270
22271 <?php $hier = _get_term_hierarchy('category');
22272
22273 foreach ($this->all_categories as $category) {
22274 $category = sanitize_term( $category, 'category', 'display' );
22275
22276 if ( (int) $category->parent == 0 ) { ?>
22277
22278 <li><label><input type="checkbox" name="cats_to_convert[]" value="<?php echo intval($category->term_id); ?>" /> <?php echo $category->name . ' (' . $category->count . ')'; ?></label><?php
22279
22280 if ( in_array( intval($category->term_id), $this->hybrids_ids ) )
22281 echo ' <a href="#note"> * </a>';
22282
22283 if ( isset($hier[$category->term_id]) )
22284 $this->_category_children($category, $hier); ?></li>
22285 <?php }
22286 } ?>
22287 </ul>
22288
22289 <?php if ( ! empty($this->hybrids_ids) )
22290 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>'; ?>
22291
22292 <p class="submit"><input type="submit" name="submit" class="button" value="<?php _e('Convert Categories to Tags'); ?>" /></p>
22293 </form>
22294
22295 <?php }
22296
22297 function tags_tab() {
22298 $this->populate_tags();
22299 $tags_num = count($this->all_tags);
22300
22301 echo '<br class="clear" />';
22302
22303 if ( $tags_num > 0 ) {
22304 screen_icon();
22305 echo '<h2>' . sprintf( __ngettext( 'Convert Tag to Category.', 'Convert Tags (%d) to Categories.', $tags_num ), $tags_num ) . '</h2>';
22306 echo '<div class="narrow">';
22307 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>';
22308 echo '<p>' . __('The newly created categories will still be associated with the same posts.') . '</p></div>';
22309
22310 $this->tags_form();
22311 } else {
22312 echo '<p>'.__('You have no tags to convert!').'</p>';
22313 }
22314 }
22315
22316 function tags_form() { ?>
22317
22318 <script type="text/javascript">
22319 /* <![CDATA[ */
22320 var checktags = "false";
22321 function check_all_tagrows() {
22322 field = document.taglist;
22323 if ( 'false' == checktags ) {
22324 for ( i = 0; i < field.length; i++ ) {
22325 if ( 'tags_to_convert[]' == field[i].name )
22326 field[i].checked = true;
22327 }
22328 checktags = 'true';
22329 return '<?php _e('Uncheck All') ?>';
22330 } else {
22331 for ( i = 0; i < field.length; i++ ) {
22332 if ( 'tags_to_convert[]' == field[i].name )
22333 field[i].checked = false;
22334 }
22335 checktags = 'false';
22336 return '<?php _e('Check All') ?>';
22337 }
22338 }
22339 /* ]]> */
22340 </script>
22341
22342 <form name="taglist" id="taglist" action="admin.php?import=wp-cat2tag&step=4" method="post">
22343 <p><input type="button" class="button-secondary" value="<?php _e('Check All'); ?>" onclick="this.value=check_all_tagrows()" />
22344 <?php wp_nonce_field('import-cat2tag'); ?></p>
22345 <ul style="list-style:none">
22346
22347 <?php foreach ( $this->all_tags as $tag ) { ?>
22348 <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>
22349
22350 <?php } ?>
22351 </ul>
22352
22353 <?php if ( ! empty($this->hybrids_ids) )
22354 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>'; ?>
22355
22356 <p class="submit"><input type="submit" name="submit_tags" class="button" value="<?php _e('Convert Tags to Categories'); ?>" /></p>
22357 </form>
22358
22359 <?php }
22360
22361 function _category_children($parent, $hier) { ?>
22362
22363 <ul style="list-style:none">
22364 <?php foreach ($hier[$parent->term_id] as $child_id) {
22365 $child =& get_category($child_id); ?>
22366 <li><label><input type="checkbox" name="cats_to_convert[]" value="<?php echo intval($child->term_id); ?>" /> <?php echo $child->name . ' (' . $child->count . ')'; ?></label><?php
22367
22368 if ( in_array( intval($child->term_id), $this->hybrids_ids ) )
22369 echo ' <a href="#note"> * </a>';
22370
22371 if ( isset($hier[$child->term_id]) )
22372 $this->_category_children($child, $hier); ?></li>
22373 <?php } ?>
22374 </ul><?php
22375 }
22376
22377 function _category_exists($cat_id) {
22378 $cat_id = (int) $cat_id;
22379
22380 $maybe_exists = category_exists($cat_id);
22381
22382 if ( $maybe_exists ) {
22383 return true;
22384 } else {
22385 return false;
22386 }
22387 }
22388
22389 function convert_categories() {
22390 global $wpdb;
22391
22392 if ( (!isset($_POST['cats_to_convert']) || !is_array($_POST['cats_to_convert'])) && empty($this->categories_to_convert)) { ?>
22393 <div class="narrow">
22394 <p><?php printf(__('Uh, oh. Something didn’t work. Please <a href="%s">try again</a>.'), 'admin.php?import=wp-cat2tag'); ?></p>
22395 </div>
22396 <?php return;
22397 }
22398
22399 if ( empty($this->categories_to_convert) )
22400 $this->categories_to_convert = $_POST['cats_to_convert'];
22401
22402 $hier = _get_term_hierarchy('category');
22403 $hybrid_cats = $clear_parents = $parents = false;
22404 $clean_term_cache = $clean_cat_cache = array();
22405 $default_cat = get_option('default_category');
22406
22407 echo '<ul>';
22408
22409 foreach ( (array) $this->categories_to_convert as $cat_id) {
22410 $cat_id = (int) $cat_id;
22411
22412 if ( ! $this->_category_exists($cat_id) ) {
22413 echo '<li>' . sprintf( __('Category %s doesn\'t exist!'), $cat_id ) . "</li>\n";
22414 } else {
22415 $category =& get_category($cat_id);
22416 echo '<li>' . sprintf(__('Converting category <strong>%s</strong> ... '), $category->name);
22417
22418 // If the category is the default, leave category in place and create tag.
22419 if ( $default_cat == $category->term_id ) {
22420
22421 if ( ! ($id = is_term( $category->slug, 'post_tag' ) ) )
22422 $id = wp_insert_term($category->name, 'post_tag', array('slug' => $category->slug));
22423
22424 $id = $id['term_taxonomy_id'];
22425 $posts = get_objects_in_term($category->term_id, 'category');
22426 $term_order = 0;
22427
22428 foreach ( $posts as $post ) {
22429 $values[] = $wpdb->prepare( "(%d, %d, %d)", $post, $id, $term_order);
22430 clean_post_cache($post);
22431 }
22432
22433 if ( $values ) {
22434 $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)");
22435
22436 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET count = %d WHERE term_id = %d AND taxonomy = 'post_tag'", $category->count, $category->term_id) );
22437 }
22438
22439 echo __('Converted successfully.') . "</li>\n";
22440 continue;
22441 }
22442
22443 // if tag already exists, add it to all posts in the category
22444 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) ) ) {
22445 $objects_ids = get_objects_in_term($category->term_id, 'category');
22446 $tag_ttid = (int) $tag_ttid;
22447 $term_order = 0;
22448
22449 foreach ( $objects_ids as $object_id )
22450 $values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $tag_ttid, $term_order);
22451
22452 if ( $values ) {
22453 $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)");
22454
22455 $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tag_ttid) );
22456 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET count = %d WHERE term_id = %d AND taxonomy = 'post_tag'", $count, $category->term_id) );
22457 }
22458 echo __('Tag added to all posts in this category.') . " *</li>\n";
22459
22460 $hybrid_cats = true;
22461 $clean_term_cache[] = $category->term_id;
22462 $clean_cat_cache[] = $category->term_id;
22463
22464 continue;
22465 }
22466
22467 $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) );
22468 if ( $tt_ids ) {
22469 $posts = $wpdb->get_col("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN (" . join(',', $tt_ids) . ") GROUP BY object_id");
22470 foreach ( (array) $posts as $post )
22471 clean_post_cache($post);
22472 }
22473
22474 // Change the category to a tag.
22475 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET taxonomy = 'post_tag' WHERE term_id = %d AND taxonomy = 'category'", $category->term_id) );
22476
22477 // Set all parents to 0 (root-level) if their parent was the converted tag
22478 $parents = $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET parent = 0 WHERE parent = %d AND taxonomy = 'category'", $category->term_id) );
22479
22480 if ( $parents ) $clear_parents = true;
22481 $clean_cat_cache[] = $category->term_id;
22482 echo __('Converted successfully.') . "</li>\n";
22483 }
22484 }
22485 echo '</ul>';
22486
22487 if ( ! empty($clean_term_cache) ) {
22488 $clean_term_cache = array_unique(array_values($clean_term_cache));
22489 foreach ( $clean_term_cache as $id )
22490 wp_cache_delete($id, 'post_tag');
22491 }
22492
22493 if ( ! empty($clean_cat_cache) ) {
22494 $clean_cat_cache = array_unique(array_values($clean_cat_cache));
22495 foreach ( $clean_cat_cache as $id )
22496 wp_cache_delete($id, 'category');
22497 }
22498
22499 if ( $clear_parents ) delete_option('category_children');
22500
22501 if ( $hybrid_cats )
22502 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>';
22503 echo '<p>' . sprintf( __('We’re all done here, but you can always <a href="%s">convert more</a>.'), 'admin.php?import=wp-cat2tag' ) . '</p>';
22504 }
22505
22506 function convert_tags() {
22507 global $wpdb;
22508
22509 if ( (!isset($_POST['tags_to_convert']) || !is_array($_POST['tags_to_convert'])) && empty($this->tags_to_convert)) {
22510 echo '<div class="narrow">';
22511 echo '<p>' . sprintf(__('Uh, oh. Something didn’t work. Please <a href="%s">try again</a>.'), 'admin.php?import=wp-cat2tag&step=3') . '</p>';
22512 echo '</div>';
22513 return;
22514 }
22515
22516 if ( empty($this->tags_to_convert) )
22517 $this->tags_to_convert = $_POST['tags_to_convert'];
22518
22519 $hybrid_tags = $clear_parents = false;
22520 $clean_cat_cache = $clean_term_cache = array();
22521 $default_cat = get_option('default_category');
22522 echo '<ul>';
22523
22524 foreach ( (array) $this->tags_to_convert as $tag_id) {
22525 $tag_id = (int) $tag_id;
22526
22527 if ( $tag = get_term( $tag_id, 'post_tag' ) ) {
22528 printf('<li>' . __('Converting tag <strong>%s</strong> ... '), $tag->name);
22529
22530 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) ) ) {
22531 $objects_ids = get_objects_in_term($tag->term_id, 'post_tag');
22532 $cat_ttid = (int) $cat_ttid;
22533 $term_order = 0;
22534
22535 foreach ( $objects_ids as $object_id ) {
22536 $values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $cat_ttid, $term_order);
22537 clean_post_cache($object_id);
22538 }
22539
22540 if ( $values ) {
22541 $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)");
22542
22543 if ( $default_cat != $tag->term_id ) {
22544 $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tag->term_id) );
22545 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET count = %d WHERE term_id = %d AND taxonomy = 'category'", $count, $tag->term_id) );
22546 }
22547 }
22548
22549 $hybrid_tags = true;
22550 $clean_term_cache[] = $tag->term_id;
22551 $clean_cat_cache[] = $tag->term_id;
22552 echo __('All posts were added to the category with the same name.') . " *</li>\n";
22553
22554 continue;
22555 }
22556
22557 // Change the tag to a category.
22558 $parent = $wpdb->get_var( $wpdb->prepare("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'post_tag'", $tag->term_id) );
22559 if ( 0 == $parent || (0 < (int) $parent && $this->_category_exists($parent)) ) {
22560 $reset_parent = '';
22561 $clear_parents = true;
22562 } else
22563 $reset_parent = ", parent = '0'";
22564
22565 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET taxonomy = 'category' $reset_parent WHERE term_id = %d AND taxonomy = 'post_tag'", $tag->term_id) );
22566
22567 $clean_term_cache[] = $tag->term_id;
22568 $clean_cat_cache[] = $cat['term_id'];
22569 echo __('Converted successfully.') . "</li>\n";
22570
22571 } else {
22572 printf( '<li>' . __('Tag #%s doesn\'t exist!') . "</li>\n", $tag_id );
22573 }
22574 }
22575
22576 if ( ! empty($clean_term_cache) ) {
22577 $clean_term_cache = array_unique(array_values($clean_term_cache));
22578 foreach ( $clean_term_cache as $id )
22579 wp_cache_delete($id, 'post_tag');
22580 }
22581
22582 if ( ! empty($clean_cat_cache) ) {
22583 $clean_cat_cache = array_unique(array_values($clean_cat_cache));
22584 foreach ( $clean_cat_cache as $id )
22585 wp_cache_delete($id, 'category');
22586 }
22587
22588 if ( $clear_parents ) delete_option('category_children');
22589
22590 echo '</ul>';
22591 if ( $hybrid_tags )
22592 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>';
22593 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>';
22594 }
22595
22596 function init() {
22597
22598 $step = (isset($_GET['step'])) ? (int) $_GET['step'] : 1;
22599
22600 $this->header();
22601
22602 if ( current_user_can('manage_categories') ) {
22603
22604 switch ($step) {
22605 case 1 :
22606 $this->categories_tab();
22607 break;
22608
22609 case 2 :
22610 check_admin_referer('import-cat2tag');
22611 $this->convert_categories();
22612 break;
22613
22614 case 3 :
22615 $this->tags_tab();
22616 break;
22617
22618 case 4 :
22619 check_admin_referer('import-cat2tag');
22620 $this->convert_tags();
22621 break;
22622 }
22623 }
22624
22625 $this->footer();
22626 }
22627
22628 function WP_Categories_to_Tags() {
22629 // Do nothing.
22630 }
22631 }
22632
22633 $wp_cat2tag_importer = new WP_Categories_to_Tags();
22634
22635 register_importer('wp-cat2tag', __('Categories and Tags Converter'), __('Convert existing categories to tags or tags to categories, selectively.'), array(&$wp_cat2tag_importer, 'init'));
22636
22637 ?>