root/branches/2.2/wp-includes/post.php

Revision 5714, 46.6 kB (checked in by ryan, 1 year ago)

Use EXTR_SKIP when extracting. For 2.2. See #4468

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2
3 //
4 // Post functions
5 //
6
7 function get_attached_file( $attachment_id, $unfiltered = false ) {
8     $file = get_post_meta( $attachment_id, '_wp_attached_file', true );
9     if ( $unfiltered )
10         return $file;
11     return apply_filters( 'get_attached_file', $file, $attachment_id );
12 }
13
14 function update_attached_file( $attachment_id, $file ) {
15     if ( !get_post( $attachment_id ) )
16         return false;
17
18     $old_file = get_attached_file( $attachment_id, true );
19
20     $file = apply_filters( 'update_attached_file', $file, $attachment_id );
21
22     if ( $old_file )
23         return update_post_meta( $attachment_id, '_wp_attached_file', $file, $old_file );
24     else
25         return add_post_meta( $attachment_id, '_wp_attached_file', $file );
26 }
27
28 function &get_children($args = '', $output = OBJECT) {
29     global $post_cache, $wpdb, $blog_id;
30
31     if ( empty($args) ) {
32         if ( isset($GLOBALS['post']) )
33             $r = array('post_parent' => & $GLOBALS['post']->post_parent);
34         else
35             return false;
36     } elseif ( is_object($args) )
37         $r = array('post_parent' => $post->post_parent);
38     elseif ( is_numeric($args) )
39         $r = array('post_parent' => $args);
40     elseif ( is_array($args) )
41         $r = &$args;
42     else
43         parse_str($args, $r);
44
45     $defaults = array('numberposts' => -1, 'post_type' => '', 'post_status' => '', 'post_parent' => 0);
46     $r = array_merge($defaults, $r);
47
48     $children = get_posts( $r );
49
50     if ( $children ) {
51         foreach ( $children as $key => $child ) {
52             $post_cache[$blog_id][$child->ID] =& $children[$key];
53             $kids[$child->ID] =& $children[$key];
54         }
55     } else {
56         return false;
57     }
58
59     if ( $output == OBJECT ) {
60         return $kids;
61     } elseif ( $output == ARRAY_A ) {
62         foreach ( $kids as $kid )
63             $weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]);
64         return $weeuns;
65     } elseif ( $output == ARRAY_N ) {
66         foreach ( $kids as $kid )
67             $babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID]));
68         return $babes;
69     } else {
70         return $kids;
71     }
72 }
73
74 // get extended entry info (<!--more-->)
75 function get_extended($post) {
76     //Match the new style more links
77     if ( preg_match('/<!--more(.*?)?-->/', $post, $matches) ) {
78         list($main, $extended) = explode($matches[0], $post, 2);
79     } else {
80         $main = $post;
81         $extended = '';
82     }
83
84     // Strip leading and trailing whitespace
85     $main = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $main);
86     $extended = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $extended);
87
88     return array('main' => $main, 'extended' => $extended);
89 }
90
91 // Retrieves post data given a post ID or post object.
92 // Handles post caching.
93 function &get_post(&$post, $output = OBJECT) {
94     global $post_cache, $wpdb, $blog_id;
95
96     if ( empty($post) ) {
97         if ( isset($GLOBALS['post']) )
98             $_post = & $GLOBALS['post'];
99         else
100             $_post = null;
101     } elseif ( is_object($post) ) {
102         if ( 'page' == $post->post_type )
103             return get_page($post, $output);
104         if ( !isset($post_cache[$blog_id][$post->ID]) )
105             $post_cache[$blog_id][$post->ID] = &$post;
106         $_post = & $post_cache[$blog_id][$post->ID];
107     } else {
108         $post = (int) $post;
109         if ( isset($post_cache[$blog_id][$post]) )
110             $_post = & $post_cache[$blog_id][$post];
111         elseif ( $_post = wp_cache_get($post, 'pages') )
112             return get_page($_post, $output);
113         else {
114             $query = "SELECT * FROM $wpdb->posts WHERE ID = '$post' LIMIT 1";
115             $_post = & $wpdb->get_row($query);
116             if ( 'page' == $_post->post_type )
117                 return get_page($_post, $output);
118             $post_cache[$blog_id][$post] = & $_post;
119         }
120     }
121
122     if ( defined('WP_IMPORTING') )
123         unset($post_cache[$blog_id]);
124
125     if ( $output == OBJECT ) {
126         return $_post;
127     } elseif ( $output == ARRAY_A ) {
128         return get_object_vars($_post);
129     } elseif ( $output == ARRAY_N ) {
130         return array_values(get_object_vars($_post));
131     } else {
132         return $_post;
133     }
134 }
135
136 // Takes a post ID, returns its mime type.
137 function get_post_mime_type($ID = '') {
138     $post = & get_post($ID);
139
140     if ( is_object($post) )
141         return $post->post_mime_type;
142
143     return false;
144 }
145
146 function get_post_status($ID = '') {
147     $post = get_post($ID);
148
149     if ( is_object($post) ) {
150         if ( ('attachment' == $post->post_type) && $post->post_parent && ($post->ID != $post->post_parent) )
151             return get_post_status($post->post_parent);
152         else
153             return $post->post_status;
154     }
155
156     return false;
157 }
158
159 function get_post_type($post = false) {
160     global $wpdb, $posts;
161
162     if ( false === $post )
163         $post = $posts[0];
164     elseif ( (int) $post )
165         $post = get_post($post, OBJECT);
166
167     if ( is_object($post) )
168         return $post->post_type;
169
170     return false;
171 }
172
173 function get_posts($args) {
174     global $wpdb;
175
176     if ( is_array($args) )
177         $r = &$args;
178     else
179         parse_str($args, $r);
180
181     $defaults = array('numberposts' => 5, 'offset' => 0, 'category' => 0,
182         'orderby' => 'post_date', 'order' => 'DESC', 'include' => '', 'exclude' => '',
183         'meta_key' => '', 'meta_value' =>'', 'post_type' => 'post', 'post_status' => 'publish', 'post_parent' => 0);
184     $r = array_merge($defaults, $r);
185     extract($r, EXTR_SKIP);
186     $numberposts = (int) $numberposts;
187     $offset = (int) $offset;
188     $category = (int) $category;
189     $post_parent = (int) $post_parent;
190
191     $inclusions = '';
192     if ( !empty($include) ) {
193         $offset = 0;    //ignore offset, category, exclude, meta_key, and meta_value, post_parent if using include
194         $category = 0;
195         $exclude = '';
196         $meta_key = '';
197         $meta_value = '';
198         $post_parent = 0;
199         $incposts = preg_split('/[\s,]+/',$include);
200         $numberposts = count($incposts);  // only the number of posts included
201         if ( count($incposts) ) {
202             foreach ( $incposts as $incpost ) {
203                 if (empty($inclusions))
204                     $inclusions = ' AND ( ID = ' . intval($incpost) . ' ';
205                 else
206                     $inclusions .= ' OR ID = ' . intval($incpost) . ' ';
207             }
208         }
209     }
210     if (!empty($inclusions))
211         $inclusions .= ')';
212
213     $exclusions = '';
214     if ( !empty($exclude) ) {
215         $exposts = preg_split('/[\s,]+/',$exclude);
216         if ( count($exposts) ) {
217             foreach ( $exposts as $expost ) {
218                 if (empty($exclusions))
219                     $exclusions = ' AND ( ID <> ' . intval($expost) . ' ';
220                 else
221                     $exclusions .= ' AND ID <> ' . intval($expost) . ' ';
222             }
223         }
224     }
225     if (!empty($exclusions))
226         $exclusions .= ')';
227
228     $query  = "SELECT DISTINCT * FROM $wpdb->posts ";
229     $query .= empty( $category ) ? '' : ", $wpdb->post2cat ";
230     $query .= empty( $meta_key ) ? '' : ", $wpdb->postmeta ";
231     $query .= " WHERE 1=1 ";
232     $query .= empty( $post_type ) ? '' : "AND post_type = '$post_type' ";
233     $query .= empty( $post_status ) ? '' : "AND post_status = '$post_status' ";
234     $query .= "$exclusions $inclusions " ;
235     $query .= empty( $category ) ? '' : "AND ($wpdb->posts.ID = $wpdb->post2cat.post_id AND $wpdb->post2cat.category_id = " . $category. ") ";
236     $query .= empty( $post_parent ) ? '' : "AND $wpdb->posts.post_parent = '$post_parent' ";
237     $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' )";
238     $query .= " GROUP BY $wpdb->posts.ID ORDER BY " . $orderby . ' ' . $order;
239     if ( 0 < $numberposts )
240         $query .= " LIMIT " . $offset . ',' . $numberposts;
241
242     $posts = $wpdb->get_results($query);
243
244     update_post_caches($posts);
245
246     return $posts;
247 }
248
249 //
250 // Post meta functions
251 //
252
253 function add_post_meta($post_id, $key, $value, $unique = false) {
254     global $wpdb, $post_meta_cache, $blog_id;
255
256     $post_id = (int) $post_id;
257
258     if ( $unique ) {
259         if ( $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key = '$key' AND post_id = '$post_id'") ) {
260             return false;
261         }
262     }
263
264     $post_meta_cache[$blog_id][$post_id][$key][] = $value;
265
266     $value = maybe_serialize($value);
267     $value = $wpdb->escape($value);
268
269     $wpdb->query("INSERT INTO $wpdb->postmeta (post_id,meta_key,meta_value) VALUES ('$post_id','$key','$value')");
270
271     return true;
272 }
273
274 function delete_post_meta($post_id, $key, $value = '') {
275     global $wpdb, $post_meta_cache, $blog_id;
276
277     $post_id = (int) $post_id;
278
279     if ( empty($value) ) {
280         $meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key'");
281     } else {
282         $meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key' AND meta_value = '$value'");
283     }
284
285     if ( !$meta_id )
286         return false;
287
288     if ( empty($value) ) {
289         $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key'");
290         unset($post_meta_cache[$blog_id][$post_id][$key]);
291     } else {
292         $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key' AND meta_value = '$value'");
293         $cache_key = $post_meta_cache[$blog_id][$post_id][$key];
294         if ($cache_key) foreach ( $cache_key as $index => $data )
295             if ( $data == $value )
296                 unset($post_meta_cache[$blog_id][$post_id][$key][$index]);
297     }
298
299     unset($post_meta_cache[$blog_id][$post_id][$key]);
300
301     return true;
302 }
303
304 function get_post_meta($post_id, $key, $single = false) {
305     global $wpdb, $post_meta_cache, $blog_id;
306
307     $post_id = (int) $post_id;
308
309     if ( isset($post_meta_cache[$blog_id][$post_id][$key]) ) {
310         if ( $single ) {
311             return maybe_unserialize( $post_meta_cache[$blog_id][$post_id][$key][0] );
312         } else {
313             return maybe_unserialize( $post_meta_cache[$blog_id][$post_id][$key] );
314         }
315     }
316
317     if ( !isset($post_meta_cache[$blog_id][$post_id]) )
318         update_postmeta_cache($post_id);
319
320     if ( $single ) {
321         if ( isset($post_meta_cache[$blog_id][$post_id][$key][0]) )
322             return maybe_unserialize($post_meta_cache[$blog_id][$post_id][$key][0]);
323         else
324             return '';
325     }    else {
326         return maybe_unserialize($post_meta_cache[$blog_id][$post_id][$key]);
327     }
328 }
329
330 function update_post_meta($post_id, $key, $value, $prev_value = '') {
331     global $wpdb, $post_meta_cache, $blog_id;
332
333     $post_id = (int) $post_id;
334
335     $original_value = $value;
336     $value = maybe_serialize($value);
337     $value = $wpdb->escape($value);
338
339     $original_prev = $prev_value;
340     $prev_value = maybe_serialize($prev_value);
341     $prev_value = $wpdb->escape($prev_value);
342
343     if (! $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key = '$key' AND post_id = '$post_id'") ) {
344         return false;
345     }
346
347     if ( empty($prev_value) ) {
348         $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE meta_key = '$key' AND post_id = '$post_id'");
349         $cache_key = $post_meta_cache[$blog_id][$post_id][$key];
350         if ( !empty($cache_key) )
351             foreach ($cache_key as $index => $data)
352                 $post_meta_cache[$blog_id][$post_id][$key][$index] = $original_value;
353     } else {
354         $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE meta_key = '$key' AND post_id = '$post_id' AND meta_value = '$prev_value'");
355         $cache_key = $post_meta_cache[$blog_id][$post_id][$key];
356         if ( !empty($cache_key) )
357             foreach ($cache_key as $index => $data)
358                 if ( $data == $original_prev )
359                     $post_meta_cache[$blog_id][$post_id][$key][$index] = $original_value;
360     }
361
362     return true;
363 }
364
365
366 function get_post_custom($post_id = 0) {
367     global $id, $post_meta_cache, $wpdb, $blog_id;
368
369     if ( !$post_id )
370         $post_id = (int) $id;
371
372     $post_id = (int) $post_id;
373
374     if ( !isset($post_meta_cache[$blog_id][$post_id]) )
375         update_postmeta_cache($post_id);
376
377     return $post_meta_cache[$blog_id][$post_id];
378 }
379
380 function get_post_custom_keys( $post_id = 0 ) {
381     $custom = get_post_custom( $post_id );
382
383     if ( !is_array($custom) )
384         return;
385
386     if ( $keys = array_keys($custom) )
387         return $keys;
388 }
389
390
391 function get_post_custom_values( $key = '', $post_id = 0 ) {
392     $custom = get_post_custom($post_id);
393
394     return $custom[$key];
395 }
396
397 function wp_delete_post($postid = 0) {
398     global $wpdb, $wp_rewrite;
399     $postid = (int) $postid;
400
401     if ( !$post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = $postid") )
402         return $post;
403
404     if ( 'attachment' == $post->post_type )
405         return wp_delete_attachment($postid);
406
407     do_action('delete_post', $postid);
408
409     if ( 'publish' == $post->post_status && 'post' == $post->post_type ) {
410         $categories = wp_get_post_categories($post->ID);
411         if( is_array( $categories ) ) {
412             foreach ( $categories as $cat_id ) {
413                 $wpdb->query("UPDATE $wpdb->categories SET category_count = category_count - 1 WHERE cat_ID = '$cat_id'");
414                 wp_cache_delete($cat_id, 'category');
415                 do_action('edit_category', $cat_id);
416             }
417         }
418     }
419
420     if ( 'page' == $post->post_type )
421         $wpdb->query("UPDATE $wpdb->posts SET post_parent = $post->post_parent WHERE post_parent = $postid AND post_type = 'page'");
422
423     $wpdb->query("UPDATE $wpdb->posts SET post_parent = $post->post_parent WHERE post_parent = $postid AND post_type = 'attachment'");
424
425     $wpdb->query("DELETE FROM $wpdb->posts WHERE ID = $postid");
426
427     $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID = $postid");
428
429     $wpdb->query("DELETE FROM $wpdb->post2cat WHERE post_id = $postid");
430
431     $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = $postid");
432
433     if ( 'page' == $post->post_type ) {
434         clean_page_cache($postid);
435         $wp_rewrite->flush_rules();
436     }
437
438     do_action('deleted_post', $postid);
439     
440     return $post;
441 }
442
443 function wp_get_post_categories($post_id = 0) {
444     $post_id = (int) $post_id;
445
446     $cats = &get_the_category($post_id);
447     $cat_ids = array();
448     foreach ( $cats as $cat )
449         $cat_ids[] = (int) $cat->cat_ID;
450     return array_unique($cat_ids);
451 }
452
453 function wp_get_recent_posts($num = 10) {
454     global $wpdb;
455
456     // Set the limit clause, if we got a limit
457     $num = (int) $num;
458     if ($num) {
459         $limit = "LIMIT $num";
460     }
461
462     $sql = "SELECT * FROM $wpdb->posts WHERE post_type = 'post' ORDER BY post_date DESC $limit";
463     $result = $wpdb->get_results($sql,ARRAY_A);
464
465     return $result?$result:array();
466 }
467
468 function wp_get_single_post($postid = 0, $mode = OBJECT) {
469     global $wpdb;
470
471     $postid = (int) $postid;
472
473     $post = get_post($postid, $mode);
474
475     // Set categories
476     if($mode == OBJECT) {
477         $post->post_category = wp_get_post_categories($postid);
478     }
479     else {
480         $post['post_category'] = wp_get_post_categories($postid);
481     }
482
483     return $post;
484 }
485
486 function wp_insert_post($postarr = array()) {
487     global $wpdb, $wp_rewrite, $allowedtags, $user_ID;
488
489     if ( is_object($postarr) )
490         $postarr = get_object_vars($postarr);
491
492