root/branches/2.0/wp-admin/admin-db.php

Revision 5099, 10.4 kB (checked in by ryan, 1 year ago)

Some int casts

  • Property svn:eol-style set to native
Line 
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) ORDER BY display_name" );
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 = (int) $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 (int) $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
270     if ( !empty($link_id) )
271         $update = true;
272
273     $link_id = (int) $link_id;
274
275     if( trim( $link_name ) == '' )
276         return 0;
277     $link_name = apply_filters('pre_link_name', $link_name);
278
279     if( trim( $link_url ) == '' )
280         return 0;
281     $link_url = apply_filters('pre_link_url', $link_url);
282
283     if ( empty($link_rating) )
284         $link_rating = 0;   
285     else
286         $link_rating = (int) $link_rating;
287
288     if ( empty($link_image) )
289         $link_image = '';
290     $link_image = apply_filters('pre_link_image', $link_image);
291
292     if ( empty($link_target) )
293         $link_target = '';   
294     $link_target = apply_filters('pre_link_target', $link_target);
295
296     if ( empty($link_visible) )
297         $link_visible = 'Y';
298     $link_visibile = preg_replace('/[^YNyn]/', '', $link_visible);
299
300     if ( empty($link_owner) )
301         $link_owner = $current_user->id;
302     else
303         $link_owner = (int) $link_owner;
304
305     if ( empty($link_notes) )
306         $link_notes = '';
307     $link_notes = apply_filters('pre_link_notes', $link_notes);
308
309     if ( empty($link_description) )
310         $link_description = '';
311     $link_description = apply_filters('pre_link_description', $link_description);
312
313     if ( empty($link_rss) )
314         $link_rss = '';
315     $link_rss = apply_filters('pre_link_rss', $link_rss);
316
317     if ( empty($link_rel) )
318         $link_rel = '';
319     $link_rel = apply_filters('pre_link_rel', $link_rel);
320
321     if ( $update ) {
322         $wpdb->query("UPDATE $wpdb->links SET link_url='$link_url',
323             link_name='$link_name', link_image='$link_image',
324             link_target='$link_target', link_category='$link_category',
325             link_visible='$link_visible', link_description='$link_description',
326             link_rating='$link_rating', link_rel='$link_rel',
327             link_notes='$link_notes', link_rss = '$link_rss'
328             WHERE link_id='$link_id'");
329     } else {
330         $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')");
331         $link_id = (int) $wpdb->insert_id;
332     }
333     
334     if ( $update )
335         do_action('edit_link', $link_id);
336     else
337         do_action('add_link', $link_id);
338
339     return $link_id;
340 }
341
342 function wp_update_link($linkdata) {
343     global $wpdb;
344
345     $link_id = (int) $linkdata['link_id'];
346     
347     $link = get_link($link_id, ARRAY_A);
348     
349     // Escape data pulled from DB.
350     $link = add_magic_quotes($link);
351     
352     // Merge old and new fields with new fields overwriting old ones.
353     $linkdata = array_merge($link, $linkdata);
354
355     return wp_insert_link($linkdata);
356 }
357
358 function wp_delete_link($link_id) {
359     global $wpdb;
360
361     do_action('delete_link', $link_id);
362     return $wpdb->query("DELETE FROM $wpdb->links WHERE link_id = '$link_id'");   
363 }
364
365 function post_exists($title, $content = '', $post_date = '') {
366     global $wpdb;
367
368     if (!empty ($post_date))
369         $post_date = "AND post_date = '$post_date'";
370
371     if (!empty ($title))
372         return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title' $post_date");
373     else
374         if (!empty ($content))
375             return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_content = '$content' $post_date");
376
377     return 0;
378 }
379
380 function comment_exists($comment_author, $comment_date) {
381     global $wpdb;
382
383     return $wpdb->get_var("SELECT comment_post_ID FROM $wpdb->comments
384             WHERE comment_author = '$comment_author' AND comment_date = '$comment_date'");
385 }
386
387 ?>
388
Note: See TracBrowser for help on using the browser.