PluginProbe ʕ •ᴥ•ʔ
Check & Log Email – Easy Email Testing & Mail logging / trunk
Check & Log Email – Easy Email Testing & Mail logging vtrunk
1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 2.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.13.1 2.0.13.2 2.0.14 2.0.2 2.0.3 2.0.4 2.0.5 2.0.5.1 2.0.6 2.0.7 2.0.8 2.0.9 trunk 0.5.7 0.6.0 0.6.1 0.6.2 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.12.1 1.0.13 1.0.13.1 1.0.2 1.0.3
check-email / include / Check_Email_Notify_Tab.php
check-email / include Last commit date
Core 1 month ago Util 1 month ago Check_Email_Encode_Tab.php 1 month ago Check_Email_Notify_Tab.php 1 month ago Check_Email_SMTP_Tab.php 1 month ago class-check-email-header-parser.php 1 month ago class-check-email-log-autoloader.php 1 month ago class-check-email-newsletter.php 1 month ago deactivate-feedback.php 1 month ago helper-function.php 1 month ago install.php 1 month ago
Check_Email_Notify_Tab.php
655 lines
1 <?php
2
3 namespace CheckEmail;
4
5 defined('ABSPATH') || exit; // Exit if accessed directly
6
7 /**
8 * @class Check_Email_Notify_Tab
9 * @since 2.0
10 */
11 class Check_Email_Notify_Tab
12 {
13
14 private $notify_options;
15 private $is_enable;
16
17 public function __construct()
18 {
19 $this->setup_vars();
20 add_action('init', array($this, 'init'));
21 add_action('admin_enqueue_scripts', array($this, 'checkemail_assets_notify'));
22 add_action('wp_mail_failed', array($this, 'handle_failed_email'), 10, 1);
23 }
24
25 public function handle_failed_email($wp_error) {
26 // Define email categories to track
27 $email_types = [
28 'on_user_register' => 'New user registration',
29 'on_login' => 'Login password reset',
30 'on_forget' => 'password reset',
31 'on_order' => 'order',
32 ];
33 $error_message = $wp_error->get_error_message();
34 $failed_recipients = $wp_error->get_error_data()['to'];
35 $email_subject = isset($wp_error->get_error_data()['subject']) ? strtolower($wp_error->get_error_data()['subject']) : '';
36 foreach ($email_types as $key => $subject) {
37 if (strpos($email_subject, strtolower($subject)) !== false) {
38 $failed_count = get_option("failed_email_count_{$key}", 0);
39 update_option("failed_email_count_{$key}", $failed_count + 1);
40 $this->send_failed_email_notification($key, $error_message, $failed_count + 1, $error_code);
41 }
42 }
43 }
44
45 public function send_failed_email_notification($email_type, $error_message, $failed_count, $error_code = 'unknown') {
46
47 if (isset($this->notify_options['is_enable']) && !empty($this->notify_options['is_enable'])) {
48 if (isset($this->notify_options[$email_type]) && $this->notify_options[$email_type] && isset($this->notify_options[$email_type.'_count']) && $this->notify_options[$email_type.'_count']) {
49 $count_limit = $this->notify_options[$email_type.'_count'];
50
51 if ($failed_count > $count_limit) {
52 if (isset($this->notify_options['is_enable_by_sms']) && $this->notify_options['is_enable_by_sms']) {
53 $this->send_sms_twilio();
54 }
55 if (isset($this->notify_options['is_enable_by_push']) && $this->notify_options['is_enable_by_push']) {
56 $this->push_notify_admin_on_email_failure();
57 }
58 if (isset($this->notify_options['notify_by_mail']) && $this->notify_options['notify_by_mail']) {
59 $this->send_mail_notification_api($error_message, $error_code);
60 }
61
62 }
63 }
64 }
65 }
66
67
68 public function init()
69 {
70 /*if (! is_admin()) {
71 return;
72 }
73 $this->setup_vars();
74 add_action('check_mail_email_notify', array($this, 'load_email_notify_settings'));
75 add_action('admin_init', array($this, 'check_mail_notify_submission_handler'));
76 add_action('init', array($this,'serve_firebase_sw'));*/
77 if (is_admin()) {
78 add_action('check_mail_email_notify', array($this, 'load_email_notify_settings'));
79 add_action('admin_init', array($this, 'check_mail_notify_submission_handler'));
80 }
81
82 add_action('init', array($this,'serve_firebase_sw'));
83 }
84
85 function push_notify_admin_on_email_failure() {
86 $check_email = wpchill_check_email();
87 $plugin_dir_url = plugin_dir_url($check_email->get_plugin_file());
88 $admin_fcm_token = get_option('checkmail_admin_fcm_token');
89
90 if ($admin_fcm_token) {
91 $notification_data = [
92 'admin_tokens' => $admin_fcm_token,
93 'title' => esc_html__('Email Sending Failed!','check-email'),
94 'body' => esc_html__('An email failed to send in your WordPress site.','check-email'),
95 'siteurl' => $this->checkmail_home_url(),
96 'icon' => esc_attr($plugin_dir_url . 'assets/images/check-log-email.png')
97 ];
98
99 $pn_response = $this->sendRequest('send-checkmail-notification', $notification_data, $method="post");
100 }
101 }
102
103
104
105 public function setup_vars()
106 {
107 $this->notify_options = get_option('check-email-email-notify-options', true);
108 }
109 public function send_sms_twilio() {
110 $account_sid = (isset($this->notify_options['twilio_sid'])) ? $this->notify_options['twilio_sid'] : '';
111 $auth_token = (isset($this->notify_options['twilio_auth_token'])) ? $this->notify_options['twilio_auth_token'] : '';
112 $twilio_number = (isset($this->notify_options['twilio_number'])) ? $this->notify_options['twilio_number'] : '';
113 $to = (isset($this->notify_options['notifier_mobile'])) ? $this->notify_options['notifier_mobile'] : '';
114
115 if ($account_sid && $auth_token && $twilio_number && $to) {
116 $url = "https://api.twilio.com/2010-04-01/Accounts/{$account_sid}/Messages.json";
117
118 $data = [
119 'To' => $to,
120 'From' => $twilio_number,
121 'Body' => esc_html__('An email failed to send in your WordPress site', 'check-email'),
122 ];
123
124 // Set the headers
125 $headers = [
126 'Authorization' => 'Basic ' . base64_encode("{$account_sid}:{$auth_token}"),
127 'Content-Type' => 'application/x-www-form-urlencoded',
128 ];
129
130 // Make the POST request using wp_remote_post
131 $response = wp_remote_post( $url, [
132 'method' => 'POST',
133 'body' => http_build_query($data),
134 'headers' => $headers,
135 'timeout' => 15,
136 'sslverify' => false, // Disable SSL verification if you're on localhost
137 ]);
138
139 // Check for errors
140 if ( is_wp_error( $response ) ) {
141 $error_message = $response->get_error_message();
142 return $error_message;
143 }
144
145 // Parse the response
146 $result = json_decode( wp_remote_retrieve_body( $response ), true );
147 return $result;
148 }
149
150
151 }
152
153 //Nasir
154 private function send_mail_notification_api($error_message, $error_code = 'unknown') {
155
156 if ( empty( $this->notify_options['notify_by_mail'] ) || 1 != $this->notify_options['notify_by_mail'] ) {
157 return;
158 }
159
160 $recipient_email = ! empty( $this->notify_options['notifier_mail'] ) ? $this->notify_options['notifier_mail'] : get_option('admin_email');
161
162 $api_url = 'https://check-email.tech/api/send-notification.php';
163 $api_key = 'oG*c|>(~.JvW,pWLvYM7+#~reK9edHDGB!:[TVknFbY]Q(*Gg%EU.-f2:)%NYi-/';
164
165 $site_url = get_site_url();
166
167 $body_data = array(
168 'api_key' => $api_key,
169 'recipient_email' => $recipient_email,
170 'site_url' => $site_url,
171 'error_code' => $error_code,
172 'error_message' => $error_message
173 );
174
175 wp_remote_post( $api_url, array(
176 'method' => 'POST',
177 'timeout' => 15,
178 'blocking' => false,
179 'body' => $body_data,
180 ) );
181 }
182
183
184 public function load_email_notify_settings()
185 {
186 $is_enable = false;
187 $is_sms_enable = false;
188 if (isset($this->notify_options['is_enable']) && !empty($this->notify_options['is_enable'])) {
189 $is_enable = true;
190 }
191 if (isset($this->notify_options['is_enable_by_sms']) && $this->notify_options['is_enable_by_sms']) {
192 $is_sms_enable = true;
193 }
194
195 ?>
196
197 <form action="" method="post">
198 <div>
199 <table class="form-table" role="presentation">
200 <tbody>
201 <tr>
202 <th style="width: 280px;" scope="row"><label for="check-email-email-notify-options-is_enable" class="check-email-opt-labels"><?php esc_html_e('Nofity When Email Failed', 'check-email'); ?></label></th>
203 <td>
204 <input class="" type="checkbox" id="check-email-email-notify-options-is_enable" name="check-email-email-notify-options[is_enable]" value="1" <?php echo (isset($this->notify_options['is_enable'])) && $this->notify_options['is_enable'] ? "checked" : ''; ?>>
205 </td>
206 </tr>
207 </tbody>
208 </table>
209 <table class="form-table" role="presentation" id="ck-notify-table-id" style="<?php echo $is_enable ? "" : 'display:none;'; ?>">
210 <tbody>
211 <?php
212 $on_user_register = (isset($this->notify_options['on_user_register'])) && $this->notify_options['on_user_register'] ? true : false;
213 $on_login = (isset($this->notify_options['on_login'])) && $this->notify_options['on_login'] ? true : false;
214 $on_forget = (isset($this->notify_options['on_forget'])) && $this->notify_options['on_forget'] ? true : false;
215 $on_order = (isset($this->notify_options['on_order'])) && $this->notify_options['on_order'] ? true : false;
216 ?>
217 <tr>
218 <th scope="row"><label style="padding-left:10px;" for="check-email-notify-options-on_user_register" class="check-email-opt-labels"><?php esc_html_e('Notify me when New User Registers', 'check-email'); ?></label></th>
219 <td style="width: 40px;">
220 <input type="checkbox" class="checkmail_trigger" name="check-email-email-notify-options[on_user_register]" id="check-email-notify-options-on_user_register" value="1" <?php echo $on_user_register ? "checked" : ''; ?>><label style="<?php echo $on_user_register ? 'display:none;' : ''; ?>" for="check-email-notify-options-on_user_register" >Yes</label>
221 </td>
222 <th style="width: 280px;" scope="row" class=""><label style="<?php echo $on_user_register ? '' : 'display:none;'; ?>" for="check-email-notify-options-on_user_register" class="check-email-opt-labels checkmail_trigger_counts"><?php esc_html_e('Failed User Registeration Count', 'check-email'); ?></label></th>
223 <td class="">
224 <input style="<?php echo $on_user_register ? '' : 'display:none;'; ?>" class="checkmail_trigger_counts" type="number" min="1" id="check-email-email-notify-options-failed-email-count" name="check-email-email-notify-options[on_user_register_count]" placeholder="Number of failed email" value="<?php echo (isset($this->notify_options['on_user_register_count'])) && $this->notify_options['on_user_register_count'] ? esc_attr($this->notify_options['on_user_register_count']) : 2; ?>">
225 </td>
226 </tr>
227
228 <tr>
229 <th scope="row"><label style="padding-left:10px;" for="check-email-notify-options-on_login" class="check-email-opt-labels"><?php esc_html_e('Notify me when Login password', 'check-email'); ?></label></th>
230 <td>
231 <input type="checkbox" class="checkmail_trigger" name="check-email-email-notify-options[on_login]" id="check-email-notify-options-on_login" value="1" <?php echo $on_login ? "checked" : ''; ?>><label style="<?php echo $on_login ? 'display:none;' : ''; ?>" for="check-email-notify-options-on_login" >Yes</label>
232 </td>
233 <th scope="row"><label style="<?php echo $on_login ? '' : 'display:none;'; ?>" for="check-email-notify-options-on_login" class="check-email-opt-labels checkmail_trigger_counts"><?php esc_html_e('Failed Login Password Count', 'check-email'); ?></label></th>
234 <td>
235 <input style="<?php echo $on_login ? '' : 'display:none;'; ?>" class="checkmail_trigger_counts" type="number" min="1" id="check-email-email-notify-options-failed-email-count" name="check-email-email-notify-options[on_login_count]" placeholder="Number of failed email" value="<?php echo (isset($this->notify_options['on_login_count'])) && $this->notify_options['on_login_count'] ? esc_attr($this->notify_options['on_login_count']) : 2; ?>">
236 </td>
237 </tr>
238
239 <tr>
240 <th style="width: 280px;" scope="row"><label style="padding-left:10px;" for="check-email-notify-options-on_forget" class="check-email-opt-labels"><?php esc_html_e('Notify me when Forget password', 'check-email'); ?></label></th>
241 <td>
242 <input type="checkbox" class="checkmail_trigger" name="check-email-email-notify-options[on_forget]" id="check-email-notify-options-on_forget" value="1" <?php echo $on_forget ? "checked" : ''; ?>><label style="<?php echo $on_forget ? 'display:none;' : ''; ?>" for="check-email-notify-options-on_forget" >Yes</label>
243 </td>
244 <th style="width: 280px;" scope="row"><label style="<?php echo $on_forget ? '' : 'display:none;'; ?>" for="check-email-notify-options-on_forget" class="check-email-opt-labels checkmail_trigger_counts"><?php esc_html_e('Failed Forget Password Count', 'check-email'); ?></label></th>
245 <td>
246 <input style="<?php echo $on_forget ? '' : 'display:none;'; ?>" class="checkmail_trigger_counts" type="number" min="1" id="check-email-email-notify-options-failed-email-count" name="check-email-email-notify-options[on_forget_count]" placeholder="Number of failed email" value="<?php echo (isset($this->notify_options['on_forget_count'])) && $this->notify_options['on_forget_count'] ? esc_attr($this->notify_options['on_forget_count']) : 2; ?>">
247 </td>
248 </tr>
249
250 <tr>
251 <th scope="row"><label style="padding-left:10px;" for="check-email-email-notify-options-secondary-email" class="check-email-opt-labels"><?php esc_html_e('Notify me when Woocommerce order', 'check-email'); ?></label></th>
252 <td>
253 <input type="checkbox" class="checkmail_trigger" name="check-email-email-notify-options[on_order]" id="check-email-notify-options-on_order" value="1" <?php echo $on_order ? "checked" : ''; ?>><label style="<?php echo $on_order ? 'display:none;' : ''; ?>" for="check-email-notify-options-on_order" >Yes</label>
254 </td>
255 <th scope="row"><label style="<?php echo $on_order ? '' : 'display:none;'; ?>" for="check-email-email-notify-options-secondary-email" class="check-email-opt-labels checkmail_trigger_counts"><?php esc_html_e('Failed Order Count', 'check-email'); ?></label></th>
256 <td>
257 <input style="<?php echo $on_order ? '' : 'display:none;'; ?>" class="checkmail_trigger_counts" type="number" min="1" id="check-email-email-notify-options-failed-email-count" name="check-email-email-notify-options[on_order_count]" placeholder="Number of failed email" value="<?php echo (isset($this->notify_options['on_order_count'])) && $this->notify_options['on_order_count'] ? esc_attr( $this->notify_options['on_order_count'] ) : 2; ?>">
258 </td>
259 </tr>
260 </tbody>
261 </table>
262 <hr/>
263 <h2><?php esc_html_e('Notification Methods', 'check-email'); ?></h2>
264 <table class="form-table" role="presentation">
265 <tbody>
266 <tr>
267 <th style="width: 280px;" scope="row"><label for="check-email-notify-by-sms-enable" class="check-email-opt-labels"><?php esc_html_e('Notify By SMS', 'check-email'); ?></label></th>
268 <td>
269 <input class="" type="checkbox" id="check-email-notify-by-sms-enable" name="check-email-email-notify-options[is_enable_by_sms]" value="1" <?php echo $is_sms_enable ? "checked" : ''; ?>>
270 <label for="check-email-notify-by-sms-enable" class="check-email-opt-labels"><?php esc_html_e('Receive instant alerts directly to your phone when emails fails to send via SMS', 'check-email'); ?></label>
271 </td>
272 </tr>
273 </tbody>
274 </table>
275 <table class="form-table check-email-twilio" role="presentation" style="<?php echo $is_sms_enable ? "" : 'display:none;'; ?>">
276 <tbody>
277 <tr>
278 <th style="width: 280px;" scope="row"><label style="padding-left:10px;" for="check-email-notify-mobile-number" class="check-email-opt-labels"><?php esc_html_e('Mobile number of notifier', 'check-email'); ?></label></th>
279 <td>
280 <input class="regular-text" type="text" id="check-email-notify-mobile-number" name="check-email-email-notify-options[notifier_mobile]" value="<?php echo (isset($this->notify_options['notifier_mobile'])) ? esc_attr( $this->notify_options['notifier_mobile'] ) : ''; ?>">
281 </td>
282 </tr>
283 <tr>
284 <th scope="row"><label style="padding-left:10px;" for="check-email-notify-twilio-sid" class="check-email-opt-labels"><?php esc_html_e('Your twilio sid', 'check-email'); ?></label></th>
285 <td>
286 <input class="regular-text" type="text" id="check-email-notify-twilio-sid" name="check-email-email-notify-options[twilio_sid]" value="<?php echo (isset($this->notify_options['twilio_sid'])) ? esc_attr( $this->notify_options['twilio_sid'] ) : ''; ?>">
287 </td>
288 </tr>
289 <tr>
290 <th scope="row"><label style="padding-left:10px;" for="check-email-notify-twilio-auth-token" class="check-email-opt-labels"><?php esc_html_e('Your twilio auth token', 'check-email'); ?></label></th>
291 <td>
292 <input class="regular-text" type="text" id="check-email-notify-twilio-auth-token" name="check-email-email-notify-options[twilio_auth_token]" value="<?php echo (isset($this->notify_options['twilio_auth_token'])) ? esc_attr( $this->notify_options['twilio_auth_token'] ) : ''; ?>">
293 </td>
294 </tr>
295 <tr>
296 <th scope="row"><label style="padding-left:10px;" for="check-email-notify-twilio-number" class="check-email-opt-labels"><?php esc_html_e('Your twilio number', 'check-email'); ?></label></th>
297 <td>
298 <input class="regular-text" type="text" id="check-email-notify-twilio-number" name="check-email-email-notify-options[twilio_number]" value="<?php echo (isset($this->notify_options['twilio_number'])) ? esc_attr( $this->notify_options['twilio_number'] ) : ''; ?>">
299 </td>
300 </tr>
301 </tbody>
302 </table>
303 <table class="form-table" role="presentation">
304 <tbody>
305 <tr>
306 <th style="width: 280px;" scope="row"><label for="check-email-notify-by-push-enable" class="check-email-opt-labels"><?php esc_html_e('Notify By Push Notification', 'check-email'); ?></label></th>
307 <td>
308 <input class="" type="checkbox" id="check-email-notify-by-push-enable" name="check-email-email-notify-options[is_enable_by_push]" value="1" <?php echo (isset($this->notify_options['is_enable_by_push'])) && $this->notify_options['is_enable_by_push'] ? "checked" : ''; ?>>
309 <label for="check-email-notify-by-push-enable" class="check-email-opt-labels"><?php esc_html_e('Receive instant alerts directly to your phone when emails fails to send by push notification', 'check-email'); ?></label>
310 </td>
311 </tr>
312
313 </tbody>
314 </table>
315 <!--Nasir-->
316 <?php
317 $is_mail_enable = isset($this->notify_options['notify_by_mail']) && $this->notify_options['notify_by_mail'] ? true : false;
318 ?>
319
320 <!-- Notify By Mail Option -->
321 <table class="form-table" role="presentation">
322 <tbody>
323 <tr>
324 <th style="width: 280px;" scope="row">
325 <label for="check-email-notify-options-notify_by_mail" class="check-email-opt-labels">
326 <?php esc_html_e('Notify By mail', 'check-email'); ?>
327 </label>
328 </th>
329 <td>
330 <input type="checkbox"
331 id="check-email-notify-options-notify_by_mail"
332 name="check-email-email-notify-options[notify_by_mail]"
333 value="1"
334 <?php echo $is_mail_enable ? 'checked' : ''; ?>>
335 <label for="check-email-notify-options-notify_by_mail_email" class="check-email-opt-labels">
336 <?php esc_html_e('Receive instant alerts directly to your mail when emails fail to send via email', 'check-email'); ?>
337 </label>
338 </td>
339 </tr>
340 </tbody>
341 </table>
342
343 <!-- Dependent Email Field -->
344 <table class="form-table check-email-mail-table" role="presentation" style="<?php echo $is_mail_enable ? '' : 'display:none;'; ?>">
345 <tbody>
346 <tr>
347 <th style="width: 280px;" scope="row">
348 <label style="padding-left:10px;" for="check-email-notify-mail" class="check-email-opt-labels">
349 <?php esc_html_e('Email ID of notifier', 'check-email'); ?>
350 </label>
351 </th>
352 <td>
353 <?php
354 $default_email = get_option('admin_email');
355 $notifier_mail = isset($this->notify_options['notifier_mail']) && !empty($this->notify_options['notifier_mail'])
356 ? $this->notify_options['notifier_mail']
357 : $default_email;
358 ?>
359 <input class="regular-text" type="email" id="check-email-notify-mail"
360 name="check-email-email-notify-options[notifier_mail]"
361 value="<?php echo esc_attr( $notifier_mail ); ?>">
362 </td>
363 </tr>
364 </tbody>
365 </table>
366
367 <script type="text/javascript">
368 document.addEventListener('DOMContentLoaded', function() {
369 const checkbox = document.getElementById('check-email-notify-options-notify_by_mail');
370 const mailTable = document.querySelector('.check-email-mail-table');
371 if(!checkbox || !mailTable) return;
372
373 checkbox.addEventListener('change', function() {
374 mailTable.style.display = checkbox.checked ? '' : 'none';
375 });
376 });
377 </script>
378
379 </div>
380 <?php wp_nonce_field('check_mail_email_notify_nonce', 'check_mail_email_notify_nonce'); ?>
381 <p class="submit"><input type="submit" name="check_mail_email_notify_submit" id="check_mail_email_notify_submit" class="button button-primary" value="<?php esc_attr_e('Save', 'check-email'); ?>"></p>
382 </form>
383 <?php
384 }
385
386
387
388 public function check_mail_notify_submission_handler()
389 {
390
391 if (isset($_POST['check_mail_email_notify_submit']) && $_POST['check_mail_email_notify_submit'] == 'Save') {
392 if (!isset($_POST['check_mail_email_notify_nonce'])) {
393 return;
394 }
395
396 if (!wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['check_mail_email_notify_nonce'])), 'check_mail_email_notify_nonce')) {
397 return;
398 }
399
400 if (! current_user_can('manage_check_email')) {
401 return;
402 }
403 $email_encode_option['is_enable'] = 0;
404 if (isset($_POST['check-email-email-notify-options']['is_enable'])) {
405 $email_encode_option['is_enable'] = 1;
406 }
407 $email_encode_option['is_enable_by_sms'] = 0;
408 if (isset($_POST['check-email-email-notify-options']['is_enable_by_sms'])) {
409 $email_encode_option['is_enable_by_sms'] = 1;
410 }
411 $email_encode_option['is_enable_by_push'] = 0;
412 if (isset($_POST['check-email-email-notify-options']['is_enable_by_push'])) {
413 $email_encode_option['is_enable_by_push'] = 1;
414 }
415 //Nasir
416 $email_encode_option['notify_by_mail'] = 0;
417 if ( isset( $_POST['check-email-email-notify-options']['notify_by_mail'] ) ) {
418 $email_encode_option['notify_by_mail'] = 1;
419 }
420
421 if ( isset( $_POST['check-email-email-notify-options']['notifier_mail'] ) ) {
422 $email_encode_option['notifier_mail'] = sanitize_email( wp_unslash( $_POST['check-email-email-notify-options']['notifier_mail'] ) );
423 } else {
424 $email_encode_option['notifier_mail'] = get_option('admin_email');
425 }
426
427 $email_encode_option['on_user_register'] = 0;
428 if ( isset( $_POST['check-email-email-notify-options']['on_user_register']) && isset( $_POST['check-email-email-notify-options']['on_user_register_count'] ) ) {
429 $email_encode_option['on_user_register'] = 1;
430 $email_encode_option['on_user_register_count'] = sanitize_text_field(wp_unslash($_POST['check-email-email-notify-options']['on_user_register_count']));
431 }
432 $email_encode_option['on_login'] = 0;
433 if ( isset($_POST['check-email-email-notify-options']['on_login']) && isset( $_POST['check-email-email-notify-options']['on_login_count'] ) ) {
434 $email_encode_option['on_login'] = 1;
435 $email_encode_option['on_login_count'] = sanitize_text_field(wp_unslash($_POST['check-email-email-notify-options']['on_login_count']));
436 }
437 $email_encode_option['on_forget'] = 0;
438 if ( isset($_POST['check-email-email-notify-options']['on_forget'] ) && isset( $_POST['check-email-email-notify-options']['on_forget_count'] ) ) {
439 $email_encode_option['on_forget'] = 1;
440 $email_encode_option['on_forget_count'] = sanitize_text_field(wp_unslash($_POST['check-email-email-notify-options']['on_forget_count']));
441 }
442 $email_encode_option['on_order'] = 0;
443 if ( isset($_POST['check-email-email-notify-options']['on_order']) && isset( $_POST['check-email-email-notify-options']['on_order_count'] ) ) {
444 $email_encode_option['on_order'] = 1;
445 $email_encode_option['on_order_count'] = sanitize_text_field(wp_unslash($_POST['check-email-email-notify-options']['on_order_count']));
446 }
447
448
449
450 if (isset($_POST['check-email-email-notify-options']['notifier_mobile'])) {
451 $email_encode_option['notifier_mobile'] = sanitize_text_field(wp_unslash($_POST['check-email-email-notify-options']['notifier_mobile']));
452 }
453 if (isset($_POST['check-email-email-notify-options']['twilio_sid'])) {
454 $email_encode_option['twilio_sid'] = sanitize_text_field(wp_unslash($_POST['check-email-email-notify-options']['twilio_sid']));
455 }
456 if (isset($_POST['check-email-email-notify-options']['twilio_auth_token'])) {
457 $email_encode_option['twilio_auth_token'] = sanitize_text_field(wp_unslash($_POST['check-email-email-notify-options']['twilio_auth_token']));
458 }
459 if (isset($_POST['check-email-email-notify-options']['twilio_number'])) {
460 $email_encode_option['twilio_number'] = sanitize_text_field(wp_unslash($_POST['check-email-email-notify-options']['twilio_number']));
461 }
462 update_option('check-email-email-notify-options', $email_encode_option);
463
464 wp_safe_redirect(admin_url('admin.php?page=check-email-settings&tab=notify'));
465
466 }
467 }
468
469 public function checkmail_https($url)
470 {
471
472 if (strpos($url, 'localhost') === false) {
473 return str_replace('http://', 'https://', $url);
474 } else {
475 return $url;
476 }
477 }
478
479 public function checkmail_home_url()
480 {
481
482 if (is_multisite()) {
483 $link = get_site_url();
484 } else {
485 $link = home_url();
486 }
487 $link = $this->checkmail_https($link);
488
489 return trailingslashit($link);
490 }
491
492 public function checkemail_assets_notify()
493 {
494 if (!isset($this->notify_options['is_enable']) || empty($this->notify_options['is_enable']) || !isset($this->notify_options['is_enable_by_push']) || empty($this->notify_options['is_enable_by_push'])) {
495 return;
496 }
497 $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '';
498 $check_email = wpchill_check_email();
499 $plugin_dir_url = plugin_dir_url($check_email->get_plugin_file());
500 $home_url = $this->checkmail_home_url();
501 wp_enqueue_script(
502 'firebase-app',
503 $plugin_dir_url . 'assets/js/admin/firebase-app.js',
504 array(),
505 $check_email->get_version(),
506 true
507 );
508
509 // Enqueue the Firebase messaging script
510 wp_enqueue_script(
511 'firebase-messaging',
512 $plugin_dir_url . 'assets/js/admin/firebase-messaging.js',
513 array('firebase-app'),
514 $check_email->get_version(),
515 true
516 );
517
518
519 $pn_response = $this->sendRequest('get-config-details', null, $method="post");
520
521 wp_enqueue_script('checkemail_push', $plugin_dir_url . 'assets/js/admin/check_mail_push' . $suffix . '.js', array(), $check_email->get_version(), true);
522 $data['ajax_url'] = admin_url('admin-ajax.php');
523 $data['ck_mail_security_nonce'] = wp_create_nonce('ck_mail_security_nonce');
524 $data['fcm_config'] = (isset($pn_response['response'])) ? $pn_response['response'] : [];
525
526 wp_localize_script('checkemail_push', 'checkemail_pushdata', $data);
527 ?>
528
529 <script type="text/javascript">
530 if ("serviceWorker" in navigator) {
531 navigator.serviceWorker
532 .register("<?php echo esc_attr($plugin_dir_url . 'assets/js/admin/checkmail-sw.js'); ?>", {
533 scope: "<?php echo esc_attr($plugin_dir_url . 'assets/js/admin/'); ?>"
534 })
535 .then((registration) => {
536 console.log("Service Worker registered:");
537 })
538 .catch((error) => {
539 console.error("Service Worker registration failed:", error);
540 });
541 }
542 </script>
543 <?php
544 $this->pwaforwp_swhtml_init_firebase_js();
545 }
546
547 public function pwaforwp_swhtml_init_firebase_js($action = null)
548 {
549 //Dummy file to work FCM perfectly
550 if ( isset( $_SERVER['DOCUMENT_ROOT'] ) ) {
551 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized , WordPress.Security.ValidatedSanitizedInput.MissingUnslash
552 $wppath = $_SERVER['DOCUMENT_ROOT']."/";
553 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
554 $wppath = apply_filters("checkmail_file_creation_path", $wppath);
555
556 $pn_sw_js = $wppath . "firebase-messaging-sw.js";
557 $swjsContent = '';
558 $status = $this->checkmail_write_a_file($pn_sw_js, $swjsContent, $action);
559 return $status;
560 }
561 }
562
563 public function checkmail_write_a_file($path, $content, $action = null)
564 {
565
566 global $wp_filesystem;
567
568 // Initialize the WP_Filesystem global
569 if (! function_exists('WP_Filesystem')) {
570 require_once(ABSPATH . 'wp-admin/includes/file.php');
571 }
572
573 WP_Filesystem();
574
575 $writestatus = '';
576
577 if ($wp_filesystem->exists($path)) {
578 $writestatus = wp_delete_file($path);
579 }
580
581 $check_email = wpchill_check_email();
582 $plugin_dir_url = plugin_dir_url($check_email->get_plugin_file());
583 $file_path = $plugin_dir_url . 'assets/js/admin/checkmail-sw.js';
584 $content = file_get_contents($file_path);
585 if (! $action && $content) {
586 $writestatus = $wp_filesystem->put_contents($path, $content, FS_CHMOD_FILE);
587 }
588
589 if ($writestatus) {
590 return true;
591 }
592 return false;
593 }
594
595 public function json_settings()
596 {
597 if (is_multisite()) {
598 $link = get_site_url();
599 } else {
600 $link = home_url();
601 }
602 $messageConfig = '';
603 $settings = array(
604 'nonce' => wp_create_nonce("checkmail_notification"),
605 'pn_config' => $messageConfig,
606 "swsource" => esc_url_raw(trailingslashit($link) . "?checkmail_notification_sw=1"),
607 "scope" => esc_url_raw(trailingslashit($link)),
608 "ajax_url" => esc_url_raw(admin_url('admin-ajax.php'))
609 );
610 return $settings;
611 }
612
613 public function sendRequest($suffixUrl, $data, $method="post"){
614 $notificationServerUrl = 'https://pushnotifications.io/api/';
615 if($method==='post'){
616 $url = $notificationServerUrl.$suffixUrl;
617 $postdata = array('body'=> $data);
618 $remoteResponse = wp_remote_post($url, $postdata);
619 }
620
621 if( is_wp_error( $remoteResponse ) ){
622 if(!empty($remoteResponse->get_error_message()) ) {
623 $error_message = strtolower($remoteResponse->get_error_message());
624 $error_pos = strpos($error_message, 'operation timed out');
625 if($error_pos !== false){
626 $message = __('Request timed out, please try again','check-email');
627 }else{
628 $message = esc_html($remoteResponse->get_error_message());
629 }
630 }else{
631 $message = __("could not connect to server",'check-email');
632 }
633 $remoteData = array('status'=>401, "response"=>$message);
634
635 }else{
636 $remoteData = wp_remote_retrieve_body($remoteResponse);
637 $remoteData = json_decode($remoteData, true);
638 }
639 return $remoteData;
640 }
641
642 function serve_firebase_sw() {
643 if (!isset($this->notify_options['is_enable']) || empty($this->notify_options['is_enable']) || !isset($this->notify_options['is_enable_by_push']) || empty($this->notify_options['is_enable_by_push'])) {
644 return;
645 }
646 $check_email = wpchill_check_email();
647 $plugin_dir_url = plugin_dir_url($check_email->get_plugin_file());
648 header("Content-Type: application/javascript");
649 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_readfile
650 readfile($plugin_dir_url . 'assets/js/admin/checkmail-sw.js');
651 exit;
652 }
653
654 }
655