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