root/branches/2.1/wp-includes/category-template.php

Revision 5092, 8.3 kB (checked in by ryan, 2 years ago)

Big int patch for 2.1

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2
3 function get_category_children($id, $before = '/', $after = '') {
4     if ( 0 == $id )
5         return '';
6
7     $chain = '';
8
9     $cat_ids = get_all_category_ids();
10     foreach ( $cat_ids as $cat_id ) {
11         if ( $cat_id == $id)
12             continue;
13
14         $category = get_category($cat_id);
15         if ( $category->category_parent == $id ) {
16             $chain .= $before.$category->cat_ID.$after;
17             $chain .= get_category_children($category->cat_ID, $before, $after);
18         }
19     }
20     return $chain;
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_option('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_option('home') . trailingslashit($catlink);
39     }
40     return apply_filters('category_link', $catlink, $category_id);
41 }
42
43 function get_category_parents($id, $link = FALSE, $separator = '/', $nicename = FALSE){
44     $chain = '';
45     $parent = &get_category($id);
46
47     if ( $nicename )
48         $name = $parent->category_nicename;
49     else
50         $name = $parent->cat_name;
51
52     if ( $parent->category_parent && ($parent->category_parent != $parent->cat_ID) )
53         $chain .= get_category_parents($parent->category_parent, $link, $separator, $nicename);
54
55     if ( $link )
56         $chain .= '<a href="' . get_category_link($parent->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $parent->cat_name) . '">'.$name.'</a>' . $separator;
57     else
58         $chain .= $name.$separator;
59     return $chain;
60 }
61
62 function get_the_category($id = false) {
63 global $post, $category_cache, $blog_id;
64
65     $id = (int) $id;
66     if ( !$id )
67         $id = (int) $post->ID;
68
69     if ( !isset($category_cache[$blog_id][$id]) )
70         update_post_category_cache($id);
71
72     $categories = $category_cache[$blog_id][$id];
73
74     if ( !empty($categories) )
75         sort($categories);
76     else
77         $categories = array();
78
79     return $categories;
80 }
81
82 function get_the_category_by_ID($cat_ID) {
83     $cat_ID = (int) $cat_ID;
84     $category = &get_category($cat_ID);
85     return $category->cat_name;
86 }
87
88 function get_the_category_list($separator = '', $parents='') {
89     $categories = get_the_category();
90     if (empty($categories))
91         return apply_filters('the_category', __('Uncategorized'), $separator, $parents);
92
93     $thelist = '';
94     if ( '' == $separator ) {
95         $thelist .= '<ul class="post-categories">';
96         foreach ( $categories as $category ) {
97             $thelist .= "\n\t<li>";
98             switch ( strtolower($parents) ) {
99                 case 'multiple':
100                     if ($category->category_parent)
101                         $thelist .= get_category_parents($category->category_parent, TRUE);
102                     $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>';
103                     break;
104                 case 'single':
105                     $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . ' rel="category tag">';
106                     if ($category->category_parent)
107                         $thelist .= get_category_parents($category->category_parent, FALSE);
108                     $thelist .= $category->cat_name.'</a></li>';
109                     break;
110                 case '':
111                 default:
112                     $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>';
113             }
114         }
115         $thelist .= '</ul>';
116     } else {
117         $i = 0;
118         foreach ( $categories as $category ) {
119             if ( 0 < $i )
120                 $thelist .= $separator . ' ';
121             switch ( strtolower($parents) ) {
122                 case 'multiple':
123                     if ( $category->category_parent )
124                         $thelist .= get_category_parents($category->category_parent, TRUE);
125                     $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>';
126                     break;
127                 case 'single':
128                     $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" rel="category tag">';
129                     if ( $category->category_parent )
130                         $thelist .= get_category_parents($category->category_parent, FALSE);
131                     $thelist .= "$category->cat_name</a>";
132                     break;
133                 case '':
134                 default:
135                     $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>';
136             }
137             ++$i;
138         }
139     }
140     return apply_filters('the_category', $thelist, $separator, $parents);
141 }
142
143 function in_category($category) { // Check if the current post is in the given category
144     global $category_cache, $post, $blog_id;
145
146     if ( isset( $category_cache[$blog_id][$post->ID][$category] ) )
147         return true;
148     else
149         return false;
150 }
151
152 function the_category($separator = '', $parents='') {
153     echo get_the_category_list($separator, $parents);
154 }
155
156 function category_description($category = 0) {
157     global $cat;
158     if ( !$category )
159         $category = $cat;
160     $category = & get_category($category);
161     return apply_filters('category_description', $category->category_description, $category->cat_ID);
162 }
163
164 function wp_dropdown_categories($args = '') {
165     if ( is_array($args) )
166         $r = &$args;
167     else
168         parse_str($args, $r);
169
170     $defaults = array('show_option_all' => '', 'show_option_none' => '', 'orderby' => 'ID',
171         'order' => 'ASC', 'show_last_update' => 0, 'show_count' => 0,
172         'hide_empty' => 1, 'child_of' => 0, 'exclude' => '', 'echo' => 1,
173         'selected' => 0, 'hierarchical' => 0, 'name' => 'cat',
174         'class' => 'postform');
175     $defaults['selected'] = ( is_category() ) ? get_query_var('cat') : 0;
176     $r = array_merge($defaults, $r);
177     $r['include_last_update_time'] = $r['show_last_update'];
178     extract($r);
179
180     $categories = get_categories($r);
181
182     $output = '';
183     if ( ! empty($categories) ) {
184         $output = "<select name='$name' id='$name' class='$class'>\n";
185
186         if ( $show_option_all ) {
187             $show_option_all = apply_filters('list_cats', $show_option_all);
188             $output .= "\t<option value='0'>$show_option_all</option>\n";
189         }
190
191         if ( $show_option_none) {
192             $show_option_none = apply_filters('list_cats', $show_option_none);
193             $output .= "\t<option value='-1'>$show_option_none</option>\n";
194         }
195
196         if ( $hierarchical )
197             $depth = 0// Walk the full depth.
198         else
199             $depth = -1; // Flat.
200
201         $output .= walk_category_dropdown_tree($categories, $depth, $r);
202         $output .= "</select>\n";
203     }
204
205     $output = apply_filters('wp_dropdown_cats', $output);
206
207     if ( $echo )
208         echo $output;
209
210     return $output;
211 }
212
213 function wp_list_categories($args = '') {
214     if ( is_array($args) )
215         $r = &$args;
216     else
217         parse_str($args, $r);
218
219     $defaults = array('show_option_all' => '', 'orderby' => 'name',
220         'order' => 'ASC', 'show_last_update' => 0, 'style' => 'list',
221         'show_count' => 0, 'hide_empty' => 1, 'use_desc_for_title' => 1,
222         'child_of' => 0, 'feed' => '', 'feed_image' => '', 'exclude' => '',
223         'hierarchical' => true, 'title_li' => __('Categories'));
224     $r = array_merge($defaults, $r);
225     if ( !isset($r['pad_counts']) && $r['show_count'] && $r['hierarchical'] )
226         $r['pad_counts'] = true;
227     if ( isset($r['show_date']) )
228         $r['include_last_update_time'] = $r['show_date'];
229     extract($r);
230
231     $categories = get_categories($r);
232
233     $output = '';
234     if ( $title_li && 'list' == $style )
235             $output = '<li class="categories">' . $r['title_li'] . '<ul>';
236
237     if ( empty($categories) ) {
238         if ( 'list' == $style )
239             $output .= '<li>' . __("No categories") . '</li>';
240         else
241             $output .= __("No categories");
242     } else {
243         global $wp_query;
244         
245         if ( is_category() )
246             $r['current_category'] = $wp_query->get_queried_object_id();
247
248         if ( $hierarchical )
249             $depth = 0// Walk the full depth.
250         else
251             $depth = -1; // Flat.
252
253         $output .= walk_category_tree($categories, $depth, $r);
254     }
255
256     if ( $title_li && 'list' == $style )
257         $output .= '</ul></li>';
258
259     echo apply_filters('wp_list_categories', $output);
260 }
261
262 //
263 // Helper functions
264 //
265
266 function walk_category_tree() {
267     $walker = new Walker_Category;
268     $args = func_get_args();
269     return call_user_func_array(array(&$walker, 'walk'), $args);
270 }
271
272 function walk_category_dropdown_tree() {
273     $walker = new Walker_CategoryDropdown;
274     $args = func_get_args();
275     return call_user_func_array(array(&$walker, 'walk'), $args);
276 }
277
278 ?>
279
Note: See TracBrowser for help on using the browser.