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

1,063 lines 32.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 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.4
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 $post_type = $post->post_type;
398
399 if ( BNFW_Notification::POST_TYPE != $post_type ) {
400 $this->send_notification_async( 'future-' . $post_type, $post_id );
401 }
402 }
403
404 /**
405 * Send notification for new comments
406 *
407 * @since 1.0
408 * @param int $comment_id
409 */
410 public function comment_post( $comment_id ) {
411 $the_comment = get_comment( $comment_id );
412 $post = get_post( $the_comment->comment_post_ID );
413
414 if ( '1' !== $the_comment->comment_approved ) {
415 if ( $this->can_send_comment_notification( $the_comment ) ) {
416 $notification_type = 'moderate-' . $post->post_type . '-comment';
417 $this->send_notification( $notification_type, $comment_id );
418 }
419 } else {
420 $notification_type = 'new-comment'; // old notification name
421
422 if ( 'post' != $post->post_type ) {
423 $notification_type = 'comment-' . $post->post_type;
424 }
425
426 $this->send_notification( $notification_type, $comment_id );
427 }
428
429 // comment reply notification.
430 if ( $this->can_send_comment_notification( $the_comment ) ) {
431 if ( $the_comment->comment_parent > 0 ) {
432 $notification_type = 'reply-comment'; // old notification name
433 if ( 'post' != $post->post_type ) {
434 $notification_type = 'commentreply-' . $post->post_type;
435 }
436 $notifications = $this->notifier->get_notifications( $notification_type );
437 if ( count( $notifications ) > 0 ) {
438 $parent = get_comment( $the_comment->comment_parent );
439 if ( $parent->comment_author_email != $the_comment->comment_author_email ) {
440 foreach ( $notifications as $notification ) {
441 $this->engine->send_comment_reply_email( $this->notifier->read_settings( $notification->ID ), $the_comment, $parent );
442 }
443 }
444 }
445 }
446 }
447 }
448
449 /**
450 * Send notification for new trackback
451 *
452 * @since 1.0
453 * @param unknown $comment_id
454 */
455 function trackback_post( $comment_id ) {
456 $the_comment = get_comment( $comment_id );
457 if ( $this->can_send_comment_notification( $the_comment ) ) {
458 $this->send_notification( 'new-trackback', $comment_id );
459 }
460 }
461
462 /**
463 * Send notification for new pingbacks
464 *
465 * @since 1.0
466 * @param unknown $comment_id
467 */
468 function pingback_post( $comment_id ) {
469 $the_comment = get_comment( $comment_id );
470 if ( $this->can_send_comment_notification( $the_comment ) ) {
471 $this->send_notification( 'new-pingback', $comment_id );
472 }
473 }
474
475 /**
476 * Send notification for lost password.
477 *
478 * @since 1.0
479 */
480 function on_lost_password() {
481 $user_login = sanitize_text_field( $_POST['user_login'] );
482 $user = get_user_by( 'login', $user_login );
483 if ( $user ) {
484 $this->send_notification( 'admin-password', $user->ID );
485 }
486 }
487
488 /**
489 * Change the title of the password reset email that is sent to the user.
490 *
491 * @since 1.1
492 *
493 * @param string $title
494 * @param string $user_login
495 * @param string $user_data
496 *
497 * @return string
498 */
499 public function change_password_email_title( $title, $user_login = '', $user_data = '' ) {
500 $notifications = $this->notifier->get_notifications( 'user-password' );
501 if ( count( $notifications ) > 0 ) {
502 // Ideally there should be only one notification for this type.
503 // If there are multiple notification then we will read data about only the last one
504 $setting = $this->notifier->read_settings( end( $notifications )->ID );
505
506 if ( '' === $user_data ) {
507 return $this->engine->handle_shortcodes( $setting['subject'], 'user-password', $user_data->ID );
508 } else {
509 return $setting['subject'];
510 }
511 }
512
513 return $title;
514 }
515
516 /**
517 * Change the message of the password reset email.
518 *
519 * @since 1.1
520 *
521 * @param string $message
522 * @param string $key
523 * @param string $user_login
524 * @param string $user_data
525 *
526 * @return string
527 */
528 public function change_password_email_message( $message, $key, $user_login = '', $user_data = '' ) {
529 $notifications = $this->notifier->get_notifications( 'user-password' );
530 if ( count( $notifications ) > 0 ) {
531 // Ideally there should be only one notification for this type.
532 // If there are multiple notification then we will read data about only the last one
533 $setting = $this->notifier->read_settings( end( $notifications )->ID );
534
535 $message = $this->engine->handle_password_reset_shortcodes( $setting, $key, $user_login, $user_data );
536
537 if ( 'html' == $setting['email-formatting'] ) {
538 add_filter( 'wp_mail_content_type', array( $this, 'set_html_content_type' ) );
539 $message = wpautop( $message );
540 } else {
541 add_filter( 'wp_mail_content_type', array( $this, 'set_text_content_type' ) );
542 }
543 } else {
544 if ( $this->notifier->notification_exists( 'user-password', false ) ) {
545 // disabled notification exists, so disable the email by returning empty string.
546 return '';
547 }
548 }
549
550 return $message;
551 }
552
553 /**
554 * On Password reset.
555 *
556 * @param WP_User $user User who's password was changed.
557 */
558 public function on_password_reset( $user ) {
559 $notifications = $this->notifier->get_notifications( 'password-changed' );
560 foreach ( $notifications as $notification ) {
561 $this->engine->send_password_changed_email( $this->notifier->read_settings( $notification->ID ), $user );
562 }
563 }
564
565 /**
566 * Should the password changed email be sent?
567 *
568 * @param $send
569 * @param $user
570 * @param $userdata
571 *
572 * @return bool
573 */
574 public function should_password_changed_email_be_sent( $send, $user, $userdata ) {
575 $bnfw = BNFW::factory();
576
577 if ( ! $send ) {
578 return $send;
579 }
580
581 return ! $bnfw->notifier->is_notification_disabled( 'password-changed' );
582 }
583
584 /**
585 * On Password Changed.
586 *
587 * @since 1.6
588 *
589 * @param array $email_data Email Data.
590 * @param array $user User data.
591 *
592 * @return array Modified Email Data
593 */
594 public function on_password_changed( $email_data, $user ) {
595 return $this->handle_filtered_data_notification( 'password-changed', $email_data, $user['ID'] );
596 }
597
598 /**
599 * Should the email changed email be sent?
600 *
601 * @param $send
602 * @param $user
603 * @param $userdata
604 *
605 * @return bool
606 */
607 public function should_email_changed_email_be_sent( $send, $user, $userdata ) {
608 $bnfw = BNFW::factory();
609
610 if ( $bnfw->notifier->notification_exists( 'admin-email-changed', false ) ) {
611 $notifications = $bnfw->notifier->get_notifications( 'admin-email-changed' );
612
613 if ( count( $notifications ) > 0 ) {
614 // Ideally there should be only one notification for this type.
615 // If there are multiple notification then we will read data about only the last one
616 $bnfw->engine->send_notification( $bnfw->notifier->read_settings( end( $notifications )->ID ), $user['ID'] );
617 }
618 }
619
620 if ( ! $send ) {
621 return $send;
622 }
623
624 return ! $bnfw->notifier->is_notification_disabled( 'email-changed' );
625 }
626
627 /**
628 * On Email Changed.
629 *
630 * @since 1.6
631 *
632 * @param array $email_data Email Data.
633 * @param array $user User data.
634 *
635 * @return array Modified Email Data
636 */
637 public function on_email_changed( $email_data, $user ) {
638 return $this->handle_filtered_data_notification( 'email-changed', $email_data, $user['ID'] );
639 }
640
641 public function on_email_changing( $email_text, $new_user_details ) {
642 $notification_name = 'email-changing';
643
644 $notifications = $this->notifier->get_notifications( $notification_name );
645 if ( count( $notifications ) > 0 ) {
646 // Ideally there should be only one notification for this type.
647 // If there are multiple notification then we will read data about only the last one
648 $setting = $this->notifier->read_settings( end( $notifications )->ID );
649
650 $email_text = $this->engine->handle_shortcodes( $setting['message'], $setting['notification'], $new_user_details['newemail'] );
651 $email_text = $this->engine->handle_global_user_shortcodes( $email_text, $new_user_details['newemail'] );
652 $email_text = str_replace( '[email_change_confirmation_link]', esc_url( admin_url( 'profile.php?newuseremail=' . $new_user_details['hash'] ) ), $email_text );
653
654 }
655
656 return $email_text;
657 }
658
659 /**
660 * Send notification on core updated event.
661 *
662 * @since 1.6
663 *
664 * @param array $email_data Email Data.
665 * @param string $type The type of email being sent. Can be one of
666 * 'success', 'fail', 'manual', 'critical'.
667 * @param object $core_update The update offer that was attempted.
668 * @param mixed $result The result for the core update. Can be WP_Error.
669 *
670 * @return array Modified Email Data.
671 */
672 public function on_core_updated( $email_data, $type, $core_update, $result ) {
673 $notifications = $this->notifier->get_notifications( 'core-updated' );
674 if ( count( $notifications ) > 0 ) {
675 // Ideally there should be only one notification for this type.
676 // If there are multiple notification then we will read data about only the last one
677 $setting = $this->notifier->read_settings( end( $notifications )->ID );
678
679 $email_data = $this->engine->handle_core_updated_notification( $email_data, $setting, $type );
680 }
681
682 return $email_data;
683 }
684
685 /**
686 * Process User update notifications.
687 *
688 * @since 1.6
689 *
690 * @param string $notification_name Notification Name.
691 * @param array $email_data Email Data.
692 * @param string|int $extra_data User Id.
693 *
694 * @return array Modified Email Data.
695 */
696 private function handle_filtered_data_notification( $notification_name, $email_data, $extra_data ) {
697 $notifications = $this->notifier->get_notifications( $notification_name );
698 if ( count( $notifications ) > 0 ) {
699 // Ideally there should be only one notification for this type.
700 // If there are multiple notification then we will read data about only the last one
701 $setting = $this->notifier->read_settings( end( $notifications )->ID );
702
703 $email_data = $this->engine->handle_filtered_data_notification( $email_data, $setting, $extra_data );
704 }
705
706 return $email_data;
707 }
708
709 /**
710 * Set the email formatting to HTML.
711 *
712 * @since 1.4
713 */
714 public function set_html_content_type() {
715 return 'text/html';
716 }
717
718 /**
719 * Set the email formatting to text.
720 *
721 * @since 1.4
722 */
723 public function set_text_content_type() {
724 return 'text/plain';
725 }
726
727 /**
728 * Send notification for new users.
729 *
730 * @since 1.0
731 * @param int $user_id
732 */
733 public function user_register( $user_id ) {
734 $this->send_notification( 'admin-user', $user_id );
735 }
736
737 /**
738 * Send notification about new users to site admin.
739 *
740 * @since 1.7.1
741 *
742 * @param array $email_data Email details.
743 * @param WP_User $user User object.
744 * @param string $blogname Blog name.
745 *
746 * @return array Modified email details.
747 */
748 public function handle_user_registered_admin_email( $email_data, $user, $blogname ) {
749 return $this->handle_filtered_data_notification( 'admin-user', $email_data, $user->ID );
750 }
751
752 /**
753 * New User - Post-registration Email
754 *
755 * @since 1.1
756 * @param int $user_id New user id
757 */
758 public function welcome_email( $user_id ) {
759 $notifications = $this->notifier->get_notifications( 'welcome-email' );
760 foreach ( $notifications as $notification ) {
761 $this->engine->send_registration_email( $this->notifier->read_settings( $notification->ID ), get_userdata( $user_id ) );
762 }
763 }
764
765 /**
766 * Send notification when a user role changes.
767 *
768 * @since 1.3.9
769 *
770 * @param int $user_id User ID
771 * @param string $new_role New User role
772 * @param array $old_roles Old User role
773 */
774 public function user_role_changed( $user_id, $new_role, $old_roles ) {
775 if ( ! empty( $old_roles ) ) {
776 $notifications = $this->notifier->get_notifications( 'user-role' );
777 foreach ( $notifications as $notification ) {
778
779 /**
780 * Trigger User Role Changed - For User notification.
781 *
782 * @since 1.6.5
783 */
784 if ( apply_filters( 'bnfw_trigger_user-role_notification', true, $notification, $new_role, $old_roles ) ) {
785 $this->engine->send_user_role_changed_email(
786 $this->notifier->read_settings( $notification->ID ),
787 $user_id,
788 $old_roles[0],
789 $new_role
790 );
791 }
792 }
793
794 $notifications = $this->notifier->get_notifications( 'admin-role' );
795 foreach ( $notifications as $notification ) {
796
797 /**
798 * Trigger User Role Changed - For User notification.
799 *
800 * @since 1.6.5
801 */
802 if ( apply_filters( 'bnfw_trigger_admin-role_notification', true, $notification, $new_role, $old_roles ) ) {
803 $setting = $this->notifier->read_settings( $notification->ID );
804 $setting['message'] = $this->engine->handle_user_role_shortcodes( $setting['message'], $old_roles[0], $new_role );
805 $setting['subject'] = $this->engine->handle_user_role_shortcodes( $setting['subject'], $old_roles[0], $new_role );
806
807 $this->engine->send_notification( $setting, $user_id );
808 }
809 }
810 }
811 }
812
813 /**
814 * Send notification based on type and ref id
815 *
816 * @since 1.0
817 * @param string $type Notification type.
818 * @param mixed $ref_id Reference data.
819 */
820 public function send_notification( $type, $ref_id ) {
821 $notifications = $this->notifier->get_notifications( $type );
822 foreach ( $notifications as $notification ) {
823 $this->engine->send_notification( $this->notifier->read_settings( $notification->ID ), $ref_id );
824 }
825 }
826
827 /**
828 * Send notification async based on type and ref id.
829 *
830 * @param string $type Notification type.
831 * @param mixed $ref_id Reference data.
832 */
833 public function send_notification_async( $type, $ref_id ) {
834 $notifications = $this->notifier->get_notifications( $type, false );
835 foreach ( $notifications as $notification ) {
836 $transient = get_transient( 'bnfw-async-notifications' );
837 if ( ! is_array( $transient ) ) {
838 $transient = array();
839 }
840
841 $notification_data = array(
842 'ref_id' => $ref_id,
843 'notification_id' => $notification->ID,
844 'notification_type' => $type,
845 );
846
847 if ( ! in_array( $notification_data, $transient ) ) {
848 $transient[] = $notification_data;
849 set_transient( 'bnfw-async-notifications', $transient, 600 );
850 }
851 }
852 }
853
854 /**
855 * Can send comment notification or not
856 *
857 * @since 1.0
858 * @param unknown $comment
859 * @return unknown
860 */
861 private function can_send_comment_notification( $comment ) {
862 // Returns false if the comment is marked as spam AND admin has enabled suppression of spam
863 $suppress_spam = get_option( 'bnfw_suppress_spam' );
864 if ( '1' === $suppress_spam && ( 0 === strcmp( $comment->comment_approved, 'spam' ) ) ) {
865 return false;
866 }
867 return true;
868 }
869
870 /**
871 * Handle user request email content.
872 *
873 * @param string $content Content.
874 * @param array $email_data Email data.
875 *
876 * @return string Modified content.
877 */
878 public function handle_user_request_email_content( $content, $email_data ) {
879 $field = 'message';
880
881 switch ( $email_data['description'] ) {
882 case 'Export Personal Data':
883 $notification_name = 'ca-export-data';
884 $content = $this->handle_user_request_notification( $notification_name, $field, $email_data );
885 break;
886 case 'Erase Personal Data':
887 $notification_name = 'ca-erase-data';
888 $content = $this->handle_user_request_notification( $notification_name, $field, $email_data );
889 break;
890 }
891
892 return $content;
893 }
894
895 /**
896 * Handle user request email subject.
897 *
898 * @param string $subject Subject
899 * @param string $blogname Blog name
900 * @param array $email_data Email data.
901 *
902 * @return string Modified subject.
903 */
904 public function handle_user_request_email_subject( $subject, $blogname, $email_data ) {
905 $field = 'subject';
906
907 switch ( $email_data['description'] ) {
908 case 'Export Personal Data':
909 $notification_name = 'ca-export-data';
910 $subject = $this->handle_user_request_notification( $notification_name, $field, $email_data );
911 break;
912 case 'Erase Personal Data':
913 $notification_name = 'ca-erase-data';
914 $content = $this->handle_user_request_notification( $notification_name, $field, $email_data );
915 break;
916 }
917
918 return $subject;
919 }
920
921 /**
922 * Handle user confirmed action email content.
923 *
924 * @param string $content Content.
925 * @param array $email_data Email data.
926 *
927 * @return string Modified content.
928 */
929 public function handle_user_confirmed_action_email_content( $content, $email_data ) {
930 $field = 'message';
931
932 switch ( $email_data['description'] ) {
933 case 'Export Personal Data':
934 $notification_name = 'uc-export-data';
935 $content = $this->handle_user_confirmed_action_notification( $notification_name, $field, $email_data );
936 break;
937 case 'Erase Personal Data':
938 $notification_name = 'uc-erase-data';
939 $content = $this->handle_user_confirmed_action_notification( $notification_name, $field, $email_data );
940 break;
941 }
942
943 return $content;
944 }
945
946 /**
947 * Handle data exported email content.
948 *
949 * @param string $content Content.
950 * @param int $request_id
951 *
952 * @return string Modified content.
953 */
954 public function handle_data_export_email_content( $content, $request_id ) {
955 $field = 'message';
956 $notification_name = 'data-export';
957
958 $notifications = $this->notifier->get_notifications( $notification_name );
959 if ( count( $notifications ) > 0 ) {
960 // Ideally there should be only one notification for this type.
961 // If there are multiple notification then we will read data about only the last one
962 $setting = $this->notifier->read_settings( end( $notifications )->ID );
963
964 return $this->engine->handle_data_export_email_shortcodes( $setting[ $field ], $setting, $request_id );
965 }
966
967 return $content;
968 }
969
970 public function handle_erasure_complete_email_subject( $subject, $sitename, $email_data ) {
971 return $this->handle_erasure_complete_email_notification( 'subject', $subject, $email_data );
972 }
973
974 public function handle_erasure_complete_email_content( $content, $email_data ) {
975 if ( isset( $email_data['privacy_policy_url'] ) ) {
976 return $this->handle_erasure_complete_email_notification( 'message', $content, $email_data );
977 }
978
979 return $content;
980 }
981
982 protected function handle_erasure_complete_email_notification( $field, $content, $email_data ) {
983 $notification_name = 'data-erased';
984
985 $notifications = $this->notifier->get_notifications( $notification_name );
986 if ( count( $notifications ) > 0 ) {
987 // Ideally there should be only one notification for this type.
988 // If there are multiple notification then we will read data about only the last one
989 $setting = $this->notifier->read_settings( end( $notifications )->ID );
990
991 return $this->engine->handle_shortcodes( $setting[ $field ], $notification_name, $email_data );
992 }
993
994 return $content;
995 }
996
997 /**
998 * Send notification emails on shutdown.
999 */
1000 public function on_shutdown() {
1001 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
1002 return;
1003 }
1004
1005 $transient = get_transient( 'bnfw-async-notifications' );
1006 if ( is_array( $transient ) ) {
1007 delete_transient( 'bnfw-async-notifications' );
1008 foreach ( $transient as $id_pairs ) {
1009 $this->engine->send_notification( $this->notifier->read_settings( $id_pairs['notification_id'] ), $id_pairs['ref_id'] );
1010 }
1011 }
1012 }
1013
1014 /**
1015 * Handle user request notification.
1016 *
1017 * @param string $notification_name Notification name.
1018 * @param string $field Field name.
1019 * @param array $email_data Email data.
1020 *
1021 * @return string Content.
1022 */
1023 protected function handle_user_request_notification( $notification_name, $field, $email_data ) {
1024 $notifications = $this->notifier->get_notifications( $notification_name );
1025 if ( count( $notifications ) > 0 ) {
1026 // Ideally there should be only one notification for this type.
1027 // If there are multiple notification then we will read data about only the last one
1028 $setting = $this->notifier->read_settings( end( $notifications )->ID );
1029
1030 return $this->engine->handle_user_request_email_shortcodes( $setting[ $field ], $setting, $email_data );
1031 }
1032
1033 return '';
1034 }
1035
1036 /**
1037 * Handle user confirmed action notification.
1038 *
1039 * @param string $notification_name Notification name.
1040 * @param string $field Field name.
1041 * @param array $email_data Email data.
1042 *
1043 * @return string Content.
1044 */
1045 protected function handle_user_confirmed_action_notification( $notification_name, $field, $email_data ) {
1046 $notifications = $this->notifier->get_notifications( $notification_name );
1047 if ( count( $notifications ) > 0 ) {
1048 // Ideally there should be only one notification for this type.
1049 // If there are multiple notification then we will read data about only the last one
1050 $setting = $this->notifier->read_settings( end( $notifications )->ID );
1051
1052 return $this->engine->handle_user_confirmed_action_email_shortcodes( $setting[ $field ], $setting, $email_data );
1053 }
1054
1055 return '';
1056 }
1057 }
1058
1059 /* ------------------------------------------------------------------------ *
1060 * Fire up the plugin
1061 * ------------------------------------------------------------------------ */
1062 BNFW::factory();
1063