]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/category.php
Wordpress 2.3.2
[autoinstalls/wordpress.git] / wp-includes / category.php
1 <?php
2
3 function get_all_category_ids() {
4         global $wpdb;
5
6         if ( ! $cat_ids = wp_cache_get('all_category_ids', 'category') ) {
7                 $cat_ids = get_terms('category', 'fields=ids&get=all');
8                 wp_cache_add('all_category_ids', $cat_ids, 'category');
9         }
10
11         return $cat_ids;
12 }
13
14 function &get_categories($args = '') {
15         $defaults = array('type' => 'category');
16         $args = wp_parse_args($args, $defaults);
17
18         $taxonomy = 'category';
19         if ( 'link' == $args['type'] )
20                 $taxonomy = 'link_category';
21         $categories = get_terms($taxonomy, $args);
22
23         foreach ( array_keys($categories) as $k )
24                 _make_cat_compat($categories[$k]);
25
26         return $categories;
27 }
28
29 // Retrieves category data given a category ID or category object.
30 // Handles category caching.
31 function &get_category($category, $output = OBJECT, $filter = 'raw') {
32         $category = get_term($category, 'category', $output, $filter);
33         if ( is_wp_error( $category ) )
34                 return $category;
35
36         _make_cat_compat($category);
37
38         return $category;
39 }
40
41 function get_category_by_path($category_path, $full_match = true, $output = OBJECT) {
42         global $wpdb;
43         $category_path = rawurlencode(urldecode($category_path));
44         $category_path = str_replace('%2F', '/', $category_path);
45         $category_path = str_replace('%20', ' ', $category_path);
46         $category_paths = '/' . trim($category_path, '/');
47         $leaf_path  = sanitize_title(basename($category_paths));
48         $category_paths = explode('/', $category_paths);
49         $full_path = '';
50         foreach ( (array) $category_paths as $pathdir )
51                 $full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title($pathdir);
52
53         $categories = get_terms('category', "get=all&slug=$leaf_path");
54
55         if ( empty($categories) )
56                 return NULL;
57
58         foreach ($categories as $category) {
59                 $path = '/' . $leaf_path;
60                 $curcategory = $category;
61                 while ( ($curcategory->parent != 0) && ($curcategory->parent != $curcategory->term_id) ) {
62                         $curcategory = get_term($curcategory->parent, 'category');
63                         if ( is_wp_error( $curcategory ) )
64                                 return $curcategory;
65                         $path = '/' . $curcategory->slug . $path;
66                 }
67
68                 if ( $path == $full_path )
69                         return get_category($category->term_id, $output);
70         }
71
72         // If full matching is not required, return the first cat that matches the leaf.
73         if ( ! $full_match )
74                 return get_category($categories[0]->term_id, $output);
75
76         return NULL;
77 }
78
79 function get_category_by_slug( $slug  ) {
80         $category = get_term_by('slug', $slug, 'category');
81         if ( $category )
82                 _make_cat_compat($category);
83
84         return $category;
85 }
86
87 // Get the ID of a category from its name
88 function get_cat_ID($cat_name='General') {
89         global $wpdb;
90
91         $cat = get_term_by('name', $cat_name, 'category');
92         if ($cat)
93                 return $cat->term_id;
94         return 0;
95 }
96
97 // Deprecate
98 function get_catname($cat_ID) {
99         return get_cat_name($cat_ID);
100 }
101
102 // Get the name of a category from its ID
103 function get_cat_name($cat_id) {
104         $cat_id = (int) $cat_id;
105         $category = &get_category($cat_id);
106         return $category->name;
107 }
108
109 function cat_is_ancestor_of($cat1, $cat2) {
110         if ( is_int($cat1) )
111                 $cat1 = & get_category($cat1);
112         if ( is_int($cat2) )
113                 $cat2 = & get_category($cat2);
114
115         if ( !$cat1->term_id || !$cat2->parent )
116                 return false;
117
118         if ( $cat2->parent == $cat1->term_id )
119                 return true;
120
121         return cat_is_ancestor_of($cat1, get_category($cat2->parent));
122 }
123
124 function sanitize_category($category, $context = 'display') {
125         return sanitize_term($category, 'category', $context);
126 }
127
128 function sanitize_category_field($field, $value, $cat_id, $context) {
129         return sanitize_term_field($field, $value, $cat_id, 'category', $context);
130 }
131
132 // Tags
133
134 function &get_tags($args = '') {
135         global $wpdb, $category_links;
136
137         $key = md5( serialize( $args ) );
138         if ( $cache = wp_cache_get( 'get_tags', 'category' ) )
139                 if ( isset( $cache[ $key ] ) )
140                         return apply_filters('get_tags', $cache[$key], $args);
141
142
143         $tags = get_terms('post_tag', $args);
144
145         if ( empty($tags) )
146                 return array();
147
148         $cache[ $key ] = $tags;
149         wp_cache_set( 'get_tags', $cache, 'category' );
150
151         $tags = apply_filters('get_tags', $tags, $args);
152         return $tags;
153 }
154
155 function &get_tag($tag, $output = OBJECT, $filter = 'raw') {
156         return get_term($tag, 'post_tag', $output, $filter);
157 }
158
159 //
160 // Cache
161 //
162
163 function update_category_cache() {
164         return true;
165 }
166
167 function clean_category_cache($id) {
168         clean_term_cache($id, 'category');
169 }
170
171 //
172 // Private helpers
173 //
174
175 function _make_cat_compat( &$category) {
176         if ( is_object($category) ) {
177                 $category->cat_ID = &$category->term_id;
178                 $category->category_count = &$category->count;
179                 $category->category_description = &$category->description;
180                 $category->cat_name = &$category->name;
181                 $category->category_nicename = &$category->slug;
182                 $category->category_parent = &$category->parent;
183         } else if ( is_array($category) && isset($category['term_id']) ) {
184                 $category['cat_ID'] = &$category['term_id'];
185                 $category['category_count'] = &$category['count'];
186                 $category['category_description'] = &$category['description'];
187                 $category['cat_name'] = &$category['name'];
188                 $category['category_nicename'] = &$category['slug'];
189                 $category['category_parent'] = &$category['parent'];
190         }
191 }
192
193 ?>