3 class WP_Categories_to_Tags {
4 var $categories_to_convert = array();
5 var $all_categories = array();
6 var $tags_to_convert = array();
7 var $all_tags = array();
8 var $hybrids_ids = array();
11 echo '<div class="wrap">';
12 if ( ! current_user_can('manage_categories') ) {
13 echo '<div class="narrow">';
14 echo '<p>' . __('Cheatin’ uh?') . '</p>';
17 <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>
18 <a style="display:inline;" class="button-secondary" href="admin.php?import=wp-cat2tag&step=3"><?php _e( "Tags to Categories" ); ?></a></p></div>
26 function populate_cats() {
28 $categories = get_categories('get=all');
29 foreach ( $categories as $category ) {
30 $this->all_categories[] = $category;
31 if ( is_term( $category->slug, 'post_tag' ) )
32 $this->hybrids_ids[] = $category->term_id;
36 function populate_tags() {
38 $tags = get_terms( array('post_tag'), 'get=all' );
39 foreach ( $tags as $tag ) {
40 $this->all_tags[] = $tag;
41 if ( is_term( $tag->slug, 'category' ) )
42 $this->hybrids_ids[] = $tag->term_id;
46 function categories_tab() {
47 $this->populate_cats();
48 $cat_num = count($this->all_categories);
50 echo '<br class="clear" />';
53 echo '<h2>' . sprintf( __ngettext( 'Convert Category to Tag.', 'Convert Categories (%d) to Tags.', $cat_num ), $cat_num ) . '</h2>';
54 echo '<div class="narrow">';
55 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>';
56 echo '<p>' . __('Keep in mind that if you convert a category with child categories, the children become top-level orphans.') . '</p></div>';
58 $this->categories_form();
60 echo '<p>'.__('You have no categories to convert!').'</p>';
64 function categories_form() { ?>
66 <script type="text/javascript">
68 var checkflag = "false";
69 function check_all_rows() {
70 field = document.catlist;
71 if ( 'false' == checkflag ) {
72 for ( i = 0; i < field.length; i++ ) {
73 if ( 'cats_to_convert[]' == field[i].name )
74 field[i].checked = true;
77 return '<?php _e('Uncheck All') ?>';
79 for ( i = 0; i < field.length; i++ ) {
80 if ( 'cats_to_convert[]' == field[i].name )
81 field[i].checked = false;
84 return '<?php _e('Check All') ?>';
90 <form name="catlist" id="catlist" action="admin.php?import=wp-cat2tag&step=2" method="post">
91 <p><input type="button" class="button-secondary" value="<?php _e('Check All'); ?>" onclick="this.value=check_all_rows()" />
92 <?php wp_nonce_field('import-cat2tag'); ?></p>
93 <ul style="list-style:none">
95 <?php $hier = _get_term_hierarchy('category');
97 foreach ($this->all_categories as $category) {
98 $category = sanitize_term( $category, 'category', 'display' );
100 if ( (int) $category->parent == 0 ) { ?>
102 <li><label><input type="checkbox" name="cats_to_convert[]" value="<?php echo intval($category->term_id); ?>" /> <?php echo $category->name . ' (' . $category->count . ')'; ?></label><?php
104 if ( in_array( intval($category->term_id), $this->hybrids_ids ) )
105 echo ' <a href="#note"> * </a>';
107 if ( isset($hier[$category->term_id]) )
108 $this->_category_children($category, $hier); ?></li>
113 <?php if ( ! empty($this->hybrids_ids) )
114 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>'; ?>
116 <p class="submit"><input type="submit" name="submit" class="button" value="<?php _e('Convert Categories to Tags'); ?>" /></p>
121 function tags_tab() {
122 $this->populate_tags();
123 $tags_num = count($this->all_tags);
125 echo '<br class="clear" />';
127 if ( $tags_num > 0 ) {
128 echo '<h2>' . sprintf( __ngettext( 'Convert Tag to Category.', 'Convert Tags (%d) to Categories.', $tags_num ), $tags_num ) . '</h2>';
129 echo '<div class="narrow">';
130 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>';
131 echo '<p>' . __('The newly created categories will still be associated with the same posts.') . '</p></div>';
135 echo '<p>'.__('You have no tags to convert!').'</p>';
139 function tags_form() { ?>
141 <script type="text/javascript">
143 var checktags = "false";
144 function check_all_tagrows() {
145 field = document.taglist;
146 if ( 'false' == checktags ) {
147 for ( i = 0; i < field.length; i++ ) {
148 if ( 'tags_to_convert[]' == field[i].name )
149 field[i].checked = true;
152 return '<?php _e('Uncheck All') ?>';
154 for ( i = 0; i < field.length; i++ ) {
155 if ( 'tags_to_convert[]' == field[i].name )
156 field[i].checked = false;
159 return '<?php _e('Check All') ?>';
165 <form name="taglist" id="taglist" action="admin.php?import=wp-cat2tag&step=4" method="post">
166 <p><input type="button" class="button-secondary" value="<?php _e('Check All'); ?>" onclick="this.value=check_all_tagrows()" />
167 <?php wp_nonce_field('import-cat2tag'); ?></p>
168 <ul style="list-style:none">
170 <?php foreach ( $this->all_tags as $tag ) { ?>
171 <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>
176 <?php if ( ! empty($this->hybrids_ids) )
177 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>'; ?>
179 <p class="submit"><input type="submit" name="submit_tags" class="button" value="<?php _e('Convert Tags to Categories'); ?>" /></p>
184 function _category_children($parent, $hier) { ?>
186 <ul style="list-style:none">
187 <?php foreach ($hier[$parent->term_id] as $child_id) {
188 $child =& get_category($child_id); ?>
189 <li><label><input type="checkbox" name="cats_to_convert[]" value="<?php echo intval($child->term_id); ?>" /> <?php echo $child->name . ' (' . $child->count . ')'; ?></label><?php
191 if ( in_array( intval($child->term_id), $this->hybrids_ids ) )
192 echo ' <a href="#note"> * </a>';
194 if ( isset($hier[$child->term_id]) )
195 $this->_category_children($child, $hier); ?></li>
200 function _category_exists($cat_id) {
201 $cat_id = (int) $cat_id;
203 $maybe_exists = category_exists($cat_id);
205 if ( $maybe_exists ) {
212 function convert_categories() {
215 if ( (!isset($_POST['cats_to_convert']) || !is_array($_POST['cats_to_convert'])) && empty($this->categories_to_convert)) { ?>
217 <p><?php printf(__('Uh, oh. Something didn’t work. Please <a href="%s">try again</a>.'), 'admin.php?import=wp-cat2tag'); ?></p>
222 if ( empty($this->categories_to_convert) )
223 $this->categories_to_convert = $_POST['cats_to_convert'];
225 $hier = _get_term_hierarchy('category');
226 $hybrid_cats = $clear_parents = $parents = false;
227 $clean_term_cache = $clean_cat_cache = array();
228 $default_cat = get_option('default_category');
232 foreach ( (array) $this->categories_to_convert as $cat_id) {
233 $cat_id = (int) $cat_id;
235 if ( ! $this->_category_exists($cat_id) ) {
236 echo '<li>' . sprintf( __('Category %s doesn\'t exist!'), $cat_id ) . "</li>\n";
238 $category =& get_category($cat_id);
239 echo '<li>' . sprintf(__('Converting category <strong>%s</strong> ... '), $category->name);
241 // If the category is the default, leave category in place and create tag.
242 if ( $default_cat == $category->term_id ) {
244 if ( ! ($id = is_term( $category->slug, 'post_tag' ) ) )
245 $id = wp_insert_term($category->name, 'post_tag', array('slug' => $category->slug));
247 $id = $id['term_taxonomy_id'];
248 $posts = get_objects_in_term($category->term_id, 'category');
251 foreach ( $posts as $post ) {
252 $values[] = $wpdb->prepare( "(%d, %d, %d)", $post, $id, $term_order);
253 clean_post_cache($post);
257 $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)");
259 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET count = %d WHERE term_id = %d AND taxonomy = 'post_tag'", $category->count, $category->term_id) );
262 echo __('Converted successfully.') . "</li>\n";
266 // if tag already exists, add it to all posts in the category
267 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) ) ) {
268 $objects_ids = get_objects_in_term($category->term_id, 'category');
269 $tag_ttid = (int) $tag_ttid;
272 foreach ( $objects_ids as $object_id )
273 $values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $tag_ttid, $term_order);
276 $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)");
278 $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tag_ttid) );
279 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET count = %d WHERE term_id = %d AND taxonomy = 'post_tag'", $count, $category->term_id) );
281 echo __('Tag added to all posts in this category.') . " *</li>\n";
284 $clean_term_cache[] = $category->term_id;
285 $clean_cat_cache[] = $category->term_id;
290 $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) );
292 $posts = $wpdb->get_col("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN (" . join(',', $tt_ids) . ") GROUP BY object_id");
293 foreach ( (array) $posts as $post )
294 clean_post_cache($post);
297 // Change the category to a tag.
298 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET taxonomy = 'post_tag' WHERE term_id = %d AND taxonomy = 'category'", $category->term_id) );
300 // Set all parents to 0 (root-level) if their parent was the converted tag
301 $parents = $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET parent = 0 WHERE parent = %d AND taxonomy = 'category'", $category->term_id) );
303 if ( $parents ) $clear_parents = true;
304 $clean_cat_cache[] = $category->term_id;
305 echo __('Converted successfully.') . "</li>\n";
310 if ( ! empty($clean_term_cache) ) {
311 $clean_term_cache = array_unique(array_values($clean_term_cache));
312 foreach ( $clean_term_cache as $id )
313 wp_cache_delete($id, 'post_tag');
316 if ( ! empty($clean_cat_cache) ) {
317 $clean_cat_cache = array_unique(array_values($clean_cat_cache));
318 foreach ( $clean_cat_cache as $id )
319 wp_cache_delete($id, 'category');
322 if ( $clear_parents ) delete_option('category_children');
325 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>';
326 echo '<p>' . sprintf( __('We’re all done here, but you can always <a href="%s">convert more</a>.'), 'admin.php?import=wp-cat2tag' ) . '</p>';
329 function convert_tags() {
332 if ( (!isset($_POST['tags_to_convert']) || !is_array($_POST['tags_to_convert'])) && empty($this->tags_to_convert)) {
333 echo '<div class="narrow">';
334 echo '<p>' . sprintf(__('Uh, oh. Something didn’t work. Please <a href="%s">try again</a>.'), 'admin.php?import=wp-cat2tag&step=3') . '</p>';
339 if ( empty($this->tags_to_convert) )
340 $this->tags_to_convert = $_POST['tags_to_convert'];
342 $hybrid_tags = $clear_parents = false;
343 $clean_cat_cache = $clean_term_cache = array();
344 $default_cat = get_option('default_category');
347 foreach ( (array) $this->tags_to_convert as $tag_id) {
348 $tag_id = (int) $tag_id;
350 if ( $tag = get_term( $tag_id, 'post_tag' ) ) {
351 printf('<li>' . __('Converting tag <strong>%s</strong> ... '), $tag->name);
353 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) ) ) {
354 $objects_ids = get_objects_in_term($tag->term_id, 'post_tag');
355 $cat_ttid = (int) $cat_ttid;
358 foreach ( $objects_ids as $object_id ) {
359 $values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $cat_ttid, $term_order);
360 clean_post_cache($object_id);
364 $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)");
366 if ( $default_cat != $tag->term_id ) {
367 $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tag->term_id) );
368 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET count = %d WHERE term_id = %d AND taxonomy = 'category'", $count, $tag->term_id) );
373 $clean_term_cache[] = $tag->term_id;
374 $clean_cat_cache[] = $tag->term_id;
375 echo __('All posts were added to the category with the same name.') . " *</li>\n";
380 // Change the tag to a category.
381 $parent = $wpdb->get_var( $wpdb->prepare("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'post_tag'", $tag->term_id) );
382 if ( 0 == $parent || (0 < (int) $parent && $this->_category_exists($parent)) ) {
384 $clear_parents = true;
386 $reset_parent = ", parent = '0'";
388 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET taxonomy = 'category' $reset_parent WHERE term_id = %d AND taxonomy = 'post_tag'", $tag->term_id) );
390 $clean_term_cache[] = $tag->term_id;
391 $clean_cat_cache[] = $cat['term_id'];
392 echo __('Converted successfully.') . "</li>\n";
395 printf( '<li>' . __('Tag #%s doesn\'t exist!') . "</li>\n", $tag_id );
399 if ( ! empty($clean_term_cache) ) {
400 $clean_term_cache = array_unique(array_values($clean_term_cache));
401 foreach ( $clean_term_cache as $id )
402 wp_cache_delete($id, 'post_tag');
405 if ( ! empty($clean_cat_cache) ) {
406 $clean_cat_cache = array_unique(array_values($clean_cat_cache));
407 foreach ( $clean_cat_cache as $id )
408 wp_cache_delete($id, 'category');
411 if ( $clear_parents ) delete_option('category_children');
415 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>';
416 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>';
421 $step = (isset($_GET['step'])) ? (int) $_GET['step'] : 1;
425 if ( current_user_can('manage_categories') ) {
429 $this->categories_tab();
433 check_admin_referer('import-cat2tag');
434 $this->convert_categories();
442 check_admin_referer('import-cat2tag');
443 $this->convert_tags();
451 function WP_Categories_to_Tags() {
456 $wp_cat2tag_importer = new WP_Categories_to_Tags();
458 register_importer('wp-cat2tag', __('Categories and Tags Converter'), __('Convert existing categories to tags or tags to categories, selectively.'), array(&$wp_cat2tag_importer, 'init'));