root/tags/2.1.1/wp-includes/category-template.php

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

Check style. Props nickshanks. fixes #3610

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