WordPress.org

Make WordPress Core


Ignore:
Timestamp:
05/23/2007 10:32:33 AM (14 years ago)
Author:
ryan
Message:

Add get_term_by() and taxonomy registration bits. Move more category stuff to taxonomy. see #4189

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/category.php

    r5521 r5525  
    88
    99    if ( ! $cat_ids = wp_cache_get('all_category_ids', 'category') ) {
    10         $cat_ids = $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories");
     10        $cat_ids = get_terms('category', 'get=ids&hierarchical=0&hide_empty=0');
    1111        wp_cache_add('all_category_ids', $cat_ids, 'category');
    1212    }
     
    1616
    1717function &get_categories($args = '') {
    18     global $wpdb, $category_links;
    19    
    20     $defaults = array(
    21         'type' => 'post', 'child_of' => 0,
    22         'orderby' => 'name', 'order' => 'ASC',
    23         'hide_empty' => true, 'include_last_update_time' => false,
    24         'hierarchical' => 1, 'exclude' => '',
    25         'include' => '', 'number' => '',
    26         'pad_counts' => false
    27     );
    28    
    29     $r = wp_parse_args( $args, $defaults );
    30    
    31     if ( $r['orderby'] == 'count' ) {
    32         $r['orderby'] = 'category_count';
    33     } else {
    34         $r['orderby'] = 'cat_' . $r['orderby'];
    35     }
    36    
    37     extract( $r );
    38 
    39     $key = md5( serialize( $r ) );
    40     if ( $cache = wp_cache_get( 'get_categories', 'category' ) )
    41         if ( isset( $cache[ $key ] ) )
    42             return apply_filters('get_categories', $cache[$key], $r);
    43 
    44     $where = 'cat_ID > 0';
    45     $inclusions = '';
    46     if ( !empty($include) ) {
    47         $child_of = 0; //ignore child_of and exclude params if using include
    48         $exclude = '';
    49         $incategories = preg_split('/[\s,]+/',$include);
    50         if ( count($incategories) ) {
    51             foreach ( $incategories as $incat ) {
    52                 if (empty($inclusions))
    53                     $inclusions = ' AND ( cat_ID = ' . intval($incat) . ' ';
    54                 else
    55                     $inclusions .= ' OR cat_ID = ' . intval($incat) . ' ';
    56             }
    57         }
    58     }
    59     if (!empty($inclusions))
    60         $inclusions .= ')';
    61     $where .= $inclusions;
    62 
    63     $exclusions = '';
    64     if ( !empty($exclude) ) {
    65         $excategories = preg_split('/[\s,]+/',$exclude);
    66         if ( count($excategories) ) {
    67             foreach ( $excategories as $excat ) {
    68                 if (empty($exclusions))
    69                     $exclusions = ' AND ( cat_ID <> ' . intval($excat) . ' ';
    70                 else
    71                     $exclusions .= ' AND cat_ID <> ' . intval($excat) . ' ';
    72                 // TODO: Exclude children of excluded cats?   Note: children are getting excluded
    73             }
    74         }
    75     }
    76     if (!empty($exclusions))
    77         $exclusions .= ')';
    78     $exclusions = apply_filters('list_cats_exclusions', $exclusions, $r );
    79     $where .= $exclusions;
    80 
    81     if ( $hide_empty && !$hierarchical ) {
    82         if ( 'link' == $type )
    83             $where .= ' AND link_count > 0';
    84         else
    85             $where .= ' AND category_count > 0';
    86     } else {
    87         $where .= ' AND ( type & ' . TAXONOMY_CATEGORY . ' != 0 ) ';
    88     }
    89 
    90    
    91 
    92     if ( !empty($number) )
    93         $number = 'LIMIT ' . $number;
    94     else
    95         $number = '';
    96 
    97     $categories = $wpdb->get_results("SELECT * FROM $wpdb->categories WHERE $where ORDER BY $orderby $order $number");
    98 
    99     if ( empty($categories) )
    100         return array();
    101 
    102     // TODO: Integrate this into the main query.
    103     if ( $include_last_update_time ) {
    104         $stamps = $wpdb->get_results("SELECT category_id, UNIX_TIMESTAMP( MAX(post_date) ) AS ts FROM $wpdb->posts, $wpdb->post2cat, $wpdb->categories
    105                             WHERE post_status = 'publish' AND post_id = ID AND $where GROUP BY category_id");
    106         global $cat_stamps;
    107         foreach ($stamps as $stamp)
    108             $cat_stamps[$stamp->category_id] = $stamp->ts;
    109         function stamp_cat($cat) {
    110             global $cat_stamps;
    111             $cat->last_update_timestamp = $cat_stamps[$cat->cat_ID];
    112             return $cat;
    113         }
    114         $categories = array_map('stamp_cat', $categories);
    115         unset($cat_stamps);
    116     }
    117 
    118     if ( $child_of || $hierarchical ) {
    119         $children = _get_category_hierarchy();
    120         if ( ! empty($children) )
    121             $categories = & _get_cat_children($child_of, $categories);
    122     }
    123 
    124     // Update category counts to include children.
    125     if ( $pad_counts )
    126         _pad_category_counts($type, $categories);
    127 
    128     // Make sure we show empty categories that have children.
    129     if ( $hierarchical && $hide_empty ) {
    130         foreach ( $categories as $k => $category ) {
    131             if ( ! $category->{'link' == $type ? 'link_count' : 'category_count'} ) {
    132                 $children = _get_cat_children($category->cat_ID, $categories);
    133                 foreach ( $children as $child )
    134                     if ( $child->{'link' == $type ? 'link_count' : 'category_count'} )
    135                         continue 2;
    136 
    137                 // It really is empty
    138                 unset($categories[$k]);
    139             }
    140         }
    141     }
    142     reset ( $categories );
    143 
    144     $cache[ $key ] = $categories;
    145     wp_cache_add( 'get_categories', $cache, 'category' );
    146 
    147     $categories = apply_filters('get_categories', $categories, $r);
    148     return $categories;
     18    // TODO Add back compat fields into each object.
     19    return get_terms('category', $args);
    14920}
    15021
     
    15223// Handles category caching.
    15324function &get_category(&$category, $output = OBJECT) {
    154     global $wpdb;
    155 
    156     if ( empty($category) )
    157         return null;
    158 
    159     if ( is_object($category) ) {
    160         wp_cache_add($category->cat_ID, $category, 'category');
    161         $_category = $category;
    162     } else {
    163         $category = (int) $category;
    164         if ( ! $_category = wp_cache_get($category, 'category') ) {
    165             $_category = $wpdb->get_row("SELECT * FROM $wpdb->categories WHERE cat_ID = '$category' LIMIT 1");
    166             wp_cache_add($category, $_category, 'category');
    167         }
    168     }
    169 
    170     $_category = apply_filters('get_category', $_category);
    171 
    172     if ( $output == OBJECT ) {
    173         return $_category;
    174     } elseif ( $output == ARRAY_A ) {
    175         return get_object_vars($_category);
    176     } elseif ( $output == ARRAY_N ) {
    177         return array_values(get_object_vars($_category));
    178     } else {
    179         return $_category;
    180     }
     25    return get_term($category, 'category', $output);
    18126}
    18227
     
    19338        $full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title($pathdir);
    19439
    195     $categories = $wpdb->get_results("SELECT cat_ID, category_nicename, category_parent FROM $wpdb->categories WHERE category_nicename = '$leaf_path'");
     40    $categories = get_terms('category', "slug=$leaf_path");
    19641
    19742    if ( empty($categories) )
     
    20146        $path = '/' . $leaf_path;
    20247        $curcategory = $category;
    203         while ( ($curcategory->category_parent != 0) && ($curcategory->category_parent != $curcategory->cat_ID) ) {
    204             $curcategory = $wpdb->get_row("SELECT cat_ID, category_nicename, category_parent FROM $wpdb->categories WHERE cat_ID = '$curcategory->category_parent'");
    205             $path = '/' . $curcategory->category_nicename . $path;
     48        while ( ($curcategory->parent != 0) && ($curcategory->parent != $curcategory->term_id) ) {
     49            $curcategory = get_term($curcategory->parent);
     50            $path = '/' . $curcategory->slug . $path;
    20651        }
    20752
    20853        if ( $path == $full_path )
    209             return get_category($category->cat_ID, $output);
     54            return get_category($category->term_id, $output);
    21055    }
    21156
    21257    // If full matching is not required, return the first cat that matches the leaf.
    21358    if ( ! $full_match )
    214         return get_category($categories[0]->cat_ID, $output);
     59        return get_category($categories[0]->term_id, $output);
    21560
    21661    return NULL;
     
    21863
    21964function get_category_by_slug( $slug  ) {
    220     global $wpdb;
    221     $slug = sanitize_title( $slug );
    222     if ( empty( $slug ) )
    223         return false;
    224     $category = $wpdb->get_var( "SELECT * FROM $wpdb->categories WHERE category_nicename = '$slug' " );
    225     return get_category( $category );
     65    return get_term_by('slug', $slug, 'category');
    22666}
    22767
     
    23070    global $wpdb;
    23171
    232     $cid = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE cat_name='$cat_name'");
    233 
    234     return $cid?$cid:1; // default to cat 1
     72    $cat = get_term_by('name', $cat_name, 'category');
     73    if ($cat)
     74        return $cat->term_id;
     75    return 0;
    23576}
    23677
     
    25394        $cat2 = & get_category($cat2);
    25495
    255     if ( !$cat1->cat_ID || !$cat2->category_parent )
     96    if ( !$cat1->term_id || !$cat2->parent )
    25697        return false;
    25798
    258     if ( $cat2->category_parent == $cat1->cat_ID )
     99    if ( $cat2->parent == $cat1->term_id )
    259100        return true;
    260101
    261     return cat_is_ancestor_of($cat1, get_category($cat2->parent_category));
     102    return cat_is_ancestor_of($cat1, get_category($cat2->parent));
    262103}
    263104
Note: See TracChangeset for help on using the changeset viewer.