/wp-includes/category.php

  1. <?php 
  2. /** 
  3. * Taxonomy API: Core category-specific functionality 
  4. * 
  5. * @package WordPress 
  6. * @subpackage Taxonomy 
  7. */ 
  8.  
  9. /** 
  10. * Retrieve list of category objects. 
  11. * 
  12. * If you change the type to 'link' in the arguments, then the link categories 
  13. * will be returned instead. Also all categories will be updated to be backward 
  14. * compatible with pre-2.3 plugins and themes. 
  15. * 
  16. * @since 2.1.0 
  17. * @see get_terms() Type of arguments that can be changed. 
  18. * 
  19. * @param string|array $args { 
  20. * Optional. Arguments to retrieve categories. See get_terms() for additional options. 
  21. * 
  22. * @type string $taxonomy Taxonomy to retrieve terms for. In this case, default 'category'. 
  23. * } 
  24. * @return array List of categories. 
  25. */ 
  26. function get_categories( $args = '' ) { 
  27. $defaults = array( 'taxonomy' => 'category' ); 
  28. $args = wp_parse_args( $args, $defaults ); 
  29.  
  30. $taxonomy = $args['taxonomy']; 
  31.  
  32. /** 
  33. * Filters the taxonomy used to retrieve terms when calling get_categories(). 
  34. * 
  35. * @since 2.7.0 
  36. * 
  37. * @param string $taxonomy Taxonomy to retrieve terms from. 
  38. * @param array $args An array of arguments. See get_terms(). 
  39. */ 
  40. $taxonomy = apply_filters( 'get_categories_taxonomy', $taxonomy, $args ); 
  41.  
  42. // Back compat 
  43. if ( isset($args['type']) && 'link' == $args['type'] ) { 
  44. /** translators: 1: "type => link", 2: "taxonomy => link_category" alternative */ 
  45. _deprecated_argument( __FUNCTION__, '3.0.0',  
  46. sprintf( __( '%1$s is deprecated. Use %2$s instead.' ),  
  47. '<code>type => link</code>',  
  48. '<code>taxonomy => link_category</code>' 
  49. ); 
  50. $taxonomy = $args['taxonomy'] = 'link_category'; 
  51.  
  52. $categories = get_terms( $taxonomy, $args ); 
  53.  
  54. if ( is_wp_error( $categories ) ) { 
  55. $categories = array(); 
  56. } else { 
  57. $categories = (array) $categories; 
  58. foreach ( array_keys( $categories ) as $k ) { 
  59. _make_cat_compat( $categories[ $k ] ); 
  60.  
  61. return $categories; 
  62.  
  63. /** 
  64. * Retrieves category data given a category ID or category object. 
  65. * 
  66. * If you pass the $category parameter an object, which is assumed to be the 
  67. * category row object retrieved the database. It will cache the category data. 
  68. * 
  69. * If you pass $category an integer of the category ID, then that category will 
  70. * be retrieved from the database, if it isn't already cached, and pass it back. 
  71. * 
  72. * If you look at get_term(), then both types will be passed through several 
  73. * filters and finally sanitized based on the $filter parameter value. 
  74. * 
  75. * The category will converted to maintain backward compatibility. 
  76. * 
  77. * @since 1.5.1 
  78. * 
  79. * @param int|object $category Category ID or Category row object 
  80. * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a 
  81. * WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT. 
  82. * @param string $filter Optional. Default is raw or no WordPress defined filter will applied. 
  83. * @return object|array|WP_Error|null Category data in type defined by $output parameter. 
  84. * WP_Error if $category is empty, null if it does not exist. 
  85. */ 
  86. function get_category( $category, $output = OBJECT, $filter = 'raw' ) { 
  87. $category = get_term( $category, 'category', $output, $filter ); 
  88.  
  89. if ( is_wp_error( $category ) ) 
  90. return $category; 
  91.  
  92. _make_cat_compat( $category ); 
  93.  
  94. return $category; 
  95.  
  96. /** 
  97. * Retrieve category based on URL containing the category slug. 
  98. * 
  99. * Breaks the $category_path parameter up to get the category slug. 
  100. * 
  101. * Tries to find the child path and will return it. If it doesn't find a 
  102. * match, then it will return the first category matching slug, if $full_match,  
  103. * is set to false. If it does not, then it will return null. 
  104. * 
  105. * It is also possible that it will return a WP_Error object on failure. Check 
  106. * for it when using this function. 
  107. * 
  108. * @since 2.1.0 
  109. * 
  110. * @param string $category_path URL containing category slugs. 
  111. * @param bool $full_match Optional. Whether full path should be matched. 
  112. * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to 
  113. * a WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT. 
  114. * @return WP_Term|array|WP_Error|null Type is based on $output value. 
  115. */ 
  116. function get_category_by_path( $category_path, $full_match = true, $output = OBJECT ) { 
  117. $category_path = rawurlencode( urldecode( $category_path ) ); 
  118. $category_path = str_replace( '%2F', '/', $category_path ); 
  119. $category_path = str_replace( '%20', ' ', $category_path ); 
  120. $category_paths = '/' . trim( $category_path, '/' ); 
  121. $leaf_path = sanitize_title( basename( $category_paths ) ); 
  122. $category_paths = explode( '/', $category_paths ); 
  123. $full_path = ''; 
  124. foreach ( (array) $category_paths as $pathdir ) { 
  125. $full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title( $pathdir ); 
  126. $categories = get_terms( 'category', array('get' => 'all', 'slug' => $leaf_path) ); 
  127.  
  128. if ( empty( $categories ) ) { 
  129. return; 
  130.  
  131. foreach ( $categories as $category ) { 
  132. $path = '/' . $leaf_path; 
  133. $curcategory = $category; 
  134. while ( ( $curcategory->parent != 0 ) && ( $curcategory->parent != $curcategory->term_id ) ) { 
  135. $curcategory = get_term( $curcategory->parent, 'category' ); 
  136. if ( is_wp_error( $curcategory ) ) { 
  137. return $curcategory; 
  138. $path = '/' . $curcategory->slug . $path; 
  139.  
  140. if ( $path == $full_path ) { 
  141. $category = get_term( $category->term_id, 'category', $output ); 
  142. _make_cat_compat( $category ); 
  143. return $category; 
  144.  
  145. // If full matching is not required, return the first cat that matches the leaf. 
  146. if ( ! $full_match ) { 
  147. $category = get_term( reset( $categories )->term_id, 'category', $output ); 
  148. _make_cat_compat( $category ); 
  149. return $category; 
  150.  
  151. /** 
  152. * Retrieve category object by category slug. 
  153. * 
  154. * @since 2.3.0 
  155. * 
  156. * @param string $slug The category slug. 
  157. * @return object Category data object 
  158. */ 
  159. function get_category_by_slug( $slug ) { 
  160. $category = get_term_by( 'slug', $slug, 'category' ); 
  161. if ( $category ) 
  162. _make_cat_compat( $category ); 
  163.  
  164. return $category; 
  165.  
  166. /** 
  167. * Retrieve the ID of a category from its name. 
  168. * 
  169. * @since 1.0.0 
  170. * 
  171. * @param string $cat_name Category name. 
  172. * @return int 0, if failure and ID of category on success. 
  173. */ 
  174. function get_cat_ID( $cat_name ) { 
  175. $cat = get_term_by( 'name', $cat_name, 'category' ); 
  176. if ( $cat ) 
  177. return $cat->term_id; 
  178. return 0; 
  179.  
  180. /** 
  181. * Retrieve the name of a category from its ID. 
  182. * 
  183. * @since 1.0.0 
  184. * 
  185. * @param int $cat_id Category ID 
  186. * @return string Category name, or an empty string if category doesn't exist. 
  187. */ 
  188. function get_cat_name( $cat_id ) { 
  189. $cat_id = (int) $cat_id; 
  190. $category = get_term( $cat_id, 'category' ); 
  191. if ( ! $category || is_wp_error( $category ) ) 
  192. return ''; 
  193. return $category->name; 
  194.  
  195. /** 
  196. * Check if a category is an ancestor of another category. 
  197. * 
  198. * You can use either an id or the category object for both parameters. If you 
  199. * use an integer the category will be retrieved. 
  200. * 
  201. * @since 2.1.0 
  202. * 
  203. * @param int|object $cat1 ID or object to check if this is the parent category. 
  204. * @param int|object $cat2 The child category. 
  205. * @return bool Whether $cat2 is child of $cat1 
  206. */ 
  207. function cat_is_ancestor_of( $cat1, $cat2 ) { 
  208. return term_is_ancestor_of( $cat1, $cat2, 'category' ); 
  209.  
  210. /** 
  211. * Sanitizes category data based on context. 
  212. * 
  213. * @since 2.3.0 
  214. * 
  215. * @param object|array $category Category data 
  216. * @param string $context Optional. Default is 'display'. 
  217. * @return object|array Same type as $category with sanitized data for safe use. 
  218. */ 
  219. function sanitize_category( $category, $context = 'display' ) { 
  220. return sanitize_term( $category, 'category', $context ); 
  221.  
  222. /** 
  223. * Sanitizes data in single category key field. 
  224. * 
  225. * @since 2.3.0 
  226. * 
  227. * @param string $field Category key to sanitize 
  228. * @param mixed $value Category value to sanitize 
  229. * @param int $cat_id Category ID 
  230. * @param string $context What filter to use, 'raw', 'display', etc. 
  231. * @return mixed Same type as $value after $value has been sanitized. 
  232. */ 
  233. function sanitize_category_field( $field, $value, $cat_id, $context ) { 
  234. return sanitize_term_field( $field, $value, $cat_id, 'category', $context ); 
  235.  
  236. /** Tags */ 
  237.  
  238. /** 
  239. * Retrieves all post tags. 
  240. * 
  241. * @since 2.3.0 
  242. * @see get_terms() For list of arguments to pass. 
  243. * 
  244. * @param string|array $args Tag arguments to use when retrieving tags. 
  245. * @return array List of tags. 
  246. */ 
  247. function get_tags( $args = '' ) { 
  248. $tags = get_terms( 'post_tag', $args ); 
  249.  
  250. if ( empty( $tags ) ) { 
  251. $return = array(); 
  252. return $return; 
  253.  
  254. /** 
  255. * Filters the array of term objects returned for the 'post_tag' taxonomy. 
  256. * 
  257. * @since 2.3.0 
  258. * 
  259. * @param array $tags Array of 'post_tag' term objects. 
  260. * @param array $args An array of arguments. @see get_terms() 
  261. */ 
  262. $tags = apply_filters( 'get_tags', $tags, $args ); 
  263. return $tags; 
  264.  
  265. /** 
  266. * Retrieve post tag by tag ID or tag object. 
  267. * 
  268. * If you pass the $tag parameter an object, which is assumed to be the tag row 
  269. * object retrieved the database. It will cache the tag data. 
  270. * 
  271. * If you pass $tag an integer of the tag ID, then that tag will 
  272. * be retrieved from the database, if it isn't already cached, and pass it back. 
  273. * 
  274. * If you look at get_term(), then both types will be passed through several 
  275. * filters and finally sanitized based on the $filter parameter value. 
  276. * 
  277. * @since 2.3.0 
  278. * 
  279. * @param int|WP_Term|object $tag A tag ID or object. 
  280. * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to 
  281. * a WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT. 
  282. * @param string $filter Optional. Default is raw or no WordPress defined filter will applied. 
  283. * @return WP_Term|array|WP_Error|null Tag data in type defined by $output parameter. WP_Error if $tag is empty, null if it does not exist. 
  284. */ 
  285. function get_tag( $tag, $output = OBJECT, $filter = 'raw' ) { 
  286. return get_term( $tag, 'post_tag', $output, $filter ); 
  287.  
  288. /** Cache */ 
  289.  
  290. /** 
  291. * Remove the category cache data based on ID. 
  292. * 
  293. * @since 2.1.0 
  294. * 
  295. * @param int $id Category ID 
  296. */ 
  297. function clean_category_cache( $id ) { 
  298. clean_term_cache( $id, 'category' ); 
  299.  
  300. /** 
  301. * Update category structure to old pre 2.3 from new taxonomy structure. 
  302. * 
  303. * This function was added for the taxonomy support to update the new category 
  304. * structure with the old category one. This will maintain compatibility with 
  305. * plugins and themes which depend on the old key or property names. 
  306. * 
  307. * The parameter should only be passed a variable and not create the array or 
  308. * object inline to the parameter. The reason for this is that parameter is 
  309. * passed by reference and PHP will fail unless it has the variable. 
  310. * 
  311. * There is no return value, because everything is updated on the variable you 
  312. * pass to it. This is one of the features with using pass by reference in PHP. 
  313. * 
  314. * @since 2.3.0 
  315. * @since 4.4.0 The `$category` parameter now also accepts a WP_Term object. 
  316. * @access private 
  317. * 
  318. * @param array|object|WP_Term $category Category Row object or array 
  319. */ 
  320. function _make_cat_compat( &$category ) { 
  321. if ( is_object( $category ) && ! is_wp_error( $category ) ) { 
  322. $category->cat_ID = $category->term_id; 
  323. $category->category_count = $category->count; 
  324. $category->category_description = $category->description; 
  325. $category->cat_name = $category->name; 
  326. $category->category_nicename = $category->slug; 
  327. $category->category_parent = $category->parent; 
  328. } elseif ( is_array( $category ) && isset( $category['term_id'] ) ) { 
  329. $category['cat_ID'] = &$category['term_id']; 
  330. $category['category_count'] = &$category['count']; 
  331. $category['category_description'] = &$category['description']; 
  332. $category['cat_name'] = &$category['name']; 
  333. $category['category_nicename'] = &$category['slug']; 
  334. $category['category_parent'] = &$category['parent']; 
.