root/tags/2.1.1/wp-includes/post.php

Revision 4822, 45.6 kB (checked in by markjaquith, 2 years ago)

Make <!--more--> regex non-greedy. Props Curloso and Viper007Bond. fixes #3698

  • 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         if ( $_post = wp_cache_get($post, 'pages') )
109             return get_page($_post, $output);
110         elseif ( isset($post_cache[$blog_id][$post]) )
111             $_post = & $post_cache[$blog_id][$post];
112         else {
113             $query = "SELECT * FROM $wpdb->posts WHERE ID = '$post' LIMIT 1";
114             $_post = & $wpdb->get_row($query);
115             if ( 'page' == $_post->post_type )
116                 return get_page($_post, $output);
117             $post_cache[$blog_id][$post] = & $_post;
118         }
119     }
120
121     if ( defined('WP_IMPORTING') )
122         unset($post_cache[$blog_id]);
123
124     if ( $output == OBJECT ) {
125         return $_post;
126     } elseif ( $output == ARRAY_A ) {
127         return get_object_vars($_post);
128     } elseif ( $output == ARRAY_N ) {
129         return array_values(get_object_vars($_post));
130     } else {
131         return $_post;
132     }
133 }
134
135 // Takes a post ID, returns its mime type.
136 function get_post_mime_type($ID = '') {
137     $post = & get_post($ID);
138
139     if ( is_object($post) )
140         return $post->post_mime_type;
141
142     return false;
143 }
144
145 function get_post_status($ID = '') {
146     $post = get_post($ID);
147
148     if ( is_object($post) ) {
149         if ( ('attachment' == $post->post_type) && $post->post_parent && ($post->ID != $post->post_parent) )
150             return get_post_status($post->post_parent);
151         else
152             return $post->post_status;
153     }
154
155     return false;
156 }
157
158 function get_post_type($post = false) {
159     global $wpdb, $posts;
160
161     if ( false === $post )
162         $post = $posts[0];
163     elseif ( (int) $post )
164         $post = get_post($post, OBJECT);
165
166     if ( is_object($post) )
167         return $post->post_type;
168
169     return false;
170 }
171
172 function get_posts($args) {
173     global $wpdb;
174
175     if ( is_array($args) )
176         $r = &$args;
177     else
178         parse_str($args, $r);
179
180     $defaults = array('numberposts' => 5, 'offset' => 0, 'category' => 0,
181         'orderby' => 'post_date', 'order' => 'DESC', 'include' => '', 'exclude' => '',
182         'meta_key' => '', 'meta_value' =>'', 'post_type' => 'post', 'post_status' => 'publish', 'post_parent' => 0);
183     $r = array_merge($defaults, $r);
184     extract($r);
185     $numberposts = (int) $numberposts;
186     $offset = (int) $offset;
187     $category = (int) $category;
188     $post_parent = (int) $post_parent;
189
190     $inclusions = '';
191     if ( !empty($include) ) {
192         $offset = 0;    //ignore offset, category, exclude, meta_key, and meta_value, post_parent if using include
193         $category = 0;
194         $exclude = '';
195         $meta_key = '';
196         $meta_value = '';
197         $post_parent = 0;
198         $incposts = preg_split('/[\s,]+/',$include);
199         $numberposts = count($incposts);  // only the number of posts included
200         if ( count($incposts) ) {
201             foreach ( $incposts as $incpost ) {
202                 if (empty($inclusions))
203                     $inclusions = ' AND ( ID = ' . intval($incpost) . ' ';
204                 else
205                     $inclusions .= ' OR ID = ' . intval($incpost) . ' ';
206             }
207         }
208     }
209     if (!empty($inclusions))
210         $inclusions .= ')';
211
212     $exclusions = '';
213     if ( !empty($exclude) ) {
214         $exposts = preg_split('/[\s,]+/',$exclude);
215         if ( count($exposts) ) {
216             foreach ( $exposts as $expost ) {
217                 if (empty($exclusions))
218                     $exclusions = ' AND ( ID <> ' . intval($expost) . ' ';
219                 else
220                     $exclusions .= ' AND ID <> ' . intval($expost) . ' ';
221             }
222         }
223     }
224     if (!empty($exclusions))
225         $exclusions .= ')';
226
227     $query ="SELECT DISTINCT * FROM $wpdb->posts " ;
228     $query .= ( empty( $category ) ? "" : ", $wpdb->post2cat " );
229     $query .= ( empty( $meta_key ) ? "" : ", $wpdb->postmeta " );
230     $query .= " WHERE (post_type = 'post' AND post_status = 'publish') $exclusions $inclusions ";
231     $query .= ( empty( $category ) ? "" : "AND ($wpdb->posts.ID = $wpdb->post2cat.post_id AND $wpdb->post2cat.category_id = " . $category. ") " );
232     $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' )" );
233     $query .= " GROUP BY $wpdb->posts.ID ORDER BY " . $orderby . " " . $order . " LIMIT " . $offset . ',' . $numberposts;
234
235     $query  = "SELECT DISTINCT * FROM $wpdb->posts ";
236     $query .= empty( $category ) ? '' : ", $wpdb->post2cat ";
237     $query .= empty( $meta_key ) ? '' : ", $wpdb->postmeta ";
238     $query .= " WHERE 1=1 ";
239     $query .= empty( $post_type ) ? '' : "AND post_type = '$post_type' ";
240     $query .= empty( $post_status ) ? '' : "AND post_status = '$post_status' ";
241     $query .= "$exclusions $inclusions " ;
242     $query .= empty( $category ) ? '' : "AND ($wpdb->posts.ID = $wpdb->post2cat.post_id AND $wpdb->post2cat.category_id = " . $category. ") ";
243     $query .= empty( $post_parent ) ? '' : "AND $wpdb->posts.post_parent = '$post_parent' ";
244     $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' )";
245     $query .= " GROUP BY $wpdb->posts.ID ORDER BY " . $orderby . ' ' . $order;
246     if ( 0 < $numberposts )
247         $query .= " LIMIT " . $offset . ',' . $numberposts;
248
249     $posts = $wpdb->get_results($query);
250
251     update_post_caches($posts);
252
253     return $posts;
254 }
255
256 //
257 // Post meta functions
258 //
259
260 function add_post_meta($post_id, $key, $value, $unique = false) {
261     global $wpdb, $post_meta_cache, $blog_id;
262
263     $post_id = (int) $post_id;
264
265     if ( $unique ) {
266         if ( $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key = '$key' AND post_id = '$post_id'") ) {
267             return false;
268         }
269     }
270
271     $post_meta_cache[$blog_id][$post_id][$key][] = $value;
272
273     $value = maybe_serialize($value);
274     $value = $wpdb->escape($value);
275
276     $wpdb->query("INSERT INTO $wpdb->postmeta (post_id,meta_key,meta_value) VALUES ('$post_id','$key','$value')");
277
278     return true;
279 }
280
281 function delete_post_meta($post_id, $key, $value = '') {
282     global $wpdb, $post_meta_cache, $blog_id;
283
284     $post_id = (int) $post_id;
285
286     if ( empty($value) ) {
287         $meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key'");
288     } else {
289         $meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key' AND meta_value = '$value'");
290     }
291
292     if ( !$meta_id )
293         return false;
294
295     if ( empty($value) ) {
296         $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key'");
297         unset($post_meta_cache[$blog_id][$post_id][$key]);
298     } else {
299         $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key' AND meta_value = '$value'");
300         $cache_key = $post_meta_cache[$blog_id][$post_id][$key];
301         if ($cache_key) foreach ( $cache_key as $index => $data )
302             if ( $data == $value )
303                 unset($post_meta_cache[$blog_id][$post_id][$key][$index]);
304     }
305
306     unset($post_meta_cache[$blog_id][$post_id][$key]);
307
308     return true;
309 }
310
311 function get_post_meta($post_id, $key, $single = false) {
312     global $wpdb, $post_meta_cache, $blog_id;
313
314     $post_id = (int) $post_id;
315
316     if ( isset($post_meta_cache[$blog_id][$post_id][$key]) ) {
317         if ( $single ) {
318             return maybe_unserialize( $post_meta_cache[$blog_id][$post_id][$key][0] );
319         } else {
320             return maybe_unserialize( $post_meta_cache[$blog_id][$post_id][$key] );
321         }
322     }
323
324     if ( !isset($post_meta_cache[$blog_id][$post_id]) )
325         update_postmeta_cache($post_id);
326
327     if ( $single ) {
328         if ( isset($post_meta_cache[$blog_id][$post_id][$key][0]) )
329             return maybe_unserialize($post_meta_cache[$blog_id][$post_id][$key][0]);
330         else
331             return '';
332     }    else {
333         return maybe_unserialize($post_meta_cache[$blog_id][$post_id][$key]);
334     }
335 }
336
337 function update_post_meta($post_id, $key, $value, $prev_value = '') {
338     global $wpdb, $post_meta_cache, $blog_id;
339
340     $post_id = (int) $post_id;
341
342     $original_value = $value;
343     $value = maybe_serialize($value);
344     $value = $wpdb->escape($value);
345
346     $original_prev = $prev_value;
347     $prev_value = maybe_serialize($prev_value);
348     $prev_value = $wpdb->escape($prev_value);
349
350     if (! $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key = '$key' AND post_id = '$post_id'") ) {
351         return false;
352     }
353
354     if ( empty($prev_value) ) {
355         $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE meta_key = '$key' AND post_id = '$post_id'");
356         $cache_key = $post_meta_cache[$blog_id][$post_id][$key];
357         if ( !empty($cache_key) )
358             foreach ($cache_key as $index => $data)
359                 $post_meta_cache[$blog_id][$post_id][$key][$index] = $original_value;
360     } else {
361         $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE meta_key = '$key' AND post_id = '$post_id' AND meta_value = '$prev_value'");
362         $cache_key = $post_meta_cache[$blog_id][$post_id][$key];
363         if ( !empty($cache_key) )
364             foreach ($cache_key as $index => $data)
365                 if ( $data == $original_prev )
366                     $post_meta_cache[$blog_id][$post_id][$key][$index] = $original_value;
367     }
368
369     return true;
370 }
371
372
373 function get_post_custom($post_id = 0) {
374     global $id, $post_meta_cache, $wpdb, $blog_id;
375
376     if ( !$post_id )
377         $post_id = $id;
378
379     $post_id = (int) $post_id;
380
381     if ( !isset($post_meta_cache[$blog_id][$post_id]) )
382         update_postmeta_cache($post_id);
383
384     return $post_meta_cache[$blog_id][$post_id];
385 }
386
387 function get_post_custom_keys( $post_id = 0 ) {
388     $custom = get_post_custom( $post_id );
389
390     if ( !is_array($custom) )
391         return;
392
393     if ( $keys = array_keys($custom) )
394         return $keys;
395 }
396
397
398 function get_post_custom_values( $key = '', $post_id = 0 ) {
399     $custom = get_post_custom($post_id);
400
401     return $custom[$key];
402 }
403
404 function wp_delete_post($postid = 0) {
405     global $wpdb, $wp_rewrite;
406     $postid = (int) $postid;
407
408     if ( !$post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = $postid") )
409         return $post;
410
411     if ( 'attachment' == $post->post_type )
412         return wp_delete_attachment($postid);
413
414     do_action('delete_post', $postid);
415
416     if ( 'publish' == $post->post_status && 'post' == $post->post_type ) {
417         $categories = wp_get_post_categories($post->ID);
418         if( is_array( $categories ) ) {
419             foreach ( $categories as $cat_id ) {
420                 $wpdb->query("UPDATE $wpdb->categories SET category_count = category_count - 1 WHERE cat_ID = '$cat_id'");
421                 wp_cache_delete($cat_id, 'category');
422                 do_action('edit_category', $cat_id);
423             }
424         }
425     }
426
427     if ( 'page' == $post->post_type )
428         $wpdb->query("UPDATE $wpdb->posts SET post_parent = $post->post_parent WHERE post_parent = $postid AND post_type = 'page'");
429
430     $wpdb->query("UPDATE $wpdb->posts SET post_parent = $post->post_parent WHERE post_parent = $postid AND post_type = 'attachment'");
431
432     $wpdb->query("DELETE FROM $wpdb->posts WHERE ID = $postid");
433
434     $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID = $postid");
435
436     $wpdb->query("DELETE FROM $wpdb->post2cat WHERE post_id = $postid");
437
438     $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = $postid");
439
440     if ( 'page' == $post->post_type ) {
441         clean_page_cache($postid);
442         $wp_rewrite->flush_rules();
443     }
444
445     return $post;
446 }
447
448 function wp_get_post_categories($post_id = 0) {
449     $cats = &get_the_category($post_id);
450     $cat_ids = array();
451     foreach ( $cats as $cat )
452         $cat_ids[] = (int) $cat->cat_ID;
453     return array_unique($cat_ids);
454 }
455
456 function wp_get_recent_posts($num = 10) {
457     global $wpdb;
458
459     // Set the limit clause, if we got a limit
460     if ($num) {
461         $limit = "LIMIT $num";
462     }
463
464     $sql = "SELECT * FROM $wpdb->posts WHERE post_type = 'post' ORDER BY post_date DESC $limit";
465     $result = $wpdb->get_results($sql,ARRAY_A);
466
467     return $result?$result:array();
468 }
469
470 function wp_get_single_post($postid = 0, $mode = OBJECT) {
471     global $wpdb;
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     }