PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/category.php

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