Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

291 linhas
7.5 KiB

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