function convert_them() { global $wpdb; if ((!isset($_POST['cats_to_convert']) || !is_array($_POST['cats_to_convert'])) && empty($this->categories_to_convert)) { echo '<div class="narrow">'; echo '<p>' . sprintf(__('Uh, oh. Something didn’t work. Please <a href="%s">try again</a>.'), 'admin.php?import=wp-cat2tag') . '</p>'; echo '</div>'; return; } if (empty($this->categories_to_convert)) { $this->categories_to_convert = $_POST['cats_to_convert']; } $hier = _get_term_hierarchy('category'); echo '<ul>'; foreach ((array) $this->categories_to_convert as $cat_id) { $cat_id = (int) $cat_id; echo '<li>' . sprintf(__('Converting category #%s ... '), $cat_id); if (!$this->_category_exists($cat_id)) { _e('Category doesn\'t exist!'); } else { $category =& get_category($cat_id); if (tag_exists($wpdb->escape($category->name))) { _e('Category is already a tag.'); echo '</li>'; continue; } // If the category is the default, leave category in place and create tag. if (get_option('default_category') == $category->term_id) { $id = wp_insert_term($category->name, 'post_tag', array('slug' => $category->slug)); $id = $id['term_taxonomy_id']; $posts = get_objects_in_term($category->term_id, 'category'); foreach ($posts as $post) { if (!$wpdb->get_var("SELECT object_id FROM {$wpdb->term_relationships} WHERE object_id = '{$post}' AND term_taxonomy_id = '{$id}'")) { $wpdb->query("INSERT INTO {$wpdb->term_relationships} (object_id, term_taxonomy_id) VALUES ('{$post}', '{$id}')"); } clean_post_cache($post); } } else { $tt_ids = $wpdb->get_col("SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE term_id = '{$category->term_id}' AND taxonomy = 'category'"); if ($tt_ids) { $posts = $wpdb->get_col("SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id IN (" . join(',', $tt_ids) . ") GROUP BY object_id"); foreach ((array) $posts as $post) { clean_post_cache($post); } } // Change the category to a tag. $wpdb->query("UPDATE {$wpdb->term_taxonomy} SET taxonomy = 'post_tag' WHERE term_id = '{$category->term_id}' AND taxonomy = 'category'"); $terms = $wpdb->get_col("SELECT term_id FROM {$wpdb->term_taxonomy} WHERE parent = '{$category->term_id}' AND taxonomy = 'category'"); foreach ((array) $terms as $term) { clean_category_cache($term); } // Set all parents to 0 (root-level) if their parent was the converted tag $wpdb->query("UPDATE {$wpdb->term_taxonomy} SET parent = 0 WHERE parent = '{$category->term_id}' AND taxonomy = 'category'"); } // Clean the cache clean_category_cache($category->term_id); _e('Converted successfully.'); } echo '</li>'; } echo '</ul>'; echo '<p>' . sprintf(__('We’re all done here, but you can always <a href="%s">convert more</a>.'), 'admin.php?import=wp-cat2tag') . '</p>'; }
function wp_delete_category($cat_ID) { global $wpdb; $cat_ID = (int) $cat_ID; $default_cat = get_option('default_category'); $default_link_cat = get_option('default_link_category'); // Don't delete either of the default cats if ($cat_ID == $default_cat || $cat_ID == $default_link_cat) { return 0; } $category = get_category($cat_ID); $parent = $category->category_parent; // Delete the category if (!$wpdb->query("DELETE FROM {$wpdb->categories} WHERE cat_ID = '{$cat_ID}'")) { return 0; } // Update children to point to new parent $wpdb->query("UPDATE {$wpdb->categories} SET category_parent = '{$parent}' WHERE category_parent = '{$cat_ID}'"); // Only set posts and links to the default category if they're not in another category already $posts = $wpdb->get_col("SELECT post_id FROM {$wpdb->post2cat} WHERE category_id='{$cat_ID}'"); foreach ((array) $posts as $post_id) { $cats = wp_get_post_categories($post_id); if (1 == count($cats)) { $cats = array($default_cat); } else { $cats = array_diff($cats, array($cat_ID)); } wp_set_post_categories($post_id, $cats); } $links = $wpdb->get_col("SELECT link_id FROM {$wpdb->link2cat} WHERE category_id='{$cat_ID}'"); foreach ((array) $links as $link_id) { $cats = wp_get_link_cats($link_id); if (1 == count($cats)) { $cats = array($default_link_cat); } else { $cats = array_diff($cats, array($cat_ID)); } wp_set_link_cats($link_id, $cats); } clean_category_cache($cat_ID); do_action('delete_category', $cat_ID); return 1; }
function wp_set_post_categories($post_ID = 0, $post_categories = array()) { global $wpdb; $post_ID = (int) $post_ID; // If $post_categories isn't already an array, make it one: if (!is_array($post_categories) || 0 == count($post_categories) || empty($post_categories)) $post_categories = array(get_option('default_category')); $post_categories = array_unique($post_categories); // First the old categories $old_categories = $wpdb->get_col(" SELECT category_id FROM $wpdb->post2cat WHERE post_id = '$post_ID'"); if (!$old_categories) { $old_categories = array(); } else { $old_categories = array_unique($old_categories); } // Delete any? $delete_cats = array_diff($old_categories,$post_categories); if ($delete_cats) { foreach ($delete_cats as $del) { $wpdb->query(" DELETE FROM $wpdb->post2cat WHERE category_id = '$del' AND post_id = '$post_ID' "); } } // Add any? $add_cats = array_diff($post_categories, $old_categories); if ($add_cats) { foreach ($add_cats as $new_cat) { $new_cat = (int) $new_cat; if ( !empty($new_cat) ) $wpdb->query(" INSERT INTO $wpdb->post2cat (post_id, category_id) VALUES ('$post_ID', '$new_cat')"); } } // Update category counts. $all_affected_cats = array_unique(array_merge($post_categories, $old_categories)); foreach ( $all_affected_cats as $cat_id ) { $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->post2cat, $wpdb->posts WHERE $wpdb->posts.ID=$wpdb->post2cat.post_id AND post_status = 'publish' AND post_type = 'post' AND category_id = '$cat_id'"); $wpdb->query("UPDATE $wpdb->categories SET category_count = '$count' WHERE cat_ID = '$cat_id'"); clean_category_cache($cat_id); do_action('edit_category', $cat_id); } } // wp_set_post_categories()
function wp_set_post_categories($post_ID = 0, $post_categories = array()) { global $wpdb; $post_ID = (int) $post_ID; // If $post_categories isn't already an array, make it one: if (!is_array($post_categories) || 0 == count($post_categories) || empty($post_categories)) { $post_categories = array(get_option('default_category')); } $post_categories = array_unique($post_categories); // First the old categories $old_categories = $wpdb->get_col("\n\t\tSELECT category_id\n\t\tFROM {$wpdb->post2cat}\n\t\tWHERE post_id = '{$post_ID}'"); if (!$old_categories) { $old_categories = array(); } else { $old_categories = array_unique($old_categories); } // Delete any? $delete_cats = array_diff($old_categories, $post_categories); if ($delete_cats) { foreach ($delete_cats as $del) { $wpdb->query("\n\t\t\t\tDELETE FROM {$wpdb->post2cat}\n\t\t\t\tWHERE category_id = '{$del}'\n\t\t\t\t\tAND post_id = '{$post_ID}'\n\t\t\t\t"); } } // Add any? $add_cats = array_diff($post_categories, $old_categories); if ($add_cats) { foreach ($add_cats as $new_cat) { $new_cat = (int) $new_cat; if (!empty($new_cat)) { $wpdb->query("\n\t\t\t\t\tINSERT INTO {$wpdb->post2cat} (post_id, category_id) \n\t\t\t\t\tVALUES ('{$post_ID}', '{$new_cat}')"); } } } // Update category counts. $all_affected_cats = array_unique(array_merge($post_categories, $old_categories)); foreach ($all_affected_cats as $cat_id) { $count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->post2cat}, {$wpdb->posts} WHERE {$wpdb->posts}.ID={$wpdb->post2cat}.post_id AND post_status = 'publish' AND post_type = 'post' AND category_id = '{$cat_id}'"); $wpdb->query("UPDATE {$wpdb->categories} SET category_count = '{$count}' WHERE cat_ID = '{$cat_id}'"); clean_category_cache($cat_id); do_action('edit_category', $cat_id); } }