| 21 | | if ( !tag_exists($wpdb->escape($category->name)) ) |
|---|
| 22 | | $this->all_categories[] = $category; |
|---|
| 23 | | } |
|---|
| 24 | | } |
|---|
| 25 | | |
|---|
| 26 | | function welcome() { |
|---|
| 27 | | $this->populate_all_categories(); |
|---|
| 28 | | |
|---|
| 29 | | echo '<div class="narrow">'; |
|---|
| 30 | | |
|---|
| 31 | | if (count($this->all_categories) > 0) { |
|---|
| | 31 | $this->all_categories[] = $category; |
|---|
| | 32 | if ( tag_exists( $wpdb->escape($category->name) ) ) |
|---|
| | 33 | $this->hybrids_ids[] = $category->term_id; |
|---|
| | 34 | } |
|---|
| | 35 | } |
|---|
| | 36 | |
|---|
| | 37 | function populate_tags() { |
|---|
| | 38 | global $wpdb; |
|---|
| | 39 | |
|---|
| | 40 | $tags = get_terms( array('post_tag'), 'get=all' ); |
|---|
| | 41 | foreach ( $tags as $tag ) { |
|---|
| | 42 | $this->all_tags[] = $tag; |
|---|
| | 43 | if ( $this->_category_exists($tag->term_id) ) |
|---|
| | 44 | $this->hybrids_ids[] = $tag->term_id; |
|---|
| | 45 | } |
|---|
| | 46 | } |
|---|
| | 47 | |
|---|
| | 48 | function categories_tab() { |
|---|
| | 49 | $this->populate_cats(); |
|---|
| | 50 | $cat_num = count($this->all_categories); |
|---|
| | 51 | |
|---|
| | 52 | echo '<br class="clear" />'; |
|---|
| | 53 | |
|---|
| | 54 | if ( $cat_num > 0 ) { |
|---|
| | 55 | echo '<h2>Convert Categories (' . $cat_num . ') to Tags.</h2>'; |
|---|
| | 56 | echo '<div class="narrow">'; |
|---|
| 69 | | <?php |
|---|
| 70 | | echo '<form name="formlist" id="formlist" action="admin.php?import=wp-cat2tag&step=2" method="post"> |
|---|
| 71 | | <p><input type="button" class="button-secondary" value="' . __('Check All') . '"' . ' onClick="this.value=check_all_rows()"></p>'; |
|---|
| 72 | | wp_nonce_field('import-cat2tag'); |
|---|
| 73 | | echo '<ul style="list-style:none">'; |
|---|
| 74 | | |
|---|
| | 93 | |
|---|
| | 94 | <form name="catlist" id="catlist" action="admin.php?import=wp-cat2tag&step=2" method="post"> |
|---|
| | 95 | <p><input type="button" class="button-secondary" value="<?php _e('Check All'); ?>" onclick="this.value=check_all_rows()"> |
|---|
| | 96 | <?php wp_nonce_field('import-cat2tag'); ?></p> |
|---|
| | 97 | <ul style="list-style:none"> |
|---|
| | 98 | <?php |
|---|
| 80 | | if ((int) $category->parent == 0) { |
|---|
| 81 | | echo '<li><label><input type="checkbox" name="cats_to_convert[]" value="' . intval($category->term_id) . '" /> ' . $category->name . ' (' . $category->count . ')</label>'; |
|---|
| 82 | | |
|---|
| 83 | | if (isset($hier[$category->term_id])) { |
|---|
| 84 | | $this->_category_children($category, $hier); |
|---|
| | 104 | if ( (int) $category->parent == 0 ) { |
|---|
| | 105 | if ( in_array( intval($category->term_id), $this->hybrids_ids ) ) { |
|---|
| | 106 | ?> |
|---|
| | 107 | <li style="color:#777;padding-left:1.3em;"><?php echo $category->name . ' (' . $category->count . ') *'; ?></li> |
|---|
| | 108 | <?php |
|---|
| | 109 | } else { |
|---|
| | 110 | ?> |
|---|
| | 111 | <li><label><input type="checkbox" name="cats_to_convert[]" value="<?php echo intval($category->term_id); ?>" /> <?php echo $category->name . ' (' . $category->count . ')'; ?></label><?php |
|---|
| | 112 | |
|---|
| | 113 | if ( isset($hier[$category->term_id]) ) |
|---|
| | 114 | $this->_category_children($category, $hier); |
|---|
| | 115 | ?></li> |
|---|
| | 116 | <?php |
|---|
| 86 | | |
|---|
| 87 | | echo '</li>'; |
|---|
| 88 | | } |
|---|
| 89 | | } |
|---|
| 90 | | |
|---|
| 91 | | echo '</ul>'; |
|---|
| 92 | | |
|---|
| 93 | | echo '<p class="submit"><input type="submit" name="submit" class="button" value="' . __('Convert Tags') . '" /></p>'; |
|---|
| 94 | | |
|---|
| 95 | | echo '</form>'; |
|---|
| | 118 | } |
|---|
| | 119 | } |
|---|
| | 120 | ?> |
|---|
| | 121 | </ul> |
|---|
| | 122 | |
|---|
| | 123 | <?php |
|---|
| | 124 | if ( ! empty($this->hybrids_ids) ) { |
|---|
| | 125 | echo '<p>' . __('* This category is also a tag. It cannot be convert again.') . '</p>'; |
|---|
| | 126 | } |
|---|
| | 127 | ?> |
|---|
| | 128 | <p class="submit"><input type="submit" name="submit" class="button" value="<?php _e('Convert Categories to Tags'); ?>" /></p> |
|---|
| | 129 | </form> |
|---|
| | 130 | |
|---|
| | 131 | <?php |
|---|
| | 132 | } |
|---|
| | 133 | |
|---|
| | 134 | function tags_tab() { |
|---|
| | 135 | $this->populate_tags(); |
|---|
| | 136 | $tags_num = count($this->all_tags); |
|---|
| | 137 | |
|---|
| | 138 | echo '<br class="clear" />'; |
|---|
| | 139 | |
|---|
| | 140 | if ( $tags_num > 0 ) { |
|---|
| | 141 | echo '<h2>Convert Tags (' . $tags_num . ') to Categories.</h2>'; |
|---|
| | 142 | echo '<div class="narrow">'; |
|---|
| | 143 | 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>'; |
|---|
| | 144 | echo '<p>' . __('The newly created categories will still be associated with the same posts.') . '</p></div>'; |
|---|
| | 145 | |
|---|
| | 146 | |
|---|
| | 147 | $this->tags_form(); |
|---|
| | 148 | } elseif ( $hyb_num > 0 ) { |
|---|
| | 149 | echo '<p>' . __('You have no tags that can be converted. However some of your tags are both a tag and a category.') . '</p>'; |
|---|
| | 150 | } else { |
|---|
| | 151 | echo '<p>'.__('You have no tags to convert!').'</p>'; |
|---|
| | 152 | } |
|---|
| | 153 | } |
|---|
| | 154 | |
|---|
| | 155 | function tags_form() { ?> |
|---|
| | 156 | |
|---|
| | 157 | <script type="text/javascript"> |
|---|
| | 158 | /* <![CDATA[ */ |
|---|
| | 159 | var checktags = "false"; |
|---|
| | 160 | function check_all_tagrows() { |
|---|
| | 161 | field = document.taglist; |
|---|
| | 162 | if ( 'false' == checktags ) { |
|---|
| | 163 | for ( i = 0; i < field.length; i++ ) { |
|---|
| | 164 | if ( 'tags_to_convert[]' == field[i].name ) |
|---|
| | 165 | field[i].checked = true; |
|---|
| | 166 | } |
|---|
| | 167 | checktags = 'true'; |
|---|
| | 168 | return '<?php _e('Uncheck All') ?>'; |
|---|
| | 169 | } else { |
|---|
| | 170 | for ( i = 0; i < field.length; i++ ) { |
|---|
| | 171 | if ( 'tags_to_convert[]' == field[i].name ) |
|---|
| | 172 | field[i].checked = false; |
|---|
| | 173 | } |
|---|
| | 174 | checktags = 'false'; |
|---|
| | 175 | return '<?php _e('Check All') ?>'; |
|---|
| | 176 | } |
|---|
| | 177 | } |
|---|
| | 178 | /* ]]> */ |
|---|
| | 179 | </script> |
|---|
| | 180 | |
|---|
| | 181 | <form name="taglist" id="taglist" action="admin.php?import=wp-cat2tag&step=4" method="post"> |
|---|
| | 182 | <p><input type="button" class="button-secondary" value="<?php _e('Check All'); ?>" onclick="this.value=check_all_tagrows()"> |
|---|
| | 183 | <?php wp_nonce_field('import-cat2tag'); ?></p> |
|---|
| | 184 | <ul style="list-style:none"> |
|---|
| | 185 | <?php |
|---|
| | 186 | foreach ( $this->all_tags as $tag ) { |
|---|
| | 187 | if ( in_array( intval($tag->term_id), $this->hybrids_ids ) ) { |
|---|
| | 188 | ?> |
|---|
| | 189 | <li style="color:#777;padding-left:1.3em;"><?php echo attribute_escape($tag->name) . ' (' . $tag->count . ') *'; ?></li> |
|---|
| | 190 | <?php |
|---|
| | 191 | } else { |
|---|
| | 192 | ?> |
|---|
| | 193 | <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></li> |
|---|
| | 194 | |
|---|
| | 195 | <?php |
|---|
| | 196 | } |
|---|
| | 197 | } |
|---|
| | 198 | ?> |
|---|
| | 199 | </ul> |
|---|
| | 200 | |
|---|
| | 201 | <?php |
|---|
| | 202 | if ( ! empty($this->hybrids_ids) ) |
|---|
| | 203 | echo '<p>' . __('* This tag is also a category. It cannot be converted again.') . '</p>'; |
|---|
| | 204 | ?> |
|---|
| | 205 | |
|---|
| | 206 | <p class="submit"><input type="submit" name="submit_tags" class="button" value="<?php _e('Convert Tags to Categories'); ?>" /></p> |
|---|
| | 207 | </form> |
|---|
| | 208 | |
|---|
| | 209 | <?php |
|---|
| | 325 | function convert_tags() { |
|---|
| | 326 | global $wpdb; |
|---|
| | 327 | |
|---|
| | 328 | if ( (!isset($_POST['tags_to_convert']) || !is_array($_POST['tags_to_convert'])) && empty($this->tags_to_convert)) { |
|---|
| | 329 | echo '<div class="narrow">'; |
|---|
| | 330 | echo '<p>' . sprintf(__('Uh, oh. Something didn’t work. Please <a href="%s">try again</a>.'), 'admin.php?import=wp-cat2tag&step=3') . '</p>'; |
|---|
| | 331 | echo '</div>'; |
|---|
| | 332 | return; |
|---|
| | 333 | } |
|---|
| | 334 | |
|---|
| | 335 | if ( empty($this->categories_to_convert) ) |
|---|
| | 336 | $this->tags_to_convert = $_POST['tags_to_convert']; |
|---|
| | 337 | |
|---|
| | 338 | $clean_cache = array(); |
|---|
| | 339 | echo '<ul>'; |
|---|
| | 340 | |
|---|
| | 341 | foreach ( (array) $this->tags_to_convert as $tag_id) { |
|---|
| | 342 | $tag_id = (int) $tag_id; |
|---|
| | 343 | |
|---|
| | 344 | echo '<li>' . sprintf(__('Converting tag #%s ... '), $tag_id); |
|---|
| | 345 | |
|---|
| | 346 | if ( ! is_term($tag_id, 'post_tag') ) { |
|---|
| | 347 | _e('Tag doesn\'t exist!'); |
|---|
| | 348 | } else { |
|---|
| | 349 | |
|---|
| | 350 | if ( is_term($tag_id, 'category') ) { |
|---|
| | 351 | _e('This Tag is already a Category.'); |
|---|
| | 352 | echo '</li>'; |
|---|
| | 353 | continue; |
|---|
| | 354 | } |
|---|
| | 355 | |
|---|
| | 356 | $tt_ids = $wpdb->get_col( $wpdb->prepare("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'post_tag'", $tag_id) ); |
|---|
| | 357 | if ( $tt_ids ) { |
|---|
| | 358 | $posts = $wpdb->get_col("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN (" . join(',', $tt_ids) . ") GROUP BY object_id"); |
|---|
| | 359 | foreach ( (array) $posts as $post ) |
|---|
| | 360 | clean_post_cache($post); |
|---|
| | 361 | } |
|---|
| | 362 | |
|---|
| | 363 | // Change the tag to a category. |
|---|
| | 364 | $parent = $wpdb->get_var( $wpdb->prepare("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'post_tag'", $tag_id) ); |
|---|
| | 365 | if ( 0 == $parent || (0 < (int) $parent && $this->_category_exists($parent)) ) |
|---|
| | 366 | $reset_parent = ''; |
|---|
| | 367 | else $reset_parent = ", parent = '0'"; |
|---|
| | 368 | |
|---|
| | 369 | $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET taxonomy = 'category' $reset_parent WHERE term_id = %d AND taxonomy = 'post_tag'", $tag_id) ); |
|---|
| | 370 | |
|---|
| | 371 | // Clean the cache |
|---|
| | 372 | $clean_cache[] = $tag_id; |
|---|
| | 373 | |
|---|
| | 374 | _e('Converted successfully.'); |
|---|
| | 375 | } |
|---|
| | 376 | |
|---|
| | 377 | echo '</li>'; |
|---|
| | 378 | } |
|---|
| | 379 | |
|---|
| | 380 | clean_term_cache( $clean_cache, 'post_tag' ); |
|---|
| | 381 | delete_option('category_children'); |
|---|
| | 382 | |
|---|
| | 383 | echo '</ul>'; |
|---|
| | 384 | echo '<p>' . sprintf( __('We’re all done here, but you can always <a href="%s">convert more</a>.'), 'admin.php?import=wp-cat2tag&step=3' ) . '</p>'; |
|---|
| | 385 | } |
|---|
| | 386 | |
|---|