+ function convert_tags() {
+ global $wpdb;
+
+ if ( (!isset($_POST['tags_to_convert']) || !is_array($_POST['tags_to_convert'])) && empty($this->tags_to_convert)) {
+ echo '<div class="narrow">';
+ echo '<p>' . sprintf(__('Uh, oh. Something didn’t work. Please <a href="%s">try again</a>.'), 'admin.php?import=wp-cat2tag&step=3') . '</p>';
+ echo '</div>';
+ return;
+ }
+
+ if ( empty($this->tags_to_convert) )
+ $this->tags_to_convert = $_POST['tags_to_convert'];
+
+ $hybrid_tags = $clear_parents = false;
+ $clean_cat_cache = $clean_term_cache = array();
+ $default_cat = get_option('default_category');
+ echo '<ul>';
+
+ foreach ( (array) $this->tags_to_convert as $tag_id) {
+ $tag_id = (int) $tag_id;
+
+ if ( $tag = get_term( $tag_id, 'post_tag' ) ) {
+ printf('<li>' . __('Converting tag <strong>%s</strong> ... '), $tag->name);
+
+ 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) ) ) {
+ $objects_ids = get_objects_in_term($tag->term_id, 'post_tag');
+ $cat_ttid = (int) $cat_ttid;
+ $term_order = 0;
+
+ foreach ( $objects_ids as $object_id ) {
+ $values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $cat_ttid, $term_order);
+ clean_post_cache($object_id);
+ }
+
+ if ( $values ) {
+ $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)");
+
+ if ( $default_cat != $tag->term_id ) {
+ $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tag->term_id) );
+ $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET count = %d WHERE term_id = %d AND taxonomy = 'category'", $count, $tag->term_id) );
+ }
+ }
+
+ $hybrid_tags = true;
+ $clean_term_cache[] = $tag->term_id;
+ $clean_cat_cache[] = $tag->term_id;
+ echo __('All posts were added to the category with the same name.') . " *</li>\n";
+
+ continue;
+ }
+
+ // Change the tag to a category.
+ $parent = $wpdb->get_var( $wpdb->prepare("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'post_tag'", $tag->term_id) );
+ if ( 0 == $parent || (0 < (int) $parent && $this->_category_exists($parent)) ) {
+ $reset_parent = '';
+ $clear_parents = true;
+ } else
+ $reset_parent = ", parent = '0'";
+
+ $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET taxonomy = 'category' $reset_parent WHERE term_id = %d AND taxonomy = 'post_tag'", $tag->term_id) );
+
+ $clean_term_cache[] = $tag->term_id;
+ $clean_cat_cache[] = $cat['term_id'];
+ echo __('Converted successfully.') . "</li>\n";
+
+ } else {
+ printf( '<li>' . __('Tag #%s doesn\'t exist!') . "</li>\n", $tag_id );
+ }
+ }
+
+ if ( ! empty($clean_term_cache) ) {
+ $clean_term_cache = array_unique(array_values($clean_term_cache));
+ foreach ( $clean_term_cache as $id )
+ wp_cache_delete($id, 'post_tag');
+ }
+
+ if ( ! empty($clean_cat_cache) ) {
+ $clean_cat_cache = array_unique(array_values($clean_cat_cache));
+ foreach ( $clean_cat_cache as $id )
+ wp_cache_delete($id, 'category');
+ }
+
+ if ( $clear_parents ) delete_option('category_children');
+
+ echo '</ul>';
+ if ( $hybrid_tags )
+ 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>';
+ 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>';
+ }
+