root/tags/2.0.9/wp-includes/functions-post.php

Revision 4602, 30.3 kB (checked in by markjaquith, 2 years ago)

change post_parent on orphaned attachments. props ryanscheuermann. fixes #2681

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2
3 /**** DB Functions ****/
4
5 /*
6  * generic function for inserting data into the posts table.
7  */
8 function wp_insert_post($postarr = array()) {
9     global $wpdb, $wp_rewrite, $allowedtags, $user_ID;
10
11     if ( is_object($postarr) )
12         $postarr = get_object_vars($postarr);
13
14     // export array as variables
15     extract($postarr);
16
17     // Are we updating or creating?
18     $update = false;
19     if ( !empty($ID) ) {
20         $update = true;
21         $post = & get_post($ID);
22         $previous_status = $post->post_status;
23     }
24
25     // Get the basics.
26     $post_content    = apply_filters('content_save_pre',   $post_content);
27     $post_content_filtered = apply_filters('content_filtered_save_pre',   $post_content_filtered);
28     $post_excerpt    = apply_filters('excerpt_save_pre',   $post_excerpt);
29     $post_title      = apply_filters('title_save_pre',     $post_title);
30     $post_category   = apply_filters('category_save_pre'$post_category);
31     $post_status     = apply_filters('status_save_pre',    $post_status);
32     $post_name       = apply_filters('name_save_pre',      $post_name);
33     $comment_status  = apply_filters('comment_status_pre', $comment_status);
34     $ping_status     = apply_filters('ping_status_pre',    $ping_status);
35     
36     // Make sure we set a valid category
37     if (0 == count($post_category) || !is_array($post_category)) {
38         $post_category = array(get_option('default_category'));
39     }
40     $post_cat = $post_category[0];
41
42     if ( empty($post_author) )
43         $post_author = $user_ID;
44
45     if ( empty($post_status) )
46         $post_status = 'draft';
47     
48     // Get the post ID.
49     if ( $update )
50         $post_ID = $ID;
51
52     // Create a valid post name.  Drafts are allowed to have an empty
53     // post name.
54     if ( empty($post_name) ) {
55         if ( 'draft' != $post_status )
56             $post_name = sanitize_title($post_title);
57     } else {
58         $post_name = sanitize_title($post_name);
59     }
60     
61
62     // If the post date is empty (due to having been new or a draft) and status is not 'draft', set date to now
63     if (empty($post_date)) {
64         if ( 'draft' != $post_status )
65             $post_date = current_time('mysql');
66     }
67
68     if (empty($post_date_gmt)) {
69         if ( 'draft' != $post_status )
70             $post_date_gmt = get_gmt_from_date($post_date);
71     }
72
73     if ( empty($comment_status) ) {
74         if ( $update )
75             $comment_status = 'closed';
76         else
77             $comment_status = get_settings('default_comment_status');
78     }
79     if ( empty($ping_status) )
80         $ping_status = get_settings('default_ping_status');
81     if ( empty($post_pingback) )
82         $post_pingback = get_option('default_pingback_flag');
83
84     if ( isset($to_ping) )
85         $to_ping = preg_replace('|\s+|', "\n", $to_ping);
86     else
87         $to_ping = '';
88
89     if ( ! isset($pinged) )
90         $pinged = '';
91
92     if ( isset($post_parent) )
93         $post_parent = (int) $post_parent;
94     else
95         $post_parent = 0;
96
97     if ( isset($menu_order) )
98         $menu_order = (int) $menu_order;
99     else
100         $menu_order = 0;
101
102     if ( !isset($post_password) )
103         $post_password = '';
104
105     if ( ('publish' == $post_status) || ('static' == $post_status) ) {
106         $post_name_check = ('publish' == $post_status)
107             ? $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$post_name' AND post_status = 'publish' AND ID != '$post_ID' LIMIT 1")
108             : $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$post_name' AND post_status = 'static' AND ID != '$post_ID' AND post_parent = '$post_parent' LIMIT 1");
109
110         if ($post_name_check) {
111             $suffix = 2;
112             while ($post_name_check) {
113                 $alt_post_name = $post_name . "-$suffix";
114                 $post_name_check = ('publish' == $post_status)
115                     ? $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$alt_post_name' AND post_status = 'publish' AND ID != '$post_ID' LIMIT 1")
116                     : $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$alt_post_name' AND post_status = 'static' AND ID != '$post_ID' AND post_parent = '$post_parent' LIMIT 1");
117                 $suffix++;
118             }
119             $post_name = $alt_post_name;
120         }
121     }
122
123     if ($update) {
124         $wpdb->query(
125             "UPDATE IGNORE $wpdb->posts SET
126             post_author = '$post_author',
127             post_date = '$post_date',
128             post_date_gmt = '$post_date_gmt',
129             post_content = '$post_content',
130             post_content_filtered = '$post_content_filtered',
131             post_title = '$post_title',
132             post_excerpt = '$post_excerpt',
133             post_status = '$post_status',
134             comment_status = '$comment_status',
135             ping_status = '$ping_status',
136             post_password = '$post_password',
137             post_name = '$post_name',
138             to_ping = '$to_ping',
139             pinged = '$pinged',
140             post_modified = '".current_time('mysql')."',
141             post_modified_gmt = '".current_time('mysql',1)."',
142             post_parent = '$post_parent',
143             menu_order = '$menu_order'
144             WHERE ID = $post_ID");
145     } else {
146         $wpdb->query(
147             "INSERT IGNORE INTO $wpdb->posts
148             (post_author, post_date, post_date_gmt, post_content, post_content_filtered, post_title, post_excerpt,  post_status, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_parent, menu_order, post_mime_type)
149             VALUES
150             ('$post_author', '$post_date', '$post_date_gmt', '$post_content', '$post_content_filtered', '$post_title', '$post_excerpt', '$post_status', '$comment_status', '$ping_status', '$post_password', '$post_name', '$to_ping', '$pinged', '$post_date', '$post_date_gmt', '$post_parent', '$menu_order', '$post_mime_type')");
151             $post_ID = $wpdb->insert_id;           
152     }
153
154     if ( empty($post_name) && 'draft' != $post_status ) {
155         $post_name = sanitize_title($post_title, $post_ID);
156         $wpdb->query( "UPDATE $wpdb->posts SET post_name = '$post_name' WHERE ID = '$post_ID'" );
157     }
158
159     wp_set_post_cats('', $post_ID, $post_category);
160
161     if ( 'static' == $post_status ) {
162         clean_page_cache($post_ID);
163         wp_cache_delete($post_ID, 'pages');
164     } else {
165         clean_post_cache($post_ID);
166     }
167
168     // Set GUID
169     if ( ! $update )
170         $wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'");
171
172     if ( $update) {
173         if ($previous_status != 'publish' && $post_status == 'publish') {
174             // Reset GUID if transitioning to publish.
175             $wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'");
176             do_action('private_to_published', $post_ID);
177         }
178         
179         do_action('edit_post', $post_ID);
180     }
181
182     if ($post_status == 'publish') {
183         do_action('publish_post', $post_ID);
184
185         if ( !defined('WP_IMPORTING') ) {
186             if ( $post_pingback )
187                 $result = $wpdb->query("
188                     INSERT INTO $wpdb->postmeta
189                     (post_id,meta_key,meta_value)
190                     VALUES ('$post_ID','_pingme','1')
191                 ");
192             $result = $wpdb->query("
193                 INSERT INTO $wpdb->postmeta
194                 (post_id,meta_key,meta_value)
195                 VALUES ('$post_ID','_encloseme','1')
196             ");
197             spawn_pinger();
198         }
199     } else if ($post_status == 'static') {
200         wp_cache_delete('all_page_ids', 'pages');
201         $wp_rewrite->flush_rules();
202
203         if ( !empty($page_template) )
204             if ( ! update_post_meta($post_ID, '_wp_page_template'$page_template))
205                 add_post_meta($post_ID, '_wp_page_template'$page_template, true);
206     }
207
208     do_action('save_post', $post_ID);
209     do_action('wp_insert_post', $post_ID);
210
211     return $post_ID;
212 }
213
214 function wp_insert_attachment($object, $file = false, $post_parent = 0) {
215     global $wpdb, $user_ID;
216
217     if ( is_object($object) )
218         $object = get_object_vars($object);
219
220     // Export array as variables
221     extract($object);
222
223     // Get the basics.
224     $post_content    = apply_filters('content_save_pre',   $post_content);
225     $post_content_filtered = apply_filters('content_filtered_save_pre',   $post_content_filtered);
226     $post_excerpt    = apply_filters('excerpt_save_pre',   $post_excerpt);
227     $post_title      = apply_filters('title_save_pre',     $post_title);
228     $post_category   = apply_filters('category_save_pre'$post_category);
229     $post_name       = apply_filters('name_save_pre',      $post_name);
230     $comment_status  = apply_filters('comment_status_pre', $comment_status);
231     $ping_status     = apply_filters('ping_status_pre',    $ping_status);
232     $post_mime_type  = apply_filters('post_mime_type_pre', $post_mime_type);
233
234     // Make sure we set a valid category
235     if (0 == count($post_category) || !is_array($post_category)) {
236         $post_category = array(get_option('default_category'));
237     }
238     $post_cat = $post_category[0];
239
240     if ( empty($post_author) )
241         $post_author = $user_ID;
242
243     $post_status = 'attachment';
244
245     // Are we updating or creating?
246     $update = false;
247     if ( !empty($ID) ) {
248         $update = true;
249         $post_ID = $ID;   
250     }
251
252     // Create a valid post name.
253     if ( empty($post_name) )
254         $post_name = sanitize_title($post_title);
255     else
256         $post_name = sanitize_title($post_name);
257     
258     if (empty($post_date))
259         $post_date = current_time('mysql');
260     if (empty($post_date_gmt))
261         $post_date_gmt = current_time('mysql', 1);
262
263     if ( empty($comment_status) ) {
264         if ( $update )
265             $comment_status = 'closed';
266         else
267             $comment_status = get_settings('default_comment_status');
268     }
269     if ( empty($ping_status) )
270         $ping_status = get_settings('default_ping_status');
271     if ( empty($post_pingback) )
272         $post_pingback = get_option('default_pingback_flag');
273
274     if ( isset($to_ping) )
275         $to_ping = preg_replace('|\s+|', "\n", $to_ping);
276     else
277         $to_ping = '';
278
279     if ( isset($post_parent) )
280         $post_parent = (int) $post_parent;
281     else
282         $post_parent = 0;
283
284     if ( isset($menu_order) )
285         $menu_order = (int) $menu_order;
286     else
287         $menu_order = 0;
288
289     if ( !isset($post_password) )
290         $post_password = '';
291
292     if ( isset($to_ping) )
293         $to_ping = preg_replace('|\s+|', "\n", $to_ping);
294     else
295         $to_ping = '';
296
297     if ( ! isset($pinged) )
298         $pinged = '';
299
300     if ($update) {
301         $wpdb->query(
302             "UPDATE $wpdb->posts SET
303             post_author = '$post_author',
304             post_date = '$post_date',
305             post_date_gmt = '$post_date_gmt',
306             post_content = '$post_content',
307             post_content_filtered = '$post_content_filtered',
308             post_title = '$post_title',
309             post_excerpt = '$post_excerpt',
310             post_status = '$post_status',
311             comment_status = '$comment_status',
312             ping_status = '$ping_status',
313             post_password = '$post_password',
314             post_name = '$post_name',
315             to_ping = '$to_ping',
316             pinged = '$pinged',
317             post_modified = '".current_time('mysql')."',
318             post_modified_gmt = '".current_time('mysql',1)."',
319             post_parent = '$post_parent',
320             menu_order = '$menu_order',
321             post_mime_type = '$post_mime_type',
322             guid = '$guid'
323             WHERE ID = $post_ID");
324     } else {
325         $wpdb->query(
326             "INSERT INTO $wpdb->posts
327             (post_author, post_date, post_date_gmt, post_content, post_content_filtered, post_title, post_excerpt,  post_status, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_parent, menu_order, post_mime_type, guid)
328             VALUES
329             ('$post_author', '$post_date', '$post_date_gmt', '$post_content', '$post_content_filtered', '$post_title', '$post_excerpt', '$post_status', '$comment_status', '$ping_status', '$post_password', '$post_name', '$to_ping', '$pinged', '$post_date', '$post_date_gmt', '$post_parent', '$menu_order', '$post_mime_type', '$guid')");
330             $post_ID = $wpdb->insert_id;           
331     }
332     
333     if ( empty($post_name) ) {
334         $post_name = sanitize_title($post_title, $post_ID);
335         $wpdb->query( "UPDATE $wpdb->posts SET post_name = '$post_name' WHERE ID = '$post_ID'" );
336     }
337
338     wp_set_post_cats('', $post_ID, $post_category);
339
340     if ( $file )
341         add_post_meta($post_ID, '_wp_attached_file', $file);
342
343     clean_post_cache($post_ID);
344
345     if ( $update) {
346         do_action('edit_attachment', $post_ID);
347     } else {
348         do_action('add_attachment', $post_ID);
349     }
350     
351     return $post_ID;
352 }
353
354 function wp_delete_attachment($postid) {
355     global $wpdb;
356     $postid = (int) $postid;
357
358     if ( !$post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = '$postid'") )
359         return $post;
360
361     if ( 'attachment' != $post->post_status )
362         return false;
363
364     $meta = get_post_meta($postid, '_wp_attachment_metadata', true);
365     $file = get_post_meta($postid, '_wp_attached_file', true);
366
367     $wpdb->query("DELETE FROM $wpdb->posts WHERE ID = '$postid'");
368
369     $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID = '$postid'");
370
371     $wpdb->query("DELETE FROM $wpdb->post2cat WHERE post_id = '$postid'");
372
373     $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$postid'");
374
375     if ( ! empty($meta['thumb']) ) {
376         // Don't delete the thumb if another attachment uses it
377         if (! $foo = $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'"))
378             @ unlink(str_replace(basename($file), $meta['thumb'], $file));
379     }
380
381     if ( ! empty($file) )
382         @ unlink($file);
383
384     do_action('delete_attachment', $postid);
385
386     return $post;
387 }
388
389 function wp_get_single_post($postid = 0, $mode = OBJECT) {
390     global $wpdb;
391
392     $post = get_post($postid, $mode);
393     
394     // Set categories
395     if($mode == OBJECT) {
396         $post->post_category = wp_get_post_cats('',$postid);
397     }
398     else {
399         $post['post_category'] = wp_get_post_cats('',$postid);
400     }
401
402     return $post;
403 }
404
405 function wp_get_recent_posts($num = 10) {
406     global $wpdb;
407
408     // Set the limit clause, if we got a limit
409     if ($num) {
410         $limit = "LIMIT $num";
411     }
412
413     $sql = "SELECT * FROM $wpdb->posts WHERE post_status IN ('publish', 'draft', 'private') ORDER BY post_date DESC $limit";
414     $result = $wpdb->get_results($sql,ARRAY_A);
415
416     return $result?$result:array();
417 }
418
419 function wp_update_post($postarr = array()) {
420     global $wpdb;
421
422     if ( is_object($postarr) )
423         $postarr = get_object_vars($postarr);
424
425     // First, get all of the original fields
426     $post = wp_get_single_post($postarr['ID'], ARRAY_A);   
427
428     // Escape data pulled from DB.
429     $post = add_magic_quotes($post);
430
431     // Passed post category list overwrites existing category list if not empty.
432      if ( isset($postarr['post_category']) && is_array($postarr['post_category'])
433              && 0 != count($postarr['post_category']) )
434          $post_cats = $postarr['post_category'];
435      else
436          $post_cats = $post['post_category'];
437
438     // Drafts shouldn't be assigned a date unless explicitly done so by the user
439     if ( 'draft' == $post['post_status'] && empty($postarr['edit_date']) && empty($postarr['post_date']) &&
440          ('0000-00-00 00:00:00' == $post['post_date']) )
441         $clear_date = true;
442     else
443         $clear_date = false;
444
445      // Merge old and new fields with new fields overwriting old ones.
446      $postarr = array_merge($post, $postarr);
447      $postarr['post_category'] = $post_cats;   
448     if ( $clear_date ) {
449         $postarr['post_date'] = '';
450         $postarr['post_date_gmt'] = '';
451     }
452
453     if ($postarr['post_status'] == 'attachment')
454         return wp_insert_attachment($postarr);
455
456     return wp_insert_post($postarr);
457 }
458
459 function wp_get_post_cats($blogid = '1', $post_ID = 0) {
460     global $wpdb;
461     
462     $post_ID = (int) $post_ID;
463
464     $sql = "SELECT category_id
465         FROM $wpdb->post2cat
466         WHERE post_id = '$post_ID'
467         ORDER BY category_id";
468
469     $result = $wpdb->get_col($sql);
470
471     if ( !$result )
472         $result = array();
473
474     return array_unique($result);
475 }
476
477 function wp_set_post_cats($blogid = '1', $post_ID = 0, $post_categories = array()) {
478     global $wpdb;
479     // If $post_categories isn't already an array, make it one:
480     if (!is_array($post_categories) || 0 == count($post_categories))
481         $post_categories = array(get_option('default_category'));
482     
483     $post_categories = array_unique($post_categories);
484
485     // First the old categories
486     $old_categories = $wpdb->get_col("
487         SELECT category_id
488         FROM $wpdb->post2cat
489         WHERE post_id = $post_ID");
490     
491     if (!$old_categories) {
492         $old_categories = array();
493     } else {
494         $old_categories = array_unique($old_categories);
495     }
496
497     // Delete any?
498     $delete_cats = array_diff($old_categories,$post_categories);
499
500     if ($delete_cats) {
501         foreach ($delete_cats as $del) {
502             $wpdb->query("
503                 DELETE FROM $wpdb->post2cat
504                 WHERE category_id = $del
505                     AND post_id = $post_ID
506                 ");
507         }
508     }
509
510     // Add any?
511     $add_cats = array_diff($post_categories, $old_categories);
512
513     if ($add_cats) {
514         foreach ($add_cats as $new_cat) {
515             $wpdb->query("
516                 INSERT INTO $wpdb->post2cat (post_id, category_id)
517                 VALUES ($post_ID, $new_cat)");
518         }
519     }
520     
521     // Update category counts.
522     $all_affected_cats = array_unique(array_merge($post_categories, $old_categories));
523     foreach ( $all_affected_cats as $cat_id ) {
524         $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->post2cat, $wpdb->posts WHERE $wpdb->posts.ID=$wpdb->post2cat.post_id AND post_status='publish' AND category_id = '$cat_id'");
525         $wpdb->query("UPDATE $wpdb->categories SET category_count = '$count' WHERE cat_ID = '$cat_id'");
526         wp_cache_delete($cat_id, 'category');       
527     }
528 }    // wp_set_post_cats()
529