Better Notifications for WordPress - Version 1.2

Version Description

  • New: WYSIWYG Editor for notifications!
  • New: From Name, From Email, CC and BCC options are now available to use for each notification!
  • New: 'Send Me a Test Email' button. Save your notification first and then send yourself a test email! This will only go to you and not to any of the other users selected in the notification. Shortcodes will not be expanded but will be shown in place.
  • Improved: The User Role / Users drop down box has been unified for simplicity.
  • Fixed: Lost Password (For User) notifications were being sent in plain text.
  • Fixed: Not all custom post types were showing when setting up a new notification.
  • Fixed: Notifications for New Terms not generating notification emails.
  • Fixed: Custom Taxonomies missing from columns in the All Notifications screen.
  • Fixed: Added a space after lists of tags, categories, and terms.
  • Thank you again for all of your support, feedback, and awesome reviews. You people make WordPress great!
Download this release

Release Info

Developer voltronik
Plugin Icon 128x128 Better Notifications for WordPress
Version 1.2
Comparing to
See all releases

Code changes from version 1.1.5.3 to 1.2

README.txt CHANGED
@@ -1,9 +1,9 @@
1
  === Better Notifications for WordPress ===
2
  Contributors: voltronik
3
- Tags: notifications, email, mail, alerts, roles, user, users, admin, HTML, plain, wp_mail, shortcode, customize, post, page, updated, pending review, scheduled, category, tag, term, custom post type, comment, akismet, trackback, pingback, lost password, welcome, new user, bulk, notice, trigger
4
  Requires at least: 3.5
5
- Tested up to: 4.2.1
6
- Stable tag: 1.1.5.3
7
  License: GPLv2 or later
8
 
9
  Send customisable HTML emails to your users for different WordPress notifications.
@@ -124,6 +124,10 @@ Many hosts place a limit on the number of emails that can be sent out within an
124
 
125
  It's possible you're inserting a shortcode into a notification that cannot use it. For example: the 'New Category' email notification cannot use any of the author or time shortcodes as WordPress only stores the category name, category slug and category description in the database. It's also worth checking the spelling and underscores in any shortcodes as well as if they are wrapped in square brackets `[]`.
126
 
 
 
 
 
127
  = Custom Post Type 'X' isn't showing in the list of available custom post types =
128
 
129
  This is most likely because it's `public` setting is set to `false`. Try changing this and see if it shows up in the list. If the custom post type has been created by a plugin and is set to private (such as [TablePress](https://wordpress.org/plugins/tablepress/ "TablePress")), you'll need to get in touch with the plugin author to see if they'll consider changing it to public instead so that BNFW can send out notifications for it.
@@ -150,6 +154,18 @@ It might do but this is untested.
150
 
151
  == Changelog ==
152
 
 
 
 
 
 
 
 
 
 
 
 
 
153
  = 1.1.5.3 =
154
  * Compatibility with WordPress 4.2.
155
 
1
  === Better Notifications for WordPress ===
2
  Contributors: voltronik
3
+ Tags: notifications, email, mail, alerts, roles, user, users, admin, HTML, plain, wp_mail, shortcode, customize, post, page, updated, pending review, scheduled, category, tag, term, custom post type, comment, akismet, trackback, pingback, lost password, welcome, new user, bulk, notice, trigger, CC, BCC, from
4
  Requires at least: 3.5
5
+ Tested up to: 4.2.2
6
+ Stable tag: 1.2
7
  License: GPLv2 or later
8
 
9
  Send customisable HTML emails to your users for different WordPress notifications.
124
 
125
  It's possible you're inserting a shortcode into a notification that cannot use it. For example: the 'New Category' email notification cannot use any of the author or time shortcodes as WordPress only stores the category name, category slug and category description in the database. It's also worth checking the spelling and underscores in any shortcodes as well as if they are wrapped in square brackets `[]`.
126
 
127
+ = I press the 'Send Me a Test Email' button but nothing happens! =
128
+
129
+ Check that you've saved your notification first, then try again. It may take a second for the email to come through. Please also check your email SPAM filter.
130
+
131
  = Custom Post Type 'X' isn't showing in the list of available custom post types =
132
 
133
  This is most likely because it's `public` setting is set to `false`. Try changing this and see if it shows up in the list. If the custom post type has been created by a plugin and is set to private (such as [TablePress](https://wordpress.org/plugins/tablepress/ "TablePress")), you'll need to get in touch with the plugin author to see if they'll consider changing it to public instead so that BNFW can send out notifications for it.
154
 
155
  == Changelog ==
156
 
157
+ = 1.2 =
158
+ * New: WYSIWYG Editor for notifications!
159
+ * New: From Name, From Email, CC and BCC options are now available to use for each notification!
160
+ * New: 'Send Me a Test Email' button. Save your notification first and then send yourself a test email! This will only go to you and not to any of the other users selected in the notification. Shortcodes will not be expanded but will be shown in place.
161
+ * Improved: The User Role / Users drop down box has been unified for simplicity.
162
+ * Fixed: Lost Password (For User) notifications were being sent in plain text.
163
+ * Fixed: Not all custom post types were showing when setting up a new notification.
164
+ * Fixed: Notifications for New Terms not generating notification emails.
165
+ * Fixed: Custom Taxonomies missing from columns in the All Notifications screen.
166
+ * Fixed: Added a space after lists of tags, categories, and terms.
167
+ * Thank you again for all of your support, feedback, and awesome reviews. You people make WordPress great!
168
+
169
  = 1.1.5.3 =
170
  * Compatibility with WordPress 4.2.
171
 
assets/js/bnfw.js CHANGED
@@ -10,37 +10,35 @@ jQuery(document).ready(function($) {
10
  }
11
 
12
  $(".select2").select2();
13
- //toggle_fields();
14
 
15
- if ( 'user-password' == $('#notification').val() || 'new-user' == $('#notification').val() || 'welcome-email' == $('#notification').val() ) {
16
- //$('#toggle-fields, #email, #cc, #bcc, #user-role').hide();
17
- $('#toggle-fields, #user-role').hide();
18
  $('#user-password-msg').show();
19
  } else {
20
- $('#toggle-fields, #user-role').show();
21
- //toggle_fields();
22
  $('#user-password-msg').hide();
23
  }
24
 
25
  $('#notification').on('change', function() {
26
  var $this = $(this);
27
  if ( 'user-password' === $this.val() || 'new-user' === $this.val() || 'welcome-email' == $this.val() ) {
28
- //$('#toggle-fields, #email, #cc, #bcc, #user-role').hide();
29
- $('#toggle-fields, #user-role').hide();
30
  $('#user-password-msg').show();
31
  } else {
32
- $('#toggle-fields, #user-role').show();
33
  $('#user-password-msg').hide();
34
- //toggle_fields();
35
  }
36
  });
37
 
38
- //$('#show-fields').change(function() {
39
- //toggle_fields();
40
- //});
41
-
42
- $("#bnfw_user_role_toggle, #bnfw_user_toggle").click(function() {
43
- $("#bnfw_user_role_container").toggle();
44
- $("#bnfw_user_container").toggle();
45
  });
 
 
 
 
 
46
  });
10
  }
11
 
12
  $(".select2").select2();
13
+ toggle_fields();
14
 
15
+ if ( 'user-password' == $('#notification').val() || 'new-user' == $('#notification').val() || 'welcome-email' == $('#notification') ) {
16
+ $('#toggle-fields, #email, #cc, #bcc, #users').hide();
 
17
  $('#user-password-msg').show();
18
  } else {
19
+ $('#toggle-fields, #users').show();
20
+ toggle_fields();
21
  $('#user-password-msg').hide();
22
  }
23
 
24
  $('#notification').on('change', function() {
25
  var $this = $(this);
26
  if ( 'user-password' === $this.val() || 'new-user' === $this.val() || 'welcome-email' == $this.val() ) {
27
+ $('#toggle-fields, #email, #cc, #bcc, #users').hide();
 
28
  $('#user-password-msg').show();
29
  } else {
30
+ $('#toggle-fields, #users').show();
31
  $('#user-password-msg').hide();
32
+ toggle_fields();
33
  }
34
  });
35
 
36
+ $('#show-fields').change(function() {
37
+ toggle_fields();
 
 
 
 
 
38
  });
39
+
40
+ // send test email
41
+ $( '#test-email' ).click(function() {
42
+ $( '#send-test-email' ).val( 'true' );
43
+ });
44
  });
bnfw.php CHANGED
@@ -3,7 +3,7 @@
3
  * Plugin Name: Better Notifications for WordPress
4
  * Plugin URI: http://wordpress.org/plugins/bnfw/
5
  * Description: Send customisable HTML emails to your users for different WordPress notifications.
6
- * Version: 1.1.5.3
7
  * Author: Voltronik
8
  * Author URI: http://www.voltronik.co.uk/
9
  * Author Email: plugins@voltronik.co.uk
@@ -108,6 +108,7 @@ class BNFW {
108
  add_action( 'lostpassword_post' , array( $this, 'on_lost_password' ) );
109
  add_filter( 'retrieve_password_title' , array( $this, 'change_password_email_title' ) );
110
  add_filter( 'retrieve_password_message' , array( $this, 'change_password_email_message' ), 10, 4 );
 
111
 
112
  add_filter( 'plugin_action_links' , array( $this, 'plugin_action_links' ), 10, 4 );
113
  }
@@ -316,6 +317,15 @@ class BNFW {
316
  return $message;
317
  }
318
 
 
 
 
 
 
 
 
 
 
319
  /**
320
  * Send notification for new uses.
321
  *
3
  * Plugin Name: Better Notifications for WordPress
4
  * Plugin URI: http://wordpress.org/plugins/bnfw/
5
  * Description: Send customisable HTML emails to your users for different WordPress notifications.
6
+ * Version: 1.2
7
  * Author: Voltronik
8
  * Author URI: http://www.voltronik.co.uk/
9
  * Author Email: plugins@voltronik.co.uk
108
  add_action( 'lostpassword_post' , array( $this, 'on_lost_password' ) );
109
  add_filter( 'retrieve_password_title' , array( $this, 'change_password_email_title' ) );
110
  add_filter( 'retrieve_password_message' , array( $this, 'change_password_email_message' ), 10, 4 );
111
+ add_filter( 'wp_mail_content_type' , array( $this, 'change_email_content_type' ) );
112
 
113
  add_filter( 'plugin_action_links' , array( $this, 'plugin_action_links' ), 10, 4 );
114
  }
317
  return $message;
318
  }
319
 
320
+ /**
321
+ * Change the email content type to HTML.
322
+ *
323
+ * @since 1.2
324
+ */
325
+ public function change_email_content_type() {
326
+ return 'text/html';
327
+ }
328
+
329
  /**
330
  * Send notification for new uses.
331
  *
includes/admin/class-bnfw-notification.php CHANGED
@@ -137,7 +137,6 @@ class BNFW_Notification {
137
  * @param unknown $post
138
  */
139
  public function render_settings_meta_box( $post ) {
140
- global $wp_roles;
141
  wp_nonce_field(
142
  // Action
143
  self::POST_TYPE,
@@ -209,7 +208,7 @@ class BNFW_Notification {
209
  'public' => true,
210
  '_builtin' => false,
211
  ), 'objects'
212
- );
213
 
214
  if ( count( $taxs > 0 ) ) {
215
  ?>
@@ -239,49 +238,58 @@ class BNFW_Notification {
239
  </td>
240
  </tr>
241
 
242
- <tr valign="top" id="user-role">
 
 
 
 
 
 
 
 
 
 
243
  <th scope="row">
244
- <?php _e( 'User Roles', 'bnfw' ); ?>
 
 
 
 
 
 
 
 
 
 
245
  </th>
246
- <?php
247
- $roles_style = '';
248
- $user_style = 'display:none';
249
 
250
- if ( count( $setting['users'] ) > 0 ) {
251
- $roles_style = 'display:none';
252
- $user_style = '';
253
- }
254
- ?>
255
  <td>
256
- <div id="bnfw_user_role_container" style="<?php echo $roles_style; ?>">
257
- <select multiple name="user-roles[]" class="select2" data-placeholder="Select User Role" style="width:75%">
258
- <?php
259
- $roles = $wp_roles->get_names();
 
260
 
261
- foreach ( $roles as $role ) {
262
- $selected = selected( true, in_array( $role, $setting['user-roles'] ), false );
263
- echo '<option value="', $role, '" ', $selected, '>', $role, '</option>';
264
- }
265
- ?>
266
- </select><br>
267
- <a id="bnfw_user_role_toggle" href="#"><?php _e( 'Define individual users instead', 'bnfw' );?></a>
268
- </div>
269
 
270
- <div id="bnfw_user_container" style="<?php echo $user_style; ?>">
271
- <select multiple name="users[]" class="select2" data-placeholder="Select Users" style="width:75%">
272
- <?php
273
- $users = get_users( array(
274
- 'order_by' => 'email',
275
- ) );
276
 
277
- foreach ( $users as $user ) {
278
- $selected = selected( true, in_array( $user->ID, $setting['users'] ), false );
279
- echo '<option value="', $user->ID, '" ', $selected, '>', $user->user_login, '</option>';
280
- }
281
- ?>
282
- </select><br>
283
- <a id="bnfw_user_toggle" href="#"><?php _e( 'Define user roles instead', 'bnfw' );?></a>
284
- </div>
285
  </td>
286
  </tr>
287
 
@@ -299,7 +307,7 @@ class BNFW_Notification {
299
  <?php _e( 'Message Body', 'bnfw' ); ?>
300
  </th>
301
  <td>
302
- <textarea name="message" rows="10" style="width:75%;"><?php echo esc_textarea( $setting['message'] ); ?></textarea>
303
  </td>
304
  </tr>
305
  </tbody>
@@ -308,32 +316,33 @@ class BNFW_Notification {
308
  }
309
 
310
  /**
311
- * Render user roles dropdown.
312
  *
313
- * @since 1.0
314
- * @param unknown $field
315
- * @param unknown $value
316
- * @param unknown $multiple (optional)
317
- * @param unknown $width (optional)
318
  */
319
- private function render_roles_dropdown( $field, $value, $multiple = '', $width = 25 ) {
320
  global $wp_roles;
321
  ?>
322
- <select <?php echo $multiple; ?> name="<?php echo $field; if ( ! empty( $multiple ) ) echo '[]';?>" id="<?php echo $field;?>" class="select2" data-placeholder="Select User Role" style="width:<?php echo $width; ?>%">
323
  <?php
324
  $roles = $wp_roles->get_names();
325
 
326
  foreach ( $roles as $role ) {
327
- if ( empty( $multiple ) ) {
328
- $selected = selected( $value, $role, false );
329
- } else {
330
- $selected = selected( true, in_array( $role, $value ), false );
331
- }
332
- echo '<option value="', $role, '" ', $selected, '>', $role, '</option>';
333
  }
334
  ?>
335
- </select>
 
336
  <?php
 
 
 
 
 
 
 
 
337
  }
338
 
339
  /**
@@ -384,29 +393,28 @@ class BNFW_Notification {
384
  $setting = array(
385
  'notification' => $_POST['notification'],
386
  'subject' => sanitize_text_field( $_POST['subject'] ),
387
- 'message' => $_POST['message'],
388
  'disabled' => isset( $_POST['disabled'] ) ? sanitize_text_field( $_POST['disabled'] ) : 'false',
389
- //'show-fields' => sanitize_text_field( $_POST['show-fields'] ),
390
  );
391
 
392
- if ( isset( $_POST['user-roles'] ) ) {
393
- $setting['user-roles'] = $_POST['user-roles'];
394
- $setting['users'] = array();
 
 
 
395
  } else {
396
- $setting['user-roles'] = array();
397
- $setting['users'] = $_POST['users'];
398
  }
399
 
400
- //if ( 'true' == $setting['show-fields'] ) {
401
- //$setting['from-name'] = sanitize_text_field( $_POST['from-name'] );
402
- //$setting['from-email'] = sanitize_email( $_POST['from-email'] );
403
- //$setting['cc-email'] = sanitize_email( $_POST['cc-email'] );
404
- //$setting['cc-roles'] = $_POST['cc-roles'];
405
- //$setting['bcc-email'] = sanitize_email( $_POST['bcc-email'] );
406
- //$setting['bcc-roles'] = $_POST['bcc-roles'];
407
- //}
408
-
409
  $this->save_settings( $post_id, $setting );
 
 
 
 
 
 
410
  }
411
 
412
  /**
@@ -436,15 +444,12 @@ class BNFW_Notification {
436
  'notification' => '',
437
  'from-name' => '',
438
  'from-email' => '',
439
- 'cc-email' => '',
440
- 'cc-roles' => '',
441
- 'bcc-email' => '',
442
- 'bcc-roles' => '',
443
- 'user-roles' => array(),
444
  'users' => array(),
445
  'subject' => '',
446
  'message' => '',
447
- //'show-fields' => 'false',
448
  'disabled' => 'false',
449
  );
450
 
@@ -457,6 +462,17 @@ class BNFW_Notification {
457
  }
458
  }
459
 
 
 
 
 
 
 
 
 
 
 
 
460
  return $setting;
461
  }
462
 
@@ -497,6 +513,14 @@ class BNFW_Notification {
497
  $setting = $this->read_settings( $post->ID );
498
  ?>
499
  <input type="checkbox" name="disabled" value="true" <?php checked( $setting['disabled'], 'true', true ); ?>><?php _e( 'Disable Notification', 'bnfw' ); ?>
 
 
 
 
 
 
 
 
500
  </div>
501
 
502
  <div id="major-publishing-actions">
@@ -577,17 +601,16 @@ class BNFW_Notification {
577
  /**
578
  * Custom columns for this post type.
579
  *
580
- *
581
  * @since 1.0
582
  * @filter manage_{post_type}_posts_columns
583
  * @param array $columns
584
  * @return array
585
  */
586
  public function columns_header( $columns ) {
587
- $columns['type'] = __( 'Notification Type', 'bnfw' );
588
- $columns['disabled'] = __( 'Enabled?', 'bnfw' );
589
- $columns['subject'] = __( 'Subject', 'bnfw' );
590
- $columns['user-roles'] = __( 'User Roles/Users', 'bnfw' );
591
 
592
  return $columns;
593
  }
@@ -615,21 +638,42 @@ class BNFW_Notification {
615
  case 'subject':
616
  echo ! empty( $setting['subject'] ) ? $setting['subject'] : '';
617
  break;
618
- case 'user-roles':
619
- if ( ! empty( $setting['users'] ) ) {
620
- $users = array();
621
- $user_query = new WP_User_Query( array( 'include' => $setting['users'] ) );
622
- foreach ( $user_query->results as $user ) {
623
- $users[] = $user->user_login;
624
- }
625
- echo implode( ', ', $users );
626
- } else {
627
- echo ! empty( $setting['user-roles'] ) ? implode( ', ', $setting['user-roles'] ) : '';
628
- }
629
  break;
630
  }
631
  }
632
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
633
  /**
634
  * Get name of the notification based on slug.
635
  *
@@ -731,4 +775,14 @@ class BNFW_Notification {
731
 
732
  return $actions;
733
  }
 
 
 
 
 
 
 
 
 
 
734
  }
137
  * @param unknown $post
138
  */
139
  public function render_settings_meta_box( $post ) {
 
140
  wp_nonce_field(
141
  // Action
142
  self::POST_TYPE,
208
  'public' => true,
209
  '_builtin' => false,
210
  ), 'objects'
211
+ );
212
 
213
  if ( count( $taxs > 0 ) ) {
214
  ?>
238
  </td>
239
  </tr>
240
 
241
+ <tr valign="top" id="toggle-fields">
242
+ <th>
243
+ <?php esc_attr_e( 'Additional Email Fields', 'bnfw' ); ?>
244
+ </th>
245
+ <td>
246
+ <input type="checkbox" id="show-fields" name="show-fields" value="true" <?php checked( $setting['show-fields'], 'true', true ); ?>>
247
+ <label for="show-fields"><?php esc_html_e( 'Show additional email fields', 'bnfw' ); ?></label>
248
+ </td>
249
+ </tr>
250
+
251
+ <tr valign="top" id="email">
252
  <th scope="row">
253
+ <?php _e( 'From Name and Email', 'bnfw' ); ?>
254
+ </th>
255
+ <td>
256
+ <input type="text" name="from-name" value="<?php echo $setting['from-name']; ?>" placeholder="Site Name" style="width: 37.35%">
257
+ <input type="email" name="from-email" value="<?php echo $setting['from-email']; ?>" placeholder="Admin Email" style="width: 37.3%">
258
+ </td>
259
+ </tr>
260
+
261
+ <tr valign="top" id="cc">
262
+ <th scope="row">
263
+ <?php _e( 'CC', 'bnfw' ); ?>
264
  </th>
 
 
 
265
 
 
 
 
 
 
266
  <td>
267
+ <select multiple name="cc[]" class="select2" data-placeholder="Select Users" style="width:75%">
268
+ <?php $this->render_users_dropdown( $setting['cc'] ); ?>
269
+ </select>
270
+ </td>
271
+ </tr>
272
 
273
+ <tr valign="top" id="bcc">
274
+ <th scope="row">
275
+ <?php _e( 'BCC', 'bnfw' ); ?>
276
+ </th>
 
 
 
 
277
 
278
+ <td>
279
+ <select multiple name="bcc[]" class="select2" data-placeholder="Select Users" style="width:75%">
280
+ <?php $this->render_users_dropdown( $setting['bcc'] ); ?>
281
+ </select>
282
+ </td>
283
+ </tr>
284
 
285
+ <tr valign="top" id="users">
286
+ <th scope="row">
287
+ <?php _e( 'Users', 'bnfw' ); ?>
288
+ </th>
289
+ <td>
290
+ <select multiple name="users[]" class="select2" data-placeholder="Select Users" style="width:75%">
291
+ <?php $this->render_users_dropdown( $setting['users'] ); ?>
292
+ </select>
293
  </td>
294
  </tr>
295
 
307
  <?php _e( 'Message Body', 'bnfw' ); ?>
308
  </th>
309
  <td>
310
+ <?php wp_editor( $setting['message'], 'notification_message', array( 'media_buttons' => false ) ); ?>
311
  </td>
312
  </tr>
313
  </tbody>
316
  }
317
 
318
  /**
319
+ * Render users dropdown.
320
  *
321
+ * @since 1.2
 
 
 
 
322
  */
323
+ private function render_users_dropdown( $selected_users ) {
324
  global $wp_roles;
325
  ?>
326
+ <optgroup label="User Roles">
327
  <?php
328
  $roles = $wp_roles->get_names();
329
 
330
  foreach ( $roles as $role ) {
331
+ $selected = selected( true, in_array( 'role-' . $role, $selected_users ), false );
332
+ echo '<option value="role-', $role, '" ', $selected, '>', $role, '</option>';
 
 
 
 
333
  }
334
  ?>
335
+ </optgroup>
336
+ <optgroup label="Users">
337
  <?php
338
+ $users = get_users( array(
339
+ 'order_by' => 'email',
340
+ ) );
341
+
342
+ foreach ( $users as $user ) {
343
+ $selected = selected( true, in_array( $user->ID, $selected_users ), false );
344
+ echo '<option value="', $user->ID, '" ', $selected, '>', $user->user_login, '</option>';
345
+ }
346
  }
347
 
348
  /**
393
  $setting = array(
394
  'notification' => $_POST['notification'],
395
  'subject' => sanitize_text_field( $_POST['subject'] ),
396
+ 'message' => $_POST['notification_message'],
397
  'disabled' => isset( $_POST['disabled'] ) ? sanitize_text_field( $_POST['disabled'] ) : 'false',
398
+ 'users' => $_POST['users'],
399
  );
400
 
401
+ if ( isset( $_POST['show-fields'] ) && 'true' == $_POST['show-fields'] ) {
402
+ $setting['show-fields'] = 'true';
403
+ $setting['from-name'] = sanitize_text_field( $_POST['from-name'] );
404
+ $setting['from-email'] = sanitize_email( $_POST['from-email'] );
405
+ $setting['cc'] = $_POST['cc'];
406
+ $setting['bcc'] = $_POST['bcc'];
407
  } else {
408
+ $setting['show-fields'] = 'false';
 
409
  }
410
 
 
 
 
 
 
 
 
 
 
411
  $this->save_settings( $post_id, $setting );
412
+
413
+ if ( isset( $_POST['send-test-email'] ) ) {
414
+ if ( 'true' == $_POST['send-test-email'] ) {
415
+ BNFW::factory()->engine->send_test_email( $setting );
416
+ }
417
+ }
418
  }
419
 
420
  /**
444
  'notification' => '',
445
  'from-name' => '',
446
  'from-email' => '',
447
+ 'cc' => array(),
448
+ 'bcc' => array(),
 
 
 
449
  'users' => array(),
450
  'subject' => '',
451
  'message' => '',
452
+ 'show-fields' => 'false',
453
  'disabled' => 'false',
454
  );
455
 
462
  }
463
  }
464
 
465
+ // compatibility code. This will be removed subsequently
466
+ $user_roles = get_post_meta( $post_id, self::META_KEY_PREFIX . 'user-roles', true );
467
+ if ( ! empty( $user_roles ) && is_array( $user_roles ) ) {
468
+ foreach ( $user_roles as $role ) {
469
+ $setting['users'][] = 'role-' . $role;
470
+ }
471
+
472
+ update_post_meta( $post_id, self::META_KEY_PREFIX . 'users', $setting['users'] );
473
+ delete_post_meta( $post_id, self::META_KEY_PREFIX . 'user-roles' );
474
+ }
475
+
476
  return $setting;
477
  }
478
 
513
  $setting = $this->read_settings( $post->ID );
514
  ?>
515
  <input type="checkbox" name="disabled" value="true" <?php checked( $setting['disabled'], 'true', true ); ?>><?php _e( 'Disable Notification', 'bnfw' ); ?>
516
+ <br>
517
+ <br>
518
+
519
+ <?php if ( 'publish' == $post->post_status ) { ?>
520
+ <input type="hidden" name="send-test-email" id="send-test-email" value="false">
521
+ <input name="test-email" type="submit" class="button button-secondary button-large" id="test-email" value="<?php esc_attr_e( 'Send Me a Test Email', 'bnfw' ); ?>">
522
+ <?php } ?>
523
+
524
  </div>
525
 
526
  <div id="major-publishing-actions">
601
  /**
602
  * Custom columns for this post type.
603
  *
 
604
  * @since 1.0
605
  * @filter manage_{post_type}_posts_columns
606
  * @param array $columns
607
  * @return array
608
  */
609
  public function columns_header( $columns ) {
610
+ $columns['type'] = __( 'Notification Type', 'bnfw' );
611
+ $columns['disabled'] = __( 'Enabled?', 'bnfw' );
612
+ $columns['subject'] = __( 'Subject', 'bnfw' );
613
+ $columns['users'] = __( 'User Roles/Users', 'bnfw' );
614
 
615
  return $columns;
616
  }
638
  case 'subject':
639
  echo ! empty( $setting['subject'] ) ? $setting['subject'] : '';
640
  break;
641
+ case 'users':
642
+ $users = $this->get_names_from_users( $setting['users'] );
643
+ echo implode( ', ', $users );
 
 
 
 
 
 
 
 
644
  break;
645
  }
646
  }
647
 
648
+ /**
649
+ * Get names from users.
650
+ *
651
+ * @since 1.2
652
+ */
653
+ private function get_names_from_users( $users ) {
654
+ $email_list = array();
655
+ $user_ids = array();
656
+ $user_roles = array();
657
+ $names_from_user_ids = array();
658
+
659
+ foreach ( $users as $user ) {
660
+ if ( $this->starts_with( $user, 'role-' ) ) {
661
+ $user_roles[] = str_replace( 'role-', '', $user );
662
+ } else {
663
+ $user_ids[] = absint( $user );
664
+ }
665
+ }
666
+
667
+ if ( ! empty( $user_ids ) ) {
668
+ $user_query = new WP_User_Query( array( 'include' => $user_ids ) );
669
+ foreach ( $user_query->results as $user ) {
670
+ $names_from_user_ids[] = $user->user_login;
671
+ }
672
+ }
673
+
674
+ return array_merge( $user_roles, $names_from_user_ids );
675
+ }
676
+
677
  /**
678
  * Get name of the notification based on slug.
679
  *
775
 
776
  return $actions;
777
  }
778
+
779
+ /**
780
+ * Find if a string starts with another string.
781
+ *
782
+ * @since 1.2
783
+ */
784
+ private function starts_with( $haystack, $needle ) {
785
+ // search backwards starting from haystack length characters from the end
786
+ return $needle === '' || strrpos( $haystack, $needle, -strlen( $haystack ) ) !== false;
787
+ }
788
  }
includes/engine/class-bnfw-engine.php CHANGED
@@ -7,6 +7,24 @@
7
 
8
  class BNFW_Engine {
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  /**
11
  * Send the notification email.
12
  *
@@ -22,7 +40,7 @@ class BNFW_Engine {
22
  $headers[] = 'Content-type: text/html';
23
 
24
  foreach ( $emails['to'] as $email ) {
25
- wp_mail( $email, $subject, $message, $headers );
26
  }
27
  }
28
 
@@ -48,7 +66,7 @@ class BNFW_Engine {
48
 
49
  $headers = array( 'Content-type: text/html' );
50
 
51
- wp_mail( $user->user_email, $subject, $message, $headers );
52
  }
53
 
54
  /**
@@ -176,10 +194,10 @@ class BNFW_Engine {
176
  $message = str_replace( '[post_scheduled_date_gmt]', 'Published', $message );
177
  }
178
 
179
- $category_list = implode( ',', wp_get_post_categories( $post_id, array( 'fields' => 'names' ) ) );
180
  $message = str_replace( '[post_category]', $category_list, $message );
181
 
182
- $tag_list = implode( ',', wp_get_post_tags( $post_id, array( 'fields' => 'names' ) ) );
183
  $message = str_replace( '[post_tag]', $tag_list, $message );
184
 
185
  $user_info = get_userdata( $post->post_author );
@@ -278,31 +296,54 @@ class BNFW_Engine {
278
  */
279
  private function get_emails( $setting ) {
280
  $emails = array();
281
- if ( ! empty( $setting['from-name'] ) && ! empty( $setting['from-email'] ) ) {
282
- $emails['from'] = $setting['from-name'] . ' <' . $setting['from-email'] . '>' ;
283
- } else {
284
- $emails['from'] = get_option( 'blogname' ) . ' <' . get_option( 'admin_email' ) . '>' ;
285
- }
286
 
287
  if ( ! empty( $setting['users'] ) ) {
288
- $emails['to'] = $this->get_emails_from_id( $setting['users'] );
289
- } else {
290
- $emails['to'] = $this->get_emails_from_role( $setting['user-roles'] );
291
  }
292
 
293
- $emails['cc'] = $this->get_emails_from_role( $setting['cc-roles'] );
294
- if ( ! empty( $setting['cc-email'] ) ) {
295
- $emails['cc'][] = $setting['cc-email'];
296
- }
 
 
 
 
 
 
297
 
298
- $emails['bcc'] = $this->get_emails_from_role( $setting['bcc-roles'] );
299
- if ( ! empty( $setting['bcc-email'] ) ) {
300
- $emails['bcc'][] = $setting['bcc-email'];
301
  }
302
 
303
  return $emails;
304
  }
305
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
306
  /**
307
  * Get user emails by user ids.
308
  *
@@ -312,9 +353,11 @@ class BNFW_Engine {
312
  */
313
  private function get_emails_from_id( $user_ids ) {
314
  $email_list = array();
315
- $user_query = new WP_User_Query( array( 'include' => $user_ids ) );
316
- foreach ( $user_query->results as $user ) {
317
- $email_list[] = $user->user_email;
 
 
318
  }
319
  return $email_list;
320
  }
@@ -347,6 +390,16 @@ class BNFW_Engine {
347
  return $email_list;
348
  }
349
 
 
 
 
 
 
 
 
 
 
 
350
  /**
351
  * Get User role name by label.
352
  *
@@ -369,20 +422,24 @@ class BNFW_Engine {
369
  * Generate email headers based on the emails.
370
  *
371
  * @since 1.0
372
- * @param unknown $emails
373
- * @return unknown
374
  */
375
  private function get_headers( $emails ) {
376
  $headers = array();
377
- return $headers;
378
 
379
- $headers[] = 'From:' . $emails['from'];
 
 
 
380
  if ( ! empty( $emails['cc'] ) ) {
381
  $headers[] = 'Cc:' . implode( ',', $emails['cc'] );
382
  }
383
  if ( ! empty( $emails['bcc'] ) ) {
384
  $headers[] = 'Bcc:' . implode( ',', $emails['bcc'] );
385
  }
 
 
386
  }
387
  }
388
  ?>
7
 
8
  class BNFW_Engine {
9
 
10
+ /**
11
+ * Send test email.
12
+ *
13
+ * @since 1.2
14
+ */
15
+ public function send_test_email( $setting ) {
16
+ $subject = $setting['subject'];
17
+ $message = '<p><strong>This is a test email. All shortcodes below will show in place but not be replaced with content.</strong></p>' . $setting['message'];
18
+
19
+ $current_user = wp_get_current_user();
20
+ $email = $current_user->user_email;
21
+
22
+ $headers = array();
23
+ $headers[] = 'Content-type: text/html';
24
+
25
+ wp_mail( $email, stripslashes( $subject ), wpautop( $message ), $headers );
26
+ }
27
+
28
  /**
29
  * Send the notification email.
30
  *
40
  $headers[] = 'Content-type: text/html';
41
 
42
  foreach ( $emails['to'] as $email ) {
43
+ wp_mail( $email, stripslashes( $subject ), wpautop( $message ), $headers );
44
  }
45
  }
46
 
66
 
67
  $headers = array( 'Content-type: text/html' );
68
 
69
+ wp_mail( $user->user_email, stripslashes( $subject ), wpautop( $message ), $headers );
70
  }
71
 
72
  /**
194
  $message = str_replace( '[post_scheduled_date_gmt]', 'Published', $message );
195
  }
196
 
197
+ $category_list = implode( ', ', wp_get_post_categories( $post_id, array( 'fields' => 'names' ) ) );
198
  $message = str_replace( '[post_category]', $category_list, $message );
199
 
200
+ $tag_list = implode( ', ', wp_get_post_tags( $post_id, array( 'fields' => 'names' ) ) );
201
  $message = str_replace( '[post_tag]', $tag_list, $message );
202
 
203
  $user_info = get_userdata( $post->post_author );
296
  */
297
  private function get_emails( $setting ) {
298
  $emails = array();
 
 
 
 
 
299
 
300
  if ( ! empty( $setting['users'] ) ) {
301
+ $emails['to'] = $this->get_emails_from_users( $setting['users'] );
 
 
302
  }
303
 
304
+ if ( 'true' == $setting['show-fields'] ) {
305
+ if ( ! empty( $setting['from-name'] ) && ! empty( $setting['from-email'] ) ) {
306
+ $emails['from'] = $setting['from-name'] . ' <' . $setting['from-email'] . '>' ;
307
+ } else {
308
+ $emails['from'] = get_option( 'blogname' ) . ' <' . get_option( 'admin_email' ) . '>' ;
309
+ }
310
+
311
+ if ( ! empty( $setting['cc'] ) ) {
312
+ $emails['cc'] = $this->get_emails_from_users( $setting['cc'] );
313
+ }
314
 
315
+ if ( ! empty( $setting['bcc'] ) ) {
316
+ $emails['bcc'] = $this->get_emails_from_users( $setting['bcc'] );
317
+ }
318
  }
319
 
320
  return $emails;
321
  }
322
 
323
+ /**
324
+ * Get emails from users.
325
+ *
326
+ * @since 1.2
327
+ */
328
+ private function get_emails_from_users( $users ) {
329
+ $email_list = array();
330
+ $user_ids = array();
331
+ $user_roles = array();
332
+
333
+ foreach ( $users as $user ) {
334
+ if ( $this->starts_with( $user, 'role-' ) ) {
335
+ $user_roles[] = str_replace( 'role-', '', $user );
336
+ } else {
337
+ $user_ids[] = absint( $user );
338
+ }
339
+ }
340
+
341
+ $emails_from_user_ids = $this->get_emails_from_id( $user_ids );
342
+ $emails_from_user_roles = $this->get_emails_from_role( $user_roles );
343
+
344
+ return array_merge( $emails_from_user_roles, $emails_from_user_ids );
345
+ }
346
+
347
  /**
348
  * Get user emails by user ids.
349
  *
353
  */
354
  private function get_emails_from_id( $user_ids ) {
355
  $email_list = array();
356
+ if ( is_array( $user_ids ) && count( $user_ids ) > 0 ) {
357
+ $user_query = new WP_User_Query( array( 'include' => $user_ids ) );
358
+ foreach ( $user_query->results as $user ) {
359
+ $email_list[] = $user->user_email;
360
+ }
361
  }
362
  return $email_list;
363
  }
390
  return $email_list;
391
  }
392
 
393
+ /**
394
+ * Find if a string starts with another string.
395
+ *
396
+ * @since 1.2
397
+ */
398
+ private function starts_with( $haystack, $needle ) {
399
+ // search backwards starting from haystack length characters from the end
400
+ return '' === $needle || strrpos( $haystack, $needle, -strlen( $haystack ) ) !== false;
401
+ }
402
+
403
  /**
404
  * Get User role name by label.
405
  *
422
  * Generate email headers based on the emails.
423
  *
424
  * @since 1.0
425
+ * @param array $emails
426
+ * @return array
427
  */
428
  private function get_headers( $emails ) {
429
  $headers = array();
 
430
 
431
+ if ( ! empty( $emails['from'] ) ) {
432
+ $headers[] = 'From:' . $emails['from'];
433
+ }
434
+
435
  if ( ! empty( $emails['cc'] ) ) {
436
  $headers[] = 'Cc:' . implode( ',', $emails['cc'] );
437
  }
438
  if ( ! empty( $emails['bcc'] ) ) {
439
  $headers[] = 'Bcc:' . implode( ',', $emails['bcc'] );
440
  }
441
+
442
+ return $headers;
443
  }
444
  }
445
  ?>
includes/tmp.php ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <tr valign="top" id="toggle-fields">
2
+ <th>
3
+ <?php esc_attr_e( 'Additional Email Fields', 'bnfw' ); ?>
4
+ </th>
5
+ <td>
6
+ <input type="checkbox" id="show-fields" name="show-fields" value="true" <?php checked( $setting['show-fields'], 'true', true ); ?>>
7
+ <?php esc_html_e( 'Show additional email fields', 'bnfw' ); ?>
8
+ </td>
9
+ </tr>
10
+
11
+ <tr valign="top" id="email">
12
+ <th scope="row">
13
+ <?php _e( 'From Name and Email', 'bnfw' ); ?>
14
+ </th>
15
+ <td>
16
+ <input type="text" name="from-name" value="<?php echo $setting['from-name']; ?>" placeholder="Site Name" style="width:37%">
17
+ <input type="email" name="from-email" value="<?php echo $setting['from-email']; ?>" placeholder="Admin Email" style="width:37%">
18
+ </td>
19
+ </tr>
20
+
21
+ <tr valign="top" id="cc">
22
+ <th scope="row">
23
+ <?php _e( 'CC', 'bnfw' ); ?>
24
+ </th>
25
+
26
+ <td>
27
+ <?php $this->render_roles_dropdown( 'cc-roles', $setting['cc-roles'] ); ?>
28
+ <input type="email" name="cc-email" value="<?php echo $setting['cc-email']; ?>" placeholder="Additional email addresses" style="width:50%;">
29
+ </td>
30
+ </tr>
31
+
32
+ <tr valign="top" id="bcc">
33
+ <th scope="row">
34
+ <?php _e( 'BCC', 'bnfw' ); ?>
35
+ </th>
36
+
37
+ <td>
38
+ <?php $this->render_roles_dropdown( 'bcc-roles', $setting['bcc-roles'] ); ?>
39
+ <input type="email" name="bcc-email" value="<?php echo $setting['bcc-email']; ?>" placeholder="Additional email addresses" style="width:50%;">
40
+ </td>
41
+ </tr>
42
+
43
+