| [ Index ] |
WordPress Source Cross Reference |
[Summary view] [Print] [Text view]
1 <?php 2 3 function get_all_category_ids() { 4 global $wpdb; 5 6 if ( ! $cat_ids = wp_cache_get('all_category_ids', 'category') ) { 7 $cat_ids = $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories"); 8 wp_cache_add('all_category_ids', $cat_ids, 'category'); 9 } 10 11 return $cat_ids; 12 } 13 14 function &get_categories($args = '') { 15 global $wpdb, $category_links; 16 17 if ( is_array($args) ) 18 $r = &$args; 19 else 20 parse_str($args, $r); 21 22 $defaults = array('type' => 'post', 'child_of' => 0, 'orderby' => 'name', 'order' => 'ASC', 23 'hide_empty' => true, 'include_last_update_time' => false, 'hierarchical' => 1, $exclude => '', $include => '', 24 'number' => ''); 25 $r = array_merge($defaults, $r); 26 if ( 'count' == $r['orderby'] ) 27 $r['orderby'] = 'category_count'; 28 else 29 $r['orderby'] = "cat_" . $r['orderby']; // restricts order by to cat_ID and cat_name fields 30 $r['number'] = (int) $r['number']; 31 extract($r); 32 33 $where = 'cat_ID > 0'; 34 $inclusions = ''; 35 if ( !empty($include) ) { 36 $child_of = 0; //ignore child_of and exclude params if using include 37 $exclude = ''; 38 $incategories = preg_split('/[\s,]+/',$include); 39 if ( count($incategories) ) { 40 foreach ( $incategories as $incat ) { 41 if (empty($inclusions)) 42 $inclusions = ' AND ( cat_ID = ' . intval($incat) . ' '; 43 else 44 $inclusions .= ' OR cat_ID = ' . intval($incat) . ' '; 45 } 46 } 47 } 48 if (!empty($inclusions)) 49 $inclusions .= ')'; 50 $where .= $inclusions; 51 52 $exclusions = ''; 53 if ( !empty($exclude) ) { 54 $excategories = preg_split('/[\s,]+/',$exclude); 55 if ( count($excategories) ) { 56 foreach ( $excategories as $excat ) { 57 if (empty($exclusions)) 58 $exclusions = ' AND ( cat_ID <> ' . intval($excat) . ' '; 59 else 60 $exclusions .= ' AND cat_ID <> ' . intval($excat) . ' '; 61 // TODO: Exclude children of excluded cats? Note: children are getting excluded 62 } 63 } 64 } 65 if (!empty($exclusions)) 66 $exclusions .= ')'; 67 $exclusions = apply_filters('list_cats_exclusions', $exclusions, $r ); 68 $where .= $exclusions; 69 70 $having = ''; 71 if ( $hide_empty ) { 72 if ( 'link' == $type ) 73 $having = 'HAVING link_count > 0'; 74 else 75 $having = 'HAVING category_count > 0'; 76 } 77 78 if ( !empty($number) ) 79 $number = 'LIMIT ' . $number; 80 else 81 $number = ''; 82 83 $categories = $wpdb->get_results("SELECT * FROM $wpdb->categories WHERE $where $having ORDER BY $orderby $order $number"); 84 85 if ( empty($categories) ) 86 return array(); 87 88 // TODO: Integrate this into the main query. 89 if ( $include_last_update_time ) { 90 $stamps = $wpdb->get_results("SELECT category_id, UNIX_TIMESTAMP( MAX(post_date) ) AS ts FROM $wpdb->posts, $wpdb->post2cat, $wpdb->categories 91 WHERE post_status = 'publish' AND post_id = ID AND $where GROUP BY category_id"); 92 global $cat_stamps; 93 foreach ($stamps as $stamp) 94 $cat_stamps[$stamp->category_id] = $stamp->ts; 95 function stamp_cat($cat) { 96 global $cat_stamps; 97 $cat->last_update_timestamp = $cat_stamps[$cat->cat_ID]; 98 return $cat; 99 } 100 $categories = array_map('stamp_cat', $categories); 101 unset($cat_stamps); 102 } 103 104 if ( $child_of || $hierarchical ) 105 $categories = & _get_cat_children($child_of, $categories); 106 107 return apply_filters('get_categories', $categories, $r); 108 } 109 110 // Retrieves category data given a category ID or category object. 111 // Handles category caching. 112 function &get_category(&$category, $output = OBJECT) { 113 global $wpdb; 114 115 if ( empty($category) ) 116 return null; 117 118 if ( is_object($category) ) { 119 wp_cache_add($category->cat_ID, $category, 'category'); 120 $_category = $category; 121 } else { 122 if ( ! $_category = wp_cache_get($category, 'category') ) { 123 $_category = $wpdb->get_row("SELECT * FROM $wpdb->categories WHERE cat_ID = '$category' LIMIT 1"); 124 wp_cache_add($category, $_category, 'category'); 125 } 126 } 127 128 $_category = apply_filters('get_category', $_category); 129 130 if ( $output == OBJECT ) { 131 return $_category; 132 } elseif ( $output == ARRAY_A ) { 133 return get_object_vars($_category); 134 } elseif ( $output == ARRAY_N ) { 135 return array_values(get_object_vars($_category)); 136 } else { 137 return $_category; 138 } 139 } 140 141 function get_category_by_path($category_path, $full_match = true, $output = OBJECT) { 142 global $wpdb; 143 $category_path = rawurlencode(urldecode($category_path)); 144 $category_path = str_replace('%2F', '/', $category_path); 145 $category_path = str_replace('%20', ' ', $category_path); 146 $category_paths = '/' . trim($category_path, '/'); 147 $leaf_path = sanitize_title(basename($category_paths)); 148 $category_paths = explode('/', $category_paths); 149 foreach($category_paths as $pathdir) 150 $full_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir); 151 152 $categories = $wpdb->get_results("SELECT cat_ID, category_nicename, category_parent FROM $wpdb->categories WHERE category_nicename = '$leaf_path'"); 153 154 if ( empty($categories) ) 155 return NULL; 156 157 foreach ($categories as $category) { 158 $path = '/' . $leaf_path; 159 $curcategory = $category; 160 while ($curcategory->category_parent != 0) { 161 $curcategory = $wpdb->get_row("SELECT cat_ID, category_nicename, category_parent FROM $wpdb->categories WHERE cat_ID = '$curcategory->category_parent'"); 162 $path = '/' . $curcategory->category_nicename . $path; 163 } 164 165 if ( $path == $full_path ) 166 return get_category($category->cat_ID, $output); 167 } 168 169 // If full matching is not required, return the first cat that matches the leaf. 170 if ( ! $full_match ) 171 return get_category($categories[0]->cat_ID, $output); 172 173 return NULL; 174 } 175 176 // Get the ID of a category from its name 177 function get_cat_ID($cat_name='General') { 178 global $wpdb; 179 180 $cid = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE cat_name='$cat_name'"); 181 182 return $cid?$cid:1; // default to cat 1 183 } 184 185 // Deprecate 186 function get_catname($cat_ID) { 187 return get_cat_name($cat_ID); 188 } 189 190 // Get the name of a category from its ID 191 function get_cat_name($cat_id) { 192 $cat_id = (int) $cat_id; 193 $category = &get_category($cat_id); 194 return $category->cat_name; 195 } 196 197 // 198 // Private 199 // 200 201 function &_get_cat_children($category_id, $categories) { 202 if ( empty($categories) ) 203 return array(); 204 205 $category_list = array(); 206 foreach ( $categories as $category ) { 207 if ( $category->category_parent == $category_id ) { 208 $category_list[] = $category; 209 if ( $children = _get_cat_children($category->cat_ID, $categories) ) 210 $category_list = array_merge($category_list, $children); 211 } 212 } 213 214 return $category_list; 215 } 216 217 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Sat Jul 15 11:57:04 2006 | Courtesy of Taragana |