3 class WP_Categories_to_Tags {
4 var $categories_to_convert = array();
5 var $all_categories = array();
8 echo '<div class="wrap">';
9 echo '<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 echo '<div class="narrow">';
31 if (count($this->all_categories) > 0) {
32 echo '<p>' . __('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.') . '</p>';
33 echo '<p>' . __('Keep in mind that if you convert a category with child categories, the children become top-level orphans.') . '</p>';
35 $this->categories_form();
37 echo '<p>'.__('You have no categories to convert!').'</p>';
43 function categories_form() {
45 <script type="text/javascript">
47 var checkflag = "false";
48 function check_all_rows() {
49 field = document.formlist;
50 if ( 'false' == checkflag ) {
51 for ( i = 0; i < field.length; i++ ) {
52 if ( 'cats_to_convert[]' == field[i].name )
53 field[i].checked = true;
56 return '<?php _e('Uncheck All') ?>';
58 for ( i = 0; i < field.length; i++ ) {
59 if ( 'cats_to_convert[]' == field[i].name )
60 field[i].checked = false;
63 return '<?php _e('Check All') ?>';
70 echo '<form name="formlist" id="formlist" action="admin.php?import=wp-cat2tag&step=2" method="post">
71 <p><input type="button" class="button-secondary" value="' . __('Check All') . '"' . ' onClick="this.value=check_all_rows()"></p>';
72 wp_nonce_field('import-cat2tag');
73 echo '<ul style="list-style:none">';
75 $hier = _get_term_hierarchy('category');
77 foreach ($this->all_categories as $category) {
78 $category = sanitize_term( $category, 'category', 'display' );
80 if ((int) $category->parent == 0) {
81 echo '<li><label><input type="checkbox" name="cats_to_convert[]" value="' . intval($category->term_id) . '" /> ' . $category->name . ' (' . $category->count . ')</label>';
83 if (isset($hier[$category->term_id])) {
84 $this->_category_children($category, $hier);
93 echo '<p class="submit"><input type="submit" name="submit" class="button" value="' . __('Convert Tags') . '" /></p>';
98 function _category_children($parent, $hier) {
99 echo '<ul style="list-style:none">';
101 foreach ($hier[$parent->term_id] as $child_id) {
102 $child =& get_category($child_id);
104 echo '<li><label><input type="checkbox" name="cats_to_convert[]" value="' . intval($child->term_id) . '" /> ' . $child->name . ' (' . $child->count . ')</label>';
106 if (isset($hier[$child->term_id])) {
107 $this->_category_children($child, $hier);
116 function _category_exists($cat_id) {
117 $cat_id = (int) $cat_id;
119 $maybe_exists = category_exists($cat_id);
121 if ( $maybe_exists ) {
128 function convert_them() {
131 if ( (!isset($_POST['cats_to_convert']) || !is_array($_POST['cats_to_convert'])) && empty($this->categories_to_convert)) {
132 echo '<div class="narrow">';
133 echo '<p>' . sprintf(__('Uh, oh. Something didn’t work. Please <a href="%s">try again</a>.'), 'admin.php?import=wp-cat2tag') . '</p>';
139 if ( empty($this->categories_to_convert) )
140 $this->categories_to_convert = $_POST['cats_to_convert'];
141 $hier = _get_term_hierarchy('category');
145 foreach ( (array) $this->categories_to_convert as $cat_id) {
146 $cat_id = (int) $cat_id;
148 echo '<li>' . sprintf(__('Converting category #%s ... '), $cat_id);
150 if (!$this->_category_exists($cat_id)) {
151 _e('Category doesn\'t exist!');
153 $category =& get_category($cat_id);
155 if ( tag_exists($wpdb->escape($category->name)) ) {
156 _e('Category is already a tag.');
161 // If the category is the default, leave category in place and create tag.
162 if ( get_option('default_category') == $category->term_id ) {
163 $id = wp_insert_term($category->name, 'post_tag', array('slug' => $category->slug));
164 $id = $id['term_taxonomy_id'];
165 $posts = get_objects_in_term($category->term_id, 'category');
166 foreach ( $posts as $post ) {
167 if ( !$wpdb->get_var("SELECT object_id FROM $wpdb->term_relationships WHERE object_id = '$post' AND term_taxonomy_id = '$id'") )
168 $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ('$post', '$id')");
169 clean_post_cache($post);
172 $tt_ids = $wpdb->get_col("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = '{$category->term_id}' AND taxonomy = 'category'");
174 $posts = $wpdb->get_col("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN (" . join(',', $tt_ids) . ") GROUP BY object_id");
175 foreach ( (array) $posts as $post )
176 clean_post_cache($post);
179 // Change the category to a tag.
180 $wpdb->query("UPDATE $wpdb->term_taxonomy SET taxonomy = 'post_tag' WHERE term_id = '{$category->term_id}' AND taxonomy = 'category'");
182 $terms = $wpdb->get_col("SELECT term_id FROM $wpdb->term_taxonomy WHERE parent = '{$category->term_id}' AND taxonomy = 'category'");
183 foreach ( (array) $terms as $term )
184 clean_category_cache($term);
186 // Set all parents to 0 (root-level) if their parent was the converted tag
187 $wpdb->query("UPDATE $wpdb->term_taxonomy SET parent = 0 WHERE parent = '{$category->term_id}' AND taxonomy = 'category'");
190 clean_category_cache($category->term_id);
192 _e('Converted successfully.');
199 echo '<p>' . sprintf( __('We’re all done here, but you can always <a href="%s">convert more</a>.'), 'admin.php?import=wp-cat2tag' ) . '</p>';
204 $step = (isset($_GET['step'])) ? (int) $_GET['step'] : 1;
208 if (!current_user_can('manage_categories')) {
209 echo '<div class="narrow">';
210 echo '<p>' . __('Cheatin’ uh?') . '</p>';
214 check_admin_referer('import-cat2tag');
222 $this->convert_them();
230 function WP_Categories_to_Tags() {
235 $wp_cat2tag_importer = new WP_Categories_to_Tags();
237 register_importer('wp-cat2tag', __('Categories to Tags Converter'), __('Convert existing categories to tags, selectively.'), array(&$wp_cat2tag_importer, 'init'));