revision.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * Revisions administration panel.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /** WordPress Administration Bootstrap */
  9. require_once('./admin.php');
  10. wp_enqueue_script('list-revisions');
  11. wp_reset_vars(array('revision', 'left', 'right', 'action'));
  12. $revision_id = absint($revision);
  13. $left = absint($left);
  14. $right = absint($right);
  15. $redirect = 'edit.php';
  16. switch ( $action ) :
  17. case 'restore' :
  18. if ( !$revision = wp_get_post_revision( $revision_id ) )
  19. break;
  20. if ( !current_user_can( 'edit_post', $revision->post_parent ) )
  21. break;
  22. if ( !$post = get_post( $revision->post_parent ) )
  23. break;
  24. // Revisions disabled and we're not looking at an autosave
  25. if ( ( ! WP_POST_REVISIONS || !post_type_supports($post->post_type, 'revisions') ) && !wp_is_post_autosave( $revision ) ) {
  26. $redirect = 'edit.php?post_type=' . $post->post_type;
  27. break;
  28. }
  29. check_admin_referer( "restore-post_$post->ID|$revision->ID" );
  30. wp_restore_post_revision( $revision->ID );
  31. $redirect = add_query_arg( array( 'message' => 5, 'revision' => $revision->ID ), get_edit_post_link( $post->ID, 'url' ) );
  32. break;
  33. case 'diff' :
  34. if ( !$left_revision = get_post( $left ) )
  35. break;
  36. if ( !$right_revision = get_post( $right ) )
  37. break;
  38. if ( !current_user_can( 'read_post', $left_revision->ID ) || !current_user_can( 'read_post', $right_revision->ID ) )
  39. break;
  40. // If we're comparing a revision to itself, redirect to the 'view' page for that revision or the edit page for that post
  41. if ( $left_revision->ID == $right_revision->ID ) {
  42. $redirect = get_edit_post_link( $left_revision->ID );
  43. include( './js/revisions-js.php' );
  44. break;
  45. }
  46. // Don't allow reverse diffs?
  47. if ( strtotime($right_revision->post_modified_gmt) < strtotime($left_revision->post_modified_gmt) ) {
  48. $redirect = add_query_arg( array( 'left' => $right, 'right' => $left ) );
  49. break;
  50. }
  51. if ( $left_revision->ID == $right_revision->post_parent ) // right is a revision of left
  52. $post =& $left_revision;
  53. elseif ( $left_revision->post_parent == $right_revision->ID ) // left is a revision of right
  54. $post =& $right_revision;
  55. elseif ( $left_revision->post_parent == $right_revision->post_parent ) // both are revisions of common parent
  56. $post = get_post( $left_revision->post_parent );
  57. else
  58. break; // Don't diff two unrelated revisions
  59. if ( ! WP_POST_REVISIONS || !post_type_supports($post->post_type, 'revisions') ) { // Revisions disabled
  60. if (
  61. // we're not looking at an autosave
  62. ( !wp_is_post_autosave( $left_revision ) && !wp_is_post_autosave( $right_revision ) )
  63. ||
  64. // we're not comparing an autosave to the current post
  65. ( $post->ID !== $left_revision->ID && $post->ID !== $right_revision->ID )
  66. ) {
  67. $redirect = 'edit.php?post_type=' . $post->post_type;
  68. break;
  69. }
  70. }
  71. if (
  72. // They're the same
  73. $left_revision->ID == $right_revision->ID
  74. ||
  75. // Neither is a revision
  76. ( !wp_get_post_revision( $left_revision->ID ) && !wp_get_post_revision( $right_revision->ID ) )
  77. )
  78. break;
  79. $post_title = '<a href="' . get_edit_post_link() . '">' . get_the_title() . '</a>';
  80. $h2 = sprintf( __( 'Compare Revisions of &#8220;%1$s&#8221;' ), $post_title );
  81. $title = __( 'Revisions' );
  82. $left = $left_revision->ID;
  83. $right = $right_revision->ID;
  84. $redirect = false;
  85. break;
  86. case 'view' :
  87. default :
  88. if ( !$revision = wp_get_post_revision( $revision_id ) )
  89. break;
  90. if ( !$post = get_post( $revision->post_parent ) )
  91. break;
  92. if ( !current_user_can( 'read_post', $revision->ID ) || !current_user_can( 'read_post', $post->ID ) )
  93. break;
  94. // Revisions disabled and we're not looking at an autosave
  95. if ( ( ! WP_POST_REVISIONS || !post_type_supports($post->post_type, 'revisions') ) && !wp_is_post_autosave( $revision ) ) {
  96. $redirect = 'edit.php?post_type=' . $post->post_type;
  97. break;
  98. }
  99. $post_title = '<a href="' . get_edit_post_link() . '">' . get_the_title() . '</a>';
  100. $revision_title = wp_post_revision_title( $revision, false );
  101. $h2 = sprintf( __( 'Revision for &#8220;%1$s&#8221; created on %2$s' ), $post_title, $revision_title );
  102. $title = __( 'Revisions' );
  103. // Sets up the diff radio buttons
  104. $left = $revision->ID;
  105. $right = $post->ID;
  106. $redirect = false;
  107. break;
  108. endswitch;
  109. // Empty post_type means either malformed object found, or no valid parent was found.
  110. if ( !$redirect && empty($post->post_type) )
  111. $redirect = 'edit.php';
  112. if ( !empty($redirect) ) {
  113. wp_redirect( $redirect );
  114. exit;
  115. }
  116. // This is so that the correct "Edit" menu item is selected.
  117. if ( !empty($post->post_type) && 'post' != $post->post_type )
  118. $parent_file = $submenu_file = 'edit.php?post_type=' . $post->post_type;
  119. else
  120. $parent_file = $submenu_file = 'edit.php';
  121. require_once( './admin-header.php' );
  122. ?>
  123. <div class="wrap">
  124. <h2 class="long-header"><?php echo $h2; ?></h2>
  125. <table class="form-table ie-fixed">
  126. <col class="th" />
  127. <?php if ( 'diff' == $action ) : ?>
  128. <tr id="revision">
  129. <th scope="row"></th>
  130. <th scope="col" class="th-full">
  131. <span class="alignleft"><?php printf( __('Older: %s'), wp_post_revision_title( $left_revision ) ); ?></span>
  132. <span class="alignright"><?php printf( __('Newer: %s'), wp_post_revision_title( $right_revision ) ); ?></span>
  133. </th>
  134. </tr>
  135. <?php endif;
  136. // use get_post_to_edit filters?
  137. $identical = true;
  138. foreach ( _wp_post_revision_fields() as $field => $field_title ) :
  139. if ( 'diff' == $action ) {
  140. $left_content = apply_filters( "_wp_post_revision_field_$field", $left_revision->$field, $field );
  141. $right_content = apply_filters( "_wp_post_revision_field_$field", $right_revision->$field, $field );
  142. if ( !$content = wp_text_diff( $left_content, $right_content ) )
  143. continue; // There is no difference between left and right
  144. $identical = false;
  145. } else {
  146. add_filter( "_wp_post_revision_field_$field", 'htmlspecialchars' );
  147. $content = apply_filters( "_wp_post_revision_field_$field", $revision->$field, $field );
  148. }
  149. ?>
  150. <tr id="revision-field-<?php echo $field; ?>">
  151. <th scope="row"><?php echo esc_html( $field_title ); ?></th>
  152. <td><div class="pre"><?php echo $content; ?></div></td>
  153. </tr>
  154. <?php
  155. endforeach;
  156. if ( 'diff' == $action && $identical ) :
  157. ?>
  158. <tr><td colspan="2"><div class="updated"><p><?php _e( 'These revisions are identical.' ); ?></p></div></td></tr>
  159. <?php
  160. endif;
  161. ?>
  162. </table>
  163. <br class="clear" />
  164. <h3><?php echo $title; ?></h3>
  165. <?php
  166. $args = array( 'format' => 'form-table', 'parent' => true, 'right' => $right, 'left' => $left );
  167. if ( ! WP_POST_REVISIONS || !post_type_supports($post->post_type, 'revisions') )
  168. $args['type'] = 'autosave';
  169. wp_list_post_revisions( $post, $args );
  170. ?>
  171. </div>
  172. <?php
  173. require_once( './admin-footer.php' );