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

Revision 3358, 26.7 kB (checked in by ryan, 3 years ago)

Silence mkdir().

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