Categories to Tags Converter - Version 0.6

Version Description

  • Add support for WordPress 6.1
Download this release

Release Info

Developer githubsync
Plugin Icon 128x128 Categories to Tags Converter
Version 0.6
Comparing to
See all releases

Code changes from version 0.5 to 0.6

Files changed (2) hide show
  1. readme.txt +18 -4
  2. wpcat2tag-importer.php +308 -313
readme.txt CHANGED
@@ -1,10 +1,12 @@
1
  === Categories to Tags Converter ===
2
  Contributors: wordpressdotorg
3
- Donate link:
4
  Tags: importer, categories and tags converter
5
  Requires at least: 3.0
6
- Tested up to: 4.1
7
- Stable tag: 0.5
 
 
8
 
9
  Convert existing categories to tags or tags to categories, selectively.
10
 
@@ -20,8 +22,20 @@ Convert existing categories to tags or tags to categories, selectively.
20
 
21
  == Changelog ==
22
 
 
 
 
23
  = 0.5 =
24
- * Fix issue where WordPress could not load the importer.
 
 
 
 
 
 
 
 
 
25
 
26
  = 0.1 =
27
  * Initial release
1
  === Categories to Tags Converter ===
2
  Contributors: wordpressdotorg
3
+ Donate link:
4
  Tags: importer, categories and tags converter
5
  Requires at least: 3.0
6
+ Tested up to: 6.1
7
+ Stable tag: 0.6
8
+ License: GPLv2 or later
9
+ License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
11
  Convert existing categories to tags or tags to categories, selectively.
12
 
22
 
23
  == Changelog ==
24
 
25
+ = 0.6 =
26
+ * Add support for WordPress 6.1
27
+
28
  = 0.5 =
29
+ * Fix issue where WordPress could not load the importer
30
+
31
+ = 0.4 =
32
+ * Fixes #WP13460
33
+
34
+ = 0.3 =
35
+ * Change handle to match plugin slug
36
+
37
+ = 0.2 =
38
+ * Add WP_LOAD_IMPORTERS check
39
 
40
  = 0.1 =
41
  * Initial release
wpcat2tag-importer.php CHANGED
@@ -5,10 +5,18 @@ Plugin URI: http://wordpress.org/extend/plugins/wpcat2tag-importer/
5
  Description: Convert existing categories to tags or tags to categories, selectively.
6
  Author: wordpressdotorg
7
  Author URI: http://wordpress.org/
8
- Version: 0.5
9
- License: GPL v2 - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10
  */
11
 
 
 
 
 
 
 
 
 
12
  if ( !defined('WP_LOAD_IMPORTERS') )
13
  return;
14
 
@@ -29,22 +37,36 @@ if ( !class_exists( 'WP_Importer' ) ) {
29
  */
30
  if ( class_exists( 'WP_Importer' ) ) {
31
  class WP_Categories_to_Tags extends WP_Importer {
32
- var $categories_to_convert = array();
33
  var $all_categories = array();
34
- var $tags_to_convert = array();
35
  var $all_tags = array();
36
  var $hybrids_ids = array();
37
 
38
- function header() {
39
- echo '<div class="wrap">';
40
- if ( ! current_user_can('manage_categories') ) {
41
- echo '<div class="narrow">';
42
- echo '<p>' . __('Cheatin&#8217; uh?', 'wpcat2tag-importer') . '</p>';
43
- echo '</div>';
44
- } else { ?>
45
- <div class="tablenav"><p style="margin:4px"><a style="display:inline;" class="button-secondary" href="admin.php?import=wpcat2tag"><?php _e( "Categories to Tags" , 'wpcat2tag-importer'); ?></a>
46
- <a style="display:inline;" class="button-secondary" href="admin.php?import=wpcat2tag&amp;step=3"><?php _e( "Tags to Categories" , 'wpcat2tag-importer'); ?></a></p></div>
47
- <?php }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  }
49
 
50
  function footer() {
@@ -52,8 +74,7 @@ class WP_Categories_to_Tags extends WP_Importer {
52
  }
53
 
54
  function populate_cats() {
55
-
56
- $categories = get_categories(array('get' => 'all'));
57
  foreach ( $categories as $category ) {
58
  $this->all_categories[] = $category;
59
  if ( term_exists( $category->slug, 'post_tag' ) )
@@ -62,7 +83,6 @@ class WP_Categories_to_Tags extends WP_Importer {
62
  }
63
 
64
  function populate_tags() {
65
-
66
  $tags = get_terms( array('post_tag'), array('get' => 'all') );
67
  foreach ( $tags as $tag ) {
68
  $this->all_tags[] = $tag;
@@ -72,94 +92,84 @@ class WP_Categories_to_Tags extends WP_Importer {
72
  }
73
 
74
  function categories_tab() {
75
- $this->populate_cats();
76
- $cat_num = count($this->all_categories);
 
 
77
 
78
- echo '<br class="clear" />';
 
79
 
80
  if ( $cat_num > 0 ) {
81
- screen_icon();
82
- echo '<h2>' . sprintf( _n( 'Convert Category to Tag.', 'Convert Categories (%d) to Tags.', $cat_num , 'wpcat2tag-importer'), $cat_num ) . '</h2>';
83
  echo '<div class="narrow">';
84
  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.', 'wpcat2tag-importer') . '</p>';
85
- echo '<p>' . __('Keep in mind that if you convert a category with child categories, the children become top-level orphans.', 'wpcat2tag-importer') . '</p></div>';
86
-
87
  $this->categories_form();
88
  } else {
89
  echo '<p>'.__('You have no categories to convert!', 'wpcat2tag-importer').'</p>';
90
  }
91
  }
92
 
93
- function categories_form() { ?>
94
-
95
- <script type="text/javascript">
96
- /* <![CDATA[ */
97
- var checkflag = "false";
98
- function check_all_rows() {
99
- field = document.catlist;
100
- if ( 'false' == checkflag ) {
101
- for ( i = 0; i < field.length; i++ ) {
102
- if ( 'cats_to_convert[]' == field[i].name )
103
- field[i].checked = true;
104
- }
105
- checkflag = 'true';
106
- return '<?php _e('Uncheck All', 'wpcat2tag-importer') ?>';
107
- } else {
108
- for ( i = 0; i < field.length; i++ ) {
109
- if ( 'cats_to_convert[]' == field[i].name )
110
- field[i].checked = false;
111
- }
112
- checkflag = 'false';
113
- return '<?php _e('Check All', 'wpcat2tag-importer') ?>';
114
- }
115
- }
116
- /* ]]> */
117
- </script>
118
-
119
- <form name="catlist" id="catlist" action="admin.php?import=wpcat2tag&amp;step=2" method="post">
120
- <p><input type="button" class="button-secondary" value="<?php esc_attr_e('Check All', 'wpcat2tag-importer'); ?>" onclick="this.value=check_all_rows()" />
121
- <?php wp_nonce_field('import-cat2tag'); ?></p>
122
- <ul style="list-style:none">
123
-
124
- <?php $hier = _get_term_hierarchy('category');
125
 
126
- foreach ($this->all_categories as $category) {
127
  $category = sanitize_term( $category, 'category', 'display' );
128
 
129
- if ( (int) $category->parent == 0 ) { ?>
 
130
 
131
- <li><label><input type="checkbox" name="cats_to_convert[]" value="<?php echo intval($category->term_id); ?>" /> <?php echo $category->name . ' (' . $category->count . ')'; ?></label><?php
132
-
133
- if ( in_array( intval($category->term_id), $this->hybrids_ids ) )
134
- echo ' <a href="#note"> * </a>';
135
 
136
  if ( isset($hier[$category->term_id]) )
137
- $this->_category_children($category, $hier); ?></li>
138
- <?php }
139
- } ?>
140
- </ul>
141
 
142
- <?php if ( ! empty($this->hybrids_ids) )
143
- 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.', 'wpcat2tag-importer') . '</p>'; ?>
 
144
 
145
- <p class="submit"><input type="submit" name="submit" class="button" value="<?php esc_attr_e('Convert Categories to Tags', 'wpcat2tag-importer'); ?>" /></p>
146
- </form>
147
 
148
- <?php }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
 
150
  function tags_tab() {
151
- $this->populate_tags();
152
- $tags_num = count($this->all_tags);
 
 
153
 
154
- echo '<br class="clear" />';
 
155
 
156
  if ( $tags_num > 0 ) {
157
- screen_icon();
158
- echo '<h2>' . sprintf( _n( 'Convert Tag to Category.', 'Convert Tags (%d) to Categories.', $tags_num , 'wpcat2tag-importer'), $tags_num ) . '</h2>';
159
  echo '<div class="narrow">';
160
  echo '<p>' . __('Here you can selectively convert existing tags to categories. To get started, check the tags you wish to be converted, then click the Convert button.', 'wpcat2tag-importer') . '</p>';
161
- echo '<p>' . __('The newly created categories will still be associated with the same posts.', 'wpcat2tag-importer') . '</p></div>';
162
-
163
  $this->tags_form();
164
  } else {
165
  echo '<p>'.__('You have no tags to convert!', 'wpcat2tag-importer').'</p>';
@@ -167,326 +177,311 @@ function check_all_rows() {
167
  }
168
 
169
  function tags_form() { ?>
170
-
171
- <script type="text/javascript">
172
- /* <![CDATA[ */
173
- var checktags = "false";
174
- function check_all_tagrows() {
175
- field = document.taglist;
176
- if ( 'false' == checktags ) {
177
- for ( i = 0; i < field.length; i++ ) {
178
- if ( 'tags_to_convert[]' == field[i].name )
179
- field[i].checked = true;
180
- }
181
- checktags = 'true';
182
- return '<?php _e('Uncheck All', 'wpcat2tag-importer') ?>';
183
- } else {
184
- for ( i = 0; i < field.length; i++ ) {
185
- if ( 'tags_to_convert[]' == field[i].name )
186
- field[i].checked = false;
187
- }
188
- checktags = 'false';
189
- return '<?php _e('Check All', 'wpcat2tag-importer') ?>';
190
- }
191
- }
192
- /* ]]> */
193
- </script>
194
-
195
- <form name="taglist" id="taglist" action="admin.php?import=wpcat2tag&amp;step=4" method="post">
196
- <p><input type="button" class="button-secondary" value="<?php esc_attr_e('Check All', 'wpcat2tag-importer'); ?>" onclick="this.value=check_all_tagrows()" />
197
- <?php wp_nonce_field('import-cat2tag'); ?></p>
198
- <ul style="list-style:none">
199
 
200
  <?php foreach ( $this->all_tags as $tag ) { ?>
201
- <li><label><input type="checkbox" name="tags_to_convert[]" value="<?php echo intval($tag->term_id); ?>" /> <?php echo esc_attr($tag->name) . ' (' . $tag->count . ')'; ?></label><?php if ( in_array( intval($tag->term_id), $this->hybrids_ids ) ) echo ' <a href="#note"> * </a>'; ?></li>
202
-
203
  <?php } ?>
204
  </ul>
205
 
206
  <?php if ( ! empty($this->hybrids_ids) )
207
  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.', 'wpcat2tag-importer') . '</p>'; ?>
208
 
209
- <p class="submit"><input type="submit" name="submit_tags" class="button" value="<?php esc_attr_e('Convert Tags to Categories', 'wpcat2tag-importer'); ?>" /></p>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
  </form>
211
 
212
  <?php }
213
 
214
- function _category_children($parent, $hier) { ?>
215
-
216
- <ul style="list-style:none">
217
- <?php foreach ($hier[$parent->term_id] as $child_id) {
218
- $child =& get_category($child_id); ?>
219
- <li><label><input type="checkbox" name="cats_to_convert[]" value="<?php echo intval($child->term_id); ?>" /> <?php echo $child->name . ' (' . $child->count . ')'; ?></label><?php
220
-
221
- if ( in_array( intval($child->term_id), $this->hybrids_ids ) )
222
- echo ' <a href="#note"> * </a>';
223
-
224
- if ( isset($hier[$child->term_id]) )
225
- $this->_category_children($child, $hier); ?></li>
226
- <?php } ?>
227
- </ul><?php
228
- }
229
-
230
- function _category_exists($cat_id) {
231
- $cat_id = (int) $cat_id;
232
-
233
- $maybe_exists = category_exists($cat_id);
234
 
235
- if ( $maybe_exists ) {
236
- return true;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
237
  } else {
238
- return false;
239
  }
240
  }
241
 
242
- function convert_categories() {
243
- global $wpdb;
244
 
245
- if ( (!isset($_POST['cats_to_convert']) || !is_array($_POST['cats_to_convert'])) && empty($this->categories_to_convert)) { ?>
246
- <div class="narrow">
247
- <p><?php printf(__('Uh, oh. Something didn&#8217;t work. Please <a href="%s">try again</a>.', 'wpcat2tag-importer'), 'admin.php?import=wpcat2tag'); ?></p>
248
- </div>
249
- <?php return;
250
  }
251
 
252
- if ( empty($this->categories_to_convert) )
253
- $this->categories_to_convert = $_POST['cats_to_convert'];
 
254
 
255
- $hier = _get_term_hierarchy('category');
256
- $hybrid_cats = $clear_parents = $parents = false;
257
- $clean_term_cache = $clean_cat_cache = array();
258
- $default_cat = get_option('default_category');
 
259
 
260
  echo '<ul>';
261
 
262
- foreach ( (array) $this->categories_to_convert as $cat_id) {
263
- $cat_id = (int) $cat_id;
264
-
265
- if ( ! $this->_category_exists($cat_id) ) {
266
- echo '<li>' . sprintf( __('Category %s doesn&#8217;t exist!', 'wpcat2tag-importer'), $cat_id ) . "</li>\n";
267
  } else {
268
- $category =& get_category($cat_id);
269
- echo '<li>' . sprintf(__('Converting category <strong>%s</strong> ... ', 'wpcat2tag-importer'), $category->name);
270
 
271
- // If the category is the default, leave category in place and create tag.
272
- if ( $default_cat == $category->term_id ) {
273
 
274
- if ( ! ($id = term_exists( $category->slug, 'post_tag' ) ) )
275
- $id = wp_insert_term($category->name, 'post_tag', array('slug' => $category->slug));
 
276
 
277
- if ( is_wp_error($id) ) {
278
- echo $id->get_error_message() . "</li>\n";
279
- continue;
280
- }
281
 
282
- $id = $id['term_taxonomy_id'];
283
- $posts = get_objects_in_term($category->term_id, 'category');
284
- $term_order = 0;
285
 
286
- foreach ( $posts as $post ) {
287
- $values[] = $wpdb->prepare( "(%d, %d, %d)", $post, $id, $term_order);
288
- clean_post_cache($post);
289
- }
290
 
291
- if ( $values ) {
292
- $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)");
293
 
294
- $wpdb->update($wpdb->term_taxonomy, array('count' => $category->count), array('term_id' => $category->term_id, 'taxonomy' => 'post_tag') );
295
- }
296
 
297
- echo __('Converted successfully.', 'wpcat2tag-importer') . "</li>\n";
298
- continue;
299
- }
300
 
301
- // if tag already exists, add it to all posts in the category
302
- 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) ) ) {
303
- $objects_ids = get_objects_in_term($category->term_id, 'category');
304
- $tag_ttid = (int) $tag_ttid;
305
- $term_order = 0;
306
 
307
- foreach ( $objects_ids as $object_id )
308
- $values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $tag_ttid, $term_order);
309
 
310
- if ( $values ) {
311
- $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)");
312
 
313
- $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tag_ttid) );
314
- $wpdb->update($wpdb->term_taxonomy, array('count' => $count), array('term_id' => $category->term_id, 'taxonomy' => 'post_tag') );
315
- }
316
- echo __('Tag added to all posts in this category.', 'wpcat2tag-importer') . " *</li>\n";
317
 
318
- $hybrid_cats = true;
319
- $clean_term_cache[] = $category->term_id;
320
- $clean_cat_cache[] = $category->term_id;
 
 
 
321
 
322
- continue;
323
- }
324
 
325
- $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) );
326
- if ( $tt_ids ) {
327
- $posts = $wpdb->get_col("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN (" . join(',', $tt_ids) . ") GROUP BY object_id");
328
- foreach ( (array) $posts as $post )
329
- clean_post_cache($post);
330
- }
 
 
 
 
 
 
 
331
 
332
- // Change the category to a tag.
333
- $wpdb->update($wpdb->term_taxonomy, array('taxonomy' => 'post_tag'), array('term_id' => $category->term_id, 'taxonomy' => 'category') );
334
 
335
- // Set all parents to 0 (root-level) if their parent was the converted tag
336
- $wpdb->update($wpdb->term_taxonomy, array('parent' => 0), array('parent' => $category->term_id, 'taxonomy' => 'category') );
 
 
 
 
 
337
 
338
- if ( $parents ) $clear_parents = true;
339
- $clean_cat_cache[] = $category->term_id;
340
- echo __('Converted successfully.', 'wpcat2tag-importer') . "</li>\n";
341
- }
342
- }
343
- echo '</ul>';
344
 
345
- if ( ! empty($clean_term_cache) ) {
346
- $clean_term_cache = array_unique(array_values($clean_term_cache));
347
- clean_term_cache($clean_term_cache, 'post_tag');
348
- }
 
349
 
350
- if ( ! empty($clean_cat_cache) ) {
351
- $clean_cat_cache = array_unique(array_values($clean_cat_cache));
352
- clean_term_cache($clean_cat_cache, 'category');
353
- }
 
 
 
354
 
355
- if ( $clear_parents ) delete_option('category_children');
 
 
 
 
 
 
 
 
 
356
 
357
- if ( $hybrid_cats )
358
- 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.', 'wpcat2tag-importer'), 'categories.php') . '</p>';
359
- echo '<p>' . sprintf( __('We&#8217;re all done here, but you can always <a href="%s">convert more</a>.', 'wpcat2tag-importer'), 'admin.php?import=wpcat2tag' ) . '</p>';
360
  }
361
 
362
  function convert_tags() {
363
- global $wpdb;
364
 
365
- if ( (!isset($_POST['tags_to_convert']) || !is_array($_POST['tags_to_convert'])) && empty($this->tags_to_convert)) {
366
  echo '<div class="narrow">';
367
- echo '<p>' . sprintf(__('Uh, oh. Something didn&#8217;t work. Please <a href="%s">try again</a>.', 'wpcat2tag-importer'), 'admin.php?import=wpcat2tag&amp;step=3') . '</p>';
368
  echo '</div>';
369
  return;
370
  }
371
 
372
- if ( empty($this->tags_to_convert) )
373
- $this->tags_to_convert = $_POST['tags_to_convert'];
 
 
 
 
 
 
 
 
 
 
 
374
 
375
- $hybrid_tags = $clear_parents = false;
376
- $clean_cat_cache = $clean_term_cache = array();
377
- $default_cat = get_option('default_category');
378
  echo '<ul>';
379
 
380
- foreach ( (array) $this->tags_to_convert as $tag_id) {
381
  $tag_id = (int) $tag_id;
 
 
 
 
 
382
 
383
- if ( $tag = get_term( $tag_id, 'post_tag' ) ) {
384
- printf('<li>' . __('Converting tag <strong>%s</strong> ... ', 'wpcat2tag-importer'), $tag->name);
385
-
386
- 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) ) ) {
387
- $objects_ids = get_objects_in_term($tag->term_id, 'post_tag');
388
- $cat_ttid = (int) $cat_ttid;
389
- $term_order = 0;
390
-
391
- foreach ( $objects_ids as $object_id ) {
392
- $values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $cat_ttid, $term_order);
393
- clean_post_cache($object_id);
394
- }
395
-
396
- if ( $values ) {
397
- $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)");
398
 
399
- if ( $default_cat != $tag->term_id ) {
400
- $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tag->term_id) );
401
- $wpdb->update($wpdb->term_taxonomy, array('count' => $count), array('term_id' => $tag->term_id, 'taxonomy' => 'category') );
402
- }
403
  }
404
-
405
- $hybrid_tags = true;
406
- $clean_term_cache[] = $tag->term_id;
407
- $clean_cat_cache[] = $tag->term_id;
408
- echo __('All posts were added to the category with the same name.', 'wpcat2tag-importer') . " *</li>\n";
409
-
410
- continue;
411
  }
412
 
413
- // Change the tag to a category.
414
- $parent = $wpdb->get_var( $wpdb->prepare("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'post_tag'", $tag->term_id) );
415
- if ( 0 == $parent || (0 < (int) $parent && $this->_category_exists($parent)) ) {
416
- $new_fields = array('taxonomy' => 'category');
417
- $clear_parents = true;
418
- } else {
419
- $new_fields = array('taxonomy' => 'category', 'parent' => 0);
420
- }
421
 
422
- $wpdb->update($wpdb->term_taxonomy, $new_fields, array('term_id' => $tag->term_id, 'taxonomy' => 'post_tag') );
423
-
424
- $clean_term_cache[] = $tag->term_id;
425
- $clean_cat_cache[] = $cat['term_id'];
426
- echo __('Converted successfully.', 'wpcat2tag-importer') . "</li>\n";
427
-
428
- } else {
429
- printf( '<li>' . __('Tag #%s doesn&#8217;t exist!', 'wpcat2tag-importer') . "</li>\n", $tag_id );
430
  }
431
  }
432
 
433
- if ( ! empty($clean_term_cache) ) {
434
- $clean_term_cache = array_unique(array_values($clean_term_cache));
435
- clean_term_cache($clean_term_cache, 'post_tag');
436
- }
437
-
438
- if ( ! empty($clean_cat_cache) ) {
439
- $clean_cat_cache = array_unique(array_values($clean_cat_cache));
440
- clean_term_cache($clean_term_cache, 'category');
441
- }
442
-
443
- if ( $clear_parents ) delete_option('category_children');
444
-
445
  echo '</ul>';
446
- if ( $hybrid_tags )
447
- 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.', 'wpcat2tag-importer'), 'edit-tags.php') . '</p>';
448
- echo '<p>' . sprintf( __('We&#8217;re all done here, but you can always <a href="%s">convert more</a>.', 'wpcat2tag-importer'), 'admin.php?import=wpcat2tag&amp;step=3' ) . '</p>';
449
  }
450
 
451
- function init() {
 
 
 
 
 
 
 
 
 
 
 
 
 
452
 
453
- $step = (isset($_GET['step'])) ? (int) $_GET['step'] : 1;
 
 
454
 
455
- $this->header();
 
 
 
 
 
 
 
 
 
 
456
 
457
- if ( current_user_can('manage_categories') ) {
 
 
458
 
459
- switch ($step) {
460
- case 1 :
461
- $this->categories_tab();
462
- break;
463
 
464
- case 2 :
465
- check_admin_referer('import-cat2tag');
466
- $this->convert_categories();
467
- break;
468
 
469
- case 3 :
470
- $this->tags_tab();
471
- break;
472
 
473
- case 4 :
474
- check_admin_referer('import-cat2tag');
475
- $this->convert_tags();
476
- break;
477
- }
478
- }
479
 
480
  $this->footer();
481
  }
482
-
483
- function WP_Categories_to_Tags() {
484
- // Do nothing.
485
- }
486
  }
487
 
488
  $wp_cat2tag_importer = new WP_Categories_to_Tags();
489
-
490
  register_importer('wpcat2tag', __('Categories and Tags Converter', 'wpcat2tag-importer'), __('Convert existing categories to tags or tags to categories, selectively.', 'wpcat2tag-importer'), array(&$wp_cat2tag_importer, 'init'));
491
 
492
  } // class_exists( 'WP_Importer' )
5
  Description: Convert existing categories to tags or tags to categories, selectively.
6
  Author: wordpressdotorg
7
  Author URI: http://wordpress.org/
8
+ Version: 0.6
9
+ License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10
  */
11
 
12
+ /* == Todo ==
13
+ * - ensure following expected behaviour in all cases... what is expected behaviour? remove+delete the old cat/tag/format?
14
+ * - cache cleaning (think wp_delete_term does most, if not all)
15
+ * - more UI cleanup (indent for child cats, what should convert to selectors look like?, ...)
16
+ * - re-introduce select all option (old button was ugly)
17
+ * - somehow use list tables? for: probably looks better, against: poss. bulky code
18
+ */
19
+
20
  if ( !defined('WP_LOAD_IMPORTERS') )
21
  return;
22
 
37
  */
38
  if ( class_exists( 'WP_Importer' ) ) {
39
  class WP_Categories_to_Tags extends WP_Importer {
 
40
  var $all_categories = array();
 
41
  var $all_tags = array();
42
  var $hybrids_ids = array();
43
 
44
+ function header( $current_tab ) {
45
+ if ( ! current_user_can('manage_categories') )
46
+ wp_die( __( 'Cheatin&#8217; uh?', 'wpcat2tag-importer' ) );
47
+
48
+ $tabs = array(
49
+ 'cats' => array( 'label' => __( 'Categories', 'wpcat2tag-importer' ), 'url' => admin_url( 'admin.php?import=wpcat2tag&tab=cats' ) ),
50
+ 'tags' => array( 'label' => __( 'Tags', 'wpcat2tag-importer' ), 'url' => admin_url( 'admin.php?import=wpcat2tag&tab=tags' ) )
51
+ );
52
+
53
+ if ( function_exists( 'set_post_format' ) )
54
+ $tabs['formats'] = array( 'label' => __( 'Formats', 'wpcat2tag-importer' ), 'url' => admin_url( 'admin.php?import=wpcat2tag&tab=formats' ) ); ?>
55
+
56
+ <div class="wrap">
57
+ <?php
58
+ if ( version_compare( get_bloginfo( 'version' ), '3.8.0', '<' ) ) {
59
+ screen_icon();
60
+ }
61
+ ?>
62
+ <h2><?php _e( 'Categories, Tags and Formats Converter', 'wpcat2tag-importer' ); ?></h2>
63
+ <h3 class="nav-tab-wrapper">
64
+ <?php foreach ( $tabs as $tab => $info ) :
65
+ $class = ($tab == $current_tab) ? ' nav-tab-active' : ''; ?>
66
+ <a href="<?php echo $info['url']; ?>" class="nav-tab<?php echo $class; ?>"><?php echo esc_html( $info['label'] ); ?></a>
67
+ <?php endforeach; ?>
68
+ </h3>
69
+ <?php
70
  }
71
 
72
  function footer() {
74
  }
75
 
76
  function populate_cats() {
77
+ $categories = get_categories( array('get' => 'all') );
 
78
  foreach ( $categories as $category ) {
79
  $this->all_categories[] = $category;
80
  if ( term_exists( $category->slug, 'post_tag' ) )
83
  }
84
 
85
  function populate_tags() {
 
86
  $tags = get_terms( array('post_tag'), array('get' => 'all') );
87
  foreach ( $tags as $tag ) {
88
  $this->all_tags[] = $tag;
92
  }
93
 
94
  function categories_tab() {
95
+ if ( isset($_POST['cats_to_convert']) ) {
96
+ $this->convert_categories();
97
+ return;
98
+ }
99
 
100
+ $this->populate_cats();
101
+ $cat_num = count( $this->all_categories );
102
 
103
  if ( $cat_num > 0 ) {
 
 
104
  echo '<div class="narrow">';
105
  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.', 'wpcat2tag-importer') . '</p>';
106
+ echo '<p>' . __('Keep in mind that if you convert a category with child categories, the children become top-level orphans.', 'wpcat2tag-importer') . '</p>';
107
+ echo '</div>';
108
  $this->categories_form();
109
  } else {
110
  echo '<p>'.__('You have no categories to convert!', 'wpcat2tag-importer').'</p>';
111
  }
112
  }
113
 
114
+ function categories_form() {
115
+ $hier = _get_term_hierarchy( 'category' );
116
+ echo '<form name="catlist" id="catlist" action="admin.php?import=wpcat2tag&amp;tab=cats" method="post">';
117
+ wp_nonce_field( 'import-cat2tag' );
118
+ echo '<ul>';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
 
120
+ foreach ( $this->all_categories as $category ) {
121
  $category = sanitize_term( $category, 'category', 'display' );
122
 
123
+ if ( (int) $category->parent == 0 ) {
124
+ echo '<li><label><input type="checkbox" name="cats_to_convert[]" value="' . intval($category->term_id) . '" /> ' . esc_html($category->name) . " ({$category->count})</label>";
125
 
126
+ if ( in_array( intval($category->term_id), $this->hybrids_ids ) )
127
+ echo ' <a href="#note"> * </a>';
 
 
128
 
129
  if ( isset($hier[$category->term_id]) )
130
+ $this->_category_children($category, $hier);
 
 
 
131
 
132
+ echo '</li>';
133
+ }
134
+ }
135
 
136
+ echo '</ul>';
 
137
 
138
+ if ( ! empty($this->hybrids_ids) )
139
+ 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.', 'wpcat2tag-importer') . '</p>';
140
+
141
+ if ( current_theme_supports( 'post-formats' ) ) :
142
+ $post_formats = get_theme_support( 'post-formats' );
143
+ if ( is_array( $post_formats[0] ) ) : ?>
144
+ <p><?php _e( 'Convert categories to:', 'wpcat2tag-importer' ); ?><br />
145
+ <label><input type="radio" name="convert_to" value="tags" checked="checked" /> <?php _e( 'Tags', 'wpcat2tag-importer' ); ?></label><br />
146
+ <label><input type="radio" name="convert_to" value="format" /> <?php _e( 'Post Format', 'wpcat2tag-importer' ); ?></label>
147
+ <select name="post_format">
148
+ <?php foreach ( $post_formats[0] as $format ) : ?>
149
+ <option value="<?php echo esc_attr( $format ); ?>"><?php echo esc_html( get_post_format_string( $format ) ); ?></option>
150
+ <?php endforeach; ?>
151
+ </select>
152
+ </p>
153
+ <?php endif; endif; ?>
154
+
155
+ <p class="submit"><input type="submit" name="submit" class="button" value="<?php esc_attr_e( 'Convert Categories', 'wpcat2tag-importer' ); ?>" /></p>
156
+ </form><?php
157
+ }
158
 
159
  function tags_tab() {
160
+ if ( isset($_POST['tags_to_convert']) ) {
161
+ $this->convert_tags();
162
+ return;
163
+ }
164
 
165
+ $this->populate_tags();
166
+ $tags_num = count( $this->all_tags );
167
 
168
  if ( $tags_num > 0 ) {
 
 
169
  echo '<div class="narrow">';
170
  echo '<p>' . __('Here you can selectively convert existing tags to categories. To get started, check the tags you wish to be converted, then click the Convert button.', 'wpcat2tag-importer') . '</p>';
171
+ echo '<p>' . __('The newly created categories will still be associated with the same posts.', 'wpcat2tag-importer') . '</p>';
172
+ echo '</div>';
173
  $this->tags_form();
174
  } else {
175
  echo '<p>'.__('You have no tags to convert!', 'wpcat2tag-importer').'</p>';
177
  }
178
 
179
  function tags_form() { ?>
180
+ <form name="taglist" id="taglist" action="admin.php?import=wpcat2tag&amp;tab=tags" method="post">
181
+ <?php wp_nonce_field( 'import-cat2tag' ); ?>
182
+ <ul>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
 
184
  <?php foreach ( $this->all_tags as $tag ) { ?>
185
+ <li><label><input type="checkbox" name="tags_to_convert[]" value="<?php echo intval($tag->term_id); ?>" /> <?php echo esc_html($tag->name) . ' (' . $tag->count . ')'; ?></label><?php if ( in_array( intval($tag->term_id), $this->hybrids_ids ) ) echo ' <a href="#note"> * </a>'; ?></li>
 
186
  <?php } ?>
187
  </ul>
188
 
189
  <?php if ( ! empty($this->hybrids_ids) )
190
  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.', 'wpcat2tag-importer') . '</p>'; ?>
191
 
192
+ <?php if ( current_theme_supports( 'post-formats' ) ) :
193
+ $post_formats = get_theme_support( 'post-formats' );
194
+ if ( is_array( $post_formats[0] ) ) : ?>
195
+ <p><?php _e( 'Convert tags to:', 'wpcat2tag-importer' ); ?><br />
196
+ <label><input type="radio" name="convert_to" value="cats" checked="checked" /> <?php _e( 'Categories', 'wpcat2tag-importer' ); ?></label><br />
197
+ <label><input type="radio" name="convert_to" value="format" /> <?php _e( 'Post Format', 'wpcat2tag-importer' ); ?></label>
198
+ <select name="post_format">
199
+ <?php foreach ( $post_formats[0] as $format ) : ?>
200
+ <option value="<?php echo esc_attr( $format ); ?>"><?php echo esc_html( get_post_format_string( $format ) ); ?></option>
201
+ <?php endforeach; ?>
202
+ </select>
203
+ </p>
204
+ <?php endif; endif; ?>
205
+
206
+ <p class="submit"><input type="submit" name="submit" class="button" value="<?php esc_attr_e( 'Convert Tags', 'wpcat2tag-importer' ); ?>" /></p>
207
  </form>
208
 
209
  <?php }
210
 
211
+ function formats_tab() {
212
+ if ( isset($_POST['post_formats']) ) {
213
+ $this->convert_formats();
214
+ return;
215
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
216
 
217
+ $formats = get_terms( array('post_format'), array('get'=>'all') );
218
+ $format_count = count( $formats );
219
+
220
+ if ( $format_count > 0 ) { ?>
221
+ <form action="admin.php?import=wpcat2tag&amp;tab=formats" method="post">
222
+ <?php wp_nonce_field( 'import-cat2tag' ); ?>
223
+
224
+ <ul>
225
+ <?php foreach ( $formats as $format ) :
226
+ $slug = substr( $format->slug, 12 ); ?>
227
+ <li><label><input type="checkbox" name="post_formats[]" value="<?php echo intval($format->term_id); ?>" /> <?php echo esc_html( get_post_format_string($slug) ) . " ({$format->count})"; ?></label></li>
228
+ <?php endforeach; ?>
229
+ </ul>
230
+
231
+ <p><?php _e( 'Convert formats to:', 'wpcat2tag-importer' ); ?><br />
232
+ <label><input type="radio" name="convert_to" value="cat" checked="checked" /> <?php _e( 'Category', 'wpcat2tag-importer' ); ?></label><br />
233
+ <label><input type="radio" name="convert_to" value="tag" /> <?php _e( 'Tag', 'wpcat2tag-importer' ); ?></label></p>
234
+ <input type="text" name="convert_to_slug" value="" />
235
+ <p class="submit"><input type="submit" name="submit" class="button" value="<?php esc_attr_e( 'Convert Formats', 'wpcat2tag-importer' ); ?>" /></p>
236
+ </form><?php
237
  } else {
238
+ echo '<p>' . __( 'You have no posts set to a specific format.', 'wpcat2tag-importer' ) . '</p>';
239
  }
240
  }
241
 
242
+ function convert_formats() {
243
+ check_admin_referer( 'import-cat2tag' );
244
 
245
+ if ( ! is_array($_POST['post_formats']) || empty($_POST['convert_to_slug']) || ('cat' != $_POST['convert_to'] && 'tag' != $_POST['convert_to']) ) {
246
+ echo '<div class="narrow">';
247
+ echo '<p>' . sprintf( __('Uh, oh. Something didn&#8217;t work. Please <a href="%s">try again</a>.', 'wpcat2tag-importer'), admin_url('admin.php?import=wpcat2tag&amp;tab=formats') ) . '</p>';
248
+ echo '</div>';
249
+ return;
250
  }
251
 
252
+ $convert_to = 'tag' == $_POST['convert_to'] ? 'post_tag' : 'category';
253
+ if ( ! $term_info = term_exists( $_POST['convert_to_slug'], $convert_to ) )
254
+ $term_info = wp_insert_term( $_POST['convert_to_slug'], $convert_to );
255
 
256
+ if ( is_wp_error($term_info) ) {
257
+ echo '<p>' . $term_info->get_error_message() . ' ';
258
+ printf( __( 'Please <a href="%s">try again</a>.', 'wpcat2tag-importer' ), 'admin.php?import=wpcat2tag&amp;tab=cats' ) . "</p>\n";
259
+ return;
260
+ }
261
 
262
  echo '<ul>';
263
 
264
+ foreach ( $_POST['post_formats'] as $format_id ) {
265
+ $format_id = (int) $format_id;
266
+ $format = get_term( $format_id, 'post_format' );
267
+ if ( ! $format ) {
268
+ echo '<li>' . sprintf( __( 'Post format #%d doesn&#8217;t exist!', 'wpcat2tag-importer' ), $format_id ) . "</li>\n";
269
  } else {
270
+ $slug = substr( $format->slug, 12 );
271
+ echo '<li>' . sprintf( __( 'Converting format <strong>%s</strong> ... ', 'wpcat2tag-importer' ), get_post_format_string( $slug ) );
272
 
273
+ $this->_convert_term( array( 'term_id' => $format->term_id, 'taxonomy' => 'post_format', 'term_taxonomy_id' => $format->term_taxonomy_id ), $term_info['term_taxonomy_id'] );
 
274
 
275
+ echo __( 'Converted successfully.', 'wpcat2tag-importer' ) . "</li>\n";
276
+ }
277
+ }
278
 
279
+ echo '</ul>';
280
+ echo '<p>' . sprintf( __( 'We&#8217;re all done here, but you can always <a href="%s">convert more</a>.', 'wpcat2tag-importer' ), admin_url( 'admin.php?import=wpcat2tag&amp;tab=formats' ) ) . '</p>';
281
+ }
 
282
 
283
+ function _category_children( $parent, $hier ) {
284
+ echo '<ul>';
 
285
 
286
+ foreach ( $hier[$parent->term_id] as $child_id ) {
287
+ $child =& get_category($child_id);
 
 
288
 
289
+ echo '<li><label><input type="checkbox" name="cats_to_convert[]" value="'. intval($child->term_id) .'" /> ' . esc_html($child->name) . " ({$child->count})</label>";
 
290
 
291
+ if ( in_array( intval($child->term_id), $this->hybrids_ids ) )
292
+ echo ' <a href="#note"> * </a>';
293
 
294
+ if ( isset($hier[$child->term_id]) )
295
+ $this->_category_children($child, $hier);
 
296
 
297
+ echo '</li>';
298
+ }
 
 
 
299
 
300
+ echo '</ul>';
301
+ }
302
 
303
+ function convert_categories() {
304
+ global $wpdb;
305
 
306
+ check_admin_referer( 'import-cat2tag' );
 
 
 
307
 
308
+ if ( ! is_array($_POST['cats_to_convert']) ) {
309
+ echo '<div class="narrow">';
310
+ echo '<p>' . sprintf(__('Uh, oh. Something didn&#8217;t work. Please <a href="%s">try again</a>.', 'wpcat2tag-importer'), 'admin.php?import=wpcat2tag&amp;tab=cats') . '</p>';
311
+ echo '</div>';
312
+ return;
313
+ }
314
 
315
+ $default = get_option( 'default_category' );
 
316
 
317
+ if ( ! isset($_POST['convert_to']) || 'format' != $_POST['convert_to'] ) {
318
+ $convert_to = 'post_tag';
319
+ } else {
320
+ $convert_to = 'post_format';
321
+ $term_info = $this->_get_format_info( sanitize_key($_POST['post_format']) );
322
+ if ( is_wp_error($term_info) ) {
323
+ echo '<div class="narrow"><p>';
324
+ echo $term_info->get_error_message() . ' ';
325
+ printf( __( 'Please <a href="%s">try again</a>.', 'wpcat2tag-importer' ), 'admin.php?import=wpcat2tag&amp;tab=cats' );
326
+ echo '</p></div>';
327
+ return;
328
+ }
329
+ }
330
 
331
+ echo '<ul>';
 
332
 
333
+ foreach ( $_POST['cats_to_convert'] as $cat_id ) {
334
+ $cat_id = (int) $cat_id;
335
+ $category = get_term( $cat_id, 'category' );
336
+ if ( ! $category ) {
337
+ echo '<li>' . sprintf( __( 'Category #%d doesn&#8217;t exist!', 'wpcat2tag-importer' ), $cat_id ) . "</li>\n";
338
+ } else {
339
+ echo '<li>' . sprintf( __( 'Converting category <strong>%s</strong> ... ', 'wpcat2tag-importer' ), esc_html($category->name) );
340
 
341
+ if ( 'post_tag' == $convert_to ) {
342
+ if ( ! $term_info = term_exists( $category->slug, 'post_tag' ) )
343
+ $term_info = wp_insert_term( $category->name, 'post_tag', array( 'description' => $category->description ) );
 
 
 
344
 
345
+ if ( is_wp_error($term_info) ) {
346
+ echo $term_info->get_error_message() . "</li>\n";
347
+ continue;
348
+ }
349
+ }
350
 
351
+ // if this is the default category then leave it in place and just add the new tag/format
352
+ if ( $default == $category->term_id ) {
353
+ $posts = get_objects_in_term( $category->term_id, 'category' );
354
+ foreach ( $posts as $post ) {
355
+ $values[] = $wpdb->prepare( "(%d, %d, 0)", $post, $term_info['term_taxonomy_id'] );
356
+ clean_post_cache( $post );
357
+ }
358
 
359
+ $wpdb->query( "INSERT INTO {$wpdb->term_relationships} (object_id, term_taxonomy_id, term_order) VALUES " . join(',', $values) );
360
+ $wpdb->update( $wpdb->term_taxonomy, array( 'count' => $category->count ), array( 'term_id' => $term_info['term_id'], 'taxonomy' => $convert_to ) );
361
+ // otherwise just convert it
362
+ } else {
363
+ $this->_convert_term( array( 'term_id' => $category->term_id, 'taxonomy' => 'category', 'term_taxonomy_id' => $category->term_taxonomy_id ), $term_info['term_taxonomy_id'] );
364
+ }
365
+
366
+ echo __( 'Converted successfully.', 'wpcat2tag-importer' ) . "</li>\n";
367
+ }
368
+ }
369
 
370
+ echo '</ul>';
371
+ echo '<p>' . sprintf( __( 'We&#8217;re all done here, but you can always <a href="%s">convert more</a>.', 'wpcat2tag-importer' ), admin_url( 'admin.php?import=wpcat2tag&amp;tab=cats' ) ) . '</p>';
 
372
  }
373
 
374
  function convert_tags() {
375
+ check_admin_referer( 'import-cat2tag' );
376
 
377
+ if ( ! is_array($_POST['tags_to_convert']) ) {
378
  echo '<div class="narrow">';
379
+ echo '<p>' . sprintf(__('Uh, oh. Something didn&#8217;t work. Please <a href="%s">try again</a>.', 'wpcat2tag-importer'), 'admin.php?import=wpcat2tag&amp;tab=tags') . '</p>';
380
  echo '</div>';
381
  return;
382
  }
383
 
384
+ if ( ! isset($_POST['convert_to']) || 'format' != $_POST['convert_to'] ) {
385
+ $convert_to = 'category';
386
+ } else {
387
+ $convert_to = 'post_format';
388
+ $term_info = $this->_get_format_info( sanitize_key($_POST['post_format']) );
389
+ if ( is_wp_error($term_info) ) {
390
+ echo '<div class="narrow"><p>';
391
+ echo $term_info->get_error_message() . ' ';
392
+ printf( __( 'Please <a href="%s">try again</a>.', 'wpcat2tag-importer' ), 'admin.php?import=wpcat2tag&amp;tab=tags' );
393
+ echo '</p></div>';
394
+ return;
395
+ }
396
+ }
397
 
 
 
 
398
  echo '<ul>';
399
 
400
+ foreach ( $_POST['tags_to_convert'] as $tag_id ) {
401
  $tag_id = (int) $tag_id;
402
+ $tag = get_term( $tag_id, 'post_tag' );
403
+ if ( ! $tag ) {
404
+ echo '<li>' . sprintf( __( 'Tag #%d doesn&#8217;t exist!', 'wpcat2tag-importer' ), $tag_id ) . "</li>\n";
405
+ } else {
406
+ echo '<li>' . sprintf( __( 'Converting tag <strong>%s</strong> ... ', 'wpcat2tag-importer' ), esc_html($tag->name) );
407
 
408
+ if ( 'category' == $convert_to ) {
409
+ if ( ! $term_info = term_exists( $tag->slug, 'category' ) )
410
+ $term_info = wp_insert_term( $tag->name, 'category', array( 'description' => $tag->description ) );
 
 
 
 
 
 
 
 
 
 
 
 
411
 
412
+ if ( is_wp_error($term_info) ) {
413
+ echo $term_info->get_error_message() . "</li>\n";
414
+ continue;
 
415
  }
 
 
 
 
 
 
 
416
  }
417
 
418
+ $this->_convert_term( array( 'term_id' => $tag->term_id, 'taxonomy' => 'post_tag', 'term_taxonomy_id' => $tag->term_taxonomy_id ), $term_info['term_taxonomy_id'] );
 
 
 
 
 
 
 
419
 
420
+ echo __( 'Converted successfully.', 'wpcat2tag-importer' ) . "</li>\n";
 
 
 
 
 
 
 
421
  }
422
  }
423
 
 
 
 
 
 
 
 
 
 
 
 
 
424
  echo '</ul>';
425
+ echo '<p>' . sprintf( __( 'We&#8217;re all done here, but you can always <a href="%s">convert more</a>.', 'wpcat2tag-importer' ), admin_url( 'admin.php?import=wpcat2tag&amp;tab=tags' ) ) . '</p>';
 
 
426
  }
427
 
428
+ /**
429
+ * Convert all term relationships to a new term, delete the old term if possible.
430
+ *
431
+ * The old term will not be deleted if it's the default category or if it's a part
432
+ * of any other taxonomies.
433
+ *
434
+ * @param array $from term_id, taxonomy and term_taxonomy_id of the term+taxonomy pair converting from
435
+ * @param int $to_ttid The term_taxonomy_id of the term+taxonomy pair we are converting to
436
+ */
437
+ function _convert_term( $from, $to_ttid ) {
438
+ global $wpdb;
439
+
440
+ // transfer all the term relationships
441
+ $wpdb->update( $wpdb->term_relationships, array( 'term_taxonomy_id' => $to_ttid ), array( 'term_taxonomy_id' => $from['term_taxonomy_id'] ) );
442
 
443
+ // remove the old term
444
+ wp_delete_term( $from['term_id'], $from['taxonomy'] );
445
+ }
446
 
447
+ function _get_format_info( $format ) {
448
+ if ( current_theme_supports( 'post-formats' ) && ! empty( $format ) ) {
449
+ $post_formats = get_theme_support( 'post-formats' );
450
+ if ( is_array( $post_formats ) ) {
451
+ $post_formats = $post_formats[0];
452
+ if ( ! in_array( $format, $post_formats ) )
453
+ return new WP_Error( 'invalid_format', sprintf( __( 'Bad post format %s.', 'wpcat2tag-importer' ), esc_html($format) ) );
454
+ }
455
+ } else {
456
+ return new WP_Error( 'invalid_format', __( 'Either your theme does not support post formats or you supplied an invalid format.', 'wpcat2tag-importer' ) );
457
+ }
458
 
459
+ $format = 'post-format-' . $format;
460
+ if ( ! $term_info = term_exists( $format, 'post_format' ) )
461
+ $term_info = wp_insert_term( $format, 'post_format' );
462
 
463
+ return $term_info;
464
+ }
 
 
465
 
466
+ function init() {
467
+ if ( ! current_user_can( 'manage_categories' ) )
468
+ wp_die( __( 'Cheatin&#8217; uh?', 'wpcat2tag-importer' ) );
 
469
 
470
+ $tab = isset( $_GET['tab'] ) ? $_GET['tab'] : 'cats';
471
+ $this->header( $tab );
 
472
 
473
+ if ( 'cats' == $tab )
474
+ $this->categories_tab();
475
+ else if ( 'tags' == $tab )
476
+ $this->tags_tab();
477
+ else if ( 'formats' == $tab && function_exists( 'set_post_format' ) )
478
+ $this->formats_tab();
479
 
480
  $this->footer();
481
  }
 
 
 
 
482
  }
483
 
484
  $wp_cat2tag_importer = new WP_Categories_to_Tags();
 
485
  register_importer('wpcat2tag', __('Categories and Tags Converter', 'wpcat2tag-importer'), __('Convert existing categories to tags or tags to categories, selectively.', 'wpcat2tag-importer'), array(&$wp_cat2tag_importer, 'init'));
486
 
487
  } // class_exists( 'WP_Importer' )