root/branches/2.0/wp-includes/template-functions-category.php

Revision 5100, 13.0 kB (checked in by ryan, 2 years ago)

More int casts

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