29 } |
29 } |
30 |
30 |
31 if ( is_null( $fields ) ) { |
31 if ( is_null( $fields ) ) { |
32 // Allow these to be versioned |
32 // Allow these to be versioned |
33 $fields = array( |
33 $fields = array( |
34 'post_title' => __( 'Title' ), |
34 'post_title' => __( 'Title' ), |
35 'post_content' => __( 'Content' ), |
35 'post_content' => __( 'Content' ), |
36 'post_excerpt' => __( 'Excerpt' ), |
36 'post_excerpt' => __( 'Excerpt' ), |
37 ); |
37 ); |
38 } |
38 } |
39 |
39 |
57 |
57 |
58 // WP uses these internally either in versioning or elsewhere - they cannot be versioned |
58 // WP uses these internally either in versioning or elsewhere - they cannot be versioned |
59 foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 'post_author' ) as $protect ) { |
59 foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 'post_author' ) as $protect ) { |
60 unset( $fields[ $protect ] ); |
60 unset( $fields[ $protect ] ); |
61 } |
61 } |
62 |
|
63 |
62 |
64 return $fields; |
63 return $fields; |
65 } |
64 } |
66 |
65 |
67 /** |
66 /** |
108 * |
107 * |
109 * @param int $post_id The ID of the post to save as a revision. |
108 * @param int $post_id The ID of the post to save as a revision. |
110 * @return int|WP_Error|void Void or 0 if error, new revision ID, if success. |
109 * @return int|WP_Error|void Void or 0 if error, new revision ID, if success. |
111 */ |
110 */ |
112 function wp_save_post_revision( $post_id ) { |
111 function wp_save_post_revision( $post_id ) { |
113 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) |
112 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
114 return; |
113 return; |
115 |
114 } |
116 if ( ! $post = get_post( $post_id ) ) |
115 |
|
116 if ( ! $post = get_post( $post_id ) ) { |
117 return; |
117 return; |
118 |
118 } |
119 if ( ! post_type_supports( $post->post_type, 'revisions' ) ) |
119 |
|
120 if ( ! post_type_supports( $post->post_type, 'revisions' ) ) { |
120 return; |
121 return; |
121 |
122 } |
122 if ( 'auto-draft' == $post->post_status ) |
123 |
|
124 if ( 'auto-draft' == $post->post_status ) { |
123 return; |
125 return; |
124 |
126 } |
125 if ( ! wp_revisions_enabled( $post ) ) |
127 |
|
128 if ( ! wp_revisions_enabled( $post ) ) { |
126 return; |
129 return; |
|
130 } |
127 |
131 |
128 // Compare the proposed update with the last stored revision verifying that |
132 // Compare the proposed update with the last stored revision verifying that |
129 // they are different, unless a plugin tells us to always save regardless. |
133 // they are different, unless a plugin tells us to always save regardless. |
130 // If no previous revisions, save one |
134 // If no previous revisions, save one |
131 if ( $revisions = wp_get_post_revisions( $post_id ) ) { |
135 if ( $revisions = wp_get_post_revisions( $post_id ) ) { |
147 * |
151 * |
148 * @param bool $check_for_changes Whether to check for changes before saving a new revision. |
152 * @param bool $check_for_changes Whether to check for changes before saving a new revision. |
149 * Default true. |
153 * Default true. |
150 * @param WP_Post $last_revision The last revision post object. |
154 * @param WP_Post $last_revision The last revision post object. |
151 * @param WP_Post $post The post object. |
155 * @param WP_Post $post The post object. |
152 * |
|
153 */ |
156 */ |
154 if ( isset( $last_revision ) && apply_filters( 'wp_save_post_revision_check_for_changes', $check_for_changes = true, $last_revision, $post ) ) { |
157 if ( isset( $last_revision ) && apply_filters( 'wp_save_post_revision_check_for_changes', $check_for_changes = true, $last_revision, $post ) ) { |
155 $post_has_changed = false; |
158 $post_has_changed = false; |
156 |
159 |
157 foreach ( array_keys( _wp_post_revision_fields( $post ) ) as $field ) { |
160 foreach ( array_keys( _wp_post_revision_fields( $post ) ) as $field ) { |
170 * @since 4.1.0 |
173 * @since 4.1.0 |
171 * |
174 * |
172 * @param bool $post_has_changed Whether the post has changed. |
175 * @param bool $post_has_changed Whether the post has changed. |
173 * @param WP_Post $last_revision The last revision post object. |
176 * @param WP_Post $last_revision The last revision post object. |
174 * @param WP_Post $post The post object. |
177 * @param WP_Post $post The post object. |
175 * |
|
176 */ |
178 */ |
177 $post_has_changed = (bool) apply_filters( 'wp_save_post_revision_post_has_changed', $post_has_changed, $last_revision, $post ); |
179 $post_has_changed = (bool) apply_filters( 'wp_save_post_revision_post_has_changed', $post_has_changed, $last_revision, $post ); |
178 |
180 |
179 //don't save revision if post unchanged |
181 //don't save revision if post unchanged |
180 if ( ! $post_has_changed ) { |
182 if ( ! $post_has_changed ) { |
187 |
189 |
188 // If a limit for the number of revisions to keep has been set, |
190 // If a limit for the number of revisions to keep has been set, |
189 // delete the oldest ones. |
191 // delete the oldest ones. |
190 $revisions_to_keep = wp_revisions_to_keep( $post ); |
192 $revisions_to_keep = wp_revisions_to_keep( $post ); |
191 |
193 |
192 if ( $revisions_to_keep < 0 ) |
194 if ( $revisions_to_keep < 0 ) { |
193 return $return; |
195 return $return; |
|
196 } |
194 |
197 |
195 $revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) ); |
198 $revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) ); |
196 |
199 |
197 $delete = count($revisions) - $revisions_to_keep; |
200 $delete = count( $revisions ) - $revisions_to_keep; |
198 |
201 |
199 if ( $delete < 1 ) |
202 if ( $delete < 1 ) { |
200 return $return; |
203 return $return; |
|
204 } |
201 |
205 |
202 $revisions = array_slice( $revisions, 0, $delete ); |
206 $revisions = array_slice( $revisions, 0, $delete ); |
203 |
207 |
204 for ( $i = 0; isset( $revisions[$i] ); $i++ ) { |
208 for ( $i = 0; isset( $revisions[ $i ] ); $i++ ) { |
205 if ( false !== strpos( $revisions[ $i ]->post_name, 'autosave' ) ) |
209 if ( false !== strpos( $revisions[ $i ]->post_name, 'autosave' ) ) { |
206 continue; |
210 continue; |
|
211 } |
207 |
212 |
208 wp_delete_post_revision( $revisions[ $i ]->ID ); |
213 wp_delete_post_revision( $revisions[ $i ]->ID ); |
209 } |
214 } |
210 |
215 |
211 return $return; |
216 return $return; |
227 function wp_get_post_autosave( $post_id, $user_id = 0 ) { |
232 function wp_get_post_autosave( $post_id, $user_id = 0 ) { |
228 $revisions = wp_get_post_revisions( $post_id, array( 'check_enabled' => false ) ); |
233 $revisions = wp_get_post_revisions( $post_id, array( 'check_enabled' => false ) ); |
229 |
234 |
230 foreach ( $revisions as $revision ) { |
235 foreach ( $revisions as $revision ) { |
231 if ( false !== strpos( $revision->post_name, "{$post_id}-autosave" ) ) { |
236 if ( false !== strpos( $revision->post_name, "{$post_id}-autosave" ) ) { |
232 if ( $user_id && $user_id != $revision->post_author ) |
237 if ( $user_id && $user_id != $revision->post_author ) { |
233 continue; |
238 continue; |
|
239 } |
234 |
240 |
235 return $revision; |
241 return $revision; |
236 } |
242 } |
237 } |
243 } |
238 |
244 |
246 * |
252 * |
247 * @param int|WP_Post $post Post ID or post object. |
253 * @param int|WP_Post $post Post ID or post object. |
248 * @return false|int False if not a revision, ID of revision's parent otherwise. |
254 * @return false|int False if not a revision, ID of revision's parent otherwise. |
249 */ |
255 */ |
250 function wp_is_post_revision( $post ) { |
256 function wp_is_post_revision( $post ) { |
251 if ( !$post = wp_get_post_revision( $post ) ) |
257 if ( ! $post = wp_get_post_revision( $post ) ) { |
252 return false; |
258 return false; |
|
259 } |
253 |
260 |
254 return (int) $post->post_parent; |
261 return (int) $post->post_parent; |
255 } |
262 } |
256 |
263 |
257 /** |
264 /** |
261 * |
268 * |
262 * @param int|WP_Post $post Post ID or post object. |
269 * @param int|WP_Post $post Post ID or post object. |
263 * @return false|int False if not a revision, ID of autosave's parent otherwise |
270 * @return false|int False if not a revision, ID of autosave's parent otherwise |
264 */ |
271 */ |
265 function wp_is_post_autosave( $post ) { |
272 function wp_is_post_autosave( $post ) { |
266 if ( !$post = wp_get_post_revision( $post ) ) |
273 if ( ! $post = wp_get_post_revision( $post ) ) { |
267 return false; |
274 return false; |
268 |
275 } |
269 if ( false !== strpos( $post->post_name, "{$post->post_parent}-autosave" ) ) |
276 |
|
277 if ( false !== strpos( $post->post_name, "{$post->post_parent}-autosave" ) ) { |
270 return (int) $post->post_parent; |
278 return (int) $post->post_parent; |
|
279 } |
271 |
280 |
272 return false; |
281 return false; |
273 } |
282 } |
274 |
283 |
275 /** |
284 /** |
281 * @param int|WP_Post|array|null $post Post ID, post object OR post array. |
290 * @param int|WP_Post|array|null $post Post ID, post object OR post array. |
282 * @param bool $autosave Optional. Is the revision an autosave? |
291 * @param bool $autosave Optional. Is the revision an autosave? |
283 * @return int|WP_Error WP_Error or 0 if error, new revision ID if success. |
292 * @return int|WP_Error WP_Error or 0 if error, new revision ID if success. |
284 */ |
293 */ |
285 function _wp_put_post_revision( $post = null, $autosave = false ) { |
294 function _wp_put_post_revision( $post = null, $autosave = false ) { |
286 if ( is_object($post) ) |
295 if ( is_object( $post ) ) { |
287 $post = get_object_vars( $post ); |
296 $post = get_object_vars( $post ); |
288 elseif ( !is_array($post) ) |
297 } elseif ( ! is_array( $post ) ) { |
289 $post = get_post($post, ARRAY_A); |
298 $post = get_post( $post, ARRAY_A ); |
290 |
299 } |
291 if ( ! $post || empty($post['ID']) ) |
300 |
|
301 if ( ! $post || empty( $post['ID'] ) ) { |
292 return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) ); |
302 return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) ); |
293 |
303 } |
294 if ( isset($post['post_type']) && 'revision' == $post['post_type'] ) |
304 |
|
305 if ( isset( $post['post_type'] ) && 'revision' == $post['post_type'] ) { |
295 return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) ); |
306 return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) ); |
|
307 } |
296 |
308 |
297 $post = _wp_post_revision_data( $post, $autosave ); |
309 $post = _wp_post_revision_data( $post, $autosave ); |
298 $post = wp_slash($post); //since data is from db |
310 $post = wp_slash( $post ); //since data is from db |
299 |
311 |
300 $revision_id = wp_insert_post( $post ); |
312 $revision_id = wp_insert_post( $post ); |
301 if ( is_wp_error($revision_id) ) |
313 if ( is_wp_error( $revision_id ) ) { |
302 return $revision_id; |
314 return $revision_id; |
|
315 } |
303 |
316 |
304 if ( $revision_id ) { |
317 if ( $revision_id ) { |
305 /** |
318 /** |
306 * Fires once a revision has been saved. |
319 * Fires once a revision has been saved. |
307 * |
320 * |
324 * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to |
337 * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to |
325 * a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT. |
338 * a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT. |
326 * @param string $filter Optional sanitation filter. See sanitize_post(). |
339 * @param string $filter Optional sanitation filter. See sanitize_post(). |
327 * @return WP_Post|array|null WP_Post (or array) on success, or null on failure. |
340 * @return WP_Post|array|null WP_Post (or array) on success, or null on failure. |
328 */ |
341 */ |
329 function wp_get_post_revision(&$post, $output = OBJECT, $filter = 'raw') { |
342 function wp_get_post_revision( &$post, $output = OBJECT, $filter = 'raw' ) { |
330 if ( !$revision = get_post( $post, OBJECT, $filter ) ) |
343 if ( ! $revision = get_post( $post, OBJECT, $filter ) ) { |
331 return $revision; |
344 return $revision; |
332 if ( 'revision' !== $revision->post_type ) |
345 } |
|
346 if ( 'revision' !== $revision->post_type ) { |
333 return null; |
347 return null; |
|
348 } |
334 |
349 |
335 if ( $output == OBJECT ) { |
350 if ( $output == OBJECT ) { |
336 return $revision; |
351 return $revision; |
337 } elseif ( $output == ARRAY_A ) { |
352 } elseif ( $output == ARRAY_A ) { |
338 $_revision = get_object_vars($revision); |
353 $_revision = get_object_vars( $revision ); |
339 return $_revision; |
354 return $_revision; |
340 } elseif ( $output == ARRAY_N ) { |
355 } elseif ( $output == ARRAY_N ) { |
341 $_revision = array_values(get_object_vars($revision)); |
356 $_revision = array_values( get_object_vars( $revision ) ); |
342 return $_revision; |
357 return $_revision; |
343 } |
358 } |
344 |
359 |
345 return $revision; |
360 return $revision; |
346 } |
361 } |
355 * @param int|WP_Post $revision_id Revision ID or revision object. |
370 * @param int|WP_Post $revision_id Revision ID or revision object. |
356 * @param array $fields Optional. What fields to restore from. Defaults to all. |
371 * @param array $fields Optional. What fields to restore from. Defaults to all. |
357 * @return int|false|null Null if error, false if no fields to restore, (int) post ID if success. |
372 * @return int|false|null Null if error, false if no fields to restore, (int) post ID if success. |
358 */ |
373 */ |
359 function wp_restore_post_revision( $revision_id, $fields = null ) { |
374 function wp_restore_post_revision( $revision_id, $fields = null ) { |
360 if ( !$revision = wp_get_post_revision( $revision_id, ARRAY_A ) ) |
375 if ( ! $revision = wp_get_post_revision( $revision_id, ARRAY_A ) ) { |
361 return $revision; |
376 return $revision; |
362 |
377 } |
363 if ( !is_array( $fields ) ) |
378 |
|
379 if ( ! is_array( $fields ) ) { |
364 $fields = array_keys( _wp_post_revision_fields( $revision ) ); |
380 $fields = array_keys( _wp_post_revision_fields( $revision ) ); |
|
381 } |
365 |
382 |
366 $update = array(); |
383 $update = array(); |
367 foreach ( array_intersect( array_keys( $revision ), $fields ) as $field ) { |
384 foreach ( array_intersect( array_keys( $revision ), $fields ) as $field ) { |
368 $update[$field] = $revision[$field]; |
385 $update[ $field ] = $revision[ $field ]; |
369 } |
386 } |
370 |
387 |
371 if ( !$update ) |
388 if ( ! $update ) { |
372 return false; |
389 return false; |
|
390 } |
373 |
391 |
374 $update['ID'] = $revision['post_parent']; |
392 $update['ID'] = $revision['post_parent']; |
375 |
393 |
376 $update = wp_slash( $update ); //since data is from db |
394 $update = wp_slash( $update ); //since data is from db |
377 |
395 |
378 $post_id = wp_update_post( $update ); |
396 $post_id = wp_update_post( $update ); |
379 if ( ! $post_id || is_wp_error( $post_id ) ) |
397 if ( ! $post_id || is_wp_error( $post_id ) ) { |
380 return $post_id; |
398 return $post_id; |
|
399 } |
381 |
400 |
382 // Update last edit user |
401 // Update last edit user |
383 update_post_meta( $post_id, '_edit_last', get_current_user_id() ); |
402 update_post_meta( $post_id, '_edit_last', get_current_user_id() ); |
384 |
403 |
385 /** |
404 /** |
437 * @param array|null $args Optional. Arguments for retrieving post revisions. Default null. |
456 * @param array|null $args Optional. Arguments for retrieving post revisions. Default null. |
438 * @return array An array of revisions, or an empty array if none. |
457 * @return array An array of revisions, or an empty array if none. |
439 */ |
458 */ |
440 function wp_get_post_revisions( $post_id = 0, $args = null ) { |
459 function wp_get_post_revisions( $post_id = 0, $args = null ) { |
441 $post = get_post( $post_id ); |
460 $post = get_post( $post_id ); |
442 if ( ! $post || empty( $post->ID ) ) |
461 if ( ! $post || empty( $post->ID ) ) { |
443 return array(); |
462 return array(); |
444 |
463 } |
445 $defaults = array( 'order' => 'DESC', 'orderby' => 'date ID', 'check_enabled' => true ); |
464 |
446 $args = wp_parse_args( $args, $defaults ); |
465 $defaults = array( |
447 |
466 'order' => 'DESC', |
448 if ( $args['check_enabled'] && ! wp_revisions_enabled( $post ) ) |
467 'orderby' => 'date ID', |
|
468 'check_enabled' => true, |
|
469 ); |
|
470 $args = wp_parse_args( $args, $defaults ); |
|
471 |
|
472 if ( $args['check_enabled'] && ! wp_revisions_enabled( $post ) ) { |
449 return array(); |
473 return array(); |
450 |
474 } |
451 $args = array_merge( $args, array( 'post_parent' => $post->ID, 'post_type' => 'revision', 'post_status' => 'inherit' ) ); |
475 |
452 |
476 $args = array_merge( |
453 if ( ! $revisions = get_children( $args ) ) |
477 $args, |
|
478 array( |
|
479 'post_parent' => $post->ID, |
|
480 'post_type' => 'revision', |
|
481 'post_status' => 'inherit', |
|
482 ) |
|
483 ); |
|
484 |
|
485 if ( ! $revisions = get_children( $args ) ) { |
454 return array(); |
486 return array(); |
|
487 } |
455 |
488 |
456 return $revisions; |
489 return $revisions; |
457 } |
490 } |
458 |
491 |
459 /** |
492 /** |
482 * @return int The number of revisions to keep. |
515 * @return int The number of revisions to keep. |
483 */ |
516 */ |
484 function wp_revisions_to_keep( $post ) { |
517 function wp_revisions_to_keep( $post ) { |
485 $num = WP_POST_REVISIONS; |
518 $num = WP_POST_REVISIONS; |
486 |
519 |
487 if ( true === $num ) |
520 if ( true === $num ) { |
488 $num = -1; |
521 $num = -1; |
489 else |
522 } else { |
490 $num = intval( $num ); |
523 $num = intval( $num ); |
491 |
524 } |
492 if ( ! post_type_supports( $post->post_type, 'revisions' ) ) |
525 |
|
526 if ( ! post_type_supports( $post->post_type, 'revisions' ) ) { |
493 $num = 0; |
527 $num = 0; |
|
528 } |
494 |
529 |
495 /** |
530 /** |
496 * Filters the number of revisions to save for the given post. |
531 * Filters the number of revisions to save for the given post. |
497 * |
532 * |
498 * Overrides the value of WP_POST_REVISIONS. |
533 * Overrides the value of WP_POST_REVISIONS. |
525 } |
560 } |
526 |
561 |
527 $preview = sanitize_post( $preview ); |
562 $preview = sanitize_post( $preview ); |
528 |
563 |
529 $post->post_content = $preview->post_content; |
564 $post->post_content = $preview->post_content; |
530 $post->post_title = $preview->post_title; |
565 $post->post_title = $preview->post_title; |
531 $post->post_excerpt = $preview->post_excerpt; |
566 $post->post_excerpt = $preview->post_excerpt; |
532 |
567 |
533 add_filter( 'get_the_terms', '_wp_preview_terms_filter', 10, 3 ); |
568 add_filter( 'get_the_terms', '_wp_preview_terms_filter', 10, 3 ); |
534 add_filter( 'get_post_metadata', '_wp_preview_post_thumbnail_filter', 10, 3 ); |
569 add_filter( 'get_post_metadata', '_wp_preview_post_thumbnail_filter', 10, 3 ); |
535 |
570 |
541 * |
576 * |
542 * @since 2.7.0 |
577 * @since 2.7.0 |
543 * @access private |
578 * @access private |
544 */ |
579 */ |
545 function _show_post_preview() { |
580 function _show_post_preview() { |
546 if ( isset($_GET['preview_id']) && isset($_GET['preview_nonce']) ) { |
581 if ( isset( $_GET['preview_id'] ) && isset( $_GET['preview_nonce'] ) ) { |
547 $id = (int) $_GET['preview_id']; |
582 $id = (int) $_GET['preview_id']; |
548 |
583 |
549 if ( false === wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $id ) ) |
584 if ( false === wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $id ) ) { |
550 wp_die( __('Sorry, you are not allowed to preview drafts.') ); |
585 wp_die( __( 'Sorry, you are not allowed to preview drafts.' ), 403 ); |
551 |
586 } |
552 add_filter('the_preview', '_set_preview'); |
587 |
|
588 add_filter( 'the_preview', '_set_preview' ); |
553 } |
589 } |
554 } |
590 } |
555 |
591 |
556 /** |
592 /** |
557 * Filters terms lookup to set the post format. |
593 * Filters terms lookup to set the post format. |
563 * @param int $post_id |
599 * @param int $post_id |
564 * @param string $taxonomy |
600 * @param string $taxonomy |
565 * @return array |
601 * @return array |
566 */ |
602 */ |
567 function _wp_preview_terms_filter( $terms, $post_id, $taxonomy ) { |
603 function _wp_preview_terms_filter( $terms, $post_id, $taxonomy ) { |
568 if ( ! $post = get_post() ) |
604 if ( ! $post = get_post() ) { |
569 return $terms; |
605 return $terms; |
570 |
606 } |
571 if ( empty( $_REQUEST['post_format'] ) || $post->ID != $post_id || 'post_format' != $taxonomy || 'revision' == $post->post_type ) |
607 |
|
608 if ( empty( $_REQUEST['post_format'] ) || $post->ID != $post_id || 'post_format' != $taxonomy || 'revision' == $post->post_type ) { |
572 return $terms; |
609 return $terms; |
573 |
610 } |
574 if ( 'standard' == $_REQUEST['post_format'] ) |
611 |
|
612 if ( 'standard' == $_REQUEST['post_format'] ) { |
575 $terms = array(); |
613 $terms = array(); |
576 elseif ( $term = get_term_by( 'slug', 'post-format-' . sanitize_key( $_REQUEST['post_format'] ), 'post_format' ) ) |
614 } elseif ( $term = get_term_by( 'slug', 'post-format-' . sanitize_key( $_REQUEST['post_format'] ), 'post_format' ) ) { |
577 $terms = array( $term ); // Can only have one post format |
615 $terms = array( $term ); // Can only have one post format |
|
616 } |
578 |
617 |
579 return $terms; |
618 return $terms; |
580 } |
619 } |
581 |
620 |
582 /** |
621 /** |
594 if ( ! $post = get_post() ) { |
633 if ( ! $post = get_post() ) { |
595 return $value; |
634 return $value; |
596 } |
635 } |
597 |
636 |
598 if ( empty( $_REQUEST['_thumbnail_id'] ) || |
637 if ( empty( $_REQUEST['_thumbnail_id'] ) || |
599 empty( $_REQUEST['preview_id'] ) || |
638 empty( $_REQUEST['preview_id'] ) || |
600 $post->ID != $post_id || |
639 $post->ID != $post_id || |
601 '_thumbnail_id' != $meta_key || |
640 '_thumbnail_id' != $meta_key || |
602 'revision' == $post->post_type || |
641 'revision' == $post->post_type || |
603 $post_id != $_REQUEST['preview_id'] |
642 $post_id != $_REQUEST['preview_id'] ) { |
604 ) { |
643 |
605 return $value; |
644 return $value; |
606 } |
645 } |
607 |
646 |
608 $thumbnail_id = intval( $_REQUEST['_thumbnail_id'] ); |
647 $thumbnail_id = intval( $_REQUEST['_thumbnail_id'] ); |
609 if ( $thumbnail_id <= 0 ) { |
648 if ( $thumbnail_id <= 0 ) { |
621 * |
660 * |
622 * @param WP_Post $revision |
661 * @param WP_Post $revision |
623 * @return int|false |
662 * @return int|false |
624 */ |
663 */ |
625 function _wp_get_post_revision_version( $revision ) { |
664 function _wp_get_post_revision_version( $revision ) { |
626 if ( is_object( $revision ) ) |
665 if ( is_object( $revision ) ) { |
627 $revision = get_object_vars( $revision ); |
666 $revision = get_object_vars( $revision ); |
628 elseif ( !is_array( $revision ) ) |
667 } elseif ( ! is_array( $revision ) ) { |
629 return false; |
668 return false; |
630 |
669 } |
631 if ( preg_match( '/^\d+-(?:autosave|revision)-v(\d+)$/', $revision['post_name'], $matches ) ) |
670 |
|
671 if ( preg_match( '/^\d+-(?:autosave|revision)-v(\d+)$/', $revision['post_name'], $matches ) ) { |
632 return (int) $matches[1]; |
672 return (int) $matches[1]; |
|
673 } |
633 |
674 |
634 return 0; |
675 return 0; |
635 } |
676 } |
636 |
677 |
637 /** |
678 /** |
648 */ |
689 */ |
649 function _wp_upgrade_revisions_of_post( $post, $revisions ) { |
690 function _wp_upgrade_revisions_of_post( $post, $revisions ) { |
650 global $wpdb; |
691 global $wpdb; |
651 |
692 |
652 // Add post option exclusively |
693 // Add post option exclusively |
653 $lock = "revision-upgrade-{$post->ID}"; |
694 $lock = "revision-upgrade-{$post->ID}"; |
654 $now = time(); |
695 $now = time(); |
655 $result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'no') /* LOCK */", $lock, $now ) ); |
696 $result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'no') /* LOCK */", $lock, $now ) ); |
656 if ( ! $result ) { |
697 if ( ! $result ) { |
657 // If we couldn't get a lock, see how old the previous lock is |
698 // If we couldn't get a lock, see how old the previous lock is |
658 $locked = get_option( $lock ); |
699 $locked = get_option( $lock ); |
659 if ( ! $locked ) { |
700 if ( ! $locked ) { |
681 $prev_revision = next( $revisions ); |
722 $prev_revision = next( $revisions ); |
682 |
723 |
683 $this_revision_version = _wp_get_post_revision_version( $this_revision ); |
724 $this_revision_version = _wp_get_post_revision_version( $this_revision ); |
684 |
725 |
685 // Something terrible happened |
726 // Something terrible happened |
686 if ( false === $this_revision_version ) |
727 if ( false === $this_revision_version ) { |
687 continue; |
728 continue; |
|
729 } |
688 |
730 |
689 // 1 is the latest revision version, so we're already up to date. |
731 // 1 is the latest revision version, so we're already up to date. |
690 // No need to add a copy of the post as latest revision. |
732 // No need to add a copy of the post as latest revision. |
691 if ( 0 < $this_revision_version ) { |
733 if ( 0 < $this_revision_version ) { |
692 $add_last = false; |
734 $add_last = false; |
703 // Update the revision version only and Leave the author as-is. |
745 // Update the revision version only and Leave the author as-is. |
704 if ( $prev_revision ) { |
746 if ( $prev_revision ) { |
705 $prev_revision_version = _wp_get_post_revision_version( $prev_revision ); |
747 $prev_revision_version = _wp_get_post_revision_version( $prev_revision ); |
706 |
748 |
707 // If the previous revision is already up to date, it no longer has the information we need :( |
749 // If the previous revision is already up to date, it no longer has the information we need :( |
708 if ( $prev_revision_version < 1 ) |
750 if ( $prev_revision_version < 1 ) { |
709 $update['post_author'] = $prev_revision->post_author; |
751 $update['post_author'] = $prev_revision->post_author; |
|
752 } |
710 } |
753 } |
711 |
754 |
712 // Upgrade this revision |
755 // Upgrade this revision |
713 $result = $wpdb->update( $wpdb->posts, $update, array( 'ID' => $this_revision->ID ) ); |
756 $result = $wpdb->update( $wpdb->posts, $update, array( 'ID' => $this_revision->ID ) ); |
714 |
757 |
715 if ( $result ) |
758 if ( $result ) { |
716 wp_cache_delete( $this_revision->ID, 'posts' ); |
759 wp_cache_delete( $this_revision->ID, 'posts' ); |
717 |
760 } |
718 } while ( $prev_revision ); |
761 } while ( $prev_revision ); |
719 |
762 |
720 delete_option( $lock ); |
763 delete_option( $lock ); |
721 |
764 |
722 // Add a copy of the post as latest revision. |
765 // Add a copy of the post as latest revision. |
723 if ( $add_last ) |
766 if ( $add_last ) { |
724 wp_save_post_revision( $post->ID ); |
767 wp_save_post_revision( $post->ID ); |
|
768 } |
725 |
769 |
726 return true; |
770 return true; |
727 } |
771 } |