PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-admin/includes/revision.php

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