PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-admin/includes/taxonomy.php

https://bitbucket.org/dkrzos/phc
PHP | 252 lines | 105 code | 37 blank | 110 comment | 24 complexity | aa8ecc5e5fb9470cdf5d18dfafacedcd MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * WordPress Taxonomy Administration API.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. //
  9. // Category
  10. //
  11. /**
  12. * {@internal Missing Short Description}}
  13. *
  14. * @since 2.0.0
  15. *
  16. * @param unknown_type $cat_name
  17. * @return unknown
  18. */
  19. function category_exists($cat_name, $parent = 0) {
  20. $id = term_exists($cat_name, 'category', $parent);
  21. if ( is_array($id) )
  22. $id = $id['term_id'];
  23. return $id;
  24. }
  25. /**
  26. * {@internal Missing Short Description}}
  27. *
  28. * @since 2.0.0
  29. *
  30. * @param unknown_type $id
  31. * @return unknown
  32. */
  33. function get_category_to_edit( $id ) {
  34. $category = get_category( $id, OBJECT, 'edit' );
  35. return $category;
  36. }
  37. /**
  38. * {@internal Missing Short Description}}
  39. *
  40. * @since 2.0.0
  41. *
  42. * @param unknown_type $cat_name
  43. * @param unknown_type $parent
  44. * @return unknown
  45. */
  46. function wp_create_category( $cat_name, $parent = 0 ) {
  47. if ( $id = category_exists($cat_name, $parent) )
  48. return $id;
  49. return wp_insert_category( array('cat_name' => $cat_name, 'category_parent' => $parent) );
  50. }
  51. /**
  52. * {@internal Missing Short Description}}
  53. *
  54. * @since 2.0.0
  55. *
  56. * @param unknown_type $categories
  57. * @param unknown_type $post_id
  58. * @return unknown
  59. */
  60. function wp_create_categories($categories, $post_id = '') {
  61. $cat_ids = array ();
  62. foreach ($categories as $category) {
  63. if ($id = category_exists($category))
  64. $cat_ids[] = $id;
  65. else
  66. if ($id = wp_create_category($category))
  67. $cat_ids[] = $id;
  68. }
  69. if ( $post_id )
  70. wp_set_post_categories($post_id, $cat_ids);
  71. return $cat_ids;
  72. }
  73. /**
  74. * Updates an existing Category or creates a new Category.
  75. *
  76. * @since 2.0.0
  77. *
  78. * @param mixed $catarr See defaults below. Set 'cat_ID' to a non-zero value to update an existing category. The 'taxonomy' key was added in 3.0.0.
  79. * @param bool $wp_error Optional, since 2.5.0. Set this to true if the caller handles WP_Error return values.
  80. * @return int|object The ID number of the new or updated Category on success. Zero or a WP_Error on failure, depending on param $wp_error.
  81. */
  82. function wp_insert_category($catarr, $wp_error = false) {
  83. $cat_defaults = array('cat_ID' => 0, 'taxonomy' => 'category', 'cat_name' => '', 'category_description' => '', 'category_nicename' => '', 'category_parent' => '');
  84. $catarr = wp_parse_args($catarr, $cat_defaults);
  85. extract($catarr, EXTR_SKIP);
  86. if ( trim( $cat_name ) == '' ) {
  87. if ( ! $wp_error )
  88. return 0;
  89. else
  90. return new WP_Error( 'cat_name', __('You did not enter a category name.') );
  91. }
  92. $cat_ID = (int) $cat_ID;
  93. // Are we updating or creating?
  94. if ( !empty ($cat_ID) )
  95. $update = true;
  96. else
  97. $update = false;
  98. $name = $cat_name;
  99. $description = $category_description;
  100. $slug = $category_nicename;
  101. $parent = $category_parent;
  102. $parent = (int) $parent;
  103. if ( $parent < 0 )
  104. $parent = 0;
  105. if ( empty( $parent ) || ! term_exists( $parent, $taxonomy ) || ( $cat_ID && term_is_ancestor_of( $cat_ID, $parent, $taxonomy ) ) )
  106. $parent = 0;
  107. $args = compact('name', 'slug', 'parent', 'description');
  108. if ( $update )
  109. $cat_ID = wp_update_term($cat_ID, $taxonomy, $args);
  110. else
  111. $cat_ID = wp_insert_term($cat_name, $taxonomy, $args);
  112. if ( is_wp_error($cat_ID) ) {
  113. if ( $wp_error )
  114. return $cat_ID;
  115. else
  116. return 0;
  117. }
  118. return $cat_ID['term_id'];
  119. }
  120. /**
  121. * Aliases wp_insert_category() with minimal args.
  122. *
  123. * If you want to update only some fields of an existing category, call this
  124. * function with only the new values set inside $catarr.
  125. *
  126. * @since 2.0.0
  127. *
  128. * @param array $catarr The 'cat_ID' value is required. All other keys are optional.
  129. * @return int|bool The ID number of the new or updated Category on success. Zero or FALSE on failure.
  130. */
  131. function wp_update_category($catarr) {
  132. $cat_ID = (int) $catarr['cat_ID'];
  133. if ( isset($catarr['category_parent']) && ($cat_ID == $catarr['category_parent']) )
  134. return false;
  135. // First, get all of the original fields
  136. $category = get_category($cat_ID, ARRAY_A);
  137. // Escape data pulled from DB.
  138. $category = add_magic_quotes($category);
  139. // Merge old and new fields with new fields overwriting old ones.
  140. $catarr = array_merge($category, $catarr);
  141. return wp_insert_category($catarr);
  142. }
  143. //
  144. // Tags
  145. //
  146. /**
  147. * {@internal Missing Short Description}}
  148. *
  149. * @since 2.3.0
  150. *
  151. * @param unknown_type $tag_name
  152. * @return unknown
  153. */
  154. function tag_exists($tag_name) {
  155. return term_exists($tag_name, 'post_tag');
  156. }
  157. /**
  158. * {@internal Missing Short Description}}
  159. *
  160. * @since 2.3.0
  161. *
  162. * @param unknown_type $tag_name
  163. * @return unknown
  164. */
  165. function wp_create_tag($tag_name) {
  166. return wp_create_term( $tag_name, 'post_tag');
  167. }
  168. /**
  169. * {@internal Missing Short Description}}
  170. *
  171. * @since 2.3.0
  172. *
  173. * @param unknown_type $post_id
  174. * @return unknown
  175. */
  176. function get_tags_to_edit( $post_id, $taxonomy = 'post_tag' ) {
  177. return get_terms_to_edit( $post_id, $taxonomy);
  178. }
  179. /**
  180. * {@internal Missing Short Description}}
  181. *
  182. * @since 2.8.0
  183. *
  184. * @param unknown_type $post_id
  185. * @return unknown
  186. */
  187. function get_terms_to_edit( $post_id, $taxonomy = 'post_tag' ) {
  188. $post_id = (int) $post_id;
  189. if ( !$post_id )
  190. return false;
  191. $tags = wp_get_post_terms($post_id, $taxonomy, array());
  192. if ( !$tags )
  193. return false;
  194. if ( is_wp_error($tags) )
  195. return $tags;
  196. foreach ( $tags as $tag )
  197. $tag_names[] = $tag->name;
  198. $tags_to_edit = join( ',', $tag_names );
  199. $tags_to_edit = esc_attr( $tags_to_edit );
  200. $tags_to_edit = apply_filters( 'terms_to_edit', $tags_to_edit, $taxonomy );
  201. return $tags_to_edit;
  202. }
  203. /**
  204. * {@internal Missing Short Description}}
  205. *
  206. * @since 2.8.0
  207. *
  208. * @param unknown_type $tag_name
  209. * @return unknown
  210. */
  211. function wp_create_term($tag_name, $taxonomy = 'post_tag') {
  212. if ( $id = term_exists($tag_name, $taxonomy) )
  213. return $id;
  214. return wp_insert_term($tag_name, $taxonomy);
  215. }