Make WordPress Core

Changeset 5525


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

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

Location:
trunk/wp-includes
Files:
2 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
  • trunk/wp-includes/taxonomy.php

    r5524 r5525  
    11<?php
     2
     3$wp_taxonomies =
     4array('category' => array('object_type' => 'post', 'hierarchical' => true),
     5'post_tag' => array('object_type' => 'post', 'hierarchical' => false),
     6'link_category' => array('object_type' => 'link', 'hierarchical' => false));
     7
     8function is_taxonomy( $taxonomy ) {
     9        global $wp_taxonomies;
     10
     11        return isset($wp_taxonomies[$taxonomy]);       
     12}
     13
     14function get_taxonomy( $taxonomy ) {
     15        global $wp_taxonomies;
     16
     17        if ( ! is_taxonomy($taxonomy) )
     18                return false;
     19
     20        return $wp_taxonomies[$taxonomy];
     21}
     22
     23function register_taxonomy( $taxonomy, $object_type, $args = array() ) {
     24        global $wp_taxonomies;
     25
     26        $args['object_type'] = $object_type;
     27        $wp_taxonomies[$taxonomy] = $args;
     28}
    229
    330/**
     
    936function wp_insert_term( $term, $taxonomy, $args = array() ) {
    1037        global $wpdb;
     38
     39        if ( ! is_taxonomy($taxonomy) )
     40                return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
    1141
    1242        $update = false;
     
    258288        global $wpdb;
    259289
    260         if ( !is_array($taxonomies) )
     290        $single_taxonomy = false;
     291        if ( !is_array($taxonomies) ) {
     292                $single_taxonomy = true;
    261293                $taxonomies = array($taxonomies);
     294        }
     295
    262296        $in_taxonomies = "'" . implode("', '", $taxonomies) . "'";
    263297
    264298        $defaults = array('orderby' => 'name', 'order' => 'ASC',
    265299                'hide_empty' => true, 'exclude' => '', 'include' => '',
    266                 'number' => '', 'get' => 'everything');
     300                'number' => '', 'get' => 'everything', 'slug' => '', 'parent' => '',
     301                'hierarchical' => true, 'child_of' => 0);
    267302        $args = wp_parse_args( $args, $defaults );
    268303        $args['number'] = (int) $args['number'];
     304        if ( ! $single_taxonomy ) {
     305                $args['child_of'] = 0;
     306                $args['hierarchical'] = false;
     307        } else {
     308                $tax = get_taxonomy($taxonomy);
     309                if ( !$tax['hierarchical'] ) {
     310                        $args['child_of'] = 0;
     311                        $args['hierarchical'] = false; 
     312                }
     313        }
    269314        extract($args);
     315
     316        $key = md5( serialize( $args ) . serialize( $taxonomies ) );
     317        if ( $cache = wp_cache_get( 'get_terms', 'terms' ) ) {
     318                if ( isset( $cache[ $key ] ) )
     319                        return apply_filters('get_terms', $cache[$key], $taxonomies, $args);
     320        }
    270321
    271322        if ( 'count' == $orderby )
     
    273324        else if ( 'name' == $orderby )
    274325                $orderby = 't.name';
     326        else
     327                $orderby = 't.term_id';
    275328
    276329        $where = '';
     
    311364        $where .= $exclusions;
    312365
    313         if ( $hide_empty )
     366        if ( !empty($slug) ) {
     367                $slug = sanitize_title($slug);
     368                $where = " AND t.slug = '$slug'";
     369        }
     370
     371        if ( !empty($parent) ) {
     372                $parent = (int) $parent;
     373                $where = " AND tt.parent = '$parent'";
     374        }
     375
     376        if ( $hide_empty && !$hierarchical )
    314377                $where .= ' AND tt.count > 0';
    315378
     
    334397                return array();
    335398
     399        if ( $child_of || $hierarchical ) {
     400                $children = _get_term_hierarchy($taxonomies[0]);
     401                if ( ! empty($children) )
     402                        $terms = & _get_term_children($child_of, $terms);
     403        }
     404
     405        /*
     406        // Update category counts to include children.
     407        if ( $pad_counts )
     408                _pad_category_counts($type, $categories);
     409
     410        // Make sure we show empty categories that have children.
     411        if ( $hierarchical && $hide_empty ) {
     412                foreach ( $categories as $k => $category ) {
     413                        if ( ! $category->{'link' == $type ? 'link_count' : 'category_count'} ) {
     414                                $children = _get_cat_children($category->cat_ID, $categories);
     415                                foreach ( $children as $child )
     416                                        if ( $child->{'link' == $type ? 'link_count' : 'category_count'} )
     417                                                continue 2;
     418
     419                                // It really is empty
     420                                unset($categories[$k]);
     421                        }
     422                }
     423        }
     424        reset ( $categories );
     425        */
     426
     427        $cache[ $key ] = $terms;
     428        wp_cache_add( 'get_terms', $cache, 'terms' );
     429
    336430        $terms = apply_filters('get_terms', $terms, $taxonomies, $args);
    337431        return $terms;
     
    345439
    346440        if ( is_object($term) ) {
    347                 wp_cache_add($term->term_id, $term, "term:$taxonomy");
     441                wp_cache_add($term->term_id, $term, $taxonomy);
    348442                $_term = $term;
    349443        } else {
    350444                $term = (int) $term;
    351                 if ( ! $_term = wp_cache_get($term, "term:$taxonomy") ) {
     445                if ( ! $_term = wp_cache_get($term, $taxonomy) ) {
    352446                        $_term = $wpdb->get_row("SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = '$taxonomy' AND t.term_id = '$term' LIMIT 1");
    353                         wp_cache_add($term, $_term, "term:$taxonomy");
     447                        wp_cache_add($term, $_term, $taxonomy);
    354448                }
    355449        }
    356450
    357451        $_term = apply_filters('get_term', $_term, $taxonomy);
     452        $_term = apply_filters("get_$taxonomy", $_term, $taxonomy);
    358453
    359454        if ( $output == OBJECT ) {
     
    368463}
    369464
     465function get_term_by($field, $value, $taxonomy, $output = OBJECT) {
     466        global $wpdb;
     467
     468        if ( ! is_taxonomy($taxonomy) )
     469                return false;
     470
     471        if ( 'slug' == $field ) {
     472                $field = 't.slug';
     473                $value = sanitize_title($field);
     474                if ( empty($value) )
     475                        return false;
     476        } else if ( 'name' == $field ) {
     477                // Assume already escaped
     478                $field = 't.name';
     479        } else {
     480                $field = 't.term_id';
     481                $value = (int) $value;
     482        }
     483
     484        $term = $wpdb->get_row("SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = '$taxonomy' AND $field = '$value' LIMIT 1");
     485        if ( !$term )
     486                return false;
     487
     488        wp_cache_add($term->term_id, $term, $taxonomy);
     489
     490        if ( $output == OBJECT ) {
     491                return $term;
     492        } elseif ( $output == ARRAY_A ) {
     493                return get_object_vars($term);
     494        } elseif ( $output == ARRAY_N ) {
     495                return array_values(get_object_vars($term));
     496        } else {
     497                return $term;
     498        }
     499}
     500
     501function _get_term_hierarchy($taxonomy) {
     502        // TODO Make sure taxonomy is hierarchical
     503        $children = get_option("{$taxonomy}_children");
     504        if ( is_array($children) )
     505                return $children;
     506
     507        $children = array();
     508        $terms = get_terms('category', 'hide_empty=0&hierarchical=0');
     509        foreach ( $terms as $term ) {
     510                if ( $term->parent > 0 )
     511                        $children[$cterm->parent][] = $term->term_id;
     512        }
     513        update_option("{$taxonomy}_children", $children);
     514
     515        return $children;
     516}
     517
     518function &_get_term_children($term_id, $terms) {
     519        if ( empty($terms) )
     520                return array();
     521
     522        $term_list = array();
     523        $has_children = _get_term_hierarchy();
     524
     525        if  ( ( 0 != $term_id ) && ! isset($has_children[$term_id]) )
     526                return array();
     527
     528        foreach ( $terms as $term ) {
     529                if ( $term->term_id == $term_id )
     530                        continue;
     531
     532                if ( $term->parent == $term_id ) {
     533                        $term_list[] = $term;
     534
     535                        if ( !isset($has_children[$term->term_id]) )
     536                                continue;
     537
     538                        if ( $children = _get_cat_children($term->term_id, $terms) )
     539                                $term_list = array_merge($term_list, $children);
     540                }
     541        }
     542
     543        return $term_list;
     544}
     545
    370546?>
Note: See TracChangeset for help on using the changeset viewer.