wp_prepare_revisions_for_js

Prepare revisions for JavaScript.

Description

(array) wp_prepare_revisions_for_js( (object|int) $post, (int) $selected_revision_id, (null) $from = null ); 

Returns (array)

An associative array of revision data and related settings.

Parameters (3)

0. $post (object|int)
The post object. Also accepts a post ID.
1. $selected_revision_id (int)
The selected revision id.
2. $from — Optional. (null) => null
The revision ID to compare from.

Usage

  1. if ( !function_exists( 'wp_prepare_revisions_for_js' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/revision.php'; 
  3.  
  4. // The post object. Also accepts a post ID. 
  5. $post = null; 
  6.  
  7. // The selected revision id. 
  8. $selected_revision_id = -1; 
  9.  
  10. // Optional. The revision ID to compare from. 
  11. $from = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = wp_prepare_revisions_for_js($post, $selected_revision_id, $from); 
  15.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/revision.php  
  1. function wp_prepare_revisions_for_js( $post, $selected_revision_id, $from = null ) { 
  2. $post = get_post( $post ); 
  3. $authors = array(); 
  4. $now_gmt = time(); 
  5.  
  6. $revisions = wp_get_post_revisions( $post->ID, array( 'order' => 'ASC', 'check_enabled' => false ) ); 
  7. // If revisions are disabled, we only want autosaves and the current post. 
  8. if ( ! wp_revisions_enabled( $post ) ) { 
  9. foreach ( $revisions as $revision_id => $revision ) { 
  10. if ( ! wp_is_post_autosave( $revision ) ) 
  11. unset( $revisions[ $revision_id ] ); 
  12. $revisions = array( $post->ID => $post ) + $revisions; 
  13.  
  14. $show_avatars = get_option( 'show_avatars' ); 
  15.  
  16. cache_users( wp_list_pluck( $revisions, 'post_author' ) ); 
  17.  
  18. $can_restore = current_user_can( 'edit_post', $post->ID ); 
  19. $current_id = false; 
  20.  
  21. foreach ( $revisions as $revision ) { 
  22. $modified = strtotime( $revision->post_modified ); 
  23. $modified_gmt = strtotime( $revision->post_modified_gmt ); 
  24. if ( $can_restore ) { 
  25. $restore_link = str_replace( '&', '&', wp_nonce_url
  26. array( 'revision' => $revision->ID,  
  27. 'action' => 'restore' ),  
  28. admin_url( 'revision.php' ) 
  29. ),  
  30. "restore-post_{$revision->ID}" 
  31. ) ); 
  32.  
  33. if ( ! isset( $authors[ $revision->post_author ] ) ) { 
  34. $authors[ $revision->post_author ] = array( 
  35. 'id' => (int) $revision->post_author,  
  36. 'avatar' => $show_avatars ? get_avatar( $revision->post_author, 32 ) : '',  
  37. 'name' => get_the_author_meta( 'display_name', $revision->post_author ),  
  38. ); 
  39.  
  40. $autosave = (bool) wp_is_post_autosave( $revision ); 
  41. $current = ! $autosave && $revision->post_modified_gmt === $post->post_modified_gmt; 
  42. if ( $current && ! empty( $current_id ) ) { 
  43. // If multiple revisions have the same post_modified_gmt, highest ID is current. 
  44. if ( $current_id < $revision->ID ) { 
  45. $revisions[ $current_id ]['current'] = false; 
  46. $current_id = $revision->ID; 
  47. } else { 
  48. $current = false; 
  49. } elseif ( $current ) { 
  50. $current_id = $revision->ID; 
  51.  
  52. $revisions_data = array( 
  53. 'id' => $revision->ID,  
  54. 'title' => get_the_title( $post->ID ),  
  55. 'author' => $authors[ $revision->post_author ],  
  56. 'date' => date_i18n( __( 'M j, Y @ H:i' ), $modified ),  
  57. 'dateShort' => date_i18n( _x( 'j M @ H:i', 'revision date short format' ), $modified ),  
  58. 'timeAgo' => sprintf( __( '%s ago' ), human_time_diff( $modified_gmt, $now_gmt ) ),  
  59. 'autosave' => $autosave,  
  60. 'current' => $current,  
  61. 'restoreUrl' => $can_restore ? $restore_link : false,  
  62. ); 
  63.  
  64. /** 
  65. * Filters the array of revisions used on the revisions screen. 
  66. * @since 4.4.0 
  67. * @param array $revisions_data { 
  68. * The bootstrapped data for the revisions screen. 
  69. * @type int $id Revision ID. 
  70. * @type string $title Title for the revision's parent WP_Post object. 
  71. * @type int $author Revision post author ID. 
  72. * @type string $date Date the revision was modified. 
  73. * @type string $dateShort Short-form version of the date the revision was modified. 
  74. * @type string $timeAgo GMT-aware amount of time ago the revision was modified. 
  75. * @type bool $autosave Whether the revision is an autosave. 
  76. * @type bool $current Whether the revision is both not an autosave and the post 
  77. * modified date matches the revision modified date (GMT-aware). 
  78. * @type bool|false $restoreUrl URL if the revision can be restored, false otherwise. 
  79. * } 
  80. * @param WP_Post $revision The revision's WP_Post object. 
  81. * @param WP_Post $post The revision's parent WP_Post object. 
  82. */ 
  83. $revisions[ $revision->ID ] = apply_filters( 'wp_prepare_revision_for_js', $revisions_data, $revision, $post ); 
  84.  
  85. /** 
  86. * If we only have one revision, the initial revision is missing; This happens 
  87. * when we have an autsosave and the user has clicked 'View the Autosave' 
  88. */ 
  89. if ( 1 === sizeof( $revisions ) ) { 
  90. $revisions[ $post->ID ] = array( 
  91. 'id' => $post->ID,  
  92. 'title' => get_the_title( $post->ID ),  
  93. 'author' => $authors[ $post->post_author ],  
  94. 'date' => date_i18n( __( 'M j, Y @ H:i' ), strtotime( $post->post_modified ) ),  
  95. 'dateShort' => date_i18n( _x( 'j M @ H:i', 'revision date short format' ), strtotime( $post->post_modified ) ),  
  96. 'timeAgo' => sprintf( __( '%s ago' ), human_time_diff( strtotime( $post->post_modified_gmt ), $now_gmt ) ),  
  97. 'autosave' => false,  
  98. 'current' => true,  
  99. 'restoreUrl' => false,  
  100. ); 
  101. $current_id = $post->ID; 
  102.  
  103. /** 
  104. * If a post has been saved since the last revision (no revisioned fields 
  105. * were changed), we may not have a "current" revision. Mark the latest 
  106. * revision as "current". 
  107. */ 
  108. if ( empty( $current_id ) ) { 
  109. if ( $revisions[ $revision->ID ]['autosave'] ) { 
  110. $revision = end( $revisions ); 
  111. while ( $revision['autosave'] ) { 
  112. $revision = prev( $revisions ); 
  113. $current_id = $revision['id']; 
  114. } else { 
  115. $current_id = $revision->ID; 
  116. $revisions[ $current_id ]['current'] = true; 
  117.  
  118. // Now, grab the initial diff. 
  119. $compare_two_mode = is_numeric( $from ); 
  120. if ( ! $compare_two_mode ) { 
  121. $found = array_search( $selected_revision_id, array_keys( $revisions ) ); 
  122. if ( $found ) { 
  123. $from = array_keys( array_slice( $revisions, $found - 1, 1, true ) ); 
  124. $from = reset( $from ); 
  125. } else { 
  126. $from = 0; 
  127.  
  128. $from = absint( $from ); 
  129.  
  130. $diffs = array( array( 
  131. 'id' => $from . ':' . $selected_revision_id,  
  132. 'fields' => wp_get_revision_ui_diff( $post->ID, $from, $selected_revision_id ),  
  133. )); 
  134.  
  135. return array( 
  136. 'postId' => $post->ID,  
  137. 'nonce' => wp_create_nonce( 'revisions-ajax-nonce' ),  
  138. 'revisionData' => array_values( $revisions ),  
  139. 'to' => $selected_revision_id,  
  140. 'from' => $from,  
  141. 'diffData' => $diffs,  
  142. 'baseUrl' => parse_url( admin_url( 'revision.php' ), PHP_URL_PATH ),  
  143. 'compareTwoMode' => absint( $compare_two_mode ), // Apparently booleans are not allowed 
  144. 'revisionIds' => array_keys( $revisions ),  
  145. );