]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/import/wp-cat2tag.php
Wordpress 2.6.2
[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         var $tags_to_convert = array();
7         var $all_tags = array();
8         var $hybrids_ids = array();
9
10         function header() {
11                 echo '<div class="wrap">';
12                 if ( ! current_user_can('manage_categories') ) {
13                         echo '<div class="narrow">';
14                         echo '<p>' . __('Cheatin&#8217; uh?') . '</p>';
15                         echo '</div>';
16                 } else { ?>
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&amp;step=3"><?php _e( "Tags to Categories" ); ?></a></p></div>
19 <?php   }
20         }
21
22         function footer() {
23                 echo '</div>';
24         }
25
26         function populate_cats() {
27
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;
33                 }
34         }
35
36         function populate_tags() {
37
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;
43                 }
44         }
45
46         function categories_tab() {
47                 $this->populate_cats();
48                 $cat_num = count($this->all_categories);
49
50                 echo '<br class="clear" />';
51
52                 if ( $cat_num > 0 ) {
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>';
57
58                         $this->categories_form();
59                 } else {
60                         echo '<p>'.__('You have no categories to convert!').'</p>';
61                 }
62         }
63
64         function categories_form() { ?>
65
66 <script type="text/javascript">
67 /* <![CDATA[ */
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;
75                 }
76                 checkflag = 'true';
77                 return '<?php _e('Uncheck All') ?>';
78         } else {
79                 for ( i = 0; i < field.length; i++ ) {
80                         if ( 'cats_to_convert[]' == field[i].name )
81                                 field[i].checked = false;
82                 }
83                 checkflag = 'false';
84                 return '<?php _e('Check All') ?>';
85         }
86 }
87 /* ]]> */
88 </script>
89
90 <form name="catlist" id="catlist" action="admin.php?import=wp-cat2tag&amp;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">
94
95 <?php   $hier = _get_term_hierarchy('category');
96
97                 foreach ($this->all_categories as $category) {
98                         $category = sanitize_term( $category, 'category', 'display' );
99
100                         if ( (int) $category->parent == 0 ) { ?>
101
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
103
104                                  if ( in_array( intval($category->term_id),  $this->hybrids_ids ) )
105                                         echo ' <a href="#note"> * </a>';
106                                 
107                                 if ( isset($hier[$category->term_id]) )
108                                         $this->_category_children($category, $hier); ?></li>
109 <?php           }
110                 } ?>
111 </ul>
112
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>'; ?>
115
116 <p class="submit"><input type="submit" name="submit" class="button" value="<?php _e('Convert Categories to Tags'); ?>" /></p>
117 </form>
118
119 <?php }
120
121         function tags_tab() {
122                 $this->populate_tags();
123                 $tags_num = count($this->all_tags);
124
125                 echo '<br class="clear" />';
126
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>';
132
133                         $this->tags_form();
134                 } else {
135                         echo '<p>'.__('You have no tags to convert!').'</p>';
136                 }
137         }
138
139         function tags_form() { ?>
140
141 <script type="text/javascript">
142 /* <![CDATA[ */
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;
150                 }
151                 checktags = 'true';
152                 return '<?php _e('Uncheck All') ?>';
153         } else {
154                 for ( i = 0; i < field.length; i++ ) {
155                         if ( 'tags_to_convert[]' == field[i].name )
156                                 field[i].checked = false;
157                 }
158                 checktags = 'false';
159                 return '<?php _e('Check All') ?>';
160         }
161 }
162 /* ]]> */
163 </script>
164
165 <form name="taglist" id="taglist" action="admin.php?import=wp-cat2tag&amp;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">
169
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>
172
173 <?php   } ?>
174 </ul>
175
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>'; ?>
178
179 <p class="submit"><input type="submit" name="submit_tags" class="button" value="<?php _e('Convert Tags to Categories'); ?>" /></p>
180 </form>
181
182 <?php }
183
184         function _category_children($parent, $hier) { ?>
185
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
190
191                         if ( in_array( intval($child->term_id), $this->hybrids_ids ) )
192                                 echo ' <a href="#note"> * </a>';
193
194                         if ( isset($hier[$child->term_id]) )
195                                 $this->_category_children($child, $hier); ?></li>
196 <?php   } ?>
197                 </ul><?php
198         }
199
200         function _category_exists($cat_id) {
201                 $cat_id = (int) $cat_id;
202
203                 $maybe_exists = category_exists($cat_id);
204
205                 if ( $maybe_exists ) {
206                         return true;
207                 } else {
208                         return false;
209                 }
210         }
211
212         function convert_categories() {
213                 global $wpdb;
214
215                 if ( (!isset($_POST['cats_to_convert']) || !is_array($_POST['cats_to_convert'])) && empty($this->categories_to_convert)) { ?>
216                         <div class="narrow">
217                         <p><?php printf(__('Uh, oh. Something didn&#8217;t work. Please <a href="%s">try again</a>.'), 'admin.php?import=wp-cat2tag'); ?></p>
218                         </div>
219 <?php           return;
220                 }
221
222                 if ( empty($this->categories_to_convert) )
223                         $this->categories_to_convert = $_POST['cats_to_convert'];
224
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');
229
230                 echo '<ul>';
231
232                 foreach ( (array) $this->categories_to_convert as $cat_id) {
233                         $cat_id = (int) $cat_id;
234
235                         if ( ! $this->_category_exists($cat_id) ) {
236                                 echo '<li>' . sprintf( __('Category %s doesn\'t exist!'),  $cat_id ) . "</li>\n";
237                         } else {
238                                 $category =& get_category($cat_id);
239                                 echo '<li>' . sprintf(__('Converting category <strong>%s</strong> ... '),  $category->name);
240
241                                 // If the category is the default, leave category in place and create tag.
242                                 if ( $default_cat == $category->term_id ) {
243
244                                         if ( ! ($id = is_term( $category->slug, 'post_tag' ) ) )
245                                                 $id = wp_insert_term($category->name, 'post_tag', array('slug' => $category->slug));
246                                         
247                                         $id = $id['term_taxonomy_id'];
248                                         $posts = get_objects_in_term($category->term_id, 'category');
249                                         $term_order = 0;
250
251                                         foreach ( $posts as $post ) {
252                                                 $values[] = $wpdb->prepare( "(%d, %d, %d)", $post, $id, $term_order);
253                                                 clean_post_cache($post);
254                                         }
255                                         
256                                         if ( $values ) {
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)");
258
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) );
260                                         }
261
262                                         echo __('Converted successfully.') . "</li>\n";
263                                         continue;
264                                 }
265                                 
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;
270                                         $term_order = 0;
271
272                                         foreach ( $objects_ids as $object_id )
273                                                 $values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $tag_ttid, $term_order);
274
275                                         if ( $values ) {
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)");
277
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) );
280                                         }
281                                         echo __('Tag added to all posts in this category.') . " *</li>\n";
282
283                                         $hybrid_cats = true;
284                                         $clean_term_cache[] = $category->term_id;
285                                         $clean_cat_cache[] = $category->term_id;
286
287                                         continue;
288                                 }
289
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) );
291                                 if ( $tt_ids ) {
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);
295                                 }
296
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) );
299
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) );
302
303                                 if ( $parents ) $clear_parents = true;
304                                 $clean_cat_cache[] = $category->term_id;
305                                 echo __('Converted successfully.') . "</li>\n";
306                         }
307                 }
308                 echo '</ul>';
309
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');
314                 }
315
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');
320                 }
321
322                 if ( $clear_parents ) delete_option('category_children');
323
324                 if ( $hybrid_cats )
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&#8217;re all done here, but you can always <a href="%s">convert more</a>.'), 'admin.php?import=wp-cat2tag' ) . '</p>';
327         }
328
329         function convert_tags() {
330                 global $wpdb;
331
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&#8217;t work. Please <a href="%s">try again</a>.'), 'admin.php?import=wp-cat2tag&amp;step=3') . '</p>';
335                         echo '</div>';
336                         return;
337                 }
338
339                 if ( empty($this->tags_to_convert) )
340                         $this->tags_to_convert = $_POST['tags_to_convert'];
341
342                 $hybrid_tags = $clear_parents = false;
343                 $clean_cat_cache = $clean_term_cache = array();
344                 $default_cat = get_option('default_category');
345                 echo '<ul>';
346
347                 foreach ( (array) $this->tags_to_convert as $tag_id) {
348                         $tag_id = (int) $tag_id;
349
350                         if ( $tag = get_term( $tag_id, 'post_tag' ) ) {
351                                 printf('<li>' . __('Converting tag <strong>%s</strong> ... '),  $tag->name);
352
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;
356                                         $term_order = 0;
357
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);
361                                         }
362
363                                         if ( $values ) {
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)");
365
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) );
369                                                 }
370                                         }
371
372                                         $hybrid_tags = true;
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";
376
377                                         continue;
378                                 }
379
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)) ) {
383                                         $reset_parent = '';
384                                         $clear_parents = true;
385                                 } else
386                                         $reset_parent = ", parent = '0'";
387
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) );
389
390                                 $clean_term_cache[] = $tag->term_id;
391                                 $clean_cat_cache[] = $cat['term_id'];
392                                 echo __('Converted successfully.') . "</li>\n";
393
394                         } else {
395                                 printf( '<li>' . __('Tag #%s doesn\'t exist!') . "</li>\n",  $tag_id );
396                         }
397                 }
398
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');
403                 }
404
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');
409                 }
410
411                 if ( $clear_parents ) delete_option('category_children');
412
413                 echo '</ul>';
414                 if ( $hybrid_tags )
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&#8217;re all done here, but you can always <a href="%s">convert more</a>.'), 'admin.php?import=wp-cat2tag&amp;step=3' ) . '</p>';
417         }
418
419         function init() {
420
421                 $step = (isset($_GET['step'])) ? (int) $_GET['step'] : 1;
422
423                 $this->header();
424
425                 if ( current_user_can('manage_categories') ) {
426
427                         switch ($step) {
428                                 case 1 :
429                                         $this->categories_tab();
430                                 break;
431
432                                 case 2 :
433                                         check_admin_referer('import-cat2tag');
434                                         $this->convert_categories();
435                                 break;
436
437                                 case 3 :
438                                         $this->tags_tab();
439                                 break;
440
441                                 case 4 :
442                                         check_admin_referer('import-cat2tag');
443                                         $this->convert_tags();
444                                 break;
445                         }
446                 }
447
448                 $this->footer();
449         }
450
451         function WP_Categories_to_Tags() {
452                 // Do nothing.
453         }
454 }
455
456 $wp_cat2tag_importer = new WP_Categories_to_Tags();
457
458 register_importer('wp-cat2tag', __('Categories and Tags Converter'), __('Convert existing categories to tags or tags to categories, selectively.'), array(&$wp_cat2tag_importer, 'init'));
459
460 ?>