Categories to Tags Converter - Version 0.5

Version Description

  • Fix issue where WordPress could not load the importer.
Download this release

Release Info

Developer nacin
Plugin Icon 128x128 Categories to Tags Converter
Version 0.5
Comparing to
See all releases

Version 0.5

languages/wpcat2tag-importer.pot ADDED
@@ -0,0 +1,196 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Translation of the WordPress plugin Categories to Tags Converter Importer 0.4 by wordpressdotorg.
2
+ # Copyright (C) 2010 wordpressdotorg
3
+ # This file is distributed under the same license as the Categories to Tags Converter Importer package.
4
+ # FIRST AUTHOR <EMAIL@ADDRESS>, 2010.
5
+ #
6
+ #, fuzzy
7
+ msgid ""
8
+ msgstr ""
9
+ "Project-Id-Version: Categories to Tags Converter Importer 0.4\n"
10
+ "Report-Msgid-Bugs-To: http://wordpress.org/tag/wpcat2tag-importer\n"
11
+ "POT-Creation-Date: 2010-06-01 13:26+0300\n"
12
+ "PO-Revision-Date: 2010-MO-DA HO:MI+ZONE\n"
13
+ "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
14
+ "Language-Team: LANGUAGE <LL@li.org>\n"
15
+ "MIME-Version: 1.0\n"
16
+ "Content-Type: text/plain; charset=utf-8\n"
17
+ "Content-Transfer-Encoding: 8bit\n"
18
+ "Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
19
+
20
+ #: wpcat2tag-importer.php:43
21
+ msgid "Cheatin&#8217; uh?"
22
+ msgstr ""
23
+
24
+ #: wpcat2tag-importer.php:46
25
+ msgid "Categories to Tags"
26
+ msgstr ""
27
+
28
+ #: wpcat2tag-importer.php:47
29
+ msgid "Tags to Categories"
30
+ msgstr ""
31
+
32
+ #: wpcat2tag-importer.php:83
33
+ #, php-format
34
+ msgid "Convert Category to Tag."
35
+ msgid_plural "Convert Categories (%d) to Tags."
36
+ msgstr[0] ""
37
+ msgstr[1] ""
38
+
39
+ #: wpcat2tag-importer.php:85
40
+ msgid ""
41
+ "Hey there. Here you can selectively convert existing categories to tags. To "
42
+ "get started, check the categories you wish to be converted, then click the "
43
+ "Convert button."
44
+ msgstr ""
45
+
46
+ #: wpcat2tag-importer.php:86
47
+ msgid ""
48
+ "Keep in mind that if you convert a category with child categories, the "
49
+ "children become top-level orphans."
50
+ msgstr ""
51
+
52
+ #: wpcat2tag-importer.php:90
53
+ msgid "You have no categories to convert!"
54
+ msgstr ""
55
+
56
+ #: wpcat2tag-importer.php:107 wpcat2tag-importer.php:183
57
+ msgid "Uncheck All"
58
+ msgstr ""
59
+
60
+ #: wpcat2tag-importer.php:114 wpcat2tag-importer.php:121
61
+ #: wpcat2tag-importer.php:190 wpcat2tag-importer.php:197
62
+ msgid "Check All"
63
+ msgstr ""
64
+
65
+ #: wpcat2tag-importer.php:144
66
+ msgid ""
67
+ "* This category is also a tag. Converting it will add that tag to all posts "
68
+ "that are currently in the category."
69
+ msgstr ""
70
+
71
+ #: wpcat2tag-importer.php:146
72
+ msgid "Convert Categories to Tags"
73
+ msgstr ""
74
+
75
+ #: wpcat2tag-importer.php:159
76
+ #, php-format
77
+ msgid "Convert Tag to Category."
78
+ msgid_plural "Convert Tags (%d) to Categories."
79
+ msgstr[0] ""
80
+ msgstr[1] ""
81
+
82
+ #: wpcat2tag-importer.php:161
83
+ msgid ""
84
+ "Here you can selectively convert existing tags to categories. To get "
85
+ "started, check the tags you wish to be converted, then click the Convert "
86
+ "button."
87
+ msgstr ""
88
+
89
+ #: wpcat2tag-importer.php:162
90
+ msgid ""
91
+ "The newly created categories will still be associated with the same posts."
92
+ msgstr ""
93
+
94
+ #: wpcat2tag-importer.php:166
95
+ msgid "You have no tags to convert!"
96
+ msgstr ""
97
+
98
+ #: wpcat2tag-importer.php:208
99
+ msgid ""
100
+ "* This tag is also a category. When converted, all posts associated with the "
101
+ "tag will also be in the category."
102
+ msgstr ""
103
+
104
+ #: wpcat2tag-importer.php:210
105
+ msgid "Convert Tags to Categories"
106
+ msgstr ""
107
+
108
+ #: wpcat2tag-importer.php:248 wpcat2tag-importer.php:368
109
+ #, php-format
110
+ msgid ""
111
+ "Uh, oh. Something didn&#8217;t work. Please <a href=\"%s\">try again</a>."
112
+ msgstr ""
113
+
114
+ #: wpcat2tag-importer.php:267
115
+ #, php-format
116
+ msgid "Category %s doesn&#8217;t exist!"
117
+ msgstr ""
118
+
119
+ #: wpcat2tag-importer.php:270
120
+ #, php-format
121
+ msgid "Converting category <strong>%s</strong> ... "
122
+ msgstr ""
123
+
124
+ #: wpcat2tag-importer.php:298 wpcat2tag-importer.php:341
125
+ #: wpcat2tag-importer.php:427
126
+ msgid "Converted successfully."
127
+ msgstr ""
128
+
129
+ #: wpcat2tag-importer.php:317
130
+ msgid "Tag added to all posts in this category."
131
+ msgstr ""
132
+
133
+ #: wpcat2tag-importer.php:359
134
+ #, php-format
135
+ msgid ""
136
+ "* This category is also a tag. The converter has added that tag to all posts "
137
+ "currently in the category. If you want to remove it, please confirm that all "
138
+ "tags were added successfully, then delete it from the <a href=\"%s\">Manage "
139
+ "Categories</a> page."
140
+ msgstr ""
141
+
142
+ #: wpcat2tag-importer.php:360 wpcat2tag-importer.php:449
143
+ #, php-format
144
+ msgid ""
145
+ "We&#8217;re all done here, but you can always <a href=\"%s\">convert more</"
146
+ "a>."
147
+ msgstr ""
148
+
149
+ #: wpcat2tag-importer.php:385
150
+ #, php-format
151
+ msgid "Converting tag <strong>%s</strong> ... "
152
+ msgstr ""
153
+
154
+ #: wpcat2tag-importer.php:409
155
+ msgid "All posts were added to the category with the same name."
156
+ msgstr ""
157
+
158
+ #: wpcat2tag-importer.php:430
159
+ #, php-format
160
+ msgid "Tag #%s doesn&#8217;t exist!"
161
+ msgstr ""
162
+
163
+ #: wpcat2tag-importer.php:448
164
+ #, php-format
165
+ msgid ""
166
+ "* This tag is also a category. The converter has added all posts from it to "
167
+ "the category. If you want to remove it, please confirm that all posts were "
168
+ "added successfully, then delete it from the <a href=\"%s\">Manage Tags</a> "
169
+ "page."
170
+ msgstr ""
171
+
172
+ #: wpcat2tag-importer.php:491
173
+ msgid "Categories and Tags Converter"
174
+ msgstr ""
175
+
176
+ #. #-#-#-#-# wpcat2tag-importer.pot (Categories to Tags Converter Importer 0.4) #-#-#-#-#
177
+ #. Description of the plugin/theme
178
+ #: wpcat2tag-importer.php:491
179
+ msgid "Convert existing categories to tags or tags to categories, selectively."
180
+ msgstr ""
181
+
182
+ #. Plugin Name of the plugin/theme
183
+ msgid "Categories to Tags Converter Importer"
184
+ msgstr ""
185
+
186
+ #. Plugin URI of the plugin/theme
187
+ msgid "http://wordpress.org/extend/plugins/wpcat2tag-importer/"
188
+ msgstr ""
189
+
190
+ #. Author of the plugin/theme
191
+ msgid "wordpressdotorg"
192
+ msgstr ""
193
+
194
+ #. Author URI of the plugin/theme
195
+ msgid "http://wordpress.org/"
196
+ msgstr ""
readme.txt ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
11
+ == Description ==
12
+
13
+ Convert existing categories to tags or tags to categories, selectively.
14
+
15
+ == Installation ==
16
+
17
+ 1. Upload the `wpcat2tag-importer` folder to the `/wp-content/plugins/` directory
18
+ 1. Activate the plugin through the 'Plugins' menu in WordPress
19
+ 1. Go to the Tools -> Import screen, Click on Categories and Tags Converter
20
+
21
+ == Changelog ==
22
+
23
+ = 0.5 =
24
+ * Fix issue where WordPress could not load the importer.
25
+
26
+ = 0.1 =
27
+ * Initial release
wpcat2tag-importer.php ADDED
@@ -0,0 +1,497 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: Categories to Tags Converter Importer
4
+ 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
+
15
+ // Load Importer API
16
+ require_once ABSPATH . 'wp-admin/includes/import.php';
17
+
18
+ if ( !class_exists( 'WP_Importer' ) ) {
19
+ $class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php';
20
+ if ( file_exists( $class_wp_importer ) )
21
+ require_once $class_wp_importer;
22
+ }
23
+
24
+ /**
25
+ * Categories to Tags Converter Importer
26
+ *
27
+ * @package WordPress
28
+ * @subpackage 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() {
51
+ echo '</div>';
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' ) )
60
+ $this->hybrids_ids[] = $category->term_id;
61
+ }
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;
69
+ if ( term_exists( $tag->slug, 'category' ) )
70
+ $this->hybrids_ids[] = $tag->term_id;
71
+ }
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>';
166
+ }
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' )
493
+
494
+ function wpcat2tag_importer_init() {
495
+ load_plugin_textdomain( 'wpcat2tag-importer', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
496
+ }
497
+ add_action( 'init', 'wpcat2tag_importer_init' );