Make WordPress Core


Ignore:
Timestamp:
05/26/2007 06:54:16 PM (15 years ago)
Author:
ryan
Message:

Update cat2tag converter. Some term API tweaks. see #4189

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/import/wp-cat2tag.php

    r5390 r5553  
    1717        global $wpdb;
    1818       
    19         $this->all_categories = $wpdb->get_results("SELECT * FROM $wpdb->categories WHERE (type & ~ " . TAXONOMY_TAG . ") != 0 AND category_count > 0 ORDER BY cat_name ASC");
     19        $this->all_categories = get_categories('get=all');
    2020    }
    2121   
     
    4141        print '<ul style="list-style:none">';
    4242       
    43         $hier = _get_category_hierarchy();
     43        $hier = _get_term_hierarchy('category');
    4444       
    4545        foreach ($this->all_categories as $category) {
    46             if ((int) $category->category_parent == 0) {
    47                 print '<li><label><input type="checkbox" name="cats_to_convert[]" value="' . intval($category->cat_ID) . '" /> ' . $category->cat_name . ' (' . $category->category_count . ')</label>';
    48                
    49                 if (isset($hier[$category->cat_ID])) {
     46            if ((int) $category->parent == 0) {
     47                print '<li><label><input type="checkbox" name="cats_to_convert[]" value="' . intval($category->term_id) . '" /> ' . $category->name . ' (' . $category->count . ')</label>';
     48               
     49                if (isset($hier[$category->term_id])) {
    5050                    $this->_category_children($category, $hier);
    5151                }
     
    6464        print '<ul style="list-style:none">';
    6565       
    66         foreach ($hier[$parent->cat_ID] as $child_id) {
     66        foreach ($hier[$parent->term_id] as $child_id) {
    6767            $child =& get_category($child_id);
    6868           
    69             print '<li><label><input type="checkbox" name="cats_to_convert[]" value="' . intval($child->cat_ID) . '" /> ' . $child->cat_name . ' (' . $child->category_count . ')</label>';
    70            
    71             if (isset($hier[$child->cat_ID])) {
     69            print '<li><label><input type="checkbox" name="cats_to_convert[]" value="' . intval($child->term_id) . '" /> ' . $child->name . ' (' . $child->count . ')</label>';
     70           
     71            if (isset($hier[$child->term_id])) {
    7272                $this->_category_children($child, $hier);
    7373            }
     
    8484        $cat_id = (int) $cat_id;
    8585       
    86         $maybe_exists = $wpdb->get_results("SELECT cat_ID from $wpdb->categories WHERE cat_ID = '$cat_id'");
    87        
    88         if (count($maybe_exists) > 0) {
     86        $maybe_exists = category_exists($cat_id);
     87       
     88        if ( $maybe_exists ) {
    8989            return true;
    9090        } else {
     
    102102        }
    103103       
    104         $this->categories_to_convert = $_POST['cats_to_convert'];
    105         $hier = _get_category_hierarchy();
     104       
     105        if ( empty($this->categories_to_convert) )
     106            $this->categories_to_convert = $_POST['cats_to_convert'];
     107        $hier = _get_term_hierarchy('category');
    106108       
    107109        print '<ul>';
     
    117119                $category =& get_category($cat_id);
    118120               
    119                 if ($category->link_count > 0) {
    120                     $type = $category->type | TAXONOMY_TAG;
    121                 } else {
    122                     $type = TAXONOMY_TAG;
    123                 }
    124                
    125121                // Set the category itself to $type from above
    126                 $wpdb->query("UPDATE $wpdb->categories SET type = '$type' WHERE cat_ID = '{$category->cat_ID}'");
    127                
    128                 // Set relationships in post2cat to 'tag', category_count becomes tag_count
    129                 $wpdb->query("UPDATE $wpdb->post2cat SET rel_type = 'tag' WHERE category_ID = '{$category->cat_ID}'");
    130                 $wpdb->query("UPDATE $wpdb->categories SET tag_count = '{$category->category_count}', category_count = '0' WHERE cat_ID = '{$category->cat_ID}'");
     122                $wpdb->query("UPDATE $wpdb->term_taxonomy SET taxonomy = '$type' WHERE term_id = '{$category->term_id}' AND taxonomy = 'category'");
    131123               
    132124                // Set all parents to 0 (root-level) if their parent was the converted tag
    133                 $wpdb->query("UPDATE $wpdb->categories SET category_parent = 0 WHERE category_parent = '{$category->cat_ID}'");
     125                $wpdb->query("UPDATE $wpdb->term_taxonomy SET parent = 0 WHERE parent = '{$category->term_id}' AND taxonomy = 'category'");
    134126               
    135127                // Clean the cache
    136                 clean_category_cache($category->cat_ID);
     128                clean_category_cache($category->term_id);
    137129               
    138130                _e('Converted successfully.');
     
    161153    function convert_all() {
    162154        global $wpdb;
    163        
    164         $cats = $wpdb->get_results("SELECT * FROM $wpdb->categories WHERE (type & ~ " . TAXONOMY_TAG . ") != 0 AND category_count > 0");
    165        
    166         $_POST['cats_to_convert'] = array();
    167        
    168         foreach ($cats as $cat) {
    169             $_POST['cats_to_convert'][] = $cat->cat_ID;
    170         }
    171        
    172         $this->convert_them();
     155
     156        $wpdb->query("UPDATE $wpdb->term_taxonomy SET taxonomy = '$type', parent = 0 WHERE taxonomy = 'category'");
     157        clean_category_cache($category->term_id);
    173158    }
    174159   
Note: See TracChangeset for help on using the changeset viewer.