1 | <?php |
---|
2 | /** |
---|
3 | * Post functions and post utility function. |
---|
4 | * |
---|
5 | * Warning: The inline documentation for the functions contained |
---|
6 | * in this file might be inaccurate, so the documentation is not |
---|
7 | * authoritative at the moment. |
---|
8 | * |
---|
9 | * @package WordPress |
---|
10 | * @subpackage Post |
---|
11 | * @since 1.5 |
---|
12 | */ |
---|
13 | |
---|
14 | /** |
---|
15 | * Retrieve attached file path based on attachment ID. |
---|
16 | * |
---|
17 | * You can optionally send it through the 'get_attached_file' filter, but by |
---|
18 | * default it will just return the file path unfiltered. |
---|
19 | * |
---|
20 | * The function works by getting the single post meta name, named |
---|
21 | * '_wp_attached_file' and returning it. This is a convenience function to |
---|
22 | * prevent looking up the meta name and provide a mechanism for sending the |
---|
23 | * attached filename through a filter. |
---|
24 | * |
---|
25 | * @package WordPress |
---|
26 | * @subpackage Post |
---|
27 | * @since 2.0 |
---|
28 | * @uses apply_filters() Calls 'get_attached_file' on file path and attachment ID |
---|
29 | * |
---|
30 | * @param int $attachment_id Attachment ID |
---|
31 | * @param bool $unfiltered Whether to apply filters or not |
---|
32 | * @return string The file path to the attached file. |
---|
33 | */ |
---|
34 | function get_attached_file( $attachment_id, $unfiltered = false ) { |
---|
35 | $file = get_post_meta( $attachment_id, '_wp_attached_file', true ); |
---|
36 | if ( $unfiltered ) |
---|
37 | return $file; |
---|
38 | return apply_filters( 'get_attached_file', $file, $attachment_id ); |
---|
39 | } |
---|
40 | |
---|
41 | /** |
---|
42 | * Update attachment file path based on attachment ID. |
---|
43 | * |
---|
44 | * Used to update the file path of the attachment, which uses post meta name |
---|
45 | * '_wp_attached_file' to store the path of the attachment. |
---|
46 | * |
---|
47 | * @package WordPress |
---|
48 | * @subpackage Post |
---|
49 | * @since 2.1 |
---|
50 | * @uses apply_filters() Calls 'update_attached_file' on file path and attachment ID |
---|
51 | * |
---|
52 | * @param int $attachment_id Attachment ID |
---|
53 | * @param string $file File path for the attachment |
---|
54 | * @return bool False on failure, true on success. |
---|
55 | */ |
---|
56 | function update_attached_file( $attachment_id, $file ) { |
---|
57 | if ( !get_post( $attachment_id ) ) |
---|
58 | return false; |
---|
59 | |
---|
60 | $file = apply_filters( 'update_attached_file', $file, $attachment_id ); |
---|
61 | |
---|
62 | return update_post_meta( $attachment_id, '_wp_attached_file', $file ); |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | * Retrieve all children of the post parent ID. |
---|
67 | * |
---|
68 | * Normally, without any enhancements, the children would apply to pages. In the |
---|
69 | * context of the inner workings of WordPress, pages, posts, and attachments |
---|
70 | * share the same table, so therefore the functionality could apply to any one |
---|
71 | * of them. It is then noted that while this function does not work on posts, it |
---|
72 | * does not mean that it won't work on posts. It is recommended that you know |
---|
73 | * what context you wish to retrieve the children of. |
---|
74 | * |
---|
75 | * Attachments may also be made the child of a post, so if that is an accurate |
---|
76 | * statement (which needs to be verified), it would then be possible to get |
---|
77 | * all of the attachments for a post. Attachments have since changed since |
---|
78 | * version 2.5, so this is most likely unaccurate, but serves generally as an |
---|
79 | * example of what is possible. |
---|
80 | * |
---|
81 | * The arguments listed as defaults are for this function and also of the |
---|
82 | * get_posts() function. The arguments are combined with the get_children |
---|
83 | * defaults and are then passed to the get_posts() function, which accepts |
---|
84 | * additional arguments. You can replace the defaults in this function, listed |
---|
85 | * below and the additional arguments listed in the get_posts() function. |
---|
86 | * |
---|
87 | * The 'post_parent' is the most important argument and important attention |
---|
88 | * needs to be paid to the $args parameter. If you pass either an object or an |
---|
89 | * integer (number), then just the 'post_parent' is grabbed and everything else |
---|
90 | * is lost. If you don't specify any arguments, then it is assumed that you are |
---|
91 | * in The Loop and the post parent will be grabbed for from the current post. |
---|
92 | * |
---|
93 | * The 'post_parent' argument is the ID to get the children. The 'numberposts' |
---|
94 | * is the amount of posts to retrieve that has a default of '-1', which is |
---|
95 | * used to get all of the posts. Giving a number higher than 0 will only |
---|
96 | * retrieve that amount of posts. |
---|
97 | * |
---|
98 | * The 'post_type' and 'post_status' arguments can be used to choose what |
---|
99 | * criteria of posts to retrieve. The 'post_type' can be anything, but WordPress |
---|
100 | * post types are 'post', 'pages', and 'attachments'. The 'post_status' |
---|
101 | * argument will accept any post status within the write administration panels. |
---|
102 | * |
---|
103 | * @see get_posts() Has additional arguments that can be replaced. |
---|
104 | * @internal Claims made in the long description might be inaccurate. |
---|
105 | * |
---|
106 | * @package WordPress |
---|
107 | * @subpackage Post |
---|
108 | * @since 2.0 |
---|
109 | * |
---|
110 | * @param mixed $args Optional. User defined arguments for replacing the defaults. |
---|
111 | * @param string $output Optional. Constant for return type, either OBJECT (default), ARRAY_A, ARRAY_N. |
---|
112 | * @return array|bool False on failure and the type will be determined by $output parameter. |
---|
113 | */ |
---|
114 | function &get_children($args = '', $output = OBJECT) { |
---|
115 | if ( empty( $args ) ) { |
---|
116 | if ( isset( $GLOBALS['post'] ) ) { |
---|
117 | $args = array('post_parent' => (int) $GLOBALS['post']->post_parent ); |
---|
118 | } else { |
---|
119 | return false; |
---|
120 | } |
---|
121 | } elseif ( is_object( $args ) ) { |
---|
122 | $args = array('post_parent' => (int) $args->post_parent ); |
---|
123 | } elseif ( is_numeric( $args ) ) { |
---|
124 | $args = array('post_parent' => (int) $args); |
---|
125 | } |
---|
126 | |
---|
127 | $defaults = array( |
---|
128 | 'numberposts' => -1, 'post_type' => '', |
---|
129 | 'post_status' => '', 'post_parent' => 0 |
---|
130 | ); |
---|
131 | |
---|
132 | $r = wp_parse_args( $args, $defaults ); |
---|
133 | |
---|
134 | $children = get_posts( $r ); |
---|
135 | if ( !$children ) |
---|
136 | return false; |
---|
137 | |
---|
138 | update_post_cache($children); |
---|
139 | |
---|
140 | foreach ( $children as $key => $child ) |
---|
141 | $kids[$child->ID] =& $children[$key]; |
---|
142 | |
---|
143 | if ( $output == OBJECT ) { |
---|
144 | return $kids; |
---|
145 | } elseif ( $output == ARRAY_A ) { |
---|
146 | foreach ( $kids as $kid ) |
---|
147 | $weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]); |
---|
148 | return $weeuns; |
---|
149 | } elseif ( $output == ARRAY_N ) { |
---|
150 | foreach ( $kids as $kid ) |
---|
151 | $babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID])); |
---|
152 | return $babes; |
---|
153 | } else { |
---|
154 | return $kids; |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * get_extended() - Get extended entry info (<!--more-->) |
---|
160 | * |
---|
161 | * {@internal Missing Long Description}} |
---|
162 | * |
---|
163 | * @package WordPress |
---|
164 | * @subpackage Post |
---|
165 | * @since 1.0.0 |
---|
166 | * |
---|
167 | * @param string $post {@internal Missing Description}} |
---|
168 | * @return array {@internal Missing Description}} |
---|
169 | */ |
---|
170 | function get_extended($post) { |
---|
171 | //Match the new style more links |
---|
172 | if ( preg_match('/<!--more(.*?)?-->/', $post, $matches) ) { |
---|
173 | list($main, $extended) = explode($matches[0], $post, 2); |
---|
174 | } else { |
---|
175 | $main = $post; |
---|
176 | $extended = ''; |
---|
177 | } |
---|
178 | |
---|
179 | // Strip leading and trailing whitespace |
---|
180 | $main = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $main); |
---|
181 | $extended = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $extended); |
---|
182 | |
---|
183 | return array('main' => $main, 'extended' => $extended); |
---|
184 | } |
---|
185 | |
---|
186 | /** |
---|
187 | * get_post() - Retrieves post data given a post ID or post object. |
---|
188 | * |
---|
189 | * {@internal Missing Long Description}} |
---|
190 | * |
---|
191 | * @package WordPress |
---|
192 | * @subpackage Post |
---|
193 | * @since 1.5.1 |
---|
194 | * @uses $wpdb |
---|
195 | * @link http://codex.wordpress.org/Function_Reference/get_post |
---|
196 | * |
---|
197 | * @param int|object &$post post ID or post object |
---|
198 | * @param string $output {@internal Missing Description}} |
---|
199 | * @param string $filter {@internal Missing Description}} |
---|
200 | * @return mixed {@internal Missing Description}} |
---|
201 | */ |
---|
202 | function &get_post(&$post, $output = OBJECT, $filter = 'raw') { |
---|
203 | global $wpdb; |
---|
204 | $null = null; |
---|
205 | |
---|
206 | if ( empty($post) ) { |
---|
207 | if ( isset($GLOBALS['post']) ) |
---|
208 | $_post = & $GLOBALS['post']; |
---|
209 | else |
---|
210 | return $null; |
---|
211 | } elseif ( is_object($post) ) { |
---|
212 | _get_post_ancestors($post); |
---|
213 | wp_cache_add($post->ID, $post, 'posts'); |
---|
214 | $_post = &$post; |
---|
215 | } else { |
---|
216 | $post = (int) $post; |
---|
217 | if ( ! $_post = wp_cache_get($post, 'posts') ) { |
---|
218 | $_post = & $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post)); |
---|
219 | if ( ! $_post ) |
---|
220 | return $null; |
---|
221 | _get_post_ancestors($_post); |
---|
222 | wp_cache_add($_post->ID, $_post, 'posts'); |
---|
223 | } |
---|
224 | } |
---|
225 | |
---|
226 | $_post = sanitize_post($_post, $filter); |
---|
227 | |
---|
228 | if ( $output == OBJECT ) { |
---|
229 | return $_post; |
---|
230 | } elseif ( $output == ARRAY_A ) { |
---|
231 | $__post = get_object_vars($_post); |
---|
232 | return $__post; |
---|
233 | } elseif ( $output == ARRAY_N ) { |
---|
234 | $__post = array_values(get_object_vars($_post)); |
---|
235 | return $__post; |
---|
236 | } else { |
---|
237 | return $_post; |
---|
238 | } |
---|
239 | } |
---|
240 | |
---|
241 | /** |
---|
242 | * Retrieve ancestors of a post. |
---|
243 | * |
---|
244 | * @package WordPress |
---|
245 | * @subpackage Post |
---|
246 | * @since 2.5 |
---|
247 | * |
---|
248 | * @param int|object $post Post ID or post object |
---|
249 | * @return array Ancestor IDs or empty array if none are found. |
---|
250 | */ |
---|
251 | function get_post_ancestors($post) { |
---|
252 | $post = get_post($post); |
---|
253 | |
---|
254 | if ( !empty($post->ancestors) ) |
---|
255 | return $post->ancestors; |
---|
256 | |
---|
257 | return array(); |
---|
258 | } |
---|
259 | |
---|
260 | /** |
---|
261 | * Retrieve data from a post field based on Post ID. |
---|
262 | * |
---|
263 | * Examples of the post field will be, 'post_type', 'post_status', 'content', |
---|
264 | * etc and based off of the post object property or key names. |
---|
265 | * |
---|
266 | * The context values are based off of the taxonomy filter functions and |
---|
267 | * supported values are found within those functions. |
---|
268 | * |
---|
269 | * @package WordPress |
---|
270 | * @subpackage Post |
---|
271 | * @since 2.3 |
---|
272 | * @uses sanitize_post_field() See for possible $context values. |
---|
273 | * |
---|
274 | * @param string $field Post field name |
---|
275 | * @param id $post Post ID |
---|
276 | * @param string $context Optional. How to filter the field. Default is display. |
---|
277 | * @return WP_Error|string Value in post field or WP_Error on failure |
---|
278 | */ |
---|
279 | function get_post_field( $field, $post, $context = 'display' ) { |
---|
280 | $post = (int) $post; |
---|
281 | $post = get_post( $post ); |
---|
282 | |
---|
283 | if ( is_wp_error($post) ) |
---|
284 | return $post; |
---|
285 | |
---|
286 | if ( !is_object($post) ) |
---|
287 | return ''; |
---|
288 | |
---|
289 | if ( !isset($post->$field) ) |
---|
290 | return ''; |
---|
291 | |
---|
292 | return sanitize_post_field($field, $post->$field, $post->ID, $context); |
---|
293 | } |
---|
294 | |
---|
295 | /** |
---|
296 | * Retrieve the mime type of an attachment based on the ID. |
---|
297 | * |
---|
298 | * This function can be used with any post type, but it makes more sense with |
---|
299 | * attachments. |
---|
300 | * |
---|
301 | * @package WordPress |
---|
302 | * @subpackage Post |
---|
303 | * @since 2.0 |
---|
304 | * |
---|
305 | * @param int $ID Optional. Post ID. |
---|
306 | * @return bool|string False on failure or returns the mime type |
---|
307 | */ |
---|
308 | function get_post_mime_type($ID = '') { |
---|
309 | $post = & get_post($ID); |
---|
310 | |
---|
311 | if ( is_object($post) ) |
---|
312 | return $post->post_mime_type; |
---|
313 | |
---|
314 | return false; |
---|
315 | } |
---|
316 | |
---|
317 | /** |
---|
318 | * Retrieve the post status based on the Post ID. |
---|
319 | * |
---|
320 | * If the post ID is of an attachment, then the parent post status will be given |
---|
321 | * instead. |
---|
322 | * |
---|
323 | * @package WordPress |
---|
324 | * @subpackage Post |
---|
325 | * @since 2.0 |
---|
326 | * |
---|
327 | * @param int $ID Post ID |
---|
328 | * @return string|bool Post status or false on failure. |
---|
329 | */ |
---|
330 | function get_post_status($ID = '') { |
---|
331 | $post = get_post($ID); |
---|
332 | |
---|
333 | if ( is_object($post) ) { |
---|
334 | if ( ('attachment' == $post->post_type) && $post->post_parent && ($post->ID != $post->post_parent) ) |
---|
335 | return get_post_status($post->post_parent); |
---|
336 | else |
---|
337 | return $post->post_status; |
---|
338 | } |
---|
339 | |
---|
340 | return false; |
---|
341 | } |
---|
342 | |
---|
343 | /** |
---|
344 | * Retrieve all of the WordPress supported post statuses. |
---|
345 | * |
---|
346 | * Posts have a limited set of valid status values, this provides the |
---|
347 | * post_status values and descriptions. |
---|
348 | * |
---|
349 | * @package WordPress |
---|
350 | * @subpackage Post |
---|
351 | * @since 2.5 |
---|
352 | * |
---|
353 | * @return array List of post statuses. |
---|
354 | */ |
---|
355 | function get_post_statuses( ) { |
---|
356 | $status = array( |
---|
357 | 'draft' => __('Draft'), |
---|
358 | 'pending' => __('Pending Review'), |
---|
359 | 'private' => __('Private'), |
---|
360 | 'publish' => __('Published') |
---|
361 | ); |
---|
362 | |
---|
363 | return $status; |
---|
364 | } |
---|
365 | |
---|
366 | /** |
---|
367 | * Retrieve all of the WordPress support page statuses. |
---|
368 | * |
---|
369 | * Pages have a limited set of valid status values, this provides the |
---|
370 | * post_status values and descriptions. |
---|
371 | * |
---|
372 | * @package WordPress |
---|
373 | * @subpackage Page |
---|
374 | * @since 2.5 |
---|
375 | * |
---|
376 | * @return array List of page statuses. |
---|
377 | */ |
---|
378 | function get_page_statuses( ) { |
---|
379 | $status = array( |
---|
380 | 'draft' => __('Draft'), |
---|
381 | 'private' => __('Private'), |
---|
382 | 'publish' => __('Published') |
---|
383 | ); |
---|
384 | |
---|
385 | return $status; |
---|
386 | } |
---|
387 | |
---|
388 | /** |
---|
389 | * get_post_type() - Returns post type |
---|
390 | * |
---|
391 | * {@internal Missing Long Description}} |
---|
392 | * |
---|
393 | * @package WordPress |
---|
394 | * @subpackage Post |
---|
395 | * @since 2.1 |
---|
396 | * |
---|
397 | * @uses $wpdb |
---|
398 | * @uses $posts {@internal Missing Description}} |
---|
399 | * |
---|
400 | * @param mixed $post post object or post ID |
---|
401 | * @return mixed post type or false |
---|
402 | */ |
---|
403 | function get_post_type($post = false) { |
---|
404 | global $posts; |
---|
405 | |
---|
406 | if ( false === $post ) |
---|
407 | $post = $posts[0]; |
---|
408 | elseif ( (int) $post ) |
---|
409 | $post = get_post($post, OBJECT); |
---|
410 | |
---|
411 | if ( is_object($post) ) |
---|
412 | return $post->post_type; |
---|
413 | |
---|
414 | return false; |
---|
415 | } |
---|
416 | |
---|
417 | /** |
---|
418 | * set_post_type() - Set post type |
---|
419 | * |
---|
420 | * {@internal Missing Long Description}} |
---|
421 | * |
---|
422 | * @package WordPress |
---|
423 | * @subpackage Post |
---|
424 | * @since 2.5 |
---|
425 | * |
---|
426 | * @uses $wpdb |
---|
427 | * @uses $posts {@internal Missing Description}} |
---|
428 | * |
---|
429 | * @param mixed $post_id post ID |
---|
430 | * @param mixed post type |
---|
431 | * @return bool {@internal Missing Description}} |
---|
432 | */ |
---|
433 | function set_post_type( $post_id = 0, $post_type = 'post' ) { |
---|
434 | global $wpdb; |
---|
435 | |
---|
436 | $post_type = sanitize_post_field('post_type', $post_type, $post_id, 'db'); |
---|
437 | $return = $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_type = %s WHERE ID = %d", $post_type, $post_id) ); |
---|
438 | |
---|
439 | if ( 'page' == $post_type ) |
---|
440 | clean_page_cache($post_id); |
---|
441 | else |
---|
442 | clean_post_cache($post_id); |
---|
443 | |
---|
444 | return $return; |
---|
445 | } |
---|
446 | |
---|
447 | /** |
---|
448 | * get_posts() - Returns a number of posts |
---|
449 | * |
---|
450 | * {@internal Missing Long Description}} |
---|
451 | * |
---|
452 | * @package WordPress |
---|
453 | * @subpackage Post |
---|
454 | * @since 1.2 |
---|
455 | * @uses $wpdb |
---|
456 | * @link http://codex.wordpress.org/Template_Tags/get_posts |
---|
457 | * |
---|
458 | * @param array $args {@internal Missing Description}} |
---|
459 | * @return array {@internal Missing Description}} |
---|
460 | */ |
---|
461 | function get_posts($args = null) { |
---|
462 | $defaults = array( |
---|
463 | 'numberposts' => 5, 'offset' => 0, |
---|
464 | 'category' => 0, 'orderby' => 'post_date', |
---|
465 | 'order' => 'DESC', 'include' => '', |
---|
466 | 'exclude' => '', 'meta_key' => '', |
---|
467 | 'meta_value' =>'', 'post_type' => 'post', |
---|
468 | 'post_parent' => 0, 'suppress_filters' => true |
---|
469 | ); |
---|
470 | |
---|
471 | $r = wp_parse_args( $args, $defaults ); |
---|
472 | if ( empty( $r['post_status'] ) ) |
---|
473 | $r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish'; |
---|
474 | if ( ! empty($r['numberposts']) ) |
---|
475 | $r['posts_per_page'] = $r['numberposts']; |
---|
476 | if ( ! empty($r['category']) ) |
---|
477 | $r['cat'] = $r['category']; |
---|
478 | if ( ! empty($r['include']) ) { |
---|
479 | $incposts = preg_split('/[\s,]+/',$r['include']); |
---|
480 | $r['posts_per_page'] = count($incposts); // only the number of posts included |
---|
481 | $r['post__in'] = $incposts; |
---|
482 | } elseif ( ! empty($r['exclude']) ) |
---|
483 | $r['post__not_in'] = preg_split('/[\s,]+/',$r['exclude']); |
---|
484 | |
---|
485 | $get_posts = new WP_Query; |
---|
486 | return $get_posts->query($r); |
---|
487 | |
---|
488 | } |
---|
489 | |
---|
490 | // |
---|
491 | // Post meta functions |
---|
492 | // |
---|
493 | |
---|
494 | /** |
---|
495 | * add_post_meta() - adds metadata for post |
---|
496 | * |
---|
497 | * {@internal Missing Long Description}} |
---|
498 | * |
---|
499 | * @package WordPress |
---|
500 | * @subpackage Post |
---|
501 | * @since 1.5 |
---|
502 | * @uses $wpdb |
---|
503 | * @link http://codex.wordpress.org/Function_Reference/add_post_meta |
---|
504 | * |
---|
505 | * @param int $post_id post ID |
---|
506 | * @param string $key {@internal Missing Description}} |
---|
507 | * @param mixed $value {@internal Missing Description}} |
---|
508 | * @param bool $unique whether to check for a value with the same key |
---|
509 | * @return bool {@internal Missing Description}} |
---|
510 | */ |
---|
511 | function add_post_meta($post_id, $meta_key, $meta_value, $unique = false) { |
---|
512 | global $wpdb; |
---|
513 | |
---|
514 | // make sure meta is added to the post, not a revision |
---|
515 | if ( $the_post = wp_is_post_revision($post_id) ) |
---|
516 | $post_id = $the_post; |
---|
517 | |
---|
518 | // expected_slashed ($meta_key) |
---|
519 | $meta_key = stripslashes($meta_key); |
---|
520 | |
---|
521 | if ( $unique && $wpdb->get_var( $wpdb->prepare( "SELECT meta_key FROM $wpdb->postmeta WHERE meta_key = %s AND post_id = %d", $meta_key, $post_id ) ) ) |
---|
522 | return false; |
---|
523 | |
---|
524 | $meta_value = maybe_serialize($meta_value); |
---|
525 | |
---|
526 | $wpdb->insert( $wpdb->postmeta, compact( 'post_id', 'meta_key', 'meta_value' ) ); |
---|
527 | |
---|
528 | wp_cache_delete($post_id, 'post_meta'); |
---|
529 | |
---|
530 | return true; |
---|
531 | } |
---|
532 | |
---|
533 | /** |
---|
534 | * delete_post_meta() - delete post metadata |
---|
535 | * |
---|
536 | * {@internal Missing Long Description}} |
---|
537 | * |
---|
538 | * @package WordPress |
---|
539 | * @subpackage Post |
---|
540 | * @since 1.5 |
---|
541 | * @uses $wpdb |
---|
542 | * @link http://codex.wordpress.org/Function_Reference/delete_post_meta |
---|
543 | * |
---|
544 | * @param int $post_id post ID |
---|
545 | * @param string $key {@internal Missing Description}} |
---|
546 | * @param mixed $value {@internal Missing Description}} |
---|
547 | * @return bool {@internal Missing Description}} |
---|
548 | */ |
---|
549 | function delete_post_meta($post_id, $key, $value = '') { |
---|
550 | global $wpdb; |
---|
551 | |
---|
552 | // make sure meta is added to the post, not a revision |
---|
553 | if ( $the_post = wp_is_post_revision($post_id) ) |
---|
554 | $post_id = $the_post; |
---|
555 | |
---|
556 | // expected_slashed ($key, $value) |
---|
557 | $key = stripslashes( $key ); |
---|
558 | $value = stripslashes( $value ); |
---|
559 | |
---|
560 | if ( empty( $value ) ) |
---|
561 | $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s", $post_id, $key ) ); |
---|
562 | else |
---|
563 | $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s AND meta_value = %s", $post_id, $key, $value ) ); |
---|
564 | |
---|
565 | if ( !$meta_id ) |
---|
566 | return false; |
---|
567 | |
---|
568 | if ( empty( $value ) ) |
---|
569 | $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s", $post_id, $key ) ); |
---|
570 | else |
---|
571 | $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s AND meta_value = %s", $post_id, $key, $value ) ); |
---|
572 | |
---|
573 | wp_cache_delete($post_id, 'post_meta'); |
---|
574 | |
---|
575 | return true; |
---|
576 | } |
---|
577 | |
---|
578 | /** |
---|
579 | * get_post_meta() - Get a post meta field |
---|
580 | * |
---|
581 | * {@internal Missing Long Description}} |
---|
582 | * |
---|
583 | * @package WordPress |
---|
584 | * @subpackage Post |
---|
585 | * @since 1.5 |
---|
586 | * @uses $wpdb |
---|
587 | * @link http://codex.wordpress.org/Function_Reference/get_post_meta |
---|
588 | * |
---|
589 | * @param int $post_id post ID |
---|
590 | * @param string $key The meta key to retrieve |
---|
591 | * @param bool $single Whether to return a single value |
---|
592 | * @return mixed {@internal Missing Description}} |
---|
593 | */ |
---|
594 | function get_post_meta($post_id, $key, $single = false) { |
---|
595 | $post_id = (int) $post_id; |
---|
596 | |
---|
597 | $meta_cache = wp_cache_get($post_id, 'post_meta'); |
---|
598 | |
---|
599 | if ( !$meta_cache ) { |
---|
600 | update_postmeta_cache($post_id); |
---|
601 | $meta_cache = wp_cache_get($post_id, 'post_meta'); |
---|
602 | } |
---|
603 | |
---|
604 | if ( isset($meta_cache[$key]) ) { |
---|
605 | if ( $single ) { |
---|
606 | return maybe_unserialize( $meta_cache[$key][0] ); |
---|
607 | } else { |
---|
608 | return array_map('maybe_unserialize', $meta_cache[$key]); |
---|
609 | } |
---|
610 | } |
---|
611 | |
---|
612 | return ''; |
---|
613 | } |
---|
614 | |
---|
615 | /** |
---|
616 | * update_post_meta() - Update a post meta field |
---|
617 | * |
---|
618 | * {@internal Missing Long Description}} |
---|
619 | * |
---|
620 | * @package WordPress |
---|
621 | * @subpackage Post |
---|
622 | * @since 1.5 |
---|
623 | * @uses $wpdb |
---|
624 | * @link http://codex.wordpress.org/Function_Reference/update_post_meta |
---|
625 | * |
---|
626 | * @param int $post_id post ID |
---|
627 | * @param string $key {@internal Missing Description}} |
---|
628 | * @param mixed $value {@internal Missing Description}} |
---|
629 | * @param mixed $prev_value previous value (for differentiating between meta fields with the same key and post ID) |
---|
630 | * @return bool {@internal Missing Description}} |
---|
631 | */ |
---|
632 | function update_post_meta($post_id, $meta_key, $meta_value, $prev_value = '') { |
---|
633 | global $wpdb; |
---|
634 | |
---|
635 | // make sure meta is added to the post, not a revision |
---|
636 | if ( $the_post = wp_is_post_revision($post_id) ) |
---|
637 | $post_id = $the_post; |
---|
638 | |
---|
639 | // expected_slashed ($meta_key) |
---|
640 | $meta_key = stripslashes($meta_key); |
---|
641 | |
---|
642 | if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT meta_key FROM $wpdb->postmeta WHERE meta_key = %s AND post_id = %d", $meta_key, $post_id ) ) ) { |
---|
643 | return add_post_meta($post_id, $meta_key, $meta_value); |
---|
644 | } |
---|
645 | |
---|
646 | $meta_value = maybe_serialize($meta_value); |
---|
647 | |
---|
648 | $data = compact( 'meta_value' ); |
---|
649 | $where = compact( 'meta_key', 'post_id' ); |
---|
650 | |
---|
651 | if ( !empty( $prev_value ) ) { |
---|
652 | $prev_value = maybe_serialize($prev_value); |
---|
653 | $where['meta_value'] = $prev_value; |
---|
654 | } |
---|
655 | |
---|
656 | $wpdb->update( $wpdb->postmeta, $data, $where ); |
---|
657 | wp_cache_delete($post_id, 'post_meta'); |
---|
658 | return true; |
---|
659 | } |
---|
660 | |
---|
661 | /** |
---|
662 | * delete_post_meta_by_key() - Delete everything from post meta matching $post_meta_key |
---|
663 | * |
---|
664 | * @package WordPress |
---|
665 | * @subpackage Post |
---|
666 | * @since 2.3 |
---|
667 | * @uses $wpdb |
---|
668 | * |
---|
669 | * @param string $post_meta_key What to search for when deleting |
---|
670 | * @return bool Whether the post meta key was deleted from the database |
---|
671 | */ |
---|
672 | function delete_post_meta_by_key($post_meta_key) { |
---|
673 | global $wpdb; |
---|
674 | if ( $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_key = %s", $post_meta_key)) ) { |
---|
675 | /** @todo Get post_ids and delete cache */ |
---|
676 | // wp_cache_delete($post_id, 'post_meta'); |
---|
677 | return true; |
---|
678 | } |
---|
679 | return false; |
---|
680 | } |
---|
681 | |
---|
682 | /** |
---|
683 | * get_post_custom() - Retrieve post custom fields |
---|
684 | * |
---|
685 | * {@internal Missing Long Description}} |
---|
686 | * |
---|
687 | * @package WordPress |
---|
688 | * @subpackage Post |
---|
689 | * @since 1.2 |
---|
690 | * @link http://codex.wordpress.org/Function_Reference/get_post_custom |
---|
691 | * |
---|
692 | * @uses $id |
---|
693 | * @uses $wpdb |
---|
694 | * |
---|
695 | * @param int $post_id post ID |
---|
696 | * @return array {@internal Missing Description}} |
---|
697 | */ |
---|
698 | function get_post_custom($post_id = 0) { |
---|
699 | global $id; |
---|
700 | |
---|
701 | if ( !$post_id ) |
---|
702 | $post_id = (int) $id; |
---|
703 | |
---|
704 | $post_id = (int) $post_id; |
---|
705 | |
---|
706 | if ( ! wp_cache_get($post_id, 'post_meta') ) |
---|
707 | update_postmeta_cache($post_id); |
---|
708 | |
---|
709 | return wp_cache_get($post_id, 'post_meta'); |
---|
710 | } |
---|
711 | |
---|
712 | /** |
---|
713 | * get_post_custom_keys() - Retrieve post custom field names |
---|
714 | * |
---|
715 | * @package WordPress |
---|
716 | * @subpackage Post |
---|
717 | * @since 1.2 |
---|
718 | * @link http://codex.wordpress.org/Function_Reference/get_post_custom_keys |
---|
719 | * |
---|
720 | * @param int $post_id post ID |
---|
721 | * @return array|null Either array of the keys, or null if keys would not be retrieved |
---|
722 | */ |
---|
723 | function get_post_custom_keys( $post_id = 0 ) { |
---|
724 | $custom = get_post_custom( $post_id ); |
---|
725 | |
---|
726 | if ( !is_array($custom) ) |
---|
727 | return; |
---|
728 | |
---|
729 | if ( $keys = array_keys($custom) ) |
---|
730 | return $keys; |
---|
731 | } |
---|
732 | |
---|
733 | /** |
---|
734 | * get_post_custom_values() - Retrieve values for a custom post field |
---|
735 | * |
---|
736 | * @package WordPress |
---|
737 | * @subpackage Post |
---|
738 | * @since 1.2 |
---|
739 | * @link http://codex.wordpress.org/Function_Reference/get_post_custom_values |
---|
740 | * |
---|
741 | * @param string $key field name |
---|
742 | * @param int $post_id post ID |
---|
743 | * @return mixed {@internal Missing Description}} |
---|
744 | */ |
---|
745 | function get_post_custom_values( $key = '', $post_id = 0 ) { |
---|
746 | $custom = get_post_custom($post_id); |
---|
747 | |
---|
748 | return $custom[$key]; |
---|
749 | } |
---|
750 | |
---|
751 | /** |
---|
752 | * sanitize_post() - Sanitize every post field |
---|
753 | * |
---|
754 | * {@internal Missing Long Description}} |
---|
755 | * |
---|
756 | * @package WordPress |
---|
757 | * @subpackage Post |
---|
758 | * @since 2.3 |
---|
759 | * |
---|
760 | * @param object|array $post The Post Object or Array |
---|
761 | * @param string $context How to sanitize post fields |
---|
762 | * @return object|array The now sanitized Post Object or Array (will be the same type as $post) |
---|
763 | */ |
---|
764 | function sanitize_post($post, $context = 'display') { |
---|
765 | if ( 'raw' == $context ) |
---|
766 | return $post; |
---|
767 | if ( is_object($post) ) { |
---|
768 | foreach ( array_keys(get_object_vars($post)) as $field ) |
---|
769 | $post->$field = sanitize_post_field($field, $post->$field, $post->ID, $context); |
---|
770 | } else { |
---|
771 | foreach ( array_keys($post) as $field ) |
---|
772 | $post[$field] = sanitize_post_field($field, $post[$field], $post['ID'], $context); |
---|
773 | } |
---|
774 | return $post; |
---|
775 | } |
---|
776 | |
---|
777 | /** |
---|
778 | * sanitize_post_field() - Sanitize post field based on context |
---|
779 | * |
---|
780 | * {@internal Missing Long Description}} |
---|
781 | * |
---|
782 | * @package WordPress |
---|
783 | * @subpackage Post |
---|
784 | * @since 2.3 |
---|
785 | * |
---|
786 | * @param string $field The Post Object field name |
---|
787 | * @param string $value The Post Object value |
---|
788 | * @param int $postid Post ID |
---|
789 | * @param string $context How to sanitize post fields |
---|
790 | * @return string Sanitized value |
---|
791 | */ |
---|
792 | function sanitize_post_field($field, $value, $post_id, $context) { |
---|
793 | $int_fields = array('ID', 'post_parent', 'menu_order'); |
---|
794 | if ( in_array($field, $int_fields) ) |
---|
795 | $value = (int) $value; |
---|
796 | |
---|
797 | if ( 'raw' == $context ) |
---|
798 | return $value; |
---|
799 | |
---|
800 | $prefixed = false; |
---|
801 | if ( false !== strpos($field, 'post_') ) { |
---|
802 | $prefixed = true; |
---|
803 | $field_no_prefix = str_replace('post_', '', $field); |
---|
804 | } |
---|
805 | |
---|
806 | if ( 'edit' == $context ) { |
---|
807 | $format_to_edit = array('post_content', 'post_excerpt', 'post_title', 'post_password'); |
---|
808 | |
---|
809 | if ( $prefixed ) { |
---|
810 | $value = apply_filters("edit_$field", $value, $post_id); |
---|
811 | // Old school |
---|
812 | $value = apply_filters("${field_no_prefix}_edit_pre", $value, $post_id); |
---|
813 | } else { |
---|
814 | $value = apply_filters("edit_post_$field", $value, $post_id); |
---|
815 | } |
---|
816 | |
---|
817 | if ( in_array($field, $format_to_edit) ) { |
---|
818 | if ( 'post_content' == $field ) |
---|
819 | $value = format_to_edit($value, user_can_richedit()); |
---|
820 | else |
---|
821 | $value = format_to_edit($value); |
---|
822 | } else { |
---|
823 | $value = attribute_escape($value); |
---|
824 | } |
---|
825 | } else if ( 'db' == $context ) { |
---|
826 | if ( $prefixed ) { |
---|
827 | $value = apply_filters("pre_$field", $value); |
---|
828 | $value = apply_filters("${field_no_prefix}_save_pre", $value); |
---|
829 | } else { |
---|
830 | $value = apply_filters("pre_post_$field", $value); |
---|
831 | $value = apply_filters("${field}_pre", $value); |
---|
832 | } |
---|
833 | } else { |
---|
834 | // Use display filters by default. |
---|
835 | if ( $prefixed ) |
---|
836 | $value = apply_filters($field, $value, $post_id, $context); |
---|
837 | else |
---|
838 | $value = apply_filters("post_$field", $value, $post_id, $context); |
---|
839 | } |
---|
840 | |
---|
841 | if ( 'attribute' == $context ) |
---|
842 | $value = attribute_escape($value); |
---|
843 | else if ( 'js' == $context ) |
---|
844 | $value = js_escape($value); |
---|
845 | |
---|
846 | return $value; |
---|
847 | } |
---|
848 | |
---|
849 | /** |
---|
850 | * Count number of posts of a post type and is user has permissions to view. |
---|
851 | * |
---|
852 | * This function provides an efficient method of finding the amount of post's |
---|
853 | * type a blog has. Another method is to count the amount of items in |
---|
854 | * get_posts(), but that method has a lot of overhead with doing so. Therefore, |
---|
855 | * when developing for 2.5+, use this function instead. |
---|
856 | * |
---|
857 | * The $perm parameter checks for 'readable' value and if the user can read |
---|
858 | * private posts, it will display that for the user that is signed in. |
---|
859 | * |
---|
860 | * @package WordPress |
---|
861 | * @subpackage Post |
---|
862 | * @since 2.5 |
---|
863 | * @link http://codex.wordpress.org/Template_Tags/wp_count_posts |
---|
864 | * |
---|
865 | * @param string $type Optional. Post type to retrieve count |
---|
866 | * @param string $perm Optional. 'readable' or empty. |
---|
867 | * @return object Number of posts for each status |
---|
868 | */ |
---|
869 | function wp_count_posts( $type = 'post', $perm = '' ) { |
---|
870 | global $wpdb; |
---|
871 | |
---|
872 | $user = wp_get_current_user(); |
---|
873 | |
---|
874 | $cache_key = $type; |
---|
875 | |
---|
876 | $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s"; |
---|
877 | if ( 'readable' == $perm && is_user_logged_in() ) { |
---|
878 | if ( !current_user_can("read_private_{$type}s") ) { |
---|
879 | $cache_key .= '_' . $perm . '_' . $user->ID; |
---|
880 | $query .= " AND (post_status != 'private' OR ( post_author = '$user->ID' AND post_status = 'private' ))"; |
---|
881 | } |
---|
882 | } |
---|
883 | $query .= ' GROUP BY post_status'; |
---|
884 | |
---|
885 | $count = wp_cache_get($cache_key, 'counts'); |
---|
886 | if ( false !== $count ) |
---|
887 | return $count; |
---|
888 | |
---|
889 | $count = $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A ); |
---|
890 | |
---|
891 | $stats = array( ); |
---|
892 | foreach( (array) $count as $row_num => $row ) { |
---|
893 | $stats[$row['post_status']] = $row['num_posts']; |
---|
894 | } |
---|
895 | |
---|
896 | $stats = (object) $stats; |
---|
897 | wp_cache_set($cache_key, $stats, 'counts'); |
---|
898 | |
---|
899 | return $stats; |
---|
900 | } |
---|
901 | |
---|
902 | |
---|
903 | /** |
---|
904 | * wp_count_attachments() - Count number of attachments |
---|
905 | * |
---|
906 | * {@internal Missing Long Description}} |
---|
907 | * |
---|
908 | * @package WordPress |
---|
909 | * @subpackage Post |
---|
910 | * @since 2.5 |
---|
911 | * |
---|
912 | * @param string|array $post_mime_type Array or comma-separated list of MIME patterns |
---|
913 | * @return array Number of posts for each post_mime_type |
---|
914 | */ |
---|
915 | |
---|
916 | function wp_count_attachments( $mime_type = '' ) { |
---|
917 | global $wpdb; |
---|
918 | |
---|
919 | $and = wp_post_mime_type_where( $mime_type ); |
---|
920 | $count = $wpdb->get_results( "SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' $and GROUP BY post_mime_type", ARRAY_A ); |
---|
921 | |
---|
922 | $stats = array( ); |
---|
923 | foreach( (array) $count as $row ) { |
---|
924 | $stats[$row['post_mime_type']] = $row['num_posts']; |
---|
925 | } |
---|
926 | |
---|
927 | return (object) $stats; |
---|
928 | } |
---|
929 | |
---|
930 | /** |
---|
931 | * wp_match_mime_type() - Check a MIME-Type against a list |
---|
932 | * |
---|
933 | * {@internal Missing Long Description}} |
---|
934 | * |
---|
935 | * @package WordPress |
---|
936 | * @subpackage Post |
---|
937 | * @since 2.5 |
---|
938 | * |
---|
939 | * @param string|array $wildcard_mime_types e.g. audio/mpeg or image (same as image/*) or flash (same as *flash*) |
---|
940 | * @param string|array $real_mime_types post_mime_type values |
---|
941 | * @return array array(wildcard=>array(real types)) |
---|
942 | */ |
---|
943 | function wp_match_mime_types($wildcard_mime_types, $real_mime_types) { |
---|
944 | $matches = array(); |
---|
945 | if ( is_string($wildcard_mime_types) ) |
---|
946 | $wildcard_mime_types = array_map('trim', explode(',', $wildcard_mime_types)); |
---|
947 | if ( is_string($real_mime_types) ) |
---|
948 | $real_mime_types = array_map('trim', explode(',', $real_mime_types)); |
---|
949 | $wild = '[-._a-z0-9]*'; |
---|
950 | foreach ( (array) $wildcard_mime_types as $type ) { |
---|
951 | $type = str_replace('*', $wild, $type); |
---|
952 | $patternses[1][$type] = "^$type$"; |
---|
953 | if ( false === strpos($type, '/') ) { |
---|
954 | $patternses[2][$type] = "^$type/"; |
---|
955 | $patternses[3][$type] = $type; |
---|
956 | } |
---|
957 | } |
---|
958 | asort($patternses); |
---|
959 | foreach ( $patternses as $patterns ) |
---|
960 | foreach ( $patterns as $type => $pattern ) |
---|
961 | foreach ( (array) $real_mime_types as $real ) |
---|
962 | if ( preg_match("#$pattern#", $real) && ( empty($matches[$type]) || false === array_search($real, $matches[$type]) ) ) |
---|
963 | $matches[$type][] = $real; |
---|
964 | return $matches; |
---|
965 | } |
---|
966 | |
---|
967 | /** |
---|
968 | * wp_get_post_mime_type_where() - Convert MIME types into SQL |
---|
969 | * |
---|
970 | * @package WordPress |
---|
971 | * @subpackage Post |
---|
972 | * @since 2.5 |
---|
973 | * |
---|
974 | * @param string|array $mime_types MIME types |
---|
975 | * @return string SQL AND clause |
---|
976 | */ |
---|
977 | function wp_post_mime_type_where($post_mime_types) { |
---|
978 | $where = ''; |
---|
979 | $wildcards = array('', '%', '%/%'); |
---|
980 | if ( is_string($post_mime_types) ) |
---|
981 | $post_mime_types = array_map('trim', explode(',', $post_mime_types)); |
---|
982 | foreach ( (array) $post_mime_types as $mime_type ) { |
---|
983 | $mime_type = preg_replace('/\s/', '', $mime_type); |
---|
984 | $slashpos = strpos($mime_type, '/'); |
---|
985 | if ( false !== $slashpos ) { |
---|
986 | $mime_group = preg_replace('/[^-*.a-zA-Z0-9]/', '', substr($mime_type, 0, $slashpos)); |
---|
987 | $mime_subgroup = preg_replace('/[^-*.a-zA-Z0-9]/', '', substr($mime_type, $slashpos + 1)); |
---|
988 | if ( empty($mime_subgroup) ) |
---|
989 | $mime_subgroup = '*'; |
---|
990 | else |
---|
991 | $mime_subgroup = str_replace('/', '', $mime_subgroup); |
---|
992 | $mime_pattern = "$mime_group/$mime_subgroup"; |
---|
993 | } else { |
---|
994 | $mime_pattern = preg_replace('/[^-*.a-zA-Z0-9]/', '', $mime_type); |
---|
995 | if ( false === strpos($mime_pattern, '*') ) |
---|
996 | $mime_pattern .= '/*'; |
---|
997 | } |
---|
998 | |
---|
999 | $mime_pattern = preg_replace('/\*+/', '%', $mime_pattern); |
---|
1000 | |
---|
1001 | if ( in_array( $mime_type, $wildcards ) ) |
---|
1002 | return ''; |
---|
1003 | |
---|
1004 | if ( false !== strpos($mime_pattern, '%') ) |
---|
1005 | $wheres[] = "post_mime_type LIKE '$mime_pattern'"; |
---|
1006 | else |
---|
1007 | $wheres[] = "post_mime_type = '$mime_pattern'"; |
---|
1008 | } |
---|
1009 | if ( !empty($wheres) ) |
---|
1010 | $where = ' AND (' . join(' OR ', $wheres) . ') '; |
---|
1011 | return $where; |
---|
1012 | } |
---|
1013 | |
---|
1014 | /** |
---|
1015 | * wp_delete_post() - Deletes a Post |
---|
1016 | * |
---|
1017 | * {@internal Missing Long Description}} |
---|
1018 | * |
---|
1019 | * @package WordPress |
---|
1020 | * @subpackage Post |
---|
1021 | * @since 1.0.0 |
---|
1022 | * |
---|
1023 | * @param int $postid post ID |
---|
1024 | * @return mixed {@internal Missing Description}} |
---|
1025 | */ |
---|
1026 | function wp_delete_post($postid = 0) { |
---|
1027 | global $wpdb, $wp_rewrite; |
---|
1028 | |
---|
1029 | if ( !$post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $postid)) ) |
---|
1030 | return $post; |
---|
1031 | |
---|
1032 | if ( 'attachment' == $post->post_type ) |
---|
1033 | return wp_delete_attachment($postid); |
---|
1034 | |
---|
1035 | do_action('delete_post', $postid); |
---|
1036 | |
---|
1037 | /** @todo delete for pluggable post taxonomies too */ |
---|
1038 | wp_delete_object_term_relationships($postid, array('category', 'post_tag')); |
---|
1039 | |
---|
1040 | $parent_data = array( 'post_parent' => $post->post_parent ); |
---|
1041 | $parent_where = array( 'post_parent' => $postid ); |
---|
1042 | |
---|
1043 | if ( 'page' == $post->post_type) { |
---|
1044 | // if the page is defined in option page_on_front or post_for_posts, |
---|
1045 | // adjust the corresponding options |
---|
1046 | if ( get_option('page_on_front') == $postid ) { |
---|
1047 | update_option('show_on_front', 'posts'); |
---|
1048 | delete_option('page_on_front'); |
---|
1049 | } |
---|
1050 | if ( get_option('page_for_posts') == $postid ) { |
---|
1051 | delete_option('page_for_posts'); |
---|
1052 | } |
---|
1053 | |
---|
1054 | // Point children of this page to its parent, also clean the cache of affected children |
---|
1055 | $children_query = $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_parent = %d AND post_type='page'", $postid); |
---|
1056 | $children = $wpdb->get_results($children_query); |
---|
1057 | |
---|
1058 | $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'page' ) ); |
---|
1059 | } |
---|
1060 | |
---|
1061 | // Do raw query. wp_get_post_revisions() is filtered |
---|
1062 | $revision_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'revision'", $postid ) ); |
---|
1063 | // Use wp_delete_post (via wp_delete_post_revision) again. Ensures any meta/misplaced data gets cleaned up. |
---|
1064 | foreach ( $revision_ids as $revision_id ) |
---|
1065 | wp_delete_post_revision( $revision_id ); |
---|
1066 | |
---|
1067 | // Point all attachments to this post up one level |
---|
1068 | $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'attachment' ) ); |
---|
1069 | |
---|
1070 | $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->posts WHERE ID = %d", $postid )); |
---|
1071 | |
---|
1072 | $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->comments WHERE comment_post_ID = %d", $postid )); |
---|
1073 | |
---|
1074 | $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE post_id = %d", $postid )); |
---|
1075 | |
---|
1076 | if ( 'page' == $post->post_type ) { |
---|
1077 | clean_page_cache($postid); |
---|
1078 | |
---|
1079 | foreach ( (array) $children as $child ) |
---|
1080 | clean_page_cache($child->ID); |
---|
1081 | |
---|
1082 | $wp_rewrite->flush_rules(); |
---|
1083 | } else { |
---|
1084 | clean_post_cache($postid); |
---|
1085 | } |
---|
1086 | |
---|
1087 | do_action('deleted_post', $postid); |
---|
1088 | |
---|
1089 | return $post; |
---|
1090 | } |
---|
1091 | |
---|
1092 | /** |
---|
1093 | * wp_get_post_categories() - Retrieve the list of categories for a post |
---|
1094 | * |
---|
1095 | * Compatibility layer for themes and plugins. Also an easy layer of abstraction |
---|
1096 | * away from the complexity of the taxonomy layer. |
---|
1097 | * |
---|
1098 | * @package WordPress |
---|
1099 | * @subpackage Post |
---|
1100 | * @since 2.1 |
---|
1101 | * |
---|
1102 | * @uses wp_get_object_terms() Retrieves the categories. Args details can be found here |
---|
1103 | * |
---|
1104 | * @param int $post_id Optional. The Post ID |
---|
1105 | * @param array $args Optional. Overwrite the defaults |
---|
1106 | * @return array {@internal Missing Description}} |
---|
1107 | */ |
---|
1108 | function wp_get_post_categories( $post_id = 0, $args = array() ) { |
---|
1109 | $post_id = (int) $post_id; |
---|
1110 | |
---|
1111 | $defaults = array('fields' => 'ids'); |
---|
1112 | $args = wp_parse_args( $args, $defaults ); |
---|
1113 | |
---|
1114 | $cats = wp_get_object_terms($post_id, 'category', $args); |
---|
1115 | return $cats; |
---|
1116 | } |
---|
1117 | |
---|
1118 | /** |
---|
1119 | * wp_get_post_tags() - Retrieve the post tags |
---|
1120 | * |
---|
1121 | * @package WordPress |
---|
1122 | * @subpackage Post |
---|
1123 | * @since 2.3 |
---|
1124 | * |
---|
1125 | * @uses wp_get_object_terms() Gets the tags for returning. Args can be found here |
---|
1126 | * |
---|
1127 | * @param int $post_id Optional. The Post ID |
---|
1128 | * @param array $args Optional. Overwrite the defaults |
---|
1129 | * @return mixed The tags the post has currently |
---|
1130 | */ |
---|
1131 | function wp_get_post_tags( $post_id = 0, $args = array() ) { |
---|
1132 | $post_id = (int) $post_id; |
---|
1133 | |
---|
1134 | $defaults = array('fields' => 'all'); |
---|
1135 | $args = wp_parse_args( $args, $defaults ); |
---|
1136 | |
---|
1137 | $tags = wp_get_object_terms($post_id, 'post_tag', $args); |
---|
1138 | |
---|
1139 | return $tags; |
---|
1140 | } |
---|
1141 | |
---|
1142 | /** |
---|
1143 | * wp_get_recent_posts() - Get the $num most recent posts |
---|
1144 | * |
---|
1145 | * {@internal Missing Long Description}} |
---|
1146 | * |
---|
1147 | * @package WordPress |
---|
1148 | * @subpackage Post |
---|
1149 | * @since 1.0.0 |
---|
1150 | * |
---|
1151 | * @param int $num number of posts to get |
---|
1152 | * @return array {@internal Missing Description}} |
---|
1153 | */ |
---|
1154 | function wp_get_recent_posts($num = 10) { |
---|
1155 | global $wpdb; |
---|
1156 | |
---|
1157 | // Set the limit clause, if we got a limit |
---|
1158 | $num = (int) $num; |
---|
1159 | if ($num) { |
---|
1160 | $limit = "LIMIT $num"; |
---|
1161 | } |
---|
1162 | |
---|
1163 | $sql = "SELECT * FROM $wpdb->posts WHERE post_type = 'post' ORDER BY post_date DESC $limit"; |
---|
1164 | $result = $wpdb->get_results($sql,ARRAY_A); |
---|
1165 | |
---|
1166 | return $result?$result:array(); |
---|
1167 | } |
---|
1168 | |
---|
1169 | /** |
---|
1170 | * wp_get_single_post() - Get one post |
---|
1171 | * |
---|
1172 | * {@internal Missing Long Description}} |
---|
1173 | * |
---|
1174 | * @package WordPress |
---|
1175 | * @subpackage Post |
---|
1176 | * @since 1.0.0 |
---|
1177 | * @uses $wpdb |
---|
1178 | * |
---|
1179 | * @param int $postid post ID |
---|
1180 | * @param string $mode How to return result, either OBJECT, ARRAY_N, or ARRAY_A |
---|
1181 | * @return object|array Post object or array holding post contents and information |
---|
1182 | */ |
---|
1183 | function wp_get_single_post($postid = 0, $mode = OBJECT) { |
---|
1184 | $postid = (int) $postid; |
---|
1185 | |
---|
1186 | $post = get_post($postid, $mode); |
---|
1187 | |
---|
1188 | // Set categories and tags |
---|
1189 | if($mode == OBJECT) { |
---|
1190 | $post->post_category = wp_get_post_categories($postid); |
---|
1191 | $post->tags_input = wp_get_post_tags($postid, array('fields' => 'names')); |
---|
1192 | } |
---|
1193 | else { |
---|
1194 | $post['post_category'] = wp_get_post_categories($postid); |
---|
1195 | $post['tags_input'] = wp_get_post_tags($postid, array('fields' => 'names')); |
---|
1196 | } |
---|
1197 | |
---|
1198 | return $post; |
---|
1199 | } |
---|
1200 | |
---|
1201 | /** |
---|
1202 | * wp_insert_post() - Insert a post |
---|
1203 | * |
---|
1204 | * {@internal Missing Long Description}} |
---|
1205 | * |
---|
1206 | * @package WordPress |
---|
1207 | * @subpackage Post |
---|
1208 | * @since 1.0.0 |
---|
1209 | * |
---|
1210 | * @uses $wpdb |
---|
1211 | * @uses $wp_rewrite |
---|
1212 | * @uses $user_ID |
---|
1213 | * @uses $allowedtags |
---|
1214 | * |
---|
1215 | * @param array $postarr post contents |
---|
1216 | * @return int post ID or 0 on error |
---|
1217 | */ |
---|
1218 | function wp_insert_post($postarr = array(), $wp_error = false) { |
---|
1219 | global $wpdb, $wp_rewrite, $user_ID; |
---|
1220 | |
---|
1221 | $defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_ID, |
---|
1222 | 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0, |
---|
1223 | 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '', |
---|
1224 | 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => ''); |
---|
1225 | |
---|
1226 | $postarr = wp_parse_args($postarr, $defaults); |
---|
1227 | $postarr = sanitize_post($postarr, 'db'); |
---|
1228 | |
---|
1229 | // export array as variables |
---|
1230 | extract($postarr, EXTR_SKIP); |
---|
1231 | |
---|
1232 | // Are we updating or creating? |
---|
1233 | $update = false; |
---|
1234 | if ( !empty($ID) ) { |
---|
1235 | $update = true; |
---|
1236 | $previous_status = get_post_field('post_status', $ID); |
---|
1237 | } else { |
---|
1238 | $previous_status = 'new'; |
---|
1239 | } |
---|
1240 | |
---|
1241 | if ( ('' == $post_content) && ('' == $post_title) && ('' == $post_excerpt) ) { |
---|
1242 | if ( $wp_error ) |
---|
1243 | return new WP_Error('empty_content', __('Content, title, and excerpt are empty.')); |
---|
1244 | else |
---|
1245 | return 0; |
---|
1246 | } |
---|
1247 | |
---|
1248 | // Make sure we set a valid category |
---|
1249 | if (0 == count($post_category) || !is_array($post_category)) { |
---|
1250 | $post_category = array(get_option('default_category')); |
---|
1251 | } |
---|
1252 | |
---|
1253 | if ( empty($post_author) ) |
---|
1254 | $post_author = $user_ID; |
---|
1255 | |
---|
1256 | if ( empty($post_status) ) |
---|
1257 | $post_status = 'draft'; |
---|
1258 | |
---|
1259 | if ( empty($post_type) ) |
---|
1260 | $post_type = 'post'; |
---|
1261 | |
---|
1262 | // Get the post ID and GUID |
---|
1263 | if ( $update ) { |
---|
1264 | $post_ID = (int) $ID; |
---|
1265 | $guid = get_post_field( 'guid', $post_ID ); |
---|
1266 | } |
---|
1267 | |
---|
1268 | // Create a valid post name. Drafts are allowed to have an empty |
---|
1269 | // post name. |
---|
1270 | if ( empty($post_name) ) { |
---|
1271 | if ( 'draft' != $post_status ) |
---|
1272 | $post_name = sanitize_title($post_title); |
---|
1273 | } else { |
---|
1274 | $post_name = sanitize_title($post_name); |
---|
1275 | } |
---|
1276 | |
---|
1277 | // If the post date is empty (due to having been new or a draft) and status is not 'draft', set date to now |
---|
1278 | if ( empty($post_date) || '0000-00-00 00:00:00' == $post_date ) { |
---|
1279 | if ( !in_array($post_status, array('draft', 'pending')) ) |
---|
1280 | $post_date = current_time('mysql'); |
---|
1281 | else |
---|
1282 | $post_date = '0000-00-00 00:00:00'; |
---|
1283 | } |
---|
1284 | |
---|
1285 | if ( empty($post_date_gmt) || '0000-00-00 00:00:00' == $post_date_gmt ) { |
---|
1286 | if ( !in_array($post_status, array('draft', 'pending')) ) |
---|
1287 | $post_date_gmt = get_gmt_from_date($post_date); |
---|
1288 | else |
---|
1289 | $post_date_gmt = '0000-00-00 00:00:00'; |
---|
1290 | } |
---|
1291 | |
---|
1292 | if ( $update || '0000-00-00 00:00:00' == $post_date ) { |
---|
1293 | $post_modified = current_time( 'mysql' ); |
---|
1294 | $post_modified_gmt = current_time( 'mysql', 1 ); |
---|
1295 | } else { |
---|
1296 | $post_modified = $post_date; |
---|
1297 | $post_modified_gmt = $post_date_gmt; |
---|
1298 | } |
---|
1299 | |
---|
1300 | if ( 'publish' == $post_status ) { |
---|
1301 | $now = gmdate('Y-m-d H:i:59'); |
---|
1302 | if ( mysql2date('U', $post_date_gmt) > mysql2date('U', $now) ) |
---|
1303 | $post_status = 'future'; |
---|
1304 | } |
---|
1305 | |
---|
1306 | if ( empty($comment_status) ) { |
---|
1307 | if ( $update ) |
---|
1308 | $comment_status = 'closed'; |
---|
1309 | else |
---|
1310 | $comment_status = get_option('default_comment_status'); |
---|
1311 | } |
---|
1312 | if ( empty($ping_status) ) |
---|
1313 | $ping_status = get_option('default_ping_status'); |
---|
1314 | |
---|
1315 | if ( isset($to_ping) ) |
---|
1316 | $to_ping = preg_replace('|\s+|', "\n", $to_ping); |
---|
1317 | else |
---|
1318 | $to_ping = ''; |
---|
1319 | |
---|
1320 | if ( ! isset($pinged) ) |
---|
1321 | $pinged = ''; |
---|
1322 | |
---|
1323 | if ( isset($post_parent) ) |
---|
1324 | $post_parent = (int) $post_parent; |
---|
1325 | else |
---|
1326 | $post_parent = 0; |
---|
1327 | |
---|
1328 | if ( isset($menu_order) ) |
---|
1329 | $menu_order = (int) $menu_order; |
---|
1330 | else |
---|
1331 | $menu_order = 0; |
---|
1332 | |
---|
1333 | if ( !isset($post_password) ) |
---|
1334 | $post_password = ''; |
---|
1335 | |
---|
1336 | if ( 'draft' != $post_status ) { |
---|
1337 | $post_name_check = $wpdb->get_var($wpdb->prepare("SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d AND post_parent = %d LIMIT 1", $post_name, $post_type, $post_ID, $post_parent)); |
---|
1338 | |
---|
1339 | if ($post_name_check || in_array($post_name, $wp_rewrite->feeds) ) { |
---|
1340 | $suffix = 2; |
---|
1341 | do { |
---|
1342 | $alt_post_name = substr($post_name, 0, 200-(strlen($suffix)+1)). "-$suffix"; |
---|
1343 | $post_name_check = $wpdb->get_var($wpdb->prepare("SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d AND post_parent = %d LIMIT 1", $alt_post_name, $post_type, $post_ID, $post_parent)); |
---|
1344 | $suffix++; |
---|
1345 | } while ($post_name_check); |
---|
1346 | $post_name = $alt_post_name; |
---|
1347 | } |
---|
1348 | } |
---|
1349 | |
---|
1350 | // expected_slashed (everything!) |
---|
1351 | $data = compact( array( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type', 'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'menu_order', 'guid' ) ); |
---|
1352 | $data = stripslashes_deep( $data ); |
---|
1353 | $where = array( 'ID' => $post_ID ); |
---|
1354 | |
---|
1355 | if ($update) { |
---|
1356 | do_action( 'pre_post_update', $post_ID ); |
---|
1357 | if ( false === $wpdb->update( $wpdb->posts, $data, $where ) ) { |
---|
1358 | if ( $wp_error ) |
---|
1359 | return new WP_Error('db_update_error', __('Could not update post in the database'), $wpdb->last_error); |
---|
1360 | else |
---|
1361 | return 0; |
---|
1362 | } |
---|
1363 | } else { |
---|
1364 | $data['post_mime_type'] = stripslashes( $post_mime_type ); // This isn't in the update |
---|
1365 | if ( false === $wpdb->insert( $wpdb->posts, $data ) ) { |
---|
1366 | if ( $wp_error ) |
---|
1367 | return new WP_Error('db_insert_error', __('Could not insert post into the database'), $wpdb->last_error); |
---|
1368 | else |
---|
1369 | return 0; |
---|
1370 | } |
---|
1371 | $post_ID = (int) $wpdb->insert_id; |
---|
1372 | |
---|
1373 | // use the newly generated $post_ID |
---|
1374 | $where = array( 'ID' => $post_ID ); |
---|
1375 | } |
---|
1376 | |
---|
1377 | if ( empty($post_name) && 'draft' != $post_status ) { |
---|
1378 | $post_name = sanitize_title($post_title, $post_ID); |
---|
1379 | $wpdb->update( $wpdb->posts, compact( 'post_name' ), $where ); |
---|
1380 | } |
---|
1381 | |
---|
1382 | wp_set_post_categories( $post_ID, $post_category ); |
---|
1383 | wp_set_post_tags( $post_ID, $tags_input ); |
---|
1384 | |
---|
1385 | $current_guid = get_post_field( 'guid', $post_ID ); |
---|
1386 | |
---|
1387 | if ( 'page' == $post_type ) |
---|
1388 | clean_page_cache($post_ID); |
---|
1389 | else |
---|
1390 | clean_post_cache($post_ID); |
---|
1391 | |
---|
1392 | // Set GUID |
---|
1393 | if ( !$update && '' == $current_guid ) |
---|
1394 | $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where ); |
---|
1395 | |
---|
1396 | $post = get_post($post_ID); |
---|
1397 | |
---|
1398 | if ( !empty($page_template) && 'page' == $post_type ) { |
---|
1399 | $post->page_template = $page_template; |
---|
1400 | $page_templates = get_page_templates(); |
---|
1401 | if ( 'default' != $page_template && !in_array($page_template, $page_templates) ) { |
---|
1402 | if ( $wp_error ) |
---|
1403 | return new WP_Error('invalid_page_template', __('The page template is invalid.')); |
---|
1404 | else |
---|
1405 | return 0; |
---|
1406 | } |
---|
1407 | update_post_meta($post_ID, '_wp_page_template', $page_template); |
---|
1408 | } |
---|
1409 | |
---|
1410 | wp_transition_post_status($post_status, $previous_status, $post); |
---|
1411 | |
---|
1412 | if ( $update) |
---|
1413 | do_action('edit_post', $post_ID, $post); |
---|
1414 | |
---|
1415 | do_action('save_post', $post_ID, $post); |
---|
1416 | do_action('wp_insert_post', $post_ID, $post); |
---|
1417 | |
---|
1418 | return $post_ID; |
---|
1419 | } |
---|
1420 | |
---|
1421 | /** |
---|
1422 | * wp_update_post() - Update a post |
---|
1423 | * |
---|
1424 | * {@internal Missing Long Description}} |
---|
1425 | * |
---|
1426 | * @package WordPress |
---|
1427 | * @subpackage Post |
---|
1428 | * @since 1.0.0 |
---|
1429 | * @uses $wpdb |
---|
1430 | * |
---|
1431 | * @param array $postarr post data |
---|
1432 | * @return int {@internal Missing Description}} |
---|
1433 | */ |
---|
1434 | function wp_update_post($postarr = array()) { |
---|
1435 | if ( is_object($postarr) ) |
---|
1436 | $postarr = get_object_vars($postarr); |
---|
1437 | |
---|
1438 | // First, get all of the original fields |
---|
1439 | $post = wp_get_single_post($postarr['ID'], ARRAY_A); |
---|
1440 | |
---|
1441 | // Escape data pulled from DB. |
---|
1442 | $post = add_magic_quotes($post); |
---|
1443 | |
---|
1444 | // Passed post category list overwrites existing category list if not empty. |
---|
1445 | if ( isset($postarr['post_category']) && is_array($postarr['post_category']) |
---|
1446 | && 0 != count($postarr['post_category']) ) |
---|
1447 | $post_cats = $postarr['post_category']; |
---|
1448 | else |
---|
1449 | $post_cats = $post['post_category']; |
---|
1450 | |
---|
1451 | // Drafts shouldn't be assigned a date unless explicitly done so by the user |
---|
1452 | if ( in_array($post['post_status'], array('draft', 'pending')) && empty($postarr['edit_date']) && empty($postarr['post_date']) && |
---|
1453 | ('0000-00-00 00:00:00' == $post['post_date']) ) |
---|
1454 | $clear_date = true; |
---|
1455 | else |
---|
1456 | $clear_date = false; |
---|
1457 | |
---|
1458 | // Merge old and new fields with new fields overwriting old ones. |
---|
1459 | $postarr = array_merge($post, $postarr); |
---|
1460 | $postarr['post_category'] = $post_cats; |
---|
1461 | if ( $clear_date ) { |
---|
1462 | $postarr['post_date'] = ''; |
---|
1463 | $postarr['post_date_gmt'] = ''; |
---|
1464 | } |
---|
1465 | |
---|
1466 | if ($postarr['post_type'] == 'attachment') |
---|
1467 | return wp_insert_attachment($postarr); |
---|
1468 | |
---|
1469 | return wp_insert_post($postarr); |
---|
1470 | } |
---|
1471 | |
---|
1472 | /** |
---|
1473 | * wp_publish_post() - Mark a post as "published" |
---|
1474 | * |
---|
1475 | * {@internal Missing Long Description}} |
---|
1476 | * |
---|
1477 | * @package WordPress |
---|
1478 | * @subpackage Post |
---|
1479 | * @since 2.1 |
---|
1480 | * @uses $wpdb |
---|
1481 | * |
---|
1482 | * @param int $post_id Post ID |
---|
1483 | * @return int|null {@internal Missing Description}} |
---|
1484 | */ |
---|
1485 | function wp_publish_post($post_id) { |
---|
1486 | global $wpdb; |
---|
1487 | |
---|
1488 | $post = get_post($post_id); |
---|
1489 | |
---|
1490 | if ( empty($post) ) |
---|
1491 | return; |
---|
1492 | |
---|
1493 | if ( 'publish' == $post->post_status ) |
---|
1494 | return; |
---|
1495 | |
---|
1496 | $wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post_id ) ); |
---|
1497 | |
---|
1498 | $old_status = $post->post_status; |
---|
1499 | $post->post_status = 'publish'; |
---|
1500 | wp_transition_post_status('publish', $old_status, $post); |
---|
1501 | |
---|
1502 | // Update counts for the post's terms. |
---|
1503 | foreach ( get_object_taxonomies('post') as $taxonomy ) { |
---|
1504 | $terms = wp_get_object_terms($post_id, $taxonomy, 'fields=tt_ids'); |
---|
1505 | wp_update_term_count($terms, $taxonomy); |
---|
1506 | } |
---|
1507 | |
---|
1508 | do_action('edit_post', $post_id, $post); |
---|
1509 | do_action('save_post', $post_id, $post); |
---|
1510 | do_action('wp_insert_post', $post_id, $post); |
---|
1511 | } |
---|
1512 | |
---|
1513 | /** |
---|
1514 | * check_and_publish_future_post() - check to make sure post has correct status before |
---|
1515 | * passing it on to be published. Invoked by cron 'publish_future_post' event |
---|
1516 | * This safeguard prevents cron from publishing drafts, etc. |
---|
1517 | * |
---|
1518 | * {@internal Missing Long Description}} |
---|
1519 | * |
---|
1520 | * @package WordPress |
---|
1521 | * @subpackage Post |
---|
1522 | * @since 2.5 |
---|
1523 | * @uses $wpdb |
---|
1524 | * |
---|
1525 | * @param int $post_id Post ID |
---|
1526 | * @return int|null {@internal Missing Description}} |
---|
1527 | */ |
---|
1528 | function check_and_publish_future_post($post_id) { |
---|
1529 | |
---|
1530 | $post = get_post($post_id); |
---|
1531 | |
---|
1532 | if ( empty($post) ) |
---|
1533 | return; |
---|
1534 | |
---|
1535 | if ( 'future' != $post->post_status ) |
---|
1536 | return; |
---|
1537 | |
---|
1538 | return wp_publish_post($post_id); |
---|
1539 | } |
---|
1540 | |
---|
1541 | /** |
---|
1542 | * wp_add_post_tags() - Adds the tags to a post |
---|
1543 | * |
---|
1544 | * @uses wp_set_post_tags() Same first two paraeters, but the last parameter is always set to true. |
---|
1545 | * |
---|
1546 | * @package WordPress |
---|
1547 | * @subpackage Post |
---|
1548 | * @since 2.3 |
---|
1549 | * |
---|
1550 | * @param int $post_id Optional. Post ID |
---|
1551 | * @param string $tags The tags to set for the post |
---|
1552 | * @return bool|null Will return false if $post_id is not an integer or is 0. Will return null otherwise |
---|
1553 | */ |
---|
1554 | function wp_add_post_tags($post_id = 0, $tags = '') { |
---|
1555 | return wp_set_post_tags($post_id, $tags, true); |
---|
1556 | } |
---|
1557 | |
---|
1558 | /** |
---|
1559 | * wp_set_post_tags() - Set the tags for a post |
---|
1560 | * |
---|
1561 | * {@internal Missing Long Description}} |
---|
1562 | * |
---|
1563 | * @package WordPress |
---|
1564 | * @subpackage Post |
---|
1565 | * @since 2.3 |
---|
1566 | * @uses $wpdb |
---|
1567 | * |
---|
1568 | * @param int $post_id post ID |
---|
1569 | * @param string $tags The tags to set for the post |
---|
1570 | * @param bool $append If true, don't delete existing tags, just add on. If false, replace the tags with the new tags. |
---|
1571 | * @return bool|null Will return false if $post_id is not an integer or is 0. Will return null otherwise |
---|
1572 | */ |
---|
1573 | function wp_set_post_tags( $post_id = 0, $tags = '', $append = false ) { |
---|
1574 | |
---|
1575 | $post_id = (int) $post_id; |
---|
1576 | |
---|
1577 | if ( !$post_id ) |
---|
1578 | return false; |
---|
1579 | |
---|
1580 | if ( empty($tags) ) |
---|
1581 | $tags = array(); |
---|
1582 | $tags = (is_array($tags)) ? $tags : explode( ',', trim($tags, " \n\t\r\0\x0B,") ); |
---|
1583 | wp_set_object_terms($post_id, $tags, 'post_tag', $append); |
---|
1584 | } |
---|
1585 | |
---|
1586 | /** |
---|
1587 | * wp_set_post_categories() - Set categories for a post |
---|
1588 | * |
---|
1589 | * {@internal Missing Long Description}} |
---|
1590 | * |
---|
1591 | * @package WordPress |
---|
1592 | * @subpackage Post |
---|
1593 | * @since 2.1 |
---|
1594 | * @uses $wpdb |
---|
1595 | * |
---|
1596 | * @param int $post_ID post ID |
---|
1597 | * @param array $post_categories |
---|
1598 | * @return bool|mixed {@internal Missing Description}} |
---|
1599 | */ |
---|
1600 | function wp_set_post_categories($post_ID = 0, $post_categories = array()) { |
---|
1601 | $post_ID = (int) $post_ID; |
---|
1602 | // If $post_categories isn't already an array, make it one: |
---|
1603 | if (!is_array($post_categories) || 0 == count($post_categories) || empty($post_categories)) |
---|
1604 | $post_categories = array(get_option('default_category')); |
---|
1605 | else if ( 1 == count($post_categories) && '' == $post_categories[0] ) |
---|
1606 | return true; |
---|
1607 | |
---|
1608 | $post_categories = array_map('intval', $post_categories); |
---|
1609 | $post_categories = array_unique($post_categories); |
---|
1610 | |
---|
1611 | return wp_set_object_terms($post_ID, $post_categories, 'category'); |
---|
1612 | } // wp_set_post_categories() |
---|
1613 | |
---|
1614 | /** |
---|
1615 | * wp_transition_post_status() - Change the post transition status |
---|
1616 | * |
---|
1617 | * {@internal Missing Long Description}} |
---|
1618 | * |
---|
1619 | * @package WordPress |
---|
1620 | * @subpackage Post |
---|
1621 | * @since 2.3 |
---|
1622 | * |
---|
1623 | * @param string $new_status {@internal Missing Description}} |
---|
1624 | * @param string $old_status {@internal Missing Description}} |
---|
1625 | * @param int $post {@internal Missing Description}} |
---|
1626 | */ |
---|
1627 | function wp_transition_post_status($new_status, $old_status, $post) { |
---|
1628 | if ( $new_status != $old_status ) { |
---|
1629 | do_action('transition_post_status', $new_status, $old_status, $post); |
---|
1630 | do_action("${old_status}_to_$new_status", $post); |
---|
1631 | } |
---|
1632 | do_action("${new_status}_$post->post_type", $post->ID, $post); |
---|
1633 | } |
---|
1634 | |
---|
1635 | // |
---|
1636 | // Trackback and ping functions |
---|
1637 | // |
---|
1638 | |
---|
1639 | /** |
---|
1640 | * add_ping() - Add a URL to those already pung |
---|
1641 | * |
---|
1642 | * {@internal Missing Long Description}} |
---|
1643 | * |
---|
1644 | * @package WordPress |
---|
1645 | * @subpackage Post |
---|
1646 | * @since 1.5 |
---|
1647 | * @uses $wpdb |
---|
1648 | * |
---|
1649 | * @param int $post_id post ID |
---|
1650 | * @param string $uri {@internal Missing Description}} |
---|
1651 | * @return mixed {@internal Missing Description}} |
---|
1652 | */ |
---|
1653 | function add_ping($post_id, $uri) { |
---|
1654 | global $wpdb; |
---|
1655 | $pung = $wpdb->get_var( $wpdb->prepare( "SELECT pinged FROM $wpdb->posts WHERE ID = %d", $post_id )); |
---|
1656 | $pung = trim($pung); |
---|
1657 | $pung = preg_split('/\s/', $pung); |
---|
1658 | $pung[] = $uri; |
---|
1659 | $new = implode("\n", $pung); |
---|
1660 | $new = apply_filters('add_ping', $new); |
---|
1661 | // expected_slashed ($new) |
---|
1662 | $new = stripslashes($new); |
---|
1663 | return $wpdb->update( $wpdb->posts, array( 'pinged' => $new ), array( 'ID' => $post_id ) ); |
---|
1664 | } |
---|
1665 | |
---|
1666 | /** |
---|
1667 | * get_enclosed() - Get enclosures already enclosed for a post |
---|
1668 | * |
---|
1669 | * {@internal Missing Long Description}} |
---|
1670 | * |
---|
1671 | * @package WordPress |
---|
1672 | * @subpackage Post |
---|
1673 | * @since 1.5 |
---|
1674 | * @uses $wpdb |
---|
1675 | * |
---|
1676 | * @param int $post_id post ID |
---|
1677 | * @return array {@internal Missing Description}} |
---|
1678 | */ |
---|
1679 | function get_enclosed($post_id) { |
---|
1680 | $custom_fields = get_post_custom( $post_id ); |
---|
1681 | $pung = array(); |
---|
1682 | if ( !is_array( $custom_fields ) ) |
---|
1683 | return $pung; |
---|
1684 | |
---|
1685 | foreach ( $custom_fields as $key => $val ) { |
---|
1686 | if ( 'enclosure' != $key || !is_array( $val ) ) |
---|
1687 | continue; |
---|
1688 | foreach( $val as $enc ) { |
---|
1689 | $enclosure = split( "\n", $enc ); |
---|
1690 | $pung[] = trim( $enclosure[ 0 ] ); |
---|
1691 | } |
---|
1692 | } |
---|
1693 | $pung = apply_filters('get_enclosed', $pung); |
---|
1694 | return $pung; |
---|
1695 | } |
---|
1696 | |
---|
1697 | /** |
---|
1698 | * get_pung() - Get URLs already pinged for a post |
---|
1699 | * |
---|
1700 | * {@internal Missing Long Description}} |
---|
1701 | * |
---|
1702 | * @package WordPress |
---|
1703 | * @subpackage Post |
---|
1704 | * @since 1.5 |
---|
1705 | * @uses $wpdb |
---|
1706 | * |
---|
1707 | * @param int $post_id post ID |
---|
1708 | * @return array {@internal Missing Description}} |
---|
1709 | */ |
---|
1710 | function get_pung($post_id) { |
---|
1711 | global $wpdb; |
---|
1712 | $pung = $wpdb->get_var( $wpdb->prepare( "SELECT pinged FROM $wpdb->posts WHERE ID = %d", $post_id )); |
---|
1713 | $pung = trim($pung); |
---|
1714 | $pung = preg_split('/\s/', $pung); |
---|
1715 | $pung = apply_filters('get_pung', $pung); |
---|
1716 | return $pung; |
---|
1717 | } |
---|
1718 | |
---|
1719 | /** |
---|
1720 | * get_to_ping() - Get any URLs in the todo list |
---|
1721 | * |
---|
1722 | * {@internal Missing Long Description}} |
---|
1723 | * |
---|
1724 | * @package WordPress |
---|
1725 | * @subpackage Post |
---|
1726 | * @since 1.5 |
---|
1727 | * @uses $wpdb |
---|
1728 | * |
---|
1729 | * @param int $post_id post ID |
---|
1730 | * @return array {@internal Missing Description}} |
---|
1731 | */ |
---|
1732 | function get_to_ping($post_id) { |
---|
1733 | global $wpdb; |
---|
1734 | $to_ping = $wpdb->get_var( $wpdb->prepare( "SELECT to_ping FROM $wpdb->posts WHERE ID = %d", $post_id )); |
---|
1735 | $to_ping = trim($to_ping); |
---|
1736 | $to_ping = preg_split('/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY); |
---|
1737 | $to_ping = apply_filters('get_to_ping', $to_ping); |
---|
1738 | return $to_ping; |
---|
1739 | } |
---|
1740 | |
---|
1741 | /** |
---|
1742 | * trackback_url_list() - Do trackbacks for a list of urls |
---|
1743 | * |
---|
1744 | * {@internal Missing Long Description}} |
---|
1745 | * |
---|
1746 | * @package WordPress |
---|
1747 | * @subpackage Post |
---|
1748 | * @since 1.0.0 |
---|
1749 | * |
---|
1750 | * @param string $tb_list comma separated list of URLs |
---|
1751 | * @param int $post_id post ID |
---|
1752 | */ |
---|
1753 | function trackback_url_list($tb_list, $post_id) { |
---|
1754 | if (!empty($tb_list)) { |
---|
1755 | // get post data |
---|
1756 | $postdata = wp_get_single_post($post_id, ARRAY_A); |
---|
1757 | |
---|
1758 | // import postdata as variables |
---|
1759 | extract($postdata, EXTR_SKIP); |
---|
1760 | |
---|
1761 | // form an excerpt |
---|
1762 | $excerpt = strip_tags($post_excerpt?$post_excerpt:$post_content); |
---|
1763 | |
---|
1764 | if (strlen($excerpt) > 255) { |
---|
1765 | $excerpt = substr($excerpt,0,252) . '...'; |
---|
1766 | } |
---|
1767 | |
---|
1768 | $trackback_urls = explode(',', $tb_list); |
---|
1769 | foreach($trackback_urls as $tb_url) { |
---|
1770 | $tb_url = trim($tb_url); |
---|
1771 | trackback($tb_url, stripslashes($post_title), $excerpt, $post_id); |
---|
1772 | } |
---|
1773 | } |
---|
1774 | } |
---|
1775 | |
---|
1776 | // |
---|
1777 | // Page functions |
---|
1778 | // |
---|
1779 | |
---|
1780 | /** |
---|
1781 | * get_all_page_ids() - Get a list of page IDs |
---|
1782 | * |
---|
1783 | * {@internal Missing Long Description}} |
---|
1784 | * |
---|
1785 | * @package WordPress |
---|
1786 | * @subpackage Post |
---|
1787 | * @since 2.0 |
---|
1788 | * @uses $wpdb |
---|
1789 | * |
---|
1790 | * @return array {@internal Missing Description}} |
---|
1791 | */ |
---|
1792 | function get_all_page_ids() { |
---|
1793 | global $wpdb; |
---|
1794 | |
---|
1795 | if ( ! $page_ids = wp_cache_get('all_page_ids', 'posts') ) { |
---|
1796 | $page_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_type = 'page'"); |
---|
1797 | wp_cache_add('all_page_ids', $page_ids, 'posts'); |
---|
1798 | } |
---|
1799 | |
---|
1800 | return $page_ids; |
---|
1801 | } |
---|
1802 | |
---|
1803 | /** |
---|
1804 | * get_page() - Retrieves page data given a page ID or page object |
---|
1805 | * |
---|
1806 | * {@internal Missing Long Description}} |
---|
1807 | * |
---|
1808 | * @package WordPress |
---|
1809 | * @subpackage Post |
---|
1810 | * @since 1.5.1 |
---|
1811 | * |
---|
1812 | * @param mixed &$page page object or page ID |
---|
1813 | * @param string $output what to output |
---|
1814 | * @param string $filter How the return value should be filtered. |
---|
1815 | * @return mixed {@internal Missing Description}} |
---|
1816 | */ |
---|
1817 | function &get_page(&$page, $output = OBJECT, $filter = 'raw') { |
---|
1818 | if ( empty($page) ) { |
---|
1819 | if ( isset( $GLOBALS['page'] ) && isset( $GLOBALS['page']->ID ) ) |
---|
1820 | return get_post($GLOBALS['page'], $output, $filter); |
---|
1821 | else |
---|
1822 | return null; |
---|
1823 | } |
---|
1824 | |
---|
1825 | return get_post($page, $output, $filter); |
---|
1826 | } |
---|
1827 | |
---|
1828 | /** |
---|
1829 | * get_page_by_path() - Retrieves a page given its path |
---|
1830 | * |
---|
1831 | * {@internal Missing Long Description}} |
---|
1832 | * |
---|
1833 | * @package WordPress |
---|
1834 | * @subpackage Post |
---|
1835 | * @since 2.1 |
---|
1836 | * @uses $wpdb |
---|
1837 | * |
---|
1838 | * @param string $page_path page path |
---|
1839 | * @param string $output output type |
---|
1840 | * @return mixed {@internal Missing Description}} |
---|
1841 | */ |
---|
1842 | function get_page_by_path($page_path, $output = OBJECT) { |
---|
1843 | global $wpdb; |
---|
1844 | $page_path = rawurlencode(urldecode($page_path)); |
---|
1845 | $page_path = str_replace('%2F', '/', $page_path); |
---|
1846 | $page_path = str_replace('%20', ' ', $page_path); |
---|
1847 | $page_paths = '/' . trim($page_path, '/'); |
---|
1848 | $leaf_path = sanitize_title(basename($page_paths)); |
---|
1849 | $page_paths = explode('/', $page_paths); |
---|
1850 | foreach($page_paths as $pathdir) |
---|
1851 | $full_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir); |
---|
1852 | |
---|
1853 | $pages = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_name = %s AND (post_type = 'page' OR post_type = 'attachment')", $leaf_path )); |
---|
1854 | |
---|
1855 | if ( empty($pages) ) |
---|
1856 | return NULL; |
---|
1857 | |
---|
1858 | foreach ($pages as $page) { |
---|
1859 | $path = '/' . $leaf_path; |
---|
1860 | $curpage = $page; |
---|
1861 | while ($curpage->post_parent != 0) { |
---|
1862 | $curpage = $wpdb->get_row( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE ID = %d and post_type='page'", $curpage->post_parent )); |
---|
1863 | $path = '/' . $curpage->post_name . $path; |
---|
1864 | } |
---|
1865 | |
---|
1866 | if ( $path == $full_path ) |
---|
1867 | return get_page($page->ID, $output); |
---|
1868 | } |
---|
1869 | |
---|
1870 | return NULL; |
---|
1871 | } |
---|
1872 | |
---|
1873 | /** |
---|
1874 | * get_page_by_title() - Retrieve a page given its title |
---|
1875 | * |
---|
1876 | * {@internal Missing Long Description}} |
---|
1877 | * |
---|
1878 | * @package WordPress |
---|
1879 | * @subpackage Post |
---|
1880 | * @since 2.1 |
---|
1881 | * @uses $wpdb |
---|
1882 | * |
---|
1883 | * @param string $page_title page title |
---|
1884 | * @param string $output output type |
---|
1885 | * @return mixed {@internal Missing Description}} |
---|
1886 | */ |
---|
1887 | function get_page_by_title($page_title, $output = OBJECT) { |
---|
1888 | global $wpdb; |
---|
1889 | $page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type='page'", $page_title )); |
---|
1890 | if ( $page ) |
---|
1891 | return get_page($page, $output); |
---|
1892 | |
---|
1893 | return NULL; |
---|
1894 | } |
---|
1895 | |
---|
1896 | /** |
---|
1897 | * get_page_children() - Retrieve child pages |
---|
1898 | * |
---|
1899 | * {@internal Missing Long Description}} |
---|
1900 | * |
---|
1901 | * @package WordPress |
---|
1902 | * @subpackage Post |
---|
1903 | * @since 1.5.1 |
---|
1904 | * |
---|
1905 | * @param int $page_id page ID |
---|
1906 | * @param array $pages list of pages |
---|
1907 | * @return array {@internal Missing Description}} |
---|
1908 | */ |
---|
1909 | function &get_page_children($page_id, $pages) { |
---|
1910 | $page_list = array(); |
---|
1911 | foreach ( $pages as $page ) { |
---|
1912 | if ( $page->post_parent == $page_id ) { |
---|
1913 | $page_list[] = $page; |
---|
1914 | if ( $children = get_page_children($page->ID, $pages) ) |
---|
1915 | $page_list = array_merge($page_list, $children); |
---|
1916 | } |
---|
1917 | } |
---|
1918 | return $page_list; |
---|
1919 | } |
---|
1920 | |
---|
1921 | /** |
---|
1922 | * get_page_hierarchy() - {@internal Missing Short Description}} |
---|
1923 | * |
---|
1924 | * Fetches the pages returned as a FLAT list, but arranged in order of their hierarchy, |
---|
1925 | * i.e., child parents immediately follow their parents. |
---|
1926 | * |
---|
1927 | * @package WordPress |
---|
1928 | * @subpackage Post |
---|
1929 | * @since 2.0 |
---|
1930 | * |
---|
1931 | * @param array $posts posts array |
---|
1932 | * @param int $parent parent page ID |
---|
1933 | * @return array {@internal Missing Description}} |
---|
1934 | */ |
---|
1935 | function get_page_hierarchy($posts, $parent = 0) { |
---|
1936 | $result = array ( ); |
---|
1937 | if ($posts) { foreach ($posts as $post) { |
---|
1938 | if ($post->post_parent == $parent) { |
---|
1939 | $result[$post->ID] = $post->post_name; |
---|
1940 | $children = get_page_hierarchy($posts, $post->ID); |
---|
1941 | $result += $children; //append $children to $result |
---|
1942 | } |
---|
1943 | } } |
---|
1944 | return $result; |
---|
1945 | } |
---|
1946 | |
---|
1947 | /** |
---|
1948 | * get_page_uri() - Builds a page URI |
---|
1949 | * |
---|
1950 | * {@internal Missing Long Description}} |
---|
1951 | * |
---|
1952 | * @package WordPress |
---|
1953 | * @subpackage Post |
---|
1954 | * @since 1.5 |
---|
1955 | * |
---|
1956 | * @param int $page_id page ID |
---|
1957 | * @return string {@internal Missing Description}} |
---|
1958 | */ |
---|
1959 | function get_page_uri($page_id) { |
---|
1960 | $page = get_page($page_id); |
---|
1961 | $uri = $page->post_name; |
---|
1962 | |
---|
1963 | // A page cannot be it's own parent. |
---|
1964 | if ( $page->post_parent == $page->ID ) |
---|
1965 | return $uri; |
---|
1966 | |
---|
1967 | while ($page->post_parent != 0) { |
---|
1968 | $page = get_page($page->post_parent); |
---|
1969 | $uri = $page->post_name . "/" . $uri; |
---|
1970 | } |
---|
1971 | |
---|
1972 | return $uri; |
---|
1973 | } |
---|
1974 | |
---|
1975 | /** |
---|
1976 | * get_pages() - Retrieve a list of pages |
---|
1977 | * |
---|
1978 | * {@internal Missing Long Description}} |
---|
1979 | * |
---|
1980 | * @package WordPress |
---|
1981 | * @subpackage Post |
---|
1982 | * @since 1.5 |
---|
1983 | * @uses $wpdb |
---|
1984 | * |
---|
1985 | * @param mixed $args Optional. Array or string of options |
---|
1986 | * @return array List of pages matching defaults or $args |
---|
1987 | */ |
---|
1988 | function &get_pages($args = '') { |
---|
1989 | global $wpdb; |
---|
1990 | |
---|
1991 | $defaults = array( |
---|
1992 | 'child_of' => 0, 'sort_order' => 'ASC', |
---|
1993 | 'sort_column' => 'post_title', 'hierarchical' => 1, |
---|
1994 | 'exclude' => '', 'include' => '', |
---|
1995 | 'meta_key' => '', 'meta_value' => '', |
---|
1996 | 'authors' => '' |
---|
1997 | ); |
---|
1998 | |
---|
1999 | $r = wp_parse_args( $args, $defaults ); |
---|
2000 | extract( $r, EXTR_SKIP ); |
---|
2001 | |
---|
2002 | $key = md5( serialize( $r ) ); |
---|
2003 | if ( $cache = wp_cache_get( 'get_pages', 'posts' ) ) |
---|
2004 | if ( isset( $cache[ $key ] ) ) |
---|
2005 | return apply_filters('get_pages', $cache[ $key ], $r ); |
---|
2006 | |
---|
2007 | $inclusions = ''; |
---|
2008 | if ( !empty($include) ) { |
---|
2009 | $child_of = 0; //ignore child_of, exclude, meta_key, and meta_value params if using include |
---|
2010 | $exclude = ''; |
---|
2011 | $meta_key = ''; |
---|
2012 | $meta_value = ''; |
---|
2013 | $hierarchical = false; |
---|
2014 | $incpages = preg_split('/[\s,]+/',$include); |
---|
2015 | if ( count($incpages) ) { |
---|
2016 | foreach ( $incpages as $incpage ) { |
---|
2017 | if (empty($inclusions)) |
---|
2018 | $inclusions = $wpdb->prepare(' AND ( ID = %d ', $incpage); |
---|
2019 | else |
---|
2020 | $inclusions .= $wpdb->prepare(' OR ID = %d ', $incpage); |
---|
2021 | } |
---|
2022 | } |
---|
2023 | } |
---|
2024 | if (!empty($inclusions)) |
---|
2025 | $inclusions .= ')'; |
---|
2026 | |
---|
2027 | $exclusions = ''; |
---|
2028 | if ( !empty($exclude) ) { |
---|
2029 | $expages = preg_split('/[\s,]+/',$exclude); |
---|
2030 | if ( count($expages) ) { |
---|
2031 | foreach ( $expages as $expage ) { |
---|
2032 | if (empty($exclusions)) |
---|
2033 | $exclusions = $wpdb->prepare(' AND ( ID <> %d ', $expage); |
---|
2034 | else |
---|
2035 | $exclusions .= $wpdb->prepare(' AND ID <> %d ', $expage); |
---|
2036 | } |
---|
2037 | } |
---|
2038 | } |
---|
2039 | if (!empty($exclusions)) |
---|
2040 | $exclusions .= ')'; |
---|
2041 | |
---|
2042 | $author_query = ''; |
---|
2043 | if (!empty($authors)) { |
---|
2044 | $post_authors = preg_split('/[\s,]+/',$authors); |
---|
2045 | |
---|
2046 | if ( count($post_authors) ) { |
---|
2047 | foreach ( $post_authors as $post_author ) { |
---|
2048 | //Do we have an author id or an author login? |
---|
2049 | if ( 0 == intval($post_author) ) { |
---|
2050 | $post_author = get_userdatabylogin($post_author); |
---|
2051 | if ( empty($post_author) ) |
---|
2052 | continue; |
---|
2053 | if ( empty($post_author->ID) ) |
---|
2054 | continue; |
---|
2055 | $post_author = $post_author->ID; |
---|
2056 | } |
---|
2057 | |
---|
2058 | if ( '' == $author_query ) |
---|
2059 | $author_query = $wpdb->prepare(' post_author = %d ', $post_author); |
---|
2060 | else |
---|
2061 | $author_query .= $wpdb->prepare(' OR post_author = %d ', $post_author); |
---|
2062 | } |
---|
2063 | if ( '' != $author_query ) |
---|
2064 | $author_query = " AND ($author_query)"; |
---|
2065 | } |
---|
2066 | } |
---|
2067 | |
---|
2068 | $join = ''; |
---|
2069 | $where = "$exclusions $inclusions "; |
---|
2070 | if ( ! empty( $meta_key ) || ! empty( $meta_value ) ) { |
---|
2071 | $join = " LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )"; |
---|
2072 | |
---|
2073 | // meta_key and met_value might be slashed |
---|
2074 | $meta_key = stripslashes($meta_key); |
---|
2075 | $meta_value = stripslashes($meta_value); |
---|
2076 | if ( ! empty( $meta_key ) ) |
---|
2077 | $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_key = %s", $meta_key); |
---|
2078 | if ( ! empty( $meta_value ) ) |
---|
2079 | $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_value = %s", $meta_value); |
---|
2080 | |
---|
2081 | } |
---|
2082 | $query = "SELECT * FROM $wpdb->posts $join WHERE (post_type = 'page' AND post_status = 'publish') $where "; |
---|
2083 | $query .= $author_query; |
---|
2084 | $query .= " ORDER BY " . $sort_column . " " . $sort_order ; |
---|
2085 | |
---|
2086 | $pages = $wpdb->get_results($query); |
---|
2087 | |
---|
2088 | if ( empty($pages) ) |
---|
2089 | return apply_filters('get_pages', array(), $r); |
---|
2090 | |
---|
2091 | // Update cache. |
---|
2092 | update_page_cache($pages); |
---|
2093 | |
---|
2094 | if ( $child_of || $hierarchical ) |
---|
2095 | $pages = & get_page_children($child_of, $pages); |
---|
2096 | |
---|
2097 | $cache[ $key ] = $pages; |
---|
2098 | wp_cache_set( 'get_pages', $cache, 'posts' ); |
---|
2099 | |
---|
2100 | $pages = apply_filters('get_pages', $pages, $r); |
---|
2101 | |
---|
2102 | return $pages; |
---|
2103 | } |
---|
2104 | |
---|
2105 | // |
---|
2106 | // Attachment functions |
---|
2107 | // |
---|
2108 | |
---|
2109 | /** |
---|
2110 | * is_local_attachment() - Check if the attachment URI is local one and is really an attachment. |
---|
2111 | * |
---|
2112 | * {@internal Missing Long Description}} |
---|
2113 | * |
---|
2114 | * @package WordPress |
---|
2115 | * @subpackage Post |
---|
2116 | * @since 2.0 |
---|
2117 | * |
---|
2118 | * @param string $url URL to check |
---|
2119 | * @return bool {@internal Missing Description}} |
---|
2120 | */ |
---|
2121 | function is_local_attachment($url) { |
---|
2122 | if (strpos($url, get_bloginfo('url')) === false) |
---|
2123 | return false; |
---|
2124 | if (strpos($url, get_bloginfo('url') . '/?attachment_id=') !== false) |
---|
2125 | return true; |
---|
2126 | if ( $id = url_to_postid($url) ) { |
---|
2127 | $post = & get_post($id); |
---|
2128 | if ( 'attachment' == $post->post_type ) |
---|
2129 | return true; |
---|
2130 | } |
---|
2131 | return false; |
---|
2132 | } |
---|
2133 | |
---|
2134 | /** |
---|
2135 | * wp_insert_attachment() - Insert an attachment |
---|
2136 | * |
---|
2137 | * {@internal Missing Long Description}} |
---|
2138 | * |
---|
2139 | * @package WordPress |
---|
2140 | * @subpackage Post |
---|
2141 | * @since 2.0 |
---|
2142 | * |
---|
2143 | * @uses $wpdb |
---|
2144 | * @uses $user_ID |
---|
2145 | * |
---|
2146 | * @param object $object attachment object |
---|
2147 | * @param string $file filename |
---|
2148 | * @param int $post_parent parent post ID |
---|
2149 | * @return int {@internal Missing Description}} |
---|
2150 | */ |
---|
2151 | function wp_insert_attachment($object, $file = false, $parent = 0) { |
---|
2152 | global $wpdb, $user_ID; |
---|
2153 | |
---|
2154 | $defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_ID, |
---|
2155 | 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0, |
---|
2156 | 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '', |
---|
2157 | 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => ''); |
---|
2158 | |
---|
2159 | $object = wp_parse_args($object, $defaults); |
---|
2160 | if ( !empty($parent) ) |
---|
2161 | $object['post_parent'] = $parent; |
---|
2162 | |
---|
2163 | $object = sanitize_post($object, 'db'); |
---|
2164 | |
---|
2165 | // export array as variables |
---|
2166 | extract($object, EXTR_SKIP); |
---|
2167 | |
---|
2168 | // Make sure we set a valid category |
---|
2169 | if (0 == count($post_category) || !is_array($post_category)) { |
---|
2170 | $post_category = array(get_option('default_category')); |
---|
2171 | } |
---|
2172 | |
---|
2173 | if ( empty($post_author) ) |
---|
2174 | $post_author = $user_ID; |
---|
2175 | |
---|
2176 | $post_type = 'attachment'; |
---|
2177 | $post_status = 'inherit'; |
---|
2178 | |
---|
2179 | // Are we updating or creating? |
---|
2180 | $update = false; |
---|
2181 | if ( !empty($ID) ) { |
---|
2182 | $update = true; |
---|
2183 | $post_ID = (int) $ID; |
---|
2184 | } |
---|
2185 | |
---|
2186 | // Create a valid post name. |
---|
2187 | if ( empty($post_name) ) |
---|
2188 | $post_name = sanitize_title($post_title); |
---|
2189 | else |
---|
2190 | $post_name = sanitize_title($post_name); |
---|
2191 | |
---|
2192 | // expected_slashed ($post_name) |
---|
2193 | $post_name_check = $wpdb->get_var( $wpdb->prepare( "SELECT post_name FROM $wpdb->posts WHERE post_name = '$post_name' AND post_status = 'inherit' AND ID != %d LIMIT 1", $post_ID)); |
---|
2194 | |
---|
2195 | if ($post_name_check) { |
---|
2196 | $suffix = 2; |
---|
2197 | while ($post_name_check) { |
---|
2198 | $alt_post_name = $post_name . "-$suffix"; |
---|
2199 | // expected_slashed ($alt_post_name, $post_name) |
---|
2200 | $post_name_check = $wpdb->get_var( $wpdb->prepare( "SELECT post_name FROM $wpdb->posts WHERE post_name = '$alt_post_name' AND post_status = 'inherit' AND ID != %d AND post_parent = %d LIMIT 1", $post_ID, $post_parent)); |
---|
2201 | $suffix++; |
---|
2202 | } |
---|
2203 | $post_name = $alt_post_name; |
---|
2204 | } |
---|
2205 | |
---|
2206 | if ( empty($post_date) ) |
---|
2207 | $post_date = current_time('mysql'); |
---|
2208 | if ( empty($post_date_gmt) ) |
---|
2209 | $post_date_gmt = current_time('mysql', 1); |
---|
2210 | |
---|
2211 | if ( empty($post_modified) ) |
---|
2212 | $post_modified = $post_date; |
---|
2213 | if ( empty($post_modified_gmt) ) |
---|
2214 | $post_modified_gmt = $post_date_gmt; |
---|
2215 | |
---|
2216 | if ( empty($comment_status) ) { |
---|
2217 | if ( $update ) |
---|
2218 | $comment_status = 'closed'; |
---|
2219 | else |
---|
2220 | $comment_status = get_option('default_comment_status'); |
---|
2221 | } |
---|
2222 | if ( empty($ping_status) ) |
---|
2223 | $ping_status = get_option('default_ping_status'); |
---|
2224 | |
---|
2225 | if ( isset($to_ping) ) |
---|
2226 | $to_ping = preg_replace('|\s+|', "\n", $to_ping); |
---|
2227 | else |
---|
2228 | $to_ping = ''; |
---|
2229 | |
---|
2230 | if ( isset($post_parent) ) |
---|
2231 | $post_parent = (int) $post_parent; |
---|
2232 | else |
---|
2233 | $post_parent = 0; |
---|
2234 | |
---|
2235 | if ( isset($menu_order) ) |
---|
2236 | $menu_order = (int) $menu_order; |
---|
2237 | else |
---|
2238 | $menu_order = 0; |
---|
2239 | |
---|
2240 | if ( !isset($post_password) ) |
---|
2241 | $post_password = ''; |
---|
2242 | |
---|
2243 | if ( ! isset($pinged) ) |
---|
2244 | $pinged = ''; |
---|
2245 | |
---|
2246 | // expected_slashed (everything!) |
---|
2247 | $data = compact( array( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type', 'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'menu_order', 'post_mime_type', 'guid' ) ); |
---|
2248 | $data = stripslashes_deep( $data ); |
---|
2249 | |
---|
2250 | if ( $update ) { |
---|
2251 | $wpdb->update( $wpdb->posts, $data, array( 'ID' => $post_ID ) ); |
---|
2252 | } else { |
---|
2253 | $wpdb->insert( $wpdb->posts, $data ); |
---|
2254 | $post_ID = (int) $wpdb->insert_id; |
---|
2255 | } |
---|
2256 | |
---|
2257 | if ( empty($post_name) ) { |
---|
2258 | $post_name = sanitize_title($post_title, $post_ID); |
---|
2259 | $wpdb->update( $wpdb->posts, compact("post_name"), array( 'ID' => $post_ID ) ); |
---|
2260 | } |
---|
2261 | |
---|
2262 | wp_set_post_categories($post_ID, $post_category); |
---|
2263 | |
---|
2264 | if ( $file ) |
---|
2265 | update_attached_file( $post_ID, $file ); |
---|
2266 | |
---|
2267 | clean_post_cache($post_ID); |
---|
2268 | |
---|
2269 | if ( $update) { |
---|
2270 | do_action('edit_attachment', $post_ID); |
---|
2271 | } else { |
---|
2272 | do_action('add_attachment', $post_ID); |
---|
2273 | } |
---|
2274 | |
---|
2275 | return $post_ID; |
---|
2276 | } |
---|
2277 | |
---|
2278 | /** |
---|
2279 | * wp_delete_attachment() - Delete an attachment |
---|
2280 | * |
---|
2281 | * {@internal Missing Long Description}} |
---|
2282 | * |
---|
2283 | * @package WordPress |
---|
2284 | * @subpackage Post |
---|
2285 | * @since 2.0 |
---|
2286 | * @uses $wpdb |
---|
2287 | * |
---|
2288 | * @param int $postid attachment Id |
---|
2289 | * @return mixed {@internal Missing Description}} |
---|
2290 | */ |
---|
2291 | function wp_delete_attachment($postid) { |
---|
2292 | global $wpdb; |
---|
2293 | |
---|
2294 | if ( !$post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d", $postid)) ) |
---|
2295 | return $post; |
---|
2296 | |
---|
2297 | if ( 'attachment' != $post->post_type ) |
---|
2298 | return false; |
---|
2299 | |
---|
2300 | $meta = wp_get_attachment_metadata( $postid ); |
---|
2301 | $file = get_attached_file( $postid ); |
---|
2302 | |
---|
2303 | /** @todo Delete for pluggable post taxonomies too */ |
---|
2304 | wp_delete_object_term_relationships($postid, array('category', 'post_tag')); |
---|
2305 | |
---|
2306 | $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->posts WHERE ID = %d", $postid )); |
---|
2307 | |
---|
2308 | $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->comments WHERE comment_post_ID = %d", $postid )); |
---|
2309 | |
---|
2310 | $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE post_id = %d ", $postid )); |
---|
2311 | |
---|
2312 | if ( ! empty($meta['thumb']) ) { |
---|
2313 | // Don't delete the thumb if another attachment uses it |
---|
2314 | if (! $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE %s AND post_id <> %d", '%'.$meta['thumb'].'%', $postid)) ) { |
---|
2315 | $thumbfile = str_replace(basename($file), $meta['thumb'], $file); |
---|
2316 | $thumbfile = apply_filters('wp_delete_file', $thumbfile); |
---|
2317 | @ unlink($thumbfile); |
---|
2318 | } |
---|
2319 | } |
---|
2320 | |
---|
2321 | // remove intermediate images if there are any |
---|
2322 | $sizes = apply_filters('intermediate_image_sizes', array('thumbnail', 'medium')); |
---|
2323 | foreach ( $sizes as $size ) { |
---|
2324 | if ( $intermediate = image_get_intermediate_size($postid, $size) ) { |
---|
2325 | $intermediate_file = apply_filters('wp_delete_file', $intermediate['path']); |
---|
2326 | @ unlink($intermediate_file); |
---|
2327 | } |
---|
2328 | } |
---|
2329 | |
---|
2330 | $file = apply_filters('wp_delete_file', $file); |
---|
2331 | |
---|
2332 | if ( ! empty($file) ) |
---|
2333 | @ unlink($file); |
---|
2334 | |
---|
2335 | clean_post_cache($postid); |
---|
2336 | |
---|
2337 | do_action('delete_attachment', $postid); |
---|
2338 | |
---|
2339 | return $post; |
---|
2340 | } |
---|
2341 | |
---|
2342 | /** |
---|
2343 | * wp_get_attachment_metadata() - Retrieve metadata for an attachment |
---|
2344 | * |
---|
2345 | * {@internal Missing Long Description}} |
---|
2346 | * |
---|
2347 | * @package WordPress |
---|
2348 | * @subpackage Post |
---|
2349 | * @since 2.1 |
---|
2350 | * |
---|
2351 | * @param int $post_id attachment ID |
---|
2352 | * @param bool $unfiltered Optional, default is false. If true, filters are not run |
---|
2353 | * @return array {@internal Missing Description}} |
---|
2354 | */ |
---|
2355 | function wp_get_attachment_metadata( $post_id, $unfiltered = false ) { |
---|
2356 | $post_id = (int) $post_id; |
---|
2357 | if ( !$post =& get_post( $post_id ) ) |
---|
2358 | return false; |
---|
2359 | |
---|
2360 | $data = get_post_meta( $post->ID, '_wp_attachment_metadata', true ); |
---|
2361 | if ( $unfiltered ) |
---|
2362 | return $data; |
---|
2363 | return apply_filters( 'wp_get_attachment_metadata', $data, $post->ID ); |
---|
2364 | } |
---|
2365 | |
---|
2366 | /** |
---|
2367 | * wp_update_attachment_metadata() - Update metadata for an attachment |
---|
2368 | * |
---|
2369 | * {@internal Missing Long Description}} |
---|
2370 | * |
---|
2371 | * @package WordPress |
---|
2372 | * @subpackage Post |
---|
2373 | * @since 2.1 |
---|
2374 | * |
---|
2375 | * @param int $post_id attachment ID |
---|
2376 | * @param array $data attachment data |
---|
2377 | * @return int {@internal Missing Description}} |
---|
2378 | */ |
---|
2379 | function wp_update_attachment_metadata( $post_id, $data ) { |
---|
2380 | $post_id = (int) $post_id; |
---|
2381 | if ( !$post =& get_post( $post_id ) ) |
---|
2382 | return false; |
---|
2383 | |
---|
2384 | $data = apply_filters( 'wp_update_attachment_metadata', $data, $post->ID ); |
---|
2385 | |
---|
2386 | return update_post_meta( $post->ID, '_wp_attachment_metadata', $data); |
---|
2387 | } |
---|
2388 | |
---|
2389 | /** |
---|
2390 | * wp_get_attachment_url() - Retrieve the URL for an attachment |
---|
2391 | * |
---|
2392 | * {@internal Missing Long Description}} |
---|
2393 | * |
---|
2394 | * @package WordPress |
---|
2395 | * @subpackage Post |
---|
2396 | * @since 2.1 |
---|
2397 | * |
---|
2398 | * @param int $post_id attachment ID |
---|
2399 | * @return string {@internal Missing Description}} |
---|
2400 | */ |
---|
2401 | function wp_get_attachment_url( $post_id = 0 ) { |
---|
2402 | $post_id = (int) $post_id; |
---|
2403 | if ( !$post =& get_post( $post_id ) ) |
---|
2404 | return false; |
---|
2405 | |
---|
2406 | $url = get_the_guid( $post->ID ); |
---|
2407 | |
---|
2408 | if ( 'attachment' != $post->post_type || !$url ) |
---|
2409 | return false; |
---|
2410 | |
---|
2411 | return apply_filters( 'wp_get_attachment_url', $url, $post->ID ); |
---|
2412 | } |
---|
2413 | |
---|
2414 | /** |
---|
2415 | * wp_get_attachment_thumb_file() - Retrieve thumbnail for an attachment |
---|
2416 | * |
---|
2417 | * {@internal Missing Long Description}} |
---|
2418 | * |
---|
2419 | * @package WordPress |
---|
2420 | * @subpackage Post |
---|
2421 | * @since 2.1 |
---|
2422 | * |
---|
2423 | * @param int $post_id attachment ID |
---|
2424 | * @return mixed {@internal Missing Description}} |
---|
2425 | */ |
---|
2426 | function wp_get_attachment_thumb_file( $post_id = 0 ) { |
---|
2427 | $post_id = (int) $post_id; |
---|
2428 | if ( !$post =& get_post( $post_id ) ) |
---|
2429 | return false; |
---|
2430 | if ( !is_array( $imagedata = wp_get_attachment_metadata( $post->ID ) ) ) |
---|
2431 | return false; |
---|
2432 | |
---|
2433 | $file = get_attached_file( $post->ID ); |
---|
2434 | |
---|
2435 | if ( !empty($imagedata['thumb']) && ($thumbfile = str_replace(basename($file), $imagedata['thumb'], $file)) && file_exists($thumbfile) ) |
---|
2436 | return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID ); |
---|
2437 | return false; |
---|
2438 | } |
---|
2439 | |
---|
2440 | /** |
---|
2441 | * wp_get_attachment_thumb_url() - Retrieve URL for an attachment thumbnail |
---|
2442 | * |
---|
2443 | * {@internal Missing Long Description}} |
---|
2444 | * |
---|
2445 | * @package WordPress |
---|
2446 | * @subpackage Post |
---|
2447 | * @since 2.1 |
---|
2448 | * |
---|
2449 | * @param int $post_id attachment ID |
---|
2450 | * @return string {@internal Missing Description}} |
---|
2451 | */ |
---|
2452 | function wp_get_attachment_thumb_url( $post_id = 0 ) { |
---|
2453 | $post_id = (int) $post_id; |
---|
2454 | if ( !$post =& get_post( $post_id ) ) |
---|
2455 | return false; |
---|
2456 | if ( !$url = wp_get_attachment_url( $post->ID ) ) |
---|
2457 | return false; |
---|
2458 | |
---|
2459 | $sized = image_downsize( $post_id, 'thumbnail' ); |
---|
2460 | if ( $sized ) |
---|
2461 | return $sized[0]; |
---|
2462 | |
---|
2463 | if ( !$thumb = wp_get_attachment_thumb_file( $post->ID ) ) |
---|
2464 | return false; |
---|
2465 | |
---|
2466 | $url = str_replace(basename($url), basename($thumb), $url); |
---|
2467 | |
---|
2468 | return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID ); |
---|
2469 | } |
---|
2470 | |
---|
2471 | /** |
---|
2472 | * wp_attachment_is_image() - Check if the attachment is an image |
---|
2473 | * |
---|
2474 | * {@internal Missing Long Description}} |
---|
2475 | * |
---|
2476 | * @package WordPress |
---|
2477 | * @subpackage Post |
---|
2478 | * @since 2.1 |
---|
2479 | * |
---|
2480 | * @param int $post_id attachment ID |
---|
2481 | * @return bool {@internal Missing Description}} |
---|
2482 | */ |
---|
2483 | function wp_attachment_is_image( $post_id = 0 ) { |
---|
2484 | $post_id = (int) $post_id; |
---|
2485 | if ( !$post =& get_post( $post_id ) ) |
---|
2486 | return false; |
---|
2487 | |
---|
2488 | if ( !$file = get_attached_file( $post->ID ) ) |
---|
2489 | return false; |
---|
2490 | |
---|
2491 | $ext = preg_match('/\.([^.]+)$/', $file, $matches) ? strtolower($matches[1]) : false; |
---|
2492 | |
---|
2493 | $image_exts = array('jpg', 'jpeg', 'gif', 'png'); |
---|
2494 | |
---|
2495 | if ( 'image/' == substr($post->post_mime_type, 0, 6) || $ext && 'import' == $post->post_mime_type && in_array($ext, $image_exts) ) |
---|
2496 | return true; |
---|
2497 | return false; |
---|
2498 | } |
---|
2499 | |
---|
2500 | /** |
---|
2501 | * wp_mime_type_icon() - Retrieve the icon for a MIME type |
---|
2502 | * |
---|
2503 | * {@internal Missing Long Description}} |
---|
2504 | * |
---|
2505 | * @package WordPress |
---|
2506 | * @subpackage Post |
---|
2507 | * @since 2.1 |
---|
2508 | * |
---|
2509 | * @param string $mime MIME type |
---|
2510 | * @return string|bool {@internal Missing Description}} |
---|
2511 | */ |
---|
2512 | function wp_mime_type_icon( $mime = 0 ) { |
---|
2513 | if ( !is_numeric($mime) ) |
---|
2514 | $icon = wp_cache_get("mime_type_icon_$mime"); |
---|
2515 | if ( empty($icon) ) { |
---|
2516 | $post_id = 0; |
---|
2517 | $post_mimes = array(); |
---|
2518 | if ( is_numeric($mime) ) { |
---|
2519 | $mime = (int) $mime; |
---|
2520 | if ( $post =& get_post( $mime ) ) { |
---|
2521 | $post_id = (int) $post->ID; |
---|
2522 | $ext = preg_replace('/^.+?\.([^.]+)$/', '$1', $post->guid); |
---|
2523 | if ( !empty($ext) ) { |
---|
2524 | $post_mimes[] = $ext; |
---|
2525 | if ( $ext_type = wp_ext2type( $ext ) ) |
---|
2526 | $post_mimes[] = $ext_type; |
---|
2527 | } |
---|
2528 | $mime = $post->post_mime_type; |
---|
2529 | } else { |
---|
2530 | $mime = 0; |
---|
2531 | } |
---|
2532 | } else { |
---|
2533 | $post_mimes[] = $mime; |
---|
2534 | } |
---|
2535 | |
---|
2536 | $icon_files = wp_cache_get('icon_files'); |
---|
2537 | |
---|
2538 | if ( !is_array($icon_files) ) { |
---|
2539 | $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/crystal' ); |
---|
2540 | $icon_dir_uri = apply_filters( 'icon_dir_uri', includes_url('images/crystal') ); |
---|
2541 | $dirs = apply_filters( 'icon_dirs', array($icon_dir => $icon_dir_uri) ); |
---|
2542 | $icon_files = array(); |
---|
2543 | while ( $dirs ) { |
---|
2544 | $dir = array_shift($keys = array_keys($dirs)); |
---|
2545 | $uri = array_shift($dirs); |
---|
2546 | if ( $dh = opendir($dir) ) { |
---|
2547 | while ( false !== $file = readdir($dh) ) { |
---|
2548 | $file = basename($file); |
---|
2549 | if ( substr($file, 0, 1) == '.' ) |
---|
2550 | continue; |
---|
2551 | if ( !in_array(strtolower(substr($file, -4)), array('.png', '.gif', '.jpg') ) ) { |
---|
2552 | if ( is_dir("$dir/$file") ) |
---|
2553 | $dirs["$dir/$file"] = "$uri/$file"; |
---|
2554 | continue; |
---|
2555 | } |
---|
2556 | $icon_files["$dir/$file"] = "$uri/$file"; |
---|
2557 | } |
---|
2558 | closedir($dh); |
---|
2559 | } |
---|
2560 | } |
---|
2561 | wp_cache_set('icon_files', $icon_files, 600); |
---|
2562 | } |
---|
2563 | |
---|
2564 | // Icon basename - extension = MIME wildcard |
---|
2565 | foreach ( $icon_files as $file => $uri ) |
---|
2566 | $types[ preg_replace('/^([^.]*).*$/', '$1', basename($file)) ] =& $icon_files[$file]; |
---|
2567 | |
---|
2568 | if ( ! empty($mime) ) { |
---|
2569 | $post_mimes[] = substr($mime, 0, strpos($mime, '/')); |
---|
2570 | $post_mimes[] = substr($mime, strpos($mime, '/') + 1); |
---|
2571 | $post_mimes[] = str_replace('/', '_', $mime); |
---|
2572 | } |
---|
2573 | |
---|
2574 | $matches = wp_match_mime_types(array_keys($types), $post_mimes); |
---|
2575 | $matches['default'] = array('default'); |
---|
2576 | |
---|
2577 | foreach ( $matches as $match => $wilds ) { |
---|
2578 | if ( isset($types[$wilds[0]])) { |
---|
2579 | $icon = $types[$wilds[0]]; |
---|
2580 | if ( !is_numeric($mime) ) |
---|
2581 | wp_cache_set("mime_type_icon_$mime", $icon); |
---|
2582 | break; |
---|
2583 | } |
---|
2584 | } |
---|
2585 | } |
---|
2586 | |
---|
2587 | return apply_filters( 'wp_mime_type_icon', $icon, $mime, $post_id ); // Last arg is 0 if function pass mime type. |
---|
2588 | } |
---|
2589 | |
---|
2590 | /** |
---|
2591 | * wp_check_for_changed_slugs() - {@internal Missing Short Description}} |
---|
2592 | * |
---|
2593 | * {@internal Missing Long Description}} |
---|
2594 | * |
---|
2595 | * @package WordPress |
---|
2596 | * @subpackage Post |
---|
2597 | * @since 2.1 |
---|
2598 | * |
---|
2599 | * @param int $post_id The Post ID |
---|
2600 | * @return int Same as $post_id |
---|
2601 | */ |
---|
2602 | function wp_check_for_changed_slugs($post_id) { |
---|
2603 | if ( !isset($_POST['wp-old-slug']) || !strlen($_POST['wp-old-slug']) ) |
---|
2604 | return $post_id; |
---|
2605 | |
---|
2606 | $post = &get_post($post_id); |
---|
2607 | |
---|
2608 | // we're only concerned with published posts |
---|
2609 | if ( $post->post_status != 'publish' || $post->post_type != 'post' ) |
---|
2610 | return $post_id; |
---|
2611 | |
---|
2612 | // only bother if the slug has changed |
---|
2613 | if ( $post->post_name == $_POST['wp-old-slug'] ) |
---|
2614 | return $post_id; |
---|
2615 | |
---|
2616 | $old_slugs = (array) get_post_meta($post_id, '_wp_old_slug'); |
---|
2617 | |
---|
2618 | // if we haven't added this old slug before, add it now |
---|
2619 | if ( !count($old_slugs) || !in_array($_POST['wp-old-slug'], $old_slugs) ) |
---|
2620 | add_post_meta($post_id, '_wp_old_slug', $_POST['wp-old-slug']); |
---|
2621 | |
---|
2622 | // if the new slug was used previously, delete it from the list |
---|
2623 | if ( in_array($post->post_name, $old_slugs) ) |
---|
2624 | delete_post_meta($post_id, '_wp_old_slug', $post->post_name); |
---|
2625 | |
---|
2626 | return $post_id; |
---|
2627 | } |
---|
2628 | |
---|
2629 | /** |
---|
2630 | * get_private_posts_cap_sql() - {@internal Missing Short Description}} |
---|
2631 | * |
---|
2632 | * This function provides a standardized way to appropriately select on |
---|
2633 | * the post_status of posts/pages. The function will return a piece of |
---|
2634 | * SQL code that can be added to a WHERE clause; this SQL is constructed |
---|
2635 | * to allow all published posts, and all private posts to which the user |
---|
2636 | * has access. |
---|
2637 | * |
---|
2638 | * @package WordPress |
---|
2639 | * @subpackage Post |
---|
2640 | * @since 2.2 |
---|
2641 | * |
---|
2642 | * @uses $user_ID |
---|
2643 | * @uses apply_filters() Call 'pub_priv_sql_capability' filter for plugins with different post types |
---|
2644 | * |
---|
2645 | * @param string $post_type currently only supports 'post' or 'page'. |
---|
2646 | * @return string SQL code that can be added to a where clause. |
---|
2647 | */ |
---|
2648 | function get_private_posts_cap_sql($post_type) { |
---|
2649 | global $user_ID; |
---|
2650 | $cap = ''; |
---|
2651 | |
---|
2652 | // Private posts |
---|
2653 | if ($post_type == 'post') { |
---|
2654 | $cap = 'read_private_posts'; |
---|
2655 | // Private pages |
---|
2656 | } elseif ($post_type == 'page') { |
---|
2657 | $cap = 'read_private_pages'; |
---|
2658 | // Dunno what it is, maybe plugins have their own post type? |
---|
2659 | } else { |
---|
2660 | $cap = apply_filters('pub_priv_sql_capability', $cap); |
---|
2661 | |
---|
2662 | if (empty($cap)) { |
---|
2663 | // We don't know what it is, filters don't change anything, |
---|
2664 | // so set the SQL up to return nothing. |
---|
2665 | return '1 = 0'; |
---|
2666 | } |
---|
2667 | } |
---|
2668 | |
---|
2669 | $sql = '(post_status = \'publish\''; |
---|
2670 | |
---|
2671 | if (current_user_can($cap)) { |
---|
2672 | // Does the user have the capability to view private posts? Guess so. |
---|
2673 | $sql .= ' OR post_status = \'private\''; |
---|
2674 | } elseif (is_user_logged_in()) { |
---|
2675 | // Users can view their own private posts. |
---|
2676 | $sql .= ' OR post_status = \'private\' AND post_author = \'' . $user_ID . '\''; |
---|
2677 | } |
---|
2678 | |
---|
2679 | $sql .= ')'; |
---|
2680 | |
---|
2681 | return $sql; |
---|
2682 | } |
---|
2683 | |
---|
2684 | /** |
---|
2685 | * get_lastpostdate() - {@internal Missing Short Description}} |
---|
2686 | * |
---|
2687 | * {@internal Missing Long Description}} |
---|
2688 | * |
---|
2689 | * @package WordPress |
---|
2690 | * @subpackage Post |
---|
2691 | * @since 0.71 |
---|
2692 | * |
---|
2693 | * @uses $wpdb |
---|
2694 | * @uses $blog_id |
---|
2695 | * @uses apply_filters() Calls 'get_lastpostdate' filter |
---|
2696 | * |
---|
2697 | * @global mixed $cache_lastpostdate Stores the last post date |
---|
2698 | * @global mixed $pagenow The current page being viewed |
---|
2699 | * |
---|
2700 | * @param string $timezone The location to get the time. Can be 'gmt', 'blog', or 'server'. |
---|
2701 | * @return string The date of the last post. |
---|
2702 | */ |
---|
2703 | function get_lastpostdate($timezone = 'server') { |
---|
2704 | global $cache_lastpostdate, $wpdb, $blog_id; |
---|
2705 | $add_seconds_server = date('Z'); |
---|
2706 | if ( !isset($cache_lastpostdate[$blog_id][$timezone]) ) { |
---|
2707 | switch(strtolower($timezone)) { |
---|
2708 | case 'gmt': |
---|
2709 | $lastpostdate = $wpdb->get_var("SELECT post_date_gmt FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1"); |
---|
2710 | break; |
---|
2711 | case 'blog': |
---|
2712 | $lastpostdate = $wpdb->get_var("SELECT post_date FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1"); |
---|
2713 | break; |
---|
2714 | case 'server': |
---|
2715 | $lastpostdate = $wpdb->get_var("SELECT DATE_ADD(post_date_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1"); |
---|
2716 | break; |
---|
2717 | } |
---|
2718 | $cache_lastpostdate[$blog_id][$timezone] = $lastpostdate; |
---|
2719 | } else { |
---|
2720 | $lastpostdate = $cache_lastpostdate[$blog_id][$timezone]; |
---|
2721 | } |
---|
2722 | return apply_filters( 'get_lastpostdate', $lastpostdate, $timezone ); |
---|
2723 | } |
---|
2724 | |
---|
2725 | /** |
---|
2726 | * get_lastpostmodified() - {@internal Missing Short Description}} |
---|
2727 | * |
---|
2728 | * {@internal Missing Long Description}} |
---|
2729 | * |
---|
2730 | * @package WordPress |
---|
2731 | * @subpackage Post |
---|
2732 | * @since 1.2 |
---|
2733 | * |
---|
2734 | * @uses $wpdb |
---|
2735 | * @uses $blog_id |
---|
2736 | * @uses apply_filters() Calls 'get_lastpostmodified' filter |
---|
2737 | * |
---|
2738 | * @global mixed $cache_lastpostmodified Stores the date the last post was modified |
---|
2739 | * @global mixed $pagenow The current page being viewed |
---|
2740 | * |
---|
2741 | * @param string $timezone The location to get the time. Can be 'gmt', 'blog', or 'server'. |
---|
2742 | * @return string The date the post was last modified. |
---|
2743 | */ |
---|
2744 | function get_lastpostmodified($timezone = 'server') { |
---|
2745 | global $cache_lastpostmodified, $wpdb, $blog_id; |
---|
2746 | $add_seconds_server = date('Z'); |
---|
2747 | if ( !isset($cache_lastpostmodified[$blog_id][$timezone]) ) { |
---|
2748 | switch(strtolower($timezone)) { |
---|
2749 | case 'gmt': |
---|
2750 | $lastpostmodified = $wpdb->get_var("SELECT post_modified_gmt FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1"); |
---|
2751 | break; |
---|
2752 | case 'blog': |
---|
2753 | $lastpostmodified = $wpdb->get_var("SELECT post_modified FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1"); |
---|
2754 | break; |
---|
2755 | case 'server': |
---|
2756 | $lastpostmodified = $wpdb->get_var("SELECT DATE_ADD(post_modified_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1"); |
---|
2757 | break; |
---|
2758 | } |
---|
2759 | $lastpostdate = get_lastpostdate($timezone); |
---|
2760 | if ( $lastpostdate > $lastpostmodified ) { |
---|
2761 | $lastpostmodified = $lastpostdate; |
---|
2762 | } |
---|
2763 | $cache_lastpostmodified[$blog_id][$timezone] = $lastpostmodified; |
---|
2764 | } else { |
---|
2765 | $lastpostmodified = $cache_lastpostmodified[$blog_id][$timezone]; |
---|
2766 | } |
---|
2767 | return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone ); |
---|
2768 | } |
---|
2769 | |
---|
2770 | /** |
---|
2771 | * update_post_cache() - Updates posts in cache |
---|
2772 | * |
---|
2773 | * @usedby update_page_cache() update_page_cache() aliased by this function. |
---|
2774 | * |
---|
2775 | * @package WordPress |
---|
2776 | * @subpackage Cache |
---|
2777 | * @since 1.5.1 |
---|
2778 | * |
---|
2779 | * @param array $posts Array of post objects |
---|
2780 | */ |
---|
2781 | function update_post_cache(&$posts) { |
---|
2782 | if ( !$posts ) |
---|
2783 | return; |
---|
2784 | |
---|
2785 | foreach ( $posts as $post ) |
---|
2786 | wp_cache_add($post->ID, $post, 'posts'); |
---|
2787 | } |
---|
2788 | |
---|
2789 | /** |
---|
2790 | * clean_post_cache() - Will clean the post in the cache |
---|
2791 | * |
---|
2792 | * Cleaning means delete from the cache of the post. Will call to clean |
---|
2793 | * the term object cache associated with the post ID. |
---|
2794 | * |
---|
2795 | * @package WordPress |
---|
2796 | * @subpackage Cache |
---|
2797 | * @since 2.0 |
---|
2798 | * |
---|
2799 | * @uses do_action() Will call the 'clean_post_cache' hook action. |
---|
2800 | * |
---|
2801 | * @param int $id The Post ID in the cache to clean |
---|
2802 | */ |
---|
2803 | function clean_post_cache($id) { |
---|
2804 | global $wpdb; |
---|
2805 | $id = (int) $id; |
---|
2806 | |
---|
2807 | wp_cache_delete($id, 'posts'); |
---|
2808 | wp_cache_delete($id, 'post_meta'); |
---|
2809 | |
---|
2810 | clean_object_term_cache($id, 'post'); |
---|
2811 | |
---|
2812 | wp_cache_delete( 'wp_get_archives', 'general' ); |
---|
2813 | |
---|
2814 | do_action('clean_post_cache', $id); |
---|
2815 | |
---|
2816 | if ( $children = $wpdb->get_col( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_parent = %d", $id) ) ) { |
---|
2817 | foreach( $children as $cid ) |
---|
2818 | clean_post_cache( $cid ); |
---|
2819 | } |
---|
2820 | } |
---|
2821 | |
---|
2822 | /** |
---|
2823 | * update_page_cache() - Alias of update_post_cache() |
---|
2824 | * |
---|
2825 | * @see update_post_cache() Posts and pages are the same, alias is intentional |
---|
2826 | * |
---|
2827 | * @package WordPress |
---|
2828 | * @subpackage Cache |
---|
2829 | * @since 1.5.1 |
---|
2830 | * |
---|
2831 | * @param array $pages list of page objects |
---|
2832 | */ |
---|
2833 | function update_page_cache(&$pages) { |
---|
2834 | update_post_cache($pages); |
---|
2835 | } |
---|
2836 | |
---|
2837 | /** |
---|
2838 | * clean_page_cache() - Will clean the page in the cache |
---|
2839 | * |
---|
2840 | * Clean (read: delete) page from cache that matches $id. Will also clean |
---|
2841 | * cache associated with 'all_page_ids' and 'get_pages'. |
---|
2842 | * |
---|
2843 | * @package WordPress |
---|
2844 | * @subpackage Cache |
---|
2845 | * @since 2.0 |
---|
2846 | * |
---|
2847 | * @uses do_action() Will call the 'clean_page_cache' hook action. |
---|
2848 | * |
---|
2849 | * @param int $id Page ID to clean |
---|
2850 | */ |
---|
2851 | function clean_page_cache($id) { |
---|
2852 | clean_post_cache($id); |
---|
2853 | |
---|
2854 | wp_cache_delete( 'all_page_ids', 'posts' ); |
---|
2855 | wp_cache_delete( 'get_pages', 'posts' ); |
---|
2856 | |
---|
2857 | do_action('clean_page_cache', $id); |
---|
2858 | } |
---|
2859 | |
---|
2860 | /** |
---|
2861 | * update_post_caches() - Call major cache updating functions for list of Post objects. |
---|
2862 | * |
---|
2863 | * @package WordPress |
---|
2864 | * @subpackage Cache |
---|
2865 | * @since 1.5 |
---|
2866 | * |
---|
2867 | * @uses $wpdb |
---|
2868 | * @uses update_post_cache() |
---|
2869 | * @uses update_object_term_cache() |
---|
2870 | * @uses update_postmeta_cache() |
---|
2871 | * |
---|
2872 | * @param array $posts Array of Post objects |
---|
2873 | */ |
---|
2874 | function update_post_caches(&$posts) { |
---|
2875 | // No point in doing all this work if we didn't match any posts. |
---|
2876 | if ( !$posts ) |
---|
2877 | return; |
---|
2878 | |
---|
2879 | update_post_cache($posts); |
---|
2880 | |
---|
2881 | $post_ids = array(); |
---|
2882 | |
---|
2883 | for ($i = 0; $i < count($posts); $i++) |
---|
2884 | $post_ids[] = $posts[$i]->ID; |
---|
2885 | |
---|
2886 | update_object_term_cache($post_ids, 'post'); |
---|
2887 | |
---|
2888 | update_postmeta_cache($post_ids); |
---|
2889 | } |
---|
2890 | |
---|
2891 | /** |
---|
2892 | * update_postmeta_cache() - {@internal Missing Short Description}} |
---|
2893 | * |
---|
2894 | * {@internal Missing Long Description}} |
---|
2895 | * |
---|
2896 | * @package WordPress |
---|
2897 | * @subpackage Cache |
---|
2898 | * @since 2.1 |
---|
2899 | * |
---|
2900 | * @uses $wpdb |
---|
2901 | * |
---|
2902 | * @param array $post_ids {@internal Missing Description}} |
---|
2903 | * @return bool|array Returns false if there is nothing to update or an array of metadata |
---|
2904 | */ |
---|
2905 | function update_postmeta_cache($post_ids) { |
---|
2906 | global $wpdb; |
---|
2907 | |
---|
2908 | if ( empty( $post_ids ) ) |
---|
2909 | return false; |
---|
2910 | |
---|
2911 | if ( !is_array($post_ids) ) { |
---|
2912 | $post_ids = preg_replace('|[^0-9,]|', '', $post_ids); |
---|
2913 | $post_ids = explode(',', $post_ids); |
---|
2914 | } |
---|
2915 | |
---|
2916 | $post_ids = array_map('intval', $post_ids); |
---|
2917 | |
---|
2918 | $ids = array(); |
---|
2919 | foreach ( (array) $post_ids as $id ) { |
---|
2920 | if ( false === wp_cache_get($id, 'post_meta') ) |
---|
2921 | $ids[] = $id; |
---|
2922 | } |
---|
2923 | |
---|
2924 | if ( empty( $ids ) ) |
---|
2925 | return false; |
---|
2926 | |
---|
2927 | // Get post-meta info |
---|
2928 | $id_list = join(',', $ids); |
---|
2929 | $cache = array(); |
---|
2930 | if ( $meta_list = $wpdb->get_results("SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta WHERE post_id IN ($id_list) ORDER BY post_id, meta_key", ARRAY_A) ) { |
---|
2931 | foreach ( (array) $meta_list as $metarow) { |
---|
2932 | $mpid = (int) $metarow['post_id']; |
---|
2933 | $mkey = $metarow['meta_key']; |
---|
2934 | $mval = $metarow['meta_value']; |
---|
2935 | |
---|
2936 | // Force subkeys to be array type: |
---|
2937 | if ( !isset($cache[$mpid]) || !is_array($cache[$mpid]) ) |
---|
2938 | $cache[$mpid] = array(); |
---|
2939 | if ( !isset($cache[$mpid][$mkey]) || !is_array($cache[$mpid][$mkey]) ) |
---|
2940 | $cache[$mpid][$mkey] = array(); |
---|
2941 | |
---|
2942 | // Add a value to the current pid/key: |
---|
2943 | $cache[$mpid][$mkey][] = $mval; |
---|
2944 | } |
---|
2945 | } |
---|
2946 | |
---|
2947 | foreach ( (array) $ids as $id ) { |
---|
2948 | if ( ! isset($cache[$id]) ) |
---|
2949 | $cache[$id] = array(); |
---|
2950 | } |
---|
2951 | |
---|
2952 | foreach ( array_keys($cache) as $post) |
---|
2953 | wp_cache_set($post, $cache[$post], 'post_meta'); |
---|
2954 | |
---|
2955 | return $cache; |
---|
2956 | } |
---|
2957 | |
---|
2958 | // |
---|
2959 | // Hooks |
---|
2960 | // |
---|
2961 | |
---|
2962 | /** |
---|
2963 | * _transition_post_status() - Hook {@internal Missing Short Description}} |
---|
2964 | * |
---|
2965 | * {@internal Missing Long Description}} |
---|
2966 | * |
---|
2967 | * @package WordPress |
---|
2968 | * @subpackage Post |
---|
2969 | * @since 2.3 |
---|
2970 | * |
---|
2971 | * @uses $wpdb |
---|
2972 | * |
---|
2973 | * @param string $new_status {@internal Missing Description}} |
---|
2974 | * @param string $old_status {@internal Missing Description}} |
---|
2975 | * @param object $post Object type containing the post information |
---|
2976 | */ |
---|
2977 | function _transition_post_status($new_status, $old_status, $post) { |
---|
2978 | global $wpdb; |
---|
2979 | |
---|
2980 | if ( $old_status != 'publish' && $new_status == 'publish' ) { |
---|
2981 | // Reset GUID if transitioning to publish and it is empty |
---|
2982 | if ( '' == get_the_guid($post->ID) ) |
---|
2983 | $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post->ID ) ), array( 'ID' => $post->ID ) ); |
---|
2984 | do_action('private_to_published', $post->ID); // Deprecated, use private_to_publish |
---|
2985 | } |
---|
2986 | |
---|
2987 | // Always clears the hook in case the post status bounced from future to draft. |
---|
2988 | wp_clear_scheduled_hook('publish_future_post', $post->ID); |
---|
2989 | } |
---|
2990 | |
---|
2991 | /** |
---|
2992 | * _future_post_hook() - Hook used to schedule publication for a post marked for the future. |
---|
2993 | * |
---|
2994 | * The $post properties used and must exist are 'ID' and 'post_date_gmt'. |
---|
2995 | * |
---|
2996 | * @package WordPress |
---|
2997 | * @subpackage Post |
---|
2998 | * @since 2.3 |
---|
2999 | * |
---|
3000 | * @param int $post_id Not Used. Can be set to null. |
---|
3001 | * @param object $post Object type containing the post information |
---|
3002 | */ |
---|
3003 | function _future_post_hook($deprecated = '', $post) { |
---|
3004 | wp_clear_scheduled_hook( 'publish_future_post', $post->ID ); |
---|
3005 | wp_schedule_single_event(strtotime($post->post_date_gmt. ' GMT'), 'publish_future_post', array($post->ID)); |
---|
3006 | } |
---|
3007 | |
---|
3008 | /** |
---|
3009 | * _publish_post_hook() - Hook {@internal Missing Short Description}} |
---|
3010 | * |
---|
3011 | * {@internal Missing Long Description}} |
---|
3012 | * |
---|
3013 | * @package WordPress |
---|
3014 | * @subpackage Post |
---|
3015 | * @since 2.3 |
---|
3016 | * |
---|
3017 | * @uses $wpdb |
---|
3018 | * @uses XMLRPC_REQUEST |
---|
3019 | * @uses APP_REQUEST |
---|
3020 | * @uses do_action Calls 'xmlprc_publish_post' action if XMLRPC_REQUEST is defined. Calls 'app_publish_post' |
---|
3021 | * action if APP_REQUEST is defined. |
---|
3022 | * |
---|
3023 | * @param int $post_id The ID in the database table of the post being published |
---|
3024 | */ |
---|
3025 | function _publish_post_hook($post_id) { |
---|
3026 | global $wpdb; |
---|
3027 | |
---|
3028 | if ( defined('XMLRPC_REQUEST') ) |
---|
3029 | do_action('xmlrpc_publish_post', $post_id); |
---|
3030 | if ( defined('APP_REQUEST') ) |
---|
3031 | do_action('app_publish_post', $post_id); |
---|
3032 | |
---|
3033 | if ( defined('WP_IMPORTING') ) |
---|
3034 | return; |
---|
3035 | |
---|
3036 | $data = array( 'post_id' => $post_id, 'meta_value' => '1' ); |
---|
3037 | if ( get_option('default_pingback_flag') ) |
---|
3038 | $wpdb->insert( $wpdb->postmeta, $data + array( 'meta_key' => '_pingme' ) ); |
---|
3039 | $wpdb->insert( $wpdb->postmeta, $data + array( 'meta_key' => '_encloseme' ) ); |
---|
3040 | wp_schedule_single_event(time(), 'do_pings'); |
---|
3041 | } |
---|
3042 | |
---|
3043 | /** |
---|
3044 | * _save_post_hook() - Hook used to prevent page/post cache and rewrite rules from staying dirty |
---|
3045 | * |
---|
3046 | * Does two things. If the post is a page and has a template then it will update/add that |
---|
3047 | * template to the meta. For both pages and posts, it will clean the post cache to make sure |
---|
3048 | * that the cache updates to the changes done recently. For pages, the rewrite rules of |
---|
3049 | * WordPress are flushed to allow for any changes. |
---|
3050 | * |
---|
3051 | * The $post parameter, only uses 'post_type' property and 'page_template' property. |
---|
3052 | * |
---|
3053 | * @package WordPress |
---|
3054 | * @subpackage Post |
---|
3055 | * @since 2.3 |
---|
3056 | * |
---|
3057 | * @uses $wp_rewrite Flushes Rewrite Rules. |
---|
3058 | * |
---|
3059 | * @param int $post_id The ID in the database table for the $post |
---|
3060 | * @param object $post Object type containing the post information |
---|
3061 | */ |
---|
3062 | function _save_post_hook($post_id, $post) { |
---|
3063 | if ( $post->post_type == 'page' ) { |
---|
3064 | clean_page_cache($post_id); |
---|
3065 | global $wp_rewrite; |
---|
3066 | $wp_rewrite->flush_rules(); |
---|
3067 | } else { |
---|
3068 | clean_post_cache($post_id); |
---|
3069 | } |
---|
3070 | } |
---|
3071 | |
---|
3072 | // |
---|
3073 | // Private |
---|
3074 | // |
---|
3075 | |
---|
3076 | function _get_post_ancestors(&$_post) { |
---|
3077 | global $wpdb; |
---|
3078 | |
---|
3079 | if ( isset($_post->ancestors) ) |
---|
3080 | return; |
---|
3081 | |
---|
3082 | $_post->ancestors = array(); |
---|
3083 | |
---|
3084 | if ( empty($_post->post_parent) || $_post->ID == $_post->post_parent ) |
---|
3085 | return; |
---|
3086 | |
---|
3087 | $id = $_post->ancestors[] = $_post->post_parent; |
---|
3088 | while ( $ancestor = $wpdb->get_var( $wpdb->prepare("SELECT `post_parent` FROM $wpdb->posts WHERE ID = %d LIMIT 1", $id) ) ) { |
---|
3089 | if ( $id == $ancestor ) |
---|
3090 | break; |
---|
3091 | $id = $_post->ancestors[] = $ancestor; |
---|
3092 | } |
---|
3093 | } |
---|
3094 | |
---|
3095 | /* Post Revisions */ |
---|
3096 | |
---|
3097 | /** |
---|
3098 | * _wp_post_revision_fields() - determines which fields of posts are to be saved in revisions |
---|
3099 | * |
---|
3100 | * Does two things. If passed a post *array*, it will return a post array ready to be |
---|
3101 | * insterted into the posts table as a post revision. |
---|
3102 | * Otherwise, returns an array whose keys are the post fields to be saved for post revisions. |
---|
3103 | * |
---|
3104 | * @package WordPress |
---|
3105 | * @subpackage Post Revisions |
---|
3106 | * @since 2.6 |
---|
3107 | * |
---|
3108 | * @param array $post optional a post array to be processed for insertion as a post revision |
---|
3109 | * @param bool $autosave optional Is the revision an autosave? |
---|
3110 | * @return array post array ready to be inserted as a post revision or array of fields that can be versioned |
---|
3111 | */ |
---|
3112 | function _wp_post_revision_fields( $post = null, $autosave = false ) { |
---|
3113 | static $fields = false; |
---|
3114 | |
---|
3115 | if ( !$fields ) { |
---|
3116 | // Allow these to be versioned |
---|
3117 | $fields = array( |
---|
3118 | 'post_title' => __( 'Title' ), |
---|
3119 | 'post_content' => __( 'Content' ), |
---|
3120 | 'post_excerpt' => __( 'Excerpt' ), |
---|
3121 | ); |
---|
3122 | |
---|
3123 | // Runs only once |
---|
3124 | $fields = apply_filters( '_wp_post_revision_fields', $fields ); |
---|
3125 | |
---|
3126 | // WP uses these internally either in versioning or elsewhere - they cannot be versioned |
---|
3127 | foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 'post_author' ) as $protect ) |
---|
3128 | unset( $fields[$protect] ); |
---|
3129 | } |
---|
3130 | |
---|
3131 | if ( !is_array($post) ) |
---|
3132 | return $fields; |
---|
3133 | |
---|
3134 | $return = array(); |
---|
3135 | foreach ( array_intersect( array_keys( $post ), array_keys( $fields ) ) as $field ) |
---|
3136 | $return[$field] = $post[$field]; |
---|
3137 | |
---|
3138 | $return['post_parent'] = $post['ID']; |
---|
3139 | $return['post_status'] = 'inherit'; |
---|
3140 | $return['post_type'] = 'revision'; |
---|
3141 | $return['post_name'] = $autosave ? "$post[ID]-autosave" : "$post[ID]-revision"; |
---|
3142 | $return['post_date'] = $post['post_modified']; |
---|
3143 | $return['post_date_gmt'] = $post['post_modified_gmt']; |
---|
3144 | |
---|
3145 | return $return; |
---|
3146 | } |
---|
3147 | |
---|
3148 | /** |
---|
3149 | * wp_save_post_revision() - Saves an already existing post as a post revision. Typically used immediately prior to post updates. |
---|
3150 | * |
---|
3151 | * @package WordPress |
---|
3152 | * @subpackage Post Revisions |
---|
3153 | * @since 2.6 |
---|
3154 | * |
---|
3155 | * @uses _wp_put_post_revision() |
---|
3156 | * |
---|
3157 | * @param int $post_id The ID of the post to save as a revision |
---|
3158 | * @return mixed null or 0 if error, new revision ID if success |
---|
3159 | */ |
---|
3160 | function wp_save_post_revision( $post_id ) { |
---|
3161 | // We do autosaves manually with wp_create_post_autosave() |
---|
3162 | if ( @constant( 'DOING_AUTOSAVE' ) ) |
---|
3163 | return; |
---|
3164 | |
---|
3165 | // WP_POST_REVISIONS = 0, false |
---|
3166 | if ( !constant('WP_POST_REVISIONS') ) |
---|
3167 | return; |
---|
3168 | |
---|
3169 | if ( !$post = get_post( $post_id, ARRAY_A ) ) |
---|
3170 | return; |
---|
3171 | |
---|
3172 | if ( !in_array( $post['post_type'], array( 'post', 'page' ) ) ) |
---|
3173 | return; |
---|
3174 | |
---|
3175 | $return = _wp_put_post_revision( $post ); |
---|
3176 | |
---|
3177 | // WP_POST_REVISIONS = true (default), -1 |
---|
3178 | if ( !is_numeric( WP_POST_REVISIONS ) || WP_POST_REVISIONS < 0 ) |
---|
3179 | return $return; |
---|
3180 | |
---|
3181 | // all revisions and (possibly) one autosave |
---|
3182 | $revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) ); |
---|
3183 | |
---|
3184 | // WP_POST_REVISIONS = (int) (# of autasaves to save) |
---|
3185 | $delete = count($revisions) - WP_POST_REVISIONS; |
---|
3186 | |
---|
3187 | if ( $delete < 1 ) |
---|
3188 | return $return; |
---|
3189 | |
---|
3190 | $revisions = array_slice( $revisions, 0, $delete ); |
---|
3191 | |
---|
3192 | for ( $i = 0; isset($revisions[$i]); $i++ ) { |
---|
3193 | if ( false !== strpos( $revisions[$i]->post_name, 'autosave' ) ) |
---|
3194 | continue; |
---|
3195 | wp_delete_post_revision( $revisions[$i]->ID ); |
---|
3196 | } |
---|
3197 | |
---|
3198 | return $return; |
---|
3199 | } |
---|
3200 | |
---|
3201 | /** |
---|
3202 | * wp_get_post_autosave() - returns the autosaved data of the specified post. |
---|
3203 | * |
---|
3204 | * Returns a post object containing the information that was autosaved for the specified post. |
---|
3205 | * |
---|
3206 | * @package WordPress |
---|
3207 | * @subpackage Post Revisions |
---|
3208 | * @since 2.6 |
---|
3209 | * |
---|
3210 | * @param int $post_id The post ID |
---|
3211 | * @return object|bool the autosaved data or false on failure or when no autosave exists |
---|
3212 | */ |
---|
3213 | function wp_get_post_autosave( $post_id ) { |
---|
3214 | global $wpdb; |
---|
3215 | if ( !$post = get_post( $post_id ) ) |
---|
3216 | return false; |
---|
3217 | |
---|
3218 | $q = array( |
---|
3219 | 'name' => "{$post->ID}-autosave", |
---|
3220 | 'post_parent' => $post->ID, |
---|
3221 | 'post_type' => 'revision', |
---|
3222 | 'post_status' => 'inherit' |
---|
3223 | ); |
---|
3224 | |
---|
3225 | // Use WP_Query so that the result gets cached |
---|
3226 | $autosave_query = new WP_Query; |
---|
3227 | |
---|
3228 | add_action( 'parse_query', '_wp_get_post_autosave_hack' ); |
---|
3229 | $autosave = $autosave_query->query( $q ); |
---|
3230 | remove_action( 'parse_query', '_wp_get_post_autosave_hack' ); |
---|
3231 | |
---|
3232 | if ( $autosave && is_array($autosave) && is_object($autosave[0]) ) |
---|
3233 | return $autosave[0]; |
---|
3234 | |
---|
3235 | return false; |
---|
3236 | } |
---|
3237 | |
---|
3238 | // Internally used to hack WP_Query into submission |
---|
3239 | function _wp_get_post_autosave_hack( $query ) { |
---|
3240 | $query->is_single = false; |
---|
3241 | } |
---|
3242 | |
---|
3243 | |
---|
3244 | /** |
---|
3245 | * wp_is_post_revision() - Determines if the specified post is a revision. |
---|
3246 | * |
---|
3247 | * @package WordPress |
---|
3248 | * @subpackage Post Revisions |
---|
3249 | * @since 2.6 |
---|
3250 | * |
---|
3251 | * @param int|object $post post ID or post object |
---|
3252 | * @return bool|int false if not a revision, ID of revision's parent otherwise |
---|
3253 | */ |
---|
3254 | function wp_is_post_revision( $post ) { |
---|
3255 | if ( !$post = wp_get_post_revision( $post ) ) |
---|
3256 | return false; |
---|
3257 | return (int) $post->post_parent; |
---|
3258 | } |
---|
3259 | |
---|
3260 | /** |
---|
3261 | * wp_is_post_autosave() - Determines if the specified post is an autosave. |
---|
3262 | * |
---|
3263 | * @package WordPress |
---|
3264 | * @subpackage Post Revisions |
---|
3265 | * @since 2.6 |
---|
3266 | * |
---|
3267 | * @param int|object $post post ID or post object |
---|
3268 | * @return bool|int false if not a revision, ID of autosave's parent otherwise |
---|
3269 | */ |
---|
3270 | function wp_is_post_autosave( $post ) { |
---|
3271 | if ( !$post = wp_get_post_revision( $post ) ) |
---|
3272 | return false; |
---|
3273 | if ( "{$post->post_parent}-autosave" !== $post->post_name ) |
---|
3274 | return false; |
---|
3275 | return (int) $post->post_parent; |
---|
3276 | } |
---|
3277 | |
---|
3278 | /** |
---|
3279 | * _wp_put_post_revision() - Inserts post data into the posts table as a post revision |
---|
3280 | * |
---|
3281 | * @package WordPress |
---|
3282 | * @subpackage Post Revisions |
---|
3283 | * @since 2.6 |
---|
3284 | * |
---|
3285 | * @uses wp_insert_post() |
---|
3286 | * |
---|
3287 | * @param int|object|array $post post ID, post object OR post array |
---|
3288 | * @param bool $autosave optional Is the revision an autosave? |
---|
3289 | * @return mixed null or 0 if error, new revision ID if success |
---|
3290 | */ |
---|
3291 | function _wp_put_post_revision( $post = null, $autosave = false ) { |
---|
3292 | if ( is_object($post) ) |
---|
3293 | $post = get_object_vars( $post ); |
---|
3294 | elseif ( !is_array($post) ) |
---|
3295 | $post = get_post($post, ARRAY_A); |
---|
3296 | if ( !$post || empty($post['ID']) ) |
---|
3297 | return; |
---|
3298 | |
---|
3299 | if ( isset($post['post_type']) && 'revision' == $post['post_type'] ) |
---|
3300 | return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) ); |
---|
3301 | |
---|
3302 | $post = _wp_post_revision_fields( $post, $autosave ); |
---|
3303 | |
---|
3304 | $revision_id = wp_insert_post( $post ); |
---|
3305 | if ( is_wp_error($revision_id) ) |
---|
3306 | return $revision_id; |
---|
3307 | |
---|
3308 | if ( $revision_id ) |
---|
3309 | do_action( '_wp_put_post_revision', $revision_id ); |
---|
3310 | return $revision_id; |
---|
3311 | } |
---|
3312 | |
---|
3313 | /** |
---|
3314 | * wp_get_post_revision() - Gets a post revision |
---|
3315 | * |
---|
3316 | * @package WordPress |
---|
3317 | * @subpackage Post Revisions |
---|
3318 | * @since 2.6 |
---|
3319 | * |
---|
3320 | * @uses get_post() |
---|
3321 | * |
---|
3322 | * @param int|object $post post ID or post object |
---|
3323 | * @param $output optional OBJECT, ARRAY_A, or ARRAY_N |
---|
3324 | * @param string $filter optional sanitation filter. @see sanitize_post() |
---|
3325 | * @return mixed null if error or post object if success |
---|
3326 | */ |
---|
3327 | function &wp_get_post_revision(&$post, $output = OBJECT, $filter = 'raw') { |
---|
3328 | $null = null; |
---|
3329 | if ( !$revision = get_post( $post, OBJECT, $filter ) ) |
---|
3330 | return $revision; |
---|
3331 | if ( 'revision' !== $revision->post_type ) |
---|
3332 | return $null; |
---|
3333 | |
---|
3334 | if ( $output == OBJECT ) { |
---|
3335 | return $revision; |
---|
3336 | } elseif ( $output == ARRAY_A ) { |
---|
3337 | $_revision = get_object_vars($revision); |
---|
3338 | return $_revision; |
---|
3339 | } elseif ( $output == ARRAY_N ) { |
---|
3340 | $_revision = array_values(get_object_vars($revision)); |
---|
3341 | return $_revision; |
---|
3342 | } |
---|
3343 | |
---|
3344 | return $revision; |
---|
3345 | } |
---|
3346 | |
---|
3347 | /** |
---|
3348 | * wp_restore_post_revision() - Restores a post to the specified revision |
---|
3349 | * |
---|
3350 | * Can restore a past using all fields of the post revision, or only selected fields. |
---|
3351 | * |
---|
3352 | * @package WordPress |
---|
3353 | * @subpackage Post Revisions |
---|
3354 | * @since 2.6 |
---|
3355 | * |
---|
3356 | * @uses wp_get_post_revision() |
---|
3357 | * @uses wp_update_post() |
---|
3358 | * |
---|
3359 | * @param int|object $revision_id revision ID or revision object |
---|
3360 | * @param array $fields optional What fields to restore from. Defaults to all. |
---|
3361 | * @return mixed null if error, false if no fields to restore, (int) post ID if success |
---|
3362 | */ |
---|
3363 | function wp_restore_post_revision( $revision_id, $fields = null ) { |
---|
3364 | if ( !$revision = wp_get_post_revision( $revision_id, ARRAY_A ) ) |
---|
3365 | return $revision; |
---|
3366 | |
---|
3367 | if ( !is_array( $fields ) ) |
---|
3368 | $fields = array_keys( _wp_post_revision_fields() ); |
---|
3369 | |
---|
3370 | $update = array(); |
---|
3371 | foreach( array_intersect( array_keys( $revision ), $fields ) as $field ) |
---|
3372 | $update[$field] = $revision[$field]; |
---|
3373 | |
---|
3374 | if ( !$update ) |
---|
3375 | return false; |
---|
3376 | |
---|
3377 | $update['ID'] = $revision['post_parent']; |
---|
3378 | |
---|
3379 | $post_id = wp_update_post( $update ); |
---|
3380 | if ( is_wp_error( $post_id ) ) |
---|
3381 | return $post_id; |
---|
3382 | |
---|
3383 | if ( $post_id ) |
---|
3384 | do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] ); |
---|
3385 | |
---|
3386 | return $post_id; |
---|
3387 | } |
---|
3388 | |
---|
3389 | /** |
---|
3390 | * wp_delete_post_revision() - Deletes a revision. |
---|
3391 | * |
---|
3392 | * Deletes the row from the posts table corresponding to the specified revision |
---|
3393 | * |
---|
3394 | * @package WordPress |
---|
3395 | * @subpackage Post Revisions |
---|
3396 | * @since 2.6 |
---|
3397 | * |
---|
3398 | * @uses wp_get_post_revision() |
---|
3399 | * @uses wp_delete_post() |
---|
3400 | * |
---|
3401 | * @param int|object $revision_id revision ID or revision object |
---|
3402 | * @param array $fields optional What fields to restore from. Defaults to all. |
---|
3403 | * @return mixed null if error, false if no fields to restore, (int) post ID if success |
---|
3404 | */ |
---|
3405 | function wp_delete_post_revision( $revision_id ) { |
---|
3406 | if ( !$revision = wp_get_post_revision( $revision_id ) ) |
---|
3407 | return $revision; |
---|
3408 | |
---|
3409 | $delete = wp_delete_post( $revision->ID ); |
---|
3410 | if ( is_wp_error( $delete ) ) |
---|
3411 | return $delete; |
---|
3412 | |
---|
3413 | if ( $delete ) |
---|
3414 | do_action( 'wp_delete_post_revision', $revision->ID, $revision ); |
---|
3415 | |
---|
3416 | return $delete; |
---|
3417 | } |
---|
3418 | |
---|
3419 | /** |
---|
3420 | * wp_get_post_revisions() - Returns all revisions of specified post |
---|
3421 | * |
---|
3422 | * @package WordPress |
---|
3423 | * @subpackage Post Revisions |
---|
3424 | * @since 2.6 |
---|
3425 | * |
---|
3426 | * @uses get_children() |
---|
3427 | * |
---|
3428 | * @param int|object $post_id post ID or post object |
---|
3429 | * @return array empty if no revisions |
---|
3430 | */ |
---|
3431 | function wp_get_post_revisions( $post_id = 0, $args = null ) { |
---|
3432 | if ( !constant('WP_POST_REVISIONS') ) |
---|
3433 | return array(); |
---|
3434 | if ( ( !$post = get_post( $post_id ) ) || empty( $post->ID ) ) |
---|
3435 | return array(); |
---|
3436 | |
---|
3437 | $defaults = array( 'order' => 'DESC', 'orderby' => 'date' ); |
---|
3438 | $args = wp_parse_args( $args, $defaults ); |
---|
3439 | $args = array_merge( $args, array( 'post_parent' => $post->ID, 'post_type' => 'revision', 'post_status' => 'inherit' ) ); |
---|
3440 | |
---|
3441 | if ( !$revisions = get_children( $args ) ) |
---|
3442 | return array(); |
---|
3443 | return $revisions; |
---|
3444 | } |
---|