Advertisement
Guest User

Post-Status change detection for Email Post Changes 1.7

a guest
Nov 26th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.73 KB | None | 0 0
  1.     function post_updated( $post_id, $post_after, $post_before ) {
  2.         $options = $this->get_options();
  3.         // 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.
  4.         if ( 0 == $options['drafts'] && 'draft' == $post_before->post_status && 'draft' == $post_after->post_status )
  5.             return;
  6.                    
  7.         if ( isset( $_POST['autosave'] ) )
  8.             return;
  9.  
  10.         if ( !in_array( $post_before->post_type, $options['post_types'] ) )
  11.             return;
  12.  
  13.         $this->left_post = $post_before;
  14.         $this->right_post = $post_after;
  15.  
  16.         // If this is a new post, set an empty title for $this->left_post so that it appears in the diff.
  17.                 $child_posts = wp_get_post_revisions( $post_id, array( 'numberposts' => 1 ) );
  18.                 if ( count( $child_posts ) == 0 ) {
  19.             $this->left_post->post_title = '';
  20.         }
  21.  
  22.         if ( !$this->left_post || !$this->right_post )
  23.             return;
  24.  
  25.         $html_diffs = array();
  26.         $text_diffs = array();
  27.         $identical = true;
  28.         foreach ( _wp_post_revision_fields() as $field => $field_title ) {
  29.             $left = apply_filters( "_wp_post_revision_field_$field", $this->left_post->$field, $field );
  30.             $right = apply_filters( "_wp_post_revision_field_$field", $this->right_post->$field, $field );
  31.  
  32.             if ( !$diff = $this->wp_text_diff( $left, $right ) )
  33.                 continue;
  34.             $html_diffs[$field_title] = $diff;
  35.  
  36.             $left  = normalize_whitespace( $left );
  37.             $right = normalize_whitespace( $right );
  38.  
  39.             $left_lines  = explode( "\n", $left );
  40.             $right_lines = explode( "\n", $right );
  41.  
  42.             require_once( dirname( __FILE__ ) . '/unified.php' );
  43.  
  44.             $text_diff = new Text_Diff( $left_lines, $right_lines );
  45.             $renderer  = new Text_Diff_Renderer_unified();
  46.             $text_diffs[$field_title] = $renderer->render($text_diff);
  47.  
  48.             $identical = false;
  49.         }
  50.  
  51.         // Ov3rfly: Detect draft to publish or similar
  52.         $post_status_left = $post_status_right = '';
  53.         if ( $this->left_post->post_status != $this->right_post->post_status ) {
  54.             $post_status_left = $post_status_right = '<br />' . __( 'Status' ) . ': ';
  55.             $post_status_left .= $this->nice_post_status( $this->left_post->post_status );
  56.             $post_status_right .= $this->nice_post_status( $this->right_post->post_status );
  57.            
  58.             $identical = false;
  59.         }
  60.        
  61.         if ( $identical ) {
  62.             $this->left_post = null;
  63.             $this->right_post = null;
  64.             return;
  65.         }
  66.  
  67.         // Grab the meta data
  68.         $the_author = get_the_author_meta( 'display_name', get_current_user_id() ); // The revision
  69.         $the_title = get_the_title( $this->right_post->ID ); // New title (may be same as old title)
  70.         $the_date = gmdate( 'j F, Y \a\t G:i \U\T\C', strtotime( $this->right_post->post_modified_gmt . '+0000' ) ); // Modified time
  71.         $the_permalink = clean_url( get_permalink( $this->right_post->ID ) );
  72.         $the_edit_link = clean_url( get_edit_post_link( $this->right_post->ID ) );
  73.  
  74.         $left_title = __( 'Revision' );
  75.         $right_title = sprintf( __( 'Current %s' ), $post_type = ucfirst( $this->right_post->post_type ) );
  76.  
  77.         $head_sprintf = __( '%s made the following changes to the %s %s on %s' );
  78.  
  79.  
  80.         // HTML
  81.         $html_diff_head  = '<h2>' . sprintf( __( '%s changed' ), $post_type ) . "</h2>\n";
  82.         $html_diff_head .= '<p>' . sprintf( $head_sprintf,
  83.             esc_html( $the_author ),
  84.             sprintf( _x( '&#8220;%s&#8221; [%s]', '1 = link, 2 = "edit"' ),
  85.                 "<a href='$the_permalink'>" . esc_html( $the_title ) . '</a>',
  86.                 "<a href='$the_edit_link'>" . __( 'edit' ) . '</a>'
  87.             ),
  88.             $this->right_post->post_type,
  89.             $the_date
  90.         ) . "</p>\n\n";
  91.  
  92.         $html_diff_head .= "<table style='width: 100%; border-collapse: collapse; border: none;'><tr>\n";
  93.         // Ov3rfly: added $post_status_left and $post_status_right
  94.         $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";
  95.         $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";
  96.  
  97.         $html_diff_head .= "</tr></table>\n\n";
  98.  
  99.         $html_diff = '';
  100.         foreach ( $html_diffs as $field_title => $diff ) {
  101.             $html_diff .= '<h3>' . esc_html( $field_title ) . "</h3>\n";
  102.             $html_diff .= "$diff\n\n";
  103.         }
  104.  
  105.         $html_diff = rtrim( $html_diff );
  106.  
  107.         // Replace classes with inline style
  108.         $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 );
  109.         $html_diff = preg_replace( '#<col[^>]+/?>#i', '', $html_diff );
  110.         $html_diff = str_replace( "class='diff-deletedline'", 'style="padding: 5px; width: 50%; background-color: #fdd;"', $html_diff );
  111.         $html_diff = str_replace( "class='diff-addedline'", 'style="padding: 5px; width: 50%; background-color: #dfd;"', $html_diff );
  112.         $html_diff = str_replace( "class='diff-context'", 'style="padding: 5px; width: 50%;"', $html_diff );
  113.         $html_diff = str_replace( '<td>', '<td style="padding: 5px;">', $html_diff );
  114.         $html_diff = str_replace( '<del>', '<del style="text-decoration: none; background-color: #f99;">', $html_diff );
  115.         $html_diff = str_replace( '<ins>', '<ins style="text-decoration: none; background-color: #9f9;">', $html_diff );
  116.         $html_diff = str_replace( array( '</td>', '</tr>', '</tbody>' ), array( "</td>\n", "</tr>\n", "</tbody>\n" ), $html_diff );
  117.  
  118.         $html_diff = $html_diff_head . $html_diff;
  119.  
  120.  
  121.         // Refactor some of the meta data for TEXT
  122.         $length = max( strlen( $left_title ), strlen( $right_title ) );
  123.         $left_title = str_pad( $left_title, $length + 2 );
  124.         $right_title = str_pad( $right_title, $length + 2 );
  125.  
  126.         // TEXT
  127.         $text_diff  = sprintf( $head_sprintf, $the_author, '"' . $the_title . '"', $this->right_post->post_type, $the_date ) . "\n";
  128.         $text_diff .= "URL:  $the_permalink\n";
  129.         $text_diff .= "Edit: $the_edit_link\n\n";
  130.  
  131.         foreach ( $text_diffs as $field_title => $diff ) {
  132.             $text_diff .= "$field_title\n";
  133.             $text_diff .= "===================================================================\n";
  134.             $text_diff .= "--- $left_title  ({$this->left_post->post_date_gmt})\n";
  135.             $text_diff .= "+++ $right_title ({$this->right_post->post_modified_gmt})\n";
  136.             // Ov3rfly:
  137.             if ( !empty( $post_status_left ) && !empty( $post_status_right ) ) {
  138.                 $text_diff .= "--- " . strip_tags( $post_status_left ) . "\n";
  139.                 $text_diff .= "+++ " . strip_tags( $post_status_right ) . "\n";        
  140.             }
  141.             $text_diff .= "$diff\n\n";
  142.         }
  143.  
  144.         $this->text_diff = $text_diff = rtrim( $text_diff );
  145.  
  146.  
  147.         // Send email
  148.         $charset = apply_filters( 'wp_mail_charset', get_option( 'blog_charset' ) );
  149.         $blogname = html_entity_decode( get_option( 'blogname' ), ENT_QUOTES, $charset );
  150.         $title = html_entity_decode( $the_title, ENT_QUOTES, $charset );
  151.  
  152.         add_action( 'phpmailer_init', array( $this, 'phpmailer_init_once' ) );
  153.  
  154.         wp_mail(
  155.             null, // see hack in ::phpmailer_init_once()
  156.             sprintf( __( '[%s] %s changed: %s' ), $blogname, $post_type, $title ),
  157.             $html_diff
  158.         );
  159.  
  160.         $this->left_post = null;
  161.         $this->right_post = null;
  162.  
  163.         do_action( 'email_post_changes_email_sent' );
  164.     }
  165.  
  166.     // Ov3rfly:
  167.     function nice_post_status( $post_status ) {
  168.         $nice = 'Unknown';
  169.         switch( $post_status ) {
  170.         case 'publish':
  171.             $nice = _x( 'Published', 'post' );
  172.             break;
  173.         case 'future':
  174.             $nice = _x( 'Scheduled', 'post' );
  175.             break;
  176.         case 'draft':
  177.             $nice = _x( 'Draft', 'post' );
  178.             break;
  179.         case 'pending':
  180.             $nice = _x( 'Pending', 'post' );
  181.             break;
  182.         case 'private':
  183.             $nice = _x( 'Private', 'post' );
  184.             break;
  185.         case 'trash':
  186.             $nice = _x( 'Trash', 'post' );
  187.             break;
  188.         case 'auto-draft':
  189.         case 'inherit':
  190.             $nice = $post_status;
  191.             break;
  192.         }
  193.         return $nice;
  194.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement