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