-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwidget.php
More file actions
1128 lines (1040 loc) · 45.4 KB
/
Copy pathwidget.php
File metadata and controls
1128 lines (1040 loc) · 45.4 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
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
class emfluence_email_signup extends WP_Widget {
function __construct(){
/* Widget settings. */
$widget_ops = array( 'classname' => 'emfluence_email_signup', 'description' => 'Creates an email signup form for your visitors.' );
/* Widget control settings. */
$control_ops = array( 'width' => 400, 'id_base' => 'emfluence_email_signup' );
/* Create the widget. */
parent::__construct( 'emfluence_email_signup', 'emfluence Marketing Platform Email Signup', $widget_ops, $control_ops );
}
/**
* Provides a static cache for groups
* @return Emfl_Group[] | NULL
*/
static function get_groups(){
static $groups = NULL;
if( $groups !== NULL ) return $groups;
require_once 'libraries/emfl_platform_api/response_objects/group.class.inc';
$cached = wp_cache_get('emfluence:groups');
$cache_valid = TRUE;
if(is_array($cached)) foreach($cached as $group) {
$has_required_properties = property_exists($group, 'groupName') && property_exists($group, 'groupID');
if(!$has_required_properties) $cache_valid = FALSE;
}
if($cache_valid && !empty($cached)) $groups = $cached;
if( NULL !== $groups ) return $groups;
$options = get_option('emfluence_global');
try {
$api = emfluence_get_api($options['api_key']);
} catch(Exception $e) {
return NULL;
}
$groups = array();
$more = TRUE;
$page_number = 1;
while($more){
$response = $api->groups_search(array(
'rpp' => 250,
'page' => $page_number,
'type' => 'Static'
));
if( !$response || !$response->success ){
$more = FALSE;
break;
}
foreach( $response->data->records as $group ){
$groups[$group->groupID] = $group;
}
if( empty($response->data->paging['next']) ){
$more = FALSE;
} else {
++$page_number;
}
}
wp_cache_set('emfluence:groups', $groups, '', 5*60);
return $groups;
}
/**
* Validates an email
*
* @param string $email
* @return boolean
*/
protected function validate_email($email) {
// Ensure the basic pattern is correct
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
return false;
}
// Test the domain's MX records to avoid more fake domains in the correct pattern.
list($user, $domain) = explode('@', $email);
try {
if( !checkdnsrr($domain, 'MX') ){
return false;
}
} catch( Exception $e ){
return false;
}
// Check domain blacklist
$options = get_option('emfluence_global');
if(!empty($options['blacklist_domains'])) {
$blacklisted_domains = array_map(
function ($domain) { return strtolower(trim($domain)); },
array_filter(explode(PHP_EOL, $options['blacklist_domains']))
);
if(in_array(strtolower($domain), $blacklisted_domains, true)) return false;
}
return true;
}
/**
* @return string
*/
protected function get_current_page_url() {
$pageURL = 'http';
if (isset($_SERVER["HTTPS"]) AND ($_SERVER["HTTPS"] == "on")) {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
/**
* Validate submitted values against widget settings.
* Return value is empty if validation passed.
* @param array $fields as saved in $instance
* @param array $values sanitized, trimmed submitted values
* @return string[]
*/
protected function widget_validate($fields, $values) {
// honeypot
if(!empty($values['numerical-validation'])) return array(array( 'type' => 'error', 'value' => 'Please enter the correct math answer to prove that you are a real person.'));
$defaults = $this->form_get_defaults();
$messages = array();
foreach( $fields as $key => $field ){
if( $field['required'] && ($field['type'] === 'true-false') && !isset($values[$key])) {
$messages[] = array( 'type' => 'error', 'field' => $field, 'value' => __( $field['required_message'] ) );
} elseif( $field['required'] && ($field['type'] !== 'true-false') && empty( $values[$key] ) ){
$messages[] = array( 'type' => 'error', 'field' => $field, 'value' => __( $field['required_message'] ) );
} elseif(empty($values[$key])) continue;
$field_name = isset($defaults['fields'][$key]) ? $defaults['fields'][$key]['name'] : str_replace(':', '', $field['label']);
switch($key) {
case 'email':
$field['type'] = 'email';
break;
}
switch($field['type']) {
case 'email':
if(!$this->validate_email( $values[$key] )) {
$messages[] = array( 'type' => 'error', 'field' => $field, 'value' => sprintf(__('%s: Invalid email address or blacklisted email domain.'), $field_name) );
}
break;
case 'number':
if(!is_numeric($values[$key])) {
$messages[] = array( 'type' => 'error', 'field' => $field, 'value' => sprintf(__('%s: Must be numeric.'), $field_name) );
}
break;
case 'date':
$time = strtotime($values[$key]);
if(empty($time)) {
$messages[] = array( 'type' => 'error', 'field' => $field, 'value' => sprintf(__('%s: Must be a date.'), $field_name) );
}
break;
}
}
return $messages;
}
/**
* Wrap the widget's content and return the html to output.
* @param string $content
* @return string
*/
protected function widget_wrap_content($args, $content, $instance) {
$title = apply_filters( 'Email Signup', empty( $instance[ 'title' ] ) ? __( 'Email Signup' ) : $instance[ 'title' ] );
if( $title ) $title = $args['before_title'] . '<span>' . $title . '</span>' . $args['after_title'];
$output = $args['before_widget'] . '<form id="' . esc_attr($args['widget_id']) . '" class="mail-form" method="post" action="#' . esc_attr($args['widget_id']) . '"><div class="holder"><div class="frame">';
$output .= $title;
$output .= $content;
$output .= '</div></div></form>' . $args['after_widget'];
return $output;
}
function widget( $args, $instance ) {
$values = array();
if( !empty( $instance[ 'groups' ] ) ) $lists = implode(',', $instance['groups'] );
// Ensure we can't sign people up without lists
if( empty($lists) ){
$output = '<p>' . __('Please select lists visitors may sign up for.') . '</p>' . "\n";
$output .= '<p>' . __('Powered by emfluence.') . '</p>' . "\n";
print $this->widget_wrap_content($args, $output, $instance);
return;
}
/**
* Form processing
*/
if( !empty($_POST) && ($_POST['action'] == 'email_signup') && ($_POST['form-id'] === $args['widget_id']) ){
$defaults = $this->form_get_defaults();
// Set the field values in case there's an error
foreach( $_POST as $key => $value ){
if(!is_string($value)) continue;
if('form-id' === $key) continue;
$values[$key] = htmlentities( trim( $value ) );
}
$messages = $this->widget_validate($instance['fields'], $values);
/**
* Filter: emfl_widget_validate
* @param array $messages If empty, the form validates successfully.
* @param array $instance Configuration of this widget.
* @pram string[] $values Submitted values, keyed by element names.
* @return array
* Any errors. Each element sould be a string.
*/
$messages = apply_filters('emfl_widget_validate', $messages, $instance, $values);
array_filter($values);
if( empty($messages) ){
// Try to subscribe them
$data = array();
$data['groupIDs'] = !empty($_POST['groups'])? $_POST['groups'] : '';
$data['originalSource'] = trim( $_POST['source'] );
if( array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER) ){
$forwarded_ip_addresses = array_values(array_filter(explode(',',$_SERVER['HTTP_X_FORWARDED_FOR'])));
$data['ipaddress'] = array_pop($forwarded_ip_addresses);
} else if( array_key_exists('REMOTE_ADDR', $_SERVER) ) {
$data['ipaddress'] = $_SERVER["REMOTE_ADDR"];
} else if( array_key_exists('HTTP_CLIENT_IP', $_SERVER) ) {
$data['ipaddress'] = $_SERVER["HTTP_CLIENT_IP"];
}
// basic contact fields
foreach($instance['fields'] as $key=>$field) {
if(empty($values[$key]) || empty($defaults['fields'][$key])) continue;
$platform_key = $defaults['fields'][$key]['platform'];
$data[$platform_key] = trim( $_POST[$key] );
}
// custom variables
$data['customFields'] = array();
for( $i = 1; $i <= 255; $i++ ){
$field = 'custom' . $i;
$parameter = 'custom_' . $i;
if( !array_key_exists($parameter, $_POST) && empty($_POST[$parameter]) ){
continue;
}
$data['customFields'][$field] = array(
'value' => trim( $_POST[$parameter] ),
);
}
// Deprecated filter 'emfl_widget_custom_fields'
$data['customFields'] = apply_filters('emfl_widget_custom_fields', $data['customFields']);
/* Allow others to insert values into the contact record (ex. user id's, etc).
* Note that the field keys in $instance['fields'] are like "custom_1"
* but the keys in $data['customFields'] need to be like "custom1".
* Remember to add custom fields in the format array('value' => $value).
*/
$data = apply_filters('emfl_widget_before_contact_save', $data, $instance);
// Only save non-empty custom fields
foreach($data['customFields'] as $key => $field ){
if(empty($field['value'])) {
unset($data['customFields'][$key]);
}
}
if(empty($data['customFields'])) unset($data['customFields']);
try {
$options = get_option('emfluence_global');
$api = emfluence_get_api($options['api_key']);
$result = $api->contacts_save($data);
} catch(Exception $e) {
$result = NULL;
}
if( empty($result) ) {
wp_mail(
get_bloginfo('admin_email'),
'Error sending contact form to emfluence Marketing Platform',
"Transmission error. \n\nSubmission data: \n" . wp_json_encode($data)
);
$messages[] = array('type' => 'error', 'value' => __('An error occurred submitting the form. We have been notified. Please try again later.'));
} elseif( empty($result->success) ){
foreach($result->errors as $err) {
$messages[] = array('type' => 'error', 'value' => __($err));
}
} else {
// SUCCESS!
do_action('emfl_widget_after_contact_save', $result, $data, $instance);
$this->send_notification($instance, $data);
if(!empty($instance['redirect']) && wp_http_validate_url($instance['redirect'])) {
// We're not using a header redirect for 2 reasons:
// 1. This output occurs after part of the page has already rendered, so we can't change headers. We could move it to a pre-rendering hook, but...
// 2. We might at some point decide to make the submit action AJAX-based, in which case a header-based redirect wouldn't make sense anyway.
$message = '
Redirecting...
<script> document.location.href = "' . esc_url($instance['redirect']) . '"; </script>
';
}
if(empty($message) && !empty($instance['success'])) $message = nl2br(wp_kses_post($instance['success']));
if(empty($message)) {
ob_start();
get_template_part('emfluence/success');
$message = ob_get_clean();
}
if(empty($message)) $message = file_get_contents( 'theme/success.php', TRUE);
print $this->widget_wrap_content($args, $message, $instance);
return;
} // result of attempted push to platform
} // passed initial validation
} // attempted submission
$output = '';
// Output error messages
if( !empty($messages) ){
$output .= '<ul class="messages">';
foreach($messages as $message){
if(is_string($message)) $message = array('type' => 'error', 'value' => $message);
$output .= '<li class="message ' . esc_attr($message['type']) . '">' . esc_html(__($message['value'])) . '</li>';
}
$output .= '</ul>';
}
if( !empty($instance['text']) ) {
$output .= '<div class="lead">' . wpautop(wp_kses_post($instance['text'])) . '</div>';
}
$current_page_url = remove_query_arg('sucess', $this->get_current_page_url());
$output .= '<input type="hidden" name="form-id" value="' . esc_attr($args['widget_id']) . '" />' . PHP_EOL;
$output .= '<input type="hidden" name="action" value="email_signup" />' . "\n";
$output .= '<input type="hidden" name="source" value="' . $current_page_url . '" />' . "\n";
$output .= '<input type="hidden" name="groups" value="' . implode(',', $instance['groups']) . '" />' . "\n";
// honeypot
$output .= '<div class="numerical-validation required" style="display: none"><label for="numerical-validation">Please enter 5+2: <input type="text" id="numerical-validation" name="numerical-validation" /></label></div>';
ob_start();
do_action('emfl_widget_top_of_form', $instance);
$output .= ob_get_clean();
usort($instance['fields'], function($a, $b){
if($a['order'] == $b['order']){
return 0;
}
return ($a['order'] < $b['order']) ? -1 : 1;
});
foreach( $instance['fields'] as $key => $field ){
if( !$field['display'] ) continue;
$label = __($field['label']);
$placeholder = __( str_replace(':', '', $field['label']) );
$required = $field['required']? 'required' : '';
$field['type'] = $this->restrict_to_types($field['type']);
/**
* Allows rendering of custom form parts that aren't directly attached to a contact field.
*/
$field_value = empty($values[$field['field_name']]) ? '' : $values[$field['field_name']];
$before_field_html = apply_filters('emfl_widget_before_field', $field, $key, $field_value, $instance, $this);
if(is_string($before_field_html)) $output .= $before_field_html;
switch( $field['type'] ){
case 'text':
case 'email':
case 'date':
case 'number':
$output .= '<div class="field row field-' . $key . '">' . "\n";
$output .= '<label for="emfluence_' . $key . '">' . esc_html($label) . '';
if( $field['required'] ){
$output .= '<span class="required">*</span>';
}
$output .= '</label>' . "\n";
$output .= '<input placeholder="' . esc_attr($placeholder) . '" type="' . $field['type'] . '" name="' . $field['field_name'] . '" id="emfluence_' . $key . '" value="' . esc_attr($field_value) . '" ' . $required . ' />' . "\n";
$output .= '</div>' . "\n";
break;
case 'textarea':
$output .= '<div class="field row field-' . $key . '">' . "\n";
$output .= '<label for="emfluence_' . $key . '">' . esc_html($label) . '';
if( $field['required'] ){
$output .= '<span class="required">*</span>';
}
$output .= '</label>' . "\n";
$output .= '<textarea placeholder="' . esc_attr($placeholder) . '" name="' . $field['field_name'] . '" id="emfluence_' . $key . '" ' . $required . '>' . esc_html($field_value) . '</textarea>' . "\n";
$output .= '</div>' . "\n";
break;
case 'true-false':
$has_value = isset($values[$field['field_name']]);
$yes_checked = ($has_value && $values[$field['field_name']]) ? 'checked="checked"' : '';
$no_checked = ($has_value && !$values[$field['field_name']]) ? 'checked="checked"' : '';
$required = $field['required'] ? '<span class="required">*</span>' : '';
$output .= '
<div class="field row field-' . $key . '">
<label for="emfluence_' . $key . '">' . esc_html($label) . $required . '</label>
<div class="radio"><input type="radio" name="' . $field['field_name'] . '" value="1" ' . $yes_checked . '>' . __('Yes') . '</div>
<div class="radio"><input type="radio" name="' . $field['field_name'] . '" value="0" ' . $no_checked . '>' . __('No') . '</div>
</div>' . "\n";
break;
case 'hidden':
$output .= '<input type="hidden" name="' . $field['field_name'] . '" id="emfluence_' . $key . '" value="' . esc_attr($field['hidden_value']) . '" />' . "\n";
break;
default:
$custom_field_html = apply_filters('emfl_widget_render_custom_field_type', $field, $key, $field_value);
if(is_string($custom_field_html)) $output .= $custom_field_html;
break;
}
}
ob_start();
do_action('emfl_widget_before_submit', $instance, $this);
$output .= ob_get_clean();
$output .= '<div class="row actions"><input type="submit" class="submit" value="' . esc_html($instance['submit']) . '" /></div>' . "\n";
echo $this->widget_wrap_content($args, $output, $instance);
if(apply_filters('wp-emfluence-use-default-styles', TRUE)) wp_enqueue_style(
'wp-emfluence',
plugins_url( '/css/widget-frontend.css', __FILE__ ),
array(),
filemtime(__DIR__ . '/css/widget-frontend.css')
);
return;
}
/**
* If a notification target has been provided, send the submission data to it.
* @param $instance
* @param $data
*/
protected function send_notification($instance, $data) {
if(empty($instance['notify'])) return;
$subject = empty($instance['notify-subject']) ? 'New email signup form submission for "' . $instance['title'] . '"' : $instance['notify-subject'];
$template = file_get_contents(__DIR__ . '/notification/template.html');
$fields_html = array();
$field_template = file_get_contents(__DIR__ . '/notification/template-field.html');
$alternating = 'even'; // a class that alternates on each row between "even" and "odd".
foreach($data as $field=>$val) {
$alternating = ('even' === $alternating) ? 'odd' : 'even';
if($field == 'customFields') {
foreach($val as $custom_id=>$custom_val) {
$instance_key = str_replace('custom', 'custom_', $custom_id);
$label = $instance['fields'][$instance_key]['label'];
if($instance['fields'][$instance_key]['type'] === 'true-false') {
$custom_val['value'] = $custom_val['value'] ? 'Yes' : 'No';
}
if($instance['fields'][$instance_key]['type'] === 'textarea') {
$custom_val['value'] = wpautop($custom_val['value']);
}
$fields_html[] = str_replace(array('{{label}}', '{{value}}', '{{alternating}}'), array($label, $custom_val['value'], $alternating), $field_template);
}
continue;
}
$fields_html[] = str_replace(array('{{label}}', '{{value}}', '{{alternating}}'), array($field, $this->recursively_convert_to_string($val), $alternating), $field_template);
}
$intro = (empty($instance['notify-intro']) ? '' : wpautop($instance['notify-intro']));
$message = str_replace(array('{{intro}}', '{{fields}}'), array(
$intro,
implode('', $fields_html)
), $template);
wp_mail( $instance['notify'], $subject, $message, array('Content-type: text/html;') );
}
/**
* Format an array for use in a plain text message.
* For use in the email notification.
* @param $data
* @param int $level
* @return string
*/
protected function recursively_convert_to_string($data, $level = 0) {
if(is_string($data)) return $data . "\n";
if(!is_array($data)) return '';
$out = "\n";
foreach($data as $key=>$val) {
$out .= str_repeat('-', $level) . ' ' . $key . ': ' . $this->recursively_convert_to_string($val, $level + 1);
}
return $out;
}
/**
* Convert a potential CSV of email addresses to an array of trimmed email addresses.
* @param string $address_string
* @return array
*/
protected function explode_emails($address_string) {
$emails = explode(',', $address_string);
foreach($emails as &$email) {
$email = trim($email);
}
return array_filter($emails);
}
/**
* @param $instance
* @param $groups
* @return string
*/
protected function form_template_groups($instance, $groups) {
$output = '
<h3>' . __('Groups') . '</h3>
<div class="groups">
<div class="filter">
<p>' . __('Search for any group by name. Contacts will be added to all groups that you select.') . '</p>
<p>
<input list="emfluence-emailer-groups-list"/>
<button type="button" onclick="emfluenceEmailerWidget.groups.add(this)">' . __('Add') . '</button>
</p>
</div>
<div class="selected">' . "\n";
if( !empty($instance['groups']) ) {
foreach ($instance['groups'] as $groupID) {
$group = $groups[$groupID];
$id = 'groups-' . $this->number . '-' . $groupID;
$output .= '
<div>
<label for="' . $id . '">
<input id="' . $id . '" type="checkbox" value="' . $groupID . '" name="groups[]" checked /> ' . $group->groupName . '
</label>
</div>';
}
}
$output .= '
</div>
</div>';
return $output;
}
/**
* @param array $instance
* @return string
*/
protected function form_template_text_display($instance) {
$output = '
<h3>' . __('Text Display') . '</h3>
<div class="text_display">
<p>
<label for="' . $this->get_field_id( 'title' ) . '">' . __('Title') . ':</label>
<input type="text" id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '" value="' . $instance['title'] . '" style="width:100%;" />
</p>
<p>
<label for="' . $this->get_field_id( 'text' ) . '">' . __('Text') . ':</label>
<textarea id="' . $this->get_field_id( 'text' ) . '" name="' . $this->get_field_name( 'text' ) . '" style="width:100%;" >' . $instance['text'] . '</textarea>
</p>
<p>
<label for="' . $this->get_field_id( 'submit' ) . '">' . __('Submit button') . ':</label>
<input type="text" id="' . $this->get_field_id( 'submit' ) . '" name="' . $this->get_field_name( 'submit' ) . '" value="' . $instance['submit'] . '" style="width:100%;" />
</p>
<p>
<label for="' . $this->get_field_id( 'success' ) . '">' . __('Success message') . ':</label>
<textarea id="' . $this->get_field_id( 'success' ) . '" name="' . $this->get_field_name( 'success' ) . '" style="width:100%;" >' . (empty($instance['success']) ? '' : $instance['success']) . '</textarea>
If you set the success message here, any theme template file emfluence/success.php will be ignored.
</p>
';
$is_valid = empty($instance['redirect']) || wp_http_validate_url($instance['redirect']);
$output .= '
<p>
<label for="' . $this->get_field_id( 'redirect' ) . '">' . __('Success redirect URL') . ':</label>
<input type="text" class="redirect-url" id="' . $this->get_field_id( 'redirect' ) . '" name="' . $this->get_field_name( 'redirect' ) . '" value="' . $instance['redirect'] . '" style="width:100%;" placeholder="https://..." />
If you redirect, the success message will be ignored.
' . ($is_valid ? '' : '<span class="validation-error">Invalid URL format</span>') . '
</p>
';
$output .= '
</div>' . "\n";
return $output;
}
/**
* @param $instance
* @return string
*/
protected function form_template_notification($instance) {
$validation = '';
if(!empty($instance['notify'])) {
$emails = $this->explode_emails($instance['notify']);
foreach($emails as $email) {
if(!is_email($email)) {
$validation = '<div class="validation error">The email address you entered is not valid</div>';
}
}
}
$output = '
<h3>' . __('Notification') . '</h3>
<div class="text_display">
<p>
This notification email will be sent through your Wordpress site\'s email system. Don\'t forget to ensure that your website is set up to send emails.
</p>
<p>
<label for="' . $this->get_field_id( 'notify' ) . '">' . __('Recipient Email Address') . ':</label>
<input type="text" id="' . $this->get_field_id( 'notify' ) . '" name="' . $this->get_field_name( 'notify' ) . '" value="' . $instance['notify'] . '" style="width:100%;" />
(Leave blank to disable notification)
' . $validation .'
</p>
<p>
<label for="' . $this->get_field_id( 'notify-subject' ) . '">' . __('Email Subject') . ':</label>
<input type="text" id="' . $this->get_field_id( 'notify-subject' ) . '" name="' . $this->get_field_name( 'notify-subject' ) . '" value="' . (empty($instance['notify-subject']) ? '' : $instance['notify-subject']) . '" style="width:100%;" />
(Default is \'New email signup form submission for "{{Form Title}}")
</p>
<p>
<label for="' . $this->get_field_id( 'notify-intro' ) . '">' . __('Introduction') . ':</label>
<textarea id="' . $this->get_field_id( 'notify-intro' ) . '" name="' . $this->get_field_name( 'notify-intro' ) . '" style="width:100%;" >' . (empty($instance['notify-intro']) ? '' : $instance['notify-intro']) . '</textarea>
(If this email is going to someone other than yourself, introduce or describe the purpose of the email here)
</p>
</div>' . "\n";
return $output;
}
/**
* @param array $fields
* @return string
*/
protected function form_template_basic_fields_adder($fields) {
$output = '
<div class="basic-fields-adder">
<p>' . __('Contact field') . '
<select>';
foreach($fields as $key=>$field) {
$output .= '<option value="' . $key . '" data-settings="' . esc_attr(json_encode($field)) . '">' . $field['name'] . '</option>';
}
$output .= '
</select>
<button type="button" onclick="emfluenceEmailerWidget.fields.add(this)">' . __('Add') . '</button>
</p>
</div>';
return $output;
}
/**
* @param array $defaults
* @param array $instance
* @return string
*/
protected function form_template_basic_fields($defaults, $instance) {
$output = '
<h3>' . __('General Contact Fields') . '</h3>
<div class="basic_contact_fields">
';
$output .= $this->form_template_basic_fields_adder($defaults['fields']);
foreach( $defaults['fields'] as $key => $field ) {
if(empty($instance['fields'][$key]['display'])) continue;
$output .= $this->form_template_field($defaults['fields'][$key]['name'], $key, $instance['fields'][$key]);
}
$output .= '
<div class="basic_contact_field_template" style="display: none;">
' . $this->form_template_field(
'CONTACT_FIELD_NAME',
'CONTACT_FIELD_KEY',
array(
'name' => 'CONTACT_FIELD_NAME',
'display' => 1,
'required' => 0,
'required_message' => 'CONTACT_FIELD_REQUIRED_MESSAGE',
'label' => 'CONTACT_FIELD_LABEL',
'order' => 'CONTACT_FIELD_ORDER',
'type' => 'text'
)) . '
</div>
</div>
';
return $output;
}
/**
* @return string
*/
protected function form_template_custom_variables_adder() {
$output = '
<div class="custom-variable-adder">
<p>' . __('Variable number') . '
<input type="number" min="1" max="250" step="1"/>
<button type="button" onclick="emfluenceEmailerWidget.variables.add(this)">' . __('Add') . '</button>
</p>
</div>';
return $output;
}
/**
* @param array $defaults
* @param array $instance
* @return string
*/
protected function form_template_custom_variables($defaults, $instance) {
$output = '
<h3>' . __('Custom Variables') . '</h3>
<div class="custom_variables">
' . $this->form_template_custom_variables_adder();
foreach( $instance['fields'] as $key => $field ) {
if(isset($defaults['fields'][$key])) continue; // so we're only dealing with custom fields
if(empty($instance['fields'][$key]['display'])) continue;
$variable_number = intval(str_replace('custom_', '', $key));
$name = sprintf(__('Variable %d'), $variable_number);
$output .= $this->form_template_field($name, $key, $instance['fields'][$key]);
}
$output .= '
<div class="custom_variable_template" style="display: none;">
' . $this->form_template_field(
'Variable CUSTOM_VARIABLE_NUMBER',
'custom_CUSTOM_VARIABLE_NUMBER',
array(
'name' => 'Variable CUSTOM_VARIABLE_NUMBER',
'display' => 1,
'required' => 0,
'required_message' => 'Custom CUSTOM_VARIABLE_NUMBER is required.',
'label' => 'Custom CUSTOM_VARIABLE_NUMBER:',
'order' => 6,
)) . '
</div>
</div>
';
return $output;
}
/**
* @param string $name Like 'First Name'
* @param string $key Like 'first_name'
* @param array $field Field definition. Like
* array(
* 'name' => __('First Name'),
* 'display' => 1,
* 'required' => 1,
* 'required_message' => __('First name is required.'),
* 'label' => __('First Name:'),
* 'order' => 1,
* )
* @return string
*/
protected function form_template_field($name, $key, $field) {
$available_types = $this->form_get_allowed_types();
$display_input = array(
'id' => $this->get_field_id( $key . '_display' ),
'name' => $this->get_field_name( $key . '_display' ),
'checked' => $field['display'] == 1? 'checked="checked"' : '',
'disabled' => '',
);
$required_input = array(
'id' => $this->get_field_id( $key . '_required' ),
'name' => $this->get_field_name( $key . '_required' ),
'checked' => $field['required'] == 1? 'checked="checked"' : '',
'disabled' => '',
);
$required_message_input = array(
'id' => $this->get_field_id( $key . '_required_message' ),
'name' => $this->get_field_name( $key . '_required_message' ),
'value' => $field['required_message'],
);
$label_input = array(
'id' => $this->get_field_id( $key . '_label' ),
'name' => $this->get_field_name( $key . '_label' ),
'value' => $field['label'],
);
$order_input = array(
'id' => $this->get_field_id( $key . '_order' ),
'name' => $this->get_field_name( $key . '_order' ),
'value' => $field['order'],
);
$type_input = array(
'id' => $this->get_field_id( $key . '_type' ),
'name' => $this->get_field_name( $key . '_type' ),
'value' => empty($field['type']) ? 'text' : $this->restrict_to_types($field['type']),
'options' => array(),
'disabled' => '',
);
$hidden_value_input = array(
'id' => $this->get_field_id( $key . '_hidden_value' ),
'name' => $this->get_field_name( $key . '_hidden_value' ),
'value' => empty($field['hidden_value']) ? '' : $field['hidden_value'],
);
if($key == 'email') {
$display_input['disabled'] = 'disabled="disabled"';
$required_input['disabled'] = 'disabled="disabled"';
$type_input['disabled'] = 'disabled="disabled"';
$type_input['value'] = 'email';
}
foreach($available_types as $type) {
$selected = ($type == $type_input['value'] ? 'selected="selected"' : '');
$type_input['options'][] = '<option ' . $selected . '>' . $type . '</option>';
}
$output = '
<div class="contact-field" data-variable-key="' . $key . '">
<p class="heading">
<label class="heading" for="' . $display_input['id'] . '">
<input type="checkbox" id="' . $display_input['id'] . '" name="' . $display_input['name'] . '" value="1" ' . $display_input['checked'] . ' ' . $display_input['disabled'] . ' />
' . __($name) . '
</label>
<label for="' . $required_input['id'] . '">
<input type="checkbox" id="' . $required_input['id'] . '" name="' . $required_input['name'] . '" value="1" ' . $required_input['checked'] . ' ' . $display_input['disabled'] . ' />
' . __('Required') . '
</label>
<label for="' . $order_input['id'] . '">
<input type="text" id="' . $order_input['id'] . '" name="' . $order_input['name'] . '" value="' . $order_input['value'] . '" class="order" size="2" />
' . __('Order') . '
</label>
</p>
<p>
<label for="' . $required_message_input['id'] . '">' . __('Required Message') . '</label>
<input type="text" id="' . $required_message_input['id'] . '" name="' . $required_message_input['name'] . '" value="' . $required_message_input['value'] . '" style="width:100%;" />
</p>
<p>
<label for="' . $label_input['id'] . '">' . __('Label') . '</label>
<input type="text" id="' . $label_input['id'] . '" name="' . $label_input['name'] . '" value="' . $label_input['value'] . '" style="width:100%;" />
</p>
<p class="type-section">
<label for="' . $type_input['id'] . '">' . __('Type') . '</label>
<select class="type-selector" id="' . $type_input['id'] . '" name="' . $type_input['name'] . '" ' . $type_input['disabled'] . '>
' . implode('', $type_input['options']) .'
</select>
<input class="hidden-value" type="text" id="' . $hidden_value_input['id'] . '" name="' . $hidden_value_input['name'] . '" value="' . $hidden_value_input['value'] . '" placeholder="Hidden value" />
</p>
</div>
';
return $output;
}
/**
* Get a list of allowed field types
* @return string[]
*/
protected function form_get_allowed_types() {
$field_types = array(
'text', 'textarea', 'email', 'date', 'number', 'true-false', 'hidden'
);
return apply_filters('emfl_widget_custom_field_types', $field_types);
}
/**
* Restrict a field type to the allowed types
* @param string $type
* @return string
*/
protected function restrict_to_types($type) {
$allowed = $this->form_get_allowed_types();
return (array_search($type, $allowed) === FALSE ? 'text' : $type);
}
/**
* @return array
*/
protected function form_get_defaults() {
$defaults = array(
'title' => __('Email Signup'),
'text' => '',
'redirect' => '',
'groups' => array(),
'custom_fields' => array(),
'submit' => __('Signup'),
'fields' => array(),
'notify' => ''
);
$contact_field_defaults = array(
'first_name' => array(
'name' => __('First Name'),
'required_message' => __('First name is required.'),
'label' => __('First Name:'),
'type' => 'text',
'platform' => 'firstName'
),
'last_name' => array(
'name' => __('Last Name'),
'required_message' => __('Last name is required.'),
'label' => __('Last Name:'),
'type' => 'text',
'platform' => 'lastName'
),
'title' => array(
'name' => __('Title'),
'required_message' => __('Title is required.'),
'label' => __('Title:'),
'type' => 'text',
'platform' => 'title'
),
'company' => array(
'name' => __('Company'),
'required_message' => __('Company is required.'),
'label' => __('Company:'),
'type' => 'text',
'platform' => 'company'
),
'email' => array(
'name' => 'Email',
'display' => 1,
'required' => 1,
'required_message' => 'Email address is required.',
'label' => 'Email:',
'type' => 'email',
'platform' => 'email'
),
'address1' => array(
'name' => 'Address 1',
'required_message' => 'Address 1 is required.',
'label' => 'Address 1:',
'type' => 'text',
'platform' => 'address1'
),
'address2' => array(
'name' => 'Address 2',
'required_message' => 'Address 2 is required.',
'label' => 'Address 2:',
'type' => 'text',
'platform' => 'address2'
),
'city' => array(
'name' => 'City',
'required_message' => 'City is required.',
'label' => 'City:',
'type' => 'text',
'platform' => 'city'
),
'state' => array(
'name' => 'State',
'required_message' => 'State is required.',
'label' => 'State:',
'type' => 'text'
),
'zipCode' => array(
'name' => 'Zip Code',
'required_message' => 'Zip Code is required.',
'label' => 'Zip Code:',
'type' => 'text',
'platform' => 'zipCode'
),
'country' => array(
'name' => 'Country',
'required_message' => 'Country is required.',
'label' => 'Country:',
'type' => 'text',
'platform' => 'country'
),
'phone' => array(
'name' => 'Phone',
'required_message' => 'Phone is required.',
'label' => 'Phone:',
'type' => 'text',
'platform' => 'phone'
),
'fax' => array(
'name' => 'Fax',
'required_message' => 'Fax is required.',
'label' => 'Fax:',
'type' => 'text',
'platform' => 'fax'
),
'dateofbirth' => array(
'name' => 'Date of birth',
'required_message' => 'Date of birth is required.',
'label' => 'Date of birth:',
'type' => 'date',
'platform' => 'dateofbirth'
),
'notes' => array(
'name' => 'Notes',
'required_message' => 'Notes is required.',
'label' => 'Notes:',
'type' => 'text',
'platform' => 'notes'
),
'memo' => array(
'name' => 'Memo',
'required_message' => 'Memo is required.',
'label' => 'Memo:',
'type' => 'textarea',
'platform' => 'memo'
)
);
foreach($contact_field_defaults as $name=>$field) {
if(empty($field['display'])) $field['display'] = 0;
if(empty($field['required'])) $field['required'] = 0;
if(empty($field['order'])) $field['order'] = 1;
$defaults['fields'][$name] = $field;
}
return $defaults;
}
public function form( $instance ) {
$options = get_option('emfluence_global');
if(empty($options['api_key'])) {
print '<div class="wp-emfluence">Please visit the emfluence plugin settings page and add an API token.</div>';
return;
}
static $ping = NULL;
try {
$api = emfluence_get_api($options['api_key']);
if (NULL === $ping) $ping = $api->ping();
} catch(Exception $e) {}
if( (NULL === $ping) || !$ping || !$ping->success ){