root/tags/1.3/wp-admin/import/wp-cat2tag.php

Revision 1125, 6.2 kB (checked in by donncha, 1 year ago)

Merge with WordPress?, rev 6285 and untested

Line 
1 <?php
2
3 class WP_Categories_to_Tags {
4     var $categories_to_convert = array();
5     var $all_categories = array();
6
7     function header() {
8         print '<div class="wrap">';
9         print '<h2>' . __('Convert Categories to Tags') . '</h2>';
10     }
11
12     function footer() {
13         print '</div>';
14     }
15
16     function populate_all_categories() {
17         global $wpdb;
18
19         $categories = get_categories('get=all');
20         foreach ( $categories as $category ) {
21             if ( !tag_exists($wpdb->escape($category->name)) )
22                 $this->all_categories[] = $category;   
23         }
24     }
25
26     function welcome() {
27         $this->populate_all_categories();
28
29         print '<div class="narrow">';
30
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>';
34
35             $this->categories_form();
36         } else {
37             print '<p>'.__('You have no categories to convert!').'</p>';
38         }
39
40         print '</div>';
41     }
42
43     function categories_form() {
44         print '<form action="admin.php?import=wp-cat2tag&amp;step=2" method="post">';
45         wp_nonce_field('import-cat2tag');
46         print '<ul style="list-style:none">';
47
48         $hier = _get_term_hierarchy('category');
49
50         foreach ($this->all_categories as $category) {
51             $category = sanitize_term( $category, 'category', 'display' );
52         
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>';
55
56                 if (isset($hier[$category->term_id])) {
57                     $this->_category_children($category, $hier);
58                 }
59
60                 print '</li>';
61             }
62         }
63
64         print '</ul>';
65
66         print '<p class="submit"><input type="submit" name="submit" value="' . __('Convert &raquo;') . '" /></p>';
67         print '</form>';
68     }
69
70     function _category_children($parent, $hier) {
71         print '<ul style="list-style:none">';
72
73         foreach ($hier[$parent->term_id] as $child_id) {
74             $child =& get_category($child_id);
75
76             print '<li><label><input type="checkbox" name="cats_to_convert[]" value="' . intval($child->term_id) . '" /> ' . $child->name . ' (' . $child->count . ')</label>';
77
78             if (isset($hier[$child->term_id])) {
79                 $this->_category_children($child, $hier);
80             }
81
82             print '</li>';
83         }
84
85         print '</ul>';
86     }
87
88     function _category_exists($cat_id) {
89         global $wpdb;
90
91         $cat_id = (int) $cat_id;
92
93         $maybe_exists = category_exists($cat_id);
94
95         if ( $maybe_exists ) {
96             return true;
97         } else {
98             return false;
99         }
100     }
101
102     function convert_them() {
103         global $wpdb;
104
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>';
108             print '</div>';
109             return;
110         }
111
112
113         if ( empty($this->categories_to_convert) )
114             $this->categories_to_convert = $_POST['cats_to_convert'];
115         $hier = _get_term_hierarchy('category');
116
117         print '<ul>';
118
119         foreach ( (array) $this->categories_to_convert as $cat_id) {
120             $cat_id = (int) $cat_id;
121
122             print '<li>' . sprintf(__('Converting category #%s ... '),  $cat_id);
123
124             if (!$this->_category_exists($cat_id)) {
125                 _e('Category doesn\'t exist!');
126             } else {
127                 $category =& get_category($cat_id);
128
129                 if ( tag_exists($wpdb->escape($category->name)) ) {
130                     _e('Category is already a tag.');
131                     print '</li>';
132                     continue;
133                 }
134
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);
144                     }
145                 } else {
146                     $tt_ids = $wpdb->get_col("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = '{$category->term_id}' AND taxonomy = 'category'");
147                     if ( $tt_ids ) {
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);
151                     }
152
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'");
155
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);
159
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'");
162                 }
163                 // Clean the cache
164                 clean_category_cache($category->term_id);
165
166                 _e('Converted successfully.');
167             }
168
169             print '</li>';
170         }
171
172         print '</ul>';
173     }
174
175     function init() {
176
177         $step = (isset($_GET['step'])) ? (int) $_GET['step'] : 1;
178
179         $this->header();
180
181         if (!current_user_can('manage_categories')) {
182             print '<div class="narrow">';
183             print '<p>' . __('Cheatin&#8217; uh?') . '</p>';
184             print '</div>';
185         } else {
186             if ( $step > 1 )
187                 check_admin_referer('import-cat2tag');
188
189             switch ($step) {
190                 case 1 :
191                     $this->welcome();
192                 break;
193
194                 case 2 :
195                     $this->convert_them();
196                 break;
197             }
198         }
199
200         $this->footer();
201     }
202
203     function WP_Categories_to_Tags() {
204         // Do nothing.
205     }
206 }
207
208 $wp_cat2tag_importer = new WP_Categories_to_Tags();
209
210 register_importer('wp-cat2tag', __('Categories to Tags Converter'), __('Convert existing categories to tags, selectively.'), array(&$wp_cat2tag_importer, 'init'));
211
212 ?>
213
Note: See TracBrowser for help on using the browser.