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

Built from https://develop.svn.wordpress.org/trunk@30396


git-svn-id: http://core.svn.wordpress.org/trunk@30392 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
John Blackbourn 2014-11-19 23:21:23 +00:00
parent 64cd0ae134
commit 3e3c383e79
2 changed files with 51 additions and 7 deletions

View File

@ -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 */ /** 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' ); $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 ) { if ( ! $diff && 'post_title' === $field ) {
// It's a better user experience to still show the Title, even if it didn't change. // It's a better user experience to still show the Title, even if it didn't change.

View File

@ -153,8 +153,25 @@ class WP_Text_Diff_Renderer_Table extends Text_Diff_Renderer {
public function _added( $lines, $encode = true ) { public function _added( $lines, $encode = true ) {
$r = ''; $r = '';
foreach ($lines as $line) { foreach ($lines as $line) {
if ( $encode ) if ( $encode ) {
$line = htmlspecialchars( $line ); $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 ) { if ( $this->_show_split_view ) {
$r .= '<tr>' . $this->emptyLine() . $this->emptyLine() . $this->addedLine( $line ) . "</tr>\n"; $r .= '<tr>' . $this->emptyLine() . $this->emptyLine() . $this->addedLine( $line ) . "</tr>\n";
} else { } else {
@ -175,8 +192,11 @@ class WP_Text_Diff_Renderer_Table extends Text_Diff_Renderer {
public function _deleted( $lines, $encode = true ) { public function _deleted( $lines, $encode = true ) {
$r = ''; $r = '';
foreach ($lines as $line) { foreach ($lines as $line) {
if ( $encode ) if ( $encode ) {
$line = htmlspecialchars( $line ); $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 ) { if ( $this->_show_split_view ) {
$r .= '<tr>' . $this->deletedLine( $line ) . $this->emptyLine() . $this->emptyLine() . "</tr>\n"; $r .= '<tr>' . $this->deletedLine( $line ) . $this->emptyLine() . $this->emptyLine() . "</tr>\n";
} else { } else {
@ -198,8 +218,11 @@ class WP_Text_Diff_Renderer_Table extends Text_Diff_Renderer {
public function _context( $lines, $encode = true ) { public function _context( $lines, $encode = true ) {
$r = ''; $r = '';
foreach ($lines as $line) { foreach ($lines as $line) {
if ( $encode ) if ( $encode ) {
$line = htmlspecialchars( $line ); $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 ) { if ( $this->_show_split_view ) {
$r .= '<tr>' . $this->contextLine( $line ) . $this->emptyLine() . $this->contextLine( $line ) . "</tr>\n"; $r .= '<tr>' . $this->contextLine( $line ) . $this->emptyLine() . $this->contextLine( $line ) . "</tr>\n";
} else { } else {