]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/post.php
Wordpress 2.3.2
[autoinstalls/wordpress.git] / wp-includes / post.php
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                         $args = 'post_parent=' . (int) $GLOBALS['post']->post_parent;
34                 } else {
35                         return false;
36                 }
37         } elseif ( is_object( $args ) ) {
38                 $args = 'post_parent=' . (int) $args->post_parent;
39         } elseif ( is_numeric( $args ) ) {
40                 $args = 'post_parent=' . (int) $args;
41         }
42
43         $defaults = array(
44                 'numberposts' => -1, 'post_type' => '',
45                 'post_status' => '', 'post_parent' => 0
46         );
47
48         $r = wp_parse_args( $args, $defaults );
49
50         $children = get_posts( $r );
51
52         if ( $children ) {
53                 foreach ( $children as $key => $child ) {
54                         $post_cache[$blog_id][$child->ID] =& $children[$key];
55                         $kids[$child->ID] =& $children[$key];
56                 }
57         } else {
58                 return false;
59         }
60
61         if ( $output == OBJECT ) {
62                 return $kids;
63         } elseif ( $output == ARRAY_A ) {
64                 foreach ( $kids as $kid )
65                         $weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]);
66                 return $weeuns;
67         } elseif ( $output == ARRAY_N ) {
68                 foreach ( $kids as $kid )
69                         $babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID]));
70                 return $babes;
71         } else {
72                 return $kids;
73         }
74 }
75
76 // get extended entry info (<!--more-->)
77 function get_extended($post) {
78         //Match the new style more links
79         if ( preg_match('/<!--more(.*?)?-->/', $post, $matches) ) {
80                 list($main, $extended) = explode($matches[0], $post, 2);
81         } else {
82                 $main = $post;
83                 $extended = '';
84         }
85
86         // Strip leading and trailing whitespace
87         $main = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $main);
88         $extended = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $extended);
89
90         return array('main' => $main, 'extended' => $extended);
91 }
92
93 // Retrieves post data given a post ID or post object.
94 // Handles post caching.
95 function &get_post(&$post, $output = OBJECT, $filter = 'raw') {
96         global $post_cache, $wpdb, $blog_id;
97
98         if ( empty($post) ) {
99                 if ( isset($GLOBALS['post']) )
100                         $_post = & $GLOBALS['post'];
101                 else
102                         $_post = null;
103         } elseif ( is_object($post) ) {
104                 if ( 'page' == $post->post_type )
105                         return get_page($post, $output, $filter);
106                 if ( !isset($post_cache[$blog_id][$post->ID]) )
107                         $post_cache[$blog_id][$post->ID] = &$post;
108                 $_post = & $post_cache[$blog_id][$post->ID];
109         } else {
110                 $post = (int) $post;
111                 if ( isset($post_cache[$blog_id][$post]) )
112                         $_post = & $post_cache[$blog_id][$post];
113                 elseif ( $_post = wp_cache_get($post, 'pages') )
114                         return get_page($_post, $output, $filter);
115                 else {
116                         $query = "SELECT * FROM $wpdb->posts WHERE ID = '$post' LIMIT 1";
117                         $_post = & $wpdb->get_row($query);
118                         if ( 'page' == $_post->post_type )
119                                 return get_page($_post, $output, $filter);
120                         $post_cache[$blog_id][$post] = & $_post;
121                 }
122         }
123
124         if ( defined('WP_IMPORTING') )
125                 unset($post_cache[$blog_id]);
126
127         $_post = sanitize_post($_post, $filter);
128
129         if ( $output == OBJECT ) {
130                 return $_post;
131         } elseif ( $output == ARRAY_A ) {
132                 return get_object_vars($_post);
133         } elseif ( $output == ARRAY_N ) {
134                 return array_values(get_object_vars($_post));
135         } else {
136                 return $_post;
137         }
138 }
139
140 function get_post_field( $field, $post, $context = 'display' ) {
141         $post = (int) $post;
142         $post = get_post( $post );
143
144         if ( is_wp_error($post) )
145                 return $post;
146
147         if ( !is_object($post) )
148                 return '';
149
150         if ( !isset($post->$field) )
151                 return '';
152
153         return sanitize_post_field($field, $post->$field, $post->ID, $context);
154 }
155
156 // Takes a post ID, returns its mime type.
157 function get_post_mime_type($ID = '') {
158         $post = & get_post($ID);
159
160         if ( is_object($post) )
161                 return $post->post_mime_type;
162
163         return false;
164 }
165
166 function get_post_status($ID = '') {
167         $post = get_post($ID);
168
169         if ( is_object($post) ) {
170                 if ( ('attachment' == $post->post_type) && $post->post_parent && ($post->ID != $post->post_parent) )
171                         return get_post_status($post->post_parent);
172                 else
173                         return $post->post_status;
174         }
175
176         return false;
177 }
178
179 function get_post_type($post = false) {
180         global $wpdb, $posts;
181
182         if ( false === $post )
183                 $post = $posts[0];
184         elseif ( (int) $post )
185                 $post = get_post($post, OBJECT);
186
187         if ( is_object($post) )
188                 return $post->post_type;
189
190         return false;
191 }
192
193 function get_posts($args) {
194         global $wpdb;
195
196         $defaults = array(
197                 'numberposts' => 5, 'offset' => 0,
198                 'category' => 0, 'orderby' => 'post_date',
199                 'order' => 'DESC', 'include' => '',
200                 'exclude' => '', 'meta_key' => '',
201                 'meta_value' =>'', 'post_type' => 'post',
202                 'post_status' => 'publish', 'post_parent' => 0
203         );
204
205         $r = wp_parse_args( $args, $defaults );
206         extract( $r, EXTR_SKIP );
207
208         $numberposts = (int) $numberposts;
209         $offset = (int) $offset;
210         $category = (int) $category;
211         $post_parent = (int) $post_parent;
212
213         $inclusions = '';
214         if ( !empty($include) ) {
215                 $offset = 0;    //ignore offset, category, exclude, meta_key, and meta_value, post_parent if using include
216                 $category = 0;
217                 $exclude = '';
218                 $meta_key = '';
219                 $meta_value = '';
220                 $post_parent = 0;
221                 $incposts = preg_split('/[\s,]+/',$include);
222                 $numberposts = count($incposts);  // only the number of posts included
223                 if ( count($incposts) ) {
224                         foreach ( $incposts as $incpost ) {
225                                 if (empty($inclusions))
226                                         $inclusions = ' AND ( ID = ' . intval($incpost) . ' ';
227                                 else
228                                         $inclusions .= ' OR ID = ' . intval($incpost) . ' ';
229                         }
230                 }
231         }
232         if (!empty($inclusions))
233                 $inclusions .= ')';
234
235         $exclusions = '';
236         if ( !empty($exclude) ) {
237                 $exposts = preg_split('/[\s,]+/',$exclude);
238                 if ( count($exposts) ) {
239                         foreach ( $exposts as $expost ) {
240                                 if (empty($exclusions))
241                                         $exclusions = ' AND ( ID <> ' . intval($expost) . ' ';
242                                 else
243                                         $exclusions .= ' AND ID <> ' . intval($expost) . ' ';
244                         }
245                 }
246         }
247         if (!empty($exclusions))
248                 $exclusions .= ')';
249
250         $query  = "SELECT DISTINCT * FROM $wpdb->posts ";
251         $query .= empty( $category ) ? '' : ", $wpdb->term_relationships, $wpdb->term_taxonomy  ";
252         $query .= empty( $meta_key ) ? '' : ", $wpdb->postmeta ";
253         $query .= " WHERE 1=1 ";
254         $query .= empty( $post_type ) ? '' : "AND post_type = '$post_type' ";
255         $query .= empty( $post_status ) ? '' : "AND post_status = '$post_status' ";
256         $query .= "$exclusions $inclusions " ;
257         $query .= empty( $category ) ? '' : "AND ($wpdb->posts.ID = $wpdb->term_relationships.object_id AND $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id AND $wpdb->term_taxonomy.term_id = " . $category. ") ";
258         $query .= empty( $post_parent ) ? '' : "AND $wpdb->posts.post_parent = '$post_parent' ";
259         $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' )";
260         $query .= " GROUP BY $wpdb->posts.ID ORDER BY " . $orderby . ' ' . $order;
261         if ( 0 < $numberposts )
262                 $query .= " LIMIT " . $offset . ',' . $numberposts;
263
264         $posts = $wpdb->get_results($query);
265
266         update_post_caches($posts);
267
268         return $posts;
269 }
270
271 //
272 // Post meta functions
273 //
274
275 function add_post_meta($post_id, $key, $value, $unique = false) {
276         global $wpdb, $post_meta_cache, $blog_id;
277
278         $post_id = (int) $post_id;
279
280         if ( $unique ) {
281                 if ( $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key = '$key' AND post_id = '$post_id'") ) {
282                         return false;
283                 }
284         }
285
286         $post_meta_cache[$blog_id][$post_id][$key][] = $value;
287
288         $value = maybe_serialize($value);
289         $value = $wpdb->escape($value);
290
291         $wpdb->query("INSERT INTO $wpdb->postmeta (post_id,meta_key,meta_value) VALUES ('$post_id','$key','$value')");
292
293         return true;
294 }
295
296 function delete_post_meta($post_id, $key, $value = '') {
297         global $wpdb, $post_meta_cache, $blog_id;
298
299         $post_id = (int) $post_id;
300
301         if ( empty($value) ) {
302                 $meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key'");
303         } else {
304                 $meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key' AND meta_value = '$value'");
305         }
306
307         if ( !$meta_id )
308                 return false;
309
310         if ( empty($value) ) {
311                 $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key'");
312                 unset($post_meta_cache[$blog_id][$post_id][$key]);
313         } else {
314                 $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key' AND meta_value = '$value'");
315                 $cache_key = $post_meta_cache[$blog_id][$post_id][$key];
316                 if ($cache_key) foreach ( $cache_key as $index => $data )
317                         if ( $data == $value )
318                                 unset($post_meta_cache[$blog_id][$post_id][$key][$index]);
319         }
320
321         unset($post_meta_cache[$blog_id][$post_id][$key]);
322
323         return true;
324 }
325
326 function get_post_meta($post_id, $key, $single = false) {
327         global $wpdb, $post_meta_cache, $blog_id;
328
329         $post_id = (int) $post_id;
330
331         if ( isset($post_meta_cache[$blog_id][$post_id][$key]) ) {
332                 if ( $single ) {
333                         return maybe_unserialize( $post_meta_cache[$blog_id][$post_id][$key][0] );
334                 } else {
335                         return maybe_unserialize( $post_meta_cache[$blog_id][$post_id][$key] );
336                 }
337         }
338
339         if ( !isset($post_meta_cache[$blog_id][$post_id]) )
340                 update_postmeta_cache($post_id);
341
342         if ( $single ) {
343                 if ( isset($post_meta_cache[$blog_id][$post_id][$key][0]) )
344                         return maybe_unserialize($post_meta_cache[$blog_id][$post_id][$key][0]);
345                 else
346                         return '';
347         }       else {
348                 return maybe_unserialize($post_meta_cache[$blog_id][$post_id][$key]);
349         }
350 }
351
352 function update_post_meta($post_id, $key, $value, $prev_value = '') {
353         global $wpdb, $post_meta_cache, $blog_id;
354
355         $post_id = (int) $post_id;
356
357         $original_value = $value;
358         $value = maybe_serialize($value);
359         $value = $wpdb->escape($value);
360
361         $original_prev = $prev_value;
362         $prev_value = maybe_serialize($prev_value);
363         $prev_value = $wpdb->escape($prev_value);
364
365         if (! $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key = '$key' AND post_id = '$post_id'") ) {
366                 return false;
367         }
368
369         if ( empty($prev_value) ) {
370                 $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE meta_key = '$key' AND post_id = '$post_id'");
371                 $cache_key = $post_meta_cache[$blog_id][$post_id][$key];
372                 if ( !empty($cache_key) )
373                         foreach ($cache_key as $index => $data)
374                                 $post_meta_cache[$blog_id][$post_id][$key][$index] = $original_value;
375         } else {
376                 $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE meta_key = '$key' AND post_id = '$post_id' AND meta_value = '$prev_value'");
377                 $cache_key = $post_meta_cache[$blog_id][$post_id][$key];
378                 if ( !empty($cache_key) )
379                         foreach ($cache_key as $index => $data)
380                                 if ( $data == $original_prev )
381                                         $post_meta_cache[$blog_id][$post_id][$key][$index] = $original_value;
382         }
383
384         return true;
385 }
386
387
388 function delete_post_meta_by_key($post_meta_key) {
389         global $wpdb, $post_meta_cache, $blog_id;
390         $post_meta_key = $wpdb->escape($post_meta_key);
391         if ( $wpdb->query("DELETE FROM $wpdb->postmeta WHERE meta_key = '$post_meta_key'") ) {
392                 unset($post_meta_cache[$blog_id]); // not worth doing the work to iterate through the cache
393                 return true;
394         }
395         return false;
396 }
397
398
399 function get_post_custom($post_id = 0) {
400         global $id, $post_meta_cache, $wpdb, $blog_id;
401
402         if ( !$post_id )
403                 $post_id = (int) $id;
404
405         $post_id = (int) $post_id;
406
407         if ( !isset($post_meta_cache[$blog_id][$post_id]) )
408                 update_postmeta_cache($post_id);
409
410         return $post_meta_cache[$blog_id][$post_id];
411 }
412
413 function get_post_custom_keys( $post_id = 0 ) {
414         $custom = get_post_custom( $post_id );
415
416         if ( !is_array($custom) )
417                 return;
418
419         if ( $keys = array_keys($custom) )
420                 return $keys;
421 }
422
423
424 function get_post_custom_values( $key = '', $post_id = 0 ) {
425         $custom = get_post_custom($post_id);
426
427         return $custom[$key];
428 }
429
430 function sanitize_post($post, $context = 'display') {
431
432         if ( 'raw' == $context )
433                 return $post;
434
435         // TODO: Use array keys instead of hard coded list
436         $fields = array('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', 'post_category');
437
438         $do_object = false;
439         if ( is_object($post) )
440                 $do_object = true;
441
442         foreach ( $fields as $field ) {
443                 if ( $do_object )
444                         $post->$field = sanitize_post_field($field, $post->$field, $post->ID, $context);
445                 else
446                         $post[$field] = sanitize_post_field($field, $post[$field], $post['ID'], $context);
447         }
448
449         return $post;
450 }
451
452 function sanitize_post_field($field, $value, $post_id, $context) {
453         $int_fields = array('ID', 'post_parent', 'menu_order');
454         if ( in_array($field, $int_fields) )
455                 $value = (int) $value;
456
457         if ( 'raw' == $context )
458                 return $value;
459
460         $prefixed = false;
461         if ( false !== strpos($field, 'post_') ) {
462                 $prefixed = true;
463                 $field_no_prefix = str_replace('post_', '', $field);
464         }
465
466         if ( 'edit' == $context ) {
467                 $format_to_edit = array('post_content', 'post_excerpt', 'post_title', 'post_password');
468
469                 if ( $prefixed ) {
470                         $value = apply_filters("edit_$field", $value, $post_id);
471                         // Old school
472                         $value = apply_filters("${field_no_prefix}_edit_pre", $value, $post_id);
473                 } else {
474                         $value = apply_filters("edit_post_$field", $value, $post_id);
475                 }
476
477                 if ( in_array($field, $format_to_edit) ) {
478                         if ( 'post_content' == $field )
479                                 $value = format_to_edit($value, user_can_richedit());
480                         else
481                                 $value = format_to_edit($value);
482                 } else {
483                         $value = attribute_escape($value);
484                 }
485         } else if ( 'db' == $context ) {
486                 if ( $prefixed ) {
487                         $value = apply_filters("pre_$field", $value);
488                         $value = apply_filters("${field_no_prefix}_save_pre", $value);
489                 } else {
490                         $value = apply_filters("pre_post_$field", $value);
491                         $value = apply_filters("${field}_pre", $value);
492                 }
493         } else {
494                 // Use display filters by default.
495                 if ( $prefixed )
496                         $value = apply_filters($field, $value, $post_id, $context);
497                 else
498                         $value = apply_filters("post_$field", $value, $post_id, $context);
499         }
500
501         if ( 'attribute' == $context )
502                 $value = attribute_escape($value);
503         else if ( 'js' == $context )
504                 $value = js_escape($value);
505
506         return $value;
507 }
508
509 function wp_delete_post($postid = 0) {
510         global $wpdb, $wp_rewrite;
511         $postid = (int) $postid;
512
513         if ( !$post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = $postid") )
514                 return $post;
515
516         if ( 'attachment' == $post->post_type )
517                 return wp_delete_attachment($postid);
518
519         do_action('delete_post', $postid);
520
521         // TODO delete for pluggable post taxonomies too
522         wp_delete_object_term_relationships($postid, array('category', 'post_tag'));
523
524         if ( 'page' == $post->post_type )
525                 $wpdb->query("UPDATE $wpdb->posts SET post_parent = $post->post_parent WHERE post_parent = $postid AND post_type = 'page'");
526
527         $wpdb->query("UPDATE $wpdb->posts SET post_parent = $post->post_parent WHERE post_parent = $postid AND post_type = 'attachment'");
528
529         $wpdb->query("DELETE FROM $wpdb->posts WHERE ID = $postid");
530
531         $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID = $postid");
532
533         $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = $postid");
534
535         if ( 'page' == $post->post_type ) {
536                 clean_page_cache($postid);
537                 $wp_rewrite->flush_rules();
538         }
539
540         do_action('deleted_post', $postid);
541
542         return $post;
543 }
544
545 function wp_get_post_categories( $post_id = 0, $args = array() ) {
546         $post_id = (int) $post_id;
547
548         $defaults = array('fields' => 'ids');
549         $args = wp_parse_args( $args, $defaults );
550
551         $cats = wp_get_object_terms($post_id, 'category', $args);
552         return $cats;
553 }
554
555 function wp_get_post_tags( $post_id = 0, $args = array() ) {
556         $post_id = (int) $post_id;
557
558         $defaults = array('fields' => 'all');
559         $args = wp_parse_args( $args, $defaults );
560
561         $tags = wp_get_object_terms($post_id, 'post_tag', $args);
562
563         return $tags;
564 }
565
566 function wp_get_recent_posts($num = 10) {
567         global $wpdb;
568
569         // Set the limit clause, if we got a limit
570         $num = (int) $num;
571         if ($num) {
572                 $limit = "LIMIT $num";
573         }
574
575         $sql = "SELECT * FROM $wpdb->posts WHERE post_type = 'post' ORDER BY post_date DESC $limit";
576         $result = $wpdb->get_results($sql,ARRAY_A);
577
578         return $result?$result:array();
579 }
580
581 function wp_get_single_post($postid = 0, $mode = OBJECT) {
582         global $wpdb;
583
584         $postid = (int) $postid;
585
586         $post = get_post($postid, $mode);
587
588         // Set categories and tags
589         if($mode == OBJECT) {
590                 $post->post_category = wp_get_post_categories($postid);
591                 $post->tags_input = wp_get_post_tags($postid, array('fields' => 'names'));
592         }
593         else {
594                 $post['post_category'] = wp_get_post_categories($postid);
595                 $post['tags_input'] = wp_get_post_tags($postid, array('fields' => 'names'));
596         }
597
598         return $post;
599 }
600
601 function wp_insert_post($postarr = array()) {
602         global $wpdb, $wp_rewrite, $allowedtags, $user_ID;
603
604         $defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_ID,
605                 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,
606                 'menu_order' => 0, 'to_ping' =>  '', 'pinged' => '', 'post_password' => '');
607
608         $postarr = wp_parse_args($postarr, $defaults);
609         $postarr = sanitize_post($postarr, 'db');
610
611         // export array as variables
612         extract($postarr, EXTR_SKIP);
613
614         // Are we updating or creating?
615         $update = false;
616         if ( !empty($ID) ) {
617                 $update = true;
618                 $previous_status = get_post_field('post_status', $ID);
619         } else {
620                 $previous_status = 'new';
621         }
622
623         if ( ('' == $post_content) && ('' == $post_title) && ('' == $post_excerpt) )
624                 return 0;
625
626         // Make sure we set a valid category
627         if (0 == count($post_category) || !is_array($post_category)) {
628                 $post_category = array(get_option('default_category'));
629         }
630
631         if ( empty($post_author) )
632                 $post_author = $user_ID;
633
634         if ( empty($post_status) )
635                 $post_status = 'draft';
636
637         if ( empty($post_type) )
638                 $post_type = 'post';
639
640         // Get the post ID.
641         if ( $update )
642                 $post_ID = (int) $ID;
643
644         // Create a valid post name.  Drafts are allowed to have an empty
645         // post name.
646         if ( empty($post_name) ) {
647                 if ( 'draft' != $post_status )
648                         $post_name = sanitize_title($post_title);
649         } else {
650                 $post_name = sanitize_title($post_name);
651         }
652
653         // If the post date is empty (due to having been new or a draft) and status is not 'draft', set date to now
654         if (empty($post_date)) {
655                 if ( !in_array($post_status, array('draft', 'pending')) )
656                         $post_date = current_time('mysql');
657         }
658
659         if (empty($post_date_gmt)) {
660                 if ( !in_array($post_status, array('draft', 'pending')) )
661                         $post_date_gmt = get_gmt_from_date($post_date);
662         }
663
664         if ( 'publish' == $post_status ) {
665                 $now = gmdate('Y-m-d H:i:59');
666                 if ( mysql2date('U', $post_date_gmt) > mysql2date('U', $now) )
667                         $post_status = 'future';
668         }
669
670         if ( empty($comment_status) ) {
671                 if ( $update )
672                         $comment_status = 'closed';
673                 else
674                         $comment_status = get_option('default_comment_status');
675         }
676         if ( empty($ping_status) )
677                 $ping_status = get_option('default_ping_status');
678
679         if ( isset($to_ping) )
680                 $to_ping = preg_replace('|\s+|', "\n", $to_ping);
681         else
682                 $to_ping = '';
683
684         if ( ! isset($pinged) )
685                 $pinged = '';
686
687         if ( isset($post_parent) )
688                 $post_parent = (int) $post_parent;
689         else
690                 $post_parent = 0;
691
692         if ( isset($menu_order) )
693                 $menu_order = (int) $menu_order;
694         else
695                 $menu_order = 0;
696
697         if ( !isset($post_password) )
698                 $post_password = '';
699
700         if ( 'draft' != $post_status ) {
701                 $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");
702
703                 if ($post_name_check || in_array($post_name, $wp_rewrite->feeds) ) {
704                         $suffix = 2;
705                         do {
706                                 $alt_post_name = substr($post_name, 0, 200-(strlen($suffix)+1)). "-$suffix";
707                                 $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");
708                                 $suffix++;
709                         } while ($post_name_check);
710                         $post_name = $alt_post_name;
711                 }
712         }
713
714         if ($update) {
715                 $wpdb->query(
716                         "UPDATE IGNORE $wpdb->posts SET
717                         post_author = '$post_author',
718                         post_date = '$post_date',
719                         post_date_gmt = '$post_date_gmt',
720                         post_content = '$post_content',
721                         post_content_filtered = '$post_content_filtered',
722                         post_title = '$post_title',
723                         post_excerpt = '$post_excerpt',
724                         post_status = '$post_status',
725                         post_type = '$post_type',
726                         comment_status = '$comment_status',
727                         ping_status = '$ping_status',
728                         post_password = '$post_password',
729                         post_name = '$post_name',
730                         to_ping = '$to_ping',
731                         pinged = '$pinged',
732                         post_modified = '".current_time('mysql')."',
733                         post_modified_gmt = '".current_time('mysql',1)."',
734                         post_parent = '$post_parent',
735                         menu_order = '$menu_order'
736                         WHERE ID = $post_ID");
737         } else {
738                 $wpdb->query(
739                         "INSERT IGNORE INTO $wpdb->posts
740                         (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)
741                         VALUES
742                         ('$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')");
743                         $post_ID = (int) $wpdb->insert_id;
744         }
745
746         if ( empty($post_name) && 'draft' != $post_status ) {
747                 $post_name = sanitize_title($post_title, $post_ID);
748                 $wpdb->query( "UPDATE $wpdb->posts SET post_name = '$post_name' WHERE ID = '$post_ID'" );
749         }
750
751         wp_set_post_categories( $post_ID, $post_category );
752         wp_set_post_tags( $post_ID, $tags_input );
753
754         if ( 'page' == $post_type ) {
755                 clean_page_cache($post_ID);
756         } else {
757                 clean_post_cache($post_ID);
758         }
759
760         // Set GUID
761         if ( ! $update )
762                 $wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'");
763
764         $post = get_post($post_ID);
765         if ( !empty($page_template) )
766                 $post->page_template = $page_template;
767
768         wp_transition_post_status($post_status, $previous_status, $post);
769
770         if ( $update)
771                 do_action('edit_post', $post_ID, $post);
772
773         do_action('save_post', $post_ID, $post);
774         do_action('wp_insert_post', $post_ID, $post);
775
776         return $post_ID;
777 }
778
779 function wp_update_post($postarr = array()) {
780         global $wpdb;
781
782         if ( is_object($postarr) )
783                 $postarr = get_object_vars($postarr);
784
785         // First, get all of the original fields
786         $post = wp_get_single_post($postarr['ID'], ARRAY_A);
787
788         // Escape data pulled from DB.
789         $post = add_magic_quotes($post);
790
791         // Passed post category list overwrites existing category list if not empty.
792         if ( isset($postarr['post_category']) && is_array($postarr['post_category'])
793                          && 0 != count($postarr['post_category']) )
794                 $post_cats = $postarr['post_category'];
795         else
796                 $post_cats = $post['post_category'];
797
798         // Drafts shouldn't be assigned a date unless explicitly done so by the user
799         if ( in_array($post['post_status'], array('draft', 'pending')) && empty($postarr['edit_date']) && empty($postarr['post_date']) &&
800                          ('0000-00-00 00:00:00' == $post['post_date']) )
801                 $clear_date = true;
802         else
803                 $clear_date = false;
804
805         // Merge old and new fields with new fields overwriting old ones.
806         $postarr = array_merge($post, $postarr);
807         $postarr['post_category'] = $post_cats;
808         if ( $clear_date ) {
809                 $postarr['post_date'] = '';
810                 $postarr['post_date_gmt'] = '';
811         }
812
813         if ($postarr['post_type'] == 'attachment')
814                 return wp_insert_attachment($postarr);
815
816         return wp_insert_post($postarr);
817 }
818
819 function wp_publish_post($post_id) {
820         global $wpdb;
821
822         $post = get_post($post_id);
823
824         if ( empty($post) )
825                 return;
826
827         if ( 'publish' == $post->post_status )
828                 return;
829
830         $wpdb->query( "UPDATE $wpdb->posts SET post_status = 'publish' WHERE ID = '$post_id'" );
831
832         $old_status = $post->post_status;
833         $post->post_status = 'publish';
834         wp_transition_post_status('publish', $old_status, $post);
835
836         do_action('edit_post', $post_id, $post);
837         do_action('save_post', $post_id, $post);
838         do_action('wp_insert_post', $post_id, $post);
839 }
840
841 function wp_add_post_tags($post_id = 0, $tags = '') {
842         return wp_set_post_tags($post_id, $tags, true);
843 }
844
845 function wp_set_post_tags( $post_id = 0, $tags = '', $append = false ) {
846         /* $append - true = don't delete existing tags, just add on, false = replace the tags with the new tags */
847         global $wpdb;
848
849         $post_id = (int) $post_id;
850
851         if ( !$post_id )
852                 return false;
853
854         if ( empty($tags) )
855                 $tags = array();
856         $tags = (is_array($tags)) ? $tags : explode( ',', $tags );
857         wp_set_object_terms($post_id, $tags, 'post_tag', $append);
858 }
859
860 function wp_set_post_categories($post_ID = 0, $post_categories = array()) {
861         global $wpdb;
862
863         $post_ID = (int) $post_ID;
864         // If $post_categories isn't already an array, make it one:
865         if (!is_array($post_categories) || 0 == count($post_categories) || empty($post_categories))
866                 $post_categories = array(get_option('default_category'));
867         else if ( 1 == count($post_categories) && '' == $post_categories[0] )
868                 return true;
869
870         $post_categories = array_map('intval', $post_categories);
871         $post_categories = array_unique($post_categories);
872
873         return wp_set_object_terms($post_ID, $post_categories, 'category');
874 }       // wp_set_post_categories()
875
876 function wp_transition_post_status($new_status, $old_status, $post) {
877         if ( $new_status != $old_status ) {
878                 do_action('transition_post_status', $new_status, $old_status, $post);
879                 do_action("${old_status}_to_$new_status", $post);
880         }
881         do_action("${new_status}_$post->post_type", $post->ID, $post);
882 }
883
884 //
885 // Trackback and ping functions
886 //
887
888 function add_ping($post_id, $uri) { // Add a URL to those already pung
889         global $wpdb;
890         $pung = $wpdb->get_var("SELECT pinged FROM $wpdb->posts WHERE ID = $post_id");
891         $pung = trim($pung);
892         $pung = preg_split('/\s/', $pung);
893         $pung[] = $uri;
894         $new = implode("\n", $pung);
895         $new = apply_filters('add_ping', $new);
896         return $wpdb->query("UPDATE $wpdb->posts SET pinged = '$new' WHERE ID = $post_id");
897 }
898
899 function get_enclosed($post_id) { // Get enclosures already enclosed for a post
900         global $wpdb;
901         $custom_fields = get_post_custom( $post_id );
902         $pung = array();
903         if ( !is_array( $custom_fields ) )
904                 return $pung;
905
906         foreach ( $custom_fields as $key => $val ) {
907                 if ( 'enclosure' != $key || !is_array( $val ) )
908                         continue;
909                 foreach( $val as $enc ) {
910                         $enclosure = split( "\n", $enc );
911                         $pung[] = trim( $enclosure[ 0 ] );
912                 }
913         }
914         $pung = apply_filters('get_enclosed', $pung);
915         return $pung;
916 }
917
918 function get_pung($post_id) { // Get URLs already pung for a post
919         global $wpdb;
920         $pung = $wpdb->get_var("SELECT pinged FROM $wpdb->posts WHERE ID = $post_id");
921         $pung = trim($pung);
922         $pung = preg_split('/\s/', $pung);
923         $pung = apply_filters('get_pung', $pung);
924         return $pung;
925 }
926
927 function get_to_ping($post_id) { // Get any URLs in the todo list
928         global $wpdb;
929         $to_ping = $wpdb->get_var("SELECT to_ping FROM $wpdb->posts WHERE ID = $post_id");
930         $to_ping = trim($to_ping);
931         $to_ping = preg_split('/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY);
932         $to_ping = apply_filters('get_to_ping',  $to_ping);
933         return $to_ping;
934 }
935
936 // do trackbacks for a list of urls
937 // accepts a comma-separated list of trackback urls and a post id
938 function trackback_url_list($tb_list, $post_id) {
939         if (!empty($tb_list)) {
940                 // get post data
941                 $postdata = wp_get_single_post($post_id, ARRAY_A);
942
943                 // import postdata as variables
944                 extract($postdata, EXTR_SKIP);
945
946                 // form an excerpt
947                 $excerpt = strip_tags($post_excerpt?$post_excerpt:$post_content);
948
949                 if (strlen($excerpt) > 255) {
950                         $excerpt = substr($excerpt,0,252) . '...';
951                 }
952
953                 $trackback_urls = explode(',', $tb_list);
954                 foreach($trackback_urls as $tb_url) {
955                                 $tb_url = trim($tb_url);
956                                 trackback($tb_url, stripslashes($post_title), $excerpt, $post_id);
957                 }
958                 }
959 }
960
961 //
962 // Page functions
963 //
964
965 function get_all_page_ids() {
966         global $wpdb;
967
968         if ( ! $page_ids = wp_cache_get('all_page_ids', 'pages') ) {
969                 $page_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_type = 'page'");
970                 wp_cache_add('all_page_ids', $page_ids, 'pages');
971         }
972
973         return $page_ids;
974 }
975
976
977 // Retrieves page data given a page ID or page object.
978 // Handles page caching.
979 function &get_page(&$page, $output = OBJECT, $filter = 'raw') {
980         global $wpdb, $blog_id;
981
982         if ( empty($page) ) {
983                 if ( isset( $GLOBALS['page'] ) && isset( $GLOBALS['page']->ID ) ) {
984                         $_page = & $GLOBALS['page'];
985                         wp_cache_add($_page->ID, $_page, 'pages');
986                 } else {
987                         // shouldn't we just return NULL at this point? ~ Mark
988                         $_page = null;
989                 }
990         } elseif ( is_object($page) ) {
991                 if ( 'post' == $page->post_type )
992                         return get_post($page, $output, $filter);
993                 wp_cache_add($page->ID, $page, 'pages');
994                 $_page = $page;
995         } else {
996                 $page = (int) $page;
997                 // first, check the cache
998                 if ( ! ( $_page = wp_cache_get($page, 'pages') ) ) {
999                         // not in the page cache?
1000                         if ( isset($GLOBALS['page']->ID) && ($page == $GLOBALS['page']->ID) ) { // for is_page() views
1001                                 // I don't think this code ever gets executed ~ Mark
1002                                 $_page = & $GLOBALS['page'];
1003                                 wp_cache_add($_page->ID, $_page, 'pages');
1004                         } elseif ( isset($GLOBALS['post_cache'][$blog_id][$page]) ) { // it's actually a page, and is cached
1005                                 return get_post($page, $output, $filter);
1006                         } else { // it's not in any caches, so off to the DB we go
1007                                 // Why are we using assignment for this query?
1008                                 $_page = & $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID= '$page' LIMIT 1");
1009                                 if ( 'post' == $_page->post_type )
1010                                         return get_post($_page, $output, $filter);
1011                                 // Potential issue: we're not checking to see if the post_type = 'page'
1012                                 // So all non-'post' posts will get cached as pages.
1013                                 wp_cache_add($_page->ID, $_page, 'pages');
1014                         }
1015                 }
1016         }
1017
1018         $_page = sanitize_post($_page, $filter);
1019
1020         // at this point, one way or another, $_post contains the page object
1021
1022         if ( $output == OBJECT ) {
1023                 return $_page;
1024         } elseif ( $output == ARRAY_A ) {
1025                 return get_object_vars($_page);
1026         } elseif ( $output == ARRAY_N ) {
1027                 return array_values(get_object_vars($_page));
1028         } else {
1029                 return $_page;
1030         }
1031 }
1032
1033 function get_page_by_path($page_path, $output = OBJECT) {
1034         global $wpdb;
1035         $page_path = rawurlencode(urldecode($page_path));
1036         $page_path = str_replace('%2F', '/', $page_path);
1037         $page_path = str_replace('%20', ' ', $page_path);
1038         $page_paths = '/' . trim($page_path, '/');
1039         $leaf_path  = sanitize_title(basename($page_paths));
1040         $page_paths = explode('/', $page_paths);
1041         foreach($page_paths as $pathdir)
1042                 $full_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir);
1043
1044         $pages = $wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_name = '$leaf_path' AND post_type='page'");
1045
1046         if ( empty($pages) )
1047                 return NULL;
1048
1049         foreach ($pages as $page) {
1050                 $path = '/' . $leaf_path;
1051                 $curpage = $page;
1052                 while ($curpage->post_parent != 0) {
1053                         $curpage = $wpdb->get_row("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE ID = '$curpage->post_parent' and post_type='page'");
1054                         $path = '/' . $curpage->post_name . $path;
1055                 }
1056
1057                 if ( $path == $full_path )
1058                         return get_page($page->ID, $output);
1059         }
1060
1061         return NULL;
1062 }
1063
1064 function get_page_by_title($page_title, $output = OBJECT) {
1065         global $wpdb;
1066         $page_title = $wpdb->escape($page_title);
1067         $page = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$page_title' AND post_type='page'");
1068         if ( $page )
1069                 return get_page($page, $output);
1070
1071         return NULL;
1072 }
1073
1074 function &get_page_children($page_id, $pages) {
1075         global $page_cache, $blog_id;
1076
1077         if ( empty($pages) )
1078                 $pages = &$page_cache[$blog_id];
1079
1080         $page_list = array();
1081         foreach ( $pages as $page ) {
1082                 if ( $page->post_parent == $page_id ) {
1083                         $page_list[] = $page;
1084                         if ( $children = get_page_children($page->ID, $pages) )
1085                                 $page_list = array_merge($page_list, $children);
1086                 }
1087         }
1088         return $page_list;
1089 }
1090
1091 //fetches the pages returned as a FLAT list, but arranged in order of their hierarchy, i.e., child parents
1092 //immediately follow their parents
1093 function get_page_hierarchy($posts, $parent = 0) {
1094         $result = array ( );
1095         if ($posts) { foreach ($posts as $post) {
1096                 if ($post->post_parent == $parent) {
1097                         $result[$post->ID] = $post->post_name;
1098                         $children = get_page_hierarchy($posts, $post->ID);
1099                         $result += $children; //append $children to $result
1100                 }
1101         } }
1102         return $result;
1103 }
1104
1105 function get_page_uri($page_id) {
1106         $page = get_page($page_id);
1107         $uri = urldecode($page->post_name);
1108
1109         // A page cannot be it's own parent.
1110         if ( $page->post_parent == $page->ID )
1111                 return $uri;
1112
1113         while ($page->post_parent != 0) {
1114                 $page = get_page($page->post_parent);
1115                 $uri = urldecode($page->post_name) . "/" . $uri;
1116         }
1117
1118         return $uri;
1119 }
1120
1121 function &get_pages($args = '') {
1122         global $wpdb;
1123
1124         $defaults = array(
1125                 'child_of' => 0, 'sort_order' => 'ASC',
1126                 'sort_column' => 'post_title', 'hierarchical' => 1,
1127                 'exclude' => '', 'include' => '',
1128                 'meta_key' => '', 'meta_value' => '',
1129                 'authors' => ''
1130         );
1131
1132         $r = wp_parse_args( $args, $defaults );
1133         extract( $r, EXTR_SKIP );
1134
1135         $key = md5( serialize( $r ) );
1136         if ( $cache = wp_cache_get( 'get_pages', 'page' ) )
1137                 if ( isset( $cache[ $key ] ) )
1138                         return apply_filters('get_pages', $cache[ $key ], $r );
1139
1140         $inclusions = '';
1141         if ( !empty($include) ) {
1142                 $child_of = 0; //ignore child_of, exclude, meta_key, and meta_value params if using include
1143                 $exclude = '';
1144                 $meta_key = '';
1145                 $meta_value = '';
1146                 $hierarchical = false;
1147                 $incpages = preg_split('/[\s,]+/',$include);
1148                 if ( count($incpages) ) {
1149                         foreach ( $incpages as $incpage ) {
1150                                 if (empty($inclusions))
1151                                         $inclusions = ' AND ( ID = ' . intval($incpage) . ' ';
1152                                 else
1153                                         $inclusions .= ' OR ID = ' . intval($incpage) . ' ';
1154                         }
1155                 }
1156         }
1157         if (!empty($inclusions))
1158                 $inclusions .= ')';
1159
1160         $exclusions = '';
1161         if ( !empty($exclude) ) {
1162                 $expages = preg_split('/[\s,]+/',$exclude);
1163                 if ( count($expages) ) {
1164                         foreach ( $expages as $expage ) {
1165                                 if (empty($exclusions))
1166                                         $exclusions = ' AND ( ID <> ' . intval($expage) . ' ';
1167                                 else
1168                                         $exclusions .= ' AND ID <> ' . intval($expage) . ' ';
1169                         }
1170                 }
1171         }
1172         if (!empty($exclusions))
1173                 $exclusions .= ')';
1174
1175         $author_query = '';
1176         if (!empty($authors)) {
1177                 $post_authors = preg_split('/[\s,]+/',$authors);
1178
1179                 if ( count($post_authors) ) {
1180                         foreach ( $post_authors as $post_author ) {
1181                                 //Do we have an author id or an author login?
1182                                 if ( 0 == intval($post_author) ) {
1183                                         $post_author = get_userdatabylogin($post_author);
1184                                         if ( empty($post_author) )
1185                                                 continue;
1186                                         if ( empty($post_author->ID) )
1187                                                 continue;
1188                                         $post_author = $post_author->ID;
1189                                 }
1190
1191                                 if ( '' == $author_query )
1192                                         $author_query = ' post_author = ' . intval($post_author) . ' ';
1193                                 else
1194                                         $author_query .= ' OR post_author = ' . intval($post_author) . ' ';
1195                         }
1196                         if ( '' != $author_query )
1197                                 $author_query = " AND ($author_query)";
1198                 }
1199         }
1200
1201         $query = "SELECT * FROM $wpdb->posts " ;
1202         $query .= ( empty( $meta_key ) ? "" : ", $wpdb->postmeta " ) ;
1203         $query .= " WHERE (post_type = 'page' AND post_status = 'publish') $exclusions $inclusions " ;
1204         $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' )" ) ;
1205         $query .= $author_query;
1206         $query .= " ORDER BY " . $sort_column . " " . $sort_order ;
1207
1208         $pages = $wpdb->get_results($query);
1209
1210         if ( empty($pages) )
1211                 return apply_filters('get_pages', array(), $r);
1212
1213         // Update cache.
1214         update_page_cache($pages);
1215
1216         if ( $child_of || $hierarchical )
1217                 $pages = & get_page_children($child_of, $pages);
1218
1219         $cache[ $key ] = $pages;
1220         wp_cache_set( 'get_pages', $cache, 'page' );
1221
1222         $pages = apply_filters('get_pages', $pages, $r);
1223
1224         return $pages;
1225 }
1226
1227 function generate_page_uri_index() {
1228         global $wpdb;
1229
1230         //get pages in order of hierarchy, i.e. children after parents
1231         $posts = get_page_hierarchy($wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'page'"));
1232         //now reverse it, because we need parents after children for rewrite rules to work properly
1233         $posts = array_reverse($posts, true);
1234
1235         $page_uris = array();
1236         $page_attachment_uris = array();
1237
1238         if ($posts) {
1239
1240                 foreach ($posts as $id => $post) {
1241
1242                         // URL => page name
1243                         $uri = get_page_uri($id);
1244                         $attachments = $wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = '$id'");
1245                         if ( $attachments ) {
1246                                 foreach ( $attachments as $attachment ) {
1247                                         $attach_uri = get_page_uri($attachment->ID);
1248                                         $page_attachment_uris[$attach_uri] = $attachment->ID;
1249                                 }
1250                         }
1251
1252                         $page_uris[$uri] = $id;
1253                 }
1254
1255                 delete_option('page_uris');
1256                 update_option('page_uris', $page_uris);
1257
1258                 if ( $page_attachment_uris ) {
1259                         delete_option('page_attachment_uris');
1260                         update_option('page_attachment_uris', $page_attachment_uris);
1261                 }
1262         }
1263 }
1264
1265 //
1266 // Attachment functions
1267 //
1268
1269 function is_local_attachment($url) {
1270         if (strpos($url, get_bloginfo('url')) === false)
1271                 return false;
1272         if (strpos($url, get_bloginfo('url') . '/?attachment_id=') !== false)
1273                 return true;
1274         if ( $id = url_to_postid($url) ) {
1275                 $post = & get_post($id);
1276                 if ( 'attachment' == $post->post_type )
1277                         return true;
1278         }
1279         return false;
1280 }
1281
1282 function wp_insert_attachment($object, $file = false, $parent = 0) {
1283         global $wpdb, $user_ID;
1284
1285         $defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_ID,
1286                 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,
1287                 'menu_order' => 0, 'to_ping' =>  '', 'pinged' => '', 'post_password' => '');
1288
1289         $object = wp_parse_args($object, $defaults);
1290         if ( !empty($parent) )
1291                 $object['post_parent'] = $parent;
1292
1293         $object = sanitize_post($object, 'db');
1294
1295         // export array as variables
1296         extract($object, EXTR_SKIP);
1297
1298         // Make sure we set a valid category
1299         if (0 == count($post_category) || !is_array($post_category)) {
1300                 $post_category = array(get_option('default_category'));
1301         }
1302
1303         if ( empty($post_author) )
1304                 $post_author = $user_ID;
1305
1306         $post_type = 'attachment';
1307         $post_status = 'inherit';
1308
1309         // Are we updating or creating?
1310         $update = false;
1311         if ( !empty($ID) ) {
1312                 $update = true;
1313                 $post_ID = (int) $ID;
1314         }
1315
1316         // Create a valid post name.
1317         if ( empty($post_name) )
1318                 $post_name = sanitize_title($post_title);
1319         else
1320                 $post_name = sanitize_title($post_name);
1321
1322         $post_name_check =
1323                 $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$post_name' AND post_status = 'inherit' AND ID != '$post_ID' LIMIT 1");
1324
1325         if ($post_name_check) {
1326                 $suffix = 2;
1327                 while ($post_name_check) {
1328                         $alt_post_name = $post_name . "-$suffix";
1329                         $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");
1330                         $suffix++;
1331                 }
1332                 $post_name = $alt_post_name;
1333         }
1334
1335         if ( empty($post_date) )
1336                 $post_date = current_time('mysql');
1337         if ( empty($post_date_gmt) )
1338                 $post_date_gmt = current_time('mysql', 1);
1339
1340         if ( empty($comment_status) ) {
1341                 if ( $update )
1342                         $comment_status = 'closed';
1343                 else
1344                         $comment_status = get_option('default_comment_status');
1345         }
1346         if ( empty($ping_status) )
1347                 $ping_status = get_option('default_ping_status');
1348
1349         if ( isset($to_ping) )
1350                 $to_ping = preg_replace('|\s+|', "\n", $to_ping);
1351         else
1352                 $to_ping = '';
1353
1354         if ( isset($post_parent) )
1355                 $post_parent = (int) $post_parent;
1356         else
1357                 $post_parent = 0;
1358
1359         if ( isset($menu_order) )
1360                 $menu_order = (int) $menu_order;
1361         else
1362                 $menu_order = 0;
1363
1364         if ( !isset($post_password) )
1365                 $post_password = '';
1366
1367         if ( ! isset($pinged) )
1368                 $pinged = '';
1369
1370         if ($update) {
1371                 $wpdb->query(
1372                         "UPDATE $wpdb->posts SET
1373                         post_author = '$post_author',
1374                         post_date = '$post_date',
1375                         post_date_gmt = '$post_date_gmt',
1376                         post_content = '$post_content',
1377                         post_content_filtered = '$post_content_filtered',
1378                         post_title = '$post_title',
1379                         post_excerpt = '$post_excerpt',
1380                         post_status = '$post_status',
1381                         post_type = '$post_type',
1382                         comment_status = '$comment_status',
1383                         ping_status = '$ping_status',
1384                         post_password = '$post_password',
1385                         post_name = '$post_name',
1386                         to_ping = '$to_ping',
1387                         pinged = '$pinged',
1388                         post_modified = '".current_time('mysql')."',
1389                         post_modified_gmt = '".current_time('mysql',1)."',
1390                         post_parent = '$post_parent',
1391                         menu_order = '$menu_order',
1392                         post_mime_type = '$post_mime_type',
1393                         guid = '$guid'
1394                         WHERE ID = $post_ID");
1395         } else {
1396                 $wpdb->query(
1397                         "INSERT INTO $wpdb->posts
1398                         (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)
1399                         VALUES
1400                         ('$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')");
1401                         $post_ID = (int) $wpdb->insert_id;
1402         }
1403
1404         if ( empty($post_name) ) {
1405                 $post_name = sanitize_title($post_title, $post_ID);
1406                 $wpdb->query( "UPDATE $wpdb->posts SET post_name = '$post_name' WHERE ID = '$post_ID'" );
1407         }
1408
1409         wp_set_post_categories($post_ID, $post_category);
1410
1411         if ( $file )
1412                 update_attached_file( $post_ID, $file );
1413
1414         clean_post_cache($post_ID);
1415
1416         if ( $update) {
1417                 do_action('edit_attachment', $post_ID);
1418         } else {
1419                 do_action('add_attachment', $post_ID);
1420         }
1421
1422         return $post_ID;
1423 }
1424
1425 function wp_delete_attachment($postid) {
1426         global $wpdb;
1427         $postid = (int) $postid;
1428
1429         if ( !$post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = '$postid'") )
1430                 return $post;
1431
1432         if ( 'attachment' != $post->post_type )
1433                 return false;
1434
1435         $meta = wp_get_attachment_metadata( $postid );
1436         $file = get_attached_file( $postid );
1437
1438         // TODO delete for pluggable post taxonomies too
1439         wp_delete_object_term_relationships($postid, array('category', 'post_tag'));
1440
1441         $wpdb->query("DELETE FROM $wpdb->posts WHERE ID = '$postid'");
1442
1443         $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID = '$postid'");
1444
1445         $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$postid'");
1446
1447         if ( ! empty($meta['thumb']) ) {
1448                 // Don't delete the thumb if another attachment uses it
1449                 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")) {
1450                         $thumbfile = str_replace(basename($file), $meta['thumb'], $file);
1451                         $thumbfile = apply_filters('wp_delete_file', $thumbfile);
1452                         @ unlink($thumbfile);
1453                 }
1454         }
1455
1456         $file = apply_filters('wp_delete_file', $file);
1457
1458         if ( ! empty($file) )
1459                 @ unlink($file);
1460
1461         do_action('delete_attachment', $postid);
1462
1463         return $post;
1464 }
1465
1466 function wp_get_attachment_metadata( $post_id, $unfiltered = false ) {
1467         $post_id = (int) $post_id;
1468         if ( !$post =& get_post( $post_id ) )
1469                 return false;
1470
1471         $data = get_post_meta( $post->ID, '_wp_attachment_metadata', true );
1472         if ( $unfiltered )
1473                 return $data;
1474         return apply_filters( 'wp_get_attachment_metadata', $data, $post->ID );
1475 }
1476
1477 function wp_update_attachment_metadata( $post_id, $data ) {
1478         $post_id = (int) $post_id;
1479         if ( !$post =& get_post( $post_id ) )
1480                 return false;
1481
1482         $old_data = wp_get_attachment_metadata( $post->ID, true );
1483
1484         $data = apply_filters( 'wp_update_attachment_metadata', $data, $post->ID );
1485
1486         if ( $old_data )
1487                 return update_post_meta( $post->ID, '_wp_attachment_metadata', $data, $old_data );
1488         else
1489                 return add_post_meta( $post->ID, '_wp_attachment_metadata', $data );
1490 }
1491
1492 function wp_get_attachment_url( $post_id = 0 ) {
1493         $post_id = (int) $post_id;
1494         if ( !$post =& get_post( $post_id ) )
1495                 return false;
1496
1497         $url = get_the_guid( $post->ID );
1498
1499         if ( 'attachment' != $post->post_type || !$url )
1500                 return false;
1501
1502         return apply_filters( 'wp_get_attachment_url', $url, $post->ID );
1503 }
1504
1505 function wp_get_attachment_thumb_file( $post_id = 0 ) {
1506         $post_id = (int) $post_id;
1507         if ( !$post =& get_post( $post_id ) )
1508                 return false;
1509         if ( !$imagedata = wp_get_attachment_metadata( $post->ID ) )
1510                 return false;
1511
1512         $file = get_attached_file( $post->ID );
1513
1514         if ( !empty($imagedata['thumb']) && ($thumbfile = str_replace(basename($file), $imagedata['thumb'], $file)) && file_exists($thumbfile) )
1515                 return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID );
1516         return false;
1517 }
1518
1519 function wp_get_attachment_thumb_url( $post_id = 0 ) {
1520         $post_id = (int) $post_id;
1521         if ( !$post =& get_post( $post_id ) )
1522                 return false;
1523         if ( !$url = wp_get_attachment_url( $post->ID ) )
1524                 return false;
1525
1526         if ( !$thumb = wp_get_attachment_thumb_file( $post->ID ) )
1527                 return false;
1528
1529         $url = str_replace(basename($url), basename($thumb), $url);
1530
1531         return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID );
1532 }
1533
1534 function wp_attachment_is_image( $post_id = 0 ) {
1535         $post_id = (int) $post_id;
1536         if ( !$post =& get_post( $post_id ) )
1537                 return false;
1538
1539         if ( !$file = get_attached_file( $post->ID ) )
1540                 return false;
1541
1542         $ext = preg_match('/\.([^.]+)$/', $file, $matches) ? strtolower($matches[1]) : false;
1543
1544         $image_exts = array('jpg', 'jpeg', 'gif', 'png');
1545
1546         if ( 'image/' == substr($post->post_mime_type, 0, 6) || $ext && 'import' == $post->post_mime_type && in_array($ext, $image_exts) )
1547                 return true;
1548         return false;
1549 }
1550
1551 function wp_mime_type_icon( $mime = 0 ) {
1552         $post_id = 0;
1553         if ( is_numeric($mime) ) {
1554                 $mime = (int) $mime;
1555                 if ( !$post =& get_post( $mime ) )
1556                         return false;
1557                 $post_id = (int) $post->ID;
1558                 $mime = $post->post_mime_type;
1559         }
1560
1561         if ( empty($mime) )
1562                 return false;
1563
1564         $icon_dir = apply_filters( 'icon_dir', get_template_directory() . '/images' );
1565         $icon_dir_uri = apply_filters( 'icon_dir_uri', get_template_directory_uri() . '/images' );
1566
1567         $types = array(
1568                 substr($mime, 0, strpos($mime, '/')),
1569                 substr($mime, strpos($mime, '/') + 1),
1570                 str_replace('/', '_', $mime)
1571         );
1572
1573         $exts = array('jpg', 'gif', 'png');
1574
1575         $src = false;
1576
1577         foreach ( $types as $type ) {
1578                 foreach ( $exts as $ext ) {
1579                         $src_file = "$icon_dir/$type.$ext";
1580                         if ( file_exists($src_file) ) {
1581                                 $src = "$icon_dir_uri/$type.$ext";
1582                                 break 2;
1583                         }
1584                 }
1585         }
1586
1587         return apply_filters( 'wp_mime_type_icon', $src, $mime, $post_id ); // Last arg is 0 if function pass mime type.
1588 }
1589
1590 function wp_check_for_changed_slugs($post_id) {
1591         if ( !strlen($_POST['wp-old-slug']) )
1592                 return $post_id;
1593
1594         $post = &get_post($post_id);
1595
1596         // we're only concerned with published posts
1597         if ( $post->post_status != 'publish' || $post->post_type != 'post' )
1598                 return $post_id;
1599
1600         // only bother if the slug has changed
1601         if ( $post->post_name == $_POST['wp-old-slug'] )
1602                 return $post_id;
1603
1604         $old_slugs = (array) get_post_meta($post_id, '_wp_old_slug');
1605
1606         // if we haven't added this old slug before, add it now
1607         if ( !count($old_slugs) || !in_array($_POST['wp-old-slug'], $old_slugs) )
1608                 add_post_meta($post_id, '_wp_old_slug', $_POST['wp-old-slug']);
1609
1610         // if the new slug was used previously, delete it from the list
1611         if ( in_array($post->post_name, $old_slugs) )
1612                 delete_post_meta($post_id, '_wp_old_slug', $post->post_name);
1613
1614         return $post_id;
1615 }
1616
1617 /**
1618  * This function provides a standardized way to appropriately select on
1619  * the post_status of posts/pages. The function will return a piece of
1620  * SQL code that can be added to a WHERE clause; this SQL is constructed
1621  * to allow all published posts, and all private posts to which the user
1622  * has access.
1623  *
1624  * @param string $post_type currently only supports 'post' or 'page'.
1625  * @return string SQL code that can be added to a where clause.
1626  */
1627 function get_private_posts_cap_sql($post_type) {
1628         global $user_ID;
1629         $cap = '';
1630
1631         // Private posts
1632         if ($post_type == 'post') {
1633                 $cap = 'read_private_posts';
1634         // Private pages
1635         } elseif ($post_type == 'page') {
1636                 $cap = 'read_private_pages';
1637         // Dunno what it is, maybe plugins have their own post type?
1638         } else {
1639                 $cap = apply_filters('pub_priv_sql_capability', $cap);
1640
1641                 if (empty($cap)) {
1642                         // We don't know what it is, filters don't change anything,
1643                         // so set the SQL up to return nothing.
1644                         return '1 = 0';
1645                 }
1646         }
1647
1648         $sql = '(post_status = \'publish\'';
1649
1650         if (current_user_can($cap)) {
1651                 // Does the user have the capability to view private posts? Guess so.
1652                 $sql .= ' OR post_status = \'private\'';
1653         } elseif (is_user_logged_in()) {
1654                 // Users can view their own private posts.
1655                 $sql .= ' OR post_status = \'private\' AND post_author = \'' . $user_ID . '\'';
1656         }
1657
1658         $sql .= ')';
1659
1660         return $sql;
1661 }
1662
1663 function get_lastpostdate($timezone = 'server') {
1664         global $cache_lastpostdate, $pagenow, $wpdb, $blog_id;
1665         $add_seconds_blog = get_option('gmt_offset') * 3600;
1666         $add_seconds_server = date('Z');
1667         if ( !isset($cache_lastpostdate[$blog_id][$timezone]) ) {
1668                 switch(strtolower($timezone)) {
1669                         case 'gmt':
1670                                 $lastpostdate = $wpdb->get_var("SELECT post_date_gmt FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
1671                                 break;
1672                         case 'blog':
1673                                 $lastpostdate = $wpdb->get_var("SELECT post_date FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
1674                                 break;
1675                         case 'server':
1676                                 $lastpostdate = $wpdb->get_var("SELECT DATE_ADD(post_date_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
1677                                 break;
1678                 }
1679                 $cache_lastpostdate[$blog_id][$timezone] = $lastpostdate;
1680         } else {
1681                 $lastpostdate = $cache_lastpostdate[$blog_id][$timezone];
1682         }
1683         return apply_filters( 'get_lastpostdate', $lastpostdate );
1684 }
1685
1686 function get_lastpostmodified($timezone = 'server') {
1687         global $cache_lastpostmodified, $pagenow, $wpdb, $blog_id;
1688         $add_seconds_blog = get_option('gmt_offset') * 3600;
1689         $add_seconds_server = date('Z');
1690         if ( !isset($cache_lastpostmodified[$blog_id][$timezone]) ) {
1691                 switch(strtolower($timezone)) {
1692                         case 'gmt':
1693                                 $lastpostmodified = $wpdb->get_var("SELECT post_modified_gmt FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
1694                                 break;
1695                         case 'blog':
1696                                 $lastpostmodified = $wpdb->get_var("SELECT post_modified FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
1697                                 break;
1698                         case 'server':
1699                                 $lastpostmodified = $wpdb->get_var("SELECT DATE_ADD(post_modified_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
1700                                 break;
1701                 }
1702                 $lastpostdate = get_lastpostdate($timezone);
1703                 if ( $lastpostdate > $lastpostmodified ) {
1704                         $lastpostmodified = $lastpostdate;
1705                 }
1706                 $cache_lastpostmodified[$blog_id][$timezone] = $lastpostmodified;
1707         } else {
1708                 $lastpostmodified = $cache_lastpostmodified[$blog_id][$timezone];
1709         }
1710         return apply_filters( 'get_lastpostmodified', $lastpostmodified );
1711 }
1712
1713 //
1714 // Cache
1715 //
1716
1717 function update_post_cache(&$posts) {
1718         global $post_cache, $blog_id;
1719
1720         if ( !$posts )
1721                 return;
1722
1723         for ($i = 0; $i < count($posts); $i++) {
1724                 $post_cache[$blog_id][$posts[$i]->ID] = &$posts[$i];
1725         }
1726 }
1727
1728 function clean_post_cache($id) {
1729         global $post_cache, $post_meta_cache, $post_term_cache, $blog_id;
1730
1731         if ( isset( $post_cache[$blog_id][$id] ) )
1732                 unset( $post_cache[$blog_id][$id] );
1733
1734         if ( isset ($post_meta_cache[$blog_id][$id] ) )
1735                 unset( $post_meta_cache[$blog_id][$id] );
1736
1737         clean_object_term_cache($id, 'post');
1738 }
1739
1740 function update_page_cache(&$pages) {
1741         global $page_cache, $blog_id;
1742
1743         if ( !$pages )
1744                 return;
1745
1746         for ($i = 0; $i < count($pages); $i++) {
1747                 $page_cache[$blog_id][$pages[$i]->ID] = &$pages[$i];
1748                 wp_cache_add($pages[$i]->ID, $pages[$i], 'pages');
1749         }
1750 }
1751
1752 function clean_page_cache($id) {
1753         global $page_cache, $blog_id;
1754
1755         if ( isset( $page_cache[$blog_id][$id] ) )
1756                 unset( $page_cache[$blog_id][$id] );
1757
1758         wp_cache_delete($id, 'pages');
1759         wp_cache_delete( 'all_page_ids', 'pages' );
1760         wp_cache_delete( 'get_pages', 'page' );
1761 }
1762
1763 function update_post_caches(&$posts) {
1764         global $post_cache;
1765         global $wpdb, $blog_id;
1766
1767         // No point in doing all this work if we didn't match any posts.
1768         if ( !$posts )
1769                 return;
1770
1771         // Get the categories for all the posts
1772         for ($i = 0; $i < count($posts); $i++) {
1773                 $post_id_array[] = $posts[$i]->ID;
1774                 $post_cache[$blog_id][$posts[$i]->ID] = &$posts[$i];
1775         }
1776
1777         $post_id_list = implode(',', $post_id_array);
1778
1779         update_object_term_cache($post_id_list, 'post');
1780
1781         update_postmeta_cache($post_id_list);
1782 }
1783
1784 function update_postmeta_cache($post_id_list = '') {
1785         global $wpdb, $post_meta_cache, $blog_id;
1786
1787         // We should validate this comma-separated list for the upcoming SQL query
1788         $post_id_list = preg_replace('|[^0-9,]|', '', $post_id_list);
1789
1790         if ( empty( $post_id_list ) )
1791                 return false;
1792
1793         // we're marking each post as having its meta cached (with no keys... empty array), to prevent posts with no meta keys from being queried again
1794         // any posts that DO have keys will have this empty array overwritten with a proper array, down below
1795         $post_id_array = (array) explode(',', $post_id_list);
1796         $count = count( $post_id_array);
1797         for ( $i = 0; $i < $count; $i++ ) {
1798                 $post_id = (int) $post_id_array[ $i ];
1799                 if ( isset( $post_meta_cache[$blog_id][$post_id] ) ) { // If the meta is already cached
1800                         unset( $post_id_array[ $i ] );
1801                         continue;
1802                 }
1803                 $post_meta_cache[$blog_id][$post_id] = array();
1804         }
1805         if ( count( $post_id_array ) == 0 )
1806                 return;
1807         $post_id_list = join( ',', $post_id_array ); // with already cached stuff removeds
1808
1809         // Get post-meta info
1810         if ( $meta_list = $wpdb->get_results("SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta WHERE post_id IN($post_id_list) ORDER BY post_id, meta_key", ARRAY_A) ) {
1811                 // Change from flat structure to hierarchical:
1812                 if ( !isset($post_meta_cache) )
1813                         $post_meta_cache[$blog_id] = array();
1814
1815                 foreach ($meta_list as $metarow) {
1816                         $mpid = (int) $metarow['post_id'];
1817                         $mkey = $metarow['meta_key'];
1818                         $mval = $metarow['meta_value'];
1819
1820                         // Force subkeys to be array type:
1821                         if ( !isset($post_meta_cache[$blog_id][$mpid]) || !is_array($post_meta_cache[$blog_id][$mpid]) )
1822                                 $post_meta_cache[$blog_id][$mpid] = array();
1823                         if ( !isset($post_meta_cache[$blog_id][$mpid]["$mkey"]) || !is_array($post_meta_cache[$blog_id][$mpid]["$mkey"]) )
1824                                 $post_meta_cache[$blog_id][$mpid]["$mkey"] = array();
1825
1826                         // Add a value to the current pid/key:
1827                         $post_meta_cache[$blog_id][$mpid][$mkey][] = $mval;
1828                 }
1829         }
1830 }
1831
1832 //
1833 // Hooks
1834 //
1835
1836 function _transition_post_status($new_status, $old_status, $post) {
1837         global $wpdb;
1838
1839         if ( $old_status != 'publish' && $new_status == 'publish' ) {
1840                         // Reset GUID if transitioning to publish.
1841                         $wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post->ID) . "' WHERE ID = '$post->ID'");
1842                         do_action('private_to_published', $post->ID);  // Deprecated, use private_to_publish
1843         }
1844
1845         // Always clears the hook in case the post status bounced from future to draft.
1846         wp_clear_scheduled_hook('publish_future_post', $post->ID);
1847 }
1848
1849 function _future_post_hook($post_id, $post) {
1850         // Schedule publication.
1851         wp_clear_scheduled_hook( 'publish_future_post', $post->ID );
1852         wp_schedule_single_event(strtotime($post->post_date_gmt. ' GMT'), 'publish_future_post', array($post->ID));
1853 }
1854
1855 function _publish_post_hook($post_id) {
1856         global $wpdb;
1857
1858         if ( defined('XMLRPC_REQUEST') )
1859                 do_action('xmlrpc_publish_post', $post_id);
1860         if ( defined('APP_REQUEST') )
1861                 do_action('app_publish_post', $post_id);
1862
1863         if ( defined('WP_IMPORTING') )
1864                 return;
1865
1866         $post = get_post($post_id);
1867
1868         if ( get_option('default_pingback_flag') )
1869                 $result = $wpdb->query("
1870                         INSERT INTO $wpdb->postmeta
1871                         (post_id,meta_key,meta_value)
1872                         VALUES ('$post_id','_pingme','1')
1873                 ");
1874         $result = $wpdb->query("
1875                 INSERT INTO $wpdb->postmeta
1876                 (post_id,meta_key,meta_value)
1877                 VALUES ('$post_id','_encloseme','1')
1878         ");
1879         wp_schedule_single_event(time(), 'do_pings');
1880 }
1881
1882 function _save_post_hook($post_id, $post) {
1883         if ( $post->post_type == 'page' ) {
1884                 if ( !empty($post->page_template) )
1885                         if ( ! update_post_meta($post_id, '_wp_page_template',  $post->page_template))
1886                                 add_post_meta($post_id, '_wp_page_template',  $post->page_template, true);
1887
1888                 clean_page_cache($post_id);
1889                 global $wp_rewrite;
1890                 $wp_rewrite->flush_rules();
1891         } else {
1892                 clean_post_cache($post_id);
1893         }
1894 }
1895
1896 ?>