category.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. /**
  3. * Taxonomy API: Core category-specific functionality
  4. *
  5. * @package WordPress
  6. * @subpackage Taxonomy
  7. */
  8. /**
  9. * Retrieves a list of category objects.
  10. *
  11. * If you set the 'taxonomy' argument to 'link_category', the link categories
  12. * will be returned instead.
  13. *
  14. * @since 2.1.0
  15. *
  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. Default 'category'.
  22. * }
  23. * @return array List of category objects.
  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. * @since 1.5.1
  75. *
  76. * @param int|object $category Category ID or category row object.
  77. * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
  78. * correspond to a WP_Term object, an associative array, or a numeric array,
  79. * respectively. Default OBJECT.
  80. * @param string $filter Optional. How to sanitize category fields. Default 'raw'.
  81. * @return object|array|WP_Error|null Category data in type defined by $output parameter.
  82. * WP_Error if $category is empty, null if it does not exist.
  83. */
  84. function get_category( $category, $output = OBJECT, $filter = 'raw' ) {
  85. $category = get_term( $category, 'category', $output, $filter );
  86. if ( is_wp_error( $category ) ) {
  87. return $category;
  88. }
  89. _make_cat_compat( $category );
  90. return $category;
  91. }
  92. /**
  93. * Retrieves a category based on URL containing the category slug.
  94. *
  95. * Breaks the $category_path parameter up to get the category slug.
  96. *
  97. * Tries to find the child path and will return it. If it doesn't find a
  98. * match, then it will return the first category matching slug, if $full_match,
  99. * is set to false. If it does not, then it will return null.
  100. *
  101. * It is also possible that it will return a WP_Error object on failure. Check
  102. * for it when using this function.
  103. *
  104. * @since 2.1.0
  105. *
  106. * @param string $category_path URL containing category slugs.
  107. * @param bool $full_match Optional. Whether full path should be matched.
  108. * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
  109. * correspond to a WP_Term object, an associative array, or a numeric array,
  110. * 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 ( ( 0 != $curcategory->parent ) && ( $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. * Retrieves a category object by category slug.
  159. *
  160. * @since 2.3.0
  161. *
  162. * @param string $slug The category slug.
  163. * @return object|false Category data object on success, false if not found.
  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. * Retrieves 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 Category ID on success, 0 if the category doesn't exist.
  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. * Retrieves 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 the 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. * Checks if a category is an ancestor of another category.
  205. *
  206. * You can use either an ID or the category object for both parameters.
  207. * If you 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 '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 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. *
  250. * @param string|array $args {
  251. * Optional. Arguments to retrieve tags. See get_terms() for additional options.
  252. *
  253. * @type string $taxonomy Taxonomy to retrieve terms for. Default 'post_tag'.
  254. * }
  255. * @return WP_Term[]|int|WP_Error Array of 'post_tag' term objects, a count thereof,
  256. * or WP_Error if any of the taxonomies do not exist.
  257. */
  258. function get_tags( $args = '' ) {
  259. $defaults = array( 'taxonomy' => 'post_tag' );
  260. $args = wp_parse_args( $args, $defaults );
  261. $tags = get_terms( $args );
  262. if ( empty( $tags ) ) {
  263. $tags = array();
  264. } else {
  265. /**
  266. * Filters the array of term objects returned for the 'post_tag' taxonomy.
  267. *
  268. * @since 2.3.0
  269. *
  270. * @param WP_Term[]|int|WP_Error $tags Array of 'post_tag' term objects, a count thereof,
  271. * or WP_Error if any of the taxonomies do not exist.
  272. * @param array $args An array of arguments. @see get_terms()
  273. */
  274. $tags = apply_filters( 'get_tags', $tags, $args );
  275. }
  276. return $tags;
  277. }
  278. /**
  279. * Retrieves a post tag by tag ID or tag object.
  280. *
  281. * If you pass the $tag parameter an object, which is assumed to be the tag row
  282. * object retrieved from the database, it will cache the tag data.
  283. *
  284. * If you pass $tag an integer of the tag ID, then that tag will be retrieved
  285. * from the database, if it isn't already cached, and passed back.
  286. *
  287. * If you look at get_term(), both types will be passed through several filters
  288. * and finally sanitized based on the $filter parameter value.
  289. *
  290. * @since 2.3.0
  291. *
  292. * @param int|WP_Term|object $tag A tag ID or object.
  293. * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
  294. * correspond to a WP_Term object, an associative array, or a numeric array,
  295. * respectively. Default OBJECT.
  296. * @param string $filter Optional. How to sanitize tag fields. Default 'raw'.
  297. * @return WP_Term|array|WP_Error|null Tag data in type defined by $output parameter.
  298. * WP_Error if $tag is empty, null if it does not exist.
  299. */
  300. function get_tag( $tag, $output = OBJECT, $filter = 'raw' ) {
  301. return get_term( $tag, 'post_tag', $output, $filter );
  302. }
  303. /* Cache */
  304. /**
  305. * Removes the category cache data based on ID.
  306. *
  307. * @since 2.1.0
  308. *
  309. * @param int $id Category ID
  310. */
  311. function clean_category_cache( $id ) {
  312. clean_term_cache( $id, 'category' );
  313. }
  314. /**
  315. * Updates category structure to old pre-2.3 from new taxonomy structure.
  316. *
  317. * This function was added for the taxonomy support to update the new category
  318. * structure with the old category one. This will maintain compatibility with
  319. * plugins and themes which depend on the old key or property names.
  320. *
  321. * The parameter should only be passed a variable and not create the array or
  322. * object inline to the parameter. The reason for this is that parameter is
  323. * passed by reference and PHP will fail unless it has the variable.
  324. *
  325. * There is no return value, because everything is updated on the variable you
  326. * pass to it. This is one of the features with using pass by reference in PHP.
  327. *
  328. * @since 2.3.0
  329. * @since 4.4.0 The `$category` parameter now also accepts a WP_Term object.
  330. * @access private
  331. *
  332. * @param array|object|WP_Term $category Category row object or array.
  333. */
  334. function _make_cat_compat( &$category ) {
  335. if ( is_object( $category ) && ! is_wp_error( $category ) ) {
  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. } elseif ( is_array( $category ) && isset( $category['term_id'] ) ) {
  343. $category['cat_ID'] = &$category['term_id'];
  344. $category['category_count'] = &$category['count'];
  345. $category['category_description'] = &$category['description'];
  346. $category['cat_name'] = &$category['name'];
  347. $category['category_nicename'] = &$category['slug'];
  348. $category['category_parent'] = &$category['parent'];
  349. }
  350. }