/wp-admin/includes/taxonomy.php

https://github.com/livinglab/openlab · PHP · 314 lines · 140 code · 35 blank · 139 comment · 27 complexity · c8d02f1c61dd106167f3d6f9a2e4e106 MD5 · raw file

  1. <?php
  2. /**
  3. * WordPress Taxonomy Administration API.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. //
  9. // Category.
  10. //
  11. /**
  12. * Check 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 $parent Optional. ID of parent term.
  20. * @return mixed
  21. */
  22. function category_exists( $cat_name, $parent = null ) {
  23. $id = term_exists( $cat_name, 'category', $parent );
  24. if ( is_array( $id ) ) {
  25. $id = $id['term_id'];
  26. }
  27. return $id;
  28. }
  29. /**
  30. * Get 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. * Add 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
  48. * @param int $parent
  49. * @return int|WP_Error
  50. */
  51. function wp_create_category( $cat_name, $parent = 0 ) {
  52. $id = category_exists( $cat_name, $parent );
  53. if ( $id ) {
  54. return $id;
  55. }
  56. return wp_insert_category(
  57. array(
  58. 'cat_name' => $cat_name,
  59. 'category_parent' => $parent,
  60. )
  61. );
  62. }
  63. /**
  64. * Create 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|object 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. * Check 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
  194. */
  195. function tag_exists( $tag_name ) {
  196. return term_exists( $tag_name, 'post_tag' );
  197. }
  198. /**
  199. * Add a new tag to the database if it does not already exist.
  200. *
  201. * @since 2.3.0
  202. *
  203. * @param int|string $tag_name
  204. * @return array|WP_Error
  205. */
  206. function wp_create_tag( $tag_name ) {
  207. return wp_create_term( $tag_name, 'post_tag' );
  208. }
  209. /**
  210. * Get comma-separated list of tags available to edit.
  211. *
  212. * @since 2.3.0
  213. *
  214. * @param int $post_id
  215. * @param string $taxonomy Optional. The taxonomy for which to retrieve terms. Default 'post_tag'.
  216. * @return string|false|WP_Error
  217. */
  218. function get_tags_to_edit( $post_id, $taxonomy = 'post_tag' ) {
  219. return get_terms_to_edit( $post_id, $taxonomy );
  220. }
  221. /**
  222. * Get comma-separated list of terms available to edit for the given post ID.
  223. *
  224. * @since 2.8.0
  225. *
  226. * @param int $post_id
  227. * @param string $taxonomy Optional. The taxonomy for which to retrieve terms. Default 'post_tag'.
  228. * @return string|false|WP_Error
  229. */
  230. function get_terms_to_edit( $post_id, $taxonomy = 'post_tag' ) {
  231. $post_id = (int) $post_id;
  232. if ( ! $post_id ) {
  233. return false;
  234. }
  235. $terms = get_object_term_cache( $post_id, $taxonomy );
  236. if ( false === $terms ) {
  237. $terms = wp_get_object_terms( $post_id, $taxonomy );
  238. wp_cache_add( $post_id, wp_list_pluck( $terms, 'term_id' ), $taxonomy . '_relationships' );
  239. }
  240. if ( ! $terms ) {
  241. return false;
  242. }
  243. if ( is_wp_error( $terms ) ) {
  244. return $terms;
  245. }
  246. $term_names = array();
  247. foreach ( $terms as $term ) {
  248. $term_names[] = $term->name;
  249. }
  250. $terms_to_edit = esc_attr( implode( ',', $term_names ) );
  251. /**
  252. * Filters the comma-separated list of terms available to edit.
  253. *
  254. * @since 2.8.0
  255. *
  256. * @see get_terms_to_edit()
  257. *
  258. * @param string $terms_to_edit A comma-separated list of term names.
  259. * @param string $taxonomy The taxonomy name for which to retrieve terms.
  260. */
  261. $terms_to_edit = apply_filters( 'terms_to_edit', $terms_to_edit, $taxonomy );
  262. return $terms_to_edit;
  263. }
  264. /**
  265. * Add a new term to the database if it does not already exist.
  266. *
  267. * @since 2.8.0
  268. *
  269. * @param string $tag_name The term name.
  270. * @param string $taxonomy Optional. The taxonomy within which to create the term. Default 'post_tag'.
  271. * @return array|WP_Error
  272. */
  273. function wp_create_term( $tag_name, $taxonomy = 'post_tag' ) {
  274. $id = term_exists( $tag_name, $taxonomy );
  275. if ( $id ) {
  276. return $id;
  277. }
  278. return wp_insert_term( $tag_name, $taxonomy );
  279. }