3 function get_users_drafts( $user_id ) {
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 );
11 function get_others_drafts( $user_id ) {
13 $user = get_userdata( $user_id );
14 $level_key = $wpdb->prefix . 'user_level';
16 $editable = get_editable_user_ids( $user_id );
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' ");
25 return apply_filters('get_others_drafts', $other_drafts);
28 function get_editable_authors( $user_id ) {
31 $editable = get_editable_user_ids( $user_id );
36 $editable = join(',', $editable);
37 $authors = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($editable)" );
40 return apply_filters('get_editable_authors', $authors);
43 function get_editable_user_ids( $user_id, $exclude_zeros = true ) {
46 $user = new WP_User( $user_id );
48 if ( ! $user->has_cap('edit_others_posts') ) {
49 if ( $user->has_cap('edit_posts') || $exclude_zeros == false )
50 return array($user->id);
55 $level_key = $wpdb->prefix . 'user_level';
57 $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key'";
59 $query .= " AND meta_value != '0'";
61 return $wpdb->get_col( $query );
64 function get_author_user_ids() {
66 $level_key = $wpdb->prefix . 'user_level';
68 $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key' AND meta_value != '0'";
70 return $wpdb->get_col( $query );
73 function get_nonauthor_user_ids() {
75 $level_key = $wpdb->prefix . 'user_level';
77 $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key' AND meta_value = '0'";
79 return $wpdb->get_col( $query );
82 function wp_insert_category($catarr) {
87 $cat_ID = (int) $cat_ID;
89 // Are we updating or creating?
95 $cat_name = wp_specialchars($cat_name);
97 if (empty ($category_nicename))
98 $category_nicename = sanitize_title($cat_name);
100 $category_nicename = sanitize_title($category_nicename);
102 if (empty ($category_description))
103 $category_description = '';
105 if (empty ($category_parent))
106 $category_parent = 0;
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;
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'");
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'" );
120 wp_cache_delete($cat_ID, 'category');
123 do_action('edit_category', $cat_ID);
125 wp_cache_delete('all_category_ids', 'category');
126 do_action('create_category', $cat_ID);
127 do_action('add_category', $cat_ID);
133 function wp_update_category($catarr) {
136 $cat_ID = (int) $catarr['cat_ID'];
138 // First, get all of the original fields
139 $category = get_category($cat_ID, ARRAY_A);
141 // Escape data pulled from DB.
142 $category = add_magic_quotes($category);
144 // Merge old and new fields with new fields overwriting old ones.
145 $catarr = array_merge($category, $catarr);
147 return wp_insert_category($catarr);
150 function wp_delete_category($cat_ID) {
153 $cat_ID = (int) $cat_ID;
155 // Don't delete the default cat.
159 $category = get_category($cat_ID);
161 $parent = $category->category_parent;
163 // Delete the category.
164 $wpdb->query("DELETE FROM $wpdb->categories WHERE cat_ID = '$cat_ID'");
166 // Update children to point to new parent.
167 $wpdb->query("UPDATE $wpdb->categories SET category_parent = '$parent' WHERE category_parent = '$cat_ID'");
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'");
172 wp_cache_delete($cat_ID, 'category');
173 wp_cache_delete('all_category_ids', 'category');
175 do_action('delete_category', $cat_ID);
180 function wp_create_category($cat_name) {
181 $cat_array = compact('cat_name');
182 return wp_insert_category($cat_array);
185 function wp_create_categories($categories, $post_id = '') {
187 foreach ($categories as $category) {
188 if ($id = category_exists($category))
191 if ($id = wp_create_category($category))
196 wp_set_post_cats('', $post_id, $cat_ids);
201 function category_exists($cat_name) {
203 if (!$category_nicename = sanitize_title($cat_name))
206 return $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '$category_nicename'");
209 function wp_delete_user($id, $reassign = 'novalue') {
213 $user = get_userdata($id);
215 if ($reassign == 'novalue') {
216 $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_author = $id");
219 foreach ($post_ids as $post_id)
220 wp_delete_post($post_id);
224 $wpdb->query("DELETE FROM $wpdb->links WHERE link_owner = $id");
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}");
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'");
235 wp_cache_delete($id, 'users');
236 wp_cache_delete($user->user_login, 'userlogins');
238 do_action('delete_user', $id);
243 function get_link($link_id, $output = OBJECT) {
246 $link = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = '$link_id'");
248 if ( $output == OBJECT ) {
250 } elseif ( $output == ARRAY_A ) {
251 return get_object_vars($link);
252 } elseif ( $output == ARRAY_N ) {
253 return array_values(get_object_vars($link));
259 function wp_insert_link($linkdata) {
260 global $wpdb, $current_user;
265 if ( !empty($link_id) )
268 if ( empty($link_rating) )
271 if ( empty($link_target) )
274 if ( empty($link_visible) )
277 if ( empty($link_owner) )
278 $link_owner = $current_user->id;
280 if ( empty($link_notes) )
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'");
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;
297 do_action('edit_link', $link_id);
299 do_action('add_link', $link_id);
304 function wp_update_link($linkdata) {
307 $link_id = (int) $linkdata['link_id'];
309 $link = get_link($link_id, ARRAY_A);
311 // Escape data pulled from DB.
312 $link = add_magic_quotes($link);
314 // Merge old and new fields with new fields overwriting old ones.
315 $linkdata = array_merge($link, $linkdata);
317 return wp_insert_link($linkdata);
320 function wp_delete_link($link_id) {
323 do_action('delete_link', $link_id);
324 return $wpdb->query("DELETE FROM $wpdb->links WHERE link_id = '$link_id'");
327 function post_exists($title, $content = '', $post_date = '') {
330 if (!empty ($post_date))
331 $post_date = "AND post_date = '$post_date'";
334 return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title' $post_date");
336 if (!empty ($content))
337 return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_content = '$content' $post_date");
342 function comment_exists($comment_author, $comment_date) {
345 return $wpdb->get_var("SELECT comment_post_ID FROM $wpdb->comments
346 WHERE comment_author = '$comment_author' AND comment_date = '$comment_date'");