| [ Index ] |
PHP Cross Reference of WordPress Trunk (Latest) |
[Summary view] [Print] [Text view]
1 <?php 2 3 /**** DB Functions ****/ 4 5 /* 6 * generic function for inserting data into the posts table. 7 */ 8 function wp_insert_post($postarr = array()) { 9 global $wpdb, $wp_rewrite, $allowedtags, $user_ID; 10 11 if ( is_object($postarr) ) 12 $postarr = get_object_vars($postarr); 13 14 // export array as variables 15 extract($postarr); 16 17 // Are we updating or creating? 18 $update = false; 19 if ( !empty($ID) ) { 20 $update = true; 21 $post = & get_post($ID); 22 $previous_status = $post->post_status; 23 } 24 25 // Get the basics. 26 $post_content = apply_filters('content_save_pre', $post_content); 27 $post_excerpt = apply_filters('excerpt_save_pre', $post_excerpt); 28 $post_title = apply_filters('title_save_pre', $post_title); 29 $post_category = apply_filters('category_save_pre', $post_category); 30 $post_status = apply_filters('status_save_pre', $post_status); 31 $post_name = apply_filters('name_save_pre', $post_name); 32 $comment_status = apply_filters('comment_status_pre', $comment_status); 33 $ping_status = apply_filters('ping_status_pre', $ping_status); 34 35 // Make sure we set a valid category 36 if (0 == count($post_category) || !is_array($post_category)) { 37 $post_category = array(get_option('default_category')); 38 } 39 $post_cat = $post_category[0]; 40 41 if ( empty($post_author) ) 42 $post_author = $user_ID; 43 44 if ( empty($post_status) ) 45 $post_status = 'draft'; 46 47 if ( empty($post_type) ) 48 $post_type = 'post'; 49 50 // Get the post ID. 51 if ( $update ) 52 $post_ID = $ID; 53 54 // Create a valid post name. Drafts are allowed to have an empty 55 // post name. 56 if ( empty($post_name) ) { 57 if ( 'draft' != $post_status ) 58 $post_name = sanitize_title($post_title); 59 } else { 60 $post_name = sanitize_title($post_name); 61 } 62 63 64 // If the post date is empty (due to having been new or a draft) and status is not 'draft', set date to now 65 if (empty($post_date)) { 66 if ( 'draft' != $post_status ) 67 $post_date = current_time('mysql'); 68 } 69 70 if (empty($post_date_gmt)) { 71 if ( 'draft' != $post_status ) 72 $post_date_gmt = get_gmt_from_date($post_date); 73 } 74 75 if ( 'publish' == $post_status ) { 76 $now = gmdate('Y-m-d H:i:59'); 77 if ( mysql2date('U', $post_date_gmt) > mysql2date('U', $now) ) 78 $post_status = 'future'; 79 } 80 81 if ( empty($comment_status) ) { 82 if ( $update ) 83 $comment_status = 'closed'; 84 else 85 $comment_status = get_settings('default_comment_status'); 86 } 87 if ( empty($ping_status) ) 88 $ping_status = get_settings('default_ping_status'); 89 if ( empty($post_pingback) ) 90 $post_pingback = get_option('default_pingback_flag'); 91 92 if ( isset($to_ping) ) 93 $to_ping = preg_replace('|\s+|', "\n", $to_ping); 94 else 95 $to_ping = ''; 96 97 if ( ! isset($pinged) ) 98 $pinged = ''; 99 100 if ( isset($post_parent) ) 101 $post_parent = (int) $post_parent; 102 else 103 $post_parent = 0; 104 105 if ( isset($menu_order) ) 106 $menu_order = (int) $menu_order; 107 else 108 $menu_order = 0; 109 110 if ( !isset($post_password) ) 111 $post_password = ''; 112 113 if ( 'draft' != $post_status ) { 114 $post_name_check = $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$post_name' AND post_type = '$post_type' AND ID != '$post_ID' AND post_parent = '$post_parent' LIMIT 1"); 115 116 if ($post_name_check) { 117 $suffix = 2; 118 while ($post_name_check) { 119 $alt_post_name = $post_name . "-$suffix"; 120 $post_name_check = $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$alt_post_name' AND post_type = '$post_type' AND ID != '$post_ID' AND post_parent = '$post_parent' LIMIT 1"); 121 $suffix++; 122 } 123 $post_name = $alt_post_name; 124 } 125 } 126 127 if ($update) { 128 $wpdb->query( 129 "UPDATE IGNORE $wpdb->posts SET 130 post_author = '$post_author', 131 post_date = '$post_date', 132 post_date_gmt = '$post_date_gmt', 133 post_content = '$post_content', 134 post_content_filtered = '$post_content_filtered', 135 post_title = '$post_title', 136 post_excerpt = '$post_excerpt', 137 post_status = '$post_status', 138 post_type = '$post_type', 139 comment_status = '$comment_status', 140 ping_status = '$ping_status', 141 post_password = '$post_password', 142 post_name = '$post_name', 143 to_ping = '$to_ping', 144 pinged = '$pinged', 145 post_modified = '".current_time('mysql')."', 146 post_modified_gmt = '".current_time('mysql',1)."', 147 post_parent = '$post_parent', 148 menu_order = '$menu_order' 149 WHERE ID = $post_ID"); 150 } else { 151 $wpdb->query( 152 "INSERT IGNORE INTO $wpdb->posts 153 (post_author, post_date, post_date_gmt, post_content, post_content_filtered, post_title, post_excerpt, post_status, post_type, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_parent, menu_order, post_mime_type) 154 VALUES 155 ('$post_author', '$post_date', '$post_date_gmt', '$post_content', '$post_content_filtered', '$post_title', '$post_excerpt', '$post_status', '$post_type', '$comment_status', '$ping_status', '$post_password', '$post_name', '$to_ping', '$pinged', '$post_date', '$post_date_gmt', '$post_parent', '$menu_order', '$post_mime_type')"); 156 $post_ID = $wpdb->insert_id; 157 } 158 159 if ( empty($post_name) && 'draft' != $post_status ) { 160 $post_name = sanitize_title($post_title, $post_ID); 161 $wpdb->query( "UPDATE $wpdb->posts SET post_name = '$post_name' WHERE ID = '$post_ID'" ); 162 } 163 164 wp_set_post_categories($post_ID, $post_category); 165 166 if ( 'page' == $post_type ) { 167 clean_page_cache($post_ID); 168 wp_cache_delete($post_ID, 'pages'); 169 } else { 170 clean_post_cache($post_ID); 171 } 172 173 // Set GUID 174 if ( ! $update ) 175 $wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'"); 176 177 if ( $update) { 178 if ($previous_status != 'publish' && $post_status == 'publish') { 179 // Reset GUID if transitioning to publish. 180 $wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'"); 181 do_action('private_to_published', $post_ID); 182 } 183 184 do_action('edit_post', $post_ID); 185 } 186 187 if ($post_status == 'publish' && $post_type == 'post') { 188 do_action('publish_post', $post_ID); 189 190 if ( !defined('WP_IMPORTING') ) { 191 if ( $post_pingback ) 192 $result = $wpdb->query(" 193 INSERT INTO $wpdb->postmeta 194 (post_id,meta_key,meta_value) 195 VALUES ('$post_ID','_pingme','1') 196 "); 197 $result = $wpdb->query(" 198 INSERT INTO $wpdb->postmeta 199 (post_id,meta_key,meta_value) 200 VALUES ('$post_ID','_encloseme','1') 201 "); 202 wp_schedule_single_event(time(), 'do_pings'); 203 } 204 } else if ($post_type == 'page') { 205 wp_cache_delete('all_page_ids', 'pages'); 206 $wp_rewrite->flush_rules(); 207 208 if ( !empty($page_template) ) 209 if ( ! update_post_meta($post_ID, '_wp_page_template', $page_template)) 210 add_post_meta($post_ID, '_wp_page_template', $page_template, true); 211 212 if ( $post_status == 'publish' ) 213 do_action('publish_page', $post_ID); 214 } 215 216 if ( 'future' == $post_status ) { 217 wp_schedule_single_event(mysql2date('U', $post_date), 'publish_future_post', $post_ID); 218 } 219 220 do_action('save_post', $post_ID); 221 do_action('wp_insert_post', $post_ID); 222 223 return $post_ID; 224 } 225 226 function wp_insert_attachment($object, $file = false, $post_parent = 0) { 227 global $wpdb, $user_ID; 228 229 if ( is_object($object) ) 230 $object = get_object_vars($object); 231 232 // Export array as variables 233 extract($object); 234 235 // Get the basics. 236 $post_content = apply_filters('content_save_pre', $post_content); 237 $post_excerpt = apply_filters('excerpt_save_pre', $post_excerpt); 238 $post_title = apply_filters('title_save_pre', $post_title); 239 $post_category = apply_filters('category_save_pre', $post_category); 240 $post_name = apply_filters('name_save_pre', $post_name); 241 $comment_status = apply_filters('comment_status_pre', $comment_status); 242 $ping_status = apply_filters('ping_status_pre', $ping_status); 243 $post_mime_type = apply_filters('post_mime_type_pre', $post_mime_type); 244 245 // Make sure we set a valid category 246 if (0 == count($post_category) || !is_array($post_category)) { 247 $post_category = array(get_option('default_category')); 248 } 249 $post_cat = $post_category[0]; 250 251 if ( empty($post_author) ) 252 $post_author = $user_ID; 253 254 $post_type = 'attachment'; 255 $post_status = 'inherit'; 256 257 // Are we updating or creating? 258 $update = false; 259 if ( !empty($ID) ) { 260 $update = true; 261 $post_ID = $ID; 262 } 263 264 // Create a valid post name. 265 if ( empty($post_name) ) 266 $post_name = sanitize_title($post_title); 267 else 268 $post_name = sanitize_title($post_name); 269 270 if (empty($post_date)) 271 $post_date = current_time('mysql'); 272 if (empty($post_date_gmt)) 273 $post_date_gmt = current_time('mysql', 1); 274 275 if ( empty($comment_status) ) { 276 if ( $update ) 277 $comment_status = 'closed'; 278 else 279 $comment_status = get_settings('default_comment_status'); 280 } 281 if ( empty($ping_status) ) 282 $ping_status = get_settings('default_ping_status'); 283 if ( empty($post_pingback) ) 284 $post_pingback = get_option('default_pingback_flag'); 285 286 if ( isset($to_ping) ) 287 $to_ping = preg_replace('|\s+|', "\n", $to_ping); 288 else 289 $to_ping = ''; 290 291 if ( isset($post_parent) ) 292 $post_parent = (int) $post_parent; 293 else 294 $post_parent = 0; 295 296 if ( isset($menu_order) ) 297 $menu_order = (int) $menu_order; 298 else 299 $menu_order = 0; 300 301 if ( !isset($post_password) ) 302 $post_password = ''; 303 304 if ( isset($to_ping) ) 305 $to_ping = preg_replace('|\s+|', "\n", $to_ping); 306 else 307 $to_ping = ''; 308 309 if ( ! isset($pinged) ) 310 $pinged = ''; 311 312 if ($update) { 313 $wpdb->query( 314 "UPDATE $wpdb->posts SET 315 post_author = '$post_author', 316 post_date = '$post_date', 317 post_date_gmt = '$post_date_gmt', 318 post_content = '$post_content', 319 post_content_filtered = '$post_content_filtered', 320 post_title = '$post_title', 321 post_excerpt = '$post_excerpt', 322 post_status = '$post_status', 323 post_type = '$post_type', 324 comment_status = '$comment_status', 325 ping_status = '$ping_status', 326 post_password = '$post_password', 327 post_name = '$post_name', 328 to_ping = '$to_ping', 329 pinged = '$pinged', 330 post_modified = '".current_time('mysql')."', 331 post_modified_gmt = '".current_time('mysql',1)."', 332 post_parent = '$post_parent', 333 menu_order = '$menu_order', 334 post_mime_type = '$post_mime_type', 335 guid = '$guid' 336 WHERE ID = $post_ID"); 337 } else { 338 $wpdb->query( 339 "INSERT INTO $wpdb->posts 340 (post_author, post_date, post_date_gmt, post_content, post_content_filtered, post_title, post_excerpt, post_status, post_type, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_parent, menu_order, post_mime_type, guid) 341 VALUES 342 ('$post_author', '$post_date', '$post_date_gmt', '$post_content', '$post_content_filtered', '$post_title', '$post_excerpt', '$post_status', '$post_type', '$comment_status', '$ping_status', '$post_password', '$post_name', '$to_ping', '$pinged', '$post_date', '$post_date_gmt', '$post_parent', '$menu_order', '$post_mime_type', '$guid')"); 343 $post_ID = $wpdb->insert_id; 344 } 345 346 if ( empty($post_name) ) { 347 $post_name = sanitize_title($post_title, $post_ID); 348 $wpdb->query( "UPDATE $wpdb->posts SET post_name = '$post_name' WHERE ID = '$post_ID'" ); 349 } 350 351 wp_set_post_categories($post_ID, $post_category); 352 353 if ( $file ) 354 add_post_meta($post_ID, '_wp_attached_file', $file ); 355 356 clean_post_cache($post_ID); 357 358 if ( $update) { 359 do_action('edit_attachment', $post_ID); 360 } else { 361 do_action('add_attachment', $post_ID); 362 } 363 364 return $post_ID; 365 } 366 367 function wp_delete_attachment($postid) { 368 global $wpdb; 369 $postid = (int) $postid; 370 371 if ( !$post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = '$postid'") ) 372 return $post; 373 374 if ( 'attachment' != $post->post_type ) 375 return false; 376 377 $meta = get_post_meta($postid, '_wp_attachment_metadata', true); 378 $file = get_post_meta($postid, '_wp_attached_file', true); 379 380 $wpdb->query("DELETE FROM $wpdb->posts WHERE ID = '$postid'"); 381 382 $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID = '$postid'"); 383 384 $wpdb->query("DELETE FROM $wpdb->post2cat WHERE post_id = '$postid'"); 385 386 $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$postid'"); 387 388 if ( ! empty($meta['thumb']) ) { 389 // Don't delete the thumb if another attachment uses it 390 if (! $wpdb->get_row("SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE '%".$wpdb->escape($meta['thumb'])."%' AND post_id <> $postid")) { 391 $thumbfile = str_replace(basename($file), $meta['thumb'], $file); 392 $thumbfile = apply_filters('wp_delete_file', $thumbfile); 393 @ unlink($thumbfile); 394 } 395 } 396 397 $file = apply_filters('wp_delete_file', $file); 398 399 if ( ! empty($file) ) 400 @ unlink($file); 401 402 do_action('delete_attachment', $postid); 403 404 return $post; 405 } 406 407 function wp_get_single_post($postid = 0, $mode = OBJECT) { 408 global $wpdb; 409 410 $post = get_post($postid, $mode); 411 412 // Set categories 413 if($mode == OBJECT) { 414 $post->post_category = wp_get_post_categories($postid); 415 } 416 else { 417 $post['post_category'] = wp_get_post_categories($postid); 418 } 419 420 return $post; 421 } 422 423 function wp_get_recent_posts($num = 10) { 424 global $wpdb; 425 426 // Set the limit clause, if we got a limit 427 if ($num) { 428 $limit = "LIMIT $num"; 429 } 430 431 $sql = "SELECT * FROM $wpdb->posts WHERE post_type = 'post' ORDER BY post_date DESC $limit"; 432 $result = $wpdb->get_results($sql,ARRAY_A); 433 434 return $result?$result:array(); 435 } 436 437 function wp_update_post($postarr = array()) { 438 global $wpdb; 439 440 if ( is_object($postarr) ) 441 $postarr = get_object_vars($postarr); 442 443 // First, get all of the original fields 444 $post = wp_get_single_post($postarr['ID'], ARRAY_A); 445 446 // Escape data pulled from DB. 447 $post = add_magic_quotes($post); 448 449 // Passed post category list overwrites existing category list if not empty. 450 if ( isset($postarr['post_category']) && is_array($postarr['post_category']) 451 && 0 != count($postarr['post_category']) ) 452