-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathsalesforce.php
1272 lines (912 loc) · 39 KB
/
salesforce.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Plugin Name: Brilliant Web-to-Lead for Salesforce
Plugin URI: http://wordpress.org/plugins/salesforce-wordpress-to-lead/
Description: Easily embed a contact form into your posts, pages or your sidebar, and capture the entries straight into Salesforce CRM. Also supports Web to Case and Comments to leads.
Author: BrilliantPlugins
Version: 2.7.3.9
Author URI: https://brilliantplugins.com/
License: GPL2
*/
// Yoast Plugin Helper Functions
require_once('lib/ov_plugin_tools.php');
// Filter Examples
if( defined('TR_DEVELOPMENT') && TR_DEVELOPMENT )
require_once('examples.php');
// Admin Class
if ( ! class_exists( 'Salesforce_Admin' ) ) {
require_once('lib/salesforce_admin.class.php');
}
$salesforce = new Salesforce_Admin();
// Comment to lead functions
require_once('lib/salesforce_comment_to_lead.php');
// Widget Class
if ( ! class_exists( 'Salesforce_WordPress_to_Lead_Widgets' ) ) {
require_once('lib/salesforce_widget.class.php');
add_action( 'widgets_init', 'salesforce_widget_func' );
}
function salesforce_widget_func() {
register_widget( 'Salesforce_WordPress_to_Lead_Widgets' );
}
// Procedural Functions
// TODO: wrap in a class
function salesforce_default_settings() {
$options = array();
$options['version'] = '2.0';
$options['successmsg'] = __('Success!','salesforce');
$options['errormsg'] = __('This field is required.','salesforce');
$options['emailerrormsg'] = __('The email address you entered is not valid.','salesforce');
$options['captchaerrormsg'] = __('The text you entered did not match the image.','salesforce');
$options['requiredfieldstext'] = __('These fields are required.','salesforce');
$options['sferrormsg'] = __('Failed to connect to Salesforce.com.','salesforce');
$options['submitbutton'] = __('Submit','salesforce');
$options['subject'] = __('Thank you for contacting %BLOG_NAME%','salesforce');
$options['showccuser'] = true;
$options['ccusermsg'] = __('Send me a copy','salesforce');
$options['email_sender'] = '';
$options['ccadmin'] = false;
$options['captcha'] = false;
$options['commentstoleads'] = false;
$options['commentsnamefields'] = false;
$options['usecss'] = true;
$options['wpcf7css'] = false;
$options['captcha_type'] = null;
$options['wpcf7jsfix'] = null;
$options['sslverify'] = null;
$options['layout'] = null;
$options['donotautoaddcolontolabels'] = null;
//$options['hide_salesforce_link']= true;
$options['forms'][1] = Salesforce_Admin::default_form();
update_option('salesforce2', $options);
return $options;
}
function salesforce_back_link($url){
return '<a href="'.$url.'">« '.__('Back to configuration page','salesforce').'</a>';
}
/**
* Sort input array by $subkey
* Taken from: http://php.net/manual/en/function.ksort.php
*/
function w2l_sksort(&$array, $subkey="id", $sort_ascending=false) {
if( !is_array( $array ) )
return $array;
$temp_array = array();
if (count($array))
$temp_array[key($array)] = array_shift($array);
foreach($array as $key => $val){
$offset = 0;
$found = false;
foreach($temp_array as $tmp_key => $tmp_val)
{
if(!$found and strtolower($val[$subkey]) > strtolower($tmp_val[$subkey])) {
$temp_array = array_merge( (array)array_slice($temp_array,0,$offset),
array($key => $val),
array_slice($temp_array,$offset)
);
$found = true;
}
$offset++;
}
if(!$found) $temp_array = array_merge($temp_array, array($key => $val));
}
if ($sort_ascending) $array = array_reverse($temp_array);
else $array = $temp_array;
}
function salesforce_captcha(){
include("lib/captcha/captcha.php");
die();
}
function get_salesforce_form_id( $form_id, $sidebar ){
return 'salesforce_w2l_lead_'.$form_id.str_replace(' ','_',$sidebar);
}
function salesforce_has_captcha( $form_id, $options ){
if( salesforce_get_option('captchaform', $form_id, $options ) == 'enabled' || ( salesforce_get_option('captchaform', $form_id, $options ) == '' && $options['captcha'] ) ){
return true;
}
return false;
}
// known WP_Query reserved request parameters
function salesforce_get_prefixed_inputs(){
$wp_query_reserved = array( 'attachment', 'attachment_id', 'author', 'author_name', 'cat', 'calendar', 'category_name', 'comments_popup', 'cpage', 'day', 'error', 'exact', 'feed', 'hour', 'm', 'minute', 'monthnum', 'more', 'name', 'order', 'orderby', 'p', 'page_id', 'page', 'paged', 'pagename', 'pb', 'post_type', 'posts', 'preview', 'robots', 's', 'search', 'second', 'sentence', 'static', 'subpost', 'subpost_id', 'taxonomy', 'tag', 'tag_id', 'tb', 'term', 'w', 'withcomments', 'withoutcomments', 'year', 'category__in', 'category__not_in', 'category__and', 'comments_per_page', 'offset', 'perm', 'post__in', 'post__not_in', 'post_mime_type', 'post_parent__in', 'tag__and', 'tag__in', 'tag__not_in', 'tag_id', 'tag_slug__and', 'tag_slug__in', 'meta_key', 'meta_value' );
return apply_filters('salesforce_w2l_get_prefixed_inputs', $wp_query_reserved);
}
// prefix certain field names to avoid collisions with WP Query reserved request parameters
function salesforce_get_input_name( $id ){
$inputs = salesforce_get_prefixed_inputs();
if( in_array( $id, $inputs ) ){
$prefix = apply_filters( 'salesforce_w2l_input_name_prefix', 'sf_' );
return $prefix . $id;
}else{
return $id;
}
}
// un-prefix certain field names to avoid collisions with WP Query reserved request parameters
function salesforce_get_input_id_from_name( $name ){
$inputs = salesforce_get_prefixed_inputs();
$prefix = apply_filters( 'salesforce_w2l_input_name_prefix', 'sf_' );
$id = substr( $name, strlen( $prefix ) + 1 );
if( in_array( $id, inputs ) ){
return $id;
}else{
return $name;
}
}
function salesforce_form($options, $is_sidebar = false, $errors = null, $form_id = 1) {
if( !isset($options['forms'][$form_id]) )
return;
$content = '';
/*
if (!empty($content))
$content = wpautop('<strong>'.$content.'</strong>');
*/
if ($options['usecss']) {
wp_enqueue_style( 'sfwp2lcss', plugins_url('/assets/css/sfwp2l.css', __FILE__) );
}
$label_location = salesforce_get_option('labellocation', $form_id, $options);
$sidebar = '';
if ( $is_sidebar )
$sidebar = ' sidebar';
if( !$label_location )
$label_location = 'top-aligned';
if( $is_sidebar )
$label_location = salesforce_get_option('labellocationsidebar', $form_id, $options);
if( $label_location == 'placeholders' )
wp_enqueue_script( 'sfwp2ljqph', plugins_url('/assets/js/jquery-placeholder/jquery.placeholder.js', __FILE__) );
if( $options['wpcf7css'] && $options['wpcf7jsfix'] )
wp_dequeue_script( 'contact-form-7');
$custom_css = '/salesforce-wordpress-to-lead/custom.css';
if( file_exists( get_stylesheet_directory() . $custom_css ) )
wp_enqueue_style( 'sfwp2lcsscustom', get_stylesheet_directory_uri() . $custom_css );
if ( $options['wpcf7css'] ) {
$content .= '<section class="form-holder clearfix"><div class="wpcf7">';
}
$sf_form_id = get_salesforce_form_id( $form_id, $sidebar );
$action = '#sf_form_'.$sf_form_id;
$action = apply_filters( 'salesforce_w2l_form_action', $action );
if( salesforce_has_captcha( $form_id, $options ) && salesforce_get_option('captcha_type', $form_id, $options ) == 'recaptcha' ){
wp_enqueue_script( 'wp2l_recaptcha_js', 'https://www.google.com/recaptcha/api.js' );
}
$content .= "\n".'<form id="sf_form_'.$sf_form_id.'" class="'.($options['wpcf7css'] ? 'wpcf7-form' : 'w2llead'.$sidebar ).' '.$label_location.'" method="post" action="'.$action.'">'."\n";
$reqtext = stripslashes( salesforce_get_option('requiredfieldstext',$form_id,$options) );
$date_fields = array();
if (!empty($reqtext) && salesforce_get_option('requiredfieldstextpos',$form_id,$options) == 'top' )
$content .= '<p class="sf_required_fields_msg" id="requiredfieldsmsg"><sup><span class="required">*</span></sup> '.esc_html( $reqtext ).'</p>';
foreach ($options['forms'][$form_id]['inputs'] as $id => $input) {
// get prefixed input name
$input_name = salesforce_get_input_name( $id );
if (!$input['show'])
continue;
if( ! isset( $input['opts'] ) )
$input['opts'] = null;
$val = '';
if ( isset( $_POST[ $input_name ] ) ){
$val = $_POST[ $input_name ];
if( is_array( $val ) ){
$val = array_map( 'esc_attr', array_map( 'salesforce_clean_field', $val ) );
}else{
$val = esc_attr(strip_tags(stripslashes($val)));
}
}else{
if( isset($input['value']) ) $val = esc_attr(strip_tags(stripslashes($input['value'])));
}
$val = apply_filters( 'salesforce_w2l_field_value', $val, sanitize_html_class( $id ), $form_id );
$val = apply_filters( 'salesforce_w2l_field_value_'.absint( $form_id ).'_'. $id, $val );
if($input['type'] != 'hidden' && $input['type'] != 'current_date') {
$content .= '<div class="sf_field sf_field_'.$id.' sf_type_'.$input['type'].'">';
}
$error = ' ';
if (isset($input['error']) && $input['error']) {
$error = ' error ';
}
if( $input['type'] == 'date' ){
$date_fields[$id] = $input;
}
if($input['type'] != 'hidden' && $input['type'] != 'current_date') {
if ($options['wpcf7css']) { $content .= '<p>'; }
if ($input['type'] == 'checkbox') {
if( isset( $_POST[ $input_name ] ) ){
$post_val = $_POST[ $input_name ];
}else{
$post_val = '';
}
$content .= "\t\n\t".'<input type="checkbox" id="sf_'.$id.'" class="w2linput checkbox" name="'.$input_name.'" value="'.$val.'" '.checked( $post_val, $val, false ).' />'."\n\n";
}
$placeholder = '';
if( $label_location == 'placeholders' && $input['type'] != 'checkbox' ){
$placeholder = stripslashes( strip_tags( $input['label'] ) );
if ($input['required'] && $input['type'] != 'hidden' && $input['type'] != 'current_date' && $input['type'] != 'select' && $input['type'] != 'multi-select')
$placeholder .= ' *';
//$placeholder = ' placeholder="'.$placeholder.'" ';
}else{
$required = '';
if( $input['required'] )
$required = 'required';
if (!empty($input['label'])) {
$content .= "\t".'<label class="w2llabel '.$required.' '.$error.$input['type'].($input['type'] == 'checkbox' ? ' w2llabel-checkbox-label' : '').'" for="sf_'.$id.'">'.( $input['opts'] == 'html' && $input['type'] == 'checkbox' ? stripslashes($input['label']) : esc_html(stripslashes($input['label'])));
if ( ! in_array($input['type'], array('checkbox', 'html') ) && ! salesforce_get_option('donotautoaddcolontolabels', $form_id, $options ) ) {
$content .= ':';
}
}
}
}
if( $label_location != 'placeholders' ){
if ($input['required'] && $input['type'] != 'hidden' && $input['type'] != 'current_date')
$content .= ' <sup><span class="required">*</span></sup>';
if($input['type'] != 'hidden' && $input['type'] != 'current_date') {
$content .= '</label>'."\n";
if ($options['wpcf7css']) { $content .= '<span class="wpcf7-form-control-wrap">'; }
}
}
if ($input['type'] == 'text') {
$content .= "\t".'<input type="text" placeholder="'.$placeholder.'" value="'.$val.'" id="sf_'.$id.'" class="';
$content .= $options['wpcf7css'] ? 'wpcf7-form-control wpcf7-text' : 'w2linput text';
$content .= $options['wpcf7css'] && $input['required'] ? ' wpcf7-validates-as-required required' : '';
$content .= '" name="'.$input_name.'" '.( !empty($input['opts']) ? ' placeholder="'.$input['opts'].'" title="'.$input['opts'].'"' : '' ).' />'."\n\n";
}else if ($input['type'] == 'email') {
$content .= "\t".'<input type="email" placeholder="'.$placeholder.'" value="'.$val.'" id="sf_'.$id.'" class="';
$content .= $options['wpcf7css'] ? 'wpcf7-form-control wpcf7-text' : 'w2linput text';
$content .= $options['wpcf7css'] && $input['required'] ? ' wpcf7-validates-as-required required' : '';
$content .= '" name="'.$input_name.'" '.( !empty($input['opts']) ? ' placeholder="'.$input['opts'].'" title="'.$input['opts'].'"' : '' ).' />'."\n\n";
}else if ($input['type'] == 'date') {
$content .= "\t".'<input type="text" placeholder="'.$placeholder.'" value="'.$val.'" id="sf_'.$id.'" class="';
$content .= $options['wpcf7css'] ? 'wpcf7-form-control wpcf7-text' : 'w2linput text';
$content .= $options['wpcf7css'] && $input['required'] ? ' wpcf7-validates-as-required required' : '';
$content .= '" name="'.$input_name.'" />'."\n\n";
} else if ($input['type'] == 'textarea') {
$content .= "\t".( !$options['wpcf7css'] ? "\n\n" : '' )."\n\t".'<textarea id="sf_'.$id.'" class="';
$content .= $options['wpcf7css'] ? 'wpcf7-form-control wpcf7-textarea' : 'w2linput textarea';
$content .= $options['wpcf7css'] && $input['required'] ? ' wpcf7-validates-as-required required' : '';
$content .= '" name="'.$input_name.'"'.( !empty($input['opts']) ? ' placeholder="'.$input['opts'].'" title="'.$input['opts'].'"' : '' ).' placeholder="'.$placeholder.'">'.$val.'</textarea>'."\n\n";
} else if ($input['type'] == 'hidden') {
$content .= "\t\n\t".'<input type="hidden" id="sf_'.$id.'" class="w2linput hidden" name="'.$input_name.'" value="'.$val.'" />'."\n\n";
} else if ($input['type'] == 'current_date') {
$content .= "\t\n\t".'<input type="hidden" id="sf_'.$id.'" class="w2linput hidden" name="'.$input_name.'" value="'.date($input['opts']).'" />'."\n\n";
} else if ($input['type'] == 'html'){
$content .= '<br>'.stripslashes($input['opts'])."\n\n";
} else if ($input['type'] == 'select' || $input['type'] == 'multi-select' ) {
$content .= "\t\n\t".'<select id="sf_'.$id.'" class="';
$content .= $options['wpcf7css'] ? 'wpcf7-form-control wpcf7-select style-select' : 'w2linput select';
$content .= $options['wpcf7css'] && $input['required'] ? ' wpcf7-validates-as-required required' : '';
if( $input['type'] == 'multi-select' ){
$content .= '" name="'.$input_name.'[]"';
$content .= ' multiple="multiple" ';
}else{
$content .= '" name="'.$input_name.'"';
}
$content .= '>';
if( $placeholder ){
if( $input['required'] ){
$content .= '<option value="" default disabled selected="selected">'. trim( $placeholder ) . ': *</option>' . "\n";
}else{
$content .= '<option value="" default selected="selected">'. trim( $placeholder ) . ':</option>' . "\n";
}
}
if( is_array( $val ) ){
$values = $val;
}else{
$values = array( $val );
}
// remove excess whitespace to avoid false positive checks for newlines
$input['opts'] = trim( $input['opts'] );
if ( strpos($input['opts'], "\n") !== false && substr_count($input['opts'], "|\n") <= 1 && substr_count($input['opts'], "|\r\n") <= 1) {
// Newlines and pipes
$delim1 = "\n";
$delim2 = "|";
}else{
// pipes and colons
$delim1 = "|";
$delim2 = ":";
}
if (strpos( $input['opts'], $delim1) !== false ) {
$opts = explode( $delim1, trim( $input['opts'] ) );
foreach ( $opts AS $opt ) {
if (strpos( $opt, $delim2 ) !== false) {
list ($k, $v) = explode($delim2, $opt);
} else {
$k = $v = $opt;
}
$v = trim(esc_attr(strip_tags(stripslashes($v))));
if( $placeholder ){
$content .= '<option value="' . esc_attr($v) . '">' . trim( stripslashes( $k ) ) . '</option>' . "\n";
}else{
$content .= '<option value="' . esc_attr($v) . '" '. selected( in_array($v, $values), true, false ).'>' . trim( stripslashes( $k ) ) . '</option>' . "\n";
}
}
}
$content .= '</select>'."\n\n";
//$content .= '<pre>'.print_r( $values, 1 ).'</pre>';
}
if( $errors && !$errors[$id]['valid'] ){
$content .= "\t\n\t<span class=\"error_message\">". $errors[$id]['message'].'</span>';
}
if($input['type'] != 'hidden' && $input['type'] != 'current_date') {
if ($options['wpcf7css']) { $content .= '</span></p>'; }
$content .= '<div class="clearfix"></div></div>';
}
}
//captcha
if( salesforce_has_captcha( $form_id, $options ) ){
if( salesforce_get_option('captcha_type', $form_id, $options ) == 'recaptcha' ){
// Use Google ReCaptcha
$content .= '<div class="sf_field sf_field_recaptcha sf_type_recaptcha">';
$content .= '<br>';
if( $sidebar ){
$content .= '<div class="g-recaptcha" data-size="compact" data-sitekey="' . esc_attr( salesforce_get_option('recaptcha_site_key', $form_id, $options ) ) . '"></div>';
}else{
$content .= '<div class="g-recaptcha" data-sitekey="' . esc_attr( salesforce_get_option('recaptcha_site_key', $form_id, $options ) ) . '"></div>';
}
if( $errors && !$errors['recaptcha']['valid'] ){
$content .= "<span class=\"error_message\">" . $errors['recaptcha']['message'] . '</span>';
}
$content .= '</div>';
}else{
// Use built in captcha system
// attempt to disable caching
if ( !defined( 'DONOTCACHEPAGE' ) )
define( 'DONOTCACHEPAGE', true );
if ( !defined( 'DONOTCACHEOBJECT' ) )
define( 'DONOTCACHEOBJECT', true );
include("lib/captcha/captcha.php");
$captcha = captcha();
//$content .= 'CODE='.$captcha['code'].'<hr>';
$sf_hash = sha1($captcha['code'].NONCE_SALT);
set_transient( $sf_hash, $captcha['code'], 60*15 );
$label = __('Type the text shown: *','salesforce');
$content .= '<div class="sf_field sf_field_captcha sf_type_captcha">';
$content .= '<label class="w2llabel">'.$label.'</label>'."\n\n".'
<img class="w2limg" src="' . $captcha['image_src'] . '&hash=' . $sf_hash . '" alt="CAPTCHA image" />'."\n\n";
$content .= '<input type="text" class="w2linput text captcha" name="captcha_text" value="" />';
if( $errors && !$errors['captcha']['valid'] ){
$content .= "<span class=\"error_message\">" . $errors['captcha']['message'] . '</span>';
}
$content .= '<input type="hidden" class="w2linput hidden" name="captcha_hash" value="'. $sf_hash .'" />';
$content .= '</div>';
}
}
//send me a copy
if( $options['showccuser'] ){
$label = $options['ccusermsg'];
if( empty($label) ) $label = __('Send me a copy','salesforce');
$content .= "\t\n\t".'<div class="sf_field sf_field_cb sf_type_checkbox sf_cc_user"><label class="w2llabel checkbox w2llabel-checkbox-label"><input type="checkbox" name="w2lcc" class="w2linput checkbox" value="1" '.checked(1, salesforce_get_post_data('w2lcc') , false).' /> '.esc_html($label)."</label></div>\n";
}
//spam honeypot
$content .= "\t".'<input type="text" name="message" class="w2linput" value="" style="display: none;" />'."\n";
//form id
$content .= "\t".'<input type="hidden" name="form_id" class="w2linput" value="'.$form_id.'" />'."\n";
$submit = stripslashes( salesforce_get_option( 'submitbutton', $form_id, $options ) );
if (empty($submit))
$submit = "Submit";
$content .= "\t";
if ($options['wpcf7css']) {
$content .= '<p class="punt">';
} else {
$content .= '<div class="w2lsubmit">';
}
$content .= '<input type="submit" name="w2lsubmit" class="';
if ($options['wpcf7css']) {
$content .= 'wpcf7-form-control wpcf7-submit btn';
} else {
$content .= 'w2linput submit';
}
$content .= '" value="'.esc_attr($submit).'" />'."\n";
if ($options['wpcf7css']) {
$content .= '</p>';
} else {
$content .= '</div>';
}
$content .= '</form>'."\n";
if (!empty($reqtext) && salesforce_get_option('requiredfieldstextpos',$form_id,$options) == '' )
$content .= '<p class="sf_required_fields_msg" id="requiredfieldsmsg"><sup><span class="required">*</span></sup> '.esc_html( $reqtext ).'</p>';
if ( $options['wpcf7css'] ) {
$content .= '</section>';
}
if( $label_location == 'placeholder' )
$content .= '<script>jQuery( document ).ready( function($) { $(".salesforce_w2l_lead input, .salesforce_w2l_lead textarea").placeholder(); } );
</script>';
if( true )
$content = str_replace("\n",'', $content);
if( $date_fields ){
wp_enqueue_script('jquery-ui-datepicker');
wp_enqueue_style('jquery-style', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css');
$content .= "<script>jQuery(document).ready(function( $ ) {";
foreach( $date_fields as $id => $date_field ){
$options = trim( stripslashes( $date_field['opts'] ) );
if( !$options ){
$options = "dateFormat : 'yy-mm-dd',
changeYear: true";
}
$content .= "
jQuery('#sf_".$id."').datepicker({
".$options."
});
";
}
$content .= "});</script>";
}
$content = apply_filters('salesforce_w2l_form_html', $content);
return $content;
}
function salesforce_get_post_data( $index ){
if( isset( $_POST[$index] ) ){
return $_POST[$index];
}else{
return false;
}
}
function submit_salesforce_form( $post, $options ) {
global $wp_version;
$form_id = absint( $_POST['form_id'] );
$org_id = salesforce_get_option('org_id', $form_id, $options);
//echo '$org_id='.$org_id;
if ( !$org_id )
$org_id = $options['org_id']; // fallback to global
if ( !$org_id ) {
error_log( "Salesforce: No SalesForce Organization ID set." );
return false;
}
//spam honeypot
if( !empty($_POST['message']) ) {
error_log( "Salesforce: No message set." );
return false;
}
//print_r($_POST); //DEBUG
//echo $org_id;
$post['oid'] = $org_id; // web to lead
$post['orgid'] = $org_id; // web to case
if( !isset( $post['lead_source'] ) ){
if (!empty($options['forms'][$form_id]['source'])) {
$post['lead_source'] = str_replace('%URL%','['.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].']',$options['forms'][$form_id]['source']);
}
}
$post['lead_source'] = apply_filters('salesforce_w2l_lead_source', $post['lead_source'], $form_id);
//$post['debug'] = 0;
/*
$body = '';
foreach( $post as $k => $v ){
if( is_array( $v ) ){
foreach( $v as $i ){
$body .= '&'.urlencode($k).'='.urlencode($i);
}
}else{
$body .= '&'.urlencode($k).'='. urlencode($v);
}
}
$body = substr( $body, 1 );
*/
$form_type = $options['forms'][$form_id]['type'];
// Filter arguments before generating POST to SF
$post = apply_filters( 'salesforce_w2l_post_data', $post, $form_id, $form_type );
$body = preg_replace('/%5B[0-9]+%5D/simU', '', http_build_query($post) ); // remove php style arrays for array values [1]
//echo $body .'<hr>';
$sslverify = false;
// setting to override
if( !empty( $options['sslverify'] ) )
$sslverify = (bool) $options['sslverify'];
// Set SSL verify to false because of server issues, unless setting is set... a filter can also be used to override arguments
$args = array(
'body' => $body,
'headers' => array(
'Content-Type' => 'application/x-www-form-urlencoded',
'user-agent' => 'Brilliant Web-to-Lead for Salesforce plugin - WordPress/'.$wp_version.'; '.get_bloginfo('url'),
),
'sslverify' => $sslverify,
);
$args = apply_filters( 'salesforce_w2l_post_args', $args );
if( $form_type == 'case' ){
$url = 'https://webto.salesforce.com/servlet/servlet.WebToCase?encoding=UTF-8';
}else{
$url = 'https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
}
// Do we need to change the URL we're submitting to?
$url = apply_filters( 'salesforce_w2l_api_url', $url, $form_type, $post );
// Pre submit actions
do_action( 'salesforce_w2l_before_submit', $post, $form_id, $form_type );
$result = wp_remote_post( $url, $args );
// Test broken submit
//$result = new WP_Error( 'broke', __( "I've fallen and can't get up", "my_textdomain" ) );
if( is_wp_error($result) ) {
error_log( "Salesforce HTTP error: " . print_r( $result, true ) );
do_action( 'salesforce_w2l_error_submit', $result, $post, $form_id, $form_type );
$subject = __( 'Salesforce Web to %%type%% Error', 'salesforce' );
$append = print_r( $result, 1 );
salesforce_cc_admin( $post, $options, $form_id, $subject, $append );
return false;
}
if ($result['response']['code'] == 200){
// Post submit actions
do_action( 'salesforce_w2l_after_submit', $post, $form_id, $form_type );
unset( $_POST['oid'] );
unset( $_POST['org_id'] );
if( isset( $_POST['w2lcc'] ) && $_POST['w2lcc'] == 1 )
salesforce_cc_user($post, $options, $form_id);
salesforce_cc_admin($post, $options, $form_id);
// Prevent multiple form submissions by clearing key data
unset( $_POST['form_id'] );
unset( $_POST['w2lsubmit'] );
return true;
}else{
error_log( "Salesforce response error: " . print_r( $result, true ) );
return false;
}
}
function salesforce_cc_user( $post, $options, $form_id = 1 ){
$from_name = salesforce_get_option( 'emailfromname', $form_id, $options );
if( !$from_name )
$from_name = get_bloginfo('name');
$from_email = salesforce_get_option( 'emailfromaddress', $form_id, $options );
if( !$from_email )
$from_email = get_option('admin_email');
$from_name = apply_filters('salesforce_w2l_cc_user_from_name', $from_name );
$from_email = apply_filters('salesforce_w2l_cc_user_from_email', $from_email );
$headers = 'From: '.$from_name.' <' . $from_email . ">\r\n";
if (!empty($options['forms'][$form_id]['cc_email_subject'])) {
$subject = str_replace('%BLOG_NAME%', get_bloginfo('name'), $options['forms'][$form_id]['cc_email_subject']);
} else {
$subject = str_replace('%BLOG_NAME%', get_bloginfo('name'), $options['subject']);
}
if( empty($subject) ) $subject = __('Thank you for contacting','salesforce').' '.get_bloginfo('name');
//remove hidden fields
foreach ($options['forms'][$form_id]['inputs'] as $id => $input) {
if( $input['type'] == 'hidden' )
unset( $post[$id] );
}
if (!empty($options['forms'][$form_id]['source'])) {
unset($post['lead_source']);
}
$remove_keys = apply_filters( 'salesforce_w2l_cc_user_suppress_fields', array('debug','debugEmail','oid','orgid') );
foreach( $remove_keys as $key ){
unset($post[$key]);
}
$message = '';
//format message
foreach($post as $name => $value){
if( isset( $options['forms'][$form_id]['inputs'][$name]['label'] ) ){
$label = trim( $options['forms'][$form_id]['inputs'][$name]['label'] );
}else{
$label = '';
}
if( !empty( $name ) && !empty( $value ) && !empty( $label ) ){
$message .= stripslashes($label).': '.salesforce_maybe_implode(',', $value)."\r\n";
}
}
$message = apply_filters('salesforce_w2l_cc_user_email_content', $message );
if( defined( WP_DEBUG ) && WP_DEBUG )
error_log( 'salesforce_cc_user:'.print_r( array($message),1 ) );
if( $message )
wp_mail( $_POST['email'], $subject, $message, $headers );
}
function salesforce_maybe_implode( $delimiter, $data ){
if( is_array($data) )
return trim( implode( $delimiter, $data ) );
return $data;
}
function salesforce_cc_admin( $post, $options, $form_id = 1, $subject = '', $append = '' ){
if( $options['forms'][$form_id]['type'] == 'case' ){
$form_type = __( 'Case', 'salesforce' );
}else{
$form_type = __( 'Lead', 'salesforce' );
}
if( !$subject )
$subject = '[' . __( 'Salesforce Web to %%type%% Submission', 'salesforce' ) . ']';
$subject = str_replace( '%%type%%', $form_type, $subject );
$subject .= ' ' . $options['forms'][$form_id]['form_name'];
$from_name = salesforce_get_option( 'emailfromname', $form_id, $options );
if( !$from_name )
$from_name = get_bloginfo('name');
$from_email = salesforce_get_option( 'emailfromaddress', $form_id, $options );
if( !$from_email )
$from_email = get_option('admin_email');
$from_name = apply_filters('salesforce_w2l_cc_admin_from_name', $from_name);
$from_email = apply_filters('salesforce_w2l_cc_admin_from_email', $from_email);
$headers = 'From: '.$from_name.' <' . $from_email . ">\r\n";
if (get_option('email_sender') != '') {
$headers .= 'Sender: '.get_option('email_sender')."\r\n";
}
$replyto_email = sanitize_email( apply_filters('salesforce_w2l_cc_admin_replyto_email', $post['email'] ) );
if( $replyto_email && is_email( $replyto_email ) ){
$headers .= 'Reply-to: ' . $replyto_email . "\r\n";
}
$message = '';
//unset($post['debug']);
//unset($post['debugEmail']);
//format message
foreach($post as $name=>$value){
if( isset( $options['forms'][$form_id]['inputs'][$name]['label'] ) ){
$label = trim( $options['forms'][$form_id]['inputs'][$name]['label'] );
}else{
$label = '';
}
if( !empty($value) && ! empty( $label ) ){
if( $label != '' && $name != 'lead_source' )
$message .= stripslashes($label).': '. salesforce_maybe_implode( ';', $value ) . "\r\n";
}
}
if ( $post['lead_source'] ) {
$message .= "\r\n".'Lead Source: '.salesforce_maybe_implode( ';', $post['lead_source'] )."\r\n";
}
// add form info
$message .= "\r\n".'Form ID: '. $form_id . "\r\n".'Form Editor: ' . add_query_arg( array( 'page' => 'salesforce-wordpress-to-lead', 'tab' => 'form', 'id' => $form_id ), admin_url( 'options-general.php' ) ) ."\r\n";
if( $append ){
$message .= "\r\n".'= Addditional Information ='."\r\n\r\n".$append."\r\n";
}
$emails = array();
// cc admin?
if( isset( $options['ccadmin'] ) && $options['ccadmin'] )
$emails[] = get_option('admin_email');
// cc others?
if( isset( $options['ccothers'] ) && $options['ccothers'] ){
$others = explode( ',', $options['ccothers'] );
if( count( $others ) ){
foreach( $others as $other ){
$emails[] = trim( $other );
}
}
}
$emails = apply_filters( 'salesforce_w2l_cc_admin_email_list', $emails );
//print_r( $emails );
$message = apply_filters('salesforce_w2l_cc_admin_email_content', $message );
$subject = apply_filters('salesforce_w2l_cc_admin_email_subject', $subject, $form_type, $post );
if( WP_DEBUG )
error_log( 'salesforce_cc_admin:'.print_r( array($emails,$message,$subject),1 ) );
if( $message ){
foreach( $emails as $email ){
wp_mail( $email, $subject, $message, $headers );
}
}
}
function salesforce_form_shortcode($atts) {
extract( shortcode_atts( array(
'form' => '1',
'sidebar' => false,
), $atts ) );
$emailerror = '';
$captchaerror = '';
$content = '';
$form = (int) $form;
$sidebar = (bool) $sidebar;
$options = get_option("salesforce2");
if (!is_array($options))
$options = salesforce_default_settings();
//don't submit unless we're in the right shortcode
if( isset( $_POST['form_id'] ) ){
$form_id = intval( $_POST['form_id'] );
if( $form_id != $form ){
$content = salesforce_form($options, $sidebar, null, $form);
$layout = salesforce_get_option('layout', $form, $options );
if( $layout ){
$layout = 'sf_' . $layout;
}
return '<div class="salesforce_w2l_lead ' . sanitize_html_class( $layout ) . '">' . $content . '</div>';
}
}
//this is the right form, continue
if( isset( $_POST['w2lsubmit'] ) ) {
$error = false;
$post = array();
$has_error = false;
// field validation
foreach ($options['forms'][$form]['inputs'] as $id => $input) {
// get prefixed input name
$input_name = salesforce_get_input_name( $id );
if( isset( $_POST[$input_name] ) ){
$val = $_POST[$input_name];
if( is_array($val) ){
$val = array_map( 'trim', $val );
}else{
$val = trim( $val );
}
}else{
$val = '';
}
$error = array(
'valid' => false,
'message' => $options['errormsg'],
);
if ( $input['show'] && $input['required'] && strlen( salesforce_maybe_implode( ';', $val ) ) == 0 ) {
$error['valid'] = false;
}else{
$error['valid'] = true;
}
if ( ( ($id == 'email' && $input['required'] ) || ( $input['type'] == 'email' && $val ) ) && !is_email($val) ) {
$error['valid'] = false;
if( isset( $options['emailerrormsg'] ) && $options['emailerrormsg'] ){
$error['message'] = $options['emailerrormsg'];
}else{
// backwards compatibility
$error['message'] = __('The email address you entered is not valid.','salesforce');
}
}
$error = apply_filters('sfwp2l_validate_field', $error, $id, $val, $options['forms'][$form]['inputs'][$id] );
//$error = apply_filters('sfwp2l_'.$id, $error, $id, $options['forms'][$form]['inputs'][$id] );
$errors[$id] = $error;