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

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