Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function post_updated( $post_id, $post_after, $post_before ) {
- $options = $this->get_options();
- // 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.
- if ( 0 == $options['drafts'] && 'draft' == $post_before->post_status && 'draft' == $post_after->post_status )
- return;
- if ( isset( $_POST['autosave'] ) )
- return;
- if ( !in_array( $post_before->post_type, $options['post_types'] ) )
- return;
- $this->left_post = $post_before;
- $this->right_post = $post_after;
- // If this is a new post, set an empty title for $this->left_post so that it appears in the diff.
- $child_posts = wp_get_post_revisions( $post_id, array( 'numberposts' => 1 ) );
- if ( count( $child_posts ) == 0 ) {
- $this->left_post->post_title = '';
- }
- if ( !$this->left_post || !$this->right_post )
- return;
- $html_diffs = array();
- $text_diffs = array();
- $identical = true;
- foreach ( _wp_post_revision_fields() as $field => $field_title ) {
- $left = apply_filters( "_wp_post_revision_field_$field", $this->left_post->$field, $field );
- $right = apply_filters( "_wp_post_revision_field_$field", $this->right_post->$field, $field );
- if ( !$diff = $this->wp_text_diff( $left, $right ) )
- continue;
- $html_diffs[$field_title] = $diff;
- $left = normalize_whitespace( $left );
- $right = normalize_whitespace( $right );
- $left_lines = explode( "\n", $left );
- $right_lines = explode( "\n", $right );
- require_once( dirname( __FILE__ ) . '/unified.php' );
- $text_diff = new Text_Diff( $left_lines, $right_lines );
- $renderer = new Text_Diff_Renderer_unified();
- $text_diffs[$field_title] = $renderer->render($text_diff);
- $identical = false;
- }
- // Ov3rfly: Detect draft to publish or similar
- $post_status_left = $post_status_right = '';
- if ( $this->left_post->post_status != $this->right_post->post_status ) {
- $post_status_left = $post_status_right = '<br />' . __( 'Status' ) . ': ';
- $post_status_left .= $this->nice_post_status( $this->left_post->post_status );
- $post_status_right .= $this->nice_post_status( $this->right_post->post_status );
- $identical = false;
- }
- if ( $identical ) {
- $this->left_post = null;
- $this->right_post = null;
- return;
- }
- // Grab the meta data
- $the_author = get_the_author_meta( 'display_name', get_current_user_id() ); // The revision
- $the_title = get_the_title( $this->right_post->ID ); // New title (may be same as old title)
- $the_date = gmdate( 'j F, Y \a\t G:i \U\T\C', strtotime( $this->right_post->post_modified_gmt . '+0000' ) ); // Modified time
- $the_permalink = clean_url( get_permalink( $this->right_post->ID ) );
- $the_edit_link = clean_url( get_edit_post_link( $this->right_post->ID ) );
- $left_title = __( 'Revision' );
- $right_title = sprintf( __( 'Current %s' ), $post_type = ucfirst( $this->right_post->post_type ) );
- $head_sprintf = __( '%s made the following changes to the %s %s on %s' );
- // HTML
- $html_diff_head = '<h2>' . sprintf( __( '%s changed' ), $post_type ) . "</h2>\n";
- $html_diff_head .= '<p>' . sprintf( $head_sprintf,
- esc_html( $the_author ),
- sprintf( _x( '“%s” [%s]', '1 = link, 2 = "edit"' ),
- "<a href='$the_permalink'>" . esc_html( $the_title ) . '</a>',
- "<a href='$the_edit_link'>" . __( 'edit' ) . '</a>'
- ),
- $this->right_post->post_type,
- $the_date
- ) . "</p>\n\n";
- $html_diff_head .= "<table style='width: 100%; border-collapse: collapse; border: none;'><tr>\n";
- // Ov3rfly: added $post_status_left and $post_status_right
- $html_diff_head .= "<td style='width: 50%; padding: 0; margin: 0;'>" . esc_html( $left_title ) . ' @ ' . esc_html( $this->left_post->post_date_gmt ) . $post_status_left . "</td>\n";
- $html_diff_head .= "<td style='width: 50%; padding: 0; margin: 0;'>" . esc_html( $right_title ) . ' @ ' . esc_html( $this->right_post->post_modified_gmt ) . $post_status_right . "</td>\n";
- $html_diff_head .= "</tr></table>\n\n";
- $html_diff = '';
- foreach ( $html_diffs as $field_title => $diff ) {
- $html_diff .= '<h3>' . esc_html( $field_title ) . "</h3>\n";
- $html_diff .= "$diff\n\n";
- }
- $html_diff = rtrim( $html_diff );
- // Replace classes with inline style
- $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 );
- $html_diff = preg_replace( '#<col[^>]+/?>#i', '', $html_diff );
- $html_diff = str_replace( "class='diff-deletedline'", 'style="padding: 5px; width: 50%; background-color: #fdd;"', $html_diff );
- $html_diff = str_replace( "class='diff-addedline'", 'style="padding: 5px; width: 50%; background-color: #dfd;"', $html_diff );
- $html_diff = str_replace( "class='diff-context'", 'style="padding: 5px; width: 50%;"', $html_diff );
- $html_diff = str_replace( '<td>', '<td style="padding: 5px;">', $html_diff );
- $html_diff = str_replace( '<del>', '<del style="text-decoration: none; background-color: #f99;">', $html_diff );
- $html_diff = str_replace( '<ins>', '<ins style="text-decoration: none; background-color: #9f9;">', $html_diff );
- $html_diff = str_replace( array( '</td>', '</tr>', '</tbody>' ), array( "</td>\n", "</tr>\n", "</tbody>\n" ), $html_diff );
- $html_diff = $html_diff_head . $html_diff;
- // Refactor some of the meta data for TEXT
- $length = max( strlen( $left_title ), strlen( $right_title ) );
- $left_title = str_pad( $left_title, $length + 2 );
- $right_title = str_pad( $right_title, $length + 2 );
- // TEXT
- $text_diff = sprintf( $head_sprintf, $the_author, '"' . $the_title . '"', $this->right_post->post_type, $the_date ) . "\n";
- $text_diff .= "URL: $the_permalink\n";
- $text_diff .= "Edit: $the_edit_link\n\n";
- foreach ( $text_diffs as $field_title => $diff ) {
- $text_diff .= "$field_title\n";
- $text_diff .= "===================================================================\n";
- $text_diff .= "--- $left_title ({$this->left_post->post_date_gmt})\n";
- $text_diff .= "+++ $right_title ({$this->right_post->post_modified_gmt})\n";
- // Ov3rfly:
- if ( !empty( $post_status_left ) && !empty( $post_status_right ) ) {
- $text_diff .= "--- " . strip_tags( $post_status_left ) . "\n";
- $text_diff .= "+++ " . strip_tags( $post_status_right ) . "\n";
- }
- $text_diff .= "$diff\n\n";
- }
- $this->text_diff = $text_diff = rtrim( $text_diff );
- // Send email
- $charset = apply_filters( 'wp_mail_charset', get_option( 'blog_charset' ) );
- $blogname = html_entity_decode( get_option( 'blogname' ), ENT_QUOTES, $charset );
- $title = html_entity_decode( $the_title, ENT_QUOTES, $charset );
- add_action( 'phpmailer_init', array( $this, 'phpmailer_init_once' ) );
- wp_mail(
- null, // see hack in ::phpmailer_init_once()
- sprintf( __( '[%s] %s changed: %s' ), $blogname, $post_type, $title ),
- $html_diff
- );
- $this->left_post = null;
- $this->right_post = null;
- do_action( 'email_post_changes_email_sent' );
- }
- // Ov3rfly:
- function nice_post_status( $post_status ) {
- $nice = 'Unknown';
- switch( $post_status ) {
- case 'publish':
- $nice = _x( 'Published', 'post' );
- break;
- case 'future':
- $nice = _x( 'Scheduled', 'post' );
- break;
- case 'draft':
- $nice = _x( 'Draft', 'post' );
- break;
- case 'pending':
- $nice = _x( 'Pending', 'post' );
- break;
- case 'private':
- $nice = _x( 'Private', 'post' );
- break;
- case 'trash':
- $nice = _x( 'Trash', 'post' );
- break;
- case 'auto-draft':
- case 'inherit':
- $nice = $post_status;
- break;
- }
- return $nice;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement