PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/taxonomy.php

https://gitlab.com/endomorphosis/reservationtelco
PHP | 271 lines | 112 code | 40 blank | 119 comment | 26 complexity | 51e016f11300c655bfdc08ad5d60b890 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. * {@internal Missing Short Description}}
  13. *
  14. * @since unknown
  15. *
  16. * @param unknown_type $cat_name
  17. * @return unknown
  18. */
  19. function category_exists($cat_name, $parent = 0) {
  20. $id = is_term($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 unknown
  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 unknown
  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 unknown
  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. * Deletes one existing category.
  75. *
  76. * @since 2.0.0
  77. *
  78. * @param int $cat_ID
  79. * @return mixed Returns true if completes delete action; false if term doesnt exist; Zero on attempted deletion of default Category; WP_Error object is also a possibility.
  80. */
  81. function wp_delete_category($cat_ID) {
  82. $cat_ID = (int) $cat_ID;
  83. $default = get_option('default_category');
  84. // Don't delete the default cat
  85. if ( $cat_ID == $default )
  86. return 0;
  87. return wp_delete_term($cat_ID, 'category', array('default' => $default));
  88. }
  89. /**
  90. * Updates an existing Category or creates a new Category.
  91. *
  92. * @since 2.0.0
  93. *
  94. * @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.
  95. * @param bool $wp_error Optional, since 2.5.0. Set this to true if the caller handles WP_Error return values.
  96. * @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.
  97. */
  98. function wp_insert_category($catarr, $wp_error = false) {
  99. $cat_defaults = array('cat_ID' => 0, 'taxonomy' => 'category', 'cat_name' => '', 'category_description' => '', 'category_nicename' => '', 'category_parent' => '');
  100. $catarr = wp_parse_args($catarr, $cat_defaults);
  101. extract($catarr, EXTR_SKIP);
  102. if ( trim( $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. $cat_ID = (int) $cat_ID;
  109. // Are we updating or creating?
  110. if ( !empty ($cat_ID) )
  111. $update = true;
  112. else
  113. $update = false;
  114. $name = $cat_name;
  115. $description = $category_description;
  116. $slug = $category_nicename;
  117. $parent = $category_parent;
  118. $parent = (int) $parent;
  119. if ( $parent < 0 )
  120. $parent = 0;
  121. if ( empty($parent) || !category_exists( $parent ) || ($cat_ID && cat_is_ancestor_of($cat_ID, $parent) ) )
  122. $parent = 0;
  123. $args = compact('name', 'slug', 'parent', 'description');
  124. if ( $update )
  125. $cat_ID = wp_update_term($cat_ID, $taxonomy, $args);
  126. else
  127. $cat_ID = wp_insert_term($cat_name, $taxonomy, $args);
  128. if ( is_wp_error($cat_ID) ) {
  129. if ( $wp_error )
  130. return $cat_ID;
  131. else
  132. return 0;
  133. }
  134. return $cat_ID['term_id'];
  135. }
  136. /**
  137. * Aliases wp_insert_category() with minimal args.
  138. *
  139. * If you want to update only some fields of an existing category, call this
  140. * function with only the new values set inside $catarr.
  141. *
  142. * @since 2.0.0
  143. *
  144. * @param array $catarr The 'cat_ID' value is required. All other keys are optional.
  145. * @return int|bool The ID number of the new or updated Category on success. Zero or FALSE on failure.
  146. */
  147. function wp_update_category($catarr) {
  148. $cat_ID = (int) $catarr['cat_ID'];
  149. if ( isset($catarr['category_parent']) && ($cat_ID == $catarr['category_parent']) )
  150. return false;
  151. // First, get all of the original fields
  152. $category = get_category($cat_ID, ARRAY_A);
  153. // Escape data pulled from DB.
  154. $category = add_magic_quotes($category);
  155. // Merge old and new fields with new fields overwriting old ones.
  156. $catarr = array_merge($category, $catarr);
  157. return wp_insert_category($catarr);
  158. }
  159. //
  160. // Tags
  161. //
  162. /**
  163. * {@internal Missing Short Description}}
  164. *
  165. * @since unknown
  166. *
  167. * @param unknown_type $post_id
  168. * @return unknown
  169. */
  170. function get_tags_to_edit( $post_id, $taxonomy = 'post_tag' ) {
  171. return get_terms_to_edit( $post_id, $taxonomy);
  172. }
  173. /**
  174. * {@internal Missing Short Description}}
  175. *
  176. * @since unknown
  177. *
  178. * @param unknown_type $post_id
  179. * @return unknown
  180. */
  181. function get_terms_to_edit( $post_id, $taxonomy = 'post_tag' ) {
  182. $post_id = (int) $post_id;
  183. if ( !$post_id )
  184. return false;
  185. $tags = wp_get_post_terms($post_id, $taxonomy, array());
  186. if ( !$tags )
  187. return false;
  188. if ( is_wp_error($tags) )
  189. return $tags;
  190. foreach ( $tags as $tag )
  191. $tag_names[] = $tag->name;
  192. $tags_to_edit = join( ',', $tag_names );
  193. $tags_to_edit = esc_attr( $tags_to_edit );
  194. $tags_to_edit = apply_filters( 'terms_to_edit', $tags_to_edit, $taxonomy );
  195. return $tags_to_edit;
  196. }
  197. /**
  198. * {@internal Missing Short Description}}
  199. *
  200. * @since unknown
  201. *
  202. * @param unknown_type $tag_name
  203. * @return unknown
  204. */
  205. function tag_exists($tag_name) {
  206. return is_term($tag_name, 'post_tag');
  207. }
  208. /**
  209. * {@internal Missing Short Description}}
  210. *
  211. * @since unknown
  212. *
  213. * @param unknown_type $tag_name
  214. * @return unknown
  215. */
  216. function wp_create_tag($tag_name) {
  217. return wp_create_term( $tag_name, 'post_tag');
  218. }
  219. /**
  220. * {@internal Missing Short Description}}
  221. *
  222. * @since unknown
  223. *
  224. * @param unknown_type $tag_name
  225. * @return unknown
  226. */
  227. function wp_create_term($tag_name, $taxonomy = 'post_tag') {
  228. if ( $id = is_term($tag_name, $taxonomy) )
  229. return $id;
  230. return wp_insert_term($tag_name, $taxonomy);
  231. }