]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/taxonomy.php
Wordpress 2.6.2
[autoinstalls/wordpress.git] / wp-admin / includes / taxonomy.php
1 <?php
2
3 //
4 // Category
5 //
6
7 function category_exists($cat_name) {
8         $id = is_term($cat_name, 'category');
9         if ( is_array($id) )
10                 $id = $id['term_id'];
11         return $id;
12 }
13
14 function get_category_to_edit( $id ) {
15         $category = get_category( $id, OBJECT, 'edit' );
16         return $category;
17 }
18
19 function wp_create_category( $cat_name, $parent = 0 ) {
20         if ( $id = category_exists($cat_name) )
21                 return $id;
22
23         return wp_insert_category( array('cat_name' => $cat_name, 'category_parent' => $parent) );
24 }
25
26 function wp_create_categories($categories, $post_id = '') {
27         $cat_ids = array ();
28         foreach ($categories as $category) {
29                 if ($id = category_exists($category))
30                         $cat_ids[] = $id;
31                 else
32                         if ($id = wp_create_category($category))
33                                 $cat_ids[] = $id;
34         }
35
36         if ($post_id)
37                 wp_set_post_categories($post_id, $cat_ids);
38
39         return $cat_ids;
40 }
41
42 function wp_delete_category($cat_ID) {
43         $cat_ID = (int) $cat_ID;
44         $default = get_option('default_category');
45
46         // Don't delete the default cat
47         if ( $cat_ID == $default )
48                 return 0;
49
50         return wp_delete_term($cat_ID, 'category', array('default' => $default));
51 }
52
53 function wp_insert_category($catarr, $wp_error = false) {
54         $cat_defaults = array('cat_ID' => 0, 'cat_name' => '', 'category_description' => '', 'category_nicename' => '', 'category_parent' => '');
55         $cat_arr = wp_parse_args($cat_arr, $cat_defaults);
56         extract($catarr, EXTR_SKIP);
57
58         if ( trim( $cat_name ) == '' ) {
59                 if ( ! $wp_error )
60                         return 0;
61                 else
62                         return new WP_Error( 'cat_name', __('You did not enter a category name.') );
63         }
64
65         $cat_ID = (int) $cat_ID;
66
67         // Are we updating or creating?
68         if ( !empty ($cat_ID) )
69                 $update = true;
70         else
71                 $update = false;
72
73         $name = $cat_name;
74         $description = $category_description;
75         $slug = $category_nicename;
76         $parent = $category_parent;
77
78         $parent = (int) $parent;
79         if ( $parent < 0 )
80                 $parent = 0;
81
82         if ( empty($parent) || !category_exists( $parent ) || ($cat_ID && cat_is_ancestor_of($cat_ID, $parent) ) )
83                 $parent = 0;
84
85         $args = compact('name', 'slug', 'parent', 'description');
86
87         if ( $update )
88                 $cat_ID = wp_update_term($cat_ID, 'category', $args);
89         else
90                 $cat_ID = wp_insert_term($cat_name, 'category', $args);
91
92         if ( is_wp_error($cat_ID) ) {
93                 if ( $wp_error )
94                         return $cat_ID;
95                 else
96                         return 0;
97         }
98
99         return $cat_ID['term_id'];
100 }
101
102 function wp_update_category($catarr) {
103         $cat_ID = (int) $catarr['cat_ID'];
104
105         if ( $cat_ID == $catarr['category_parent'] )
106                 return false;
107
108         // First, get all of the original fields
109         $category = get_category($cat_ID, ARRAY_A);
110
111         // Escape data pulled from DB.
112         $category = add_magic_quotes($category);
113
114         // Merge old and new fields with new fields overwriting old ones.
115         $catarr = array_merge($category, $catarr);
116
117         return wp_insert_category($catarr);
118 }
119
120 //
121 // Tags
122 //
123
124 function get_tags_to_edit( $post_id ) {
125         $post_id = (int) $post_id;
126         if ( !$post_id )
127                 return false;
128
129         $tags = wp_get_post_tags($post_id);
130
131         if ( !$tags )
132                 return false;
133
134         foreach ( $tags as $tag )
135                 $tag_names[] = $tag->name;
136         $tags_to_edit = join( ',', $tag_names );
137         $tags_to_edit = attribute_escape( $tags_to_edit );
138         $tags_to_edit = apply_filters( 'tags_to_edit', $tags_to_edit );
139         return $tags_to_edit;
140 }
141
142 function tag_exists($tag_name) {
143         return is_term($tag_name, 'post_tag');
144 }
145
146 function wp_create_tag($tag_name) {
147         if ( $id = tag_exists($tag_name) )
148                 return $id;
149
150         return wp_insert_term($tag_name, 'post_tag');
151 }
152
153 ?>