PluginProbe ʕ •ᴥ•ʔ
Check & Log Email – Easy Email Testing & Mail logging / 2.0.9
Check & Log Email – Easy Email Testing & Mail logging v2.0.9
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 9 months ago Util 9 months ago Check_Email_Encode_Tab.php 9 months ago Check_Email_Notify_Tab.php 9 months ago Check_Email_SMTP_Tab.php 9 months ago class-check-email-header-parser.php 9 months ago class-check-email-log-autoloader.php 9 months ago class-check-email-newsletter.php 9 months ago deactivate-feedback.php 9 months ago helper-function.php 9 months ago install.php 9 months ago
Check_Email_Notify_Tab.php
534 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 add_action('init', array($this, 'init'));
20 add_action('admin_enqueue_scripts', array($this, 'checkemail_assets_notify'));
21 add_action('wp_mail_failed', array($this, 'handle_failed_email'), 10, 1);
22 }
23
24 public function handle_failed_email($wp_error) {
25 // Define email categories to track
26 $email_types = [
27 'on_user_register' => 'New user registration',
28 'on_login' => 'Login password reset',
29 'on_forget' => 'Forget password reset',
30 'on_order' => 'WooCommerce order email',
31 ];
32 $error_message = $wp_error->get_error_message();
33 $failed_recipients = $wp_error->get_error_data()['to'];
34 $email_subject = isset($wp_error->get_error_data()['subject']) ? strtolower($wp_error->get_error_data()['subject']) : '';
35 foreach ($email_types as $key => $subject) {
36 if (strpos($email_subject, strtolower($subject)) !== false) {
37 $failed_count = get_option("failed_email_count_{$key}", 0);
38 update_option("failed_email_count_{$key}", $failed_count + 1);
39 $this->send_failed_email_notification($key, $error_message, $failed_count + 1);
40 }
41 }
42 }
43
44 public function send_failed_email_notification($email_type, $error_message, $failed_count) {
45
46 if (isset($this->notify_options['is_enable']) && !empty($this->notify_options['is_enable'])) {
47 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']) {
48 $count_limit = $this->notify_options[$email_type.'_count'];
49 if ($failed_count > $count_limit) {
50 if (isset($this->notify_options['is_enable_by_sms']) && $this->notify_options['is_enable_by_sms']) {
51 $this->send_sms_twilio();
52 }
53 if (isset($this->notify_options['is_enable_by_push']) && $this->notify_options['is_enable_by_push']) {
54 $this->push_notify_admin_on_email_failure();
55 }
56 }
57 }
58 }
59 }
60
61
62 public function init()
63 {
64 if (! is_admin()) {
65 return;
66 }
67 $this->setup_vars();
68 add_action('check_mail_email_notify', array($this, 'load_email_notify_settings'));
69 add_action('admin_init', array($this, 'check_mail_notify_submission_handler'));
70 add_action('init', array($this,'serve_firebase_sw'));
71 }
72
73 function push_notify_admin_on_email_failure() {
74 $check_email = wpchill_check_email();
75 $plugin_dir_url = plugin_dir_url($check_email->get_plugin_file());
76 $admin_fcm_token = get_option('checkmail_admin_fcm_token');
77
78 if ($admin_fcm_token) {
79 $notification_data = [
80 'admin_tokens' => $admin_fcm_token,
81 'title' => esc_html__('Email Sending Failed!','check-email'),
82 'body' => esc_html__('An email failed to send in your WordPress site.','check-email'),
83 'siteurl' => $this->checkmail_home_url(),
84 'icon' => esc_attr($plugin_dir_url . 'assets/images/check-log-email.png')
85 ];
86
87 $pn_response = $this->sendRequest('send-checkmail-notification', $notification_data, $method="post");
88 }
89 }
90
91
92
93 public function setup_vars()
94 {
95 $this->notify_options = get_option('check-email-email-notify-options', true);
96 }
97 public function send_sms_twilio() {
98 $account_sid = (isset($this->notify_options['twilio_sid'])) ? $this->notify_options['twilio_sid'] : '';
99 $auth_token = (isset($this->notify_options['twilio_auth_token'])) ? $this->notify_options['twilio_auth_token'] : '';
100 $twilio_number = (isset($this->notify_options['twilio_number'])) ? $this->notify_options['twilio_number'] : '';
101 $to = (isset($this->notify_options['notifier_mobile'])) ? $this->notify_options['notifier_mobile'] : '';
102
103 if ($account_sid && $auth_token && $twilio_number && $to) {
104 $url = "https://api.twilio.com/2010-04-01/Accounts/{$account_sid}/Messages.json";
105
106 $data = [
107 'To' => $to,
108 'From' => $twilio_number,
109 'Body' => esc_html__('An email failed to send in your WordPress site', 'check-email'),
110 ];
111
112 // Set the headers
113 $headers = [
114 'Authorization' => 'Basic ' . base64_encode("{$account_sid}:{$auth_token}"),
115 'Content-Type' => 'application/x-www-form-urlencoded',
116 ];
117
118 // Make the POST request using wp_remote_post
119 $response = wp_remote_post( $url, [
120 'method' => 'POST',
121 'body' => http_build_query($data),
122 'headers' => $headers,
123 'timeout' => 15,
124 'sslverify' => false, // Disable SSL verification if you're on localhost
125 ]);
126
127 // Check for errors
128 if ( is_wp_error( $response ) ) {
129 $error_message = $response->get_error_message();
130 return $error_message;
131 }
132
133 // Parse the response
134 $result = json_decode( wp_remote_retrieve_body( $response ), true );
135 return $result;
136 }
137
138
139 }
140 public function load_email_notify_settings()
141 {
142 $is_enable = false;
143 $is_sms_enable = false;
144 if (isset($this->notify_options['is_enable']) && !empty($this->notify_options['is_enable'])) {
145 $is_enable = true;
146 }
147 if (isset($this->notify_options['is_enable_by_sms']) && $this->notify_options['is_enable_by_sms']) {
148 $is_sms_enable = true;
149 }
150
151 ?>
152
153 <form action="" method="post">
154 <div>
155 <table class="form-table" role="presentation">
156 <tbody>
157 <tr>
158 <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>
159 <td>
160 <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" : ''; ?>>
161 </td>
162 </tr>
163 </tbody>
164 </table>
165 <table class="form-table" role="presentation" id="ck-notify-table-id" style="<?php echo $is_enable ? "" : 'display:none;'; ?>">
166 <tbody>
167 <?php
168 $on_user_register = (isset($this->notify_options['on_user_register'])) && $this->notify_options['on_user_register'] ? true : false;
169 $on_login = (isset($this->notify_options['on_login'])) && $this->notify_options['on_login'] ? true : false;
170 $on_forget = (isset($this->notify_options['on_forget'])) && $this->notify_options['on_forget'] ? true : false;
171 $on_order = (isset($this->notify_options['on_order'])) && $this->notify_options['on_order'] ? true : false;
172 ?>
173 <tr>
174 <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>
175 <td style="width: 40px;">
176 <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>
177 </td>
178 <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>
179 <td class="">
180 <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; ?>">
181 </td>
182 </tr>
183
184 <tr>
185 <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>
186 <td>
187 <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>
188 </td>
189 <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>
190 <td>
191 <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; ?>">
192 </td>
193 </tr>
194
195 <tr>
196 <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>
197 <td>
198 <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>
199 </td>
200 <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>
201 <td>
202 <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; ?>">
203 </td>
204 </tr>
205
206 <tr>
207 <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>
208 <td>
209 <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>
210 </td>
211 <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>
212 <td>
213 <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; ?>">
214 </td>
215 </tr>
216 </tbody>
217 </table>
218 <hr/>
219 <h2><?php esc_html_e('Notification Methods', 'check-email'); ?></h2>
220 <table class="form-table" role="presentation">
221 <tbody>
222 <tr>
223 <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>
224 <td>
225 <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" : ''; ?>>
226 <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>
227 </td>
228 </tr>
229 </tbody>
230 </table>
231 <table class="form-table check-email-twilio" role="presentation" style="<?php echo $is_sms_enable ? "" : 'display:none;'; ?>">
232 <tbody>
233 <tr>
234 <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>
235 <td>
236 <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'] ) : ''; ?>">
237 </td>
238 </tr>
239 <tr>
240 <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>
241 <td>
242 <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'] ) : ''; ?>">
243 </td>
244 </tr>
245 <tr>
246 <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>
247 <td>
248 <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'] ) : ''; ?>">
249 </td>
250 </tr>
251 <tr>
252 <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>
253 <td>
254 <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'] ) : ''; ?>">
255 </td>
256 </tr>
257 </tbody>
258 </table>
259 <table class="form-table" role="presentation">
260 <tbody>
261 <tr>
262 <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>
263 <td>
264 <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" : ''; ?>>
265 <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>
266 </td>
267 </tr>
268
269 </tbody>
270 </table>
271 </div>
272 <?php wp_nonce_field('check_mail_email_notify_nonce', 'check_mail_email_notify_nonce'); ?>
273 <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>
274 </form>
275 <?php
276 }
277
278
279
280 public function check_mail_notify_submission_handler()
281 {
282
283 if (isset($_POST['check_mail_email_notify_submit']) && $_POST['check_mail_email_notify_submit'] == 'Save') {
284 if (!isset($_POST['check_mail_email_notify_nonce'])) {
285 return;
286 }
287
288 if (!wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['check_mail_email_notify_nonce'])), 'check_mail_email_notify_nonce')) {
289 return;
290 }
291
292 if (! current_user_can('manage_check_email')) {
293 return;
294 }
295 $email_encode_option['is_enable'] = 0;
296 if (isset($_POST['check-email-email-notify-options']['is_enable'])) {
297 $email_encode_option['is_enable'] = 1;
298 }
299 $email_encode_option['is_enable_by_sms'] = 0;
300 if (isset($_POST['check-email-email-notify-options']['is_enable_by_sms'])) {
301 $email_encode_option['is_enable_by_sms'] = 1;
302 }
303 $email_encode_option['is_enable_by_push'] = 0;
304 if (isset($_POST['check-email-email-notify-options']['is_enable_by_push'])) {
305 $email_encode_option['is_enable_by_push'] = 1;
306 }
307
308 $email_encode_option['on_user_register'] = 0;
309 if ( isset( $_POST['check-email-email-notify-options']['on_user_register']) && isset( $_POST['check-email-email-notify-options']['on_user_register_count'] ) ) {
310 $email_encode_option['on_user_register'] = 1;
311 $email_encode_option['on_user_register_count'] = sanitize_text_field(wp_unslash($_POST['check-email-email-notify-options']['on_user_register_count']));
312 }
313 $email_encode_option['on_login'] = 0;
314 if ( isset($_POST['check-email-email-notify-options']['on_login']) && isset( $_POST['check-email-email-notify-options']['on_login_count'] ) ) {
315 $email_encode_option['on_login'] = 1;
316 $email_encode_option['on_login_count'] = sanitize_text_field(wp_unslash($_POST['check-email-email-notify-options']['on_login_count']));
317 }
318 $email_encode_option['on_forget'] = 0;
319 if ( isset($_POST['check-email-email-notify-options']['on_forget'] ) && isset( $_POST['check-email-email-notify-options']['on_forget_count'] ) ) {
320 $email_encode_option['on_forget'] = 1;
321 $email_encode_option['on_forget_count'] = sanitize_text_field(wp_unslash($_POST['check-email-email-notify-options']['on_forget_count']));
322 }
323 $email_encode_option['on_order'] = 0;
324 if ( isset($_POST['check-email-email-notify-options']['on_order']) && isset( $_POST['check-email-email-notify-options']['on_order_count'] ) ) {
325 $email_encode_option['on_order'] = 1;
326 $email_encode_option['on_order_count'] = sanitize_text_field(wp_unslash($_POST['check-email-email-notify-options']['on_order_count']));
327 }
328
329
330
331 if (isset($_POST['check-email-email-notify-options']['notifier_mobile'])) {
332 $email_encode_option['notifier_mobile'] = sanitize_text_field(wp_unslash($_POST['check-email-email-notify-options']['notifier_mobile']));
333 }
334 if (isset($_POST['check-email-email-notify-options']['twilio_sid'])) {
335 $email_encode_option['twilio_sid'] = sanitize_text_field(wp_unslash($_POST['check-email-email-notify-options']['twilio_sid']));
336 }
337 if (isset($_POST['check-email-email-notify-options']['twilio_auth_token'])) {
338 $email_encode_option['twilio_auth_token'] = sanitize_text_field(wp_unslash($_POST['check-email-email-notify-options']['twilio_auth_token']));
339 }
340 if (isset($_POST['check-email-email-notify-options']['twilio_number'])) {
341 $email_encode_option['twilio_number'] = sanitize_text_field(wp_unslash($_POST['check-email-email-notify-options']['twilio_number']));
342 }
343 update_option('check-email-email-notify-options', $email_encode_option);
344
345 wp_safe_redirect(admin_url('admin.php?page=check-email-settings&tab=notify'));
346 }
347 }
348
349 public function checkmail_https($url)
350 {
351
352 if (strpos($url, 'localhost') === false) {
353 return str_replace('http://', 'https://', $url);
354 } else {
355 return $url;
356 }
357 }
358
359 public function checkmail_home_url()
360 {
361
362 if (is_multisite()) {
363 $link = get_site_url();
364 } else {
365 $link = home_url();
366 }
367 $link = $this->checkmail_https($link);
368
369 return trailingslashit($link);
370 }
371
372 public function checkemail_assets_notify()
373 {
374 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'])) {
375 return;
376 }
377 $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '';
378 $check_email = wpchill_check_email();
379 $plugin_dir_url = plugin_dir_url($check_email->get_plugin_file());
380 $home_url = $this->checkmail_home_url();
381 wp_enqueue_script(
382 'firebase-app',
383 $plugin_dir_url . 'assets/js/admin/firebase-app.js',
384 array(),
385 $check_email->get_version(),
386 true
387 );
388
389 // Enqueue the Firebase messaging script
390 wp_enqueue_script(
391 'firebase-messaging',
392 $plugin_dir_url . 'assets/js/admin/firebase-messaging.js',
393 array('firebase-app'),
394 $check_email->get_version(),
395 true
396 );
397
398
399 $pn_response = $this->sendRequest('get-config-details', null, $method="post");
400
401 wp_enqueue_script('checkemail_push', $plugin_dir_url . 'assets/js/admin/check_mail_push' . $suffix . '.js', array(), $check_email->get_version(), true);
402 $data['ajax_url'] = admin_url('admin-ajax.php');
403 $data['ck_mail_security_nonce'] = wp_create_nonce('ck_mail_security_nonce');
404 $data['fcm_config'] = (isset($pn_response['response'])) ? $pn_response['response'] : [];
405
406 wp_localize_script('checkemail_push', 'checkemail_pushdata', $data);
407 ?>
408
409 <script type="text/javascript">
410 if ("serviceWorker" in navigator) {
411 navigator.serviceWorker
412 .register("<?php echo esc_attr($plugin_dir_url . 'assets/js/admin/checkmail-sw.js'); ?>", {
413 scope: "<?php echo esc_attr($plugin_dir_url . 'assets/js/admin/'); ?>"
414 })
415 .then((registration) => {
416 console.log("Service Worker registered:");
417 })
418 .catch((error) => {
419 console.error("Service Worker registration failed:", error);
420 });
421 }
422 </script>
423 <?php
424 $this->pwaforwp_swhtml_init_firebase_js();
425 }
426
427 public function pwaforwp_swhtml_init_firebase_js($action = null)
428 {
429 //Dummy file to work FCM perfectly
430 if ( isset( $_SERVER['DOCUMENT_ROOT'] ) ) {
431 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized , WordPress.Security.ValidatedSanitizedInput.MissingUnslash
432 $wppath = $_SERVER['DOCUMENT_ROOT']."/";
433 $wppath = apply_filters("checkmail_file_creation_path", $wppath);
434
435 $pn_sw_js = $wppath . "firebase-messaging-sw.js";
436 $swjsContent = '';
437 $status = $this->checkmail_write_a_file($pn_sw_js, $swjsContent, $action);
438 return $status;
439 }
440 }
441
442 public function checkmail_write_a_file($path, $content, $action = null)
443 {
444
445 global $wp_filesystem;
446
447 // Initialize the WP_Filesystem global
448 if (! function_exists('WP_Filesystem')) {
449 require_once(ABSPATH . 'wp-admin/includes/file.php');
450 }
451
452 WP_Filesystem();
453
454 $writestatus = '';
455
456 if ($wp_filesystem->exists($path)) {
457 $writestatus = wp_delete_file($path);
458 }
459
460 $check_email = wpchill_check_email();
461 $plugin_dir_url = plugin_dir_url($check_email->get_plugin_file());
462 $file_path = $plugin_dir_url . 'assets/js/admin/checkmail-sw.js';
463 $content = file_get_contents($file_path);
464 if (! $action && $content) {
465 $writestatus = $wp_filesystem->put_contents($path, $content, FS_CHMOD_FILE);
466 }
467
468 if ($writestatus) {
469 return true;
470 }
471 return false;
472 }
473
474 public function json_settings()
475 {
476 if (is_multisite()) {
477 $link = get_site_url();
478 } else {
479 $link = home_url();
480 }
481 $messageConfig = '';
482 $settings = array(
483 'nonce' => wp_create_nonce("checkmail_notification"),
484 'pn_config' => $messageConfig,
485 "swsource" => esc_url_raw(trailingslashit($link) . "?checkmail_notification_sw=1"),
486 "scope" => esc_url_raw(trailingslashit($link)),
487 "ajax_url" => esc_url_raw(admin_url('admin-ajax.php'))
488 );
489 return $settings;
490 }
491
492 public function sendRequest($suffixUrl, $data, $method="post"){
493 $notificationServerUrl = 'https://pushnotifications.io/api/';
494 if($method==='post'){
495 $url = $notificationServerUrl.$suffixUrl;
496 $postdata = array('body'=> $data);
497 $remoteResponse = wp_remote_post($url, $postdata);
498 }
499
500 if( is_wp_error( $remoteResponse ) ){
501 if(!empty($remoteResponse->get_error_message()) ) {
502 $error_message = strtolower($remoteResponse->get_error_message());
503 $error_pos = strpos($error_message, 'operation timed out');
504 if($error_pos !== false){
505 $message = __('Request timed out, please try again','check-email');
506 }else{
507 $message = esc_html($remoteResponse->get_error_message());
508 }
509 }else{
510 $message = __("could not connect to server",'check-email');
511 }
512 $remoteData = array('status'=>401, "response"=>$message);
513
514 }else{
515 $remoteData = wp_remote_retrieve_body($remoteResponse);
516 $remoteData = json_decode($remoteData, true);
517 }
518 return $remoteData;
519 }
520
521 function serve_firebase_sw() {
522 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'])) {
523 return;
524 }
525 $check_email = wpchill_check_email();
526 $plugin_dir_url = plugin_dir_url($check_email->get_plugin_file());
527 header("Content-Type: application/javascript");
528 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_readfile
529 readfile($plugin_dir_url . 'assets/js/admin/checkmail-sw.js');
530 exit;
531 }
532
533 }
534