1. <?php
  2.  
  3. class Email_Post_Changes {
  4.     var $defaults;
  5.  
  6.     var $left_post;
  7.     var $right_post;
  8.  
  9.     var $text_diff;
  10.    
  11.     // Ov3rfly:
  12.     var $left_taxonomies;
  13.  
  14.     const ADMIN_PAGE = 'email_post_changes';
  15.     const OPTION_GROUP = 'email_post_changes';
  16.     const OPTION = 'email_post_changes';
  17.  
  18.     static function init() {
  19.         static $instance = null;
  20.  
  21.         if ( $instance )
  22.             return $instance;
  23.  
  24.         $class = __CLASS__;
  25.         $instance = new $class;
  26.         return $instance;
  27.     }
  28.  
  29.     function __construct() {
  30.         $this->defaults = apply_filters( 'email_post_changes_default_options', array(
  31.             'enable'     => 1,
  32.             'users'      => array(),
  33.             'emails'     => array( get_option( 'admin_email' ) ),
  34.             'post_types' => array( 'post', 'page' ),
  35.             'drafts'     => 0,
  36.         ) );
  37.  
  38.         $options = $this->get_options();
  39.  
  40.         if ( $options['enable'] )
  41.             add_action( 'post_updated', array( $this, 'post_updated' ), 10, 3 );
  42.        
  43.         // Ov3rfly:
  44.         if ( $options['enable'] )
  45.             add_action( 'pre_post_update', array( $this, 'pre_post_update' ), 10, 2 );         
  46.  
  47.         if ( current_user_can( 'manage_options' ) )
  48.             add_action( 'admin_menu', array( $this, 'admin_menu' ), 115 );
  49.     }
  50.  
  51.     function get_post_types() {
  52.         $post_types = get_post_types( array( 'public' => true ) );
  53.         $_post_types = array();
  54.  
  55.         foreach ( $post_types as $post_type ) {
  56.             if ( post_type_supports( $post_type, 'revisions' ) )
  57.                 $_post_types[] = $post_type;
  58.         }
  59.  
  60.         return $_post_types;
  61.     }
  62.  
  63.     function get_options( $just_defaults = false ) {
  64.         if ( $just_defaults )
  65.             return $this->defaults;
  66.  
  67.         $options = (array) get_option( 'email_post_changes' );
  68.  
  69.         return wp_parse_args( $options, $this->defaults );
  70.     }
  71.  
  72.     // Ov3rfly:
  73.     function pre_post_update( $post_id, $data ) {
  74.         $left_taxonomies = array();
  75.         if ( isset( $data['post_type'] ) ) {
  76.             $taxonomies = get_object_taxonomies( $data['post_type'], 'objects' );
  77.             foreach( $taxonomies as $taxonomy_slug => $taxonomy ) {
  78.                 $left_taxonomies[ $taxonomy_slug ] = strip_tags( get_the_term_list( $post_id, $taxonomy_slug, '', ', ', '' ) );
  79.             }
  80.         }
  81.         $this->left_taxonomies = $left_taxonomies;
  82.     }
  83.    
  84.     // The meat of the plugin
  85.     function post_updated( $post_id, $post_after, $post_before ) {
  86.         $options = $this->get_options();
  87.         // If we're purely saving a draft, and don't have the draft option enabled, skip. If we're transitioning one way or the other, send a notification.
  88.         if ( 0 == $options['drafts'] && 'draft' == $post_before->post_status && 'draft' == $post_after->post_status )
  89.             return;
  90.                    
  91.         if ( isset( $_POST['autosave'] ) )
  92.             return;
  93.  
  94.         if ( !in_array( $post_before->post_type, $options['post_types'] ) )
  95.             return;
  96.  
  97.         $this->left_post = $post_before;
  98.         $this->right_post = $post_after;
  99.  
  100.         // If this is a new post, set an empty title for $this->left_post so that it appears in the diff.
  101.                 $child_posts = wp_get_post_revisions( $post_id, array( 'numberposts' => 1 ) );
  102.                 if ( count( $child_posts ) == 0 ) {
  103.             $this->left_post->post_title = '';
  104.         }
  105.  
  106.         if ( !$this->left_post || !$this->right_post )
  107.             return;
  108.  
  109.         $html_diffs = array();
  110.         $text_diffs = array();
  111.         $identical = true;
  112.         foreach ( _wp_post_revision_fields() as $field => $field_title ) {
  113.             $left = apply_filters( "_wp_post_revision_field_$field", $this->left_post->$field, $field );
  114.             $right = apply_filters( "_wp_post_revision_field_$field", $this->right_post->$field, $field );
  115.  
  116.             if ( !$diff = $this->wp_text_diff( $left, $right ) )
  117.                 continue;
  118.             $html_diffs[$field_title] = $diff;
  119.  
  120.             $left  = normalize_whitespace( $left );
  121.             $right = normalize_whitespace( $right );
  122.  
  123.             $left_lines  = explode( "\n", $left );
  124.             $right_lines = explode( "\n", $right );
  125.  
  126.             require_once( dirname( __FILE__ ) . '/unified.php' );
  127.  
  128.             $text_diff = new Text_Diff( $left_lines, $right_lines );
  129.             $renderer  = new Text_Diff_Renderer_unified();
  130.             $text_diffs[$field_title] = $renderer->render($text_diff);
  131.  
  132.             $identical = false;
  133.         }
  134.  
  135.         // Ov3rfly: Detect draft to publish or similar
  136.         if ( $this->left_post->post_status != $this->right_post->post_status ) {
  137.             $left = $this->nice_post_status( $this->left_post->post_status );
  138.             $right = $this->nice_post_status( $this->right_post->post_status );
  139.             $field_title = __( 'Status' );
  140.  
  141.             if ( $diff = $this->wp_text_diff( $left, $right ) ) {
  142.                 $html_diffs[$field_title] = $diff;
  143.  
  144.                 require_once( dirname( __FILE__ ) . '/unified.php' );
  145.  
  146.                 $text_diff = new Text_Diff( array( $left ), array( $right ) );
  147.                 $renderer  = new Text_Diff_Renderer_unified();
  148.                 $text_diffs[$field_title] = $renderer->render($text_diff);
  149.  
  150.                 $identical = false;
  151.             }
  152.         }
  153.  
  154.         // Ov3rfly: Detect taxonomy changes (tags, categories.. ), see also function pre_post_update()
  155.         if ( !empty( $this->left_taxonomies ) ) {
  156.             $taxonomies = get_object_taxonomies( $this->right_post->post_type, 'objects' );
  157.             foreach( $taxonomies as $taxonomy_slug => $taxonomy ) {
  158.                 $left = $this->left_taxonomies[ $taxonomy_slug ];
  159.                 $right = strip_tags( get_the_term_list( $this->right_post->ID, $taxonomy_slug, '', ', ', '' ) );
  160.                 $field_title = __( $taxonomy->label );
  161.  
  162.                 if ( $diff = $this->wp_text_diff( $left, $right ) ) {
  163.                     $html_diffs[$field_title] = $diff;
  164.  
  165.                     require_once( dirname( __FILE__ ) . '/unified.php' );
  166.  
  167.                     $text_diff = new Text_Diff( array( $left ), array( $right ) );
  168.                     $renderer  = new Text_Diff_Renderer_unified();
  169.                     $text_diffs[$field_title] = $renderer->render($text_diff);                 
  170.                    
  171.                     $identical = false;
  172.                 }
  173.             }
  174.         }
  175.  
  176.         if ( $identical ) {
  177.             $this->left_post = null;
  178.             $this->right_post = null;
  179.             return;
  180.         }
  181.  
  182.         // Grab the meta data
  183.         $the_author = get_the_author_meta( 'display_name', get_current_user_id() ); // The revision
  184.         $the_title = get_the_title( $this->right_post->ID ); // New title (may be same as old title)
  185.         $the_date = gmdate( 'j F, Y \a\t G:i \U\T\C', strtotime( $this->right_post->post_modified_gmt . '+0000' ) ); // Modified time
  186.         $the_permalink = clean_url( get_permalink( $this->right_post->ID ) );
  187.         $the_edit_link = clean_url( get_edit_post_link( $this->right_post->ID ) );
  188.  
  189.         $left_title = __( 'Revision' );
  190.         $right_title = sprintf( __( 'Current %s' ), $post_type = ucfirst( $this->right_post->post_type ) );
  191.  
  192.         $head_sprintf = __( '%s made the following changes to the %s %s on %s' );
  193.  
  194.  
  195.         // HTML
  196.         $html_diff_head  = '<h2>' . sprintf( __( '%s changed' ), $post_type ) . "</h2>\n";
  197.         $html_diff_head .= '<p>' . sprintf( $head_sprintf,
  198.             esc_html( $the_author ),
  199.             sprintf( _x( '&#8220;%s&#8221; [%s]', '1 = link, 2 = "edit"' ),
  200.                 "<a href='$the_permalink'>" . esc_html( $the_title ) . '</a>',
  201.                 "<a href='$the_edit_link'>" . __( 'edit' ) . '</a>'
  202.             ),
  203.             $this->right_post->post_type,
  204.             $the_date
  205.         ) . "</p>\n\n";
  206.  
  207.         $html_diff_head .= "<table style='width: 100%; border-collapse: collapse; border: none;'><tr>\n";
  208.         $html_diff_head .= "<td style='width: 50%; padding: 0; margin: 0;'>" . esc_html( $left_title ) . ' @ ' . esc_html( $this->left_post->post_date_gmt ) . "</td>\n";
  209.         $html_diff_head .= "<td style='width: 50%; padding: 0; margin: 0;'>" . esc_html( $right_title ) . ' @ ' . esc_html( $this->right_post->post_modified_gmt ) . "</td>\n";
  210.         $html_diff_head .= "</tr></table>\n\n";
  211.  
  212.         $html_diff = '';
  213.         foreach ( $html_diffs as $field_title => $diff ) {
  214.             $html_diff .= '<h3>' . esc_html( $field_title ) . "</h3>\n";
  215.             $html_diff .= "$diff\n\n";
  216.         }
  217.  
  218.         $html_diff = rtrim( $html_diff );
  219.  
  220.         // Replace classes with inline style
  221.         $html_diff = str_replace( "class='diff'", 'style="width: 100%; border-collapse: collapse; border: none; white-space: pre-wrap; word-wrap: break-word; font-family: Consolas,Monaco,Courier,monospace;"', $html_diff );
  222.         $html_diff = preg_replace( '#<col[^>]+/?>#i', '', $html_diff );
  223.         $html_diff = str_replace( "class='diff-deletedline'", 'style="padding: 5px; width: 50%; background-color: #fdd;"', $html_diff );
  224.         $html_diff = str_replace( "class='diff-addedline'", 'style="padding: 5px; width: 50%; background-color: #dfd;"', $html_diff );
  225.         $html_diff = str_replace( "class='diff-context'", 'style="padding: 5px; width: 50%;"', $html_diff );
  226.         $html_diff = str_replace( '<td>', '<td style="padding: 5px;">', $html_diff );
  227.         $html_diff = str_replace( '<del>', '<del style="text-decoration: none; background-color: #f99;">', $html_diff );
  228.         $html_diff = str_replace( '<ins>', '<ins style="text-decoration: none; background-color: #9f9;">', $html_diff );
  229.         $html_diff = str_replace( array( '</td>', '</tr>', '</tbody>' ), array( "</td>\n", "</tr>\n", "</tbody>\n" ), $html_diff );
  230.  
  231.         $html_diff = $html_diff_head . $html_diff;
  232.  
  233.  
  234.         // Refactor some of the meta data for TEXT
  235.         $length = max( strlen( $left_title ), strlen( $right_title ) );
  236.         $left_title = str_pad( $left_title, $length + 2 );
  237.         $right_title = str_pad( $right_title, $length + 2 );
  238.  
  239.         // TEXT
  240.         $text_diff  = sprintf( $head_sprintf, $the_author, '"' . $the_title . '"', $this->right_post->post_type, $the_date ) . "\n";
  241.         $text_diff .= "URL:  $the_permalink\n";
  242.         $text_diff .= "Edit: $the_edit_link\n\n";
  243.  
  244.         foreach ( $text_diffs as $field_title => $diff ) {
  245.             $text_diff .= "$field_title\n";
  246.             $text_diff .= "===================================================================\n";
  247.             $text_diff .= "--- $left_title  ({$this->left_post->post_date_gmt})\n";
  248.             $text_diff .= "+++ $right_title ({$this->right_post->post_modified_gmt})\n";
  249.             $text_diff .= "$diff\n\n";
  250.         }
  251.  
  252.         $this->text_diff = $text_diff = rtrim( $text_diff );
  253.  
  254.  
  255.         // Send email
  256.         $charset = apply_filters( 'wp_mail_charset', get_option( 'blog_charset' ) );
  257.         $blogname = html_entity_decode( get_option( 'blogname' ), ENT_QUOTES, $charset );
  258.         $title = html_entity_decode( $the_title, ENT_QUOTES, $charset );
  259.  
  260.         add_action( 'phpmailer_init', array( $this, 'phpmailer_init' ) );
  261.  
  262.         $user_emails = array();
  263.         foreach( $options['users'] as $user_id ) {
  264.             if ( function_exists( 'is_multisite' ) && is_multisite() ) {
  265.                 if ( is_user_member_of_blog( $user_id, get_current_blog_id() ) )
  266.                     $user_emails[] = get_user_option( 'user_email', $user_id );
  267.             } else {
  268.                 if ( $user_email = get_user_option( 'user_email', $user_id ) )
  269.                     $user_emails[] = $user_email;
  270.             }
  271.         }
  272.  
  273.         $emails = array_unique( array_merge( $options['emails'], $user_emails ) );
  274.         if ( ! count( $emails ) && apply_filters( 'email_post_changes_admin_email_fallback', true ) )
  275.             $emails[] = get_option( 'admin_email' );
  276.  
  277.         $emails = apply_filters( 'email_post_changes_emails', $emails, $this->left_post->ID, $this->right_post->ID );
  278.  
  279.         foreach ( $emails as $email ) {
  280.             wp_mail(
  281.                 $email,
  282.                 sprintf( __( '[%s] %s changed: %s' ), $blogname, $post_type, $title ),
  283.                 $html_diff
  284.             );
  285.         }
  286.  
  287.         remove_action( 'phpmailer_init', array( &$this, 'phpmailer_init' ) );
  288.  
  289.         do_action( 'email_post_changes_email_sent' );
  290.     }
  291.  
  292.     // Ov3rfly:
  293.     function nice_post_status( $post_status ) {
  294.         $nice = 'Unknown';
  295.         switch( $post_status ) {
  296.         case 'publish':
  297.             $nice = _x( 'Published', 'post' );
  298.             break;
  299.         case 'future':
  300.             $nice = _x( 'Scheduled', 'post' );
  301.             break;
  302.         case 'draft':
  303.             $nice = _x( 'Draft', 'post' );
  304.             break;
  305.         case 'pending':
  306.             $nice = _x( 'Pending', 'post' );
  307.             break;
  308.         case 'private':
  309.             $nice = _x( 'Private', 'post' );
  310.             break;
  311.         case 'trash':
  312.             $nice = _x( 'Trash', 'post' );
  313.             break;
  314.         case 'auto-draft':
  315.         case 'inherit':
  316.             $nice = $post_status;
  317.             break;
  318.         }
  319.         return $nice;
  320.     }
  321.  
  322.     function phpmailer_init( &$phpmailer ) {
  323.         $phpmailer->AltBody = $this->text_diff;
  324.  
  325.         $phpmailer->AddReplyTo(
  326.             get_the_author_meta( 'email', $this->right_post->post_author ),
  327.             get_the_author_meta( 'display_name', $this->right_post->post_author )
  328.         );
  329.     }
  330.  
  331.     function get_post_type_label( $post_type ) {
  332.         // 2.9
  333.         if ( !function_exists( 'get_post_type_object' ) )
  334.             return ucwords( str_replace( '_', ' ', $post_type ) );
  335.  
  336.         // 3.0
  337.         $post_type_object = get_post_type_object( $post_type );
  338.         if ( empty( $post_type_object->label ) )
  339.             return ucwords( str_replace( '_', ' ', $post_type ) );
  340.         return $post_type_object->label;
  341.     }
  342.  
  343.     /* Admin */
  344.     function admin_menu() {
  345.         register_setting( self::OPTION_GROUP, self::OPTION, array( $this, 'validate_options' ) );
  346.  
  347.         add_settings_section( self::ADMIN_PAGE, __( 'Email Post Changes' ), array( $this, 'settings_section' ), self::ADMIN_PAGE );
  348.         add_settings_field( self::ADMIN_PAGE . '_enable', __( 'Enable' ), array( $this, 'enable_setting' ), self::ADMIN_PAGE, self::ADMIN_PAGE );
  349.         add_settings_field( self::ADMIN_PAGE . '_users', __( 'Users to Email' ), array( $this, 'users_setting' ), self::ADMIN_PAGE, self::ADMIN_PAGE );
  350.         add_settings_field( self::ADMIN_PAGE . '_emails', __( 'Additional Email Addresses' ), array( $this, 'emails_setting' ), self::ADMIN_PAGE, self::ADMIN_PAGE );
  351.         add_settings_field( self::ADMIN_PAGE . '_post_types', __( 'Post Types' ), array( $this, 'post_types_setting' ), self::ADMIN_PAGE, self::ADMIN_PAGE );
  352.         add_settings_field( self::ADMIN_PAGE . '_drafts', __( 'Drafts' ), array( $this, 'drafts_setting' ), self::ADMIN_PAGE, self::ADMIN_PAGE );
  353.  
  354.         add_options_page( __( 'Email Post Changes' ), __( 'Email Post Changes' ), 'manage_options', self::ADMIN_PAGE, array( $this, 'admin_page' ) );
  355.     }
  356.  
  357.     function validate_options( $options ) {
  358.         if ( !$options || !is_array( $options ) )
  359.             return $this->defaults;
  360.  
  361.         $return = array();
  362.  
  363.         $return['enable'] = ( empty( $options['enable'] ) ) ? 0 : 1;
  364.  
  365.         if ( empty( $options['users'] ) || !is_array( $options ) ) {
  366.             $return['users'] = $this->defaults['users'];
  367.         } else {
  368.             $return['users'] = $options['users'];
  369.         }
  370.  
  371.         if ( empty( $options['emails'] ) ) {
  372.             if ( count( $return['users'] ) )
  373.                 $return['emails'] = array();
  374.             else
  375.                 $return['emails'] = $this->defaults['emails'];
  376.         } else {
  377.             $_emails = is_string( $options['emails'] ) ? preg_split( '(\n|\r)', $options['emails'], -1, PREG_SPLIT_NO_EMPTY ) : array();
  378.             $_emails = array_unique( $_emails );
  379.             $emails = array_filter( $_emails, 'is_email' );
  380.  
  381.             $invalid_emails = array_diff( $_emails, $emails );
  382.             if ( $invalid_emails )
  383.                 $return['invalid_emails'] = $invalid_emails;
  384.  
  385.             if ( $emails )
  386.                 $return['emails'] = $emails;
  387.             elseif ( count( $return['users'] ) )
  388.                 $return['emails'] = array();
  389.             else
  390.                 $return['emails'] = $this->defaults['emails'];
  391.  
  392.             // Don't store a huge list of invalid emails addresses in the option
  393.             if ( isset ( $return['invalid_emails'] ) && count( $return['invalid_emails'] ) > 200 ) {
  394.                 $return['invalid_emails'] = array_slice( $return['invalid_emails'], 0, 200 );
  395.                 $return['invalid_emails'][] = __( 'and many more not listed here' );
  396.             }
  397.  
  398.             // Cap to at max 200 email addresses
  399.             if ( count( $return['emails'] ) > 200 ) {
  400.                 $return['emails'] = array_slice( $return['emails'], 0, 200 );
  401.             }
  402.         }
  403.  
  404.         if ( empty( $options['post_types'] ) || !is_array( $options ) ) {
  405.             $return['post_types'] = $this->defaults['post_types'];
  406.         } else {
  407.             $post_types = array_intersect( $options['post_types'], $this->get_post_types() );
  408.             $return['post_types'] = $post_types ? $post_types : $this->defaults['post_types'];
  409.         }
  410.  
  411.         $return['drafts'] = ( empty( $options['drafts'] ) ) ? 0 : 1;
  412.  
  413.         do_action( 'email_post_changes_validate_options', $this->get_options(), $return );
  414.  
  415.         return $return;
  416.     }
  417.  
  418.     function admin_page() {
  419.         $options = $this->get_options();
  420. ?>
  421.  
  422. <div class="wrap">
  423.     <h2><?php _e( 'Email Post Changes' ); ?></h2>
  424. <?php   if ( !empty( $options['invalid_emails'] ) && $_GET['settings-updated'] ) : ?>
  425.     <div class="error">
  426.         <p><?php printf( _n( 'Invalid Email: %s', 'Invalid Emails: %s', count( $options['invalid_emails'] ) ), '<kbd>' . join( '</kbd>, <kbd>', array_map( 'esc_html', $options['invalid_emails'] ) ) ); ?></p>
  427.     </div>
  428. <?php   endif; ?>
  429.  
  430.     <form action="options.php" method="post">
  431.         <?php settings_fields( self::OPTION_GROUP ); ?>
  432.         <?php do_settings_sections( self::ADMIN_PAGE ); ?>
  433.         <p class="submit">
  434.             <input type="submit" class="button-primary" value="<?php esc_attr_e( 'Save Changes' ); ?>" />
  435.         </p>
  436.     </form>
  437. </div>
  438. <?php
  439.     }
  440.  
  441.     function settings_section() {} // stub
  442.  
  443.     function enable_setting() {
  444.         $options = $this->get_options();
  445. ?>
  446.         <p><label><input type="checkbox" name="email_post_changes[enable]" value="1"<?php checked( $options['enable'], 1 ); ?> /> <?php _e( 'Send an email when a post or page changes.' ); ?></label></p>
  447. <?php
  448.     }
  449.  
  450.     function users_setting() {
  451.         $options = $this->get_options();
  452. ?>
  453.         <div style="overflow: auto; max-height: 300px;">
  454.             <ul>
  455. <?php       $users = get_users();
  456.         usort( $users, array( $this, 'sort_users_by_display_name' ) );
  457.  
  458.         foreach ( $users as $user ) : ?>
  459.                 <li><label><input type="checkbox" name="email_post_changes[users][]" value="<?php echo (int) $user->ID; ?>"<?php checked( in_array( $user->ID, $options['users'] ) ); ?> /> <?php echo esc_html( $user->display_name ); ?> ( <?php echo esc_html( $user->user_login ); ?> - <?php echo esc_html( $user->user_email ); ?> )</label></li>
  460.  
  461. <?php       endforeach; ?>
  462.             </ul>
  463.         </div>
  464. <?php
  465.     }
  466.  
  467.     function sort_users_by_display_name( $a, $b ) {
  468.         return strcmp( strtolower( $a->display_name ), strtolower( $b->display_name ) );
  469.     }
  470.  
  471.     function emails_setting() {
  472.         $options = $this->get_options();
  473. ?>
  474.         <textarea rows="4" cols="40" style="width: 40em;" name="email_post_changes[emails]"><?php echo esc_html( join( "\n", $options['emails'] ) ); ?></textarea>
  475.         <p class="description"><?php _e( 'One email address per line.' ); ?></p>
  476. <?php
  477.     }
  478.  
  479.     function post_types_setting() {
  480.         $options = $this->get_options();
  481. ?>
  482.         <ul>
  483. <?php       foreach ( $this->get_post_types() as $post_type ) :
  484.             $label = $this->get_post_type_label( $post_type );
  485. ?>
  486.             <li><label><input type="checkbox" name="email_post_changes[post_types][]" value="<?php echo esc_attr( $post_type ); ?>"<?php checked( in_array( $post_type, $options['post_types'] ) ); ?> /> <?php echo esc_html( $label ); ?></label></li>
  487. <?php       endforeach; ?>
  488.         </ul>
  489. <?php
  490.     }
  491.  
  492.     function drafts_setting() {
  493.         $options = $this->get_options();
  494. ?>
  495.         <p><label><input type="checkbox" name="email_post_changes[drafts]" value="1"<?php checked( $options['drafts'], 1 ); ?> /> <?php _e( 'Email changes to drafts, not just published items.' ); ?></label></p>
  496. <?php
  497.     }
  498.  
  499.     function wp_text_diff( $left_string, $right_string, $args = null ) {
  500.         $defaults = array( 'title' => '', 'title_left' => '', 'title_right' => '' );
  501.         $args = wp_parse_args( $args, $defaults );
  502.  
  503.         $left_string  = normalize_whitespace( $left_string );
  504.         $right_string = normalize_whitespace( $right_string );
  505.         $left_lines  = explode( "\n", $left_string );
  506.         $right_lines = explode( "\n", $right_string );
  507.  
  508.         $text_diff = new Text_Diff( $left_lines, $right_lines );
  509.         $renderer  = new Email_Post_Changes_Diff();
  510.         $diff = $renderer->render( $text_diff );
  511.  
  512.         if ( !$diff )
  513.             return '';
  514.  
  515.         $r  = "<table class='diff'>\n";
  516.         $r .= "<col class='ltype' /><col class='content' /><col class='ltype' /><col class='content' />";
  517.  
  518.         if ( $args['title'] || $args['title_left'] || $args['title_right'] )
  519.             $r .= "<thead>";
  520.         if ( $args['title'] )
  521.             $r .= "<tr class='diff-title'><th colspan='4'>$args[title]</th></tr>\n";
  522.         if ( $args['title_left'] || $args['title_right'] ) {
  523.             $r .= "<tr class='diff-sub-title'>\n";
  524.             $r .= "\t<td></td><th>$args[title_left]</th>\n";
  525.             $r .= "\t<td></td><th>$args[title_right]</th>\n";
  526.             $r .= "</tr>\n";
  527.         }
  528.         if ( $args['title'] || $args['title_left'] || $args['title_right'] )
  529.             $r .= "</thead>\n";
  530.         $r .= "<tbody>\n$diff\n</tbody>\n";
  531.         $r .= "</table>";
  532.         return $r;
  533.     }
  534. }
  535.  
  536. if ( !class_exists( 'WP_Text_Diff_Renderer_Table' ) )
  537.     require( ABSPATH . WPINC . '/wp-diff.php' );
  538.  
  539. class Email_Post_Changes_Diff extends WP_Text_Diff_Renderer_Table {
  540.     var $_leading_context_lines  = 2;
  541.     var $_trailing_context_lines = 2;
  542. }