taxonomy.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. /**
  3. * WordPress Taxonomy Administration API.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. //
  9. // Category.
  10. //
  11. /**
  12. * Checks whether a category exists.
  13. *
  14. * @since 2.0.0
  15. *
  16. * @see term_exists()
  17. *
  18. * @param int|string $cat_name Category name.
  19. * @param int $category_parent Optional. ID of parent category.
  20. * @return string|null Returns the category ID as a numeric string if the pairing exists, null if not.
  21. */
  22. function category_exists( $cat_name, $category_parent = null ) {
  23. $id = term_exists( $cat_name, 'category', $category_parent );
  24. if ( is_array( $id ) ) {
  25. $id = $id['term_id'];
  26. }
  27. return $id;
  28. }
  29. /**
  30. * Gets category object for given ID and 'edit' filter context.
  31. *
  32. * @since 2.0.0
  33. *
  34. * @param int $id
  35. * @return object
  36. */
  37. function get_category_to_edit( $id ) {
  38. $category = get_term( $id, 'category', OBJECT, 'edit' );
  39. _make_cat_compat( $category );
  40. return $category;
  41. }
  42. /**
  43. * Adds a new category to the database if it does not already exist.
  44. *
  45. * @since 2.0.0
  46. *
  47. * @param int|string $cat_name Category name.
  48. * @param int $category_parent Optional. ID of parent category.
  49. * @return int|WP_Error
  50. */
  51. function wp_create_category( $cat_name, $category_parent = 0 ) {
  52. $id = category_exists( $cat_name, $category_parent );
  53. if ( $id ) {
  54. return $id;
  55. }
  56. return wp_insert_category(
  57. array(
  58. 'cat_name' => $cat_name,
  59. 'category_parent' => $category_parent,
  60. )
  61. );
  62. }
  63. /**
  64. * Creates categories for the given post.
  65. *
  66. * @since 2.0.0
  67. *
  68. * @param string[] $categories Array of category names to create.
  69. * @param int $post_id Optional. The post ID. Default empty.
  70. * @return int[] Array of IDs of categories assigned to the given post.
  71. */
  72. function wp_create_categories( $categories, $post_id = '' ) {
  73. $cat_ids = array();
  74. foreach ( $categories as $category ) {
  75. $id = category_exists( $category );
  76. if ( $id ) {
  77. $cat_ids[] = $id;
  78. } else {
  79. $id = wp_create_category( $category );
  80. if ( $id ) {
  81. $cat_ids[] = $id;
  82. }
  83. }
  84. }
  85. if ( $post_id ) {
  86. wp_set_post_categories( $post_id, $cat_ids );
  87. }
  88. return $cat_ids;
  89. }
  90. /**
  91. * Updates an existing Category or creates a new Category.
  92. *
  93. * @since 2.0.0
  94. * @since 2.5.0 $wp_error parameter was added.
  95. * @since 3.0.0 The 'taxonomy' argument was added.
  96. *
  97. * @param array $catarr {
  98. * Array of arguments for inserting a new category.
  99. *
  100. * @type int $cat_ID Category ID. A non-zero value updates an existing category.
  101. * Default 0.
  102. * @type string $taxonomy Taxonomy slug. Default 'category'.
  103. * @type string $cat_name Category name. Default empty.
  104. * @type string $category_description Category description. Default empty.
  105. * @type string $category_nicename Category nice (display) name. Default empty.
  106. * @type int|string $category_parent Category parent ID. Default empty.
  107. * }
  108. * @param bool $wp_error Optional. Default false.
  109. * @return int|WP_Error The ID number of the new or updated Category on success. Zero or a WP_Error on failure,
  110. * depending on param `$wp_error`.
  111. */
  112. function wp_insert_category( $catarr, $wp_error = false ) {
  113. $cat_defaults = array(
  114. 'cat_ID' => 0,
  115. 'taxonomy' => 'category',
  116. 'cat_name' => '',
  117. 'category_description' => '',
  118. 'category_nicename' => '',
  119. 'category_parent' => '',
  120. );
  121. $catarr = wp_parse_args( $catarr, $cat_defaults );
  122. if ( '' === trim( $catarr['cat_name'] ) ) {
  123. if ( ! $wp_error ) {
  124. return 0;
  125. } else {
  126. return new WP_Error( 'cat_name', __( 'You did not enter a category name.' ) );
  127. }
  128. }
  129. $catarr['cat_ID'] = (int) $catarr['cat_ID'];
  130. // Are we updating or creating?
  131. $update = ! empty( $catarr['cat_ID'] );
  132. $name = $catarr['cat_name'];
  133. $description = $catarr['category_description'];
  134. $slug = $catarr['category_nicename'];
  135. $parent = (int) $catarr['category_parent'];
  136. if ( $parent < 0 ) {
  137. $parent = 0;
  138. }
  139. if ( empty( $parent )
  140. || ! term_exists( $parent, $catarr['taxonomy'] )
  141. || ( $catarr['cat_ID'] && term_is_ancestor_of( $catarr['cat_ID'], $parent, $catarr['taxonomy'] ) ) ) {
  142. $parent = 0;
  143. }
  144. $args = compact( 'name', 'slug', 'parent', 'description' );
  145. if ( $update ) {
  146. $catarr['cat_ID'] = wp_update_term( $catarr['cat_ID'], $catarr['taxonomy'], $args );
  147. } else {
  148. $catarr['cat_ID'] = wp_insert_term( $catarr['cat_name'], $catarr['taxonomy'], $args );
  149. }
  150. if ( is_wp_error( $catarr['cat_ID'] ) ) {
  151. if ( $wp_error ) {
  152. return $catarr['cat_ID'];
  153. } else {
  154. return 0;
  155. }
  156. }
  157. return $catarr['cat_ID']['term_id'];
  158. }
  159. /**
  160. * Aliases wp_insert_category() with minimal args.
  161. *
  162. * If you want to update only some fields of an existing category, call this
  163. * function with only the new values set inside $catarr.
  164. *
  165. * @since 2.0.0
  166. *
  167. * @param array $catarr The 'cat_ID' value is required. All other keys are optional.
  168. * @return int|false The ID number of the new or updated Category on success. Zero or FALSE on failure.
  169. */
  170. function wp_update_category( $catarr ) {
  171. $cat_ID = (int) $catarr['cat_ID'];
  172. if ( isset( $catarr['category_parent'] ) && ( $cat_ID == $catarr['category_parent'] ) ) {
  173. return false;
  174. }
  175. // First, get all of the original fields.
  176. $category = get_term( $cat_ID, 'category', ARRAY_A );
  177. _make_cat_compat( $category );
  178. // Escape data pulled from DB.
  179. $category = wp_slash( $category );
  180. // Merge old and new fields with new fields overwriting old ones.
  181. $catarr = array_merge( $category, $catarr );
  182. return wp_insert_category( $catarr );
  183. }
  184. //
  185. // Tags.
  186. //
  187. /**
  188. * Checks whether a post tag with a given name exists.
  189. *
  190. * @since 2.3.0
  191. *
  192. * @param int|string $tag_name
  193. * @return mixed Returns null if the term does not exist.
  194. * Returns an array of the term ID and the term taxonomy ID if the pairing exists.
  195. * Returns 0 if term ID 0 is passed to the function.
  196. */
  197. function tag_exists( $tag_name ) {
  198. return term_exists( $tag_name, 'post_tag' );
  199. }
  200. /**
  201. * Adds a new tag to the database if it does not already exist.
  202. *
  203. * @since 2.3.0
  204. *
  205. * @param int|string $tag_name
  206. * @return array|WP_Error
  207. */
  208. function wp_create_tag( $tag_name ) {
  209. return wp_create_term( $tag_name, 'post_tag' );
  210. }
  211. /**
  212. * Gets comma-separated list of tags available to edit.
  213. *
  214. * @since 2.3.0
  215. *
  216. * @param int $post_id
  217. * @param string $taxonomy Optional. The taxonomy for which to retrieve terms. Default 'post_tag'.
  218. * @return string|false|WP_Error
  219. */
  220. function get_tags_to_edit( $post_id, $taxonomy = 'post_tag' ) {
  221. return get_terms_to_edit( $post_id, $taxonomy );
  222. }
  223. /**
  224. * Gets comma-separated list of terms available to edit for the given post ID.
  225. *
  226. * @since 2.8.0
  227. *
  228. * @param int $post_id
  229. * @param string $taxonomy Optional. The taxonomy for which to retrieve terms. Default 'post_tag'.
  230. * @return string|false|WP_Error
  231. */
  232. function get_terms_to_edit( $post_id, $taxonomy = 'post_tag' ) {
  233. $post_id = (int) $post_id;
  234. if ( ! $post_id ) {
  235. return false;
  236. }
  237. $terms = get_object_term_cache( $post_id, $taxonomy );
  238. if ( false === $terms ) {
  239. $terms = wp_get_object_terms( $post_id, $taxonomy );
  240. wp_cache_add( $post_id, wp_list_pluck( $terms, 'term_id' ), $taxonomy . '_relationships' );
  241. }
  242. if ( ! $terms ) {
  243. return false;
  244. }
  245. if ( is_wp_error( $terms ) ) {
  246. return $terms;
  247. }
  248. $term_names = array();
  249. foreach ( $terms as $term ) {
  250. $term_names[] = $term->name;
  251. }
  252. $terms_to_edit = esc_attr( implode( ',', $term_names ) );
  253. /**
  254. * Filters the comma-separated list of terms available to edit.
  255. *
  256. * @since 2.8.0
  257. *
  258. * @see get_terms_to_edit()
  259. *
  260. * @param string $terms_to_edit A comma-separated list of term names.
  261. * @param string $taxonomy The taxonomy name for which to retrieve terms.
  262. */
  263. $terms_to_edit = apply_filters( 'terms_to_edit', $terms_to_edit, $taxonomy );
  264. return $terms_to_edit;
  265. }
  266. /**
  267. * Adds a new term to the database if it does not already exist.
  268. *
  269. * @since 2.8.0
  270. *
  271. * @param string $tag_name The term name.
  272. * @param string $taxonomy Optional. The taxonomy within which to create the term. Default 'post_tag'.
  273. * @return array|WP_Error
  274. */
  275. function wp_create_term( $tag_name, $taxonomy = 'post_tag' ) {
  276. $id = term_exists( $tag_name, $taxonomy );
  277. if ( $id ) {
  278. return $id;
  279. }
  280. return wp_insert_term( $tag_name, $taxonomy );
  281. }