PluginProbe
Email Post Changes / 0.7
Email Post Changes v0.7
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
← All changes | class.email-post-changes.php +27 -140 1.20.7 View file →
@@ -23,22 +23,16 @@
23 23 return $instance;
24 24 }
25 25
26 26 function __construct() {
27 - $this->defaults = apply_filters( 'email_post_changes_default_options', array(
28 - 'enable' => 1,
29 - 'users' => array(),
27 + $this->defaults = array(
30 28 'emails' => array( get_option( 'admin_email' ) ),
31 29 'post_types' => array( 'post', 'page' ),
32 30 'drafts' => 0,
33 - ) );
31 + );
34 32
35 - $options = $this->get_options();
36 -
37 - if ( $options['enable'] )
38 - add_action( 'wp_insert_post', array( &$this, 'wp_insert_post' ), 10, 2 );
39 - if ( current_user_can( 'manage_options' ) )
40 - add_action( 'admin_menu', array( &$this, 'admin_menu' ) );
33 + add_action( 'wp_insert_post', array( &$this, 'wp_insert_post' ), 10, 2 );
34 + add_action( 'admin_menu', array( &$this, 'admin_menu' ) );
41 35 }
42 36
43 37 function get_post_types() {
44 38 $post_types = get_post_types();
@@ -59,9 +53,9 @@
59 53 // The meat of the plugin
60 54 function wp_insert_post( $post_id, $post ) {
61 55 $options = $this->get_options();
62 56
63 - if ( ! $options['drafts'] && 'draft' == $post->post_status )
57 + if ( !$options['drafts'] && 'draft' == $post->post_status )
64 58 return;
65 59
66 60 if ( 'revision' == $post->post_type ) { // Revision is saved first
67 61 if ( wp_is_post_autosave( $post ) )
@@ -82,9 +76,9 @@
82 76 foreach ( _wp_post_revision_fields() as $field => $field_title ) {
83 77 $left = apply_filters( "_wp_post_revision_field_$field", $this->left_post->$field, $field );
84 78 $right = apply_filters( "_wp_post_revision_field_$field", $this->right_post->$field, $field );
85 79
86 - if ( !$diff = $this->wp_text_diff( $left, $right ) )
80 + if ( !$diff = wp_text_diff( $left, $right ) )
87 81 continue;
88 82 $html_diffs[$field_title] = $diff;
89 83
90 84 $left = normalize_whitespace( $left );
@@ -101,13 +95,10 @@
101 95
102 96 $identical = false;
103 97 }
104 98
105 - if ( $identical ) {
106 - $this->left_post = null;
107 - $this->right_post = null;
99 + if ( $identical )
108 100 return;
109 - }
110 101
111 102 // Grab the meta data
112 103 $the_author = get_the_author_meta( 'display_name', $this->left_post->post_author ); // The revision
113 104 $the_title = get_the_title( $this->right_post->ID ); // New title (may be same as old title)
@@ -192,43 +183,19 @@
192 183 null, // see hack in ::phpmailer_init_once()
193 184 sprintf( __( '[%s] %s changed: %s' ), $blogname, $post_type, $title ),
194 185 $html_diff
195 186 );
196 -
197 - $this->left_post = null;
198 - $this->right_post = null;
199 -
200 - do_action( 'email_post_changes_email_sent' );
201 187 }
202 188
203 189 /* Email hook */
204 190 function phpmailer_init_once( &$phpmailer ) {
205 - global $blog_id;
206 -
207 191 remove_action( 'phpmailer_init', array( &$this, 'phpmailer_init_once' ) );
208 192 $phpmailer->AltBody = $this->text_diff;
209 193
210 - $phpmailer->ClearAddresses(); // hack
194 + $phpmailer->to = array(); // Hack
211 195
212 196 $options = $this->get_options();
213 -
214 - $user_emails = array();
215 - foreach( $options['users'] as $user_id ) {
216 - if ( function_exists( 'is_multisite' ) && is_multisite() ) {
217 - if ( is_user_member_of_blog( $user_id, $blog_id ) )
218 - $user_emails[] = get_user_option( 'user_email', $user_id );
219 - } else {
220 - if ( $user_email = get_user_option( 'user_email', $user_id ) )
221 - $user_emails[] = $user_email;
222 - }
223 - }
224 -
225 - $emails = array_unique( array_merge( $options['emails'], $user_emails ) );
226 -
227 - if ( !count( $emails ) )
228 - $emails[] = get_option( 'admin_email');
229 -
230 - foreach ( $emails as $email )
197 + foreach ( $options['emails'] as $email )
231 198 $phpmailer->AddAddress( $email );
232 199
233 200 $phpmailer->AddReplyTo(
234 201 get_the_author_meta( 'email', $this->right_post->post_author ),
@@ -244,8 +211,9 @@
244 211 // 3.0
245 212 $post_type_object = get_post_type_object( $post_type );
246 213 if ( empty( $post_type_object->label ) )
247 214 return ucwords( str_replace( '_', ' ', $post_type ) );
215 +
248 216 return $post_type_object->label;
249 217 }
250 218
251 219 /* Admin */
@@ -252,11 +220,9 @@
252 220 function admin_menu() {
253 221 register_setting( self::OPTION_GROUP, self::OPTION, array( &$this, 'validate_options' ) );
254 222
255 223 add_settings_section( self::ADMIN_PAGE, __( 'Email Post Changes' ), array( &$this, 'settings_section' ), self::ADMIN_PAGE );
256 - add_settings_field( self::ADMIN_PAGE . '_enable', __( 'Enable' ), array( &$this, 'enable_setting' ), self::ADMIN_PAGE, self::ADMIN_PAGE );
257 - add_settings_field( self::ADMIN_PAGE . '_users', __( 'Users to Email' ), array( &$this, 'users_setting' ), self::ADMIN_PAGE, self::ADMIN_PAGE );
258 - add_settings_field( self::ADMIN_PAGE . '_emails', __( 'Additional Email Addresses' ), array( &$this, 'emails_setting' ), self::ADMIN_PAGE, self::ADMIN_PAGE );
224 + add_settings_field( self::ADMIN_PAGE . '_emails', __( 'Email Addresses' ), array( &$this, 'emails_setting' ), self::ADMIN_PAGE, self::ADMIN_PAGE );
259 225 add_settings_field( self::ADMIN_PAGE . '_post_types', __( 'Post Types' ), array( &$this, 'post_types_setting' ), self::ADMIN_PAGE, self::ADMIN_PAGE );
260 226 add_settings_field( self::ADMIN_PAGE . '_drafts', __( 'Drafts' ), array( &$this, 'drafts_setting' ), self::ADMIN_PAGE, self::ADMIN_PAGE );
261 227
262 228 add_options_page( __( 'Email Post Changes' ), __( 'Email Post Changes' ), 'manage_options', self::ADMIN_PAGE, array( &$this, 'admin_page' ) );
@@ -267,21 +233,10 @@
267 233 return $this->defaults;
268 234
269 235 $return = array();
270 236
271 - $return['enable'] = ( empty( $options['enable'] ) ) ? 0 : 1;
272 -
273 - if ( empty( $options['users'] ) || !is_array( $options ) ) {
274 - $return['users'] = $this->defaults['users'];
275 - } else {
276 - $return['users'] = $options['users'];
277 - }
278 -
279 237 if ( empty( $options['emails'] ) ) {
280 - if ( count( $return['users'] ) )
281 - $return['emails'] = array();
282 - else
283 - $return['emails'] = $this->defaults['emails'];
238 + $return['emails'] = $this->defaults['emails'];
284 239 } else {
285 240 if ( is_string( $options['emails'] ) )
286 241 $_emails = preg_split( '(\n|\r)', $options['emails'], -1, PREG_SPLIT_NO_EMPTY );
287 242 $_emails = array_unique( (array) $_emails );
@@ -287,14 +242,9 @@
287 242 $_emails = array_unique( (array) $_emails );
288 243 $emails = array_filter( $_emails, 'is_email' );
289 244 if ( $diff = array_diff( $_emails, $emails ) )
290 245 $return['invalid_emails'] = $diff;
291 - if ( $emails )
292 - $return['emails'] = $emails;
293 - elseif ( count( $return['users'] ) )
294 - $return['emails'] = array();
295 - else
296 - $return['emails'] = $this->defaults['emails'];
246 + $return['emails'] = $emails ? $emails : $this->defaults['emails'];
297 247 }
298 248
299 249 if ( empty( $options['post_types'] ) || !is_array( $options ) ) {
300 250 $return['post_types'] = $this->defaults['post_types'];
@@ -304,10 +254,8 @@
304 254 }
305 255
306 256 $return['drafts'] = ( empty( $options['drafts'] ) ) ? 0 : 1;
307 257
308 - do_action( 'email_post_changes_validate_options', $this->get_options(), $return );
309 -
310 258 return $return;
311 259 }
312 260
313 261 function admin_page() {
@@ -315,9 +263,10 @@
315 263 ?>
316 264
317 265 <div class="wrap">
318 266 <h2><?php _e( 'Email Post Changes' ); ?></h2>
319 -<?php if ( !empty( $options['invalid_emails'] ) && $_GET['updated'] ) : ?>
267 +<?php if ( !empty( $options['invalid_emails'] ) ) : ?>
268 +
320 269 <div class="error">
321 270 <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>
322 271 </div>
323 272 <?php endif; ?>
@@ -334,41 +283,17 @@
334 283 }
335 284
336 285 function settings_section() {} // stub
337 286
338 - function enable_setting() {
339 - $options = $this->get_options();
340 -?>
341 - <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>
342 -<?php
343 - }
344 -
345 - function users_setting() {
346 - $options = $this->get_options();
347 -?>
348 - <div style="overflow: auto; max-height: 300px;">
349 - <ul>
350 -<?php $users = get_users_of_blog();
351 - usort( $users, array( $this, 'sort_users_by_display_name' ) );
352 -
353 - foreach ( $users as $user ) : ?>
354 - <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>
355 -
356 -<?php endforeach; ?>
357 - </ul>
358 - </div>
359 -<?php
360 - }
361 -
362 - function sort_users_by_display_name( $a, $b ) {
363 - return strcmp( strtolower( $a->display_name ), strtolower( $b->display_name ) );
364 - }
365 -
366 287 function emails_setting() {
367 288 $options = $this->get_options();
368 289 ?>
369 290 <textarea rows="4" cols="40" style="width: 40em;" name="email_post_changes[emails]"><?php echo esc_html( join( "\n", $options['emails'] ) ); ?></textarea>
370 - <p class="description"><?php _e( 'One email address per line.' ); ?></p>
291 + <p class="description"><?php _e( 'Send emails to these addresses. One per line.' ); ?></p>
292 + <p class="description"><?php printf(
293 + __( "If blank, send emails to this site&#8217;s <a href='%s'>admin email address</a>." ),
294 + clean_url( get_bloginfo( 'wpurl' ) . '/wp-admin/options-general.php#admin_email' )
295 + ); ?></p>
371 296 <?php
372 297 }
373 298
374 299 function post_types_setting() {
@@ -373,15 +298,20 @@
373 298
374 299 function post_types_setting() {
375 300 $options = $this->get_options();
376 301 ?>
302 +
377 303 <ul>
378 -<?php foreach ( $this->get_post_types() as $post_type ) :
304 +<?php
305 + foreach ( $this->get_post_types() as $post_type ) :
379 306 $label = $this->get_post_type_label( $post_type );
380 307 ?>
381 308 <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>
382 309 <?php endforeach; ?>
310 +
383 311 </ul>
312 + <p class="description"><?php _e( 'Send emails when a post of these post types changes.' ); ?></p>
313 + <p class="description"><?php _e( 'If none are selected, &#8220;post&#8221; and &#8220;page&#8221; will be checked by default.' ); ?></p>
384 314 <?php
385 315 }
386 316
387 317 function drafts_setting() {
@@ -386,52 +316,9 @@
386 316
387 317 function drafts_setting() {
388 318 $options = $this->get_options();
389 319 ?>
320 +
390 321 <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>
391 322 <?php
392 323 }
393 -
394 - function wp_text_diff( $left_string, $right_string, $args = null ) {
395 - $defaults = array( 'title' => '', 'title_left' => '', 'title_right' => '' );
396 - $args = wp_parse_args( $args, $defaults );
397 -
398 - $left_string = normalize_whitespace( $left_string );
399 - $right_string = normalize_whitespace( $right_string );
400 - $left_lines = explode( "\n", $left_string );
401 - $right_lines = explode( "\n", $right_string );
402 -
403 - $text_diff = new Text_Diff( $left_lines, $right_lines );
404 - $renderer = new Email_Post_Changes_Diff();
405 - $diff = $renderer->render( $text_diff );
406 -
407 - if ( !$diff )
408 - return '';
409 -
410 - $r = "<table class='diff'>\n";
411 - $r .= "<col class='ltype' /><col class='content' /><col class='ltype' /><col class='content' />";
412 -
413 - if ( $args['title'] || $args['title_left'] || $args['title_right'] )
414 - $r .= "<thead>";
415 - if ( $args['title'] )
416 - $r .= "<tr class='diff-title'><th colspan='4'>$args[title]</th></tr>\n";
417 - if ( $args['title_left'] || $args['title_right'] ) {
418 - $r .= "<tr class='diff-sub-title'>\n";
419 - $r .= "\t<td></td><th>$args[title_left]</th>\n";
420 - $r .= "\t<td></td><th>$args[title_right]</th>\n";
421 - $r .= "</tr>\n";
422 - }
423 - if ( $args['title'] || $args['title_left'] || $args['title_right'] )
424 - $r .= "</thead>\n";
425 - $r .= "<tbody>\n$diff\n</tbody>\n";
426 - $r .= "</table>";
427 - return $r;
428 - }
429 -}
430 -
431 -if ( !class_exists( 'WP_Text_Diff_Renderer_Table' ) )
432 - require( ABSPATH . WPINC . '/wp-diff.php' );
433 -
434 -class Email_Post_Changes_Diff extends WP_Text_Diff_Renderer_Table {
435 - var $_leading_context_lines = 2;
436 - var $_trailing_context_lines = 2;
437 324 }