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 = apply_filters('pre_category_name', $cat_name);
97 if (empty ($category_nicename))
98 $category_nicename = sanitize_title($cat_name);
100 $category_nicename = sanitize_title($category_nicename);
101 $category_nicename = apply_filters('pre_category_nicename', $category_nicename);
103 if (empty ($category_description))
104 $category_description = '';
105 $category_description = apply_filters('pre_category_description', $category_description);
107 $category_parent = (int) $category_parent;
108 if (empty ($category_parent))
109 $category_parent = 0;
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 = $wpdb->insert_id;
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'");
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'" );
123 wp_cache_delete($cat_ID, 'category');
126 do_action('edit_category', $cat_ID);
128 wp_cache_delete('all_category_ids', 'category');
129 do_action('create_category', $cat_ID);
130 do_action('add_category', $cat_ID);
136 function wp_update_category($catarr) {
139 $cat_ID = (int) $catarr['cat_ID'];
141 // First, get all of the original fields
142 $category = get_category($cat_ID, ARRAY_A);
144 // Escape data pulled from DB.
145 $category = add_magic_quotes($category);
147 // Merge old and new fields with new fields overwriting old ones.
148 $catarr = array_merge($category, $catarr);
150 return wp_insert_category($catarr);
153 function wp_delete_category($cat_ID) {
156 $cat_ID = (int) $cat_ID;
158 // Don't delete the default cat.
159 if ($cat_ID == get_option('default_category'))
162 $category = get_category($cat_ID);
164 $parent = $category->category_parent;
166 // Delete the category.
167 $wpdb->query("DELETE FROM $wpdb->categories WHERE cat_ID = '$cat_ID'");
169 // Update children to point to new parent.
170 $wpdb->query("UPDATE $wpdb->categories SET category_parent = '$parent' WHERE category_parent = '$cat_ID'");
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'");
176 wp_cache_delete($cat_ID, 'category');
177 wp_cache_delete('all_category_ids', 'category');
179 do_action('delete_category', $cat_ID);
184 function wp_create_category($cat_name) {
185 $cat_array = compact('cat_name');
186 return wp_insert_category($cat_array);
189 function wp_create_categories($categories, $post_id = '') {
191 foreach ($categories as $category) {
192 if ($id = category_exists($category))
195 if ($id = wp_create_category($category))
200 wp_set_post_cats('', $post_id, $cat_ids);
205 function category_exists($cat_name) {
207 if (!$category_nicename = sanitize_title($cat_name))
210 return $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '$category_nicename'");
213 function wp_delete_user($id, $reassign = 'novalue') {
217 $user = get_userdata($id);
219 if ($reassign == 'novalue') {
220 $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_author = $id");
223 foreach ($post_ids as $post_id)
224 wp_delete_post($post_id);
228 $wpdb->query("DELETE FROM $wpdb->links WHERE link_owner = $id");
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}");
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'");
239 wp_cache_delete($id, 'users');
240 wp_cache_delete($user->user_login, 'userlogins');
242 do_action('delete_user', $id);
247 function get_link($link_id, $output = OBJECT) {
250 $link = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = '$link_id'");
252 if ( $output == OBJECT ) {
254 } elseif ( $output == ARRAY_A ) {
255 return get_object_vars($link);
256 } elseif ( $output == ARRAY_N ) {
257 return array_values(get_object_vars($link));
263 function wp_insert_link($linkdata) {
264 global $wpdb, $current_user;
269 if ( !empty($link_id) )
272 if ( empty($link_rating) )
275 if ( empty($link_target) )
278 if ( empty($link_visible) )
281 if ( empty($link_owner) )
282 $link_owner = $current_user->id;
284 if ( empty($link_notes) )
288 $wpdb->query("UPDATE $wpdb->links SET link_url='$link_url',
289 link_name='$link_name', link_image='$link_image',
290 link_target='$link_target', link_category='$link_category',
291 link_visible='$link_visible', link_description='$link_description',
292 link_rating='$link_rating', link_rel='$link_rel',
293 link_notes='$link_notes', link_rss = '$link_rss'
294 WHERE link_id='$link_id'");
296 $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')");
297 $link_id = $wpdb->insert_id;
301 do_action('edit_link', $link_id);
303 do_action('add_link', $link_id);
308 function wp_update_link($linkdata) {
311 $link_id = (int) $linkdata['link_id'];
313 $link = get_link($link_id, ARRAY_A);
315 // Escape data pulled from DB.
316 $link = add_magic_quotes($link);
318 // Merge old and new fields with new fields overwriting old ones.
319 $linkdata = array_merge($link, $linkdata);
321 return wp_insert_link($linkdata);
324 function wp_delete_link($link_id) {
327 do_action('delete_link', $link_id);
328 return $wpdb->query("DELETE FROM $wpdb->links WHERE link_id = '$link_id'");
331 function post_exists($title, $content = '', $post_date = '') {
334 if (!empty ($post_date))
335 $post_date = "AND post_date = '$post_date'";
338 return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title' $post_date");
340 if (!empty ($content))
341 return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_content = '$content' $post_date");
346 function comment_exists($comment_author, $comment_date) {
349 return $wpdb->get_var("SELECT comment_post_ID FROM $wpdb->comments
350 WHERE comment_author = '$comment_author' AND comment_date = '$comment_date'");