PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/category.php

https://github.com/socialplanning/opencore-wordpress-mu
PHP | 331 lines | 253 code | 58 blank | 20 comment | 74 complexity | 374e17346a873a201af25191badd5f6a MD5 | raw file
  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_add('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, EXTR_SKIP);
  26. $key = md5( serialize( $r ) );
  27. if ( $cache = wp_cache_get( 'get_categories', 'category' ) )
  28. if ( isset( $cache[ $key ] ) )
  29. return apply_filters('get_categories', $cache[$key], $r);
  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. $children = _get_category_hierarchy();
  95. if ( ! empty($children) )
  96. $categories = & _get_cat_children($child_of, $categories);
  97. }
  98. // Update category counts to include children.
  99. if ( $pad_counts )
  100. _pad_category_counts($type, $categories);
  101. // Make sure we show empty categories that have children.
  102. if ( $hierarchical && $hide_empty ) {
  103. foreach ( $categories as $k => $category ) {
  104. if ( ! $category->{'link' == $type ? 'link_count' : 'category_count'} ) {
  105. $children = _get_cat_children($category->cat_ID, $categories);
  106. foreach ( $children as $child )
  107. if ( $child->{'link' == $type ? 'link_count' : 'category_count'} )
  108. continue 2;
  109. // It really is empty
  110. unset($categories[$k]);
  111. }
  112. }
  113. }
  114. reset ( $categories );
  115. $cache[ $key ] = $categories;
  116. wp_cache_add( 'get_categories', $cache, 'category' );
  117. $categories = apply_filters('get_categories', $categories, $r);
  118. return $categories;
  119. }
  120. // Retrieves category data given a category ID or category object.
  121. // Handles category caching.
  122. function &get_category(&$category, $output = OBJECT) {
  123. global $wpdb;
  124. if ( empty($category) )
  125. return null;
  126. if ( is_object($category) ) {
  127. wp_cache_add($category->cat_ID, $category, 'category');
  128. $_category = $category;
  129. } else {
  130. $category = (int) $category;
  131. if ( ! $_category = wp_cache_get($category, 'category') ) {
  132. $_category = $wpdb->get_row("SELECT * FROM $wpdb->categories WHERE cat_ID = '$category' LIMIT 1");
  133. wp_cache_add($category, $_category, 'category');
  134. }
  135. }
  136. $_category = apply_filters('get_category', $_category);
  137. if ( $output == OBJECT ) {
  138. return $_category;
  139. } elseif ( $output == ARRAY_A ) {
  140. return get_object_vars($_category);
  141. } elseif ( $output == ARRAY_N ) {
  142. return array_values(get_object_vars($_category));
  143. } else {
  144. return $_category;
  145. }
  146. }
  147. function get_category_by_path($category_path, $full_match = true, $output = OBJECT) {
  148. global $wpdb;
  149. $category_path = rawurlencode(urldecode($category_path));
  150. $category_path = str_replace('%2F', '/', $category_path);
  151. $category_path = str_replace('%20', ' ', $category_path);
  152. $category_paths = '/' . trim($category_path, '/');
  153. $leaf_path = sanitize_title(basename($category_paths));
  154. $category_paths = explode('/', $category_paths);
  155. $full_path = '';
  156. foreach ( (array) $category_paths as $pathdir )
  157. $full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title($pathdir);
  158. $categories = $wpdb->get_results("SELECT cat_ID, category_nicename, category_parent FROM $wpdb->categories WHERE category_nicename = '$leaf_path'");
  159. if ( empty($categories) )
  160. return NULL;
  161. foreach ($categories as $category) {
  162. $path = '/' . $leaf_path;
  163. $curcategory = $category;
  164. while ( ($curcategory->category_parent != 0) && ($curcategory->category_parent != $curcategory->cat_ID) ) {
  165. $curcategory = $wpdb->get_row("SELECT cat_ID, category_nicename, category_parent FROM $wpdb->categories WHERE cat_ID = '$curcategory->category_parent'");
  166. $path = '/' . $curcategory->category_nicename . $path;
  167. }
  168. if ( $path == $full_path )
  169. return get_category($category->cat_ID, $output);
  170. }
  171. // If full matching is not required, return the first cat that matches the leaf.
  172. if ( ! $full_match )
  173. return get_category($categories[0]->cat_ID, $output);
  174. return NULL;
  175. }
  176. // Get the ID of a category from its name
  177. function get_cat_ID($cat_name='General') {
  178. global $wpdb;
  179. $cid = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE cat_name='$cat_name'");
  180. return $cid?$cid:1; // default to cat 1
  181. }
  182. // Deprecate
  183. function get_catname($cat_ID) {
  184. return get_cat_name($cat_ID);
  185. }
  186. // Get the name of a category from its ID
  187. function get_cat_name($cat_id) {
  188. $cat_id = (int) $cat_id;
  189. $category = &get_category($cat_id);
  190. return $category->cat_name;
  191. }
  192. function cat_is_ancestor_of($cat1, $cat2) {
  193. if ( is_int($cat1) )
  194. $cat1 = & get_category($cat1);
  195. if ( is_int($cat2) )
  196. $cat2 = & get_category($cat2);
  197. if ( !$cat1->cat_ID || !$cat2->category_parent )
  198. return false;
  199. if ( $cat2->category_parent == $cat1->cat_ID )
  200. return true;
  201. return cat_is_ancestor_of($cat1, get_category($cat2->parent_category));
  202. }
  203. //
  204. // Private
  205. //
  206. function &_get_cat_children($category_id, $categories) {
  207. if ( empty($categories) )
  208. return array();
  209. $category_list = array();
  210. $has_children = _get_category_hierarchy();
  211. if ( ( 0 != $category_id ) && ! isset($has_children[$category_id]) )
  212. return array();
  213. foreach ( $categories as $category ) {
  214. if ( $category->cat_ID == $category_id )
  215. continue;
  216. if ( $category->category_parent == $category_id ) {
  217. $category_list[] = $category;
  218. if ( !isset($has_children[$category->cat_ID]) )
  219. continue;
  220. if ( $children = _get_cat_children($category->cat_ID, $categories) )
  221. $category_list = array_merge($category_list, $children);
  222. }
  223. }
  224. return $category_list;
  225. }
  226. // Recalculates link or post counts by including items from child categories
  227. // Assumes all relevant children are already in the $categories argument
  228. function _pad_category_counts($type, &$categories) {
  229. global $wpdb;
  230. // Set up some useful arrays
  231. foreach ( $categories as $key => $cat ) {
  232. $cats[$cat->cat_ID] = & $categories[$key];
  233. $cat_IDs[] = $cat->cat_ID;
  234. }
  235. // Get the relevant post2cat or link2cat records and stick them in a lookup table
  236. if ( $type == 'post' ) {
  237. $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'");
  238. foreach ( $results as $row )
  239. ++$cat_items[$row->category_id][$row->post_id];
  240. } else {
  241. $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'");
  242. foreach ( $results as $row )
  243. ++$cat_items[$row->category_id][$row->link_id];
  244. }
  245. // Touch every ancestor's lookup row for each post in each category
  246. foreach ( $cat_IDs as $cat_ID ) {
  247. $child = $cat_ID;
  248. while ( $parent = $cats[$child]->category_parent ) {
  249. if ( !empty($cat_items[$cat_ID]) )
  250. foreach ( $cat_items[$cat_ID] as $item_id => $touches )
  251. ++$cat_items[$parent][$item_id];
  252. $child = $parent;
  253. }
  254. }
  255. // Transfer the touched cells
  256. foreach ( (array) $cat_items as $id => $items )
  257. if ( isset($cats[$id]) )
  258. $cats[$id]->{'link' == $type ? 'link_count' : 'category_count'} = count($items);
  259. }
  260. function _get_category_hierarchy() {
  261. $children = get_option('category_children');
  262. if ( is_array($children) )
  263. return $children;
  264. $children = array();
  265. $categories = get_categories('hide_empty=0&hierarchical=0');
  266. foreach ( $categories as $cat ) {
  267. if ( $cat->category_parent > 0 )
  268. $children[$cat->category_parent][] = $cat->cat_ID;
  269. }
  270. update_option('category_children', $children);
  271. return $children;
  272. }
  273. ?>