contact-form-properties.php
5.91 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
<?php
add_filter(
'wpcf7_pre_construct_contact_form_properties',
'wpcf7_constant_contact_register_property',
10, 2
);
/**
* Registers the constant_contact contact form property.
*/
function wpcf7_constant_contact_register_property( $properties, $contact_form ) {
$service = WPCF7_ConstantContact::get_instance();
if ( $service->is_active() ) {
$properties += array(
'constant_contact' => array(),
);
}
return $properties;
}
add_filter(
'wpcf7_contact_form_property_constant_contact',
'wpcf7_constant_contact_setup_property',
10, 2
);
/**
* Sets up the constant_contact property value. For back-compat, this attempts
* to take over the value from old settings if the property is empty.
*/
function wpcf7_constant_contact_setup_property( $property, $contact_form ) {
if ( ! empty( $property ) ) {
$property = wp_parse_args(
$property,
array(
'enable_contact_list' => false,
'contact_lists' => array(),
)
);
return $property;
}
$property = array(
'enable_contact_list' => true,
'contact_lists' => array(),
);
if ( $contact_form->initial() ) {
return $property;
}
$service_option = (array) WPCF7::get_option( 'constant_contact' );
$property['enable_contact_list'] = ! $contact_form->is_false(
'constant_contact'
);
if ( isset( $service_option['contact_lists'] ) ) {
$contact_lists = (array) $service_option['contact_lists'];
$contact_lists_selected = array();
foreach ( $contact_lists as $list ) {
if ( empty( $list['selected'] ) ) {
continue;
}
foreach ( (array) $list['selected'] as $key => $val ) {
if ( ! isset( $contact_lists_selected[$key] ) ) {
$contact_lists_selected[$key] = array();
}
$contact_lists_selected[$key][] = $list['list_id'];
}
}
$related_keys = array(
sprintf( 'wpcf7_contact_form:%d', $contact_form->id() ),
'default',
);
foreach ( $related_keys as $key ) {
if ( ! empty( $contact_lists_selected[$key] ) ) {
$property['contact_lists'] = $contact_lists_selected[$key];
break;
}
}
}
return $property;
}
add_action(
'wpcf7_save_contact_form',
'wpcf7_constant_contact_save_contact_form',
10, 1
);
/**
* Saves the constant_contact property value.
*/
function wpcf7_constant_contact_save_contact_form( $contact_form ) {
$service = WPCF7_ConstantContact::get_instance();
if ( ! $service->is_active() ) {
return;
}
$prop = isset( $_POST['wpcf7-ctct'] )
? (array) $_POST['wpcf7-ctct']
: array();
$prop = wp_parse_args(
$prop,
array(
'enable_contact_list' => false,
'contact_lists' => array(),
)
);
$contact_form->set_properties( array(
'constant_contact' => $prop,
) );
}
add_filter(
'wpcf7_editor_panels',
'wpcf7_constant_contact_editor_panels',
10, 1
);
/**
* Builds the editor panel for the constant_contact property.
*/
function wpcf7_constant_contact_editor_panels( $panels ) {
$service = WPCF7_ConstantContact::get_instance();
if ( ! $service->is_active() ) {
return $panels;
}
$contact_form = WPCF7_ContactForm::get_current();
$prop = wp_parse_args(
$contact_form->prop( 'constant_contact' ),
array(
'enable_contact_list' => false,
'contact_lists' => array(),
)
);
$editor_panel = function () use ( $prop, $service ) {
$description = sprintf(
esc_html(
__( "You can set up the Constant Contact integration here. For details, see %s.", 'contact-form-7' )
),
wpcf7_link(
__( 'https://contactform7.com/constant-contact-integration/', 'contact-form-7' ),
__( 'Constant Contact integration', 'contact-form-7' )
)
);
$lists = $service->get_contact_lists();
?>
<h2><?php echo esc_html( __( 'Constant Contact', 'contact-form-7' ) ); ?></h2>
<fieldset>
<legend><?php echo $description; ?></legend>
<table class="form-table" role="presentation">
<tbody>
<tr class="<?php echo $prop['enable_contact_list'] ? '' : 'inactive'; ?>">
<th scope="row">
<?php
echo esc_html( __( 'Contact lists', 'contact-form-7' ) );
?>
</th>
<td>
<fieldset>
<legend class="screen-reader-text">
<?php
echo esc_html( __( 'Contact lists', 'contact-form-7' ) );
?>
</legend>
<label for="wpcf7-ctct-enable-contact-list">
<input type="checkbox" name="wpcf7-ctct[enable_contact_list]" id="wpcf7-ctct-enable-contact-list" value="1" <?php checked( $prop['enable_contact_list'] ); ?> />
<?php
echo esc_html(
__( "Add form submitters to your contact lists", 'contact-form-7' )
);
?>
</label>
</fieldset>
</td>
</tr>
<tr>
<th scope="row"></th>
<td>
<fieldset>
<?php
if ( $lists ) {
echo sprintf(
'<legend>%1$s</legend>',
esc_html( __( 'Select lists to which contacts are added:', 'contact-form-7' ) )
);
echo '<ul>';
foreach ( $lists as $list ) {
echo sprintf(
'<li><label><input %1$s /> %2$s</label></li>',
wpcf7_format_atts( array(
'type' => 'checkbox',
'name' => 'wpcf7-ctct[contact_lists][]',
'value' => $list['list_id'],
'checked' => in_array( $list['list_id'], $prop['contact_lists'] ),
) ),
esc_html( $list['name'] )
);
}
echo '</ul>';
} else {
echo sprintf(
'<legend>%1$s</legend>',
esc_html( __( 'You have no contact list yet.', 'contact-form-7' ) )
);
}
?>
</fieldset>
<?php
echo sprintf(
'<p><a %1$s>%2$s <span class="screen-reader-text">%3$s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a></p>',
wpcf7_format_atts( array(
'href' => 'https://app.constantcontact.com/pages/contacts/ui#lists',
'target' => '_blank',
'rel' => 'external noreferrer noopener',
) ),
esc_html( __( 'Manage your contact lists', 'contact-form-7' ) ),
esc_html( __( '(opens in a new tab)', 'contact-form-7' ) )
);
?>
</td>
</tr>
</tbody>
</table>
</fieldset>
<?php
};
$panels += array(
'ctct-panel' => array(
'title' => __( 'Constant Contact', 'contact-form-7' ),
'callback' => $editor_panel,
),
);
return $panels;
}