]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/admin-db.php
Wordpress 2.0.4
[autoinstalls/wordpress.git] / wp-admin / admin-db.php
1 <?php
2
3 function get_users_drafts( $user_id ) {
4         global $wpdb;
5         $user_id = (int) $user_id;
6         $query = "SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'draft' AND post_author = $user_id ORDER BY ID DESC";
7         $query = apply_filters('get_users_drafts', $query);
8         return $wpdb->get_results( $query );
9 }
10
11 function get_others_drafts( $user_id ) {
12         global $wpdb;
13         $user = get_userdata( $user_id );
14         $level_key = $wpdb->prefix . 'user_level';
15
16         $editable = get_editable_user_ids( $user_id );
17         
18         if( !$editable ) {
19                 $other_drafts = '';
20         } else {
21                 $editable = join(',', $editable);
22                 $other_drafts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'draft' AND post_author IN ($editable) AND post_author != '$user_id' ");
23         }
24
25         return apply_filters('get_others_drafts', $other_drafts);
26 }
27
28 function get_editable_authors( $user_id ) {
29         global $wpdb;
30
31         $editable = get_editable_user_ids( $user_id );
32
33         if( !$editable ) {
34                 return false;
35         } else {
36                 $editable = join(',', $editable);
37                 $authors = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($editable)" );
38         }
39
40         return apply_filters('get_editable_authors', $authors);
41 }
42
43 function get_editable_user_ids( $user_id, $exclude_zeros = true ) {
44         global $wpdb;
45         
46         $user = new WP_User( $user_id );
47         
48         if ( ! $user->has_cap('edit_others_posts') ) {
49                 if ( $user->has_cap('edit_posts') || $exclude_zeros == false )
50                         return array($user->id);
51                 else 
52                         return false;
53         }
54
55         $level_key = $wpdb->prefix . 'user_level';
56
57         $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key'";
58         if ( $exclude_zeros )
59                 $query .= " AND meta_value != '0'";
60                 
61         return $wpdb->get_col( $query );
62 }
63
64 function get_author_user_ids() {
65         global $wpdb;
66         $level_key = $wpdb->prefix . 'user_level';
67
68         $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key' AND meta_value != '0'";
69
70         return $wpdb->get_col( $query );
71 }
72
73 function get_nonauthor_user_ids() {
74         global $wpdb;
75         $level_key = $wpdb->prefix . 'user_level';
76
77         $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key' AND meta_value = '0'";
78
79         return $wpdb->get_col( $query );
80 }
81
82 function wp_insert_category($catarr) {
83         global $wpdb;
84
85         extract($catarr);
86
87         $cat_ID = (int) $cat_ID;
88
89         // Are we updating or creating?
90         if (!empty ($cat_ID))
91                 $update = true;
92         else
93                 $update = false;
94
95         $cat_name = apply_filters('pre_category_name', $cat_name);
96         
97         if (empty ($category_nicename))
98                 $category_nicename = sanitize_title($cat_name);
99         else
100                 $category_nicename = sanitize_title($category_nicename);
101         $category_nicename = apply_filters('pre_category_nicename', $category_nicename);
102
103         if (empty ($category_description))
104                 $category_description = '';
105         $category_description = apply_filters('pre_category_description', $category_description);
106
107         $category_parent = (int) $category_parent;
108         if (empty ($category_parent))
109                 $category_parent = 0;
110
111         if (!$update) {
112                 $wpdb->query("INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_description, category_parent) VALUES ('0', '$cat_name', '$category_nicename', '$category_description', '$category_parent')");
113                 $cat_ID = $wpdb->insert_id;
114         } else {
115                 $wpdb->query ("UPDATE $wpdb->categories SET cat_name = '$cat_name', category_nicename = '$category_nicename', category_description = '$category_description', category_parent = '$category_parent' WHERE cat_ID = '$cat_ID'");
116         }
117         
118         if ( $category_nicename == '' ) {
119                 $category_nicename = sanitize_title($cat_name, $cat_ID );
120                 $wpdb->query( "UPDATE $wpdb->categories SET category_nicename = '$category_nicename' WHERE cat_ID = '$cat_ID'" );
121         }
122
123         wp_cache_delete($cat_ID, 'category');
124
125         if ($update) {
126                 do_action('edit_category', $cat_ID);
127         } else {
128                 wp_cache_delete('all_category_ids', 'category');
129                 do_action('create_category', $cat_ID);
130                 do_action('add_category', $cat_ID);
131         }
132
133         return $cat_ID;
134 }
135
136 function wp_update_category($catarr) {
137         global $wpdb;
138
139         $cat_ID = (int) $catarr['cat_ID'];
140
141         // First, get all of the original fields
142         $category = get_category($cat_ID, ARRAY_A);
143
144         // Escape data pulled from DB.
145         $category = add_magic_quotes($category);
146
147         // Merge old and new fields with new fields overwriting old ones.
148         $catarr = array_merge($category, $catarr);
149
150         return wp_insert_category($catarr);
151 }
152
153 function wp_delete_category($cat_ID) {
154         global $wpdb;
155
156         $cat_ID = (int) $cat_ID;
157
158         // Don't delete the default cat.
159         if ($cat_ID == get_option('default_category'))
160                 return 0;
161
162         $category = get_category($cat_ID);
163
164         $parent = $category->category_parent;
165
166         // Delete the category.
167         $wpdb->query("DELETE FROM $wpdb->categories WHERE cat_ID = '$cat_ID'");
168
169         // Update children to point to new parent.
170         $wpdb->query("UPDATE $wpdb->categories SET category_parent = '$parent' WHERE category_parent = '$cat_ID'");
171
172         // TODO: Only set categories to general if they're not in another category already
173         $default_cat = get_option('default_category');
174         $wpdb->query("UPDATE $wpdb->post2cat SET category_id='$default_cat' WHERE category_id='$cat_ID'");
175
176         wp_cache_delete($cat_ID, 'category');
177         wp_cache_delete('all_category_ids', 'category');
178
179         do_action('delete_category', $cat_ID);
180
181         return 1;
182 }
183
184 function wp_create_category($cat_name) {
185         $cat_array = compact('cat_name');
186         return wp_insert_category($cat_array);
187 }
188
189 function wp_create_categories($categories, $post_id = '') {
190         $cat_ids = array ();
191         foreach ($categories as $category) {
192                 if ($id = category_exists($category))
193                         $cat_ids[] = $id;
194                 else
195                         if ($id = wp_create_category($category))
196                                 $cat_ids[] = $id;
197         }
198
199         if ($post_id)
200                 wp_set_post_cats('', $post_id, $cat_ids);
201
202         return $cat_ids;
203 }
204
205 function category_exists($cat_name) {
206         global $wpdb;
207         if (!$category_nicename = sanitize_title($cat_name))
208                 return 0;
209
210         return $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '$category_nicename'");
211 }
212
213 function wp_delete_user($id, $reassign = 'novalue') {
214         global $wpdb;
215
216         $id = (int) $id;
217         $user = get_userdata($id);
218
219         if ($reassign == 'novalue') {
220                 $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_author = $id");
221
222                 if ($post_ids) {
223                         foreach ($post_ids as $post_id)
224                                 wp_delete_post($post_id);
225                 }
226
227                 // Clean links
228                 $wpdb->query("DELETE FROM $wpdb->links WHERE link_owner = $id");
229         } else {
230                 $reassign = (int) $reassign;
231                 $wpdb->query("UPDATE $wpdb->posts SET post_author = {$reassign} WHERE post_author = {$id}");
232                 $wpdb->query("UPDATE $wpdb->links SET link_owner = {$reassign} WHERE link_owner = {$id}");
233         }
234
235         // FINALLY, delete user
236         $wpdb->query("DELETE FROM $wpdb->users WHERE ID = $id");
237         $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = '$id'");
238
239         wp_cache_delete($id, 'users');
240         wp_cache_delete($user->user_login, 'userlogins');
241
242         do_action('delete_user', $id);
243
244         return true;
245 }
246
247 function get_link($link_id, $output = OBJECT) {
248         global $wpdb;
249         
250         $link = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = '$link_id'");
251
252         if ( $output == OBJECT ) {
253                 return $link;
254         } elseif ( $output == ARRAY_A ) {
255                 return get_object_vars($link);
256         } elseif ( $output == ARRAY_N ) {
257                 return array_values(get_object_vars($link));
258         } else {
259                 return $link;
260         }
261 }
262
263 function wp_insert_link($linkdata) {
264         global $wpdb, $current_user;
265         
266         extract($linkdata);
267
268         $update = false;
269         if ( !empty($link_id) )
270                 $update = true;
271
272         if ( empty($link_rating) )
273                 $link_rating = 0;       
274
275         if ( empty($link_target) )
276                 $link_target = '';      
277
278         if ( empty($link_visible) )
279                 $link_visible = 'Y';
280                 
281         if ( empty($link_owner) )
282                 $link_owner = $current_user->id;
283
284         if ( empty($link_notes) )
285                 $link_notes = '';
286
287         if ( $update ) {
288                 $wpdb->query("UPDATE $wpdb->links SET link_url='$link_url',
289                         link_name='$link_name', link_image='$link_image',
290                         link_target='$link_target', link_category='$link_category',
291                         link_visible='$link_visible', link_description='$link_description',
292                         link_rating='$link_rating', link_rel='$link_rel',
293                         link_notes='$link_notes', link_rss = '$link_rss'
294                         WHERE link_id='$link_id'");
295         } else {
296                 $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_image, link_target, link_category, link_description, link_visible, link_owner, link_rating, link_rel, link_notes, link_rss) VALUES('$link_url','$link_name', '$link_image', '$link_target', '$link_category', '$link_description', '$link_visible', '$link_owner', '$link_rating', '$link_rel', '$link_notes', '$link_rss')");
297                 $link_id = $wpdb->insert_id;
298         }
299         
300         if ( $update )
301                 do_action('edit_link', $link_id);
302         else
303                 do_action('add_link', $link_id);
304
305         return $link_id;
306 }
307
308 function wp_update_link($linkdata) {
309         global $wpdb;
310
311         $link_id = (int) $linkdata['link_id'];
312         
313         $link = get_link($link_id, ARRAY_A);
314         
315         // Escape data pulled from DB.
316         $link = add_magic_quotes($link);
317         
318         // Merge old and new fields with new fields overwriting old ones.
319         $linkdata = array_merge($link, $linkdata);
320
321         return wp_insert_link($linkdata);
322 }
323
324 function wp_delete_link($link_id) {
325         global $wpdb;
326
327         do_action('delete_link', $link_id);
328         return $wpdb->query("DELETE FROM $wpdb->links WHERE link_id = '$link_id'");     
329 }
330
331 function post_exists($title, $content = '', $post_date = '') {
332         global $wpdb;
333
334         if (!empty ($post_date))
335                 $post_date = "AND post_date = '$post_date'";
336
337         if (!empty ($title))
338                 return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title' $post_date");
339         else
340                 if (!empty ($content))
341                         return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_content = '$content' $post_date");
342
343         return 0;
344 }
345
346 function comment_exists($comment_author, $comment_date) {
347         global $wpdb;
348
349         return $wpdb->get_var("SELECT comment_post_ID FROM $wpdb->comments
350                         WHERE comment_author = '$comment_author' AND comment_date = '$comment_date'");
351 }
352
353 ?>