Repository navigation
Expand file tree
/
Copy pathjw-core.php
More file actions
1747 lines (1532 loc) · 59.5 KB
/
Copy pathjw-core.php
File metadata and controls
1747 lines (1532 loc) · 59.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Plugin Name: JW Core
* Description: Core functions for JW themes and plugins.
* Version: 1.0.3
* Author: Juzaweb
* Author URI: https://juzaweb.com
* Text Domain: jw-core
*
* @package jw_core
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
require_once __DIR__ . '/inc/helpers.php';
/**
* Lazyload images with lazysizes
*/
function jw_core_lazyload_attachment_image_attributes( $attr ) {
// Only apply lazysizes on the frontend, not in the admin dashboard (e.g., Media Library)
if ( is_admin() || is_feed() || is_preview() || get_option( 'jw_core_enable_lazyload', '1' ) !== '1' ) {
return $attr;
}
// Skip WooCommerce gallery images and specified classes
if ( isset( $attr['class'] ) && (
strpos( $attr['class'], 'woocommerce-product-gallery__image' ) !== false ||
strpos( $attr['class'], 'skip-lazy' ) !== false ||
strpos( $attr['class'], 'no-lazyload' ) !== false
) ) {
return $attr;
}
if ( isset( $attr['src'] ) ) {
$attr['data-src'] = $attr['src'];
$attr['src'] = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
}
if ( isset( $attr['srcset'] ) ) {
$attr['data-srcset'] = $attr['srcset'];
unset( $attr['srcset'] );
}
if ( isset( $attr['sizes'] ) ) {
$attr['data-sizes'] = $attr['sizes'];
unset( $attr['sizes'] );
}
// Add lazysizes class
$attr['class'] = isset( $attr['class'] ) ? $attr['class'] . ' lazyload' : 'lazyload';
return $attr;
}
add_filter( 'wp_get_attachment_image_attributes', 'jw_core_lazyload_attachment_image_attributes' );
/**
* Lazyload images in post content
*/
function jw_core_lazyload_content_images( $content ) {
if ( is_admin() || is_feed() || is_preview() || get_option( 'jw_core_enable_lazyload', '1' ) !== '1' ) {
return $content;
}
if ( function_exists( 'wp_filter_content_tags' ) ) {
$content = preg_replace_callback(
'/<img([^>]+)>/i',
function ( $matches ) {
$img_tag = $matches[0];
$attributes = $matches[1];
// Skip if already has data-src or exclusion classes
if ( stripos( $attributes, 'data-src' ) !== false ||
stripos( $attributes, 'skip-lazy' ) !== false ||
stripos( $attributes, 'no-lazyload' ) !== false ||
stripos( $attributes, 'woocommerce-product-gallery__image' ) !== false
) {
return $img_tag;
}
// Replace src with placeholder and data-src
$img_tag = preg_replace( '/\bsrc=(["\'])(.*?)\1/i', 'src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" data-src=$1$2$1', $img_tag );
// Replace srcset and sizes
$img_tag = preg_replace( '/\b(srcset|sizes)=/i', 'data-$1=', $img_tag );
// Add lazyload class
if ( preg_match( '/\bclass=(["\'])(.*?)\1/i', $img_tag, $class_matches ) ) {
$img_tag = preg_replace( '/\bclass=(["\'])(.*?)\1/i', 'class=$1$2 lazyload$1', $img_tag );
} else {
$img_tag = str_replace( '<img', '<img class="lazyload"', $img_tag );
}
return $img_tag;
},
$content
);
}
return $content;
}
add_filter( 'the_content', 'jw_core_lazyload_content_images', 99 );
/**
* Enqueue scripts for JW Core
*/
function jw_core_enqueue_scripts() {
// Enqueue lazysizes script
if ( get_option( 'jw_core_enable_lazyload', '1' ) === '1' ) {
wp_enqueue_script( 'lazysizes', plugin_dir_url( __FILE__ ) . 'assets/js/lazysizes.min.js', array(), '5.3.2', false );
}
// Enqueue reCAPTCHA script if enabled
if ( get_option( 'jw_core_enable_recaptcha', '0' ) === '1' ) {
$site_key = get_option( 'jw_core_recaptcha_site_key', '' );
if ( ! empty( $site_key ) ) {
wp_enqueue_script( 'google-recaptcha', 'https://www.google.com/recaptcha/api.js', array(), null, false );
wp_add_inline_script( 'google-recaptcha', 'window.jwCoreRecaptchaSiteKey = "' . esc_js( $site_key ) . '";', 'before' );
}
}
}
add_action( 'wp_enqueue_scripts', 'jw_core_enqueue_scripts' );
/**
* Output FCM initialization script in footer
*/
function jw_core_get_fcm_option( $option_name, $default = '' ) {
if ( is_multisite() ) {
return get_site_option( $option_name, $default );
}
return get_option( $option_name, $default );
}
/**
* Output FCM initialization script in footer
*/
function jw_core_fcm_footer_script() {
$enable_fcm = jw_core_get_fcm_option( 'jw_core_enable_fcm', '0' );
$fcm_config = jw_core_get_fcm_option( 'jw_core_fcm_config', '' );
if ( $enable_fcm !== '1' || empty( $fcm_config ) ) {
return;
}
$ajax_url = admin_url( 'admin-ajax.php' );
$fcm_nonce = wp_create_nonce( 'jw_core_fcm_nonce' );
?>
<script src="https://www.gstatic.com/firebasejs/8.10.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.10.1/firebase-messaging.js"></script>
<script>
(function() {
if (!firebase.apps.length) {
const firebaseConfig = JSON.parse('{<?php echo wp_kses_post( $fcm_config ); ?>}');
firebase.initializeApp(firebaseConfig);
}
const messaging = firebase.messaging();
// Request permission and get token
if ('Notification' in window) {
Notification.requestPermission().then(function(permission) {
if (permission === 'granted') {
console.log('Notification permission granted.');
messaging.getToken().then((currentToken) => {
if (currentToken) {
fetch('<?php echo esc_url( $ajax_url ); ?>', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
action: 'jw_core_fcm_save_token',
token: currentToken,
nonce: '<?php echo esc_js( $fcm_nonce ); ?>'
})
}).catch(err => console.error('Error sending token to server', err));
} else {
console.log('No Instance ID token available. Request permission to generate one.');
}
}).catch((err) => {
console.log('An error occurred while retrieving token. ', err);
});
} else {
console.log('Unable to get permission to notify.');
}
});
}
// Handle foreground messages
messaging.onMessage((payload) => {
console.log('Message received in foreground. ', payload);
fetch('<?php echo esc_url( $ajax_url ); ?>', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
action: 'jw_core_fcm_message',
payload: JSON.stringify(payload),
nonce: '<?php echo esc_js( $fcm_nonce ); ?>'
})
}).catch(err => console.error('Error forwarding foreground message to server', err));
});
})();
</script>
<?php
}
add_action( 'wp_footer', 'jw_core_fcm_footer_script', 100 );
/**
* Output Google Analytics script in header
*/
function jw_core_google_analytics_script() {
$ga_id = get_option( 'jw_core_google_analytics_id', '' );
if ( empty( $ga_id ) ) {
return;
}
?>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=<?php echo esc_attr( $ga_id ); ?>"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '<?php echo esc_attr( $ga_id ); ?>');
</script>
<?php
}
add_action( 'wp_head', 'jw_core_google_analytics_script', 20 );
/**
* Output Custom Header Code.
*/
function jw_core_custom_header_code_output() {
$custom_code = get_option( 'jw_core_custom_header_code', '' );
if ( ! empty( $custom_code ) ) {
echo $custom_code; // Output directly (intended to be raw HTML/JS/CSS)
}
}
add_action( 'wp_head', 'jw_core_custom_header_code_output', 20 );
/**
* Settings Page
*/
function jw_core_admin_menu() {
add_menu_page(
__( 'Juzaweb Settings', 'jw-core' ),
__( 'Juzaweb', 'jw-core' ),
'manage_options',
'jw-core-settings',
'jw_core_settings_page',
'dashicons-admin-generic',
99
);
add_submenu_page(
'jw-core-settings',
__( 'Juzaweb Settings', 'jw-core' ),
__( 'Settings', 'jw-core' ),
'manage_options',
'jw-core-settings',
'jw_core_settings_page'
);
add_submenu_page(
'jw-core-settings',
__( 'SMTP Settings', 'jw-core' ),
__( 'SMTP Setup', 'jw-core' ),
'manage_options',
'jw-core-smtp',
'jw_core_smtp_settings_page'
);
}
add_action( 'admin_menu', 'jw_core_admin_menu' );
function jw_core_network_admin_menu() {
add_menu_page(
__( 'Juzaweb Settings', 'jw-core' ),
__( 'Juzaweb', 'jw-core' ),
'manage_network_options',
'jw-core-network-settings',
'jw_core_network_settings_page',
'dashicons-admin-generic',
99
);
}
add_action( 'network_admin_menu', 'jw_core_network_admin_menu' );
/**
* Sanitize SMTP Password to allow leaving blank without overwriting.
*
* @param string $input The new password.
* @return string
*/
function jw_core_sanitize_smtp_password( $input ) {
if ( empty( $input ) ) {
return get_option( 'jw_core_smtp_password' );
}
return sanitize_text_field( $input );
}
function jw_core_register_settings() {
register_setting( 'jw_core_settings_group', 'jw_core_enable_lazyload' );
// SMTP Settings
register_setting( 'jw_core_smtp_settings_group', 'jw_core_smtp_enable' );
register_setting( 'jw_core_smtp_settings_group', 'jw_core_smtp_host' );
register_setting( 'jw_core_smtp_settings_group', 'jw_core_smtp_port' );
register_setting( 'jw_core_smtp_settings_group', 'jw_core_smtp_encryption' );
register_setting( 'jw_core_smtp_settings_group', 'jw_core_smtp_auth' );
register_setting( 'jw_core_smtp_settings_group', 'jw_core_smtp_username' );
register_setting( 'jw_core_smtp_settings_group', 'jw_core_smtp_password', array( 'sanitize_callback' => 'jw_core_sanitize_smtp_password' ) );
register_setting( 'jw_core_smtp_settings_group', 'jw_core_smtp_from_email', array( 'sanitize_callback' => 'sanitize_email' ) );
register_setting( 'jw_core_smtp_settings_group', 'jw_core_smtp_from_name', array( 'sanitize_callback' => 'sanitize_text_field' ) );
register_setting( 'jw_core_settings_group', 'jw_core_enable_fcm' );
register_setting( 'jw_core_settings_group', 'jw_core_enable_email_verification' );
register_setting( 'jw_core_settings_group', 'jw_core_enable_recaptcha' );
register_setting( 'jw_core_settings_group', 'jw_core_custom_header_code' );
register_setting( 'jw_core_settings_group', 'jw_core_recaptcha_site_key', array( 'sanitize_callback' => 'sanitize_text_field' ) );
register_setting( 'jw_core_settings_group', 'jw_core_recaptcha_secret_key', array( 'sanitize_callback' => 'sanitize_text_field' ) );
register_setting( 'jw_core_settings_group', 'jw_core_show_admin_bar_user' );
register_setting( 'jw_core_settings_group', 'jw_core_show_admin_bar_admin' );
register_setting(
'jw_core_settings_group',
'jw_core_fcm_config',
array(
'sanitize_callback' => 'jw_core_sanitize_fcm_config',
)
);
register_setting(
'jw_core_settings_group',
'jw_core_google_analytics_id',
array(
'sanitize_callback' => 'sanitize_text_field',
)
);
}
add_action( 'admin_init', 'jw_core_register_settings' );
/**
* Sanitize and validate FCM config JSON.
*/
function jw_core_sanitize_fcm_config( $input ) {
if ( empty( trim( $input ) ) ) {
return '';
}
$unslashed_input = wp_unslash( $input );
// Try parsing as JSON by wrapping it in { }
$json_test = '{' . $unslashed_input . '}';
$decoded = json_decode( $json_test );
if ( json_last_error() !== JSON_ERROR_NONE ) {
add_settings_error(
'jw_core_fcm_config',
'invalid_fcm_config',
__( 'Invalid Firebase Config format. It must be valid JSON content inside the braces.', 'jw-core' )
);
return get_option( 'jw_core_fcm_config', '' );
}
return $unslashed_input;
}
/**
* Handle writing/deleting firebase-messaging-sw.js
*/
function jw_core_handle_fcm_sw_file( $old_value, $new_value ) {
$enable_fcm = jw_core_get_fcm_option( 'jw_core_enable_fcm', '0' );
$fcm_config = jw_core_get_fcm_option( 'jw_core_fcm_config', '' );
// Check which option is being saved based on the current action hook
$current_filter = current_filter();
if ( strpos( $current_filter, 'jw_core_enable_fcm' ) !== false ) {
$enable_fcm = $new_value;
} elseif ( strpos( $current_filter, 'jw_core_fcm_config' ) !== false ) {
$fcm_config = $new_value;
}
$file_path = ABSPATH . 'firebase-messaging-sw.js';
$ajax_url = admin_url( 'admin-ajax.php' );
if ( $enable_fcm === '1' && ! empty( $fcm_config ) ) {
$sw_content = <<<EOD
importScripts('https://www.gstatic.com/firebasejs/8.10.1/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/8.10.1/firebase-messaging.js');
firebase.initializeApp({
{$fcm_config}
});
const messaging = firebase.messaging();
messaging.onBackgroundMessage((payload) => {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Forward to server
fetch('{$ajax_url}', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
action: 'jw_core_fcm_message',
payload: JSON.stringify(payload)
})
});
});
EOD;
file_put_contents( $file_path, $sw_content );
} elseif ( file_exists( $file_path ) ) {
unlink( $file_path );
}
}
add_action( 'update_option_jw_core_enable_fcm', 'jw_core_handle_fcm_sw_file', 10, 2 );
add_action( 'add_option_jw_core_enable_fcm', 'jw_core_handle_fcm_sw_file', 10, 2 );
add_action( 'update_option_jw_core_fcm_config', 'jw_core_handle_fcm_sw_file', 10, 2 );
add_action( 'add_option_jw_core_fcm_config', 'jw_core_handle_fcm_sw_file', 10, 2 );
add_action( 'update_site_option_jw_core_enable_fcm', 'jw_core_handle_fcm_sw_file', 10, 2 );
add_action( 'add_site_option_jw_core_enable_fcm', 'jw_core_handle_fcm_sw_file', 10, 2 );
add_action( 'update_site_option_jw_core_fcm_config', 'jw_core_handle_fcm_sw_file', 10, 2 );
add_action( 'add_site_option_jw_core_fcm_config', 'jw_core_handle_fcm_sw_file', 10, 2 );
/**
* Handle FCM message AJAX request
*/
function jw_core_fcm_message_handler() {
check_ajax_referer( 'jw_core_fcm_nonce', 'nonce' );
$payload = isset( $_POST['payload'] ) ? json_decode( wp_unslash( $_POST['payload'] ), true ) : array();
if ( ! empty( $payload ) ) {
// Sanitize payload data
$sanitized_payload = array_map( function( $item ) {
return is_array( $item ) ? $item : sanitize_text_field( $item );
}, $payload );
do_action( 'jw_fcm_message_received', $sanitized_payload );
}
wp_send_json_success();
}
add_action( 'wp_ajax_jw_core_fcm_message', 'jw_core_fcm_message_handler' );
add_action( 'wp_ajax_nopriv_jw_core_fcm_message', 'jw_core_fcm_message_handler' );
/**
* Handle FCM token saving AJAX request
*/
function jw_core_fcm_save_token_handler() {
check_ajax_referer( 'jw_core_fcm_nonce', 'nonce' );
$token = isset( $_POST['token'] ) ? sanitize_text_field( wp_unslash( $_POST['token'] ) ) : '';
if ( ! empty( $token ) ) {
global $wpdb;
$table_name = $wpdb->prefix . 'jw_notification_subscribers';
$ip = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '127.0.0.1';
$wpdb->replace(
$table_name,
array(
'driver' => 'fcm',
'identifier' => $ip,
'token' => $token,
),
array(
'%s',
'%s',
'%s',
)
);
do_action( 'jw_fcm_token_received', $token );
}
wp_send_json_success();
}
add_action( 'wp_ajax_jw_core_fcm_save_token', 'jw_core_fcm_save_token_handler' );
add_action( 'wp_ajax_nopriv_jw_core_fcm_save_token', 'jw_core_fcm_save_token_handler' );
function jw_core_network_settings_page() {
if ( ! current_user_can( 'manage_network_options' ) ) {
return;
}
if ( isset( $_POST['jw_core_network_settings_nonce'] ) && wp_verify_nonce( wp_unslash( $_POST['jw_core_network_settings_nonce'] ), 'jw_core_network_settings_action' ) ) {
$enable_fcm = isset( $_POST['jw_core_enable_fcm'] ) ? '1' : '0';
$fcm_config = isset( $_POST['jw_core_fcm_config'] ) ? jw_core_sanitize_fcm_config( wp_unslash( $_POST['jw_core_fcm_config'] ) ) : '';
update_site_option( 'jw_core_enable_fcm', $enable_fcm );
update_site_option( 'jw_core_fcm_config', $fcm_config );
echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'Settings saved.', 'jw-core' ) . '</p></div>';
}
$default_fcm_config = <<<'EOD'
"apiKey": "YOUR_API_KEY",
"projectId": "YOUR_PROJECT_ID",
"messagingSenderId": "YOUR_SENDER_ID",
"appId": "YOUR_APP_ID"
EOD;
$fcm_config = get_site_option( 'jw_core_fcm_config', $default_fcm_config );
?>
<div class="wrap">
<h1><?php esc_html_e( 'Juzaweb Network Settings', 'jw-core' ); ?></h1>
<form method="post" action="">
<?php wp_nonce_field( 'jw_core_network_settings_action', 'jw_core_network_settings_nonce' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><?php esc_html_e( 'Enable Firebase Cloud Messaging (FCM)', 'jw-core' ); ?></th>
<td>
<input type="hidden" name="jw_core_enable_fcm" value="0" />
<input type="checkbox" name="jw_core_enable_fcm" value="1" <?php checked( '1', get_site_option( 'jw_core_enable_fcm', '0' ) ); ?> />
<p class="description"><?php esc_html_e( 'Turn on/off FCM integration. When enabled, the service worker with the config below will be generated in the root folder.', 'jw-core' ); ?></p>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php esc_html_e( 'Firebase Config (JSON format)', 'jw-core' ); ?></th>
<td>
<textarea name="jw_core_fcm_config" rows="10" class="large-text code"><?php echo esc_textarea( $fcm_config ); ?></textarea>
<p class="description">
<?php
printf(
/* translators: %s: Code snippet */
esc_html__( 'Enter the contents inside %s here. Do not include the outer curly braces.', 'jw-core' ),
'<code>firebase.initializeApp({ ... })</code>'
);
?>
</p>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
function jw_core_smtp_settings_page() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
?>
<div class="wrap">
<h1><?php esc_html_e( 'SMTP Settings', 'jw-core' ); ?></h1>
<form method="post" action="options.php">
<?php settings_fields( 'jw_core_smtp_settings_group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><?php esc_html_e( 'Enable SMTP', 'jw-core' ); ?></th>
<td>
<input type="hidden" name="jw_core_smtp_enable" value="0" />
<input type="checkbox" name="jw_core_smtp_enable" value="1" <?php checked( '1', get_option( 'jw_core_smtp_enable', '0' ) ); ?> />
</td>
</tr>
<tr valign="top">
<th scope="row"><?php esc_html_e( 'SMTP Host', 'jw-core' ); ?></th>
<td><input type="text" name="jw_core_smtp_host" value="<?php echo esc_attr( get_option( 'jw_core_smtp_host' ) ); ?>" class="regular-text" /></td>
</tr>
<tr valign="top">
<th scope="row"><?php esc_html_e( 'SMTP Port', 'jw-core' ); ?></th>
<td><input type="number" name="jw_core_smtp_port" value="<?php echo esc_attr( get_option( 'jw_core_smtp_port', '587' ) ); ?>" class="small-text" /></td>
</tr>
<tr valign="top">
<th scope="row"><?php esc_html_e( 'Encryption', 'jw-core' ); ?></th>
<td>
<select name="jw_core_smtp_encryption">
<option value="none" <?php selected( 'none', get_option( 'jw_core_smtp_encryption', 'none' ) ); ?>><?php esc_html_e( 'None', 'jw-core' ); ?></option>
<option value="ssl" <?php selected( 'ssl', get_option( 'jw_core_smtp_encryption', 'none' ) ); ?>><?php esc_html_e( 'SSL', 'jw-core' ); ?></option>
<option value="tls" <?php selected( 'tls', get_option( 'jw_core_smtp_encryption', 'none' ) ); ?>><?php esc_html_e( 'TLS', 'jw-core' ); ?></option>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php esc_html_e( 'SMTP Authentication', 'jw-core' ); ?></th>
<td>
<input type="hidden" name="jw_core_smtp_auth" value="0" />
<input type="checkbox" name="jw_core_smtp_auth" value="1" <?php checked( '1', get_option( 'jw_core_smtp_auth', '1' ) ); ?> />
</td>
</tr>
<tr valign="top">
<th scope="row"><?php esc_html_e( 'SMTP Username', 'jw-core' ); ?></th>
<td><input type="text" name="jw_core_smtp_username" value="<?php echo esc_attr( get_option( 'jw_core_smtp_username' ) ); ?>" class="regular-text" /></td>
</tr>
<tr valign="top">
<th scope="row"><?php esc_html_e( 'SMTP Password', 'jw-core' ); ?></th>
<td>
<input type="password" name="jw_core_smtp_password" value="" class="regular-text" placeholder="<?php echo get_option( 'jw_core_smtp_password' ) ? '********' : ''; ?>" />
<p class="description"><?php esc_html_e( 'Leave blank to keep the current password.', 'jw-core' ); ?></p>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php esc_html_e( 'From Email', 'jw-core' ); ?></th>
<td><input type="email" name="jw_core_smtp_from_email" value="<?php echo esc_attr( get_option( 'jw_core_smtp_from_email' ) ); ?>" class="regular-text" /></td>
</tr>
<tr valign="top">
<th scope="row"><?php esc_html_e( 'From Name', 'jw-core' ); ?></th>
<td><input type="text" name="jw_core_smtp_from_name" value="<?php echo esc_attr( get_option( 'jw_core_smtp_from_name' ) ); ?>" class="regular-text" /></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
<hr>
<h2><?php esc_html_e( 'Test Send Email', 'jw-core' ); ?></h2>
<p><?php esc_html_e( 'Enter an email address to send a test email. Make sure to save the settings above first.', 'jw-core' ); ?></p>
<table class="form-table">
<tr valign="top">
<th scope="row"><?php esc_html_e( 'Test Email Address', 'jw-core' ); ?></th>
<td>
<input type="email" id="jw-core-test-email" class="regular-text" />
<button type="button" class="button button-secondary" id="jw-core-test-email-btn"><?php esc_html_e( 'Send Test Email', 'jw-core' ); ?></button>
<span class="spinner" id="jw-core-test-email-spinner"></span>
<p class="description" id="jw-core-test-email-msg" style="margin-top: 5px;"></p>
</td>
</tr>
</table>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#jw-core-test-email-btn').on('click', function() {
var email = $('#jw-core-test-email').val();
var btn = $(this);
var spinner = $('#jw-core-test-email-spinner');
var msg = $('#jw-core-test-email-msg');
if (!email) {
alert('<?php esc_html_e( 'Please enter an email address.', 'jw-core' ); ?>');
return;
}
btn.prop('disabled', true);
spinner.addClass('is-active');
msg.text('<?php esc_html_e( 'Sending...', 'jw-core' ); ?>').css('color', '');
$.ajax({
url: ajaxurl,
type: 'POST',
data: {
action: 'jw_core_test_smtp',
email: email,
nonce: '<?php echo wp_create_nonce( 'jw_core_test_smtp_nonce' ); ?>'
},
success: function(response) {
btn.prop('disabled', false);
spinner.removeClass('is-active');
if (response.success) {
msg.text(response.data.message).css('color', 'green');
} else {
msg.text(response.data.message).css('color', 'red');
}
},
error: function() {
btn.prop('disabled', false);
spinner.removeClass('is-active');
msg.text('<?php esc_html_e( 'An error occurred during the AJAX request.', 'jw-core' ); ?>').css('color', 'red');
}
});
});
});
</script>
</div>
<?php
}
function jw_core_settings_page() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$default_fcm_config = <<<'EOD'
"apiKey": "YOUR_API_KEY",
"projectId": "YOUR_PROJECT_ID",
"messagingSenderId": "YOUR_SENDER_ID",
"appId": "YOUR_APP_ID"
EOD;
$fcm_config = get_option( 'jw_core_fcm_config', $default_fcm_config );
?>
<div class="wrap">
<h1><?php esc_html_e( 'Juzaweb Settings', 'jw-core' ); ?></h1>
<form method="post" action="options.php">
<?php settings_fields( 'jw_core_settings_group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><?php esc_html_e( 'Enable Image Lazyload (Lazysizes)', 'jw-core' ); ?></th>
<td>
<input type="hidden" name="jw_core_enable_lazyload" value="0" />
<input type="checkbox" name="jw_core_enable_lazyload" value="1" <?php checked( '1', get_option( 'jw_core_enable_lazyload', '1' ) ); ?> />
<p class="description"><?php esc_html_e( 'Turn on/off lazy loading for images on the frontend.', 'jw-core' ); ?></p>
</td>
</tr>
<?php if ( ! is_multisite() ) : ?>
<tr valign="top">
<th scope="row"><?php esc_html_e( 'Enable Firebase Cloud Messaging (FCM)', 'jw-core' ); ?></th>
<td>
<input type="hidden" name="jw_core_enable_fcm" value="0" />
<input type="checkbox" name="jw_core_enable_fcm" value="1" <?php checked( '1', get_option( 'jw_core_enable_fcm', '0' ) ); ?> />
<p class="description"><?php esc_html_e( 'Turn on/off FCM integration. When enabled, the service worker with the config below will be generated in the root folder.', 'jw-core' ); ?></p>
</td>
</tr>
<?php endif; ?>
<tr valign="top">
<th scope="row"><?php esc_html_e( 'Enable Email Verification', 'jw-core' ); ?></th>
<td>
<input type="hidden" name="jw_core_enable_email_verification" value="0" />
<input type="checkbox" name="jw_core_enable_email_verification" value="1" <?php checked( '1', get_option( 'jw_core_enable_email_verification', '0' ) ); ?> />
<p class="description"><?php esc_html_e( 'If enabled, users must verify their email after registration to access features.', 'jw-core' ); ?></p>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php esc_html_e( 'Enable reCAPTCHA v2 Invisible', 'jw-core' ); ?></th>
<td>
<input type="hidden" name="jw_core_enable_recaptcha" value="0" />
<input type="checkbox" name="jw_core_enable_recaptcha" value="1" <?php checked( '1', get_option( 'jw_core_enable_recaptcha', '0' ) ); ?> />
<p class="description"><?php esc_html_e( 'If enabled, users must pass reCAPTCHA to subscribe email.', 'jw-core' ); ?></p>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php esc_html_e( 'reCAPTCHA Site Key', 'jw-core' ); ?></th>
<td>
<input type="text" name="jw_core_recaptcha_site_key" value="<?php echo esc_attr( get_option( 'jw_core_recaptcha_site_key', '' ) ); ?>" class="regular-text" />
<p class="description"><?php esc_html_e( 'Enter your reCAPTCHA v2 Invisible Site Key.', 'jw-core' ); ?></p>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php esc_html_e( 'reCAPTCHA Secret Key', 'jw-core' ); ?></th>
<td>
<input type="text" name="jw_core_recaptcha_secret_key" value="<?php echo esc_attr( get_option( 'jw_core_recaptcha_secret_key', '' ) ); ?>" class="regular-text" />
<p class="description"><?php esc_html_e( 'Enter your reCAPTCHA v2 Invisible Secret Key.', 'jw-core' ); ?></p>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php esc_html_e( 'Show Admin Bar for Users', 'jw-core' ); ?></th>
<td>
<input type="hidden" name="jw_core_show_admin_bar_user" value="0" />
<input type="checkbox" name="jw_core_show_admin_bar_user" value="1" <?php checked( '1', get_option( 'jw_core_show_admin_bar_user', '0' ) ); ?> />
<p class="description"><?php esc_html_e( 'Turn on/off admin bar for regular users on the frontend.', 'jw-core' ); ?></p>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php esc_html_e( 'Show Admin Bar for Admins', 'jw-core' ); ?></th>
<td>
<input type="hidden" name="jw_core_show_admin_bar_admin" value="0" />
<input type="checkbox" name="jw_core_show_admin_bar_admin" value="1" <?php checked( '1', get_option( 'jw_core_show_admin_bar_admin', '1' ) ); ?> />
<p class="description"><?php esc_html_e( 'Turn on/off admin bar for administrators on the frontend.', 'jw-core' ); ?></p>
</td>
</tr>
<?php if ( ! is_multisite() ) : ?>
<tr valign="top">
<th scope="row"><?php esc_html_e( 'Firebase Config (JSON format)', 'jw-core' ); ?></th>
<td>
<textarea name="jw_core_fcm_config" rows="10" class="large-text code"><?php echo esc_textarea( $fcm_config ); ?></textarea>
<p class="description">
<?php
printf(
/* translators: %s: Code snippet */
esc_html__( 'Enter the contents inside %s here. Do not include the outer curly braces.', 'jw-core' ),
'<code>firebase.initializeApp({ ... })</code>'
);
?>
</p>
</td>
</tr>
<?php endif; ?>
<tr valign="top">
<th scope="row"><?php esc_html_e( 'Google Analytics ID', 'jw-core' ); ?></th>
<td>
<input type="text" name="jw_core_google_analytics_id" value="<?php echo esc_attr( get_option( 'jw_core_google_analytics_id', '' ) ); ?>" class="regular-text" />
<p class="description"><?php esc_html_e( 'Enter your Google Analytics Measurement ID (e.g., G-XXXXXXXXXX).', 'jw-core' ); ?></p>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php esc_html_e( 'Custom Header Code', 'jw-core' ); ?></th>
<td>
<textarea name="jw_core_custom_header_code" rows="10" class="large-text code"><?php echo esc_textarea( get_option( 'jw_core_custom_header_code', '' ) ); ?></textarea>
<p class="description"><?php esc_html_e( 'Enter your custom code (HTML/JS/CSS) to be injected into the theme\'s <head>.', 'jw-core' ); ?></p>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php esc_html_e( 'Create Missing Template Pages', 'jw-core' ); ?></th>
<td>
<button type="button" class="button button-secondary" id="jw-core-create-pages-btn">
<?php esc_html_e( 'Create Pages', 'jw-core' ); ?>
</button>
<span class="spinner" id="jw-core-create-pages-spinner"></span>
<p class="description" id="jw-core-create-pages-msg" style="margin-top: 5px;"></p>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#jw-core-create-pages-btn').on('click', function() {
var btn = $(this);
var spinner = $('#jw-core-create-pages-spinner');
var msg = $('#jw-core-create-pages-msg');
btn.prop('disabled', true);
spinner.addClass('is-active');
msg.text('Creating pages...').css('color', '');
$.ajax({
url: ajaxurl,
type: 'POST',
data: {
action: 'jw_core_create_template_pages',
_ajax_nonce: '<?php echo wp_create_nonce( 'jw_core_create_pages_nonce' ); ?>'
},
success: function(response) {
if (response.success) {
msg.text(response.data.message).css('color', 'green');
} else {
msg.text(response.data.message || 'An error occurred.').css('color', 'red');
}
},
error: function() {
msg.text('An error occurred.').css('color', 'red');
},
complete: function() {
btn.prop('disabled', false);
spinner.removeClass('is-active');
}
});
});
});
</script>
<?php
}
/**
* Handle AJAX request to create missing template pages.
*/
function jw_core_create_template_pages_ajax() {
// phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( ! isset( $_POST['_ajax_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['_ajax_nonce'] ), 'jw_core_create_pages_nonce' ) ) {
wp_send_json_error( array( 'message' => __( 'Security check failed.', 'jw-core' ) ) );
return;
}
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( array( 'message' => __( 'You do not have permission to do this.', 'jw-core' ) ) );
return;
}
$templates = wp_get_theme()->get_page_templates();
if ( empty( $templates ) ) {
wp_send_json_error( array( 'message' => __( 'No templates found in the active theme.', 'jw-core' ) ) );
return;
}
$created = 0;
$existing = 0;
foreach ( $templates as $file => $name ) {
$pages = get_pages(
array(
'meta_key' => '_wp_page_template',
'meta_value' => $file,
'number' => 1,
)
);
if ( ! empty( $pages ) ) {
++$existing;
continue;
}
$post_content = '';
if ( stripos( $file, 'contact' ) !== false || stripos( $name, 'contact' ) !== false ) {
$cf7_forms = get_posts(
array(
'post_type' => 'wpcf7_contact_form',
'posts_per_page' => 1,
'post_status' => 'publish',
)
);
if ( ! empty( $cf7_forms ) ) {
$cf7_form = $cf7_forms[0];
$post_content = sprintf( '[contact-form-7 id="%1$d" title="%2$s"]', $cf7_form->ID, $cf7_form->post_title );
}
}
$page_id = wp_insert_post(
array(
'post_title' => sanitize_text_field( $name ),
'post_status' => 'publish',
'post_type' => 'page',
'post_content' => $post_content,
'page_template' => sanitize_text_field( $file ),
)
);
if ( ! is_wp_error( $page_id ) ) {
++$created;
if ( stripos( $file, 'home' ) !== false || stripos( $name, 'home' ) !== false ) {
update_option( 'show_on_front', 'page' );
update_option( 'page_on_front', $page_id );
}
do_action( 'jw_core_after_create_template_page', $page_id, $file, $name );
}
}
/* translators: 1: number of created pages, 2: number of existing pages */
$message = sprintf( __( 'Created %1$d new pages. %2$d pages already existed.', 'jw-core' ), $created, $existing );
wp_send_json_success( array( 'message' => $message ) );
}
add_action( 'wp_ajax_jw_core_create_template_pages', 'jw_core_create_template_pages_ajax' );
/**
* Create JW Notifications Table
*/
function jw_core_create_notification_table() {
global $wpdb;
$table_name = $wpdb->prefix . 'jw_notifications';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id bigint(20) NOT NULL AUTO_INCREMENT,
title varchar(255) NOT NULL,
driver varchar(50) NOT NULL,
body text NOT NULL,
image varchar(255) DEFAULT '' NOT NULL,
status varchar(20) DEFAULT 'pending' NOT NULL,
created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
$table_subscribers = $wpdb->prefix . 'jw_notification_subscribers';
$sql_subscribers = "CREATE TABLE $table_subscribers (
id bigint(20) NOT NULL AUTO_INCREMENT,
driver varchar(50) NOT NULL,
identifier varchar(255) NOT NULL,
token text DEFAULT NULL,
created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY driver_identifier (driver, identifier)
) $charset_collate;";
$table_queue = $wpdb->prefix . 'jw_notification_queue';
$sql_queue = "CREATE TABLE $table_queue (
id bigint(20) NOT NULL AUTO_INCREMENT,
notification_id bigint(20) NOT NULL,
subscriber_id bigint(20) NOT NULL,
status varchar(20) DEFAULT 'pending' NOT NULL,
created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (id),
KEY notification_id (notification_id),
KEY status (status)
) $charset_collate;";
$table_user_notifications = $wpdb->prefix . 'jw_user_notifications';
$sql_user_notifications = "CREATE TABLE $table_user_notifications (
id bigint(20) NOT NULL AUTO_INCREMENT,
user_id bigint(20) NOT NULL,
title varchar(255) NOT NULL,
body text NOT NULL,
image varchar(255) DEFAULT '' NOT NULL,
is_read tinyint(1) DEFAULT 0 NOT NULL,
created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (id),
KEY user_id (user_id)
) $charset_collate;";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
dbDelta( $sql_subscribers );
dbDelta( $sql_queue );
dbDelta( $sql_user_notifications );
}
register_activation_hook( __FILE__, 'jw_core_create_notification_table' );
/**
* Register Cron Schedules
*/
function jw_core_cron_schedules( $schedules ) {
if ( ! isset( $schedules['every_minute'] ) ) {
$schedules['every_minute'] = array(
'interval' => 60,