diff --git a/wp-admin/import/wp-cat2tag.php b/wp-admin/import/wp-cat2tag.php
index fc54c6149b..6b3aa30f19 100644
--- a/wp-admin/import/wp-cat2tag.php
+++ b/wp-admin/import/wp-cat2tag.php
@@ -3,50 +3,75 @@
class WP_Categories_to_Tags {
var $categories_to_convert = array();
var $all_categories = array();
+ var $tags_to_convert = array();
+ var $all_tags = array();
+ var $hybrids_ids = array();
function header() {
echo '
';
- echo '
' . __('Convert Categories to Tags') . ' ';
+ if ( ! current_user_can('manage_categories') ) {
+ echo '
';
+ echo '
' . __('Cheatin’ uh?') . '
';
+ echo '
';
+ } else { ?>
+
+';
}
- function populate_all_categories() {
+ function populate_cats() {
global $wpdb;
$categories = get_categories('get=all');
foreach ( $categories as $category ) {
- if ( !tag_exists($wpdb->escape($category->name)) )
- $this->all_categories[] = $category;
+ $this->all_categories[] = $category;
+ if ( tag_exists( $wpdb->escape($category->name) ) )
+ $this->hybrids_ids[] = $category->term_id;
}
}
- function welcome() {
- $this->populate_all_categories();
+ function populate_tags() {
+ global $wpdb;
- echo '
';
+ $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;
+ }
+ }
- if (count($this->all_categories) > 0) {
+ 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.') . '
';
+ 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!').'
';
}
-
- echo '
';
}
- function categories_form() {
-?>
+ function categories_form() { ?>
+
-
-
';
- wp_nonce_field('import-cat2tag');
- echo '
- echo '
';
+';
+ function tags_tab() {
+ $this->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)) {
@@ -135,7 +259,6 @@ function check_all_rows() {
return;
}
-
if ( empty($this->categories_to_convert) )
$this->categories_to_convert = $_POST['cats_to_convert'];
$hier = _get_term_hierarchy('category');
@@ -199,27 +322,93 @@ function check_all_rows() {
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')) {
- echo '
';
- echo '
' . __('Cheatin’ uh?') . '
';
- echo '
';
- } else {
- if ( $step > 1 )
- check_admin_referer('import-cat2tag');
+ if ( current_user_can('manage_categories') ) {
switch ($step) {
case 1 :
- $this->welcome();
+ $this->categories_tab();
break;
case 2 :
- $this->convert_them();
+ 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;
}
}
@@ -234,6 +423,6 @@ function check_all_rows() {
$wp_cat2tag_importer = new WP_Categories_to_Tags();
-register_importer('wp-cat2tag', __('Categories to Tags Converter'), __('Convert existing categories to tags, selectively.'), array(&$wp_cat2tag_importer, 'init'));
+register_importer('wp-cat2tag', __('Categories and Tags Converter'), __('Convert existing categories to tags or tags to categories, selectively.'), array(&$wp_cat2tag_importer, 'init'));
?>
\ No newline at end of file