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

1,070 lines 32.3 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 WP
4 * Plugin URI: https://wordpress.org/plugins/bnfw/
5 * Description: Supercharge your WordPress notifications using a WYSIWYG editor and shortcodes. Default and new notifications available. Add more power with Add-ons.
6 * Version: 1.7.5
7 * Author: Made with Fuel
8 * Author URI: https://betternotificationsforwp.com/
9 * Author Email: hello@betternotificationsforwp.com
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 © 2019 Made with Fuel Ltd. (hello@betternotificationsforwp.com)
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
30 class BNFW {
31
32 /**
33 * Constructor.
34 *
35 * @since 1.0
36 */
37 function __construct() {
38 $this->load_textdomain();
39 $this->includes();
40 $this->hooks();
41
42 /**
43 * BNFW Notification.
44 *
45 * @var \BNFW_Notification
46 */
47 $this->notifier = new BNFW_Notification;
48
49 /**
50 * BNFW Engine.
51 *
52 * @var \BNFW_Engine
53 */
54 $this->engine = new BNFW_Engine;
55 }
56
57 /**
58 * Factory method to return the instance of the class.
59 *
60 * Makes sure that only one instance is created.
61 *
62 * @return \BNFW Instance of the class.
63 */
64 public static function factory() {
65 static $instance = false;
66 if ( ! $instance ) {
67 $instance = new self();
68 }
69 return $instance;
70 }
71
72 /**
73 * Loads the plugin language files
74 *
75 * @since 1.0
76 */
77 public function load_textdomain() {
78 // Load localization domain
79 $this->translations = dirname( plugin_basename( __FILE__ ) ) . '/languages/';
80 load_plugin_textdomain( 'bnfw', false, $this->translations );
81 }
82
83 /**
84 * Include required files.
85 *
86 * @since 1.0
87 */
88 public function includes() {
89
90 // Load license related classes
91 if ( ! class_exists( 'EDD_SL_Plugin_Updater' ) ) {
92 require_once 'includes/libraries/EDD_SL_Plugin_Updater.php';
93 }
94
95 require_once 'vendor/persist-admin-notices-dismissal/persist-admin-notices-dismissal.php';
96
97 require_once 'includes/license/class-bnfw-license.php';
98 require_once 'includes/license/class-bnfw-license-setting.php';
99
100 // Load Engine
101 require_once 'includes/engine/class-bnfw-engine.php';
102 require_once 'includes/overrides.php';
103
104 // Load notification post type and notification helpers
105 require_once 'includes/admin/class-bnfw-notification.php';
106 require_once 'includes/notification/post-notification.php';
107
108 // Helpers
109 require_once 'includes/helpers/helpers.php';
110 require_once 'includes/helpers/ajax-helpers.php';
111
112 // Load Admin Pages
113 if ( is_admin() ) {
114 require_once 'includes/admin/bnfw-settings.php';
115 }
116 }
117
118 /**
119 * Register Hooks.
120 *
121 * @since 1.0
122 */
123 public function hooks() {
124 global $wp_version;
125
126 register_activation_hook( __FILE__, array( $this, 'activate' ) );
127
128 add_action( 'admin_init', array( 'PAnD', 'init' ) );
129 add_action( 'admin_init', array( $this, 'add_capability_to_admin' ) );
130
131 add_action( 'draft_to_private' , array( $this, 'private_post' ) );
132 add_action( 'future_to_private' , array( $this, 'private_post' ) );
133 add_action( 'pending_to_private' , array( $this, 'private_post' ) );
134 add_action( 'publish_to_private' , array( $this, 'private_post' ) );
135
136 add_action( 'wp_insert_post' , array( $this, 'insert_post' ), 10, 2 );
137
138 add_action( 'auto-draft_to_publish' , array( $this, 'publish_post' ) );
139 add_action( 'draft_to_publish' , array( $this, 'publish_post' ) );
140 add_action( 'future_to_publish' , array( $this, 'publish_post' ) );
141 add_action( 'pending_to_publish' , array( $this, 'publish_post' ) );
142 add_action( 'private_to_publish' , array( $this, 'publish_post' ) );
143 // add_action( 'acf/submit_form' , array( $this, 'acf_submit_form' ), 10, 2 );
144
145 add_action( 'publish_to_publish' , array( $this, 'update_post' ) );
146 add_action( 'private_to_private' , array( $this, 'update_post' ) );
147
148 add_action( 'init' , array( $this, 'custom_post_type_hooks' ), 100 );
149 add_action( 'create_term' , array( $this, 'create_term' ), 10, 3 );
150
151 add_action( 'comment_post' , array( $this, 'comment_post' ) );
152 add_action( 'trackback_post' , array( $this, 'trackback_post' ) );
153 add_action( 'pingback_post' , array( $this, 'pingback_post' ) );
154
155 add_action( 'user_register', array( $this, 'user_register' ) );
156
157 add_action( 'user_register' , array( $this, 'welcome_email' ) );
158 add_action( 'set_user_role' , array( $this, 'user_role_changed' ), 10, 3 );
159
160 if ( version_compare( $wp_version, '4.4', '>=' ) ) {
161 add_filter( 'retrieve_password_title', array( $this, 'change_password_email_title' ), 10, 3 );
162 } else {
163 add_filter( 'retrieve_password_title', array( $this, 'change_password_email_title' ) );
164 }
165 add_action( 'lostpassword_post' , array( $this, 'on_lost_password' ) );
166 add_filter( 'retrieve_password_message' , array( $this, 'change_password_email_message' ), 10, 4 );
167
168 add_action( 'after_password_reset' , array( $this, 'on_password_reset' ) );
169
170 add_filter( 'send_password_change_email', array( $this, 'should_password_changed_email_be_sent' ), 10, 3 );
171 add_filter( 'password_change_email' , array( $this, 'on_password_changed' ), 10, 2 );
172
173 add_filter( 'send_email_change_email', array( $this, 'should_email_changed_email_be_sent' ), 10, 3 );
174 add_filter( 'email_change_email' , array( $this, 'on_email_changed' ), 10, 2 );
175 add_filter( 'new_user_email_content', array( $this, 'on_email_changing' ), 10, 2 );
176
177 add_filter( 'auto_core_update_email' , array( $this, 'on_core_updated' ), 10, 4 );
178
179 add_filter( 'user_request_action_email_content', array( $this, 'handle_user_request_email_content' ), 10, 2 );
180 add_filter( 'user_request_action_email_subject', array( $this, 'handle_user_request_email_subject' ), 10, 3 );
181
182 add_filter( 'user_confirmed_action_email_content', array( $this, 'handle_user_confirmed_action_email_content' ), 10, 2 );
183
184 add_filter( 'wp_privacy_personal_data_email_content', array( $this, 'handle_data_export_email_content' ), 10, 2 );
185
186 add_filter( 'user_erasure_complete_email_subject', array( $this, 'handle_erasure_complete_email_subject' ), 10, 3 );
187 add_filter( 'user_confirmed_action_email_content', array( $this, 'handle_erasure_complete_email_content' ), 10, 2 );
188
189 add_filter( 'plugin_action_links' , array( $this, 'plugin_action_links' ), 10, 4 );
190 add_action( 'shutdown' , array( $this, 'on_shutdown' ) );
191 }
192
193 /**
194 * Add 'bnfw' capability to admin.
195 */
196 public function add_capability_to_admin() {
197 $admins = get_role( 'administrator' );
198
199 if ( ! $admins->has_cap( 'bnfw' ) ) {
200 $admins->add_cap( 'bnfw' );
201 }
202 }
203
204 /**
205 * Setup hooks for custom post types.
206 *
207 * @since 1.2
208 */
209 function custom_post_type_hooks() {
210 $post_types = get_post_types( array( 'public' => true ), 'names' );
211 $post_types = array_diff( $post_types, array( BNFW_Notification::POST_TYPE ) );
212
213 foreach ( $post_types as $post_type ) {
214 add_action( 'pending_' . $post_type, array( $this, 'on_post_pending' ), 10, 2 );
215 add_action( 'future_' . $post_type, array( $this, 'on_post_scheduled' ), 10, 2 );
216 }
217 }
218
219 /**
220 * importer
221 */
222 public function activate() {
223 require_once dirname( __FILE__ ) . '/includes/import.php';
224 $importer = new BNFW_Import;
225 $importer->import();
226 }
227
228 /**
229 * Add 'Settings' link below BNFW in Plugins list.
230 *
231 * @since 1.0
232 * @param unknown $links
233 * @param unknown $file
234 * @return unknown
235 */
236 public function plugin_action_links( $links, $file ) {
237 $plugin_file = 'bnfw/bnfw.php';
238 if ( $file == $plugin_file ) {
239 $settings_link = '<a href="' . esc_url( admin_url( 'edit.php?post_type=bnfw_notification&page=bnfw-settings' ) ) . '">' . esc_html__( 'Settings', 'bnfw' ) . '</a>';
240 array_unshift( $links, $settings_link );
241 }
242 return $links;
243 }
244
245 /**
246 * When a new term is created.
247 *
248 * @since 1.0
249 * @param int $term_id
250 * @param int $tt_id
251 * @param string $taxonomy
252 */
253 public function create_term( $term_id, $tt_id, $taxonomy ) {
254 $this->send_notification( 'newterm-' . $taxonomy, $term_id );
255 }
256
257 /**
258 * Fires when a post is created for the first time.
259 *
260 * @since 1.3.1
261 * @param int $post_id Post ID
262 * @param object $post Post object
263 */
264 public function insert_post( $post_id, $post ) {
265 // Some themes like P2, directly insert posts into DB.
266 $insert_post_themes = apply_filters( 'bnfw_insert_post_themes', array( 'P2', 'Syncope' ) );
267 $current_theme = wp_get_theme();
268
269 /**
270 * Whether to trigger insert post hook.
271 *
272 * @since 1.4
273 */
274 $trigger_insert_post = apply_filters( 'bnfw_trigger_insert_post', false, $post_id );
275
276 if ( in_array( $current_theme->get( 'Name' ), $insert_post_themes ) || $trigger_insert_post ) {
277 $this->handle_inserted_post( $post_id );
278 }
279 }
280
281 /**
282 * Trigger New Post published notification for ACF forms.
283 *
284 * @param string $form ACF Form.
285 * @param int $post_id Post ID.
286 */
287 public function acf_submit_form( $form, $post_id ) {
288 $this->handle_inserted_post( $post_id );
289 }
290
291 /**
292 * Trigger correct notifications for inserted posts.
293 *
294 * @since 1.6.7
295 *
296 * @param int $post_id Post id.
297 */
298 private function handle_inserted_post( $post_id ) {
299 $post = get_post( $post_id );
300
301 if ( ! is_a( $post, 'WP_Post' ) ) {
302 return;
303 }
304
305 switch ( $post->post_status ) {
306 case 'publish':
307 $this->publish_post( $post );
308 break;
309
310 case 'private':
311 $this->private_post( $post );
312 break;
313
314 case 'pending':
315 $this->on_post_pending( $post_id, $post );
316 break;
317
318 case 'future':
319 $this->on_post_scheduled( $post_id, $post );
320 break;
321 }
322 }
323
324 /**
325 * Fires when a post is created for the first time.
326 *
327 * @since 1.0
328 * @param object $post Post Object
329 */
330 function publish_post( $post ) {
331 $post_id = $post->ID;
332 $post_type = $post->post_type;
333
334 if ( BNFW_Notification::POST_TYPE != $post_type ) {
335 $this->send_notification_async( 'new-' . $post_type, $post_id );
336 }
337 }
338
339 /**
340 * Fires when a private post is created.
341 *
342 * @since 1.6
343 * @param object $post Post Object
344 */
345 public function private_post( $post ) {
346 $post_id = $post->ID;
347 $post_type = $post->post_type;
348
349 if ( BNFW_Notification::POST_TYPE != $post_type ) {
350 $this->send_notification_async( 'private-' . $post_type, $post_id );
351 }
352 }
353
354 /**
355 * Fires when a post is updated.
356 *
357 * @since 1.0
358 * @param unknown $post
359 */
360 function update_post( $post ) {
361 // Block editor sends duplicate requests on post update.
362 if ( ( isset( $_GET['meta-box-loader'] ) || isset( $_GET['meta_box'] ) ) ) {
363 return;
364 }
365
366 $post_id = $post->ID;
367 $post_type = $post->post_type;
368
369 if ( BNFW_Notification::POST_TYPE != $post_type ) {
370 $this->send_notification_async( 'update-' . $post_type, $post_id );
371 }
372 }
373
374 /**
375 * Fires when a post is pending for review.
376 *
377 * @since 1.1
378 * @param int $post_id Post ID
379 * @param object $post Post object
380 */
381 function on_post_pending( $post_id, $post ) {
382 $post_type = $post->post_type;
383
384 if ( BNFW_Notification::POST_TYPE != $post_type ) {
385 $this->send_notification_async( 'pending-' . $post_type, $post_id );
386 }
387 }
388
389 /**
390 * Fires when a post is scheduled.
391 *
392 * @since 1.1.5
393 * @param int $post_id Post ID
394 * @param object $post Post object
395 */
396 function on_post_scheduled( $post_id, $post ) {
397 // Rest request also triggers the same hook. We can ignore it.
398 if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
399 return;
400 }
401
402 $post_type = $post->post_type;
403
404 if ( BNFW_Notification::POST_TYPE != $post_type ) {
405 $this->send_notification_async( 'future-' . $post_type, $post_id );
406 }
407 }
408
409 /**
410 * Send notification for new comments
411 *
412 * @since 1.0
413 * @param int $comment_id
414 */
415 public function comment_post( $comment_id ) {
416 $the_comment = get_comment( $comment_id );
417 $post = get_post( $the_comment->comment_post_ID );
418
419 if ( '1' !== $the_comment->comment_approved ) {
420 if ( $this->can_send_comment_notification( $the_comment ) ) {
421 $notification_type = 'moderate-' . $post->post_type . '-comment';
422 $this->send_notification( $notification_type, $comment_id );
423 }
424 } else {
425 $notification_type = 'new-comment'; // old notification name
426
427 if ( 'post' != $post->post_type ) {
428 $notification_type = 'comment-' . $post->post_type;
429 }
430
431 $this->send_notification( $notification_type, $comment_id );
432 }
433
434 // comment reply notification.
435 if ( $this->can_send_comment_notification( $the_comment ) ) {
436 if ( $the_comment->comment_parent > 0 ) {
437 $notification_type = 'reply-comment'; // old notification name
438 if ( 'post' != $post->post_type ) {
439 $notification_type = 'commentreply-' . $post->post_type;
440 }
441 $notifications = $this->notifier->get_notifications( $notification_type );
442 if ( count( $notifications ) > 0 ) {
443 $parent = get_comment( $the_comment->comment_parent );
444 if ( $parent->comment_author_email != $the_comment->comment_author_email ) {
445 foreach ( $notifications as $notification ) {
446 $this->engine->send_comment_reply_email( $this->notifier->read_settings( $notification->ID ), $the_comment, $parent );
447 }
448 }
449 }
450 }
451 }
452 }
453
454 /**
455 * Send notification for new trackback
456 *
457 * @since 1.0
458 * @param unknown $comment_id
459 */
460 function trackback_post( $comment_id ) {
461 $the_comment = get_comment( $comment_id );
462 if ( $this->can_send_comment_notification( $the_comment ) ) {
463 $this->send_notification( 'new-trackback', $comment_id );
464 }
465 }
466
467 /**
468 * Send notification for new pingbacks
469 *
470 * @since 1.0
471 * @param unknown $comment_id
472 */
473 function pingback_post( $comment_id ) {
474 $the_comment = get_comment( $comment_id );
475 if ( $this->can_send_comment_notification( $the_comment ) ) {
476 $this->send_notification( 'new-pingback', $comment_id );
477 }
478 }
479
480 /**
481 * Send notification for lost password.
482 *
483 * @since 1.0
484 */
485 function on_lost_password() {
486 $user_login = sanitize_text_field( $_POST['user_login'] );
487 $user = get_user_by( 'login', $user_login );
488 if ( $user ) {
489 $this->send_notification( 'admin-password', $user->ID );
490 }
491 }
492
493 /**
494 * Change the title of the password reset email that is sent to the user.
495 *
496 * @since 1.1
497 *
498 * @param string $title
499 * @param string $user_login
500 * @param string $user_data
501 *
502 * @return string
503 */
504 public function change_password_email_title( $title, $user_login = '', $user_data = '' ) {
505 $notifications = $this->notifier->get_notifications( 'user-password' );
506 if ( count( $notifications ) > 0 ) {
507 // Ideally there should be only one notification for this type.
508 // If there are multiple notification then we will read data about only the last one
509 $setting = $this->notifier->read_settings( end( $notifications )->ID );
510
511 if ( '' === $user_data ) {
512 return $this->engine->handle_shortcodes( $setting['subject'], 'user-password', $user_data->ID );
513 } else {
514 return $setting['subject'];
515 }
516 }
517
518 return $title;
519 }
520
521 /**
522 * Change the message of the password reset email.
523 *
524 * @since 1.1
525 *
526 * @param string $message
527 * @param string $key
528 * @param string $user_login
529 * @param string $user_data
530 *
531 * @return string
532 */
533 public function change_password_email_message( $message, $key, $user_login = '', $user_data = '' ) {
534 $notifications = $this->notifier->get_notifications( 'user-password' );
535 if ( count( $notifications ) > 0 ) {
536 // Ideally there should be only one notification for this type.
537 // If there are multiple notification then we will read data about only the last one
538 $setting = $this->notifier->read_settings( end( $notifications )->ID );
539
540 $message = $this->engine->handle_password_reset_shortcodes( $setting, $key, $user_login, $user_data );
541
542 if ( 'html' == $setting['email-formatting'] ) {
543 add_filter( 'wp_mail_content_type', array( $this, 'set_html_content_type' ) );
544 if ( 'true' !== $setting['disable-autop'] ) {
545 $message = wpautop( $message );
546 }
547 } else {
548 add_filter( 'wp_mail_content_type', array( $this, 'set_text_content_type' ) );
549 }
550 } else {
551 if ( $this->notifier->notification_exists( 'user-password', false ) ) {
552 // disabled notification exists, so disable the email by returning empty string.
553 return '';
554 }
555 }
556
557 return $message;
558 }
559
560 /**
561 * On Password reset.
562 *
563 * @param WP_User $user User who's password was changed.
564 */
565 public function on_password_reset( $user ) {
566 $notifications = $this->notifier->get_notifications( 'password-changed' );
567 foreach ( $notifications as $notification ) {
568 $this->engine->send_password_changed_email( $this->notifier->read_settings( $notification->ID ), $user );
569 }
570 }
571
572 /**
573 * Should the password changed email be sent?
574 *
575 * @param $send
576 * @param $user
577 * @param $userdata
578 *
579 * @return bool
580 */
581 public function should_password_changed_email_be_sent( $send, $user, $userdata ) {
582 $bnfw = BNFW::factory();
583
584 if ( ! $send ) {
585 return $send;
586 }
587
588 return ! $bnfw->notifier->is_notification_disabled( 'password-changed' );
589 }
590
591 /**
592 * On Password Changed.
593 *
594 * @since 1.6
595 *
596 * @param array $email_data Email Data.
597 * @param array $user User data.
598 *
599 * @return array Modified Email Data
600 */
601 public function on_password_changed( $email_data, $user ) {
602 return $this->handle_filtered_data_notification( 'password-changed', $email_data, $user['ID'] );
603 }
604
605 /**
606 * Should the email changed email be sent?
607 *
608 * @param $send
609 * @param $user
610 * @param $userdata
611 *
612 * @return bool
613 */
614 public function should_email_changed_email_be_sent( $send, $user, $userdata ) {
615 $bnfw = BNFW::factory();
616
617 if ( $bnfw->notifier->notification_exists( 'admin-email-changed', false ) ) {
618 $notifications = $bnfw->notifier->get_notifications( 'admin-email-changed' );
619
620 if ( count( $notifications ) > 0 ) {
621 // Ideally there should be only one notification for this type.
622 // If there are multiple notification then we will read data about only the last one
623 $bnfw->engine->send_notification( $bnfw->notifier->read_settings( end( $notifications )->ID ), $user['ID'] );
624 }
625 }
626
627 if ( ! $send ) {
628 return $send;
629 }
630
631 return ! $bnfw->notifier->is_notification_disabled( 'email-changed' );
632 }
633
634 /**
635 * On Email Changed.
636 *
637 * @since 1.6
638 *
639 * @param array $email_data Email Data.
640 * @param array $user User data.
641 *
642 * @return array Modified Email Data
643 */
644 public function on_email_changed( $email_data, $user ) {
645 return $this->handle_filtered_data_notification( 'email-changed', $email_data, $user['ID'] );
646 }
647
648 public function on_email_changing( $email_text, $new_user_details ) {
649 $notification_name = 'email-changing';
650
651 $notifications = $this->notifier->get_notifications( $notification_name );
652 if ( count( $notifications ) > 0 ) {
653 // Ideally there should be only one notification for this type.
654 // If there are multiple notification then we will read data about only the last one
655 $setting = $this->notifier->read_settings( end( $notifications )->ID );
656
657 $email_text = $this->engine->handle_shortcodes( $setting['message'], $setting['notification'], $new_user_details['newemail'] );
658 $email_text = $this->engine->handle_global_user_shortcodes( $email_text, $new_user_details['newemail'] );
659 $email_text = str_replace( '[email_change_confirmation_link]', esc_url( admin_url( 'profile.php?newuseremail=' . $new_user_details['hash'] ) ), $email_text );
660
661 }
662
663 return $email_text;
664 }
665
666 /**
667 * Send notification on core updated event.
668 *
669 * @since 1.6
670 *
671 * @param array $email_data Email Data.
672 * @param string $type The type of email being sent. Can be one of
673 * 'success', 'fail', 'manual', 'critical'.
674 * @param object $core_update The update offer that was attempted.
675 * @param mixed $result The result for the core update. Can be WP_Error.
676 *
677 * @return array Modified Email Data.
678 */
679 public function on_core_updated( $email_data, $type, $core_update, $result ) {
680 $notifications = $this->notifier->get_notifications( 'core-updated' );
681 if ( count( $notifications ) > 0 ) {
682 // Ideally there should be only one notification for this type.
683 // If there are multiple notification then we will read data about only the last one
684 $setting = $this->notifier->read_settings( end( $notifications )->ID );
685
686 $email_data = $this->engine->handle_core_updated_notification( $email_data, $setting, $type );
687 }
688
689 return $email_data;
690 }
691
692 /**
693 * Process User update notifications.
694 *
695 * @since 1.6
696 *
697 * @param string $notification_name Notification Name.
698 * @param array $email_data Email Data.
699 * @param string|int $extra_data User Id.
700 *
701 * @return array Modified Email Data.
702 */
703 private function handle_filtered_data_notification( $notification_name, $email_data, $extra_data ) {
704 $notifications = $this->notifier->get_notifications( $notification_name );
705 if ( count( $notifications ) > 0 ) {
706 // Ideally there should be only one notification for this type.
707 // If there are multiple notification then we will read data about only the last one
708 $setting = $this->notifier->read_settings( end( $notifications )->ID );
709
710 $email_data = $this->engine->handle_filtered_data_notification( $email_data, $setting, $extra_data );
711 }
712
713 return $email_data;
714 }
715
716 /**
717 * Set the email formatting to HTML.
718 *
719 * @since 1.4
720 */
721 public function set_html_content_type() {
722 return 'text/html';
723 }
724
725 /**
726 * Set the email formatting to text.
727 *
728 * @since 1.4
729 */
730 public function set_text_content_type() {
731 return 'text/plain';
732 }
733
734 /**
735 * Send notification for new users.
736 *
737 * @since 1.0
738 * @param int $user_id
739 */
740 public function user_register( $user_id ) {
741 $this->send_notification( 'admin-user', $user_id );
742 }
743
744 /**
745 * Send notification about new users to site admin.
746 *
747 * @since 1.7.1
748 *
749 * @param array $email_data Email details.
750 * @param WP_User $user User object.
751 * @param string $blogname Blog name.
752 *
753 * @return array Modified email details.
754 */
755 public function handle_user_registered_admin_email( $email_data, $user, $blogname ) {
756 return $this->handle_filtered_data_notification( 'admin-user', $email_data, $user->ID );
757 }
758
759 /**
760 * New User - Post-registration Email
761 *
762 * @since 1.1
763 * @param int $user_id New user id
764 */
765 public function welcome_email( $user_id ) {
766 $notifications = $this->notifier->get_notifications( 'welcome-email' );
767 foreach ( $notifications as $notification ) {
768 $this->engine->send_registration_email( $this->notifier->read_settings( $notification->ID ), get_userdata( $user_id ) );
769 }
770 }
771
772 /**
773 * Send notification when a user role changes.
774 *
775 * @since 1.3.9
776 *
777 * @param int $user_id User ID
778 * @param string $new_role New User role
779 * @param array $old_roles Old User role
780 */
781 public function user_role_changed( $user_id, $new_role, $old_roles ) {
782 if ( ! empty( $old_roles ) ) {
783 $notifications = $this->notifier->get_notifications( 'user-role' );
784 foreach ( $notifications as $notification ) {
785
786 /**
787 * Trigger User Role Changed - For User notification.
788 *
789 * @since 1.6.5
790 */
791 if ( apply_filters( 'bnfw_trigger_user-role_notification', true, $notification, $new_role, $old_roles ) ) {
792 $this->engine->send_user_role_changed_email(
793 $this->notifier->read_settings( $notification->ID ),
794 $user_id,
795 $old_roles[0],
796 $new_role
797 );
798 }
799 }
800
801 $notifications = $this->notifier->get_notifications( 'admin-role' );
802 foreach ( $notifications as $notification ) {
803
804 /**
805 * Trigger User Role Changed - For User notification.
806 *
807 * @since 1.6.5
808 */
809 if ( apply_filters( 'bnfw_trigger_admin-role_notification', true, $notification, $new_role, $old_roles ) ) {
810 $setting = $this->notifier->read_settings( $notification->ID );
811 $setting['message'] = $this->engine->handle_user_role_shortcodes( $setting['message'], $old_roles[0], $new_role );
812 $setting['subject'] = $this->engine->handle_user_role_shortcodes( $setting['subject'], $old_roles[0], $new_role );
813
814 $this->engine->send_notification( $setting, $user_id );
815 }
816 }
817 }
818 }
819
820 /**
821 * Send notification based on type and ref id
822 *
823 * @since 1.0
824 * @param string $type Notification type.
825 * @param mixed $ref_id Reference data.
826 */
827 public function send_notification( $type, $ref_id ) {
828 $notifications = $this->notifier->get_notifications( $type );
829 foreach ( $notifications as $notification ) {
830 $this->engine->send_notification( $this->notifier->read_settings( $notification->ID ), $ref_id );
831 }
832 }
833
834 /**
835 * Send notification async based on type and ref id.
836 *
837 * @param string $type Notification type.
838 * @param mixed $ref_id Reference data.
839 */
840 public function send_notification_async( $type, $ref_id ) {
841 $notifications = $this->notifier->get_notifications( $type, false );
842 foreach ( $notifications as $notification ) {
843 $transient = get_transient( 'bnfw-async-notifications' );
844 if ( ! is_array( $transient ) ) {
845 $transient = array();
846 }
847
848 $notification_data = array(
849 'ref_id' => $ref_id,
850 'notification_id' => $notification->ID,
851 'notification_type' => $type,
852 );
853
854 if ( ! in_array( $notification_data, $transient ) ) {
855 $transient[] = $notification_data;
856 set_transient( 'bnfw-async-notifications', $transient, 600 );
857 }
858 }
859 }
860
861 /**
862 * Can send comment notification or not
863 *
864 * @since 1.0
865 * @param unknown $comment
866 * @return unknown
867 */
868 private function can_send_comment_notification( $comment ) {
869 // Returns false if the comment is marked as spam AND admin has enabled suppression of spam
870 $suppress_spam = get_option( 'bnfw_suppress_spam' );
871 if ( '1' === $suppress_spam && ( 0 === strcmp( $comment->comment_approved, 'spam' ) ) ) {
872 return false;
873 }
874 return true;
875 }
876
877 /**
878 * Handle user request email content.
879 *
880 * @param string $content Content.
881 * @param array $email_data Email data.
882 *
883 * @return string Modified content.
884 */
885 public function handle_user_request_email_content( $content, $email_data ) {
886 $field = 'message';
887
888 switch ( $email_data['description'] ) {
889 case 'Export Personal Data':
890 $notification_name = 'ca-export-data';
891 $content = $this->handle_user_request_notification( $notification_name, $field, $email_data );
892 break;
893 case 'Erase Personal Data':
894 $notification_name = 'ca-erase-data';
895 $content = $this->handle_user_request_notification( $notification_name, $field, $email_data );
896 break;
897 }
898
899 return $content;
900 }
901
902 /**
903 * Handle user request email subject.
904 *
905 * @param string $subject Subject
906 * @param string $blogname Blog name
907 * @param array $email_data Email data.
908 *
909 * @return string Modified subject.
910 */
911 public function handle_user_request_email_subject( $subject, $blogname, $email_data ) {
912 $field = 'subject';
913
914 switch ( $email_data['description'] ) {
915 case 'Export Personal Data':
916 $notification_name = 'ca-export-data';
917 $subject = $this->handle_user_request_notification( $notification_name, $field, $email_data );
918 break;
919 case 'Erase Personal Data':
920 $notification_name = 'ca-erase-data';
921 $content = $this->handle_user_request_notification( $notification_name, $field, $email_data );
922 break;
923 }
924
925 return $subject;
926 }
927
928 /**
929 * Handle user confirmed action email content.
930 *
931 * @param string $content Content.
932 * @param array $email_data Email data.
933 *
934 * @return string Modified content.
935 */
936 public function handle_user_confirmed_action_email_content( $content, $email_data ) {
937 $field = 'message';
938
939 switch ( $email_data['description'] ) {
940 case 'Export Personal Data':
941 $notification_name = 'uc-export-data';
942 $content = $this->handle_user_confirmed_action_notification( $notification_name, $field, $email_data );
943 break;
944 case 'Erase Personal Data':
945 $notification_name = 'uc-erase-data';
946 $content = $this->handle_user_confirmed_action_notification( $notification_name, $field, $email_data );
947 break;
948 }
949
950 return $content;
951 }
952
953 /**
954 * Handle data exported email content.
955 *
956 * @param string $content Content.
957 * @param int $request_id
958 *
959 * @return string Modified content.
960 */
961 public function handle_data_export_email_content( $content, $request_id ) {
962 $field = 'message';
963 $notification_name = 'data-export';
964
965 $notifications = $this->notifier->get_notifications( $notification_name );
966 if ( count( $notifications ) > 0 ) {
967 // Ideally there should be only one notification for this type.
968 // If there are multiple notification then we will read data about only the last one
969 $setting = $this->notifier->read_settings( end( $notifications )->ID );
970
971 return $this->engine->handle_data_export_email_shortcodes( $setting[ $field ], $setting, $request_id );
972 }
973
974 return $content;
975 }
976
977 public function handle_erasure_complete_email_subject( $subject, $sitename, $email_data ) {
978 return $this->handle_erasure_complete_email_notification( 'subject', $subject, $email_data );
979 }
980
981 public function handle_erasure_complete_email_content( $content, $email_data ) {
982 if ( isset( $email_data['privacy_policy_url'] ) ) {
983 return $this->handle_erasure_complete_email_notification( 'message', $content, $email_data );
984 }
985
986 return $content;
987 }
988
989 protected function handle_erasure_complete_email_notification( $field, $content, $email_data ) {
990 $notification_name = 'data-erased';
991
992 $notifications = $this->notifier->get_notifications( $notification_name );
993 if ( count( $notifications ) > 0 ) {
994 // Ideally there should be only one notification for this type.
995 // If there are multiple notification then we will read data about only the last one
996 $setting = $this->notifier->read_settings( end( $notifications )->ID );
997
998 return $this->engine->handle_shortcodes( $setting[ $field ], $notification_name, $email_data );
999 }
1000
1001 return $content;
1002 }
1003
1004 /**
1005 * Send notification emails on shutdown.
1006 */
1007 public function on_shutdown() {
1008 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
1009 return;
1010 }
1011
1012 $transient = get_transient( 'bnfw-async-notifications' );
1013 if ( is_array( $transient ) ) {
1014 delete_transient( 'bnfw-async-notifications' );
1015 foreach ( $transient as $id_pairs ) {
1016 $this->engine->send_notification( $this->notifier->read_settings( $id_pairs['notification_id'] ), $id_pairs['ref_id'] );
1017 }
1018 }
1019 }
1020
1021 /**
1022 * Handle user request notification.
1023 *
1024 * @param string $notification_name Notification name.
1025 * @param string $field Field name.
1026 * @param array $email_data Email data.
1027 *
1028 * @return string Content.
1029 */
1030 protected function handle_user_request_notification( $notification_name, $field, $email_data ) {
1031 $notifications = $this->notifier->get_notifications( $notification_name );
1032 if ( count( $notifications ) > 0 ) {
1033 // Ideally there should be only one notification for this type.
1034 // If there are multiple notification then we will read data about only the last one
1035 $setting = $this->notifier->read_settings( end( $notifications )->ID );
1036
1037 return $this->engine->handle_user_request_email_shortcodes( $setting[ $field ], $setting, $email_data );
1038 }
1039
1040 return '';
1041 }
1042
1043 /**
1044 * Handle user confirmed action notification.
1045 *
1046 * @param string $notification_name Notification name.
1047 * @param string $field Field name.
1048 * @param array $email_data Email data.
1049 *
1050 * @return string Content.
1051 */
1052 protected function handle_user_confirmed_action_notification( $notification_name, $field, $email_data ) {
1053 $notifications = $this->notifier->get_notifications( $notification_name );
1054 if ( count( $notifications ) > 0 ) {
1055 // Ideally there should be only one notification for this type.
1056 // If there are multiple notification then we will read data about only the last one
1057 $setting = $this->notifier->read_settings( end( $notifications )->ID );
1058
1059 return $this->engine->handle_user_confirmed_action_email_shortcodes( $setting[ $field ], $setting, $email_data );
1060 }
1061
1062 return '';
1063 }
1064 }
1065
1066 /* ------------------------------------------------------------------------ *
1067 * Fire up the plugin
1068 * ------------------------------------------------------------------------ */
1069 BNFW::factory();
1070