PluginProbe
Customize WordPress Emails and Alerts – Better Notifications for WP / 1.3.7
Customize WordPress Emails and Alerts – Better Notifications for WP v1.3.7
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.7, at bnfw.php

448 lines 12.5 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.7
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
129 add_action( 'lostpassword_post' , array( $this, 'on_lost_password' ) );
130 add_filter( 'retrieve_password_title' , array( $this, 'change_password_email_title' ) );
131 add_filter( 'retrieve_password_message' , array( $this, 'change_password_email_message' ), 10, 4 );
132
133 add_filter( 'plugin_action_links' , array( $this, 'plugin_action_links' ), 10, 4 );
134 }
135
136 /**
137 * Setup hooks for custom post types.
138 *
139 * @since 1.2
140 */
141 function custom_post_type_hooks() {
142 $post_types = get_post_types( array( 'public' => true ), 'names' );
143 $post_types = array_diff( $post_types, array( BNFW_Notification::POST_TYPE ) );
144
145 foreach ( $post_types as $post_type ) {
146 add_action( 'pending_' . $post_type, array( $this, 'on_post_pending' ), 10, 2 );
147 add_action( 'future_' . $post_type, array( $this, 'on_post_scheduled' ), 10, 2 );
148 }
149 }
150
151 /**
152 * importer
153 */
154 public function activate() {
155 require_once dirname( __FILE__ ) . '/includes/import.php';
156 $importer = new BNFW_Import;
157 $importer->import();
158 }
159
160 /**
161 * Add 'Settings' link below BNFW in Plugins list.
162 *
163 * @since 1.0
164 * @param unknown $links
165 * @param unknown $file
166 * @return unknown
167 */
168 public function plugin_action_links( $links, $file ) {
169 $plugin_file = 'bnfw/bnfw.php';
170 if ( $file == $plugin_file ) {
171 $settings_link = '<a href="' . admin_url( 'edit.php?post_type=bnfw_notification&page=bnfw-settings' ) . '">' . 'Settings' . '</a>';
172 array_unshift( $links, $settings_link );
173 }
174 return $links;
175 }
176
177 /**
178 * When a new term is created.
179 *
180 * @since 1.0
181 * @param int $term_id
182 * @param int $tt_id
183 * @param string $taxonomy
184 */
185 public function create_term( $term_id, $tt_id, $taxonomy ) {
186 $this->send_notification( 'newterm-' . $taxonomy, $term_id );
187 }
188
189 /**
190 * Fires when a post is created for the first time.
191 *
192 * @since 1.3.1
193 * @param int $post_id Post ID
194 * @param object $post Post object
195 * @param bool $update Whether it was an update
196 */
197 public function insert_post( $post_id, $post, $update ) {
198 $this->publish_post( $post );
199 }
200
201 /**
202 * Fires when a post is created for the first time.
203 *
204 * @since 1.0
205 * @param object $post Post Object
206 */
207 function publish_post( $post ) {
208 $post_id = $post->ID;
209 $post_type = $post->post_type;
210
211 if ( BNFW_Notification::POST_TYPE != $post_type ) {
212 $this->send_notification( 'new-' . $post_type, $post_id );
213 }
214 }
215
216 /**
217 * Fires when a post is updated.
218 *
219 * @since 1.0
220 * @param unknown $post
221 */
222 function update_post( $post ) {
223 $post_id = $post->ID;
224 $post_type = $post->post_type;
225
226 if ( BNFW_Notification::POST_TYPE != $post_type ) {
227 $this->send_notification( 'update-' . $post_type, $post_id );
228 }
229 }
230
231 /**
232 * Fires when a post is pending for review.
233 *
234 * @since 1.1
235 * @param int $post_id Post ID
236 * @param object $post Post object
237 */
238 function on_post_pending( $post_id, $post ) {
239 $post_type = $post->post_type;
240
241 if ( BNFW_Notification::POST_TYPE != $post_type ) {
242 $this->send_notification( 'pending-' . $post_type, $post_id );
243 }
244 }
245
246 /**
247 * Fires when a post is scheduled.
248 *
249 * @since 1.1.5
250 * @param int $post_id Post ID
251 * @param object $post Post object
252 */
253 function on_post_scheduled( $post_id, $post ) {
254 $post_type = $post->post_type;
255
256 if ( BNFW_Notification::POST_TYPE != $post_type ) {
257 $this->send_notification( 'future-' . $post_type, $post_id );
258 }
259 }
260
261 /**
262 * Send notification for new comments
263 *
264 * @since 1.0
265 * @param int $comment_id
266 */
267 function comment_post( $comment_id ) {
268 $the_comment = get_comment( $comment_id );
269 if ( $this->can_send_comment_notification( $the_comment ) ) {
270 $post = get_post( $the_comment->comment_post_ID );
271 $notification_type = 'new-comment'; // old notification name
272 if ( 'post' != $post->post_type ) {
273 $notification_type = 'comment-' . $post->post_type;
274 }
275 $this->send_notification( $notification_type, $comment_id );
276
277 // comment reply notification.
278 if ( $the_comment->comment_parent > 0 ) {
279 $notifications = $this->notifier->get_notifications( 'reply-comment' );
280 if ( count( $notifications ) > 0 ) {
281 $parent = get_comment( $the_comment->comment_parent );
282 if ( $parent->comment_author_email != $the_comment->comment_author_email ) {
283 foreach ( $notifications as $notification ) {
284 $this->engine->send_comment_reply_email( $this->notifier->read_settings( $notification->ID ), $the_comment, $parent );
285 }
286 }
287 }
288 }
289 }
290 }
291
292 /**
293 * Send notification for new trackback
294 *
295 * @since 1.0
296 * @param unknown $comment_id
297 */
298 function trackback_post( $comment_id ) {
299 $the_comment = get_comment( $comment_id );
300 if ( $this->can_send_comment_notification( $the_comment ) ) {
301 $this->send_notification( 'new-trackback', $comment_id );
302 }
303 }
304
305 /**
306 * Send notification for new pingbacks
307 *
308 * @since 1.0
309 * @param unknown $comment_id
310 */
311 function pingback_post( $comment_id ) {
312 $the_comment = get_comment( $comment_id );
313 if ( $this->can_send_comment_notification( $the_comment ) ) {
314 $this->send_notification( 'new-pingback', $comment_id );
315 }
316 }
317
318 /**
319 * Send notification for lost password.
320 *
321 * @since 1.0
322 */
323 function on_lost_password() {
324 $user = get_user_by( 'login', trim( $_POST['user_login'] ) );
325 if ( $user ) {
326 $this->send_notification( 'admin-password', $user->ID );
327 }
328 }
329
330 /**
331 * Change the title of the password reset email that is sent to the user.
332 *
333 * @since 1.1
334 */
335 public function change_password_email_title( $title ) {
336 $notifications = $this->notifier->get_notifications( 'user-password' );
337 if ( count( $notifications ) > 0 ) {
338 // Ideally there should be only one notification for this type.
339 // If there are multiple notification then we will read data about only the last one
340 $setting = $this->notifier->read_settings( end( $notifications )->ID );
341 return $setting['subject'];
342 }
343
344 return $title;
345 }
346
347 /**
348 * Change the message of the password reset email.
349 *
350 * @since 1.1
351 */
352 public function change_password_email_message( $message, $key, $user_login = '', $user_data = '' ) {
353 $notifications = $this->notifier->get_notifications( 'user-password' );
354 if ( count( $notifications ) > 0 ) {
355 // Ideally there should be only one notification for this type.
356 // If there are multiple notification then we will read data about only the last one
357 $setting = $this->notifier->read_settings( end( $notifications )->ID );
358
359 if ( 'html' == $setting['email-formatting'] ) {
360 add_filter( 'wp_mail_content_type', array( $this, 'set_html_content_type' ) );
361 } else {
362 add_filter( 'wp_mail_content_type', array( $this, 'set_text_content_type' ) );
363 }
364
365 return $this->engine->handle_password_reset_shortcodes( $setting, $key, $user_login, $user_data );
366 }
367
368 return $message;
369 }
370
371 /**
372 * Set the email formatting to HTML.
373 *
374 * @since 1.4
375 */
376 public function set_html_content_type() {
377 return 'text/html';
378 }
379
380 /**
381 * Set the email formatting to text.
382 *
383 * @since 1.4
384 */
385 public function set_text_content_type() {
386 return 'text/plain';
387 }
388
389 /**
390 * Send notification for new uses.
391 *
392 * @since 1.0
393 * @param unknown $user_id
394 */
395 function user_register( $user_id ) {
396 $this->send_notification( 'admin-user', $user_id );
397 }
398
399 /**
400 * New User - Welcome email
401 *
402 * @since 1.1
403 * @param int $user_id New user id
404 */
405 function welcome_email( $user_id ) {
406 $notifications = $this->notifier->get_notifications( 'welcome-email' );
407 foreach ( $notifications as $notification ) {
408 $this->engine->send_registration_email( $this->notifier->read_settings( $notification->ID ), get_userdata( $user_id ) );
409 }
410 }
411
412 /**
413 * Send notification based on type and ref id
414 *
415 * @access private
416 * @since 1.0
417 * @param unknown $type
418 * @param unknown $ref_id
419 */
420 private function send_notification( $type, $ref_id ) {
421 $notifications = $this->notifier->get_notifications( $type );
422 foreach ( $notifications as $notification ) {
423 $this->engine->send_notification( $this->notifier->read_settings( $notification->ID ), $ref_id );
424 }
425 }
426
427 /**
428 * Can send comment notification or not
429 *
430 * @since 1.0
431 * @param unknown $comment
432 * @return unknown
433 */
434 private function can_send_comment_notification( $comment ) {
435 // Returns false if the comment is marked as spam AND admin has enabled suppression of spam
436 $suppress_spam = get_option( 'bnfw_suppress_spam' );
437 if ( '1' === $suppress_spam && ( 0 === strcmp( $comment->comment_approved, 'spam' ) ) ) {
438 return false;
439 }
440 return true;
441 }
442 }
443
444 /* ------------------------------------------------------------------------ *
445 * Fire up the plugin
446 * ------------------------------------------------------------------------ */
447 BNFW::factory();
448