PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/2.1/wp-includes/category.php

#
PHP | 302 lines | 230 code | 52 blank | 20 comment | 67 complexity | 3b8db20247c997610190c452e4be0087 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. function get_all_category_ids() {
  3. global $wpdb;
  4. if ( ! $cat_ids = wp_cache_get('all_category_ids', 'category') ) {
  5. $cat_ids = $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories");
  6. wp_cache_set('all_category_ids', $cat_ids, 'category');
  7. }
  8. return $cat_ids;
  9. }
  10. function &get_categories($args = '') {
  11. global $wpdb, $category_links;
  12. if ( is_array($args) )
  13. $r = &$args;
  14. else
  15. parse_str($args, $r);
  16. $defaults = array('type' => 'post', 'child_of' => 0, 'orderby' => 'name', 'order' => 'ASC',
  17. 'hide_empty' => true, 'include_last_update_time' => false, 'hierarchical' => 1, 'exclude' => '', 'include' => '',
  18. 'number' => '', 'pad_counts' => false);
  19. $r = array_merge($defaults, $r);
  20. if ( 'count' == $r['orderby'] )
  21. $r['orderby'] = 'category_count';
  22. else
  23. $r['orderby'] = "cat_" . $r['orderby']; // restricts order by to cat_ID and cat_name fields
  24. $r['number'] = (int) $r['number'];
  25. extract($r);
  26. $key = md5( serialize( $r ) );
  27. if ( $cache = wp_cache_get( 'get_categories', 'category' ) )
  28. if ( isset( $cache[ $key ] ) )
  29. return $cache[ $key ];
  30. $where = 'cat_ID > 0';
  31. $inclusions = '';
  32. if ( !empty($include) ) {
  33. $child_of = 0; //ignore child_of and exclude params if using include
  34. $exclude = '';
  35. $incategories = preg_split('/[\s,]+/',$include);
  36. if ( count($incategories) ) {
  37. foreach ( $incategories as $incat ) {
  38. if (empty($inclusions))
  39. $inclusions = ' AND ( cat_ID = ' . intval($incat) . ' ';
  40. else
  41. $inclusions .= ' OR cat_ID = ' . intval($incat) . ' ';
  42. }
  43. }
  44. }
  45. if (!empty($inclusions))
  46. $inclusions .= ')';
  47. $where .= $inclusions;
  48. $exclusions = '';
  49. if ( !empty($exclude) ) {
  50. $excategories = preg_split('/[\s,]+/',$exclude);
  51. if ( count($excategories) ) {
  52. foreach ( $excategories as $excat ) {
  53. if (empty($exclusions))
  54. $exclusions = ' AND ( cat_ID <> ' . intval($excat) . ' ';
  55. else
  56. $exclusions .= ' AND cat_ID <> ' . intval($excat) . ' ';
  57. // TODO: Exclude children of excluded cats? Note: children are getting excluded
  58. }
  59. }
  60. }
  61. if (!empty($exclusions))
  62. $exclusions .= ')';
  63. $exclusions = apply_filters('list_cats_exclusions', $exclusions, $r );
  64. $where .= $exclusions;
  65. if ( $hide_empty && !$hierarchical ) {
  66. if ( 'link' == $type )
  67. $where .= ' AND link_count > 0';
  68. else
  69. $where .= ' AND category_count > 0';
  70. }
  71. if ( !empty($number) )
  72. $number = 'LIMIT ' . $number;
  73. else
  74. $number = '';
  75. $categories = $wpdb->get_results("SELECT * FROM $wpdb->categories WHERE $where ORDER BY $orderby $order $number");
  76. if ( empty($categories) )
  77. return array();
  78. // TODO: Integrate this into the main query.
  79. if ( $include_last_update_time ) {
  80. $stamps = $wpdb->get_results("SELECT category_id, UNIX_TIMESTAMP( MAX(post_date) ) AS ts FROM $wpdb->posts, $wpdb->post2cat, $wpdb->categories
  81. WHERE post_status = 'publish' AND post_id = ID AND $where GROUP BY category_id");
  82. global $cat_stamps;
  83. foreach ($stamps as $stamp)
  84. $cat_stamps[$stamp->category_id] = $stamp->ts;
  85. function stamp_cat($cat) {
  86. global $cat_stamps;
  87. $cat->last_update_timestamp = $cat_stamps[$cat->cat_ID];
  88. return $cat;
  89. }
  90. $categories = array_map('stamp_cat', $categories);
  91. unset($cat_stamps);
  92. }
  93. if ( $child_of || $hierarchical )
  94. $categories = & _get_cat_children($child_of, $categories);
  95. // Update category counts to include children.
  96. if ( $pad_counts )
  97. _pad_category_counts($type, $categories);
  98. // Make sure we show empty categories that have children.
  99. if ( $hierarchical && $hide_empty ) {
  100. foreach ( $categories as $k => $category ) {
  101. if ( ! $category->{'link' == $type ? 'link_count' : 'category_count'} ) {
  102. $children = _get_cat_children($category->cat_ID, $categories);
  103. foreach ( $children as $child )
  104. if ( $child->{'link' == $type ? 'link_count' : 'category_count'} )
  105. continue 2;
  106. // It really is empty
  107. unset($categories[$k]);
  108. }
  109. }
  110. }
  111. reset ( $categories );
  112. $cache[ $key ] = $categories;
  113. wp_cache_set( 'get_categories', $cache, 'category' );
  114. return apply_filters('get_categories', $categories, $r);
  115. }
  116. // Retrieves category data given a category ID or category object.
  117. // Handles category caching.
  118. function &get_category(&$category, $output = OBJECT) {
  119. global $wpdb;
  120. if ( empty($category) )
  121. return null;
  122. if ( is_object($category) ) {
  123. wp_cache_add($category->cat_ID, $category, 'category');
  124. $_category = $category;
  125. } else {
  126. if ( ! $_category = wp_cache_get($category, 'category') ) {
  127. $_category = $wpdb->get_row("SELECT * FROM $wpdb->categories WHERE cat_ID = '$category' LIMIT 1");
  128. wp_cache_set($category, $_category, 'category');
  129. }
  130. }
  131. $_category = apply_filters('get_category', $_category);
  132. if ( $output == OBJECT ) {
  133. return $_category;
  134. } elseif ( $output == ARRAY_A ) {
  135. return get_object_vars($_category);
  136. } elseif ( $output == ARRAY_N ) {
  137. return array_values(get_object_vars($_category));
  138. } else {
  139. return $_category;
  140. }
  141. }
  142. function get_category_by_path($category_path, $full_match = true, $output = OBJECT) {
  143. global $wpdb;
  144. $category_path = rawurlencode(urldecode($category_path));
  145. $category_path = str_replace('%2F', '/', $category_path);
  146. $category_path = str_replace('%20', ' ', $category_path);
  147. $category_paths = '/' . trim($category_path, '/');
  148. $leaf_path = sanitize_title(basename($category_paths));
  149. $category_paths = explode('/', $category_paths);
  150. $full_path = '';
  151. foreach ( (array) $category_paths as $pathdir )
  152. $full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title($pathdir);
  153. $categories = $wpdb->get_results("SELECT cat_ID, category_nicename, category_parent FROM $wpdb->categories WHERE category_nicename = '$leaf_path'");
  154. if ( empty($categories) )
  155. return NULL;
  156. foreach ($categories as $category) {
  157. $path = '/' . $leaf_path;
  158. $curcategory = $category;
  159. while ( ($curcategory->category_parent != 0) && ($curcategory->category_parent != $curcategory->cat_ID) ) {
  160. $curcategory = $wpdb->get_row("SELECT cat_ID, category_nicename, category_parent FROM $wpdb->categories WHERE cat_ID = '$curcategory->category_parent'");
  161. $path = '/' . $curcategory->category_nicename . $path;
  162. }
  163. if ( $path == $full_path )
  164. return get_category($category->cat_ID, $output);
  165. }
  166. // If full matching is not required, return the first cat that matches the leaf.
  167. if ( ! $full_match )
  168. return get_category($categories[0]->cat_ID, $output);
  169. return NULL;
  170. }
  171. // Get the ID of a category from its name
  172. function get_cat_ID($cat_name='General') {
  173. global $wpdb;
  174. $cid = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE cat_name='$cat_name'");
  175. return $cid?$cid:1; // default to cat 1
  176. }
  177. // Deprecate
  178. function get_catname($cat_ID) {
  179. return get_cat_name($cat_ID);
  180. }
  181. // Get the name of a category from its ID
  182. function get_cat_name($cat_id) {
  183. $cat_id = (int) $cat_id;
  184. $category = &get_category($cat_id);
  185. return $category->cat_name;
  186. }
  187. function cat_is_ancestor_of($cat1, $cat2) {
  188. if ( is_int($cat1) )
  189. $cat1 = & get_category($cat1);
  190. if ( is_int($cat2) )
  191. $cat2 = & get_category($cat2);
  192. if ( !$cat1->cat_ID || !$cat2->category_parent )
  193. return false;
  194. if ( $cat2->category_parent == $cat1->cat_ID )
  195. return true;
  196. return cat_is_ancestor_of($cat1, get_category($cat2->parent_category));
  197. }
  198. //
  199. // Private
  200. //
  201. function &_get_cat_children($category_id, $categories) {
  202. if ( empty($categories) )
  203. return array();
  204. $category_list = array();
  205. foreach ( $categories as $category ) {
  206. if ( $category->cat_ID == $category_id )
  207. continue;
  208. if ( $category->category_parent == $category_id ) {
  209. $category_list[] = $category;
  210. if ( $children = _get_cat_children($category->cat_ID, $categories) )
  211. $category_list = array_merge($category_list, $children);
  212. }
  213. }
  214. return $category_list;
  215. }
  216. // Recalculates link or post counts by including items from child categories
  217. // Assumes all relevant children are already in the $categories argument
  218. function _pad_category_counts($type, &$categories) {
  219. global $wpdb;
  220. // Set up some useful arrays
  221. foreach ( $categories as $key => $cat ) {
  222. $cats[$cat->cat_ID] = & $categories[$key];
  223. $cat_IDs[] = $cat->cat_ID;
  224. }
  225. // Get the relevant post2cat or link2cat records and stick them in a lookup table
  226. if ( $type == 'post' ) {
  227. $results = $wpdb->get_results("SELECT post_id, category_id FROM $wpdb->post2cat LEFT JOIN $wpdb->posts ON post_id = ID WHERE category_id IN (".join(',', $cat_IDs).") AND post_type = 'post' AND post_status = 'publish'");
  228. foreach ( $results as $row )
  229. ++$cat_items[$row->category_id][$row->post_id];
  230. } else {
  231. $results = $wpdb->get_results("SELECT $wpdb->link2cat.link_id, category_id FROM $wpdb->link2cat LEFT JOIN $wpdb->links USING (link_id) WHERE category_id IN (".join(',', $cat_IDs).") AND link_visible = 'Y'");
  232. foreach ( $results as $row )
  233. ++$cat_items[$row->category_id][$row->link_id];
  234. }
  235. // Touch every ancestor's lookup row for each post in each category
  236. foreach ( $cat_IDs as $cat_ID ) {
  237. $child = $cat_ID;
  238. while ( $parent = $cats[$child]->category_parent ) {
  239. if ( !empty($cat_items[$cat_ID]) )
  240. foreach ( $cat_items[$cat_ID] as $item_id => $touches )
  241. ++$cat_items[$parent][$item_id];
  242. $child = $parent;
  243. }
  244. }
  245. // Transfer the touched cells
  246. foreach ( (array) $cat_items as $id => $items )
  247. if ( isset($cats[$id]) )
  248. $cats[$id]->{'link' == $type ? 'link_count' : 'category_count'} = count($items);
  249. }
  250. ?>