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

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

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