Conditional Fields for Contact Form 7 - Version 1.5

Version Description

(04-21-19) = * Make it possible to load forms with AJAX https://github.com/pwkip/contact-form-7-conditional-fields/issues/25 and https://conditional-fields-cf7.bdwm.be/docs/faq/how-to-initialize-the-conditional-logic-after-an-ajax-call/ * Massive code reorganization in scripts.js * Fixed bug that could appear after removing an AND condition. * solve WPCF7_ADMIN_READ_WRITE_CAPABILITY - https://github.com/pwkip/contact-form-7-conditional-fields/pull/16 * disable part of the faulty remove_hidden_post_data function. - https://github.com/pwkip/contact-form-7-conditional-fields/pull/17 * Fix "Dismiss notice" on Conditional Fields for Contact Form 7 Settings page * use the "wpcf7_before_send_mail" hook instead of "wpcf7_mail_components" to hide mail groups. The wpcf7_before_send_mail hook is called earlier, so it allows to also hide groups in the attachment field and in messages. * Allow conditional group tags in success and error messages. https://github.com/pwkip/contact-form-7-conditional-fields/issues/23 * duplicating a form will also duplicate conditions https://github.com/pwkip/contact-form-7-conditional-fields/issues/28

Download this release

Release Info

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

Code changes from version 1.4.3 to 1.5

Files changed (7) hide show
  1. admin.php +58 -9
  2. cf7cf.php +20 -29
  3. contact-form-7-conditional-fields.php +1 -1
  4. init.php +1 -1
  5. js/scripts.js +108 -143
  6. js/scripts_admin.js +23 -18
  7. readme.txt +12 -1
admin.php CHANGED
@@ -15,10 +15,12 @@ function wpcf7cf_admin_enqueue_scripts( $hook_suffix ) {
15
  add_filter('wpcf7_editor_panels', 'add_conditional_panel');
16
 
17
  function add_conditional_panel($panels) {
18
- $panels['wpcf7cf-conditional-panel'] = array(
19
- 'title' => __( 'Conditional fields', 'wpcf7cf' ),
20
- 'callback' => 'wpcf7cf_editor_panel_conditional'
21
- );
 
 
22
  return $panels;
23
  }
24
 
@@ -104,18 +106,49 @@ function wpcf7cf_editor_panel_conditional($form) {
104
  <?php
105
  }
106
 
107
- // define the wpcf7_save_contact_form callback
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  function wpcf7cf_save_contact_form( $contact_form )
109
  {
110
- if ( ! isset( $_POST ) || empty( $_POST ) || ! isset( $_POST['wpcf7cf_options'] ) || ! is_array( $_POST['wpcf7cf_options'] ) )
 
111
  return;
 
112
  $post_id = $contact_form->id();
113
  if ( ! $post_id )
114
  return;
115
 
116
  unset($_POST['wpcf7cf_options']['{id}']); // remove the dummy entry
117
 
118
- $options = array_values($_POST['wpcf7cf_options']);
 
 
119
 
120
  update_post_meta( $post_id, 'wpcf7cf_options', $options );
121
 
@@ -123,8 +156,24 @@ function wpcf7cf_save_contact_form( $contact_form )
123
 
124
  };
125
 
126
- // add the action
127
- add_action( 'wpcf7_save_contact_form', 'wpcf7cf_save_contact_form', 10, 1 );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
 
129
  function print_entries_html($form, $wpcf7cf_entries = false) {
130
 
15
  add_filter('wpcf7_editor_panels', 'add_conditional_panel');
16
 
17
  function add_conditional_panel($panels) {
18
+ if ( current_user_can( 'wpcf7_edit_contact_form' ) ) {
19
+ $panels['wpcf7cf-conditional-panel'] = array(
20
+ 'title' => __( 'Conditional fields', 'wpcf7cf' ),
21
+ 'callback' => 'wpcf7cf_editor_panel_conditional'
22
+ );
23
+ }
24
  return $panels;
25
  }
26
 
106
  <?php
107
  }
108
 
109
+ // duplicate conditions on duplicate form part 1.
110
+ add_filter('wpcf7_copy','wpcf7cf_copy', 10, 2);
111
+ function wpcf7cf_copy($new_form,$current_form) {
112
+
113
+ $id = $current_form->id();
114
+ $props = $new_form->get_properties();
115
+ $props['messages']['wpcf7cf_copied'] = $id;
116
+ $new_form->set_properties($props);
117
+
118
+ return $new_form;
119
+ }
120
+
121
+ // duplicate conditions on duplicate form part 2.
122
+ add_action('wpcf7_after_save','wpcf7cf_after_save',10,1);
123
+ function wpcf7cf_after_save($contact_form) {
124
+ $props = $contact_form->get_properties();
125
+ $original_id = isset($props['messages']['wpcf7cf_copied']) ? $props['messages']['wpcf7cf_copied'] : 0;
126
+ if ($original_id !== 0) {
127
+ $post_id = $contact_form->id();
128
+ unset($props['messages']['wpcf7cf_copied']);
129
+ $contact_form->set_properties($props);
130
+ update_post_meta( $post_id, 'wpcf7cf_options', get_post_meta($original_id, 'wpcf7cf_options', true));
131
+ return;
132
+ }
133
+ }
134
+
135
+ // wpcf7_save_contact_form callback
136
+ add_action( 'wpcf7_save_contact_form', 'wpcf7cf_save_contact_form', 10, 1 );
137
  function wpcf7cf_save_contact_form( $contact_form )
138
  {
139
+
140
+ if ( ! isset( $_POST ) || empty( $_POST ) || ! isset( $_POST['wpcf7cf_options'] ) || ! is_array( $_POST['wpcf7cf_options'] ) ) {
141
  return;
142
+ }
143
  $post_id = $contact_form->id();
144
  if ( ! $post_id )
145
  return;
146
 
147
  unset($_POST['wpcf7cf_options']['{id}']); // remove the dummy entry
148
 
149
+ $options = wpcf7cf_sanitize_options($_POST['wpcf7cf_options']);
150
+
151
+
152
 
153
  update_post_meta( $post_id, 'wpcf7cf_options', $options );
154
 
156
 
157
  };
158
 
159
+ function wpcf7cf_sanitize_options($options) {
160
+ //$options = array_values($options);
161
+ $sanitized_options = [];
162
+ foreach ($options as $option_entry) {
163
+ $sanitized_option = [];
164
+ $sanitized_option['then_field'] = sanitize_text_field($option_entry['then_field']);
165
+ foreach ($option_entry['and_rules'] as $and_rule) {
166
+ $sanitized_option['and_rules'][] = [
167
+ 'if_field' => sanitize_text_field($and_rule['if_field']),
168
+ 'operator' => sanitize_text_field($and_rule['operator']),
169
+ 'if_value' => sanitize_text_field($and_rule['if_value']),
170
+ ];
171
+ }
172
+
173
+ $sanitized_options[] = $sanitized_option;
174
+ }
175
+ return $sanitized_options;
176
+ }
177
 
178
  function print_entries_html($form, $wpcf7cf_entries = false) {
179
 
cf7cf.php CHANGED
@@ -24,10 +24,8 @@ class ContactForm7ConditionalFields {
24
  add_action('wp_ajax_cf7mls_validation', array($this,'cf7mls_validation_callback'),9);
25
  add_action('wp_ajax_nopriv_cf7mls_validation', array($this,'cf7mls_validation_callback'),9);
26
 
 
27
  add_filter( 'wpcf7_posted_data', array($this, 'remove_hidden_post_data') );
28
- add_filter( 'wpcf7_mail_components', array($this, 'hide_hidden_mail_fields') );
29
-
30
- //add_filter( 'wpcf7_additional_mail', array($this, 'hide_hidden_mail_fields_additional_mail'), 10, 2);
31
 
32
  add_filter( 'wpcf7_validate', array($this, 'skip_validation_for_hidden_fields'), 2, 2 );
33
 
@@ -35,6 +33,7 @@ class ContactForm7ConditionalFields {
35
  add_action('wpcf7_config_validator_validate', array($this,'wpcf7cf_config_validator_validate'));
36
 
37
 
 
38
 
39
  register_activation_hook(__FILE__, array($this, 'activate'));
40
 
@@ -88,7 +87,8 @@ class ContactForm7ConditionalFields {
88
  }
89
 
90
  public static function enqueue_js() {
91
- // nothing here. We will only load the CF7 script if there is a CF7 form on the page.
 
92
  }
93
 
94
  public static function enqueue_css() {
@@ -182,9 +182,12 @@ class ContactForm7ConditionalFields {
182
  function remove_hidden_post_data($posted_data) {
183
  $this->set_hidden_fields_arrays($posted_data);
184
 
185
- foreach( $this->hidden_fields as $name => $value ) {
186
- unset( $posted_data[$name] );
187
- }
 
 
 
188
 
189
  return $posted_data;
190
  }
@@ -225,7 +228,6 @@ class ContactForm7ConditionalFields {
225
  $this->set_hidden_fields_arrays($_POST);
226
  }
227
 
228
-
229
  /**
230
  * Finds the currently submitted form and set the hidden_fields variables accoringly
231
  *
@@ -250,21 +252,16 @@ class ContactForm7ConditionalFields {
250
  $this->visible_groups = json_decode(stripslashes($posted_data['_wpcf7cf_visible_groups']));
251
  }
252
 
253
- function hide_hidden_mail_fields( $components ) {
254
-
255
- foreach ($components as $key => $val) {
256
- $components[$key] = preg_replace_callback(WPCF7CF_REGEX_MAIL_GROUP, array($this, 'hide_hidden_mail_fields_regex_callback'), $val );
257
- }
258
-
259
- return $components;
260
- }
261
-
262
- // Seems like it was not needed.
263
- // function hide_hidden_mail_fields_additional_mail($additional_mail, $contact_form) {
264
- // if (!is_array($additional_mail) || !array_key_exists('mail_2', $additional_mail)) return $additional_mail;
265
- // $additional_mail['mail_2'] = $this->hide_hidden_mail_fields($additional_mail['mail_2']);
266
- // return $additional_mail;
267
- // }
268
 
269
  function hide_hidden_mail_fields_regex_callback ( $matches ) {
270
  $name = $matches[1];
@@ -330,12 +327,6 @@ function wpcf7cf_properties($properties, $wpcf7form) {
330
  return $properties;
331
  }
332
 
333
- add_action('wpcf7_contact_form', 'wpcf7cf_enqueue_scripts', 10, 1);
334
- function wpcf7cf_enqueue_scripts(WPCF7_ContactForm $cf7form) {
335
- if (is_admin()) return;
336
- wp_enqueue_script('wpcf7cf-scripts', plugins_url('js/scripts.js', __FILE__), array('jquery'), WPCF7CF_VERSION, true);
337
- }
338
-
339
  add_action('wpcf7_form_hidden_fields', 'wpcf7cf_form_hidden_fields',10,1);
340
 
341
  function wpcf7cf_form_hidden_fields($hidden_fields) {
24
  add_action('wp_ajax_cf7mls_validation', array($this,'cf7mls_validation_callback'),9);
25
  add_action('wp_ajax_nopriv_cf7mls_validation', array($this,'cf7mls_validation_callback'),9);
26
 
27
+ // check which fields are hidden during form submission and change some stuff accordingly
28
  add_filter( 'wpcf7_posted_data', array($this, 'remove_hidden_post_data') );
 
 
 
29
 
30
  add_filter( 'wpcf7_validate', array($this, 'skip_validation_for_hidden_fields'), 2, 2 );
31
 
33
  add_action('wpcf7_config_validator_validate', array($this,'wpcf7cf_config_validator_validate'));
34
 
35
 
36
+ add_action("wpcf7_before_send_mail", [$this, 'hide_hidden_mail_fields'], 10, 3);
37
 
38
  register_activation_hook(__FILE__, array($this, 'activate'));
39
 
87
  }
88
 
89
  public static function enqueue_js() {
90
+ if (is_admin()) return;
91
+ wp_enqueue_script('wpcf7cf-scripts', plugins_url('js/scripts.js', __FILE__), array('jquery'), WPCF7CF_VERSION, true);
92
  }
93
 
94
  public static function enqueue_css() {
182
  function remove_hidden_post_data($posted_data) {
183
  $this->set_hidden_fields_arrays($posted_data);
184
 
185
+ // TODO: activating the code below will change the behaviour of the plugin, as all hidden fields will not be POSTed.
186
+ // We might consider activating this based on a setting in the future. But for now, we're not gonna use it.
187
+
188
+ // foreach( $this->hidden_fields as $name => $value ) {
189
+ // unset( $posted_data[$value] ); // Yes, it should be $value, not $name. https://github.com/pwkip/contact-form-7-conditional-fields/pull/17
190
+ // }
191
 
192
  return $posted_data;
193
  }
228
  $this->set_hidden_fields_arrays($_POST);
229
  }
230
 
 
231
  /**
232
  * Finds the currently submitted form and set the hidden_fields variables accoringly
233
  *
252
  $this->visible_groups = json_decode(stripslashes($posted_data['_wpcf7cf_visible_groups']));
253
  }
254
 
255
+ function hide_hidden_mail_fields($form,$abort,$submission) {
256
+ $props = $form->get_properties();
257
+ $mails = ['mail','mail_2','messages'];
258
+ foreach ($mails as $mail) {
259
+ foreach ($props[$mail] as $key=>$val) {
260
+ $props[$mail][$key] = preg_replace_callback(WPCF7CF_REGEX_MAIL_GROUP, array($this, 'hide_hidden_mail_fields_regex_callback'), $val );
261
+ }
262
+ }
263
+ $form->set_properties($props);
264
+ }
 
 
 
 
 
265
 
266
  function hide_hidden_mail_fields_regex_callback ( $matches ) {
267
  $name = $matches[1];
327
  return $properties;
328
  }
329
 
 
 
 
 
 
 
330
  add_action('wpcf7_form_hidden_fields', 'wpcf7cf_form_hidden_fields',10,1);
331
 
332
  function wpcf7cf_form_hidden_fields($hidden_fields) {
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.4.3
8
  Author URI: http://bdwm.be/
9
  */
10
 
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.5
8
  Author URI: http://bdwm.be/
9
  */
10
 
init.php CHANGED
@@ -1,6 +1,6 @@
1
  <?php
2
 
3
- if (!defined('WPCF7CF_VERSION')) define( 'WPCF7CF_VERSION', '1.4.3' );
4
  if (!defined('WPCF7CF_REQUIRED_WP_VERSION')) define( 'WPCF7CF_REQUIRED_WP_VERSION', '4.1' );
5
  if (!defined('WPCF7CF_PLUGIN')) define( 'WPCF7CF_PLUGIN', __FILE__ );
6
  if (!defined('WPCF7CF_PLUGIN_BASENAME')) define( 'WPCF7CF_PLUGIN_BASENAME', plugin_basename( WPCF7CF_PLUGIN ) );
1
  <?php
2
 
3
+ if (!defined('WPCF7CF_VERSION')) define( 'WPCF7CF_VERSION', '1.5' );
4
  if (!defined('WPCF7CF_REQUIRED_WP_VERSION')) define( 'WPCF7CF_REQUIRED_WP_VERSION', '4.1' );
5
  if (!defined('WPCF7CF_PLUGIN')) define( 'WPCF7CF_PLUGIN', __FILE__ );
6
  if (!defined('WPCF7CF_PLUGIN_BASENAME')) define( 'WPCF7CF_PLUGIN_BASENAME', plugin_basename( WPCF7CF_PLUGIN ) );
js/scripts.js CHANGED
@@ -1,152 +1,123 @@
1
  var cf7signature_resized = 0; // for compatibility with contact-form-7-signature-addon
2
 
3
- (function($) {
4
-
5
- var i=0;
6
- var options = [];
7
 
8
- var show_animation = { "height": "show", "marginTop": "show", "marginBottom": "show", "paddingTop": "show", "paddingBottom": "show" };
9
- var hide_animation = { "height": "hide", "marginTop": "hide", "marginBottom": "hide", "paddingTop": "hide", "paddingBottom": "hide" };
10
 
11
- $('.wpcf7').each(function(){
 
12
 
13
- // Bug fix thanks to 972 creative (@toddedelman) https://wordpress.org/support/topic/conditional-fields-not-opening-using-radio-buttons/#post-10442923
14
 
15
- var $this = $(this);
16
- var options_element = $this.find('input[name="_wpcf7cf_options"]').eq(0);
17
  if (options_element.length) {
18
  var value = options_element.val();
19
  if (value) {
20
- form_options = JSON.parse(value);
21
- form_options.unit_tag = $this.attr('id');
22
- options.push(form_options);
23
- }
24
- }
25
- });
26
-
27
- $(document).ready(function() {
28
-
29
- //wpcf7pro-togglebutton
30
-
31
- $('.wpcf7cf-togglebutton').click(function() {
32
- $this = $(this);
33
- if ($this.text() == $this.data('val-1')) {
34
- $this.text($this.data('val-2'));
35
- $this.val($this.data('val-2'));
36
- } else {
37
- $this.text($this.data('val-1'));
38
- $this.val($this.data('val-1'));
39
- }
40
- });
41
 
 
 
42
 
43
- function display_fields(unit_tag, wpcf7cf_conditions, wpcf7cf_settings) {
 
 
44
 
45
- $current_form = $('#'+unit_tag);
46
- $groups = $("[data-class='wpcf7cf_group']",$current_form);
47
 
48
- //for compatibility with contact-form-7-signature-addon
49
- if (cf7signature_resized == 0 && typeof signatures !== 'undefined' && signatures.constructor === Array && signatures.length > 0 ) {
50
- for (var i = 0; i < signatures.length; i++) {
51
- if (signatures[i].canvas.width == 0) {
 
52
 
53
- jQuery(".wpcf7-form-control-signature-body>canvas").eq(i).attr('width', jQuery(".wpcf7-form-control-signature-wrap").width());
54
- jQuery(".wpcf7-form-control-signature-body>canvas").eq(i).attr('height', jQuery(".wpcf7-form-control-signature-wrap").height());
 
 
55
 
56
- cf7signature_resized = 1;
57
- }
58
- }
59
  }
 
60
 
61
- $groups.addClass('wpcf7cf-hidden');
 
 
62
 
63
- for (var i=0; i < wpcf7cf_conditions.length; i++) {
64
 
65
- var condition = wpcf7cf_conditions[i];
 
66
 
67
- // compatibility with conditional forms created with older versions of the plugin ( < 1.4 )
68
- if (!('and_rules' in condition)) {
69
- condition.and_rules = [{'if_field':condition.if_field,'if_value':condition.if_value,'operator':condition.operator}];
70
- }
71
 
72
- var show_group = should_group_be_shown(condition, $current_form);
 
73
 
74
- if (show_group) {
75
- $('[data-id='+condition.then_field+']',$current_form).eq(0).removeClass('wpcf7cf-hidden');
76
  }
77
  }
 
78
 
79
- var animation_intime = parseInt(wpcf7cf_settings.animation_intime);
80
- var animation_outtime = parseInt(wpcf7cf_settings.animation_outtime);
81
-
82
- if (wpcf7cf_settings.animation === 'no') {
83
- animation_intime = 0;
84
- animation_outtime = 0;
85
- }
86
-
87
- $groups.each(function (index) {
88
- $group = $(this);
89
- if ($group.is(':animated')) $group.finish(); // stop any current animations on the group
90
-
91
- if ($group.css('display') === 'none' && !$group.hasClass('wpcf7cf-hidden')) { // show the group
92
-
93
- if ($group.prop('tagName') === 'SPAN') {
94
- $group.show().trigger('wpcf7cf_show_group'); // Never animate inline groups because it tends to look messy.
95
- } else {
96
- $group.animate(show_animation, animation_intime).trigger('wpcf7cf_show_group'); // show
97
- }
98
-
99
- } else if ($group.css('display') !== 'none' && $group.hasClass('wpcf7cf-hidden')) { // hide the group
100
 
 
101
 
102
- if ($group.attr('data-clear_on_hide') !== undefined) {
103
- $inputs = $(':input', $group).not(':button, :submit, :reset, :hidden');
104
- $inputs.prop('checked', false).prop('selected', false).prop('selectedIndex', 0);
105
- $inputs.not('[type=checkbox],[type=radio],select').val('');
106
- $inputs.change();
107
- //display_fields();
108
- }
109
 
110
- if ($group.prop('tagName') === 'SPAN') {
111
- $group.hide().trigger('wpcf7cf_hide_group'); // Never animate inline groups because it tends to look messy.
112
- } else {
113
- $group.animate(hide_animation, animation_outtime).trigger('wpcf7cf_hide_group');
114
- }
115
 
116
- }
117
- });
118
 
119
- wpcf7cf_update_hidden_fields($current_form);
 
 
120
  }
121
 
122
- var timeout;
123
-
124
- for (var i = 0; i<options.length; i++) {
125
 
126
- var unit_tag = options[i]['unit_tag'];
127
- var conditions = options[i]['conditions'];
128
- var settings = options[i]['settings'];
129
-
130
- display_fields(unit_tag, conditions, settings);
131
-
132
- $('#'+unit_tag+' input, #'+unit_tag+' select, #'+unit_tag+' textarea, #'+unit_tag+' button').on('input paste change click',{unit_tag:unit_tag, conditions:conditions, settings:settings}, function(e) {
133
- clearTimeout(timeout);
134
- timeout = setTimeout(display_fields, 100, e.data.unit_tag, e.data.conditions, e.data.settings);
135
- });
136
-
137
- // bring form in initial state if
138
- $('#'+unit_tag+' form').on('reset', {unit_tag:unit_tag, conditions:conditions, settings:settings}, function(e) {
139
- setTimeout(display_fields, 200, e.data.unit_tag, e.data.conditions, e.data.settings);
140
- });
141
  }
142
 
143
- // Also add hidden fields in case a form gets submitted without any input:
144
- $('form.wpcf7-form').each(function(){
145
- wpcf7cf_update_hidden_fields($(this));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  });
147
- });
148
 
149
- function wpcf7cf_update_hidden_fields($form) {
 
 
 
 
150
 
151
  $hidden_group_fields = $form.find('[name="_wpcf7cf_hidden_group_fields"]');
152
  $hidden_groups = $form.find('[name="_wpcf7cf_hidden_groups"]');
@@ -174,32 +145,10 @@ var cf7signature_resized = 0; // for compatibility with contact-form-7-signature
174
  $visible_groups.val(JSON.stringify(visible_groups));
175
 
176
  return true;
177
- }
178
-
179
- //reset the form completely
180
- $( document ).ajaxComplete(function(e,xhr) {
181
- if( typeof xhr.responseJSON !== 'undefined' &&
182
- typeof xhr.responseJSON.mailSent !== 'undefined' &&
183
- typeof xhr.responseJSON.into !== 'undefined' &&
184
- xhr.responseJSON.mailSent === true)
185
- {
186
- $( xhr.responseJSON.into + ' input, '+xhr.responseJSON.into+' select, ' + xhr.responseJSON.into + ' textarea, ' + xhr.responseJSON.into + ' button' ).change();
187
- }
188
- });
189
-
190
- // fix for exclusive checkboxes in IE (this will call the change-event again after all other checkboxes are unchecked, triggering the display_fields() function)
191
- var old_wpcf7ExclusiveCheckbox = $.fn.wpcf7ExclusiveCheckbox;
192
- $.fn.wpcf7ExclusiveCheckbox = function() {
193
- return this.find('input:checkbox').click(function() {
194
- var name = $(this).attr('name');
195
- $(this).closest('form').find('input:checkbox[name="' + name + '"]').not(this).prop('checked', false).eq(0).change();
196
- });
197
- };
198
-
199
 
200
-
201
-
202
- function should_group_be_shown(condition, $current_form) {
203
 
204
  var show_group = true;
205
 
@@ -227,22 +176,22 @@ var cf7signature_resized = 0; // for compatibility with contact-form-7-signature
227
 
228
  $field.find('option:selected').each(function () {
229
  var $option = $(this);
 
230
  if (
231
- condition_and_rule.operator == 'equals' && $option.val() == condition_and_rule.if_value ||
232
  condition_and_rule.operator == 'equals (regex)' && regex_patt.test($option.val())
233
  ) {
234
  condition_ok = true;
235
  } else if (
236
- condition_and_rule.operator == 'not equals' && $option.val() == condition_and_rule.if_value ||
237
  condition_and_rule.operator == 'not equals (regex)' && !regex_patt.test($option.val())
238
  ) {
239
  condition_ok = false;
 
240
  }
241
  });
242
 
243
  show_group = show_group && condition_ok;
244
-
245
- return show_group;
246
  }
247
 
248
  if ($field.attr('type') == 'checkbox') {
@@ -321,9 +270,25 @@ var cf7signature_resized = 0; // for compatibility with contact-form-7-signature
321
 
322
  return show_group;
323
 
324
- }
 
 
325
 
326
 
327
- //removed pro functions
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328
 
329
  })( jQuery );
1
  var cf7signature_resized = 0; // for compatibility with contact-form-7-signature-addon
2
 
3
+ var i=0;
4
+ var timeout;
 
 
5
 
6
+ var show_animation = { "height": "show", "marginTop": "show", "marginBottom": "show", "paddingTop": "show", "paddingBottom": "show" };
7
+ var hide_animation = { "height": "hide", "marginTop": "hide", "marginBottom": "hide", "paddingTop": "hide", "paddingBottom": "hide" };
8
 
9
+ var wpcf7cf = {
10
+ initForm : function($form) {
11
 
12
+ var $ = jQuery;
13
 
14
+ var options_element = $form.find('input[name="_wpcf7cf_options"]').eq(0);
 
15
  if (options_element.length) {
16
  var value = options_element.val();
17
  if (value) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
+ form_options = JSON.parse(value);
20
+ form_options.unit_tag = $form.closest('.wpcf7').attr('id');
21
 
22
+ var unit_tag = form_options['unit_tag'];
23
+ var conditions = form_options['conditions'];
24
+ var settings = form_options['settings'];
25
 
26
+ wpcf7cf.display_fields(unit_tag, conditions, settings);
 
27
 
28
+ // monitor input changes, and call display_fields() is something has changed
29
+ $('#'+unit_tag+' input, #'+unit_tag+' select, #'+unit_tag+' textarea, #'+unit_tag+' button').on('input paste change click',{unit_tag:unit_tag, conditions:conditions, settings:settings}, function(e) {
30
+ clearTimeout(timeout);
31
+ timeout = setTimeout(wpcf7cf.display_fields, 100, e.data.unit_tag, e.data.conditions, e.data.settings);
32
+ });
33
 
34
+ // bring form in initial state if the reset event is fired on it.
35
+ $('#'+unit_tag+' form').on('reset', {unit_tag:unit_tag, conditions:conditions, settings:settings}, function(e) {
36
+ setTimeout(wpcf7cf.display_fields, 200, e.data.unit_tag, e.data.conditions, e.data.settings);
37
+ });
38
 
 
 
 
39
  }
40
+ }
41
 
42
+ //removed pro functions
43
+ },
44
+ display_fields : function(unit_tag, wpcf7cf_conditions, wpcf7cf_settings) {
45
 
46
+ var $ = jQuery;
47
 
48
+ $current_form = $('#'+unit_tag);
49
+ $groups = $("[data-class='wpcf7cf_group']",$current_form);
50
 
51
+ //for compatibility with contact-form-7-signature-addon
52
+ if (cf7signature_resized == 0 && typeof signatures !== 'undefined' && signatures.constructor === Array && signatures.length > 0 ) {
53
+ for (var i = 0; i < signatures.length; i++) {
54
+ if (signatures[i].canvas.width == 0) {
55
 
56
+ jQuery(".wpcf7-form-control-signature-body>canvas").eq(i).attr('width', jQuery(".wpcf7-form-control-signature-wrap").width());
57
+ jQuery(".wpcf7-form-control-signature-body>canvas").eq(i).attr('height', jQuery(".wpcf7-form-control-signature-wrap").height());
58
 
59
+ cf7signature_resized = 1;
 
60
  }
61
  }
62
+ }
63
 
64
+ $groups.addClass('wpcf7cf-hidden');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
+ for (var i=0; i < wpcf7cf_conditions.length; i++) {
67
 
68
+ var condition = wpcf7cf_conditions[i];
 
 
 
 
 
 
69
 
70
+ // compatibility with conditional forms created with older versions of the plugin ( < 1.4 )
71
+ if (!('and_rules' in condition)) {
72
+ condition.and_rules = [{'if_field':condition.if_field,'if_value':condition.if_value,'operator':condition.operator}];
73
+ }
 
74
 
75
+ var show_group = wpcf7cf.should_group_be_shown(condition, $current_form);
 
76
 
77
+ if (show_group) {
78
+ $('[data-id='+condition.then_field+']',$current_form).eq(0).removeClass('wpcf7cf-hidden');
79
+ }
80
  }
81
 
82
+ var animation_intime = parseInt(wpcf7cf_settings.animation_intime);
83
+ var animation_outtime = parseInt(wpcf7cf_settings.animation_outtime);
 
84
 
85
+ if (wpcf7cf_settings.animation === 'no') {
86
+ animation_intime = 0;
87
+ animation_outtime = 0;
 
 
 
 
 
 
 
 
 
 
 
 
88
  }
89
 
90
+ $groups.each(function (index) {
91
+ $group = $(this);
92
+ if ($group.is(':animated')) $group.finish(); // stop any current animations on the group
93
+ if ($group.css('display') === 'none' && !$group.hasClass('wpcf7cf-hidden')) {
94
+ if ($group.prop('tagName') === 'SPAN') {
95
+ $group.show().trigger('wpcf7cf_show_group');
96
+ } else {
97
+ $group.animate(show_animation, animation_intime).trigger('wpcf7cf_show_group'); // show
98
+ }
99
+ } else if ($group.css('display') !== 'none' && $group.hasClass('wpcf7cf-hidden')) {
100
+ if ($group.prop('tagName') === 'SPAN') {
101
+ $group.hide().trigger('wpcf7cf_hide_group');
102
+ } else {
103
+ $group.animate(hide_animation, animation_outtime).trigger('wpcf7cf_hide_group'); // hide
104
+ }
105
+
106
+ if ($group.attr('data-clear_on_hide') !== undefined) {
107
+ $inputs = $(':input', $group).not(':button, :submit, :reset, :hidden');
108
+ $inputs.prop('checked', false).prop('selected', false).prop('selectedIndex', 0);
109
+ $inputs.not('[type=checkbox],[type=radio],select').val('');
110
+ $inputs.change();
111
+ //display_fields();
112
+ }
113
+ }
114
  });
 
115
 
116
+ wpcf7cf.wpcf7cf_update_hidden_fields($current_form);
117
+ },
118
+ wpcf7cf_update_hidden_fields : function($form) {
119
+
120
+ var $ = jQuery;
121
 
122
  $hidden_group_fields = $form.find('[name="_wpcf7cf_hidden_group_fields"]');
123
  $hidden_groups = $form.find('[name="_wpcf7cf_hidden_groups"]');
145
  $visible_groups.val(JSON.stringify(visible_groups));
146
 
147
  return true;
148
+ },
149
+ should_group_be_shown : function(condition, $current_form) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
 
151
+ var $ = jQuery;
 
 
152
 
153
  var show_group = true;
154
 
176
 
177
  $field.find('option:selected').each(function () {
178
  var $option = $(this);
179
+ option_val = $option.val()
180
  if (
181
+ condition_and_rule.operator == 'equals' && option_val == condition_and_rule.if_value ||
182
  condition_and_rule.operator == 'equals (regex)' && regex_patt.test($option.val())
183
  ) {
184
  condition_ok = true;
185
  } else if (
186
+ condition_and_rule.operator == 'not equals' && option_val == condition_and_rule.if_value ||
187
  condition_and_rule.operator == 'not equals (regex)' && !regex_patt.test($option.val())
188
  ) {
189
  condition_ok = false;
190
+ return false; // break out of the loop
191
  }
192
  });
193
 
194
  show_group = show_group && condition_ok;
 
 
195
  }
196
 
197
  if ($field.attr('type') == 'checkbox') {
270
 
271
  return show_group;
272
 
273
+ },
274
+ //removed pro functions
275
+ };
276
 
277
 
278
+
279
+ (function($) {
280
+
281
+ $('.wpcf7-form').each(function(){
282
+ wpcf7cf.initForm($(this));
283
+ });
284
+
285
+ // fix for exclusive checkboxes in IE (this will call the change-event again after all other checkboxes are unchecked, triggering the display_fields() function)
286
+ var old_wpcf7ExclusiveCheckbox = $.fn.wpcf7ExclusiveCheckbox;
287
+ $.fn.wpcf7ExclusiveCheckbox = function() {
288
+ return this.find('input:checkbox').click(function() {
289
+ var name = $(this).attr('name');
290
+ $(this).closest('form').find('input:checkbox[name="' + name + '"]').not(this).prop('checked', false).eq(0).change();
291
+ });
292
+ };
293
 
294
  })( jQuery );
js/scripts_admin.js CHANGED
@@ -310,28 +310,33 @@ if ($wpcf7cf_new_entry.length > 0) {
310
  });
311
  }
312
 
313
- // ------------------------------------
314
- // OPTIONS PAGE
315
- // ------------------------------------
316
 
317
- $(document).ready(function() {
318
 
319
- $('.wpcf7cf-options-notice .notice-dismiss-2').click(function () {
320
- $('.wpcf7cf-options-notice .notice-dismiss').click();
321
- });
322
- $('.wpcf7cf-options-notice .notice-dismiss').click(function () {
323
- wpcf7cf_dismiss_notice();
324
- });
325
 
326
- function wpcf7cf_dismiss_notice() {
327
- $('input[name="wpcf7cf_options[notice_dismissed]"]').val('true');
328
- $.post(ajaxurl, {action:'wpcf7cf_dismiss_notice'}, function(response) {
329
- // nothing to do. dismiss_notice option should be set to TRUE server side by now.
330
- });
331
- }
332
 
 
 
 
 
 
333
  });
334
 
335
- })( jQuery );
 
 
 
 
 
 
 
 
336
 
337
- }
 
310
  });
311
  }
312
 
313
+ })( jQuery );
 
 
314
 
315
+ }
316
 
317
+ (function($) {
318
+ // ------------------------------------
319
+ // OPTIONS PAGE
320
+ // ------------------------------------
 
 
321
 
322
+ $(document).ready(function() {
 
 
 
 
 
323
 
324
+ $('.wpcf7cf-options-notice .notice-dismiss-2').click(function () {
325
+ $('.wpcf7cf-options-notice .notice-dismiss').click();
326
+ });
327
+ $('.wpcf7cf-options-notice .notice-dismiss').click(function () {
328
+ wpcf7cf_dismiss_notice();
329
  });
330
 
331
+ function wpcf7cf_dismiss_notice() {
332
+ console.log(ajaxurl);
333
+
334
+ $('input[name="wpcf7cf_options[notice_dismissed]"]').val('true');
335
+
336
+ $.post(ajaxurl, {action:'wpcf7cf_dismiss_notice'}, function(response) {
337
+ // nothing to do. dismiss_notice option should be set to TRUE server side by now.
338
+ });
339
+ }
340
 
341
+ });
342
+ })( 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: 5.1.1
9
- Stable tag: 1.4.3
10
  Requires PHP: 5.3
11
  License: GPLv2 or later
12
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
@@ -102,6 +102,17 @@ The conditional fields javascript code is loaded during wp_footer, so a call to
102
 
103
  == Changelog ==
104
 
 
 
 
 
 
 
 
 
 
 
 
105
  = 1.4.3 (04-12-19) =
106
  * Really fix clear_on_hide problem (https://wordpress.org/support/topic/clear_on_hide-still-not-working-right-after-1-4-2-update/)
107
 
6
  Tags: wordpress, contact form 7, forms, conditional fields
7
  Requires at least: 4.1
8
  Tested up to: 5.1.1
9
+ Stable tag: 1.5
10
  Requires PHP: 5.3
11
  License: GPLv2 or later
12
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
102
 
103
  == Changelog ==
104
 
105
+ = 1.5 (04-21-19) =
106
+ * Make it possible to load forms with AJAX https://github.com/pwkip/contact-form-7-conditional-fields/issues/25 and https://conditional-fields-cf7.bdwm.be/docs/faq/how-to-initialize-the-conditional-logic-after-an-ajax-call/
107
+ * Massive code reorganization in scripts.js
108
+ * Fixed bug that could appear after removing an AND condition.
109
+ * solve WPCF7_ADMIN_READ_WRITE_CAPABILITY - https://github.com/pwkip/contact-form-7-conditional-fields/pull/16
110
+ * disable part of the faulty remove_hidden_post_data function. - https://github.com/pwkip/contact-form-7-conditional-fields/pull/17
111
+ * Fix "Dismiss notice" on Conditional Fields for Contact Form 7 Settings page
112
+ * use the "wpcf7_before_send_mail" hook instead of "wpcf7_mail_components" to hide mail groups. The wpcf7_before_send_mail hook is called earlier, so it allows to also hide groups in the attachment field and in messages.
113
+ * Allow conditional group tags in success and error messages. https://github.com/pwkip/contact-form-7-conditional-fields/issues/23
114
+ * duplicating a form will also duplicate conditions https://github.com/pwkip/contact-form-7-conditional-fields/issues/28
115
+
116
  = 1.4.3 (04-12-19) =
117
  * Really fix clear_on_hide problem (https://wordpress.org/support/topic/clear_on_hide-still-not-working-right-after-1-4-2-update/)
118