]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/import/wp-cat2tag.php
Wordpress 2.5.1
[autoinstalls/wordpress.git] / wp-admin / import / wp-cat2tag.php
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                 echo '<div class="wrap">';
9                 echo '<h2>' . __('Convert Categories to Tags') . '</h2>';
10         }
11
12         function footer() {
13                 echo '</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                 echo '<div class="narrow">';
30
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>';
34
35                         $this->categories_form();
36                 } else {
37                         echo '<p>'.__('You have no categories to convert!').'</p>';
38                 }
39
40                 echo '</div>';
41         }
42
43         function categories_form() {
44 ?>
45 <script type="text/javascript">
46 <!--
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;
54                 }
55                 checkflag = 'true';
56                 return '<?php _e('Uncheck All') ?>';
57         } else {
58                 for ( i = 0; i < field.length; i++ ) {
59                         if ( 'cats_to_convert[]' == field[i].name )
60                                 field[i].checked = false;
61                 }
62                 checkflag = 'false';
63                 return '<?php _e('Check All') ?>';
64         }
65 }
66
67 //  -->
68 </script>
69 <?php
70                 echo '<form name="formlist" id="formlist" action="admin.php?import=wp-cat2tag&amp;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">';
74
75                 $hier = _get_term_hierarchy('category');
76
77                 foreach ($this->all_categories as $category) {
78                         $category = sanitize_term( $category, 'category', 'display' );
79
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>';
82
83                                 if (isset($hier[$category->term_id])) {
84                                         $this->_category_children($category, $hier);
85                                 }
86
87                                 echo '</li>';
88                         }
89                 }
90
91                 echo '</ul>';
92
93                 echo '<p class="submit"><input type="submit" name="submit" class="button" value="' . __('Convert Tags') . '" /></p>';
94
95                 echo '</form>';
96         }
97
98         function _category_children($parent, $hier) {
99                 echo '<ul style="list-style:none">';
100
101                 foreach ($hier[$parent->term_id] as $child_id) {
102                         $child =& get_category($child_id);
103
104                         echo '<li><label><input type="checkbox" name="cats_to_convert[]" value="' . intval($child->term_id) . '" /> ' . $child->name . ' (' . $child->count . ')</label>';
105
106                         if (isset($hier[$child->term_id])) {
107                                 $this->_category_children($child, $hier);
108                         }
109
110                         echo '</li>';
111                 }
112
113                 echo '</ul>';
114         }
115
116         function _category_exists($cat_id) {
117                 $cat_id = (int) $cat_id;
118
119                 $maybe_exists = category_exists($cat_id);
120
121                 if ( $maybe_exists ) {
122                         return true;
123                 } else {
124                         return false;
125                 }
126         }
127
128         function convert_them() {
129                 global $wpdb;
130
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&#8217;t work. Please <a href="%s">try again</a>.'), 'admin.php?import=wp-cat2tag') . '</p>';
134                         echo '</div>';
135                         return;
136                 }
137
138
139                 if ( empty($this->categories_to_convert) )
140                         $this->categories_to_convert = $_POST['cats_to_convert'];
141                 $hier = _get_term_hierarchy('category');
142
143                 echo '<ul>';
144
145                 foreach ( (array) $this->categories_to_convert as $cat_id) {
146                         $cat_id = (int) $cat_id;
147
148                         echo '<li>' . sprintf(__('Converting category #%s ... '),  $cat_id);
149
150                         if (!$this->_category_exists($cat_id)) {
151                                 _e('Category doesn\'t exist!');
152                         } else {
153                                 $category =& get_category($cat_id);
154
155                                 if ( tag_exists($wpdb->escape($category->name)) ) {
156                                         _e('Category is already a tag.');
157                                         echo '</li>';
158                                         continue;
159                                 }
160
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);
170                                         }
171                                 } else {
172                                         $tt_ids = $wpdb->get_col("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = '{$category->term_id}' AND taxonomy = 'category'");
173                                         if ( $tt_ids ) {
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);
177                                         }
178
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'");
181
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);
185
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'");
188                                 }
189                                 // Clean the cache
190                                 clean_category_cache($category->term_id);
191
192                                 _e('Converted successfully.');
193                         }
194
195                         echo '</li>';
196                 }
197
198                 echo '</ul>';
199                 echo '<p>' . sprintf( __('We&#8217;re all done here, but you can always <a href="%s">convert more</a>.'), 'admin.php?import=wp-cat2tag' ) . '</p>';
200         }
201
202         function init() {
203
204                 $step = (isset($_GET['step'])) ? (int) $_GET['step'] : 1;
205
206                 $this->header();
207
208                 if (!current_user_can('manage_categories')) {
209                         echo '<div class="narrow">';
210                         echo '<p>' . __('Cheatin&#8217; uh?') . '</p>';
211                         echo '</div>';
212                 } else {
213                         if ( $step > 1 )
214                                 check_admin_referer('import-cat2tag');
215
216                         switch ($step) {
217                                 case 1 :
218                                         $this->welcome();
219                                 break;
220
221                                 case 2 :
222                                         $this->convert_them();
223                                 break;
224                         }
225                 }
226
227                 $this->footer();
228         }
229
230         function WP_Categories_to_Tags() {
231                 // Do nothing.
232         }
233 }
234
235 $wp_cat2tag_importer = new WP_Categories_to_Tags();
236
237 register_importer('wp-cat2tag', __('Categories to Tags Converter'), __('Convert existing categories to tags, selectively.'), array(&$wp_cat2tag_importer, 'init'));
238
239 ?>