forked from liedekef/events-made-easy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eme-countries.php
1107 lines (1030 loc) · 44.9 KB
/
eme-countries.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
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
function eme_new_country() {
$country = [
'alpha_2' => '',
'alpha_3' => '',
'num_3' => '',
'name' => '',
'lang' => '',
];
return $country;
}
function eme_new_state() {
$state = [
'code' => '',
'name' => '',
'country_id' => 0,
];
return $state;
}
function eme_countries_page() {
if ( ! current_user_can( get_option( 'eme_cap_settings' ) ) && ( isset( $_GET['eme_admin_action'] ) || isset( $_POST['eme_admin_action'] ) ) ) {
$message = __( 'You have no right to manage discounts!', 'events-made-easy' );
eme_countries_main_layout( $message );
return;
}
$message = '';
$csvMimes = [ 'text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain' ];
// handle possible ations
if ( isset( $_POST['eme_admin_action'] ) ) {
check_admin_referer( 'eme_admin', 'eme_admin_nonce' );
if ( $_POST['eme_admin_action'] == 'do_importcountries' && isset( $_FILES['eme_csv'] ) && current_user_can( get_option( 'eme_cap_cleanup' ) ) ) {
$inserted = 0;
$errors = 0;
$error_msg = '';
//validate whether uploaded file is a csv file
if ( ! empty( $_FILES['eme_csv']['name'] ) && in_array( $_FILES['eme_csv']['type'], $csvMimes ) ) {
if ( is_uploaded_file( $_FILES['eme_csv']['tmp_name'] ) ) {
$handle = fopen( $_FILES['eme_csv']['tmp_name'], 'r' );
if ( ! $handle ) {
$message = __( 'Problem accessing the uploaded the file, maybe some security issue?', 'events-made-easy' );
} else {
// BOM as a string for comparison.
$bom = "\xef\xbb\xbf";
// Progress file pointer and get first 3 characters to compare to the BOM string.
if ( fgets( $handle, 4 ) !== $bom ) {
// BOM not found - rewind pointer to start of file.
rewind( $handle );
}
if ( ! eme_is_empty_string( $_POST['enclosure'] ) ) {
$enclosure = eme_sanitize_request( $_POST['enclosure'] );
$enclosure = substr( $enclosure, 0, 1 );
} else {
$enclosure = '"';
}
if ( ! eme_is_empty_string( $_POST['delimiter'] ) ) {
$delimiter = eme_sanitize_request( $_POST['delimiter'] );
} else {
$delimiter = ',';
}
// first line is the column headers
$headers = array_map( 'strtolower', fgetcsv( $handle, 0, $delimiter, $enclosure ) );
// check required columns
if ( ! in_array( 'alpha_2', $headers ) || ! in_array( 'name', $headers ) ) {
$message = __( 'Not all required fields present.', 'events-made-easy' );
} else {
while ( ( $row = fgetcsv( $handle, 0, $delimiter, $enclosure ) ) !== false ) {
$country = array_combine( $headers, $row );
$res = eme_db_insert_country( $country );
if ( $res ) {
++$inserted;
} else {
++$errors;
$error_msg .= '<br>' . esc_html( sprintf( __( 'Not imported: %s', 'events-made-easy' ), implode( ',', $row ) ) );
}
}
$message = sprintf( __( 'Import finished: %d inserts, %d errors', 'events-made-easy' ), $inserted, $errors );
if ( $errors ) {
$message .= '<br>' . $error_msg;
}
}
fclose( $handle );
}
} else {
$message = __( 'Problem detected while uploading the file', 'events-made-easy' );
}
} else {
$message = sprintf( esc_html__( 'No CSV file detected: %s', 'events-made-easy' ), $_FILES['eme_csv']['type'] );
}
} elseif ( $_POST['eme_admin_action'] == 'do_importstates' && isset( $_FILES['eme_csv'] ) && current_user_can( get_option( 'eme_cap_cleanup' ) ) ) {
$inserted = 0;
$errors = 0;
$error_msg = '';
//validate whether uploaded file is a csv file
if ( ! empty( $_FILES['eme_csv']['name'] ) && in_array( $_FILES['eme_csv']['type'], $csvMimes ) ) {
if ( is_uploaded_file( $_FILES['eme_csv']['tmp_name'] ) ) {
$handle = fopen( $_FILES['eme_csv']['tmp_name'], 'r' );
if ( ! $handle ) {
$message = __( 'Problem accessing the uploaded the file, maybe some security issue?', 'events-made-easy' );
} else {
// BOM as a string for comparison.
$bom = "\xef\xbb\xbf";
// Progress file pointer and get first 3 characters to compare to the BOM string.
if ( fgets( $handle, 4 ) !== $bom ) {
// BOM not found - rewind pointer to start of file.
rewind( $handle );
}
if ( ! eme_is_empty_string( $_POST['enclosure'] ) ) {
$enclosure = eme_sanitize_request( $_POST['enclosure'] );
$enclosure = substr( $enclosure, 0, 1 );
} else {
$enclosure = '"';
}
if ( ! eme_is_empty_string( $_POST['delimiter'] ) ) {
$delimiter = eme_sanitize_request( $_POST['delimiter'] );
} else {
$delimiter = ',';
}
// first line is the column headers
$headers = array_map( 'strtolower', fgetcsv( $handle, 0, $delimiter, $enclosure ) );
// check required columns
if ( ! in_array( 'code', $headers ) || ! in_array( 'name', $headers ) || ! in_array( 'country_id', $headers ) ) {
$message = __( 'Not all required fields present.', 'events-made-easy' );
} else {
while ( ( $row = fgetcsv( $handle, 0, $delimiter, $enclosure ) ) !== false ) {
$state = array_combine( $headers, $row );
$res = eme_db_insert_state( $state );
if ( $res ) {
++$inserted;
} else {
++$errors;
$error_msg .= '<br>' . esc_html( sprintf( __( 'Not imported: %s', 'events-made-easy' ), implode( ',', $row ) ) );
}
}
$message = sprintf( __( 'Import finished: %d inserts, %d errors', 'events-made-easy' ), $inserted, $errors );
if ( $errors ) {
$message .= '<br>' . $error_msg;
}
}
fclose( $handle );
}
} else {
$message = __( 'Problem detected while uploading the file', 'events-made-easy' );
}
} else {
$message = sprintf( __( 'No CSV file detected: %s', 'events-made-easy' ), $_FILES['eme_csv']['type'] );
}
} elseif ( $_POST['eme_admin_action'] == 'do_editstate' ) {
if ( ! empty( $_POST['id'] ) ) {
$state_id = intval( $_POST['id'] );
$state = eme_get_state( $state_id );
} else {
$state_id = 0;
$state = eme_new_state();
}
foreach ( $state as $key => $val ) {
if ( isset( $_POST[ $key ] ) ) {
$state[ $key ] = eme_sanitize_request( $_POST[ $key ] );
}
}
$validation_result = eme_validate_state( $state );
if ( ! empty( $validation_result ) ) {
$message = sprintf( __( 'Problem detected: %s, please correct try again.', 'events-made-easy' ), $validation_result );
} elseif ( $state_id ) {
$update_result = eme_db_update_state( $state_id, $state );
if ( $update_result !== false ) {
$message = __( 'Successfully edited the state', 'events-made-easy' );
if ( get_option( 'eme_stay_on_edit_page' ) ) {
eme_states_edit_layout( $state_id, $message );
return;
}
} else {
$message = __( 'There was a problem editing the state, please try again.', 'events-made-easy' );
}
} else {
$new_id = eme_db_insert_state( $state );
if ( $new_id ) {
$message = __( 'Successfully added the state', 'events-made-easy' );
if ( get_option( 'eme_stay_on_edit_page' ) ) {
eme_states_edit_layout( $new_id, $message );
return;
}
} else {
$message = __( 'There was a problem adding the state, please try again.', 'events-made-easy' );
}
}
eme_manage_states_layout( $message );
return;
} elseif ( $_POST['eme_admin_action'] == 'do_editcountry' ) {
if ( ! empty( $_POST['id'] ) ) {
$country_id = intval( $_POST['id'] );
$country = eme_get_country( $country_id );
} else {
$country_id = 0;
$country = eme_new_country();
}
foreach ( $country as $key => $val ) {
if ( isset( $_POST[ $key ] ) ) {
$country[ $key ] = eme_sanitize_request( $_POST[ $key ] );
}
}
// the language POST var has a different name ('language', not 'lang') to avoid a conflict with qtranslate-xt that
// also checks for $_POST['lang'] and redirects to that lang, which is of course not the intention when editing a country
if ( isset( $_POST['language'] ) ) {
$country['lang'] = eme_sanitize_request( $_POST['language'] );
}
$validation_result = eme_validate_country( $country );
if ( ! empty( $validation_result ) ) {
$message = sprintf( __( 'Problem detected: %s, please correct try again.', 'events-made-easy' ), $validation_result );
} elseif ( $country_id ) {
$update_result = eme_db_update_country( $country_id, $country );
if ( $update_result !== false ) {
$message = __( 'Successfully edited the country', 'events-made-easy' );
if ( get_option( 'eme_stay_on_edit_page' ) ) {
eme_countries_edit_layout( $country_id, $message );
return;
}
} else {
$message = __( 'There was a problem editing the country, please try again.', 'events-made-easy' );
}
} else {
$new_id = eme_db_insert_country( $country );
if ( $new_id ) {
$message = __( 'Successfully added the country', 'events-made-easy' );
if ( get_option( 'eme_stay_on_edit_page' ) ) {
eme_countries_edit_layout( $new_id, $message );
return;
}
} else {
$message = __( 'There was a problem adding the country, please try again.', 'events-made-easy' );
}
}
eme_manage_countries_layout( $message );
return;
}
}
// now that we handled possible ations, let's show the wanted screen
if ( isset( $_POST['eme_admin_action'] ) && $_POST['eme_admin_action'] == 'add_country' ) {
check_admin_referer( 'eme_admin', 'eme_admin_nonce' );
eme_countries_edit_layout();
return;
}
if ( isset( $_POST['eme_admin_action'] ) && $_POST['eme_admin_action'] == 'add_state' ) {
check_admin_referer( 'eme_admin', 'eme_admin_nonce' );
eme_states_edit_layout();
return;
}
if ( isset( $_GET['eme_admin_action'] ) && $_GET['eme_admin_action'] == 'edit_country' ) {
check_admin_referer( 'eme_admin', 'eme_admin_nonce' );
eme_countries_edit_layout();
return;
}
if ( isset( $_GET['eme_admin_action'] ) && $_GET['eme_admin_action'] == 'edit_state' ) {
check_admin_referer( 'eme_admin', 'eme_admin_nonce' );
eme_states_edit_layout();
return;
}
if ( isset( $_GET['eme_admin_action'] ) && $_GET['eme_admin_action'] == 'countries' ) {
eme_manage_countries_layout( $message );
return;
}
if ( isset( $_GET['eme_admin_action'] ) && $_GET['eme_admin_action'] == 'states' ) {
eme_manage_states_layout( $message );
return;
}
eme_countries_main_layout();
}
function eme_countries_main_layout( $message = '' ) {
$countries_destination = admin_url( 'admin.php?page=eme-countries&eme_admin_action=countries' );
$states_destination = admin_url( 'admin.php?page=eme-countries&eme_admin_action=states' );
$html = "
<div class='wrap nosubsub'>\n
<div id='icon-edit' class='icon32'>
</div>
<h1>" . __( 'Manage countries and states', 'events-made-easy' ) . '</h1>
';
if ( ! empty( $message ) ) {
$html .= '<div id="countries-message" class="eme-message-admin"><p>' . $message . '</p></div>';
}
$html .= '<p>' . __( 'For personal info (people, members, event rsvp) EME allows you to use auto-completion on states, based on the country choice. However, since there are way too many states and languages, it is impossible to provide that list in EME itself. So if you want to use country and state info, you should enter the countries and states here.', 'events-made-easy' ) . '</p>';
$html .= '<p><b>' . __( 'This is NOT used for event locations, only for personal info of people.', 'events-made-easy' ) . '</b></p>';
$html .= '<h2>' . __( 'Manage countries', 'events-made-easy' ) . '</h2>';
$html .= "<a href='$countries_destination'>" . __( 'Manage countries', 'events-made-easy' ) . '</a><br>';
$html .= '<h2>' . __( 'Manage states', 'events-made-easy' ) . '</h2>';
$countries_count = eme_get_countries_count();
if ( $countries_count > 0 ) {
$html .= "<a href='$states_destination'>" . __( 'Manage states', 'events-made-easy' ) . '</a><br>';
} else {
$html .= __( 'There are no countries defined yet. First define some countries, then you can manage the states.', 'events-made-easy' ) . '<br>';
}
echo $html;
}
function eme_manage_countries_layout( $message = '' ) {
global $plugin_page;
$lang = eme_detect_lang();
$nonce_field = wp_nonce_field( 'eme_admin', 'eme_admin_nonce', false, false );
if ( empty( $message ) ) {
$hidden_class = 'eme-hidden';
} else {
$hidden_class = '';
}
$countries_count = eme_get_countries_count();
if ( $countries_count == 1 ) {
$countries = eme_get_countries();
$country = $countries[0];
if ( ! empty( $country['lang'] ) && $country['lang'] != $lang ) {
$message .= sprintf( __( "There's only one country defined, but the language of the country is not empty and doesn't match the WordPress detected language (%s), meaning it won't show up when editing people. Either add extra countries or correct the language of this country.", 'events-made-easy' ), $lang );
}
}
?>
<div class="wrap nosubsub">
<div id="poststuff">
<div id="icon-edit" class="icon32">
</div>
<div id="countries-message" class="notice is-dismissible eme-message-admin <?php echo $hidden_class; ?>">
<p><?php echo $message; ?></p>
</div>
<h1><?php esc_html_e( 'Add a new country', 'events-made-easy' ); ?></h1>
<div class="wrap">
<form method="post" action="<?php echo admin_url( "admin.php?page=$plugin_page" ); ?>">
<?php echo $nonce_field; ?>
<input type="hidden" name="eme_admin_action" value="add_country">
<input type="submit" class="button-primary" name="submit" value="<?php esc_html_e( 'Add country', 'events-made-easy' ); ?>">
</form>
</div>
<h1><?php esc_html_e( 'Manage countries', 'events-made-easy' ); ?></h1>
<?php if ( current_user_can( get_option( 'eme_cap_cleanup' ) ) ) { ?>
<span class="eme_import_form_img">
<?php esc_html_e( 'Click on the icon to show the import form', 'events-made-easy' ); ?>
<img src="<?php echo esc_url(EME_PLUGIN_URL); ?>images/showhide.png" class="showhidebutton" alt="show/hide" data-showhide="div_import" style="cursor: pointer; vertical-align: middle; ">
</span>
<div id='div_import' style='display:none;'>
<form id='countries-import' method='post' enctype='multipart/form-data' action='#'>
<?php echo $nonce_field; ?>
<input type="file" name="eme_csv">
<?php esc_html_e( 'Delimiter:', 'events-made-easy' ); ?>
<input type="text" size=1 maxlength=1 name="delimiter" value=',' required='required'>
<?php esc_html_e( 'Enclosure:', 'events-made-easy' ); ?>
<input required="required" type="text" size=1 maxlength=1 name="enclosure" value='"' required='required'>
<input type="hidden" name="eme_admin_action" value="do_importcountries">
<input type="submit" value="<?php esc_html_e( 'Import', 'events-made-easy' ); ?>" name="doaction" id="doaction" class="button-primary action">
<?php esc_html_e( 'If you want, use this to import countries into the database', 'events-made-easy' ); ?>
</form>
</div>
<br>
<?php echo sprintf( __( 'See <a href="%s">here</a> for more info on country codes', 'events-made-easy' ), 'https://en.wikipedia.org/wiki/ISO_3166-1' ); ?>
<br>
<?php esc_html_e( 'The language should correspond to one of the WordPress languages you want to support, or leave it empty as a default or fallback. Some examples are: nl, fr, en, de', 'events-made-easy' ); ?>
<br>
<?php } ?>
<br>
<form id='countries-form' action="#" method="post">
<?php echo $nonce_field; ?>
<select id="eme_admin_action" name="eme_admin_action">
<option value="" selected="selected"><?php esc_html_e( 'Bulk Actions', 'events-made-easy' ); ?></option>
<option value="deleteCountries"><?php esc_html_e( 'Delete selected countries', 'events-made-easy' ); ?></option>
</select>
<button id="CountriesActionsButton" class="button-secondary action"><?php esc_html_e( 'Apply', 'events-made-easy' ); ?></button>
<span class="rightclickhint">
<?php esc_html_e( 'Hint: rightclick on the column headers to show/hide columns', 'events-made-easy' ); ?>
</span>
<div id="CountriesTableContainer"></div>
</form>
</div>
</div>
<?php
}
function eme_manage_states_layout( $message = '' ) {
global $plugin_page;
$nonce_field = wp_nonce_field( 'eme_admin', 'eme_admin_nonce', false, false );
if ( empty( $message ) ) {
$hidden_class = 'eme-hidden';
} else {
$hidden_class = '';
}
?>
<div class="wrap nosubsub">
<div id="poststuff">
<div id="icon-edit" class="icon32">
</div>
<div id="states-message" class="notice is-dismissible eme-message-admin <?php echo $hidden_class; ?>">
<p><?php echo $message; ?></p>
</div>
<h1><?php esc_html_e( 'Add a new state', 'events-made-easy' ); ?></h1>
<div class="wrap">
<form method="post" action="<?php echo admin_url( "admin.php?page=$plugin_page" ); ?>">
<?php echo $nonce_field; ?>
<input type="hidden" name="eme_admin_action" value="add_state">
<input type="submit" class="button-primary" name="submit" value="<?php esc_html_e( 'Add state', 'events-made-easy' ); ?>">
</form>
</div>
<h1><?php esc_html_e( 'Manage states', 'events-made-easy' ); ?></h1>
<?php if ( current_user_can( get_option( 'eme_cap_cleanup' ) ) ) { ?>
<span class="eme_import_form_img">
<?php esc_html_e( 'Click on the icon to show the import form', 'events-made-easy' ); ?>
<img src="<?php echo esc_url(EME_PLUGIN_URL); ?>images/showhide.png" class="showhidebutton" alt="show/hide" data-showhide="div_import" style="cursor: pointer; vertical-align: middle; ">
</span>
<div id='div_import' style='display:none;'>
<form id='states-import' method='post' enctype='multipart/form-data' action='#'>
<?php echo $nonce_field; ?>
<input type="file" name="eme_csv">
<?php esc_html_e( 'Delimiter:', 'events-made-easy' ); ?>
<input type="text" size=1 maxlength=1 name="delimiter" value=',' required='required'>
<?php esc_html_e( 'Enclosure:', 'events-made-easy' ); ?>
<input required="required" type="text" size=1 maxlength=1 name="enclosure" value='"' required='required'>
<input type="hidden" name="eme_admin_action" value="do_importstates">
<input type="submit" value="<?php esc_html_e( 'Import', 'events-made-easy' ); ?>" name="doaction" id="doaction" class="button-primary action">
<?php esc_html_e( 'If you want, use this to import states into the database', 'events-made-easy' ); ?>
</form>
</div>
<br>
<?php echo sprintf( __( 'See <a href="%s">here</a> for more info on state codes', 'events-made-easy' ), 'https://wikipedia.org/wiki/ISO_3166-2' ); ?>
<br>
<?php esc_html_e( 'The code should consist of 2 letters. An example would be the code "WA" for "Washington, US"', 'events-made-easy' ); ?>
<br>
<?php } ?>
<br>
<form id='states-form' action="#" method="post">
<?php echo $nonce_field; ?>
<select id="eme_admin_action" name="eme_admin_action">
<option value="" selected="selected"><?php esc_html_e( 'Bulk Actions', 'events-made-easy' ); ?></option>
<option value="deleteStates"><?php esc_html_e( 'Delete selected states', 'events-made-easy' ); ?></option>
</select>
<button id="StatesActionsButton" class="button-secondary action"><?php esc_html_e( 'Apply', 'events-made-easy' ); ?></button>
<span class="rightclickhint">
<?php esc_html_e( 'Hint: rightclick on the column headers to show/hide columns', 'events-made-easy' ); ?>
</span>
<div id="StatesTableContainer"></div>
</form>
</div>
</div>
<?php
}
function eme_states_edit_layout( $state_id = 0, $message = '' ) {
global $plugin_page;
$countries = eme_get_countries_lang();
if ( ! empty( $state_id ) ) {
$state = eme_get_state( $state_id );
$h1_string = __( 'Edit state', 'events-made-easy' );
$action_string = __( 'Update state', 'events-made-easy' );
} elseif ( isset( $_GET['id'] ) ) {
$state_id = intval( $_GET['id'] );
$state = eme_get_state( $state_id );
$h1_string = __( 'Edit state', 'events-made-easy' );
$action_string = __( 'Update state', 'events-made-easy' );
} else {
$state_id = 0;
$state = eme_new_state();
$h1_string = __( 'Create state', 'events-made-easy' );
$action_string = __( 'Add state', 'events-made-easy' );
}
$nonce_field = wp_nonce_field( 'eme_admin', 'eme_admin_nonce', false, false );
$layout = "
<div class='wrap'>
<div id='icon-edit' class='icon32'>
</div>
<h1>" . $h1_string . '</h1>';
if ( $message != '' ) {
$layout .= "
<div id='message' class='updated notice notice-success is-dismissible'>
<p>$message</p>
</div>";
}
$layout .= "
<div id='ajax-response'></div>
<form name='edit_states' id='edit_states' method='post' action='" . admin_url( "admin.php?page=$plugin_page" ) . "'>
<input type='hidden' name='eme_admin_action' value='do_editstate'>
<input type='hidden' name='id' value='" . $state_id . "'>
$nonce_field
<table class='form-table'>
<tr class='form-field'>
<th scope='row' style='vertical-align:top'><label for='name'>" . __( 'State name', 'events-made-easy' ) . "</label></th>
<td><input name='name' id='name' required='required' type='text' value='" . esc_html( $state['name'] ) . "' size='40'><br>
" . __( 'The name of the state', 'events-made-easy' ) . "</td>
</tr>
<tr class='form-field'>
<th scope='row' style='vertical-align:top'><label for='value'>" . __( 'Code', 'events-made-easy' ) . "</label></th>
<td><input name='code' id='code' type='text' value='" . esc_html( $state['code'] ) . "' size='40'>
<br>" . sprintf( __( 'See <a href="%s">here</a> for more info on state codes', 'events-made-easy' ), 'https://wikipedia.org/wiki/ISO_3166-2' ) . '
<br>' . __( 'The code should consist of 2 letters. An example would be the code "WA" for "Washington, US"', 'events-made-easy' ) . "
</td>
</tr>
<tr class='form-field'>
<th scope='row' style='vertical-align:top'><label for='type'>" . __( 'Country', 'events-made-easy' ) . '</label></th>
<td>' . eme_ui_select_key_value( $state['country_id'], 'country_id', $countries, 'id', 'name' ) . "
</tr>
</table>
<p class='submit'><input type='submit' class='button-primary' name='submit' value='" . $action_string . "'></p>
</form>
</div>
";
echo $layout;
}
function eme_countries_edit_layout( $country_id = 0, $message = '' ) {
global $plugin_page;
if ( ! empty( $country_id ) ) {
$country = eme_get_country( $country_id );
$h1_string = __( 'Edit country', 'events-made-easy' );
$action_string = __( 'Update country', 'events-made-easy' );
} elseif ( isset( $_GET['id'] ) ) {
$country_id = intval( $_GET['id'] );
$country = eme_get_country( $country_id );
$h1_string = __( 'Edit country', 'events-made-easy' );
$action_string = __( 'Update country', 'events-made-easy' );
} else {
$country_id = 0;
$country = eme_new_country();
$h1_string = __( 'Create country', 'events-made-easy' );
$action_string = __( 'Add country', 'events-made-easy' );
}
$nonce_field = wp_nonce_field( 'eme_admin', 'eme_admin_nonce', false, false );
$layout = "
<div class='wrap'>
<div id='icon-edit' class='icon32'>
</div>
<h1>" . $h1_string . '</h1>';
if ( $message != '' ) {
$layout .= "
<div id='message' class='updated notice notice-success is-dismissible'>
<p>$message</p>
</div>";
}
$layout .= "
<div id='ajax-response'></div>
<form name='edit_countries' id='edit_countries' method='post' action='" . admin_url( "admin.php?page=$plugin_page" ) . "'>
<input type='hidden' name='eme_admin_action' value='do_editcountry'>
<input type='hidden' name='id' value='" . esc_attr( $country_id ) . "'>
$nonce_field
<table class='form-table'>
<tr class='form-field'>
<th scope='row' style='vertical-align:top'><label for='name'>" . __( 'Country name', 'events-made-easy' ) . "</label></th>
<td><input name='name' id='name' required='required' type='text' value='" . esc_attr( $country['name'] ) . "' size='40'><br>
" . __( 'The name of the country', 'events-made-easy' ) . "</td>
</tr>
<tr class='form-field'>
<th scope='row' style='vertical-align:top'><label for='alpha_2'>" . __( 'Alpha-2', 'events-made-easy' ) . "</label></th>
<td><input name='alpha_2' id='alpha_2' required='required' type='text' value='" . esc_attr( $country['alpha_2'] ) . "' size='40'></td>
</tr>
<tr class='form-field'>
<th scope='row' style='vertical-align:top'><label for='alpha_3'>" . __( 'Alpha-3', 'events-made-easy' ) . "</label></th>
<td><input name='alpha_3' id='alpha_3' required='required' type='text' value='" . esc_attr( $country['alpha_3'] ) . "' size='40'></td>
</tr>
<tr class='form-field'>
<th scope='row' style='vertical-align:top'><label for='num_3'>" . __( 'Num-3', 'events-made-easy' ) . "</label></th>
<td><input name='num_3' id='num_3' required='required' type='text' value='" . esc_attr( $country['num_3'] ) . "' size='40'></td>
</tr>
<tr class='form-field'>
<th scope='row' style='vertical-align:top'><label for='lang'>" . __( 'Language', 'events-made-easy' ) . "</label></th>
<td><input name='language' id='language' type='text' value='" . esc_attr( $country['lang'] ) . "' size='40'>
<br>" . __( 'The language should correspond to one of the WordPress languages you want to support, or leave it empty as a default or fallback. Some examples are: nl, fr, en, de', 'events-made-easy' ) . '
</td>
</tr>
</table>
<br>' . sprintf( __( 'See <a href="%s">here</a> for more info on country codes', 'events-made-easy' ), 'https://en.wikipedia.org/wiki/ISO_3166-1' ) . "
<p class='submit'><input type='submit' class='button-primary' name='submit' value='" . $action_string . "'></p>
</form>
</div>
";
echo $layout;
}
function eme_get_localized_states( $country_code = '' ) {
global $wpdb;
$table = EME_DB_PREFIX . EME_STATES_TBNAME;
$countries_table = EME_DB_PREFIX . EME_COUNTRIES_TBNAME;
$lang = eme_detect_lang();
if ( empty( $country_code ) ) {
$sql = $wpdb->prepare( "SELECT state.*,country.name AS country,country.lang FROM $table AS state LEFT JOIN $countries_table AS country ON state.country_id=country.id WHERE lang=%s", $lang );
} else {
$sql = $wpdb->prepare( "SELECT state.*,country.name AS country,country.lang FROM $table AS state LEFT JOIN $countries_table AS country ON state.country_id=country.id WHERE country.alpha_2=%s AND country.lang=%s", $country_code, $lang );
}
$res = $wpdb->get_results( $sql, ARRAY_A );
if ( empty( $res ) ) {
if ( empty( $country_code ) ) {
$sql = "SELECT state.*,country.name AS country,country.lang FROM $table AS state LEFT JOIN $countries_table AS country ON state.country_id=country.id WHERE lang=''";
} else {
$sql = $wpdb->prepare( "SELECT state.*,country.name AS country,country.lang FROM $table AS state LEFT JOIN $countries_table AS country ON state.country_id=country.id WHERE country.alpha_2=%s AND country.lang=''", $country_code );
}
$res = $wpdb->get_results( $sql, ARRAY_A );
}
return $res;
}
function eme_get_countries_count() {
global $wpdb;
$table = EME_DB_PREFIX . EME_COUNTRIES_TBNAME;
$sql = "SELECT count(*) FROM $table";
return $wpdb->get_var( $sql );
}
function eme_get_countries() {
global $wpdb;
$table = EME_DB_PREFIX . EME_COUNTRIES_TBNAME;
$sql = "SELECT * FROM $table";
return $wpdb->get_results( $sql, ARRAY_A );
}
function eme_get_localized_countries() {
global $wpdb;
$table = EME_DB_PREFIX . EME_COUNTRIES_TBNAME;
$lang = eme_detect_lang();
$sql = $wpdb->prepare( "SELECT * FROM $table WHERE lang=%s", $lang );
$res = $wpdb->get_results( $sql, ARRAY_A );
if ( empty( $res ) ) {
$sql = "SELECT * FROM $table WHERE lang=''";
$res = $wpdb->get_results( $sql, ARRAY_A );
}
return $res;
}
function eme_get_countries_alpha2() {
global $wpdb;
$table = EME_DB_PREFIX . EME_COUNTRIES_TBNAME;
$sql = "SELECT alpha_2 FROM $table GROUP BY alpha_2";
return $wpdb->get_col( $sql );
}
function eme_validate_state( $state ) {
if ( strlen( $state['code'] ) != 2 ) {
return __( 'Incorrect code', 'events-made-easy' );
}
return '';
}
function eme_validate_country( $country ) {
global $wpdb;
$table = EME_DB_PREFIX . EME_COUNTRIES_TBNAME;
if ( strlen( $country['alpha_2'] ) != 2 ) {
return __( 'Incorrect alpha-2 code', 'events-made-easy' );
}
if ( $country['alpha_3'] && strlen( $country['alpha_3'] ) != 3 ) {
return __( 'Incorrect alpha-3 code', 'events-made-easy' );
}
if ( $country['num_3'] && strlen( $country['num_3'] ) != 3 ) {
return __( 'Incorrect num-3 code', 'events-made-easy' );
}
if ( empty($country['id'] ) ) {
$sql = $wpdb->prepare( "SELECT COUNT(*) FROM $table WHERE alpha_2=%s AND lang=%s", $country['alpha_2'], $country['lang'] );
} else {
$sql = $wpdb->prepare( "SELECT COUNT(*) FROM $table WHERE id != %d AND alpha_2=%s AND lang=%s", $country['id'], $country['alpha_2'], $country['lang'] );
}
$count = $wpdb->get_var( $sql );
if ( $count > 0 ) {
return __( 'Duplicate country with the same language and alpha-2 code detected', 'events-made-easy' );
}
return '';
}
function eme_db_insert_country( $line ) {
global $wpdb;
$table = EME_DB_PREFIX . EME_COUNTRIES_TBNAME;
$country = eme_new_country();
// we only want the columns that interest us
$keys = array_intersect_key( $line, $country );
$new_line = array_merge( $country, $keys );
$new_line['alpha_2'] = strtoupper( $new_line['alpha_2'] );
$new_line['alpha_3'] = strtoupper( $new_line['alpha_3'] );
if ( $wpdb->insert( $table, $new_line ) === false ) {
return false;
} else {
return $wpdb->insert_id;
}
}
function eme_db_update_country( $id, $line ) {
global $wpdb;
$table = EME_DB_PREFIX . EME_COUNTRIES_TBNAME;
$people_table = EME_DB_PREFIX . EME_PEOPLE_TBNAME;
$line['alpha_2'] = strtoupper( $line['alpha_2'] );
$line['alpha_3'] = strtoupper( $line['alpha_3'] );
// first get the existing country, compary the alpha_2 and change ALL countries and people from the old to new alpha_2 if not the same
$country = eme_get_country( $id );
if ( $country['alpha_2'] != $line['alpha_2'] ) {
$sql = $wpdb->prepare( "UPDATE $people_table SET country_code=%s WHERE country_code=%s", $line['alpha_2'], $country['alpha_2'] );
$wpdb->query( $sql );
$sql = $wpdb->prepare( "UPDATE $table SET alpha_2=%s WHERE alpha_2=%s", $line['alpha_2'], $country['alpha_2'] );
$wpdb->query( $sql );
}
if ( $country['alpha_3'] != $line['alpha_3'] ) {
$sql = $wpdb->prepare( "UPDATE $table SET alpha_3=%s WHERE alpha_3=%s", $line['alpha_3'], $country['alpha_3'] );
$wpdb->query( $sql );
}
if ( $country['num_3'] != $line['num_3'] ) {
// we take into account that old values were not correctly prefixed by a 0
$sql = $wpdb->prepare( "UPDATE $table SET num_3=%03d WHERE num_3=%03d or num_3=%s", $line['num_3'], $country['num_3'], $country['num_3'] );
$wpdb->query( $sql );
}
// we only want the columns that interest us
$keys = array_intersect_key( $line, $country );
$new_line = array_merge( $country, $keys );
$where = [ 'id' => $id ];
if ( isset( $new_line['id'] ) ) {
unset( $new_line['id'] );
}
if ( $wpdb->update( $table, $new_line, $where ) === false ) {
return false;
} else {
return $id;
}
}
function eme_db_insert_state( $line ) {
global $wpdb;
$table = EME_DB_PREFIX . EME_STATES_TBNAME;
$state = eme_new_state();
// we only want the columns that interest us
$keys = array_intersect_key( $line, $state );
$new_line = array_merge( $state, $keys );
$new_line['code'] = strtoupper( $new_line['code'] );
if ( $wpdb->insert( $table, $new_line ) === false ) {
return false;
} else {
return $wpdb->insert_id;
}
}
function eme_db_update_state( $id, $line ) {
global $wpdb;
$table = EME_DB_PREFIX . EME_STATES_TBNAME;
$people_table = EME_DB_PREFIX . EME_PEOPLE_TBNAME;
$line['code'] = strtoupper( $line['code'] );
// first get the existing state, compary the state code and change ALL relevant states and people from the old to new code if not the same
$state = eme_get_state( $id );
if ( $state['code'] != $line['code'] ) {
$sql = $wpdb->prepare( "UPDATE $people_table SET state_code=%s WHERE state_code=%s", $line['code'], $state['code'] );
$wpdb->query( $sql );
$sql = $wpdb->prepare( "UPDATE $table SET code=%s WHERE code=%s AND country_id=%d", $line['code'], $state['code'], $state['country_id'] );
$wpdb->query( $sql );
}
// we only want the columns that interest us
$keys = array_intersect_key( $line, $state );
$new_line = array_merge( $state, $keys );
$where = [ 'id' => $id ];
if ( isset( $new_line['id'] ) ) {
unset( $new_line['id'] );
}
if ( $wpdb->update( $table, $new_line, $where ) === false ) {
return false;
} else {
return $id;
}
}
function eme_get_country( $id ) {
global $wpdb;
$table = EME_DB_PREFIX . EME_COUNTRIES_TBNAME;
$sql = $wpdb->prepare( "SELECT * FROM $table WHERE id = %d", $id );
$res = $wpdb->get_row( $sql, ARRAY_A );
$res['num_3'] = sprintf( '%03s', $res['num_3'] );
return $res;
}
function eme_get_country_name( $code, $lang = '' ) {
global $wpdb;
$table = EME_DB_PREFIX . EME_COUNTRIES_TBNAME;
if ( empty( $lang ) ) {
$lang = eme_detect_lang();
}
$sql = $wpdb->prepare( "SELECT name FROM $table WHERE alpha_2 = %s AND lang=%s LIMIT 1", $code, $lang );
$res = $wpdb->get_var( $sql );
if ( empty( $res ) ) {
$sql = $wpdb->prepare( "SELECT name FROM $table WHERE alpha_2 = %s AND lang='' LIMIT 1", $code );
$res = $wpdb->get_var( $sql );
}
return $res;
}
function eme_get_state( $id ) {
global $wpdb;
$table = EME_DB_PREFIX . EME_STATES_TBNAME;
$sql = $wpdb->prepare( "SELECT * FROM $table WHERE id=%d", $id );
return $wpdb->get_row( $sql, ARRAY_A );
}
function eme_get_state_lang( $id ) {
global $wpdb;
$table = EME_DB_PREFIX . EME_STATES_TBNAME;
$countries_table = EME_DB_PREFIX . EME_COUNTRIES_TBNAME;
$sql = $wpdb->prepare( "SELECT country.lang FROM $table AS state LEFT JOIN $countries_table AS country ON state.country_id=country.id WHERE state.id=%d", $id );
return $wpdb->get_var( $sql );
}
function eme_get_state_name( $code, $country_code, $lang = '' ) {
global $wpdb;
$table = EME_DB_PREFIX . EME_STATES_TBNAME;
$countries_table = EME_DB_PREFIX . EME_COUNTRIES_TBNAME;
if ( empty( $lang ) ) {
$lang = eme_detect_lang();
}
if ( empty( $country_code ) ) {
$sql = $wpdb->prepare( "SELECT state.name FROM $table AS state LEFT JOIN $countries_table AS country ON state.country_id=country.id WHERE state.code=%s AND (country.lang='' OR country.lang=%s) LIMIT 1", $code, $lang );
} else {
$sql = $wpdb->prepare( "SELECT state.name FROM $table AS state LEFT JOIN $countries_table AS country ON state.country_id=country.id WHERE state.code=%s and country.alpha_2=%s AND (country.lang='' OR country.lang=%s) LIMIT 1", $code, $country_code, $lang );
}
return $wpdb->get_var( $sql );
}
function eme_get_countries_lang() {
global $wpdb;
$table = EME_DB_PREFIX . EME_COUNTRIES_TBNAME;
$sql = "SELECT id, CONCAT (name, ' (', lang, ')') AS name FROM $table";
return $wpdb->get_results( $sql, ARRAY_A );
}
add_action( 'wp_ajax_eme_countries_list', 'eme_ajax_countries_list' );
add_action( 'wp_ajax_eme_manage_countries', 'eme_ajax_manage_countries' );
add_action( 'wp_ajax_eme_states_list', 'eme_ajax_states_list' );
add_action( 'wp_ajax_eme_manage_states', 'eme_ajax_manage_states' );
function eme_ajax_countries_list() {
global $wpdb;
check_ajax_referer( 'eme_admin', 'eme_admin_nonce' );
$table = EME_DB_PREFIX . EME_COUNTRIES_TBNAME;
$jTableResult = [];
// The toolbar search input
$q = isset( $_REQUEST['q'] ) ? eme_sanitize_request($_REQUEST['q']) : '';
$opt = isset( $_REQUEST['opt'] ) ? eme_sanitize_request($_REQUEST['opt']) : '';
$where = '';
$where_array = [];
if ( $q ) {
for ( $i = 0; $i < count( $opt ); $i++ ) {
$fld = esc_sql( $opt[ $i ] );
$where_array[] = "`$fld` LIKE '%" . esc_sql( $wpdb->esc_like( $q[ $i ] ) ) . "%'";
}
$where = ' WHERE ' . implode( ' AND ', $where_array );
}
if ( current_user_can( get_option( 'eme_cap_settings' ) ) ) {
$sql = "SELECT COUNT(*) FROM $table $where";
$recordCount = $wpdb->get_var( $sql );
$sorting = ( ! empty( $_REQUEST['jtSorting'] ) && ! empty( eme_verify_sql_orderby( $_REQUEST['jtSorting'] ) ) ) ? 'ORDER BY ' . esc_sql($_REQUEST['jtSorting']) : '';
if ( isset( $_REQUEST['jtStartIndex'] ) && isset( $_REQUEST['jtPageSize'] ) ) {
$limit = ' LIMIT ' . intval( $_REQUEST['jtStartIndex'] ) . ',' . intval( $_REQUEST['jtPageSize'] );
} else {
$limit = '';
}
$sql = "SELECT * FROM $table $where $sorting $limit";
$rows = $wpdb->get_results( $sql, ARRAY_A );
foreach ( $rows as $key => $row ) {
$rows[ $key ]['name'] = "<a href='" . wp_nonce_url( admin_url( 'admin.php?page=eme-countries&eme_admin_action=edit_country&id=' . $row['id'] ), 'eme_admin', 'eme_admin_nonce' ) . "'>" . $row['name'] . '</a>';
}
$jTableResult['Result'] = 'OK';
$jTableResult['TotalRecordCount'] = $recordCount;
$jTableResult['Records'] = $rows;
} else {
$jTableResult['Result'] = 'Error';
$jTableResult['Message'] = __( 'Access denied!', 'events-made-easy' );
}
print wp_json_encode( $jTableResult );
wp_die();
}
function eme_ajax_states_list() {
global $wpdb;
check_ajax_referer( 'eme_admin', 'eme_admin_nonce' );
$table = EME_DB_PREFIX . EME_STATES_TBNAME;
$countries_table = EME_DB_PREFIX . EME_COUNTRIES_TBNAME;
$jTableResult = [];
if ( current_user_can( get_option( 'eme_cap_settings' ) ) ) {
$sql = "SELECT COUNT(*) FROM $table";
$recordCount = $wpdb->get_var( $sql );
$sorting = ( ! empty( $_REQUEST['jtSorting'] ) && ! empty( eme_verify_sql_orderby( $_REQUEST['jtSorting'] ) ) ) ? 'ORDER BY ' . esc_sql($_REQUEST['jtSorting']) : '';
if ( isset( $_REQUEST['jtStartIndex'] ) && isset( $_REQUEST['jtPageSize'] ) ) {
$limit = ' LIMIT ' . intval( $_REQUEST['jtStartIndex'] ) . ',' . intval( $_REQUEST['jtPageSize'] );
} else {
$limit = '';
}
$sql = "SELECT state.*,country.lang,country.name AS country_name FROM $table AS state LEFT JOIN $countries_table AS country ON state.country_id=country.id $sorting $limit";
$rows = $wpdb->get_results( $sql, ARRAY_A );
foreach ( $rows as $key => $row ) {
$rows[ $key ]['name'] = "<a href='" . wp_nonce_url( admin_url( 'admin.php?page=eme-countries&eme_admin_action=edit_state&id=' . $row['id'] ), 'eme_admin', 'eme_admin_nonce' ) . "'>" . $row['name'] . '</a>';
}
$jTableResult['Result'] = 'OK';
$jTableResult['TotalRecordCount'] = $recordCount;
$jTableResult['Records'] = $rows;
} else {
$jTableResult['Result'] = 'Error';
$jTableResult['Message'] = __( 'Access denied!', 'events-made-easy' );
}
print wp_json_encode( $jTableResult );
wp_die();
}
function eme_ajax_manage_countries() {
check_ajax_referer( 'eme_admin', 'eme_admin_nonce' );
if ( ! current_user_can( get_option( 'eme_cap_settings' ) ) ) {
$jTableResult['Result'] = 'Error';
$jTableResult['Message'] = __( 'Access denied!', 'events-made-easy' );
print wp_json_encode( $jTableResult );
wp_die();
}
if ( isset( $_REQUEST['do_action'] ) ) {
$do_action = eme_sanitize_request( $_REQUEST['do_action'] );
switch ( $do_action ) {
case 'deleteCountries':
eme_ajax_country_delete();
break;
}
}
wp_die();
}
function eme_ajax_manage_states() {
check_ajax_referer( 'eme_admin', 'eme_admin_nonce' );
if ( ! current_user_can( get_option( 'eme_cap_settings' ) ) ) {
wp_die();
}
if ( isset( $_REQUEST['do_action'] ) ) {
$do_action = eme_sanitize_request( $_REQUEST['do_action'] );
switch ( $do_action ) {
case 'deleteStates':
eme_ajax_record_delete( EME_STATES_TBNAME, 'eme_cap_settings', 'id' );
break;
}
}
wp_die();
}
function eme_ajax_country_delete() {
global $wpdb;
$countries_table = EME_DB_PREFIX . EME_COUNTRIES_TBNAME;
$states_table = EME_DB_PREFIX . EME_STATES_TBNAME;
check_ajax_referer( 'eme_admin', 'eme_admin_nonce' );
if ( ! current_user_can( get_option( 'eme_cap_settings' ) ) ) {
$jTableResult['Result'] = 'Error';
$jTableResult['Message'] = __( 'Access denied!', 'events-made-easy' );
print wp_json_encode( $jTableResult );
wp_die();
}
$ids_list = eme_sanitize_request($_POST['id']);
if ( eme_is_list_of_int( $ids_list ) ) {
$wpdb->query( "DELETE FROM $countries_table WHERE id in ($ids_list)" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->query( "UPDATE $states_table SET country_id=0 where country_id in ($ids_list)" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}
$jTableResult['Result'] = 'OK';
$jTableResult['Message'] = __( 'Country deleted!', 'events-made-easy' );
print wp_json_encode( $jTableResult );
wp_die();
}
function eme_ajax_state_edit() {
check_ajax_referer( 'eme_admin', 'eme_admin_nonce' );
if ( ! current_user_can( get_option( 'eme_cap_settings' ) ) ) {
$jTableResult['Result'] = 'Error';
$jTableResult['Message'] = __( 'Access denied!', 'events-made-easy' );
print wp_json_encode( $jTableResult );
wp_die();
}
$jTableResult = [];
if ( isset( $_POST['id'] ) ) {
$state = eme_get_state( intval( $_POST['id'] ) );