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 | // Post Type Registration |
---|
12 | // |
---|
13 | |
---|
14 | /** |
---|
15 | * Creates the initial post types when 'init' action is fired. |
---|
16 | */ |
---|
17 | function create_initial_post_types() { |
---|
18 | register_post_type( 'post', array( |
---|
19 | 'public' => true, |
---|
20 | '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
---|
21 | '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */ |
---|
22 | 'capability_type' => 'post', |
---|
23 | 'map_meta_cap' => true, |
---|
24 | 'hierarchical' => false, |
---|
25 | 'rewrite' => false, |
---|
26 | 'query_var' => false, |
---|
27 | 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'sticky', 'trackbacks', 'custom-fields', 'comments', 'revisions' ), |
---|
28 | ) ); |
---|
29 | |
---|
30 | register_post_type( 'page', array( |
---|
31 | 'public' => true, |
---|
32 | '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
---|
33 | '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */ |
---|
34 | 'capability_type' => 'page', |
---|
35 | 'map_meta_cap' => true, |
---|
36 | 'hierarchical' => true, |
---|
37 | 'rewrite' => false, |
---|
38 | 'query_var' => false, |
---|
39 | 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ), |
---|
40 | ) ); |
---|
41 | |
---|
42 | register_post_type( 'attachment', array( |
---|
43 | 'labels' => array( |
---|
44 | 'name' => __( 'Media' ), |
---|
45 | ), |
---|
46 | 'public' => true, |
---|
47 | 'show_ui' => false, |
---|
48 | '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
---|
49 | '_edit_link' => 'media.php?attachment_id=%d', /* internal use only. don't use this when registering your own post type. */ |
---|
50 | 'capability_type' => 'post', |
---|
51 | 'map_meta_cap' => true, |
---|
52 | 'hierarchical' => false, |
---|
53 | 'rewrite' => false, |
---|
54 | 'query_var' => false, |
---|
55 | 'can_export' => false, |
---|
56 | 'show_in_nav_menus' => false, |
---|
57 | ) ); |
---|
58 | |
---|
59 | register_post_type( 'revision', array( |
---|
60 | 'labels' => array( |
---|
61 | 'name' => __( 'Revisions' ), |
---|
62 | 'singular_name' => __( 'Revision' ), |
---|
63 | ), |
---|
64 | 'public' => false, |
---|
65 | '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
---|
66 | '_edit_link' => 'revision.php?revision=%d', /* internal use only. don't use this when registering your own post type. */ |
---|
67 | 'capability_type' => 'post', |
---|
68 | 'map_meta_cap' => true, |
---|
69 | 'hierarchical' => false, |
---|
70 | 'rewrite' => false, |
---|
71 | 'query_var' => false, |
---|
72 | ) ); |
---|
73 | |
---|
74 | register_post_type( 'nav_menu_item', array( |
---|
75 | 'labels' => array( |
---|
76 | 'name' => __( 'Navigation Menu Items' ), |
---|
77 | 'singular_name' => __( 'Navigation Menu Item' ), |
---|
78 | ), |
---|
79 | 'public' => false, |
---|
80 | '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
---|
81 | 'hierarchical' => false, |
---|
82 | 'rewrite' => false, |
---|
83 | 'query_var' => false, |
---|
84 | ) ); |
---|
85 | |
---|
86 | register_post_status( 'publish', array( |
---|
87 | 'label' => _x( 'Published', 'post' ), |
---|
88 | 'public' => true, |
---|
89 | '_builtin' => true, /* internal use only. */ |
---|
90 | 'label_count' => _n_noop( 'Published <span class="count">(%s)</span>', 'Published <span class="count">(%s)</span>' ), |
---|
91 | ) ); |
---|
92 | |
---|
93 | register_post_status( 'future', array( |
---|
94 | 'label' => _x( 'Scheduled', 'post' ), |
---|
95 | 'protected' => true, |
---|
96 | '_builtin' => true, /* internal use only. */ |
---|
97 | 'label_count' => _n_noop('Scheduled <span class="count">(%s)</span>', 'Scheduled <span class="count">(%s)</span>' ), |
---|
98 | ) ); |
---|
99 | |
---|
100 | register_post_status( 'draft', array( |
---|
101 | 'label' => _x( 'Draft', 'post' ), |
---|
102 | 'protected' => true, |
---|
103 | '_builtin' => true, /* internal use only. */ |
---|
104 | 'label_count' => _n_noop( 'Draft <span class="count">(%s)</span>', 'Drafts <span class="count">(%s)</span>' ), |
---|
105 | ) ); |
---|
106 | |
---|
107 | register_post_status( 'pending', array( |
---|
108 | 'label' => _x( 'Pending', 'post' ), |
---|
109 | 'protected' => true, |
---|
110 | '_builtin' => true, /* internal use only. */ |
---|
111 | 'label_count' => _n_noop( 'Pending <span class="count">(%s)</span>', 'Pending <span class="count">(%s)</span>' ), |
---|
112 | ) ); |
---|
113 | |
---|
114 | register_post_status( 'private', array( |
---|
115 | 'label' => _x( 'Private', 'post' ), |
---|
116 | 'private' => true, |
---|
117 | '_builtin' => true, /* internal use only. */ |
---|
118 | 'label_count' => _n_noop( 'Private <span class="count">(%s)</span>', 'Private <span class="count">(%s)</span>' ), |
---|
119 | ) ); |
---|
120 | |
---|
121 | register_post_status( 'trash', array( |
---|
122 | 'label' => _x( 'Trash', 'post' ), |
---|
123 | 'internal' => true, |
---|
124 | '_builtin' => true, /* internal use only. */ |
---|
125 | 'label_count' => _n_noop( 'Trash <span class="count">(%s)</span>', 'Trash <span class="count">(%s)</span>' ), |
---|
126 | 'show_in_admin_status_list' => true, |
---|
127 | ) ); |
---|
128 | |
---|
129 | register_post_status( 'auto-draft', array( |
---|
130 | 'label' => 'auto-draft', |
---|
131 | 'internal' => true, |
---|
132 | '_builtin' => true, /* internal use only. */ |
---|
133 | ) ); |
---|
134 | |
---|
135 | register_post_status( 'inherit', array( |
---|
136 | 'label' => 'inherit', |
---|
137 | 'internal' => true, |
---|
138 | '_builtin' => true, /* internal use only. */ |
---|
139 | 'exclude_from_search' => false, |
---|
140 | ) ); |
---|
141 | } |
---|
142 | add_action( 'init', 'create_initial_post_types', 0 ); // highest priority |
---|
143 | |
---|
144 | /** |
---|
145 | * Retrieve attached file path based on attachment ID. |
---|
146 | * |
---|
147 | * You can optionally send it through the 'get_attached_file' filter, but by |
---|
148 | * default it will just return the file path unfiltered. |
---|
149 | * |
---|
150 | * The function works by getting the single post meta name, named |
---|
151 | * '_wp_attached_file' and returning it. This is a convenience function to |
---|
152 | * prevent looking up the meta name and provide a mechanism for sending the |
---|
153 | * attached filename through a filter. |
---|
154 | * |
---|
155 | * @since 2.0.0 |
---|
156 | * @uses apply_filters() Calls 'get_attached_file' on file path and attachment ID. |
---|
157 | * |
---|
158 | * @param int $attachment_id Attachment ID. |
---|
159 | * @param bool $unfiltered Whether to apply filters. |
---|
160 | * @return string The file path to the attached file. |
---|
161 | */ |
---|
162 | function get_attached_file( $attachment_id, $unfiltered = false ) { |
---|
163 | $file = get_post_meta( $attachment_id, '_wp_attached_file', true ); |
---|
164 | // If the file is relative, prepend upload dir |
---|
165 | if ( 0 !== strpos($file, '/') && !preg_match('|^.:\\\|', $file) && ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) ) |
---|
166 | $file = $uploads['basedir'] . "/$file"; |
---|
167 | if ( $unfiltered ) |
---|
168 | return $file; |
---|
169 | return apply_filters( 'get_attached_file', $file, $attachment_id ); |
---|
170 | } |
---|
171 | |
---|
172 | /** |
---|
173 | * Update attachment file path based on attachment ID. |
---|
174 | * |
---|
175 | * Used to update the file path of the attachment, which uses post meta name |
---|
176 | * '_wp_attached_file' to store the path of the attachment. |
---|
177 | * |
---|
178 | * @since 2.1.0 |
---|
179 | * @uses apply_filters() Calls 'update_attached_file' on file path and attachment ID. |
---|
180 | * |
---|
181 | * @param int $attachment_id Attachment ID |
---|
182 | * @param string $file File path for the attachment |
---|
183 | * @return bool False on failure, true on success. |
---|
184 | */ |
---|
185 | function update_attached_file( $attachment_id, $file ) { |
---|
186 | if ( !get_post( $attachment_id ) ) |
---|
187 | return false; |
---|
188 | |
---|
189 | $file = apply_filters( 'update_attached_file', $file, $attachment_id ); |
---|
190 | $file = _wp_relative_upload_path($file); |
---|
191 | |
---|
192 | return update_post_meta( $attachment_id, '_wp_attached_file', $file ); |
---|
193 | } |
---|
194 | |
---|
195 | /** |
---|
196 | * Return relative path to an uploaded file. |
---|
197 | * |
---|
198 | * The path is relative to the current upload dir. |
---|
199 | * |
---|
200 | * @since 2.9.0 |
---|
201 | * @uses apply_filters() Calls '_wp_relative_upload_path' on file path. |
---|
202 | * |
---|
203 | * @param string $path Full path to the file |
---|
204 | * @return string relative path on success, unchanged path on failure. |
---|
205 | */ |
---|
206 | function _wp_relative_upload_path( $path ) { |
---|
207 | $new_path = $path; |
---|
208 | |
---|
209 | if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { |
---|
210 | if ( 0 === strpos($new_path, $uploads['basedir']) ) { |
---|
211 | $new_path = str_replace($uploads['basedir'], '', $new_path); |
---|
212 | $new_path = ltrim($new_path, '/'); |
---|
213 | } |
---|
214 | } |
---|
215 | |
---|
216 | return apply_filters( '_wp_relative_upload_path', $new_path, $path ); |
---|
217 | } |
---|
218 | |
---|
219 | /** |
---|
220 | * Retrieve all children of the post parent ID. |
---|
221 | * |
---|
222 | * Normally, without any enhancements, the children would apply to pages. In the |
---|
223 | * context of the inner workings of WordPress, pages, posts, and attachments |
---|
224 | * share the same table, so therefore the functionality could apply to any one |
---|
225 | * of them. It is then noted that while this function does not work on posts, it |
---|
226 | * does not mean that it won't work on posts. It is recommended that you know |
---|
227 | * what context you wish to retrieve the children of. |
---|
228 | * |
---|
229 | * Attachments may also be made the child of a post, so if that is an accurate |
---|
230 | * statement (which needs to be verified), it would then be possible to get |
---|
231 | * all of the attachments for a post. Attachments have since changed since |
---|
232 | * version 2.5, so this is most likely unaccurate, but serves generally as an |
---|
233 | * example of what is possible. |
---|
234 | * |
---|
235 | * The arguments listed as defaults are for this function and also of the |
---|
236 | * {@link get_posts()} function. The arguments are combined with the |
---|
237 | * get_children defaults and are then passed to the {@link get_posts()} |
---|
238 | * function, which accepts additional arguments. You can replace the defaults in |
---|
239 | * this function, listed below and the additional arguments listed in the |
---|
240 | * {@link get_posts()} function. |
---|
241 | * |
---|
242 | * The 'post_parent' is the most important argument and important attention |
---|
243 | * needs to be paid to the $args parameter. If you pass either an object or an |
---|
244 | * integer (number), then just the 'post_parent' is grabbed and everything else |
---|
245 | * is lost. If you don't specify any arguments, then it is assumed that you are |
---|
246 | * in The Loop and the post parent will be grabbed for from the current post. |
---|
247 | * |
---|
248 | * The 'post_parent' argument is the ID to get the children. The 'numberposts' |
---|
249 | * is the amount of posts to retrieve that has a default of '-1', which is |
---|
250 | * used to get all of the posts. Giving a number higher than 0 will only |
---|
251 | * retrieve that amount of posts. |
---|
252 | * |
---|
253 | * The 'post_type' and 'post_status' arguments can be used to choose what |
---|
254 | * criteria of posts to retrieve. The 'post_type' can be anything, but WordPress |
---|
255 | * post types are 'post', 'pages', and 'attachments'. The 'post_status' |
---|
256 | * argument will accept any post status within the write administration panels. |
---|
257 | * |
---|
258 | * @see get_posts() Has additional arguments that can be replaced. |
---|
259 | * @internal Claims made in the long description might be inaccurate. |
---|
260 | * |
---|
261 | * @since 2.0.0 |
---|
262 | * |
---|
263 | * @param mixed $args Optional. User defined arguments for replacing the defaults. |
---|
264 | * @param string $output Optional. Constant for return type, either OBJECT (default), ARRAY_A, ARRAY_N. |
---|
265 | * @return array|bool False on failure and the type will be determined by $output parameter. |
---|
266 | */ |
---|
267 | function &get_children($args = '', $output = OBJECT) { |
---|
268 | $kids = array(); |
---|
269 | if ( empty( $args ) ) { |
---|
270 | if ( isset( $GLOBALS['post'] ) ) { |
---|
271 | $args = array('post_parent' => (int) $GLOBALS['post']->post_parent ); |
---|
272 | } else { |
---|
273 | return $kids; |
---|
274 | } |
---|
275 | } elseif ( is_object( $args ) ) { |
---|
276 | $args = array('post_parent' => (int) $args->post_parent ); |
---|
277 | } elseif ( is_numeric( $args ) ) { |
---|
278 | $args = array('post_parent' => (int) $args); |
---|
279 | } |
---|
280 | |
---|
281 | $defaults = array( |
---|
282 | 'numberposts' => -1, 'post_type' => 'any', |
---|
283 | 'post_status' => 'any', 'post_parent' => 0, |
---|
284 | ); |
---|
285 | |
---|
286 | $r = wp_parse_args( $args, $defaults ); |
---|
287 | |
---|
288 | $children = get_posts( $r ); |
---|
289 | |
---|
290 | if ( !$children ) |
---|
291 | return $kids; |
---|
292 | |
---|
293 | update_post_cache($children); |
---|
294 | |
---|
295 | foreach ( $children as $key => $child ) |
---|
296 | $kids[$child->ID] =& $children[$key]; |
---|
297 | |
---|
298 | if ( $output == OBJECT ) { |
---|
299 | return $kids; |
---|
300 | } elseif ( $output == ARRAY_A ) { |
---|
301 | foreach ( (array) $kids as $kid ) |
---|
302 | $weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]); |
---|
303 | return $weeuns; |
---|
304 | } elseif ( $output == ARRAY_N ) { |
---|
305 | foreach ( (array) $kids as $kid ) |
---|
306 | $babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID])); |
---|
307 | return $babes; |
---|
308 | } else { |
---|
309 | return $kids; |
---|
310 | } |
---|
311 | } |
---|
312 | |
---|
313 | /** |
---|
314 | * Get extended entry info (<!--more-->). |
---|
315 | * |
---|
316 | * There should not be any space after the second dash and before the word |
---|
317 | * 'more'. There can be text or space(s) after the word 'more', but won't be |
---|
318 | * referenced. |
---|
319 | * |
---|
320 | * The returned array has 'main' and 'extended' keys. Main has the text before |
---|
321 | * the <code><!--more--></code>. The 'extended' key has the content after the |
---|
322 | * <code><!--more--></code> comment. |
---|
323 | * |
---|
324 | * @since 1.0.0 |
---|
325 | * |
---|
326 | * @param string $post Post content. |
---|
327 | * @return array Post before ('main') and after ('extended'). |
---|
328 | */ |
---|
329 | function get_extended($post) { |
---|
330 | //Match the new style more links |
---|
331 | if ( preg_match('/<!--more(.*?)?-->/', $post, $matches) ) { |
---|
332 | list($main, $extended) = explode($matches[0], $post, 2); |
---|
333 | } else { |
---|
334 | $main = $post; |
---|
335 | $extended = ''; |
---|
336 | } |
---|
337 | |
---|
338 | // Strip leading and trailing whitespace |
---|
339 | $main = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $main); |
---|
340 | $extended = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $extended); |
---|
341 | |
---|
342 | return array('main' => $main, 'extended' => $extended); |
---|
343 | } |
---|
344 | |
---|
345 | /** |
---|
346 | * Retrieves post data given a post ID or post object. |
---|
347 | * |
---|
348 | * See {@link sanitize_post()} for optional $filter values. Also, the parameter |
---|
349 | * $post, must be given as a variable, since it is passed by reference. |
---|
350 | * |
---|
351 | * @since 1.5.1 |
---|
352 | * @uses $wpdb |
---|
353 | * @link http://codex.wordpress.org/Function_Reference/get_post |
---|
354 | * |
---|
355 | * @param int|object $post Post ID or post object. |
---|
356 | * @param string $output Optional, default is Object. Either OBJECT, ARRAY_A, or ARRAY_N. |
---|
357 | * @param string $filter Optional, default is raw. |
---|
358 | * @return mixed Post data |
---|
359 | */ |
---|
360 | function &get_post(&$post, $output = OBJECT, $filter = 'raw') { |
---|
361 | global $wpdb; |
---|
362 | $null = null; |
---|
363 | |
---|
364 | if ( empty($post) ) { |
---|
365 | if ( isset($GLOBALS['post']) ) |
---|
366 | $_post = & $GLOBALS['post']; |
---|
367 | else |
---|
368 | return $null; |
---|
369 | } elseif ( is_object($post) && empty($post->filter) ) { |
---|
370 | _get_post_ancestors($post); |
---|
371 | $_post = sanitize_post($post, 'raw'); |
---|
372 | wp_cache_add($post->ID, $_post, 'posts'); |
---|
373 | } else { |
---|
374 | if ( is_object($post) ) |
---|
375 | $post_id = $post->ID; |
---|
376 | else |
---|
377 | $post_id = $post; |
---|
378 | |
---|
379 | $post_id = (int) $post_id; |
---|
380 | if ( ! $_post = wp_cache_get($post_id, 'posts') ) { |
---|
381 | $_post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id)); |
---|
382 | if ( ! $_post ) |
---|
383 | return $null; |
---|
384 | _get_post_ancestors($_post); |
---|
385 | $_post = sanitize_post($_post, 'raw'); |
---|
386 | wp_cache_add($_post->ID, $_post, 'posts'); |
---|
387 | } |
---|
388 | } |
---|
389 | |
---|
390 | if ($filter != 'raw') |
---|
391 | $_post = sanitize_post($_post, $filter); |
---|
392 | |
---|
393 | if ( $output == OBJECT ) { |
---|
394 | return $_post; |
---|
395 | } elseif ( $output == ARRAY_A ) { |
---|
396 | $__post = get_object_vars($_post); |
---|
397 | return $__post; |
---|
398 | } elseif ( $output == ARRAY_N ) { |
---|
399 | $__post = array_values(get_object_vars($_post)); |
---|
400 | return $__post; |
---|
401 | } else { |
---|
402 | return $_post; |
---|
403 | } |
---|
404 | } |
---|
405 | |
---|
406 | /** |
---|
407 | * Retrieve ancestors of a post. |
---|
408 | * |
---|
409 | * @since 2.5.0 |
---|
410 | * |
---|
411 | * @param int|object $post Post ID or post object |
---|
412 | * @return array Ancestor IDs or empty array if none are found. |
---|
413 | */ |
---|
414 | function get_post_ancestors($post) { |
---|
415 | $post = get_post($post); |
---|
416 | |
---|
417 | if ( !empty($post->ancestors) ) |
---|
418 | return $post->ancestors; |
---|
419 | |
---|
420 | return array(); |
---|
421 | } |
---|
422 | |
---|
423 | /** |
---|
424 | * Retrieve data from a post field based on Post ID. |
---|
425 | * |
---|
426 | * Examples of the post field will be, 'post_type', 'post_status', 'content', |
---|
427 | * etc and based off of the post object property or key names. |
---|
428 | * |
---|
429 | * The context values are based off of the taxonomy filter functions and |
---|
430 | * supported values are found within those functions. |
---|
431 | * |
---|
432 | * @since 2.3.0 |
---|
433 | * @uses sanitize_post_field() See for possible $context values. |
---|
434 | * |
---|
435 | * @param string $field Post field name |
---|
436 | * @param id $post Post ID |
---|
437 | * @param string $context Optional. How to filter the field. Default is display. |
---|
438 | * @return WP_Error|string Value in post field or WP_Error on failure |
---|
439 | */ |
---|
440 | function get_post_field( $field, $post, $context = 'display' ) { |
---|
441 | $post = (int) $post; |
---|
442 | $post = get_post( $post ); |
---|
443 | |
---|
444 | if ( is_wp_error($post) ) |
---|
445 | return $post; |
---|
446 | |
---|
447 | if ( !is_object($post) ) |
---|
448 | return ''; |
---|
449 | |
---|
450 | if ( !isset($post->$field) ) |
---|
451 | return ''; |
---|
452 | |
---|
453 | return sanitize_post_field($field, $post->$field, $post->ID, $context); |
---|
454 | } |
---|
455 | |
---|
456 | /** |
---|
457 | * Retrieve the mime type of an attachment based on the ID. |
---|
458 | * |
---|
459 | * This function can be used with any post type, but it makes more sense with |
---|
460 | * attachments. |
---|
461 | * |
---|
462 | * @since 2.0.0 |
---|
463 | * |
---|
464 | * @param int $ID Optional. Post ID. |
---|
465 | * @return bool|string False on failure or returns the mime type |
---|
466 | */ |
---|
467 | function get_post_mime_type($ID = '') { |
---|
468 | $post = & get_post($ID); |
---|
469 | |
---|
470 | if ( is_object($post) ) |
---|
471 | return $post->post_mime_type; |
---|
472 | |
---|
473 | return false; |
---|
474 | } |
---|
475 | |
---|
476 | /** |
---|
477 | * Retrieve the format for a post |
---|
478 | * |
---|
479 | * @param int|object $post A post |
---|
480 | * |
---|
481 | * @return mixed The format if successful. False if no format is set. WP_Error if errors. |
---|
482 | */ |
---|
483 | function get_post_format( $post ) { |
---|
484 | $post = get_post($post); |
---|
485 | |
---|
486 | $format = wp_get_object_terms( $post->ID, 'post_format', array('orderby' => 'none', 'fields' => 'names') ); |
---|
487 | |
---|
488 | if ( is_wp_error($format) ) |
---|
489 | return $format; |
---|
490 | |
---|
491 | if ( empty($format) ) |
---|
492 | return false; |
---|
493 | |
---|
494 | return ( str_replace('post-format-', '', $format[0]) ); |
---|
495 | } |
---|
496 | |
---|
497 | /** |
---|
498 | * Check if a post has a particular format |
---|
499 | * |
---|
500 | * @since 3.1.0 |
---|
501 | * @uses has_term() |
---|
502 | * |
---|
503 | * @param string $format The format to check for |
---|
504 | * @param object|id $post The post to check. If not supplied, defaults to the current post if used in the loop. |
---|
505 | * @return bool True if the post has the format, false otherwise. |
---|
506 | */ |
---|
507 | function has_post_format( $format, $post = null ) { |
---|
508 | return has_term('post-format-' . sanitize_key($format), 'post_format', $post); |
---|
509 | } |
---|
510 | |
---|
511 | /** |
---|
512 | * Assign a format to a post |
---|
513 | * |
---|
514 | * @param int|object $post The post for which to assign a format |
---|
515 | * @param string $format A format to assign. Use an empty string or array to remove all formats from the post. |
---|
516 | * @return mixed WP_Error on error. Array of affected term IDs on success. |
---|
517 | */ |
---|
518 | function set_post_format( $post, $format ) { |
---|
519 | $post = get_post($post); |
---|
520 | |
---|
521 | if ( empty($post) ) |
---|
522 | return new WP_Error('invalid_post', __('Invalid post')); |
---|
523 | |
---|
524 | if ( !empty($format) ) |
---|
525 | $format = 'post-format-' . sanitize_key($format); |
---|
526 | |
---|
527 | return wp_set_post_terms($post->ID, $format, 'post_format'); |
---|
528 | } |
---|
529 | |
---|
530 | /** |
---|
531 | * Retrieve the post status based on the Post ID. |
---|
532 | * |
---|
533 | * If the post ID is of an attachment, then the parent post status will be given |
---|
534 | * instead. |
---|
535 | * |
---|
536 | * @since 2.0.0 |
---|
537 | * |
---|
538 | * @param int $ID Post ID |
---|
539 | * @return string|bool Post status or false on failure. |
---|
540 | */ |
---|
541 | function get_post_status($ID = '') { |
---|
542 | $post = get_post($ID); |
---|
543 | |
---|
544 | if ( !is_object($post) ) |
---|
545 | return false; |
---|
546 | |
---|
547 | // Unattached attachments are assumed to be published. |
---|
548 | if ( ('attachment' == $post->post_type) && ('inherit' == $post->post_status) && ( 0 == $post->post_parent) ) |
---|
549 | return 'publish'; |
---|
550 | |
---|
551 | if ( ('attachment' == $post->post_type) && $post->post_parent && ($post->ID != $post->post_parent) ) |
---|
552 | return get_post_status($post->post_parent); |
---|
553 | |
---|
554 | return $post->post_status; |
---|
555 | } |
---|
556 | |
---|
557 | /** |
---|
558 | * Retrieve all of the WordPress supported post statuses. |
---|
559 | * |
---|
560 | * Posts have a limited set of valid status values, this provides the |
---|
561 | * post_status values and descriptions. |
---|
562 | * |
---|
563 | * @since 2.5.0 |
---|
564 | * |
---|
565 | * @return array List of post statuses. |
---|
566 | */ |
---|
567 | function get_post_statuses( ) { |
---|
568 | $status = array( |
---|
569 | 'draft' => __('Draft'), |
---|
570 | 'pending' => __('Pending Review'), |
---|
571 | 'private' => __('Private'), |
---|
572 | 'publish' => __('Published') |
---|
573 | ); |
---|
574 | |
---|
575 | return $status; |
---|
576 | } |
---|
577 | |
---|
578 | /** |
---|
579 | * Retrieve all of the WordPress support page statuses. |
---|
580 | * |
---|
581 | * Pages have a limited set of valid status values, this provides the |
---|
582 | * post_status values and descriptions. |
---|
583 | * |
---|
584 | * @since 2.5.0 |
---|
585 | * |
---|
586 | * @return array List of page statuses. |
---|
587 | */ |
---|
588 | function get_page_statuses( ) { |
---|
589 | $status = array( |
---|
590 | 'draft' => __('Draft'), |
---|
591 | 'private' => __('Private'), |
---|
592 | 'publish' => __('Published') |
---|
593 | ); |
---|
594 | |
---|
595 | return $status; |
---|
596 | } |
---|
597 | |
---|
598 | /** |
---|
599 | * Register a post type. Do not use before init. |
---|
600 | * |
---|
601 | * A simple function for creating or modifying a post status based on the |
---|
602 | * parameters given. The function will accept an array (second optional |
---|
603 | * parameter), along with a string for the post status name. |
---|
604 | * |
---|
605 | * |
---|
606 | * Optional $args contents: |
---|
607 | * |
---|
608 | * label - A descriptive name for the post status marked for translation. Defaults to $post_status. |
---|
609 | * public - Whether posts of this status should be shown in the admin UI. Defaults to true. |
---|
610 | * exclude_from_search - Whether to exclude posts with this post status from search results. Defaults to true. |
---|
611 | * |
---|
612 | * @package WordPress |
---|
613 | * @subpackage Post |
---|
614 | * @since 3.0.0 |
---|
615 | * @uses $wp_post_statuses Inserts new post status object into the list |
---|
616 | * |
---|
617 | * @param string $post_status Name of the post status. |
---|
618 | * @param array|string $args See above description. |
---|
619 | */ |
---|
620 | function register_post_status($post_status, $args = array()) { |
---|
621 | global $wp_post_statuses; |
---|
622 | |
---|
623 | if (!is_array($wp_post_statuses)) |
---|
624 | $wp_post_statuses = array(); |
---|
625 | |
---|
626 | // Args prefixed with an underscore are reserved for internal use. |
---|
627 | $defaults = array('label' => false, 'label_count' => false, 'exclude_from_search' => null, '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'hierarchical' => false, 'public' => null, 'internal' => null, 'protected' => null, 'private' => null, 'show_in_admin_all' => null, 'publicly_queryable' => null, 'show_in_admin_status_list' => null, 'show_in_admin_all_list' => null, 'single_view_cap' => null); |
---|
628 | $args = wp_parse_args($args, $defaults); |
---|
629 | $args = (object) $args; |
---|
630 | |
---|
631 | $post_status = sanitize_key($post_status); |
---|
632 | $args->name = $post_status; |
---|
633 | |
---|
634 | if ( null === $args->public && null === $args->internal && null === $args->protected && null === $args->private ) |
---|
635 | $args->internal = true; |
---|
636 | |
---|
637 | if ( null === $args->public ) |
---|
638 | $args->public = false; |
---|
639 | |
---|
640 | if ( null === $args->private ) |
---|
641 | $args->private = false; |
---|
642 | |
---|
643 | if ( null === $args->protected ) |
---|
644 | $args->protected = false; |
---|
645 | |
---|
646 | if ( null === $args->internal ) |
---|
647 | $args->internal = false; |
---|
648 | |
---|
649 | if ( null === $args->publicly_queryable ) |
---|
650 | $args->publicly_queryable = $args->public; |
---|
651 | |
---|
652 | if ( null === $args->exclude_from_search ) |
---|
653 | $args->exclude_from_search = $args->internal; |
---|
654 | |
---|
655 | if ( null === $args->show_in_admin_all_list ) |
---|
656 | $args->show_in_admin_all_list = !$args->internal; |
---|
657 | |
---|
658 | if ( null === $args->show_in_admin_status_list ) |
---|
659 | $args->show_in_admin_status_list = !$args->internal; |
---|
660 | |
---|
661 | if ( null === $args->single_view_cap ) |
---|
662 | $args->single_view_cap = $args->public ? '' : 'edit'; |
---|
663 | |
---|
664 | if ( false === $args->label ) |
---|
665 | $args->label = $post_status; |
---|
666 | |
---|
667 | if ( false === $args->label_count ) |
---|
668 | $args->label_count = array( $args->label, $args->label ); |
---|
669 | |
---|
670 | $wp_post_statuses[$post_status] = $args; |
---|
671 | |
---|
672 | return $args; |
---|
673 | } |
---|
674 | |
---|
675 | /** |
---|
676 | * Retrieve a post status object by name |
---|
677 | * |
---|
678 | * @package WordPress |
---|
679 | * @subpackage Post |
---|
680 | * @since 3.0.0 |
---|
681 | * @uses $wp_post_statuses |
---|
682 | * @see register_post_status |
---|
683 | * @see get_post_statuses |
---|
684 | * |
---|
685 | * @param string $post_status The name of a registered post status |
---|
686 | * @return object A post status object |
---|
687 | */ |
---|
688 | function get_post_status_object( $post_status ) { |
---|
689 | global $wp_post_statuses; |
---|
690 | |
---|
691 | if ( empty($wp_post_statuses[$post_status]) ) |
---|
692 | return null; |
---|
693 | |
---|
694 | return $wp_post_statuses[$post_status]; |
---|
695 | } |
---|
696 | |
---|
697 | /** |
---|
698 | * Get a list of all registered post status objects. |
---|
699 | * |
---|
700 | * @package WordPress |
---|
701 | * @subpackage Post |
---|
702 | * @since 3.0.0 |
---|
703 | * @uses $wp_post_statuses |
---|
704 | * @see register_post_status |
---|
705 | * @see get_post_status_object |
---|
706 | * |
---|
707 | * @param array|string $args An array of key => value arguments to match against the post status objects. |
---|
708 | * @param string $output The type of output to return, either post status 'names' or 'objects'. 'names' is the default. |
---|
709 | * @param string $operator The logical operation to perform. 'or' means only one element |
---|
710 | * from the array needs to match; 'and' means all elements must match. The default is 'and'. |
---|
711 | * @return array A list of post type names or objects |
---|
712 | */ |
---|
713 | function get_post_stati( $args = array(), $output = 'names', $operator = 'and' ) { |
---|
714 | global $wp_post_statuses; |
---|
715 | |
---|
716 | $field = ('names' == $output) ? 'name' : false; |
---|
717 | |
---|
718 | return wp_filter_object_list($wp_post_statuses, $args, $operator, $field); |
---|
719 | } |
---|
720 | |
---|
721 | /** |
---|
722 | * Whether the post type is hierarchical. |
---|
723 | * |
---|
724 | * A false return value might also mean that the post type does not exist. |
---|
725 | * |
---|
726 | * @since 3.0.0 |
---|
727 | * @see get_post_type_object |
---|
728 | * |
---|
729 | * @param string $post_type Post type name |
---|
730 | * @return bool Whether post type is hierarchical. |
---|
731 | */ |
---|
732 | function is_post_type_hierarchical( $post_type ) { |
---|
733 | if ( ! post_type_exists( $post_type ) ) |
---|
734 | return false; |
---|
735 | |
---|
736 | $post_type = get_post_type_object( $post_type ); |
---|
737 | return $post_type->hierarchical; |
---|
738 | } |
---|
739 | |
---|
740 | /** |
---|
741 | * Checks if a post type is registered. |
---|
742 | * |
---|
743 | * @since 3.0.0 |
---|
744 | * @uses get_post_type_object() |
---|
745 | * |
---|
746 | * @param string $post_type Post type name |
---|
747 | * @return bool Whether post type is registered. |
---|
748 | */ |
---|
749 | function post_type_exists( $post_type ) { |
---|
750 | return (bool) get_post_type_object( $post_type ); |
---|
751 | } |
---|
752 | |
---|
753 | /** |
---|
754 | * Retrieve the post type of the current post or of a given post. |
---|
755 | * |
---|
756 | * @since 2.1.0 |
---|
757 | * |
---|
758 | * @uses $post The Loop current post global |
---|
759 | * |
---|
760 | * @param mixed $the_post Optional. Post object or post ID. |
---|
761 | * @return bool|string post type or false on failure. |
---|
762 | */ |
---|
763 | function get_post_type( $the_post = false ) { |
---|
764 | global $post; |
---|
765 | |
---|
766 | if ( false === $the_post ) |
---|
767 | $the_post = $post; |
---|
768 | elseif ( is_numeric($the_post) ) |
---|
769 | $the_post = get_post($the_post); |
---|
770 | |
---|
771 | if ( is_object($the_post) ) |
---|
772 | return $the_post->post_type; |
---|
773 | |
---|
774 | return false; |
---|
775 | } |
---|
776 | |
---|
777 | /** |
---|
778 | * Retrieve a post type object by name |
---|
779 | * |
---|
780 | * @package WordPress |
---|
781 | * @subpackage Post |
---|
782 | * @since 3.0.0 |
---|
783 | * @uses $wp_post_types |
---|
784 | * @see register_post_type |
---|
785 | * @see get_post_types |
---|
786 | * |
---|
787 | * @param string $post_type The name of a registered post type |
---|
788 | * @return object A post type object |
---|
789 | */ |
---|
790 | function get_post_type_object( $post_type ) { |
---|
791 | global $wp_post_types; |
---|
792 | |
---|
793 | if ( empty($wp_post_types[$post_type]) ) |
---|
794 | return null; |
---|
795 | |
---|
796 | return $wp_post_types[$post_type]; |
---|
797 | } |
---|
798 | |
---|
799 | /** |
---|
800 | * Get a list of all registered post type objects. |
---|
801 | * |
---|
802 | * @package WordPress |
---|
803 | * @subpackage Post |
---|
804 | * @since 2.9.0 |
---|
805 | * @uses $wp_post_types |
---|
806 | * @see register_post_type |
---|
807 | * |
---|
808 | * @param array|string $args An array of key => value arguments to match against the post type objects. |
---|
809 | * @param string $output The type of output to return, either post type 'names' or 'objects'. 'names' is the default. |
---|
810 | * @param string $operator The logical operation to perform. 'or' means only one element |
---|
811 | * from the array needs to match; 'and' means all elements must match. The default is 'and'. |
---|
812 | * @return array A list of post type names or objects |
---|
813 | */ |
---|
814 | function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) { |
---|
815 | global $wp_post_types; |
---|
816 | |
---|
817 | $field = ('names' == $output) ? 'name' : false; |
---|
818 | |
---|
819 | return wp_filter_object_list($wp_post_types, $args, $operator, $field); |
---|
820 | } |
---|
821 | |
---|
822 | /** |
---|
823 | * Register a post type. Do not use before init. |
---|
824 | * |
---|
825 | * A function for creating or modifying a post type based on the |
---|
826 | * parameters given. The function will accept an array (second optional |
---|
827 | * parameter), along with a string for the post type name. |
---|
828 | * |
---|
829 | * Optional $args contents: |
---|
830 | * |
---|
831 | * - label - Name of the post type shown in the menu. Usually plural. If not set, labels['name'] will be used. |
---|
832 | * - description - A short descriptive summary of what the post type is. Defaults to blank. |
---|
833 | * - public - Whether posts of this type should be shown in the admin UI. Defaults to false. |
---|
834 | * - exclude_from_search - Whether to exclude posts with this post type from search results. |
---|
835 | * Defaults to true if the type is not public, false if the type is public. |
---|
836 | * - publicly_queryable - Whether post_type queries can be performed from the front page. |
---|
837 | * Defaults to whatever public is set as. |
---|
838 | * - show_ui - Whether to generate a default UI for managing this post type. Defaults to true |
---|
839 | * if the type is public, false if the type is not public. |
---|
840 | * - show_in_menu - Where to show the post type in the admin menu. True for a top level menu, |
---|
841 | * false for no menu, or can be a top level page like 'tools.php' or 'edit.php?post_type=page'. |
---|
842 | * show_ui must be true. |
---|
843 | * - menu_position - The position in the menu order the post type should appear. Defaults to the bottom. |
---|
844 | * - menu_icon - The url to the icon to be used for this menu. Defaults to use the posts icon. |
---|
845 | * - capability_type - The post type to use for checking read, edit, and delete capabilities. Defaults to 'post'. |
---|
846 | * May be passed as an array to allow for alternative plurals when using this argument as a base to construct the |
---|
847 | * capabilities, e.g. array('story', 'stories'). |
---|
848 | * - capabilities - Array of capabilities for this post type. By default the capability_type is used |
---|
849 | * as a base to construct capabilities. You can see accepted values in {@link get_post_type_capabilities()}. |
---|
850 | * - map_meta_cap - Whether to use the internal default meta capability handling. Defaults to false. |
---|
851 | * - hierarchical - Whether the post type is hierarchical. Defaults to false. |
---|
852 | * - supports - An alias for calling add_post_type_support() directly. See {@link add_post_type_support()} |
---|
853 | * for documentation. Defaults to none. |
---|
854 | * - register_meta_box_cb - Provide a callback function that will be called when setting up the |
---|
855 | * meta boxes for the edit form. Do remove_meta_box() and add_meta_box() calls in the callback. |
---|
856 | * - taxonomies - An array of taxonomy identifiers that will be registered for the post type. |
---|
857 | * Default is no taxonomies. Taxonomies can be registered later with register_taxonomy() or |
---|
858 | * register_taxonomy_for_object_type(). |
---|
859 | * - labels - An array of labels for this post type. By default post labels are used for non-hierarchical |
---|
860 | * types and page labels for hierarchical ones. You can see accepted values in {@link get_post_type_labels()}. |
---|
861 | * - permalink_epmask - The default rewrite endpoint bitmasks. |
---|
862 | * - has_archive - True to enable post type archives. Will generate the proper rewrite rules if rewrite is enabled. |
---|
863 | * - rewrite - false to prevent rewrite. Defaults to true. Use array('slug'=>$slug) to customize permastruct; |
---|
864 | * default will use $post_type as slug. Other options include 'with_front', 'feeds', and 'pages'. |
---|
865 | * - query_var - false to prevent queries, or string to value of the query var to use for this post type |
---|
866 | * - can_export - true allows this post type to be exported. |
---|
867 | * - show_in_nav_menus - true makes this post type available for selection in navigation menus. |
---|
868 | * - _builtin - true if this post type is a native or "built-in" post_type. THIS IS FOR INTERNAL USE ONLY! |
---|
869 | * - _edit_link - URL segement to use for edit link of this post type. THIS IS FOR INTERNAL USE ONLY! |
---|
870 | * |
---|
871 | * @since 2.9.0 |
---|
872 | * @uses $wp_post_types Inserts new post type object into the list |
---|
873 | * |
---|
874 | * @param string $post_type Name of the post type. |
---|
875 | * @param array|string $args See above description. |
---|
876 | * @return object the registered post type object |
---|
877 | */ |
---|
878 | function register_post_type($post_type, $args = array()) { |
---|
879 | global $wp_post_types, $wp_rewrite, $wp; |
---|
880 | |
---|
881 | if ( !is_array($wp_post_types) ) |
---|
882 | $wp_post_types = array(); |
---|
883 | |
---|
884 | // Args prefixed with an underscore are reserved for internal use. |
---|
885 | $defaults = array( |
---|
886 | 'labels' => array(), 'description' => '', 'publicly_queryable' => null, 'exclude_from_search' => null, |
---|
887 | 'capability_type' => 'post', 'capabilities' => array(), 'map_meta_cap' => false, |
---|
888 | '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'hierarchical' => false, |
---|
889 | 'public' => false, 'rewrite' => true, 'has_archive' => false, 'query_var' => true, |
---|
890 | 'supports' => array(), 'register_meta_box_cb' => null, |
---|
891 | 'taxonomies' => array(), 'show_ui' => null, 'menu_position' => null, 'menu_icon' => null, |
---|
892 | 'permalink_epmask' => EP_PERMALINK, 'can_export' => true, 'show_in_nav_menus' => null, 'show_in_menu' => null, |
---|
893 | ); |
---|
894 | $args = wp_parse_args($args, $defaults); |
---|
895 | $args = (object) $args; |
---|
896 | |
---|
897 | $post_type = sanitize_key($post_type); |
---|
898 | $args->name = $post_type; |
---|
899 | |
---|
900 | // If not set, default to the setting for public. |
---|
901 | if ( null === $args->publicly_queryable ) |
---|
902 | $args->publicly_queryable = $args->public; |
---|
903 | |
---|
904 | // If not set, default to the setting for public. |
---|
905 | if ( null === $args->show_ui ) |
---|
906 | $args->show_ui = $args->public; |
---|
907 | |
---|
908 | // If not set, default to the setting for show_ui. |
---|
909 | if ( null === $args->show_in_menu || ! $args->show_ui ) |
---|
910 | $args->show_in_menu = $args->show_ui; |
---|
911 | |
---|
912 | // Whether to show this type in nav-menus.php. Defaults to the setting for public. |
---|
913 | if ( null === $args->show_in_nav_menus ) |
---|
914 | $args->show_in_nav_menus = $args->public; |
---|
915 | |
---|
916 | // If not set, default to true if not public, false if public. |
---|
917 | if ( null === $args->exclude_from_search ) |
---|
918 | $args->exclude_from_search = !$args->public; |
---|
919 | |
---|
920 | if ( empty($args->capability_type) ) |
---|
921 | $args->capability_type = 'post'; |
---|
922 | |
---|
923 | $args->cap = get_post_type_capabilities( $args ); |
---|
924 | unset($args->capabilities); |
---|
925 | |
---|
926 | if ( is_array( $args->capability_type ) ) |
---|
927 | $args->capability_type = $args->capability_type[0]; |
---|
928 | |
---|
929 | if ( ! empty($args->supports) ) { |
---|
930 | add_post_type_support($post_type, $args->supports); |
---|
931 | unset($args->supports); |
---|
932 | } else { |
---|
933 | // Add default features |
---|
934 | add_post_type_support($post_type, array('title', 'editor')); |
---|
935 | } |
---|
936 | |
---|
937 | if ( false !== $args->query_var && !empty($wp) ) { |
---|
938 | if ( true === $args->query_var ) |
---|
939 | $args->query_var = $post_type; |
---|
940 | $args->query_var = sanitize_title_with_dashes($args->query_var); |
---|
941 | $wp->add_query_var($args->query_var); |
---|
942 | } |
---|
943 | |
---|
944 | if ( false !== $args->rewrite && '' != get_option('permalink_structure') ) { |
---|
945 | if ( ! is_array( $args->rewrite ) ) |
---|
946 | $args->rewrite = array(); |
---|
947 | if ( ! isset( $args->rewrite['slug'] ) ) |
---|
948 | $args->rewrite['slug'] = $post_type; |
---|
949 | if ( ! isset( $args->rewrite['with_front'] ) ) |
---|
950 | $args->rewrite['with_front'] = true; |
---|
951 | if ( ! isset( $args->rewrite['pages'] ) ) |
---|
952 | $args->rewrite['pages'] = true; |
---|
953 | if ( ! isset( $args->rewrite['feeds'] ) || ! $args->has_archive ) |
---|
954 | $args->rewrite['feeds'] = (bool) $args->has_archive; |
---|
955 | |
---|
956 | if ( $args->hierarchical ) |
---|
957 | $wp_rewrite->add_rewrite_tag("%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name="); |
---|
958 | else |
---|
959 | $wp_rewrite->add_rewrite_tag("%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name="); |
---|
960 | |
---|
961 | if ( $args->has_archive ) { |
---|
962 | $archive_slug = $args->has_archive === true ? $args->rewrite['slug'] : $args->has_archive; |
---|
963 | $wp_rewrite->add_rule( "{$archive_slug}/?$", "index.php?post_type=$post_type", 'top' ); |
---|
964 | if ( $args->rewrite['feeds'] && $wp_rewrite->feeds ) { |
---|
965 | $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')'; |
---|
966 | $wp_rewrite->add_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' ); |
---|
967 | $wp_rewrite->add_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' ); |
---|
968 | } |
---|
969 | if ( $args->rewrite['pages'] ) |
---|
970 | $wp_rewrite->add_rule( "{$archive_slug}/page/([0-9]{1,})/?$", "index.php?post_type=$post_type" . '&paged=$matches[1]', 'top' ); |
---|
971 | } |
---|
972 | |
---|
973 | $wp_rewrite->add_permastruct($post_type, "{$args->rewrite['slug']}/%$post_type%", $args->rewrite['with_front'], $args->permalink_epmask); |
---|
974 | } |
---|
975 | |
---|
976 | if ( $args->register_meta_box_cb ) |
---|
977 | add_action('add_meta_boxes_' . $post_type, $args->register_meta_box_cb, 10, 1); |
---|
978 | |
---|
979 | $args->labels = get_post_type_labels( $args ); |
---|
980 | $args->label = $args->labels->name; |
---|
981 | |
---|
982 | $wp_post_types[$post_type] = $args; |
---|
983 | |
---|
984 | add_action( 'future_' . $post_type, '_future_post_hook', 5, 2 ); |
---|
985 | |
---|
986 | foreach ( $args->taxonomies as $taxonomy ) { |
---|
987 | register_taxonomy_for_object_type( $taxonomy, $post_type ); |
---|
988 | } |
---|
989 | |
---|
990 | return $args; |
---|
991 | } |
---|
992 | |
---|
993 | /** |
---|
994 | * Builds an object with all post type capabilities out of a post type object |
---|
995 | * |
---|
996 | * Post type capabilities use the 'capability_type' argument as a base, if the |
---|
997 | * capability is not set in the 'capabilities' argument array or if the |
---|
998 | * 'capabilities' argument is not supplied. |
---|
999 | * |
---|
1000 | * The capability_type argument can optionally be registered as an array, with |
---|
1001 | * the first value being singular and the second plural, e.g. array('story, 'stories') |
---|
1002 | * Otherwise, an 's' will be added to the value for the plural form. After |
---|
1003 | * registration, capability_type will always be a string of the singular value. |
---|
1004 | * |
---|
1005 | * By default, seven keys are accepted as part of the capabilities array: |
---|
1006 | * |
---|
1007 | * - edit_post, read_post, and delete_post are meta capabilities, which are then |
---|
1008 | * generally mapped to corresponding primitive capabilities depending on the |
---|
1009 | * context, which would be the post being edited/read/deleted and the user or |
---|
1010 | * role being checked. Thus these capabilities would generally not be granted |
---|
1011 | * directly to users or roles. |
---|
1012 | * |
---|
1013 | * - edit_posts - Controls whether objects of this post type can be edited. |
---|
1014 | * - edit_others_posts - Controls whether objects of this type owned by other users |
---|
1015 | * can be edited. If the post type does not support an author, then this will |
---|
1016 | * behave like edit_posts. |
---|
1017 | * - publish_posts - Controls publishing objects of this post type. |
---|
1018 | * - read_private_posts - Controls whether private objects can be read. |
---|
1019 | |
---|
1020 | * These four primitive capabilities are checked in core in various locations. |
---|
1021 | * There are also seven other primitive capabilities which are not referenced |
---|
1022 | * directly in core, except in map_meta_cap(), which takes the three aforementioned |
---|
1023 | * meta capabilities and translates them into one or more primitive capabilities |
---|
1024 | * that must then be checked against the user or role, depending on the context. |
---|
1025 | * |
---|
1026 | * - read - Controls whether objects of this post type can be read. |
---|
1027 | * - delete_posts - Controls whether objects of this post type can be deleted. |
---|
1028 | * - delete_private_posts - Controls whether private objects can be deleted. |
---|
1029 | * - delete_published_posts - Controls whether published objects can be deleted. |
---|
1030 | * - delete_others_posts - Controls whether objects owned by other users can be |
---|
1031 | * can be deleted. If the post type does not support an author, then this will |
---|
1032 | * behave like delete_posts. |
---|
1033 | * - edit_private_posts - Controls whether private objects can be edited. |
---|
1034 | * - edit_published_posts - Controls whether published objects can be deleted. |
---|
1035 | * |
---|
1036 | * These additional capabilities are only used in map_meta_cap(). Thus, they are |
---|
1037 | * only assigned by default if the post type is registered with the 'map_meta_cap' |
---|
1038 | * argument set to true (default is false). |
---|
1039 | * |
---|
1040 | * @see map_meta_cap() |
---|
1041 | * @since 3.0.0 |
---|
1042 | * |
---|
1043 | * @param object $args Post type registration arguments |
---|
1044 | * @return object object with all the capabilities as member variables |
---|
1045 | */ |
---|
1046 | function get_post_type_capabilities( $args ) { |
---|
1047 | if ( ! is_array( $args->capability_type ) ) |
---|
1048 | $args->capability_type = array( $args->capability_type, $args->capability_type . 's' ); |
---|
1049 | |
---|
1050 | // Singular base for meta capabilities, plural base for primitive capabilities. |
---|
1051 | list( $singular_base, $plural_base ) = $args->capability_type; |
---|
1052 | |
---|
1053 | $default_capabilities = array( |
---|
1054 | // Meta capabilities |
---|
1055 | 'edit_post' => 'edit_' . $singular_base, |
---|
1056 | 'read_post' => 'read_' . $singular_base, |
---|
1057 | 'delete_post' => 'delete_' . $singular_base, |
---|
1058 | // Primitive capabilities used outside of map_meta_cap(): |
---|
1059 | 'edit_posts' => 'edit_' . $plural_base, |
---|
1060 | 'edit_others_posts' => 'edit_others_' . $plural_base, |
---|
1061 | 'publish_posts' => 'publish_' . $plural_base, |
---|
1062 | 'read_private_posts' => 'read_private_' . $plural_base, |
---|
1063 | ); |
---|
1064 | |
---|
1065 | // Primitive capabilities used within map_meta_cap(): |
---|
1066 | if ( $args->map_meta_cap ) { |
---|
1067 | $default_capabilities_for_mapping = array( |
---|
1068 | 'read' => 'read', |
---|
1069 | 'delete_posts' => 'delete_' . $plural_base, |
---|
1070 | 'delete_private_posts' => 'delete_private_' . $plural_base, |
---|
1071 | 'delete_published_posts' => 'delete_published_' . $plural_base, |
---|
1072 | 'delete_others_posts' => 'delete_others_' . $plural_base, |
---|
1073 | 'edit_private_posts' => 'edit_private_' . $plural_base, |
---|
1074 | 'edit_published_posts' => 'edit_published_' . $plural_base, |
---|
1075 | ); |
---|
1076 | $default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping ); |
---|
1077 | } |
---|
1078 | |
---|
1079 | if ( ! post_type_supports( $args->name, 'author' ) ) { |
---|
1080 | // While these may be checked in core, users/roles shouldn't need to be granted these. |
---|
1081 | $default_capabilities['edit_others_posts'] = $default_capabilities['edit_posts']; |
---|
1082 | if ( $args->map_meta_cap ) |
---|
1083 | $default_capabilities['delete_others_posts'] = $default_capabilities['delete_posts']; |
---|
1084 | } |
---|
1085 | |
---|
1086 | $capabilities = array_merge( $default_capabilities, $args->capabilities ); |
---|
1087 | |
---|
1088 | // Remember meta capabilities for future reference. |
---|
1089 | if ( $args->map_meta_cap ) |
---|
1090 | _post_type_meta_capabilities( $capabilities ); |
---|
1091 | |
---|
1092 | return (object) $capabilities; |
---|
1093 | } |
---|
1094 | |
---|
1095 | /** |
---|
1096 | * Stores or returns a list of post type meta caps for map_meta_cap(). |
---|
1097 | * |
---|
1098 | * @since 3.1.0 |
---|
1099 | * @access private |
---|
1100 | */ |
---|
1101 | function _post_type_meta_capabilities( $capabilities = null ) { |
---|
1102 | static $meta_caps = array(); |
---|
1103 | if ( null === $capabilities ) |
---|
1104 | return $meta_caps; |
---|
1105 | foreach ( $capabilities as $core => $custom ) { |
---|
1106 | if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ) ) ) |
---|
1107 | $meta_caps[ $custom ] = $core; |
---|
1108 | } |
---|
1109 | } |
---|
1110 | |
---|
1111 | /** |
---|
1112 | * Builds an object with all post type labels out of a post type object |
---|
1113 | * |
---|
1114 | * Accepted keys of the label array in the post type object: |
---|
1115 | * - name - general name for the post type, usually plural. The same and overriden by $post_type_object->label. Default is Posts/Pages |
---|
1116 | * - singular_name - name for one object of this post type. Default is Post/Page |
---|
1117 | * - add_new - Default is Add New for both hierarchical and non-hierarchical types. When internationalizing this string, please use a {@link http://codex.wordpress.org/I18n_for_WordPress_Developers#Disambiguation_by_context gettext context} matching your post type. Example: <code>_x('Add New', 'product');</code> |
---|
1118 | * - add_new_item - Default is Add New Post/Add New Page |
---|
1119 | * - edit_item - Default is Edit Post/Edit Page |
---|
1120 | * - new_item - Default is New Post/New Page |
---|
1121 | * - view_item - Default is View Post/View Page |
---|
1122 | * - search_items - Default is Search Posts/Search Pages |
---|
1123 | * - not_found - Default is No posts found/No pages found |
---|
1124 | * - not_found_in_trash - Default is No posts found in Trash/No pages found in Trash |
---|
1125 | * - parent_item_colon - This string isn't used on non-hierarchical types. In hierarchical ones the default is Parent Page: |
---|
1126 | * |
---|
1127 | * Above, the first default value is for non-hierarchical post types (like posts) and the second one is for hierarchical post types (like pages.) |
---|
1128 | * |
---|
1129 | * @since 3.0.0 |
---|
1130 | * @param object $post_type_object |
---|
1131 | * @return object object with all the labels as member variables |
---|
1132 | */ |
---|
1133 | function get_post_type_labels( $post_type_object ) { |
---|
1134 | $nohier_vs_hier_defaults = array( |
---|
1135 | 'name' => array( _x('Posts', 'post type general name'), _x('Pages', 'post type general name') ), |
---|
1136 | 'singular_name' => array( _x('Post', 'post type singular name'), _x('Page', 'post type singular name') ), |
---|
1137 | 'add_new' => array( _x('Add New', 'post'), _x('Add New', 'page') ), |
---|
1138 | 'add_new_item' => array( __('Add New Post'), __('Add New Page') ), |
---|
1139 | 'edit_item' => array( __('Edit Post'), __('Edit Page') ), |
---|
1140 | 'new_item' => array( __('New Post'), __('New Page') ), |
---|
1141 | 'view_item' => array( __('View Post'), __('View Page') ), |
---|
1142 | 'search_items' => array( __('Search Posts'), __('Search Pages') ), |
---|
1143 | 'not_found' => array( __('No posts found'), __('No pages found') ), |
---|
1144 | 'not_found_in_trash' => array( __('No posts found in Trash'), __('No pages found in Trash') ), |
---|
1145 | 'parent_item_colon' => array( null, __('Parent Page:') ) |
---|
1146 | ); |
---|
1147 | return _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults ); |
---|
1148 | } |
---|
1149 | |
---|
1150 | /** |
---|
1151 | * Builds an object with custom-something object (post type, taxonomy) labels out of a custom-something object |
---|
1152 | * |
---|
1153 | * @access private |
---|
1154 | * @since 3.0.0 |
---|
1155 | */ |
---|
1156 | function _get_custom_object_labels( $object, $nohier_vs_hier_defaults ) { |
---|
1157 | |
---|
1158 | if ( isset( $object->label ) && empty( $object->labels['name'] ) ) |
---|
1159 | $object->labels['name'] = $object->label; |
---|
1160 | |
---|
1161 | if ( !isset( $object->labels['singular_name'] ) && isset( $object->labels['name'] ) ) |
---|
1162 | $object->labels['singular_name'] = $object->labels['name']; |
---|
1163 | |
---|
1164 | $defaults = array_map( create_function( '$x', $object->hierarchical? 'return $x[1];' : 'return $x[0];' ), $nohier_vs_hier_defaults ); |
---|
1165 | $labels = array_merge( $defaults, $object->labels ); |
---|
1166 | return (object)$labels; |
---|
1167 | } |
---|
1168 | |
---|
1169 | /** |
---|
1170 | * Adds submenus for post types. |
---|
1171 | * |
---|
1172 | * @access private |
---|
1173 | * @since 3.1.0 |
---|
1174 | */ |
---|
1175 | function _add_post_type_submenus() { |
---|
1176 | foreach ( get_post_types( array( 'show_ui' => true ) ) as $ptype ) { |
---|
1177 | $ptype_obj = get_post_type_object( $ptype ); |
---|
1178 | // Submenus only. |
---|
1179 | if ( ! $ptype_obj->show_in_menu || $ptype_obj->show_in_menu === true ) |
---|
1180 | continue; |
---|
1181 | add_submenu_page( $ptype_obj->show_in_menu, $ptype_obj->labels->name, $ptype_obj->labels->name, $ptype_obj->cap->edit_posts, "edit.php?post_type=$ptype" ); |
---|
1182 | } |
---|
1183 | } |
---|
1184 | add_action( 'admin_menu', '_add_post_type_submenus' ); |
---|
1185 | |
---|
1186 | /** |
---|
1187 | * Register support of certain features for a post type. |
---|
1188 | * |
---|
1189 | * All features are directly associated with a functional area of the edit screen, such as the |
---|
1190 | * editor or a meta box: 'title', 'editor', 'comments', 'revisions', 'trackbacks', 'author', |
---|
1191 | * 'excerpt', 'page-attributes', 'thumbnail', and 'custom-fields'. |
---|
1192 | * |
---|
1193 | * Additionally, the 'revisions' feature dictates whether the post type will store revisions, |
---|
1194 | * and the 'comments' feature dicates whether the comments count will show on the edit screen. |
---|
1195 | * |
---|
1196 | * @since 3.0.0 |
---|
1197 | * @param string $post_type The post type for which to add the feature |
---|
1198 | * @param string|array $feature the feature being added, can be an array of feature strings or a single string |
---|
1199 | */ |
---|
1200 | function add_post_type_support( $post_type, $feature ) { |
---|
1201 | global $_wp_post_type_features; |
---|
1202 | |
---|
1203 | $features = (array) $feature; |
---|
1204 | foreach ($features as $feature) { |
---|
1205 | if ( func_num_args() == 2 ) |
---|
1206 | $_wp_post_type_features[$post_type][$feature] = true; |
---|
1207 | else |
---|
1208 | $_wp_post_type_features[$post_type][$feature] = array_slice( func_get_args(), 2 ); |
---|
1209 | } |
---|
1210 | } |
---|
1211 | |
---|
1212 | /** |
---|
1213 | * Remove support for a feature from a post type. |
---|
1214 | * |
---|
1215 | * @since 3.0.0 |
---|
1216 | * @param string $post_type The post type for which to remove the feature |
---|
1217 | * @param string $feature The feature being removed |
---|
1218 | */ |
---|
1219 | function remove_post_type_support( $post_type, $feature ) { |
---|
1220 | global $_wp_post_type_features; |
---|
1221 | |
---|
1222 | if ( !isset($_wp_post_type_features[$post_type]) ) |
---|
1223 | return; |
---|
1224 | |
---|
1225 | if ( isset($_wp_post_type_features[$post_type][$feature]) ) |
---|
1226 | unset($_wp_post_type_features[$post_type][$feature]); |
---|
1227 | } |
---|
1228 | |
---|
1229 | /** |
---|
1230 | * Checks a post type's support for a given feature |
---|
1231 | * |
---|
1232 | * @since 3.0.0 |
---|
1233 | * @param string $post_type The post type being checked |
---|
1234 | * @param string $feature the feature being checked |
---|
1235 | * @return boolean |
---|
1236 | */ |
---|
1237 | |
---|
1238 | function post_type_supports( $post_type, $feature ) { |
---|
1239 | global $_wp_post_type_features; |
---|
1240 | |
---|
1241 | if ( !isset( $_wp_post_type_features[$post_type][$feature] ) ) |
---|
1242 | return false; |
---|
1243 | |
---|
1244 | // If no args passed then no extra checks need be performed |
---|
1245 | if ( func_num_args() <= 2 ) |
---|
1246 | return true; |
---|
1247 | |
---|
1248 | // @todo Allow pluggable arg checking |
---|
1249 | //$args = array_slice( func_get_args(), 2 ); |
---|
1250 | |
---|
1251 | return true; |
---|
1252 | } |
---|
1253 | |
---|
1254 | /** |
---|
1255 | * Updates the post type for the post ID. |
---|
1256 | * |
---|
1257 | * The page or post cache will be cleaned for the post ID. |
---|
1258 | * |
---|
1259 | * @since 2.5.0 |
---|
1260 | * |
---|
1261 | * @uses $wpdb |
---|
1262 | * |
---|
1263 | * @param int $post_id Post ID to change post type. Not actually optional. |
---|
1264 | * @param string $post_type Optional, default is post. Supported values are 'post' or 'page' to |
---|
1265 | * name a few. |
---|
1266 | * @return int Amount of rows changed. Should be 1 for success and 0 for failure. |
---|
1267 | */ |
---|
1268 | function set_post_type( $post_id = 0, $post_type = 'post' ) { |
---|
1269 | global $wpdb; |
---|
1270 | |
---|
1271 | $post_type = sanitize_post_field('post_type', $post_type, $post_id, 'db'); |
---|
1272 | $return = $wpdb->update($wpdb->posts, array('post_type' => $post_type), array('ID' => $post_id) ); |
---|
1273 | |
---|
1274 | if ( 'page' == $post_type ) |
---|
1275 | clean_page_cache($post_id); |
---|
1276 | else |
---|
1277 | clean_post_cache($post_id); |
---|
1278 | |
---|
1279 | return $return; |
---|
1280 | } |
---|
1281 | |
---|
1282 | /** |
---|
1283 | * Retrieve list of latest posts or posts matching criteria. |
---|
1284 | * |
---|
1285 | * The defaults are as follows: |
---|
1286 | * 'numberposts' - Default is 5. Total number of posts to retrieve. |
---|
1287 | * 'offset' - Default is 0. See {@link WP_Query::query()} for more. |
---|
1288 | * 'category' - What category to pull the posts from. |
---|
1289 | * 'orderby' - Default is 'post_date'. How to order the posts. |
---|
1290 | * 'order' - Default is 'DESC'. The order to retrieve the posts. |
---|
1291 | * 'include' - See {@link WP_Query::query()} for more. |
---|
1292 | * 'exclude' - See {@link WP_Query::query()} for more. |
---|
1293 | * 'meta_key' - See {@link WP_Query::query()} for more. |
---|
1294 | * 'meta_value' - See {@link WP_Query::query()} for more. |
---|
1295 | * 'post_type' - Default is 'post'. Can be 'page', or 'attachment' to name a few. |
---|
1296 | * 'post_parent' - The parent of the post or post type. |
---|
1297 | * 'post_status' - Default is 'published'. Post status to retrieve. |
---|
1298 | * |
---|
1299 | * @since 1.2.0 |
---|
1300 | * @uses $wpdb |
---|
1301 | * @uses WP_Query::query() See for more default arguments and information. |
---|
1302 | * @link http://codex.wordpress.org/Template_Tags/get_posts |
---|
1303 | * |
---|
1304 | * @param array $args Optional. Overrides defaults. |
---|
1305 | * @return array List of posts. |
---|
1306 | */ |
---|
1307 | function get_posts($args = null) { |
---|
1308 | $defaults = array( |
---|
1309 | 'numberposts' => 5, 'offset' => 0, |
---|
1310 | 'category' => 0, 'orderby' => 'post_date', |
---|
1311 | 'order' => 'DESC', 'include' => array(), |
---|
1312 | 'exclude' => array(), 'meta_key' => '', |
---|
1313 | 'meta_value' =>'', 'post_type' => 'post', |
---|
1314 | 'suppress_filters' => true |
---|
1315 | ); |
---|
1316 | |
---|
1317 | $r = wp_parse_args( $args, $defaults ); |
---|
1318 | if ( empty( $r['post_status'] ) ) |
---|
1319 | $r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish'; |
---|
1320 | if ( ! empty($r['numberposts']) && empty($r['posts_per_page']) ) |
---|
1321 | $r['posts_per_page'] = $r['numberposts']; |
---|
1322 | if ( ! empty($r['category']) ) |
---|
1323 | $r['cat'] = $r['category']; |
---|
1324 | if ( ! empty($r['include']) ) { |
---|
1325 | $incposts = wp_parse_id_list( $r['include'] ); |
---|
1326 | $r['posts_per_page'] = count($incposts); // only the number of posts included |
---|
1327 | $r['post__in'] = $incposts; |
---|
1328 | } elseif ( ! empty($r['exclude']) ) |
---|
1329 | $r['post__not_in'] = wp_parse_id_list( $r['exclude'] ); |
---|
1330 | |
---|
1331 | $r['ignore_sticky_posts'] = true; |
---|
1332 | $r['no_found_rows'] = true; |
---|
1333 | |
---|
1334 | $get_posts = new WP_Query; |
---|
1335 | return $get_posts->query($r); |
---|
1336 | |
---|
1337 | } |
---|
1338 | |
---|
1339 | // |
---|
1340 | // Post meta functions |
---|
1341 | // |
---|
1342 | |
---|
1343 | /** |
---|
1344 | * Add meta data field to a post. |
---|
1345 | * |
---|
1346 | * Post meta data is called "Custom Fields" on the Administration Panels. |
---|
1347 | * |
---|
1348 | * @since 1.5.0 |
---|
1349 | * @uses $wpdb |
---|
1350 | * @link http://codex.wordpress.org/Function_Reference/add_post_meta |
---|
1351 | * |
---|
1352 | * @param int $post_id Post ID. |
---|
1353 | * @param string $meta_key Metadata name. |
---|
1354 | * @param mixed $meta_value Metadata value. |
---|
1355 | * @param bool $unique Optional, default is false. Whether the same key should not be added. |
---|
1356 | * @return bool False for failure. True for success. |
---|
1357 | */ |
---|
1358 | function add_post_meta($post_id, $meta_key, $meta_value, $unique = false) { |
---|
1359 | // make sure meta is added to the post, not a revision |
---|
1360 | if ( $the_post = wp_is_post_revision($post_id) ) |
---|
1361 | $post_id = $the_post; |
---|
1362 | |
---|
1363 | return add_metadata('post', $post_id, $meta_key, $meta_value, $unique); |
---|
1364 | } |
---|
1365 | |
---|
1366 | /** |
---|
1367 | * Remove metadata matching criteria from a post. |
---|
1368 | * |
---|
1369 | * You can match based on the key, or key and value. Removing based on key and |
---|
1370 | * value, will keep from removing duplicate metadata with the same key. It also |
---|
1371 | * allows removing all metadata matching key, if needed. |
---|
1372 | * |
---|
1373 | * @since 1.5.0 |
---|
1374 | * @uses $wpdb |
---|
1375 | * @link http://codex.wordpress.org/Function_Reference/delete_post_meta |
---|
1376 | * |
---|
1377 | * @param int $post_id post ID |
---|
1378 | * @param string $meta_key Metadata name. |
---|
1379 | * @param mixed $meta_value Optional. Metadata value. |
---|
1380 | * @return bool False for failure. True for success. |
---|
1381 | */ |
---|
1382 | function delete_post_meta($post_id, $meta_key, $meta_value = '') { |
---|
1383 | // make sure meta is added to the post, not a revision |
---|
1384 | if ( $the_post = wp_is_post_revision($post_id) ) |
---|
1385 | $post_id = $the_post; |
---|
1386 | |
---|
1387 | return delete_metadata('post', $post_id, $meta_key, $meta_value); |
---|
1388 | } |
---|
1389 | |
---|
1390 | /** |
---|
1391 | * Retrieve post meta field for a post. |
---|
1392 | * |
---|
1393 | * @since 1.5.0 |
---|
1394 | * @uses $wpdb |
---|
1395 | * @link http://codex.wordpress.org/Function_Reference/get_post_meta |
---|
1396 | * |
---|
1397 | * @param int $post_id Post ID. |
---|
1398 | * @param string $key The meta key to retrieve. |
---|
1399 | * @param bool $single Whether to return a single value. |
---|
1400 | * @return mixed Will be an array if $single is false. Will be value of meta data field if $single |
---|
1401 | * is true. |
---|
1402 | */ |
---|
1403 | function get_post_meta($post_id, $key, $single = false) { |
---|
1404 | return get_metadata('post', $post_id, $key, $single); |
---|
1405 | } |
---|
1406 | |
---|
1407 | /** |
---|
1408 | * Update post meta field based on post ID. |
---|
1409 | * |
---|
1410 | * Use the $prev_value parameter to differentiate between meta fields with the |
---|
1411 | * same key and post ID. |
---|
1412 | * |
---|
1413 | * If the meta field for the post does not exist, it will be added. |
---|
1414 | * |
---|
1415 | * @since 1.5 |
---|
1416 | * @uses $wpdb |
---|
1417 | * @link http://codex.wordpress.org/Function_Reference/update_post_meta |
---|
1418 | * |
---|
1419 | * @param int $post_id Post ID. |
---|
1420 | * @param string $meta_key Metadata key. |
---|
1421 | * @param mixed $meta_value Metadata value. |
---|
1422 | * @param mixed $prev_value Optional. Previous value to check before removing. |
---|
1423 | * @return bool False on failure, true if success. |
---|
1424 | */ |
---|
1425 | function update_post_meta($post_id, $meta_key, $meta_value, $prev_value = '') { |
---|
1426 | // make sure meta is added to the post, not a revision |
---|
1427 | if ( $the_post = wp_is_post_revision($post_id) ) |
---|
1428 | $post_id = $the_post; |
---|
1429 | |
---|
1430 | return update_metadata('post', $post_id, $meta_key, $meta_value, $prev_value); |
---|
1431 | } |
---|
1432 | |
---|
1433 | /** |
---|
1434 | * Delete everything from post meta matching meta key. |
---|
1435 | * |
---|
1436 | * @since 2.3.0 |
---|
1437 | * @uses $wpdb |
---|
1438 | * |
---|
1439 | * @param string $post_meta_key Key to search for when deleting. |
---|
1440 | * @return bool Whether the post meta key was deleted from the database |
---|
1441 | */ |
---|
1442 | function delete_post_meta_by_key($post_meta_key) { |
---|
1443 | if ( !$post_meta_key ) |
---|
1444 | return false; |
---|
1445 | |
---|
1446 | global $wpdb; |
---|
1447 | $post_ids = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT post_id FROM $wpdb->postmeta WHERE meta_key = %s", $post_meta_key)); |
---|
1448 | if ( $post_ids ) { |
---|
1449 | $postmetaids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = %s", $post_meta_key ) ); |
---|
1450 | $in = implode( ',', array_fill(1, count($postmetaids), '%d')); |
---|
1451 | do_action( 'delete_postmeta', $postmetaids ); |
---|
1452 | $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_id IN($in)", $postmetaids )); |
---|
1453 | do_action( 'deleted_postmeta', $postmetaids ); |
---|
1454 | foreach ( $post_ids as $post_id ) |
---|
1455 | wp_cache_delete($post_id, 'post_meta'); |
---|
1456 | return true; |
---|
1457 | } |
---|
1458 | return false; |
---|
1459 | } |
---|
1460 | |
---|
1461 | /** |
---|
1462 | * Retrieve post meta fields, based on post ID. |
---|
1463 | * |
---|
1464 | * The post meta fields are retrieved from the cache, so the function is |
---|
1465 | * optimized to be called more than once. It also applies to the functions, that |
---|
1466 | * use this function. |
---|
1467 | * |
---|
1468 | * @since 1.2.0 |
---|
1469 | * @link http://codex.wordpress.org/Function_Reference/get_post_custom |
---|
1470 | * |
---|
1471 | * @uses $id Current Loop Post ID |
---|
1472 | * |
---|
1473 | * @param int $post_id post ID |
---|
1474 | * @return array |
---|
1475 | */ |
---|
1476 | function get_post_custom( $post_id = 0 ) { |
---|
1477 | $post_id = absint( $post_id ); |
---|
1478 | |
---|
1479 | if ( ! $post_id ) |
---|
1480 | $post_id = get_the_ID(); |
---|
1481 | |
---|
1482 | if ( ! wp_cache_get( $post_id, 'post_meta' ) ) |
---|
1483 | update_postmeta_cache( $post_id ); |
---|
1484 | |
---|
1485 | return wp_cache_get( $post_id, 'post_meta' ); |
---|
1486 | } |
---|
1487 | |
---|
1488 | /** |
---|
1489 | * Retrieve meta field names for a post. |
---|
1490 | * |
---|
1491 | * If there are no meta fields, then nothing (null) will be returned. |
---|
1492 | * |
---|
1493 | * @since 1.2.0 |
---|
1494 | * @link http://codex.wordpress.org/Function_Reference/get_post_custom_keys |
---|
1495 | * |
---|
1496 | * @param int $post_id post ID |
---|
1497 | * @return array|null Either array of the keys, or null if keys could not be retrieved. |
---|
1498 | */ |
---|
1499 | function get_post_custom_keys( $post_id = 0 ) { |
---|
1500 | $custom = get_post_custom( $post_id ); |
---|
1501 | |
---|
1502 | if ( !is_array($custom) ) |
---|
1503 | return; |
---|
1504 | |
---|
1505 | if ( $keys = array_keys($custom) ) |
---|
1506 | return $keys; |
---|
1507 | } |
---|
1508 | |
---|
1509 | /** |
---|
1510 | * Retrieve values for a custom post field. |
---|
1511 | * |
---|
1512 | * The parameters must not be considered optional. All of the post meta fields |
---|
1513 | * will be retrieved and only the meta field key values returned. |
---|
1514 | * |
---|
1515 | * @since 1.2.0 |
---|
1516 | * @link http://codex.wordpress.org/Function_Reference/get_post_custom_values |
---|
1517 | * |
---|
1518 | * @param string $key Meta field key. |
---|
1519 | * @param int $post_id Post ID |
---|
1520 | * @return array Meta field values. |
---|
1521 | */ |
---|
1522 | function get_post_custom_values( $key = '', $post_id = 0 ) { |
---|
1523 | if ( !$key ) |
---|
1524 | return null; |
---|
1525 | |
---|
1526 | $custom = get_post_custom($post_id); |
---|
1527 | |
---|
1528 | return isset($custom[$key]) ? $custom[$key] : null; |
---|
1529 | } |
---|
1530 | |
---|
1531 | /** |
---|
1532 | * Check if post is sticky. |
---|
1533 | * |
---|
1534 | * Sticky posts should remain at the top of The Loop. If the post ID is not |
---|
1535 | * given, then The Loop ID for the current post will be used. |
---|
1536 | * |
---|
1537 | * @since 2.7.0 |
---|
1538 | * |
---|
1539 | * @param int $post_id Optional. Post ID. |
---|
1540 | * @return bool Whether post is sticky. |
---|
1541 | */ |
---|
1542 | function is_sticky( $post_id = 0 ) { |
---|
1543 | $post_id = absint( $post_id ); |
---|
1544 | |
---|
1545 | if ( ! $post_id ) |
---|
1546 | $post_id = get_the_ID(); |
---|
1547 | |
---|
1548 | $stickies = get_option( 'sticky_posts' ); |
---|
1549 | |
---|
1550 | if ( ! is_array( $stickies ) ) |
---|
1551 | return false; |
---|
1552 | |
---|
1553 | if ( in_array( $post_id, $stickies ) ) |
---|
1554 | return true; |
---|
1555 | |
---|
1556 | return false; |
---|
1557 | } |
---|
1558 | |
---|
1559 | /** |
---|
1560 | * Sanitize every post field. |
---|
1561 | * |
---|
1562 | * If the context is 'raw', then the post object or array will get minimal santization of the int fields. |
---|
1563 | * |
---|
1564 | * @since 2.3.0 |
---|
1565 | * @uses sanitize_post_field() Used to sanitize the fields. |
---|
1566 | * |
---|
1567 | * @param object|array $post The Post Object or Array |
---|
1568 | * @param string $context Optional, default is 'display'. How to sanitize post fields. |
---|
1569 | * @return object|array The now sanitized Post Object or Array (will be the same type as $post) |
---|
1570 | */ |
---|
1571 | function sanitize_post($post, $context = 'display') { |
---|
1572 | if ( is_object($post) ) { |
---|
1573 | // Check if post already filtered for this context |
---|
1574 | if ( isset($post->filter) && $context == $post->filter ) |
---|
1575 | return $post; |
---|
1576 | if ( !isset($post->ID) ) |
---|
1577 | $post->ID = 0; |
---|
1578 | foreach ( array_keys(get_object_vars($post)) as $field ) |
---|
1579 | $post->$field = sanitize_post_field($field, $post->$field, $post->ID, $context); |
---|
1580 | $post->filter = $context; |
---|
1581 | } else { |
---|
1582 | // Check if post already filtered for this context |
---|
1583 | if ( isset($post['filter']) && $context == $post['filter'] ) |
---|
1584 | return $post; |
---|
1585 | if ( !isset($post['ID']) ) |
---|
1586 | $post['ID'] = 0; |
---|
1587 | foreach ( array_keys($post) as $field ) |
---|
1588 | $post[$field] = sanitize_post_field($field, $post[$field], $post['ID'], $context); |
---|
1589 | $post['filter'] = $context; |
---|
1590 | } |
---|
1591 | return $post; |
---|
1592 | } |
---|
1593 | |
---|
1594 | /** |
---|
1595 | * Sanitize post field based on context. |
---|
1596 | * |
---|
1597 | * Possible context values are: 'raw', 'edit', 'db', 'display', 'attribute' and 'js'. The |
---|
1598 | * 'display' context is used by default. 'attribute' and 'js' contexts are treated like 'display' |
---|
1599 | * when calling filters. |
---|
1600 | * |
---|
1601 | * @since 2.3.0 |
---|
1602 | * @uses apply_filters() Calls 'edit_$field' and '${field_no_prefix}_edit_pre' passing $value and |
---|
1603 | * $post_id if $context == 'edit' and field name prefix == 'post_'. |
---|
1604 | * |
---|
1605 | * @uses apply_filters() Calls 'edit_post_$field' passing $value and $post_id if $context == 'db'. |
---|
1606 | * @uses apply_filters() Calls 'pre_$field' passing $value if $context == 'db' and field name prefix == 'post_'. |
---|
1607 | * @uses apply_filters() Calls '${field}_pre' passing $value if $context == 'db' and field name prefix != 'post_'. |
---|
1608 | * |
---|
1609 | * @uses apply_filters() Calls '$field' passing $value, $post_id and $context if $context == anything |
---|
1610 | * other than 'raw', 'edit' and 'db' and field name prefix == 'post_'. |
---|
1611 | * @uses apply_filters() Calls 'post_$field' passing $value if $context == anything other than 'raw', |
---|
1612 | * 'edit' and 'db' and field name prefix != 'post_'. |
---|
1613 | * |
---|
1614 | * @param string $field The Post Object field name. |
---|
1615 | * @param mixed $value The Post Object value. |
---|
1616 | * @param int $post_id Post ID. |
---|
1617 | * @param string $context How to sanitize post fields. Looks for 'raw', 'edit', 'db', 'display', |
---|
1618 | * 'attribute' and 'js'. |
---|
1619 | * @return mixed Sanitized value. |
---|
1620 | */ |
---|
1621 | function sanitize_post_field($field, $value, $post_id, $context) { |
---|
1622 | $int_fields = array('ID', 'post_parent', 'menu_order'); |
---|
1623 | if ( in_array($field, $int_fields) ) |
---|
1624 | $value = (int) $value; |
---|
1625 | |
---|
1626 | // Fields which contain arrays of ints. |
---|
1627 | $array_int_fields = array( 'ancestors' ); |
---|
1628 | if ( in_array($field, $array_int_fields) ) { |
---|
1629 | $value = array_map( 'absint', $value); |
---|
1630 | return $value; |
---|
1631 | } |
---|
1632 | |
---|
1633 | if ( 'raw' == $context ) |
---|
1634 | return $value; |
---|
1635 | |
---|
1636 | $prefixed = false; |
---|
1637 | if ( false !== strpos($field, 'post_') ) { |
---|
1638 | $prefixed = true; |
---|
1639 | $field_no_prefix = str_replace('post_', '', $field); |
---|
1640 | } |
---|
1641 | |
---|
1642 | if ( 'edit' == $context ) { |
---|
1643 | $format_to_edit = array('post_content', 'post_excerpt', 'post_title', 'post_password'); |
---|
1644 | |
---|
1645 | if ( $prefixed ) { |
---|
1646 | $value = apply_filters("edit_$field", $value, $post_id); |
---|
1647 | // Old school |
---|
1648 | $value = apply_filters("${field_no_prefix}_edit_pre", $value, $post_id); |
---|
1649 | } else { |
---|
1650 | $value = apply_filters("edit_post_$field", $value, $post_id); |
---|
1651 | } |
---|
1652 | |
---|
1653 | if ( in_array($field, $format_to_edit) ) { |
---|
1654 | if ( 'post_content' == $field ) |
---|
1655 | $value = format_to_edit($value, user_can_richedit()); |
---|
1656 | else |
---|
1657 | $value = format_to_edit($value); |
---|
1658 | } else { |
---|
1659 | $value = esc_attr($value); |
---|
1660 | } |
---|
1661 | } else if ( 'db' == $context ) { |
---|
1662 | if ( $prefixed ) { |
---|
1663 | $value = apply_filters("pre_$field", $value); |
---|
1664 | $value = apply_filters("${field_no_prefix}_save_pre", $value); |
---|
1665 | } else { |
---|
1666 | $value = apply_filters("pre_post_$field", $value); |
---|
1667 | $value = apply_filters("${field}_pre", $value); |
---|
1668 | } |
---|
1669 | } else { |
---|
1670 | // Use display filters by default. |
---|
1671 | if ( $prefixed ) |
---|
1672 | $value = apply_filters($field, $value, $post_id, $context); |
---|
1673 | else |
---|
1674 | $value = apply_filters("post_$field", $value, $post_id, $context); |
---|
1675 | } |
---|
1676 | |
---|
1677 | if ( 'attribute' == $context ) |
---|
1678 | $value = esc_attr($value); |
---|
1679 | else if ( 'js' == $context ) |
---|
1680 | $value = esc_js($value); |
---|
1681 | |
---|
1682 | return $value; |
---|
1683 | } |
---|
1684 | |
---|
1685 | /** |
---|
1686 | * Make a post sticky. |
---|
1687 | * |
---|
1688 | * Sticky posts should be displayed at the top of the front page. |
---|
1689 | * |
---|
1690 | * @since 2.7.0 |
---|
1691 | * |
---|
1692 | * @param int $post_id Post ID. |
---|
1693 | */ |
---|
1694 | function stick_post($post_id) { |
---|
1695 | $stickies = get_option('sticky_posts'); |
---|
1696 | |
---|
1697 | if ( !is_array($stickies) ) |
---|
1698 | $stickies = array($post_id); |
---|
1699 | |
---|
1700 | if ( ! in_array($post_id, $stickies) ) |
---|
1701 | $stickies[] = $post_id; |
---|
1702 | |
---|
1703 | update_option('sticky_posts', $stickies); |
---|
1704 | } |
---|
1705 | |
---|
1706 | /** |
---|
1707 | * Unstick a post. |
---|
1708 | * |
---|
1709 | * Sticky posts should be displayed at the top of the front page. |
---|
1710 | * |
---|
1711 | * @since 2.7.0 |
---|
1712 | * |
---|
1713 | * @param int $post_id Post ID. |
---|
1714 | */ |
---|
1715 | function unstick_post($post_id) { |
---|
1716 | $stickies = get_option('sticky_posts'); |
---|
1717 | |
---|
1718 | if ( !is_array($stickies) ) |
---|
1719 | return; |
---|
1720 | |
---|
1721 | if ( ! in_array($post_id, $stickies) ) |
---|
1722 | return; |
---|
1723 | |
---|
1724 | $offset = array_search($post_id, $stickies); |
---|
1725 | if ( false === $offset ) |
---|
1726 | return; |
---|
1727 | |
---|
1728 | array_splice($stickies, $offset, 1); |
---|
1729 | |
---|
1730 | update_option('sticky_posts', $stickies); |
---|
1731 | } |
---|
1732 | |
---|
1733 | /** |
---|
1734 | * Count number of posts of a post type and is user has permissions to view. |
---|
1735 | * |
---|
1736 | * This function provides an efficient method of finding the amount of post's |
---|
1737 | * type a blog has. Another method is to count the amount of items in |
---|
1738 | * get_posts(), but that method has a lot of overhead with doing so. Therefore, |
---|
1739 | * when developing for 2.5+, use this function instead. |
---|
1740 | * |
---|
1741 | * The $perm parameter checks for 'readable' value and if the user can read |
---|
1742 | * private posts, it will display that for the user that is signed in. |
---|
1743 | * |
---|
1744 | * @since 2.5.0 |
---|
1745 | * @link http://codex.wordpress.org/Template_Tags/wp_count_posts |
---|
1746 | * |
---|
1747 | * @param string $type Optional. Post type to retrieve count |
---|
1748 | * @param string $perm Optional. 'readable' or empty. |
---|
1749 | * @return object Number of posts for each status |
---|
1750 | */ |
---|
1751 | function wp_count_posts( $type = 'post', $perm = '' ) { |
---|
1752 | global $wpdb; |
---|
1753 | |
---|
1754 | $user = wp_get_current_user(); |
---|
1755 | |
---|
1756 | $cache_key = $type; |
---|
1757 | |
---|
1758 | $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s"; |
---|
1759 | if ( 'readable' == $perm && is_user_logged_in() ) { |
---|
1760 | $post_type_object = get_post_type_object($type); |
---|
1761 | if ( !current_user_can( $post_type_object->cap->read_private_posts ) ) { |
---|
1762 | $cache_key .= '_' . $perm . '_' . $user->ID; |
---|
1763 | $query .= " AND (post_status != 'private' OR ( post_author = '$user->ID' AND post_status = 'private' ))"; |
---|
1764 | } |
---|
1765 | } |
---|
1766 | $query .= ' GROUP BY post_status'; |
---|
1767 | |
---|
1768 | $count = wp_cache_get($cache_key, 'counts'); |
---|
1769 | if ( false !== $count ) |
---|
1770 | return $count; |
---|
1771 | |
---|
1772 | $count = $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A ); |
---|
1773 | |
---|
1774 | $stats = array(); |
---|
1775 | foreach ( get_post_stati() as $state ) |
---|
1776 | $stats[$state] = 0; |
---|
1777 | |
---|
1778 | foreach ( (array) $count as $row ) |
---|
1779 | $stats[$row['post_status']] = $row['num_posts']; |
---|
1780 | |
---|
1781 | $stats = (object) $stats; |
---|
1782 | wp_cache_set($cache_key, $stats, 'counts'); |
---|
1783 | |
---|
1784 | return $stats; |
---|
1785 | } |
---|
1786 | |
---|
1787 | |
---|
1788 | /** |
---|
1789 | * Count number of attachments for the mime type(s). |
---|
1790 | * |
---|
1791 | * If you set the optional mime_type parameter, then an array will still be |
---|
1792 | * returned, but will only have the item you are looking for. It does not give |
---|
1793 | * you the number of attachments that are children of a post. You can get that |
---|
1794 | * by counting the number of children that post has. |
---|
1795 | * |
---|
1796 | * @since 2.5.0 |
---|
1797 | * |
---|
1798 | * @param string|array $mime_type Optional. Array or comma-separated list of MIME patterns. |
---|
1799 | * @return array Number of posts for each mime type. |
---|
1800 | */ |
---|
1801 | function wp_count_attachments( $mime_type = '' ) { |
---|
1802 | global $wpdb; |
---|
1803 | |
---|
1804 | $and = wp_post_mime_type_where( $mime_type ); |
---|
1805 | $count = $wpdb->get_results( "SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' $and GROUP BY post_mime_type", ARRAY_A ); |
---|
1806 | |
---|
1807 | $stats = array( ); |
---|
1808 | foreach( (array) $count as $row ) { |
---|
1809 | $stats[$row['post_mime_type']] = $row['num_posts']; |
---|
1810 | } |
---|
1811 | $stats['trash'] = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status = 'trash' $and"); |
---|
1812 | |
---|
1813 | return (object) $stats; |
---|
1814 | } |
---|
1815 | |
---|
1816 | /** |
---|
1817 | * Check a MIME-Type against a list. |
---|
1818 | * |
---|
1819 | * If the wildcard_mime_types parameter is a string, it must be comma separated |
---|
1820 | * list. If the real_mime_types is a string, it is also comma separated to |
---|
1821 | * create the list. |
---|
1822 | * |
---|
1823 | * @since 2.5.0 |
---|
1824 | * |
---|
1825 | * @param string|array $wildcard_mime_types e.g. audio/mpeg or image (same as image/*) or |
---|
1826 | * flash (same as *flash*). |
---|
1827 | * @param string|array $real_mime_types post_mime_type values |
---|
1828 | * @return array array(wildcard=>array(real types)) |
---|
1829 | */ |
---|
1830 | function wp_match_mime_types($wildcard_mime_types, $real_mime_types) { |
---|
1831 | $matches = array(); |
---|
1832 | if ( is_string($wildcard_mime_types) ) |
---|
1833 | $wildcard_mime_types = array_map('trim', explode(',', $wildcard_mime_types)); |
---|
1834 | if ( is_string($real_mime_types) ) |
---|
1835 | $real_mime_types = array_map('trim', explode(',', $real_mime_types)); |
---|
1836 | $wild = '[-._a-z0-9]*'; |
---|
1837 | foreach ( (array) $wildcard_mime_types as $type ) { |
---|
1838 | $type = str_replace('*', $wild, $type); |
---|
1839 | $patternses[1][$type] = "^$type$"; |
---|
1840 | if ( false === strpos($type, '/') ) { |
---|
1841 | $patternses[2][$type] = "^$type/"; |
---|
1842 | $patternses[3][$type] = $type; |
---|
1843 | } |
---|
1844 | } |
---|
1845 | asort($patternses); |
---|
1846 | foreach ( $patternses as $patterns ) |
---|
1847 | foreach ( $patterns as $type => $pattern ) |
---|
1848 | foreach ( (array) $real_mime_types as $real ) |
---|
1849 | if ( preg_match("#$pattern#", $real) && ( empty($matches[$type]) || false === array_search($real, $matches[$type]) ) ) |
---|
1850 | $matches[$type][] = $real; |
---|
1851 | return $matches; |
---|
1852 | } |
---|
1853 | |
---|
1854 | /** |
---|
1855 | * Convert MIME types into SQL. |
---|
1856 | * |
---|
1857 | * @since 2.5.0 |
---|
1858 | * |
---|
1859 | * @param string|array $post_mime_types List of mime types or comma separated string of mime types. |
---|
1860 | * @param string $table_alias Optional. Specify a table alias, if needed. |
---|
1861 | * @return string The SQL AND clause for mime searching. |
---|
1862 | */ |
---|
1863 | function wp_post_mime_type_where($post_mime_types, $table_alias = '') { |
---|
1864 | $where = ''; |
---|
1865 | $wildcards = array('', '%', '%/%'); |
---|
1866 | if ( is_string($post_mime_types) ) |
---|
1867 | $post_mime_types = array_map('trim', explode(',', $post_mime_types)); |
---|
1868 | foreach ( (array) $post_mime_types as $mime_type ) { |
---|
1869 | $mime_type = preg_replace('/\s/', '', $mime_type); |
---|
1870 | $slashpos = strpos($mime_type, '/'); |
---|
1871 | if ( false !== $slashpos ) { |
---|
1872 | $mime_group = preg_replace('/[^-*.a-zA-Z0-9]/', '', substr($mime_type, 0, $slashpos)); |
---|
1873 | $mime_subgroup = preg_replace('/[^-*.+a-zA-Z0-9]/', '', substr($mime_type, $slashpos + 1)); |
---|
1874 | if ( empty($mime_subgroup) ) |
---|
1875 | $mime_subgroup = '*'; |
---|
1876 | else |
---|
1877 | $mime_subgroup = str_replace('/', '', $mime_subgroup); |
---|
1878 | $mime_pattern = "$mime_group/$mime_subgroup"; |
---|
1879 | } else { |
---|
1880 | $mime_pattern = preg_replace('/[^-*.a-zA-Z0-9]/', '', $mime_type); |
---|
1881 | if ( false === strpos($mime_pattern, '*') ) |
---|
1882 | $mime_pattern .= '/*'; |
---|
1883 | } |
---|
1884 | |
---|
1885 | $mime_pattern = preg_replace('/\*+/', '%', $mime_pattern); |
---|
1886 | |
---|
1887 | if ( in_array( $mime_type, $wildcards ) ) |
---|
1888 | return ''; |
---|
1889 | |
---|
1890 | if ( false !== strpos($mime_pattern, '%') ) |
---|
1891 | $wheres[] = empty($table_alias) ? "post_mime_type LIKE '$mime_pattern'" : "$table_alias.post_mime_type LIKE '$mime_pattern'"; |
---|
1892 | else |
---|
1893 | $wheres[] = empty($table_alias) ? "post_mime_type = '$mime_pattern'" : "$table_alias.post_mime_type = '$mime_pattern'"; |
---|
1894 | } |
---|
1895 | if ( !empty($wheres) ) |
---|
1896 | $where = ' AND (' . join(' OR ', $wheres) . ') '; |
---|
1897 | return $where; |
---|
1898 | } |
---|
1899 | |
---|
1900 | /** |
---|
1901 | * Trashes or deletes a post or page. |
---|
1902 | * |
---|
1903 | * When the post and page is permanently deleted, everything that is tied to it is deleted also. |
---|
1904 | * This includes comments, post meta fields, and terms associated with the post. |
---|
1905 | * |
---|
1906 | * The post or page is moved to trash instead of permanently deleted unless trash is |
---|
1907 | * disabled, item is already in the trash, or $force_delete is true. |
---|
1908 | * |
---|
1909 | * @since 1.0.0 |
---|
1910 | * @uses do_action() on 'delete_post' before deletion unless post type is 'attachment'. |
---|
1911 | * @uses do_action() on 'deleted_post' after deletion unless post type is 'attachment'. |
---|
1912 | * @uses wp_delete_attachment() if post type is 'attachment'. |
---|
1913 | * @uses wp_trash_post() if item should be trashed. |
---|
1914 | * |
---|
1915 | * @param int $postid Post ID. |
---|
1916 | * @param bool $force_delete Whether to bypass trash and force deletion. Defaults to false. |
---|
1917 | * @return mixed False on failure |
---|
1918 | */ |
---|
1919 | function wp_delete_post( $postid = 0, $force_delete = false ) { |
---|
1920 | global $wpdb, $wp_rewrite; |
---|
1921 | |
---|
1922 | if ( !$post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $postid)) ) |
---|
1923 | return $post; |
---|
1924 | |
---|
1925 | if ( !$force_delete && ( $post->post_type == 'post' || $post->post_type == 'page') && get_post_status( $postid ) != 'trash' && EMPTY_TRASH_DAYS ) |
---|
1926 | return wp_trash_post($postid); |
---|
1927 | |
---|
1928 | if ( $post->post_type == 'attachment' ) |
---|
1929 | return wp_delete_attachment( $postid, $force_delete ); |
---|
1930 | |
---|
1931 | do_action('delete_post', $postid); |
---|
1932 | |
---|
1933 | delete_post_meta($postid,'_wp_trash_meta_status'); |
---|
1934 | delete_post_meta($postid,'_wp_trash_meta_time'); |
---|
1935 | |
---|
1936 | wp_delete_object_term_relationships($postid, get_object_taxonomies($post->post_type)); |
---|
1937 | |
---|
1938 | $parent_data = array( 'post_parent' => $post->post_parent ); |
---|
1939 | $parent_where = array( 'post_parent' => $postid ); |
---|
1940 | |
---|
1941 | if ( 'page' == $post->post_type) { |
---|
1942 | // if the page is defined in option page_on_front or post_for_posts, |
---|
1943 | // adjust the corresponding options |
---|
1944 | if ( get_option('page_on_front') == $postid ) { |
---|
1945 | update_option('show_on_front', 'posts'); |
---|
1946 | delete_option('page_on_front'); |
---|
1947 | } |
---|
1948 | if ( get_option('page_for_posts') == $postid ) { |
---|
1949 | delete_option('page_for_posts'); |
---|
1950 | } |
---|
1951 | |
---|
1952 | // Point children of this page to its parent, also clean the cache of affected children |
---|
1953 | $children_query = $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_parent = %d AND post_type='page'", $postid); |
---|
1954 | $children = $wpdb->get_results($children_query); |
---|
1955 | |
---|
1956 | $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'page' ) ); |
---|
1957 | } else { |
---|
1958 | unstick_post($postid); |
---|
1959 | } |
---|
1960 | |
---|
1961 | // Do raw query. wp_get_post_revisions() is filtered |
---|
1962 | $revision_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'revision'", $postid ) ); |
---|
1963 | // Use wp_delete_post (via wp_delete_post_revision) again. Ensures any meta/misplaced data gets cleaned up. |
---|
1964 | foreach ( $revision_ids as $revision_id ) |
---|
1965 | wp_delete_post_revision( $revision_id ); |
---|
1966 | |
---|
1967 | // Point all attachments to this post up one level |
---|
1968 | $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'attachment' ) ); |
---|
1969 | |
---|
1970 | $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $postid )); |
---|
1971 | if ( ! empty($comment_ids) ) { |
---|
1972 | do_action( 'delete_comment', $comment_ids ); |
---|
1973 | foreach ( $comment_ids as $comment_id ) |
---|
1974 | wp_delete_comment( $comment_id, true ); |
---|
1975 | do_action( 'deleted_comment', $comment_ids ); |
---|
1976 | } |
---|
1977 | |
---|
1978 | $post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $postid )); |
---|
1979 | if ( !empty($post_meta_ids) ) { |
---|
1980 | do_action( 'delete_postmeta', $post_meta_ids ); |
---|
1981 | $in_post_meta_ids = "'" . implode("', '", $post_meta_ids) . "'"; |
---|
1982 | $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_id IN($in_post_meta_ids)" ); |
---|
1983 | do_action( 'deleted_postmeta', $post_meta_ids ); |
---|
1984 | } |
---|
1985 | |
---|
1986 | do_action( 'delete_post', $postid ); |
---|
1987 | $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->posts WHERE ID = %d", $postid )); |
---|
1988 | do_action( 'deleted_post', $postid ); |
---|
1989 | |
---|
1990 | if ( 'page' == $post->post_type ) { |
---|
1991 | clean_page_cache($postid); |
---|
1992 | |
---|
1993 | foreach ( (array) $children as $child ) |
---|
1994 | clean_page_cache($child->ID); |
---|
1995 | |
---|
1996 | $wp_rewrite->flush_rules(false); |
---|
1997 | } else { |
---|
1998 | clean_post_cache($postid); |
---|
1999 | } |
---|
2000 | |
---|
2001 | wp_clear_scheduled_hook('publish_future_post', array( $postid ) ); |
---|
2002 | |
---|
2003 | do_action('deleted_post', $postid); |
---|
2004 | |
---|
2005 | return $post; |
---|
2006 | } |
---|
2007 | |
---|
2008 | /** |
---|
2009 | * Moves a post or page to the Trash |
---|
2010 | * |
---|
2011 | * If trash is disabled, the post or page is permanently deleted. |
---|
2012 | * |
---|
2013 | * @since 2.9.0 |
---|
2014 | * @uses do_action() on 'trash_post' before trashing |
---|
2015 | * @uses do_action() on 'trashed_post' after trashing |
---|
2016 | * @uses wp_delete_post() if trash is disabled |
---|
2017 | * |
---|
2018 | * @param int $post_id Post ID. |
---|
2019 | * @return mixed False on failure |
---|
2020 | */ |
---|
2021 | function wp_trash_post($post_id = 0) { |
---|
2022 | if ( !EMPTY_TRASH_DAYS ) |
---|
2023 | return wp_delete_post($post_id, true); |
---|
2024 | |
---|
2025 | if ( !$post = wp_get_single_post($post_id, ARRAY_A) ) |
---|
2026 | return $post; |
---|
2027 | |
---|
2028 | if ( $post['post_status'] == 'trash' ) |
---|
2029 | return false; |
---|
2030 | |
---|
2031 | do_action('trash_post', $post_id); |
---|
2032 | |
---|
2033 | add_post_meta($post_id,'_wp_trash_meta_status', $post['post_status']); |
---|
2034 | add_post_meta($post_id,'_wp_trash_meta_time', time()); |
---|
2035 | |
---|
2036 | $post['post_status'] = 'trash'; |
---|
2037 | wp_insert_post($post); |
---|
2038 | |
---|
2039 | wp_trash_post_comments($post_id); |
---|
2040 | |
---|
2041 | do_action('trashed_post', $post_id); |
---|
2042 | |
---|
2043 | return $post; |
---|
2044 | } |
---|
2045 | |
---|
2046 | /** |
---|
2047 | * Restores a post or page from the Trash |
---|
2048 | * |
---|
2049 | * @since 2.9.0 |
---|
2050 | * @uses do_action() on 'untrash_post' before undeletion |
---|
2051 | * @uses do_action() on 'untrashed_post' after undeletion |
---|
2052 | * |
---|
2053 | * @param int $post_id Post ID. |
---|
2054 | * @return mixed False on failure |
---|
2055 | */ |
---|
2056 | function wp_untrash_post($post_id = 0) { |
---|
2057 | if ( !$post = wp_get_single_post($post_id, ARRAY_A) ) |
---|
2058 | return $post; |
---|
2059 | |
---|
2060 | if ( $post['post_status'] != 'trash' ) |
---|
2061 | return false; |
---|
2062 | |
---|
2063 | do_action('untrash_post', $post_id); |
---|
2064 | |
---|
2065 | $post_status = get_post_meta($post_id, '_wp_trash_meta_status', true); |
---|
2066 | |
---|
2067 | $post['post_status'] = $post_status; |
---|
2068 | |
---|
2069 | delete_post_meta($post_id, '_wp_trash_meta_status'); |
---|
2070 | delete_post_meta($post_id, '_wp_trash_meta_time'); |
---|
2071 | |
---|
2072 | wp_insert_post($post); |
---|
2073 | |
---|
2074 | wp_untrash_post_comments($post_id); |
---|
2075 | |
---|
2076 | do_action('untrashed_post', $post_id); |
---|
2077 | |
---|
2078 | return $post; |
---|
2079 | } |
---|
2080 | |
---|
2081 | /** |
---|
2082 | * Moves comments for a post to the trash |
---|
2083 | * |
---|
2084 | * @since 2.9.0 |
---|
2085 | * @uses do_action() on 'trash_post_comments' before trashing |
---|
2086 | * @uses do_action() on 'trashed_post_comments' after trashing |
---|
2087 | * |
---|
2088 | * @param int $post Post ID or object. |
---|
2089 | * @return mixed False on failure |
---|
2090 | */ |
---|
2091 | function wp_trash_post_comments($post = null) { |
---|
2092 | global $wpdb; |
---|
2093 | |
---|
2094 | $post = get_post($post); |
---|
2095 | if ( empty($post) ) |
---|
2096 | return; |
---|
2097 | |
---|
2098 | $post_id = $post->ID; |
---|
2099 | |
---|
2100 | do_action('trash_post_comments', $post_id); |
---|
2101 | |
---|
2102 | $comments = $wpdb->get_results( $wpdb->prepare("SELECT comment_ID, comment_approved FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id) ); |
---|
2103 | if ( empty($comments) ) |
---|
2104 | return; |
---|
2105 | |
---|
2106 | // Cache current status for each comment |
---|
2107 | $statuses = array(); |
---|
2108 | foreach ( $comments as $comment ) |
---|
2109 | $statuses[$comment->comment_ID] = $comment->comment_approved; |
---|
2110 | add_post_meta($post_id, '_wp_trash_meta_comments_status', $statuses); |
---|
2111 | |
---|
2112 | // Set status for all comments to post-trashed |
---|
2113 | $result = $wpdb->update($wpdb->comments, array('comment_approved' => 'post-trashed'), array('comment_post_ID' => $post_id)); |
---|
2114 | |
---|
2115 | clean_comment_cache( array_keys($statuses) ); |
---|
2116 | |
---|
2117 | do_action('trashed_post_comments', $post_id, $statuses); |
---|
2118 | |
---|
2119 | return $result; |
---|
2120 | } |
---|
2121 | |
---|
2122 | /** |
---|
2123 | * Restore comments for a post from the trash |
---|
2124 | * |
---|
2125 | * @since 2.9.0 |
---|
2126 | * @uses do_action() on 'untrash_post_comments' before trashing |
---|
2127 | * @uses do_action() on 'untrashed_post_comments' after trashing |
---|
2128 | * |
---|
2129 | * @param int $post Post ID or object. |
---|
2130 | * @return mixed False on failure |
---|
2131 | */ |
---|
2132 | function wp_untrash_post_comments($post = null) { |
---|
2133 | global $wpdb; |
---|
2134 | |
---|
2135 | $post = get_post($post); |
---|
2136 | if ( empty($post) ) |
---|
2137 | return; |
---|
2138 | |
---|
2139 | $post_id = $post->ID; |
---|
2140 | |
---|
2141 | $statuses = get_post_meta($post_id, '_wp_trash_meta_comments_status', true); |
---|
2142 | |
---|
2143 | if ( empty($statuses) ) |
---|
2144 | return true; |
---|
2145 | |
---|
2146 | do_action('untrash_post_comments', $post_id); |
---|
2147 | |
---|
2148 | // Restore each comment to its original status |
---|
2149 | $group_by_status = array(); |
---|
2150 | foreach ( $statuses as $comment_id => $comment_status ) |
---|
2151 | $group_by_status[$comment_status][] = $comment_id; |
---|
2152 | |
---|
2153 | foreach ( $group_by_status as $status => $comments ) { |
---|
2154 | // Sanity check. This shouldn't happen. |
---|
2155 | if ( 'post-trashed' == $status ) |
---|
2156 | $status = '0'; |
---|
2157 | $comments_in = implode( "', '", $comments ); |
---|
2158 | $wpdb->query( "UPDATE $wpdb->comments SET comment_approved = '$status' WHERE comment_ID IN ('" . $comments_in . "')" ); |
---|
2159 | } |
---|
2160 | |
---|
2161 | clean_comment_cache( array_keys($statuses) ); |
---|
2162 | |
---|
2163 | delete_post_meta($post_id, '_wp_trash_meta_comments_status'); |
---|
2164 | |
---|
2165 | do_action('untrashed_post_comments', $post_id); |
---|
2166 | } |
---|
2167 | |
---|
2168 | /** |
---|
2169 | * Retrieve the list of categories for a post. |
---|
2170 | * |
---|
2171 | * Compatibility layer for themes and plugins. Also an easy layer of abstraction |
---|
2172 | * away from the complexity of the taxonomy layer. |
---|
2173 | * |
---|
2174 | * @since 2.1.0 |
---|
2175 | * |
---|
2176 | * @uses wp_get_object_terms() Retrieves the categories. Args details can be found here. |
---|
2177 | * |
---|
2178 | * @param int $post_id Optional. The Post ID. |
---|
2179 | * @param array $args Optional. Overwrite the defaults. |
---|
2180 | * @return array |
---|
2181 | */ |
---|
2182 | function wp_get_post_categories( $post_id = 0, $args = array() ) { |
---|
2183 | $post_id = (int) $post_id; |
---|
2184 | |
---|
2185 | $defaults = array('fields' => 'ids'); |
---|
2186 | $args = wp_parse_args( $args, $defaults ); |
---|
2187 | |
---|
2188 | $cats = wp_get_object_terms($post_id, 'category', $args); |
---|
2189 | return $cats; |
---|
2190 | } |
---|
2191 | |
---|
2192 | /** |
---|
2193 | * Retrieve the tags for a post. |
---|
2194 | * |
---|
2195 | * There is only one default for this function, called 'fields' and by default |
---|
2196 | * is set to 'all'. There are other defaults that can be overridden in |
---|
2197 | * {@link wp_get_object_terms()}. |
---|
2198 | * |
---|
2199 | * @package WordPress |
---|
2200 | * @subpackage Post |
---|
2201 | * @since 2.3.0 |
---|
2202 | * |
---|
2203 | * @uses wp_get_object_terms() Gets the tags for returning. Args can be found here |
---|
2204 | * |
---|
2205 | * @param int $post_id Optional. The Post ID |
---|
2206 | * @param array $args Optional. Overwrite the defaults |
---|
2207 | * @return array List of post tags. |
---|
2208 | */ |
---|
2209 | function wp_get_post_tags( $post_id = 0, $args = array() ) { |
---|
2210 | return wp_get_post_terms( $post_id, 'post_tag', $args); |
---|
2211 | } |
---|
2212 | |
---|
2213 | /** |
---|
2214 | * Retrieve the terms for a post. |
---|
2215 | * |
---|
2216 | * There is only one default for this function, called 'fields' and by default |
---|
2217 | * is set to 'all'. There are other defaults that can be overridden in |
---|
2218 | * {@link wp_get_object_terms()}. |
---|
2219 | * |
---|
2220 | * @package WordPress |
---|
2221 | * @subpackage Post |
---|
2222 | * @since 2.8.0 |
---|
2223 | * |
---|
2224 | * @uses wp_get_object_terms() Gets the tags for returning. Args can be found here |
---|
2225 | * |
---|
2226 | * @param int $post_id Optional. The Post ID |
---|
2227 | * @param string $taxonomy The taxonomy for which to retrieve terms. Defaults to post_tag. |
---|
2228 | * @param array $args Optional. Overwrite the defaults |
---|
2229 | * @return array List of post tags. |
---|
2230 | */ |
---|
2231 | function wp_get_post_terms( $post_id = 0, $taxonomy = 'post_tag', $args = array() ) { |
---|
2232 | $post_id = (int) $post_id; |
---|
2233 | |
---|
2234 | $defaults = array('fields' => 'all'); |
---|
2235 | $args = wp_parse_args( $args, $defaults ); |
---|
2236 | |
---|
2237 | $tags = wp_get_object_terms($post_id, $taxonomy, $args); |
---|
2238 | |
---|
2239 | return $tags; |
---|
2240 | } |
---|
2241 | |
---|
2242 | /** |
---|
2243 | * Retrieve number of recent posts. |
---|
2244 | * |
---|
2245 | * @since 1.0.0 |
---|
2246 | * @uses $wpdb |
---|
2247 | * |
---|
2248 | * @param int $num Optional, default is 10. Number of posts to get. |
---|
2249 | * @return array List of posts. |
---|
2250 | */ |
---|
2251 | function wp_get_recent_posts($num = 10) { |
---|
2252 | global $wpdb; |
---|
2253 | |
---|
2254 | // Set the limit clause, if we got a limit |
---|
2255 | $num = (int) $num; |
---|
2256 | if ( $num ) { |
---|
2257 | $limit = "LIMIT $num"; |
---|
2258 | } |
---|
2259 | |
---|
2260 | $sql = "SELECT * FROM $wpdb->posts WHERE post_type = 'post' AND post_status IN ( 'draft', 'publish', 'future', 'pending', 'private' ) ORDER BY post_date DESC $limit"; |
---|
2261 | $result = $wpdb->get_results($sql, ARRAY_A); |
---|
2262 | |
---|
2263 | return $result ? $result : array(); |
---|
2264 | } |
---|
2265 | |
---|
2266 | /** |
---|
2267 | * Retrieve a single post, based on post ID. |
---|
2268 | * |
---|
2269 | * Has categories in 'post_category' property or key. Has tags in 'tags_input' |
---|
2270 | * property or key. |
---|
2271 | * |
---|
2272 | * @since 1.0.0 |
---|
2273 | * |
---|
2274 | * @param int $postid Post ID. |
---|
2275 | * @param string $mode How to return result, either OBJECT, ARRAY_N, or ARRAY_A. |
---|
2276 | * @return object|array Post object or array holding post contents and information |
---|
2277 | */ |
---|
2278 | function wp_get_single_post($postid = 0, $mode = OBJECT) { |
---|
2279 | $postid = (int) $postid; |
---|
2280 | |
---|
2281 | $post = get_post($postid, $mode); |
---|
2282 | |
---|
2283 | if ( |
---|
2284 | ( OBJECT == $mode && empty( $post->ID ) ) || |
---|
2285 | ( OBJECT != $mode && empty( $post['ID'] ) ) |
---|
2286 | ) |
---|
2287 | return ( OBJECT == $mode ? null : array() ); |
---|
2288 | |
---|
2289 | // Set categories and tags |
---|
2290 | if ( $mode == OBJECT ) { |
---|
2291 | $post->post_category = array(); |
---|
2292 | if ( is_object_in_taxonomy($post->post_type, 'category') ) |
---|
2293 | $post->post_category = wp_get_post_categories($postid); |
---|
2294 | $post->tags_input = array(); |
---|
2295 | if ( is_object_in_taxonomy($post->post_type, 'post_tag') ) |
---|
2296 | $post->tags_input = wp_get_post_tags($postid, array('fields' => 'names')); |
---|
2297 | } else { |
---|
2298 | $post['post_category'] = array(); |
---|
2299 | if ( is_object_in_taxonomy($post['post_type'], 'category') ) |
---|
2300 | $post['post_category'] = wp_get_post_categories($postid); |
---|
2301 | $post['tags_input'] = array(); |
---|
2302 | if ( is_object_in_taxonomy($post['post_type'], 'post_tag') ) |
---|
2303 | $post['tags_input'] = wp_get_post_tags($postid, array('fields' => 'names')); |
---|
2304 | } |
---|
2305 | |
---|
2306 | return $post; |
---|
2307 | } |
---|
2308 | |
---|
2309 | /** |
---|
2310 | * Insert a post. |
---|
2311 | * |
---|
2312 | * If the $postarr parameter has 'ID' set to a value, then post will be updated. |
---|
2313 | * |
---|
2314 | * You can set the post date manually, but setting the values for 'post_date' |
---|
2315 | * and 'post_date_gmt' keys. You can close the comments or open the comments by |
---|
2316 | * setting the value for 'comment_status' key. |
---|
2317 | * |
---|
2318 | * The defaults for the parameter $postarr are: |
---|
2319 | * 'post_status' - Default is 'draft'. |
---|
2320 | * 'post_type' - Default is 'post'. |
---|
2321 | * 'post_author' - Default is current user ID ($user_ID). The ID of the user who added the post. |
---|
2322 | * 'ping_status' - Default is the value in 'default_ping_status' option. |
---|
2323 | * Whether the attachment can accept pings. |
---|
2324 | * 'post_parent' - Default is 0. Set this for the post it belongs to, if any. |
---|
2325 | * 'menu_order' - Default is 0. The order it is displayed. |
---|
2326 | * 'to_ping' - Whether to ping. |
---|
2327 | * 'pinged' - Default is empty string. |
---|
2328 | * 'post_password' - Default is empty string. The password to access the attachment. |
---|
2329 | * 'guid' - Global Unique ID for referencing the attachment. |
---|
2330 | * 'post_content_filtered' - Post content filtered. |
---|
2331 | * 'post_excerpt' - Post excerpt. |
---|
2332 | * |
---|
2333 | * @since 1.0.0 |
---|
2334 | * @uses $wpdb |
---|
2335 | * @uses $wp_rewrite |
---|
2336 | * @uses $user_ID |
---|
2337 | * @uses do_action() Calls 'pre_post_update' on post ID if this is an update. |
---|
2338 | * @uses do_action() Calls 'pre_post_insert' passing $data, $postarr prior to database insert. |
---|
2339 | * @uses do_action() Calls 'edit_post' action on post ID and post data if this is an update. |
---|
2340 | * @uses do_action() Calls 'save_post' and 'wp_insert_post' on post id and post data just before returning. |
---|
2341 | * @uses apply_filters() Calls 'wp_insert_post_data' passing $data, $postarr prior to database update or insert. |
---|
2342 | * @uses wp_transition_post_status() |
---|
2343 | * |
---|
2344 | * @param array $postarr Elements that make up post to insert. |
---|
2345 | * @param bool $wp_error Optional. Allow return of WP_Error on failure. |
---|
2346 | * @return int|WP_Error The value 0 or WP_Error on failure. The post ID on success. |
---|
2347 | */ |
---|
2348 | function wp_insert_post($postarr, $wp_error = false) { |
---|
2349 | global $wpdb, $wp_rewrite, $user_ID; |
---|
2350 | |
---|
2351 | $defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_ID, |
---|
2352 | 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0, |
---|
2353 | 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '', |
---|
2354 | 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0, |
---|
2355 | 'post_content' => '', 'post_title' => ''); |
---|
2356 | |
---|
2357 | $postarr = wp_parse_args($postarr, $defaults); |
---|
2358 | $postarr = sanitize_post($postarr, 'db'); |
---|
2359 | |
---|
2360 | // export array as variables |
---|
2361 | extract($postarr, EXTR_SKIP); |
---|
2362 | |
---|
2363 | // Are we updating or creating? |
---|
2364 | $update = false; |
---|
2365 | if ( !empty($ID) ) { |
---|
2366 | $update = true; |
---|
2367 | $previous_status = get_post_field('post_status', $ID); |
---|
2368 | } else { |
---|
2369 | $previous_status = 'new'; |
---|
2370 | } |
---|
2371 | |
---|
2372 | if ( ('' == $post_content) && ('' == $post_title) && ('' == $post_excerpt) && ('attachment' != $post_type) ) { |
---|
2373 | if ( $wp_error ) |
---|
2374 | return new WP_Error('empty_content', __('Content, title, and excerpt are empty.')); |
---|
2375 | else |
---|
2376 | return 0; |
---|
2377 | } |
---|
2378 | |
---|
2379 | if ( empty($post_type) ) |
---|
2380 | $post_type = 'post'; |
---|
2381 | |
---|
2382 | if ( empty($post_status) ) |
---|
2383 | $post_status = 'draft'; |
---|
2384 | |
---|
2385 | if ( !empty($post_category) ) |
---|
2386 | $post_category = array_filter($post_category); // Filter out empty terms |
---|
2387 | |
---|
2388 | // Make sure we set a valid category. |
---|
2389 | if ( empty($post_category) || 0 == count($post_category) || !is_array($post_category) ) { |
---|
2390 | // 'post' requires at least one category. |
---|
2391 | if ( 'post' == $post_type && 'auto-draft' != $post_status ) |
---|
2392 | $post_category = array( get_option('default_category') ); |
---|
2393 | else |
---|
2394 | $post_category = array(); |
---|
2395 | } |
---|
2396 | |
---|
2397 | if ( empty($post_author) ) |
---|
2398 | $post_author = $user_ID; |
---|
2399 | |
---|
2400 | $post_ID = 0; |
---|
2401 | |
---|
2402 | // Get the post ID and GUID |
---|
2403 | if ( $update ) { |
---|
2404 | $post_ID = (int) $ID; |
---|
2405 | $guid = get_post_field( 'guid', $post_ID ); |
---|
2406 | $post_before = get_post($post_ID); |
---|
2407 | } |
---|
2408 | |
---|
2409 | // Don't allow contributors to set to set the post slug for pending review posts |
---|
2410 | if ( 'pending' == $post_status && !current_user_can( 'publish_posts' ) ) |
---|
2411 | $post_name = ''; |
---|
2412 | |
---|
2413 | // Create a valid post name. Drafts and pending posts are allowed to have an empty |
---|
2414 | // post name. |
---|
2415 | if ( empty($post_name) ) { |
---|
2416 | if ( !in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) |
---|
2417 | $post_name = sanitize_title($post_title); |
---|
2418 | else |
---|
2419 | $post_name = ''; |
---|
2420 | } else { |
---|
2421 | $post_name = sanitize_title($post_name); |
---|
2422 | } |
---|
2423 | |
---|
2424 | // 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 |
---|
2425 | if ( empty($post_date) || '0000-00-00 00:00:00' == $post_date ) |
---|
2426 | $post_date = current_time('mysql'); |
---|
2427 | |
---|
2428 | if ( empty($post_date_gmt) || '0000-00-00 00:00:00' == $post_date_gmt ) { |
---|
2429 | if ( !in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) |
---|
2430 | $post_date_gmt = get_gmt_from_date($post_date); |
---|
2431 | else |
---|
2432 | $post_date_gmt = '0000-00-00 00:00:00'; |
---|
2433 | } |
---|
2434 | |
---|
2435 | if ( $update || '0000-00-00 00:00:00' == $post_date ) { |
---|
2436 | $post_modified = current_time( 'mysql' ); |
---|
2437 | $post_modified_gmt = current_time( 'mysql', 1 ); |
---|
2438 | } else { |
---|
2439 | $post_modified = $post_date; |
---|
2440 | $post_modified_gmt = $post_date_gmt; |
---|
2441 | } |
---|
2442 | |
---|
2443 | if ( 'publish' == $post_status ) { |
---|
2444 | $now = gmdate('Y-m-d H:i:59'); |
---|
2445 | if ( mysql2date('U', $post_date_gmt, false) > mysql2date('U', $now, false) ) |
---|
2446 | $post_status = 'future'; |
---|
2447 | } elseif( 'future' == $post_status ) { |
---|
2448 | $now = gmdate('Y-m-d H:i:59'); |
---|
2449 | if ( mysql2date('U', $post_date_gmt, false) <= mysql2date('U', $now, false) ) |
---|
2450 | $post_status = 'publish'; |
---|
2451 | } |
---|
2452 | |
---|
2453 | if ( empty($comment_status) ) { |
---|
2454 | if ( $update ) |
---|
2455 | $comment_status = 'closed'; |
---|
2456 | else |
---|
2457 | $comment_status = get_option('default_comment_status'); |
---|
2458 | } |
---|
2459 | if ( empty($ping_status) ) |
---|
2460 | $ping_status = get_option('default_ping_status'); |
---|
2461 | |
---|
2462 | if ( isset($to_ping) ) |
---|
2463 | $to_ping = preg_replace('|\s+|', "\n", $to_ping); |
---|
2464 | else |
---|
2465 | $to_ping = ''; |
---|
2466 | |
---|
2467 | if ( ! isset($pinged) ) |
---|
2468 | $pinged = ''; |
---|
2469 | |
---|
2470 | if ( isset($post_parent) ) |
---|
2471 | $post_parent = (int) $post_parent; |
---|
2472 | else |
---|
2473 | $post_parent = 0; |
---|
2474 | |
---|
2475 | // Check the post_parent to see if it will cause a hierarchy loop |
---|
2476 | $post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, compact( array_keys( $postarr ) ), $postarr ); |
---|
2477 | |
---|
2478 | if ( isset($menu_order) ) |
---|
2479 | $menu_order = (int) $menu_order; |
---|
2480 | else |
---|
2481 | $menu_order = 0; |
---|
2482 | |
---|
2483 | if ( !isset($post_password) || 'private' == $post_status ) |
---|
2484 | $post_password = ''; |
---|
2485 | |
---|
2486 | $post_name = wp_unique_post_slug($post_name, $post_ID, $post_status, $post_type, $post_parent); |
---|
2487 | |
---|
2488 | // expected_slashed (everything!) |
---|
2489 | $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' ) ); |
---|
2490 | $data = apply_filters('wp_insert_post_data', $data, $postarr); |
---|
2491 | $data = stripslashes_deep( $data ); |
---|
2492 | $where = array( 'ID' => $post_ID ); |
---|
2493 | |
---|
2494 | if ( $update ) { |
---|
2495 | do_action( 'pre_post_update', $post_ID ); |
---|
2496 | if ( false === $wpdb->update( $wpdb->posts, $data, $where ) ) { |
---|
2497 | if ( $wp_error ) |
---|
2498 | return new WP_Error('db_update_error', __('Could not update post in the database'), $wpdb->last_error); |
---|
2499 | else |
---|
2500 | return 0; |
---|
2501 | } |
---|
2502 | } else { |
---|
2503 | do_action( 'pre_post_insert', $data, $postarr ); |
---|
2504 | if ( isset($post_mime_type) ) |
---|
2505 | $data['post_mime_type'] = stripslashes( $post_mime_type ); // This isn't in the update |
---|
2506 | // If there is a suggested ID, use it if not already present |
---|
2507 | if ( !empty($import_id) ) { |
---|
2508 | $import_id = (int) $import_id; |
---|
2509 | if ( ! $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id) ) ) { |
---|
2510 | $data['ID'] = $import_id; |
---|
2511 | } |
---|
2512 | } |
---|
2513 | if ( false === $wpdb->insert( $wpdb->posts, $data ) ) { |
---|
2514 | if ( $wp_error ) |
---|
2515 | return new WP_Error('db_insert_error', __('Could not insert post into the database'), $wpdb->last_error); |
---|
2516 | else |
---|
2517 | return 0; |
---|
2518 | } |
---|
2519 | $post_ID = (int) $wpdb->insert_id; |
---|
2520 | |
---|
2521 | // use the newly generated $post_ID |
---|
2522 | $where = array( 'ID' => $post_ID ); |
---|
2523 | } |
---|
2524 | |
---|
2525 | if ( empty($data['post_name']) && !in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) { |
---|
2526 | $data['post_name'] = sanitize_title($data['post_title'], $post_ID); |
---|
2527 | $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where ); |
---|
2528 | } |
---|
2529 | |
---|
2530 | if ( is_object_in_taxonomy($post_type, 'category') ) |
---|
2531 | wp_set_post_categories( $post_ID, $post_category ); |
---|
2532 | |
---|
2533 | if ( isset( $tags_input ) && is_object_in_taxonomy($post_type, 'post_tag') ) |
---|
2534 | wp_set_post_tags( $post_ID, $tags_input ); |
---|
2535 | |
---|
2536 | // new-style support for all custom taxonomies |
---|
2537 | if ( !empty($tax_input) ) { |
---|
2538 | foreach ( $tax_input as $taxonomy => $tags ) { |
---|
2539 | $taxonomy_obj = get_taxonomy($taxonomy); |
---|
2540 | if ( is_array($tags) ) // array = hierarchical, string = non-hierarchical. |
---|
2541 | $tags = array_filter($tags); |
---|
2542 | if ( current_user_can($taxonomy_obj->cap->assign_terms) ) |
---|
2543 | wp_set_post_terms( $post_ID, $tags, $taxonomy ); |
---|
2544 | } |
---|
2545 | } |
---|
2546 | |
---|
2547 | $current_guid = get_post_field( 'guid', $post_ID ); |
---|
2548 | |
---|
2549 | if ( 'page' == $data['post_type'] ) |
---|
2550 | clean_page_cache($post_ID); |
---|
2551 | else |
---|
2552 | clean_post_cache($post_ID); |
---|
2553 | |
---|
2554 | // Set GUID |
---|
2555 | if ( !$update && '' == $current_guid ) |
---|
2556 | $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where ); |
---|
2557 | |
---|
2558 | $post = get_post($post_ID); |
---|
2559 | |
---|
2560 | if ( !empty($page_template) && 'page' == $data['post_type'] ) { |
---|
2561 | $post->page_template = $page_template; |
---|
2562 | $page_templates = get_page_templates(); |
---|
2563 | if ( 'default' != $page_template && !in_array($page_template, $page_templates) ) { |
---|
2564 | if ( $wp_error ) |
---|
2565 | return new WP_Error('invalid_page_template', __('The page template is invalid.')); |
---|
2566 | else |
---|
2567 | return 0; |
---|
2568 | } |
---|
2569 | update_post_meta($post_ID, '_wp_page_template', $page_template); |
---|
2570 | } |
---|
2571 | |
---|
2572 | wp_transition_post_status($data['post_status'], $previous_status, $post); |
---|
2573 | |
---|
2574 | if ( $update ) { |
---|
2575 | do_action('edit_post', $post_ID, $post); |
---|
2576 | $post_after = get_post($post_ID); |
---|
2577 | do_action( 'post_updated', $post_ID, $post_after, $post_before); |
---|
2578 | } |
---|
2579 | |
---|
2580 | do_action('save_post', $post_ID, $post); |
---|
2581 | do_action('wp_insert_post', $post_ID, $post); |
---|
2582 | |
---|
2583 | return $post_ID; |
---|
2584 | } |
---|
2585 | |
---|
2586 | /** |
---|
2587 | * Update a post with new post data. |
---|
2588 | * |
---|
2589 | * The date does not have to be set for drafts. You can set the date and it will |
---|
2590 | * not be overridden. |
---|
2591 | * |
---|
2592 | * @since 1.0.0 |
---|
2593 | * |
---|
2594 | * @param array|object $postarr Post data. Arrays are expected to be escaped, objects are not. |
---|
2595 | * @return int 0 on failure, Post ID on success. |
---|
2596 | */ |
---|
2597 | function wp_update_post($postarr = array()) { |
---|
2598 | if ( is_object($postarr) ) { |
---|
2599 | // non-escaped post was passed |
---|
2600 | $postarr = get_object_vars($postarr); |
---|
2601 | $postarr = add_magic_quotes($postarr); |
---|
2602 | } |
---|
2603 | |
---|
2604 | // First, get all of the original fields |
---|
2605 | $post = wp_get_single_post($postarr['ID'], ARRAY_A); |
---|
2606 | |
---|
2607 | // Escape data pulled from DB. |
---|
2608 | $post = add_magic_quotes($post); |
---|
2609 | |
---|
2610 | // Passed post category list overwrites existing category list if not empty. |
---|
2611 | if ( isset($postarr['post_category']) && is_array($postarr['post_category']) |
---|
2612 | && 0 != count($postarr['post_category']) ) |
---|
2613 | $post_cats = $postarr['post_category']; |
---|
2614 | else |
---|
2615 | $post_cats = $post['post_category']; |
---|
2616 | |
---|
2617 | // Drafts shouldn't be assigned a date unless explicitly done so by the user |
---|
2618 | if ( isset( $post['post_status'] ) && in_array($post['post_status'], array('draft', 'pending', 'auto-draft')) && empty($postarr['edit_date']) && |
---|
2619 | ('0000-00-00 00:00:00' == $post['post_date_gmt']) ) |
---|
2620 | $clear_date = true; |
---|
2621 | else |
---|
2622 | $clear_date = false; |
---|
2623 | |
---|
2624 | // Merge old and new fields with new fields overwriting old ones. |
---|
2625 | $postarr = array_merge($post, $postarr); |
---|
2626 | $postarr['post_category'] = $post_cats; |
---|
2627 | if ( $clear_date ) { |
---|
2628 | $postarr['post_date'] = current_time('mysql'); |
---|
2629 | $postarr['post_date_gmt'] = ''; |
---|
2630 | } |
---|
2631 | |
---|
2632 | if ($postarr['post_type'] == 'attachment') |
---|
2633 | return wp_insert_attachment($postarr); |
---|
2634 | |
---|
2635 | return wp_insert_post($postarr); |
---|
2636 | } |
---|
2637 | |
---|
2638 | /** |
---|
2639 | * Publish a post by transitioning the post status. |
---|
2640 | * |
---|
2641 | * @since 2.1.0 |
---|
2642 | * @uses $wpdb |
---|
2643 | * @uses do_action() Calls 'edit_post', 'save_post', and 'wp_insert_post' on post_id and post data. |
---|
2644 | * |
---|
2645 | * @param int $post_id Post ID. |
---|
2646 | * @return null |
---|
2647 | */ |
---|
2648 | function wp_publish_post($post_id) { |
---|
2649 | global $wpdb; |
---|
2650 | |
---|
2651 | $post = get_post($post_id); |
---|
2652 | |
---|
2653 | if ( empty($post) ) |
---|
2654 | return; |
---|
2655 | |
---|
2656 | if ( 'publish' == $post->post_status ) |
---|
2657 | return; |
---|
2658 | |
---|
2659 | $wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post_id ) ); |
---|
2660 | |
---|
2661 | $old_status = $post->post_status; |
---|
2662 | $post->post_status = 'publish'; |
---|
2663 | wp_transition_post_status('publish', $old_status, $post); |
---|
2664 | |
---|
2665 | // Update counts for the post's terms. |
---|
2666 | foreach ( (array) get_object_taxonomies('post') as $taxonomy ) { |
---|
2667 | $tt_ids = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'tt_ids')); |
---|
2668 | wp_update_term_count($tt_ids, $taxonomy); |
---|
2669 | } |
---|
2670 | |
---|
2671 | do_action('edit_post', $post_id, $post); |
---|
2672 | do_action('save_post', $post_id, $post); |
---|
2673 | do_action('wp_insert_post', $post_id, $post); |
---|
2674 | } |
---|
2675 | |
---|
2676 | /** |
---|
2677 | * Publish future post and make sure post ID has future post status. |
---|
2678 | * |
---|
2679 | * Invoked by cron 'publish_future_post' event. This safeguard prevents cron |
---|
2680 | * from publishing drafts, etc. |
---|
2681 | * |
---|
2682 | * @since 2.5.0 |
---|
2683 | * |
---|
2684 | * @param int $post_id Post ID. |
---|
2685 | * @return null Nothing is returned. Which can mean that no action is required or post was published. |
---|
2686 | */ |
---|
2687 | function check_and_publish_future_post($post_id) { |
---|
2688 | |
---|
2689 | $post = get_post($post_id); |
---|
2690 | |
---|
2691 | if ( empty($post) ) |
---|
2692 | return; |
---|
2693 | |
---|
2694 | if ( 'future' != $post->post_status ) |
---|
2695 | return; |
---|
2696 | |
---|
2697 | $time = strtotime( $post->post_date_gmt . ' GMT' ); |
---|
2698 | |
---|
2699 | if ( $time > time() ) { // Uh oh, someone jumped the gun! |
---|
2700 | wp_clear_scheduled_hook( 'publish_future_post', array( $post_id ) ); // clear anything else in the system |
---|
2701 | wp_schedule_single_event( $time, 'publish_future_post', array( $post_id ) ); |
---|
2702 | return; |
---|
2703 | } |
---|
2704 | |
---|
2705 | return wp_publish_post($post_id); |
---|
2706 | } |
---|
2707 | |
---|
2708 | |
---|
2709 | /** |
---|
2710 | * Computes a unique slug for the post, when given the desired slug and some post details. |
---|
2711 | * |
---|
2712 | * @global wpdb $wpdb |
---|
2713 | * @global WP_Rewrite $wp_rewrite |
---|
2714 | * @param string $slug the desired slug (post_name) |
---|
2715 | * @param integer $post_ID |
---|
2716 | * @param string $post_status no uniqueness checks are made if the post is still draft or pending |
---|
2717 | * @param string $post_type |
---|
2718 | * @param integer $post_parent |
---|
2719 | * @return string unique slug for the post, based on $post_name (with a -1, -2, etc. suffix) |
---|
2720 | */ |
---|
2721 | function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent ) { |
---|
2722 | if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) |
---|
2723 | return $slug; |
---|
2724 | |
---|
2725 | global $wpdb, $wp_rewrite; |
---|
2726 | |
---|
2727 | $feeds = $wp_rewrite->feeds; |
---|
2728 | if ( ! is_array( $feeds ) ) |
---|
2729 | $feeds = array(); |
---|
2730 | |
---|
2731 | $hierarchical_post_types = get_post_types( array('hierarchical' => true) ); |
---|
2732 | if ( 'attachment' == $post_type ) { |
---|
2733 | // Attachment slugs must be unique across all types. |
---|
2734 | $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1"; |
---|
2735 | $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID ) ); |
---|
2736 | |
---|
2737 | if ( $post_name_check || in_array( $slug, $feeds ) ) { |
---|
2738 | $suffix = 2; |
---|
2739 | do { |
---|
2740 | $alt_post_name = substr ($slug, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; |
---|
2741 | $post_name_check = $wpdb->get_var( $wpdb->prepare($check_sql, $alt_post_name, $post_ID ) ); |
---|
2742 | $suffix++; |
---|
2743 | } while ( $post_name_check ); |
---|
2744 | $slug = $alt_post_name; |
---|
2745 | } |
---|
2746 | } elseif ( in_array( $post_type, $hierarchical_post_types ) ) { |
---|
2747 | // Page slugs must be unique within their own trees. Pages are in a separate |
---|
2748 | // namespace than posts so page slugs are allowed to overlap post slugs. |
---|
2749 | $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( '" . implode( "', '", esc_sql( $hierarchical_post_types ) ) . "' ) AND ID != %d AND post_parent = %d LIMIT 1"; |
---|
2750 | $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID, $post_parent ) ); |
---|
2751 | |
---|
2752 | if ( $post_name_check || in_array( $slug, $feeds ) || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug ) ) { |
---|
2753 | $suffix = 2; |
---|
2754 | do { |
---|
2755 | $alt_post_name = substr( $slug, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; |
---|
2756 | $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_ID, $post_parent ) ); |
---|
2757 | $suffix++; |
---|
2758 | } while ( $post_name_check ); |
---|
2759 | $slug = $alt_post_name; |
---|
2760 | } |
---|
2761 | } else { |
---|
2762 | // Post slugs must be unique across all posts. |
---|
2763 | $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1"; |
---|
2764 | $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) ); |
---|
2765 | |
---|
2766 | if ( $post_name_check || in_array( $slug, $feeds ) ) { |
---|
2767 | $suffix = 2; |
---|
2768 | do { |
---|
2769 | $alt_post_name = substr( $slug, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; |
---|
2770 | $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID ) ); |
---|
2771 | $suffix++; |
---|
2772 | } while ( $post_name_check ); |
---|
2773 | $slug = $alt_post_name; |
---|
2774 | } |
---|
2775 | } |
---|
2776 | |
---|
2777 | return $slug; |
---|
2778 | } |
---|
2779 | |
---|
2780 | /** |
---|
2781 | * Adds tags to a post. |
---|
2782 | * |
---|
2783 | * @uses wp_set_post_tags() Same first two parameters, but the last parameter is always set to true. |
---|
2784 | * |
---|
2785 | * @package WordPress |
---|
2786 | * @subpackage Post |
---|
2787 | * @since 2.3.0 |
---|
2788 | * |
---|
2789 | * @param int $post_id Post ID |
---|
2790 | * @param string $tags The tags to set for the post, separated by commas. |
---|
2791 | * @return bool|null Will return false if $post_id is not an integer or is 0. Will return null otherwise |
---|
2792 | */ |
---|
2793 | function wp_add_post_tags($post_id = 0, $tags = '') { |
---|
2794 | return wp_set_post_tags($post_id, $tags, true); |
---|
2795 | } |
---|
2796 | |
---|
2797 | |
---|
2798 | /** |
---|
2799 | * Set the tags for a post. |
---|
2800 | * |
---|
2801 | * @since 2.3.0 |
---|
2802 | * @uses wp_set_object_terms() Sets the tags for the post. |
---|
2803 | * |
---|
2804 | * @param int $post_id Post ID. |
---|
2805 | * @param string $tags The tags to set for the post, separated by commas. |
---|
2806 | * @param bool $append If true, don't delete existing tags, just add on. If false, replace the tags with the new tags. |
---|
2807 | * @return mixed Array of affected term IDs. WP_Error or false on failure. |
---|
2808 | */ |
---|
2809 | function wp_set_post_tags( $post_id = 0, $tags = '', $append = false ) { |
---|
2810 | return wp_set_post_terms( $post_id, $tags, 'post_tag', $append); |
---|
2811 | } |
---|
2812 | |
---|
2813 | /** |
---|
2814 | * Set the terms for a post. |
---|
2815 | * |
---|
2816 | * @since 2.8.0 |
---|
2817 | * @uses wp_set_object_terms() Sets the tags for the post. |
---|
2818 | * |
---|
2819 | * @param int $post_id Post ID. |
---|
2820 | * @param string $tags The tags to set for the post, separated by commas. |
---|
2821 | * @param bool $append If true, don't delete existing tags, just add on. If false, replace the tags with the new tags. |
---|
2822 | * @return mixed Array of affected term IDs. WP_Error or false on failure. |
---|
2823 | */ |
---|
2824 | function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false ) { |
---|
2825 | $post_id = (int) $post_id; |
---|
2826 | |
---|
2827 | if ( !$post_id ) |
---|
2828 | return false; |
---|
2829 | |
---|
2830 | if ( empty($tags) ) |
---|
2831 | $tags = array(); |
---|
2832 | |
---|
2833 | $tags = is_array($tags) ? $tags : explode( ',', trim($tags, " \n\t\r\0\x0B,") ); |
---|
2834 | |
---|
2835 | // Hierarchical taxonomies must always pass IDs rather than names so that children with the same |
---|
2836 | // names but different parents aren't confused. |
---|
2837 | if ( is_taxonomy_hierarchical( $taxonomy ) ) { |
---|
2838 | $tags = array_map( 'intval', $tags ); |
---|
2839 | $tags = array_unique( $tags ); |
---|
2840 | } |
---|
2841 | |
---|
2842 | return wp_set_object_terms($post_id, $tags, $taxonomy, $append); |
---|
2843 | } |
---|
2844 | |
---|
2845 | /** |
---|
2846 | * Set categories for a post. |
---|
2847 | * |
---|
2848 | * If the post categories parameter is not set, then the default category is |
---|
2849 | * going used. |
---|
2850 | * |
---|
2851 | * @since 2.1.0 |
---|
2852 | * |
---|
2853 | * @param int $post_ID Post ID. |
---|
2854 | * @param array $post_categories Optional. List of categories. |
---|
2855 | * @return bool|mixed |
---|
2856 | */ |
---|
2857 | function wp_set_post_categories($post_ID = 0, $post_categories = array()) { |
---|
2858 | $post_ID = (int) $post_ID; |
---|
2859 | $post_type = get_post_type( $post_ID ); |
---|
2860 | $post_status = get_post_status( $post_ID ); |
---|
2861 | // If $post_categories isn't already an array, make it one: |
---|
2862 | if ( !is_array($post_categories) || empty($post_categories) ) { |
---|
2863 | if ( 'post' == $post_type && 'auto-draft' != $post_status ) |
---|
2864 | $post_categories = array( get_option('default_category') ); |
---|
2865 | else |
---|
2866 | $post_categories = array(); |
---|
2867 | } else if ( 1 == count($post_categories) && '' == reset($post_categories) ) { |
---|
2868 | return true; |
---|
2869 | } |
---|
2870 | |
---|
2871 | if ( !empty($post_categories) ) { |
---|
2872 | $post_categories = array_map('intval', $post_categories); |
---|
2873 | $post_categories = array_unique($post_categories); |
---|
2874 | } |
---|
2875 | |
---|
2876 | return wp_set_object_terms($post_ID, $post_categories, 'category'); |
---|
2877 | } |
---|
2878 | |
---|
2879 | /** |
---|
2880 | * Transition the post status of a post. |
---|
2881 | * |
---|
2882 | * Calls hooks to transition post status. |
---|
2883 | * |
---|
2884 | * The first is 'transition_post_status' with new status, old status, and post data. |
---|
2885 | * |
---|
2886 | * The next action called is 'OLDSTATUS_to_NEWSTATUS' the 'NEWSTATUS' is the |
---|
2887 | * $new_status parameter and the 'OLDSTATUS' is $old_status parameter; it has the |
---|
2888 | * post data. |
---|
2889 | * |
---|
2890 | * The final action is named 'NEWSTATUS_POSTTYPE', 'NEWSTATUS' is from the $new_status |
---|
2891 | * parameter and POSTTYPE is post_type post data. |
---|
2892 | * |
---|
2893 | * @since 2.3.0 |
---|
2894 | * @link http://codex.wordpress.org/Post_Status_Transitions |
---|
2895 | * |
---|
2896 | * @uses do_action() Calls 'transition_post_status' on $new_status, $old_status and |
---|
2897 | * $post if there is a status change. |
---|
2898 | * @uses do_action() Calls '${old_status}_to_$new_status' on $post if there is a status change. |
---|
2899 | * @uses do_action() Calls '${new_status}_$post->post_type' on post ID and $post. |
---|
2900 | * |
---|
2901 | * @param string $new_status Transition to this post status. |
---|
2902 | * @param string $old_status Previous post status. |
---|
2903 | * @param object $post Post data. |
---|
2904 | */ |
---|
2905 | function wp_transition_post_status($new_status, $old_status, $post) { |
---|
2906 | do_action('transition_post_status', $new_status, $old_status, $post); |
---|
2907 | do_action("${old_status}_to_$new_status", $post); |
---|
2908 | do_action("${new_status}_$post->post_type", $post->ID, $post); |
---|
2909 | } |
---|
2910 | |
---|
2911 | // |
---|
2912 | // Trackback and ping functions |
---|
2913 | // |
---|
2914 | |
---|
2915 | /** |
---|
2916 | * Add a URL to those already pung. |
---|
2917 | * |
---|
2918 | * @since 1.5.0 |
---|
2919 | * @uses $wpdb |
---|
2920 | * |
---|
2921 | * @param int $post_id Post ID. |
---|
2922 | * @param string $uri Ping URI. |
---|
2923 | * @return int How many rows were updated. |
---|
2924 | */ |
---|
2925 | function add_ping($post_id, $uri) { |
---|
2926 | global $wpdb; |
---|
2927 | $pung = $wpdb->get_var( $wpdb->prepare( "SELECT pinged FROM $wpdb->posts WHERE ID = %d", $post_id )); |
---|
2928 | $pung = trim($pung); |
---|
2929 | $pung = preg_split('/\s/', $pung); |
---|
2930 | $pung[] = $uri; |
---|
2931 | $new = implode("\n", $pung); |
---|
2932 | $new = apply_filters('add_ping', $new); |
---|
2933 | // expected_slashed ($new) |
---|
2934 | $new = stripslashes($new); |
---|
2935 | return $wpdb->update( $wpdb->posts, array( 'pinged' => $new ), array( 'ID' => $post_id ) ); |
---|
2936 | } |
---|
2937 | |
---|
2938 | /** |
---|
2939 | * Retrieve enclosures already enclosed for a post. |
---|
2940 | * |
---|
2941 | * @since 1.5.0 |
---|
2942 | * @uses $wpdb |
---|
2943 | * |
---|
2944 | * @param int $post_id Post ID. |
---|
2945 | * @return array List of enclosures |
---|
2946 | */ |
---|
2947 | function get_enclosed($post_id) { |
---|
2948 | $custom_fields = get_post_custom( $post_id ); |
---|
2949 | $pung = array(); |
---|
2950 | if ( !is_array( $custom_fields ) ) |
---|
2951 | return $pung; |
---|
2952 | |
---|
2953 | foreach ( $custom_fields as $key => $val ) { |
---|
2954 | if ( 'enclosure' != $key || !is_array( $val ) ) |
---|
2955 | continue; |
---|
2956 | foreach( $val as $enc ) { |
---|
2957 | $enclosure = split( "\n", $enc ); |
---|
2958 | $pung[] = trim( $enclosure[ 0 ] ); |
---|
2959 | } |
---|
2960 | } |
---|
2961 | $pung = apply_filters('get_enclosed', $pung); |
---|
2962 | return $pung; |
---|
2963 | } |
---|
2964 | |
---|
2965 | /** |
---|
2966 | * Retrieve URLs already pinged for a post. |
---|
2967 | * |
---|
2968 | * @since 1.5.0 |
---|
2969 | * @uses $wpdb |
---|
2970 | * |
---|
2971 | * @param int $post_id Post ID. |
---|
2972 | * @return array |
---|
2973 | */ |
---|
2974 | function get_pung($post_id) { |
---|
2975 | global $wpdb; |
---|
2976 | $pung = $wpdb->get_var( $wpdb->prepare( "SELECT pinged FROM $wpdb->posts WHERE ID = %d", $post_id )); |
---|
2977 | $pung = trim($pung); |
---|
2978 | $pung = preg_split('/\s/', $pung); |
---|
2979 | $pung = apply_filters('get_pung', $pung); |
---|
2980 | return $pung; |
---|
2981 | } |
---|
2982 | |
---|
2983 | /** |
---|
2984 | * Retrieve URLs that need to be pinged. |
---|
2985 | * |
---|
2986 | * @since 1.5.0 |
---|
2987 | * @uses $wpdb |
---|
2988 | * |
---|
2989 | * @param int $post_id Post ID |
---|
2990 | * @return array |
---|
2991 | */ |
---|
2992 | function get_to_ping($post_id) { |
---|
2993 | global $wpdb; |
---|
2994 | $to_ping = $wpdb->get_var( $wpdb->prepare( "SELECT to_ping FROM $wpdb->posts WHERE ID = %d", $post_id )); |
---|
2995 | $to_ping = trim($to_ping); |
---|
2996 | $to_ping = preg_split('/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY); |
---|
2997 | $to_ping = apply_filters('get_to_ping', $to_ping); |
---|
2998 | return $to_ping; |
---|
2999 | } |
---|
3000 | |
---|
3001 | /** |
---|
3002 | * Do trackbacks for a list of URLs. |
---|
3003 | * |
---|
3004 | * @since 1.0.0 |
---|
3005 | * |
---|
3006 | * @param string $tb_list Comma separated list of URLs |
---|
3007 | * @param int $post_id Post ID |
---|
3008 | */ |
---|
3009 | function trackback_url_list($tb_list, $post_id) { |
---|
3010 | if ( ! empty( $tb_list ) ) { |
---|
3011 | // get post data |
---|
3012 | $postdata = wp_get_single_post($post_id, ARRAY_A); |
---|
3013 | |
---|
3014 | // import postdata as variables |
---|
3015 | extract($postdata, EXTR_SKIP); |
---|
3016 | |
---|
3017 | // form an excerpt |
---|
3018 | $excerpt = strip_tags($post_excerpt ? $post_excerpt : $post_content); |
---|
3019 | |
---|
3020 | if (strlen($excerpt) > 255) { |
---|
3021 | $excerpt = substr($excerpt,0,252) . '...'; |
---|
3022 | } |
---|
3023 | |
---|
3024 | $trackback_urls = explode(',', $tb_list); |
---|
3025 | foreach( (array) $trackback_urls as $tb_url) { |
---|
3026 | $tb_url = trim($tb_url); |
---|
3027 | trackback($tb_url, stripslashes($post_title), $excerpt, $post_id); |
---|
3028 | } |
---|
3029 | } |
---|
3030 | } |
---|
3031 | |
---|
3032 | // |
---|
3033 | // Page functions |
---|
3034 | // |
---|
3035 | |
---|
3036 | /** |
---|
3037 | * Get a list of page IDs. |
---|
3038 | * |
---|
3039 | * @since 2.0.0 |
---|
3040 | * @uses $wpdb |
---|
3041 | * |
---|
3042 | * @return array List of page IDs. |
---|
3043 | */ |
---|
3044 | function get_all_page_ids() { |
---|
3045 | global $wpdb; |
---|
3046 | |
---|
3047 | if ( ! $page_ids = wp_cache_get('all_page_ids', 'posts') ) { |
---|
3048 | $page_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_type = 'page'"); |
---|
3049 | wp_cache_add('all_page_ids', $page_ids, 'posts'); |
---|
3050 | } |
---|
3051 | |
---|
3052 | return $page_ids; |
---|
3053 | } |
---|
3054 | |
---|
3055 | /** |
---|
3056 | * Retrieves page data given a page ID or page object. |
---|
3057 | * |
---|
3058 | * @since 1.5.1 |
---|
3059 | * |
---|
3060 | * @param mixed $page Page object or page ID. Passed by reference. |
---|
3061 | * @param string $output What to output. OBJECT, ARRAY_A, or ARRAY_N. |
---|
3062 | * @param string $filter How the return value should be filtered. |
---|
3063 | * @return mixed Page data. |
---|
3064 | */ |
---|
3065 | function &get_page(&$page, $output = OBJECT, $filter = 'raw') { |
---|
3066 | $p = get_post($page, $output, $filter); |
---|
3067 | return $p; |
---|
3068 | } |
---|
3069 | |
---|
3070 | /** |
---|
3071 | * Retrieves a page given its path. |
---|
3072 | * |
---|
3073 | * @since 2.1.0 |
---|
3074 | * @uses $wpdb |
---|
3075 | * |
---|
3076 | * @param string $page_path Page path |
---|
3077 | * @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A. Default OBJECT. |
---|
3078 | * @param string $post_type Optional. Post type. Default page. |
---|
3079 | * @return mixed Null when complete. |
---|
3080 | */ |
---|
3081 | function get_page_by_path($page_path, $output = OBJECT, $post_type = 'page') { |
---|
3082 | global $wpdb; |
---|
3083 | $page_path = rawurlencode(urldecode($page_path)); |
---|
3084 | $page_path = str_replace('%2F', '/', $page_path); |
---|
3085 | $page_path = str_replace('%20', ' ', $page_path); |
---|
3086 | $page_paths = '/' . trim($page_path, '/'); |
---|
3087 | $leaf_path = sanitize_title(basename($page_paths)); |
---|
3088 | $page_paths = explode('/', $page_paths); |
---|
3089 | $full_path = ''; |
---|
3090 | foreach ( (array) $page_paths as $pathdir ) |
---|
3091 | $full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title($pathdir); |
---|
3092 | |
---|
3093 | $pages = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_name = %s AND (post_type = %s OR post_type = 'attachment')", $leaf_path, $post_type )); |
---|
3094 | |
---|
3095 | if ( empty($pages) ) |
---|
3096 | return null; |
---|
3097 | |
---|
3098 | foreach ( $pages as $page ) { |
---|
3099 | $path = '/' . $leaf_path; |
---|
3100 | $curpage = $page; |
---|
3101 | while ( $curpage->post_parent != 0 ) { |
---|
3102 | $curpage = $wpdb->get_row( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE ID = %d and post_type = %s", $curpage->post_parent, $post_type )); |
---|
3103 | $path = '/' . $curpage->post_name . $path; |
---|
3104 | } |
---|
3105 | |
---|
3106 | if ( $path == $full_path ) |
---|
3107 | return get_page($page->ID, $output, $post_type); |
---|
3108 | } |
---|
3109 | |
---|
3110 | return null; |
---|
3111 | } |
---|
3112 | |
---|
3113 | /** |
---|
3114 | * Retrieve a page given its title. |
---|
3115 | * |
---|
3116 | * @since 2.1.0 |
---|
3117 | * @uses $wpdb |
---|
3118 | * |
---|
3119 | * @param string $page_title Page title |
---|
3120 | * @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A. Default OBJECT. |
---|
3121 | * @param string $post_type Optional. Post type. Default page. |
---|
3122 | * @return mixed |
---|
3123 | */ |
---|
3124 | function get_page_by_title($page_title, $output = OBJECT, $post_type = 'page' ) { |
---|
3125 | global $wpdb; |
---|
3126 | $page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type= %s", $page_title, $post_type ) ); |
---|
3127 | if ( $page ) |
---|
3128 | return get_page($page, $output); |
---|
3129 | |
---|
3130 | return null; |
---|
3131 | } |
---|
3132 | |
---|
3133 | /** |
---|
3134 | * Retrieve child pages from list of pages matching page ID. |
---|
3135 | * |
---|
3136 | * Matches against the pages parameter against the page ID. Also matches all |
---|
3137 | * children for the same to retrieve all children of a page. Does not make any |
---|
3138 | * SQL queries to get the children. |
---|
3139 | * |
---|
3140 | * @since 1.5.1 |
---|
3141 | * |
---|
3142 | * @param int $page_id Page ID. |
---|
3143 | * @param array $pages List of pages' objects. |
---|
3144 | * @return array |
---|
3145 | */ |
---|
3146 | function &get_page_children($page_id, $pages) { |
---|
3147 | $page_list = array(); |
---|
3148 | foreach ( (array) $pages as $page ) { |
---|
3149 | if ( $page->post_parent == $page_id ) { |
---|
3150 | $page_list[] = $page; |
---|
3151 | if ( $children = get_page_children($page->ID, $pages) ) |
---|
3152 | $page_list = array_merge($page_list, $children); |
---|
3153 | } |
---|
3154 | } |
---|
3155 | return $page_list; |
---|
3156 | } |
---|
3157 | |
---|
3158 | /** |
---|
3159 | * Order the pages with children under parents in a flat list. |
---|
3160 | * |
---|
3161 | * It uses auxiliary structure to hold parent-children relationships and |
---|
3162 | * runs in O(N) complexity |
---|
3163 | * |
---|
3164 | * @since 2.0.0 |
---|
3165 | * |
---|
3166 | * @param array $pages Posts array. |
---|
3167 | * @param int $page_id Parent page ID. |
---|
3168 | * @return array A list arranged by hierarchy. Children immediately follow their parents. |
---|
3169 | */ |
---|
3170 | function &get_page_hierarchy( &$pages, $page_id = 0 ) { |
---|
3171 | if ( empty( $pages ) ) { |
---|
3172 | $result = array(); |
---|
3173 | return $result; |
---|
3174 | } |
---|
3175 | |
---|
3176 | $children = array(); |
---|
3177 | foreach ( (array) $pages as $p ) { |
---|
3178 | $parent_id = intval( $p->post_parent ); |
---|
3179 | $children[ $parent_id ][] = $p; |
---|
3180 | } |
---|
3181 | |
---|
3182 | $result = array(); |
---|
3183 | _page_traverse_name( $page_id, $children, $result ); |
---|
3184 | |
---|
3185 | return $result; |
---|
3186 | } |
---|
3187 | |
---|
3188 | /** |
---|
3189 | * function to traverse and return all the nested children post names of a root page. |
---|
3190 | * $children contains parent-chilren relations |
---|
3191 | * |
---|
3192 | */ |
---|
3193 | function _page_traverse_name( $page_id, &$children, &$result ){ |
---|
3194 | if ( isset( $children[ $page_id ] ) ){ |
---|
3195 | foreach( (array)$children[ $page_id ] as $child ) { |
---|
3196 | $result[ $child->ID ] = $child->post_name; |
---|
3197 | _page_traverse_name( $child->ID, $children, $result ); |
---|
3198 | } |
---|
3199 | } |
---|
3200 | } |
---|
3201 | |
---|
3202 | /** |
---|
3203 | * Builds URI for a page. |
---|
3204 | * |
---|
3205 | * Sub pages will be in the "directory" under the parent page post name. |
---|
3206 | * |
---|
3207 | * @since 1.5.0 |
---|
3208 | * |
---|
3209 | * @param mixed $page Page object or page ID. |
---|
3210 | * @return string Page URI. |
---|
3211 | */ |
---|
3212 | function get_page_uri($page) { |
---|
3213 | if ( ! is_object($page) ) |
---|
3214 | $page = get_page($page); |
---|
3215 | $uri = $page->post_name; |
---|
3216 | |
---|
3217 | // A page cannot be it's own parent. |
---|
3218 | if ( $page->post_parent == $page->ID ) |
---|
3219 | return $uri; |
---|
3220 | |
---|
3221 | while ($page->post_parent != 0) { |
---|
3222 | $page = get_page($page->post_parent); |
---|
3223 | $uri = $page->post_name . "/" . $uri; |
---|
3224 | } |
---|
3225 | |
---|
3226 | return $uri; |
---|
3227 | } |
---|
3228 | |
---|
3229 | /** |
---|
3230 | * Retrieve a list of pages. |
---|
3231 | * |
---|
3232 | * The defaults that can be overridden are the following: 'child_of', |
---|
3233 | * 'sort_order', 'sort_column', 'post_title', 'hierarchical', 'exclude', |
---|
3234 | * 'include', 'meta_key', 'meta_value','authors', 'number', and 'offset'. |
---|
3235 | * |
---|
3236 | * @since 1.5.0 |
---|
3237 | * @uses $wpdb |
---|
3238 | * |
---|
3239 | * @param mixed $args Optional. Array or string of options that overrides defaults. |
---|
3240 | * @return array List of pages matching defaults or $args |
---|
3241 | */ |
---|
3242 | function &get_pages($args = '') { |
---|
3243 | global $wpdb; |
---|
3244 | |
---|
3245 | $defaults = array( |
---|
3246 | 'child_of' => 0, 'sort_order' => 'ASC', |
---|
3247 | 'sort_column' => 'post_title', 'hierarchical' => 1, |
---|
3248 | 'exclude' => array(), 'include' => array(), |
---|
3249 | 'meta_key' => '', 'meta_value' => '', |
---|
3250 | 'authors' => '', 'parent' => -1, 'exclude_tree' => '', |
---|
3251 | 'number' => '', 'offset' => 0, |
---|
3252 | 'post_type' => 'page', 'post_status' => 'publish', |
---|
3253 | ); |
---|
3254 | |
---|
3255 | $r = wp_parse_args( $args, $defaults ); |
---|
3256 | extract( $r, EXTR_SKIP ); |
---|
3257 | $number = (int) $number; |
---|
3258 | $offset = (int) $offset; |
---|
3259 | |
---|
3260 | // Make sure the post type is hierarchical |
---|
3261 | $hierarchical_post_types = get_post_types( array( 'hierarchical' => true ) ); |
---|
3262 | if ( !in_array( $post_type, $hierarchical_post_types ) ) |
---|
3263 | return false; |
---|
3264 | |
---|
3265 | // Make sure we have a valid post status |
---|
3266 | if ( !in_array($post_status, get_post_stati()) ) |
---|
3267 | return false; |
---|
3268 | |
---|
3269 | $cache = array(); |
---|
3270 | $key = md5( serialize( compact(array_keys($defaults)) ) ); |
---|
3271 | if ( $cache = wp_cache_get( 'get_pages', 'posts' ) ) { |
---|
3272 | if ( is_array($cache) && isset( $cache[ $key ] ) ) { |
---|
3273 | $pages = apply_filters('get_pages', $cache[ $key ], $r ); |
---|
3274 | return $pages; |
---|
3275 | } |
---|
3276 | } |
---|
3277 | |
---|
3278 | if ( !is_array($cache) ) |
---|
3279 | $cache = array(); |
---|
3280 | |
---|
3281 | $inclusions = ''; |
---|
3282 | if ( !empty($include) ) { |
---|
3283 | $child_of = 0; //ignore child_of, parent, exclude, meta_key, and meta_value params if using include |
---|
3284 | $parent = -1; |
---|
3285 | $exclude = ''; |
---|
3286 | $meta_key = ''; |
---|
3287 | $meta_value = ''; |
---|
3288 | $hierarchical = false; |
---|
3289 | $incpages = wp_parse_id_list( $include ); |
---|
3290 | if ( ! empty( $incpages ) ) { |
---|
3291 | foreach ( $incpages as $incpage ) { |
---|
3292 | if (empty($inclusions)) |
---|
3293 | $inclusions = $wpdb->prepare(' AND ( ID = %d ', $incpage); |
---|
3294 | else |
---|
3295 | $inclusions .= $wpdb->prepare(' OR ID = %d ', $incpage); |
---|
3296 | } |
---|
3297 | } |
---|
3298 | } |
---|
3299 | if (!empty($inclusions)) |
---|
3300 | $inclusions .= ')'; |
---|
3301 | |
---|
3302 | $exclusions = ''; |
---|
3303 | if ( !empty($exclude) ) { |
---|
3304 | $expages = wp_parse_id_list( $exclude ); |
---|
3305 | if ( ! empty( $expages ) ) { |
---|
3306 | foreach ( $expages as $expage ) { |
---|
3307 | if (empty($exclusions)) |
---|
3308 | $exclusions = $wpdb->prepare(' AND ( ID <> %d ', $expage); |
---|
3309 | else |
---|
3310 | $exclusions .= $wpdb->prepare(' AND ID <> %d ', $expage); |
---|
3311 | } |
---|
3312 | } |
---|
3313 | } |
---|
3314 | if (!empty($exclusions)) |
---|
3315 | $exclusions .= ')'; |
---|
3316 | |
---|
3317 | $author_query = ''; |
---|
3318 | if (!empty($authors)) { |
---|
3319 | $post_authors = preg_split('/[\s,]+/',$authors); |
---|
3320 | |
---|
3321 | if ( ! empty( $post_authors ) ) { |
---|
3322 | foreach ( $post_authors as $post_author ) { |
---|
3323 | //Do we have an author id or an author login? |
---|
3324 | if ( 0 == intval($post_author) ) { |
---|
3325 | $post_author = get_userdatabylogin($post_author); |
---|
3326 | if ( empty($post_author) ) |
---|
3327 | continue; |
---|
3328 | if ( empty($post_author->ID) ) |
---|
3329 | continue; |
---|
3330 | $post_author = $post_author->ID; |
---|
3331 | } |
---|
3332 | |
---|
3333 | if ( '' == $author_query ) |
---|
3334 | $author_query = $wpdb->prepare(' post_author = %d ', $post_author); |
---|
3335 | else |
---|
3336 | $author_query .= $wpdb->prepare(' OR post_author = %d ', $post_author); |
---|
3337 | } |
---|
3338 | if ( '' != $author_query ) |
---|
3339 | $author_query = " AND ($author_query)"; |
---|
3340 | } |
---|
3341 | } |
---|
3342 | |
---|
3343 | $join = ''; |
---|
3344 | $where = "$exclusions $inclusions "; |
---|
3345 | if ( ! empty( $meta_key ) || ! empty( $meta_value ) ) { |
---|
3346 | $join = " LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )"; |
---|
3347 | |
---|
3348 | // meta_key and meta_value might be slashed |
---|
3349 | $meta_key = stripslashes($meta_key); |
---|
3350 | $meta_value = stripslashes($meta_value); |
---|
3351 | if ( ! empty( $meta_key ) ) |
---|
3352 | $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_key = %s", $meta_key); |
---|
3353 | if ( ! empty( $meta_value ) ) |
---|
3354 | $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_value = %s", $meta_value); |
---|
3355 | |
---|
3356 | } |
---|
3357 | |
---|
3358 | if ( $parent >= 0 ) |
---|
3359 | $where .= $wpdb->prepare(' AND post_parent = %d ', $parent); |
---|
3360 | |
---|
3361 | $where_post_type = $wpdb->prepare( "post_type = '%s' AND post_status = '%s'", $post_type, $post_status ); |
---|
3362 | |
---|
3363 | $query = "SELECT * FROM $wpdb->posts $join WHERE ($where_post_type) $where "; |
---|
3364 | $query .= $author_query; |
---|
3365 | $query .= " ORDER BY " . $sort_column . " " . $sort_order ; |
---|
3366 | |
---|
3367 | if ( !empty($number) ) |
---|
3368 | $query .= ' LIMIT ' . $offset . ',' . $number; |
---|
3369 | |
---|
3370 | $pages = $wpdb->get_results($query); |
---|
3371 | |
---|
3372 | if ( empty($pages) ) { |
---|
3373 | $pages = apply_filters('get_pages', array(), $r); |
---|
3374 | return $pages; |
---|
3375 | } |
---|
3376 | |
---|
3377 | // Sanitize before caching so it'll only get done once |
---|
3378 | $num_pages = count($pages); |
---|
3379 | for ($i = 0; $i < $num_pages; $i++) { |
---|
3380 | $pages[$i] = sanitize_post($pages[$i], 'raw'); |
---|
3381 | } |
---|
3382 | |
---|
3383 | // Update cache. |
---|
3384 | update_page_cache($pages); |
---|
3385 | |
---|
3386 | if ( $child_of || $hierarchical ) |
---|
3387 | $pages = & get_page_children($child_of, $pages); |
---|
3388 | |
---|
3389 | if ( !empty($exclude_tree) ) { |
---|
3390 | $exclude = (int) $exclude_tree; |
---|
3391 | $children = get_page_children($exclude, $pages); |
---|
3392 | $excludes = array(); |
---|
3393 | foreach ( $children as $child ) |
---|
3394 | $excludes[] = $child->ID; |
---|
3395 | $excludes[] = $exclude; |
---|
3396 | $num_pages = count($pages); |
---|
3397 | for ( $i = 0; $i < $num_pages; $i++ ) { |
---|
3398 | if ( in_array($pages[$i]->ID, $excludes) ) |
---|
3399 | unset($pages[$i]); |
---|
3400 | } |
---|
3401 | } |
---|
3402 | |
---|
3403 | $cache[ $key ] = $pages; |
---|
3404 | wp_cache_set( 'get_pages', $cache, 'posts' ); |
---|
3405 | |
---|
3406 | $pages = apply_filters('get_pages', $pages, $r); |
---|
3407 | |
---|
3408 | return $pages; |
---|
3409 | } |
---|
3410 | |
---|
3411 | // |
---|
3412 | // Attachment functions |
---|
3413 | // |
---|
3414 | |
---|
3415 | /** |
---|
3416 | * Check if the attachment URI is local one and is really an attachment. |
---|
3417 | * |
---|
3418 | * @since 2.0.0 |
---|
3419 | * |
---|
3420 | * @param string $url URL to check |
---|
3421 | * @return bool True on success, false on failure. |
---|
3422 | */ |
---|
3423 | function is_local_attachment($url) { |
---|
3424 | if (strpos($url, home_url()) === false) |
---|
3425 | return false; |
---|
3426 | if (strpos($url, home_url('/?attachment_id=')) !== false) |
---|
3427 | return true; |
---|
3428 | if ( $id = url_to_postid($url) ) { |
---|
3429 | $post = & get_post($id); |
---|
3430 | if ( 'attachment' == $post->post_type ) |
---|
3431 | return true; |
---|
3432 | } |
---|
3433 | return false; |
---|
3434 | } |
---|
3435 | |
---|
3436 | /** |
---|
3437 | * Insert an attachment. |
---|
3438 | * |
---|
3439 | * If you set the 'ID' in the $object parameter, it will mean that you are |
---|
3440 | * updating and attempt to update the attachment. You can also set the |
---|
3441 | * attachment name or title by setting the key 'post_name' or 'post_title'. |
---|
3442 | * |
---|
3443 | * You can set the dates for the attachment manually by setting the 'post_date' |
---|
3444 | * and 'post_date_gmt' keys' values. |
---|
3445 | * |
---|
3446 | * By default, the comments will use the default settings for whether the |
---|
3447 | * comments are allowed. You can close them manually or keep them open by |
---|
3448 | * setting the value for the 'comment_status' key. |
---|
3449 | * |
---|
3450 | * The $object parameter can have the following: |
---|
3451 | * 'post_status' - Default is 'draft'. Can not be overridden, set the same as parent post. |
---|
3452 | * 'post_type' - Default is 'post', will be set to attachment. Can not override. |
---|
3453 | * 'post_author' - Default is current user ID. The ID of the user, who added the attachment. |
---|
3454 | * 'ping_status' - Default is the value in default ping status option. Whether the attachment |
---|
3455 | * can accept pings. |
---|
3456 | * 'post_parent' - Default is 0. Can use $parent parameter or set this for the post it belongs |
---|
3457 | * to, if any. |
---|
3458 | * 'menu_order' - Default is 0. The order it is displayed. |
---|
3459 | * 'to_ping' - Whether to ping. |
---|
3460 | * 'pinged' - Default is empty string. |
---|
3461 | * 'post_password' - Default is empty string. The password to access the attachment. |
---|
3462 | * 'guid' - Global Unique ID for referencing the attachment. |
---|
3463 | * 'post_content_filtered' - Attachment post content filtered. |
---|
3464 | * 'post_excerpt' - Attachment excerpt. |
---|
3465 | * |
---|
3466 | * @since 2.0.0 |
---|
3467 | * @uses $wpdb |
---|
3468 | * @uses $user_ID |
---|
3469 | * @uses do_action() Calls 'edit_attachment' on $post_ID if this is an update. |
---|
3470 | * @uses do_action() Calls 'add_attachment' on $post_ID if this is not an update. |
---|
3471 | * |
---|
3472 | * @param string|array $object Arguments to override defaults. |
---|
3473 | * @param string $file Optional filename. |
---|
3474 | * @param int $parent Parent post ID. |
---|
3475 | * @return int Attachment ID. |
---|
3476 | */ |
---|
3477 | function wp_insert_attachment($object, $file = false, $parent = 0) { |
---|
3478 | global $wpdb, $user_ID; |
---|
3479 | |
---|
3480 | $defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_ID, |
---|
3481 | 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0, |
---|
3482 | 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '', |
---|
3483 | 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0); |
---|
3484 | |
---|
3485 | $object = wp_parse_args($object, $defaults); |
---|
3486 | if ( !empty($parent) ) |
---|
3487 | $object['post_parent'] = $parent; |
---|
3488 | |
---|
3489 | $object = sanitize_post($object, 'db'); |
---|
3490 | |
---|
3491 | // export array as variables |
---|
3492 | extract($object, EXTR_SKIP); |
---|
3493 | |
---|
3494 | if ( empty($post_author) ) |
---|
3495 | $post_author = $user_ID; |
---|
3496 | |
---|
3497 | $post_type = 'attachment'; |
---|
3498 | $post_status = 'inherit'; |
---|
3499 | |
---|
3500 | // Make sure we set a valid category. |
---|
3501 | if ( !isset($post_category) || 0 == count($post_category) || !is_array($post_category) ) { |
---|
3502 | // 'post' requires at least one category. |
---|
3503 | if ( 'post' == $post_type ) |
---|
3504 | $post_category = array( get_option('default_category') ); |
---|
3505 | else |
---|
3506 | $post_category = array(); |
---|
3507 | } |
---|
3508 | |
---|
3509 | // Are we updating or creating? |
---|
3510 | if ( !empty($ID) ) { |
---|
3511 | $update = true; |
---|
3512 | $post_ID = (int) $ID; |
---|
3513 | } else { |
---|
3514 | $update = false; |
---|
3515 | $post_ID = 0; |
---|
3516 | } |
---|
3517 | |
---|
3518 | // Create a valid post name. |
---|
3519 | if ( empty($post_name) ) |
---|
3520 | $post_name = sanitize_title($post_title); |
---|
3521 | else |
---|
3522 | $post_name = sanitize_title($post_name); |
---|
3523 | |
---|
3524 | // expected_slashed ($post_name) |
---|
3525 | $post_name = wp_unique_post_slug($post_name, $post_ID, $post_status, $post_type, $post_parent); |
---|
3526 | |
---|
3527 | if ( empty($post_date) ) |
---|
3528 | $post_date = current_time('mysql'); |
---|
3529 | if ( empty($post_date_gmt) ) |
---|
3530 | $post_date_gmt = current_time('mysql', 1); |
---|
3531 | |
---|
3532 | if ( empty($post_modified) ) |
---|
3533 | $post_modified = $post_date; |
---|
3534 | if ( empty($post_modified_gmt) ) |
---|
3535 | $post_modified_gmt = $post_date_gmt; |
---|
3536 | |
---|
3537 | if ( empty($comment_status) ) { |
---|
3538 | if ( $update ) |
---|
3539 | $comment_status = 'closed'; |
---|
3540 | else |
---|
3541 | $comment_status = get_option('default_comment_status'); |
---|
3542 | } |
---|
3543 | if ( empty($ping_status) ) |
---|
3544 | $ping_status = get_option('default_ping_status'); |
---|
3545 | |
---|
3546 | if ( isset($to_ping) ) |
---|
3547 | $to_ping = preg_replace('|\s+|', "\n", $to_ping); |
---|
3548 | else |
---|
3549 | $to_ping = ''; |
---|
3550 | |
---|
3551 | if ( isset($post_parent) ) |
---|
3552 | $post_parent = (int) $post_parent; |
---|
3553 | else |
---|
3554 | $post_parent = 0; |
---|
3555 | |
---|
3556 | if ( isset($menu_order) ) |
---|
3557 | $menu_order = (int) $menu_order; |
---|
3558 | else |
---|
3559 | $menu_order = 0; |
---|
3560 | |
---|
3561 | if ( !isset($post_password) ) |
---|
3562 | $post_password = ''; |
---|
3563 | |
---|
3564 | if ( ! isset($pinged) ) |
---|
3565 | $pinged = ''; |
---|
3566 | |
---|
3567 | // expected_slashed (everything!) |
---|
3568 | $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' ) ); |
---|
3569 | $data = stripslashes_deep( $data ); |
---|
3570 | |
---|
3571 | if ( $update ) { |
---|
3572 | $wpdb->update( $wpdb->posts, $data, array( 'ID' => $post_ID ) ); |
---|
3573 | } else { |
---|
3574 | // If there is a suggested ID, use it if not already present |
---|
3575 | if ( !empty($import_id) ) { |
---|
3576 | $import_id = (int) $import_id; |
---|
3577 | if ( ! $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id) ) ) { |
---|
3578 | $data['ID'] = $import_id; |
---|
3579 | } |
---|
3580 | } |
---|
3581 | |
---|
3582 | $wpdb->insert( $wpdb->posts, $data ); |
---|
3583 | $post_ID = (int) $wpdb->insert_id; |
---|
3584 | } |
---|
3585 | |
---|
3586 | if ( empty($post_name) ) { |
---|
3587 | $post_name = sanitize_title($post_title, $post_ID); |
---|
3588 | $wpdb->update( $wpdb->posts, compact("post_name"), array( 'ID' => $post_ID ) ); |
---|
3589 | } |
---|
3590 | |
---|
3591 | wp_set_post_categories($post_ID, $post_category); |
---|
3592 | |
---|
3593 | if ( $file ) |
---|
3594 | update_attached_file( $post_ID, $file ); |
---|
3595 | |
---|
3596 | clean_post_cache($post_ID); |
---|
3597 | |
---|
3598 | if ( isset($post_parent) && $post_parent < 0 ) |
---|
3599 | add_post_meta($post_ID, '_wp_attachment_temp_parent', $post_parent, true); |
---|
3600 | |
---|
3601 | if ( $update) { |
---|
3602 | do_action('edit_attachment', $post_ID); |
---|
3603 | } else { |
---|
3604 | do_action('add_attachment', $post_ID); |
---|
3605 | } |
---|
3606 | |
---|
3607 | return $post_ID; |
---|
3608 | } |
---|
3609 | |
---|
3610 | /** |
---|
3611 | * Trashes or deletes an attachment. |
---|
3612 | * |
---|
3613 | * When an attachment is permanently deleted, the file will also be removed. |
---|
3614 | * Deletion removes all post meta fields, taxonomy, comments, etc. associated |
---|
3615 | * with the attachment (except the main post). |
---|
3616 | * |
---|
3617 | * The attachment is moved to the trash instead of permanently deleted unless trash |
---|
3618 | * for media is disabled, item is already in the trash, or $force_delete is true. |
---|
3619 | * |
---|
3620 | * @since 2.0.0 |
---|
3621 | * @uses $wpdb |
---|
3622 | * @uses do_action() Calls 'delete_attachment' hook on Attachment ID. |
---|
3623 | * |
---|
3624 | * @param int $post_id Attachment ID. |
---|
3625 | * @param bool $force_delete Whether to bypass trash and force deletion. Defaults to false. |
---|
3626 | * @return mixed False on failure. Post data on success. |
---|
3627 | */ |
---|
3628 | function wp_delete_attachment( $post_id, $force_delete = false ) { |
---|
3629 | global $wpdb; |
---|
3630 | |
---|
3631 | if ( !$post = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $post_id) ) ) |
---|
3632 | return $post; |
---|
3633 | |
---|
3634 | if ( 'attachment' != $post->post_type ) |
---|
3635 | return false; |
---|
3636 | |
---|
3637 | if ( !$force_delete && EMPTY_TRASH_DAYS && MEDIA_TRASH && 'trash' != $post->post_status ) |
---|
3638 | return wp_trash_post( $post_id ); |
---|
3639 | |
---|
3640 | delete_post_meta($post_id, '_wp_trash_meta_status'); |
---|
3641 | delete_post_meta($post_id, '_wp_trash_meta_time'); |
---|
3642 | |
---|
3643 | $meta = wp_get_attachment_metadata( $post_id ); |
---|
3644 | $backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true ); |
---|
3645 | $file = get_attached_file( $post_id ); |
---|
3646 | |
---|
3647 | if ( is_multisite() ) |
---|
3648 | delete_transient( 'dirsize_cache' ); |
---|
3649 | |
---|
3650 | do_action('delete_attachment', $post_id); |
---|
3651 | |
---|
3652 | wp_delete_object_term_relationships($post_id, array('category', 'post_tag')); |
---|
3653 | wp_delete_object_term_relationships($post_id, get_object_taxonomies($post->post_type)); |
---|
3654 | |
---|
3655 | $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key = '_thumbnail_id' AND meta_value = %d", $post_id )); |
---|
3656 | |
---|
3657 | $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id )); |
---|
3658 | if ( ! empty( $comment_ids ) ) { |
---|
3659 | do_action( 'delete_comment', $comment_ids ); |
---|
3660 | foreach ( $comment_ids as $comment_id ) |
---|
3661 | wp_delete_comment( $comment_id, true ); |
---|
3662 | do_action( 'deleted_comment', $comment_ids ); |
---|
3663 | } |
---|
3664 | |
---|
3665 | $post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $post_id )); |
---|
3666 | if ( !empty($post_meta_ids) ) { |
---|
3667 | do_action( 'delete_postmeta', $post_meta_ids ); |
---|
3668 | $in_post_meta_ids = "'" . implode("', '", $post_meta_ids) . "'"; |
---|
3669 | $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_id IN($in_post_meta_ids)" ); |
---|
3670 | do_action( 'deleted_postmeta', $post_meta_ids ); |
---|
3671 | } |
---|
3672 | |
---|
3673 | do_action( 'delete_post', $post_id ); |
---|
3674 | $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->posts WHERE ID = %d", $post_id )); |
---|
3675 | do_action( 'deleted_post', $post_id ); |
---|
3676 | |
---|
3677 | $uploadpath = wp_upload_dir(); |
---|
3678 | |
---|
3679 | if ( ! empty($meta['thumb']) ) { |
---|
3680 | // Don't delete the thumb if another attachment uses it |
---|
3681 | 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'] . '%', $post_id)) ) { |
---|
3682 | $thumbfile = str_replace(basename($file), $meta['thumb'], $file); |
---|
3683 | $thumbfile = apply_filters('wp_delete_file', $thumbfile); |
---|
3684 | @ unlink( path_join($uploadpath['basedir'], $thumbfile) ); |
---|
3685 | } |
---|
3686 | } |
---|
3687 | |
---|
3688 | // remove intermediate and backup images if there are any |
---|
3689 | foreach ( get_intermediate_image_sizes() as $size ) { |
---|
3690 | if ( $intermediate = image_get_intermediate_size($post_id, $size) ) { |
---|
3691 | $intermediate_file = apply_filters('wp_delete_file', $intermediate['path']); |
---|
3692 | @ unlink( path_join($uploadpath['basedir'], $intermediate_file) ); |
---|
3693 | } |
---|
3694 | } |
---|
3695 | |
---|
3696 | if ( is_array($backup_sizes) ) { |
---|
3697 | foreach ( $backup_sizes as $size ) { |
---|
3698 | $del_file = path_join( dirname($meta['file']), $size['file'] ); |
---|
3699 | $del_file = apply_filters('wp_delete_file', $del_file); |
---|
3700 | @ unlink( path_join($uploadpath['basedir'], $del_file) ); |
---|
3701 | } |
---|
3702 | } |
---|
3703 | |
---|
3704 | $file = apply_filters('wp_delete_file', $file); |
---|
3705 | |
---|
3706 | if ( ! empty($file) ) |
---|
3707 | @ unlink($file); |
---|
3708 | |
---|
3709 | clean_post_cache($post_id); |
---|
3710 | |
---|
3711 | return $post; |
---|
3712 | } |
---|
3713 | |
---|
3714 | /** |
---|
3715 | * Retrieve attachment meta field for attachment ID. |
---|
3716 | * |
---|
3717 | * @since 2.1.0 |
---|
3718 | * |
---|
3719 | * @param int $post_id Attachment ID |
---|
3720 | * @param bool $unfiltered Optional, default is false. If true, filters are not run. |
---|
3721 | * @return string|bool Attachment meta field. False on failure. |
---|
3722 | */ |
---|
3723 | function wp_get_attachment_metadata( $post_id = 0, $unfiltered = false ) { |
---|
3724 | $post_id = (int) $post_id; |
---|
3725 | if ( !$post =& get_post( $post_id ) ) |
---|
3726 | return false; |
---|
3727 | |
---|
3728 | $data = get_post_meta( $post->ID, '_wp_attachment_metadata', true ); |
---|
3729 | |
---|
3730 | if ( $unfiltered ) |
---|
3731 | return $data; |
---|
3732 | |
---|
3733 | return apply_filters( 'wp_get_attachment_metadata', $data, $post->ID ); |
---|
3734 | } |
---|
3735 | |
---|
3736 | /** |
---|
3737 | * Update metadata for an attachment. |
---|
3738 | * |
---|
3739 | * @since 2.1.0 |
---|
3740 | * |
---|
3741 | * @param int $post_id Attachment ID. |
---|
3742 | * @param array $data Attachment data. |
---|
3743 | * @return int |
---|
3744 | */ |
---|
3745 | function wp_update_attachment_metadata( $post_id, $data ) { |
---|
3746 | $post_id = (int) $post_id; |
---|
3747 | if ( !$post =& get_post( $post_id ) ) |
---|
3748 | return false; |
---|
3749 | |
---|
3750 | $data = apply_filters( 'wp_update_attachment_metadata', $data, $post->ID ); |
---|
3751 | |
---|
3752 | return update_post_meta( $post->ID, '_wp_attachment_metadata', $data); |
---|
3753 | } |
---|
3754 | |
---|
3755 | /** |
---|
3756 | * Retrieve the URL for an attachment. |
---|
3757 | * |
---|
3758 | * @since 2.1.0 |
---|
3759 | * |
---|
3760 | * @param int $post_id Attachment ID. |
---|
3761 | * @return string |
---|
3762 | */ |
---|
3763 | function wp_get_attachment_url( $post_id = 0 ) { |
---|
3764 | $post_id = (int) $post_id; |
---|
3765 | if ( !$post =& get_post( $post_id ) ) |
---|
3766 | return false; |
---|
3767 | |
---|
3768 | $url = ''; |
---|
3769 | if ( $file = get_post_meta( $post->ID, '_wp_attached_file', true) ) { //Get attached file |
---|
3770 | if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { //Get upload directory |
---|
3771 | if ( 0 === strpos($file, $uploads['basedir']) ) //Check that the upload base exists in the file location |
---|
3772 | $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file); //replace file location with url location |
---|
3773 | elseif ( false !== strpos($file, 'wp-content/uploads') ) |
---|
3774 | $url = $uploads['baseurl'] . substr( $file, strpos($file, 'wp-content/uploads') + 18 ); |
---|
3775 | else |
---|
3776 | $url = $uploads['baseurl'] . "/$file"; //Its a newly uploaded file, therefor $file is relative to the basedir. |
---|
3777 | } |
---|
3778 | } |
---|
3779 | |
---|
3780 | 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. |
---|
3781 | $url = get_the_guid( $post->ID ); |
---|
3782 | |
---|
3783 | if ( 'attachment' != $post->post_type || empty($url) ) |
---|
3784 | return false; |
---|
3785 | |
---|
3786 | return apply_filters( 'wp_get_attachment_url', $url, $post->ID ); |
---|
3787 | } |
---|
3788 | |
---|
3789 | /** |
---|
3790 | * Retrieve thumbnail for an attachment. |
---|
3791 | * |
---|
3792 | * @since 2.1.0 |
---|
3793 | * |
---|
3794 | * @param int $post_id Attachment ID. |
---|
3795 | * @return mixed False on failure. Thumbnail file path on success. |
---|
3796 | */ |
---|
3797 | function wp_get_attachment_thumb_file( $post_id = 0 ) { |
---|
3798 | $post_id = (int) $post_id; |
---|
3799 | if ( !$post =& get_post( $post_id ) ) |
---|
3800 | return false; |
---|
3801 | if ( !is_array( $imagedata = wp_get_attachment_metadata( $post->ID ) ) ) |
---|
3802 | return false; |
---|
3803 | |
---|
3804 | $file = get_attached_file( $post->ID ); |
---|
3805 | |
---|
3806 | if ( !empty($imagedata['thumb']) && ($thumbfile = str_replace(basename($file), $imagedata['thumb'], $file)) && file_exists($thumbfile) ) |
---|
3807 | return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID ); |
---|
3808 | return false; |
---|
3809 | } |
---|
3810 | |
---|
3811 | /** |
---|
3812 | * Retrieve URL for an attachment thumbnail. |
---|
3813 | * |
---|
3814 | * @since 2.1.0 |
---|
3815 | * |
---|
3816 | * @param int $post_id Attachment ID |
---|
3817 | * @return string|bool False on failure. Thumbnail URL on success. |
---|
3818 | */ |
---|
3819 | function wp_get_attachment_thumb_url( $post_id = 0 ) { |
---|
3820 | $post_id = (int) $post_id; |
---|
3821 | if ( !$post =& get_post( $post_id ) ) |
---|
3822 | return false; |
---|
3823 | if ( !$url = wp_get_attachment_url( $post->ID ) ) |
---|
3824 | return false; |
---|
3825 | |
---|
3826 | $sized = image_downsize( $post_id, 'thumbnail' ); |
---|
3827 | if ( $sized ) |
---|
3828 | return $sized[0]; |
---|
3829 | |
---|
3830 | if ( !$thumb = wp_get_attachment_thumb_file( $post->ID ) ) |
---|
3831 | return false; |
---|
3832 | |
---|
3833 | $url = str_replace(basename($url), basename($thumb), $url); |
---|
3834 | |
---|
3835 | return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID ); |
---|
3836 | } |
---|
3837 | |
---|
3838 | /** |
---|
3839 | * Check if the attachment is an image. |
---|
3840 | * |
---|
3841 | * @since 2.1.0 |
---|
3842 | * |
---|
3843 | * @param int $post_id Attachment ID |
---|
3844 | * @return bool |
---|
3845 | */ |
---|
3846 | function wp_attachment_is_image( $post_id = 0 ) { |
---|
3847 | $post_id = (int) $post_id; |
---|
3848 | if ( !$post =& get_post( $post_id ) ) |
---|
3849 | return false; |
---|
3850 | |
---|
3851 | if ( !$file = get_attached_file( $post->ID ) ) |
---|
3852 | return false; |
---|
3853 | |
---|
3854 | $ext = preg_match('/\.([^.]+)$/', $file, $matches) ? strtolower($matches[1]) : false; |
---|
3855 | |
---|
3856 | $image_exts = array('jpg', 'jpeg', 'gif', 'png'); |
---|
3857 | |
---|
3858 | if ( 'image/' == substr($post->post_mime_type, 0, 6) || $ext && 'import' == $post->post_mime_type && in_array($ext, $image_exts) ) |
---|
3859 | return true; |
---|
3860 | return false; |
---|
3861 | } |
---|
3862 | |
---|
3863 | /** |
---|
3864 | * Retrieve the icon for a MIME type. |
---|
3865 | * |
---|
3866 | * @since 2.1.0 |
---|
3867 | * |
---|
3868 | * @param string $mime MIME type |
---|
3869 | * @return string|bool |
---|
3870 | */ |
---|
3871 | function wp_mime_type_icon( $mime = 0 ) { |
---|
3872 | if ( !is_numeric($mime) ) |
---|
3873 | $icon = wp_cache_get("mime_type_icon_$mime"); |
---|
3874 | if ( empty($icon) ) { |
---|
3875 | $post_id = 0; |
---|
3876 | $post_mimes = array(); |
---|
3877 | if ( is_numeric($mime) ) { |
---|
3878 | $mime = (int) $mime; |
---|
3879 | if ( $post =& get_post( $mime ) ) { |
---|
3880 | $post_id = (int) $post->ID; |
---|
3881 | $ext = preg_replace('/^.+?\.([^.]+)$/', '$1', $post->guid); |
---|
3882 | if ( !empty($ext) ) { |
---|
3883 | $post_mimes[] = $ext; |
---|
3884 | if ( $ext_type = wp_ext2type( $ext ) ) |
---|
3885 | $post_mimes[] = $ext_type; |
---|
3886 | } |
---|
3887 | $mime = $post->post_mime_type; |
---|
3888 | } else { |
---|
3889 | $mime = 0; |
---|
3890 | } |
---|
3891 | } else { |
---|
3892 | $post_mimes[] = $mime; |
---|
3893 | } |
---|
3894 | |
---|
3895 | $icon_files = wp_cache_get('icon_files'); |
---|
3896 | |
---|
3897 | if ( !is_array($icon_files) ) { |
---|
3898 | $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/crystal' ); |
---|
3899 | $icon_dir_uri = apply_filters( 'icon_dir_uri', includes_url('images/crystal') ); |
---|
3900 | $dirs = apply_filters( 'icon_dirs', array($icon_dir => $icon_dir_uri) ); |
---|
3901 | $icon_files = array(); |
---|
3902 | while ( $dirs ) { |
---|
3903 | $dir = array_shift($keys = array_keys($dirs)); |
---|
3904 | $uri = array_shift($dirs); |
---|
3905 | if ( $dh = opendir($dir) ) { |
---|
3906 | while ( false !== $file = readdir($dh) ) { |
---|
3907 | $file = basename($file); |
---|
3908 | if ( substr($file, 0, 1) == '.' ) |
---|
3909 | continue; |
---|
3910 | if ( !in_array(strtolower(substr($file, -4)), array('.png', '.gif', '.jpg') ) ) { |
---|
3911 | if ( is_dir("$dir/$file") ) |
---|
3912 | $dirs["$dir/$file"] = "$uri/$file"; |
---|
3913 | continue; |
---|
3914 | } |
---|
3915 | $icon_files["$dir/$file"] = "$uri/$file"; |
---|
3916 | } |
---|
3917 | closedir($dh); |
---|
3918 | } |
---|
3919 | } |
---|
3920 | wp_cache_set('icon_files', $icon_files, 600); |
---|
3921 | } |
---|
3922 | |
---|
3923 | // Icon basename - extension = MIME wildcard |
---|
3924 | foreach ( $icon_files as $file => $uri ) |
---|
3925 | $types[ preg_replace('/^([^.]*).*$/', '$1', basename($file)) ] =& $icon_files[$file]; |
---|
3926 | |
---|
3927 | if ( ! empty($mime) ) { |
---|
3928 | $post_mimes[] = substr($mime, 0, strpos($mime, '/')); |
---|
3929 | $post_mimes[] = substr($mime, strpos($mime, '/') + 1); |
---|
3930 | $post_mimes[] = str_replace('/', '_', $mime); |
---|
3931 | } |
---|
3932 | |
---|
3933 | $matches = wp_match_mime_types(array_keys($types), $post_mimes); |
---|
3934 | $matches['default'] = array('default'); |
---|
3935 | |
---|
3936 | foreach ( $matches as $match => $wilds ) { |
---|
3937 | if ( isset($types[$wilds[0]])) { |
---|
3938 | $icon = $types[$wilds[0]]; |
---|
3939 | if ( !is_numeric($mime) ) |
---|
3940 | wp_cache_set("mime_type_icon_$mime", $icon); |
---|
3941 | break; |
---|
3942 | } |
---|
3943 | } |
---|
3944 | } |
---|
3945 | |
---|
3946 | return apply_filters( 'wp_mime_type_icon', $icon, $mime, $post_id ); // Last arg is 0 if function pass mime type. |
---|
3947 | } |
---|
3948 | |
---|
3949 | /** |
---|
3950 | * Checked for changed slugs for published post objects and save the old slug. |
---|
3951 | * |
---|
3952 | * The function is used when a post object of any type is updated, |
---|
3953 | * by comparing the current and previous post objects. |
---|
3954 | * |
---|
3955 | * If the slug was changed and not already part of the old slugs then it will be |
---|
3956 | * added to the post meta field ('_wp_old_slug') for storing old slugs for that |
---|
3957 | * post. |
---|
3958 | * |
---|
3959 | * The most logically usage of this function is redirecting changed post objects, so |
---|
3960 | * that those that linked to an changed post will be redirected to the new post. |
---|
3961 | * |
---|
3962 | * @since 2.1.0 |
---|
3963 | * |
---|
3964 | * @param int $post_id Post ID. |
---|
3965 | * @param object $post The Post Object |
---|
3966 | * @param object $post_before The Previous Post Object |
---|
3967 | * @return int Same as $post_id |
---|
3968 | */ |
---|
3969 | function wp_check_for_changed_slugs($post_id, $post, $post_before) { |
---|
3970 | // dont bother if it hasnt changed |
---|
3971 | if ( $post->post_name == $post_before->post_name ) |
---|
3972 | return; |
---|
3973 | |
---|
3974 | // we're only concerned with published objects |
---|
3975 | if ( $post->post_status != 'publish' ) |
---|
3976 | return; |
---|
3977 | |
---|
3978 | $old_slugs = (array) get_post_meta($post_id, '_wp_old_slug'); |
---|
3979 | |
---|
3980 | // if we haven't added this old slug before, add it now |
---|
3981 | if ( !in_array($post_before->post_name, $old_slugs) ) |
---|
3982 | add_post_meta($post_id, '_wp_old_slug', $post_before->post_name); |
---|
3983 | |
---|
3984 | // if the new slug was used previously, delete it from the list |
---|
3985 | if ( in_array($post->post_name, $old_slugs) ) |
---|
3986 | delete_post_meta($post_id, '_wp_old_slug', $post->post_name); |
---|
3987 | } |
---|
3988 | |
---|
3989 | /** |
---|
3990 | * Retrieve the private post SQL based on capability. |
---|
3991 | * |
---|
3992 | * This function provides a standardized way to appropriately select on the |
---|
3993 | * post_status of posts/pages. The function will return a piece of SQL code that |
---|
3994 | * can be added to a WHERE clause; this SQL is constructed to allow all |
---|
3995 | * published posts, and all private posts to which the user has access. |
---|
3996 | * |
---|
3997 | * It also allows plugins that define their own post type to control the cap by |
---|
3998 | * using the hook 'pub_priv_sql_capability'. The plugin is expected to return |
---|
3999 | * the capability the user must have to read the private post type. |
---|
4000 | * |
---|
4001 | * @since 2.2.0 |
---|
4002 | * |
---|
4003 | * @uses $user_ID |
---|
4004 | * @uses apply_filters() Call 'pub_priv_sql_capability' filter for plugins with different post types. |
---|
4005 | * |
---|
4006 | * @param string $post_type currently only supports 'post' or 'page'. |
---|
4007 | * @return string SQL code that can be added to a where clause. |
---|
4008 | */ |
---|
4009 | function get_private_posts_cap_sql($post_type) { |
---|
4010 | return get_posts_by_author_sql($post_type, FALSE); |
---|
4011 | } |
---|
4012 | |
---|
4013 | /** |
---|
4014 | * Retrieve the post SQL based on capability, author, and type. |
---|
4015 | * |
---|
4016 | * See above for full description. |
---|
4017 | * |
---|
4018 | * @since 3.0.0 |
---|
4019 | * @param string $post_type currently only supports 'post' or 'page'. |
---|
4020 | * @param bool $full Optional. Returns a full WHERE statement instead of just an 'andalso' term. |
---|
4021 | * @param int $post_author Optional. Query posts having a single author ID. |
---|
4022 | * @return string SQL WHERE code that can be added to a query. |
---|
4023 | */ |
---|
4024 | function get_posts_by_author_sql($post_type, $full = TRUE, $post_author = NULL) { |
---|
4025 | global $user_ID, $wpdb; |
---|
4026 | |
---|
4027 | // Private posts |
---|
4028 | if ($post_type == 'post') { |
---|
4029 | $cap = 'read_private_posts'; |
---|
4030 | // Private pages |
---|
4031 | } elseif ($post_type == 'page') { |
---|
4032 | $cap = 'read_private_pages'; |
---|
4033 | // Dunno what it is, maybe plugins have their own post type? |
---|
4034 | } else { |
---|
4035 | $cap = ''; |
---|
4036 | $cap = apply_filters('pub_priv_sql_capability', $cap); |
---|
4037 | |
---|
4038 | if (empty($cap)) { |
---|
4039 | // We don't know what it is, filters don't change anything, |
---|
4040 | // so set the SQL up to return nothing. |
---|
4041 | return ' 1 = 0 '; |
---|
4042 | } |
---|
4043 | } |
---|
4044 | |
---|
4045 | if ($full) { |
---|
4046 | if (is_null($post_author)) { |
---|
4047 | $sql = $wpdb->prepare('WHERE post_type = %s AND ', $post_type); |
---|
4048 | } else { |
---|
4049 | $sql = $wpdb->prepare('WHERE post_author = %d AND post_type = %s AND ', $post_author, $post_type); |
---|
4050 | } |
---|
4051 | } else { |
---|
4052 | $sql = ''; |
---|
4053 | } |
---|
4054 | |
---|
4055 | $sql .= "(post_status = 'publish'"; |
---|
4056 | |
---|
4057 | if (current_user_can($cap)) { |
---|
4058 | // Does the user have the capability to view private posts? Guess so. |
---|
4059 | $sql .= " OR post_status = 'private'"; |
---|
4060 | } elseif (is_user_logged_in()) { |
---|
4061 | // Users can view their own private posts. |
---|
4062 | $id = (int) $user_ID; |
---|
4063 | if (is_null($post_author) || !$full) { |
---|
4064 | $sql .= " OR post_status = 'private' AND post_author = $id"; |
---|
4065 | } elseif ($id == (int)$post_author) { |
---|
4066 | $sql .= " OR post_status = 'private'"; |
---|
4067 | } // else none |
---|
4068 | } // else none |
---|
4069 | |
---|
4070 | $sql .= ')'; |
---|
4071 | |
---|
4072 | return $sql; |
---|
4073 | } |
---|
4074 | |
---|
4075 | /** |
---|
4076 | * Retrieve the date that the last post was published. |
---|
4077 | * |
---|
4078 | * The server timezone is the default and is the difference between GMT and |
---|
4079 | * server time. The 'blog' value is the date when the last post was posted. The |
---|
4080 | * 'gmt' is when the last post was posted in GMT formatted date. |
---|
4081 | * |
---|
4082 | * @since 0.71 |
---|
4083 | * |
---|
4084 | * @uses apply_filters() Calls 'get_lastpostdate' filter |
---|
4085 | * |
---|
4086 | * @param string $timezone The location to get the time. Can be 'gmt', 'blog', or 'server'. |
---|
4087 | * @return string The date of the last post. |
---|
4088 | */ |
---|
4089 | function get_lastpostdate($timezone = 'server') { |
---|
4090 | return apply_filters( 'get_lastpostdate', _get_last_post_time( $timezone, 'date' ), $timezone ); |
---|
4091 | } |
---|
4092 | |
---|
4093 | /** |
---|
4094 | * Retrieve last post modified date depending on timezone. |
---|
4095 | * |
---|
4096 | * The server timezone is the default and is the difference between GMT and |
---|
4097 | * server time. The 'blog' value is just when the last post was modified. The |
---|
4098 | * 'gmt' is when the last post was modified in GMT time. |
---|
4099 | * |
---|
4100 | * @since 1.2.0 |
---|
4101 | * @uses apply_filters() Calls 'get_lastpostmodified' filter |
---|
4102 | * |
---|
4103 | * @param string $timezone The location to get the time. Can be 'gmt', 'blog', or 'server'. |
---|
4104 | * @return string The date the post was last modified. |
---|
4105 | */ |
---|
4106 | function get_lastpostmodified($timezone = 'server') { |
---|
4107 | $lastpostmodified = _get_last_post_time( $timezone, 'modified' ); |
---|
4108 | |
---|
4109 | $lastpostdate = get_lastpostdate($timezone); |
---|
4110 | if ( $lastpostdate > $lastpostmodified ) |
---|
4111 | $lastpostmodified = $lastpostdate; |
---|
4112 | |
---|
4113 | return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone ); |
---|
4114 | } |
---|
4115 | |
---|
4116 | /** |
---|
4117 | * Retrieve latest post date data based on timezone. |
---|
4118 | * |
---|
4119 | * @access private |
---|
4120 | * @since 3.1.0 |
---|
4121 | * |
---|
4122 | * @param string $timezone The location to get the time. Can be 'gmt', 'blog', or 'server'. |
---|
4123 | * @param string $field Field to check. Can be 'date' or 'modified'. |
---|
4124 | * @return string The date. |
---|
4125 | */ |
---|
4126 | function _get_last_post_time( $timezone, $field ) { |
---|
4127 | global $wpdb, $blog_id; |
---|
4128 | |
---|
4129 | if ( !in_array( $field, array( 'date', 'modified' ) ) ) |
---|
4130 | return false; |
---|
4131 | |
---|
4132 | $post_types = get_query_var('post_type'); |
---|
4133 | if ( empty($post_types) ) |
---|
4134 | $post_types = 'post'; |
---|
4135 | |
---|
4136 | $post_types = apply_filters( "get_lastpost{$field}_post_types", (array) $post_types ); |
---|
4137 | |
---|
4138 | $key = "lastpost{$field}:$blog_id:$timezone:" . md5( serialize( $post_types ) ); |
---|
4139 | |
---|
4140 | $date = wp_cache_get( $key, 'timeinfo' ); |
---|
4141 | |
---|
4142 | if ( !$date ) { |
---|
4143 | $add_seconds_server = date('Z'); |
---|
4144 | |
---|
4145 | array_walk( $post_types, array( &$wpdb, 'escape_by_ref' ) ); |
---|
4146 | $post_types = "'" . implode( "', '", $post_types ) . "'"; |
---|
4147 | |
---|
4148 | switch ( strtolower( $timezone ) ) { |
---|
4149 | case 'gmt': |
---|
4150 | $date = $wpdb->get_var("SELECT post_{$field}_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1"); |
---|
4151 | break; |
---|
4152 | case 'blog': |
---|
4153 | $date = $wpdb->get_var("SELECT post_{$field} FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1"); |
---|
4154 | break; |
---|
4155 | case 'server': |
---|
4156 | $date = $wpdb->get_var("SELECT DATE_ADD(post_{$field}_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1"); |
---|
4157 | break; |
---|
4158 | } |
---|
4159 | |
---|
4160 | if ( $date ) |
---|
4161 | wp_cache_set( $key, $date, 'timeinfo' ); |
---|
4162 | } |
---|
4163 | |
---|
4164 | return $date; |
---|
4165 | } |
---|
4166 | |
---|
4167 | /** |
---|
4168 | * Updates posts in cache. |
---|
4169 | * |
---|
4170 | * @usedby update_page_cache() Aliased by this function. |
---|
4171 | * |
---|
4172 | * @package WordPress |
---|
4173 | * @subpackage Cache |
---|
4174 | * @since 1.5.1 |
---|
4175 | * |
---|
4176 | * @param array $posts Array of post objects |
---|
4177 | */ |
---|
4178 | function update_post_cache(&$posts) { |
---|
4179 | if ( !$posts ) |
---|
4180 | return; |
---|
4181 | |
---|
4182 | foreach ( $posts as $post ) |
---|
4183 | wp_cache_add($post->ID, $post, 'posts'); |
---|
4184 | } |
---|
4185 | |
---|
4186 | /** |
---|
4187 | * Will clean the post in the cache. |
---|
4188 | * |
---|
4189 | * Cleaning means delete from the cache of the post. Will call to clean the term |
---|
4190 | * object cache associated with the post ID. |
---|
4191 | * |
---|
4192 | * clean_post_cache() will call itself recursively for each child post. |
---|
4193 | * |
---|
4194 | * This function not run if $_wp_suspend_cache_invalidation is not empty. See |
---|
4195 | * wp_suspend_cache_invalidation(). |
---|
4196 | * |
---|
4197 | * @package WordPress |
---|
4198 | * @subpackage Cache |
---|
4199 | * @since 2.0.0 |
---|
4200 | * |
---|
4201 | * @uses do_action() Calls 'clean_post_cache' on $id before adding children (if any). |
---|
4202 | * |
---|
4203 | * @param int $id The Post ID in the cache to clean |
---|
4204 | */ |
---|
4205 | function clean_post_cache($id) { |
---|
4206 | global $_wp_suspend_cache_invalidation, $wpdb; |
---|
4207 | |
---|
4208 | if ( !empty($_wp_suspend_cache_invalidation) ) |
---|
4209 | return; |
---|
4210 | |
---|
4211 | $id = (int) $id; |
---|
4212 | |
---|
4213 | if ( 0 === $id ) |
---|
4214 | return; |
---|
4215 | |
---|
4216 | wp_cache_delete($id, 'posts'); |
---|
4217 | wp_cache_delete($id, 'post_meta'); |
---|
4218 | |
---|
4219 | clean_object_term_cache($id, 'post'); |
---|
4220 | |
---|
4221 | wp_cache_delete( 'wp_get_archives', 'general' ); |
---|
4222 | |
---|
4223 | do_action('clean_post_cache', $id); |
---|
4224 | |
---|
4225 | if ( $children = $wpdb->get_col( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_parent = %d", $id) ) ) { |
---|
4226 | foreach ( $children as $cid ) { |
---|
4227 | // Loop detection |
---|
4228 | if ( $cid == $id ) |
---|
4229 | continue; |
---|
4230 | clean_post_cache( $cid ); |
---|
4231 | } |
---|
4232 | } |
---|
4233 | |
---|
4234 | if ( is_multisite() ) |
---|
4235 | wp_cache_delete( $wpdb->blogid . '-' . $id, 'global-posts' ); |
---|
4236 | } |
---|
4237 | |
---|
4238 | /** |
---|
4239 | * Alias of update_post_cache(). |
---|
4240 | * |
---|
4241 | * @see update_post_cache() Posts and pages are the same, alias is intentional |
---|
4242 | * |
---|
4243 | * @package WordPress |
---|
4244 | * @subpackage Cache |
---|
4245 | * @since 1.5.1 |
---|
4246 | * |
---|
4247 | * @param array $pages list of page objects |
---|
4248 | */ |
---|
4249 | function update_page_cache(&$pages) { |
---|
4250 | update_post_cache($pages); |
---|
4251 | } |
---|
4252 | |
---|
4253 | /** |
---|
4254 | * Will clean the page in the cache. |
---|
4255 | * |
---|
4256 | * Clean (read: delete) page from cache that matches $id. Will also clean cache |
---|
4257 | * associated with 'all_page_ids' and 'get_pages'. |
---|
4258 | * |
---|
4259 | * @package WordPress |
---|
4260 | * @subpackage Cache |
---|
4261 | * @since 2.0.0 |
---|
4262 | * |
---|
4263 | * @uses do_action() Will call the 'clean_page_cache' hook action. |
---|
4264 | * |
---|
4265 | * @param int $id Page ID to clean |
---|
4266 | */ |
---|
4267 | function clean_page_cache($id) { |
---|
4268 | clean_post_cache($id); |
---|
4269 | |
---|
4270 | wp_cache_delete( 'all_page_ids', 'posts' ); |
---|
4271 | wp_cache_delete( 'get_pages', 'posts' ); |
---|
4272 | |
---|
4273 | do_action('clean_page_cache', $id); |
---|
4274 | } |
---|
4275 | |
---|
4276 | /** |
---|
4277 | * Call major cache updating functions for list of Post objects. |
---|
4278 | * |
---|
4279 | * @package WordPress |
---|
4280 | * @subpackage Cache |
---|
4281 | * @since 1.5.0 |
---|
4282 | * |
---|
4283 | * @uses $wpdb |
---|
4284 | * @uses update_post_cache() |
---|
4285 | * @uses update_object_term_cache() |
---|
4286 | * @uses update_postmeta_cache() |
---|
4287 | * |
---|
4288 | * @param array $posts Array of Post objects |
---|
4289 | * @param string $post_type The post type of the posts in $posts. Default is 'post'. |
---|
4290 | * @param bool $update_term_cache Whether to update the term cache. Default is true. |
---|
4291 | * @param bool $update_meta_cache Whether to update the meta cache. Default is true. |
---|
4292 | */ |
---|
4293 | function update_post_caches(&$posts, $post_type = 'post', $update_term_cache = true, $update_meta_cache = true) { |
---|
4294 | // No point in doing all this work if we didn't match any posts. |
---|
4295 | if ( !$posts ) |
---|
4296 | return; |
---|
4297 | |
---|
4298 | update_post_cache($posts); |
---|
4299 | |
---|
4300 | $post_ids = array(); |
---|
4301 | foreach ( $posts as $post ) |
---|
4302 | $post_ids[] = $post->ID; |
---|
4303 | |
---|
4304 | if ( empty($post_type) ) |
---|
4305 | $post_type = 'post'; |
---|
4306 | |
---|
4307 | if ( $update_term_cache ) { |
---|
4308 | if ( is_array($post_type) ) { |
---|
4309 | $ptypes = $post_type; |
---|
4310 | } elseif ( 'any' == $post_type ) { |
---|
4311 | // Just use the post_types in the supplied posts. |
---|
4312 | foreach ( $posts as $post ) |
---|
4313 | $ptypes[] = $post->post_type; |
---|
4314 | $ptypes = array_unique($ptypes); |
---|
4315 | } else { |
---|
4316 | $ptypes = array($post_type); |
---|
4317 | } |
---|
4318 | |
---|
4319 | if ( ! empty($ptypes) ) |
---|
4320 | update_object_term_cache($post_ids, $ptypes); |
---|
4321 | } |
---|
4322 | |
---|
4323 | if ( $update_meta_cache ) |
---|
4324 | update_postmeta_cache($post_ids); |
---|
4325 | } |
---|
4326 | |
---|
4327 | /** |
---|
4328 | * Updates metadata cache for list of post IDs. |
---|
4329 | * |
---|
4330 | * Performs SQL query to retrieve the metadata for the post IDs and updates the |
---|
4331 | * metadata cache for the posts. Therefore, the functions, which call this |
---|
4332 | * function, do not need to perform SQL queries on their own. |
---|
4333 | * |
---|
4334 | * @package WordPress |
---|
4335 | * @subpackage Cache |
---|
4336 | * @since 2.1.0 |
---|
4337 | * |
---|
4338 | * @uses $wpdb |
---|
4339 | * |
---|
4340 | * @param array $post_ids List of post IDs. |
---|
4341 | * @return bool|array Returns false if there is nothing to update or an array of metadata. |
---|
4342 | */ |
---|
4343 | function update_postmeta_cache($post_ids) { |
---|
4344 | return update_meta_cache('post', $post_ids); |
---|
4345 | } |
---|
4346 | |
---|
4347 | /** |
---|
4348 | * Will clean the attachment in the cache. |
---|
4349 | * |
---|
4350 | * Cleaning means delete from the cache. Optionaly will clean the term |
---|
4351 | * object cache associated with the attachment ID. |
---|
4352 | * |
---|
4353 | * This function will not run if $_wp_suspend_cache_invalidation is not empty. See |
---|
4354 | * wp_suspend_cache_invalidation(). |
---|
4355 | * |
---|
4356 | * @package WordPress |
---|
4357 | * @subpackage Cache |
---|
4358 | * @since 3.0.0 |
---|
4359 | * |
---|
4360 | * @uses do_action() Calls 'clean_attachment_cache' on $id. |
---|
4361 | * |
---|
4362 | * @param int $id The attachment ID in the cache to clean |
---|
4363 | * @param bool $clean_terms optional. Whether to clean terms cache |
---|
4364 | */ |
---|
4365 | function clean_attachment_cache($id, $clean_terms = false) { |
---|
4366 | global $_wp_suspend_cache_invalidation; |
---|
4367 | |
---|
4368 | if ( !empty($_wp_suspend_cache_invalidation) ) |
---|
4369 | return; |
---|
4370 | |
---|
4371 | $id = (int) $id; |
---|
4372 | |
---|
4373 | wp_cache_delete($id, 'posts'); |
---|
4374 | wp_cache_delete($id, 'post_meta'); |
---|
4375 | |
---|
4376 | if ( $clean_terms ) |
---|
4377 | clean_object_term_cache($id, 'attachment'); |
---|
4378 | |
---|
4379 | do_action('clean_attachment_cache', $id); |
---|
4380 | } |
---|
4381 | |
---|
4382 | // |
---|
4383 | // Hooks |
---|
4384 | // |
---|
4385 | |
---|
4386 | /** |
---|
4387 | * Hook for managing future post transitions to published. |
---|
4388 | * |
---|
4389 | * @since 2.3.0 |
---|
4390 | * @access private |
---|
4391 | * @uses $wpdb |
---|
4392 | * @uses do_action() Calls 'private_to_published' on post ID if this is a 'private_to_published' call. |
---|
4393 | * @uses wp_clear_scheduled_hook() with 'publish_future_post' and post ID. |
---|
4394 | * |
---|
4395 | * @param string $new_status New post status |
---|
4396 | * @param string $old_status Previous post status |
---|
4397 | * @param object $post Object type containing the post information |
---|
4398 | */ |
---|
4399 | function _transition_post_status($new_status, $old_status, $post) { |
---|
4400 | global $wpdb; |
---|
4401 | |
---|
4402 | if ( $old_status != 'publish' && $new_status == 'publish' ) { |
---|
4403 | // Reset GUID if transitioning to publish and it is empty |
---|
4404 | if ( '' == get_the_guid($post->ID) ) |
---|
4405 | $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post->ID ) ), array( 'ID' => $post->ID ) ); |
---|
4406 | do_action('private_to_published', $post->ID); // Deprecated, use private_to_publish |
---|
4407 | } |
---|
4408 | |
---|
4409 | // If published posts changed clear the lastpostmodified cache |
---|
4410 | if ( 'publish' == $new_status || 'publish' == $old_status) { |
---|
4411 | wp_cache_delete( 'lastpostmodified:server', 'timeinfo' ); |
---|
4412 | wp_cache_delete( 'lastpostmodified:gmt', 'timeinfo' ); |
---|
4413 | wp_cache_delete( 'lastpostmodified:blog', 'timeinfo' ); |
---|
4414 | } |
---|
4415 | |
---|
4416 | // Always clears the hook in case the post status bounced from future to draft. |
---|
4417 | wp_clear_scheduled_hook('publish_future_post', array( $post->ID ) ); |
---|
4418 | } |
---|
4419 | |
---|
4420 | /** |
---|
4421 | * Hook used to schedule publication for a post marked for the future. |
---|
4422 | * |
---|
4423 | * The $post properties used and must exist are 'ID' and 'post_date_gmt'. |
---|
4424 | * |
---|
4425 | * @since 2.3.0 |
---|
4426 | * @access private |
---|
4427 | * |
---|
4428 | * @param int $deprecated Not used. Can be set to null. Never implemented. |
---|
4429 | * Not marked as deprecated with _deprecated_argument() as it conflicts with |
---|
4430 | * wp_transition_post_status() and the default filter for _future_post_hook(). |
---|
4431 | * @param object $post Object type containing the post information |
---|
4432 | */ |
---|
4433 | function _future_post_hook( $deprecated = '', $post ) { |
---|
4434 | wp_clear_scheduled_hook( 'publish_future_post', array( $post->ID ) ); |
---|
4435 | wp_schedule_single_event( strtotime( get_gmt_from_date( $post->post_date ) . ' GMT') , 'publish_future_post', array( $post->ID ) ); |
---|
4436 | } |
---|
4437 | |
---|
4438 | /** |
---|
4439 | * Hook to schedule pings and enclosures when a post is published. |
---|
4440 | * |
---|
4441 | * @since 2.3.0 |
---|
4442 | * @access private |
---|
4443 | * @uses $wpdb |
---|
4444 | * @uses XMLRPC_REQUEST and APP_REQUEST constants. |
---|
4445 | * @uses do_action() Calls 'xmlprc_publish_post' on post ID if XMLRPC_REQUEST is defined. |
---|
4446 | * @uses do_action() Calls 'app_publish_post' on post ID if APP_REQUEST is defined. |
---|
4447 | * |
---|
4448 | * @param int $post_id The ID in the database table of the post being published |
---|
4449 | */ |
---|
4450 | function _publish_post_hook($post_id) { |
---|
4451 | global $wpdb; |
---|
4452 | |
---|
4453 | if ( defined('XMLRPC_REQUEST') ) |
---|
4454 | do_action('xmlrpc_publish_post', $post_id); |
---|
4455 | if ( defined('APP_REQUEST') ) |
---|
4456 | do_action('app_publish_post', $post_id); |
---|
4457 | |
---|
4458 | if ( defined('WP_IMPORTING') ) |
---|
4459 | return; |
---|
4460 | |
---|
4461 | $data = array( 'post_id' => $post_id, 'meta_value' => '1' ); |
---|
4462 | if ( get_option('default_pingback_flag') ) { |
---|
4463 | $wpdb->insert( $wpdb->postmeta, $data + array( 'meta_key' => '_pingme' ) ); |
---|
4464 | do_action( 'added_postmeta', $wpdb->insert_id, $post_id, '_pingme', 1 ); |
---|
4465 | } |
---|
4466 | $wpdb->insert( $wpdb->postmeta, $data + array( 'meta_key' => '_encloseme' ) ); |
---|
4467 | do_action( 'added_postmeta', $wpdb->insert_id, $post_id, '_encloseme', 1 ); |
---|
4468 | |
---|
4469 | wp_schedule_single_event(time(), 'do_pings'); |
---|
4470 | } |
---|
4471 | |
---|
4472 | /** |
---|
4473 | * Hook used to prevent page/post cache and rewrite rules from staying dirty. |
---|
4474 | * |
---|
4475 | * Does two things. If the post is a page and has a template then it will |
---|
4476 | * update/add that template to the meta. For both pages and posts, it will clean |
---|
4477 | * the post cache to make sure that the cache updates to the changes done |
---|
4478 | * recently. For pages, the rewrite rules of WordPress are flushed to allow for |
---|
4479 | * any changes. |
---|
4480 | * |
---|
4481 | * The $post parameter, only uses 'post_type' property and 'page_template' |
---|
4482 | * property. |
---|
4483 | * |
---|
4484 | * @since 2.3.0 |
---|
4485 | * @access private |
---|
4486 | * @uses $wp_rewrite Flushes Rewrite Rules. |
---|
4487 | * |
---|
4488 | * @param int $post_id The ID in the database table for the $post |
---|
4489 | * @param object $post Object type containing the post information |
---|
4490 | */ |
---|
4491 | function _save_post_hook($post_id, $post) { |
---|
4492 | if ( $post->post_type == 'page' ) { |
---|
4493 | clean_page_cache($post_id); |
---|
4494 | // Avoid flushing rules for every post during import. |
---|
4495 | if ( !defined('WP_IMPORTING') ) { |
---|
4496 | global $wp_rewrite; |
---|
4497 | $wp_rewrite->flush_rules(false); |
---|
4498 | } |
---|
4499 | } else { |
---|
4500 | clean_post_cache($post_id); |
---|
4501 | } |
---|
4502 | } |
---|
4503 | |
---|
4504 | /** |
---|
4505 | * Retrieve post ancestors and append to post ancestors property. |
---|
4506 | * |
---|
4507 | * Will only retrieve ancestors once, if property is already set, then nothing |
---|
4508 | * will be done. If there is not a parent post, or post ID and post parent ID |
---|
4509 | * are the same then nothing will be done. |
---|
4510 | * |
---|
4511 | * The parameter is passed by reference, so nothing needs to be returned. The |
---|
4512 | * property will be updated and can be referenced after the function is |
---|
4513 | * complete. The post parent will be an ancestor and the parent of the post |
---|
4514 | * parent will be an ancestor. There will only be two ancestors at the most. |
---|
4515 | * |
---|
4516 | * @since unknown |
---|
4517 | * @access private |
---|
4518 | * @uses $wpdb |
---|
4519 | * |
---|
4520 | * @param object $_post Post data. |
---|
4521 | * @return null When nothing needs to be done. |
---|
4522 | */ |
---|
4523 | function _get_post_ancestors(&$_post) { |
---|
4524 | global $wpdb; |
---|
4525 | |
---|
4526 | if ( isset($_post->ancestors) ) |
---|
4527 | return; |
---|
4528 | |
---|
4529 | $_post->ancestors = array(); |
---|
4530 | |
---|
4531 | if ( empty($_post->post_parent) || $_post->ID == $_post->post_parent ) |
---|
4532 | return; |
---|
4533 | |
---|
4534 | $id = $_post->ancestors[] = $_post->post_parent; |
---|
4535 | while ( $ancestor = $wpdb->get_var( $wpdb->prepare("SELECT `post_parent` FROM $wpdb->posts WHERE ID = %d LIMIT 1", $id) ) ) { |
---|
4536 | // Loop detection: If the ancestor has been seen before, break. |
---|
4537 | if ( ( $ancestor == $_post->ID ) || in_array($ancestor, $_post->ancestors) ) |
---|
4538 | break; |
---|
4539 | $id = $_post->ancestors[] = $ancestor; |
---|
4540 | } |
---|
4541 | } |
---|
4542 | |
---|
4543 | /** |
---|
4544 | * Determines which fields of posts are to be saved in revisions. |
---|
4545 | * |
---|
4546 | * Does two things. If passed a post *array*, it will return a post array ready |
---|
4547 | * to be insterted into the posts table as a post revision. Otherwise, returns |
---|
4548 | * an array whose keys are the post fields to be saved for post revisions. |
---|
4549 | * |
---|
4550 | * @package WordPress |
---|
4551 | * @subpackage Post_Revisions |
---|
4552 | * @since 2.6.0 |
---|
4553 | * @access private |
---|
4554 | * @uses apply_filters() Calls '_wp_post_revision_fields' on 'title', 'content' and 'excerpt' fields. |
---|
4555 | * |
---|
4556 | * @param array $post Optional a post array to be processed for insertion as a post revision. |
---|
4557 | * @param bool $autosave optional Is the revision an autosave? |
---|
4558 | * @return array Post array ready to be inserted as a post revision or array of fields that can be versioned. |
---|
4559 | */ |
---|
4560 | function _wp_post_revision_fields( $post = null, $autosave = false ) { |
---|
4561 | static $fields = false; |
---|
4562 | |
---|
4563 | if ( !$fields ) { |
---|
4564 | // Allow these to be versioned |
---|
4565 | $fields = array( |
---|
4566 | 'post_title' => __( 'Title' ), |
---|
4567 | 'post_content' => __( 'Content' ), |
---|
4568 | 'post_excerpt' => __( 'Excerpt' ), |
---|
4569 | ); |
---|
4570 | |
---|
4571 | // Runs only once |
---|
4572 | $fields = apply_filters( '_wp_post_revision_fields', $fields ); |
---|
4573 | |
---|
4574 | // WP uses these internally either in versioning or elsewhere - they cannot be versioned |
---|
4575 | foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 'post_author' ) as $protect ) |
---|
4576 | unset( $fields[$protect] ); |
---|
4577 | } |
---|
4578 | |
---|
4579 | if ( !is_array($post) ) |
---|
4580 | return $fields; |
---|
4581 | |
---|
4582 | $return = array(); |
---|
4583 | foreach ( array_intersect( array_keys( $post ), array_keys( $fields ) ) as $field ) |
---|
4584 | $return[$field] = $post[$field]; |
---|
4585 | |
---|
4586 | $return['post_parent'] = $post['ID']; |
---|
4587 | $return['post_status'] = 'inherit'; |
---|
4588 | $return['post_type'] = 'revision'; |
---|
4589 | $return['post_name'] = $autosave ? "$post[ID]-autosave" : "$post[ID]-revision"; |
---|
4590 | $return['post_date'] = isset($post['post_modified']) ? $post['post_modified'] : ''; |
---|
4591 | $return['post_date_gmt'] = isset($post['post_modified_gmt']) ? $post['post_modified_gmt'] : ''; |
---|
4592 | |
---|
4593 | return $return; |
---|
4594 | } |
---|
4595 | |
---|
4596 | /** |
---|
4597 | * Saves an already existing post as a post revision. |
---|
4598 | * |
---|
4599 | * Typically used immediately prior to post updates. |
---|
4600 | * |
---|
4601 | * @package WordPress |
---|
4602 | * @subpackage Post_Revisions |
---|
4603 | * @since 2.6.0 |
---|
4604 | * |
---|
4605 | * @uses _wp_put_post_revision() |
---|
4606 | * |
---|
4607 | * @param int $post_id The ID of the post to save as a revision. |
---|
4608 | * @return mixed Null or 0 if error, new revision ID, if success. |
---|
4609 | */ |
---|
4610 | function wp_save_post_revision( $post_id ) { |
---|
4611 | // We do autosaves manually with wp_create_post_autosave() |
---|
4612 | if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) |
---|
4613 | return; |
---|
4614 | |
---|
4615 | // WP_POST_REVISIONS = 0, false |
---|
4616 | if ( ! WP_POST_REVISIONS ) |
---|
4617 | return; |
---|
4618 | |
---|
4619 | if ( !$post = get_post( $post_id, ARRAY_A ) ) |
---|
4620 | return; |
---|
4621 | |
---|
4622 | if ( !post_type_supports($post['post_type'], 'revisions') ) |
---|
4623 | return; |
---|
4624 | |
---|
4625 | $return = _wp_put_post_revision( $post ); |
---|
4626 | |
---|
4627 | // WP_POST_REVISIONS = true (default), -1 |
---|
4628 | if ( !is_numeric( WP_POST_REVISIONS ) || WP_POST_REVISIONS < 0 ) |
---|
4629 | return $return; |
---|
4630 | |
---|
4631 | // all revisions and (possibly) one autosave |
---|
4632 | $revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) ); |
---|
4633 | |
---|
4634 | // WP_POST_REVISIONS = (int) (# of autosaves to save) |
---|
4635 | $delete = count($revisions) - WP_POST_REVISIONS; |
---|
4636 | |
---|
4637 | if ( $delete < 1 ) |
---|
4638 | return $return; |
---|
4639 | |
---|
4640 | $revisions = array_slice( $revisions, 0, $delete ); |
---|
4641 | |
---|
4642 | for ( $i = 0; isset($revisions[$i]); $i++ ) { |
---|
4643 | if ( false !== strpos( $revisions[$i]->post_name, 'autosave' ) ) |
---|
4644 | continue; |
---|
4645 | wp_delete_post_revision( $revisions[$i]->ID ); |
---|
4646 | } |
---|
4647 | |
---|
4648 | return $return; |
---|
4649 | } |
---|
4650 | |
---|
4651 | /** |
---|
4652 | * Retrieve the autosaved data of the specified post. |
---|
4653 | * |
---|
4654 | * Returns a post object containing the information that was autosaved for the |
---|
4655 | * specified post. |
---|
4656 | * |
---|
4657 | * @package WordPress |
---|
4658 | * @subpackage Post_Revisions |
---|
4659 | * @since 2.6.0 |
---|
4660 | * |
---|
4661 | * @param int $post_id The post ID. |
---|
4662 | * @return object|bool The autosaved data or false on failure or when no autosave exists. |
---|
4663 | */ |
---|
4664 | function wp_get_post_autosave( $post_id ) { |
---|
4665 | |
---|
4666 | if ( !$post = get_post( $post_id ) ) |
---|
4667 | return false; |
---|
4668 | |
---|
4669 | $q = array( |
---|
4670 | 'name' => "{$post->ID}-autosave", |
---|
4671 | 'post_parent' => $post->ID, |
---|
4672 | 'post_type' => 'revision', |
---|
4673 | 'post_status' => 'inherit' |
---|
4674 | ); |
---|
4675 | |
---|
4676 | // Use WP_Query so that the result gets cached |
---|
4677 | $autosave_query = new WP_Query; |
---|
4678 | |
---|
4679 | add_action( 'parse_query', '_wp_get_post_autosave_hack' ); |
---|
4680 | $autosave = $autosave_query->query( $q ); |
---|
4681 | remove_action( 'parse_query', '_wp_get_post_autosave_hack' ); |
---|
4682 | |
---|
4683 | if ( $autosave && is_array($autosave) && is_object($autosave[0]) ) |
---|
4684 | return $autosave[0]; |
---|
4685 | |
---|
4686 | return false; |
---|
4687 | } |
---|
4688 | |
---|
4689 | /** |
---|
4690 | * Internally used to hack WP_Query into submission. |
---|
4691 | * |
---|
4692 | * @package WordPress |
---|
4693 | * @subpackage Post_Revisions |
---|
4694 | * @since 2.6.0 |
---|
4695 | * |
---|
4696 | * @param object $query WP_Query object |
---|
4697 | */ |
---|
4698 | function _wp_get_post_autosave_hack( $query ) { |
---|
4699 | $query->is_single = false; |
---|
4700 | } |
---|
4701 | |
---|
4702 | /** |
---|
4703 | * Determines if the specified post is a revision. |
---|
4704 | * |
---|
4705 | * @package WordPress |
---|
4706 | * @subpackage Post_Revisions |
---|
4707 | * @since 2.6.0 |
---|
4708 | * |
---|
4709 | * @param int|object $post Post ID or post object. |
---|
4710 | * @return bool|int False if not a revision, ID of revision's parent otherwise. |
---|
4711 | */ |
---|
4712 | function wp_is_post_revision( $post ) { |
---|
4713 | if ( !$post = wp_get_post_revision( $post ) ) |
---|
4714 | return false; |
---|
4715 | return (int) $post->post_parent; |
---|
4716 | } |
---|
4717 | |
---|
4718 | /** |
---|
4719 | * Determines if the specified post is an autosave. |
---|
4720 | * |
---|
4721 | * @package WordPress |
---|
4722 | * @subpackage Post_Revisions |
---|
4723 | * @since 2.6.0 |
---|
4724 | * |
---|
4725 | * @param int|object $post Post ID or post object. |
---|
4726 | * @return bool|int False if not a revision, ID of autosave's parent otherwise |
---|
4727 | */ |
---|
4728 | function wp_is_post_autosave( $post ) { |
---|
4729 | if ( !$post = wp_get_post_revision( $post ) ) |
---|
4730 | return false; |
---|
4731 | if ( "{$post->post_parent}-autosave" !== $post->post_name ) |
---|
4732 | return false; |
---|
4733 | return (int) $post->post_parent; |
---|
4734 | } |
---|
4735 | |
---|
4736 | /** |
---|
4737 | * Inserts post data into the posts table as a post revision. |
---|
4738 | * |
---|
4739 | * @package WordPress |
---|
4740 | * @subpackage Post_Revisions |
---|
4741 | * @since 2.6.0 |
---|
4742 | * |
---|
4743 | * @uses wp_insert_post() |
---|
4744 | * |
---|
4745 | * @param int|object|array $post Post ID, post object OR post array. |
---|
4746 | * @param bool $autosave Optional. Is the revision an autosave? |
---|
4747 | * @return mixed Null or 0 if error, new revision ID if success. |
---|
4748 | */ |
---|
4749 | function _wp_put_post_revision( $post = null, $autosave = false ) { |
---|
4750 | if ( is_object($post) ) |
---|
4751 | $post = get_object_vars( $post ); |
---|
4752 | elseif ( !is_array($post) ) |
---|
4753 | $post = get_post($post, ARRAY_A); |
---|
4754 | if ( !$post || empty($post['ID']) ) |
---|
4755 | return; |
---|
4756 | |
---|
4757 | if ( isset($post['post_type']) && 'revision' == $post['post_type'] ) |
---|
4758 | return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) ); |
---|
4759 | |
---|
4760 | $post = _wp_post_revision_fields( $post, $autosave ); |
---|
4761 | $post = add_magic_quotes($post); //since data is from db |
---|
4762 | |
---|
4763 | $revision_id = wp_insert_post( $post ); |
---|
4764 | if ( is_wp_error($revision_id) ) |
---|
4765 | return $revision_id; |
---|
4766 | |
---|
4767 | if ( $revision_id ) |
---|
4768 | do_action( '_wp_put_post_revision', $revision_id ); |
---|
4769 | return $revision_id; |
---|
4770 | } |
---|
4771 | |
---|
4772 | /** |
---|
4773 | * Gets a post revision. |
---|
4774 | * |
---|
4775 | * @package WordPress |
---|
4776 | * @subpackage Post_Revisions |
---|
4777 | * @since 2.6.0 |
---|
4778 | * |
---|
4779 | * @uses get_post() |
---|
4780 | * |
---|
4781 | * @param int|object $post Post ID or post object |
---|
4782 | * @param string $output Optional. OBJECT, ARRAY_A, or ARRAY_N. |
---|
4783 | * @param string $filter Optional sanitation filter. @see sanitize_post() |
---|
4784 | * @return mixed Null if error or post object if success |
---|
4785 | */ |
---|
4786 | function &wp_get_post_revision(&$post, $output = OBJECT, $filter = 'raw') { |
---|
4787 | $null = null; |
---|
4788 | if ( !$revision = get_post( $post, OBJECT, $filter ) ) |
---|
4789 | return $revision; |
---|
4790 | if ( 'revision' !== $revision->post_type ) |
---|
4791 | return $null; |
---|
4792 | |
---|
4793 | if ( $output == OBJECT ) { |
---|
4794 | return $revision; |
---|
4795 | } elseif ( $output == ARRAY_A ) { |
---|
4796 | $_revision = get_object_vars($revision); |
---|
4797 | return $_revision; |
---|
4798 | } elseif ( $output == ARRAY_N ) { |
---|
4799 | $_revision = array_values(get_object_vars($revision)); |
---|
4800 | return $_revision; |
---|
4801 | } |
---|
4802 | |
---|
4803 | return $revision; |
---|
4804 | } |
---|
4805 | |
---|
4806 | /** |
---|
4807 | * Restores a post to the specified revision. |
---|
4808 | * |
---|
4809 | * Can restore a past revision using all fields of the post revision, or only selected fields. |
---|
4810 | * |
---|
4811 | * @package WordPress |
---|
4812 | * @subpackage Post_Revisions |
---|
4813 | * @since 2.6.0 |
---|
4814 | * |
---|
4815 | * @uses wp_get_post_revision() |
---|
4816 | * @uses wp_update_post() |
---|
4817 | * @uses do_action() Calls 'wp_restore_post_revision' on post ID and revision ID if wp_update_post() |
---|
4818 | * is successful. |
---|
4819 | * |
---|
4820 | * @param int|object $revision_id Revision ID or revision object. |
---|
4821 | * @param array $fields Optional. What fields to restore from. Defaults to all. |
---|
4822 | * @return mixed Null if error, false if no fields to restore, (int) post ID if success. |
---|
4823 | */ |
---|
4824 | function wp_restore_post_revision( $revision_id, $fields = null ) { |
---|
4825 | if ( !$revision = wp_get_post_revision( $revision_id, ARRAY_A ) ) |
---|
4826 | return $revision; |
---|
4827 | |
---|
4828 | if ( !is_array( $fields ) ) |
---|
4829 | $fields = array_keys( _wp_post_revision_fields() ); |
---|
4830 | |
---|
4831 | $update = array(); |
---|
4832 | foreach( array_intersect( array_keys( $revision ), $fields ) as $field ) |
---|
4833 | $update[$field] = $revision[$field]; |
---|
4834 | |
---|
4835 | if ( !$update ) |
---|
4836 | return false; |
---|
4837 | |
---|
4838 | $update['ID'] = $revision['post_parent']; |
---|
4839 | |
---|
4840 | $update = add_magic_quotes( $update ); //since data is from db |
---|
4841 | |
---|
4842 | $post_id = wp_update_post( $update ); |
---|
4843 | if ( is_wp_error( $post_id ) ) |
---|
4844 | return $post_id; |
---|
4845 | |
---|
4846 | if ( $post_id ) |
---|
4847 | do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] ); |
---|
4848 | |
---|
4849 | return $post_id; |
---|
4850 | } |
---|
4851 | |
---|
4852 | /** |
---|
4853 | * Deletes a revision. |
---|
4854 | * |
---|
4855 | * Deletes the row from the posts table corresponding to the specified revision. |
---|
4856 | * |
---|
4857 | * @package WordPress |
---|
4858 | * @subpackage Post_Revisions |
---|
4859 | * @since 2.6.0 |
---|
4860 | * |
---|
4861 | * @uses wp_get_post_revision() |
---|
4862 | * @uses wp_delete_post() |
---|
4863 | * |
---|
4864 | * @param int|object $revision_id Revision ID or revision object. |
---|
4865 | * @return mixed Null or WP_Error if error, deleted post if success. |
---|
4866 | */ |
---|
4867 | function wp_delete_post_revision( $revision_id ) { |
---|
4868 | if ( !$revision = wp_get_post_revision( $revision_id ) ) |
---|
4869 | return $revision; |
---|
4870 | |
---|
4871 | $delete = wp_delete_post( $revision->ID ); |
---|
4872 | if ( is_wp_error( $delete ) ) |
---|
4873 | return $delete; |
---|
4874 | |
---|
4875 | if ( $delete ) |
---|
4876 | do_action( 'wp_delete_post_revision', $revision->ID, $revision ); |
---|
4877 | |
---|
4878 | return $delete; |
---|
4879 | } |
---|
4880 | |
---|
4881 | /** |
---|
4882 | * Returns all revisions of specified post. |
---|
4883 | * |
---|
4884 | * @package WordPress |
---|
4885 | * @subpackage Post_Revisions |
---|
4886 | * @since 2.6.0 |
---|
4887 | * |
---|
4888 | * @uses get_children() |
---|
4889 | * |
---|
4890 | * @param int|object $post_id Post ID or post object |
---|
4891 | * @return array empty if no revisions |
---|
4892 | */ |
---|
4893 | function wp_get_post_revisions( $post_id = 0, $args = null ) { |
---|
4894 | if ( ! WP_POST_REVISIONS ) |
---|
4895 | return array(); |
---|
4896 | if ( ( !$post = get_post( $post_id ) ) || empty( $post->ID ) ) |
---|
4897 | return array(); |
---|
4898 | |
---|
4899 | $defaults = array( 'order' => 'DESC', 'orderby' => 'date' ); |
---|
4900 | $args = wp_parse_args( $args, $defaults ); |
---|
4901 | $args = array_merge( $args, array( 'post_parent' => $post->ID, 'post_type' => 'revision', 'post_status' => 'inherit' ) ); |
---|
4902 | |
---|
4903 | if ( !$revisions = get_children( $args ) ) |
---|
4904 | return array(); |
---|
4905 | return $revisions; |
---|
4906 | } |
---|
4907 | |
---|
4908 | function _set_preview($post) { |
---|
4909 | |
---|
4910 | if ( ! is_object($post) ) |
---|
4911 | return $post; |
---|
4912 | |
---|
4913 | $preview = wp_get_post_autosave($post->ID); |
---|
4914 | |
---|
4915 | if ( ! is_object($preview) ) |
---|
4916 | return $post; |
---|
4917 | |
---|
4918 | $preview = sanitize_post($preview); |
---|
4919 | |
---|
4920 | $post->post_content = $preview->post_content; |
---|
4921 | $post->post_title = $preview->post_title; |
---|
4922 | $post->post_excerpt = $preview->post_excerpt; |
---|
4923 | |
---|
4924 | return $post; |
---|
4925 | } |
---|
4926 | |
---|
4927 | function _show_post_preview |
---|