/wp-includes/category.php

https://github.com/mhoofman/wordpress-heroku · PHP · 339 lines · 117 code · 33 blank · 189 comment · 21 complexity · 1e9b2359a41c9a8ec44cb1e002146f5a MD5 · raw file

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