| [ Index ] |
WordPress Source Cross Reference |
[Summary view] [Print] [Text view]
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 (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 ( isset($posts_private) ) 112 $posts_private = (int) $posts_private; 113 else 114 $posts_private = 0; 115 116 if ( isset($links_private) ) 117 $links_private = (int) $links_private; 118 else 119 $links_private = 0; 120 121 if (!$update) { 122 $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')"); 123 $cat_ID = $wpdb->insert_id; 124 } else { 125 $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'"); 126 } 127 128 if ( $category_nicename == '' ) { 129 $category_nicename = sanitize_title($cat_name, $cat_ID ); 130 $wpdb->query( "UPDATE $wpdb->categories SET category_nicename = '$category_nicename' WHERE cat_ID = '$cat_ID'" ); 131 } 132 133 wp_cache_delete($cat_ID, 'category'); 134 135 if ($update) { 136 do_action('edit_category', $cat_ID); 137 } else { 138 wp_cache_delete('all_category_ids', 'category'); 139 do_action('create_category', $cat_ID); 140 do_action('add_category', $cat_ID); 141 } 142 143 return $cat_ID; 144 } 145 146 function wp_update_category($catarr) { 147 global $wpdb; 148 149 $cat_ID = (int) $catarr['cat_ID']; 150 151 // First, get all of the original fields 152 $category = get_category($cat_ID, ARRAY_A); 153 154 // Escape data pulled from DB. 155 $category = add_magic_quotes($category); 156 157 // Merge old and new fields with new fields overwriting old ones. 158 $catarr = array_merge($category, $catarr); 159 160 return wp_insert_category($catarr); 161 } 162 163 function wp_delete_category($cat_ID) { 164 global $wpdb; 165 166 $cat_ID = (int) $cat_ID; 167 168 // Don't delete the default cat. 169 if ( $cat_ID == get_option('default_category') ) 170 return 0; 171 172 if ( $cat_ID == get_option('default_link_category') ) 173 return 0; 174 175 $category = get_category($cat_ID); 176 177 $parent = $category->category_parent; 178 179 // Delete the category. 180 if ( !$wpdb->query("DELETE FROM $wpdb->categories WHERE cat_ID = '$cat_ID'") ) 181 return 0; 182 183 // Update children to point to new parent. 184 $wpdb->query("UPDATE $wpdb->categories SET category_parent = '$parent' WHERE category_parent = '$cat_ID'"); 185 186 // Only set posts and links to the default category if they're not in another category already. 187 $default_cat = get_option('default_category'); 188 $posts = $wpdb->get_col("SELECT post_id FROM $wpdb->post2cat WHERE category_id='$cat_ID'"); 189 if ( is_array($posts) ) foreach ($posts as $post_id) { 190 $cats = wp_get_post_categories($post_id); 191 if ( 1 == count($cats) ) 192 $cats = array($default_cat); 193 else 194 $cats = array_diff($cats, array($cat_ID)); 195 wp_set_post_categories($post_id, $cats); 196 } 197 198 $default_link_cat = get_option('default_link_category'); 199 $links = $wpdb->get_col("SELECT link_id FROM $wpdb->link2cat WHERE category_id='$cat_ID'"); 200 if ( is_array($links) ) foreach ($links as $link_id) { 201 $cats = wp_get_link_cats($link_id); 202 if ( 1 == count($cats) ) 203 $cats = array($default_link_cat); 204 else 205 $cats = array_diff($cats, array($cat_ID)); 206 wp_set_link_cats($link_id, $cats); 207 } 208 209 wp_cache_delete($cat_ID, 'category'); 210 wp_cache_delete('all_category_ids', 'category'); 211 212 do_action('delete_category', $cat_ID); 213 214 return 1; 215 } 216 217 function wp_create_category($cat_name) { 218 $cat_array = compact('cat_name'); 219 return wp_insert_category($cat_array); 220 } 221 222 function wp_create_categories($categories, $post_id = '') { 223 $cat_ids = array (); 224 foreach ($categories as $category) { 225 if ($id = category_exists($category)) 226 $cat_ids[] = $id; 227 else 228 if ($id = wp_create_category($category)) 229 $cat_ids[] = $id; 230 } 231 232 if ($post_id) 233 wp_set_post_categories($post_id, $cat_ids); 234 235 return $cat_ids; 236 } 237 238 function category_exists($cat_name) { 239 global $wpdb; 240 if (!$category_nicename = sanitize_title($cat_name)) 241 return 0; 242 243 return $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '$category_nicename'"); 244 } 245 246 function wp_delete_user($id, $reassign = 'novalue') { 247 global $wpdb; 248 249 $id = (int) $id; 250 $user = get_userdata($id); 251 252 if ($reassign == 'novalue') { 253 $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_author = $id"); 254 255 if ($post_ids) { 256 foreach ($post_ids as $post_id) 257 wp_delete_post($post_id); 258 } 259 260 // Clean links 261 $wpdb->query("DELETE FROM $wpdb->links WHERE link_owner = $id"); 262 } else { 263 $reassign = (int) $reassign; 264 $wpdb->query("UPDATE $wpdb->posts SET post_author = {$reassign} WHERE post_author = {$id}"); 265 $wpdb->query("UPDATE $wpdb->links SET link_owner = {$reassign} WHERE link_owner = {$id}"); 266 } 267 268 // FINALLY, delete user 269 $wpdb->query("DELETE FROM $wpdb->users WHERE ID = $id"); 270 $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = '$id'"); 271 272 wp_cache_delete($id, 'users'); 273 wp_cache_delete($user->user_login, 'userlogins'); 274 275 do_action('delete_user', $id); 276 277 return true; 278 } 279 280 function wp_revoke_user($id) { 281 $id = (int) $id; 282 283 $user = new WP_User($id); 284 $user->remove_all_caps(); 285 } 286 287 function wp_insert_link($linkdata) { 288 global $wpdb, $current_user; 289 290 extract($linkdata); 291 292 $update = false; 293 if ( !empty($link_id) ) 294 $update = true; 295 296 if ( empty($link_rating) ) 297 $link_rating = 0; 298 299 if ( empty($link_target) ) 300 $link_target = ''; 301 302 if ( empty($link_visible) ) 303 $link_visible = 'Y'; 304 305 if ( empty($link_owner) ) 306 $link_owner = $current_user->id; 307 308 if ( empty($link_notes) ) 309 $link_notes = ''; 310 311 // Make sure we set a valid category 312 if (0 == count($link_category) || !is_array($link_category)) { 313 $link_category = array(get_option('default_link_category')); 314 } 315 316 if ( $update ) { 317 $wpdb->query("UPDATE $wpdb->links SET link_url='$link_url', 318 link_name='$link_name', link_image='$link_image', 319 link_target='$link_target', 320 link_visible='$link_visible', link_description='$link_description', 321 link_rating='$link_rating', link_rel='$link_rel', 322 link_notes='$link_notes', link_rss = '$link_rss' 323 WHERE link_id='$link_id'"); 324 } else { 325 $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')"); 326 $link_id = $wpdb->insert_id; 327 } 328 329 wp_set_link_cats($link_id, $link_category); 330 331 if ( $update ) 332 do_action('edit_link', $link_id); 333 else 334 do_action('add_link', $link_id); 335 336 return $link_id; 337 } 338 339 function wp_update_link($linkdata) { 340 global $wpdb; 341 342 $link_id = (int) $linkdata['link_id']; 343 344 $link = get_link($link_id, ARRAY_A); 345 346 // Escape data pulled from DB. 347 $link = add_magic_quotes($link); 348 349 // Passed link category list overwrites existing category list if not empty. 350 if ( isset($linkdata['link_category']) && is_array($linkdata['link_category']) 351 && 0 != count($linkdata['link_category']) ) 352 $link_cats = $linkdata['link_category']; 353 else 354 $link_cats = $link['link_category']; 355 356 // Merge old and new fields with new fields overwriting old ones. 357 $linkdata = array_merge($link, $linkdata); 358 $linkdata['link_category'] = $link_cats; 359 360 return wp_insert_link($linkdata); 361 } 362 363 function wp_delete_link($link_id) { 364 global $wpdb; 365 366 do_action('delete_link', $link_id); 367 368 $categories = wp_get_link_cats($link_id); 369 if( is_array( $categories ) ) { 370 foreach ( $categories as $category ) { 371 $wpdb->query("UPDATE $wpdb->categories SET link_count = link_count - 1 WHERE cat_ID = '$category'"); 372 wp_cache_delete($category, 'category'); 373 } 374 } 375 376 $wpdb->query("DELETE FROM $wpdb->link2cat WHERE link_id = '$link_id'"); 377 return $wpdb->query("DELETE FROM $wpdb->links WHERE link_id = '$link_id'"); 378 } 379 380 function wp_get_link_cats($link_ID = 0) { 381 global $wpdb; 382 383 $sql = "SELECT category_id 384 FROM $wpdb->link2cat 385 WHERE link_id = $link_ID 386 ORDER BY category_id"; 387 388 $result = $wpdb->get_col($sql); 389 390 if ( !$result ) 391 $result = array(); 392 393 return array_unique($result); 394 } 395 396 function wp_set_link_cats($link_ID = 0, $link_categories = array()) { 397 global $wpdb; 398 // If $link_categories isn't already an array, make it one: 399 if (!is_array($link_categories) || 0 == count($link_categories)) 400 $link_categories = array(get_option('default_link_category')); 401 402 $link_categories = array_unique($link_categories); 403 404 // First the old categories 405 $old_categories = $wpdb->get_col(" 406 SELECT category_id 407 FROM $wpdb->link2cat 408 WHERE link_id = $link_ID"); 409 410 if (!$old_categories) { 411 $old_categories = array(); 412 } else { 413 $old_categories = array_unique($old_categories); 414 } 415 416 // Delete any? 417 $delete_cats = array_diff($old_categories,$link_categories); 418 419 if ($delete_cats) { 420 foreach ($delete_cats as $del) { 421 $wpdb->query(" 422 DELETE FROM $wpdb->link2cat 423 WHERE category_id = $del 424 AND link_id = $link_ID 425 "); 426 } 427 } 428 429 // Add any? 430 $add_cats = array_diff($link_categories, $old_categories); 431 432 if ($add_cats) { 433 foreach ($add_cats as $new_cat) { 434 $wpdb->query(" 435 INSERT INTO $wpdb->link2cat (link_id, category_id) 436 VALUES ($link_ID, $new_cat)"); 437 } 438 } 439 440 // Update category counts. 441 $all_affected_cats = array_unique(array_merge($link_categories, $old_categories)); 442 foreach ( $all_affected_cats as $cat_id ) { 443 $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'"); 444 $wpdb->query("UPDATE $wpdb->