diff --git a/wp-admin/import/wp-cat2tag.php b/wp-admin/import/wp-cat2tag.php
index 6b3aa30f19..3c57c9a1b7 100644
--- a/wp-admin/import/wp-cat2tag.php
+++ b/wp-admin/import/wp-cat2tag.php
@@ -24,23 +24,21 @@ class WP_Categories_to_Tags {
}
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) ) )
+ if ( is_term( $category->slug, 'post_tag' ) )
$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) )
+ if ( is_term( $tag->slug, 'category' ) )
$this->hybrids_ids[] = $tag->term_id;
}
}
@@ -58,8 +56,6 @@ class WP_Categories_to_Tags {
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!').'
';
}
@@ -92,44 +88,35 @@ function check_all_rows() {
-populate_tags();
@@ -142,11 +129,8 @@ function check_all_rows() {
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!').'
';
}
@@ -179,62 +163,38 @@ function check_all_tagrows() {
-
+ function _category_children($parent, $hier) { ?>
-_category_children($child, $hier); ?>
+
+ categories_to_convert)) {
- echo '';
- echo '
' . sprintf(__('Uh, oh. Something didn’t work. Please try again.'), 'admin.php?import=wp-cat2tag') . '
';
- echo '
';
- return;
+ if ( (!isset($_POST['cats_to_convert']) || !is_array($_POST['cats_to_convert'])) && empty($this->categories_to_convert)) { ?>
+
+
try again.'), 'admin.php?import=wp-cat2tag'); ?>
+
+categories_to_convert) )
$this->categories_to_convert = $_POST['cats_to_convert'];
+
$hier = _get_term_hierarchy('category');
+ $hybrid_cats = $clear_parents = $parents = false;
+ $clean_term_cache = $clean_cat_cache = array();
+ $default_cat = get_option('default_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!');
+ if ( ! $this->_category_exists($cat_id) ) {
+ echo '
- ' . sprintf( __('Category %s doesn\'t exist!'), $cat_id ) . "
\n";
} else {
$category =& get_category($cat_id);
+ echo '- ' . sprintf(__('Converting category %s ... '), $category->name);
+
+ // If the category is the default, leave category in place and create tag.
+ if ( $default_cat == $category->term_id ) {
+
+ if ( ! ($id = is_term( $category->slug, 'post_tag' ) ) )
+ $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');
+ $term_order = 0;
+
+ foreach ( $posts as $post ) {
+ $values[] = $wpdb->prepare( "(%d, %d, %d)", $post, $id, $term_order);
+ clean_post_cache($post);
+ }
+
+ 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)");
+
+ $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET count = %d WHERE term_id = %d AND taxonomy = 'post_tag'", $category->count, $category->term_id) );
+ }
+
+ echo __('Converted successfully.') . "
\n";
+ continue;
+ }
+
+ // if tag already exists, add it to all posts in the category
+ 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) ) ) {
+ $objects_ids = get_objects_in_term($category->term_id, 'category');
+ $tag_ttid = (int) $tag_ttid;
+ $term_order = 0;
+
+ foreach ( $objects_ids as $object_id )
+ $values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $tag_ttid, $term_order);
+
+ 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)");
+
+ $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tag_ttid) );
+ $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET count = %d WHERE term_id = %d AND taxonomy = 'post_tag'", $count, $category->term_id) );
+ }
+ echo __('Tag added to all posts in this category.') . " *\n";
+
+ $hybrid_cats = true;
+ $clean_term_cache[] = $category->term_id;
+ $clean_cat_cache[] = $category->term_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) );
+ $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);
- }
- } 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.');
+ // 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) );
+
+ // Set all parents to 0 (root-level) if their parent was the converted tag
+ $parents = $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET parent = 0 WHERE parent = %d AND taxonomy = 'category'", $category->term_id) );
+
+ if ( $parents ) $clear_parents = true;
+ $clean_cat_cache[] = $category->term_id;
+ echo __('Converted successfully.') . "\n";
}
+ }
+ echo '
';
- echo '';
+ 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');
}
- echo '';
+ 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');
+
+ if ( $hybrid_cats )
+ echo '' . 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 Manage Categories page.'), 'categories.php') . '
';
echo '' . sprintf( __('We’re all done here, but you can always convert more.'), 'admin.php?import=wp-cat2tag' ) . '
';
}
@@ -332,55 +336,83 @@ function check_all_tagrows() {
return;
}
- if ( empty($this->categories_to_convert) )
+ if ( empty($this->tags_to_convert) )
$this->tags_to_convert = $_POST['tags_to_convert'];
- $clean_cache = array();
+ $hybrid_tags = $clear_parents = false;
+ $clean_cat_cache = $clean_term_cache = array();
+ $default_cat = get_option('default_category');
echo '';
foreach ( (array) $this->tags_to_convert as $tag_id) {
$tag_id = (int) $tag_id;
- echo '- ' . sprintf(__('Converting tag #%s ... '), $tag_id);
+ if ( $tag = get_term( $tag_id, 'post_tag' ) ) {
+ printf('
- ' . __('Converting tag %s ... '), $tag->name);
- if ( ! is_term($tag_id, 'post_tag') ) {
- _e('Tag doesn\'t exist!');
- } else {
+ 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.') . " *
\n";
- 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)) )
+ $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 = '';
- else $reset_parent = ", parent = '0'";
+ $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_id) );
+ $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 the cache
- $clean_cache[] = $tag_id;
+ $clean_term_cache[] = $tag->term_id;
+ $clean_cat_cache[] = $cat['term_id'];
+ echo __('Converted successfully.') . "\n";
- _e('Converted successfully.');
+ } else {
+ printf( '- ' . __('Tag #%s doesn\'t exist!') . "
\n", $tag_id );
}
-
- echo '';
}
- clean_term_cache( $clean_cache, 'post_tag' );
- delete_option('category_children');
-
+ 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 '
';
+ if ( $hybrid_tags )
+ echo '' . 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 Manage Tags page.'), 'edit-tags.php') . '
';
echo '' . sprintf( __('We’re all done here, but you can always convert more.'), 'admin.php?import=wp-cat2tag&step=3' ) . '
';
}
@@ -401,11 +433,11 @@ function check_all_tagrows() {
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();
diff --git a/wp-admin/wp-admin.css b/wp-admin/wp-admin.css
index f61932bd1b..1cbdbcc455 100644
--- a/wp-admin/wp-admin.css
+++ b/wp-admin/wp-admin.css
@@ -600,7 +600,7 @@ a.view-comment-post-link {
border-top-style: solid;
}
-#wphead a, #dashmenu a, #adminmenu a, #submenu a, #sidemenu a {
+#wphead a, #dashmenu a, #adminmenu a, #submenu a, #sidemenu a, #taglist a, #catlist a {
text-decoration: none;
}