Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 

361 wiersze
12 KiB

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