1<?php
2/**
3 * WordPress Administration Revisions API
4 *
5 * @package WordPress
6 * @subpackage Administration
7 * @since 3.6.0
8 */
9
10/**
11 * Get the revision UI diff.
12 *
13 * @since 3.6.0
14 *
15 * @param WP_Post|int $post The post object or post ID.
16 * @param int $compare_from The revision ID to compare from.
17 * @param int $compare_to The revision ID to come to.
18 * @return array|false Associative array of a post's revisioned fields and their diffs.
19 * Or, false on failure.
20 */
21function wp_get_revision_ui_diff( $post, $compare_from, $compare_to ) {
22 $post = get_post( $post );
23 if ( ! $post ) {
24 return false;
25 }
26
27 if ( $compare_from ) {
28 $compare_from = get_post( $compare_from );
29 if ( ! $compare_from ) {
30 return false;
31 }
32 } else {
33 // If we're dealing with the first revision...
34 $compare_from = false;
35 }
36
37 $compare_to = get_post( $compare_to );
38 if ( ! $compare_to ) {
39 return false;
40 }
41
42 /*
43 * If comparing revisions, make sure we are dealing with the right post parent.
44 * The parent post may be a 'revision' when revisions are disabled and we're looking at autosaves.
45 */
46 if ( $compare_from && $compare_from->post_parent !== $post->ID && $compare_from->ID !== $post->ID ) {
47 return false;
48 }
49 if ( $compare_to->post_parent !== $post->ID && $compare_to->ID !== $post->ID ) {
50 return false;
51 }
52
53 if ( $compare_from && strtotime( $compare_from->post_date_gmt ) > strtotime( $compare_to->post_date_gmt ) ) {
54 $temp = $compare_from;
55 $compare_from = $compare_to;
56 $compare_to = $temp;
57 }
58
59 // Add default title if title field is empty.
60 if ( $compare_from && empty( $compare_from->post_title ) ) {
61 $compare_from->post_title = __( '(no title)' );
62 }
63 if ( empty( $compare_to->post_title ) ) {
64 $compare_to->post_title = __( '(no title)' );
65 }
66
67 $return = array();
68
69 foreach ( _wp_post_revision_fields( $post ) as $field => $name ) {
70 /**
71 * Contextually filter a post revision field.
72 *
73 * The dynamic portion of the hook name, `$field`, corresponds to a name of a
74 * field of the revision object.
75 *
76 * Possible hook names include:
77 *
78 * - `_wp_post_revision_field_post_title`
79 * - `_wp_post_revision_field_post_content`
80 * - `_wp_post_revision_field_post_excerpt`
81 *
82 * @since 3.6.0
83 *
84 * @param string $revision_field The current revision field to compare to or from.
85 * @param string $field The current revision field.
86 * @param WP_Post $compare_from The revision post object to compare to or from.
87 * @param string $context The context of whether the current revision is the old
88 * or the new one. Either 'to' or 'from'.
89 */
90 $content_from = $compare_from ? apply_filters( "_wp_post_revision_field_{$field}", $compare_from->$field, $field, $compare_from, 'from' ) : '';
91
92 /** This filter is documented in wp-admin/includes/revision.php */
93 $content_to = apply_filters( "_wp_post_revision_field_{$field}", $compare_to->$field, $field, $compare_to, 'to' );
94
95 $args = array(
96 'show_split_view' => true,
97 'title_left' => __( 'Removed' ),
98 'title_right' => __( 'Added' ),
99 );
100
101 /**
102 * Filters revisions text diff options.
103 *
104 * Filters the options passed to wp_text_diff() when viewing a post revision.
105 *
106 * @since 4.1.0
107 *
108 * @param array $args {
109 * Associative array of options to pass to wp_text_diff().
110 *
111 * @type bool $show_split_view True for split view (two columns), false for
112 * un-split view (single column). Default true.
113 * }
114 * @param string $field The current revision field.
115 * @param WP_Post $compare_from The revision post to compare from.
116 * @param WP_Post $compare_to The revision post to compare to.
117 */
118 $args = apply_filters( 'revision_text_diff_options', $args, $field, $compare_from, $compare_to );
119
120 $diff = wp_text_diff( $content_from, $content_to, $args );
121
122 if ( ! $diff && 'post_title' === $field ) {
123 /*
124 * It's a better user experience to still show the Title, even if it didn't change.
125 * No, you didn't see this.
126 */
127 $diff = '<table class="diff"><colgroup><col class="content diffsplit left"><col class="content diffsplit middle"><col class="content diffsplit right"></colgroup><tbody><tr>';
128
129 // In split screen mode, show the title before/after side by side.
130 if ( true === $args['show_split_view'] ) {
131 $diff .= '<td>' . esc_html( $compare_from->post_title ) . '</td><td></td><td>' . esc_html( $compare_to->post_title ) . '</td>';
132 } else {
133 $diff .= '<td>' . esc_html( $compare_from->post_title ) . '</td>';
134
135 // In single column mode, only show the title once if unchanged.
136 if ( $compare_from->post_title !== $compare_to->post_title ) {
137 $diff .= '</tr><tr><td>' . esc_html( $compare_to->post_title ) . '</td>';
138 }
139 }
140
141 $diff .= '</tr></tbody>';
142 $diff .= '</table>';
143 }
144
145 if ( $diff ) {
146 $return[] = array(
147 'id' => $field,
148 'name' => $name,
149 'diff' => $diff,
150 );
151 }
152 }
153
154 /**
155 * Filters the fields displayed in the post revision diff UI.
156 *
157 * @since 4.1.0
158 *
159 * @param array[] $return Array of revision UI fields. Each item is an array of id, name, and diff.
160 * @param WP_Post $compare_from The revision post to compare from.
161 * @param WP_Post $compare_to The revision post to compare to.
162 */
163 return apply_filters( 'wp_get_revision_ui_diff', $return, $compare_from, $compare_to );
164}
165
166/**
167 * Prepare revisions for JavaScript.
168 *
169 * @since 3.6.0
170 *
171 * @param WP_Post|int $post The post object or post ID.
172 * @param int $selected_revision_id The selected revision ID.
173 * @param int $from Optional. The revision ID to compare from.
174 * @return array An associative array of revision data and related settings.
175 */
176function wp_prepare_revisions_for_js( $post, $selected_revision_id, $from = null ) {
177 $post = get_post( $post );
178 $authors = array();
179 $now_gmt = time();
180
181 $revisions = wp_get_post_revisions(
182 $post->ID,
183 array(
184 'order' => 'ASC',
185 'check_enabled' => false,
186 )
187 );
188 // If revisions are disabled, we only want autosaves and the current post.
189 if ( ! wp_revisions_enabled( $post ) ) {
190 foreach ( $revisions as $revision_id => $revision ) {
191 if ( ! wp_is_post_autosave( $revision ) ) {
192 unset( $revisions[ $revision_id ] );
193 }
194 }
195 $revisions = array( $post->ID => $post ) + $revisions;
196 }
197
198 $show_avatars = get_option( 'show_avatars' );
199
200 update_post_author_caches( $revisions );
201
202 $can_restore = current_user_can( 'edit_post', $post->ID );
203 $current_id = false;
204
205 foreach ( $revisions as $revision ) {
206 $modified = strtotime( $revision->post_modified );
207 $modified_gmt = strtotime( $revision->post_modified_gmt . ' +0000' );
208 if ( $can_restore ) {
209 $restore_link = str_replace(
210 '&',
211 '&',
212 wp_nonce_url(
213 add_query_arg(
214 array(
215 'revision' => $revision->ID,
216 'action' => 'restore',
217 ),
218 admin_url( 'revision.php' )
219 ),
220 "restore-post_{$revision->ID}"
221 )
222 );
223 }
224
225 if ( ! isset( $authors[ $revision->post_author ] ) ) {
226 $authors[ $revision->post_author ] = array(
227 'id' => (int) $revision->post_author,
228 'avatar' => $show_avatars ? get_avatar( $revision->post_author, 32 ) : '',
229 'name' => get_the_author_meta( 'display_name', $revision->post_author ),
230 );
231 }
232
233 $autosave = (bool) wp_is_post_autosave( $revision );
234 $current = ! $autosave && $revision->post_modified_gmt === $post->post_modified_gmt;
235 if ( $current && ! empty( $current_id ) ) {
236 // If multiple revisions have the same post_modified_gmt, highest ID is current.
237 if ( $current_id < $revision->ID ) {
238 $revisions[ $current_id ]['current'] = false;
239 $current_id = $revision->ID;
240 } else {
241 $current = false;
242 }
243 } elseif ( $current ) {
244 $current_id = $revision->ID;
245 }
246
247 $revisions_data = array(
248 'id' => $revision->ID,
249 'title' => get_the_title( $post->ID ),
250 'author' => $authors[ $revision->post_author ],
251 'date' => date_i18n( __( 'M j, Y @ H:i' ), $modified ),
252 'dateShort' => date_i18n( _x( 'j M Y @ H:i', 'revision date short format' ), $modified ),
253 /* translators: %s: Human-readable time difference. */
254 'timeAgo' => sprintf( __( '%s ago' ), human_time_diff( $modified_gmt, $now_gmt ) ),
255 'autosave' => $autosave,
256 'current' => $current,
257 'restoreUrl' => $can_restore ? $restore_link : false,
258 );
259
260 /**
261 * Filters the array of revisions used on the revisions screen.
262 *
263 * @since 4.4.0
264 *
265 * @param array $revisions_data {
266 * The bootstrapped data for the revisions screen.
267 *
268 * @type int $id Revision ID.
269 * @type string $title Title for the revision's parent WP_Post object.
270 * @type int $author Revision post author ID.
271 * @type string $date Date the revision was modified.
272 * @type string $dateShort Short-form version of the date the revision was modified.
273 * @type string $timeAgo GMT-aware amount of time ago the revision was modified.
274 * @type bool $autosave Whether the revision is an autosave.
275 * @type bool $current Whether the revision is both not an autosave and the post
276 * modified date matches the revision modified date (GMT-aware).
277 * @type bool|false $restoreUrl URL if the revision can be restored, false otherwise.
278 * }
279 * @param WP_Post $revision The revision's WP_Post object.
280 * @param WP_Post $post The revision's parent WP_Post object.
281 */
282 $revisions[ $revision->ID ] = apply_filters( 'wp_prepare_revision_for_js', $revisions_data, $revision, $post );
283 }
284
285 /*
286 * If we only have one revision, the initial revision is missing. This happens
287 * when we have an autosave and the user has clicked 'View the Autosave'.
288 */
289 if ( 1 === count( $revisions ) ) {
290 $revisions[ $post->ID ] = array(
291 'id' => $post->ID,
292 'title' => get_the_title( $post->ID ),
293 'author' => $authors[ $revision->post_author ],
294 'date' => date_i18n( __( 'M j, Y @ H:i' ), strtotime( $post->post_modified ) ),
295 'dateShort' => date_i18n( _x( 'j M @ H:i', 'revision date short format' ), strtotime( $post->post_modified ) ),
296 /* translators: %s: Human-readable time difference. */
297 'timeAgo' => sprintf( __( '%s ago' ), human_time_diff( strtotime( $post->post_modified_gmt ), $now_gmt ) ),
298 'autosave' => false,
299 'current' => true,
300 'restoreUrl' => false,
301 );
302 $current_id = $post->ID;
303 }
304
305 /*
306 * If a post has been saved since the latest revision (no revisioned fields
307 * were changed), we may not have a "current" revision. Mark the latest
308 * revision as "current".
309 */
310 if ( empty( $current_id ) ) {
311 if ( $revisions[ $revision->ID ]['autosave'] ) {
312 $revision = end( $revisions );
313 while ( $revision['autosave'] ) {
314 $revision = prev( $revisions );
315 }
316 $current_id = $revision['id'];
317 } else {
318 $current_id = $revision->ID;
319 }
320 $revisions[ $current_id ]['current'] = true;
321 }
322
323 // Now, grab the initial diff.
324 $compare_two_mode = is_numeric( $from );
325 if ( ! $compare_two_mode ) {
326 $found = array_search( $selected_revision_id, array_keys( $revisions ), true );
327 if ( $found ) {
328 $from = array_keys( array_slice( $revisions, $found - 1, 1, true ) );
329 $from = reset( $from );
330 } else {
331 $from = 0;
332 }
333 }
334
335 $from = absint( $from );
336
337 $diffs = array(
338 array(
339 'id' => $from . ':' . $selected_revision_id,
340 'fields' => wp_get_revision_ui_diff( $post->ID, $from, $selected_revision_id ),
341 ),
342 );
343
344 return array(
345 'postId' => $post->ID,
346 'nonce' => wp_create_nonce( 'revisions-ajax-nonce' ),
347 'revisionData' => array_values( $revisions ),
348 'to' => $selected_revision_id,
349 'from' => $from,
350 'diffData' => $diffs,
351 'baseUrl' => parse_url( admin_url( 'revision.php' ), PHP_URL_PATH ),
352 'compareTwoMode' => absint( $compare_two_mode ), // Apparently booleans are not allowed.
353 'revisionIds' => array_keys( $revisions ),
354 );
355}
356
357/**
358 * Print JavaScript templates required for the revisions experience.
359 *
360 * @since 4.1.0
361 *
362 * @global WP_Post $post Global post object.
363 */
364function wp_print_revision_templates() {
365 global $post;
366 ?><script id="tmpl-revisions-frame" type="text/html">
367 <div class="revisions-control-frame"></div>
368 <div class="revisions-diff-frame"></div>
369 </script>
370
371 <script id="tmpl-revisions-buttons" type="text/html">
372 <div class="revisions-previous">
373 <input class="button" type="button" value="<?php echo esc_attr_x( 'Previous', 'Button label for a previous revision' ); ?>" />
374 </div>
375
376 <div class="revisions-next">
377 <input class="button" type="button" value="<?php echo esc_attr_x( 'Next', 'Button label for a next revision' ); ?>" />
378 </div>
379 </script>
380
381 <script id="tmpl-revisions-slider-hidden-help" type="text/html">
382 <h2 class="screen-reader-text"><?php esc_html_e( 'Select a revision' ); ?></h2>
383 <p id="revisions-slider-hidden-help" hidden><?php esc_html_e( 'Change revision by using the left and right arrow keys' ); ?></p>
384 </script>
385
386 <script id="tmpl-revisions-checkbox" type="text/html">
387 <div class="revision-toggle-compare-mode">
388 <label>
389 <input type="checkbox" class="compare-two-revisions"
390 <#
391 if ( 'undefined' !== typeof data && data.model.attributes.compareTwoMode ) {
392 #> checked="checked"<#
393 }
394 #>
395 />
396 <?php esc_html_e( 'Compare any two revisions' ); ?>
397 </label>
398 </div>
399 </script>
400
401 <script id="tmpl-revisions-meta" type="text/html">
402 <# if ( ! _.isUndefined( data.attributes ) ) { #>
403 <div class="diff-title">
404 <# if ( 'from' === data.type ) { #>
405 <strong id="diff-title-from"><?php _ex( 'From:', 'Followed by post revision info' ); ?></strong>
406 <# } else if ( 'to' === data.type ) { #>
407 <strong id="diff-title-to"><?php _ex( 'To:', 'Followed by post revision info' ); ?></strong>
408 <# } #>
409 <div class="author-card<# if ( data.attributes.autosave ) { #> autosave<# } #>">
410 <div>
411 {{{ data.attributes.author.avatar }}}
412 <div class="author-info" id="diff-title-author">
413 <# if ( data.attributes.autosave ) { #>
414 <span class="byline">
415 <?php
416 printf(
417 /* translators: %s: User's display name. */
418 __( 'Autosave by %s' ),
419 '<span class="author-name">{{ data.attributes.author.name }}</span>'
420 );
421 ?>
422 </span>
423 <# } else if ( data.attributes.current ) { #>
424 <span class="byline">
425 <?php
426 printf(
427 /* translators: %s: User's display name. */
428 __( 'Current Revision by %s' ),
429 '<span class="author-name">{{ data.attributes.author.name }}</span>'
430 );
431 ?>
432 </span>
433 <# } else { #>
434 <span class="byline">
435 <?php
436 printf(
437 /* translators: %s: User's display name. */
438 __( 'Revision by %s' ),
439 '<span class="author-name">{{ data.attributes.author.name }}</span>'
440 );
441 ?>
442 </span>
443 <# } #>
444 <span class="time-ago">{{ data.attributes.timeAgo }}</span>
445 <span class="date">({{ data.attributes.dateShort }})</span>
446 </div>
447 </div>
448 <# if ( 'to' === data.type && data.attributes.restoreUrl ) { #>
449 <input <?php if ( wp_check_post_lock( $post->ID ) ) { ?>
450 disabled="disabled"
451 <?php } else { ?>
452 <# if ( data.attributes.current ) { #>
453 disabled="disabled"
454 <# } #>
455 <?php } ?>
456 <# if ( data.attributes.autosave ) { #>
457 type="button" class="restore-revision button button-primary" value="<?php esc_attr_e( 'Restore This Autosave' ); ?>" />
458 <# } else { #>
459 type="button" class="restore-revision button button-primary" value="<?php esc_attr_e( 'Restore This Revision' ); ?>" />
460 <# } #>
461 <# } #>
462 </div>
463 <# if ( 'tooltip' === data.type ) { #>
464 <div class="revisions-tooltip-arrow"><span></span></div>
465 <# } #>
466 <# } #>
467 </script>
468
469 <script id="tmpl-revisions-diff" type="text/html">
470 <div class="loading-indicator"><span class="spinner"></span></div>
471 <div class="diff-error"><?php _e( 'An error occurred while loading the comparison. Please refresh the page and try again.' ); ?></div>
472 <div class="diff">
473 <# _.each( data.fields, function( field ) { #>
474 <h2>{{ field.name }}</h2>
475 {{{ field.diff }}}
476 <# }); #>
477 </div>
478 </script>
479 <?php
480}
481