class-gravity-forms.php
5.88 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
<?php
defined('ABSPATH') or exit;
/**
* Class MC4WP_Ninja_Forms_Integration
*
* @ignore
*/
class MC4WP_Gravity_Forms_Integration extends MC4WP_Integration
{
/**
* @var string
*/
public $name = 'Gravity Forms';
/**
* @var string
*/
public $description = 'Subscribe visitors from your Gravity Forms forms.';
/**
* Add hooks
*/
public function add_hooks()
{
add_action('gform_field_standard_settings', array( $this, 'settings_fields' ), 10, 2);
add_action('gform_editor_js', array( $this, 'editor_js' ));
add_action('gform_after_submission', array( $this, 'after_submission' ), 10, 2);
}
public function after_submission($submission, $form)
{
$subscribe = false;
$email_address = '';
$mailchimp_list_id = '';
$double_optin = $this->options['double_optin'];
// find email field & checkbox value
foreach ($form['fields'] as $field) {
if ($field->type === 'email' && empty($email_address) && ! empty($submission[ $field->id ])) {
$email_address = $submission[ $field->id ];
}
if ($field->type === 'mailchimp' && ! empty($submission[ $field->id ])) {
$subscribe = true;
$mailchimp_list_id = $field->mailchimp_list;
if (isset($field->mailchimp_double_optin)) {
$double_optin = $field->mailchimp_double_optin;
}
}
}
if (! $subscribe || empty($email_address)) {
return;
}
// override integration settings with field options
$orig_options = $this->options;
$this->options['lists'] = array( $mailchimp_list_id );
$this->options['double_optin'] = $double_optin;
// perform the sign-up
$this->subscribe(array( 'EMAIL' => $email_address ), $submission['form_id']);
// revert back to original options in case request lives on
$this->options = $orig_options;
}
public function editor_js()
{
?>
<script type="text/javascript">
jQuery(document).on('gform_load_field_settings', function(evt, field) {
jQuery('#field_mailchimp_list').val(field.mailchimp_list || '');
jQuery('#field_mailchimp_double_optin').val(field.mailchimp_double_optin || "1");
jQuery('#field_mailchimp_precheck').val(field.mailchimp_precheck || "0");
});
</script>
<?php
}
public function settings_fields($pos, $form_id)
{
if ($pos !== 0) {
return;
}
$mailchimp = new MC4WP_MailChimp();
$lists = $mailchimp->get_lists();
?>
<li class="mailchimp_list_setting field_setting">
<label for="field_mailchimp_list" class="section_label">
<?php esc_html_e('Mailchimp list', 'mailchimp-for-wp'); ?>
</label>
<select id="field_mailchimp_list" onchange="SetFieldProperty('mailchimp_list', this.value)">
<option value="" disabled><?php _e('Select a Mailchimp list', 'mailchimp-for-wp'); ?></option>
<?php
foreach ($lists as $list) {
echo sprintf('<option value="%s">%s</option>', $list->id, $list->name);
}
?>
</select>
<p class="help">
<?php echo __('Select the list(s) to which people who check the checkbox should be subscribed.', 'mailchimp-for-wp'); ?>
</p>
</li>
<li class="mailchimp_double_optin field_setting">
<label for="field_mailchimp_double_optin" class="section_label">
<?php esc_html_e('Double opt-in?', 'mailchimp-for-wp'); ?>
</label>
<select id="field_mailchimp_double_optin" onchange="SetFieldProperty('mailchimp_double_optin', this.value)">
<option value="1"><?php echo __('Yes', 'mailchimp-for-wp'); ?></option>
<option value="0"><?php echo __('No', 'mailchimp-for-wp'); ?></option>
</select>
<p class="help">
<?php _e('Select "yes" if you want people to confirm their email address before being subscribed (recommended)', 'mailchimp-for-wp'); ?>
</p>
</li>
<li class="mailchimp_precheck field_setting">
<label for="field_mailchimp_precheck" class="section_label">
<?php esc_html_e('Pre-check the checkbox?', 'mailchimp-for-wp'); ?>
</label>
<select id="field_mailchimp_precheck" onchange="SetFieldProperty('mailchimp_precheck', this.value)">
<option value="1"><?php echo __('Yes', 'mailchimp-for-wp'); ?></option>
<option value="0"><?php echo __('No', 'mailchimp-for-wp'); ?></option>
</select>
<p class="help">
<?php
_e('Select "yes" if the checkbox should be pre-checked.', 'mailchimp-for-wp');
echo '<br />';
printf(__('<strong>Warning: </strong> enabling this may affect your <a href="%s">GDPR compliance</a>.', 'mailchimp-for-wp'), 'https://www.mc4wp.com/kb/gdpr-compliance/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=integrations-page');
?>
</p>
</li>
<?php
}
/**
* @return bool
*/
public function is_installed()
{
return class_exists('GF_Field') && class_exists('GF_Fields');
}
/**
* @since 3.0
* @return array
*/
public function get_ui_elements()
{
return array();
}
/**
* @param int $form_id
* @return string
*/
public function get_object_link($form_id)
{
return '<a href="' . admin_url(sprintf('admin.php?page=gf_edit_forms&id=%d', $form_id)) . '">Gravity Forms</a>';
}
}