Changeset 5529

Show
Ignore:
Timestamp:
05/23/07 18:07:53 (1 year ago)
Author:
ryan
Message:

Set and get post cats to taxonomy. see #4189

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/wp-admin/admin-functions.php

    r5528 r5529  
    679679 
    680680function get_nested_categories( $default = 0, $parent = 0 ) { 
    681     global $post_ID, $link_id, $mode, $wpdb; 
     681    global $post_ID, $mode, $wpdb; 
    682682 
    683683    if ( $post_ID ) { 
    684         $checked_categories = $wpdb->get_col( " 
    685              SELECT category_id 
    686              FROM $wpdb->categories, $wpdb->post2cat 
    687              WHERE $wpdb->post2cat.category_id = cat_ID AND $wpdb->post2cat.post_id = '$post_ID' AND rel_type = 'category' 
    688              " ); 
     684        $checked_categories = wp_get_post_categories($post_ID); 
    689685 
    690686        if ( count( $checked_categories ) == 0 ) { 
     
    692688            $checked_categories[] = $default; 
    693689        } 
    694     } else if ( $link_id ) { 
    695         $checked_categories = $wpdb->get_col( " 
    696              SELECT category_id 
    697              FROM $wpdb->categories, $wpdb->link2cat 
    698              WHERE $wpdb->link2cat.category_id = cat_ID AND $wpdb->link2cat.link_id = '$link_id' 
    699              " ); 
    700  
    701         if ( count( $checked_categories ) == 0 ) { 
    702             // No selected categories, strange 
    703             $checked_categories[] = $default; 
    704         } 
    705690    } else { 
    706691        $checked_categories[] = $default; 
    707692    } 
    708693 
    709     $cats = return_categories_list( $parent); 
     694    $cats = get_categories("child_of=$parent&hide_empty=0&get=ids"); 
    710695    $result = array (); 
    711696 
    712697    if ( is_array( $cats ) ) { 
    713698        foreach ( $cats as $cat) { 
    714             $result[$cat]['children'] = get_nested_categories( $default, $cat); 
     699            //$result[$cat]['children'] = get_nested_categories( $default, $cat); 
    715700            $result[$cat]['cat_ID'] = $cat; 
    716701            $result[$cat]['checked'] = in_array( $cat, $checked_categories ); 
     
    755740    } 
    756741 
    757     $categories = get_terms('link_category', 'orderby=count'); 
     742    $categories = get_terms('link_category', 'orderby=count&hide_empty=0'); 
    758743     
    759744    if ( empty($categories) ) 
     
    773758        $categories = get_categories( 'hide_empty=0' ); 
    774759 
    775     $children = _get_category_hierarchy(); 
     760    $children = _get_term_hierarchy('category'); 
    776761 
    777762    if ( $categories ) { 
     
    960945    if ( $categories ) { 
    961946        foreach ( $categories as $category ) { 
    962             if ( $currentcat != $category->cat_ID && $parent == $category->category_parent) { 
     947            if ( $currentcat != $category->term_id && $parent == $category->parent) { 
    963948                $pad = str_repeat( '– ', $level ); 
    964                 $category->cat_name = wp_specialchars( $category->cat_name ); 
    965                 echo "\n\t<option value='$category->cat_ID'"; 
    966                 if ( $currentparent == $category->cat_ID
     949                $category->name = wp_specialchars( $category->name ); 
     950                echo "\n\t<option value='$category->term_id'"; 
     951                if ( $currentparent == $category->term_id
    967952                    echo " selected='selected'"; 
    968                 echo ">$pad$category->cat_name</option>"; 
    969                 wp_dropdown_cats( $currentcat, $currentparent, $category->cat_ID, $level +1, $categories ); 
     953                echo ">$pad$category->name</option>"; 
     954                wp_dropdown_cats( $currentcat, $currentparent, $category->term_id, $level +1, $categories ); 
    970955            } 
    971956        } 
  • trunk/wp-admin/edit-category-form.php

    r4941 r5529  
    2222<?php echo $form ?> 
    2323<input type="hidden" name="action" value="<?php echo $action ?>" /> 
    24 <input type="hidden" name="cat_ID" value="<?php echo $category->cat_ID ?>" /> 
     24<input type="hidden" name="cat_ID" value="<?php echo $category->term_id ?>" /> 
    2525<?php wp_nonce_field($nonce_action); ?> 
    2626    <table class="editform" width="100%" cellspacing="2" cellpadding="5"> 
    2727        <tr> 
    2828            <th width="33%" scope="row" valign="top"><label for="cat_name"><?php _e('Category name:') ?></label></th> 
    29             <td width="67%"><input name="cat_name" id="cat_name" type="text" value="<?php echo attribute_escape($category->cat_name); ?>" size="40" /></td> 
     29            <td width="67%"><input name="cat_name" id="cat_name" type="text" value="<?php echo attribute_escape($category->name); ?>" size="40" /></td> 
    3030        </tr> 
    3131        <tr> 
    3232            <th scope="row" valign="top"><label for="category_nicename"><?php _e('Category slug:') ?></label></th> 
    33             <td><input name="category_nicename" id="category_nicename" type="text" value="<?php echo attribute_escape($category->category_nicename); ?>" size="40" /></td> 
     33            <td><input name="category_nicename" id="category_nicename" type="text" value="<?php echo attribute_escape($category->slug); ?>" size="40" /></td> 
    3434        </tr> 
    3535        <tr> 
    3636            <th scope="row" valign="top"><label for="category_parent"><?php _e('Category parent:') ?></label></th> 
    3737            <td>         
    38                 <?php wp_dropdown_categories('hide_empty=0&name=category_parent&orderby=name&selected=' . $category->category_parent . '&hierarchical=1&show_option_none=' . __('None')); ?> 
     38                <?php wp_dropdown_categories('hide_empty=0&name=category_parent&orderby=name&selected=' . $category->parent . '&hierarchical=1&show_option_none=' . __('None')); ?> 
    3939            </td> 
    4040        </tr> 
    4141        <tr> 
    4242            <th scope="row" valign="top"><label for="category_description"><?php _e('Description: (optional)') ?></label></th> 
    43             <td><textarea name="category_description" id="category_description" rows="5" cols="50" style="width: 97%;"><?php echo wp_specialchars($category->category_description); ?></textarea></td> 
     43            <td><textarea name="category_description" id="category_description" rows="5" cols="50" style="width: 97%;"><?php echo wp_specialchars($category->description); ?></textarea></td> 
    4444        </tr> 
    4545    </table> 
  • trunk/wp-includes/category-template.php

    r5521 r5529  
    1313 
    1414        $category = get_category($cat_id); 
    15         if ( $category->category_parent == $id ) { 
    16             $chain .= $before.$category->cat_ID.$after; 
    17             $chain .= get_category_children($category->cat_ID, $before, $after); 
     15        if ( $category->parent == $id ) { 
     16            $chain .= $before.$category->term_id.$after; 
     17            $chain .= get_category_children($category->term_id, $before, $after); 
    1818        } 
    1919    } 
     
    3030    } else { 
    3131        $category = &get_category($category_id); 
    32         $category_nicename = $category->category_nicename
    33  
    34         if ( $parent = $category->category_parent ) 
     32        $category_nicename = $category->slug
     33 
     34        if ( $parent = $category->parent ) 
    3535            $category_nicename = get_category_parents($parent, false, '/', true) . $category_nicename; 
    3636 
     
    4646 
    4747    if ( $nicename ) 
    48         $name = $parent->category_nicename
     48        $name = $parent->slug
    4949    else 
    5050        $name = $parent->cat_name; 
    5151 
    52     if ( $parent->category_parent && ($parent->category_parent != $parent->cat_ID) ) 
    53         $chain .= get_category_parents($parent->category_parent, $link, $separator, $nicename); 
     52    if ( $parent->parent && ($parent->parent != $parent->term_id) ) 
     53        $chain .= get_category_parents($parent->parent, $link, $separator, $nicename); 
    5454 
    5555    if ( $link ) 
    56         $chain .= '<a href="' . get_category_link($parent->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $parent->cat_name) . '">'.$name.'</a>' . $separator; 
     56        $chain .= '<a href="' . get_category_link($parent->term_id) . '" title="' . sprintf(__("View all posts in %s"), $parent->cat_name) . '">'.$name.'</a>' . $separator; 
    5757    else 
    5858        $chain .= $name.$separator; 
     
    6161 
    6262function get_the_category($id = false) { 
    63 global $post, $category_cache, $blog_id; 
     63   global $post, $category_cache, $blog_id; 
    6464 
    6565    $id = (int) $id; 
     
    6767        $id = (int) $post->ID; 
    6868 
    69     if ( !isset($category_cache[$blog_id][$id]) ) 
    70         update_post_category_cache($id); 
    71  
    72     $categories = $category_cache[$blog_id][$id]; 
     69    $categories = get_object_terms($id, 'category'); 
    7370 
    7471    if ( !empty($categories) ) 
     
    8784    $cat_ID = (int) $cat_ID; 
    8885    $category = &get_category($cat_ID); 
    89     return $category->cat_name; 
     86    return $category->name; 
    9087} 
    9188 
     
    105102            switch ( strtolower($parents) ) { 
    106103                case 'multiple': 
    107                     if ($category->category_parent) 
    108                         $thelist .= get_category_parents($category->category_parent, TRUE); 
    109                     $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" ' . $rel . '>' . $category->cat_name.'</a></li>'; 
     104                    if ($category->parent) 
     105                        $thelist .= get_category_parents($category->parent, TRUE); 
     106                    $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>' . $category->name.'</a></li>'; 
    110107                    break; 
    111108                case 'single': 
    112                     $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" ' . $rel . '>'; 
    113                     if ($category->category_parent) 
    114                         $thelist .= get_category_parents($category->category_parent, FALSE); 
    115                     $thelist .= $category->cat_name.'</a></li>'; 
     109                    $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>'; 
     110                    if ($category->parent) 
     111                        $thelist .= get_category_parents($category->parent, FALSE); 
     112                    $thelist .= $category->name.'</a></li>'; 
    116113                    break; 
    117114                case '': 
    118115                default: 
    119                     $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" ' . $rel . '>' . $category->cat_name.'</a></li>'; 
     116                    $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>' . $category->cat_name.'</a></li>'; 
    120117            } 
    121118        } 
     
    128125            switch ( strtolower($parents) ) { 
    129126                case 'multiple': 
    130                     if ( $category->category_parent ) 
    131                         $thelist .= get_category_parents($category->category_parent, TRUE); 
    132                     $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" ' . $rel . '>' . $category->cat_name.'</a>'; 
     127                    if ( $category->parent ) 
     128                        $thelist .= get_category_parents($category->parent, TRUE); 
     129                    $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>' . $category->cat_name.'</a>'; 
    133130                    break; 
    134131                case 'single': 
    135                     $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" ' . $rel . '>'; 
    136                     if ( $category->category_parent ) 
    137                         $thelist .= get_category_parents($category->category_parent, FALSE); 
     132                    $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>'; 
     133                    if ( $category->parent ) 
     134                        $thelist .= get_category_parents($category->parent, FALSE); 
    138135                    $thelist .= "$category->cat_name</a>"; 
    139136                    break; 
    140137                case '': 
    141138                default: 
    142                     $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" ' . $rel . '>' . $category->cat_name.'</a>'; 
     139                    $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>' . $category->name.'</a>'; 
    143140            } 
    144141            ++$i; 
     
    166163        $category = $cat; 
    167164    $category = & get_category($category); 
    168     return apply_filters('category_description', $category->category_description, $category->cat_ID); 
     165    return apply_filters('category_description', $category->description, $category->term_id); 
    169166} 
    170167 
  • trunk/wp-includes/category.php

    r5528 r5529  
    103103}  
    104104 
    105 // 
    106 // Private 
    107 // 
    108  
    109 function &_get_cat_children($category_id, $categories) { 
    110     if ( empty($categories) ) 
    111         return array(); 
    112  
    113     $category_list = array(); 
    114     $has_children = _get_category_hierarchy(); 
    115  
    116     if  ( ( 0 != $category_id ) && ! isset($has_children[$category_id]) ) 
    117         return array(); 
    118  
    119     foreach ( $categories as $category ) { 
    120         if ( $category->cat_ID == $category_id ) 
    121             continue; 
    122  
    123         if ( $category->category_parent == $category_id ) { 
    124             $category_list[] = $category; 
    125  
    126             if ( !isset($has_children[$category->cat_ID]) ) 
    127                 continue; 
    128  
    129             if ( $children = _get_cat_children($category->cat_ID, $categories) ) 
    130                 $category_list = array_merge($category_list, $children); 
    131         } 
    132     } 
    133  
    134     return $category_list; 
    135 } 
    136  
    137 // Recalculates link or post counts by including items from child categories 
    138 // Assumes all relevant children are already in the $categories argument 
    139 function _pad_category_counts($type, &$categories) { 
    140     global $wpdb; 
    141  
    142     // Set up some useful arrays 
    143     foreach ( $categories as $key => $cat ) { 
    144         $cats[$cat->cat_ID] = & $categories[$key]; 
    145         $cat_IDs[] = $cat->cat_ID; 
    146     } 
    147  
    148     // Get the relevant post2cat or link2cat records and stick them in a lookup table 
    149     if ( $type == 'post' ) { 
    150         $results = $wpdb->get_results("SELECT post_id, category_id FROM $wpdb->post2cat LEFT JOIN $wpdb->posts ON post_id = ID WHERE category_id IN (".join(',', $cat_IDs).") AND post_type = 'post' AND post_status = 'publish'"); 
    151         foreach ( $results as $row ) 
    152             ++$cat_items[$row->category_id][$row->post_id]; 
    153     } else { 
    154         $results = $wpdb->get_results("SELECT $wpdb->link2cat.link_id, category_id FROM $wpdb->link2cat LEFT JOIN $wpdb->links USING (link_id) WHERE category_id IN (".join(',', $cat_IDs).") AND link_visible = 'Y'"); 
    155         foreach ( $results as $row ) 
    156             ++$cat_items[$row->category_id][$row->link_id]; 
    157     } 
    158  
    159     // Touch every ancestor's lookup row for each post in each category 
    160     foreach ( $cat_IDs as $cat_ID ) { 
    161         $child = $cat_ID; 
    162         while ( $parent = $cats[$child]->category_parent ) { 
    163             if ( !empty($cat_items[$cat_ID]) ) 
    164                 foreach ( $cat_items[$cat_ID] as $item_id => $touches ) 
    165                     ++$cat_items[$parent][$item_id]; 
    166             $child = $parent; 
    167         } 
    168     } 
    169  
    170     // Transfer the touched cells  
    171     foreach ( (array) $cat_items as $id => $items ) 
    172         if ( isset($cats[$id]) ) 
    173             $cats[$id]->{'link' == $type ? 'link_count' : 'category_count'} = count($items); 
    174 } 
    175  
    176 function _get_category_hierarchy() { 
    177     return _get_term_hierarchy('category'); 
    178 } 
    179  
    180105// Tags 
    181106 
  • trunk/wp-includes/post.php

    r5515 r5529  
    448448    $post_id = (int) $post_id; 
    449449 
    450     $cats = &get_the_category($post_id); 
    451     $cat_ids = array(); 
    452     foreach ( $cats as $cat ) 
    453         $cat_ids[] = (int) $cat->cat_ID; 
    454     return array_unique($cat_ids); 
     450    $cats = get_object_terms($post_id, 'category', 'get=ids'); 
     451    return $cats; 
    455452} 
    456453 
     
    806803        $post_categories = array(get_option('default_category')); 
    807804 
     805    $post_categories = array_map('intval', $post_categories); 
    808806    $post_categories = array_unique($post_categories); 
    809807 
    810     // First the old categories 
    811     $old_categories = $wpdb->get_col(" 
    812         SELECT category_id 
    813         FROM $wpdb->post2cat 
    814         WHERE post_id = '$post_ID' AND rel_type = 'category'"); 
    815  
    816     if (!$old_categories) { 
    817         $old_categories = array(); 
    818     } else { 
    819         $old_categories = array_unique($old_categories); 
    820     } 
    821  
    822     // Delete any? 
    823     $delete_cats = array_diff($old_categories,$post_categories); 
    824  
    825     if ($delete_cats) { 
    826         foreach ($delete_cats as $del) { 
    827             $wpdb->query(" 
    828                 DELETE FROM $wpdb->post2cat 
    829                 WHERE category_id = '$del' 
    830                     AND post_id = '$post_ID' AND rel_type = 'category' 
    831                 "); 
    832         } 
    833     } 
    834  
    835     // Add any? 
    836     $add_cats = array_diff($post_categories, $old_categories); 
    837  
    838     if ($add_cats) { 
    839         foreach ($add_cats as $new_cat) { 
    840             $new_cat = (int) $new_cat; 
    841             if ( !empty($new_cat) ) 
    842                 $wpdb->query(" 
    843                     INSERT INTO $wpdb->post2cat (post_id, category_id)  
    844                     VALUES ('$post_ID', '$new_cat')"); 
    845         } 
    846     } 
    847  
    848     // Update category counts. 
    849     $all_affected_cats = array_unique(array_merge($post_categories, $old_categories)); 
    850     foreach ( $all_affected_cats as $cat_id ) { 
    851         $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' AND rel_type = 'category'"); 
    852         $wpdb->query("UPDATE $wpdb->categories SET category_count = '$count', type = type | " . TAXONOMY_CATEGORY . " WHERE cat_ID = '$cat_id'"); 
    853         clean_category_cache($cat_id); 
    854         do_action('edit_category', $cat_id); 
    855     } 
     808    return wp_set_object_terms($post_ID, $post_categories, 'category'); 
    856809}   // wp_set_post_categories() 
    857810 
  • trunk/wp-includes/taxonomy.php

    r5528 r5529  
    225225        $in_taxonomies = "'" . implode("', '", $taxonomies) . "'"; 
    226226        $old_terms = $wpdb->get_col("SELECT tr.term_taxonomy_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($in_taxonomies) AND tr.object_id = '$object_id'"); 
     227        if ( empty($old_terms) ) 
     228            $old_terms = array(); 
    227229    } 
    228230 
     
    263265    global $wpdb; 
    264266    $taxonomies = ($single_taxonomy = !is_array($taxonomy)) ? array($taxonomy) : $taxonomy; 
     267    // TODO cast to int 
    265268    $object_ids = ($single_object = !is_array($object_id)) ? array($object_id) : $object_id; 
    266269