-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwc_sendsms.php
1766 lines (1630 loc) · 79.9 KB
/
wc_sendsms.php
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: SendSMS
Plugin URI: https://www.sendsms.ro/ro/ecommerce/plugin-woocommerce/
Description: Use our SMS shipping solution to deliver the right information at the right time. Give your customers a superior experience!
Version: 1.2.9
Author: sendSMS
License: GPLv2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Domain Path: /languages/
*/
$pluginDir = plugin_dir_path(__FILE__);
$pluginDirUrl = plugin_dir_url(__FILE__);
global $wc_sendsms_db_version;
$wc_sendsms_db_version = '1.2.8';
$need = false;
if (!function_exists('is_plugin_active_for_network')) {
require_once(ABSPATH . '/wp-admin/includes/plugin.php');
}
// multisite
if (is_multisite()) {
// this plugin is network activated - Woo must be network activated
// this plugin is network activated - Woo must be network activated
// this plugin is network activated - Woo must be network activated
if (is_plugin_active_for_network(plugin_basename(__FILE__))) {
$need = is_plugin_active_for_network('woocommerce/woocommerce.php') ? false : true;
$need = is_plugin_active_for_network('woocommerce/woocommerce.php') ? false : true;
$need = is_plugin_active_for_network('woocommerce/woocommerce.php') ? false : true;
// this plugin is locally activated - Woo can be network or locally activated
// this plugin is locally activated - Woo can be network or locally activated
// this plugin is locally activated - Woo can be network or locally activated
} else {
$need = is_plugin_active('woocommerce/woocommerce.php') ? false : true;
}
// this plugin runs on a single site
// this plugin runs on a single site
// this plugin runs on a single site
} else {
$need = is_plugin_active('woocommerce/woocommerce.php') ? false : true;
}
if ($need === true) {
return;
}
# history table
if (!class_exists('WP_List_Table')) {
require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
}
include 'HistoryListTable.php';
# create database
function wc_sendsms_install()
{
global $wpdb;
global $wc_sendsms_db_version;
$table_name = $wpdb->prefix . 'wcsendsms_history';
$charset_collate = $wpdb->get_charset_collate();
$installed_ver = get_option('wc_sendsms_db_version');
if ($installed_ver != $wc_sendsms_db_version) {
$sql = "CREATE TABLE `$table_name` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`phone` varchar(255) DEFAULT NULL,
`status` varchar(255) DEFAULT NULL,
`message` varchar(255) DEFAULT NULL,
`details` longtext,
`content` longtext,
`type` varchar(255) DEFAULT NULL,
`sent_on` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
add_option('wc_sendsms_db_version', $wc_sendsms_db_version);
}
}
register_activation_hook(__FILE__, 'wc_sendsms_install');
add_action('init', 'wc_sendsms_load_textdomain');
/**
* Load plugin textdomain.
*/
function wc_sendsms_load_textdomain()
{
load_plugin_textdomain('sendsms', false, dirname(plugin_basename(__FILE__)) . '/languages');
}
# update db structure
function wc_sendsms_update_db_check()
{
global $wc_sendsms_db_version;
if (get_site_option('wc_sendsms_db_version') != $wc_sendsms_db_version) {
wc_sendsms_install();
}
}
add_action('plugins_loaded', 'wc_sendsms_update_db_check');
# add scripts
function wc_sendsms_load_scripts()
{
# load jquery if it's not loaded
if (!wp_script_is('jquery', 'enqueued')) {
wp_enqueue_script('jquery');
}
# script for datepicker
wp_enqueue_style('datepickerdefault', trailingslashit(plugin_dir_url(__FILE__)) . 'datepicker/themes/default.css');
wp_enqueue_style('datepickerdefaultdate', trailingslashit(plugin_dir_url(__FILE__)) . 'datepicker/themes/default.date.css');
wp_enqueue_script('datepickerdefault', trailingslashit(plugin_dir_url(__FILE__)) . 'datepicker/picker.js', array('jquery'));
wp_enqueue_script('datepickerdefaultdate', trailingslashit(plugin_dir_url(__FILE__)) . 'datepicker/picker.date.js', array('jquery'));
wp_enqueue_script('wcsendsms', trailingslashit(plugin_dir_url(__FILE__)) . 'wc_sendsms.js', array('jquery'));
# script & style for jquery
wp_enqueue_style('select2', trailingslashit(plugin_dir_url(__FILE__)) . 'jquery/select2/select2.min.css');
wp_enqueue_script('select2', trailingslashit(plugin_dir_url(__FILE__)) . 'jquery/select2/select2.min.js', array('jquery'));
// please create also an empty JS file in your theme directory and include it too
wp_enqueue_script('js_for_select2', trailingslashit(plugin_dir_url(__FILE__)) . 'forselect2.js', array('jquery', 'select2'));
}
add_action('admin_enqueue_scripts', 'wc_sendsms_load_scripts');
# checkout field for opt-out
function wc_sendsms_optout($checkout)
{
$options = get_option('wc_sendsms_plugin_options');
if (!empty($options) && is_array($options) && isset($options['optout'])) {
$optout = $options['optout'];
} else {
$optout = '';
}
if (!empty($optout)) {
echo '<div>';
woocommerce_form_field('wc_sendsms_optout', array(
'type' => 'checkbox',
'class' => array('input-checkbox', 'form-row-wide'),
'label' => __(' I do not want to receive an SMS with the status of the order', 'sendsms'),
), $checkout->get_value('wc_sendsms_optout'));
echo '</div><div style="clear: both"> </div>';
}
}
add_action('woocommerce_after_order_notes', 'wc_sendsms_optout');
function wc_sendsms_optout_update_order_meta($orderId)
{
if (isset($_POST['wc_sendsms_optout'])) {
update_post_meta($orderId, 'wc_sendsms_optout', wc_sendsms_sanitize_bool($_POST['wc_sendsms_optout']));
}
}
add_action('woocommerce_checkout_update_order_meta', 'wc_sendsms_optout_update_order_meta');
# admin page
add_action('admin_menu', 'wc_sendsms_add_menu');
function wc_sendsms_add_menu()
{
add_menu_page(
__('SendSMS', 'sendsms'),
__('SendSMS', 'sendsms'),
'manage_options',
'wc_sendsms_main',
'wc_sendsms_main',
plugin_dir_url(__FILE__) . 'images/sendsms.png'
);
add_submenu_page(
'wc_sendsms_main',
__('Configuration', 'sendsms'),
__('Configuration', 'sendsms'),
'manage_options',
'wc_sendsms_login',
'wc_sendsms_login'
);
add_submenu_page(
'wc_sendsms_main',
__('History', 'sendsms'),
__('History', 'sendsms'),
'manage_options',
'wc_sendsms_history',
'wc_sendsms_history'
);
add_submenu_page(
'wc_sendsms_main',
__('Campaign', 'sendsms'),
__('Campaign', 'sendsms'),
'manage_options',
'wc_sendsms_campaign',
'wc_sendsms_campaign'
);
add_submenu_page(
'wc_sendsms_main',
__('Send a test', 'sendsms'),
__('Send a test', 'sendsms'),
'manage_options',
'wc_sendsms_test',
'wc_sendsms_test'
);
}
function wc_sendsms_main()
{
?>
<div class="wrap">
<h2><?php echo esc_html('SendSMS for WooCommerce', 'sendsms') ?></h2>
<br />
<p><?php echo esc_html('To use the module, please enter your credentials on the configuration page.', 'sendsms') ?></p><br />
<p><?php echo esc_html('You don\'t have a sendSMS account?', 'sendsms') ?><br />
<?php echo esc_html('Sign up for FREE', 'sendsms') ?> <a href="http://www.sendsms.ro/ro" target="_blank"><?php echo esc_html('here', 'sendsms') ?></a>.<br />
<?php echo esc_html('You can find out more about sendSMS', 'sendsms') ?> <a href="http://www.sendsms.ro/ro"><?php echo esc_html('here', 'sendsms') ?></a>.</p>
<p><?php echo esc_html('On the settings page, below the credentials, you\'ll find a text field for each status available in WooCommerce. You will need to enter a message for the fields to which you want to send the notification. If a field is empty, then the text message will not be sent.', 'sendsms') ?></p>
<p><?php echo esc_html('Example: If you want to send a message when the status of the order changes to Completed, then you will need to fill in a message in the text field.', 'sendsms') ?> <strong><?php echo esc_html('"Message: Completed"', 'sendsms') ?></strong>.</p><br />
<p><?php echo esc_html('You can enter variables that will be filled in according to the order data.', 'sendsms') ?></p>
<p><?php echo esc_html('Example message:', 'sendsms') ?> <strong><?php echo esc_html('Hi {billing_first_name}. Your order with order {order_number} has been completed.', 'sendsms') ?></strong></p>
<p><?php echo esc_html('The message entered must not contain diacritics. If they are entered the letters with diacritics will be replaced with their equivalent without diacritics.', 'sendsms') ?></p>
<br /><br />
<p style="text-align: center"><a href="http://sendsms.ro" target="_blank"><img src="<?php plugin_dir_url(__FILE__) . 'images/sendsms_logo.png' ?>" /></a></p>
</div>
<?php
}
# options
add_action('admin_init', 'wc_sendsms_admin_init');
function wc_sendsms_admin_init()
{
# for login
register_setting(
'wc_sendsms_plugin_options',
'wc_sendsms_plugin_options',
'wc_sendsms_plugin_options_validate'
);
add_settings_section(
'wc_sendsms_plugin_login',
'',
'wc_sendsms_plugin_login_section_text',
'wc_sendsms_plugin'
);
add_settings_field(
'wc_sendsms_plugin_options_username',
__('Username', 'sendsms'),
'wc_sendsms_settings_display_username',
'wc_sendsms_plugin',
'wc_sendsms_plugin_login'
);
add_settings_field(
'wc_sendsms_plugin_options_password',
__('Password / API Key', 'sendsms'),
'wc_sendsms_settings_display_password',
'wc_sendsms_plugin',
'wc_sendsms_plugin_login'
);
add_settings_field(
'wc_sendsms_plugin_options_from',
__('Shipper label', 'sendsms'),
'wc_sendsms_settings_display_from',
'wc_sendsms_plugin',
'wc_sendsms_plugin_login'
);
add_settings_field(
'wc_sendsms_plugin_options_cc',
__('Country Code', 'sendsms'),
'wc_sendsms_settings_display_cc',
'wc_sendsms_plugin',
'wc_sendsms_plugin_login'
);
add_settings_field(
'wc_sendsms_plugin_options_simulation',
__('SMS sending simulation', 'sendsms'),
'wc_sendsms_settings_display_simulation',
'wc_sendsms_plugin',
'wc_sendsms_plugin_login'
);
add_settings_field(
'wc_sendsms_plugin_options_simulation_number',
__('Simulation phone number', 'sendsms'),
'wc_sendsms_settings_display_simulation_number',
'wc_sendsms_plugin',
'wc_sendsms_plugin_login'
);
add_settings_field(
'wc_sendsms_plugin_options_send_to_owner',
__('Send an SMS to each new order', 'sendsms'),
'wc_sendsms_settings_display_send_to_owner',
'wc_sendsms_plugin',
'wc_sendsms_plugin_login'
);
add_settings_field(
'wc_sendsms_plugin_options_send_to_owner_short',
__('Short URL?', 'sendsms'),
'wc_sendsms_settings_display_send_to_owner_short',
'wc_sendsms_plugin',
'wc_sendsms_plugin_login'
);
add_settings_field(
'wc_sendsms_plugin_options_send_to_owner_gdpr',
__('Add unsubscribe link?', 'sendsms'),
'wc_sendsms_settings_display_send_to_owner_gdpr',
'wc_sendsms_plugin',
'wc_sendsms_plugin_login'
);
add_settings_field(
'wc_sendsms_plugin_options_send_to_owner_number',
__('The phone number where the messages will be sent', 'sendsms'),
'wc_sendsms_settings_display_send_to_owner_number',
'wc_sendsms_plugin',
'wc_sendsms_plugin_login'
);
add_settings_field(
'wc_sendsms_plugin_options_send_to_owner_content',
__('Message', 'sendsms'),
'wc_sendsms_settings_display_send_to_owner_content',
'wc_sendsms_plugin',
'wc_sendsms_plugin_login'
);
add_settings_field(
'wc_sendsms_plugin_options_optout',
__('Opt-out in cart', 'sendsms'),
'wc_sendsms_settings_display_optout',
'wc_sendsms_plugin',
'wc_sendsms_plugin_login'
);
add_settings_field(
'wc_sendsms_plugin_options_content',
__('Status Updates', 'sendsms'),
'wc_sendsms_settings_display_content',
'wc_sendsms_plugin',
'wc_sendsms_plugin_login'
);
add_settings_field(
'wc_sendsms_plugin_options_enabled',
'',
'wc_sendsms_settings_display_enabled',
'wc_sendsms_plugin',
'wc_sendsms_plugin_login'
);
}
function wc_sendsms_login()
{
?>
<div class="wrap">
<h2><?php __('SendSMS - Login data', 'sendsms') ?></h2>
<h3><?php
$options = get_option('wc_sendsms_plugin_options');
$username = "";
$password = "";
$from = "";
wc_sendsms_get_account_info($username, $password, $from, $options);
$results = json_decode(wp_remote_retrieve_body(wp_remote_get('http://api.sendsms.ro/json?action=user_get_balance&username=' . urlencode($username) . '&password=' . urlencode($password))), true);
if ($results['status'] >= 0) {
echo esc_html('You have ', 'sendsms') . esc_html($results['details']) . esc_html(' euro in your sendSMS account.', 'sendsms');
} else {
echo esc_html('The plugin is not configured.', 'sendsms');
}
?></h3>
<?php settings_errors(); ?>
<form action="options.php" method="post">
<?php settings_fields('wc_sendsms_plugin_options'); ?>
<?php do_settings_sections('wc_sendsms_plugin'); ?>
<input name="Submit" type="submit" class="button button-primary button-large" value="<?php echo esc_html('Save', 'sendsms') ?>" />
</form>
</div>
<?php
}
function wc_sendsms_get_woocommerce_product_list()
{
$full_product_list = array();
$loop = new WP_Query(array('post_type' => array('product', 'product_variation'), 'posts_per_page' => -1));
while ($loop->have_posts()) : $loop->the_post();
$theid = get_the_ID();
if (get_post_type() == 'product_variation') {
$product = new WC_Product_Variation($theid);
} else {
$product = new WC_Product($theid);
}
// its a variable product
if (get_post_type() == 'product_variation') {
$parent_id = wp_get_post_parent_id($theid);
$sku = get_post_meta($theid, '_sku', true);
$thetitle = get_the_title($parent_id);
// ****** Some error checking for product database *******
// check if variation sku is set
if ($sku == '') {
if ($parent_id == 0) {
// Remove unexpected orphaned variations.. set to auto-draft
$false_post = array();
$false_post['ID'] = $theid;
$false_post['post_status'] = 'auto-draft';
wp_update_post($false_post);
//if (function_exists(add_to_debug)) add_to_debug('false post_type set to auto-draft. id='.$theid);
} else {
// there's no sku for this variation > copy parent sku to variation sku
// & remove the parent sku so the parent check below triggers
$sku = get_post_meta($parent_id, '_sku', true);
//if (function_exists(add_to_debug)) add_to_debug('empty sku id='.$theid.'parent='.$parent_id.'setting sku to '.$sku);
update_post_meta($theid, '_sku', $sku);
update_post_meta($parent_id, '_sku', '');
}
}
// ****************** end error checking *****************
// its a simple product
} else {
$sku = get_post_meta($theid, '_sku', true);
$thetitle = get_the_title();
}
// add product to array but don't add the parent of product variations
if (!empty($sku)) $full_product_list[] = array($thetitle, $sku, $theid);
endwhile;
wp_reset_query();
// sort into alphabetical order, by title
sort($full_product_list);
return $full_product_list;
}
function wc_sendsms_test()
{
if (isset($_POST) && !empty($_POST)) {
if (empty($_POST['wc_sendsms_phone'])) {
echo '<div class="notice notice-error is-dismissible">
<p>' . esc_html('You have not entered your phone number!', 'sendsms') . '</p>
</div>';
}
if (empty($_POST['wc_sendsms_message'])) {
echo '<div class="notice notice-error is-dismissible">
<p>' . esc_html('You have not entered a message!', 'sendsms') . '</p>
</div>';
}
if (!empty($_POST['wc_sendsms_message']) && !empty($_POST['wc_sendsms_phone'])) {
$options = get_option('wc_sendsms_plugin_options');
$username = '';
$password = '';
$short = filter_var(isset($_POST['wc_sendsms_url']) ? $_POST['wc_sendsms_url'] : "false", FILTER_VALIDATE_BOOLEAN);
$gdpr = filter_var(isset($_POST['wc_sendsms_gdpr']) ? $_POST['wc_sendsms_gdpr'] : "false", FILTER_VALIDATE_BOOLEAN);
if (!empty($options) && is_array($options) && isset($options['username'])) {
$username = $options['username'];
}
if (!empty($options) && is_array($options) && isset($options['password'])) {
$password = $options['password'];
}
if (!empty($options) && is_array($options) && isset($options['from'])) {
$from = $options['from'];
}
if (!empty($username) && !empty($password) && !empty($from)) {
$phone = wc_sendsms_validate_phone($_POST['wc_sendsms_phone']);
if (!empty($phone)) {
wc_sendsms_send($username, $password, $phone, sanitize_textarea_field($_POST['wc_sendsms_message']), $from, 'test', $short, $gdpr);
echo '<div class="notice notice-success is-dismissible">
<p>' . esc_html('The message was sent.', 'sendsms') . '</p>
</div>';
} else {
echo '<div class="notice notice-error is-dismissible">
<p>' . esc_html('The validated phone number is empty!', 'sendsms') . '</p>
</div>';
}
} else {
echo '<div class="notice notice-error is-dismissible">
<p>' . esc_html('You have not configured the module!', 'sendsms') . '</p>
</div>';
}
}
}
?>
<div class="wrap">
<h2><?php __('SendSMS - Send an SMS test', 'sendsms') ?></h2>
<form method="post" action="<?php admin_url('admin.php?page=wc_sendsms_test') ?>">
<table class="form-table">
<tbody>
<tr>
<th scope="row"><?php echo esc_html('Phone number', 'sendsms') ?></th>
<td><input type="text" name="wc_sendsms_phone" style="width: 400px;" /></td>
</tr>
<tr>
<th scope="row"><?php echo esc_html('Short URL? (Please use only links starting with https:// or http://)', 'sendsms') ?></th>
<td><input type="checkbox" name="wc_sendsms_url" /></td>
</tr>
<tr>
<th scope="row"><?php echo esc_html('Add unsubscribe link? (You must specify the {gdpr} key message. The {gdpr} key will be automatically replaced with the unique confirmation link. If the {gdpr} key is not specified, the confirmation link will be placed at the end of the message.)', 'sendsms') ?></th>
<td><input type="checkbox" name="wc_sendsms_gdpr" /></td>
</tr>
<tr>
<th scope="row"><?php echo esc_html('Message', 'sendsms') ?></th>
<td>
<textarea name="wc_sendsms_message" class="wc_sendsms_content" style="width: 400px; height: 100px;"></textarea>
<p><?php echo esc_html("The field is empty", 'sendsms') ?></p>
</td>
</tr>
</tbody>
</table>
<p style="clear: both;"><button type="submit" class="button button-primary button-large" id="wc_sendsms_send_test"><?php echo esc_html('Send the message', 'sendsms') ?></button></p>
</form>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", (event) => {
var wc_sendsms_content = document.getElementsByClassName('wc_sendsms_content')[0];
wc_sendsms_content.addEventListener("input", (event) => {
lenghtCounter(event.target || event.srcElement, event.target.nextElementSibling || event.srcElement.nextElementSibling);
});
wc_sendsms_content.addEventListener("change", (event) => {
lenghtCounter(event.target || event.srcElement, event.target.nextElementSibling || event.srcElement.nextElementSibling);
});
function lenghtCounter(textarea, counter) {
var lenght = textarea.value.length;
var messages = lenght / 160 + 1;
if (lenght > 0) {
if (lenght % 160 === 0) {
messages--;
}
counter.textContent = "<?php echo esc_html('The approximate number of messages: ', 'sendsms'); ?>" + Math.floor(messages) + " (" + lenght + ")";
} else {
counter.textContent = "<?php echo esc_html('The field is empty', 'sendsms'); ?>";
}
}
});
</script>
</div>
<?php
}
function wc_sendsms_campaign()
{
global $wpdb;
# get all products
$products = wc_sendsms_get_woocommerce_product_list();
$billing_states = $wpdb->get_results('SELECT DISTINCT meta_value FROM ' . $wpdb->prefix . 'postmeta WHERE meta_key = \'_billing_state\' ORDER BY meta_value ASC');
$orders = array();
if (!isset($_REQUEST['filtering'])) {
$orders = wc_sendsms_get_all_orders();
}
if (isset($_REQUEST['filtering']) && $_REQUEST['filtering'] === "true") {
if (!wp_verify_nonce($_GET['_wpnonce'], "wc_sendsms_send_campaign")) die("You are not supposed to be here");
$orders = wc_sendsms_get_orders_filtered(
isset($_GET['perioada_start']) ? $_GET['perioada_start'] : "",
isset($_GET['perioada_final']) ? $_GET['perioada_final'] : "",
isset($_GET['suma']) ? $_GET['suma'] : "",
isset($_GET['judete']) ? $_GET['judete'] : "",
isset($_GET['produse']) ? $_GET['produse'] : ""
);
}
$phones = array();
if (count($orders)) {
foreach ($orders as $order) {
$phone = wc_sendsms_validate_phone($order->_billing_phone);
if (!empty($phone)) {
$phones[] = $phone;
}
}
}
$phones = array_unique($phones);
// // Generate dumy phones for testing
// $phones = array();
// for ($i = 0; $i < 10; $i++) {
// $phones[] = "4021" . wc_sendsms_randomNumberSequence();
// }
?>
<div class="wrap">
<h2><?php echo esc_html('SendSMS - Campaign', 'sendsms') ?></h2>
<!-- This is the filtering form -->
<form method="GET" action="">
<?php
wp_nonce_field("wc_sendsms_send_campaign");
?>
<input type="hidden" name="page" value="wc_sendsms_campaign" />
<input type="hidden" name="filtering" value="true" />
<div style="width: 100%; clear: both;">
<div style="width: 48%; float: left;">
<p><?php echo esc_html('Period', 'sendsms') ?> <input type="text" class="wcsendsmsdatepicker" name="perioada_start" value="<?php isset($_GET['perioada_start']) ? wc_sendsms_sanitize_event_time($_GET['perioada_start']) : '' ?>" /> - <input type="text" class="wcsendsmsdatepicker" name="perioada_final" value="<?php isset($_GET['perioada_final']) ? wc_sendsms_sanitize_event_time($_GET['perioada_final']) : '' ?>" /></p>
</div>
<div style="width: 48%; float: left">
<p><?php echo esc_html('Minimum amount per order:', 'sendsms') ?> <input type="number" name="suma" value="<?php isset($_GET['suma']) ? wc_sendsms_sanitize_float($_GET['suma']) : '0' ?>" /></p>
</div>
<div style="width: 100%; clear: both;">
<div style="width: 48%; float: left;" class="mySelect">
<p><?php echo esc_html('The purchased product (leave blank to select all products):', 'sendsms') ?>
<select id="produse_selectate" name="produse[]" multiple="multiple" style="width:80%;max-width:25em;">
<?php
for ($i = 0; $i < count($products); $i++) {
$selected = false;
if (isset($_GET['produse'])) {
$lenght = count($_GET['produse']);
for ($j = 0; $j < $lenght; $j++) {
if (strcmp($_GET['produse'][$j], "id_" . $products[$i][2]) === 0) {
$selected = true;
}
}
}
?>
<option value="<?php "id_" . esc_attr($products[$i][2]) ?>" <?php $selected ? 'selected="selected"' : '' ?>><?php esc_attr($products[$i][0]) . " - " . esc_attr($products[$i][1]) ?></option>
<?php
}
?>
</select>
</p>
</div>
<div style="width: 48%; float: left;">
<p><?php echo esc_html('Billing County (leave blank to select all counties):', 'sendsms') ?>
<select id="judete_selectate" name="judete[]" multiple="multiple" style="width:80%;max-width:25em;">
<?php
for ($i = 0; $i < count($billing_states); $i++) {
$selected = false;
if (isset($_GET['judete'])) {
$lenght = count($_GET['judete']);
for ($j = 0; $j < $lenght; $j++) {
if (strcmp($_GET['judete'][$j], "id_" . $billing_states[$i]->meta_value) === 0) {
$selected = true;
}
}
}
?>
<option value="<?php "id_" . esc_attr($billing_states[$i]->meta_value) ?>" <?php $selected ? 'selected="selected"' : '' ?>><?php esc_attr($billing_states[$i]->meta_value) ?></option>
<?php
}
?>
</select>
</p>
</div>
</div>
</div>
<div style="width: 100%; clear: both;">
<button type="submit" class="button button-default button-large aligncenter" value="filter"><?php echo esc_html('Filter', 'sendsms') ?></button>
</div>
</form>
<hr />
<h3><?php echo esc_html('Filter results:', 'sendsms') ?> <?php echo count($phones) ?> <?php echo esc_html('phone number(s)', 'sendsms') ?></h3>
<!-- Send campaign form -->
<form method="POST" action="">
<input type="hidden" name="page" value="wc_sendsms_campaign" />
<input type="hidden" name="action" value="send_campaign" />
<div style="width: 100%; clear: both; padding-top: 20px;">
<div style="width: 73%; float: left">
<div><?php echo esc_html('Message:', 'sendsms') ?> <br />
<textarea name="content" class="wc_sendsms_content" id="wc_sendsms_content" style="width: 90%; height: 250px;"></textarea>
<p><?php echo esc_html('The field is empty', 'sendsms') ?></p>
</div>
</div>
<div style="width: 25%; float: left">
<p><?php echo esc_html('Phone numbers:', 'sendsms') ?> <br /></p>
<div style="margin-bottom: 10px">
<input type="checkbox" id="wc_sendsms_to_all" class="wc_sendsms_to_all" name="wc_sendsms_to_all" checked />
<?php echo esc_html('Send SMS to every number.', 'sendsms') ?></label>
</div>
<select name="phones[]" id="phones" multiple="MULTIPLE" style="width: 90%; height: 250px" size="<?php empty($phones) ? 0 : count($phones) ?>">
<?php
if (!empty($phones)) :
foreach ($phones as $phone) :
?>
<option value="<?php $phone ?>" selected><?php $phone ?></option>
<?php
endforeach;
endif;
?>
</select>
</div>
</div>
<p style="clear: both;">
<button type="submit" class="button button-primary button-large" id="wc_sendsms_send_campaign"><?php echo esc_html('Send the message', 'sendsms') ?></button>
<button type="button" class="button button-primary button-large" name="action" value="estimate_price" id="wc_sendsms_send_campaign_estimate_price"><?php echo esc_html('Estimate the price', 'sendsms') ?></button>
</p>
</form>
</div>
<?php wc_sendsms_javascript_estimate_price(); //just add the check price as a separated function
?>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", (event) => {
var wc_sendsms_content = document.getElementsByClassName('wc_sendsms_content')[0];
wc_sendsms_content.addEventListener("input", (event) => {
lenghtCounter(event.target || event.srcElement, event.target.nextElementSibling || event.srcElement.nextElementSibling);
});
wc_sendsms_content.addEventListener("change", (event) => {
lenghtCounter(event.target || event.srcElement, event.target.nextElementSibling || event.srcElement.nextElementSibling);
});
function lenghtCounter(textarea, counter) {
var lenght = textarea.value.length;
var messages = lenght / 160 + 1;
if (lenght > 0) {
if (lenght % 160 === 0) {
messages--;
}
counter.textContent = "<?php echo esc_html('The approximate number of messages: ', 'sendsms'); ?>" + Math.floor(messages) + " (" + lenght + ")";
} else {
counter.textContent = "<?php echo esc_html('The field is empty', 'sendsms'); ?>";
}
}
});
</script>
<?php
}
function wc_sendsms_javascript_send()
{ ?>
<script type="text/javascript">
jQuery(document).ready(function($) {
jQuery('#wc_sendsms_send_campaign').on('click', function() {
jQuery('#wc_sendsms_send_campaign').html("<?php echo esc_html('It\'s being sent...', 'sendsms') ?>");
jQuery('#wc_sendsms_send_campaign').attr('disabled', 'disabled');
all = jQuery('#wc_sendsms_to_all').is(":checked");
if (all) {
phones = '';
produse = <?php isset($_GET['produse']) ? json_encode($_GET['produse']) : "[]" ?>;
judete = <?php isset($_GET['judete']) ? json_encode($_GET['judete']) : "[]" ?>;
suma = "<?php isset($_GET['suma']) ? wc_sendsms_sanitize_float($_GET['suma']) : "" ?>";
perioada_final = "<?php isset($_GET['perioada_final']) ? wc_sendsms_sanitize_event_time($_GET['perioada_final']) : "" ?>";
perioada_start = "<?php isset($_GET['perioada_start']) ? wc_sendsms_sanitize_event_time($_GET['perioada_start']) : "" ?>";
filtering = "<?php isset($_REQUEST['filtering']) ? true : false ?>";
} else {
phones = jQuery('#phones').val().join("|");
produse = "";
judete = "";
suma = "";
perioada_final = "";
perioada_start = "";
filtering = "";
}
var data = {
'security': '<?php wp_create_nonce('wc_sendsms_send_campaign') ?>',
'action': 'wc_sendsms_campaign',
'all': all,
'phones': phones,
'perioada_start': perioada_start,
'perioada_final': perioada_final,
'suma': suma,
'judete': judete,
'produse': produse,
'filtering': filtering,
'content': jQuery('#wc_sendsms_content').val(),
// 'short': jQuery('#wc_sendsms_short').is(":checked"),
// 'gdpr': jQuery('#wc_sendsms_gdpr').is(":checked")
};
jQuery.post(ajaxurl, data, function(response) {
jQuery('#wc_sendsms_send_campaign').html('<?php echo esc_html('Send the message', 'sendsms') ?>');
jQuery('#wc_sendsms_send_campaign').removeAttr('disabled');
alert(response);
});
});
});
</script>
<?php
}
add_action('admin_footer', 'wc_sendsms_javascript_send');
function wc_sendsms_ajax_send()
{
if (!check_ajax_referer('wc_sendsms_send_campaign', 'security', false)) {
wp_die();
}
if (!empty($_POST['content'])) {
if (isset($_POST['all']) && $_POST['all'] === "true") {
$orders = array();
if (empty($_POST['filtering'])) {
$orders = wc_sendsms_get_all_orders();
}
if (isset($_POST['filtering']) && $_POST['filtering'] === "1") {
$orders = wc_sendsms_get_orders_filtered(
isset($_POST['perioada_start']) ? $_POST['perioada_start'] : "",
isset($_POST['perioada_final']) ? $_POST['perioada_final'] : "",
isset($_POST['suma']) ? $_POST['suma'] : "",
isset($_POST['judete']) ? $_POST['judete'] : "",
isset($_POST['produse']) ? $_POST['produse'] : ""
);
}
$phones = array();
if (count($orders)) {
foreach ($orders as $order) {
$phone = wc_sendsms_validate_phone($order->_billing_phone);
if (!empty($phone)) {
$phones[] = $phone;
}
}
}
$phones = array_unique($phones);
} else {
$phones = explode("|", $_POST['phones']);
if (count($phones) === 0) {
echo esc_html('You must choose at least one phone number.', 'sendsms');
wp_die();
}
}
} else {
echo esc_html('You must complete the message first.', 'sendsms');
wp_die();
}
global $pluginDir;
global $wp_filesystem;
// Initialize the WP_Filesystem if it isn't already
if (!function_exists('WP_Filesystem')) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
WP_Filesystem();
if ($file = fopen("$pluginDir/batches/batch.csv", "w")) {
$options = get_option('wc_sendsms_plugin_options');
$username = '';
$password = '';
$from = '';
if (!empty($options) && is_array($options) && isset($options['username'])) {
$username = $options['username'];
} else {
echo esc_html('You did not enter a username', 'sendsms');
wp_die();
}
if (!empty($options) && is_array($options) && isset($options['password'])) {
$password = $options['password'];
} else {
echo esc_html('You have not entered a password', 'sendsms');
wp_die();
}
if (!empty($options) && is_array($options) && isset($options['from'])) {
$from = $options['from'];
} else {
$from = '';
}
$headers = array(
"message",
"to",
"from"
);
fputcsv($file, $headers);
foreach ($phones as $phone) {
fputcsv($file, array(
$_POST['content'],
$phone,
$from
), ',', '"', '');
}
// $start_time = "2970-01-01 02:00:00";
$start_time = "";
$name = 'Wordpress - ' . get_site_url() . ' - ' . uniqid();
$data = file_get_contents("$pluginDir/batches/batch.csv");
$results = json_decode(wp_remote_retrieve_body(wp_remote_post(
'https://api.sendsms.ro/json?action=batch_create&username=' . urlencode($username) . '&password=' . urlencode($password) . '&start_time=' . urlencode($start_time) . '&name=' . urlencode($name),
array(
'body' => array('data' => $data)
)
)), true);
if (!isset($results['status']) || $results['status'] < 0) {
echo json_encode($results);
wp_die();
}
//log into history table
global $wpdb;
$table_name = $wpdb->prefix . 'wcsendsms_history';
$wpdb->query(
$wpdb->prepare(
"
INSERT INTO $table_name
(`phone`, `status`, `message`, `details`, `content`, `type`, `sent_on`)
VALUES ( %s, %s, %s, %s, %s, %s, %s)
",
esc_html("Go to hub.sendsms.ro", 'sendsms'),
isset($results['status']) ? $results['status'] : '',
isset($results['message']) ? $results['message'] : '',
isset($results['details']) ? $results['details'] : '',
esc_html("We created your campaign. Go and check the batch called: ", 'sendsms') . $name,
esc_html("Batch Campaign", 'sendsms'),
date('Y-m-d H:i:s')
)
);
fclose($file);
if (!unlink("$pluginDir/batches/batch.csv")) {
echo esc_html("Unable to delete previous batch file! Please check file/folder permisions ($pluginDir/batches/batch.csv)");
wp_die();
}
echo esc_html("Success", 'sendsms');
wp_die();
} else {
echo esc_html("Unable to open/create batch file! Please check file/folder permisions ($pluginDir/batches/batch.csv)");
wp_die();
}
}
add_action('wp_ajax_wc_sendsms_campaign', 'wc_sendsms_ajax_send');
function wc_sendsms_javascript_estimate_price()
{ ?>
<script type="text/javascript">
jQuery(document).ready(function($) {
jQuery('#wc_sendsms_send_campaign_estimate_price').on('click', function() {
all = jQuery('#wc_sendsms_to_all').is(":checked");
if (all) {
phones = jQuery('select[id=phones] > option').length;
} else {
phones = jQuery('#phones').val().length;
}
var wc_sendsms_content = document.getElementsByClassName('wc_sendsms_content')[0];
var lenght = wc_sendsms_content.value.length;
var messages = lenght / 160 + 1;
if (lenght > 0) {
if (lenght % 160 === 0) {
messages--
}
messages = Math.floor(messages);
price = <?php get_option('wc-sendsms-default-price', 0) ?>;
if (price > 0) {
alert("<?php echo esc_html('The estimate price is: ', 'sendsms') ?>" + parseFloat(messages * price * phones).toPrecision(4) + "<?php echo esc_html(' (This is just an estimation, and not the actual price)', 'sendsms') ?>");
} else {
alert("<?php echo esc_html('Please send a message first', 'sendsms') ?>");
}
} else {
alert("<?php echo esc_html('Please fill the message box first', 'sendsms') ?>")
}
});
});
</script>
<?php
}
add_action('wp_ajax_wc_sendsms_estimate_price', 'wc_sendsms_ajax_estimate_price');
function wc_sendsms_history()
{
?>
<div class="wrap">
<h2><?php echo esc_html('SendSMS - Historic', 'sendsms') ?></h2>
<form method="get">
<?php
$_table_list = new WC_SendSMS_History_List_Table();
$_table_list->prepare_items();
echo '<input type="hidden" name="page" value="wc_sendsms_history" />';
$_table_list->views();
$_table_list->search_box(esc_html('Search', 'sendsms'), 'key');
$_table_list->display();
?>
</form>
</div>
<?php
}
function wc_sendsms_plugin_login_section_text()
{
//
}
function wc_sendsms_settings_display_username()
{
$options = get_option('wc_sendsms_plugin_options');
if (!empty($options) && is_array($options) && isset($options['username'])) {
$username = $options['username'];
} else {
$username = '';
}
echo '<input id="wc_sendsms_settings_username" name="wc_sendsms_plugin_options[username]" type="text" value="' . esc_html($username) . '" style="width: 400px;" />';
}
function wc_sendsms_settings_display_password()
{
$options = get_option('wc_sendsms_plugin_options');
if (!empty($options) && is_array($options) && isset($options['password'])) {
$password = $options['password'];
} else {
$password = '';
}
echo '<input id="wc_sendsms_settings_password" name="wc_sendsms_plugin_options[password]" type="password" value="' . esc_html($password) . '" style="width: 400px;" />';
}
function wc_sendsms_settings_display_from()
{
$options = get_option('wc_sendsms_plugin_options');
if (!empty($options) && is_array($options) && isset($options['from'])) {
$from = $options['from'];
} else {
$from = '';
}
echo '<input id="wc_sendsms_settings_from" name="wc_sendsms_plugin_options[from]" type="text" value="' . esc_html($from) . '" style="width: 400px;" /> <span>' . esc_html('maximum 11 alpha-numeric characters', 'sendsms') . '</span>';
}