PluginProbe
Email Post Changes / 1.4
Email Post Changes v1.4
trunk 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.7.1 1.7.2
email-post-changes / class.email-post-changes.php

class.email-post-changes.php in Email Post Changes 1.4, at class.email-post-changes.php

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