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