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

1,665 lines 55.7 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.9.3
7 * Requires at least: 4.8
8 * Requires PHP: 7.1
9 * Author: Made with Fuel
10 * Author URI: https://madewithfuel.com/
11 * License: GPLv2 or later
12 * License URI: http://www.gnu.org/licenses/gpl-2.0.html
13 * Text Domain: bnfw
14 * Domain Path: /languages
15 *
16 * @package bnfw
17 */
18
19 /**
20 * Copyright © 2023 Made with Fuel Ltd. (hello@betternotificationsforwp.com)
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License, version 2, as
23 * published by the Free Software Foundation.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 */
32 if ( ! class_exists( 'BNFW', false ) ) {
33 /**
34 * BNFW main class.
35 */
36 class BNFW {
37 /**
38 * BNFW version.
39 *
40 * @var string
41 */
42 public $bnfw_version = '1.9.3';
43 /**
44 * Class Constructor.
45 *
46 * @since 1.0
47 */
48 public function __construct() {
49 $this->bnfw_define_constants();
50 $this->includes();
51 $this->hooks();
52 /**
53 * BNFW Notification.
54 *
55 * @var \BNFW_Notification
56 */
57 $this->notifier = new BNFW_Notification();
58
59 /**
60 * BNFW Engine.
61 *
62 * @var \BNFW_Engine
63 */
64 $this->engine = new BNFW_Engine();
65 }
66
67 /**
68 * Factory method to return the instance of the class.
69 *
70 * Makes sure that only one instance is created.
71 *
72 * @return \BNFW Instance of the class.
73 */
74 public static function factory() {
75 static $instance = false;
76 if ( ! $instance ) {
77 $instance = new self();
78 }
79 return $instance;
80 }
81
82 /**
83 * Loads the plugin language files
84 *
85 * @since 1.0
86 */
87 public function load_textdomain() {
88 // Load localization domain.
89 $this->translations = dirname( plugin_basename( __FILE__ ) ) . '/languages/';
90 load_plugin_textdomain( 'bnfw', false, $this->translations );
91 }
92
93 /**
94 * Include required files.
95 *
96 * @since 1.0
97 */
98 public function includes() {
99
100 // Load license related classes.
101 if ( ! class_exists( 'EDD_SL_Plugin_Updater' ) ) {
102 require_once 'includes/libraries/class-edd-sl-plugin-updater.php';
103 }
104
105 include_once ABSPATH . 'wp-admin/includes/plugin.php';
106
107 require_once 'vendor/persist-admin-notices-dismissal/persist-admin-notices-dismissal.php';
108
109 require_once 'includes/license/class-bnfw-license.php';
110 require_once 'includes/license/class-bnfw-license-setting.php';
111
112 // Load Engine.
113 require_once 'includes/engine/class-bnfw-engine.php';
114 require_once 'includes/overrides.php';
115
116 // Load notification post type and notification helpers.
117 require_once 'includes/admin/class-bnfw-notification.php';
118 require_once 'includes/notification/post-notification.php';
119
120 // Helpers.
121 require_once 'includes/helpers/helpers.php';
122 require_once 'includes/helpers/class-bnfw-ajax.php';
123
124 // Load Admin Pages.
125 if ( is_admin() ) {
126 require_once 'includes/admin/class-bnfw-settings.php';
127 }
128 }
129 /**
130 * Define BNFW Constants.
131 */
132 private function bnfw_define_constants() {
133 define( 'BNFW_VERSION', $this->bnfw_version );
134 }
135 /**
136 * Register Hooks.
137 *
138 * @since 1.0
139 */
140 public function hooks() {
141 global $wp_version;
142
143 register_activation_hook( __FILE__, array( $this, 'activate' ) );
144
145 add_action( 'init', array( $this, 'bnfw_init' ) );
146 add_action( 'admin_init', array( 'PAnD', 'init' ) );
147 add_action( 'admin_init', array( $this, 'add_capability_to_admin' ) );
148
149 // Fire when privately publish.
150 add_action( 'auto-draft_to_private', array( $this, 'publish_private_post' ), 10, 1 );
151
152 add_action( 'draft_to_private', array( $this, 'private_post' ) );
153 add_action( 'future_to_private', array( $this, 'private_post' ) );
154 add_action( 'pending_to_private', array( $this, 'private_post' ) );
155 add_action( 'publish_to_private', array( $this, 'private_post' ) );
156
157 add_action( 'wp_insert_post', array( $this, 'insert_post' ), 10, 3 );
158
159 add_action( 'attachment_updated', array( $this, 'trash_attachment' ), 10, 3 );
160
161 add_action( 'publish_to_trash', array( $this, 'trash_post' ) );
162
163 add_action( 'auto-draft_to_publish', array( $this, 'publish_post' ) );
164 add_action( 'draft_to_publish', array( $this, 'publish_post' ) );
165 add_action( 'future_to_publish', array( $this, 'publish_post' ) );
166 add_action( 'pending_to_publish', array( $this, 'publish_post' ) );
167 add_action( 'private_to_publish', array( $this, 'publish_post' ) );
168
169 add_action( 'publish_to_publish', array( $this, 'update_post' ) );
170 add_action( 'private_to_private', array( $this, 'update_post' ) );
171
172 add_action( 'add_attachment', array( $this, 'new_publish_media_notification' ), 10, 1 );
173 add_action( 'edit_attachment', array( $this, 'media_attachment_data_update_notification' ), 10 );
174
175 add_action( 'transition_post_status', array( $this, 'on_post_transition' ), 10, 3 );
176
177 add_action( 'init', array( $this, 'custom_post_type_hooks' ), 100 );
178 add_action( 'create_term', array( $this, 'create_term' ), 10, 3 );
179
180 add_action( 'transition_comment_status', array( $this, 'on_comment_status_change' ), 10, 3 );
181 add_action( 'comment_post', array( $this, 'comment_post' ) );
182 add_action( 'trackback_post', array( $this, 'trackback_post' ) );
183 add_action( 'pingback_post', array( $this, 'pingback_post' ) );
184
185 add_action( 'user_register', array( $this, 'user_register' ) );
186
187 add_action( 'user_register', array( $this, 'welcome_email' ) );
188
189 if ( is_plugin_active( 'members/members.php' ) ) {
190
191 add_action( 'add_user_role', array( $this, 'user_role_added_from_member_plugin' ), 10, 2 );
192 add_action( 'remove_user_role', array( $this, 'user_role_removed_from_member_plugin' ), 10, 2 );
193 add_action( 'set_user_role', array( $this, 'user_role_changed' ), 10, 3 );
194
195 add_action( 'profile_update', array( $this, 'user_role_added' ), 10, 2 );
196 } else {
197 add_action( 'set_user_role', array( $this, 'user_role_changed' ), 10, 3 );
198 }
199
200 add_action( 'wp_login', array( $this, 'user_login' ), 10, 2 );
201
202 if ( version_compare( $wp_version, '4.4', '>=' ) ) {
203 add_filter( 'retrieve_password_title', array( $this, 'change_password_email_title' ), 10, 3 );
204 } else {
205 add_filter( 'retrieve_password_title', array( $this, 'change_password_email_title' ) );
206 }
207 add_action( 'lostpassword_post', array( $this, 'on_lost_password' ) );
208 add_filter( 'retrieve_password_message', array( $this, 'change_password_email_message' ), 10, 4 );
209
210 add_action( 'after_password_reset', array( $this, 'on_password_reset' ) );
211
212 add_filter( 'send_password_change_email', array( $this, 'should_password_changed_email_be_sent' ), 10, 3 );
213 add_filter( 'password_change_email', array( $this, 'on_password_changed' ), 10, 2 );
214
215 add_filter( 'send_email_change_email', array( $this, 'should_email_changed_email_be_sent' ), 10, 3 );
216 add_filter( 'email_change_email', array( $this, 'on_email_changed' ), PHP_INT_MAX, 3 );
217 add_filter( 'new_user_email_content', array( $this, 'on_email_changing' ), PHP_INT_MAX, 2 );
218
219 add_filter( 'auto_core_update_email', array( $this, 'on_core_updated' ), 10, 4 );
220
221 add_filter( 'user_request_action_email_content', array( $this, 'handle_user_request_email_content' ), 10, 2 );
222 add_filter( 'user_request_action_email_subject', array( $this, 'handle_user_request_email_subject' ), 10, 3 );
223
224 add_filter( 'user_confirmed_action_email_content', array( $this, 'handle_user_confirmed_action_email_content' ), 10, 2 );
225
226 add_filter( 'wp_privacy_personal_data_email_content', array( $this, 'handle_data_export_email_content' ), 10, 3 );
227
228 add_filter( 'user_erasure_complete_email_subject', array( $this, 'handle_erasure_complete_email_subject' ), 10, 3 );
229 add_filter( 'user_confirmed_action_email_content', array( $this, 'handle_erasure_complete_email_content' ), 10, 2 );
230
231 add_filter( 'plugin_action_links', array( $this, 'plugin_action_links' ), 10, 4 );
232 add_filter( 'wp_mail', array( $this, 'bnfw_update_email_changing_subject' ), 10, 1 );
233 add_action( 'shutdown', array( $this, 'on_shutdown' ) );
234
235 }
236
237 /**
238 * Loads the plugin language files
239 *
240 * @since 1.8.7
241 */
242 public function bnfw_init() {
243 // Load localization domain
244 $this->load_textdomain();
245 }
246
247 /**
248 * Changed the subject when use request to change the email.
249 *
250 * @param array $args Email arguments.
251 *
252 * @return mixed
253 */
254 public function bnfw_update_email_changing_subject( $args ) {
255 $notification_name = 'email-changed';
256 $notifications = $this->notifier->get_notifications( $notification_name );
257 if ( count( $notifications ) > 0 ) {
258 $setting = $this->notifier->read_settings( end( $notifications )->ID );
259 if ( str_contains( $args['message'], '/profile.php?newuseremail=' ) ) {
260 $subject = str_replace( '[global_site_title]', get_bloginfo( 'name' ), $setting['subject'] );
261 $subject = str_replace( '[[global_site_title]]', get_bloginfo( 'name' ), $subject );
262 $args['subject'] = $subject;
263 } elseif ( str_contains( $args['message'], '?newuseremail=' ) ) {
264 $subject = str_replace( '[global_site_title]', get_bloginfo( 'name' ), $setting['subject'] );
265 $subject = str_replace( '[[global_site_title]]', get_bloginfo( 'name' ), $subject );
266 $args['subject'] = $subject;
267 }
268 }
269
270 return $args;
271 }
272
273 /**
274 * Fires when a media is moved to trash.
275 *
276 * @param int $post_id Post ID.
277 * @param WP_Post $post_after Post object following the update.
278 * @param WP_Post $post_before Post object before the update.
279 */
280 public function trash_attachment( $post_id, $post_after, $post_before ) {
281 $post_type = get_post_type( $post_id );
282 $post_status_before = $post_before->post_status;
283 $post_status_after = $post_after->post_status;
284 if ( BNFW_Notification::POST_TYPE !== $post_type && 'inherit' === $post_status_before && 'trash' === $post_status_after ) {
285 $this->send_notification_async( 'trash-' . $post_type, $post_id );
286 }
287 }
288
289 /**
290 * Add 'bnfw' capability to admin.
291 */
292 public function add_capability_to_admin() {
293 $admins = get_role( 'administrator' );
294
295 if ( is_null( $admins ) ) {
296 return;
297 }
298
299 if ( ! $admins->has_cap( 'bnfw' ) ) {
300 $admins->add_cap( 'bnfw' );
301 }
302 }
303
304 /**
305 * On post transition.
306 *
307 * @param string $new_status New post status.
308 * @param string $old_status Old post status.
309 * @param \WP_Post $post Post object.
310 */
311 public function on_post_transition( $new_status, $old_status, $post ) {
312 if ( ! is_a( $post, 'WP_Post' ) ) {
313 return;
314 }
315
316 if ( 'pending' === $old_status ) {
317 return;
318 }
319
320 if ( 'pending' !== $new_status ) {
321 return;
322 }
323
324 $this->on_post_pending( $post->ID, $post );
325 }
326
327 /**
328 * Setup hooks for custom post types.
329 *
330 * @since 1.2
331 */
332 public function custom_post_type_hooks() {
333 $post_types = get_post_types( array( 'public' => true ), 'names' );
334 $post_types = array_diff( $post_types, array( BNFW_Notification::POST_TYPE ) );
335
336 foreach ( $post_types as $post_type ) {
337 add_action( 'future_' . $post_type, array( $this, 'on_post_scheduled' ), 10, 2 );
338 }
339 }
340
341 /**
342 * Importer.
343 */
344 public function activate() {
345 require_once dirname( __FILE__ ) . '/includes/class-bnfw-import.php';
346 $importer = new BNFW_Import();
347 $importer->import();
348 }
349
350 /**
351 * Add 'Settings' link below BNFW in Plugins list.
352 *
353 * @since 1.0
354 * @param string[] $links An array of plugin action links. By default this can include 'activate',
355 * 'deactivate', and 'delete'. With Multisite active this can also include.
356 * @param string $file Path to the plugin file relative to the plugins directory.
357 * @return array plugin action links.
358 */
359 public function plugin_action_links( $links, $file ) {
360 $plugin_file = 'bnfw/bnfw.php';
361 if ( $file === $plugin_file ) {
362 $settings_link = '<a href="' . esc_url( admin_url( 'edit.php?post_type=bnfw_notification&page=bnfw-settings' ) ) . '">' . esc_html__( 'Settings', 'bnfw' ) . '</a>';
363 array_unshift( $links, $settings_link );
364 }
365 return $links;
366 }
367
368 /**
369 * When a new term is created.
370 *
371 * @since 1.0
372 * @param int $term_id Term ID.
373 * @param int $tt_id Term taxonomy ID.
374 * @param string $taxonomy Taxonomy slug.
375 */
376 public function create_term( $term_id, $tt_id, $taxonomy ) {
377 $this->send_notification( 'newterm-' . $taxonomy, $term_id );
378 }
379
380 /**
381 * Fires when a post is created for the first time.
382 *
383 * @param int $post_id Post ID.
384 * @param object $post Post object.
385 * @param bool $update Whether this is an existing post being updated or not.
386 *
387 * @since 1.3.1
388 */
389 public function insert_post( $post_id, $post, $update ) {
390 // Some themes like P2, directly insert posts into DB.
391 $insert_post_themes = apply_filters( 'bnfw_insert_post_themes', array( 'P2', 'Syncope' ) );
392 $current_theme = wp_get_theme();
393
394 /**
395 * Whether to trigger insert post hook.
396 *
397 * @since 1.4
398 */
399 $trigger_insert_post = apply_filters( 'bnfw_trigger_insert_post', false, $post_id, $update );
400
401 if ( in_array( $current_theme->get( 'Name' ), $insert_post_themes, true ) || $trigger_insert_post ) {
402 $this->handle_inserted_post( $post_id, $update );
403 }
404 }
405
406 /**
407 * Trigger New Post published notification for ACF forms.
408 *
409 * @param string $form ACF Form.
410 * @param int $post_id Post ID.
411 */
412 public function acf_submit_form( $form, $post_id ) {
413 $this->handle_inserted_post( $post_id );
414 }
415
416 /**
417 * Trigger correct notifications for inserted posts.
418 *
419 * @param int $post_id Post id.
420 * @param bool $update Whether the post was updated.
421 *
422 * @since 1.6.7
423 */
424 private function handle_inserted_post( $post_id, $update ) {
425 $post = get_post( $post_id );
426
427 if ( ! is_a( $post, 'WP_Post' ) ) {
428 return;
429 }
430
431 switch ( $post->post_status ) {
432 case 'publish':
433 if ( $update ) {
434 $this->update_post( $post );
435 } else {
436 $this->publish_post( $post );
437 }
438 break;
439
440 case 'private':
441 $this->private_post( $post );
442 break;
443
444 case 'pending':
445 $this->on_post_pending( $post_id, $post );
446 break;
447
448 case 'future':
449 $this->on_post_scheduled( $post_id, $post );
450 break;
451 }
452 }
453
454 /**
455 * Fires when a post is created for the first time.
456 *
457 * @since 1.0
458 * @param object $post Post Object.
459 */
460 public function publish_post( $post ) {
461 $post_id = $post->ID;
462 $post_type = $post->post_type;
463
464 if ( BNFW_Notification::POST_TYPE !== $post_type ) {
465 $this->send_notification_async( 'new-' . $post_type, $post_id );
466 }
467 }
468
469 /**
470 * Fire an email when post status change to private.
471 *
472 * @param object $post Post Object.
473 *
474 * @return void
475 */
476 public function publish_private_post( $post ) {
477 $this->private_post( $post );
478 }
479
480 /**
481 * Fires when a private post is created.
482 *
483 * @since 1.6
484 * @param object $post Post Object.
485 */
486 public function private_post( $post ) {
487 $post_id = $post->ID;
488 $post_type = $post->post_type;
489
490 if ( BNFW_Notification::POST_TYPE !== $post_type ) {
491 $this->send_notification_async( 'private-' . $post_type, $post_id );
492 }
493 }
494
495 /**
496 * Fires when a post is updated.
497 *
498 * @since 1.0
499 * @param WP_Post $post Post object.
500 */
501 public function update_post( $post ) {
502 if ( $this->is_metabox_request() ) {
503 return;
504 }
505
506 $post_id = $post->ID;
507 $post_type = $post->post_type;
508
509 if ( BNFW_Notification::POST_TYPE !== $post_type ) {
510 $this->send_notification_async( 'update-' . $post_type, $post_id );
511 }
512 }
513
514 /**
515 * Fires when a post is moved publish to trash.
516 *
517 * @param WP_Post $post Post object.
518 */
519 public function trash_post( $post ) {
520 if ( $this->is_metabox_request() ) {
521 return;
522 }
523 $post_id = $post->ID;
524 $post_type = $post->post_type;
525
526 if ( BNFW_Notification::POST_TYPE !== $post_type ) {
527 $this->send_notification_async( 'trash-' . $post_type, $post_id );
528 }
529 }
530
531 /**
532 * Fires when a post is pending for review.
533 *
534 * @since 1.1
535 * @param int $post_id Post ID.
536 * @param object $post Post object.
537 */
538 public function on_post_pending( $post_id, $post ) {
539 if ( $this->is_metabox_request() ) {
540 return;
541 }
542
543 $post_type = $post->post_type;
544
545 if ( BNFW_Notification::POST_TYPE !== $post_type ) {
546 $this->send_notification_async( 'pending-' . $post_type, $post_id );
547 }
548 }
549
550 /**
551 * On Media Published.
552 *
553 * @param int $post_id Attachment post id.
554 */
555 public function new_publish_media_notification( $post_id ) {
556 $post_type = get_post_type( $post_id );
557
558 if ( BNFW_Notification::POST_TYPE !== $post_type && 'attachment' === $post_type ) {
559 $this->send_notification_async( 'new-media', $post_id );
560 }
561 }
562
563 /**
564 * On Media Attachment Data Update.
565 *
566 * @param int $post_id Attachment post id.
567 */
568 public function media_attachment_data_update_notification( $post_id ) {
569 $post_type = get_post_type( $post_id );
570 if ( BNFW_Notification::POST_TYPE !== $post_type && 'attachment' === $post_type ) {
571 $this->send_notification_async( 'update-media', $post_id );
572 }
573 }
574
575 /**
576 * Fires when a post is scheduled.
577 *
578 * @since 1.1.5
579 * @param int $post_id Post ID.
580 * @param object $post Post object.
581 */
582 public function on_post_scheduled( $post_id, $post ) {
583 // Rest request also triggers the same hook. We can ignore it.
584 if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
585 return;
586 }
587
588 $post_type = $post->post_type;
589
590 if ( BNFW_Notification::POST_TYPE !== $post_type ) {
591 $this->send_notification_async( 'future-' . $post_type, $post_id );
592 }
593 }
594
595 /**
596 * When the status of a comment is changed.
597 *
598 * @param string $new_status New status.
599 * @param string $old_status Old status.
600 * @param \WP_Comment $comment Comment.
601 */
602 public function on_comment_status_change( $new_status, $old_status, $comment ) {
603 if ( 'approved' !== $new_status ) {
604 return;
605 }
606
607 $post = get_post( $comment->comment_post_ID );
608
609 $notification_type = 'approve-' . $post->post_type . '-comment';
610
611 $this->send_notification( $notification_type, $comment->comment_ID, false );
612
613 // Send new comment notification after comment approve.
614 $notification_type = 'new-comment'; // old notification name.
615
616 if ( 'post' !== $post->post_type ) {
617 $notification_type = 'comment-' . $post->post_type;
618 }
619
620 $this->send_notification( $notification_type, $comment->comment_ID );
621
622 // Send comment reply notification after comment approve.
623 $this->comments_reply( $comment->comment_ID );
624 }
625
626 /**
627 * Send notification for new comments
628 *
629 * @since 1.0
630 * @param int $comment_id The comment ID.
631 */
632 public function comment_post( $comment_id ) {
633 $the_comment = get_comment( $comment_id );
634 $post = get_post( $the_comment->comment_post_ID );
635
636 if ( '1' !== $the_comment->comment_approved ) {
637 if ( $this->can_send_comment_notification( $the_comment ) ) {
638 $notification_type = 'moderate-' . $post->post_type . '-comment';
639 $this->send_notification( $notification_type, $comment_id );
640 }
641 } else {
642 $notification_type = 'new-comment'; // old notification name.
643
644 if ( 'post' !== $post->post_type ) {
645 $notification_type = 'comment-' . $post->post_type;
646 }
647
648 $this->send_notification( $notification_type, $comment_id );
649
650 // comment reply notification.
651 $this->comments_reply( $comment_id );
652 }
653 }
654
655 /**
656 * Send notification for comments reply
657 *
658 * @since 1.0
659 * @param int $comment_id The comment ID.
660 */
661 public function comments_reply( $comment_id ) {
662 $the_comment = get_comment( $comment_id );
663 $post = get_post( $the_comment->comment_post_ID );
664
665 // comment reply notification.
666 if ( $this->can_send_comment_notification( $the_comment ) ) {
667 if ( $the_comment->comment_parent > 0 ) {
668 $notification_type = 'reply-comment'; // old notification name.
669 if ( 'post' !== $post->post_type ) {
670 $notification_type = 'commentreply-' . $post->post_type;
671 }
672 $notifications = $this->notifier->get_notifications( $notification_type );
673 if ( count( $notifications ) > 0 ) {
674 $parent = get_comment( $the_comment->comment_parent );
675 if ( $parent->comment_author_email !== $the_comment->comment_author_email ) {
676 foreach ( $notifications as $notification ) {
677 $this->engine->send_comment_reply_email( $this->notifier->read_settings( $notification->ID ), $the_comment, $parent );
678 }
679 }
680 }
681 }
682 }
683 }
684
685 /**
686 * Send notification for new trackback.
687 *
688 * @since 1.0
689 * @param int $comment_id Trackback ID.
690 */
691 public function trackback_post( $comment_id ) {
692 $the_comment = get_comment( $comment_id );
693 if ( $this->can_send_comment_notification( $the_comment ) ) {
694 $this->send_notification( 'new-trackback', $comment_id );
695 }
696 }
697
698 /**
699 * Send notification for new pingbacks
700 *
701 * @since 1.0
702 * @param int $comment_id Comment ID.
703 */
704 public function pingback_post( $comment_id ) {
705 $the_comment = get_comment( $comment_id );
706 if ( $this->can_send_comment_notification( $the_comment ) ) {
707 $this->send_notification( 'new-pingback', $comment_id );
708 }
709 }
710
711 /**
712 * Send notification for lost password.
713 *
714 * @since 1.0
715 */
716 public function on_lost_password() {
717 $user_login = ! empty( $_POST['user_login'] ) ? sanitize_text_field( wp_unslash( $_POST['user_login'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
718 $user = get_user_by( 'login', $user_login );
719 if ( false === $user ) {
720 $user = get_user_by( 'email', $user_login );
721 }
722 if ( $user ) {
723 $this->send_notification( 'admin-password', $user->ID );
724 }
725 }
726
727 /**
728 * Change the title of the password reset email that is sent to the user.
729 *
730 * @since 1.1
731 *
732 * @param string $title Email subject.
733 * @param string $user_login The username for the user.
734 * @param string $user_data WP_User object.
735 *
736 * @return string
737 */
738 public function change_password_email_title( $title, $user_login = '', $user_data = '' ) {
739 $notifications = $this->notifier->get_notifications( 'user-password' );
740 if ( count( $notifications ) > 0 ) {
741 // Ideally there should be only one notification for this type.
742 // If there are multiple notification then we will read data about only the last one.
743 $setting = $this->notifier->read_settings( end( $notifications )->ID );
744
745 if ( '' === $user_data ) {
746 return $this->engine->handle_shortcodes( $setting['subject'], 'user-password', $user_data->ID );
747 } else {
748 return $this->engine->handle_shortcodes( $setting['subject'], 'user-password', $user_data->ID );
749 }
750 }
751
752 return $title;
753 }
754
755 /**
756 * Change the message of the password reset email.
757 *
758 * @since 1.1
759 *
760 * @param string $message Email message.
761 * @param string $key The activation key.
762 * @param string $user_login The username for the user.
763 * @param string $user_data WP_User object.
764 *
765 * @return string
766 */
767 public function change_password_email_message( $message, $key, $user_login = '', $user_data = '' ) {
768 $notifications = $this->notifier->get_notifications( 'user-password' );
769 if ( count( $notifications ) > 0 ) {
770 // Ideally there should be only one notification for this type.
771 // If there are multiple notification then we will read data about only the last one.
772 $setting = $this->notifier->read_settings( end( $notifications )->ID );
773
774 $message = $this->engine->handle_password_reset_shortcodes( $setting, $key, $user_login, $user_data );
775
776 if ( 'html' === $setting['email-formatting'] ) {
777 add_filter( 'wp_mail_content_type', array( $this, 'set_html_content_type' ) );
778 if ( 'true' !== $setting['disable-autop'] ) {
779 $message = wpautop( $message );
780 }
781 } else {
782 add_filter( 'wp_mail_content_type', array( $this, 'set_text_content_type' ) );
783 if ( 'text' === $setting['email-formatting'] ) {
784 $message = wp_strip_all_tags( $message );
785 }
786 }
787 } else {
788 if ( $this->notifier->notification_exists( 'user-password', false ) ) {
789 // disabled notification exists, so disable the email by returning empty string.
790 return '';
791 }
792 }
793
794 return $message;
795 }
796
797 /**
798 * On Password reset.
799 *
800 * @param WP_User $user User who's password was changed.
801 */
802 public function on_password_reset( $user ) {
803 $notifications = $this->notifier->get_notifications( 'password-changed' );
804 foreach ( $notifications as $notification ) {
805 $this->engine->send_password_changed_email( $this->notifier->read_settings( $notification->ID ), $user );
806 }
807 }
808
809 /**
810 * Should the password changed email be sent?
811 *
812 * @param bool $send Whether to send the email.
813 * @param array $user The original user array.
814 * @param array $userdata The updated user array.
815 *
816 * @return bool
817 */
818 public function should_password_changed_email_be_sent( $send, $user, $userdata ) {
819 $bnfw = self::factory();
820
821 if ( ! $send ) {
822 return $send;
823 }
824
825 return ! $bnfw->notifier->is_notification_disabled( 'password-changed' );
826 }
827
828 /**
829 * On Password Changed.
830 *
831 * @since 1.6
832 *
833 * @param array $email_data Email Data.
834 * @param array $user User data.
835 *
836 * @return array Modified Email Data
837 */
838 public function on_password_changed( $email_data, $user ) {
839 return $this->handle_filtered_data_notification( 'password-changed', $email_data, $user['ID'] );
840 }
841
842 /**
843 * Should the email changed email be sent?
844 *
845 * @param bool $send Whether to send the email.
846 * @param array $user_old_data The original user array.
847 * @param array $user_new_data The updated user array.
848 *
849 * @return bool
850 */
851 public function should_email_changed_email_be_sent( $send, $user_old_data, $user_new_data ) {
852 $bnfw = self::factory();
853
854 if ( $bnfw->notifier->notification_exists( 'admin-email-changed', false ) ) {
855 $notifications = $bnfw->notifier->get_notifications( 'admin-email-changed' );
856
857 if ( count( $notifications ) > 0 ) {
858 // Ideally there should be only one notification for this type.
859 // If there are multiple notification then we will read data about only the last one.
860 $setting = $bnfw->notifier->read_settings( end( $notifications )->ID );
861 $notification_disabled = apply_filters( 'bnfw_notification_disabled', ( 'true' === $setting['disabled'] ), $id, $setting );
862
863 if ( ! $notification_disabled ) {
864
865 $setting['message'] = str_replace( '[user_old_email]', $user_old_data['user_email'], $setting['message'] );
866 $setting['message'] = str_replace( '[user_new_email]', $user_new_data['user_email'], $setting['message'] );
867 $bnfw->engine->send_notification( $setting, $user_old_data['ID'] );
868 }
869 }
870 }
871
872 if ( ! $send ) {
873 return $send;
874 }
875
876 return ! $bnfw->notifier->is_notification_disabled( 'email-changed' );
877 }
878
879 /**
880 * On Email Changed.
881 *
882 * @since 1.6
883 *
884 * @param array $email_data Email Data.
885 * @param array $user_old_data The original user array.
886 * @param array $user_new_data The updated user array.
887 *
888 * @return array Modified Email Data
889 */
890 public function on_email_changed( $email_data, $user_old_data, $user_new_data ) {
891
892 $email = $this->handle_filtered_data_notification( 'email-changed', $email_data, $user_old_data['ID'] );
893 $user = get_user_by( 'ID', $user_old_data['ID'] );
894 $email['message'] = str_replace( '[user_nicename]', $user->user_login, $email['message'] );
895 $email['message'] = str_replace( '[user_old_email]', $user_old_data['user_email'], $email['message'] );
896 $email['message'] = str_replace( '[user_new_email]', $user_new_data['user_email'], $email['message'] );
897 $email_data['message'] = $email['message'];
898
899 $notifications = $this->notifier->get_notifications( 'email-changed' );
900 if ( count( $notifications ) > 0 ) {
901 $setting = $this->notifier->read_settings( end( $notifications )->ID );
902 // Ensures the content type of the message.
903 if ( 'html' === $setting['email-formatting'] ) {
904 $email_data['message'] = wpautop( $email_data['message'] );
905 }
906 }
907
908 return $email_data;
909 }
910 /**
911 * Filters the text of the email sent when a change of user email address is attempted.
912 *
913 * @param string $email_text Text in the email.
914 * @param array $new_user_details {
915 * Data relating to the new user email address.
916 *
917 * @type string $hash The secure hash used in the confirmation link URL.
918 * @type string $newemail The proposed new email address.
919 * }
920 */
921 public function on_email_changing( $email_text, $new_user_details ) {
922 $notification_name = 'email-changing';
923
924 $notifications = $this->notifier->get_notifications( $notification_name );
925 if ( count( $notifications ) > 0 ) {
926
927 $user = wp_get_current_user();
928
929 // Ideally there should be only one notification for this type.
930 // If there are multiple notification then we will read data about only the last one.
931 $setting = $this->notifier->read_settings( end( $notifications )->ID );
932
933 // Ensures the content type of the message.
934 if ( 'html' === $setting['email-formatting'] ) {
935 add_filter( 'wp_mail_content_type', array( $this, 'set_html_content_type' ) );
936 } else {
937 add_filter( 'wp_mail_content_type', array( $this, 'set_text_content_type' ) );
938 }
939
940 $email_text = $this->engine->handle_shortcodes( $setting['message'], $setting['notification'], $new_user_details['newemail'] );
941 $email_text = $this->engine->handle_global_user_shortcodes( $email_text, $new_user_details['newemail'] );
942 $email_text = str_replace( '[email_change_confirmation_link]', esc_url( admin_url( 'profile.php?newuseremail=' . $new_user_details['hash'] ) ), $email_text );
943 $email_text = str_replace( '[user_nicename]', $user->data->user_nicename, $email_text );
944 $email_text = str_replace( '[user_id]', $user->data->ID, $email_text );
945 $email_text = str_replace( '[user_login]', $user->data->user_login, $email_text );
946 $email_text = str_replace( '[user_email]', $user->data->user_email, $email_text );
947 $email_text = str_replace( '[user_url]', $user->data->user_url, $email_text );
948 $email_text = str_replace( '[user_registered]', $user->data->user_registered, $email_text );
949 $email_text = str_replace( '[user_display_name]', $user->data->display_name, $email_text );
950 $email_text = str_replace( '[user_firstname]', get_user_meta( $user->data->ID, 'first_name', true ), $email_text );
951 $email_text = str_replace( '[user_lastname]', get_user_meta( $user->data->ID, 'last_name', true ), $email_text );
952 $email_text = str_replace( '[user_nickname]', get_user_meta( $user->data->ID, 'nickname', true ), $email_text );
953 $email_text = str_replace( '[user_description]', get_user_meta( $user->data->ID, 'description', true ), $email_text );
954 $email_text = str_replace( '[user_wp_capabilities]', implode( ', ', $user->roles ), $email_text );
955 $email_text = str_replace( '[user_avatar]', get_avatar( $user->data->ID ), $email_text );
956 $email_text = str_replace( '[nickname]', $user->nickname, $email_text );
957 $email_text = str_replace( '[display_name]', $user->display_name, $email_text );
958 $email_text = str_replace( '[email_user_id]', $user->data->ID, $email_text );
959 $email_text = str_replace( '[email_user_login]', $user->data->user_login, $email_text );
960 $email_text = str_replace( '[email_user_nicename]', $user->data->user_nicename, $email_text );
961 $email_text = str_replace( '[email_user_email]', $user->data->user_email, $email_text );
962 $email_text = str_replace( '[email_user_url]', $user->data->user_url, $email_text );
963 $email_text = str_replace( '[email_user_registered]', $user->data->user_registered, $email_text );
964 $email_text = str_replace( '[email_user_display_name]', $user->data->display_name, $email_text );
965 $email_text = str_replace( '[email_user_firstname]', get_user_meta( $user->data->ID, 'first_name', true ), $email_text );
966 $email_text = str_replace( '[email_user_lastname]', get_user_meta( $user->data->ID, 'last_name', true ), $email_text );
967 $email_text = str_replace( '[email_user_nickname]', get_user_meta( $user->data->ID, 'nickname', true ), $email_text );
968 $email_text = str_replace( '[email_user_description]', get_user_meta( $user->data->ID, 'description', true ), $email_text );
969 $email_text = str_replace( '[email_user_wp_capabilities]', implode( ', ', $user->roles ), $email_text );
970 $email_text = str_replace( '[email_user_avatar]', get_avatar( $user->data->ID ), $email_text );
971 $email_text = str_replace( '[user_old_email]', $user->data->user_email, $email_text );
972 $email_text = str_replace( '[user_new_email]', $new_user_details['newemail'], $email_text );
973 $email_text = str_replace( '[user_role]', implode( ',', $user->roles ), $email_text );
974
975 $user_capabilities = bnfw_format_user_capabilities( $user->wp_capabilities );
976 if ( ! empty( $user_capabilities ) ) {
977 $email_text = str_replace( '[wp_capabilities]', $user_capabilities, $email_text );
978 }
979
980 if ( 'html' === $setting['email-formatting'] ) {
981 $email_text = wpautop( $email_text );
982 }
983 }
984
985 return $email_text;
986 }
987
988 /**
989 * Send notification on core updated event.
990 *
991 * @since 1.6
992 *
993 * @param array $email_data Email Data.
994 * @param string $type The type of email being sent. Can be one of
995 * 'success', 'fail', 'manual', 'critical'.
996 * @param object $core_update The update offer that was attempted.
997 * @param mixed $result The result for the core update. Can be WP_Error.
998 *
999 * @return array Modified Email Data.
1000 */
1001 public function on_core_updated( $email_data, $type, $core_update, $result ) {
1002 $notifications = $this->notifier->get_notifications( 'core-updated' );
1003 if ( count( $notifications ) > 0 ) {
1004 // Ideally there should be only one notification for this type.
1005 // If there are multiple notification then we will read data about only the last one.
1006 $setting = $this->notifier->read_settings( end( $notifications )->ID );
1007
1008 $email_data = $this->engine->handle_core_updated_notification( $email_data, $setting, $type );
1009 }
1010
1011 return $email_data;
1012 }
1013
1014 /**
1015 * Process User update notifications.
1016 *
1017 * @since 1.6
1018 *
1019 * @param string $notification_name Notification Name.
1020 * @param array $email_data Email Data.
1021 * @param string|int $extra_data User Id.
1022 *
1023 * @return array Modified Email Data.
1024 */
1025 private function handle_filtered_data_notification( $notification_name, $email_data, $extra_data ) {
1026 $notifications = $this->notifier->get_notifications( $notification_name );
1027 if ( count( $notifications ) > 0 ) {
1028 // Ideally there should be only one notification for this type.
1029 // If there are multiple notification then we will read data about only the last one.
1030 $setting = $this->notifier->read_settings( end( $notifications )->ID );
1031
1032 // Ensures the content type of the message.
1033 if ( 'html' === $setting['email-formatting'] ) {
1034 add_filter( 'wp_mail_content_type', array( $this, 'set_html_content_type' ) );
1035 } else {
1036 add_filter( 'wp_mail_content_type', array( $this, 'set_text_content_type' ) );
1037 }
1038
1039 $email_data = $this->engine->handle_filtered_data_notification( $email_data, $setting, $extra_data );
1040 }
1041
1042 return $email_data;
1043 }
1044
1045 /**
1046 * Set the email formatting to HTML.
1047 *
1048 * @since 1.4
1049 */
1050 public function set_html_content_type() {
1051 return 'text/html';
1052 }
1053
1054 /**
1055 * Set the email formatting to text.
1056 *
1057 * @since 1.4
1058 */
1059 public function set_text_content_type() {
1060 return 'text/plain';
1061 }
1062
1063 /**
1064 * Send notification for new users.
1065 *
1066 * @since 1.0
1067 * @param int $user_id User ID.
1068 */
1069 public function user_register( $user_id ) {
1070 $this->send_notification( 'admin-user', $user_id );
1071 }
1072
1073 /**
1074 * Send notification for user when user login.
1075 *
1076 * @since 1.0
1077 * @param string $user_name Username.
1078 * @param object $user_data User object.
1079 */
1080 public function user_login( $user_name, $user_data ) {
1081 $user_id = $user_data->ID;
1082 $notifications = $this->notifier->get_notifications( 'user-login' );
1083
1084 foreach ( $notifications as $notification ) {
1085 $this->engine->send_user_login_email( $this->notifier->read_settings( $notification->ID ), get_userdata( $user_id ) );
1086 }
1087
1088 $this->user_login_admin_notification( $user_id );
1089 }
1090
1091 /**
1092 * Send notification for admin when user login.
1093 *
1094 * @since 1.0
1095 * @param int $user_id User ID.
1096 */
1097 public function user_login_admin_notification( $user_id ) {
1098 $notifications = $this->notifier->get_notifications( 'admin-user-login' );
1099
1100 foreach ( $notifications as $notification ) {
1101 $this->engine->send_user_login_email_for_admin( $this->notifier->read_settings( $notification->ID ), get_userdata( $user_id ) );
1102 }
1103 }
1104
1105 /**
1106 * Send notification about new users to site admin.
1107 *
1108 * @since 1.7.1
1109 *
1110 * @param array $email_data Email details.
1111 * @param WP_User $user User object.
1112 * @param string $blogname Blog name.
1113 *
1114 * @return array Modified email details.
1115 */
1116 public function handle_user_registered_admin_email( $email_data, $user, $blogname ) {
1117 return $this->handle_filtered_data_notification( 'admin-user', $email_data, $user->ID );
1118 }
1119
1120 /**
1121 * New User - Post-registration Email
1122 *
1123 * @since 1.1
1124 * @param int $user_id New user id.
1125 */
1126 public function welcome_email( $user_id ) {
1127 $notifications = $this->notifier->get_notifications( 'welcome-email' );
1128 foreach ( $notifications as $notification ) {
1129 $this->engine->send_registration_email( $this->notifier->read_settings( $notification->ID ), get_userdata( $user_id ) );
1130 }
1131 }
1132
1133 /**
1134 * Send notification when a user role changes.
1135 *
1136 * @since 1.3.9
1137 *
1138 * @param int $user_id User ID.
1139 * @param string $new_role New User role.
1140 * @param array $old_roles Old User role.
1141 */
1142 public function user_role_changed( $user_id, $new_role, $old_roles ) {
1143 if ( ! empty( $old_roles ) ) {
1144 $notifications = $this->notifier->get_notifications( 'user-role' );
1145 foreach ( $notifications as $notification ) {
1146
1147 /**
1148 * Trigger User Role Changed - For User notification.
1149 *
1150 * @since 1.6.5
1151 */
1152 if ( apply_filters( 'bnfw_trigger_user-role_notification', true, $notification, $new_role, $old_roles ) ) {
1153 $this->engine->send_user_role_changed_email( $this->notifier->read_settings( $notification->ID ), $user_id, $old_roles[0], $new_role );
1154 }
1155 }
1156 $notifications = $this->notifier->get_notifications( 'admin-role' );
1157 foreach ( $notifications as $notification ) {
1158
1159 /**
1160 * Trigger User Role Changed - For User notification.
1161 *
1162 * @since 1.6.5
1163 */
1164 if ( apply_filters( 'bnfw_trigger_admin-role_notification', true, $notification, $new_role, $old_roles ) ) {
1165 $setting = $this->notifier->read_settings( $notification->ID );
1166 $setting['message'] = $this->engine->handle_user_role_shortcodes( $setting['message'], $old_roles[0], $new_role );
1167 $setting['subject'] = $this->engine->handle_user_role_shortcodes( $setting['subject'], $old_roles[0], $new_role );
1168
1169 $this->engine->send_notification( $setting, $user_id );
1170 }
1171 }
1172 }
1173 }
1174
1175 /**
1176 * Send notification when a user role added through Members Plugin.
1177 *
1178 * @since 1.8.4
1179 *
1180 * @param int $user_id User ID.
1181 * @param string $new_role New User role.
1182 */
1183 public function user_role_added_from_member_plugin( $user_id, $new_role ) {
1184
1185 global $pagenow;
1186
1187 if ( 'users.php' !== $pagenow ) {
1188 return;
1189 }
1190 if ( ! $user_id ) {
1191 return;
1192 }
1193
1194 $notifications = $this->notifier->get_notifications( 'user-role' );
1195
1196 foreach ( $notifications as $notification ) {
1197 if ( apply_filters( 'bnfw_trigger_user-role_notification', true, $notification, $new_role, null ) ) {
1198 $this->engine->send_user_role_changed_email( $this->notifier->read_settings( $notification->ID ), $user_id, null, $new_role );
1199 }
1200 }
1201
1202 $notifications_admin = $this->notifier->get_notifications( 'admin-role' );
1203 foreach ( $notifications_admin as $notification ) {
1204 if ( apply_filters( 'bnfw_trigger_admin-role_notification', true, $notification, $new_role, null ) ) {
1205 $setting = $this->notifier->read_settings( $notification->ID );
1206 $setting['message'] = $this->engine->handle_user_role_shortcodes( $setting['message'], null, $new_role );
1207 $setting['subject'] = $this->engine->handle_user_role_shortcodes( $setting['subject'], null, $new_role );
1208
1209 $this->engine->send_notification( $setting, $user_id );
1210 }
1211 }
1212
1213 }
1214
1215 /**
1216 * Send notification when a user role removed through Members Plugin.
1217 *
1218 * @since 1.8.4
1219 *
1220 * @param int $user_id User ID.
1221 * @param string $old_role New User role.
1222 */
1223 public function user_role_removed_from_member_plugin( $user_id, $old_role ) {
1224 global $pagenow;
1225
1226 if ( 'users.php' !== $pagenow ) {
1227 return;
1228 }
1229 if ( ! $user_id ) {
1230 return;
1231 }
1232
1233 $notifications = $this->notifier->get_notifications( 'user-role' );
1234
1235 foreach ( $notifications as $notification ) {
1236 if ( apply_filters( 'bnfw_trigger_user-role_notification', true, $notification, null, array( $old_role ) ) ) {
1237 $this->engine->send_user_role_changed_email( $this->notifier->read_settings( $notification->ID ), $user_id, $old_role, null );
1238 }
1239 }
1240
1241 $notifications_admin = $this->notifier->get_notifications( 'admin-role' );
1242 foreach ( $notifications_admin as $notification ) {
1243 if ( apply_filters( 'bnfw_trigger_admin-role_notification', true, $notification, null, array( $old_role ) ) ) {
1244 $setting = $this->notifier->read_settings( $notification->ID );
1245 $setting['message'] = $this->engine->handle_user_role_shortcodes( $setting['message'], $old_role, null );
1246 $setting['subject'] = $this->engine->handle_user_role_shortcodes( $setting['subject'], $old_role, null );
1247
1248 $this->engine->send_notification( $setting, $user_id );
1249 }
1250 }
1251
1252 }
1253
1254 /**
1255 * Send notification when a user role added support User Role Editor by Members Plugin.
1256 *
1257 * @since 1.3.9
1258 *
1259 * @param int $user_id User ID.
1260 * @param array $old_user_data Old User role.
1261 */
1262 public function user_role_added( $user_id, $old_user_data ) {
1263
1264 if ( isset( $_POST['members_user_roles'] ) && ! empty( $_POST['members_user_roles'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
1265 // Get the current user roles.
1266 $old_roles = (array) $old_user_data->roles;
1267
1268 // Sanitize the posted roles.
1269 $new_roles = array_map( array( $this, 'bnfw_members_sanitize_role' ), $_POST['members_user_roles'] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
1270
1271 sort( $old_roles );
1272 sort( $new_roles );
1273 $old_roles_str = implode( '', $old_roles );
1274 $new_roles_str = implode( '', $new_roles );
1275 if ( ! empty( $old_roles ) && $old_roles_str !== $new_roles_str ) {
1276 $notifications = $this->notifier->get_notifications( 'user-role' );
1277 foreach ( $notifications as $notification ) {
1278
1279 /**
1280 * Trigger User Role Changed - For User notification.
1281 *
1282 * @since 1.6.5
1283 */
1284 if ( apply_filters( 'bnfw_trigger_user-role-added_notification', true, $notification, $new_roles, $old_roles ) ) {
1285 $this->engine->send_user_role_added_email( $this->notifier->read_settings( $notification->ID ), $user_id, $old_roles, $new_roles );
1286 }
1287 }
1288
1289 $notifications = $this->notifier->get_notifications( 'admin-role' );
1290 foreach ( $notifications as $notification ) {
1291
1292 /**
1293 * Trigger User Role Changed - For User notification.
1294 *
1295 * @since 1.6.5
1296 */
1297 if ( apply_filters( 'bnfw_trigger_user-role-added_notification', true, $notification, $new_roles, $old_roles ) ) {
1298 $setting = $this->notifier->read_settings( $notification->ID );
1299 $setting['message'] = $this->engine->handle_user_added_role_shortcodes( $setting['message'], $old_roles, $new_roles );
1300 $setting['subject'] = $this->engine->handle_user_added_role_shortcodes( $setting['subject'], $old_roles, $new_roles );
1301
1302 $this->engine->send_notification( $setting, $user_id );
1303 }
1304 }
1305 }
1306 }
1307 }
1308
1309 /**
1310 * Sanitizes a role name. This is a wrapper for the `sanitize_key()` WordPress function. Only
1311 * alphanumeric characters and underscores are allowed. Hyphens are also replaced with underscores.
1312 *
1313 * @since 1.0.0
1314 * @access public
1315 * @param array $role Posted user role.
1316 * @return int
1317 */
1318 public function bnfw_members_sanitize_role( $role ) {
1319
1320 $_role = strtolower( $role );
1321 $_role = preg_replace( '/[^a-z0-9_\-\s]/', '', $_role );
1322
1323 return apply_filters( 'bnfw_members_sanitize_role', str_replace( ' ', '_', $_role ), $role );
1324 }
1325
1326 /**
1327 * Send notification based on type and ref id.
1328 *
1329 * @since 1.0
1330 * @param string $type Notification type.
1331 * @param mixed $ref_id Reference data.
1332 * @param bool $include_disabled Include disabled.
1333 */
1334 public function send_notification( $type, $ref_id, $include_disabled = true ) {
1335 $notifications = $this->notifier->get_notifications( $type, $include_disabled );
1336 foreach ( $notifications as $notification ) {
1337 $this->engine->send_notification( $this->notifier->read_settings( $notification->ID ), $ref_id );
1338 }
1339 }
1340
1341 /**
1342 * Send notification async based on type and ref id.
1343 *
1344 * @param string $type Notification type.
1345 * @param mixed $ref_id Reference data.
1346 */
1347 public function send_notification_async( $type, $ref_id ) {
1348 $notifications = $this->notifier->get_notifications( $type, false );
1349 foreach ( $notifications as $notification ) {
1350 $transient = get_transient( 'bnfw-async-notifications' );
1351 if ( ! is_array( $transient ) ) {
1352 $transient = array();
1353 }
1354
1355 $notification_data = array(
1356 'ref_id' => $ref_id,
1357 'notification_id' => $notification->ID,
1358 'notification_type' => $type,
1359 );
1360
1361 if ( ! in_array( $notification_data, $transient, true ) ) {
1362 $transient[] = $notification_data;
1363 set_transient( 'bnfw-async-notifications', $transient, 600 );
1364 }
1365 }
1366 }
1367
1368 /**
1369 * Can send comment notification or not.
1370 *
1371 * @since 1.0
1372 * @param WP_Comment $comment Comment data.
1373 * @return bool
1374 */
1375 private function can_send_comment_notification( $comment ) {
1376 // Returns false if the comment is marked as spam AND admin has enabled suppression of spam.
1377 $suppress_spam = get_option( 'bnfw_suppress_spam' );
1378 if ( '1' === $suppress_spam && ( 0 === strcmp( $comment->comment_approved, 'spam' ) ) ) {
1379 return false;
1380 }
1381 return true;
1382 }
1383
1384 /**
1385 * Handle user request email content.
1386 *
1387 * @param string $content Content.
1388 * @param array $email_data Email data.
1389 *
1390 * @return string Modified content.
1391 */
1392 public function handle_user_request_email_content( $content, $email_data ) {
1393 $field = 'message';
1394 $new_content = '';
1395
1396 switch ( $email_data['description'] ) {
1397 case 'Export Personal Data':
1398 $notification_name = 'ca-export-data';
1399 $new_content = $this->handle_user_request_notification( $notification_name, $field, $email_data );
1400 break;
1401 case 'Erase Personal Data':
1402 $notification_name = 'ca-erase-data';
1403 $new_content = $this->handle_user_request_notification( $notification_name, $field, $email_data );
1404 break;
1405 }
1406
1407 if ( ! empty( $new_content ) ) {
1408 return $new_content;
1409 } else {
1410 return $content;
1411 }
1412 }
1413
1414 /**
1415 * Handle user request email subject.
1416 *
1417 * @param string $subject Subject.
1418 * @param string $blogname Blog name.
1419 * @param array $email_data Email data.
1420 *
1421 * @return string Modified subject.
1422 */
1423 public function handle_user_request_email_subject( $subject, $blogname, $email_data ) {
1424 $field = 'subject';
1425 $new_subject = '';
1426
1427 switch ( $email_data['description'] ) {
1428 case 'Export Personal Data':
1429 $notification_name = 'ca-export-data';
1430 $new_subject = $this->handle_user_request_notification( $notification_name, $field, $email_data );
1431 break;
1432 case 'Erase Personal Data':
1433 $notification_name = 'ca-erase-data';
1434 $new_subject = $this->handle_user_request_notification( $notification_name, $field, $email_data );
1435 break;
1436 }
1437 if ( ! empty( $new_subject ) ) {
1438 return $new_subject;
1439 } else {
1440 return $subject;
1441 }
1442 }
1443
1444 /**
1445 * Handle user confirmed action email content.
1446 *
1447 * @param string $content Content.
1448 * @param array $email_data Email data.
1449 *
1450 * @return string Modified content.
1451 */
1452 public function handle_user_confirmed_action_email_content( $content, $email_data ) {
1453 $field = 'message';
1454 $new_content = '';
1455
1456 switch ( $email_data['description'] ) {
1457 case 'Export Personal Data':
1458 $notification_name = 'uc-export-data';
1459 $new_content = $this->handle_user_confirmed_action_notification( $notification_name, $field, $email_data );
1460 break;
1461 case 'Erase Personal Data':
1462 $notification_name = 'uc-erase-data';
1463 $new_content = $this->handle_user_confirmed_action_notification( $notification_name, $field, $email_data );
1464 break;
1465 }
1466
1467 if ( ! empty( $new_content ) ) {
1468 return $new_content;
1469 } else {
1470 return $content;
1471 }
1472 }
1473
1474 /**
1475 * Handle data exported email content.
1476 *
1477 * @param string $content Text in the email.
1478 * @param int $request_id The request ID for this personal data export.
1479 * @param array $email_data Data relating to the account action email.
1480 *
1481 * @return string Modified content.
1482 */
1483 public function handle_data_export_email_content( $content, $request_id, $email_data ) {
1484
1485 $field = 'message';
1486 $notification_name = 'data-export';
1487 $new_content = '';
1488
1489 $notifications = $this->notifier->get_notifications( $notification_name );
1490 if ( count( $notifications ) > 0 ) {
1491 // Ideally there should be only one notification for this type.
1492 // If there are multiple notification then we will read data about only the last one.
1493 $setting = $this->notifier->read_settings( end( $notifications )->ID );
1494
1495 $new_content = $this->engine->handle_data_export_email_shortcodes( $setting[ $field ], $setting, $request_id );
1496 $new_content = $this->engine->handle_global_user_shortcodes( $new_content, $email_data['message_recipient'] );
1497 }
1498
1499 if ( ! empty( $new_content ) ) {
1500 return $new_content;
1501 } else {
1502 return $content;
1503 }
1504 }
1505 /**
1506 * Filters the subject of the email sent when an erasure request is completed.
1507 *
1508 * @param string $subject The email subject.
1509 * @param string $sitename The name of the site.
1510 * @param array $email_data Data relating to the account action email.
1511 */
1512 public function handle_erasure_complete_email_subject( $subject, $sitename, $email_data ) {
1513 return $this->handle_erasure_complete_email_notification( 'subject', $subject, $email_data );
1514 }
1515 /**
1516 * Filters the body of the data erasure fulfillment notification.
1517 *
1518 * @param string $content The email content.
1519 * @param array $email_data Data relating to the account action email.
1520 */
1521 public function handle_erasure_complete_email_content( $content, $email_data ) {
1522 if ( isset( $email_data['privacy_policy_url'] ) ) {
1523 return $this->handle_erasure_complete_email_notification( 'message', $content, $email_data );
1524 }
1525
1526 return $content;
1527 }
1528 /**
1529 * Handles erasure fulfillment notification.
1530 *
1531 * @param string $field Field name.
1532 * @param string $content The email content.
1533 * @param array $email_data Data relating to the account action email.
1534 */
1535 protected function handle_erasure_complete_email_notification( $field, $content, $email_data ) {
1536 $notification_name = 'data-erased';
1537 $new_content = '';
1538 $notifications = $this->notifier->get_notifications( $notification_name );
1539 if ( count( $notifications ) > 0 ) {
1540 // Ideally there should be only one notification for this type.
1541 // If there are multiple notification then we will read data about only the last one.
1542 $setting = $this->notifier->read_settings( end( $notifications )->ID );
1543 $new_content = $this->engine->handle_shortcodes( $setting[ $field ], $notification_name, $email_data );
1544 }
1545 if ( ! empty( $new_content ) ) {
1546 return $new_content;
1547 } else {
1548 return $content;
1549 }
1550 }
1551
1552 /**
1553 * Send notification emails on shutdown.
1554 */
1555 public function on_shutdown() {
1556 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
1557 return;
1558 }
1559
1560 $transient = get_transient( 'bnfw-async-notifications' );
1561 if ( is_array( $transient ) ) {
1562 delete_transient( 'bnfw-async-notifications' );
1563 foreach ( $transient as $id_pairs ) {
1564 $this->engine->send_notification( $this->notifier->read_settings( $id_pairs['notification_id'] ), $id_pairs['ref_id'] );
1565 }
1566 }
1567 }
1568
1569 /**
1570 * Handle user request notification.
1571 *
1572 * @param string $notification_name Notification name.
1573 * @param string $field Field name.
1574 * @param array $email_data Email data.
1575 *
1576 * @return string Content.
1577 */
1578 protected function handle_user_request_notification( $notification_name, $field, $email_data ) {
1579 $notifications = $this->notifier->get_notifications( $notification_name );
1580 if ( count( $notifications ) > 0 ) {
1581 // Ideally there should be only one notification for this type.
1582 // If there are multiple notification then we will read data about only the last one.
1583 $setting = $this->notifier->read_settings( end( $notifications )->ID );
1584
1585 return $this->engine->handle_user_request_email_shortcodes( $setting[ $field ], $setting, $email_data );
1586 }
1587
1588 return '';
1589 }
1590
1591 /**
1592 * Handle user confirmed action notification.
1593 *
1594 * @param string $notification_name Notification name.
1595 * @param string $field Field name.
1596 * @param array $email_data Email data.
1597 *
1598 * @return string Content.
1599 */
1600 protected function handle_user_confirmed_action_notification( $notification_name, $field, $email_data ) {
1601 $notifications = $this->notifier->get_notifications( $notification_name );
1602 if ( count( $notifications ) > 0 ) {
1603 // Ideally there should be only one notification for this type.
1604 // If there are multiple notification then we will read data about only the last one.
1605 $setting = $this->notifier->read_settings( end( $notifications )->ID );
1606
1607 return $this->engine->handle_user_confirmed_action_email_shortcodes( $setting[ $field ], $setting, $email_data );
1608 }
1609
1610 return '';
1611 }
1612
1613 /**
1614 * Is this a metabox request?
1615 *
1616 * Block editor sends duplicate requests on post update.
1617 *
1618 * @return bool True if metabox request, False otherwise.
1619 */
1620 protected function is_metabox_request() {
1621 return ( isset( $_GET['meta-box-loader'] ) || isset( $_GET['meta_box'] ) );
1622 }
1623
1624 /**
1625 * Check if Gutenberg is active.
1626 *
1627 * @return bool
1628 * @since 1.3
1629 */
1630 public function is_gutenberg_active() {
1631 $gutenberg = false;
1632 $block_editor = false;
1633
1634 if ( has_filter( 'replace_editor', 'gutenberg_init' ) ) {
1635 // Gutenberg is installed and activated.
1636 $gutenberg = true;
1637 }
1638
1639 if ( version_compare( $GLOBALS['wp_version'], '5.0-beta', '>' ) ) {
1640 // Block editor.
1641 $block_editor = true;
1642 }
1643
1644 if ( ! $gutenberg && ! $block_editor ) {
1645 return false;
1646 }
1647
1648 include_once ABSPATH . 'wp-admin/includes/plugin.php';
1649
1650 if ( ! is_plugin_active( 'classic-editor/classic-editor.php' ) ) {
1651 return true;
1652 }
1653
1654 $use_block_editor = ( get_option( 'classic-editor-replace' ) === 'no-replace' );
1655
1656 return $use_block_editor;
1657 }
1658
1659 }
1660 /**
1661 * Fire up the plugin.
1662 */
1663 BNFW::factory();
1664 }
1665