/wp-admin/includes/revision.php

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