root/branches/2.0/wp-includes/functions-post.php

Revision 5504, 30.4 kB (checked in by markjaquith, 2 years ago)

Revert accidental debug commit in [5502]

  • 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 = (int) $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     $num = (int) $num;
410     if ($num) {
411         $limit = "LIMIT $num";
412     }
413
414     $sql = "SELECT * FROM $wpdb->posts WHERE post_status IN ('publish', 'draft', 'private') ORDER BY post_date DESC $limit";
415     $result = $wpdb->get_results($sql,ARRAY_A);
416
417     return $result?$result:array();
418 }
419
420 function wp_update_post($postarr = array()) {
421     global $wpdb;
422
423     if ( is_object($postarr) )
424         $postarr = get_object_vars($postarr);
425
426     // First, get all of the original fields
427     $post = wp_get_single_post($postarr['ID'], ARRAY_A);   
428
429     // Escape data pulled from DB.
430     $post = add_magic_quotes($post);
431
432     // Passed post category list overwrites existing category list if not empty.
433      if ( isset($postarr['post_category']) && is_array($postarr['post_category'])
434              && 0 != count($postarr['post_category']) )
435          $post_cats = $postarr['post_category'];
436      else
437          $post_cats = $post['post_category'];
438
439     // Drafts shouldn't be assigned a date unless explicitly done so by the user
440     if ( 'draft' == $post['post_status'] && empty($postarr['edit_date']) && empty($postarr['post_date']) &&
441          ('0000-00-00 00:00:00' == $post['post_date']) )
442         $clear_date = true;
443     else
444         $clear_date = false;
445
446      // Merge old and new fields with new fields overwriting old ones.
447      $postarr = array_merge($post, $postarr);
448      $postarr['post_category'] = $post_cats;   
449     if ( $clear_date ) {
450         $postarr['post_date'] = '';
451         $postarr['post_date_gmt'] = '';
452     }
453
454     if ($postarr['post_status'] == 'attachment')
455         return wp_insert_attachment($postarr);
456
457     return wp_insert_post($postarr);
458 }
459
460 function wp_get_post_cats($blogid = '1', $post_ID = 0) {
461     global $wpdb;
462     
463     $post_ID = (int) $post_ID;
464
465     $sql = "SELECT category_id
466         FROM $wpdb->post2cat
467         WHERE post_id = '$post_ID'
468         ORDER BY category_id";
469
470     $result = $wpdb->get_col($sql);
471
472     if ( !$result )
473         $result = array();
474
475     return array_unique($result);
476 }
477
478 function wp_set_post_cats($blogid = '1', $post_ID = 0, $post_categories = array()) {
479     global $wpdb;
480     
481     $post_ID = (int) $post_ID;
482
483     // If $post_categories isn't already an array, make it one:
484     if (!is_array($post_categories) || 0 == count($post_categories))
485         $post_categories = array(get_option('default_category'));
486     
487     $post_categories = array_unique($post_categories);
488
489     // First the old categories
490     $old_categories = $wpdb->get_col("
491         SELECT category_id
492         FROM $wpdb->post2cat
493         WHERE post_id = '$post_ID'");
494     
495     if (!$old_categories) {
496         $old_categories = array();
497     } else {
498         $old_categories = array_unique($old_categories);
499     }
500
501     // Delete any?
502     $delete_cats = array_diff($old_categories,$post_categories);
503
504     if ($delete_cats) {
505         foreach ($delete_cats as $del) {
506             $wpdb->query("
507                 DELETE FROM $wpdb->post2cat
508                 WHERE category_id = '$del'
509                     AND post_id = '$post_ID'
510                 ");
511         }
512     }
513
514     // Add any?
515     $add_cats = array_diff($post_categories, $old_categories);
516
517     if ($add_cats) {
518         foreach ($add_cats as $new_cat) {
519             $new_cat = (int) $new_cat;
520             if ( !empty($new_cat) )
521                 $wpdb->query("
522                     INSERT INTO $wpdb->post2cat (post_id, category_id)
523                     VALUES ('$post_ID', '$new_cat')");
524         }
525     }
526
527     // Update category counts.
528     $all_affected_cats = array_unique(array_merge($post_categories, $old_categories));
529     foreach ( $all_affected_cats as $cat_id ) {
530         $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'"