-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmailtarget-form.php
More file actions
807 lines (753 loc) · 27 KB
/
mailtarget-form.php
File metadata and controls
807 lines (753 loc) · 27 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
<?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
/**
* Plugin Name: MTARGET Form
* Description: The MTARGET plugin to simplify embedding MTARGET Form in your post or as widget, also easily to set MTARGET Forms as popup.
* Version: 2.1.8
* Author: MTARGET Teams
* Author URI: https://mtarget.co/
* License: GPL V3
* Requires at least: 4.0.0
* Tested up to: 6.6.2
*
* @package MailtargetForm
* @author {{author_name}} <{{author_email}}>
* @copyright {{author_copyright}}
* @license {{author_license}}
* @link {{author_url}}
*/
define( 'MAILTARGET_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'MAILTARGET_PLUGIN_URL', plugins_url( '', __FILE__ ) );
if ( ! class_exists( 'MailtargetApi' ) ) {
require_once MAILTARGET_PLUGIN_DIR . '/lib/MailtargetApi.php';
}
/**
* MyClass Class Doc Comment
*
* @category Class
* @package MyClass
* @author A N Other
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
* @link http://www.hashbangcode.com/
*/
class MailtargetFormPlugin {
/**
* Instance
*
* @var null
*/
private static $instance = null;
/**
* Plugin path
*
* @var string
*/
private $plugin_path;
/**
* Plugin Url
*
* @var string
*/
private $plugin_url;
/**
* Text domain
*
* @var string
*/
private $text_domain = '';
/**
* Option Group
*
* @var string
*/
private $option_group = 'mtg-form-group';
/**
* Ajax Post
*
* @var string
*/
private $ajax_post = false;
/**
* Creates or returns an instance of this class.
*/
public static function get_instance() {
// If an instance hasn't been created and set to $instance create an instance and set it to $instance.
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Initializes the plugin by setting localization, hooks, filters, and administrative functions.
*/
private function __construct() {
$this->plugin_path = plugin_dir_path( __FILE__ );
$this->plugin_url = plugin_dir_url( __FILE__ );
load_plugin_textdomain( $this->text_domain, false, $this->plugin_path . '\lang' );
add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'register_styles' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'register_admin_styles' ) );
register_activation_hook( __FILE__, array( $this, 'activation' ) );
register_deactivation_hook( __FILE__, array( $this, 'deactivation' ) );
add_action( 'admin_menu', array( $this, 'set_admin_menu' ) );
add_action( 'admin_init', array( $this, 'register_setting' ) );
add_action( 'admin_init', array( $this, 'handling_admin_post' ) );
add_action( 'init', array( $this, 'handling_post' ) );
}
/**
* Get plugin url
*/
public function get_plugin_url() {
return $this->plugin_url;
}
/**
* Get Plugin Path
*/
public function get_plugin_path() {
return $this->plugin_path;
}
/**
* Place code that runs at plugin activation here.
*/
public function activation() {
global $wpdb;
$table_name = $wpdb->base_prefix . 'mailtarget_forms';
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$charset_collate = ' CHARACTER SET utf8mb4 COLLATE utf8mb4_bin';
$sql = 'CREATE TABLE IF NOT EXISTS ' . $table_name . " (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
form_id tinytext NOT NULL,
name tinytext NOT NULL,
type tinyint(1) default '1' NOT NULL,
data text NOT NULL,
PRIMARY KEY (id)
) DEFAULT " . $charset_collate . ';';
dbDelta( $sql );
}
/**
* Place code that runs at plugin deactivation here.
*/
public function deactivation() {
}
/**
* Enqueue and register JavaScript files here.
*/
public function register_scripts() {
wp_register_script( 'mailtarget-jquery', esc_url( '/wp-includes/js/jquery/jquery.min.js' ), array( 'jquery' ), '3.6.1' ); //phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter
wp_register_script( 'mailtarget-action', esc_url( MAILTARGET_PLUGIN_URL . '/assets/js/main.js' ), array( 'mailtarget-jquery' ), '1.1.2', true );
wp_register_script( 'mailtarget-tingle', esc_url( MAILTARGET_PLUGIN_URL . '/assets/js/tingle/tingle.min.js' ), array( 'mailtarget-jquery' ), '1.1.2' ); //phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter
wp_enqueue_script( 'mailtarget-jquery' );
wp_enqueue_script( 'mailtarget-action' );
wp_enqueue_script( 'mailtarget-tingle' );
wp_localize_script( 'mailtarget-action', 'WPURLS', array( 'siteurl' => get_option( 'siteurl' ) ) );
}
/**
* Enqueue and register CSS files here.
*/
public function register_styles() {
wp_register_style( 'mailtarget_style', esc_url( MAILTARGET_PLUGIN_URL . '/assets/css/style.css' ), '2.2.1' ); //phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
wp_register_style( 'mailtarget_tingle_style', esc_url( MAILTARGET_PLUGIN_URL . '/assets/js/tingle/tingle.min.css' ), '2.2.1' ); //phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
wp_enqueue_style( 'mailtarget_style' );
wp_enqueue_style( 'mailtarget_tingle_style' );
}
/**
* Register Admin Styles
*/
public function register_admin_styles() {
?>
<link rel="stylesheet" href="<?php echo esc_url( MAILTARGET_PLUGIN_URL . '/assets/css/mailtarget_admin.css' ); ?>" type="text/css" media="all" /><?php //phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet ?>
<?php
}
/**
* Register Setting
*/
public function register_setting() {
register_setting( $this->option_group, 'mtg_api_token' );
register_setting( $this->option_group, 'mtg_company_id' );
register_setting( $this->option_group, 'mtg_popup_enable' );
register_setting( $this->option_group, 'mtg_popup_form_id' );
register_setting( $this->option_group, 'mtg_popup_form_name' );
register_setting( $this->option_group, 'mtg_popup_delay' );
register_setting( $this->option_group, 'mtg_popup_title' );
register_setting( $this->option_group, 'mtg_popup_description' );
register_setting( $this->option_group, 'mtg_popup_submit' );
register_setting( $this->option_group, 'mtg_popup_redirect' );
}
/**
* Handling Admin Post
*/
public function handling_admin_post() {
$verify = isset( $_GET['_wpnonce'] ) ? wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'delete_action' ) : false;
if ( $verify ) {
$get_action = isset( $_GET['action'] ) ? sanitize_key( $_GET['action'] ) : null;
if ( null !== $get_action ) {
if ( 'delete' === $get_action ) {
$id = isset( $_GET['id'] ) ? sanitize_text_field( wp_unslash( $_GET['id'] ) ) : null;
if ( null === $id ) {
return false;
}
global $wpdb;
$wpdb->delete( $wpdb->base_prefix . 'mailtarget_forms', array( 'id' => $id ) );// WPCS: db call ok. // WPCS: cache ok.
return wp_safe_redirect( 'admin.php?page=mailtarget-form-plugin--admin-menu' );
}
}
}
$verify = isset( $_POST['_wpnonce'] ) ? wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'wpnonce_action' ) : false;
if ( $verify ) {
$post_action = isset( $_POST['mailtarget_form_action'] ) ? sanitize_key( $_POST['mailtarget_form_action'] ) : null;
if ( null === $post_action ) {
return false;
}
switch ( $post_action ) {
case 'setup_setting':
$api_token = isset( $_POST['mtg_api_token'] ) ? sanitize_text_field( wp_unslash( $_POST['mtg_api_token'] ) ) : null;
$popup_enable = isset( $_POST['mtg_popup_enable'] ) && intval( $_POST['mtg_popup_enable'] ) === 1 ? 1 : 0;
$data = array(
'mtg_api_token' => $api_token,
'mtg_popup_enable' => $popup_enable,
);
$api = $this->get_api( $data['mtg_api_token'] );
if ( ! $api ) {
return false;
}
$team = $api->get_team();
$redirect = 'admin.php?page=mailtarget-form-plugin--admin-menu-config';
if ( ! is_wp_error( $team ) ) {
$redirect .= '&success=1';
update_option( 'mtg_company_id', $team['companyId'] );
update_option( 'mtg_api_token', $data['mtg_api_token'] );
update_option( 'mtg_popup_enable', $data['mtg_popup_enable'] );
}
wp_safe_redirect( $redirect );
break;
case 'popup_config':
$popup_form_id = isset( $_POST['popup_form_id'] ) && '' !== $_POST['popup_form_id'] ? sanitize_text_field( wp_unslash( $_POST['popup_form_id'] ) ) : null;
$popup_form_name = isset( $_POST['popup_form_name'] ) && '' !== $_POST['popup_form_name'] ? sanitize_text_field( wp_unslash( $_POST['popup_form_name'] ) ) : __( 'Join for Newsletter', 'mailtarget' );
$popup_form_delay = isset( $_POST['popup_delay'] ) && intval( $_POST['popup_delay'] ) > 0 ? intval( $_POST['popup_delay'] ) : 10;
$popup_title = isset( $_POST['popup_title'] ) && '' !== $_POST['popup_title'] ? sanitize_text_field( wp_unslash( $_POST['popup_title'] ) ) : __( 'Join form', 'mailtarget' );
$popup_desc = isset( $_POST['popup_description'] ) && '' !== $_POST['popup_description'] ? sanitize_textarea_field( wp_unslash( $_POST['popup_description'] ) ) : __( 'Please send me your newsletter', 'mailtarget' );
$popup_redirect = isset( $_POST['popup_redirect'] ) && '' !== $_POST['popup_redirect'] ? esc_url_raw( wp_unslash( $_POST['popup_redirect'] ) ) : null;
$popup_enable = isset( $_POST['mtg_popup_enable'] ) && intval( $_POST['mtg_popup_enable'] ) === 1 ? 1 : 0;
update_option( 'mtg_popup_form_id', $popup_form_id );
update_option( 'mtg_popup_form_name', $popup_form_name );
update_option( 'mtg_popup_delay', $popup_form_delay );
update_option( 'mtg_popup_title', $popup_title );
update_option( 'mtg_popup_description', $popup_desc );
update_option( 'mtg_popup_redirect', $popup_redirect );
update_option( 'mtg_popup_enable', $popup_enable );
wp_safe_redirect( 'admin.php?page=mailtarget-form-plugin--admin-menu-popup-main&success=1' );
break;
case 'create_widget':
global $wpdb;
$table_name = $wpdb->base_prefix . 'mailtarget_forms';
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$form_id = isset( $_POST['form_id'] ) && '' !== $_POST['form_id'] ? sanitize_text_field( wp_unslash( $_POST['form_id'] ) ) : null;
$widget_name = isset( $_POST['widget_name'] ) && '' !== $_POST['widget_name'] ? sanitize_text_field( wp_unslash( $_POST['widget_name'] ) ) : __( 'Newsletter Form', 'mailtarget' );
$widget_title = isset( $_POST['widget_title'] ) && '' !== $_POST['widget_title'] ? sanitize_text_field( wp_unslash( $_POST['widget_title'] ) ) : __( 'Newsletter Form', 'mailtarget' );
$widget_desc = isset( $_POST['widget_description'] ) && '' !== $_POST['widget_description'] ? sanitize_textarea_field( wp_unslash( $_POST['widget_description'] ) ) : __( 'Please send me your newsletter', 'mailtarget' );
$widget_submit = isset( $_POST['widget_submit_desc'] ) && '' !== $_POST['widget_submit_desc'] ? sanitize_text_field( wp_unslash( $_POST['widget_submit_desc'] ) ) : __( 'Submit', 'mailtarget' );
$widget_redir = isset( $_POST['widget_redir'] ) && '' !== $_POST['widget_redir'] ? sanitize_text_field( wp_unslash( $_POST['widget_redir'] ) ) : null;
$input = array(
'time' => current_time( 'mysql' ),
'form_id' => $form_id,
'name' => $widget_name,
'type' => 1,
'data' => wp_json_encode(
array(
'widget_title' => $widget_title,
'widget_description' => $widget_desc,
'widget_submit_desc' => $widget_submit,
'widget_redir' => $widget_redir,
)
),
);
$wpdb->insert( $table_name, $input );// WPCS: db call ok.
break;
case 'edit_widget':
global $wpdb;
$table_name = $wpdb->base_prefix . 'mailtarget_forms';
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$widget_id = isset( $_POST['widget_id'] ) && '' !== $_POST['widget_id'] ? sanitize_text_field( wp_unslash( $_POST['widget_id'] ) ) : null;
$widget_name = isset( $_POST['widget_name'] ) && '' !== $_POST['widget_name'] ? sanitize_text_field( wp_unslash( $_POST['widget_name'] ) ) : __( 'Newsletter Form', 'mailtarget' );
$widget_title = isset( $_POST['widget_title'] ) && '' !== $_POST['widget_title'] ? sanitize_text_field( wp_unslash( $_POST['widget_title'] ) ) : __( 'Newsletter Form', 'mailtarget' );
$widget_desc = isset( $_POST['widget_description'] ) && '' !== $_POST['widget_description'] ? sanitize_textarea_field( wp_unslash( $_POST['widget_description'] ) ) : __( 'Please send me your newsletter', 'mailtarget' );
$widget_submit = isset( $_POST['widget_submit_desc'] ) && '' !== $_POST['widget_submit_desc'] ? sanitize_text_field( wp_unslash( $_POST['widget_submit_desc'] ) ) : __( 'Submit', 'mailtarget' );
$widget_redir = isset( $_POST['widget_redir'] ) && '' !== $_POST['widget_redir'] ? sanitize_text_field( wp_unslash( $_POST['widget_redir'] ) ) : null;
$input = array(
'time' => current_time( 'mysql' ),
'name' => $widget_name,
'type' => 1,
'data' => wp_json_encode(
array(
'widget_title' => $widget_title,
'widget_description' => $widget_desc,
'widget_submit_desc' => $widget_submit,
'widget_redir' => $widget_redir,
)
),
);
if ( null !== $widget_id ) {
$wpdb->update( $table_name, $input, array( 'id' => $widget_id ) );// WPCS: db call ok. // WPCS: cache ok.
}
break;
default:
break;
}
}
}
/**
* Handling Ajax Post
*/
public function handling_ajax_post() {
$this->is_ajax = true;
return $this->handling_post();
}
/**
* Handling Post
*/
public function handling_post() {
$action = isset( $_POST['mailtarget_form_action'] ) ? sanitize_key( $_POST['mailtarget_form_action'] ) : null;
if ( null === $action ) {
return false;
}
$verify = isset( $_POST['_wpnonce'] ) ? wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'wpnonce_action' ) : false;
if ( $verify ) {
switch ( $action ) {
case 'submit_form':
$id = isset( $_POST['mailtarget_form_id'] ) ? sanitize_key( $_POST['mailtarget_form_id'] ) : null;
$this->ajax_post = isset( $_POST['mailtarget_ajax_post'] );
$api = $this->get_api();
if ( ! $api ) {
return;
}
$form = $api->get_form_detail( $id );
if ( is_wp_error( $form ) ) {
$this->error_response( 'Failed to get form data' );
die();
}
$input = array();
if ( ! isset( $form['component'] ) ) {
$this->error_response( 'form data not valid' );
die();
}
foreach ( $form['component'] as $item ) {
$setting = $item['setting'];
$input_val = isset( $_POST[ 'mtin__' . $setting['name'] ] ) ?
sanitize_text_field( wp_unslash( $_POST[ 'mtin__' . $setting['name'] ] ) ) : null;
$input[ $setting['name'] ] = $input_val;
if ( 'inputMultiple' === $item['type']
&& $setting['showOtherOption']
&& 'mtiot__' . $setting['name'] === $input_val
) {
$input_val = isset( $_POST[ 'mtino__' . $setting['name'] ] ) ?
sanitize_text_field( wp_unslash( $_POST[ 'mtino__' . $setting['name'] ] ) ) : null;
$input[ $setting['name'] ] = $input_val;
}
if ( 'inputCheckbox' === $item['type'] ) {
$in = isset( $_POST[ 'mtin__' . $setting['name'] ] ) ?
(array) sanitize_key( $_POST[ 'mtin__' . $setting['name'] ] ) : array();
$in = array_map( 'sanitize_text_field', $in );
$use_other = isset( $_POST[ 'mtiot__' . $setting['name'] ] )
&& sanitize_text_field( wp_unslash( $_POST[ 'mtiot__' . $setting['name'] ] ) ) === 'yes' ? true : false;
if ( $setting['showOtherOption'] && $use_other ) {
$other_input = isset( $_POST[ 'mtino__' . $setting['name'] ] ) ?
sanitize_text_field( wp_unslash( $_POST[ 'mtino__' . $setting['name'] ] ) ) : null;
if ( null !== $other_input ) {
$in[] = $other_input;
}
}
$input[ $setting['name'] ] = join( ', ', $in );
}
if ( 'inputPhone' === $item['type'] ) {
$input_val = isset( $_POST[ 'mtin__' . $setting['name'] ] ) ?
sanitize_text_field( wp_unslash( $_POST[ 'mtin__' . $setting['name'] ] ) ) : null;
$input[ $setting['name'] ] = $input_val;
}
}
$submit_url = $form['url'];
$res = $api->submit( $input, $submit_url );
if ( is_wp_error( $res ) ) {
$this->error_response( $this->submit_error_process( $res ) );
die();
}
$url = wp_get_referer();
$form_mode = isset( $_POST['mailtarget_form_mode'] ) ?
sanitize_text_field( wp_unslash( $_POST['mailtarget_form_mode'] ) ) : null;
if ( null !== $form_mode ) {
$popup_url = esc_url( get_option( 'mtg_popup_redirect' ) );
if ( 'popup' === $form_mode && '' !== $popup_url ) {
$url = $popup_url;
}
}
if ( isset( $_POST['mailtarget_form_redir'] ) && sanitize_key( $_POST['mailtarget_form_redir'] ) ) {
$url = esc_url_raw( wp_unslash( $_POST['mailtarget_form_redir'] ) );
}
if ( $this->ajax_post ) {
echo wp_json_encode(
array(
'code' => 200,
'msg' => 'ok',
)
);
die();
} else {
wp_safe_redirect( $url );
}
break;
default:
break;
}
} else {
wp_die( esc_html_e( 'Security check', 'mailtarget' ) );
}
}
/**
* Error Response
*
* @param string $msg message.
* @param array $data data.
*/
public function error_response( $msg, $data = array() ) {
if ( $this->ajax_post ) {
echo wp_json_encode(
array(
'code' => 400,
'msg' => $msg,
'data' => $data,
)
);
} else {
echo esc_attr( $msg );
}
}
/**
* Submit Error Process
*
* @param array $err error.
*/
public function submit_error_process( $err ) {
$msg = 'Failed to submit form';
if ( isset( $err->{0} ) ) {
$err = $err->{0};
}
if ( isset( $err->errors ) ) {
$err = $err->errors;
}
if ( isset( $err['mailtarget-error'] ) ) {
$err = $err['mailtarget-error'];
}
if ( isset( $err[0] ) ) {
$err = $err[0];
}
if ( isset( $err['data'] ) ) {
$err = $err['data'];
}
if ( isset( $err['code'] ) ) {
$msg = $err['message'];
}
return $msg;
}
/**
* Set Admin Menu
*/
public function set_admin_menu() {
add_menu_page(
'MTARGET Form',
'MTARGET Form',
'manage_options',
'mailtarget-form-plugin--admin-menu',
null,
MAILTARGET_PLUGIN_URL . '/assets/image/mt-icon-20x20.png'
);
add_submenu_page(
'mailtarget-form-plugin--admin-menu',
'List Form',
'List Form',
'manage_options',
'mailtarget-form-plugin--admin-menu',
array( $this, 'list_widget_view' )
);
add_submenu_page(
'mailtarget-form-plugin--admin-menu',
'New Form',
'New Form',
'manage_options',
'mailtarget-form-plugin--admin-menu-widget-form',
array( $this, 'add_widget_view_form' )
);
add_submenu_page(
'mailtarget-form-plugin--admin-menu',
'Popup Setting',
'Popup Setting',
'manage_options',
'mailtarget-form-plugin--admin-menu-popup-main',
array( $this, 'add_popup_view' )
);
add_submenu_page(
'mailtarget-form-plugin--admin-menu',
'Form Api Setting',
'Setting',
'manage_options',
'mailtarget-form-plugin--admin-menu-config',
array( $this, 'admin_config_view' )
);
add_submenu_page(
null,
'Edit Form',
'Edit Form',
'manage_options',
'mailtarget-form-plugin--admin-menu-widget-edit',
array( $this, 'edit_widget_view' )
);
add_submenu_page(
null,
'New Form',
'New Form',
'manage_options',
'mailtarget-form-plugin--admin-menu-widget-add',
array( $this, 'add_widget_view' )
);
}
/**
* List Widget View
*/
public function list_widget_view() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html_e( 'You do not have sufficient permissions to access this page.', 'mailtarget' ) );
}
$valid = $this->is_key_valid();
if ( true === $valid ) {
global $wpdb, $forms;
if ( ! current_user_can( 'edit_posts' ) ) {
return false;
}
$widgets = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->base_prefix . 'mailtarget_forms' );// WPCS: db call ok. // WPCS: cache ok.
require_once MAILTARGET_PLUGIN_DIR . '/views/admin/wp-form-list.php';
}
}
/**
* Add Widget View Form
*/
public function add_widget_view_form() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html_e( 'You do not have sufficient permissions to access this page.', 'mailtarget' ) );
}
$verify = isset( $_GET['_wpnonce'] ) ? wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'admin-menu-widget-form-action' ) : false;
$valid = $this->is_key_valid();
if ( true === $valid ) {
$api = $this->get_api();
if ( ! $api ) {
return null;
}
$pg = isset( $_GET['pg'] ) ? intval( $_GET['pg'] ) : 1;
$forms = $api->get_form_list( $pg );
if ( is_wp_error( $forms ) ) {
$error = $forms;
require_once MAILTARGET_PLUGIN_DIR . '/views/admin/error.php';
return false;
}
require_once MAILTARGET_PLUGIN_DIR . '/views/admin/form-list.php';
}
}
/**
* Add Widget View
*/
public function add_widget_view() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html_e( 'You do not have sufficient permissions to access this page.', 'mailtarget' ) );
}
$verify = isset( $_GET['_wpnonce'] ) ? wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'admin-menu-widget-form-action' ) : false;
$valid = $this->is_key_valid();
if ( true === $valid ) {
$form_id = isset( $_GET['form_id'] ) ? sanitize_text_field( wp_unslash( $_GET['form_id'] ) ) : null;
if ( null === $form_id ) {
return false;
}
$api = $this->get_api();
if ( ! $api ) {
return null;
}
$form = $api->get_form_detail( $form_id );
if ( is_wp_error( $form ) ) {
$error = $form;
require_once MAILTARGET_PLUGIN_DIR . '/views/admin/error.php';
return false;
}
require_once MAILTARGET_PLUGIN_DIR . '/views/admin/wp_form_add.php';
}
}
/**
* Edit Widget View
*/
public function edit_widget_view() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html_e( 'You do not have sufficient permissions to access this page.', 'mailtarget' ) );
}
$verify = isset( $_REQUEST['_wpnonce'] ) ? wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'edit_action' ) : false;
if ( $verify ) {
$valid = $this->is_key_valid();
if ( true === $valid ) {
global $wpdb;
$widget_id = isset( $_GET['id'] ) ? sanitize_key( $_GET['id'] ) : null;
$widget = $wpdb->get_row(
'SELECT * FROM ' . $wpdb->base_prefix .
"mailtarget_forms where id = $widget_id" //phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
); // WPCS: db call ok. // WPCS: cache ok.
if ( ! isset( $widget->form_id ) ) {
wp_safe_redirect( 'admin.php?page=mailtarget-form-plugin--admin-menu' );
return false;
}
$api = $this->get_api();
if ( ! $api ) {
return null;
}
$form = $api->get_form_detail( $widget->form_id );
if ( is_wp_error( $form ) ) {
$error = $form;
require_once MAILTARGET_PLUGIN_DIR . '/views/admin/error.php';
return false;
}
require_once MAILTARGET_PLUGIN_DIR . '/views/admin/wp_form_edit.php';
}
} else {
wp_die( esc_html_e( 'Security check', 'mailtarget' ) );
}
}
/**
* Admin Config View
*/
public function admin_config_view() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html_e( 'You do not have sufficient permissions to access this page.', 'mailtarget' ) );
}
$valid = $this->is_key_valid( true );
if ( false !== $valid ) {
require_once MAILTARGET_PLUGIN_DIR . '/views/admin/setup.php';
}
}
/**
* Add Popup View
*/
public function add_popup_view() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html_e( 'You do not have sufficient permissions to access this page.', 'mailtarget' ) );
}
$valid = $this->is_key_valid();
if ( true === $valid ) {
$form_id = '';
$form_name = '';
$verify = isset( $_REQUEST['_wpnonce'] ) ? wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'edit_action' ) : false;
$get_form_id = isset( $_GET['form_id'] ) ? sanitize_text_field( wp_unslash( $_GET['form_id'] ) ) : null;
if ( null !== $get_form_id ) {
$api = $this->get_api();
if ( ! $api ) {
return;
}
$form = $api->get_form_detail( $get_form_id );
if ( ! is_wp_error( $form ) ) {
$form_id = $form['formId'];
$form_name = $form['name'];
} else {
$error = $form;
require_once MAILTARGET_PLUGIN_DIR . '/views/admin/error.php';
return false;
}
}
require_once MAILTARGET_PLUGIN_DIR . '/views/admin/form_popup.php';
}
}
/**
* Is Key Valid
*
* @param boolean $setup setup.
*/
public function is_key_valid( $setup = false ) {
if ( $this->get_key() === '' && false === $setup ) {
$error = array( 'code' => 101 );
require_once MAILTARGET_PLUGIN_DIR . '/views/admin/error.php';
return null;
}
$api = $this->get_api();
if ( ! $api ) {
return null;
}
$valid = $api->ping();
if ( is_wp_error( $valid ) ) {
if ( $this->get_code_from_error( $valid ) === 400 ) {
return null;
}
if ( $this->get_code_from_error( $valid ) === 401 ) {
return null;
}
if ( $this->get_code_from_error( $valid ) === 32 && $setup ) {
return null;
}
$error = $valid;
require_once MAILTARGET_PLUGIN_DIR . '/views/admin/error.php';
return false;
}
$company_id = $this->get_company_id();
if ( '' === $company_id ) {
$cek = $api->get_team();
if ( is_wp_error( $cek ) ) {
if ( $this->get_code_from_error( $valid ) === 32 && $setup ) {
return null;
}
$error = $cek;
require_once MAILTARGET_PLUGIN_DIR . '/views/admin/error.php';
return false;
}
update_option( 'mtg_company_id', $cek['companyId'] );
}
return true;
}
/**
* Get Api
*
* @param boolean $key key.
*/
public function get_api( $key = false ) {
if ( ! $key ) {
$key = $this->get_key();
}
if ( ! $key ) {
return false;
}
$company_id = $this->get_company_id();
return new MailtargetApi( $key, $company_id );
}
/**
* Get key
*/
public function get_key() {
return esc_attr( get_option( 'mtg_api_token' ) );
}
/**
* Get company id
*/
public function get_company_id() {
return esc_attr( get_option( 'mtg_company_id' ) );
}
/**
* Get code from error
*
* @param array $error error.
*/
public function get_code_from_error( $error ) {
$error = (array) $error;
if ( isset( $error['errors'] ) ) {
$error = $error['errors'];
}
if ( isset( $error['mailtarget-error'] ) ) {
$error = $error['mailtarget-error'];
}
if ( isset( $error[0] ) ) {
return $error[0]['code'];
}
return false;
}
}
require_once MAILTARGET_PLUGIN_DIR . 'include/mailtarget_shortcode.php';
require_once MAILTARGET_PLUGIN_DIR . 'include/mailtarget_widget.php';
require_once MAILTARGET_PLUGIN_DIR . 'include/mailtarget_popup.php';
MailtargetFormPlugin::get_instance();