root/tags/1.3/wp-admin/includes/taxonomy.php

Revision 1069, 2.9 kB (checked in by donncha, 1 year ago)

Merge with WP 2.3 - testing use only!
Move pluggable functions out of wpmu-functions and into pluggable.php, fixes #439

Line 
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) {
20     if ( $id = category_exists($cat_name) )
21         return $id;
22
23     return wp_insert_category( array('cat_name' => $cat_name) );
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     global $wpdb;
44
45     $cat_ID = (int) $cat_ID;
46     $default = get_option('default_category');
47
48     // Don't delete the default cat
49     if ( $cat_ID == $default )
50         return 0;
51
52     return wp_delete_term($cat_ID, 'category', "default=$default");
53 }
54
55 function wp_insert_category($catarr) {
56     global $wpdb;
57
58     extract($catarr, EXTR_SKIP);
59
60     if ( trim( $cat_name ) == '' )
61         return 0;
62
63     $cat_ID = (int) $cat_ID;
64
65     // Are we updating or creating?
66     if ( !empty ($cat_ID) )
67         $update = true;
68     else
69         $update = false;
70
71     $name = $cat_name;
72     $description = $category_description;
73     $slug = $category_nicename;
74     $parent = $category_parent;
75
76     $parent = (int) $parent;
77     if ( empty($parent) || !category_exists( $parent ) || ($cat_ID && cat_is_ancestor_of($cat_ID, $parent) ) )
78         $parent = 0;
79
80     $args = compact('name', 'slug', 'parent', 'description');
81
82     if ( $update )
83         $cat_ID = wp_update_term($cat_ID, 'category', $args);
84     else
85         $cat_ID = wp_insert_term($cat_name, 'category', $args);
86
87     if ( is_wp_error($cat_ID) )
88         return 0;
89
90     return $cat_ID['term_id'];
91 }
92
93 function wp_update_category($catarr) {
94     global $wpdb;
95
96     $cat_ID = (int) $catarr['cat_ID'];
97
98     if ( $cat_ID == $catarr['category_parent'] )
99         return false;
100
101     // First, get all of the original fields
102     $category = get_category($cat_ID, ARRAY_A);
103
104     // Escape data pulled from DB.
105     $category = add_magic_quotes($category);
106
107     // Merge old and new fields with new fields overwriting old ones.
108     $catarr = array_merge($category, $catarr);
109
110     return wp_insert_category($catarr);
111 }
112
113 //
114 // Tags
115 //
116
117 function get_tags_to_edit( $post_id ) {
118     global $wpdb;
119
120     $post_id = (int) $post_id;
121     if ( !$post_id )
122         return false;
123
124     $tags = wp_get_post_tags($post_id);
125
126     if ( !$tags )
127         return false;
128
129     foreach ( $tags as $tag )
130         $tag_names[] = $tag->name;
131     $tags_to_edit = join( ', ', $tag_names );
132     $tags_to_edit = attribute_escape( $tags_to_edit );
133     $tags_to_edit = apply_filters( 'tags_to_edit', $tags_to_edit );
134     return $tags_to_edit;
135 }
136
137 function tag_exists($tag_name) {
138     return is_term($tag_name, 'post_tag');
139 }
140
141 function wp_create_tag($tag_name) {
142     if ( $id = tag_exists($tag_name) )
143         return $id;
144
145     return wp_insert_term($tag_name, 'post_tag');
146 }
147
148 ?>
Note: See TracBrowser for help on using the browser.