]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/admin-db.php
Wordpress 2.0.2-scripts
[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 = wp_specialchars($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
102         if (empty ($category_description))
103                 $category_description = '';
104
105         if (empty ($category_parent))
106                 $category_parent = 0;
107
108         if (!$update) {
109                 $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')");
110                 $cat_ID = $wpdb->insert_id;
111         } else {
112                 $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'");
113         }
114         
115         if ( $category_nicename == '' ) {
116                 $category_nicename = sanitize_title($cat_name, $cat_ID );
117                 $wpdb->query( "UPDATE $wpdb->categories SET category_nicename = '$category_nicename' WHERE cat_ID = '$cat_ID'" );
118         }
119
120         wp_cache_delete($cat_ID, 'category');
121
122         if ($update) {
123                 do_action('edit_category', $cat_ID);
124         } else {
125                 wp_cache_delete('all_category_ids', 'category');
126                 do_action('create_category', $cat_ID);
127                 do_action('add_category', $cat_ID);
128         }
129
130         return $cat_ID;
131 }
132
133 function wp_update_category($catarr) {
134         global $wpdb;
135
136         $cat_ID = (int) $catarr['cat_ID'];
137
138         // First, get all of the original fields
139         $category = get_category($cat_ID, ARRAY_A);
140
141         // Escape data pulled from DB.
142         $category = add_magic_quotes($category);
143
144         // Merge old and new fields with new fields overwriting old ones.
145         $catarr = array_merge($category, $catarr);
146
147         return wp_insert_category($catarr);
148 }
149
150 function wp_delete_category($cat_ID) {
151         global $wpdb;
152
153         $cat_ID = (int) $cat_ID;
154
155         // Don't delete the default cat.
156         if (1 == $cat_ID)
157                 return 0;
158
159         $category = get_category($cat_ID);
160
161         $parent = $category->category_parent;
162
163         // Delete the category.
164         $wpdb->query("DELETE FROM $wpdb->categories WHERE cat_ID = '$cat_ID'");
165
166         // Update children to point to new parent.
167         $wpdb->query("UPDATE $wpdb->categories SET category_parent = '$parent' WHERE category_parent = '$cat_ID'");
168
169         // TODO: Only set categories to general if they're not in another category already
170         $wpdb->query("UPDATE $wpdb->post2cat SET category_id='1' WHERE category_id='$cat_ID'");
171
172         wp_cache_delete($cat_ID, 'category');
173         wp_cache_delete('all_category_ids', 'category');
174
175         do_action('delete_category', $cat_ID);
176
177         return 1;
178 }
179
180 function wp_create_category($cat_name) {
181         $cat_array = compact('cat_name');
182         return wp_insert_category($cat_array);
183 }
184
185 function wp_create_categories($categories, $post_id = '') {
186         $cat_ids = array ();
187         foreach ($categories as $category) {
188                 if ($id = category_exists($category))
189                         $cat_ids[] = $id;
190                 else
191                         if ($id = wp_create_category($category))
192                                 $cat_ids[] = $id;
193         }
194
195         if ($post_id)
196                 wp_set_post_cats('', $post_id, $cat_ids);
197
198         return $cat_ids;
199 }
200
201 function category_exists($cat_name) {
202         global $wpdb;
203         if (!$category_nicename = sanitize_title($cat_name))
204                 return 0;
205
206         return $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '$category_nicename'");
207 }
208
209 function wp_delete_user($id, $reassign = 'novalue') {
210         global $wpdb;
211
212         $id = (int) $id;
213         $user = get_userdata($id);
214
215         if ($reassign == 'novalue') {
216                 $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_author = $id");
217
218                 if ($post_ids) {
219                         foreach ($post_ids as $post_id)
220                                 wp_delete_post($post_id);
221                 }
222
223                 // Clean links
224                 $wpdb->query("DELETE FROM $wpdb->links WHERE link_owner = $id");
225         } else {
226                 $reassign = (int) $reassign;
227                 $wpdb->query("UPDATE $wpdb->posts SET post_author = {$reassign} WHERE post_author = {$id}");
228                 $wpdb->query("UPDATE $wpdb->links SET link_owner = {$reassign} WHERE link_owner = {$id}");
229         }
230
231         // FINALLY, delete user
232         $wpdb->query("DELETE FROM $wpdb->users WHERE ID = $id");
233         $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = '$id'");
234
235         wp_cache_delete($id, 'users');
236         wp_cache_delete($user->user_login, 'userlogins');
237
238         do_action('delete_user', $id);
239
240         return true;
241 }
242
243 function get_link($link_id, $output = OBJECT) {
244         global $wpdb;
245         
246         $link = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = '$link_id'");
247
248         if ( $output == OBJECT ) {
249                 return $link;
250         } elseif ( $output == ARRAY_A ) {
251                 return get_object_vars($link);
252         } elseif ( $output == ARRAY_N ) {
253                 return array_values(get_object_vars($link));
254         } else {
255                 return $link;
256         }
257 }
258
259 function wp_insert_link($linkdata) {
260         global $wpdb, $current_user;
261         
262         extract($linkdata);
263
264         $update = false;
265         if ( !empty($link_id) )
266                 $update = true;
267
268         if ( empty($link_rating) )
269                 $link_rating = 0;       
270
271         if ( empty($link_target) )
272                 $link_target = '';      
273
274         if ( empty($link_visible) )
275                 $link_visible = 'Y';
276                 
277         if ( empty($link_owner) )
278                 $link_owner = $current_user->id;
279
280         if ( empty($link_notes) )
281                 $link_notes = '';
282
283         if ( $update ) {
284                 $wpdb->query("UPDATE $wpdb->links SET link_url='$link_url',
285                         link_name='$link_name', link_image='$link_image',
286                         link_target='$link_target', link_category='$link_category',
287                         link_visible='$link_visible', link_description='$link_description',
288                         link_rating='$link_rating', link_rel='$link_rel',
289                         link_notes='$link_notes', link_rss = '$link_rss'
290                         WHERE link_id='$link_id'");
291         } else {
292                 $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')");
293                 $link_id = $wpdb->insert_id;
294         }
295         
296         if ( $update )
297                 do_action('edit_link', $link_id);
298         else
299                 do_action('add_link', $link_id);
300
301         return $link_id;
302 }
303
304 function wp_update_link($linkdata) {
305         global $wpdb;
306
307         $link_id = (int) $linkdata['link_id'];
308         
309         $link = get_link($link_id, ARRAY_A);
310         
311         // Escape data pulled from DB.
312         $link = add_magic_quotes($link);
313         
314         // Merge old and new fields with new fields overwriting old ones.
315         $linkdata = array_merge($link, $linkdata);
316
317         return wp_insert_link($linkdata);
318 }
319
320 function wp_delete_link($link_id) {
321         global $wpdb;
322
323         do_action('delete_link', $link_id);
324         return $wpdb->query("DELETE FROM $wpdb->links WHERE link_id = '$link_id'");     
325 }
326
327 function post_exists($title, $content = '', $post_date = '') {
328         global $wpdb;
329
330         if (!empty ($post_date))
331                 $post_date = "AND post_date = '$post_date'";
332
333         if (!empty ($title))
334                 return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title' $post_date");
335         else
336                 if (!empty ($content))
337                         return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_content = '$content' $post_date");
338
339         return 0;
340 }
341
342 function comment_exists($comment_author, $comment_date) {
343         global $wpdb;
344
345         return $wpdb->get_var("SELECT comment_post_ID FROM $wpdb->comments
346                         WHERE comment_author = '$comment_author' AND comment_date = '$comment_date'");
347 }
348
349 ?>