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