root/tags/2.1.2/wp-admin/admin-db.php

Revision 4749, 14.2 kB (checked in by ryan, 2 years ago)

Cat ID filter relo. Props donncha. fixes #3591

  • 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_type = 'post' AND 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_type = 'post' AND 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     if( trim( $cat_name ) == '' )
88         return 0;
89
90     $cat_ID = (int) $cat_ID;
91
92     // Are we updating or creating?
93     if (!empty ($cat_ID))
94         $update = true;
95     else
96         $update = false;
97
98     $cat_name = apply_filters('pre_category_name', $cat_name);
99
100     if (empty ($category_nicename))
101         $category_nicename = sanitize_title($cat_name);
102     else
103         $category_nicename = sanitize_title($category_nicename);
104     $category_nicename = apply_filters('pre_category_nicename', $category_nicename);
105
106     if (empty ($category_description))
107         $category_description = '';
108     $category_description = apply_filters('pre_category_description', $category_description);
109
110     $category_parent = (int) $category_parent;
111     if ( empty($category_parent) || !get_category( $category_parent ) || ($cat_ID && cat_is_ancestor_of($cat_ID, $category_parent) ) )
112         $category_parent = 0;
113
114     if ( isset($posts_private) )
115         $posts_private = (int) $posts_private;
116     else
117         $posts_private = 0;
118
119     if ( isset($links_private) )
120         $links_private = (int) $links_private;
121     else
122         $links_private = 0;
123
124     if (!$update) {
125         $wpdb->query("INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_description, category_parent, links_private, posts_private) VALUES ('0', '$cat_name', '$category_nicename', '$category_description', '$category_parent', '$links_private', '$posts_private')");
126         $cat_ID = $wpdb->insert_id;
127     } else {
128         $wpdb->query ("UPDATE $wpdb->categories SET cat_name = '$cat_name', category_nicename = '$category_nicename', category_description = '$category_description', category_parent = '$category_parent', links_private = '$links_private', posts_private = '$posts_private' WHERE cat_ID = '$cat_ID'");
129     }
130
131     if ( $category_nicename == '' ) {
132         $category_nicename = sanitize_title($cat_name, $cat_ID );
133         $wpdb->query( "UPDATE $wpdb->categories SET category_nicename = '$category_nicename' WHERE cat_ID = '$cat_ID'" );
134     }
135
136     // Keep in mind when using this filter and altering the cat_ID that the two queries above
137     // have already taken place with the OLD cat_ID
138     // Also note that you may have post2cat entries with the old cat_ID if this is an update
139
140     if ($update) {
141         do_action('edit_category', $cat_ID);
142     } else {
143         do_action('create_category', $cat_ID);
144         do_action('add_category', $cat_ID);
145     }
146
147     $cat_ID = apply_filters('cat_id_filter', $cat_ID, $update);
148
149     clean_category_cache($cat_ID);
150
151     return $cat_ID;
152 }
153
154 function wp_update_category($catarr) {
155     global $wpdb;
156
157     $cat_ID = (int) $catarr['cat_ID'];
158
159     if( $cat_ID == $catarr['category_parent'] )
160         return false;
161
162     // First, get all of the original fields
163     $category = get_category($cat_ID, ARRAY_A);
164
165     // Escape data pulled from DB.
166     $category = add_magic_quotes($category);
167
168     // Merge old and new fields with new fields overwriting old ones.
169     $catarr = array_merge($category, $catarr);
170
171     return wp_insert_category($catarr);
172 }
173
174 function wp_delete_category($cat_ID) {
175     global $wpdb;
176
177     $cat_ID = (int) $cat_ID;
178     $default_cat = get_option('default_category');
179     $default_link_cat = get_option('default_link_category');
180
181     // Don't delete either of the default cats
182     if ( $cat_ID == $default_cat || $cat_ID == $default_link_cat )
183         return 0;
184
185     $category = get_category($cat_ID);
186
187     $parent = $category->category_parent;
188
189     // Delete the category
190     if ( !$wpdb->query("DELETE FROM $wpdb->categories WHERE cat_ID = '$cat_ID'") )
191         return 0;
192
193     // Update children to point to new parent
194     $wpdb->query("UPDATE $wpdb->categories SET category_parent = '$parent' WHERE category_parent = '$cat_ID'");
195
196     // Only set posts and links to the default category if they're not in another category already
197     $posts = $wpdb->get_col("SELECT post_id FROM $wpdb->post2cat WHERE category_id='$cat_ID'");
198     foreach ( (array) $posts as $post_id ) {
199         $cats = wp_get_post_categories($post_id);
200         if ( 1 == count($cats) )
201             $cats = array($default_cat);
202         else
203             $cats = array_diff($cats, array($cat_ID));
204         wp_set_post_categories($post_id, $cats);
205     }
206
207     $links = $wpdb->get_col("SELECT link_id FROM $wpdb->link2cat WHERE category_id='$cat_ID'");
208     foreach ( (array) $links as $link_id ) {
209         $cats = wp_get_link_cats($link_id);
210         if ( 1 == count($cats) )
211             $cats = array($default_link_cat);
212         else
213             $cats = array_diff($cats, array($cat_ID));
214         wp_set_link_cats($link_id, $cats);
215     }
216
217     clean_category_cache($cat_ID);
218     do_action('delete_category', $cat_ID);
219     return 1;
220 }
221
222 function wp_create_category($cat_name) {
223     $cat_array = compact('cat_name');
224     return wp_insert_category($cat_array);
225 }
226
227 function wp_create_categories($categories, $post_id = '') {
228     $cat_ids = array ();
229     foreach ($categories as $category) {
230         if ($id = category_exists($category))
231             $cat_ids[] = $id;
232         else
233             if ($id = wp_create_category($category))
234                 $cat_ids[] = $id;
235     }
236
237     if ($post_id)
238         wp_set_post_categories($post_id, $cat_ids);
239
240     return $cat_ids;
241 }
242
243 function category_exists($cat_name) {
244     global $wpdb;
245     if (!$category_nicename = sanitize_title($cat_name))
246         return 0;
247
248     return $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '$category_nicename'");
249 }
250
251 function wp_delete_user($id, $reassign = 'novalue') {
252     global $wpdb;
253
254     $id = (int) $id;
255     $user = get_userdata($id);
256
257     if ($reassign == 'novalue') {
258         $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_author = $id");
259
260         if ($post_ids) {
261             foreach ($post_ids as $post_id)
262                 wp_delete_post($post_id);
263         }
264
265         // Clean links
266         $wpdb->query("DELETE FROM $wpdb->links WHERE link_owner = $id");
267     } else {
268         $reassign = (int) $reassign;
269         $wpdb->query("UPDATE $wpdb->posts SET post_author = {$reassign} WHERE post_author = {$id}");
270         $wpdb->query("UPDATE $wpdb->links SET link_owner = {$reassign} WHERE link_owner = {$id}");
271     }
272
273     // FINALLY, delete user
274     do_action('delete_user', $id);
275
276     $wpdb->query("DELETE FROM $wpdb->users WHERE ID = $id");
277     $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = '$id'");
278
279     wp_cache_delete($id, 'users');
280     wp_cache_delete($user->user_login, 'userlogins');
281
282     return true;
283 }
284
285 function wp_revoke_user($id) {
286     $id = (int) $id;
287
288     $user = new WP_User($id);
289     $user->remove_all_caps();
290 }
291
292 function wp_insert_link($linkdata) {
293     global $wpdb, $current_user;
294
295     extract($linkdata);
296
297     $update = false;
298
299     if ( !empty($link_id) )
300         $update = true;
301
302     if( trim( $link_name ) == '' )
303         return 0;
304     $link_name = apply_filters('pre_link_name', $link_name);
305
306     if( trim( $link_url ) == '' )
307         return 0;
308     $link_url = apply_filters('pre_link_url', $link_url);
309
310     if ( empty($link_rating) )
311         $link_rating = 0;
312     else
313         $link_rating = (int) $link_rating;
314
315     if ( empty($link_image) )
316         $link_image = '';
317     $link_image = apply_filters('pre_link_image', $link_image);
318
319     if ( empty($link_target) )
320         $link_target = '';
321     $link_target = apply_filters('pre_link_target', $link_target);
322
323     if ( empty($link_visible) )
324         $link_visible = 'Y';
325     $link_visibile = preg_replace('/[^YNyn]/', '', $link_visible);
326
327     if ( empty($link_owner) )
328         $link_owner = $current_user->id;
329     else
330         $link_owner = (int) $link_owner;
331
332     if ( empty($link_notes) )
333         $link_notes = '';
334     $link_notes = apply_filters('pre_link_notes', $link_notes);
335
336     if ( empty($link_description) )
337         $link_description = '';
338     $link_description = apply_filters('pre_link_description', $link_description);
339
340     if ( empty($link_rss) )
341         $link_rss = '';
342     $link_rss = apply_filters('pre_link_rss', $link_rss);
343
344     if ( empty($link_rel) )
345         $link_rel = '';
346     $link_rel = apply_filters('pre_link_rel', $link_rel);
347
348     // Make sure we set a valid category
349     if (0 == count($link_category) || !is_array($link_category)) {
350         $link_category = array(get_option('default_link_category'));
351     }
352
353     if ( $update ) {
354         $wpdb->query("UPDATE $wpdb->links SET link_url='$link_url',
355             link_name='$link_name', link_image='$link_image',
356             link_target='$link_target',
357             link_visible='$link_visible', link_description='$link_description',
358             link_rating='$link_rating', link_rel='$link_rel',
359             link_notes='$link_notes', link_rss = '$link_rss'
360             WHERE link_id='$link_id'");
361     } else {
362         $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_image, link_target, link_description, link_visible, link_owner, link_rating, link_rel, link_notes, link_rss) VALUES('$link_url','$link_name', '$link_image', '$link_target', '$link_description', '$link_visible', '$link_owner', '$link_rating', '$link_rel', '$link_notes', '$link_rss')");
363         $link_id = $wpdb->insert_id;
364     }
365
366     wp_set_link_cats($link_id, $link_category);
367
368     if ( $update )
369         do_action('edit_link', $link_id);
370     else
371         do_action('add_link', $link_id);
372
373     return $link_id;
374 }
375
376 function wp_update_link($linkdata) {
377     global $wpdb;
378
379     $link_id = (int) $linkdata['link_id'];
380
381     $link = get_link($link_id, ARRAY_A);
382
383     // Escape data pulled from DB.
384     $link = add_magic_quotes($link);
385
386     // Passed link category list overwrites existing category list if not empty.
387     if ( isset($linkdata['link_category']) && is_array($linkdata['link_category'])
388              && 0 != count($linkdata['link_category']) )
389         $link_cats = $linkdata['link_category'];
390     else
391         $link_cats = $link['link_category'];
392
393     // Merge old and new fields with new fields overwriting old ones.
394     $linkdata = array_merge($link, $linkdata);
395     $linkdata['link_category'] = $link_cats;
396
397     return wp_insert_link($linkdata);
398 }
399
400 function wp_delete_link($link_id) {
401     global $wpdb;
402
403     do_action('delete_link', $link_id);
404
405     $categories = wp_get_link_cats($link_id);
406     if( is_array( $categories ) ) {
407         foreach ( $categories as $category ) {
408             $wpdb->query("UPDATE $wpdb->categories SET link_count = link_count - 1 WHERE cat_ID = '$category'");
409             wp_cache_delete($category, 'category');
410             do_action('edit_category', $cat_id);
411         }
412     }
413
414     $wpdb->query("DELETE FROM $wpdb->link2cat WHERE link_id = '$link_id'");
415     return $wpdb->query("DELETE FROM $wpdb->links WHERE link_id = '$link_id'");
416 }
417
418 function wp_get_link_cats($link_ID = 0) {
419     global $wpdb;
420
421     $sql = "SELECT category_id
422         FROM $wpdb->link2cat
423         WHERE link_id = $link_ID
424         ORDER BY category_id";
425
426     $result = $wpdb->get_col($sql);
427
428     if ( !$result )
429         $result = array();
430
431     return array_unique($result);
432 }
433
434 function wp_set_link_cats($link_ID = 0, $link_categories = array()) {
435     global $wpdb;
436     // If $link_categories isn't already an array, make it one:
437     if (!is_array($link_categories) || 0 == count($link_categories))
438         $link_categories = array(get_option('default_link_category'));
439
440     $link_categories = array_unique($link_categories);
441
442     // First the old categories
443     $old_categories = $wpdb->get_col("
444         SELECT category_id
445         FROM $wpdb->link2cat
446         WHERE link_id = $link_ID");
447
448     if (!$old_categories) {
449         $old_categories = array();
450     } else {
451         $old_categories = array_unique($old_categories);
452     }
453
454     // Delete any?
455     $delete_cats = array_diff($old_categories,$link_categories);
456
457     if ($delete_cats) {
458         foreach ($delete_cats as $del) {
459             $wpdb->query("
460                 DELETE FROM $wpdb->link2cat
461                 WHERE category_id = $del
462                     AND link_id = $link_ID
463                 ");
464         }
465     }
466
467     // Add any?
468     $add_cats = array_diff($link_categories, $old_categories);
469
470     if ($add_cats) {
471         foreach ($add_cats as $new_cat) {
472             $wpdb->query("
473                 INSERT INTO $wpdb->link2cat (link_id, category_id)
474                 VALUES ($link_ID, $new_cat)");
475         }
476     }
477
478     // Update category counts.
479     $all_affected_cats = array_unique(array_merge($link_categories, $old_categories));
480     foreach ( $all_affected_cats as $cat_id ) {
481         $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->link2cat, $wpdb->links WHERE $wpdb->links.link_id = $wpdb->link2cat.link_id AND category_id = '$cat_id'");
482         $wpdb->query("UPDATE $wpdb->categories SET link_count = '$count' WHERE cat_ID = '$cat_id'");
483         wp_cache_delete($cat_id, 'category');
484         do_action('edit_category', $cat_id);
485     }
486
487 }    // wp_set_link_cats()
488
489 function post_exists($title, $content = '', $post_date = '') {
490     global $wpdb;
491
492     if (!empty ($post_date))
493         $post_date = "AND post_date = '$post_date'";
494
495     if (!empty ($title))
496         return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title' $post_date");
497     else
498         if (!empty ($content))
499             return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_content = '$content' $post_date");
500
501     return 0;
502 }
503
504 function comment_exists($comment_author, $comment_date) {
505     global $wpdb;
506
507     return $wpdb->get_var("SELECT comment_post_ID FROM $wpdb->comments
508             WHERE comment_author = '$comment_author' AND comment_date = '$comment_date'");
509 }
510
511 ?>
512
Note: See TracBrowser for help on using the browser.