| 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 | | ?> |
|---|
| | 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 } ?> |
|---|
| 216 | | <?php |
|---|
| 217 | | foreach ($hier[$parent->term_id] as $child_id) { |
|---|
| 218 | | $child =& get_category($child_id); |
|---|
| 219 | | |
|---|
| 220 | | if ( in_array( intval($child->term_id), $this->hybrids_ids ) ) { |
|---|
| 221 | | ?> |
|---|
| 222 | | <li style="color:#777;padding-left:1.3em;"><?php echo $child->name . ' (' . $child->count . ') *'; ?></li> |
|---|
| 223 | | <?php |
|---|
| 224 | | } else { |
|---|
| 225 | | ?> |
|---|
| 226 | | <li><label><input type="checkbox" name="cats_to_convert[]" value="<?php echo intval($child->term_id); ?>" /> <?php echo $child->name . ' (' . $child->count . ')'; ?></label> |
|---|
| 227 | | <?php |
|---|
| | 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>'; |
|---|
| 277 | | |
|---|
| 278 | | if ( tag_exists($wpdb->escape($category->name)) ) { |
|---|
| 279 | | _e('Category is already a tag.'); |
|---|
| 280 | | echo '</li>'; |
|---|
| | 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"; |
|---|
| 283 | | |
|---|
| 284 | | // If the category is the default, leave category in place and create tag. |
|---|
| 285 | | if ( get_option('default_category') == $category->term_id ) { |
|---|
| 286 | | $id = wp_insert_term($category->name, 'post_tag', array('slug' => $category->slug)); |
|---|
| 287 | | $id = $id['term_taxonomy_id']; |
|---|
| 288 | | $posts = get_objects_in_term($category->term_id, 'category'); |
|---|
| 289 | | foreach ( $posts as $post ) { |
|---|
| 290 | | if ( !$wpdb->get_var( $wpdb->prepare("SELECT object_id FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id = %d", $post, $id) ) ) |
|---|
| 291 | | $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES (%d, %d)", $post, $id) ); |
|---|
| | 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 ) |
|---|
| 293 | | } |
|---|
| 294 | | } else { |
|---|
| 295 | | $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) ); |
|---|
| 296 | | if ( $tt_ids ) { |
|---|
| 297 | | $posts = $wpdb->get_col("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN (" . join(',', $tt_ids) . ") GROUP BY object_id"); |
|---|
| 298 | | foreach ( (array) $posts as $post ) |
|---|
| 299 | | clean_post_cache($post); |
|---|
| 300 | | } |
|---|
| 301 | | |
|---|
| 302 | | // Change the category to a tag. |
|---|
| 303 | | $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET taxonomy = 'post_tag' WHERE term_id = %d AND taxonomy = 'category'", $category->term_id) ); |
|---|
| 304 | | |
|---|
| 305 | | $terms = $wpdb->get_col( $wpdb->prepare("SELECT term_id FROM $wpdb->term_taxonomy WHERE parent = %d AND taxonomy = 'category'", $category->term_id) ); |
|---|
| 306 | | foreach ( (array) $terms as $term ) |
|---|
| 307 | | clean_category_cache($term); |
|---|
| 308 | | |
|---|
| 309 | | // Set all parents to 0 (root-level) if their parent was the converted tag |
|---|
| 310 | | $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET parent = 0 WHERE parent = %d AND taxonomy = 'category'", $category->term_id) ); |
|---|
| 312 | | // Clean the cache |
|---|
| 313 | | clean_category_cache($category->term_id); |
|---|
| 314 | | |
|---|
| 315 | | _e('Converted successfully.'); |
|---|
| | 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"; |
|---|
| | 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>'; |
|---|
| 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>'; |
|---|
| | 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 | |
|---|
| 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.'); |
|---|
| | 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 ); |
|---|
| 376 | | |
|---|
| 377 | | echo '</li>'; |
|---|
| 378 | | } |
|---|
| 379 | | |
|---|
| 380 | | clean_term_cache( $clean_cache, 'post_tag' ); |
|---|
| 381 | | delete_option('category_children'); |
|---|
| 382 | | |
|---|
| | 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 | |
|---|