Conditional Fields for Contact Form 7 - Version 1.2

Version Description

  • Made compatible with Contact Form 7 Multi-Step Forms
  • Small bug fix by Manual from advantia.net: now only considering fields which are strictly inside hidden group tags with form submit. Important in some edge cases where form elements get hidden by other mechanisms, i.e. tabbed forms.
  • Started work on CF7CF Pro, made some structural code modifications so the free plugin can function as the base for both plugins.
  • Removed some debug code
  • Updated readme file
Download this release

Release Info

Developer Jules Colle
Plugin Icon 128x128 Conditional Fields for Contact Form 7
Version 1.2
Comparing to
See all releases

Code changes from version 1.1 to 1.2

Files changed (6) hide show
  1. admin.php +9 -107
  2. cf7cf.php +338 -0
  3. contact-form-7-conditional-fields.php +13 -309
  4. js/scripts.js +77 -31
  5. js/scripts_admin.js +111 -0
  6. readme.txt +49 -6
admin.php CHANGED
@@ -46,12 +46,15 @@ function all_group_options($post, $selected = '-1') {
46
  }
47
  }
48
 
49
- function all_operator_options($selected = 'equals') {
50
- $all_options = array('equals', 'not equals');
51
- foreach($all_options as $option) {
52
- ?>
53
- <option value="<?php echo $option ?>" <?php echo $selected == $option?'selected':'' ?>><?php echo $option ?></option>
54
- <?php
 
 
 
55
  }
56
  }
57
 
@@ -113,107 +116,6 @@ function wpcf7cf_editor_panel_conditional($form) {
113
 
114
  </div>
115
  </div>
116
-
117
-
118
- <script>
119
- (function($) {
120
-
121
- var index = $('#wpcf7cf-entries .entry').length;
122
-
123
- $('.delete-button').click(function(){
124
-
125
- //if (confirm('You sure?')===false) return false;
126
- $(this).parent().remove();
127
- return false;
128
-
129
- });
130
-
131
- $('#wpcf7cf-add-button').click(function(){
132
-
133
- var id = add_condition_fields();
134
-
135
- return false;
136
-
137
- });
138
-
139
- function clear_all_condition_fields() {
140
- $('.entry').remove();
141
- }
142
-
143
- function add_condition_fields() {
144
- var $delete_button = $('#wpcf7cf-delete-button').clone().removeAttr('id');
145
- $('<div class="entry" id="entry-'+index+'">'+($('#wpcf7cf-new-entry').html().replace(/{id}/g, index))+'</div>').prependTo('#wpcf7cf-entries').append($delete_button);
146
- $delete_button.click(function(){
147
- $(this).parent().remove();
148
- return false;
149
- });
150
- index++;
151
-
152
- return (index-1);
153
- }
154
-
155
- function import_condition_fields() {
156
- var lines = $('#wpcf7cf-settings-text').val().split(/\r?\n/);
157
- console.log(lines);
158
- for (var i = lines.length+1; i>-1; i--) {
159
-
160
- var str = lines[i];
161
-
162
- var match = regex.exec(str);
163
-
164
- if (match == null) continue;
165
-
166
- console.log(match[1]+' '+match[2]+' '+match[3]+' '+match[4]);
167
-
168
- var id = add_condition_fields();
169
-
170
- $('#entry-'+id+' .if-field-select').val(match[1]);
171
- $('#entry-'+id+' .operator').val(match[2]);
172
- $('#entry-'+id+' .if-value').val(match[3]);
173
- $('#entry-'+id+' .then-field-select').val(match[4]);
174
-
175
- regex.lastIndex = 0;
176
- }
177
- }
178
-
179
- // export/import settings
180
-
181
- $('#wpcf7cf-settings-text-wrap').hide();
182
-
183
- $('#wpcf7cf-settings-to-text').click(function() {
184
- $('#wpcf7cf-settings-text-wrap').show();
185
-
186
- $('#wpcf7cf-settings-text').val('');
187
- $('#wpcf7cf-entries .entry').each(function() {
188
- var $entry = $(this);
189
- var line = 'if [' + $entry.find('.if-field-select').val() + ']'
190
- + ' ' + $entry.find('.operator').val()
191
- + ' "' + $entry.find('.if-value').val() + '" then show'
192
- + ' [' + $entry.find('.then-field-select').val() + ']';
193
- $('#wpcf7cf-settings-text').val($('#wpcf7cf-settings-text').val() + line + "\n" ).select();
194
- });
195
- return false;
196
- });
197
-
198
- var regex = /if \[(.*)] (.*equals) "(.*)" then show \[(.*)]/g;
199
-
200
- $('#add-fields').click(function() {
201
- import_condition_fields();
202
- });
203
-
204
- $('#overwrite-fields').click(function() {
205
- clear_all_condition_fields();
206
- import_condition_fields();
207
- });
208
-
209
- $('#wpcf7cf-settings-text-clear').click(function() {
210
- $('#wpcf7cf-settings-text-wrap').hide();
211
- $('#wpcf7cf-settings-text').val('');
212
- return false;
213
- });
214
-
215
- })( jQuery );
216
- </script>
217
  <?php
218
  }
219
 
46
  }
47
  }
48
 
49
+ if (!function_exists('all_operator_options')) {
50
+ function all_operator_options($selected = 'equals') {
51
+ $all_options = array('equals', 'not equals');
52
+ $all_options = apply_filters('wpcf7cf_get_operators', $all_options);
53
+ foreach($all_options as $option) {
54
+ ?>
55
+ <option value="<?php echo htmlentities($option) ?>" <?php echo $selected == $option?'selected':'' ?>><?php echo htmlentities($option) ?></option>
56
+ <?php
57
+ }
58
  }
59
  }
60
 
116
 
117
  </div>
118
  </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  <?php
120
  }
121
 
cf7cf.php ADDED
@@ -0,0 +1,338 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class ContactForm7ConditionalFields {
3
+ private $hidden_fields = array();
4
+ private $visible_groups = array();
5
+ private $hidden_groups = array();
6
+
7
+ function __construct() {
8
+
9
+ add_action('wpcf7_enqueue_scripts', array(__CLASS__, 'enqueue_js'));
10
+ add_action('wpcf7_enqueue_styles', array(__CLASS__, 'enqueue_css'));
11
+
12
+ // Register shortcodes
13
+ add_action('wpcf7_init', array(__CLASS__, 'add_shortcodes'));
14
+
15
+ // Tag generator
16
+ add_action('load-contact_page_wpcf7-new', array(__CLASS__, 'tag_generator'));
17
+ add_action('load-toplevel_page_wpcf7', array(__CLASS__, 'tag_generator'));
18
+
19
+ // compatibility with CF7 multi-step forms
20
+ if (!function_exists('cf7msm_get')) add_filter( 'wpcf7_posted_data', array($this,'cf7msm_merge_post_with_cookie'), 8, 1 );
21
+
22
+ add_filter( 'wpcf7_posted_data', array($this, 'remove_hidden_post_data') );
23
+ add_filter( 'wpcf7_mail_components', array($this, 'hide_hidden_mail_fields') );
24
+ add_filter('wpcf7_additional_mail', array($this, 'hide_hidden_mail_fields_additional_mail'), 10, 2);
25
+
26
+ //apply_filters( 'wpcf7_additional_mail',$additional_mail, $contact_form )
27
+
28
+ add_filter( 'wpcf7_validate', array($this, 'skip_validation_for_hidden_fields'), 2, 2 );
29
+
30
+ register_activation_hook(__FILE__, array($this, 'activate'));
31
+
32
+ if (is_admin()) {
33
+ require_once dirname(__FILE__) . '/admin.php';
34
+ }
35
+ }
36
+
37
+ function activate() {
38
+ //add options with add_option and stuff
39
+ }
40
+
41
+ public static function enqueue_js() {
42
+ // nothing here. We will only load the CF7 script if there is a CF7 form on the page.
43
+ }
44
+
45
+ public static function enqueue_css() {
46
+ wp_enqueue_style('cf7cf-style', plugins_url('style.css', __FILE__), array(), WPCF7CF_VERSION);
47
+ }
48
+
49
+ public static function add_shortcodes() {
50
+ //wpcf7_add_shortcode('group', array(__CLASS__, 'shortcode_handler'), true);
51
+ //add_shortcode( 'group', array(__CLASS__, 'group_shortcode_handler') );
52
+ if (function_exists('wpcf7_add_form_tag'))
53
+ wpcf7_add_form_tag('group', array(__CLASS__, 'shortcode_handler'), true);
54
+ else if (function_exists('wpcf7_add_shortcode')) {
55
+ wpcf7_add_shortcode('group', array(__CLASS__, 'shortcode_handler'), true);
56
+ } else {
57
+ throw new Exception('functions wpcf7_add_form_tag and wpcf7_add_shortcode not found.');
58
+ }
59
+ }
60
+
61
+ function group_shortcode_handler( $atts, $content = "" ) {
62
+ return $content;
63
+ }
64
+
65
+ function shortcode_handler($tag) {
66
+ //$tag = new WPCF7_Shortcode($tag);
67
+ $tag = new WPCF7_FormTag($tag);
68
+ //ob_start();
69
+ //print_r($tag);
70
+ //return print_r($tag, true);
71
+ return $tag->content;
72
+ }
73
+
74
+
75
+ public static function tag_generator() {
76
+ if (! function_exists( 'wpcf7_add_tag_generator'))
77
+ return;
78
+
79
+ wpcf7_add_tag_generator('group',
80
+ __('Conditional fields Group', 'wpcf7cf'),
81
+ 'wpcf7-tg-pane-group',
82
+ array(__CLASS__, 'tg_pane')
83
+ );
84
+ }
85
+
86
+ static function tg_pane( $contact_form, $args = '' ) {
87
+ $args = wp_parse_args( $args, array() );
88
+ $type = 'group';
89
+
90
+ $description = __( "Generate a group tag to group form elements that can be shown conditionally.", 'cf7cf' );
91
+
92
+ ?>
93
+ <div class="control-box">
94
+ <fieldset>
95
+ <legend><?php echo sprintf( esc_html( $description ) ); ?></legend>
96
+
97
+ <table class="form-table">
98
+ <tbody>
99
+
100
+ <tr>
101
+ <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-name' ); ?>"><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?></label></th>
102
+ <td><input type="text" name="name" class="tg-name oneline" id="<?php echo esc_attr( $args['content'] . '-name' ); ?>" /></td>
103
+ </tr>
104
+
105
+ </tbody>
106
+ </table>
107
+ </fieldset>
108
+ </div>
109
+
110
+ <div class="insert-box">
111
+ <input type="text" name="<?php echo $type; ?>" class="tag code" readonly="readonly" onfocus="this.select()" />
112
+
113
+ <div class="submitbox">
114
+ <input type="button" class="button button-primary insert-tag" value="<?php echo esc_attr( __( 'Insert Tag', 'contact-form-7' ) ); ?>" />
115
+ </div>
116
+
117
+ <br class="clear" />
118
+ </div>
119
+ <?php
120
+ }
121
+
122
+ /**
123
+ * Remove validation requirements for fields that are hidden at the time of form submission.
124
+ * Called using add_filter( 'wpcf7_validate_[tag_type]', array($this, 'skip_validation_for_hidden_fields'), 2, 2 );
125
+ * where the priority of 2 causes this to kill any validations with a priority higher than 2
126
+ *
127
+ * @param $result
128
+ * @param $tag
129
+ *
130
+ * @return mixed
131
+ */
132
+ function skip_validation_for_hidden_fields($result, $tags) {
133
+
134
+ if (count($this->hidden_fields) == 0) return $result;
135
+
136
+ $return_result = new WPCF7_Validation();
137
+
138
+ $invalid_fields = $result->get_invalid_fields();
139
+
140
+ if (!is_array($invalid_fields) || count($invalid_fields) == 0) return $result;
141
+
142
+ foreach ($invalid_fields as $invalid_field_key => $invalid_field_data) {
143
+ if (!in_array($invalid_field_key, $this->hidden_fields)) {
144
+ // the invalid field is not a hidden field, so we'll add it to the final validation result
145
+ $return_result->invalidate($invalid_field_key, $invalid_field_data['reason']);
146
+ }
147
+ }
148
+
149
+ return $return_result;
150
+ }
151
+
152
+
153
+ /**
154
+ * When a CF7 form is posted, check the form for hidden fields, then remove those fields from the post data
155
+ *
156
+ * @param $posted_data
157
+ *
158
+ * @return mixed
159
+ */
160
+ function remove_hidden_post_data($posted_data) {
161
+ $this->set_hidden_fields_arrays($posted_data);
162
+
163
+ foreach( $this->hidden_fields as $name => $value ) {
164
+ unset( $posted_data[$name] );
165
+ }
166
+
167
+ return $posted_data;
168
+ }
169
+
170
+ function cf7msm_merge_post_with_cookie($posted_data) {
171
+
172
+ if (!$posted_data) {
173
+ $posted_data = WPCF7_Submission::get_instance()->get_posted_data();
174
+ }
175
+
176
+ // this will temporarily set the hidden fields data to the posted_data.
177
+ // later this function will be called again with the updated posted_data
178
+ $this->set_hidden_fields_arrays($posted_data);
179
+
180
+ // get cookie data
181
+ $cookie_data = cf7msm_get('cf7msm_posted_data');
182
+ $cookie_data_hidden_group_fields = json_decode(stripslashes($cookie_data['_wpcf7cf_hidden_group_fields']));
183
+ $cookie_data_hidden_groups = json_decode(stripslashes($cookie_data['_wpcf7cf_hidden_groups']));
184
+ $cookie_data_visible_groups = json_decode(stripslashes($cookie_data['_wpcf7cf_visible_groups']));
185
+
186
+ // remove all the currently posted data from the cookie data (we don't wanna add it twice)
187
+ $cookie_data_hidden_group_fields = array_diff($cookie_data_hidden_group_fields, array_keys($posted_data));
188
+ $cookie_data_hidden_groups = array_diff($cookie_data_hidden_groups, $this->hidden_groups, $this->visible_groups);
189
+ $cookie_data_visible_groups = array_diff($cookie_data_visible_groups, $this->hidden_groups, $this->visible_groups);
190
+
191
+ // update current post data with cookie data
192
+ $posted_data['_wpcf7cf_hidden_group_fields'] = addslashes(json_encode(array_merge((array) $cookie_data_hidden_group_fields, $this->hidden_fields)));
193
+ $posted_data['_wpcf7cf_hidden_groups'] = addslashes(json_encode(array_merge((array) $cookie_data_hidden_groups, $this->hidden_groups)));
194
+ $posted_data['_wpcf7cf_visible_groups'] = addslashes(json_encode(array_merge((array) $cookie_data_visible_groups, $this->visible_groups)));
195
+
196
+ return $posted_data;
197
+ }
198
+
199
+
200
+ /**
201
+ * Finds the currently submitted form and set the hidden_fields variables accoringly
202
+ *
203
+ * @param bool|array $posted_data
204
+ */
205
+ function set_hidden_fields_arrays($posted_data = false) {
206
+
207
+ if (!$posted_data) {
208
+ $posted_data = WPCF7_Submission::get_instance()->get_posted_data();
209
+ }
210
+
211
+ $hidden_fields = json_decode(stripslashes($posted_data['_wpcf7cf_hidden_group_fields']));
212
+ if (is_array($hidden_fields) && count($hidden_fields) > 0) {
213
+ foreach ($hidden_fields as $field) {
214
+ $this->hidden_fields[] = $field;
215
+ if (wpcf7cf_endswith($field, '[]')) {
216
+ $this->hidden_fields[] = substr($field,0,strlen($field)-2);
217
+ }
218
+ }
219
+ }
220
+ $this->hidden_groups = json_decode(stripslashes($posted_data['_wpcf7cf_hidden_groups']));
221
+ $this->visible_groups = json_decode(stripslashes($posted_data['_wpcf7cf_visible_groups']));
222
+ }
223
+
224
+ function hide_hidden_mail_fields( $components ) {
225
+ $regex = '@\[[\t ]*([a-zA-Z_][0-9a-zA-Z:._-]*)[\t ]*\](.*?)\[[\t ]*/[\t ]*\1[\t ]*\]@s';
226
+ // [1] = name [2] = contents
227
+
228
+ $components['body'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $components['body'] );
229
+ $components['subject'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $components['subject'] );
230
+ $components['sender'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $components['sender'] );
231
+ $components['recipient'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $components['recipient'] );
232
+ $components['additional_headers'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $components['additional_headers'] );
233
+
234
+ return $components;
235
+ }
236
+
237
+ function hide_hidden_mail_fields_additional_mail($additional_mail, $contact_form) {
238
+
239
+ if (!is_array($additional_mail) || !array_key_exists('mail_2', $additional_mail)) return $additional_mail;
240
+
241
+ $regex = '@\[[\t ]*([a-zA-Z_][0-9a-zA-Z:._-]*)[\t ]*\](.*?)\[[\t ]*/[\t ]*\1[\t ]*\]@s';
242
+
243
+ $additional_mail['mail_2']['body'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $additional_mail['mail_2']['body'] );
244
+ $additional_mail['mail_2']['subject'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $additional_mail['mail_2']['subject'] );
245
+ $additional_mail['mail_2']['sender'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $additional_mail['mail_2']['sender'] );
246
+ $additional_mail['mail_2']['recipient'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $additional_mail['mail_2']['recipient'] );
247
+ $additional_mail['mail_2']['additional_headers'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $additional_mail['mail_2']['additional_headers'] );
248
+
249
+ return $additional_mail;
250
+ }
251
+
252
+ function hide_hidden_mail_fields_regex_callback ( $matches ) {
253
+ $name = $matches[1];
254
+ $content = $matches[2];
255
+ if ( in_array( $name, $this->hidden_groups ) ) {
256
+ // The tag name represents a hidden group, so replace everything from [tagname] to [/tagname] with nothing
257
+ return '';
258
+ } elseif ( in_array( $name, $this->visible_groups ) ) {
259
+ // The tag name represents a visible group, so remove the tags themselves, but return everything else
260
+ //return $content;
261
+ $regex = '@\[[\t ]*([a-zA-Z_][0-9a-zA-Z:._-]*)[\t ]*\](.*?)\[[\t ]*/[\t ]*\1[\t ]*\]@s';
262
+
263
+ // instead of just returning the $content, return the preg_replaced content :)
264
+ return preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $content );
265
+ } else {
266
+ // The tag name doesn't represent a group that was used in the form. Leave it alone (return the entire match).
267
+ return $matches[0];
268
+ }
269
+ }
270
+ }
271
+
272
+ new ContactForm7ConditionalFields;
273
+
274
+ add_filter( 'wpcf7_contact_form_properties', 'wpcf7cf_properties', 10, 2 );
275
+
276
+ function wpcf7cf_properties($properties, $wpcf7form) {
277
+ if (!is_admin()) { // TODO: kind of hacky. maybe find a better solution. Needed because otherwise the group tags will be replaced in the editor as well.
278
+ $form = $properties['form'];
279
+
280
+ $find = array(
281
+ '/\[group\s*\]/s', // matches [group ] or [group]
282
+ '/\[group\s+([^\s\]]*)\s*([^\]]*)\]/s', // matches [group something some:thing] or [group something som ]
283
+ // doesn't match [group-special something]
284
+ '/\[\/group\]/s'
285
+ );
286
+
287
+ $replace = array(
288
+ '<div data-class="wpcf7cf_group">',
289
+ '<div id="$1" data-class="wpcf7cf_group">',
290
+ '</div>'
291
+ );
292
+
293
+ $form = preg_replace( $find, $replace, $form );
294
+
295
+ $properties['form'] = $form;
296
+ }
297
+ return $properties;
298
+ }
299
+
300
+ $global_count = 0;
301
+
302
+ add_action('wpcf7_contact_form', 'wpcf7cf_enqueue_scripts', 10, 1);
303
+ function wpcf7cf_enqueue_scripts(WPCF7_ContactForm $cf7form) {
304
+
305
+ if (is_admin()) return;
306
+
307
+ global $global_count, $post;
308
+ $global_count++;
309
+
310
+ if ( in_the_loop() ) {
311
+ $post_id = empty($post->ID) ? '0' : $post->ID;
312
+ $unit_tag = 'wpcf7-f'.$cf7form->id().'-p'.$post_id.'-o'.$global_count;
313
+ } else {
314
+ $unit_tag = 'wpcf7-f'.$cf7form->id().'-o'.$global_count;
315
+ }
316
+
317
+ $options = array(
318
+ 'form_id' => $cf7form->id(),
319
+ 'unit_tag' => $unit_tag,
320
+ 'conditions' => get_post_meta($cf7form->id(),'wpcf7cf_options', true),
321
+ );
322
+
323
+ wp_enqueue_script('cf7cf-scripts', plugins_url('js/scripts.js', __FILE__), array('jquery'), WPCF7CF_VERSION, true);
324
+ wp_localize_script('cf7cf-scripts', 'wpcf7cf_options_'.$global_count, $options);
325
+ }
326
+
327
+ add_action('wpcf7_form_hidden_fields', 'wpcf7cf_form_hidden_fields',10,1);
328
+
329
+ function wpcf7cf_form_hidden_fields($hidden_fields) {
330
+ return array('_wpcf7cf_hidden_group_fields' => '', '_wpcf7cf_hidden_groups' => '', '_wpcf7cf_visible_groups' => '');
331
+ }
332
+
333
+ function wpcf7cf_endswith($string, $test) {
334
+ $strlen = strlen($string);
335
+ $testlen = strlen($test);
336
+ if ($testlen > $strlen) return false;
337
+ return substr_compare($string, $test, $strlen - $testlen, $testlen) === 0;
338
+ }
contact-form-7-conditional-fields.php CHANGED
@@ -4,7 +4,7 @@ Plugin Name: Contact Form 7 Conditional Fields
4
  Plugin URI: http://bdwm.be/
5
  Description: Adds support for conditional fields to Contact Form 7. This plugin depends on Contact Form 7.
6
  Author: Jules Colle
7
- Version: 1.1
8
  Author URI: http://bdwm.be/
9
  */
10
 
@@ -23,15 +23,13 @@ Author URI: http://bdwm.be/
23
  * along with this program; if not, write to the Free Software
24
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
  */
26
- ?>
27
- <?php
28
 
29
- define( 'WPCF7CF_VERSION', '1.1' );
30
- define( 'WPCF7CF_REQUIRED_WP_VERSION', '4.1' );
31
- define( 'WPCF7CF_PLUGIN', __FILE__ );
32
- define( 'WPCF7CF_PLUGIN_BASENAME', plugin_basename( WPCF7CF_PLUGIN ) );
33
- define( 'WPCF7CF_PLUGIN_NAME', trim( dirname( WPCF7CF_PLUGIN_BASENAME ), '/' ) );
34
- define( 'WPCF7CF_PLUGIN_DIR', untrailingslashit( dirname( WPCF7CF_PLUGIN ) ) );
35
 
36
  function wpcf7cf_plugin_path( $path = '' ) {
37
  return path_join( WPCF7CF_PLUGIN_DIR, trim( $path, '/' ) );
@@ -45,306 +43,12 @@ function wpcf7cf_plugin_url( $path = '' ) {
45
  return $url;
46
  }
47
 
48
- class ContactForm7ConditionalFields {
49
- private $hidden_fields = array();
50
- private $visible_groups = array();
51
- private $hidden_groups = array();
52
-
53
- function __construct() {
54
-
55
- add_action('wpcf7_enqueue_scripts', array(__CLASS__, 'enqueue_js'));
56
- add_action('wpcf7_enqueue_styles', array(__CLASS__, 'enqueue_css'));
57
-
58
- // Register shortcodes
59
- add_action('wpcf7_init', array(__CLASS__, 'add_shortcodes'));
60
-
61
- // Tag generator
62
- add_action('load-contact_page_wpcf7-new', array(__CLASS__, 'tag_generator'));
63
- add_action('load-toplevel_page_wpcf7', array(__CLASS__, 'tag_generator'));
64
-
65
- add_filter( 'wpcf7_posted_data', array($this, 'remove_hidden_post_data') );
66
- add_filter( 'wpcf7_mail_components', array($this, 'hide_hidden_mail_fields') );
67
- add_filter('wpcf7_additional_mail', array($this, 'hide_hidden_mail_fields_additional_mail'), 10, 2);
68
-
69
- //apply_filters( 'wpcf7_additional_mail',$additional_mail, $contact_form )
70
-
71
- add_filter( 'wpcf7_validate', array($this, 'skip_validation_for_hidden_fields'), 2, 2 );
72
-
73
- register_activation_hook(__FILE__, array($this, 'activate'));
74
-
75
- if (is_admin()) {
76
- require_once dirname(__FILE__) . '/admin.php';
77
- }
78
- }
79
-
80
- function activate() {
81
- //add options with add_option and stuff
82
- }
83
-
84
- public static function enqueue_js() {
85
- // nothing here. We will only load the CF7 script if there is a CF7 form on the page.
86
- }
87
-
88
- public static function enqueue_css() {
89
- wp_enqueue_style('cf7cf-style', plugins_url('style.css', __FILE__), array(), WPCF7CF_VERSION);
90
- }
91
-
92
- public static function add_shortcodes() {
93
- //wpcf7_add_shortcode('group', array(__CLASS__, 'shortcode_handler'), true);
94
- //add_shortcode( 'group', array(__CLASS__, 'group_shortcode_handler') );
95
- if (function_exists('wpcf7_add_form_tag'))
96
- wpcf7_add_form_tag('group', array(__CLASS__, 'shortcode_handler'), true);
97
- else if (function_exists('wpcf7_add_shortcode')) {
98
- wpcf7_add_shortcode('group', array(__CLASS__, 'shortcode_handler'), true);
99
- } else {
100
- throw new Exception('functions wpcf7_add_form_tag and wpcf7_add_shortcode not found.');
101
- }
102
- }
103
-
104
- function group_shortcode_handler( $atts, $content = "" ) {
105
- return $content;
106
- }
107
-
108
- function shortcode_handler($tag) {
109
- //$tag = new WPCF7_Shortcode($tag);
110
- $tag = new WPCF7_FormTag($tag);
111
- //ob_start();
112
- //print_r($tag);
113
- //return print_r($tag, true);
114
- return $tag->content;
115
- }
116
-
117
-
118
- public static function tag_generator() {
119
- if (! function_exists( 'wpcf7_add_tag_generator'))
120
- return;
121
-
122
- wpcf7_add_tag_generator('group',
123
- __('Conditional fields Group', 'wpcf7cf'),
124
- 'wpcf7-tg-pane-group',
125
- array(__CLASS__, 'tg_pane')
126
- );
127
- }
128
-
129
- static function tg_pane( $contact_form, $args = '' ) {
130
- $args = wp_parse_args( $args, array() );
131
- $type = 'group';
132
-
133
- $description = __( "Generate a group tag to group form elements that can be shown conditionally.", 'cf7cf' );
134
-
135
- ?>
136
- <div class="control-box">
137
- <fieldset>
138
- <legend><?php echo sprintf( esc_html( $description ) ); ?></legend>
139
-
140
- <table class="form-table">
141
- <tbody>
142
-
143
- <tr>
144
- <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-name' ); ?>"><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?></label></th>
145
- <td><input type="text" name="name" class="tg-name oneline" id="<?php echo esc_attr( $args['content'] . '-name' ); ?>" /></td>
146
- </tr>
147
-
148
- </tbody>
149
- </table>
150
- </fieldset>
151
- </div>
152
-
153
- <div class="insert-box">
154
- <input type="text" name="<?php echo $type; ?>" class="tag code" readonly="readonly" onfocus="this.select()" />
155
-
156
- <div class="submitbox">
157
- <input type="button" class="button button-primary insert-tag" value="<?php echo esc_attr( __( 'Insert Tag', 'contact-form-7' ) ); ?>" />
158
- </div>
159
-
160
- <br class="clear" />
161
- </div>
162
- <?php
163
- }
164
-
165
- /**
166
- * Remove validation requirements for fields that are hidden at the time of form submission.
167
- * Called using add_filter( 'wpcf7_validate_[tag_type]', array($this, 'skip_validation_for_hidden_fields'), 2, 2 );
168
- * where the priority of 2 causes this to kill any validations with a priority higher than 2
169
- *
170
- * @param $result
171
- * @param $tag
172
- *
173
- * @return mixed
174
- */
175
- function skip_validation_for_hidden_fields($result, $tags) {
176
-
177
- if (count($this->hidden_fields) == 0) return $result;
178
-
179
- $return_result = new WPCF7_Validation();
180
-
181
- $invalid_fields = $result->get_invalid_fields();
182
-
183
- if (!is_array($invalid_fields) || count($invalid_fields) == 0) return $result;
184
-
185
- foreach ($invalid_fields as $invalid_field_key => $invalid_field_data) {
186
- if (!in_array($invalid_field_key, $this->hidden_fields)) {
187
- // the invalid field is not a hidden field, so we'll add it to the final validation result
188
- $return_result->invalidate($invalid_field_key, $invalid_field_data['reason']);
189
- }
190
- }
191
-
192
- return $return_result;
193
- }
194
-
195
-
196
- /**
197
- * When a CF7 form is posted, check the form for hidden fields, then remove those fields from the post data
198
- *
199
- * @param $posted_data
200
- *
201
- * @return mixed
202
- */
203
- function remove_hidden_post_data($posted_data) {
204
- $this->set_hidden_fields_arrays($posted_data);
205
-
206
- foreach( $this->hidden_fields as $name => $value ) {
207
- unset( $posted_data[$name] );
208
- }
209
-
210
- return $posted_data;
211
- }
212
-
213
-
214
- /**
215
- * Finds the currently submitted form and set the hidden_fields variables accoringly
216
- *
217
- * @param bool|array $posted_data
218
- */
219
- function set_hidden_fields_arrays($posted_data = false) {
220
-
221
- if (!$posted_data) {
222
- $posted_data = WPCF7_Submission::get_instance()->get_posted_data();
223
- }
224
-
225
- $hidden_fields = json_decode(stripslashes($posted_data['_wpcf7cf_hidden_group_fields']));
226
- if (!is_array($hidden_fields) || count($hidden_fields) == 0) return;
227
- foreach ($hidden_fields as $field) {
228
- $this->hidden_fields[] = $field;
229
- if (wpcf7cf_endswith($field, '[]')) {
230
- $this->hidden_fields[] = substr($field,0,strlen($field)-2);
231
- }
232
- }
233
- $this->hidden_groups = json_decode(stripslashes($posted_data['_wpcf7cf_hidden_groups']));
234
- $this->visible_groups = json_decode(stripslashes($posted_data['_wpcf7cf_visible_groups']));
235
- }
236
-
237
- function hide_hidden_mail_fields( $components ) {
238
- $regex = '@\[[\t ]*([a-zA-Z_][0-9a-zA-Z:._-]*)[\t ]*\](.*?)\[[\t ]*/[\t ]*\1[\t ]*\]@s';
239
- // [1] = name [2] = contents
240
-
241
- $components['body'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $components['body'] );
242
- $components['subject'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $components['subject'] );
243
- $components['sender'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $components['sender'] );
244
- $components['recipient'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $components['recipient'] );
245
- $components['additional_headers'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $components['additional_headers'] );
246
-
247
- return $components;
248
- }
249
-
250
- function hide_hidden_mail_fields_additional_mail($additional_mail, $contact_form) {
251
-
252
- if (!is_array($additional_mail) || !array_key_exists('mail_2', $additional_mail)) return $additional_mail;
253
-
254
- $regex = '@\[[\t ]*([a-zA-Z_][0-9a-zA-Z:._-]*)[\t ]*\](.*?)\[[\t ]*/[\t ]*\1[\t ]*\]@s';
255
-
256
- $additional_mail['mail_2']['body'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $additional_mail['mail_2']['body'] );
257
- $additional_mail['mail_2']['subject'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $additional_mail['mail_2']['subject'] );
258
- $additional_mail['mail_2']['sender'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $additional_mail['mail_2']['sender'] );
259
- $additional_mail['mail_2']['recipient'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $additional_mail['mail_2']['recipient'] );
260
- $additional_mail['mail_2']['additional_headers'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $additional_mail['mail_2']['additional_headers'] );
261
-
262
- return $additional_mail;
263
- }
264
-
265
- function hide_hidden_mail_fields_regex_callback ( $matches ) {
266
- $name = $matches[1];
267
- $content = $matches[2];
268
- if ( in_array( $name, $this->hidden_groups ) ) {
269
- // The tag name represents a hidden group, so replace everything from [tagname] to [/tagname] with nothing
270
- return '';
271
- } elseif ( in_array( $name, $this->visible_groups ) ) {
272
- // The tag name represents a visible group, so remove the tags themselves, but return everything else
273
- //return $content;
274
- $regex = '@\[[\t ]*([a-zA-Z_][0-9a-zA-Z:._-]*)[\t ]*\](.*?)\[[\t ]*/[\t ]*\1[\t ]*\]@s';
275
-
276
- // instead of just returning the $content, return the preg_replaced content :)
277
- return preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $content );
278
- } else {
279
- // The tag name doesn't represent a group that was used in the form. Leave it alone (return the entire match).
280
- return $matches[0];
281
- }
282
- }
283
- }
284
-
285
- new ContactForm7ConditionalFields;
286
-
287
- add_filter( 'wpcf7_contact_form_properties', 'wpcf7cf_properties', 10, 2 );
288
-
289
- function wpcf7cf_properties($properties, $wpcf7form) {
290
- if (!is_admin()) { // TODO: kind of hacky. maybe find a better solution. Needed because otherwise the group tags will be replaced in the editor as well.
291
- $form = $properties['form'];
292
-
293
- $find = array(
294
- '/\[group\s*\]/s', // matches [group ] or [group]
295
- '/\[group\s+([^\s\]]*)\s*([^\]]*)\]/s', // matches [group something some:thing] or [group something som ]
296
- // doesn't match [group-special something]
297
- '/\[\/group\]/s'
298
- );
299
-
300
- $replace = array(
301
- '<div data-class="wpcf7cf_group">',
302
- '<div id="$1" data-class="wpcf7cf_group">',
303
- '</div>'
304
- );
305
-
306
- $form = preg_replace( $find, $replace, $form );
307
-
308
- $properties['form'] = $form;
309
- }
310
- return $properties;
311
- }
312
-
313
- $global_count = 0;
314
-
315
- add_action('wpcf7_contact_form', 'wpcf7cf_enqueue_scripts', 10, 1);
316
- function wpcf7cf_enqueue_scripts(WPCF7_ContactForm $cf7form) {
317
-
318
- if (is_admin()) return;
319
-
320
- global $global_count, $post;
321
- $global_count++;
322
-
323
- if ( in_the_loop() ) {
324
- $unit_tag = 'wpcf7-f'.$cf7form->id().'-p'.$post->ID.'-o'.$global_count;
325
- } else {
326
- $unit_tag = 'wpcf7-f'.$cf7form->id().'-o'.$global_count;
327
- }
328
-
329
- $options = array(
330
- 'form_id' => $cf7form->id(),
331
- 'unit_tag' => $unit_tag,
332
- 'conditions' => get_post_meta($cf7form->id(),'wpcf7cf_options', true),
333
- );
334
-
335
- wp_enqueue_script('cf7cf-scripts', plugins_url('js/scripts.js', __FILE__), array('jquery'), WPCF7CF_VERSION, true);
336
- wp_localize_script('cf7cf-scripts', 'wpcf7cf_options_'.$global_count, $options);
337
- }
338
-
339
- add_action('wpcf7_form_hidden_fields', 'wpcf7cf_form_hidden_fields',10,1);
340
 
341
- function wpcf7cf_form_hidden_fields($hidden_fields) {
342
- return array('_wpcf7cf_hidden_group_fields' => '', '_wpcf7cf_hidden_groups' => '', '_wpcf7cf_visible_groups' => '');
 
 
 
343
  }
344
 
345
- function wpcf7cf_endswith($string, $test) {
346
- $strlen = strlen($string);
347
- $testlen = strlen($test);
348
- if ($testlen > $strlen) return false;
349
- return substr_compare($string, $test, $strlen - $testlen, $testlen) === 0;
350
- }
4
  Plugin URI: http://bdwm.be/
5
  Description: Adds support for conditional fields to Contact Form 7. This plugin depends on Contact Form 7.
6
  Author: Jules Colle
7
+ Version: 1.2
8
  Author URI: http://bdwm.be/
9
  */
10
 
23
  * along with this program; if not, write to the Free Software
24
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
  */
 
 
26
 
27
+ if (!defined('WPCF7CF_VERSION')) define( 'WPCF7CF_VERSION', '1.2' );
28
+ if (!defined('WPCF7CF_REQUIRED_WP_VERSION')) define( 'WPCF7CF_REQUIRED_WP_VERSION', '4.1' );
29
+ if (!defined('WPCF7CF_PLUGIN')) define( 'WPCF7CF_PLUGIN', __FILE__ );
30
+ if (!defined('WPCF7CF_PLUGIN_BASENAME')) define( 'WPCF7CF_PLUGIN_BASENAME', plugin_basename( WPCF7CF_PLUGIN ) );
31
+ if (!defined('WPCF7CF_PLUGIN_NAME')) define( 'WPCF7CF_PLUGIN_NAME', trim( dirname( WPCF7CF_PLUGIN_BASENAME ), '/' ) );
32
+ if (!defined('WPCF7CF_PLUGIN_DIR')) define( 'WPCF7CF_PLUGIN_DIR', untrailingslashit( dirname( WPCF7CF_PLUGIN ) ) );
33
 
34
  function wpcf7cf_plugin_path( $path = '' ) {
35
  return path_join( WPCF7CF_PLUGIN_DIR, trim( $path, '/' ) );
43
  return $url;
44
  }
45
 
46
+ require_once 'cf7cf.php';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
+ if(file_exists(WPCF7CF_PLUGIN_DIR.'/pro-functions.php')) {
49
+ if (!defined('WPCF7CF_IS_PRO')) define( 'WPCF7CF_IS_PRO', true );
50
+ require_once 'pro-functions.php';
51
+ } else {
52
+ if (!defined('WPCF7CF_IS_PRO')) define( 'WPCF7CF_IS_PRO', false );
53
  }
54
 
 
 
 
 
 
 
js/scripts.js CHANGED
@@ -29,11 +29,13 @@ var cf7signature_resized = 0; // for compatibility with contact-form-7-signature
29
  }
30
  }
31
 
32
- $("#"+unit_tag+" [data-class='wpcf7cf_group']").hide();
33
  for (var i=0; i < wpcf7cf_conditions.length; i++) {
34
 
35
  var condition = wpcf7cf_conditions[i];
36
 
 
 
37
  $field = $('#'+unit_tag+' [name="'+condition.if_field+'"]').length ? $('#'+unit_tag+' [name="'+condition.if_field+'"]') : $('#'+unit_tag+' [name="'+condition.if_field+'[]"]');
38
 
39
  if ($field.length == 1) {
@@ -50,15 +52,21 @@ var cf7signature_resized = 0; // for compatibility with contact-form-7-signature
50
 
51
  $field.find('option:selected').each(function () {
52
  var $option = $(this);
53
- if (condition.operator == 'equals' && $option.val() == condition.if_value) {
 
 
 
54
  show = true;
55
- } else if (condition.operator == 'not equals' && $option.val() == condition.if_value) {
 
 
 
56
  show = false;
57
  }
58
  });
59
 
60
  if(show == true) {
61
- $('#' + unit_tag + ' #' + condition.then_field).show();
62
  }
63
 
64
  continue;
@@ -66,14 +74,32 @@ var cf7signature_resized = 0; // for compatibility with contact-form-7-signature
66
 
67
  if ($field.attr('type') == 'checkbox') {
68
  if (
69
- $field.is(':checked') && condition.operator == 'equals' && $field.val() == condition.if_value
70
- || !$field.is(':checked') && condition.operator == 'not equals' && $field.val() == condition.if_value
71
- || condition.operator == 'not equals' && $field.val() != condition.if_value
 
 
 
 
 
 
 
72
  ) {
73
- $('#'+unit_tag+' #'+condition.then_field).show();
74
  }
75
- } else if (condition.operator == 'equals' && $field.val() == condition.if_value || condition.operator == 'not equals' && $field.val() != condition.if_value) {
76
- $('#'+unit_tag+' #'+condition.then_field).show();
 
 
 
 
 
 
 
 
 
 
 
77
  }
78
 
79
 
@@ -90,12 +116,36 @@ var cf7signature_resized = 0; // for compatibility with contact-form-7-signature
90
  }
91
  });
92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
 
95
- if (condition.operator == 'equals' && $.inArray(condition.if_value, checked_values) != -1) {
96
- $('#'+unit_tag+' #'+condition.then_field).show();
97
- } else if (condition.operator == 'not equals' && $.inArray(condition.if_value, all_values) != -1 && $.inArray(condition.if_value, checked_values) == -1) {
98
- $('#'+unit_tag+' #'+condition.then_field).show();
 
 
 
 
 
 
 
 
 
99
  }
100
  }
101
 
@@ -107,15 +157,12 @@ var cf7signature_resized = 0; // for compatibility with contact-form-7-signature
107
  var conditions = options[i]['conditions'];
108
  display_fields(unit_tag, conditions);
109
  $('#'+unit_tag+' input, #'+unit_tag+' select, #'+unit_tag+' textarea').change({unit_tag:unit_tag, conditions:conditions}, function(e) {
110
- console.log('change');
111
  display_fields(e.data.unit_tag, e.data.conditions);
112
  });
113
  }
114
 
115
  // before the form values are serialized to submit via ajax, we quickly add all invisible fields in the hidden
116
  // _wpcf7cf_hidden_group_fields field, so the PHP code knows which fields were inside hidden groups.
117
- // TODO: maybe modify this code so it only takes fields which are strictly inside hidden group tags.
118
- // TODO: For now the hidden field is filled with all hidden form elements.
119
 
120
  $('form.wpcf7-form').on('form-pre-serialize', function(form,options,veto) {
121
  $form = $(form.target);
@@ -128,21 +175,21 @@ var cf7signature_resized = 0; // for compatibility with contact-form-7-signature
128
  var hidden_groups = [];
129
  var visible_groups = [];
130
 
131
- $form.find('input:hidden,select:hidden,textarea:hidden').each(function () {
132
- hidden_fields.push($(this).attr('name'));
133
- });
134
-
135
- $form.find('[data-class="wpcf7cf_group"]:hidden').each(function () {
136
- hidden_groups.push($(this).attr('id'));
137
- });
138
-
139
- $form.find('[data-class="wpcf7cf_group"]:visible').each(function () {
140
- visible_groups.push($(this).attr('id'));
141
  });
142
 
143
- $($hidden_group_fields).val(JSON.stringify(hidden_fields));
144
- $($hidden_groups).val(JSON.stringify(hidden_groups));
145
- $($visible_groups).val(JSON.stringify(visible_groups));
146
 
147
  return true;
148
  });
@@ -164,7 +211,6 @@ var cf7signature_resized = 0; // for compatibility with contact-form-7-signature
164
  $.fn.wpcf7ExclusiveCheckbox = function() {
165
  return this.find('input:checkbox').click(function() {
166
  var name = $(this).attr('name');
167
- console.log('new func');
168
  $(this).closest('form').find('input:checkbox[name="' + name + '"]').not(this).prop('checked', false).eq(0).change();
169
  });
170
  };
29
  }
30
  }
31
 
32
+ $("#"+unit_tag+" [data-class='wpcf7cf_group']").hide().addClass('wpcf7cf-hidden');
33
  for (var i=0; i < wpcf7cf_conditions.length; i++) {
34
 
35
  var condition = wpcf7cf_conditions[i];
36
 
37
+ var regex_patt = new RegExp(condition.if_value,'i');
38
+
39
  $field = $('#'+unit_tag+' [name="'+condition.if_field+'"]').length ? $('#'+unit_tag+' [name="'+condition.if_field+'"]') : $('#'+unit_tag+' [name="'+condition.if_field+'[]"]');
40
 
41
  if ($field.length == 1) {
52
 
53
  $field.find('option:selected').each(function () {
54
  var $option = $(this);
55
+ if (
56
+ condition.operator == 'equals' && $option.val() == condition.if_value ||
57
+ condition.operator == 'equals (regex)' && regex_patt.test($option.val())
58
+ ) {
59
  show = true;
60
+ } else if (
61
+ condition.operator == 'not equals' && $option.val() == condition.if_value ||
62
+ condition.operator == 'not equals (regex)' && !regex_patt.test($option.val())
63
+ ) {
64
  show = false;
65
  }
66
  });
67
 
68
  if(show == true) {
69
+ $('#' + unit_tag + ' #' + condition.then_field).show().removeClass('wpcf7cf-hidden');
70
  }
71
 
72
  continue;
74
 
75
  if ($field.attr('type') == 'checkbox') {
76
  if (
77
+ condition.operator == 'equals' && $field.is(':checked') && $field.val() == condition.if_value ||
78
+ condition.operator == 'not equals' && !$field.is(':checked') ||
79
+ condition.operator == 'is empty' && !$field.is(':checked') ||
80
+ condition.operator == 'not empty' && $field.is(':checked') ||
81
+ condition.operator == '>' && $field.is(':checked') && $field.val() > condition.if_value ||
82
+ condition.operator == '<' && $field.is(':checked') && $field.val() < condition.if_value ||
83
+ condition.operator == '>=' && $field.is(':checked') && $field.val() >= condition.if_value ||
84
+ condition.operator == '<=' && $field.is(':checked') && $field.val() <= condition.if_value ||
85
+ condition.operator == 'equals (regex)' && $field.is(':checked') && regex_patt.test($field.val()) ||
86
+ condition.operator == 'not equals (regex)' && !$field.is(':checked')
87
  ) {
88
+ $('#'+unit_tag+' #'+condition.then_field).show().removeClass('wpcf7cf-hidden');
89
  }
90
+ } else if (
91
+ ( condition.operator == 'equals' && $field.val() == condition.if_value ) ||
92
+ ( condition.operator == 'not equals' && $field.val() != condition.if_value ) ||
93
+ ( condition.operator == 'equals (regex)' && regex_patt.test($field.val()) ) ||
94
+ ( condition.operator == 'not equals (regex)' && !regex_patt.test($field.val()) ) ||
95
+ ( condition.operator == '>' && $field.val() > condition.if_value ) ||
96
+ ( condition.operator == '<' && $field.val() < condition.if_value ) ||
97
+ ( condition.operator == '>=' && $field.val() >= condition.if_value ) ||
98
+ ( condition.operator == '<=' && $field.val() <= condition.if_value ) ||
99
+ ( condition.operator == 'is empty' && $field.val() == '' ) ||
100
+ ( condition.operator == 'not empty' && $field.val() != '' )
101
+ ) {
102
+ $('#'+unit_tag+' #'+condition.then_field).show().removeClass('wpcf7cf-hidden');
103
  }
104
 
105
 
116
  }
117
  });
118
 
119
+ var checked_value_index = $.inArray(condition.if_value, checked_values);
120
+ var value_index = $.inArray(condition.if_value, all_values);
121
+
122
+ // console.log(all_values);
123
+ // console.log(checked_values);
124
+ // console.log(condition);
125
+ // console.log(value_index);
126
+ // console.log(checked_value_index);
127
+
128
+ if (
129
+ ( condition.operator == 'is empty' && checked_values.length == 0 ) ||
130
+ ( condition.operator == 'not empty' && checked_values.length > 0 )
131
+ ) {
132
+ $('#'+unit_tag+' #'+condition.then_field).show().removeClass('wpcf7cf-hidden');
133
+ }
134
 
135
 
136
+ for(var ind=0; ind<checked_values.length; ind++) {
137
+ if (
138
+ ( condition.operator == 'equals' && checked_values[ind] == condition.if_value ) ||
139
+ ( condition.operator == 'not equals' && checked_values[ind] != condition.if_value ) ||
140
+ ( condition.operator == 'equals (regex)' && regex_patt.test(checked_values[ind]) ) ||
141
+ ( condition.operator == 'not equals (regex)' && !regex_patt.test(checked_values[ind]) ) ||
142
+ ( condition.operator == '>' && checked_values[ind] > condition.if_value ) ||
143
+ ( condition.operator == '<' && checked_values[ind] < condition.if_value ) ||
144
+ ( condition.operator == '>=' && checked_values[ind] >= condition.if_value ) ||
145
+ ( condition.operator == '<=' && checked_values[ind] <= condition.if_value )
146
+ ) {
147
+ $('#'+unit_tag+' #'+condition.then_field).show().removeClass('wpcf7cf-hidden');
148
+ }
149
  }
150
  }
151
 
157
  var conditions = options[i]['conditions'];
158
  display_fields(unit_tag, conditions);
159
  $('#'+unit_tag+' input, #'+unit_tag+' select, #'+unit_tag+' textarea').change({unit_tag:unit_tag, conditions:conditions}, function(e) {
 
160
  display_fields(e.data.unit_tag, e.data.conditions);
161
  });
162
  }
163
 
164
  // before the form values are serialized to submit via ajax, we quickly add all invisible fields in the hidden
165
  // _wpcf7cf_hidden_group_fields field, so the PHP code knows which fields were inside hidden groups.
 
 
166
 
167
  $('form.wpcf7-form').on('form-pre-serialize', function(form,options,veto) {
168
  $form = $(form.target);
175
  var hidden_groups = [];
176
  var visible_groups = [];
177
 
178
+ $form.find('[data-class="wpcf7cf_group"]').each(function () {
179
+ var $this = $(this);
180
+ if ($this.hasClass('wpcf7cf-hidden')) {
181
+ hidden_groups.push($this.attr('id'));
182
+ $this.find('input,select,textarea').each(function () {
183
+ hidden_fields.push($(this).attr('name'));
184
+ });
185
+ } else {
186
+ visible_groups.push($this.attr('id'));
187
+ }
188
  });
189
 
190
+ $hidden_group_fields.val(JSON.stringify(hidden_fields));
191
+ $hidden_groups.val(JSON.stringify(hidden_groups));
192
+ $visible_groups.val(JSON.stringify(visible_groups));
193
 
194
  return true;
195
  });
211
  $.fn.wpcf7ExclusiveCheckbox = function() {
212
  return this.find('input:checkbox').click(function() {
213
  var name = $(this).attr('name');
 
214
  $(this).closest('form').find('input:checkbox[name="' + name + '"]').not(this).prop('checked', false).eq(0).change();
215
  });
216
  };
js/scripts_admin.js CHANGED
@@ -24,4 +24,115 @@ var old_compose = _wpcf7.taggen.compose;
24
  return ret;
25
  };
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  })( jQuery );
24
  return ret;
25
  };
26
 
27
+ var index = $('#wpcf7cf-entries .entry').length;
28
+
29
+ $('.delete-button').click(function(){
30
+
31
+ //if (confirm('You sure?')===false) return false;
32
+ $(this).parent().remove();
33
+ return false;
34
+
35
+ });
36
+
37
+ $('#wpcf7cf-add-button').click(function(){
38
+
39
+ var id = add_condition_fields();
40
+
41
+ return false;
42
+
43
+ });
44
+
45
+ function clear_all_condition_fields() {
46
+ $('.entry').remove();
47
+ }
48
+
49
+ function add_condition_fields() {
50
+ var $delete_button = $('#wpcf7cf-delete-button').clone().removeAttr('id');
51
+ $('<div class="entry" id="entry-'+index+'">'+($('#wpcf7cf-new-entry').html().replace(/{id}/g, index))+'</div>').prependTo('#wpcf7cf-entries').append($delete_button);
52
+ $delete_button.click(function(){
53
+ $(this).parent().remove();
54
+ return false;
55
+ });
56
+ index++;
57
+
58
+ return (index-1);
59
+ }
60
+
61
+ function import_condition_fields() {
62
+ var lines = $('#wpcf7cf-settings-text').val().split(/\r?\n/);
63
+ for (var i = lines.length+1; i>-1; i--) {
64
+
65
+ var str = lines[i];
66
+
67
+ var match = regex.exec(str);
68
+
69
+ if (match == null) continue;
70
+
71
+ var id = add_condition_fields();
72
+
73
+ $('#entry-'+id+' .if-field-select').val(match[1]);
74
+ $('#entry-'+id+' .operator').val(match[2]);
75
+ $('#entry-'+id+' .if-value').val(match[3]);
76
+ $('#entry-'+id+' .then-field-select').val(match[4]);
77
+
78
+ regex.lastIndex = 0;
79
+ }
80
+ }
81
+
82
+ // export/import settings
83
+
84
+ $('#wpcf7cf-settings-text-wrap').hide();
85
+
86
+ $('#wpcf7cf-settings-to-text').click(function() {
87
+ $('#wpcf7cf-settings-text-wrap').show();
88
+
89
+ $('#wpcf7cf-settings-text').val('');
90
+ $('#wpcf7cf-entries .entry').each(function() {
91
+ var $entry = $(this);
92
+ var line = 'if [' + $entry.find('.if-field-select').val() + ']'
93
+ + ' ' + $entry.find('.operator').val()
94
+ + ' "' + $entry.find('.if-value').val() + '" then show'
95
+ + ' [' + $entry.find('.then-field-select').val() + ']';
96
+ $('#wpcf7cf-settings-text').val($('#wpcf7cf-settings-text').val() + line + "\n" ).select();
97
+ });
98
+ return false;
99
+ });
100
+
101
+ var regex = /if \[(.*)] (equals|not equals|equals \(regex\)|not equals \(regex\)|>|>=|<=|<|is empty|not empty) "(.*)" then show \[(.*)]/g;
102
+
103
+ $('#add-fields').click(function() {
104
+ import_condition_fields();
105
+ update_entries();
106
+ return false;
107
+ });
108
+
109
+ $('#overwrite-fields').click(function() {
110
+ clear_all_condition_fields();
111
+ import_condition_fields();
112
+ update_entries();
113
+ return false;
114
+ });
115
+
116
+ $('#wpcf7cf-settings-text-clear').click(function() {
117
+ $('#wpcf7cf-settings-text-wrap').hide();
118
+ $('#wpcf7cf-settings-text').val('');
119
+ return false;
120
+ });
121
+
122
+ function update_entries() {
123
+ $('.if-value').css({'visibility':'visible'});
124
+ $('.entry').each(function() {
125
+ var $entry = $(this);
126
+ if ($entry.find('.operator').eq(0).val() == 'is empty' || $entry.find('.operator').eq(0).val() == 'not empty') {
127
+ $entry.find('.if-value').eq(0).css({'visibility':'hidden'});
128
+ }
129
+ });
130
+ }
131
+
132
+ update_entries();
133
+ $('.operator').change(function() {
134
+ update_entries();
135
+ });
136
+
137
+
138
  })( jQuery );
readme.txt CHANGED
@@ -6,7 +6,7 @@ Website: http://bdwm.be
6
  Tags: wordpress, contact form 7, forms, conditional fields
7
  Requires at least: 4.1
8
  Tested up to: 4.7.2
9
- Stable tag: 1.1
10
  License: GPLv2 or later
11
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
12
 
@@ -24,10 +24,43 @@ Then you should go to the "Conditional fields" tab to create one or more conditi
24
 
25
  A detailed example of how to use the plugin can be found here: [http://bdwm.be/wpcf7cf/how-to-set-up-conditional-fields-for-contact-form-7/](http://bdwm.be/wpcf7cf/how-to-set-up-conditional-fields-for-contact-form-7/)
26
 
27
- = What's new? =
28
 
29
- * Required fields can be used inside hidden groups without causing validation problems.
30
- * Conditional groups can now be added to the emails as well. Just wrap the content with `[group-name] ... [/group-name]` tags.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
 
33
  == Installation ==
@@ -36,9 +69,12 @@ Please follow the [standard installation procedure for WordPress plugins](http:/
36
 
37
  == Frequently Asked Questions ==
38
 
39
- = Why are there not more Frequently asked questions? =
 
 
40
 
41
- Because no questions have been asked frequently about this plugin.
 
42
 
43
  == Screenshots ==
44
 
@@ -47,6 +83,13 @@ Because no questions have been asked frequently about this plugin.
47
 
48
  == Changelog ==
49
 
 
 
 
 
 
 
 
50
  = 1.1 =
51
  * Added import feature
52
  * Added support for nested groups in email
6
  Tags: wordpress, contact form 7, forms, conditional fields
7
  Requires at least: 4.1
8
  Tested up to: 4.7.2
9
+ Stable tag: 1.2
10
  License: GPLv2 or later
11
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
12
 
24
 
25
  A detailed example of how to use the plugin can be found here: [http://bdwm.be/wpcf7cf/how-to-set-up-conditional-fields-for-contact-form-7/](http://bdwm.be/wpcf7cf/how-to-set-up-conditional-fields-for-contact-form-7/)
26
 
27
+ == Main/ New features ==
28
 
29
+ = Compatible with Contact Form 7 Multi-Step Forms =
30
+
31
+ Conditional Fields for Contact Form 7 is now fully compatible with <a target="_blank" href="https://wordpress.org/plugins/contact-form-7-multi-step-module/">Contact Form 7 Multi-Step Forms</a>
32
+
33
+ = Support for required fields =
34
+
35
+ Required fields can be used inside hidden groups without causing validation problems.
36
+
37
+ = Hide/show info in emails based on what groups are visible =
38
+
39
+ Conditional groups can now be added to the emails as well.
40
+ Just wrap the content with `[group-name] ... [/group-name]` tags.
41
+
42
+ = Groups can be nested =
43
+ Groups can be nested, both in the form and in the email
44
+
45
+ Example form:
46
+ `
47
+ [group group-1]
48
+ [group group-inside-1]
49
+ ...
50
+ [/group]
51
+ [/group]`
52
+
53
+ Example email:
54
+ `
55
+ [group-1]
56
+ [group-inside-1]
57
+ ...
58
+ [/group-inside-1]
59
+ [/group-1]`
60
+
61
+ = Advanced =
62
+
63
+ Advanced users can now code up the conditions as plain text instead of using the select boxes, using the import/export feature.
64
 
65
 
66
  == Installation ==
69
 
70
  == Frequently Asked Questions ==
71
 
72
+ = Something isn't working. Why? =
73
+
74
+ I will assume that you successfully installed both plugins, that you were able to create some conditional groups, and that you managed to create some conditions. But for some reason it's not working the way you expect it too. Here are the most common problems/causes people have encountered in the support forums. (Ordered from most frequent to least frequent.)
75
 
76
+ 1. <strong>All field names should be unique</strong> - Even though your fields might never show up at the same time, it is still important to realize that WPCF7CF will not remove the fields, it merely hides them. So all fields will be submitted when the form is sent. Because of this no two fields can have the same name.
77
+ 1. <strong>All my groups show up all the time and never get hidden.</strong> - Likely this is due to a javascript error caused by your theme or another plugin. WPCF7CF loads it's scripts at the bottom of the HTML page. If some javascript error gets triggered before, the code will not be executed. Before reaching out to the support forum try to determine which plugin or theme is causing the problem, by gradually disabling plugins and changing theme. Your browser's developer tools (F12) might point you in the right direction.
78
 
79
  == Screenshots ==
80
 
83
 
84
  == Changelog ==
85
 
86
+ = 1.2 =
87
+ * Made compatible with <a href="https://wordpress.org/plugins/contact-form-7-multi-step-module/">Contact Form 7 Multi-Step Forms</a>
88
+ * Small bug fix by Manual from advantia.net: now only considering fields which are strictly inside hidden group tags with form submit. Important in some edge cases where form elements get hidden by other mechanisms, i.e. tabbed forms.
89
+ * Started work on CF7CF Pro, made some structural code modifications so the free plugin can function as the base for both plugins.
90
+ * Removed some debug code
91
+ * Updated readme file
92
+
93
  = 1.1 =
94
  * Added import feature
95
  * Added support for nested groups in email