/
core.php
4191 lines (3604 loc) · 128 KB
/
core.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
/**
* Caldera Forms.
*
* @package Caldera_Forms
* @author David <david@digilab.co.za>
* @license GPL-2.0+
* @link
* @copyright 2014 David Cramer
*/
/**
* Caldera_Forms Plugin class.
* @package Caldera_Forms
* @author David Cramer <david@digilab.co.za>
*/
class Caldera_Forms {
/**
* @var string
*/
const VERSION = CFCORE_VER;
/**
* @var string
*/
protected $plugin_slug = 'caldera-forms';
/**
* @var string
*/
protected $screen_prefix = null;
/**
* @var object
*/
protected static $instance = null;
/**
* Initialize the plugin by setting localization, filters, and administration functions.
*
*/
function __construct() {
// Load plugin text domain
add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
// add element & fields filters
add_filter('caldera_forms_get_field_types', array( $this, 'get_internal_field_types'));
add_filter('caldera_forms_get_form_processors', array( $this, 'get_form_processors'));
add_filter('caldera_forms_submit_redirect_complete', array( $this, 'do_redirect'),10, 4);
add_action('caldera_forms_edit_end', array($this, 'calculations_templates') );
add_filter('caldera_forms_render_get_field', array( $this, 'auto_populate_options_field' ), 10, 2);
//add_filter('caldera_forms_render_get_field_type-radio', array( $this, 'auto_populate_options_field' ), 10, 2);
//add_filter('caldera_forms_render_get_field_type-checkbox', array( $this, 'auto_populate_options_field' ), 10, 2);
//add_filter('caldera_forms_render_get_field_type-dropdown', array( $this, 'auto_populate_options_field' ), 10, 2);
//add_filter('caldera_forms_render_get_field_type-toggle_switch', array( $this, 'auto_populate_options_field' ), 10, 2);
add_filter('caldera_forms_view_field_paragraph', 'wpautop' );
// magic tags
//add_filter('caldera_forms_render_magic_tag', array( $this, 'do_magic_tags'));
// mailser
add_filter('caldera_forms_get_magic_tags', array( $this, 'set_magic_tags'),1);
add_filter('caldera_forms_mailer', array( $this, 'mail_attachment_check'),10, 3);
// action
add_action('caldera_forms_submit_complete', array( $this, 'save_final_form'),50);
add_action("wp_ajax_get_entry", array( $this, 'get_entry') );
// find if profile is loaded
add_action('wp', array( $this, 'cf_init_system'));
// render shortcode
add_shortcode( 'caldera_form', array( $this, 'render_form') );
// modal shortcode
add_shortcode( 'caldera_form_modal', array( $this, 'render_modal_form') );
add_action( 'wp_footer', array( $this, 'render_footer_modals') );
// check update version
$version = get_option('_calderaforms_lastupdate');
if(empty($version) || version_compare($version, CFCORE_VER) < 0){
self::activate_caldera_forms();
update_option('_calderaforms_lastupdate',CFCORE_VER);
wp_redirect( $_SERVER['REQUEST_URI'] );
exit;
}
}
/**
* Load the plugin text domain for translation.
*
*/
public function load_plugin_textdomain() {
/*add_rewrite_tag('%cf_api%', '([^&]+)');
add_rewrite_tag('%cf_entry%', '([^&]+)');
// INIT API
add_rewrite_rule('^api/cf/([^/]*)/([^/]*)/?','index.php?cf_api=$matches[1]&cf_entry=$matches[2]','top');
add_rewrite_rule('^api/cf/([^/]*)/?','index.php?cf_api=$matches[1]','top');
//flush_rewrite_rules();
*/
load_plugin_textdomain( $this->plugin_slug, FALSE, basename( CFCORE_PATH ) . '/languages');
}
/// activator
public static function activate_caldera_forms(){
global $wpdb;
$version = get_option('_calderaforms_lastupdate');
if(!empty($version) ){
if( version_compare($version, CFCORE_VER) === 0 ){ // no change
//echo version_compare('1.1.1', CFCORE_VER);
return;
}
}
$tables = $wpdb->get_results("SHOW TABLES", ARRAY_A);
foreach($tables as $table){
$alltables[] = implode($table);
}
// meta table
if(!in_array($wpdb->prefix.'cf_form_entry_meta', $alltables)){
// create meta tables
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
$meta_table = "CREATE TABLE `" . $wpdb->prefix . "cf_form_entry_meta` (
`meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`entry_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`process_id` varchar(255) DEFAULT NULL,
`meta_key` varchar(255) DEFAULT NULL,
`meta_value` longtext,
PRIMARY KEY (`meta_id`),
KEY `meta_key` (`meta_key`),
KEY `entry_id` (`entry_id`)
) DEFAULT CHARSET=utf8;";
dbDelta( $meta_table );
}
if(!in_array($wpdb->prefix.'cf_form_entries', $alltables)){
// create tables
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
$entry_table = "CREATE TABLE `" . $wpdb->prefix . "cf_form_entries` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`form_id` varchar(18) NOT NULL DEFAULT '',
`user_id` int(11) NOT NULL,
`datestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`status` varchar(20) NOT NULL DEFAULT 'active',
PRIMARY KEY (`id`),
KEY `form_id` (`form_id`),
KEY `user_id` (`user_id`),
KEY `date_time` (`datestamp`),
KEY `status` (`status`)
) DEFAULT CHARSET=utf8;";
dbDelta( $entry_table );
$values_table = "CREATE TABLE `" . $wpdb->prefix . "cf_form_entry_values` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`entry_id` int(11) NOT NULL,
`field_id` varchar(20) NOT NULL,
`slug` varchar(255) NOT NULL DEFAULT '',
`value` longtext NOT NULL,
PRIMARY KEY (`id`),
KEY `form_id` (`entry_id`),
KEY `field_id` (`field_id`),
KEY `slug` (`slug`)
) DEFAULT CHARSET=utf8;";
dbDelta( $values_table );
}else{
if($version >= '1.1.5'){
return; // only if 1.1.4 or lower
}
// check for field_id from 1.0.4
$columns = $wpdb->get_results("SHOW COLUMNS FROM `" . $wpdb->prefix . "cf_form_entry_values`", ARRAY_A);
$fields = array();
foreach($columns as $column){
$fields[] = $column['Field'];
}
if(!in_array('field_id', $fields)){
$wpdb->query( "ALTER TABLE `" . $wpdb->prefix . "cf_form_entry_values` ADD `field_id` varchar(20) NOT NULL AFTER `entry_id`;" );
$wpdb->query( "CREATE INDEX `field_id` ON `" . $wpdb->prefix . "cf_form_entry_values` (`field_id`); ");
// update all entries
$forms = $wpdb->get_results("SELECT `id`,`form_id` FROM `" . $wpdb->prefix . "cf_form_entries`", ARRAY_A);
$known = array();
if( !empty($forms)){
foreach($forms as $form){
if(!isset($known[$form['form_id']])){
$config = get_option($form['form_id']);
if(empty($config)){
continue;
}
$known[$form['form_id']] = $config;
}else{
$config = $known[$form['form_id']];
}
foreach($config['fields'] as $field_id=>$field){
$wpdb->update($wpdb->prefix . "cf_form_entry_values", array('field_id'=>$field_id), array('entry_id' => $form['id'], 'slug' => $field['slug']));
}
}
}
}
// add status
$columns = $wpdb->get_results("SHOW COLUMNS FROM `" . $wpdb->prefix . "cf_form_entries`", ARRAY_A);
$fields = array();
if(!in_array('status', $fields)){
$wpdb->query( "ALTER TABLE `" . $wpdb->prefix . "cf_form_entries` ADD `status` varchar(20) NOT NULL DEFAULT 'active' AFTER `datestamp`;" );
$wpdb->query( "CREATE INDEX `status` ON `" . $wpdb->prefix . "cf_form_entries` (`status`); ");
}
}
}
public static function star_rating_viewer($value, $field, $form){
if(!is_admin()){
// only for the front
return $value;
}
$out = "<div style=\"color: " . $field['config']['color'] . "; font-size: 10px;\" >";
if(!empty($field['config']['number'])){
for( $i = 1; $i <= $field['config']['number']; $i++){
$star = 'raty-'.$field['config']['type'].'-off';
if( $i<= $value){
$star = 'raty-'.$field['config']['type'].'-on';
}
$out .= '<span data-alt="'.$i.'" class="'.$star.'" title="'.$i.'" style="margin-right: -2px;"></span> ';
}
}
$out .= '</div>';
return $out;
}
public static function handle_file_view($value, $field, $form){
if( is_array( $value ) ){
return $value;
}
return '<a href="' . $value .'" target="_blank">' . basename($value) .'</a>';
}
public static function mail_attachment_check($mail, $data, $form){
// check for
foreach($form['fields'] as $field_id=>$field){
if($field['type'] == 'file' && isset($field['config']['attach'])){
$dir = wp_upload_dir();
$file = str_replace($dir['baseurl'], $dir['basedir'], self::get_field_data($field_id, $form));
if(file_exists($file)){
$mail['attachments'][] = $file;
}
}
}
return $mail;
}
public static function captcha_check($value, $field, $form){
if( !isset( $_POST['g-recaptcha-response'] ) || empty( $_POST['g-recaptcha-response'] )){
return new WP_Error( 'error' );
}
$args = array(
'secret' => $field['config']['private_key'],
'response' => sanitize_text_field( $_POST['g-recaptcha-response'] )
);
$request = wp_remote_get( 'https://www.google.com/recaptcha/api/siteverify?' . build_query($args) );
$result = json_decode( wp_remote_retrieve_body( $request ) );
if( empty( $result->success ) ){
return new WP_Error( 'error', __("The wasn't entered correct.", 'caldera-forms') . ' <a href="#" class="reset_' . sanitize_text_field( $_POST[ $field['ID'] ] ) . '">' . __("Reset", 'caldera-forms') . '<a>.' );
}
return true;
}
public static function update_field_data($field, $entry_id, $form){
global $wpdb, $form;
$field_types = self::get_field_types();
// is capture?
if(isset($field_types[$form['fields'][$field['ID']]['type']]['setup']['not_supported'])){
if(in_array('entry_list', $field_types[$form['fields'][$field['ID']]['type']]['setup']['not_supported'])){
return;
}
}
$new_data = self::get_field_data($field['ID'], $form);
$original_data = self::get_field_data($field['ID'], $form, $entry_id);
if($original_data === $new_data){
// no change
return;
}
if( has_filter( 'caldera_forms_save_field' ) ){
$new_data = apply_filters( 'caldera_forms_update_field', $new_data, $field, $form );
}
if( has_filter( 'caldera_forms_save_field_' . $field['type'] ) ){
$new_data = apply_filters( 'caldera_forms_update_field_' . $field['type'], $new_data, $field, $form );
}
if($original_data === null){
// no entry - add first
$new_entry = array(
'entry_id' => $entry_id,
'field_id' => $field['ID'],
'slug' => $field['slug'],
'value' => $new_data,
);
$wpdb->insert($wpdb->prefix . 'cf_form_entry_values', $new_entry);
}else{
$wpdb->update($wpdb->prefix . 'cf_form_entry_values', array('value' => $new_data), array('entry_id' => $entry_id, 'field_id' => $field['ID']));
}
}
public static function save_field_data($field, $entry_id, $form){
global $wpdb, $form;
if(!empty($field['conditions']['type'])){
if(!self::check_condition($field['conditions'], $form)){
return;
}
}
$data = self::get_field_data($field['ID'], $form);
if(empty($data)){
return;
}
foreach((array) $data as $key=>$entry){
if( has_filter( 'caldera_forms_save_field' ) ){
$entry = apply_filters( 'caldera_forms_save_field', $entry, $field );
}
if( has_filter( 'caldera_forms_save_field_' . $field['type'] ) ){
$entry = apply_filters( 'caldera_forms_save_field_' . $field['type'], $entry, $field );
}
$field_item = array(
'entry_id' => $entry_id,
'field_id' => $field['ID'],
'slug' => $field['slug'],
'value' => self::do_magic_tags( $entry )
);
// named key kets .key to slug
if(!is_int($key)){
// Keyed
$keyed = true;
$field_item['slug'] .= '.'.$key;
}
// Save
$wpdb->insert($wpdb->prefix . 'cf_form_entry_values', $field_item);
}
if(!empty($keyed)){
if( has_filter( 'caldera_forms_save_field_combined' . $field['type'] ) ){
$data = apply_filters( 'caldera_forms_save_field_combined' . $field['type'], $entry, $field );
}
$field_item = array(
'entry_id' => $entry_id,
'field_id' => $field['ID'],
'slug' => $field['slug'],
'value' => json_encode( $data )
);
$wpdb->insert($wpdb->prefix . 'cf_form_entry_values', $field_item);
}
}
public static function save_final_form($form){
global $wpdb, $processed_meta, $transdata;
// check submit type (new or update)
if(isset($_POST['_cf_frm_edt'])){
// is edit
//check user can edit this item.
$user_id = get_current_user_id();
$details = self::get_entry_detail($_POST['_cf_frm_edt'], $form);
// check token
if(isset($_POST['_cf_frm_edt_tkn'])){
// build token
$token_array = array(
'id' => (int) $details['id'],
'datestamp' => $details['datestamp'],
'user_id' => (int) $details['user_id'],
'form_id' => $form['ID']
);
if( sha1( json_encode( $token_array ) ) !== trim( $_POST['_cf_frm_edt_tkn'] ) ){
return new WP_Error( 'error', __( "Permission denied.", "caldera-forms" ) );
}else{
$entryid = (int) $details['id'];
$edit_token = sha1( json_encode( $token_array ) );
}
}else{
if(!empty($user_id)){
if(!empty($details)){
// check user can edit
if( current_user_can( 'edit_posts' ) || $details['user_id'] === $user_id ){
$entryid = $_POST['_cf_frm_edt'];
}else{
return new WP_Error( 'error', __( "Permission denied.", "caldera-forms" ) );
}
}
}
}
}
if(!empty($form['db_support'])){
// new entry or update
if(empty($entryid)){
$entryid = self::get_field_data( '_entry_id', $form );
foreach($form['fields'] as $field_id=>$field){
// add new and update
self::save_field_data($field, $entryid, $form);
}
// save form meta if any
if(isset($processed_meta[$form['ID']])){
foreach($processed_meta[$form['ID']] as $process_id=>$meta_data){
foreach($meta_data as $meta_key=>$meta_value){
if(is_array($meta_value)){
foreach ($meta_value as &$check_value) {
if(is_array($check_value)){
foreach($check_value as &$sub_check_value){
if(!is_array($sub_check_value)){
$sub_check_value = self::do_magic_tags( $sub_check_value );
}
}
}else{
$check_value = self::do_magic_tags( $check_value );
}
}
}else{
$meta_value = self::do_magic_tags( $meta_value );
}
if(count($meta_value) > 1){
$meta_value = json_encode($meta_value);
}else{
$meta_value = $meta_value[0];
if(is_array($meta_value) || is_object($meta_value)){
$meta_value = json_encode($meta_value);
}
}
$meta_entry = array(
'entry_id' => $entryid,
'process_id' => $process_id,
'meta_key' => $meta_key,
'meta_value' => $meta_value
);
$wpdb->insert($wpdb->prefix . 'cf_form_entry_meta', $meta_entry);
}
}
}
// update status
$wpdb->update($wpdb->prefix . 'cf_form_entries', array('status' => 'active'), array( 'id' => $entryid ) );
}else{
// do update
foreach($form['fields'] as $field_id=>$field){
// add new and update
self::update_field_data($field, $entryid, $form);
}
if(isset($processed_meta[$form['ID']])){
foreach($processed_meta[$form['ID']] as $process_id=>$meta_data){
foreach($meta_data as $meta_key=>$meta_value){
if(count($meta_value) > 1){
$meta_value = json_encode($meta_value);
}else{
$meta_value = $meta_value[0];
}
$meta_entry = array(
'entry_id' => $entryid,
'process_id' => $process_id,
'meta_key' => $meta_key,
'meta_value' => $meta_value
);
$wpdb->insert($wpdb->prefix . 'cf_form_entry_meta', $meta_entry);
}
}
}
// return
return;
}
}
if(empty($form['mailer']['enable_mailer'])){
return;
}
// add entry ID to transient data
$transdata['entry_id'] = $entryid;
// do mailer!
$sendername = __('Caldera Forms Notification', 'caldera-forms');
if(!empty($form['mailer']['sender_name'])){
$sendername = $form['mailer']['sender_name'];
if( false !== strpos($sendername, '%')){
$isname = self::get_slug_data( trim($sendername, '%'), $form);
if(!empty( $isname )){
$sendername = $isname;
}
}
}
if(empty($form['mailer']['sender_email'])){
$sendermail = get_option( 'admin_email' );
}else{
$sendermail = $form['mailer']['sender_email'];
if( false !== strpos($sendermail, '%')){
$ismail = self::get_slug_data( trim($sendermail, '%'), $form);
if(is_email( $ismail )){
$sendermail = $ismail;
}
}
}
// use summary
if(empty($form['mailer']['email_message'])){
$form['mailer']['email_message'] = "{summary}";
}
$mail = array(
'recipients' => array(),
'subject' => self::do_magic_tags($form['mailer']['email_subject']),
'message' => stripslashes( $form['mailer']['email_message'] ) ."\r\n",
'headers' => array(
self::do_magic_tags( 'From: ' . $sendername . ' <' . $sendermail . '>' ),
),
'attachments' => array()
);
// if added a bcc
if( !empty( trim( $form['mailer']['bcc_to'] ) ) ){
$mail['headers'][] = self::do_magic_tags( 'Bcc: ' . trim( $form['mailer']['bcc_to'] ) );
}
// if added a replyto
if( !empty( trim( $form['mailer']['reply_to'] ) ) ){
$mail['headers'][] = self::do_magic_tags( 'Reply-To: <' . trim( $form['mailer']['reply_to'] ) . '>' );
}
// Filter Mailer first as not to have user input be filtered
$mail['message'] = self::do_magic_tags($mail['message']);
if($form['mailer']['email_type'] == 'html'){
$mail['headers'][] = "Content-type: text/html";
$mail['message'] = wpautop( $mail['message'] );
}
// get tags
preg_match_all("/%(.+?)%/", $mail['message'], $hastags);
if(!empty($hastags[1])){
foreach($hastags[1] as $tag_key=>$tag){
$tagval = self::get_slug_data($tag, $form);
if(is_array($tagval)){
$tagval = implode(', ', $tagval);
}
$mail['message'] = str_replace($hastags[0][$tag_key], $tagval, $mail['message']);
}
}
//$mail['message']
// ifs
preg_match_all("/\[if (.+?)?\](?:(.+?)?\[\/if\])?/", $mail['message'], $hasifs);
if(!empty($hasifs[1])){
// process ifs
foreach($hasifs[0] as $if_key=>$if_tag){
$content = explode('[else]', $hasifs[2][$if_key]);
if(empty($content[1])){
$content[1] = '';
}
$vars = shortcode_parse_atts( $hasifs[1][$if_key]);
foreach($vars as $varkey=>$varval){
if(is_string($varkey)){
$var = self::get_slug_data($varkey, $form);
if( in_array($varval, (array) $var) ){
// yes show code
$mail['message'] = str_replace( $hasifs[0][$if_key], $content[0], $mail['message']);
}else{
// nope- no code
$mail['message'] = str_replace( $hasifs[0][$if_key], $content[1], $mail['message']);
}
}else{
$var = self::get_slug_data($varval, $form);
if(!empty($var)){
// show code
$mail['message'] = str_replace( $hasifs[0][$if_key], $content[0], $mail['message']);
}else{
// no code
$mail['message'] = str_replace( $hasifs[0][$if_key], $content[1], $mail['message']);
}
}
}
}
}
if(!empty($form['mailer']['recipients'])){
$mail['recipients'] = explode(',', self::do_magic_tags( $form['mailer']['recipients']) );
}else{
$mail['recipients'][] = get_option( 'admin_email' );
}
$data = self::get_submission_data($form);
$submission = array();
foreach ($data as $field_id=>$row) {
if($row === null || !isset($form['fields'][$field_id]) ){
continue;
}
$key = $form['fields'][$field_id]['slug'];
if(is_array($row)){
if(!empty($row)){
$keys = array_keys($row);
if(is_int($keys[0])){
$row = implode(', ', $row);
}else{
$tmp = array();
foreach($row as $linekey=>$item){
if(is_array($item)){
$item = '( ' . implode(', ', $item).' )';
}
$tmp[] = $linekey.': '.$item;
}
$row = implode(', ', $tmp);
}
}else{
$row = null;
}
}
$mail['message'] = str_replace('%'.$key.'%', $row, $mail['message']);
$mail['subject'] = str_replace('%'.$key.'%', $row, $mail['subject']);
$submission[] = $row;
$labels[] = $form['fields'][$field_id]['label'];
}
// final magic
$mail['message'] = self::do_magic_tags( $mail['message'] );
$mail['subject'] = self::do_magic_tags( $mail['subject'] );
// CSV
if(!empty($form['mailer']['csv_data'])){
ob_start();
$df = fopen("php://output", 'w');
fputcsv($df, $labels);
fputcsv($df, $submission);
fclose($df);
$csv = ob_get_clean();
$csvfile = wp_upload_bits( uniqid().'.csv', null, $csv );
$mail['attachments'][] = $csvfile['file'];
}
if(empty($mail)){
return;
}
$mail = apply_filters( 'caldera_forms_mailer', $mail, $data, $form);
/*// recipients
foreach($mail['recipients'] as &$recipient){
// trim spaces
$recipient = trim($recipient);
}
$recipients = implode(',', $mail['recipients']);
*/
$headers = implode("\r\n", $mail['headers']);
do_action( 'caldera_forms_do_mailer', $mail, $data, $form);
// force message to string.
if( is_array( $mail['message'] ) ){
$mail['message'] = implode( "\n", $mail['message'] );
}
if( ! empty( $mail ) ){
// is send debug enabled?
if( !empty( $form['debug_mailer'] ) ){
add_action( 'phpmailer_init', array( 'Caldera_Forms', 'debug_mail_send' ), 1000 );
}
if( wp_mail( (array) $mail['recipients'], $mail['subject'], stripslashes( $mail['message'] ), $headers, $mail['attachments'] )){
// kill attachment.
if(!empty($csvfile['file'])){
if(file_exists($csvfile['file'])){
unlink($csvfile['file']);
}
}
}
}else{
if(!empty($csvfile['file'])){
if(file_exists($csvfile['file'])){
unlink($csvfile['file']);
}
}
}
}
/**
* creates a send log to debug mailer problems
*
*/
public static function debug_mail_send( $phpmailer ) {
global $transdata, $wpdb;
// this is a hack since there is not filter / action for a failed mail... yet
//$phpmailer->SMTPDebug = 3;
ob_start();
$phpmailer->SMTPDebug = 3;
try {
$phpmailer->Send();
} catch ( phpmailerException $e ) {
print_r( $phpmailer->ErrorInfo );
}
print_r( $phpmailer );
$phpmailer->SMTPDebug = 0;
$result = ob_get_clean();
$meta_entry = array(
'entry_id' => $transdata['entry_id'],
'process_id' => '_debug_log',
'meta_key' => 'debug_log',
'meta_value' => $result
);
$wpdb->insert($wpdb->prefix . 'cf_form_entry_meta', $meta_entry);
}
/**
* Return an instance of this class.
*
*
* @return object A single instance of this class.
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
public static function override_redirect_notice($notices, $form){
if(isset($form['processors'])){
foreach($form['processors'] as $processor){
if($processor['type'] == 'form_redirect'){
$notices['success']['note'] = self::do_magic_tags($processor['config']['message']);
}
}
}
return $notices;
}
public static function do_redirect($referrer, $form, $processid){
if(isset($form['processors'])){
foreach($form['processors'] as $processor){
if($processor['type'] == 'form_redirect'){
if(isset($processor['conditions']) && !empty($processor['conditions']['type'])){
if(!self::check_condition($processor['conditions'], $form)){
continue;
}
}
if(!empty($processor['config']['url'])){
// set message
add_filter('caldera_forms_render_notices', array('Caldera_Forms', 'override_redirect_notice' ), 10, 2 );
//passback urls
$referrer = parse_url( $referrer );
$query_vars = array();
if(!empty($referrer['query'])){
parse_str($referrer['query'], $referrer['query']);
if(isset($referrer['query']['cf_su'])){
unset($referrer['query']['cf_su']);
}
$query_vars = array_merge($query_vars, $referrer['query']);
}
// get vars in url
$base_redirect = self::do_magic_tags( $processor['config']['url'] );
$redirect = parse_url( $base_redirect );
if(!empty($redirect['query'])){
parse_str($redirect['query'], $redirect['query']);
$base_redirect = explode('?', $base_redirect, 2);
$query_vars = array_merge($redirect['query'], $query_vars);
$redirect = $base_redirect[0] . '?' . http_build_query($query_vars);
}else{
$redirect = $base_redirect . '?' . http_build_query($query_vars);
}
return $redirect;
}
}
}
}
return $referrer;
}
public static function send_auto_response($config, $form){
global $form;
// new filter to alter the config.
$config = apply_filters( 'caldera_forms_autoresponse_config', $config, $form);
// remove required bounds.
unset($config['_required_bounds']);
$message = $config['message'];
foreach($config as $tag=>&$value){
if($tag !== 'message'){
$message = str_replace('%'.$tag.'%', $value, $message);
$value = self::do_magic_tags( $value );
}
}
// set header
$headers[] = 'From: ' . $config['sender_name'] . ' <' . $config['sender_email'] . '>';
$message = self::do_magic_tags( $message );
// setup mailer
$subject = $config['subject'];
$email_message = array(
'recipients' => array(
$config['recipient_name'].' <'.$config['recipient_email'].'>'
),
'subject' => $subject,
'message' => $message,
'headers' => $headers,
'attachments' => array()
);
$email_message = apply_filters( 'caldera_forms_autoresponse_mail', $email_message, $config, $form);
do_action( 'caldera_forms_do_autoresponse', $config, $form);
// send mail
wp_mail( $email_message['recipients'], $email_message['subject'], implode( "\r\n", (array) $email_message['message'] ), implode("\r\n", (array) $email_message['headers']), $email_message['attachments'] );
}
// get built in form processors
public function get_form_processors($processors){
$internal_processors = array(
'auto_responder' => array(
"name" => __('Auto Responder', 'caldera-forms'),
"description" => __("Sends out an auto response e-mail", 'caldera-forms'),
"post_processor" => array($this, 'send_auto_response'),
"template" => CFCORE_PATH . "processors/auto_responder/config.php",
"default" => array(
'subject' => __('Thank you for contacting us', 'caldera-forms')
),
),
'form_redirect' => array(
"name" => __('Redirect', 'caldera-forms'),
"description" => __("Redirects user to URL on successful submit", 'caldera-forms'),
"template" => CFCORE_PATH . "processors/redirect/config.php",
"single" => false
),
'increment_capture' => array(
"name" => __('Increment Value', 'caldera-forms'),
"description" => __("Increment a value per entry.", 'caldera-forms'),
"processor" => array( $this, 'increment_value' ),
"template" => CFCORE_PATH . "processors/increment/config.php",
"single" => true,
"conditionals" => false,
"magic_tags" => array(
'increment_value'
)
)
);
// akismet
$wp_api_key = get_option( 'wordpress_api_key' );
if(!empty($wp_api_key)){
$internal_processors['akismet'] = array(
"name" => __('Akismet', 'caldera-forms'),
"description" => __("Anti-spam filtering", 'caldera-forms'),
"pre_processor" => array( $this, 'akismet_scanner'),
"template" => CFCORE_PATH . "processors/akismet/config.php",
"single" => false,
);
}
return array_merge( $processors, $internal_processors );
}
// incremenet value process
public function increment_value( $config, $form ){
// get increment value;
$increment_value = get_option('_increment_' . $config['processor_id'], $config['start'] );
update_option( '_increment_' . $config['processor_id'], $increment_value + 1 );
if( !empty( $config['field'] ) ){
self::set_field_data( $config['field'], $increment_value, $form );
}
return array('increment_value' => $increment_value );
}
static public function akismet_scanner($config, $form){
global $post;
$wp_api_key = get_option( 'wordpress_api_key' );
if(empty($wp_api_key)){
return array('type' => 'error', 'note' => __('Akismet not setup.'));
}
// set permalink
if($post->ID){
$permalink = get_permalink( $post->ID );
}else{
$permalink = get_home_url();
}
// is contact form or reg form
$regform = self::get_processor_by_type('user_register', $form);
if(!empty($regform)){
$type = 'signup';
}else{
$type = 'contact-form';
}
// Call to comment check
$data = array(
'blog' => get_home_url(),
'user_ip' => $_SERVER['REMOTE_ADDR'],
'user_agent' => $_SERVER['HTTP_USER_AGENT'],
'referrer' => $_SERVER['HTTP_REFERER'],
'permalink' => $permalink,
'comment_type' => $type,
'comment_author' => self::do_magic_tags($config['sender_name']),
'comment_author_email' => self::do_magic_tags($config['sender_email'])
);
if(!empty($config['url'])){
$data['comment_author_url'] = self::do_magic_tags($config['url']);
};