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

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