Better Notifications for WordPress - Version 1.1

Version Description

  • Lots of cool new stuff and more to come soon!
  • Transactional notifications (notifications intended only for the end user) have been added as a new group in the notifications select box.
  • New Notification: Post Pending Review (for all post types).
  • New Notification: New User Registration (Transactional).
  • New Notification: Password Reset (Transactional).
  • New Notification: New User Welcome Email (Transactional).
  • Added an option to enable or disable each notification.
  • Better support for custom taxonomies and terms.
  • Fixed: A few PHP warnings were showing when using WP_DEBUG.
  • Fixed: HTML tags being stripped from [post_content] and [comment_content].
  • Removed: Some WordPress Multisite shortcodes that didn't work properly. Proper support for Multisite will come later.
  • If you liked this plugin, please leave a review. If you didn't or have a problem, please send me a message in the Support Forum.
Download this release

Release Info

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

Code changes from version 1.0.2 to 1.1

README.txt CHANGED
@@ -2,9 +2,9 @@
2
  Contributors: voltronik
3
  Tags: notifications, email, alerts, roles, users, HTML
4
  Requires at least: 3.5
5
- Tested up to: 4.1
6
- Stable tag: 1.0.2
7
- License: GPLv3
8
 
9
  Send customisable HTML emails to your users for different WordPress notifications.
10
 
@@ -19,19 +19,38 @@ You want a user with the editor role (or all users using the Editor role) to be
19
 
20
  Notifications that are currently available to use are:
21
 
22
- * New category
23
- * New Post
24
- * Post updated
25
- * New comment (and it's corresponding status)
26
- * New trackback
27
- * New pingback
28
- * New user registration (admin)
29
- * Lost password reset (admin)
30
- * New custom taxonomy
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  If you'd like to see a notification in the list above, please drop me a line in the forums and we'll see what we can do to add it.
33
 
34
- A long and handy list of shortcodes you can use is available [here](http://www.voltronik.co.uk/wordpress-plugins/better-notifications-for-wordpress/ "Shortcodes for use in Better Notifications for WordPress").
35
 
36
 
37
  Having problems? Please let me know via this plugin's forum so we can address them and let it act as a source of information for future reference and other users.
@@ -49,7 +68,7 @@ Don't think the plugin is worthy of 5 stars? Let me know on the forum and we'll
49
  1. Upload the 'bnfw' plugin folder to the '/wp-content/plugins/' directory or download it via your WordPress installation.
50
  2. Activate the plugin through the 'Plugins' menu in WordPress
51
  3. Use the settings in the new 'Notifications' Menu item in the sidebar to configure the plugin.
52
- 4. (Optional) Install a plugin to use SMTP instead of wp_mail(). We recommend WP-Mail-SMTP.
53
  5. Test by creating a notification.
54
 
55
 
@@ -60,16 +79,16 @@ Don't think the plugin is worthy of 5 stars? Let me know on the forum and we'll
60
 
61
  Shortcodes are little blocks of content inserted by WordPress, contained in square brackets. For example, if you want to add the content of a post to a notification for 'New Post Published', you can use the `[post_content]` shortcode to display this in the email that is sent out.
62
 
63
- Shortcodes can be used in the 'Subject' and 'Message Body' of your notifications.
64
 
65
- A long and handy list of shortcodes you can use is available [here](http://www.voltronik.co.uk/wordpress-plugins/better-notifications-for-wordpress/ "Shortcodes for use in Better Notifications for WordPress").
66
 
67
  = What are some scenarios this plugin could be used for? =
68
 
69
  * Outreach: A blog/news site with hundreds of subscribers and want to use it to alert them of new blog posts.
70
- * Communication: A small, internal WordPress site and use it to alert staff of new comments.
71
  * Monitor: A website for an awesome new product or service and use it for notifications of pingbacks and trackbacks.
72
- * Security: To receive alerts of password reset requests and their corresponding email address.
73
 
74
  The possibilities are endless!
75
 
@@ -95,7 +114,7 @@ Many hosts place a limit on the number of emails that can be sent out within an
95
 
96
  = Some of my shortcodes aren't working! =
97
 
98
- 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 hyphens in any shortcodes as well as if they are wrapped in square brackets '[]'.
99
 
100
  = Can I translate this plugin? =
101
 
@@ -119,8 +138,22 @@ It might do but this is untested.
119
 
120
  == Changelog ==
121
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  = 1.0.2 =
123
- * Bug fix for [ID] shortcode not outputting anything.
124
  * Bug fix for `[post_category]` showing as empty.
125
  * Added tags shortcode for use in certain notifications using `[post_tag]`.
126
 
2
  Contributors: voltronik
3
  Tags: notifications, email, alerts, roles, users, HTML
4
  Requires at least: 3.5
5
+ Tested up to: 4.1.1
6
+ Stable tag: 1.1
7
+ License: GPLv2 or later
8
 
9
  Send customisable HTML emails to your users for different WordPress notifications.
10
 
19
 
20
  Notifications that are currently available to use are:
21
 
22
+ **WordPress Defaults**
23
+
24
+ * New Comment / Comment Awaiting Moderation
25
+ * New Trackback
26
+ * New Pingback
27
+ * Lost Password (For Admin)
28
+ * New User Registration (For Admin)
29
+
30
+ **Transactional**
31
+
32
+ * Lost Password (For User)
33
+ * New User Registration (For User)
34
+ * New User - Welcome Email
35
+
36
+ **Posts / Custom Post Types**
37
+
38
+ * New Post Published
39
+ * Post Updated
40
+ * Post Pending Review
41
+
42
+ **Posts**
43
+
44
+ * New Category
45
+ * New Tag
46
+
47
+ **Custom Post Types**
48
+
49
+ * New Taxonomy / Term
50
 
51
  If you'd like to see a notification in the list above, please drop me a line in the forums and we'll see what we can do to add it.
52
 
53
+ > A long and handy list of shortcodes you can use is available [here](http://www.voltronik.co.uk/wordpress-plugins/better-notifications-for-wordpress/ "Shortcodes for use in Better Notifications for WordPress").
54
 
55
 
56
  Having problems? Please let me know via this plugin's forum so we can address them and let it act as a source of information for future reference and other users.
68
  1. Upload the 'bnfw' plugin folder to the '/wp-content/plugins/' directory or download it via your WordPress installation.
69
  2. Activate the plugin through the 'Plugins' menu in WordPress
70
  3. Use the settings in the new 'Notifications' Menu item in the sidebar to configure the plugin.
71
+ 4. (Optional) Install a plugin to use SMTP instead of `wp_mail()`. We recommend WP-Mail-SMTP.
72
  5. Test by creating a notification.
73
 
74
 
79
 
80
  Shortcodes are little blocks of content inserted by WordPress, contained in square brackets. For example, if you want to add the content of a post to a notification for 'New Post Published', you can use the `[post_content]` shortcode to display this in the email that is sent out.
81
 
82
+ Shortcodes can be used in the 'Subject' and 'Message Body' of your notifications, except for a select few (due to a restriction in WordPress).
83
 
84
+ > A long and handy list of shortcodes you can use is available [here](http://www.voltronik.co.uk/wordpress-plugins/better-notifications-for-wordpress/ "Shortcodes for use in Better Notifications for WordPress").
85
 
86
  = What are some scenarios this plugin could be used for? =
87
 
88
  * Outreach: A blog/news site with hundreds of subscribers and want to use it to alert them of new blog posts.
89
+ * Communication: A small, internal WordPress site and use it to alert staff of new posts or comments.
90
  * Monitor: A website for an awesome new product or service and use it for notifications of pingbacks and trackbacks.
91
+ * Security: To receive alerts of password reset requests and their corresponding user.
92
 
93
  The possibilities are endless!
94
 
114
 
115
  = Some of my shortcodes aren't working! =
116
 
117
+ 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 hyphens in any shortcodes as well as if they are wrapped in square brackets `[]`.
118
 
119
  = Can I translate this plugin? =
120
 
138
 
139
  == Changelog ==
140
 
141
+ = 1.1 =
142
+ * Lots of cool new stuff and more to come soon!
143
+ * Transactional notifications (notifications intended only for the end user) have been added as a new group in the notifications select box.
144
+ * New Notification: Post Pending Review (for all post types).
145
+ * New Notification: New User Registration (Transactional).
146
+ * New Notification: Password Reset (Transactional).
147
+ * New Notification: New User Welcome Email (Transactional).
148
+ * Added an option to enable or disable each notification.
149
+ * Better support for custom taxonomies and terms.
150
+ * Fixed: A few PHP warnings were showing when using `WP_DEBUG`.
151
+ * Fixed: HTML tags being stripped from `[post_content]` and `[comment_content]`.
152
+ * Removed: Some WordPress Multisite shortcodes that didn't work properly. Proper support for Multisite will come later.
153
+ * If you liked this plugin, please leave a review. If you didn't or have a problem, please send me a message in the Support Forum.
154
+
155
  = 1.0.2 =
156
+ * Bug fix for `[ID]` shortcode not outputting anything.
157
  * Bug fix for `[post_category]` showing as empty.
158
  * Added tags shortcode for use in certain notifications using `[post_tag]`.
159
 
assets/images/notification-enabled.png ADDED
Binary file
assets/js/bnfw.js CHANGED
@@ -1,5 +1,43 @@
1
  jQuery(document).ready(function($) {
 
 
 
 
 
 
 
 
 
 
2
  $(".select2").select2();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  $("#bnfw_user_role_toggle, #bnfw_user_toggle").click(function() {
5
  $("#bnfw_user_role_container").toggle();
1
  jQuery(document).ready(function($) {
2
+ function toggle_fields() {
3
+ var show_fields = $('#show-fields').is(":checked");
4
+
5
+ if ( show_fields ) {
6
+ $('#email, #cc, #bcc').show();
7
+ } else {
8
+ $('#email, #cc, #bcc').hide();
9
+ }
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();
bnfw.php CHANGED
@@ -1,257 +1,352 @@
1
  <?php
2
  /**
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.0.2
7
- Author: Voltronik
8
- Author URI: http://www.voltronik.co.uk/
9
- Author Email: plugins@voltronik.co.uk
10
- License: GPLv2 or later
11
- License URI: http://www.gnu.org/licenses/gpl-2.0.html
12
- Text Domain: bnfw
13
- Domain Path: languages/
14
- **/
15
 
16
  /**
17
- Copyright © 2014 Voltronik (plugins@voltronik.co.uk)
18
-
19
- This program is free software; you can redistribute it and/or modify
20
- it under the terms of the GNU General Public License, version 2, as
21
- published by the Free Software Foundation.
22
-
23
- This program is distributed in the hope that it will be useful,
24
- but WITHOUT ANY WARRANTY; without even the implied warranty of
25
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26
- GNU General Public License for more details.
27
-
28
- You should have received a copy of the GNU General Public License
29
- along with this program; if not, write to the Free Software
30
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31
- */
32
-
33
  class BNFW {
34
 
35
- /**
36
- * Constructor.
37
- *
38
- * @since 1.0
39
- */
40
- function __construct() {
41
- $this->load_textdomain();
42
- $this->includes();
43
- $this->hooks();
44
-
45
- $this->notifier = new BNFW_Notification;
46
- $this->engine = new BNFW_Engine;
47
- }
48
-
49
- /**
50
- * Loads the plugin language files
51
- *
52
- * @since 1.0
53
- */
54
- public function load_textdomain() {
55
- // Load localization domain
56
- $this->translations = dirname( plugin_basename( __FILE__ ) ) . '/languages/';
57
- load_plugin_textdomain( 'bnfw', false, $this->translations );
58
- }
59
-
60
- /**
61
- * Include required files.
62
- *
63
- * @since 1.0
64
- */
65
- public function includes() {
66
- // Load Engine and post type
67
- require_once( 'includes/engine/class-bnfw-engine.php' );
68
- require_once( 'includes/admin/class-bnfw-notification.php' );
69
-
70
- // Load Admin Pages
71
- if ( is_admin() ) {
72
- require_once( 'includes/admin/bnfw-settings.php' );
73
- }
74
-
75
- // uncomment for debugging
76
- //require_once ('includes/debug.php');
77
- }
78
-
79
- /**
80
- * Register Hooks.
81
- *
82
- * @since 1.0
83
- */
84
- public function hooks() {
85
- register_activation_hook( __FILE__, array( $this, 'activate' ) );
86
-
87
- add_action( 'draft_to_publish' , array( $this, 'publish_post' ) );
88
- add_action( 'publish_to_publish' , array( $this, 'update_post' ) );
89
-
90
- add_action( 'comment_post' , array( $this, 'comment_post' ) );
91
- add_action( 'trackback_post' , array( $this, 'trackback_post' ) );
92
- add_action( 'pingback_post' , array( $this, 'pingback_post' ) );
93
-
94
- add_action( 'user_register' , array( $this, 'user_register' ) );
95
- add_action( 'lostpassword_post' , array( $this, 'lost_password' ) );
96
-
97
- add_action( 'create_term' , array( $this, 'create_term' ), 10, 3 );
98
-
99
- add_filter( 'plugin_action_links' , array( $this, 'plugin_action_links' ), 10, 4 );
100
- }
101
-
102
- /**
103
- * Run this on first-time plugin activation
104
- *
105
- * @since 1.0
106
- */
107
- // importer
108
- public function activate() {
109
- require_once( dirname( __FILE__ ) . '/includes/import.php' );
110
- $importer = new BNFW_Import;
111
- $importer->import();
112
- }
113
-
114
- /**
115
- * Add 'Settings' link below BNFW in Plugins list.
116
- *
117
- * @since 1.0
118
- */
119
- public function plugin_action_links( $links, $file ) {
120
- $plugin_file = 'bnfw/bnfw.php';
121
- if ( $file == $plugin_file ) {
122
- $settings_link = '<a href="' . admin_url( 'admin.php?page=bnfw-settings' ) . '">' . 'Settings' . '</a>';
123
- array_unshift( $links, $settings_link );
124
- }
125
- return $links;
126
- }
127
-
128
- /**
129
- * When a new term is created.
130
- *
131
- * @since 1.0
132
- */
133
- public function create_term( $term_id, $tt_id, $taxonomy ) {
134
- if ( 'category' == $taxonomy ) {
135
- $this->send_notification( 'new-category', $term_id );
136
- } else {
137
- $this->send_notification( 'new-term', $term_id );
138
- }
139
- }
140
-
141
- /**
142
- * Fires when a post is created for the first time.
143
- *
144
- * @since 1.0
145
- */
146
- function publish_post( $post ) {
147
- $post_id = $post->ID;
148
- $post_type = $post->post_type;
149
-
150
- if ( BNFW_Notification::POST_TYPE != $post_type ) {
151
- $this->send_notification( 'new-' . $post_type, $post_id );
152
- }
153
- }
154
-
155
- /**
156
- * Fires when a post is updated.
157
- *
158
- * @since 1.0
159
- */
160
- function update_post( $post ) {
161
- $post_id = $post->ID;
162
- $post_type = $post->post_type;
163
-
164
- if ( BNFW_Notification::POST_TYPE != $post_type ) {
165
- $this->send_notification( 'update-' . $post_type, $post_id );
166
- }
167
- }
168
-
169
- /**
170
- * Send notification for new comments
171
- *
172
- * @since 1.0
173
- */
174
- function comment_post( $comment_id ) {
175
- $the_comment = get_comment( $comment_id );
176
- if ( $this->can_send_comment_notification( $the_comment ) ) {
177
- $this->send_notification( 'new-comment', $comment_id );
178
- }
179
- }
180
-
181
- /**
182
- * Send notification for new trackback
183
- *
184
- * @since 1.0
185
- */
186
- function trackback_post( $comment_id ){
187
- $the_comment = get_comment( $comment_id );
188
- if ( $this->can_send_comment_notification( $the_comment ) ) {
189
- $this->send_notification( 'new-trackback', $comment_id );
190
- }
191
- }
192
-
193
- /**
194
- * Send notification for new pingbacks
195
- *
196
- * @since 1.0
197
- */
198
- function pingback_post( $comment_id ) {
199
- $the_comment = get_comment( $comment_id );
200
- if ( $this->can_send_comment_notification( $the_comment ) ) {
201
- $this->send_notification( 'new-pingback', $comment_id );
202
- }
203
- }
204
-
205
- /**
206
- * Send notification for lost password.
207
- *
208
- * @since 1.0
209
- */
210
- function lost_password() {
211
- $user = get_user_by( 'login', trim( $_POST['user_login'] ) );
212
- if ( $user ) {
213
- $this->send_notification( 'user-password', $user->ID );
214
- }
215
- }
216
-
217
- /**
218
- * Send notification for new uses.
219
- *
220
- * @since 1.0
221
- */
222
- function user_register( $user_id ) {
223
- $this->send_notification( 'new-user', $user_id );
224
- }
225
-
226
- /**
227
- * Send notification based on type and ref id
228
- *
229
- * @access private
230
- * @since 1.0
231
- */
232
- private function send_notification( $type, $ref_id ) {
233
- $notifications = $this->notifier->get_notifications( $type );
234
- foreach ( $notifications as $notification ) {
235
- $this->engine->send_notification( $this->notifier->read_settings( $notification->ID ), $ref_id );
236
- }
237
- }
238
-
239
- /**
240
- * Can send comment notification or not
241
- *
242
- * @since 1.0
243
- */
244
- private function can_send_comment_notification( $comment ) {
245
- // Returns false if the comment is marked as spam AND admin has enabled suppression of spam
246
- $suppress_spam = get_option( 'bnfw_suppress_spam' );
247
- if ( '1' === $suppress_spam && ( 0 === strcmp( $comment->comment_approved, 'spam' ) ) ) {
248
- return false;
249
- }
250
- return true;
251
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
252
  }
253
 
254
  /* ------------------------------------------------------------------------ *
255
  * Fire up the plugin
256
  * ------------------------------------------------------------------------ */
257
- new BNFW;
1
  <?php
2
  /**
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
7
+ * Author: Voltronik
8
+ * Author URI: http://www.voltronik.co.uk/
9
+ * Author Email: plugins@voltronik.co.uk
10
+ * License: GPLv2 or later
11
+ * License URI: http://www.gnu.org/licenses/gpl-2.0.html
12
+ * Text Domain: bnfw
13
+ * Domain Path: languages/
14
+ */
15
 
16
  /**
17
+ * Copyright © 2014 Voltronik (plugins@voltronik.co.uk)
18
+ * This program is free software; you can redistribute it and/or modify
19
+ * it under the terms of the GNU General Public License, version 2, as
20
+ * published by the Free Software Foundation.
21
+ * This program is distributed in the hope that it will be useful,
22
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
+ * GNU General Public License for more details.
25
+ * You should have received a copy of the GNU General Public License
26
+ * along with this program; if not, write to the Free Software
27
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28
+ */
 
 
 
 
29
  class BNFW {
30
 
31
+ /**
32
+ * Constructor.
33
+ *
34
+ * @since 1.0
35
+ */
36
+ function __construct() {
37
+ $this->load_textdomain();
38
+ $this->includes();
39
+ $this->hooks();
40
+
41
+ $this->notifier = new BNFW_Notification;
42
+ $this->engine = new BNFW_Engine;
43
+ }
44
+
45
+ /**
46
+ * Factory method to return the instance of the class.
47
+ *
48
+ * Makes sure that only one instance is created.
49
+ *
50
+ * @return object Instance of the class
51
+ */
52
+ public static function factory() {
53
+ static $instance = false;
54
+ if ( ! $instance ) {
55
+ $instance = new self();
56
+ }
57
+ return $instance;
58
+ }
59
+
60
+ /**
61
+ * Loads the plugin language files
62
+ *
63
+ * @since 1.0
64
+ */
65
+ public function load_textdomain() {
66
+ // Load localization domain
67
+ $this->translations = dirname( plugin_basename( __FILE__ ) ) . '/languages/';
68
+ load_plugin_textdomain( 'bnfw', false, $this->translations );
69
+ }
70
+
71
+ /**
72
+ * Include required files.
73
+ *
74
+ * @since 1.0
75
+ */
76
+ public function includes() {
77
+ // Load Engine and post type
78
+ require_once 'includes/engine/class-bnfw-engine.php';
79
+ require_once 'includes/admin/class-bnfw-notification.php';
80
+ require_once 'includes/overrides.php';
81
+
82
+ // Load Admin Pages
83
+ if ( is_admin() ) {
84
+ require_once 'includes/admin/bnfw-settings.php';
85
+ }
86
+ }
87
+
88
+ /**
89
+ * Register Hooks.
90
+ *
91
+ * @since 1.0
92
+ */
93
+ public function hooks() {
94
+ register_activation_hook( __FILE__ , array( $this, 'activate' ) );
95
+
96
+ add_action( 'draft_to_publish' , array( $this, 'publish_post' ) );
97
+ add_action( 'publish_to_publish' , array( $this, 'update_post' ) );
98
+
99
+ add_action( 'draft_to_pending' , array( $this, 'pending_post' ) );
100
+ add_action( 'new_to_pending' , array( $this, 'pending_post' ) );
101
+ add_action( 'auto-draft_to_pending' , array( $this, 'pending_post' ) );
102
+
103
+ add_action( 'comment_post' , array( $this, 'comment_post' ) );
104
+ add_action( 'trackback_post' , array( $this, 'trackback_post' ) );
105
+ add_action( 'pingback_post' , array( $this, 'pingback_post' ) );
106
+
107
+ add_action( 'user_register' , array( $this, 'user_register' ) );
108
+ add_action( 'user_register' , array( $this, 'welcome_email' ) );
109
+
110
+ add_action( 'lostpassword_post' , array( $this, 'on_lost_password' ) );
111
+ add_filter( 'retrieve_password_title' , array( $this, 'change_password_email_title' ) );
112
+ add_filter( 'retrieve_password_message' , array( $this, 'change_password_email_message' ), 10, 4 );
113
+
114
+ add_action( 'create_term' , array( $this, 'create_term' ), 10, 3 );
115
+
116
+ add_filter( 'plugin_action_links' , array( $this, 'plugin_action_links' ), 10, 4 );
117
+ }
118
+
119
+ /**
120
+ * Run this on first-time plugin activation
121
+ *
122
+ * @since 1.0
123
+ */
124
+
125
+ /**
126
+ * importer
127
+ */
128
+ public function activate() {
129
+ require_once dirname( __FILE__ ) . '/includes/import.php';
130
+ $importer = new BNFW_Import;
131
+ $importer->import();
132
+ }
133
+
134
+ /**
135
+ * Add 'Settings' link below BNFW in Plugins list.
136
+ *
137
+ * @since 1.0
138
+ * @param unknown $links
139
+ * @param unknown $file
140
+ * @return unknown
141
+ */
142
+ public function plugin_action_links( $links, $file ) {
143
+ $plugin_file = 'bnfw/bnfw.php';
144
+ if ( $file == $plugin_file ) {
145
+ $settings_link = '<a href="' . admin_url( 'admin.php?page=bnfw-settings' ) . '">' . 'Settings' . '</a>';
146
+ array_unshift( $links, $settings_link );
147
+ }
148
+ return $links;
149
+ }
150
+
151
+ /**
152
+ * When a new term is created.
153
+ *
154
+ * @since 1.0
155
+ * @param int $term_id
156
+ * @param int $tt_id
157
+ * @param string $taxonomy
158
+ */
159
+ public function create_term( $term_id, $tt_id, $taxonomy ) {
160
+ $this->send_notification( 'new-' . $taxonomy, $term_id );
161
+ }
162
+
163
+ /**
164
+ * Fires when a post is created for the first time.
165
+ *
166
+ * @since 1.0
167
+ * @param unknown $post
168
+ */
169
+ function publish_post( $post ) {
170
+ $post_id = $post->ID;
171
+ $post_type = $post->post_type;
172
+
173
+ if ( BNFW_Notification::POST_TYPE != $post_type ) {
174
+ $this->send_notification( 'new-' . $post_type, $post_id );
175
+ }
176
+ }
177
+
178
+ /**
179
+ * Fires when a post is updated.
180
+ *
181
+ * @since 1.0
182
+ * @param unknown $post
183
+ */
184
+ function update_post( $post ) {
185
+ $post_id = $post->ID;
186
+ $post_type = $post->post_type;
187
+
188
+ if ( BNFW_Notification::POST_TYPE != $post_type ) {
189
+ $this->send_notification( 'update-' . $post_type, $post_id );
190
+ }
191
+ }
192
+
193
+ /**
194
+ * Fires when a post is pending for review.
195
+ *
196
+ * @since 1.1
197
+ * @param unknown $post
198
+ */
199
+ function pending_post( $post ) {
200
+ $post_id = $post->ID;
201
+ $post_type = $post->post_type;
202
+
203
+ if ( BNFW_Notification::POST_TYPE != $post_type ) {
204
+ $this->send_notification( 'pending-' . $post_type, $post_id );
205
+ }
206
+ }
207
+
208
+ /**
209
+ * Send notification for new comments
210
+ *
211
+ * @since 1.0
212
+ * @param unknown $comment_id
213
+ */
214
+ function comment_post( $comment_id ) {
215
+ $the_comment = get_comment( $comment_id );
216
+ if ( $this->can_send_comment_notification( $the_comment ) ) {
217
+ $this->send_notification( 'new-comment', $comment_id );
218
+ }
219
+ }
220
+
221
+ /**
222
+ * Send notification for new trackback
223
+ *
224
+ * @since 1.0
225
+ * @param unknown $comment_id
226
+ */
227
+ function trackback_post( $comment_id ) {
228
+ $the_comment = get_comment( $comment_id );
229
+ if ( $this->can_send_comment_notification( $the_comment ) ) {
230
+ $this->send_notification( 'new-trackback', $comment_id );
231
+ }
232
+ }
233
+
234
+ /**
235
+ * Send notification for new pingbacks
236
+ *
237
+ * @since 1.0
238
+ * @param unknown $comment_id
239
+ */
240
+ function pingback_post( $comment_id ) {
241
+ $the_comment = get_comment( $comment_id );
242
+ if ( $this->can_send_comment_notification( $the_comment ) ) {
243
+ $this->send_notification( 'new-pingback', $comment_id );
244
+ }
245
+ }
246
+
247
+ /**
248
+ * Send notification for lost password.
249
+ *
250
+ * @since 1.0
251
+ */
252
+ function on_lost_password() {
253
+ $user = get_user_by( 'login', trim( $_POST['user_login'] ) );
254
+ if ( $user ) {
255
+ $this->send_notification( 'admin-password', $user->ID );
256
+ }
257
+ }
258
+
259
+ /**
260
+ * Change the title of the password reset email that is sent to the user.
261
+ *
262
+ * @since 1.1
263
+ */
264
+ public function change_password_email_title( $title ) {
265
+ $notifications = $this->notifier->get_notifications( 'user-password' );
266
+ if ( count( $notifications ) > 0 ) {
267
+ // Ideally there should be only one notification for this type.
268
+ // If there are multiple notification then we will read data about only the last one
269
+ $setting = $this->notifier->read_settings( end( $notifications )->ID );
270
+ return $setting['subject'];
271
+ }
272
+
273
+ return $title;
274
+ }
275
+
276
+ /**
277
+ * Change the message of the password reset email.
278
+ *
279
+ * @since 1.1
280
+ */
281
+ public function change_password_email_message( $message, $key, $user_login, $user_data ) {
282
+ $notifications = $this->notifier->get_notifications( 'user-password' );
283
+ if ( count( $notifications ) > 0 ) {
284
+ // Ideally there should be only one notification for this type.
285
+ // If there are multiple notification then we will read data about only the last one
286
+ $setting = $this->notifier->read_settings( end( $notifications )->ID );
287
+
288
+ return $this->engine->handle_password_reset_shortcodes( $setting, $key, $user_login, $user_data );
289
+ }
290
+
291
+ return $message;
292
+ }
293
+
294
+ /**
295
+ * Send notification for new uses.
296
+ *
297
+ * @since 1.0
298
+ * @param unknown $user_id
299
+ */
300
+ function user_register( $user_id ) {
301
+ $this->send_notification( 'admin-user', $user_id );
302
+ }
303
+
304
+ /**
305
+ * New User - Welcome email
306
+ *
307
+ * @since 1.1
308
+ * @param int $user_id New user id
309
+ */
310
+ function welcome_email( $user_id ) {
311
+ $notifications = $this->notifier->get_notifications( 'welcome-email' );
312
+ foreach ( $notifications as $notification ) {
313
+ $this->engine->send_registration_email( $this->notifier->read_settings( $notification->ID ), get_userdata( $user_id ) );
314
+ }
315
+ }
316
+
317
+ /**
318
+ * Send notification based on type and ref id
319
+ *
320
+ * @access private
321
+ * @since 1.0
322
+ * @param unknown $type
323
+ * @param unknown $ref_id
324
+ */
325
+ private function send_notification( $type, $ref_id ) {
326
+ $notifications = $this->notifier->get_notifications( $type );
327
+ foreach ( $notifications as $notification ) {
328
+ $this->engine->send_notification( $this->notifier->read_settings( $notification->ID ), $ref_id );
329
+ }
330
+ }
331
+
332
+ /**
333
+ * Can send comment notification or not
334
+ *
335
+ * @since 1.0
336
+ * @param unknown $comment
337
+ * @return unknown
338
+ */
339
+ private function can_send_comment_notification( $comment ) {
340
+ // Returns false if the comment is marked as spam AND admin has enabled suppression of spam
341
+ $suppress_spam = get_option( 'bnfw_suppress_spam' );
342
+ if ( '1' === $suppress_spam && ( 0 === strcmp( $comment->comment_approved, 'spam' ) ) ) {
343
+ return false;
344
+ }
345
+ return true;
346
+ }
347
  }
348
 
349
  /* ------------------------------------------------------------------------ *
350
  * Fire up the plugin
351
  * ------------------------------------------------------------------------ */
352
+ BNFW::factory();
includes/admin/bnfw-settings.php CHANGED
@@ -1,20 +1,22 @@
1
  <?php
2
- /* ------------------------------------------------------------------------ *
3
  * Register the Admin pages and load the scripts action
4
- * ------------------------------------------------------------------------ */
5
 
6
- // Sub-menu pages
 
 
7
  function bnfw_admin_menu() {
8
 
9
- // New Notifications Sub-menu
10
- add_submenu_page(
11
- 'edit.php?post_type=bnfw_notification',
12
- __( 'Notification Settings', 'bnfw' ),
13
- __( 'Settings', 'bnfw' ),
14
- 'manage_options',
15
- 'bnfw-settings',
16
- 'bnfw_settings_page'
17
- );
18
  }
19
  // Add the Admin pages to the WordPress menu
20
  add_action( 'admin_menu', 'bnfw_admin_menu' );
@@ -23,22 +25,24 @@ add_action( 'admin_menu', 'bnfw_admin_menu' );
23
  * Menu Pages
24
  * ------------------------------------------------------------------------ */
25
 
26
- // Settings Page
 
 
27
  function bnfw_settings_page() {
28
- ob_start(); ?>
29
 
30
  <div class="wrap">
31
  <?php screen_icon(); ?>
32
- <h2><?php _e( 'BNFW Settings', 'bnfw'); ?></h2>
33
 
34
  <form method="post" action="options.php" class="bnfw-form">
35
- <?php
36
- settings_errors();
37
- settings_fields('bnfw-settings');
38
- do_settings_sections( 'bnfw-settings' );
39
 
40
- submit_button( 'Save Settings' );
41
- ?>
42
  </form>
43
  </div>
44
 
@@ -48,89 +52,58 @@ function bnfw_settings_page() {
48
  /* ------------------------------------------------------------------------ *
49
  * Settings Page - Setting Registration
50
  * ------------------------------------------------------------------------ */
51
- function bnfw_general_options() {
52
-
53
- // Set-up - General Options Section
54
- add_settings_section (
55
- 'bnfw_general_options_section', // Section ID
56
- '', // Title above settings section
57
- 'bnfw_general_options_callback', // Name of function that renders a description of the settings section
58
- 'bnfw-settings' // Page to show on
59
- );
60
-
61
- // Add - Specify Name, Email, CC and BCC Checkbox
62
- //add_settings_field (
63
- //'bnfw_specify_email_headers', // Field ID
64
- //__( 'Email Headers', 'bnfw' ), // Label to the left
65
- //'bnfw_general_options_section_checkbox', // Name of function that renders options on the page
66
- //'bnfw-settings', // Page to show on
67
- //'bnfw_general_options_section', // Associate with which settings section?
68
- //array(
69
- //__( 'Do you want to specify a Name, Email, CC and BCC for each notification?', 'bnfw' )
70
- //)
71
- //);
72
 
73
- // Suppress notifications for SPAM comments
74
- add_settings_field (
75
- 'bnfw_suppress_spam', // Field ID
76
- __( 'Suppress SPAM comment notification', 'bnfw' ), // Label to the left
77
- 'bnfw_suppress_spam_checkbox', // Name of function that renders options on the page
78
- 'bnfw-settings', // Page to show on
79
- 'bnfw_general_options_section', // Associate with which settings section?
80
- array(
81
- __( "Don't send notifications for comments marked as SPAM by Akismet", 'bnfw' )
82
- )
83
- );
84
-
85
- //// Add - Minimum access role dropdown
86
- //add_settings_field (
87
- //'bnfw_choose_admin', // Field ID
88
- //__( 'Set minimum role to have access to this plugin', 'bnfw' ), // Label to the left
89
- //'bnfw_choose_admin_dropdown', // Name of function that renders options on the page
90
- //'bnfw-settings', // Page to show on
91
- //'bnfw_general_options_section' // Associate with which settings section?
92
- //);
93
-
94
- // Register - Specify Name, Email, CC and BCC Checkbox
95
- //register_setting (
96
- //'bnfw-settings',
97
- //'bnfw_specify_email_headers'
98
- //);
99
-
100
- // Register - Suppress SPAM Checkbox
101
- register_setting (
102
- 'bnfw-settings',
103
- 'bnfw_suppress_spam'
104
- );
105
 
106
- //// Register - Minimum access role dropdown
107
- //register_setting (
108
- //'bnfw-settings',
109
- //'bnfw_choose_admin'
110
- //);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  }
112
- add_action('admin_init', 'bnfw_general_options');
113
 
114
  /* ------------------------------------------------------------------------ *
115
  * Settings Page - Settings Section Callbacks
116
  * ------------------------------------------------------------------------ */
117
 
 
 
 
118
  function bnfw_general_options_callback() {}
119
 
120
  /* ------------------------------------------------------------------------ *
121
  * Settings Page - Settings Field Callbacks
122
  * ------------------------------------------------------------------------ */
123
 
124
- // Specify Name, Email, CC and BCC Checkbox
125
- function bnfw_general_options_section_checkbox($args) {
126
- echo '<input type="checkbox" id="bnfw_specify_email_headers" name="bnfw_specify_email_headers" value="1" ' . checked(1, get_option('bnfw_specify_email_headers'), false) . '/>';
127
- echo '<label for="bnfw_specify_email_headers"> ' . $args[0] . '</label>';
128
- }
129
-
130
  /**
131
  * Suppress SPAM checkbox.
132
  *
133
  * @since 1.0
 
134
  */
135
  function bnfw_suppress_spam_checkbox( $args ) {
136
  ?>
@@ -138,125 +111,3 @@ function bnfw_suppress_spam_checkbox( $args ) {
138
  <label for="bnfw_suppress_spam"><?php echo $args[0]; ?></label>
139
  <?php
140
  }
141
-
142
- // Minimum access role dropdown
143
- function bnfw_choose_admin_dropdown($args) {
144
-
145
- echo '<select id="bnfw_choose_admin" name="bnfw_choose_admin">';
146
- global $wp_roles;
147
- $roles = $wp_roles->get_names();
148
-
149
- foreach($roles as $role) {
150
- echo '<option value="'.$role.'"' . selected(get_option('bnfw_choose_admin'), $role , false) . '>' . $role . '</option>';
151
- }
152
- echo '</select>';
153
- }
154
-
155
- /* ------------------------------------------------------------------------ *
156
- * Notification Generator - Setting Registration
157
- * ------------------------------------------------------------------------ */
158
- function bnfw_generator_options() {
159
-
160
- // Set-up - Generator Section
161
- add_settings_section (
162
- 'bnfw_generator_section', // Section ID
163
- '', // Title above settings section
164
- 'bnfw_generator_callback', // Name of function that renders a description of the settings section
165
- 'bnfw-generator' // Page to show on
166
- );
167
-
168
- // Add - Notification Name Text
169
- add_settings_field (
170
- 'bnfw_notification_name', // Field ID
171
- __( 'Notification Name', 'bnfw' ), // Label to the left
172
- 'bnfw_generator_notification_name', // Name of function that renders options on the page
173
- 'bnfw-generator', // Page to show on
174
- 'bnfw_generator_section' // Associate with which settings section?
175
- );
176
-
177
- // Add - Notification User Roles
178
- add_settings_field (
179
- 'bnfw_user_roles', // Field ID
180
- __( 'User Roles', 'bnfw' ), // Label to the left
181
- 'bnfw_generator_user_roles', // Name of function that renders options on the page
182
- 'bnfw-generator', // Page to show on
183
- 'bnfw_generator_section' // Associate with which settings section?
184
- );
185
-
186
- // Add - Notification Users
187
- add_settings_field (
188
- 'bnfw_users', // Field ID
189
- __( 'User', 'bnfw' ), // Label to the left
190
- 'bnfw_generator_users', // Name of function that renders options on the page
191
- 'bnfw-generator', // Page to show on
192
- 'bnfw_generator_section' // Associate with which settings section?
193
- );
194
-
195
-
196
- // Register - All Generator Fields
197
- register_setting ( 'bnfw-generator', 'bnfw_notification_name' );
198
- register_setting ( 'bnfw-generator', 'bnfw_user_roles' );
199
- register_setting ( 'bnfw-generator', 'bnfw_users' );
200
- }
201
- add_action('admin_init', 'bnfw_generator_options');
202
-
203
- /* ------------------------------------------------------------------------ *
204
- * Notification Generator - Form Section Callbacks
205
- * ------------------------------------------------------------------------ */
206
-
207
- function bnfw_generator_callback() {}
208
-
209
- /* ------------------------------------------------------------------------ *
210
- * Notification Generator - Field Callbacks
211
- * ------------------------------------------------------------------------ */
212
-
213
- function bnfw_generator_notification_name($args) {
214
-
215
- $settings = get_option( 'bnfw_notification_name' );
216
- // Show this field if page isn't bnfw-notifications.php
217
- if (strcmp(basename($_SERVER['PHP_SELF']), "bnfw-notifications.php") != 0) {
218
- echo '<input type="text" name="bnfw_notification_name" value="'. $settings .'">';
219
- }
220
- }
221
-
222
- function bnfw_generator_user_roles($args) {
223
-
224
- $settings = get_option( 'bnfw_user_roles' );
225
-
226
- echo '<select multiple name="bnfw_user_roles" id="' . $settings . '" class="select2 populate">';
227
- global $wp_roles;
228
- $roles = $wp_roles->get_names();
229
-
230
- echo '<option value="'.$settings.'" selected>' . $settings . '</option>';
231
-
232
- foreach($roles as $role) {
233
- echo '<option value="'.$role.'">' . $role . '</option>';
234
- }
235
- echo '</select>';
236
- }
237
-
238
-
239
- function bnfw_generator_users($args) {
240
-
241
- $user_roles = get_option( 'bnfw_user_roles' );
242
- $single_users = get_option( 'bnfw_users' );
243
-
244
- // Enable Users Checkbox
245
- echo '<div id="bnfw-users-enable" class="checkbox bnfw-field">
246
- <label for="bnfw-users-enable-checkbox">',
247
- __( 'Do you want to specify individual users from the user roles selected above?', 'bnfw' ),
248
- '</label>
249
- <input type="checkbox" id="bnfw-users-enable-checkbox">
250
- </div>';
251
-
252
- echo '<select multiple name="bnfw_users" id="' . $single_users . '" class="select2 populate">';
253
- global $wp_roles;
254
- $roles = $wp_roles->get_names();
255
-
256
- echo '<option value="'.$single_users.'" selected>' . $single_users . '</option>';
257
-
258
- foreach($roles as $role) {
259
- echo '<option value="'.$role.'">' . $role . '</option>';
260
- }
261
- echo '</select>';
262
- }
1
  <?php
2
+ /**
3
  * Register the Admin pages and load the scripts action
4
+ **/
5
 
6
+ /**
7
+ * Sub-menu pages
8
+ */
9
  function bnfw_admin_menu() {
10
 
11
+ // New Notifications Sub-menu
12
+ add_submenu_page(
13
+ 'edit.php?post_type=bnfw_notification',
14
+ __( 'Notification Settings', 'bnfw' ),
15
+ __( 'Settings', 'bnfw' ),
16
+ 'manage_options',
17
+ 'bnfw-settings',
18
+ 'bnfw_settings_page'
19
+ );
20
  }
21
  // Add the Admin pages to the WordPress menu
22
  add_action( 'admin_menu', 'bnfw_admin_menu' );
25
  * Menu Pages
26
  * ------------------------------------------------------------------------ */
27
 
28
+ /**
29
+ * Settings Page
30
+ */
31
  function bnfw_settings_page() {
32
+ ob_start(); ?>
33
 
34
  <div class="wrap">
35
  <?php screen_icon(); ?>
36
+ <h2><?php _e( 'BNFW Settings', 'bnfw' ); ?></h2>
37
 
38
  <form method="post" action="options.php" class="bnfw-form">
39
+ <?php
40
+ settings_errors();
41
+ settings_fields( 'bnfw-settings' );
42
+ do_settings_sections( 'bnfw-settings' );
43
 
44
+ submit_button( 'Save Settings' );
45
+ ?>
46
  </form>
47
  </div>
48
 
52
  /* ------------------------------------------------------------------------ *
53
  * Settings Page - Setting Registration
54
  * ------------------------------------------------------------------------ */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
+ /**
57
+ *
58
+ */
59
+ function bnfw_general_options() {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
+ // Set-up - General Options Section
62
+ add_settings_section (
63
+ 'bnfw_general_options_section', // Section ID
64
+ '', // Title above settings section
65
+ 'bnfw_general_options_callback', // Name of function that renders a description of the settings section
66
+ 'bnfw-settings' // Page to show on
67
+ );
68
+
69
+ // Suppress notifications for SPAM comments
70
+ add_settings_field (
71
+ 'bnfw_suppress_spam', // Field ID
72
+ __( 'Suppress SPAM comment notification', 'bnfw' ), // Label to the left
73
+ 'bnfw_suppress_spam_checkbox', // Name of function that renders options on the page
74
+ 'bnfw-settings', // Page to show on
75
+ 'bnfw_general_options_section', // Associate with which settings section?
76
+ array(
77
+ __( "Don't send notifications for comments marked as SPAM by Akismet", 'bnfw' )
78
+ )
79
+ );
80
+
81
+ // Register - Suppress SPAM Checkbox
82
+ register_setting (
83
+ 'bnfw-settings',
84
+ 'bnfw_suppress_spam'
85
+ );
86
  }
87
+ add_action( 'admin_init', 'bnfw_general_options' );
88
 
89
  /* ------------------------------------------------------------------------ *
90
  * Settings Page - Settings Section Callbacks
91
  * ------------------------------------------------------------------------ */
92
 
93
+ /**
94
+ *
95
+ */
96
  function bnfw_general_options_callback() {}
97
 
98
  /* ------------------------------------------------------------------------ *
99
  * Settings Page - Settings Field Callbacks
100
  * ------------------------------------------------------------------------ */
101
 
 
 
 
 
 
 
102
  /**
103
  * Suppress SPAM checkbox.
104
  *
105
  * @since 1.0
106
+ * @param unknown $args
107
  */
108
  function bnfw_suppress_spam_checkbox( $args ) {
109
  ?>
111
  <label for="bnfw_suppress_spam"><?php echo $args[0]; ?></label>
112
  <?php
113
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
includes/admin/class-bnfw-notification.php CHANGED
@@ -4,144 +4,148 @@
4
  *
5
  * @since 1.0
6
  */
 
7
  class BNFW_Notification {
8
 
9
- const POST_TYPE = 'bnfw_notification';
10
- const META_KEY_PREFIX = 'bnfw_';
11
-
12
- public function __construct() {
13
-
14
- add_action( 'init', array( $this, 'register_post_type' ) );
15
- add_action( 'do_meta_boxes', array( $this, 'remove_meta_boxes' ) );
16
- add_action( 'add_meta_boxes_' . self::POST_TYPE, array( $this, 'add_meta_boxes' ) );
17
- add_action( 'save_post', array( $this, 'save_meta_data' ) );
18
- add_filter( 'post_updated_messages', array( $this, 'post_updated_messages' ) );
19
-
20
- // Custom row actions.
21
- add_filter( 'post_row_actions', array( $this, 'custom_row_actions' ), 10, 2 );
22
-
23
- // Custom columns
24
- add_filter( sprintf( 'manage_%s_posts_columns', self::POST_TYPE ), array( $this, 'columns_header' ) );
25
- add_action( sprintf( 'manage_%s_posts_custom_column', self::POST_TYPE ), array( $this, 'custom_column_row' ), 10, 2 );
26
-
27
- // Enqueue scripts/styles and disables autosave for this post type.
28
- add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
29
- }
30
-
31
- /**
32
- * Register bnfw_notification custom post type.
33
- *
34
- * @since 1.0
35
- */
36
- public function register_post_type() {
37
- register_post_type( self::POST_TYPE, array(
38
- 'labels' => array(
39
- 'name' => __( 'Notifications', 'bnfw' ),
40
- 'singular_name' => __( 'Notification', 'bnfw' ),
41
- 'add_new' => __( 'Add New', 'bnfw' ),
42
- 'menu_name' => __( 'Notifications', 'bnfw' ),
43
- 'name_admin_bar' => __( 'Notifications', 'bnfw' ),
44
- 'add_new_item' => __( 'Add New Notification', 'bnfw' ),
45
- 'edit_item' => __( 'Edit Notification', 'bnfw' ),
46
- 'new_item' => __( 'New Notification', 'bnfw' ),
47
- 'view_item' => __( 'View Notification', 'bnfw' ),
48
- 'search_items' => __( 'Search Notifications', 'bnfw' ),
49
- 'not_found' => __( 'No Notifications found', 'bnfw' ),
50
- 'not_found_in_trash' => __( 'No Notifications found in trash', 'bnfw' ),
51
- 'all_items' => __( 'All Notifications', 'bnfw' )
52
- ),
53
- 'public' => false,
54
- 'show_in_nav_menus' => true,
55
- 'show_in_admin_bar' => true,
56
- 'has_archive' => false,
57
- 'show_ui' => true,
58
- 'show_in_menu' => true,
59
- 'menu_icon' => 'dashicons-email-alt',
60
- 'menu_position' => 100,
61
- 'rewrite' => false,
62
- 'map_meta_cap' => false,
63
- 'capabilities' => array(
64
-
65
- // meta caps (don't assign these to roles)
66
- 'edit_post' => 'manage_options',
67
- 'read_post' => 'manage_options',
68
- 'delete_post' => 'manage_options',
69
-
70
- // primitive/meta caps
71
- 'create_posts' => 'manage_options',
72
-
73
- // primitive caps used outside of map_meta_cap()
74
- 'edit_posts' => 'manage_options',
75
- 'edit_others_posts' => 'manage_options',
76
- 'publish_posts' => 'manage_options',
77
- 'read_private_posts' => 'manage_options',
78
-
79
- // primitive caps used inside of map_meta_cap()
80
- 'read' => 'manage_options',
81
- 'delete_posts' => 'manage_options',
82
- 'delete_private_posts' => 'manage_options',
83
- 'delete_published_posts' => 'manage_options',
84
- 'delete_others_posts' => 'manage_options',
85
- 'edit_private_posts' => 'manage_options',
86
- 'edit_published_posts' => 'manage_options',
87
- ),
88
-
89
- // What features the post type supports.
90
- 'supports' => array(
91
- 'title',
92
- ),
93
- ));
94
- }
95
-
96
- /**
97
- * Remove unwanted meta boxes.
98
- *
99
- * @since 1.0
100
- */
101
- public function remove_meta_boxes() {
102
- remove_meta_box( 'submitdiv', self::POST_TYPE, 'side' );
103
- }
104
-
105
- /**
106
- * Add meta box to the post editor screen.
107
- *
108
- * @since 1.0
109
- */
110
- public function add_meta_boxes() {
111
- add_meta_box(
112
- 'bnfw-post-notification', // Unique ID
113
- esc_html__( 'Notification Settings', 'bnfw'), // Title
114
- array( $this, 'render_settings_meta_box' ), // Callback function
115
- self::POST_TYPE, // Admin page (or post type)
116
- 'normal' // Context
117
- );
118
-
119
- add_meta_box(
120
- 'bnfw_submitdiv',
121
- __( 'Save Notification', 'bnfw' ),
122
- array( $this, 'render_submitdiv' ),
123
- self::POST_TYPE,
124
- 'side',
125
- 'core'
126
- );
127
- }
128
-
129
- /**
130
- * Render the settings meta box.
131
- *
132
- * @since 1.0
133
- */
134
- public function render_settings_meta_box( $post ) {
135
- global $wp_roles;
136
- wp_nonce_field(
137
- // Action
138
- self::POST_TYPE,
139
-
140
- // Name.
141
- self::POST_TYPE . '_nonce'
142
- );
143
-
144
- $setting = $this->read_settings( $post->ID );
 
 
 
145
  ?>
146
  <table class="form-table">
147
  <tbody>
@@ -155,99 +159,94 @@ class BNFW_Notification {
155
  <option value="new-comment" <?php selected( 'new-comment', $setting['notification'] );?>><?php _e( 'New Comment / Awaiting Moderation', 'bnfw' ); ?></option>
156
  <option value="new-trackback" <?php selected( 'new-trackback', $setting['notification'] );?>><?php _e( 'New Trackback', 'bnfw' );?></option>
157
  <option value="new-pingback" <?php selected( 'new-pingback', $setting['notification'] );?>><?php _e( 'New Pingback', 'bnfw' );?></option>
158
- <option value="user-password" <?php selected( 'user-password', $setting['notification'] );?>><?php _e( 'Password Reset', 'bnfw' );?></option>
159
- <option value="new-user" <?php selected( 'new-user', $setting['notification'] );?>><?php _e( 'New User Registration', 'bnfw' );?></option>
 
 
 
 
 
160
  </optgroup>
161
  <optgroup label="Posts">
162
  <option value="new-post" <?php selected( 'new-post', $setting['notification'] );?>><?php _e( 'New Post Published', 'bnfw' );?></option>
163
  <option value="update-post" <?php selected( 'update-post', $setting['notification'] );?>><?php _e( 'Post Updated', 'bnfw' );?></option>
164
- <!-- <option value="pending-post" <?php selected( 'pending-post', $setting['notification'] );?>>Post Pending Review</option> -->
165
- <option value="new-category" <?php selected( 'new-category', $setting['notification'] );?>><?php _e( 'New Category', 'bnfw' ); ?></option>
 
166
  </optgroup>
167
  <?php
168
- $types = get_post_types( array(
169
- '_builtin' => false
170
- ), 'names'
171
- );
172
-
173
- foreach ( $types as $type ) {
174
- if ( self::POST_TYPE != $type ) {
 
 
175
  ?>
176
  <optgroup label="<?php _e( 'Custom Post Type - ', 'bnfw' ); echo $type; ?>">
177
- <option value="new-<?php echo $type; ?>" <?php selected( 'new-' . $type, $setting['notification'] );?>><?php echo __( 'New ', 'bnfw' ), $type; ?></option>
178
- <option value="update-<?php echo $type; ?>" <?php selected( 'update-' . $type, $setting['notification'] );?>><?php echo __( 'Update ', 'bnfw' ), $type; ?></option>
179
- <-- <option value="pending-<?php echo $type; ?>" <?php selected( 'pending-' . $type, $setting['notification'] );?>><?php echo $type, __( ' Pending Review', 'bnfw' ); ?></option> -->
180
  </optgroup>
181
  <?php
182
- }
183
- }
 
 
 
 
 
 
 
 
 
 
184
  ?>
185
  <optgroup label="<?php _e( 'Custom Taxonomy', 'bnfw' );?>">
186
- <option value="new-term" <?php selected( 'new-term', $setting['notification'] );?>><?php _e( 'New Term', 'bnfw' ); ?></option>
187
  </optgroup>
188
- </select>
189
- </td>
190
- </tr>
191
  <?php
192
- if ( get_option( 'bnfw_specify_email_headers', 0 ) == 1 ) {
 
193
  ?>
194
- <tr valign="top">
195
- <th scope="row">
196
- <?php _e( 'From Name and Email', 'bnfw' ); ?>
197
- </th>
198
- <td>
199
- <input type="text" name="from-name" value="<?php echo $setting['from-name']; ?>" placeholder="Site Name" style="width:37%">
200
- <input type="email" name="from-email" value="<?php echo $setting['from-email']; ?>" placeholder="Admin Email" style="width:37%">
201
  </td>
202
  </tr>
203
 
204
- <tr valign="top">
205
- <th scope="row">
206
- <?php _e( 'CC', 'bnfw' ); ?>
207
- </th>
208
-
209
- <td>
210
- <?php $this->render_roles_dropdown( 'cc-roles', $setting['cc-roles'] ); ?>
211
- <input type="email" name="cc-email" value="<?php echo $setting['cc-email']; ?>" placeholder="Additional email addresses" style="width:50%;">
212
- </td>
213
  </tr>
214
 
215
- <tr valign="top">
216
- <th scope="row">
217
- <?php _e( 'BCC', 'bnfw' ); ?>
218
- </th>
219
-
220
- <td>
221
- <?php $this->render_roles_dropdown( 'bcc-roles', $setting['bcc-roles'] ); ?>
222
- <input type="email" name="bcc-email" value="<?php echo $setting['bcc-email']; ?>" placeholder="Additional email addresses" style="width:50%;">
223
- </td>
224
- </tr>
225
- <?php
226
- }
227
- ?>
228
- <tr valign="top">
229
  <th scope="row">
230
  <?php _e( 'User Roles', 'bnfw' ); ?>
231
  </th>
232
  <?php
233
- $roles_style = '';
234
- $user_style = 'display:none';
235
 
236
- if ( count( $setting['users'] ) > 0 ) {
237
- $roles_style = 'display:none';
238
- $user_style = '';
239
- }
240
  ?>
241
  <td>
242
  <div id="bnfw_user_role_container" style="<?php echo $roles_style; ?>">
243
  <select multiple name="user-roles[]" class="select2" data-placeholder="Select User Role" style="width:75%">
244
  <?php
245
- $roles = $wp_roles->get_names();
246
 
247
- foreach ( $roles as $role ) {
248
- $selected = selected( true, in_array( $role, $setting['user-roles'] ), false );
249
- echo '<option value="', $role, '" ', $selected, '>', $role, '</option>';
250
- }
251
  ?>
252
  </select><br>
253
  <a id="bnfw_user_role_toggle" href="#"><?php _e( 'Define individual users instead', 'bnfw' );?></a>
@@ -256,14 +255,14 @@ class BNFW_Notification {
256
  <div id="bnfw_user_container" style="<?php echo $user_style; ?>">
257
  <select multiple name="users[]" class="select2" data-placeholder="Select Users" style="width:75%">
258
  <?php
259
- $users = get_users( array (
260
- 'order_by' => 'email',
261
- ) );
262
-
263
- foreach ( $users as $user ) {
264
- $selected = selected( true, in_array( $user->ID, $setting['users'] ), false );
265
- echo '<option value="', $user->ID, '" ', $selected, '>', $user->user_login, '</option>';
266
- }
267
  ?>
268
  </select><br>
269
  <a id="bnfw_user_toggle" href="#"><?php _e( 'Define user roles instead', 'bnfw' );?></a>
@@ -291,167 +290,182 @@ class BNFW_Notification {
291
  </tbody>
292
  </table>
293
  <?php
294
- }
295
-
296
- /**
297
- * Render user roles dropdown.
298
- *
299
- * @since 1.0
300
- */
301
- private function render_roles_dropdown( $field, $value, $multiple = '', $width = 25 ) {
302
- global $wp_roles;
 
 
 
 
303
  ?>
304
- <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; ?>%">
305
  <?php
306
- $roles = $wp_roles->get_names();
307
-
308
- foreach( $roles as $role ) {
309
- if( empty( $multiple ) ) {
310
- $selected = selected( $value, $role, false );
311
- } else {
312
- $selected = selected( true, in_array( $role, $value ), false );
313
- }
314
- echo '<option value="', $role, '" ', $selected, '>', $role, '</option>';
315
- }
316
  ?>
317
  </select>
318
  <?php
319
- }
320
-
321
- /**
322
- * Enqueue scripts.
323
- *
324
- * @since 1.0
325
- */
326
- public function enqueue_scripts() {
327
- if ( self::POST_TYPE === get_post_type() ) {
328
- wp_dequeue_script( 'autosave' );
329
-
330
- wp_enqueue_style( 'select2', '//cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.min.css', array(), '3.5.2' );
331
- wp_enqueue_script( 'select2', '//cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.min.js', array( 'jquery' ), '3.5.2', true );
332
- wp_enqueue_script( 'bnfw', plugins_url( '../assets/js/bnfw.js', dirname( __FILE__ ) ), array( 'jquery' ), '0.1', true );
333
- }
334
- }
335
-
336
- /**
337
- * Save the meta box's post metadata.
338
- *
339
- * @since 1.0
340
- * @param int $post_id The ID of the post being saved.
341
- */
342
- public function save_meta_data( $post_id ) {
343
-
344
- if ( self::POST_TYPE !== get_post_type( $post_id ) ) {
345
- return;
346
- }
347
-
348
- // Check nonce.
349
- if ( empty( $_POST[ self::POST_TYPE . '_nonce' ] ) ) {
350
- return;
351
- }
352
-
353
- // Verify nonce.
354
- if ( ! wp_verify_nonce( $_POST[ self::POST_TYPE . '_nonce' ], self::POST_TYPE ) ) {
355
- return;
356
- }
357
-
358
- if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
359
- return;
360
- }
361
-
362
- if ( ! current_user_can( 'manage_options' ) ) {
363
- return;
364
- }
365
-
366
- $setting = array(
367
- 'notification' => $_POST['notification'],
368
- 'subject' => sanitize_text_field( $_POST['subject'] ),
369
- 'message' => $_POST['message'],
370
- );
371
-
372
- if ( isset( $_POST['user-roles'] ) ) {
373
- $setting['user-roles'] = $_POST['user-roles'];
374
- $setting['users'] = array();
375
- } else {
376
- $setting['user-roles'] = array();
377
- $setting['users'] = $_POST['users'];
378
- }
379
-
380
- if ( get_option( 'bnfw_specify_email_headers', 0 ) == 1 ) {
381
- $setting['from-name'] = sanitize_text_field( $_POST['from-name'] );
382
- $setting['from-email'] = sanitize_email( $_POST['from-email'] );
383
- $setting['cc-email'] = sanitize_email( $_POST['cc-email'] );
384
- $setting['cc-roles'] = $_POST['cc-roles'];
385
- $setting['bcc-email'] = sanitize_email( $_POST['bcc-email'] );
386
- $setting['bcc-roles'] = $_POST['bcc-roles'];
387
- }
388
-
389
- $this->save_settings( $post_id, $setting );
390
- }
391
-
392
- /**
393
- * Save settings in post meta.
394
- *
395
- * @since 1.0
396
- * @access private
397
- */
398
- private function save_settings( $post_id, $setting ) {
399
- foreach( $setting as $key => $value ) {
400
- update_post_meta( $post_id, self::META_KEY_PREFIX . $key, $value );
401
- }
402
- }
403
-
404
- /**
405
- * Read settings from post meta.
406
- *
407
- * @since 1.0
408
- */
409
- public function read_settings( $post_id ) {
410
- $setting = array();
411
- $default = array(
412
- 'notification' => '',
413
- 'from-name' => '',
414
- 'from-email' => '',
415
- 'cc-email' => '',
416
- 'cc-roles' => '',
417
- 'bcc-email' => '',
418
- 'bcc-roles' => '',
419
- 'user-roles' => array(),
420
- 'users' => array(),
421
- 'subject' => '',
422
- 'message' => '',
423
- );
424
-
425
- foreach( $default as $key => $default_value ) {
426
- $value = get_post_meta( $post_id, self::META_KEY_PREFIX . $key, true );
427
- if ( ! empty( $value ) ) {
428
- $setting[ $key ] = $value;
429
- } else {
430
- $setting[ $key ] = $default_value;
431
- }
432
- }
433
-
434
- return $setting;
435
- }
436
-
437
- /**
438
- * Change the post updated message for notification post type.
439
- *
440
- * @since 1.0
441
- */
442
- public function post_updated_messages( $messages ) {
443
- $messages[ self::POST_TYPE ] = array_fill( 0, 11, __( 'Notification saved.', 'bnfw' ) );
444
-
445
- return $messages;
446
- }
447
-
448
- /**
449
- * Render submit div meta box.
450
- *
451
- * @since 1.0
452
- */
453
- public function render_submitdiv() {
454
- global $post;
 
 
 
 
 
 
 
 
 
 
 
455
  ?>
456
  <div class="submitbox" id="submitpost">
457
 
@@ -461,25 +475,32 @@ class BNFW_Notification {
461
  </div>
462
 
463
  <?php // Always publish. ?>
464
- <input type="hidden" name="post_status" id="hidden_post_status" value="publish">
 
 
 
 
 
 
 
465
 
466
  <div id="major-publishing-actions">
467
 
468
  <div id="delete-action">
469
  <?php
470
- if ( ! EMPTY_TRASH_DAYS ) {
471
- $delete_text = __( 'Delete Permanently', 'bnfw' );
472
- } else {
473
- $delete_text = __( 'Move to Trash', 'bnfw' );
474
- }
475
- ?>
476
  <a class="submitdelete deletion" href="<?php echo get_delete_post_link( $post->ID ); ?>"><?php echo $delete_text; ?></a>
477
  </div>
478
 
479
  <div id="publishing-action">
480
  <span class="spinner"></span>
481
  <input name="original_publish" type="hidden" id="original_publish" value="<?php esc_attr_e( 'Save' ) ?>">
482
- <input name="save" type="submit" class="button button-primary button-large" id="publish" accesskey="p" value="<?php esc_attr_e('Save' ) ?>">
483
  </div>
484
  <div class="clear"></div>
485
 
@@ -490,138 +511,182 @@ class BNFW_Notification {
490
  </div>
491
  <!-- #submitpost -->
492
  <?php
493
- }
494
-
495
- /**
496
- * Get notifications based on type.
497
- *
498
- * @since 1.0
499
- */
500
- public function get_notifications( $type ) {
501
- $args = array(
502
- 'post_type' => self::POST_TYPE,
503
- 'meta_query' => array(
504
- array(
505
- 'key' => self::META_KEY_PREFIX . 'notification',
506
- 'value' => $type,
507
- ),
508
- ),
509
- );
510
-
511
- $wp_query = new WP_Query();
512
- $posts = $wp_query->query( $args );
513
- return $posts;
514
- }
515
-
516
- /**
517
- * Custom columns for this post type.
518
- *
519
- * @param array $columns
520
- * @return array
521
- *
522
- * @since 1.0
523
- * @filter manage_{post_type}_posts_columns
524
- */
525
- public function columns_header( $columns ) {
526
- $columns['type'] = __( 'Notification Type', 'bnfw' );
527
- $columns['subject'] = __( 'Subject', 'bnfw' );
528
- $columns['user-roles'] = __( 'User Roles/Users', 'bnfw' );
529
-
530
- return $columns;
531
- }
532
-
533
- /**
534
- * Custom column appears in each row.
535
- *
536
- * @param string $column Column name
537
- * @param int $post_id Post ID
538
- *
539
- * @since 1.0
540
- * @action manage_{post_type}_posts_custom_column
541
- */
542
- public function custom_column_row( $column, $post_id ) {
543
- $setting = $this->read_settings( $post_id );
544
- switch ( $column ) {
545
- case 'type':
546
- echo $this->get_notifications_name( $setting['notification'] );
547
- break;
548
- case 'subject':
549
- echo ! empty( $setting['subject'] ) ? $setting['subject'] : '';
550
- break;
551
- case 'user-roles':
552
- if ( ! empty( $setting['users'] ) ) {
553
- $users = array();
554
- $user_query = new WP_User_Query( array( 'include' => $setting['users'] ) );
555
- foreach ( $user_query->results as $user ) {
556
- $users[] = $user->user_login;
557
- }
558
- echo implode( ', ', $users );
559
- } else {
560
- echo ! empty( $setting['user-roles'] ) ? implode( ', ', $setting['user-roles'] ) : '';
561
- }
562
- break;
563
- }
564
- }
565
-
566
- /**
567
- * Get name of the notification based on slug.
568
- *
569
- * @param mixed $slug
570
- */
571
- private function get_notifications_name( $slug ) {
572
- switch ($slug) {
573
- case 'new-comment':
574
- return __( 'New Comment', 'bnfw' );
575
- break;
576
- case 'new-trackback':
577
- return __( 'New Trackback', 'bnfw' );
578
- break;
579
- case 'new-pingback':
580
- return __( 'New Pingback', 'bnfw' );
581
- break;
582
- case 'user-password':
583
- return __( 'Password Reset', 'bnfw' );
584
- break;
585
- case 'new-user':
586
- return __( 'User Registration', 'bnfw' );
587
- break;
588
- case 'new-post':
589
- return __( 'New Post Published', 'bnfw' );
590
- break;
591
- case 'update-post':
592
- return __( 'Post Updated', 'bnfw' );
593
- break;
594
- case 'pending-post':
595
- return __( 'Post Pending Review', 'bnfw' );
596
- break;
597
- case 'new-category':
598
- return __( 'New Category', 'bnfw' );
599
- break;
600
- default:
601
- $splited = explode( '-', $slug );
602
- switch ( $splited[1] ) {
603
- case 'new':
604
- return __( 'New ', 'bnfw' ) . $splited[1];
605
- break;
606
- case 'update':
607
- return __( 'Updated ', 'bnfw' ) . $splited[1];
608
- break;
609
- case 'pending':
610
- return $splited[1] . __( ' Pending Review', 'bnfw' );
611
- break;
612
- }
613
- break;
614
- }
615
- }
616
 
617
  /**
618
- * Custom row actions for this post type.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
619
  *
620
- * @param array $actions
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
621
  * @return array
622
- *
623
- * @since 1.0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
624
  * @filter post_row_actions
 
 
625
  */
626
  public function custom_row_actions( $actions ) {
627
  $post = get_post();
4
  *
5
  * @since 1.0
6
  */
7
+
8
  class BNFW_Notification {
9
 
10
+ const POST_TYPE = 'bnfw_notification';
11
+ const META_KEY_PREFIX = 'bnfw_';
12
+
13
+ /**
14
+ *
15
+ */
16
+ public function __construct() {
17
+
18
+ add_action( 'init', array( $this, 'register_post_type' ) );
19
+ add_action( 'do_meta_boxes', array( $this, 'remove_meta_boxes' ) );
20
+ add_action( 'add_meta_boxes_' . self::POST_TYPE, array( $this, 'add_meta_boxes' ) );
21
+ add_action( 'save_post', array( $this, 'save_meta_data' ) );
22
+ add_filter( 'post_updated_messages', array( $this, 'post_updated_messages' ) );
23
+
24
+ // Custom row actions.
25
+ add_filter( 'post_row_actions', array( $this, 'custom_row_actions' ), 10, 2 );
26
+
27
+ // Custom columns
28
+ add_filter( sprintf( 'manage_%s_posts_columns', self::POST_TYPE ), array( $this, 'columns_header' ) );
29
+ add_action( sprintf( 'manage_%s_posts_custom_column', self::POST_TYPE ), array( $this, 'custom_column_row' ), 10, 2 );
30
+
31
+ // Enqueue scripts/styles and disables autosave for this post type.
32
+ add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
33
+ }
34
+
35
+ /**
36
+ * Register bnfw_notification custom post type.
37
+ *
38
+ * @since 1.0
39
+ */
40
+ public function register_post_type() {
41
+ register_post_type( self::POST_TYPE, array(
42
+ 'labels' => array(
43
+ 'name' => __( 'Notifications', 'bnfw' ),
44
+ 'singular_name' => __( 'Notification', 'bnfw' ),
45
+ 'add_new' => __( 'Add New', 'bnfw' ),
46
+ 'menu_name' => __( 'Notifications', 'bnfw' ),
47
+ 'name_admin_bar' => __( 'Notifications', 'bnfw' ),
48
+ 'add_new_item' => __( 'Add New Notification', 'bnfw' ),
49
+ 'edit_item' => __( 'Edit Notification', 'bnfw' ),
50
+ 'new_item' => __( 'New Notification', 'bnfw' ),
51
+ 'view_item' => __( 'View Notification', 'bnfw' ),
52
+ 'search_items' => __( 'Search Notifications', 'bnfw' ),
53
+ 'not_found' => __( 'No Notifications found', 'bnfw' ),
54
+ 'not_found_in_trash' => __( 'No Notifications found in trash', 'bnfw' ),
55
+ 'all_items' => __( 'All Notifications', 'bnfw' )
56
+ ),
57
+ 'public' => false,
58
+ 'show_in_nav_menus' => true,
59
+ 'show_in_admin_bar' => true,
60
+ 'has_archive' => false,
61
+ 'show_ui' => true,
62
+ 'show_in_menu' => true,
63
+ 'menu_icon' => 'dashicons-email-alt',
64
+ 'menu_position' => 100,
65
+ 'rewrite' => false,
66
+ 'map_meta_cap' => false,
67
+ 'capabilities' => array(
68
+
69
+ // meta caps (don't assign these to roles)
70
+ 'edit_post' => 'manage_options',
71
+ 'read_post' => 'manage_options',
72
+ 'delete_post' => 'manage_options',
73
+
74
+ // primitive/meta caps
75
+ 'create_posts' => 'manage_options',
76
+
77
+ // primitive caps used outside of map_meta_cap()
78
+ 'edit_posts' => 'manage_options',
79
+ 'edit_others_posts' => 'manage_options',
80
+ 'publish_posts' => 'manage_options',
81
+ 'read_private_posts' => 'manage_options',
82
+
83
+ // primitive caps used inside of map_meta_cap()
84
+ 'read' => 'manage_options',
85
+ 'delete_posts' => 'manage_options',
86
+ 'delete_private_posts' => 'manage_options',
87
+ 'delete_published_posts' => 'manage_options',
88
+ 'delete_others_posts' => 'manage_options',
89
+ 'edit_private_posts' => 'manage_options',
90
+ 'edit_published_posts' => 'manage_options',
91
+ ),
92
+
93
+ // What features the post type supports.
94
+ 'supports' => array(
95
+ 'title',
96
+ ),
97
+ ) );
98
+ }
99
+
100
+ /**
101
+ * Remove unwanted meta boxes.
102
+ *
103
+ * @since 1.0
104
+ */
105
+ public function remove_meta_boxes() {
106
+ remove_meta_box( 'submitdiv', self::POST_TYPE, 'side' );
107
+ }
108
+
109
+ /**
110
+ * Add meta box to the post editor screen.
111
+ *
112
+ * @since 1.0
113
+ */
114
+ public function add_meta_boxes() {
115
+ add_meta_box(
116
+ 'bnfw-post-notification', // Unique ID
117
+ esc_html__( 'Notification Settings', 'bnfw' ), // Title
118
+ array( $this, 'render_settings_meta_box' ), // Callback function
119
+ self::POST_TYPE, // Admin page (or post type)
120
+ 'normal' // Context
121
+ );
122
+
123
+ add_meta_box(
124
+ 'bnfw_submitdiv',
125
+ __( 'Save Notification', 'bnfw' ),
126
+ array( $this, 'render_submitdiv' ),
127
+ self::POST_TYPE,
128
+ 'side',
129
+ 'core'
130
+ );
131
+ }
132
+
133
+ /**
134
+ * Render the settings meta box.
135
+ *
136
+ * @since 1.0
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,
144
+ // Name.
145
+ self::POST_TYPE . '_nonce'
146
+ );
147
+
148
+ $setting = $this->read_settings( $post->ID );
149
  ?>
150
  <table class="form-table">
151
  <tbody>
159
  <option value="new-comment" <?php selected( 'new-comment', $setting['notification'] );?>><?php _e( 'New Comment / Awaiting Moderation', 'bnfw' ); ?></option>
160
  <option value="new-trackback" <?php selected( 'new-trackback', $setting['notification'] );?>><?php _e( 'New Trackback', 'bnfw' );?></option>
161
  <option value="new-pingback" <?php selected( 'new-pingback', $setting['notification'] );?>><?php _e( 'New Pingback', 'bnfw' );?></option>
162
+ <option value="admin-password" <?php selected( 'admin-password', $setting['notification'] );?>><?php _e( 'Lost Password - For Admin', 'bnfw' );?></option>
163
+ <option value="admin-user" <?php selected( 'admin-user', $setting['notification'] );?>><?php _e( 'New User Registration - For Admin', 'bnfw' );?></option>
164
+ </optgroup>
165
+ <optgroup label="Transactional">
166
+ <option value="user-password" <?php selected( 'user-password', $setting['notification'] );?>><?php _e( 'Lost Password - For User', 'bnfw' );?></option>
167
+ <option value="new-user" <?php selected( 'new-user', $setting['notification'] );?>><?php _e( 'New User Registration - For User', 'bnfw' );?></option>
168
+ <option value="welcome-email" <?php selected( 'welcome-email', $setting['notification'] );?>><?php _e( 'New User - Welcome Email', 'bnfw' );?></option>
169
  </optgroup>
170
  <optgroup label="Posts">
171
  <option value="new-post" <?php selected( 'new-post', $setting['notification'] );?>><?php _e( 'New Post Published', 'bnfw' );?></option>
172
  <option value="update-post" <?php selected( 'update-post', $setting['notification'] );?>><?php _e( 'Post Updated', 'bnfw' );?></option>
173
+ <option value="pending-post" <?php selected( 'pending-post', $setting['notification'] );?>><?php _e( 'Post Pending Review', 'bnfw' );?></option>
174
+ <option value="new-category" <?php selected( 'new-category', $setting['notification'] );?>><?php _e( 'New Category', 'bnfw' ); ?></option>
175
+ <option value="new-post_tag" <?php selected( 'new-post_tag', $setting['notification'] );?>><?php _e( 'New Tag', 'bnfw' ); ?></option>
176
  </optgroup>
177
  <?php
178
+ $types = get_post_types( array(
179
+ '_builtin' => false,
180
+ ), 'names'
181
+ );
182
+
183
+ foreach ( $types as $type ) {
184
+ if ( $type != self::POST_TYPE ) {
185
+ $post_obj = get_post_type_object( $type );
186
+ $label = $post_obj->labels->singular_name;
187
  ?>
188
  <optgroup label="<?php _e( 'Custom Post Type - ', 'bnfw' ); echo $type; ?>">
189
+ <option value="new-<?php echo $type; ?>" <?php selected( 'new-' . $type, $setting['notification'] );?>><?php echo __( 'New ', 'bnfw' ), "'$label'"; ?></option>
190
+ <option value="update-<?php echo $type; ?>" <?php selected( 'update-' . $type, $setting['notification'] );?>><?php echo "'$label' " . __( 'Update ', 'bnfw' ); ?></option>
191
+ <option value="pending-<?php echo $type; ?>" <?php selected( 'pending-' . $type, $setting['notification'] );?>><?php echo "'$label' ", __( 'Pending Review', 'bnfw' ); ?></option>
192
  </optgroup>
193
  <?php
194
+ }
195
+ }
196
+
197
+ $taxs = get_taxonomies( array(
198
+ 'public' => true,
199
+ '_builtin' => false,
200
+ ), 'objects'
201
+ );
202
+
203
+ if ( count( $taxs > 0 ) ) {
204
+ foreach ( $taxs as $tax ) {
205
+ $tax_name = 'newterm-' . $tax->name;
206
  ?>
207
  <optgroup label="<?php _e( 'Custom Taxonomy', 'bnfw' );?>">
208
+ <option value="<?php echo $tax_name; ?>" <?php selected( $tax_name, $setting['notification'] );?>><?php printf( '%s %s', __( 'New', 'bnfw' ), $tax->labels->name ); ?></option>
209
  </optgroup>
 
 
 
210
  <?php
211
+ }
212
+ }
213
  ?>
214
+ </select>
 
 
 
 
 
 
215
  </td>
216
  </tr>
217
 
218
+ <tr valign="top" id="user-password-msg">
219
+ <td>&nbsp;</td>
220
+ <td>
221
+ <div style="background:#FFFFE1;padding:15px;">
222
+ <p style="margin-top: 0;"><?php esc_html_e( "This notification doesn't support additional email fields or shortcodes in the subject line", 'bnfw' ); ?></p>
223
+ </div>
224
+ </td>
 
 
225
  </tr>
226
 
227
+ <tr valign="top" id="user-role">
 
 
 
 
 
 
 
 
 
 
 
 
 
228
  <th scope="row">
229
  <?php _e( 'User Roles', 'bnfw' ); ?>
230
  </th>
231
  <?php
232
+ $roles_style = '';
233
+ $user_style = 'display:none';
234
 
235
+ if ( count( $setting['users'] ) > 0 ) {
236
+ $roles_style = 'display:none';
237
+ $user_style = '';
238
+ }
239
  ?>
240
  <td>
241
  <div id="bnfw_user_role_container" style="<?php echo $roles_style; ?>">
242
  <select multiple name="user-roles[]" class="select2" data-placeholder="Select User Role" style="width:75%">
243
  <?php
244
+ $roles = $wp_roles->get_names();
245
 
246
+ foreach ( $roles as $role ) {
247
+ $selected = selected( true, in_array( $role, $setting['user-roles'] ), false );
248
+ echo '<option value="', $role, '" ', $selected, '>', $role, '</option>';
249
+ }
250
  ?>
251
  </select><br>
252
  <a id="bnfw_user_role_toggle" href="#"><?php _e( 'Define individual users instead', 'bnfw' );?></a>
255
  <div id="bnfw_user_container" style="<?php echo $user_style; ?>">
256
  <select multiple name="users[]" class="select2" data-placeholder="Select Users" style="width:75%">
257
  <?php
258
+ $users = get_users( array(
259
+ 'order_by' => 'email',
260
+ ) );
261
+
262
+ foreach ( $users as $user ) {
263
+ $selected = selected( true, in_array( $user->ID, $setting['users'] ), false );
264
+ echo '<option value="', $user->ID, '" ', $selected, '>', $user->user_login, '</option>';
265
+ }
266
  ?>
267
  </select><br>
268
  <a id="bnfw_user_toggle" href="#"><?php _e( 'Define user roles instead', 'bnfw' );?></a>
290
  </tbody>
291
  </table>
292
  <?php
293
+ }
294
+
295
+ /**
296
+ * Render user roles dropdown.
297
+ *
298
+ * @since 1.0
299
+ * @param unknown $field
300
+ * @param unknown $value
301
+ * @param unknown $multiple (optional)
302
+ * @param unknown $width (optional)
303
+ */
304
+ private function render_roles_dropdown( $field, $value, $multiple = '', $width = 25 ) {
305
+ global $wp_roles;
306
  ?>
307
+ <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; ?>%">
308
  <?php
309
+ $roles = $wp_roles->get_names();
310
+
311
+ foreach ( $roles as $role ) {
312
+ if ( empty( $multiple ) ) {
313
+ $selected = selected( $value, $role, false );
314
+ } else {
315
+ $selected = selected( true, in_array( $role, $value ), false );
316
+ }
317
+ echo '<option value="', $role, '" ', $selected, '>', $role, '</option>';
318
+ }
319
  ?>
320
  </select>
321
  <?php
322
+ }
323
+
324
+ /**
325
+ * Enqueue scripts.
326
+ *
327
+ * @since 1.0
328
+ */
329
+ public function enqueue_scripts() {
330
+ if ( self::POST_TYPE === get_post_type() ) {
331
+ wp_dequeue_script( 'autosave' );
332
+
333
+ wp_enqueue_style( 'select2', '//cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.min.css', array(), '3.5.2' );
334
+ wp_enqueue_script( 'select2', '//cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.min.js', array( 'jquery' ), '3.5.2', true );
335
+ wp_enqueue_script( 'bnfw', plugins_url( '../assets/js/bnfw.js', dirname( __FILE__ ) ), array( 'jquery' ), '0.1', true );
336
+ }
337
+ }
338
+
339
+ /**
340
+ * Save the meta box's post metadata.
341
+ *
342
+ * @since 1.0
343
+ * @param int $post_id The ID of the post being saved.
344
+ */
345
+ public function save_meta_data( $post_id ) {
346
+
347
+ if ( self::POST_TYPE !== get_post_type( $post_id ) ) {
348
+ return;
349
+ }
350
+
351
+ // Check nonce.
352
+ if ( empty( $_POST[ self::POST_TYPE . '_nonce' ] ) ) {
353
+ return;
354
+ }
355
+
356
+ // Verify nonce.
357
+ if ( ! wp_verify_nonce( $_POST[ self::POST_TYPE . '_nonce' ], self::POST_TYPE ) ) {
358
+ return;
359
+ }
360
+
361
+ if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
362
+ return;
363
+ }
364
+
365
+ if ( ! current_user_can( 'manage_options' ) ) {
366
+ return;
367
+ }
368
+
369
+ $setting = array(
370
+ 'notification' => $_POST['notification'],
371
+ 'subject' => sanitize_text_field( $_POST['subject'] ),
372
+ 'message' => $_POST['message'],
373
+ 'disabled' => sanitize_text_field( $_POST['disabled'] ),
374
+ //'show-fields' => sanitize_text_field( $_POST['show-fields'] ),
375
+ );
376
+
377
+ if ( isset( $_POST['user-roles'] ) ) {
378
+ $setting['user-roles'] = $_POST['user-roles'];
379
+ $setting['users'] = array();
380
+ } else {
381
+ $setting['user-roles'] = array();
382
+ $setting['users'] = $_POST['users'];
383
+ }
384
+
385
+ //if ( 'true' == $setting['show-fields'] ) {
386
+ //$setting['from-name'] = sanitize_text_field( $_POST['from-name'] );
387
+ //$setting['from-email'] = sanitize_email( $_POST['from-email'] );
388
+ //$setting['cc-email'] = sanitize_email( $_POST['cc-email'] );
389
+ //$setting['cc-roles'] = $_POST['cc-roles'];
390
+ //$setting['bcc-email'] = sanitize_email( $_POST['bcc-email'] );
391
+ //$setting['bcc-roles'] = $_POST['bcc-roles'];
392
+ //}
393
+
394
+ $this->save_settings( $post_id, $setting );
395
+ }