category.php 12 KB

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