]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/template-functions-category.php
Wordpress 2.0.2-scripts
[autoinstalls/wordpress.git] / wp-includes / template-functions-category.php
1 <?php
2
3 function get_the_category($id = false) {
4 global $post, $category_cache;
5
6         if ( !$id )
7                 $id = $post->ID;
8
9         if ( !isset($category_cache[$id]) )
10                 update_post_category_cache($id);
11
12         $categories = $category_cache[$id];
13
14         if ( !empty($categories) )
15                 sort($categories);
16         else
17                 $categories = array();
18
19         return $categories;
20 }
21
22 function get_category_link($category_id) {
23         global $wp_rewrite;
24         $catlink = $wp_rewrite->get_category_permastruct();
25
26         if ( empty($catlink) ) {
27                 $file = get_settings('home') . '/';
28                 $catlink = $file . '?cat=' . $category_id;
29         } else {
30                 $category = &get_category($category_id);
31                 $category_nicename = $category->category_nicename;
32
33                 if ( $parent = $category->category_parent )
34                         $category_nicename = get_category_parents($parent, false, '/', true) . $category_nicename . '/';
35
36                 $catlink = str_replace('%category%', $category_nicename, $catlink);
37                 $catlink = get_settings('home') . trailingslashit($catlink);
38         }
39         return apply_filters('category_link', $catlink, $category_id);
40 }
41
42 function get_the_category_list($separator = '', $parents='') {
43         $categories = get_the_category();
44         if (empty($categories))
45                 return apply_filters('the_category', __('Uncategorized'), $separator, $parents);
46
47         $thelist = '';
48         if ( '' == $separator ) {
49                 $thelist .= '<ul class="post-categories">';
50                 foreach ( $categories as $category ) {
51                         $category->cat_name = $category->cat_name;
52                         $thelist .= "\n\t<li>";
53                         switch ( strtolower($parents) ) {
54                                 case 'multiple':
55                                         if ($category->category_parent)
56                                                 $thelist .= get_category_parents($category->category_parent, TRUE);
57                                         $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" rel="category tag">'.$category->cat_name.'</a></li>';
58                                         break;
59                                 case 'single':
60                                         $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . ' rel="category tag">';
61                                         if ($category->category_parent)
62                                                 $thelist .= get_category_parents($category->category_parent, FALSE);
63                                         $thelist .= $category->cat_name.'</a></li>';
64                                         break;
65                                 case '':
66                                 default:
67                                         $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" rel="category tag">'.$category->cat_name.'</a></li>';
68                         }
69                 }
70                 $thelist .= '</ul>';
71         } else {
72                 $i = 0;
73                 foreach ( $categories as $category ) {
74                         $category->cat_name = $category->cat_name;
75                         if ( 0 < $i )
76                                 $thelist .= $separator . ' ';
77                         switch ( strtolower($parents) ) {
78                                 case 'multiple':
79                                         if ( $category->category_parent )
80                                                 $thelist .= get_category_parents($category->category_parent, TRUE);
81                                         $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" rel="category tag">'.$category->cat_name.'</a>';
82                                         break;
83                                 case 'single':
84                                         $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" rel="category tag">';
85                                         if ( $category->category_parent )
86                                                 $thelist .= get_category_parents($category->category_parent, FALSE);
87                                         $thelist .= "$category->cat_name</a>";
88                                         break;
89                                 case '':
90                                 default:
91                                         $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" rel="category tag">'.$category->cat_name.'</a>';
92                         }
93                         ++$i;
94                 }
95         }
96         return apply_filters('the_category', $thelist, $separator, $parents);
97 }
98
99 function the_category($separator = '', $parents='') {
100         echo get_the_category_list($separator, $parents);
101 }
102
103 function get_the_category_by_ID($cat_ID) {
104         $cat_ID = (int) $cat_ID;
105         $category = &get_category($cat_ID);
106         return $category->cat_name;
107 }
108
109 function get_category_parents($id, $link = FALSE, $separator = '/', $nicename = FALSE){
110         $chain = '';
111         $parent = &get_category($id);
112
113         if ( $nicename )
114                 $name = $parent->category_nicename;
115         else
116                 $name = $parent->cat_name;
117
118         if ( $parent->category_parent )
119                 $chain .= get_category_parents($parent->category_parent, $link, $separator, $nicename);
120
121         if ( $link )
122                 $chain .= '<a href="' . get_category_link($parent->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $parent->cat_name) . '">'.$name.'</a>' . $separator;
123         else
124                 $chain .= $name.$separator;
125         return $chain;
126 }
127
128 function get_category_children($id, $before = '/', $after = '') {
129         if ( 0 == $id )
130                 return '';
131
132         $cat_ids = get_all_category_ids();
133         foreach ( $cat_ids as $cat_id ) {
134                 if ( $cat_id == $id)
135                         continue;
136
137                 $category = get_category($cat_id);
138                 if ( $category->category_parent == $id ) {
139                         $chain .= $before.$category->cat_ID.$after;
140                         $chain .= get_category_children($category->cat_ID, $before, $after);
141                 }
142         }
143         return $chain;
144 }
145
146 // Deprecated.
147 function the_category_ID($echo = true) {
148         // Grab the first cat in the list.
149         $categories = get_the_category();
150         $cat = $categories[0]->cat_ID;
151
152         if ( $echo )
153                 echo $cat;
154
155         return $cat;
156 }
157
158 // Deprecated.
159 function the_category_head($before='', $after='') {
160         global $currentcat, $previouscat;
161         // Grab the first cat in the list.
162         $categories = get_the_category();
163         $currentcat = $categories[0]->category_id;
164         if ( $currentcat != $previouscat ) {
165                 echo $before;
166                 echo get_the_category_by_ID($currentcat);
167                 echo $after;
168                 $previouscat = $currentcat;
169         }
170 }
171
172 function category_description($category = 0) {
173         global $cat;
174         if ( !$category )
175                 $category = $cat;
176         $category = & get_category($category);
177         return apply_filters('category_description', $category->category_description, $category->cat_ID);
178 }
179
180 // out of the WordPress loop
181 function dropdown_cats($optionall = 1, $all = 'All', $sort_column = 'ID', $sort_order = 'asc',
182                 $optiondates = 0, $optioncount = 0, $hide_empty = 1, $optionnone=FALSE,
183                 $selected=0, $hide=0) {
184         global $wpdb;
185         if ( ($file == 'blah') || ($file == '') )
186                 $file = get_settings('home') . '/';
187         if ( !$selected )
188                 $selected=$cat;
189         $sort_column = 'cat_'.$sort_column;
190
191         $query = "
192                 SELECT cat_ID, cat_name, category_nicename,category_parent,
193                 COUNT($wpdb->post2cat.post_id) AS cat_count,
194                 DAYOFMONTH(MAX(post_date)) AS lastday, MONTH(MAX(post_date)) AS lastmonth
195                 FROM $wpdb->categories LEFT JOIN $wpdb->post2cat ON (cat_ID = category_id)
196                 LEFT JOIN $wpdb->posts ON (ID = post_id)
197                 WHERE cat_ID > 0
198                 ";
199         if ( $hide ) {
200                 $query .= " AND cat_ID != $hide";
201                 $query .= get_category_children($hide, " AND cat_ID != ");
202         }
203         $query .=" GROUP BY cat_ID";
204         if ( intval($hide_empty) == 1 )
205                 $query .= " HAVING cat_count > 0";
206         $query .= " ORDER BY $sort_column $sort_order, post_date DESC";
207
208         $categories = $wpdb->get_results($query);
209         echo "<select name='cat' class='postform'>\n";
210         if ( intval($optionall) == 1 ) {
211                 $all = apply_filters('list_cats', $all);
212                 echo "\t<option value='0'>$all</option>\n";
213         }
214         if ( intval($optionnone) == 1 )
215                 echo "\t<option value='-1'>".__('None')."</option>\n";
216         if ( $categories ) {
217                 foreach ( $categories as $category ) {
218                         $cat_name = apply_filters('list_cats', $category->cat_name, $category);
219                         echo "\t<option value=\"".$category->cat_ID."\"";
220                         if ( $category->cat_ID == $selected )
221                                 echo ' selected="selected"';
222                         echo '>';
223                         echo $cat_name;
224                         if ( intval($optioncount) == 1 )
225                                 echo '&nbsp;&nbsp;('.$category->cat_count.')';
226                         if ( intval($optiondates) == 1 )
227                                 echo '&nbsp;&nbsp;'.$category->lastday.'/'.$category->lastmonth;
228                         echo "</option>\n";
229                 }
230         }
231         echo "</select>\n";
232 }
233
234 // out of the WordPress loop
235 function wp_list_cats($args = '') {
236         parse_str($args, $r);
237         if ( !isset($r['optionall']))
238                 $r['optionall'] = 0;
239         if ( !isset($r['all']))
240                 $r['all'] = 'All';
241         if ( !isset($r['sort_column']) )
242                 $r['sort_column'] = 'ID';
243         if ( !isset($r['sort_order']) )
244                 $r['sort_order'] = 'asc';
245         if ( !isset($r['file']) )
246                 $r['file'] = '';
247         if ( !isset($r['list']) )
248                 $r['list'] = true;
249         if ( !isset($r['optiondates']) )
250                 $r['optiondates'] = 0;
251         if ( !isset($r['optioncount']) )
252                 $r['optioncount'] = 0;
253         if ( !isset($r['hide_empty']) )
254                 $r['hide_empty'] = 1;
255         if ( !isset($r['use_desc_for_title']) )
256                 $r['use_desc_for_title'] = 1;
257         if ( !isset($r['children']) )
258                 $r['children'] = true;
259         if ( !isset($r['child_of']) )
260                 $r['child_of'] = 0;
261         if ( !isset($r['categories']) )
262                 $r['categories'] = 0;
263         if ( !isset($r['recurse']) )
264                 $r['recurse'] = 0;
265         if ( !isset($r['feed']) )
266                 $r['feed'] = '';
267         if ( !isset($r['feed_image']) )
268                 $r['feed_image'] = '';
269         if ( !isset($r['exclude']) )
270                 $r['exclude'] = '';
271         if ( !isset($r['hierarchical']) )
272                 $r['hierarchical'] = true;
273
274         return list_cats($r['optionall'], $r['all'], $r['sort_column'], $r['sort_order'], $r['file'],   $r['list'], $r['optiondates'], $r['optioncount'], $r['hide_empty'], $r['use_desc_for_title'], $r['children'], $r['child_of'], $r['categories'], $r['recurse'], $r['feed'], $r['feed_image'], $r['exclude'], $r['hierarchical']);
275 }
276
277 function list_cats($optionall = 1, $all = 'All', $sort_column = 'ID', $sort_order = 'asc', $file = '', $list = true, $optiondates = 0, $optioncount = 0, $hide_empty = 1, $use_desc_for_title = 1, $children=FALSE, $child_of=0, $categories=0, $recurse=0, $feed = '', $feed_image = '', $exclude = '', $hierarchical=FALSE) {
278         global $wpdb, $wp_query;
279         // Optiondates now works
280         if ( '' == $file )
281                 $file = get_settings('home') . '/';
282
283         $exclusions = '';
284         if ( !empty($exclude) ) {
285                 $excats = preg_split('/[\s,]+/',$exclude);
286                 if ( count($excats) ) {
287                         foreach ( $excats as $excat ) {
288                                 $exclusions .= ' AND cat_ID <> ' . intval($excat) . ' ';
289                         }
290                 }
291         }
292
293         $exclusions = apply_filters('list_cats_exclusions', $exclusions );
294
295         if ( intval($categories) == 0 ) {
296                 $sort_column = 'cat_'.$sort_column;
297
298                 $query = "
299                         SELECT cat_ID, cat_name, category_nicename, category_description, category_parent, category_count
300                         FROM $wpdb->categories
301                         WHERE cat_ID > 0 $exclusions
302                         ORDER BY $sort_column $sort_order";
303
304                 $categories = $wpdb->get_results($query);
305         }
306
307         if ( $optiondates ) {
308                 $cat_dates = $wpdb->get_results("       SELECT category_id,
309                 UNIX_TIMESTAMP( MAX(post_date) ) AS ts
310                 FROM $wpdb->posts, $wpdb->post2cat, $wpdb->categories
311                 WHERE post_status = 'publish' AND post_id = ID $exclusions
312                 GROUP BY category_id");
313                 foreach ( $cat_dates as $cat_date ) {
314                         $category_timestamp["$cat_date->category_id"] = $cat_date->ts;
315                 }
316         }
317
318         $num_found=0;
319         $thelist = "";
320
321         foreach ( $categories as $category ) {
322                 if ( ( intval($hide_empty) == 0 || $category->category_count) && (!$hierarchical || $category->category_parent == $child_of) ) {
323                         $num_found++;
324                         $link = '<a href="'.get_category_link($category->cat_ID).'" ';
325                         if ( $use_desc_for_title == 0 || empty($category->category_description) )
326                                 $link .= 'title="'. sprintf(__("View all posts filed under %s"), wp_specialchars($category->cat_name)) . '"';
327                         else
328                                 $link .= 'title="' . wp_specialchars(apply_filters('category_description',$category->category_description,$category)) . '"';
329                         $link .= '>';
330                         $link .= apply_filters('list_cats', $category->cat_name, $category).'</a>';
331
332                         if ( (! empty($feed_image)) || (! empty($feed)) ) {
333
334                                 $link .= ' ';
335
336                                 if ( empty($feed_image) )
337                                         $link .= '(';
338
339                                 $link .= '<a href="' . get_category_rss_link(0, $category->cat_ID, $category->category_nicename) . '"';
340
341                                 if ( !empty($feed) ) {
342                                         $title = ' title="' . $feed . '"';
343                                         $alt = ' alt="' . $feed . '"';
344                                         $name = $feed;
345                                         $link .= $title;
346                                 }
347
348                                 $link .= '>';
349
350                                 if ( !empty($feed_image) )
351                                         $link .= "<img src='$feed_image' $alt$title" . ' />';
352                                 else
353                                         $link .= $name;
354
355                                 $link .= '</a>';
356
357                                 if (empty($feed_image))
358                                         $link .= ')';
359                         }
360
361                         if ( intval($optioncount) == 1 )
362                                 $link .= ' ('.intval($category->category_count).')';
363
364                         if ( $optiondates ) {
365                                 if ( $optiondates == 1 )
366                                         $optiondates = 'Y-m-d';
367                                 $link .= ' ' . gmdate($optiondates, $category_timestamp["$category->cat_ID"]);
368                         }
369
370                         if ( $list ) {
371                                 $thelist .= "\t<li";
372                                 if (($category->cat_ID == $wp_query->get_queried_object_id()) && is_category()) {
373                                         $thelist .=  ' class="current-cat"';
374                                 }
375                                 $thelist .= ">$link\n";
376                         } else {
377                                 $thelist .= "\t$link<br />\n";
378                         }
379
380                         if ($hierarchical && $children)
381                                 $thelist .= list_cats($optionall, $all, $sort_column, $sort_order, $file, $list, $optiondates, $optioncount, $hide_empty, $use_desc_for_title, $hierarchical, $category->cat_ID, $categories, 1, $feed, $feed_image, $exclude, $hierarchical);
382                         if ($list)
383                                 $thelist .= "</li>\n";
384                 }
385         }
386         if ( !$num_found && !$child_of ) {
387                 if ( $list ) {
388                         $before = '<li>';
389                         $after = '</li>';
390                 }
391                 echo $before . __("No categories") . $after . "\n";
392                 return;
393         }
394         if ( $list && $child_of && $num_found && $recurse ) {
395                 $pre = "\t\t<ul class='children'>";
396                 $post = "\t\t</ul>\n";
397         } else {
398                 $pre = $post = '';
399         }
400         $thelist = $pre . $thelist . $post;
401         if ( $recurse )
402                 return $thelist;
403         echo apply_filters('list_cats', $thelist);
404 }
405
406 function in_category($category) { // Check if the current post is in the given category
407         global $category_cache, $post;
408
409         if ( isset( $category_cache[$post->ID][$category] ) )
410                 return true;
411         else
412                 return false;
413 }
414
415 ?>