3 class WP_Categories_to_Tags {
4 var $categories_to_convert = array();
5 var $all_categories = array();
8 print '<div class="wrap">';
9 print '<h2>' . __('Convert Categories to Tags') . '</h2>';
16 function populate_all_categories() {
19 $categories = get_categories('get=all');
20 foreach ( $categories as $category ) {
21 if ( !tag_exists($wpdb->escape($category->name)) )
22 $this->all_categories[] = $category;
27 $this->populate_all_categories();
29 print '<div class="narrow">';
31 if (count($this->all_categories) > 0) {
32 print '<p>' . __('Howdy! This converter allows you to selectively convert existing categories to tags. To get started, check the checkboxes of the categories you wish to be converted, then click the Convert button.') . '</p>';
33 print '<p>' . __('Keep in mind that if you convert a category with child categories, those child categories get their parent setting removed, so they\'re in the root.') . '</p>';
35 $this->categories_form();
37 print '<p>'.__('You have no categories to convert!').'</p>';
43 function categories_form() {
44 print '<form action="admin.php?import=wp-cat2tag&step=2" method="post">';
45 wp_nonce_field('import-cat2tag');
46 print '<ul style="list-style:none">';
48 $hier = _get_term_hierarchy('category');
50 foreach ($this->all_categories as $category) {
51 $category = sanitize_term( $category, 'category', 'display' );
53 if ((int) $category->parent == 0) {
54 print '<li><label><input type="checkbox" name="cats_to_convert[]" value="' . intval($category->term_id) . '" /> ' . $category->name . ' (' . $category->count . ')</label>';
56 if (isset($hier[$category->term_id])) {
57 $this->_category_children($category, $hier);
66 print '<p class="submit"><input type="submit" name="submit" value="' . __('Convert »') . '" /></p>';
70 function _category_children($parent, $hier) {
71 print '<ul style="list-style:none">';
73 foreach ($hier[$parent->term_id] as $child_id) {
74 $child =& get_category($child_id);
76 print '<li><label><input type="checkbox" name="cats_to_convert[]" value="' . intval($child->term_id) . '" /> ' . $child->name . ' (' . $child->count . ')</label>';
78 if (isset($hier[$child->term_id])) {
79 $this->_category_children($child, $hier);
88 function _category_exists($cat_id) {
91 $cat_id = (int) $cat_id;
93 $maybe_exists = category_exists($cat_id);
95 if ( $maybe_exists ) {
102 function convert_them() {
105 if ( (!isset($_POST['cats_to_convert']) || !is_array($_POST['cats_to_convert'])) && empty($this->categories_to_convert)) {
106 print '<div class="narrow">';
107 print '<p>' . sprintf(__('Uh, oh. Something didn\'t work. Please <a href="%s">try again</a>.'), 'admin.php?import=wp-cat2tag') . '</p>';
113 if ( empty($this->categories_to_convert) )
114 $this->categories_to_convert = $_POST['cats_to_convert'];
115 $hier = _get_term_hierarchy('category');
119 foreach ( (array) $this->categories_to_convert as $cat_id) {
120 $cat_id = (int) $cat_id;
122 print '<li>' . sprintf(__('Converting category #%s ... '), $cat_id);
124 if (!$this->_category_exists($cat_id)) {
125 _e('Category doesn\'t exist!');
127 $category =& get_category($cat_id);
129 if ( tag_exists($wpdb->escape($category->name)) ) {
130 _e('Category is already a tag.');
135 // If the category is the default, leave category in place and create tag.
136 if ( get_option('default_category') == $category->term_id ) {
137 $id = wp_insert_term($category->name, 'post_tag', array('slug' => $category->slug));
138 $id = $id['term_taxonomy_id'];
139 $posts = get_objects_in_term($category->term_id, 'category');
140 foreach ( $posts as $post ) {
141 if ( !$wpdb->get_var("SELECT object_id FROM $wpdb->term_relationships WHERE object_id = '$post' AND term_taxonomy_id = '$id'") )
142 $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ('$post', '$id')");
143 clean_post_cache($post);
146 $tt_ids = $wpdb->get_col("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = '{$category->term_id}' AND taxonomy = 'category'");
148 $posts = $wpdb->get_col("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN (" . join(',', $tt_ids) . ") GROUP BY object_id");
149 foreach ( (array) $posts as $post )
150 clean_post_cache($post);
153 // Change the category to a tag.
154 $wpdb->query("UPDATE $wpdb->term_taxonomy SET taxonomy = 'post_tag' WHERE term_id = '{$category->term_id}' AND taxonomy = 'category'");
156 $terms = $wpdb->get_col("SELECT term_id FROM $wpdb->term_taxonomy WHERE parent = '{$category->term_id}' AND taxonomy = 'category'");
157 foreach ( (array) $terms as $term )
158 clean_category_cache($term);
160 // Set all parents to 0 (root-level) if their parent was the converted tag
161 $wpdb->query("UPDATE $wpdb->term_taxonomy SET parent = 0 WHERE parent = '{$category->term_id}' AND taxonomy = 'category'");
164 clean_category_cache($category->term_id);
166 _e('Converted successfully.');
177 $step = (isset($_GET['step'])) ? (int) $_GET['step'] : 1;
181 if (!current_user_can('manage_categories')) {
182 print '<div class="narrow">';
183 print '<p>' . __('Cheatin’ uh?') . '</p>';
187 check_admin_referer('import-cat2tag');
195 $this->convert_them();
203 function WP_Categories_to_Tags() {
208 $wp_cat2tag_importer = new WP_Categories_to_Tags();
210 register_importer('wp-cat2tag', __('Categories to Tags Converter'), __('Convert existing categories to tags, selectively.'), array(&$wp_cat2tag_importer, 'init'));