PluginProbe
Customize WordPress Emails and Alerts – Better Notifications for WP / 1.3.9
Customize WordPress Emails and Alerts – Better Notifications for WP v1.3.9
1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.8 1.8.1 1.8.10 1.8.11 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5 All 81 releases
bnfw / bnfw.php

bnfw.php in Customize WordPress Emails and Alerts – Better Notifications for WP 1.3.9, at bnfw.php

463 lines 13.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Better Notifications for WordPress
4 * Plugin URI: http://wordpress.org/plugins/bnfw/
5 * Description: Send customisable emails to your users for different WordPress notifications.
6 * Version: 1.3.9
7 * Author: Voltronik
8 * Author URI: https://betternotificationsforwp.com/
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 © 2015 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 license related classes
78 if ( ! class_exists( 'EDD_SL_Plugin_Updater' ) ) {
79 require_once 'includes/libraries/EDD_SL_Plugin_Updater.php';
80 }
81 require_once 'includes/license/class-bnfw-license.php';
82 require_once 'includes/license/class-bnfw-license-setting.php';
83
84 // Load Engine
85 require_once 'includes/engine/class-bnfw-engine.php';
86 require_once 'includes/overrides.php';
87
88 // Load notification post type and notification helpers
89 require_once 'includes/admin/class-bnfw-notification.php';
90 require_once 'includes/notification/post-notification.php';
91
92 // helpers
93 require_once 'includes/helpers/helpers.php';
94 require_once 'includes/helpers/ajax-helpers.php';
95
96 // Load Admin Pages
97 if ( is_admin() ) {
98 require_once 'includes/admin/bnfw-settings.php';
99 }
100 }
101
102 /**
103 * Register Hooks.
104 *
105 * @since 1.0
106 */
107 public function hooks() {
108 register_activation_hook( __FILE__ , array( $this, 'activate' ) );
109
110 // P2 theme directly inserts the post into db
111 if ( 'P2' == wp_get_theme() ) {
112 add_action( 'wp_insert_post' , array( $this, 'insert_post' ), 10, 3 );
113 }
114
115 add_action( 'draft_to_publish' , array( $this, 'publish_post' ) );
116 add_action( 'future_to_publish' , array( $this, 'publish_post' ) );
117 add_action( 'pending_to_publish' , array( $this, 'publish_post' ) );
118 add_action( 'publish_to_publish' , array( $this, 'update_post' ) );
119 add_action( 'init' , array( $this, 'custom_post_type_hooks' ), 100 );
120 add_action( 'create_term' , array( $this, 'create_term' ), 10, 3 );
121
122 add_action( 'comment_post' , array( $this, 'comment_post' ) );
123 add_action( 'trackback_post' , array( $this, 'trackback_post' ) );
124 add_action( 'pingback_post' , array( $this, 'pingback_post' ) );
125
126 add_action( 'user_register' , array( $this, 'user_register' ) );
127 add_action( 'user_register' , array( $this, 'welcome_email' ) );
128 add_action( 'set_user_role' , array( $this, 'user_role_changed' ), 10, 2 );
129
130 add_action( 'lostpassword_post' , array( $this, 'on_lost_password' ) );
131 add_filter( 'retrieve_password_title' , array( $this, 'change_password_email_title' ) );
132 add_filter( 'retrieve_password_message' , array( $this, 'change_password_email_message' ), 10, 4 );
133
134 add_filter( 'plugin_action_links' , array( $this, 'plugin_action_links' ), 10, 4 );
135 }
136
137 /**
138 * Setup hooks for custom post types.
139 *
140 * @since 1.2
141 */
142 function custom_post_type_hooks() {
143 $post_types = get_post_types( array( 'public' => true ), 'names' );
144 $post_types = array_diff( $post_types, array( BNFW_Notification::POST_TYPE ) );
145
146 foreach ( $post_types as $post_type ) {
147 add_action( 'pending_' . $post_type, array( $this, 'on_post_pending' ), 10, 2 );
148 add_action( 'future_' . $post_type, array( $this, 'on_post_scheduled' ), 10, 2 );
149 }
150 }
151
152 /**
153 * importer
154 */
155 public function activate() {
156 require_once dirname( __FILE__ ) . '/includes/import.php';
157 $importer = new BNFW_Import;
158 $importer->import();
159 }
160
161 /**
162 * Add 'Settings' link below BNFW in Plugins list.
163 *
164 * @since 1.0
165 * @param unknown $links
166 * @param unknown $file
167 * @return unknown
168 */
169 public function plugin_action_links( $links, $file ) {
170 $plugin_file = 'bnfw/bnfw.php';
171 if ( $file == $plugin_file ) {
172 $settings_link = '<a href="' . admin_url( 'edit.php?post_type=bnfw_notification&page=bnfw-settings' ) . '">' . 'Settings' . '</a>';
173 array_unshift( $links, $settings_link );
174 }
175 return $links;
176 }
177
178 /**
179 * When a new term is created.
180 *
181 * @since 1.0
182 * @param int $term_id
183 * @param int $tt_id
184 * @param string $taxonomy
185 */
186 public function create_term( $term_id, $tt_id, $taxonomy ) {
187 $this->send_notification( 'newterm-' . $taxonomy, $term_id );
188 }
189
190 /**
191 * Fires when a post is created for the first time.
192 *
193 * @since 1.3.1
194 * @param int $post_id Post ID
195 * @param object $post Post object
196 * @param bool $update Whether it was an update
197 */
198 public function insert_post( $post_id, $post, $update ) {
199 $this->publish_post( $post );
200 }
201
202 /**
203 * Fires when a post is created for the first time.
204 *
205 * @since 1.0
206 * @param object $post Post Object
207 */
208 function publish_post( $post ) {
209 $post_id = $post->ID;
210 $post_type = $post->post_type;
211
212 if ( BNFW_Notification::POST_TYPE != $post_type ) {
213 $this->send_notification( 'new-' . $post_type, $post_id );
214 }
215 }
216
217 /**
218 * Fires when a post is updated.
219 *
220 * @since 1.0
221 * @param unknown $post
222 */
223 function update_post( $post ) {
224 $post_id = $post->ID;
225 $post_type = $post->post_type;
226
227 if ( BNFW_Notification::POST_TYPE != $post_type ) {
228 $this->send_notification( 'update-' . $post_type, $post_id );
229 }
230 }
231
232 /**
233 * Fires when a post is pending for review.
234 *
235 * @since 1.1
236 * @param int $post_id Post ID
237 * @param object $post Post object
238 */
239 function on_post_pending( $post_id, $post ) {
240 $post_type = $post->post_type;
241
242 if ( BNFW_Notification::POST_TYPE != $post_type ) {
243 $this->send_notification( 'pending-' . $post_type, $post_id );
244 }
245 }
246
247 /**
248 * Fires when a post is scheduled.
249 *
250 * @since 1.1.5
251 * @param int $post_id Post ID
252 * @param object $post Post object
253 */
254 function on_post_scheduled( $post_id, $post ) {
255 $post_type = $post->post_type;
256
257 if ( BNFW_Notification::POST_TYPE != $post_type ) {
258 $this->send_notification( 'future-' . $post_type, $post_id );
259 }
260 }
261
262 /**
263 * Send notification for new comments
264 *
265 * @since 1.0
266 * @param int $comment_id
267 */
268 function comment_post( $comment_id ) {
269 $the_comment = get_comment( $comment_id );
270 if ( $this->can_send_comment_notification( $the_comment ) ) {
271 $post = get_post( $the_comment->comment_post_ID );
272 $notification_type = 'new-comment'; // old notification name
273 if ( 'post' != $post->post_type ) {
274 $notification_type = 'comment-' . $post->post_type;
275 }
276 $this->send_notification( $notification_type, $comment_id );
277
278 // comment reply notification.
279 if ( $the_comment->comment_parent > 0 ) {
280 $notifications = $this->notifier->get_notifications( 'reply-comment' );
281 if ( count( $notifications ) > 0 ) {
282 $parent = get_comment( $the_comment->comment_parent );
283 if ( $parent->comment_author_email != $the_comment->comment_author_email ) {
284 foreach ( $notifications as $notification ) {
285 $this->engine->send_comment_reply_email( $this->notifier->read_settings( $notification->ID ), $the_comment, $parent );
286 }
287 }
288 }
289 }
290 }
291 }
292
293 /**
294 * Send notification for new trackback
295 *
296 * @since 1.0
297 * @param unknown $comment_id
298 */
299 function trackback_post( $comment_id ) {
300 $the_comment = get_comment( $comment_id );
301 if ( $this->can_send_comment_notification( $the_comment ) ) {
302 $this->send_notification( 'new-trackback', $comment_id );
303 }
304 }
305
306 /**
307 * Send notification for new pingbacks
308 *
309 * @since 1.0
310 * @param unknown $comment_id
311 */
312 function pingback_post( $comment_id ) {
313 $the_comment = get_comment( $comment_id );
314 if ( $this->can_send_comment_notification( $the_comment ) ) {
315 $this->send_notification( 'new-pingback', $comment_id );
316 }
317 }
318
319 /**
320 * Send notification for lost password.
321 *
322 * @since 1.0
323 */
324 function on_lost_password() {
325 $user = get_user_by( 'login', trim( $_POST['user_login'] ) );
326 if ( $user ) {
327 $this->send_notification( 'admin-password', $user->ID );
328 }
329 }
330
331 /**
332 * Change the title of the password reset email that is sent to the user.
333 *
334 * @since 1.1
335 */
336 public function change_password_email_title( $title ) {
337 $notifications = $this->notifier->get_notifications( 'user-password' );
338 if ( count( $notifications ) > 0 ) {
339 // Ideally there should be only one notification for this type.
340 // If there are multiple notification then we will read data about only the last one
341 $setting = $this->notifier->read_settings( end( $notifications )->ID );
342 return $setting['subject'];
343 }
344
345 return $title;
346 }
347
348 /**
349 * Change the message of the password reset email.
350 *
351 * @since 1.1
352 */
353 public function change_password_email_message( $message, $key, $user_login = '', $user_data = '' ) {
354 $notifications = $this->notifier->get_notifications( 'user-password' );
355 if ( count( $notifications ) > 0 ) {
356 // Ideally there should be only one notification for this type.
357 // If there are multiple notification then we will read data about only the last one
358 $setting = $this->notifier->read_settings( end( $notifications )->ID );
359
360 if ( 'html' == $setting['email-formatting'] ) {
361 add_filter( 'wp_mail_content_type', array( $this, 'set_html_content_type' ) );
362 } else {
363 add_filter( 'wp_mail_content_type', array( $this, 'set_text_content_type' ) );
364 }
365
366 return $this->engine->handle_password_reset_shortcodes( $setting, $key, $user_login, $user_data );
367 }
368
369 return $message;
370 }
371
372 /**
373 * Set the email formatting to HTML.
374 *
375 * @since 1.4
376 */
377 public function set_html_content_type() {
378 return 'text/html';
379 }
380
381 /**
382 * Set the email formatting to text.
383 *
384 * @since 1.4
385 */
386 public function set_text_content_type() {
387 return 'text/plain';
388 }
389
390 /**
391 * Send notification for new uses.
392 *
393 * @since 1.0
394 * @param int $user_id
395 */
396 function user_register( $user_id ) {
397 $this->send_notification( 'admin-user', $user_id );
398 }
399
400 /**
401 * New User - Welcome email
402 *
403 * @since 1.1
404 * @param int $user_id New user id
405 */
406 function welcome_email( $user_id ) {
407 $notifications = $this->notifier->get_notifications( 'welcome-email' );
408 foreach ( $notifications as $notification ) {
409 $this->engine->send_registration_email( $this->notifier->read_settings( $notification->ID ), get_userdata( $user_id ) );
410 }
411 }
412
413 /**
414 * Send notification when a user role changes.
415 *
416 * @since 1.3.9
417 * @param int $user_id User ID
418 * @param string $new_role New User role
419 */
420 public function user_role_changed( $user_id, $new_role ) {
421 $notifications = $this->notifier->get_notifications( 'user-role' );
422 foreach ( $notifications as $notification ) {
423 $this->engine->send_user_role_chnaged_email( $this->notifier->read_settings( $notification->ID ), $user_id );
424 }
425 }
426
427 /**
428 * Send notification based on type and ref id
429 *
430 * @access private
431 * @since 1.0
432 * @param unknown $type
433 * @param unknown $ref_id
434 */
435 private function send_notification( $type, $ref_id ) {
436 $notifications = $this->notifier->get_notifications( $type );
437 foreach ( $notifications as $notification ) {
438 $this->engine->send_notification( $this->notifier->read_settings( $notification->ID ), $ref_id );
439 }
440 }
441
442 /**
443 * Can send comment notification or not
444 *
445 * @since 1.0
446 * @param unknown $comment
447 * @return unknown
448 */
449 private function can_send_comment_notification( $comment ) {
450 // Returns false if the comment is marked as spam AND admin has enabled suppression of spam
451 $suppress_spam = get_option( 'bnfw_suppress_spam' );
452 if ( '1' === $suppress_spam && ( 0 === strcmp( $comment->comment_approved, 'spam' ) ) ) {
453 return false;
454 }
455 return true;
456 }
457 }
458
459 /* ------------------------------------------------------------------------ *
460 * Fire up the plugin
461 * ------------------------------------------------------------------------ */
462 BNFW::factory();
463