revision.php 16 KB

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