Skip to content

Instantly share code, notes, and snippets.

@hadamlenz
Created December 8, 2016 14:18
Show Gist options
  • Save hadamlenz/e20c62e31348ba3d4dbf25cac3b2ed05 to your computer and use it in GitHub Desktop.
Save hadamlenz/e20c62e31348ba3d4dbf25cac3b2ed05 to your computer and use it in GitHub Desktop.
Class to display inline diff using wordpress's native text diff engine
<?php
class wp_native_inline_diffing{
public function diffcheck($oldValue, $newValue){
if ( ! class_exists( 'WP_Text_Diff_Renderer_inline', false ) )
require( ABSPATH . WPINC . '/wp-diff.php' );
$renderer = new WP_Text_Diff_Renderer_inline();
$old_lines = explode( "\n", $oldValue );
$new_lines = explode( "\n", $newValue );
$text_diff = new Text_Diff( 'auto', array( $old_lines, $new_lines ) );
$diff = $renderer->render( $text_diff );
$diff = $this->_rebuilt_diffed_html( $diff );
return $diff;
}
public function _rebuilt_diffed_html( $string ){
$string = htmlspecialchars_decode( $string );
$string = str_replace( "<del> <li>", '<li><del>', $string);
$string = str_replace( "</li></del>", '</del></li>', $string);
$string = str_replace( "<ins> <li>", '<li><ins>', $string);
$string = str_replace( "</li></ins>", '</ins></li>', $string);
return $string;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment