~canonical-sysadmins/wordpress/4.7.4

« back to all changes in this revision

Viewing changes to wp-includes/revision.php

  • Committer: Jacek Nykis
  • Date: 2015-01-05 16:17:05 UTC
  • Revision ID: jacek.nykis@canonical.com-20150105161705-w544l1h5mcg7u4w9
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Post revision functions.
 
4
 *
 
5
 * @package WordPress
 
6
 * @subpackage Post_Revisions
 
7
 */
 
8
 
 
9
/**
 
10
 * Determines which fields of posts are to be saved in revisions.
 
11
 *
 
12
 * Does two things. If passed a post *array*, it will return a post array ready
 
13
 * to be inserted into the posts table as a post revision. Otherwise, returns
 
14
 * an array whose keys are the post fields to be saved for post revisions.
 
15
 *
 
16
 * @since 2.6.0
 
17
 * @access private
 
18
 *
 
19
 * @param array $post Optional a post array to be processed for insertion as a post revision.
 
20
 * @param bool $autosave optional Is the revision an autosave?
 
21
 * @return array Post array ready to be inserted as a post revision or array of fields that can be versioned.
 
22
 */
 
23
function _wp_post_revision_fields( $post = null, $autosave = false ) {
 
24
        static $fields = false;
 
25
 
 
26
        if ( !$fields ) {
 
27
                // Allow these to be versioned
 
28
                $fields = array(
 
29
                        'post_title' => __( 'Title' ),
 
30
                        'post_content' => __( 'Content' ),
 
31
                        'post_excerpt' => __( 'Excerpt' ),
 
32
                );
 
33
 
 
34
                /**
 
35
                 * Filter the list of fields saved in post revisions.
 
36
                 *
 
37
                 * Included by default: 'post_title', 'post_content' and 'post_excerpt'.
 
38
                 *
 
39
                 * Disallowed fields: 'ID', 'post_name', 'post_parent', 'post_date',
 
40
                 * 'post_date_gmt', 'post_status', 'post_type', 'comment_count',
 
41
                 * and 'post_author'.
 
42
                 *
 
43
                 * @since 2.6.0
 
44
                 *
 
45
                 * @param array $fields List of fields to revision. Contains 'post_title',
 
46
                 *                      'post_content', and 'post_excerpt' by default.
 
47
                 */
 
48
                $fields = apply_filters( '_wp_post_revision_fields', $fields );
 
49
 
 
50
                // WP uses these internally either in versioning or elsewhere - they cannot be versioned
 
51
                foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 'post_author' ) as $protect )
 
52
                        unset( $fields[$protect] );
 
53
        }
 
54
 
 
55
        if ( !is_array($post) )
 
56
                return $fields;
 
57
 
 
58
        $return = array();
 
59
        foreach ( array_intersect( array_keys( $post ), array_keys( $fields ) ) as $field )
 
60
                $return[$field] = $post[$field];
 
61
 
 
62
        $return['post_parent']   = $post['ID'];
 
63
        $return['post_status']   = 'inherit';
 
64
        $return['post_type']     = 'revision';
 
65
        $return['post_name']     = $autosave ? "$post[ID]-autosave-v1" : "$post[ID]-revision-v1"; // "1" is the revisioning system version
 
66
        $return['post_date']     = isset($post['post_modified']) ? $post['post_modified'] : '';
 
67
        $return['post_date_gmt'] = isset($post['post_modified_gmt']) ? $post['post_modified_gmt'] : '';
 
68
 
 
69
        return $return;
 
70
}
 
71
 
 
72
/**
 
73
 * Saves an already existing post as a post revision.
 
74
 *
 
75
 * Typically used immediately after post updates.
 
76
 * Adds a copy of the current post as a revision, so latest revision always matches current post
 
77
 *
 
78
 * @since 2.6.0
 
79
 *
 
80
 * @uses _wp_put_post_revision()
 
81
 *
 
82
 * @param int $post_id The ID of the post to save as a revision.
 
83
 * @return mixed Null or 0 if error, new revision ID, if success.
 
84
 */
 
85
function wp_save_post_revision( $post_id ) {
 
86
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
 
87
                return;
 
88
 
 
89
        if ( ! $post = get_post( $post_id ) )
 
90
                return;
 
91
 
 
92
        if ( ! post_type_supports( $post->post_type, 'revisions' ) )
 
93
                return;
 
94
 
 
95
        if ( 'auto-draft' == $post->post_status )
 
96
                return;
 
97
 
 
98
        if ( ! wp_revisions_enabled( $post ) )
 
99
                return;
 
100
 
 
101
        // Compare the proposed update with the last stored revision verifying that
 
102
        // they are different, unless a plugin tells us to always save regardless.
 
103
        // If no previous revisions, save one
 
104
        if ( $revisions = wp_get_post_revisions( $post_id ) ) {
 
105
                // grab the last revision, but not an autosave
 
106
                foreach ( $revisions as $revision ) {
 
107
                        if ( false !== strpos( $revision->post_name, "{$revision->post_parent}-revision" ) ) {
 
108
                                $last_revision = $revision;
 
109
                                break;
 
110
                        }
 
111
                }
 
112
 
 
113
                /**
 
114
                 * Filter whether the post has changed since the last revision.
 
115
                 *
 
116
                 * By default a revision is saved only if one of the revisioned fields has changed.
 
117
                 * This filter can override that so a revision is saved even if nothing has changed.
 
118
                 *
 
119
                 * @since 3.6.0
 
120
                 *
 
121
                 * @param bool $check_for_changes Whether to check for changes before saving a new revision.
 
122
                 *                                Default true.
 
123
                 * @param int  $last_revision     ID of the last revision.
 
124
                 * @param int  $post              Post ID.
 
125
                 *
 
126
                 */
 
127
                if ( isset( $last_revision ) && apply_filters( 'wp_save_post_revision_check_for_changes', $check_for_changes = true, $last_revision, $post ) ) {
 
128
                        $post_has_changed = false;
 
129
 
 
130
                        foreach ( array_keys( _wp_post_revision_fields() ) as $field ) {
 
131
                                if ( normalize_whitespace( $post->$field ) != normalize_whitespace( $last_revision->$field ) ) {
 
132
                                        $post_has_changed = true;
 
133
                                        break;
 
134
                                }
 
135
                        }
 
136
                        //don't save revision if post unchanged
 
137
                        if( ! $post_has_changed )
 
138
                                return;
 
139
                }
 
140
        }
 
141
 
 
142
        $return = _wp_put_post_revision( $post );
 
143
 
 
144
        $revisions_to_keep = wp_revisions_to_keep( $post );
 
145
 
 
146
        if ( $revisions_to_keep < 0 )
 
147
                return $return;
 
148
 
 
149
        // all revisions and autosaves
 
150
        $revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) );
 
151
 
 
152
        $delete = count($revisions) - $revisions_to_keep;
 
153
 
 
154
        if ( $delete < 1 )
 
155
                return $return;
 
156
 
 
157
        $revisions = array_slice( $revisions, 0, $delete );
 
158
 
 
159
        for ( $i = 0; isset( $revisions[$i] ); $i++ ) {
 
160
                if ( false !== strpos( $revisions[ $i ]->post_name, 'autosave' ) )
 
161
                        continue;
 
162
 
 
163
                wp_delete_post_revision( $revisions[ $i ]->ID );
 
164
        }
 
165
 
 
166
        return $return;
 
167
}
 
168
 
 
169
/**
 
170
 * Retrieve the autosaved data of the specified post.
 
171
 *
 
172
 * Returns a post object containing the information that was autosaved for the
 
173
 * specified post. If the optional $user_id is passed, returns the autosave for that user
 
174
 * otherwise returns the latest autosave.
 
175
 *
 
176
 * @since 2.6.0
 
177
 *
 
178
 * @uses wp_get_post_revisions()
 
179
 *
 
180
 * @param int $post_id The post ID.
 
181
 * @param int $user_id optional The post author ID.
 
182
 * @return object|bool The autosaved data or false on failure or when no autosave exists.
 
183
 */
 
184
function wp_get_post_autosave( $post_id, $user_id = 0 ) {
 
185
        $revisions = wp_get_post_revisions( $post_id, array( 'check_enabled' => false ) );
 
186
 
 
187
        foreach ( $revisions as $revision ) {
 
188
                if ( false !== strpos( $revision->post_name, "{$post_id}-autosave" ) ) {
 
189
                        if ( $user_id && $user_id != $revision->post_author )
 
190
                                continue;
 
191
 
 
192
                        return $revision;
 
193
                }
 
194
        }
 
195
 
 
196
        return false;
 
197
}
 
198
 
 
199
/**
 
200
 * Determines if the specified post is a revision.
 
201
 *
 
202
 * @since 2.6.0
 
203
 *
 
204
 * @param int|object $post Post ID or post object.
 
205
 * @return bool|int False if not a revision, ID of revision's parent otherwise.
 
206
 */
 
207
function wp_is_post_revision( $post ) {
 
208
        if ( !$post = wp_get_post_revision( $post ) )
 
209
                return false;
 
210
 
 
211
        return (int) $post->post_parent;
 
212
}
 
213
 
 
214
/**
 
215
 * Determines if the specified post is an autosave.
 
216
 *
 
217
 * @since 2.6.0
 
218
 *
 
219
 * @param int|object $post Post ID or post object.
 
220
 * @return bool|int False if not a revision, ID of autosave's parent otherwise
 
221
 */
 
222
function wp_is_post_autosave( $post ) {
 
223
        if ( !$post = wp_get_post_revision( $post ) )
 
224
                return false;
 
225
 
 
226
        if ( false !== strpos( $post->post_name, "{$post->post_parent}-autosave" ) )
 
227
                return (int) $post->post_parent;
 
228
 
 
229
        return false;
 
230
}
 
231
 
 
232
/**
 
233
 * Inserts post data into the posts table as a post revision.
 
234
 *
 
235
 * @since 2.6.0
 
236
 * @access private
 
237
 *
 
238
 * @uses wp_insert_post()
 
239
 *
 
240
 * @param int|object|array $post Post ID, post object OR post array.
 
241
 * @param bool $autosave Optional. Is the revision an autosave?
 
242
 * @return mixed WP_Error or 0 if error, new revision ID if success.
 
243
 */
 
244
function _wp_put_post_revision( $post = null, $autosave = false ) {
 
245
        if ( is_object($post) )
 
246
                $post = get_object_vars( $post );
 
247
        elseif ( !is_array($post) )
 
248
                $post = get_post($post, ARRAY_A);
 
249
 
 
250
        if ( ! $post || empty($post['ID']) )
 
251
                return new WP_Error( 'invalid_post', __( 'Invalid post ID' ) );
 
252
 
 
253
        if ( isset($post['post_type']) && 'revision' == $post['post_type'] )
 
254
                return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );
 
255
 
 
256
        $post = _wp_post_revision_fields( $post, $autosave );
 
257
        $post = wp_slash($post); //since data is from db
 
258
 
 
259
        $revision_id = wp_insert_post( $post );
 
260
        if ( is_wp_error($revision_id) )
 
261
                return $revision_id;
 
262
 
 
263
        if ( $revision_id ) {
 
264
                /**
 
265
                 * Fires once a revision has been saved.
 
266
                 *
 
267
                 * @since 2.6.0
 
268
                 *
 
269
                 * @param int $revision_id Post revision ID.
 
270
                 */
 
271
                do_action( '_wp_put_post_revision', $revision_id );
 
272
        }
 
273
 
 
274
        return $revision_id;
 
275
}
 
276
 
 
277
/**
 
278
 * Gets a post revision.
 
279
 *
 
280
 * @since 2.6.0
 
281
 *
 
282
 * @uses get_post()
 
283
 *
 
284
 * @param int|object $post The post ID or object.
 
285
 * @param string $output Optional. OBJECT, ARRAY_A, or ARRAY_N.
 
286
 * @param string $filter Optional sanitation filter. @see sanitize_post().
 
287
 * @return mixed Null if error or post object if success.
 
288
 */
 
289
function wp_get_post_revision(&$post, $output = OBJECT, $filter = 'raw') {
 
290
        if ( !$revision = get_post( $post, OBJECT, $filter ) )
 
291
                return $revision;
 
292
        if ( 'revision' !== $revision->post_type )
 
293
                return null;
 
294
 
 
295
        if ( $output == OBJECT ) {
 
296
                return $revision;
 
297
        } elseif ( $output == ARRAY_A ) {
 
298
                $_revision = get_object_vars($revision);
 
299
                return $_revision;
 
300
        } elseif ( $output == ARRAY_N ) {
 
301
                $_revision = array_values(get_object_vars($revision));
 
302
                return $_revision;
 
303
        }
 
304
 
 
305
        return $revision;
 
306
}
 
307
 
 
308
/**
 
309
 * Restores a post to the specified revision.
 
310
 *
 
311
 * Can restore a past revision using all fields of the post revision, or only selected fields.
 
312
 *
 
313
 * @since 2.6.0
 
314
 *
 
315
 * @uses wp_get_post_revision()
 
316
 * @uses wp_update_post()
 
317
 *
 
318
 * @param int|object $revision_id Revision ID or revision object.
 
319
 * @param array $fields Optional. What fields to restore from. Defaults to all.
 
320
 * @return mixed Null if error, false if no fields to restore, (int) post ID if success.
 
321
 */
 
322
function wp_restore_post_revision( $revision_id, $fields = null ) {
 
323
        if ( !$revision = wp_get_post_revision( $revision_id, ARRAY_A ) )
 
324
                return $revision;
 
325
 
 
326
        if ( !is_array( $fields ) )
 
327
                $fields = array_keys( _wp_post_revision_fields() );
 
328
 
 
329
        $update = array();
 
330
        foreach( array_intersect( array_keys( $revision ), $fields ) as $field ) {
 
331
                $update[$field] = $revision[$field];
 
332
        }
 
333
 
 
334
        if ( !$update )
 
335
                return false;
 
336
 
 
337
        $update['ID'] = $revision['post_parent'];
 
338
 
 
339
        $update = wp_slash( $update ); //since data is from db
 
340
 
 
341
        $post_id = wp_update_post( $update );
 
342
        if ( ! $post_id || is_wp_error( $post_id ) )
 
343
                return $post_id;
 
344
 
 
345
        // Add restore from details
 
346
        $restore_details = array(
 
347
                'restored_revision_id' => $revision_id,
 
348
                'restored_by_user'     => get_current_user_id(),
 
349
                'restored_time'        => time()
 
350
        );
 
351
        update_post_meta( $post_id, '_post_restored_from', $restore_details );
 
352
 
 
353
        // Update last edit user
 
354
        update_post_meta( $post_id, '_edit_last', get_current_user_id() );
 
355
 
 
356
        /**
 
357
         * Fires after a post revision has been restored.
 
358
         *
 
359
         * @since 2.6.0
 
360
         *
 
361
         * @param int $post_id     Post ID.
 
362
         * @param int $revision_id Post revision ID.
 
363
         */
 
364
        do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] );
 
365
 
 
366
        return $post_id;
 
367
}
 
368
 
 
369
/**
 
370
 * Deletes a revision.
 
371
 *
 
372
 * Deletes the row from the posts table corresponding to the specified revision.
 
373
 *
 
374
 * @since 2.6.0
 
375
 *
 
376
 * @uses wp_get_post_revision()
 
377
 * @uses wp_delete_post()
 
378
 *
 
379
 * @param int|object $revision_id Revision ID or revision object.
 
380
 * @return mixed Null or WP_Error if error, deleted post if success.
 
381
 */
 
382
function wp_delete_post_revision( $revision_id ) {
 
383
        if ( !$revision = wp_get_post_revision( $revision_id ) )
 
384
                return $revision;
 
385
 
 
386
        $delete = wp_delete_post( $revision->ID );
 
387
        if ( is_wp_error( $delete ) )
 
388
                return $delete;
 
389
 
 
390
        if ( $delete ) {
 
391
                /**
 
392
                 * Fires once a post revision has been deleted.
 
393
                 *
 
394
                 * @since 2.6.0
 
395
                 *
 
396
                 * @param int          $revision_id Post revision ID.
 
397
                 * @param object|array $revision    Post revision object or array.
 
398
                 */
 
399
                do_action( 'wp_delete_post_revision', $revision->ID, $revision );
 
400
        }
 
401
 
 
402
        return $delete;
 
403
}
 
404
 
 
405
/**
 
406
 * Returns all revisions of specified post.
 
407
 *
 
408
 * @since 2.6.0
 
409
 *
 
410
 * @uses get_children()
 
411
 *
 
412
 * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
 
413
 * @return array An array of revisions, or an empty array if none.
 
414
 */
 
415
function wp_get_post_revisions( $post_id = 0, $args = null ) {
 
416
        $post = get_post( $post_id );
 
417
        if ( ! $post || empty( $post->ID ) )
 
418
                return array();
 
419
 
 
420
        $defaults = array( 'order' => 'DESC', 'orderby' => 'date ID', 'check_enabled' => true );
 
421
        $args = wp_parse_args( $args, $defaults );
 
422
 
 
423
        if ( $args['check_enabled'] && ! wp_revisions_enabled( $post ) )
 
424
                return array();
 
425
 
 
426
        $args = array_merge( $args, array( 'post_parent' => $post->ID, 'post_type' => 'revision', 'post_status' => 'inherit' ) );
 
427
 
 
428
        if ( ! $revisions = get_children( $args ) )
 
429
                return array();
 
430
 
 
431
        return $revisions;
 
432
}
 
433
 
 
434
/**
 
435
 * Determine if revisions are enabled for a given post.
 
436
 *
 
437
 * @since 3.6.0
 
438
 *
 
439
 * @uses wp_revisions_to_keep()
 
440
 *
 
441
 * @param object $post The post object.
 
442
 * @return bool True if number of revisions to keep isn't zero, false otherwise.
 
443
 */
 
444
function wp_revisions_enabled( $post ) {
 
445
        return wp_revisions_to_keep( $post ) != 0;
 
446
}
 
447
 
 
448
/**
 
449
 * Determine how many revisions to retain for a given post.
 
450
 * By default, an infinite number of revisions are stored if a post type supports revisions.
 
451
 *
 
452
 * @since 3.6.0
 
453
 *
 
454
 * @uses post_type_supports()
 
455
 *
 
456
 * @param object $post The post object.
 
457
 * @return int The number of revisions to keep.
 
458
 */
 
459
function wp_revisions_to_keep( $post ) {
 
460
        $num = WP_POST_REVISIONS;
 
461
 
 
462
        if ( true === $num )
 
463
                $num = -1;
 
464
        else
 
465
                $num = intval( $num );
 
466
 
 
467
        if ( ! post_type_supports( $post->post_type, 'revisions' ) )
 
468
                $num = 0;
 
469
 
 
470
        /**
 
471
         * Filter the number of revisions to save for the given post.
 
472
         *
 
473
         * Overrides the value of WP_POST_REVISIONS.
 
474
         *
 
475
         * @since 3.6.0
 
476
         *
 
477
         * @param int     $num  Number of revisions to store.
 
478
         * @param WP_Post $post Post object.
 
479
         */
 
480
        return (int) apply_filters( 'wp_revisions_to_keep', $num, $post );
 
481
}
 
482
 
 
483
/**
 
484
 * Sets up the post object for preview based on the post autosave.
 
485
 *
 
486
 * @since 2.7.0
 
487
 * @access private
 
488
 */
 
489
function _set_preview($post) {
 
490
 
 
491
        if ( ! is_object($post) )
 
492
                return $post;
 
493
 
 
494
        $preview = wp_get_post_autosave($post->ID);
 
495
 
 
496
        if ( ! is_object($preview) )
 
497
                return $post;
 
498
 
 
499
        $preview = sanitize_post($preview);
 
500
 
 
501
        $post->post_content = $preview->post_content;
 
502
        $post->post_title = $preview->post_title;
 
503
        $post->post_excerpt = $preview->post_excerpt;
 
504
 
 
505
        add_filter( 'get_the_terms', '_wp_preview_terms_filter', 10, 3 );
 
506
 
 
507
        return $post;
 
508
}
 
509
 
 
510
/**
 
511
 * Filters the latest content for preview from the post autosave.
 
512
 *
 
513
 * @since 2.7.0
 
514
 * @access private
 
515
 */
 
516
function _show_post_preview() {
 
517
 
 
518
        if ( isset($_GET['preview_id']) && isset($_GET['preview_nonce']) ) {
 
519
                $id = (int) $_GET['preview_id'];
 
520
 
 
521
                if ( false == wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $id ) )
 
522
                        wp_die( __('You do not have permission to preview drafts.') );
 
523
 
 
524
                add_filter('the_preview', '_set_preview');
 
525
        }
 
526
}
 
527
 
 
528
/**
 
529
 * Filters terms lookup to set the post format.
 
530
 *
 
531
 * @since 3.6.0
 
532
 * @access private
 
533
 */
 
534
function _wp_preview_terms_filter( $terms, $post_id, $taxonomy ) {
 
535
        if ( ! $post = get_post() )
 
536
                return $terms;
 
537
 
 
538
        if ( empty( $_REQUEST['post_format'] ) || $post->ID != $post_id || 'post_format' != $taxonomy || 'revision' == $post->post_type )
 
539
                return $terms;
 
540
 
 
541
        if ( 'standard' == $_REQUEST['post_format'] )
 
542
                $terms = array();
 
543
        elseif ( $term = get_term_by( 'slug', 'post-format-' . sanitize_key( $_REQUEST['post_format'] ), 'post_format' ) )
 
544
                $terms = array( $term ); // Can only have one post format
 
545
 
 
546
        return $terms;
 
547
}
 
548
 
 
549
/**
 
550
 * Gets the post revision version.
 
551
 *
 
552
 * @since 3.6.0
 
553
 * @access private
 
554
*/
 
555
function _wp_get_post_revision_version( $revision ) {
 
556
        if ( is_object( $revision ) )
 
557
                $revision = get_object_vars( $revision );
 
558
        elseif ( !is_array( $revision ) )
 
559
                return false;
 
560
 
 
561
        if ( preg_match( '/^\d+-(?:autosave|revision)-v(\d+)$/', $revision['post_name'], $matches ) )
 
562
                return (int) $matches[1];
 
563
 
 
564
        return 0;
 
565
}
 
566
 
 
567
/**
 
568
 * Upgrade the revisions author, add the current post as a revision and set the revisions version to 1
 
569
 *
 
570
 * @since 3.6.0
 
571
 * @access private
 
572
 *
 
573
 * @uses wp_get_post_revisions()
 
574
 *
 
575
 * @param object $post Post object
 
576
 * @param array $revisions Current revisions of the post
 
577
 * @return bool true if the revisions were upgraded, false if problems
 
578
 */
 
579
function _wp_upgrade_revisions_of_post( $post, $revisions ) {
 
580
        global $wpdb;
 
581
 
 
582
        // Add post option exclusively
 
583
        $lock = "revision-upgrade-{$post->ID}";
 
584
        $now = time();
 
585
        $result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'no') /* LOCK */", $lock, $now ) );
 
586
        if ( ! $result ) {
 
587
                // If we couldn't get a lock, see how old the previous lock is
 
588
                $locked = get_option( $lock );
 
589
                if ( ! $locked ) {
 
590
                        // Can't write to the lock, and can't read the lock.
 
591
                        // Something broken has happened
 
592
                        return false;
 
593
                }
 
594
 
 
595
                if ( $locked > $now - 3600 ) {
 
596
                        // Lock is not too old: some other process may be upgrading this post.  Bail.
 
597
                        return false;
 
598
                }
 
599
 
 
600
                // Lock is too old - update it (below) and continue
 
601
        }
 
602
 
 
603
        // If we could get a lock, re-"add" the option to fire all the correct filters.
 
604
        update_option( $lock, $now );
 
605
 
 
606
        reset( $revisions );
 
607
        $add_last = true;
 
608
 
 
609
        do {
 
610
                $this_revision = current( $revisions );
 
611
                $prev_revision = next( $revisions );
 
612
 
 
613
                $this_revision_version = _wp_get_post_revision_version( $this_revision );
 
614
 
 
615
                // Something terrible happened
 
616
                if ( false === $this_revision_version )
 
617
                        continue;
 
618
 
 
619
                // 1 is the latest revision version, so we're already up to date.
 
620
                // No need to add a copy of the post as latest revision.
 
621
                if ( 0 < $this_revision_version ) {
 
622
                        $add_last = false;
 
623
                        continue;
 
624
                }
 
625
 
 
626
                // Always update the revision version
 
627
                $update = array(
 
628
                        'post_name' => preg_replace( '/^(\d+-(?:autosave|revision))[\d-]*$/', '$1-v1', $this_revision->post_name ),
 
629
                );
 
630
 
 
631
                // If this revision is the oldest revision of the post, i.e. no $prev_revision,
 
632
                // the correct post_author is probably $post->post_author, but that's only a good guess.
 
633
                // Update the revision version only and Leave the author as-is.
 
634
                if ( $prev_revision ) {
 
635
                        $prev_revision_version = _wp_get_post_revision_version( $prev_revision );
 
636
 
 
637
                        // If the previous revision is already up to date, it no longer has the information we need :(
 
638
                        if ( $prev_revision_version < 1 )
 
639
                                $update['post_author'] = $prev_revision->post_author;
 
640
                }
 
641
 
 
642
                // Upgrade this revision
 
643
                $result = $wpdb->update( $wpdb->posts, $update, array( 'ID' => $this_revision->ID ) );
 
644
 
 
645
                if ( $result )
 
646
                        wp_cache_delete( $this_revision->ID, 'posts' );
 
647
 
 
648
        } while ( $prev_revision );
 
649
 
 
650
        delete_option( $lock );
 
651
 
 
652
        // Add a copy of the post as latest revision.
 
653
        if ( $add_last )
 
654
                wp_save_post_revision( $post->ID );
 
655
 
 
656
        return true;
 
657
}