[ Index ] |
|
Code source de WordPress 2.1.2 |
1 <?php 2 3 // 4 // Post functions 5 // 6 7 function get_attached_file( $attachment_id, $unfiltered = false ) { 8 $file = get_post_meta( $attachment_id, '_wp_attached_file', true ); 9 if ( $unfiltered ) 10 return $file; 11 return apply_filters( 'get_attached_file', $file, $attachment_id ); 12 } 13 14 function update_attached_file( $attachment_id, $file ) { 15 if ( !get_post( $attachment_id ) ) 16 return false; 17 18 $old_file = get_attached_file( $attachment_id, true ); 19 20 $file = apply_filters( 'update_attached_file', $file, $attachment_id ); 21 22 if ( $old_file ) 23 return update_post_meta( $attachment_id, '_wp_attached_file', $file, $old_file ); 24 else 25 return add_post_meta( $attachment_id, '_wp_attached_file', $file ); 26 } 27 28 function &get_children($args = '', $output = OBJECT) { 29 global $post_cache, $wpdb, $blog_id; 30 31 if ( empty($args) ) { 32 if ( isset($GLOBALS['post']) ) 33 $r = array('post_parent' => & $GLOBALS['post']->post_parent); 34 else 35 return false; 36 } elseif ( is_object($args) ) 37 $r = array('post_parent' => $post->post_parent); 38 elseif ( is_numeric($args) ) 39 $r = array('post_parent' => $args); 40 elseif ( is_array($args) ) 41 $r = &$args; 42 else 43 parse_str($args, $r); 44 45 $defaults = array('numberposts' => -1, 'post_type' => '', 'post_status' => '', 'post_parent' => 0); 46 $r = array_merge($defaults, $r); 47 48 $children = get_posts( $r ); 49 50 if ( $children ) { 51 foreach ( $children as $key => $child ) { 52 $post_cache[$blog_id][$child->ID] =& $children[$key]; 53 $kids[$child->ID] =& $children[$key]; 54 } 55 } else { 56 return false; 57 } 58 59 if ( $output == OBJECT ) { 60 return $kids; 61 } elseif ( $output == ARRAY_A ) { 62 foreach ( $kids as $kid ) 63 $weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]); 64 return $weeuns; 65 } elseif ( $output == ARRAY_N ) { 66 foreach ( $kids as $kid ) 67 $babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID])); 68 return $babes; 69 } else { 70 return $kids; 71 } 72 } 73 74 // get extended entry info (<!--more-->) 75 function get_extended($post) { 76 //Match the new style more links 77 if ( preg_match('/<!--more(.*?)-->/', $post, $matches) ) { 78 list($main, $extended) = explode($matches[0], $post, 2); 79 } else { 80 $main = $post; 81 $extended = ''; 82 } 83 84 // Strip leading and trailing whitespace 85 $main = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $main); 86 $extended = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $extended); 87 88 return array('main' => $main, 'extended' => $extended); 89 } 90 91 // Retrieves post data given a post ID or post object. 92 // Handles post caching. 93 function &get_post(&$post, $output = OBJECT) { 94 global $post_cache, $wpdb, $blog_id; 95 96 if ( empty($post) ) { 97 if ( isset($GLOBALS['post']) ) 98 $_post = & $GLOBALS['post']; 99 else 100 $_post = null; 101 } elseif ( is_object($post) ) { 102 if ( 'page' == $post->post_type ) 103 return get_page($post, $output); 104 if ( !isset($post_cache[$blog_id][$post->ID]) ) 105 $post_cache[$blog_id][$post->ID] = &$post; 106 $_post = & $post_cache[$blog_id][$post->ID]; 107 } else { 108 if ( $_post = wp_cache_get($post, 'pages') ) 109 return get_page($_post, $output); 110 elseif ( isset($post_cache[$blog_id][$post]) ) 111 $_post = & $post_cache[$blog_id][$post]; 112 else { 113 $query = "SELECT * FROM $wpdb->posts WHERE ID = '$post' LIMIT 1"; 114 $_post = & $wpdb->get_row($query); 115 if ( 'page' == $_post->post_type ) 116 return get_page($_post, $output); 117 $post_cache[$blog_id][$post] = & $_post; 118 } 119 } 120 121 if ( defined('WP_IMPORTING') ) 122 unset($post_cache[$blog_id]); 123 124 if ( $output == OBJECT ) { 125 return $_post; 126 } elseif ( $output == ARRAY_A ) { 127 return get_object_vars($_post); 128 } elseif ( $output == ARRAY_N ) { 129 return array_values(get_object_vars($_post)); 130 } else { 131 return $_post; 132 } 133 } 134 135 // Takes a post ID, returns its mime type. 136 function get_post_mime_type($ID = '') { 137 $post = & get_post($ID); 138 139 if ( is_object($post) ) 140 return $post->post_mime_type; 141 142 return false; 143 } 144 145 function get_post_status($ID = '') { 146 $post = get_post($ID); 147 148 if ( is_object($post) ) { 149 if ( ('attachment' == $post->post_type) && $post->post_parent && ($post->ID != $post->post_parent) ) 150 return get_post_status($post->post_parent); 151 else 152 return $post->post_status; 153 } 154 155 return false; 156 } 157 158 function get_post_type($post = false) { 159 global $wpdb, $posts; 160 161 if ( false === $post ) 162 $post = $posts[0]; 163 elseif ( (int) $post ) 164 $post = get_post($post, OBJECT); 165 166 if ( is_object($post) ) 167 return $post->post_type; 168 169 return false; 170 } 171 172 function get_posts($args) { 173 global $wpdb; 174 175 if ( is_array($args) ) 176 $r = &$args; 177 else 178 parse_str($args, $r); 179 180 $defaults = array('numberposts' => 5, 'offset' => 0, 'category' => 0, 181 'orderby' => 'post_date', 'order' => 'DESC', 'include' => '', 'exclude' => '', 182 'meta_key' => '', 'meta_value' =>'', 'post_type' => 'post', 'post_status' => 'publish', 'post_parent' => 0); 183 $r = array_merge($defaults, $r); 184 extract($r); 185 $numberposts = (int) $numberposts; 186 $offset = (int) $offset; 187 $category = (int) $category; 188 $post_parent = (int) $post_parent; 189 190 $inclusions = ''; 191 if ( !empty($include) ) { 192 $offset = 0; //ignore offset, category, exclude, meta_key, and meta_value, post_parent if using include 193 $category = 0; 194 $exclude = ''; 195 $meta_key = ''; 196 $meta_value = ''; 197 $post_parent = 0; 198 $incposts = preg_split('/[\s,]+/',$include); 199 $numberposts = count($incposts); // only the number of posts included 200 if ( count($incposts) ) { 201 foreach ( $incposts as $incpost ) { 202 if (empty($inclusions)) 203 $inclusions = ' AND ( ID = ' . intval($incpost) . ' '; 204 else 205 $inclusions .= ' OR ID = ' . intval($incpost) . ' '; 206 } 207 } 208 } 209 if (!empty($inclusions)) 210 $inclusions .= ')'; 211 212 $exclusions = ''; 213 if ( !empty($exclude) ) { 214 $exposts = preg_split('/[\s,]+/',$exclude); 215 if ( count($exposts) ) { 216 foreach ( $exposts as $expost ) { 217 if (empty($exclusions)) 218 $exclusions = ' AND ( ID <> ' . intval($expost) . ' '; 219 else 220 $exclusions .= ' AND ID <> ' . intval($expost) . ' '; 221 } 222 } 223 } 224 if (!empty($exclusions)) 225 $exclusions .= ')'; 226 227 $query ="SELECT DISTINCT * FROM $wpdb->posts " ; 228 $query .= ( empty( $category ) ? "" : ", $wpdb->post2cat " ); 229 $query .= ( empty( $meta_key ) ? "" : ", $wpdb->postmeta " ); 230 $query .= " WHERE (post_type = 'post' AND post_status = 'publish') $exclusions $inclusions "; 231 $query .= ( empty( $category ) ? "" : "AND ($wpdb->posts.ID = $wpdb->post2cat.post_id AND $wpdb->post2cat.category_id = " . $category. ") " ); 232 $query .= ( empty( $meta_key ) | empty($meta_value) ? "" : " AND ($wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = '$meta_key' AND $wpdb->postmeta.meta_value = '$meta_value' )" ); 233 $query .= " GROUP BY $wpdb->posts.ID ORDER BY " . $orderby . " " . $order . " LIMIT " . $offset . ',' . $numberposts; 234 235 $query = "SELECT DISTINCT * FROM $wpdb->posts "; 236 $query .= empty( $category ) ? '' : ", $wpdb->post2cat "; 237 $query .= empty( $meta_key ) ? '' : ", $wpdb->postmeta "; 238 $query .= " WHERE 1=1 "; 239 $query .= empty( $post_type ) ? '' : "AND post_type = '$post_type' "; 240 $query .= empty( $post_status ) ? '' : "AND post_status = '$post_status' "; 241 $query .= "$exclusions $inclusions " ; 242 $query .= empty( $category ) ? '' : "AND ($wpdb->posts.ID = $wpdb->post2cat.post_id AND $wpdb->post2cat.category_id = " . $category. ") "; 243 $query .= empty( $post_parent ) ? '' : "AND $wpdb->posts.post_parent = '$post_parent' "; 244 $query .= empty( $meta_key ) | empty($meta_value) ? '' : " AND ($wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = '$meta_key' AND $wpdb->postmeta.meta_value = '$meta_value' )"; 245 $query .= " GROUP BY $wpdb->posts.ID ORDER BY " . $orderby . ' ' . $order; 246 if ( 0 < $numberposts ) 247 $query .= " LIMIT " . $offset . ',' . $numberposts; 248 249 $posts = $wpdb->get_results($query); 250 251 update_post_caches($posts); 252 253 return $posts; 254 } 255 256 // 257 // Post meta functions 258 // 259 260 function add_post_meta($post_id, $key, $value, $unique = false) { 261 global $wpdb, $post_meta_cache, $blog_id; 262 263 $post_id = (int) $post_id; 264 265 if ( $unique ) { 266 if ( $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key = '$key' AND post_id = '$post_id'") ) { 267 return false; 268 } 269 } 270 271 $post_meta_cache[$blog_id][$post_id][$key][] = $value; 272 273 $value = maybe_serialize($value); 274 $value = $wpdb->escape($value); 275 276 $wpdb->query("INSERT INTO $wpdb->postmeta (post_id,meta_key,meta_value) VALUES ('$post_id','$key','$value')"); 277 278 return true; 279 } 280 281 function delete_post_meta($post_id, $key, $value = '') { 282 global $wpdb, $post_meta_cache, $blog_id; 283 284 $post_id = (int) $post_id; 285 286 if ( empty($value) ) { 287 $meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key'"); 288 } else { 289 $meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key' AND meta_value = '$value'"); 290 } 291 292 if ( !$meta_id ) 293 return false; 294 295 if ( empty($value) ) { 296 $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key'"); 297 unset($post_meta_cache[$blog_id][$post_id][$key]); 298 } else { 299 $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key' AND meta_value = '$value'"); 300 $cache_key = $post_meta_cache[$blog_id][$post_id][$key]; 301 if ($cache_key) foreach ( $cache_key as $index => $data ) 302 if ( $data == $value ) 303 unset($post_meta_cache[$blog_id][$post_id][$key][$index]); 304 } 305 306 unset($post_meta_cache[$blog_id][$post_id][$key]); 307 308 return true; 309 } 310 311 function get_post_meta($post_id, $key, $single = false) { 312 global $wpdb, $post_meta_cache, $blog_id; 313 314 $post_id = (int) $post_id; 315 316 if ( isset($post_meta_cache[$blog_id][$post_id][$key]) ) { 317 if ( $single ) { 318 return maybe_unserialize( $post_meta_cache[$blog_id][$post_id][$key][0] ); 319 } else { 320 return maybe_unserialize( $post_meta_cache[$blog_id][$post_id][$key] ); 321 } 322 } 323 324 if ( !isset($post_meta_cache[$blog_id][$post_id]) ) 325 update_postmeta_cache($post_id); 326 327 if ( $single ) { 328 if ( isset($post_meta_cache[$blog_id][$post_id][$key][0]) ) 329 return maybe_unserialize($post_meta_cache[$blog_id][$post_id][$key][0]); 330 else 331 return ''; 332 } else { 333 return maybe_unserialize($post_meta_cache[$blog_id][$post_id][$key]); 334 } 335 } 336 337 function update_post_meta($post_id, $key, $value, $prev_value = '') { 338 global $wpdb, $post_meta_cache, $blog_id; 339 340 $post_id = (int) $post_id; 341 342 $original_value = $value; 343 $value = maybe_serialize($value); 344 $value = $wpdb->escape($value); 345 346 $original_prev = $prev_value; 347 $prev_value = maybe_serialize($prev_value); 348 $prev_value = $wpdb->escape($prev_value); 349 350 if (! $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key = '$key' AND post_id = '$post_id'") ) { 351 return false; 352 } 353 354 if ( empty($prev_value) ) { 355 $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE meta_key = '$key' AND post_id = '$post_id'"); 356 $cache_key = $post_meta_cache[$blog_id][$post_id][$key]; 357 if ( !empty($cache_key) ) 358 foreach ($cache_key as $index => $data) 359 $post_meta_cache[$blog_id][$post_id][$key][$index] = $original_value; 360 } else { 361 $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE meta_key = '$key' AND post_id = '$post_id' AND meta_value = '$prev_value'"); 362 $cache_key = $post_meta_cache[$blog_id][$post_id][$key]; 363 if ( !empty($cache_key) ) 364 foreach ($cache_key as $index => $data) 365 if ( $data == $original_prev ) 366 $post_meta_cache[$blog_id][$post_id][$key][$index] = $original_value; 367 } 368 369 return true; 370 } 371 372 373 function get_post_custom($post_id = 0) { 374 global $id, $post_meta_cache, $wpdb, $blog_id; 375 376 if ( !$post_id ) 377 $post_id = $id; 378 379 $post_id = (int) $post_id; 380 381 if ( !isset($post_meta_cache[$blog_id][$post_id]) ) 382 update_postmeta_cache($post_id); 383 384 return $post_meta_cache[$blog_id][$post_id]; 385 } 386 387 function get_post_custom_keys( $post_id = 0 ) { 388 $custom = get_post_custom( $post_id ); 389 390 if ( !is_array($custom) ) 391 return; 392 393 if ( $keys = array_keys($custom) ) 394 return $keys; 395 } 396 397 398 function get_post_custom_values( $key = '', $post_id = 0 ) { 399 $custom = get_post_custom($post_id); 400 401 return $custom[$key]; 402 } 403 404 function wp_delete_post($postid = 0) { 405 global $wpdb, $wp_rewrite; 406 $postid = (int) $postid; 407 408 if ( !$post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = $postid") ) 409 return $post; 410 411 if ( 'attachment' == $post->post_type ) 412 return wp_delete_attachment($postid); 413 414 do_action('delete_post', $postid); 415 416 if ( 'publish' == $post->post_status && 'post' == $post->post_type ) { 417 $categories = wp_get_post_categories($post->ID); 418 if( is_array( $categories ) ) { 419 foreach ( $categories as $cat_id ) { 420 $wpdb->query("UPDATE $wpdb->categories SET category_count = category_count - 1 WHERE cat_ID = '$cat_id'"); 421 wp_cache_delete($cat_id, 'category'); 422 do_action('edit_category', $cat_id); 423 } 424 } 425 } 426 427 if ( 'page' == $post->post_type ) 428 $wpdb->query("UPDATE $wpdb->posts SET post_parent = $post->post_parent WHERE post_parent = $postid AND post_type = 'page'"); 429 430 $wpdb->query("UPDATE $wpdb->posts SET post_parent = $post->post_parent WHERE post_parent = $postid AND post_type = 'attachment'"); 431 432 $wpdb->query("DELETE FROM $wpdb->posts WHERE ID = $postid"); 433 434 $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID = $postid"); 435 436 $wpdb->query("DELETE FROM $wpdb->post2cat WHERE post_id = $postid"); 437 438 $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = $postid"); 439 440 if ( 'page' == $post->post_type ) { 441 clean_page_cache($postid); 442 $wp_rewrite->flush_rules(); 443 } 444 445 return $post; 446 } 447 448 function wp_get_post_categories($post_id = 0) { 449 $cats = &get_the_category($post_id); 450 $cat_ids = array(); 451 foreach ( $cats as $cat ) 452 $cat_ids[] = (int) $cat->cat_ID; 453 return array_unique($cat_ids); 454 } 455 456 function wp_get_recent_posts($num = 10) { 457 global $wpdb; 458 459 // Set the limit clause, if we got a limit 460 if ($num) { 461 $limit = "LIMIT $num"; 462 } 463 464 $sql = "SELECT * FROM $wpdb->posts WHERE post_type = 'post' ORDER BY post_date DESC $limit"; 465 $result = $wpdb->get_results($sql,ARRAY_A); 466 467 return $result?$result:array(); 468 } 469 470 function wp_get_single_post($postid = 0, $mode = OBJECT) { 471 global $wpdb; 472 473 $post = get_post($postid, $mode); 474 475 // Set categories 476 if($mode == OBJECT) { 477 $post->post_category = wp_get_post_categories($postid); 478 } 479 else { 480 $post['post_category'] = wp_get_post_categories($postid); 481 } 482 483 return $post; 484 } 485 486 function wp_insert_post($postarr = array()) { 487 global $wpdb, $wp_rewrite, $allowedtags, $user_ID; 488 489 if ( is_object($postarr) ) 490 $postarr = get_object_vars($postarr); 491 492 // export array as variables 493 extract($postarr); 494 495 // Are we updating or creating? 496 $update = false; 497 if ( !empty($ID) ) { 498 $update = true; 499 $post = & get_post($ID); 500 $previous_status = $post->post_status; 501 } 502 503 // Get the basics. 504 if ( empty($no_filter) ) { 505 $post_content = apply_filters('content_save_pre', $post_content); 506 $post_content_filtered = apply_filters('content_filtered_save_pre', $post_content_filtered); 507 $post_excerpt = apply_filters('excerpt_save_pre', $post_excerpt); 508 $post_title = apply_filters('title_save_pre', $post_title); 509 $post_category = apply_filters('category_save_pre', $post_category); 510 $post_status = apply_filters('status_save_pre', $post_status); 511 $post_name = apply_filters('name_save_pre', $post_name); 512 $comment_status = apply_filters('comment_status_pre', $comment_status); 513 $ping_status = apply_filters('ping_status_pre', $ping_status); 514 } 515 516 if ( ('' == $post_content) && ('' == $post_title) && ('' == $post_excerpt) ) 517 return 0; 518 519 // Make sure we set a valid category 520 if (0 == count($post_category) || !is_array($post_category)) { 521 $post_category = array(get_option('default_category')); 522 } 523 $post_cat = $post_category[0]; 524 525 if ( empty($post_author) ) 526 $post_author = $user_ID; 527 528 if ( empty($post_status) ) 529 $post_status = 'draft'; 530 531 if ( empty($post_type) ) 532 $post_type = 'post'; 533 534 // Get the post ID. 535 if ( $update ) 536 $post_ID = $ID; 537 538 // Create a valid post name. Drafts are allowed to have an empty 539 // post name. 540 if ( empty($post_name) ) { 541 if ( 'draft' != $post_status ) 542 $post_name = sanitize_title($post_title); 543 } else { 544 $post_name = sanitize_title($post_name); 545 } 546 547 548 // If the post date is empty (due to having been new or a draft) and status is not 'draft', set date to now 549 if (empty($post_date)) { 550 if ( 'draft' != $post_status ) 551 $post_date = current_time('mysql'); 552 } 553 554 if (empty($post_date_gmt)) { 555 if ( 'draft' != $post_status ) 556 $post_date_gmt = get_gmt_from_date($post_date); 557 } 558 559 if ( 'publish' == $post_status ) { 560 $now = gmdate('Y-m-d H:i:59'); 561 if ( mysql2date('U', $post_date_gmt) > mysql2date('U', $now) ) 562 $post_status = 'future'; 563 } 564 565 if ( empty($comment_status) ) { 566 if ( $update ) 567 $comment_status = 'closed'; 568 else 569 $comment_status = get_option('default_comment_status'); 570 } 571 if ( empty($ping_status) ) 572 $ping_status = get_option('default_ping_status'); 573 if ( empty($post_pingback) ) 574 $post_pingback = get_option('default_pingback_flag'); 575 576 if ( isset($to_ping) ) 577 $to_ping = preg_replace('|\s+|', "\n", $to_ping); 578 else 579 $to_ping = ''; 580 581 if ( ! isset($pinged) ) 582 $pinged = ''; 583 584 if ( isset($post_parent) ) 585 $post_parent = (int) $post_parent; 586 else 587 $post_parent = 0; 588 589 if ( isset($menu_order) ) 590 $menu_order = (int) $menu_order; 591 else 592 $menu_order = 0; 593 594 if ( !isset($post_password) ) 595 $post_password = ''; 596 597 if ( 'draft' != $post_status ) { 598 $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"); 599 600 if ($post_name_check || in_array($post_name, $wp_rewrite->feeds) ) { 601 $suffix = 2; 602 do { 603 $alt_post_name = $post_name . "-$suffix"; 604 $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"); 605 $suffix++; 606 } while ($post_name_check); 607 $post_name = $alt_post_name; 608 } 609 } 610 611 if ($update) { 612 $wpdb->query( 613 "UPDATE IGNORE $wpdb->posts SET 614 post_author = '$post_author', 615 post_date = '$post_date', 616 post_date_gmt = '$post_date_gmt', 617 post_content = '$post_content', 618 post_content_filtered = '$post_content_filtered', 619 post_title = '$post_title', 620 post_excerpt = '$post_excerpt', 621 post_status = '$post_status', 622 post_type = '$post_type', 623 comment_status = '$comment_status', 624 ping_status = '$ping_status', 625 post_password = '$post_password', 626 post_name = '$post_name', 627 to_ping = '$to_ping', 628 pinged = '$pinged', 629 post_modified = '".current_time('mysql')."', 630 post_modified_gmt = '".current_time('mysql',1)."', 631 post_parent = '$post_parent', 632 menu_order = '$menu_order' 633 WHERE ID = $post_ID"); 634 } else { 635 $wpdb->query( 636 "INSERT IGNORE INTO $wpdb->posts 637 (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) 638 VALUES 639 ('$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')"); 640 $post_ID = $wpdb->insert_id; 641 } 642 643 if ( empty($post_name) && 'draft' != $post_status ) { 644 $post_name = sanitize_title($post_title, $post_ID); 645 $wpdb->query( "UPDATE $wpdb->posts SET post_name = '$post_name' WHERE ID = '$post_ID'" ); 646 } 647 648 wp_set_post_categories($post_ID, $post_category); 649 650 if ( 'page' == $post_type ) { 651 clean_page_cache($post_ID); 652 $wp_rewrite->flush_rules(); 653 } else { 654 clean_post_cache($post_ID); 655 } 656 657 // Set GUID 658 if ( ! $update ) 659 $wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'"); 660 661 if ( $update) { 662 if ($previous_status != 'publish' && $post_status == 'publish') { 663 // Reset GUID if transitioning to publish. 664 $wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'"); 665 do_action('private_to_published', $post_ID); 666 } 667 668 do_action('edit_post', $post_ID); 669 } 670 671 if ($post_status == 'publish' && $post_type == 'post') { 672 do_action('publish_post', $post_ID); 673 if ( defined('XMLRPC_REQUEST') ) 674 do_action('xmlrpc_publish_post', $post_ID); 675 676 if ( !defined('WP_IMPORTING') ) { 677 if ( $post_pingback ) 678 $result = $wpdb->query(" 679 INSERT INTO $wpdb->postmeta 680 (post_id,meta_key,meta_value) 681 VALUES ('$post_ID','_pingme','1') 682 "); 683 $result = $wpdb->query(" 684 INSERT INTO $wpdb->postmeta 685 (post_id,meta_key,meta_value) 686 VALUES ('$post_ID','_encloseme','1') 687 "); 688 wp_schedule_single_event(time(), 'do_pings'); 689 } 690 } else if ($post_type == 'page') { 691 if ( !empty($page_template) ) 692 if ( ! update_post_meta($post_ID, '_wp_page_template', $page_template)) 693 add_post_meta($post_ID, '_wp_page_template', $page_template, true); 694 695 if ( $post_status == 'publish' ) 696 do_action('publish_page', $post_ID); 697 } 698 699 // Always clears the hook in case the post status bounced from future to draft. 700 wp_clear_scheduled_hook('publish_future_post', $post_ID); 701 702 // Schedule publication. 703 if ( 'future' == $post_status ) 704 wp_schedule_single_event(strtotime($post_date_gmt. ' GMT'), 'publish_future_post', array($post_ID)); 705 706 do_action('save_post', $post_ID); 707 do_action('wp_insert_post', $post_ID); 708 709 return $post_ID; 710 } 711 712 function wp_update_post($postarr = array()) { 713 global $wpdb; 714 715 if ( is_object($postarr) ) 716 $postarr = get_object_vars($postarr); 717 718 // First, get all of the original fields 719 $post = wp_get_single_post($postarr['ID'], ARRAY_A); 720 721 // Escape data pulled from DB. 722 $post = add_magic_quotes($post); 723 724 // Passed post category list overwrites existing category list if not empty. 725 if ( isset($postarr['post_category']) && is_array($postarr['post_category']) 726 && 0 != count($postarr['post_category']) ) 727 $post_cats = $postarr['post_category']; 728 else 729 $post_cats = $post['post_category']; 730 731 // Drafts shouldn't be assigned a date unless explicitly done so by the user 732 if ( 'draft' == $post['post_status'] && empty($postarr['edit_date']) && empty($postarr['post_date']) && 733 ('0000-00-00 00:00:00' == $post['post_date']) ) 734 $clear_date = true; 735 else 736 $clear_date = false; 737 738 // Merge old and new fields with new fields overwriting old ones. 739 $postarr = array_merge($post, $postarr); 740 $postarr['post_category'] = $post_cats; 741 if ( $clear_date ) { 742 $postarr['post_date'] = ''; 743 $postarr['post_date_gmt'] = ''; 744 } 745 746 if ($postarr['post_type'] == 'attachment') 747 return wp_insert_attachment($postarr); 748 749 return wp_insert_post($postarr); 750 } 751 752 function wp_publish_post($post_id) { 753 $post = get_post($post_id); 754 755 if ( empty($post) ) 756 return; 757 758 if ( 'publish' == $post->post_status ) 759 return; 760 761 return wp_update_post(array('post_status' => 'publish', 'ID' => $post_id, 'no_filter' => true)); 762 } 763 764 function wp_set_post_categories($post_ID = 0, $post_categories = array()) { 765 global $wpdb; 766 // If $post_categories isn't already an array, make it one: 767 if (!is_array($post_categories) || 0 == count($post_categories) || empty($post_categories)) 768 $post_categories = array(get_option('default_category')); 769 770 $post_categories = array_unique($post_categories); 771 772 // First the old categories 773 $old_categories = $wpdb->get_col(" 774 SELECT category_id 775 FROM $wpdb->post2cat 776 WHERE post_id = $post_ID"); 777 778 if (!$old_categories) { 779 $old_categories = array(); 780 } else { 781 $old_categories = array_unique($old_categories); 782 } 783 784 // Delete any? 785 $delete_cats = array_diff($old_categories,$post_categories); 786 787 if ($delete_cats) { 788 foreach ($delete_cats as $del) { 789 $wpdb->query(" 790 DELETE FROM $wpdb->post2cat 791 WHERE category_id = $del 792 AND post_id = $post_ID 793 "); 794 } 795 } 796 797 // Add any? 798 $add_cats = array_diff($post_categories, $old_categories); 799 800 if ($add_cats) { 801 foreach ($add_cats as $new_cat) { 802 if ( !empty($new_cat) ) 803 $wpdb->query(" 804 INSERT INTO $wpdb->post2cat (post_id, category_id) 805 VALUES ($post_ID, $new_cat)"); 806 } 807 } 808 809 // Update category counts. 810 $all_affected_cats = array_unique(array_merge($post_categories, $old_categories)); 811 foreach ( $all_affected_cats as $cat_id ) { 812 $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->post2cat, $wpdb->posts WHERE $wpdb->posts.ID=$wpdb->post2cat.post_id AND post_status = 'publish' AND post_type = 'post' AND category_id = '$cat_id'"); 813 $wpdb->query("UPDATE $wpdb->categories SET category_count = '$count' WHERE cat_ID = '$cat_id'"); 814 clean_category_cache($cat_id); 815 do_action('edit_category', $cat_id); 816 } 817 } // wp_set_post_categories() 818 819 // 820 // Trackback and ping functions 821 // 822 823 function add_ping($post_id, $uri) { // Add a URL to those already pung 824 global $wpdb; 825 $pung = $wpdb->get_var("SELECT pinged FROM $wpdb->posts WHERE ID = $post_id"); 826 $pung = trim($pung); 827 $pung = preg_split('/\s/', $pung); 828 $pung[] = $uri; 829 $new = implode("\n", $pung); 830 $new = apply_filters('add_ping', $new); 831 return $wpdb->query("UPDATE $wpdb->posts SET pinged = '$new' WHERE ID = $post_id"); 832 } 833 834 function get_enclosed($post_id) { // Get enclosures already enclosed for a post 835 global $wpdb; 836 $custom_fields = get_post_custom( $post_id ); 837 $pung = array(); 838 if ( !is_array( $custom_fields ) ) 839 return $pung; 840 841 foreach ( $custom_fields as $key => $val ) { 842 if ( 'enclosure' != $key || !is_array( $val ) ) 843 continue; 844 foreach( $val as $enc ) { 845 $enclosure = split( "\n", $enc ); 846 $pung[] = trim( $enclosure[ 0 ] ); 847 } 848 } 849 $pung = apply_filters('get_enclosed', $pung); 850 return $pung; 851 } 852 853 function get_pung($post_id) { // Get URLs already pung for a post 854 global $wpdb; 855 $pung = $wpdb->get_var("SELECT pinged FROM $wpdb->posts WHERE ID = $post_id"); 856 $pung = trim($pung); 857 $pung = preg_split('/\s/', $pung); 858 $pung = apply_filters('get_pung', $pung); 859 return $pung; 860 } 861 862 function get_to_ping($post_id) { // Get any URLs in the todo list 863 global $wpdb; 864 $to_ping = $wpdb->get_var("SELECT to_ping FROM $wpdb->posts WHERE ID = $post_id"); 865 $to_ping = trim($to_ping); 866 $to_ping = preg_split('/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY); 867 $to_ping = apply_filters('get_to_ping', $to_ping); 868 return $to_ping; 869 } 870 871 // do trackbacks for a list of urls 872 // accepts a comma-separated list of trackback urls and a post id 873 function trackback_url_list($tb_list, $post_id) { 874 if (!empty($tb_list)) { 875 // get post data 876 $postdata = wp_get_single_post($post_id, ARRAY_A); 877 878 // import postdata as variables 879 extract($postdata); 880 881 // form an excerpt 882 $excerpt = strip_tags($post_excerpt?$post_excerpt:$post_content); 883 884 if (strlen($excerpt) > 255) { 885 $excerpt = substr($excerpt,0,252) . '...'; 886 } 887 888 $trackback_urls = explode(',', $tb_list); 889 foreach($trackback_urls as $tb_url) { 890 $tb_url = trim($tb_url); 891 trackback($tb_url, stripslashes($post_title), $excerpt, $post_id); 892 } 893 } 894 } 895 896 // 897 // Page functions 898 // 899 900 function get_all_page_ids() { 901 global $wpdb; 902 903 if ( ! $page_ids = wp_cache_get('all_page_ids', 'pages') ) { 904 $page_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_type = 'page'"); 905 wp_cache_set('all_page_ids', $page_ids, 'pages'); 906 } 907 908 return $page_ids; 909 } 910 911 912 // Retrieves page data given a page ID or page object. 913 // Handles page caching. 914 function &get_page(&$page, $output = OBJECT) { 915 global $wpdb, $blog_id; 916 917 if ( empty($page) ) { 918 if ( isset($GLOBALS['page']) ) { 919 $_page = & $GLOBALS['page']; 920 wp_cache_add($_page->ID, $_page, 'pages'); 921 } else { 922 // shouldn't we just return NULL at this point? ~ Mark 923 $_page = null; 924 } 925 } elseif ( is_object($page) ) { 926 if ( 'post' == $page->post_type ) 927 return get_post($page, $output); 928 wp_cache_add($page->ID, $page, 'pages'); 929 $_page = $page; 930 } else { 931 // first, check the cache 932 if ( ! ( $_page = wp_cache_get($page, 'pages') ) ) { 933 // not in the page cache? 934 if ( isset($GLOBALS['page']->ID) && ($page == $GLOBALS['page']->ID) ) { // for is_page() views 935 // I don't think this code ever gets executed ~ Mark 936 $_page = & $GLOBALS['page']; 937 wp_cache_add($_page->ID, $_page, 'pages'); 938 } elseif ( isset($GLOBALS['post_cache'][$blog_id][$page]) ) { // it's actually a page, and is cached 939 return get_post($page, $output); 940 } else { // it's not in any caches, so off to the DB we go 941 // Why are we using assignment for this query? 942 $_page = & $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID= '$page' LIMIT 1"); 943 if ( 'post' == $_page->post_type ) 944 return get_post($_page, $output); 945 // Potential issue: we're not checking to see if the post_type = 'page' 946 // So all non-'post' posts will get cached as pages. 947 wp_cache_set($_page->ID, $_page, 'pages'); 948 } 949 } 950 } 951 952 // at this point, one way or another, $_post contains the page object 953 954 if ( $output == OBJECT ) { 955 return $_page; 956 } elseif ( $output == ARRAY_A ) { 957 return get_object_vars($_page); 958 } elseif ( $output == ARRAY_N ) { 959 return array_values(get_object_vars($_page)); 960 } else { 961 return $_page; 962 } 963 } 964 965 function get_page_by_path($page_path, $output = OBJECT) { 966 global $wpdb; 967 $page_path = rawurlencode(urldecode($page_path)); 968 $page_path = str_replace('%2F', '/', $page_path); 969 $page_path = str_replace('%20', ' ', $page_path); 970 $page_paths = '/' . trim($page_path, '/'); 971 $leaf_path = sanitize_title(basename($page_paths)); 972 $page_paths = explode('/', $page_paths); 973 foreach($page_paths as $pathdir) 974 $full_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir); 975 976 $pages = $wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_name = '$leaf_path' AND post_type='page'"); 977 978 if ( empty($pages) ) 979 return NULL; 980 981 foreach ($pages as $page) { 982 $path = '/' . $leaf_path; 983 $curpage = $page; 984 while ($curpage->post_parent != 0) { 985 $curpage = $wpdb->get_row("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE ID = '$curpage->post_parent' and post_type='page'"); 986 $path = '/' . $curpage->post_name . $path; 987 } 988 989 if ( $path == $full_path ) 990 return get_page($page->ID, $output); 991 } 992 993 return NULL; 994 } 995 996 function get_page_by_title($page_title, $output = OBJECT) { 997 global $wpdb; 998 $page_title = $wpdb->escape($page_title); 999 $page = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$page_title' AND post_type='page'"); 1000 if ( $page ) 1001 return get_page($page, $output); 1002 1003 return NULL; 1004 } 1005 1006 function &get_page_children($page_id, $pages) { 1007 global $page_cache, $blog_id; 1008 1009 if ( empty($pages) ) 1010 $pages = &$page_cache[$blog_id]; 1011 1012 $page_list = array(); 1013 foreach ( $pages as $page ) { 1014 if ( $page->post_parent == $page_id ) { 1015 $page_list[] = $page; 1016 if ( $children = get_page_children($page->ID, $pages) ) 1017 $page_list = array_merge($page_list, $children); 1018 } 1019 } 1020 return $page_list; 1021 } 1022 1023 //fetches the pages returned as a FLAT list, but arranged in order of their hierarchy, i.e., child parents 1024 //immediately follow their parents 1025 function get_page_hierarchy($posts, $parent = 0) { 1026 $result = array ( ); 1027 if ($posts) { foreach ($posts as $post) { 1028 if ($post->post_parent == $parent) { 1029 $result[$post->ID] = $post->post_name; 1030 $children = get_page_hierarchy($posts, $post->ID); 1031 $result += $children; //append $children to $result 1032 } 1033 } } 1034 return $result; 1035 } 1036 1037 function get_page_uri($page_id) { 1038 $page = get_page($page_id); 1039 $uri = urldecode($page->post_name); 1040 1041 // A page cannot be it's own parent. 1042 if ( $page->post_parent == $page->ID ) 1043 return $uri; 1044 1045 while ($page->post_parent != 0) { 1046 $page = get_page($page->post_parent); 1047 $uri = urldecode($page->post_name) . "/" . $uri; 1048 } 1049 1050 return $uri; 1051 } 1052 1053 function &get_pages($args = '') { 1054 global $wpdb; 1055 1056 if ( is_array($args) ) 1057 $r = &$args; 1058 else 1059 parse_str($args, $r); 1060 1061 $defaults = array('child_of' => 0, 'sort_order' => 'ASC', 'sort_column' => 'post_title', 1062 'hierarchical' => 1, 'exclude' => '', 'include' => '', 'meta_key' => '', 'meta_value' => '', 'authors' => ''); 1063 $r = array_merge($defaults, $r); 1064 extract($r); 1065 1066 $key = md5( serialize( $r ) ); 1067 if ( $cache = wp_cache_get( 'get_pages', 'page' ) ) 1068 if ( isset( $cache[ $key ] ) ) 1069 return apply_filters('get_pages', $cache[ $key ], $r ); 1070 1071 $inclusions = ''; 1072 if ( !empty($include) ) { 1073 $child_of = 0; //ignore child_of, exclude, meta_key, and meta_value params if using include 1074 $exclude = ''; 1075 $meta_key = ''; 1076 $meta_value = ''; 1077 $incpages = preg_split('/[\s,]+/',$include); 1078 if ( count($incpages) ) { 1079 foreach ( $incpages as $incpage ) { 1080 if (empty($inclusions)) 1081 $inclusions = ' AND ( ID = ' . intval($incpage) . ' '; 1082 else 1083 $inclusions .= ' OR ID = ' . intval($incpage) . ' '; 1084 } 1085 } 1086 } 1087 if (!empty($inclusions)) 1088 $inclusions .= ')'; 1089 1090 $exclusions = ''; 1091 if ( !empty($exclude) ) { 1092 $expages = preg_split('/[\s,]+/',$exclude); 1093 if ( count($expages) ) { 1094 foreach ( $expages as $expage ) { 1095 if (empty($exclusions)) 1096 $exclusions = ' AND ( ID <> ' . intval($expage) . ' '; 1097 else 1098 $exclusions .= ' AND ID <> ' . intval($expage) . ' '; 1099 } 1100 } 1101 } 1102 if (!empty($exclusions)) 1103 $exclusions .= ')'; 1104 1105 $author_query = ''; 1106 if (!empty($authors)) { 1107 $post_authors = preg_split('/[\s,]+/',$authors); 1108 1109 if ( count($post_authors) ) { 1110 foreach ( $post_authors as $post_author ) { 1111 //Do we have an author id or an author login? 1112 if ( 0 == intval($post_author) ) { 1113 $post_author = get_userdatabylogin($post_author); 1114 if ( empty($post_author) ) 1115 continue; 1116 if ( empty($post_author->ID) ) 1117 continue; 1118 $post_author = $post_author->ID; 1119 } 1120 1121 if ( '' == $author_query ) 1122 $author_query = ' post_author = ' . intval($post_author) . ' '; 1123 else 1124 $author_query .= ' OR post_author = ' . intval($post_author) . ' '; 1125 } 1126 if ( '' != $author_query ) 1127 $author_query = " AND ($author_query)"; 1128 } 1129 } 1130 1131 $query = "SELECT * FROM $wpdb->posts " ; 1132 $query .= ( empty( $meta_key ) ? "" : ", $wpdb->postmeta " ) ; 1133 $query .= " WHERE (post_type = 'page' AND post_status = 'publish') $exclusions $inclusions " ; 1134 $query .= ( empty( $meta_key ) | empty($meta_value) ? "" : " AND ($wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = '$meta_key' AND $wpdb->postmeta.meta_value = '$meta_value' )" ) ; 1135 $query .= $author_query; 1136 $query .= " ORDER BY " . $sort_column . " " . $sort_order ; 1137 1138 $pages = $wpdb->get_results($query); 1139 $pages = apply_filters('get_pages', $pages, $r); 1140 1141 if ( empty($pages) ) 1142 return array(); 1143 1144 // Update cache. 1145 update_page_cache($pages); 1146 1147 if ( $child_of || $hierarchical ) 1148 $pages = & get_page_children($child_of, $pages); 1149 1150 $cache[ $key ] = $pages; 1151 wp_cache_set( 'get_pages', $cache, 'page' ); 1152 1153 return $pages; 1154 } 1155 1156 function generate_page_uri_index() { 1157 global $wpdb; 1158 1159 //get pages in order of hierarchy, i.e. children after parents 1160 $posts = get_page_hierarchy($wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'page'")); 1161 //now reverse it, because we need parents after children for rewrite rules to work properly 1162 $posts = array_reverse($posts, true); 1163 1164 $page_uris = array(); 1165 $page_attachment_uris = array(); 1166 1167 if ($posts) { 1168 1169 foreach ($posts as $id => $post) { 1170 1171 // URL => page name 1172 $uri = get_page_uri($id); 1173 $attachments = $wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = '$id'"); 1174 if ( $attachments ) { 1175 foreach ( $attachments as $attachment ) { 1176 $attach_uri = get_page_uri($attachment->ID); 1177 $page_attachment_uris[$attach_uri] = $attachment->ID; 1178 } 1179 } 1180 1181 $page_uris[$uri] = $id; 1182 } 1183 1184 delete_option('page_uris'); 1185 update_option('page_uris', $page_uris); 1186 1187 if ( $page_attachment_uris ) { 1188 delete_option('page_attachment_uris'); 1189 update_option('page_attachment_uris', $page_attachment_uris); 1190 } 1191 } 1192 } 1193 1194 // 1195 // Attachment functions 1196 // 1197 1198 function is_local_attachment($url) { 1199 if ( !strstr($url, get_bloginfo('home') ) ) 1200 return false; 1201 if ( strstr($url, get_bloginfo('home') . '/?attachment_id=') ) 1202 return true; 1203 if ( $id = url_to_postid($url) ) { 1204 $post = & get_post($id); 1205 if ( 'attachment' == $post->post_type ) 1206 return true; 1207 } 1208 return false; 1209 } 1210 1211 function wp_insert_attachment($object, $file = false, $post_parent = 0) { 1212 global $wpdb, $user_ID; 1213 1214 if ( is_object($object) ) 1215 $object = get_object_vars($object); 1216 1217 // Export array as variables 1218 extract($object); 1219 1220 // Get the basics. 1221 $post_content = apply_filters('content_save_pre', $post_content); 1222 $post_content_filtered = apply_filters('content_filtered_save_pre', $post_content_filtered); 1223 $post_excerpt = apply_filters('excerpt_save_pre', $post_excerpt); 1224 $post_title = apply_filters('title_save_pre', $post_title); 1225 $post_category = apply_filters('category_save_pre', $post_category); 1226 $post_name = apply_filters('name_save_pre', $post_name); 1227 $comment_status = apply_filters('comment_status_pre', $comment_status); 1228 $ping_status = apply_filters('ping_status_pre', $ping_status); 1229 $post_mime_type = apply_filters('post_mime_type_pre', $post_mime_type); 1230 1231 // Make sure we set a valid category 1232 if (0 == count($post_category) || !is_array($post_category)) { 1233 $post_category = array(get_option('default_category')); 1234 } 1235 $post_cat = $post_category[0]; 1236 1237 if ( empty($post_author) ) 1238 $post_author = $user_ID; 1239 1240 $post_type = 'attachment'; 1241 $post_status = 'inherit'; 1242 1243 // Are we updating or creating? 1244 $update = false; 1245 if ( !empty($ID) ) { 1246 $update = true; 1247 $post_ID = $ID; 1248 } 1249 1250 // Create a valid post name. 1251 if ( empty($post_name) ) 1252 $post_name = sanitize_title($post_title); 1253 else 1254 $post_name = sanitize_title($post_name); 1255 1256 $post_name_check = 1257 $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$post_name' AND post_status = 'inherit' AND ID != '$post_ID' LIMIT 1"); 1258 1259 if ($post_name_check) { 1260 $suffix = 2; 1261 while ($post_name_check) { 1262 $alt_post_name = $post_name . "-$suffix"; 1263 $post_name_check = $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$alt_post_name' AND post_status = 'inherit' AND ID != '$post_ID' AND post_parent = '$post_parent' LIMIT 1"); 1264 $suffix++; 1265 } 1266 $post_name = $alt_post_name; 1267 } 1268 1269 if (empty($post_date)) 1270 $post_date = current_time('mysql'); 1271 if (empty($post_date_gmt)) 1272 $post_date_gmt = current_time('mysql', 1); 1273 1274 if ( empty($comment_status) ) { 1275 if ( $update ) 1276 $comment_status = 'closed'; 1277 else 1278 $comment_status = get_option('default_comment_status'); 1279 } 1280 if ( empty($ping_status) ) 1281 $ping_status = get_option('default_ping_status'); 1282 if ( empty($post_pingback) ) 1283 $post_pingback = get_option('default_pingback_flag'); 1284 1285 if ( isset($to_ping) ) 1286 $to_ping = preg_replace('|\s+|', "\n", $to_ping); 1287 else 1288 $to_ping = ''; 1289 1290 if ( isset($post_parent) ) 1291 $post_parent = (int) $post_parent; 1292 else 1293 $post_parent = 0; 1294 1295 if ( isset($menu_order) ) 1296 $menu_order = (int) $menu_order; 1297 else 1298 $menu_order = 0; 1299 1300 if ( !isset($post_password) ) 1301 $post_password = ''; 1302 1303 if ( isset($to_ping) ) 1304 $to_ping = preg_replace('|\s+|', "\n", $to_ping); 1305 else 1306 $to_ping = ''; 1307 1308 if ( ! isset($pinged) ) 1309 $pinged = ''; 1310 1311 if ($update) { 1312 $wpdb->query( 1313 "UPDATE $wpdb->posts SET 1314 post_author = '$post_author', 1315 post_date = '$post_date', 1316 post_date_gmt = '$post_date_gmt', 1317 post_content = '$post_content', 1318 post_content_filtered = '$post_content_filtered', 1319 post_title = '$post_title', 1320 post_excerpt = '$post_excerpt', 1321 post_status = '$post_status', 1322 post_type = '$post_type', 1323 comment_status = '$comment_status', 1324 ping_status = '$ping_status', 1325 post_password = '$post_password', 1326 post_name = '$post_name', 1327 to_ping = '$to_ping', 1328 pinged = '$pinged', 1329 post_modified = '".current_time('mysql')."', 1330 post_modified_gmt = '".current_time('mysql',1)."', 1331 post_parent = '$post_parent', 1332 menu_order = '$menu_order', 1333 post_mime_type = '$post_mime_type', 1334 guid = '$guid' 1335 WHERE ID = $post_ID"); 1336 } else { 1337 $wpdb->query( 1338 "INSERT INTO $wpdb->posts 1339 (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) 1340 VALUES 1341 ('$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')"); 1342 $post_ID = $wpdb->insert_id; 1343 } 1344 1345 if ( empty($post_name) ) { 1346 $post_name = sanitize_title($post_title, $post_ID); 1347 $wpdb->query( "UPDATE $wpdb->posts SET post_name = '$post_name' WHERE ID = '$post_ID'" ); 1348 } 1349 1350 wp_set_post_categories($post_ID, $post_category); 1351 1352 if ( $file ) 1353 update_attached_file( $post_ID, $file ); 1354 1355 clean_post_cache($post_ID); 1356 1357 if ( $update) { 1358 do_action('edit_attachment', $post_ID); 1359 } else { 1360 do_action('add_attachment', $post_ID); 1361 } 1362 1363 return $post_ID; 1364 } 1365 1366 function wp_delete_attachment($postid) { 1367 global $wpdb; 1368 $postid = (int) $postid; 1369 1370 if ( !$post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = '$postid'") ) 1371 return $post; 1372 1373 if ( 'attachment' != $post->post_type ) 1374 return false; 1375 1376 $meta = wp_get_attachment_metadata( $postid ); 1377 $file = get_attached_file( $postid ); 1378 1379 $wpdb->query("DELETE FROM $wpdb->posts WHERE ID = '$postid'"); 1380 1381 $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID = '$postid'"); 1382 1383 $wpdb->query("DELETE FROM $wpdb->post2cat WHERE post_id = '$postid'"); 1384 1385 $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$postid'"); 1386 1387 if ( ! empty($meta['thumb']) ) { 1388 // Don't delete the thumb if another attachment uses it 1389 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")) { 1390 $thumbfile = str_replace(basename($file), $meta['thumb'], $file); 1391 $thumbfile = apply_filters('wp_delete_file', $thumbfile); 1392 @ unlink($thumbfile); 1393 } 1394 } 1395 1396 $file = apply_filters('wp_delete_file', $file); 1397 1398 if ( ! empty($file) ) 1399 @ unlink($file); 1400 1401 do_action('delete_attachment', $postid); 1402 1403 return $post; 1404 } 1405 1406 function wp_get_attachment_metadata( $post_id, $unfiltered = false ) { 1407 $post_id = (int) $post_id; 1408 if ( !$post =& get_post( $post_id ) ) 1409 return false; 1410 1411 $data = get_post_meta( $post->ID, '_wp_attachment_metadata', true ); 1412 if ( $unfiltered ) 1413 return $data; 1414 return apply_filters( 'wp_get_attachment_metadata', $data, $post->ID ); 1415 } 1416 1417 function wp_update_attachment_metadata( $post_id, $data ) { 1418 $post_id = (int) $post_id; 1419 if ( !$post =& get_post( $post_id ) ) 1420 return false; 1421 1422 $old_data = wp_get_attachment_metadata( $post->ID, true ); 1423 1424 $data = apply_filters( 'wp_update_attachment_metadata', $data, $post->ID ); 1425 1426 if ( $old_data ) 1427 return update_post_meta( $post->ID, '_wp_attachment_metadata', $data, $old_data ); 1428 else 1429 return add_post_meta( $post->ID, '_wp_attachment_metadata', $data ); 1430 } 1431 1432 function wp_get_attachment_url( $post_id = 0 ) { 1433 $post_id = (int) $post_id; 1434 if ( !$post =& get_post( $post_id ) ) 1435 return false; 1436 1437 $url = get_the_guid( $post->ID ); 1438 1439 if ( 'attachment' != $post->post_type || !$url ) 1440 return false; 1441 1442 return apply_filters( 'wp_get_attachment_url', $url, $post->ID ); 1443 } 1444 1445 function wp_get_attachment_thumb_file( $post_id = 0 ) { 1446 $post_id = (int) $post_id; 1447 if ( !$post =& get_post( $post_id ) ) 1448 return false; 1449 if ( !$imagedata = wp_get_attachment_metadata( $post->ID ) ) 1450 return false; 1451 1452 $file = get_attached_file( $post->ID ); 1453 1454 if ( !empty($imagedata['thumb']) && ($thumbfile = str_replace(basename($file), $imagedata['thumb'], $file)) && file_exists($thumbfile) ) 1455 return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID ); 1456 return false; 1457 } 1458 1459 function wp_get_attachment_thumb_url( $post_id = 0 ) { 1460 $post_id = (int) $post_id; 1461 if ( !$post =& get_post( $post_id ) ) 1462 return false; 1463 if ( !$url = wp_get_attachment_url( $post->ID ) ) 1464 return false; 1465 1466 if ( !$thumb = wp_get_attachment_thumb_file( $post->ID ) ) 1467 return false; 1468 1469 $url = str_replace(basename($url), basename($thumb), $url); 1470 1471 return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID ); 1472 } 1473 1474 function wp_attachment_is_image( $post_id = 0 ) { 1475 $post_id = (int) $post_id; 1476 if ( !$post =& get_post( $post_id ) ) 1477 return false; 1478 1479 if ( !$file = get_attached_file( $post->ID ) ) 1480 return false; 1481 1482 $ext = preg_match('/\.([^.]+)$/', $file, $matches) ? strtolower($matches[1]) : false; 1483 1484 $image_exts = array('jpg', 'jpeg', 'gif', 'png'); 1485 1486 if ( 'image/' == substr($post->post_mime_type, 0, 6) || $ext && 'import' == $post->post_mime_type && in_array($ext, $image_exts) ) 1487 return true; 1488 return false; 1489 } 1490 1491 function wp_mime_type_icon( $mime = 0 ) { 1492 $post_id = 0; 1493 if ( is_numeric($mime) ) { 1494 $mime = (int) $mime; 1495 if ( !$post =& get_post( $mime ) ) 1496 return false; 1497 $post_id = $post->ID; 1498 $mime = $post->post_mime_type; 1499 } 1500 1501 if ( empty($mime) ) 1502 return false; 1503 1504 $icon_dir = apply_filters( 'icon_dir', get_template_directory() . '/images' ); 1505 $icon_dir_uri = apply_filters( 'icon_dir_uri', get_template_directory_uri() . '/images' ); 1506 1507 $types = array( 1508 substr($mime, 0, strpos($mime, '/')), 1509 substr($mime, strpos($mime, '/') + 1), 1510 str_replace('/', '_', $mime) 1511 ); 1512 1513 $exts = array('jpg', 'gif', 'png'); 1514 1515 $src = false; 1516 1517 foreach ( $types as $type ) { 1518 foreach ( $exts as $ext ) { 1519 $src_file = "$icon_dir/$type.$ext"; 1520 if ( file_exists($src_file) ) { 1521 $src = "$icon_dir_uri/$type.$ext"; 1522 break 2; 1523 } 1524 } 1525 } 1526 1527 return apply_filters( 'wp_mime_type_icon', $src, $mime, $post_id ); // Last arg is 0 if function pass mime type. 1528 } 1529 1530 function wp_check_for_changed_slugs($post_id) { 1531 if ( !strlen($_POST['wp-old-slug']) ) 1532 return $post_id; 1533 1534 $post = &get_post($post_id); 1535 1536 // we're only concerned with published posts 1537 if ( $post->post_status != 'publish' || $post->post_type != 'post' ) 1538 return $post_id; 1539 1540 // only bother if the slug has changed 1541 if ( $post->post_name == $_POST['wp-old-slug'] ) 1542 return $post_id; 1543 1544 $old_slugs = (array) get_post_meta($post_id, '_wp_old_slug'); 1545 1546 // if we haven't added this old slug before, add it now 1547 if ( !count($old_slugs) || !in_array($_POST['wp-old-slug'], $old_slugs) ) 1548 add_post_meta($post_id, '_wp_old_slug', $_POST['wp-old-slug']); 1549 1550 // if the new slug was used previously, delete it from the list 1551 if ( in_array($post->post_name, $old_slugs) ) 1552 delete_post_meta($post_id, '_wp_old_slug', $post->post_name); 1553 1554 return $post_id; 1555 } 1556 1557 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 30 19:41:27 2007 | par Balluche grâce à PHPXref 0.7 |