wp_get_revision_ui_diff

Get the revision UI diff.

Description

(array|bool) wp_get_revision_ui_diff( (object|int) $post, (int) $compare_from, (int) $compare_to ); 

Returns (array|bool)

Associative array of a post's revisioned fields and their diffs. Or, false on failure.

Parameters (3)

0. $post (object|int)
The post object. Also accepts a post ID.
1. $compare_from (int)
The revision ID to compare from.
2. $compare_to (int)
The revision ID to come to.

Usage

  1. if ( !function_exists( 'wp_get_revision_ui_diff' ) ) { 
  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 revision ID to compare from. 
  8. $compare_from = -1; 
  9.  
  10. // The revision ID to come to. 
  11. $compare_to = -1; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = wp_get_revision_ui_diff($post, $compare_from, $compare_to); 
  15.  

Defined (1)

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

/wp-admin/includes/revision.php  
  1. function wp_get_revision_ui_diff( $post, $compare_from, $compare_to ) { 
  2. if ( ! $post = get_post( $post ) ) 
  3. return false; 
  4.  
  5. if ( $compare_from ) { 
  6. if ( ! $compare_from = get_post( $compare_from ) ) 
  7. return false; 
  8. } else { 
  9. // If we're dealing with the first revision... 
  10. $compare_from = false; 
  11.  
  12. if ( ! $compare_to = get_post( $compare_to ) ) 
  13. return false; 
  14.  
  15. // If comparing revisions, make sure we're dealing with the right post parent. 
  16. // The parent post may be a 'revision' when revisions are disabled and we're looking at autosaves. 
  17. if ( $compare_from && $compare_from->post_parent !== $post->ID && $compare_from->ID !== $post->ID ) 
  18. return false; 
  19. if ( $compare_to->post_parent !== $post->ID && $compare_to->ID !== $post->ID ) 
  20. return false; 
  21.  
  22. if ( $compare_from && strtotime( $compare_from->post_date_gmt ) > strtotime( $compare_to->post_date_gmt ) ) { 
  23. $temp = $compare_from; 
  24. $compare_from = $compare_to; 
  25. $compare_to = $temp; 
  26.  
  27. // Add default title if title field is empty 
  28. if ( $compare_from && empty( $compare_from->post_title ) ) 
  29. $compare_from->post_title = __( '(no title)' ); 
  30. if ( empty( $compare_to->post_title ) ) 
  31. $compare_to->post_title = __( '(no title)' ); 
  32.  
  33. $return = array(); 
  34.  
  35. foreach ( _wp_post_revision_fields( $post ) as $field => $name ) { 
  36. /** 
  37. * Contextually filter a post revision field. 
  38. * The dynamic portion of the hook name, `$field`, corresponds to each of the post 
  39. * fields of the revision object being iterated over in a foreach statement. 
  40. * @since 3.6.0 
  41. * @param string $compare_from->$field The current revision field to compare to or from. 
  42. * @param string $field The current revision field. 
  43. * @param WP_Post $compare_from The revision post object to compare to or from. 
  44. * @param string null The context of whether the current revision is the old 
  45. * or the new one. Values are 'to' or 'from'. 
  46. */ 
  47. $content_from = $compare_from ? apply_filters( "_wp_post_revision_field_{$field}", $compare_from->$field, $field, $compare_from, 'from' ) : ''; 
  48.  
  49. /** This filter is documented in wp-admin/includes/revision.php */ 
  50. $content_to = apply_filters( "_wp_post_revision_field_{$field}", $compare_to->$field, $field, $compare_to, 'to' ); 
  51.  
  52. $args = array( 
  53. 'show_split_view' => true 
  54. ); 
  55.  
  56. /** 
  57. * Filters revisions text diff options. 
  58. * Filters the options passed to wp_text_diff() when viewing a post revision. 
  59. * @since 4.1.0 
  60. * @param array $args { 
  61. * Associative array of options to pass to wp_text_diff(). 
  62. * @type bool $show_split_view True for split view (two columns), false for 
  63. * un-split view (single column). Default true. 
  64. * } 
  65. * @param string $field The current revision field. 
  66. * @param WP_Post $compare_from The revision post to compare from. 
  67. * @param WP_Post $compare_to The revision post to compare to. 
  68. */ 
  69. $args = apply_filters( 'revision_text_diff_options', $args, $field, $compare_from, $compare_to ); 
  70.  
  71. $diff = wp_text_diff( $content_from, $content_to, $args ); 
  72.  
  73. if ( ! $diff && 'post_title' === $field ) { 
  74. // It's a better user experience to still show the Title, even if it didn't change. 
  75. // No, you didn't see this. 
  76. $diff = '<table class="diff"><colgroup><col class="content diffsplit left"><col class="content diffsplit middle"><col class="content diffsplit right"></colgroup><tbody><tr>'; 
  77. $diff .= '<td>' . esc_html( $compare_from->post_title ) . '</td><td></td><td>' . esc_html( $compare_to->post_title ) . '</td>'; 
  78. $diff .= '</tr></tbody>'; 
  79. $diff .= '</table>'; 
  80.  
  81. if ( $diff ) { 
  82. $return[] = array( 
  83. 'id' => $field,  
  84. 'name' => $name,  
  85. 'diff' => $diff,  
  86. ); 
  87.  
  88. /** 
  89. * Filters the fields displayed in the post revision diff UI. 
  90. * @since 4.1.0 
  91. * @param array $return Revision UI fields. Each item is an array of id, name and diff. 
  92. * @param WP_Post $compare_from The revision post to compare from. 
  93. * @param WP_Post $compare_to The revision post to compare to. 
  94. */ 
  95. return apply_filters( 'wp_get_revision_ui_diff', $return, $compare_from, $compare_to ); 
  96.