root/tags/1_0-rc1/wp-admin/admin-db.php

Revision 550, 15.2 kB (checked in by donncha, 3 years ago)

WP Merge and new features

  • 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)" );
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 ( !$update && category_exists($cat_name) )
98         return 0;
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))
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         $maxcat = $wpdb->get_var( "SELECT max(cat_ID) FROM {$wpdb->categories}" );
126         $cat_ID = mt_rand( $maxcat+100, $maxcat+4000 );
127         $wpdb->query("INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_description, category_parent, links_private, posts_private) VALUES ('$cat_ID', '$cat_name', '$category_nicename', '$category_description', '$category_parent', '$links_private', '$posts_private')");
128     } else {
129         $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'");
130     }
131
132     if ( $category_nicename == '' ) {
133         $category_nicename = sanitize_title($cat_name, $cat_ID );
134         $wpdb->query( "UPDATE $wpdb->categories SET category_nicename = '$category_nicename' WHERE cat_ID = '$cat_ID'" );
135     }
136
137     wp_cache_delete($cat_ID, 'category');
138
139     if ($update) {
140         do_action('edit_category', $cat_ID);
141     } else {
142         wp_cache_delete('all_category_ids', 'category');
143         do_action('create_category', $cat_ID);
144         do_action('add_category', $cat_ID);
145     }
146     $cat_ID = apply_filters( "cat_id_filter", $cat_ID );
147
148     return $cat_ID;
149 }
150
151 function wp_update_category($catarr) {
152     global $wpdb;
153
154     $cat_ID = (int) $catarr['cat_ID'];
155
156     // First, get all of the original fields
157     $category = get_category($cat_ID, ARRAY_A);
158
159     // Escape data pulled from DB.
160     $category = add_magic_quotes($category);
161
162     // Merge old and new fields with new fields overwriting old ones.
163     $catarr = array_merge($category, $catarr);
164
165     return wp_insert_category($catarr);
166 }
167
168 function wp_delete_category($cat_ID) {
169     global $wpdb;
170
171     $cat_ID = (int) $cat_ID;
172
173     // Don't delete the default cat.
174     if ( $cat_ID == get_option('default_category') )
175         return 0;
176
177     if ( $cat_ID == get_option('default_link_category') )
178         return 0;
179
180     $category = get_category($cat_ID);
181
182     $parent = $category->category_parent;
183
184     // Delete the category.
185     if ( !$wpdb->query("DELETE FROM $wpdb->categories WHERE cat_ID = '$cat_ID'") )
186         return 0;
187
188     // Update children to point to new parent.
189     $wpdb->query("UPDATE $wpdb->categories SET category_parent = '$parent' WHERE category_parent = '$cat_ID'");
190
191     // Only set posts and links to the default category if they're not in another category already.
192     $default_cat = get_option('default_category');
193     $posts = $wpdb->get_col("SELECT post_id FROM $wpdb->post2cat WHERE category_id='$cat_ID'");
194     if ( is_array($posts) ) foreach ($posts as $post_id) {
195         $cats = wp_get_post_categories($post_id);
196         if ( 1 == count($cats) )
197             $cats = array($default_cat);
198         else
199             $cats = array_diff($cats, array($cat_ID));
200         wp_set_post_categories($post_id, $cats);
201     }
202
203     $default_link_cat = get_option('default_link_category');
204     $links = $wpdb->get_col("SELECT link_id FROM $wpdb->link2cat WHERE category_id='$cat_ID'");
205     if ( is_array($links) ) foreach ($links as $link_id) {
206         $cats = wp_get_link_cats($link_id);
207         if ( 1 == count($cats) )
208             $cats = array($default_link_cat);
209         else
210             $cats = array_diff($cats, array($cat_ID));
211         wp_set_link_cats($link_id, $cats);
212     }
213     
214     wp_cache_delete($cat_ID, 'category');
215     wp_cache_delete('all_category_ids', 'category');
216
217     do_action('delete_category', $cat_ID);
218
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     $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = $id AND meta_key = '{$wpdb->prefix}capabilities'");
275
276     wp_cache_delete($id, 'users');
277     wp_cache_delete($user->user_login, 'userlogins');
278
279     do_action('delete_user', $id);
280
281     return true;
282 }
283
284 function wp_revoke_user($id) {
285     $id = (int) $id;
286     
287     $user = new WP_User($id);
288     $user->remove_all_caps();   
289 }
290
291 function wp_insert_link($linkdata) {
292     global $wpdb, $current_user;
293
294     extract($linkdata);
295
296     $update = false;
297     if ( !empty($link_id) )
298         $update = true;
299
300     if ( empty($link_rating) )
301         $link_rating = 0;
302
303     if ( empty($link_target) )
304         $link_target = '';
305
306     if ( empty($link_visible) )
307         $link_visible = 'Y';
308
309     if ( empty($link_owner) )
310         $link_owner = $current_user->id;
311
312     if ( empty($link_notes) )
313         $link_notes = '';
314
315     // Make sure we set a valid category
316     if (0 == count($link_category) || !is_array($link_category)) {
317         $link_category = array(get_option('default_link_category'));
318     }
319
320     if ( $update ) {
321         $wpdb->query("UPDATE $wpdb->links SET link_url='$link_url',
322             link_name='$link_name', link_image='$link_image',
323             link_target='$link_target',
324             link_visible='$link_visible', link_description='$link_description',
325             link_rating='$link_rating', link_rel='$link_rel',
326             link_notes='$link_notes', link_rss = '$link_rss'
327             WHERE link_id='$link_id'");
328     } else {
329         $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')");
330         $link_id = $wpdb->insert_id;
331     }
332
333     wp_set_link_cats($link_id, $link_category);
334
335     if ( $update )
336         do_action('edit_link', $link_id);
337     else
338         do_action('add_link', $link_id);
339
340     return $link_id;
341 }
342
343 function wp_update_link($linkdata) {
344     global $wpdb;
345
346     $link_id = (int) $linkdata['link_id'];
347
348     $link = get_link($link_id, ARRAY_A);
349
350     // Escape data pulled from DB.
351     $link = add_magic_quotes($link);
352
353     // Passed link category list overwrites existing category list if not empty.
354      if ( isset($linkdata['link_category']) && is_array($linkdata['link_category'])
355              && 0 != count($linkdata['link_category']) )
356          $link_cats = $linkdata['link_category'];
357      else
358          $link_cats = $link['link_category'];
359
360     // Merge old and new fields with new fields overwriting old ones.
361     $linkdata = array_merge($link, $linkdata);
362      $linkdata['link_category'] = $link_cats;
363
364     return wp_insert_link($linkdata);
365 }
366
367 function wp_delete_link($link_id) {
368     global $wpdb;
369
370     do_action('delete_link', $link_id);
371     
372     $categories = wp_get_link_cats($link_id);
373     if( is_array( $categories ) ) {
374         foreach ( $categories as $category ) {
375             $wpdb->query("UPDATE $wpdb->categories SET link_count = link_count - 1 WHERE cat_ID = '$category'");
376             wp_cache_delete($category, 'category');
377         }
378     }
379
380     $wpdb->query("DELETE FROM $wpdb->link2cat WHERE link_id = '$link_id'");
381     return $wpdb->query("DELETE FROM $wpdb->links WHERE link_id = '$link_id'");
382 }
383
384 function wp_get_link_cats($link_ID = 0) {
385     global $wpdb;
386
387     $sql = "SELECT category_id
388         FROM $wpdb->link2cat
389         WHERE link_id = $link_ID
390         ORDER BY category_id";
391
392     $result = $wpdb->get_col($sql);
393
394     if ( !$result )
395         $result = array();
396
397     return array_unique($result);
398 }
399
400 function wp_set_link_cats($link_ID = 0, $link_categories = array()) {
401     global $wpdb;
402     // If $link_categories isn't already an array, make it one:
403     if (!is_array($link_categories) || 0 == count($link_categories))
404         $link_categories = array(get_option('default_link_category'));
405
406     $link_categories = array_unique($link_categories);
407
408     // First the old categories
409     $old_categories = $wpdb->get_col("
410         SELECT category_id
411         FROM $wpdb->link2cat
412         WHERE link_id = $link_ID");
413
414     if (!$old_categories) {
415         $old_categories = array();
416     } else {
417         $old_categories = array_unique($old_categories);
418     }
419
420     // Delete any?
421     $delete_cats = array_diff($old_categories,$link_categories);
422
423     if ($delete_cats) {
424         foreach ($delete_cats as $del) {
425             $wpdb->query("
426                 DELETE FROM $wpdb->link2cat
427                 WHERE category_id = $del
428                     AND link_id = $link_ID
429                 ");
430         }
431     }
432
433     // Add any?
434     $add_cats = array_diff($link_categories, $old_categories);
435
436     if ($add_cats) {
437         foreach ($add_cats as $new_cat) {
438             $wpdb->query("
439                 INSERT INTO $wpdb->link2cat (link_id, category_id)
440                 VALUES ($link_ID, $new_cat)");
441         }
442     }
443     
444     // Update category counts.
445     $all_affected_cats = array_unique(array_merge($link_categories, $old_categories));
446     foreach ( $all_affected_cats as $cat_id ) {
447         $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'");
448         $wpdb->query("UPDATE $wpdb->categories SET link_count = '$count' WHERE cat_ID = '$cat_id'");
449         wp_cache_delete($cat_id, 'category');
450     }
451 }    // wp_set_link_cats()
452
453 function post_exists($title, $content = '', $post_date = '') {
454     global $wpdb;
455
456     if (!empty ($post_date))
457         $post_date = "AND post_date = '$post_date'";
458
459     if (!empty ($title))
460         return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title' $post_date");
461     else
462         if (!empty ($content))
463             return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_content = '$content' $post_date");
464
465     return 0;
466 }
467
468 function comment_exists($comment_author, $comment_date) {
469     global $wpdb;
470
471     return $wpdb->get_var("SELECT comment_post_ID FROM $wpdb->comments
472             WHERE comment_author = '$comment_author' AND comment_date = '$comment_date'");
473 }
474
475 function wpmu_delete_blog($blog_id, $drop = false) {
476     global $wpdb, $wpmuBaseTablePrefix;
477
478     if ( $blog_id != $wpdb->blogid ) {
479         $switch = true;
480         switch_to_blog($blog_id);   
481     }
482
483     do_action('delete_blog', $blog_id, $drop);
484
485     $users = get_users_of_blog($blog_id);
486
487     // Remove users from this blog.
488     if ( !empty($users) ) foreach ($users as $user) {
489         remove_user_from_blog($user->user_id, $blog_id);
490     }
491
492     update_blog_status( $wpdb->blogid, 'deleted', 1 );
493
494     if ( $drop ) {
495         $drop_tables = array( $wpmuBaseTablePrefix . $blog_id . "_categories",
496             $wpmuBaseTablePrefix . $blog_id . "_comments",
497             $wpmuBaseTablePrefix . $blog_id . "_linkcategories",
498             $wpmuBaseTablePrefix . $blog_id . "_links",
499             $wpmuBaseTablePrefix . $blog_id . "_link2cat",
500             $wpmuBaseTablePrefix . $blog_id . "_options",
501             $wpmuBaseTablePrefix . $blog_id . "_post2cat",
502             $wpmuBaseTablePrefix . $blog_id . "_postmeta",
503             $wpmuBaseTablePrefix . $blog_id . "_posts",
504             $wpmuBaseTablePrefix . $blog_id . "_referer_visitLog",
505             $wpmuBaseTablePrefix . $blog_id . "_referer_blacklist" );
506         reset( $drop_tables );
507
508         while( list( $key, $val ) = each( $drop_tables  ) )
509             $wpdb->query( "DROP TABLE IF EXISTS $val" );
510
511         $wpdb->query( "DELETE FROM $wpdb->blogs WHERE blog_id = '$blog_id'" );
512     }
513
514     if ( $switch )
515         restore_current_blog();
516 }
517
518 function wpmu_delete_user($id) {
519     global $wpdb;
520
521     $id = (int) $id;
522     $user = get_userdata($id);
523
524     do_action('wpmu_delete_user', $id);
525
526     $blogs = get_blogs_of_user($id);
527     
528     if ( ! empty($blogs) ) foreach ($blogs as $blog) {
529         switch_to_blog($blog->userblog_id);
530         remove_user_from_blog($id, $blog->userblog_id);
531
532         $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_author = $id");
533
534         if ($post_ids) {
535             foreach ($post_ids as $post_id)
536                 wp_delete_post($post_id);
537         }
538
539         // Clean links
540         $wpdb->query("DELETE FROM $wpdb->links WHERE link_owner = $id");
541
542         restore_current_blog();
543     }
544
545     $wpdb->query("DELETE FROM $wpdb->users WHERE ID = $id");