/wp-includes/category.php

https://gitlab.com/Gashler/sg · PHP · 340 lines · 119 code · 31 blank · 190 comment · 22 complexity · 7fe788f54bdbd3cca6a0f6eb047f560c 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 https://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.
  64. * WP_Error if $category is empty, null if it does not exist.
  65. */
  66. function get_category( $category, $output = OBJECT, $filter = 'raw' ) {
  67. $category = get_term( $category, 'category', $output, $filter );
  68. if ( is_wp_error( $category ) )
  69. return $category;
  70. _make_cat_compat( $category );
  71. return $category;
  72. }
  73. /**
  74. * Retrieve category based on URL containing the category slug.
  75. *
  76. * Breaks the $category_path parameter up to get the category slug.
  77. *
  78. * Tries to find the child path and will return it. If it doesn't find a
  79. * match, then it will return the first category matching slug, if $full_match,
  80. * is set to false. If it does not, then it will return null.
  81. *
  82. * It is also possible that it will return a WP_Error object on failure. Check
  83. * for it when using this function.
  84. *
  85. * @since 2.1.0
  86. *
  87. * @param string $category_path URL containing category slugs.
  88. * @param bool $full_match Optional. Whether full path should be matched.
  89. * @param string $output Optional. Constant OBJECT, ARRAY_A, or ARRAY_N
  90. * @return object|array|WP_Error|void Type is based on $output value.
  91. */
  92. function get_category_by_path( $category_path, $full_match = true, $output = OBJECT ) {
  93. $category_path = rawurlencode( urldecode( $category_path ) );
  94. $category_path = str_replace( '%2F', '/', $category_path );
  95. $category_path = str_replace( '%20', ' ', $category_path );
  96. $category_paths = '/' . trim( $category_path, '/' );
  97. $leaf_path = sanitize_title( basename( $category_paths ) );
  98. $category_paths = explode( '/', $category_paths );
  99. $full_path = '';
  100. foreach ( (array) $category_paths as $pathdir ) {
  101. $full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title( $pathdir );
  102. }
  103. $categories = get_terms( 'category', array('get' => 'all', 'slug' => $leaf_path) );
  104. if ( empty( $categories ) ) {
  105. return;
  106. }
  107. foreach ( $categories as $category ) {
  108. $path = '/' . $leaf_path;
  109. $curcategory = $category;
  110. while ( ( $curcategory->parent != 0 ) && ( $curcategory->parent != $curcategory->term_id ) ) {
  111. $curcategory = get_term( $curcategory->parent, 'category' );
  112. if ( is_wp_error( $curcategory ) ) {
  113. return $curcategory;
  114. }
  115. $path = '/' . $curcategory->slug . $path;
  116. }
  117. if ( $path == $full_path ) {
  118. $category = get_term( $category->term_id, 'category', $output );
  119. _make_cat_compat( $category );
  120. return $category;
  121. }
  122. }
  123. // If full matching is not required, return the first cat that matches the leaf.
  124. if ( ! $full_match ) {
  125. $category = get_term( reset( $categories )->term_id, 'category', $output );
  126. _make_cat_compat( $category );
  127. return $category;
  128. }
  129. }
  130. /**
  131. * Retrieve category object by category slug.
  132. *
  133. * @since 2.3.0
  134. *
  135. * @param string $slug The category slug.
  136. * @return object Category data object
  137. */
  138. function get_category_by_slug( $slug ) {
  139. $category = get_term_by( 'slug', $slug, 'category' );
  140. if ( $category )
  141. _make_cat_compat( $category );
  142. return $category;
  143. }
  144. /**
  145. * Retrieve the ID of a category from its name.
  146. *
  147. * @since 1.0.0
  148. *
  149. * @param string $cat_name Category name.
  150. * @return int 0, if failure and ID of category on success.
  151. */
  152. function get_cat_ID( $cat_name ) {
  153. $cat = get_term_by( 'name', $cat_name, 'category' );
  154. if ( $cat )
  155. return $cat->term_id;
  156. return 0;
  157. }
  158. /**
  159. * Retrieve the name of a category from its ID.
  160. *
  161. * @since 1.0.0
  162. *
  163. * @param int $cat_id Category ID
  164. * @return string Category name, or an empty string if category doesn't exist.
  165. */
  166. function get_cat_name( $cat_id ) {
  167. $cat_id = (int) $cat_id;
  168. $category = get_term( $cat_id, 'category' );
  169. if ( ! $category || is_wp_error( $category ) )
  170. return '';
  171. return $category->name;
  172. }
  173. /**
  174. * Check if a category is an ancestor of another category.
  175. *
  176. * You can use either an id or the category object for both parameters. If you
  177. * use an integer the category will be retrieved.
  178. *
  179. * @since 2.1.0
  180. *
  181. * @param int|object $cat1 ID or object to check if this is the parent category.
  182. * @param int|object $cat2 The child category.
  183. * @return bool Whether $cat2 is child of $cat1
  184. */
  185. function cat_is_ancestor_of( $cat1, $cat2 ) {
  186. return term_is_ancestor_of( $cat1, $cat2, 'category' );
  187. }
  188. /**
  189. * Sanitizes category data based on context.
  190. *
  191. * @since 2.3.0
  192. *
  193. * @param object|array $category Category data
  194. * @param string $context Optional. Default is 'display'.
  195. * @return object|array Same type as $category with sanitized data for safe use.
  196. */
  197. function sanitize_category( $category, $context = 'display' ) {
  198. return sanitize_term( $category, 'category', $context );
  199. }
  200. /**
  201. * Sanitizes data in single category key field.
  202. *
  203. * @since 2.3.0
  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. *
  221. * @param string|array $args Tag arguments to use when retrieving tags.
  222. * @return array List of tags.
  223. */
  224. function get_tags( $args = '' ) {
  225. $tags = get_terms( 'post_tag', $args );
  226. if ( empty( $tags ) ) {
  227. $return = array();
  228. return $return;
  229. }
  230. /**
  231. * Filter the array of term objects returned for the 'post_tag' taxonomy.
  232. *
  233. * @since 2.3.0
  234. *
  235. * @param array $tags Array of 'post_tag' term objects.
  236. * @param array $args An array of arguments. @see get_terms()
  237. */
  238. $tags = apply_filters( 'get_tags', $tags, $args );
  239. return $tags;
  240. }
  241. /**
  242. * Retrieve post tag by tag ID or tag object.
  243. *
  244. * If you pass the $tag parameter an object, which is assumed to be the tag row
  245. * object retrieved the database. It will cache the tag data.
  246. *
  247. * If you pass $tag an integer of the tag ID, then that tag will
  248. * be retrieved from the database, if it isn't already cached, and pass it back.
  249. *
  250. * If you look at get_term(), then both types will be passed through several
  251. * filters and finally sanitized based on the $filter parameter value.
  252. *
  253. * @since 2.3.0
  254. *
  255. * @param int|object $tag
  256. * @param string $output Optional. Constant OBJECT, ARRAY_A, or ARRAY_N
  257. * @param string $filter Optional. Default is raw or no WordPress defined filter will applied.
  258. * @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.
  259. */
  260. function get_tag( $tag, $output = OBJECT, $filter = 'raw' ) {
  261. return get_term( $tag, 'post_tag', $output, $filter );
  262. }
  263. /* Cache */
  264. /**
  265. * Remove the category cache data based on ID.
  266. *
  267. * @since 2.1.0
  268. *
  269. * @param int $id Category ID
  270. */
  271. function clean_category_cache( $id ) {
  272. clean_term_cache( $id, 'category' );
  273. }
  274. /**
  275. * Update category structure to old pre 2.3 from new taxonomy structure.
  276. *
  277. * This function was added for the taxonomy support to update the new category
  278. * structure with the old category one. This will maintain compatibility with
  279. * plugins and themes which depend on the old key or property names.
  280. *
  281. * The parameter should only be passed a variable and not create the array or
  282. * object inline to the parameter. The reason for this is that parameter is
  283. * passed by reference and PHP will fail unless it has the variable.
  284. *
  285. * There is no return value, because everything is updated on the variable you
  286. * pass to it. This is one of the features with using pass by reference in PHP.
  287. *
  288. * @since 2.3.0
  289. * @access private
  290. *
  291. * @param array|object $category Category Row object or array
  292. */
  293. function _make_cat_compat( &$category ) {
  294. if ( is_object( $category ) && ! is_wp_error( $category ) ) {
  295. $category->cat_ID = &$category->term_id;
  296. $category->category_count = &$category->count;
  297. $category->category_description = &$category->description;
  298. $category->cat_name = &$category->name;
  299. $category->category_nicename = &$category->slug;
  300. $category->category_parent = &$category->parent;
  301. } elseif ( is_array( $category ) && isset( $category['term_id'] ) ) {
  302. $category['cat_ID'] = &$category['term_id'];
  303. $category['category_count'] = &$category['count'];
  304. $category['category_description'] = &$category['description'];
  305. $category['cat_name'] = &$category['name'];
  306. $category['category_nicename'] = &$category['slug'];
  307. $category['category_parent'] = &$category['parent'];
  308. }
  309. }