From b670f503e529462414ac3cb55a1bd6002035f903 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Wed, 19 Nov 2014 23:20:07 +0000 Subject: [PATCH] Introduce two new filters to the post revisions screen: * `process_text_diff_html` for contextually filtering a diffed line. Allows for the line to be processed in a different manner to the default `htmlspecialchars`. * `revision_text_diff_options` for filtering the options passed to `wp_text_diff()` when viewing a post revision. Fixes #24908 Props adamsilverstein git-svn-id: https://develop.svn.wordpress.org/trunk@30396 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/revision.php | 23 +++++++++++++++++++- src/wp-includes/wp-diff.php | 35 +++++++++++++++++++++++++----- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/src/wp-admin/includes/revision.php b/src/wp-admin/includes/revision.php index bf0279651c..164129d10f 100644 --- a/src/wp-admin/includes/revision.php +++ b/src/wp-admin/includes/revision.php @@ -73,7 +73,28 @@ function wp_get_revision_ui_diff( $post, $compare_from, $compare_to ) { /** This filter is documented in wp-admin/includes/revision.php */ $content_to = apply_filters( "_wp_post_revision_field_$field", $compare_to->$field, $field, $compare_to, 'to' ); - $diff = wp_text_diff( $content_from, $content_to, array( 'show_split_view' => true ) ); + $args = array( + 'show_split_view' => false + ); + /** + * Filter revisions text diff options. + * + * Filter the options passed to `wp_text_diff()` when viewing a post revision. + * + * @since 4.1.0 + * + * @param array $args { + * Associative array of options to pass to `wp_text_diff()`. + * + * @type bool $show_split_view False for split view (two columns), true for + * un-split view (single column). Default false. + * } + * @param string $field The current revision field. + * @param WP_Post $compare_from The revision post to compare from. + * @param WP_Post $compare_to The revision post to compare to. + */ + $args = apply_filters( 'revision_text_diff_options', $args, $field, $compare_from, $compare_to ); + $diff = wp_text_diff( $content_from, $content_to, $args ); if ( ! $diff && 'post_title' === $field ) { // It's a better user experience to still show the Title, even if it didn't change. diff --git a/src/wp-includes/wp-diff.php b/src/wp-includes/wp-diff.php index 97e7eb9bb5..9b1b5e093a 100644 --- a/src/wp-includes/wp-diff.php +++ b/src/wp-includes/wp-diff.php @@ -153,8 +153,25 @@ class WP_Text_Diff_Renderer_Table extends Text_Diff_Renderer { public function _added( $lines, $encode = true ) { $r = ''; foreach ($lines as $line) { - if ( $encode ) - $line = htmlspecialchars( $line ); + if ( $encode ) { + $processed_line = htmlspecialchars( $line ); + + /** + * Contextually filter a diffed line. + * + * Filters TextDiff processing of diffed line. By default, diffs are processed with + * htmlspecialchars. Use this filter to remove or change the processing. Passes a context + * indicating if the line is added, deleted or unchanged. + * + * @since 4.1.0 + * + * @param String $processed_line The processed diffed line. + * @param String $line The unprocessed diffed line. + * @param string null The line context. Values are 'added', 'deleted' or 'unchanged'. + */ + $line = apply_filters( 'process_text_diff_html', $processed_line, $line, 'added' ); + } + if ( $this->_show_split_view ) { $r .= '' . $this->emptyLine() . $this->emptyLine() . $this->addedLine( $line ) . "\n"; } else { @@ -175,8 +192,11 @@ class WP_Text_Diff_Renderer_Table extends Text_Diff_Renderer { public function _deleted( $lines, $encode = true ) { $r = ''; foreach ($lines as $line) { - if ( $encode ) - $line = htmlspecialchars( $line ); + if ( $encode ) { + $processed_line = htmlspecialchars( $line ); + /** This filter is documented in wp-includes/wp-diff.php */ + $line = apply_filters( 'process_text_diff_html', $processed_line, $line, 'deleted' ); + } if ( $this->_show_split_view ) { $r .= '' . $this->deletedLine( $line ) . $this->emptyLine() . $this->emptyLine() . "\n"; } else { @@ -198,8 +218,11 @@ class WP_Text_Diff_Renderer_Table extends Text_Diff_Renderer { public function _context( $lines, $encode = true ) { $r = ''; foreach ($lines as $line) { - if ( $encode ) - $line = htmlspecialchars( $line ); + if ( $encode ) { + $processed_line = htmlspecialchars( $line ); + /** This filter is documented in wp-includes/wp-diff.php */ + $line = apply_filters( 'process_text_diff_html', $processed_line, $line, 'unchanged' ); + } if ( $this->_show_split_view ) { $r .= '' . $this->contextLine( $line ) . $this->emptyLine() . $this->contextLine( $line ) . "\n"; } else {