';
if ( ! current_user_can('manage_categories') ) {
echo '
';
echo '
' . __('Cheatin’ uh?') . '
';
echo '
';
} else { ?>
';
}
function populate_cats() {
global $wpdb;
$categories = get_categories('get=all');
foreach ( $categories as $category ) {
$this->all_categories[] = $category;
if ( tag_exists( $wpdb->escape($category->name) ) )
$this->hybrids_ids[] = $category->term_id;
}
}
function populate_tags() {
global $wpdb;
$tags = get_terms( array('post_tag'), 'get=all' );
foreach ( $tags as $tag ) {
$this->all_tags[] = $tag;
if ( $this->_category_exists($tag->term_id) )
$this->hybrids_ids[] = $tag->term_id;
}
}
function categories_tab() {
$this->populate_cats();
$cat_num = count($this->all_categories);
echo '
';
if ( $cat_num > 0 ) {
echo 'Convert Categories (' . $cat_num . ') to Tags.
';
echo '';
echo '
' . __('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.') . '
';
echo '
' . __('Keep in mind that if you convert a category with child categories, the children become top-level orphans.') . '
';
$this->categories_form();
} elseif ( $hyb_num > 0 ) {
echo '' . __('You have no categories that can be converted. However some of your categories are both a tag and a category.') . '
';
} else {
echo ''.__('You have no categories to convert!').'
';
}
}
function categories_form() { ?>
populate_tags();
$tags_num = count($this->all_tags);
echo '
';
if ( $tags_num > 0 ) {
echo 'Convert Tags (' . $tags_num . ') to Categories.
';
echo '';
echo '
' . __('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.') . '
';
echo '
' . __('The newly created categories will still be associated with the same posts.') . '
';
$this->tags_form();
} elseif ( $hyb_num > 0 ) {
echo '' . __('You have no tags that can be converted. However some of your tags are both a tag and a category.') . '
';
} else {
echo ''.__('You have no tags to convert!').'
';
}
}
function tags_form() { ?>
categories_to_convert)) {
echo '';
echo '
' . sprintf(__('Uh, oh. Something didn’t work. Please try again.'), 'admin.php?import=wp-cat2tag') . '
';
echo '
';
return;
}
if ( empty($this->categories_to_convert) )
$this->categories_to_convert = $_POST['cats_to_convert'];
$hier = _get_term_hierarchy('category');
echo '';
foreach ( (array) $this->categories_to_convert as $cat_id) {
$cat_id = (int) $cat_id;
echo '- ' . sprintf(__('Converting category #%s ... '), $cat_id);
if (!$this->_category_exists($cat_id)) {
_e('Category doesn\'t exist!');
} else {
$category =& get_category($cat_id);
if ( tag_exists($wpdb->escape($category->name)) ) {
_e('Category is already a tag.');
echo '
';
continue;
}
// If the category is the default, leave category in place and create tag.
if ( get_option('default_category') == $category->term_id ) {
$id = wp_insert_term($category->name, 'post_tag', array('slug' => $category->slug));
$id = $id['term_taxonomy_id'];
$posts = get_objects_in_term($category->term_id, 'category');
foreach ( $posts as $post ) {
if ( !$wpdb->get_var( $wpdb->prepare("SELECT object_id FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id = %d", $post, $id) ) )
$wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES (%d, %d)", $post, $id) );
clean_post_cache($post);
}
} else {
$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) );
if ( $tt_ids ) {
$posts = $wpdb->get_col("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN (" . join(',', $tt_ids) . ") GROUP BY object_id");
foreach ( (array) $posts as $post )
clean_post_cache($post);
}
// Change the category to a tag.
$wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET taxonomy = 'post_tag' WHERE term_id = %d AND taxonomy = 'category'", $category->term_id) );
$terms = $wpdb->get_col( $wpdb->prepare("SELECT term_id FROM $wpdb->term_taxonomy WHERE parent = %d AND taxonomy = 'category'", $category->term_id) );
foreach ( (array) $terms as $term )
clean_category_cache($term);
// Set all parents to 0 (root-level) if their parent was the converted tag
$wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET parent = 0 WHERE parent = %d AND taxonomy = 'category'", $category->term_id) );
}
// Clean the cache
clean_category_cache($category->term_id);
_e('Converted successfully.');
}
echo '';
}
echo '
';
echo '' . sprintf( __('We’re all done here, but you can always convert more.'), 'admin.php?import=wp-cat2tag' ) . '
';
}
function convert_tags() {
global $wpdb;
if ( (!isset($_POST['tags_to_convert']) || !is_array($_POST['tags_to_convert'])) && empty($this->tags_to_convert)) {
echo '';
echo '
' . sprintf(__('Uh, oh. Something didn’t work. Please try again.'), 'admin.php?import=wp-cat2tag&step=3') . '
';
echo '
';
return;
}
if ( empty($this->categories_to_convert) )
$this->tags_to_convert = $_POST['tags_to_convert'];
$clean_cache = array();
echo '';
foreach ( (array) $this->tags_to_convert as $tag_id) {
$tag_id = (int) $tag_id;
echo '- ' . sprintf(__('Converting tag #%s ... '), $tag_id);
if ( ! is_term($tag_id, 'post_tag') ) {
_e('Tag doesn\'t exist!');
} else {
if ( is_term($tag_id, 'category') ) {
_e('This Tag is already a Category.');
echo '
';
continue;
}
$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) );
if ( $tt_ids ) {
$posts = $wpdb->get_col("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN (" . join(',', $tt_ids) . ") GROUP BY object_id");
foreach ( (array) $posts as $post )
clean_post_cache($post);
}
// 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_id) );
if ( 0 == $parent || (0 < (int) $parent && $this->_category_exists($parent)) )
$reset_parent = '';
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_id) );
// Clean the cache
$clean_cache[] = $tag_id;
_e('Converted successfully.');
}
echo '';
}
clean_term_cache( $clean_cache, 'post_tag' );
delete_option('category_children');
echo '
';
echo '' . sprintf( __('We’re all done here, but you can always convert more.'), 'admin.php?import=wp-cat2tag&step=3' ) . '
';
}
function init() {
$step = (isset($_GET['step'])) ? (int) $_GET['step'] : 1;
$this->header();
if ( current_user_can('manage_categories') ) {
switch ($step) {
case 1 :
$this->categories_tab();
break;
case 2 :
check_admin_referer('import-cat2tag');
$this->convert_categories();
break;
case 3 :
$this->tags_tab();
break;
case 4 :
check_admin_referer('import-cat2tag');
$this->convert_tags();
break;
}
}
$this->footer();
}
function WP_Categories_to_Tags() {
// Do nothing.
}
}
$wp_cat2tag_importer = new WP_Categories_to_Tags();
register_importer('wp-cat2tag', __('Categories and Tags Converter'), __('Convert existing categories to tags or tags to categories, selectively.'), array(&$wp_cat2tag_importer, 'init'));
?>