Conditional Fields for Contact Form 7 - Version 0.1.4

Version Description

  • Prevent conflicts between different forms on one page.
  • Prevent conflicts between multiple instances of the same form on one page. (https://wordpress.org/support/topic/bug-153)
  • Changed regex to convert [group] tags to <div> tags, as it was posing some conflicts with other plugins (https://wordpress.org/support/topic/plugin-influence-cf7-send-button-style)
Download this release

Release Info

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

Code changes from version 0.1.3 to 0.1.4

Files changed (4) hide show
  1. admin.php +3 -0
  2. contact-form-7-conditional-fields.php +24 -10
  3. js/scripts.js +65 -41
  4. readme.txt +8 -1
admin.php CHANGED
@@ -66,6 +66,9 @@ function wpcf7cf_editor_panel_conditional($form) {
66
  $form_id = $_GET['post'];
67
  $wpcf7cf_entries = get_post_meta($form_id,'wpcf7cf_options',true);
68
 
 
 
 
69
  ?>
70
  <h3><?php echo esc_html( __( 'Conditional fields', 'wpcf7cf' ) ); ?></h3>
71
 
66
  $form_id = $_GET['post'];
67
  $wpcf7cf_entries = get_post_meta($form_id,'wpcf7cf_options',true);
68
 
69
+ if (!is_array($wpcf7cf_entries)) $wpcf7cf_entries = array();
70
+
71
+
72
  ?>
73
  <h3><?php echo esc_html( __( 'Conditional fields', 'wpcf7cf' ) ); ?></h3>
74
 
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: 0.1.3
8
  Author URI: http://bdwm.be/
9
  */
10
 
@@ -26,7 +26,7 @@ Author URI: http://bdwm.be/
26
  ?>
27
  <?php
28
 
29
- define( 'WPCF7CF_VERSION', '0.1.3' );
30
  define( 'WPCF7CF_REQUIRED_WP_VERSION', '4.1' );
31
  define( 'WPCF7CF_PLUGIN', __FILE__ );
32
  define( 'WPCF7CF_PLUGIN_BASENAME', plugin_basename( WPCF7CF_PLUGIN ) );
@@ -154,26 +154,40 @@ function wpcf7cf_properties($properties, $wpcf7form) {
154
  $form = $properties['form'];
155
 
156
  $find = array(
157
- '/\[group\s(.*?)\s.*?class:([^\s:\]]*).*?\]/s', // match the class
158
- // remove all other junk
159
- '/\[group\s([^\s\]]*)\s?(.*?)\]/s'
 
160
  );
161
 
162
  $replace = array(
163
- '[group $1 class="$2"]',
164
- '<div id="$1" data-class="wpcf7cf_group" $2>'
 
165
  );
166
 
167
  $form = preg_replace( $find, $replace, $form );
168
- $form = preg_replace( '/\[\/group\]/s', '</div>', $form );
169
 
170
  $properties['form'] = $form;
171
  }
172
  return $properties;
173
  }
174
 
 
 
175
  add_action('wpcf7_contact_form', 'wpcf7cf_enqueue_scripts', 10, 1);
176
  function wpcf7cf_enqueue_scripts($cf7form) {
177
- wp_enqueue_script('cf7cf-scripts', plugins_url('js/scripts.js', __FILE__), array('jquery'), '', true);
178
- wp_localize_script('cf7cf-scripts', 'wpcf7cf_options', get_post_meta($cf7form->id,'wpcf7cf_options',true));
 
 
 
 
 
 
 
 
 
 
 
179
  }
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: 0.1.4
8
  Author URI: http://bdwm.be/
9
  */
10
 
26
  ?>
27
  <?php
28
 
29
+ define( 'WPCF7CF_VERSION', '0.1.4' );
30
  define( 'WPCF7CF_REQUIRED_WP_VERSION', '4.1' );
31
  define( 'WPCF7CF_PLUGIN', __FILE__ );
32
  define( 'WPCF7CF_PLUGIN_BASENAME', plugin_basename( WPCF7CF_PLUGIN ) );
154
  $form = $properties['form'];
155
 
156
  $find = array(
157
+ '/\[group\s*\]/s', // matches [group ] or [group]
158
+ '/\[group\s+([^\s\]]*)\s*([^\]]*)\]/s', // matches [group something some:thing] or [group something som ]
159
+ // doesn't match [group-special something]
160
+ '/\[\/group\]/s'
161
  );
162
 
163
  $replace = array(
164
+ '<div data-class="wpcf7cf_group">',
165
+ '<div id="$1" data-class="wpcf7cf_group">',
166
+ '</div>'
167
  );
168
 
169
  $form = preg_replace( $find, $replace, $form );
 
170
 
171
  $properties['form'] = $form;
172
  }
173
  return $properties;
174
  }
175
 
176
+ $global_count = 0;
177
+
178
  add_action('wpcf7_contact_form', 'wpcf7cf_enqueue_scripts', 10, 1);
179
  function wpcf7cf_enqueue_scripts($cf7form) {
180
+ global $global_count, $post;
181
+ $global_count++;
182
+
183
+ $unit_tag = 'wpcf7-f'.$cf7form->id.'-p'.$post->ID.'-o'.$global_count;
184
+
185
+ $options = array(
186
+ 'form_id' => $cf7form->id,
187
+ 'unit_tag' => $unit_tag,
188
+ 'conditions' => get_post_meta($cf7form->id,'wpcf7cf_options', true),
189
+ );
190
+
191
+ wp_enqueue_script('cf7cf-scripts', plugins_url('js/scripts.js', __FILE__), array('jquery'), WPCF7CF_VERSION, true);
192
+ wp_localize_script('cf7cf-scripts', 'wpcf7cf_options_'.$global_count, $options);
193
  }
js/scripts.js CHANGED
@@ -1,61 +1,85 @@
1
  (function($) {
2
- if (typeof wpcf7cf_options == 'undefined') return; // return if there is no form on the page
3
  //console.log(wpcf7cf_options);
4
-
5
- $(document).ready(function() {
6
- function display_fields() {
7
- $('[data-class=wpcf7cf_group]').hide();
8
- for (var i=0; i < wpcf7cf_options.length; i++) {
9
 
10
- var condition = wpcf7cf_options[i];
11
- if (condition.then_visibility == 'hide') continue;
 
 
 
 
 
 
 
 
12
 
13
- $field = $('[name='+condition.if_field+']').length ? $('[name='+condition.if_field+']') : $('[name='+condition.if_field+'\\[\\]]');
 
14
 
15
- if ($field.length == 1) {
 
16
 
17
- // single field (tested with text field, single checkbox, select with single value (dropdown), select with multiple values)
 
18
 
19
- if ($field.is('select')) {
20
- $field.find('option:selected').each(function() {
21
- var $option = $(this);
22
- if (condition.operator == 'equals' && $option.val() == condition.if_value) {
23
- $('#'+condition.then_field).show();
24
- }
25
- });
26
- continue;
27
- }
28
 
29
- if (condition.operator == 'equals' && $field.val() == condition.if_value || condition.operator == 'not equals' && $field.val() != condition.if_value) {
30
- if ($field.attr('type') == 'checkbox' && !$field.attr('checked')) continue;
31
- $('#'+condition.then_field).show();
32
- }
33
 
 
34
 
35
- } else if ($field.length > 1) {
36
 
37
- // multiple fields (tested with checkboxes, exclusive checkboxes, dropdown with multiple values)
 
 
 
 
 
 
 
 
38
 
39
- var all_values = [];
40
- var checked_values = [];
41
- $field.each(function() {
42
- all_values.push($(this).val());
43
- if($(this).is(':checked')) {
44
- checked_values.push($(this).val());
45
  }
46
- });
47
 
48
- if (condition.operator == 'equals' && $.inArray(condition.if_value, checked_values) != -1) {
49
- $('#'+condition.then_field).show();
50
- } else if (condition.operator == 'not equals' && $.inArray(condition.if_value, all_values) != -1 && $.inArray(condition.if_value, checked_values) == -1) {
51
- $('#'+condition.then_field).show();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  }
 
53
  }
 
54
 
 
 
 
 
 
 
 
 
55
  }
56
- }
57
- display_fields();
58
- $('input, select, textarea').change(display_fields);
59
- });
60
 
61
  })( jQuery );
1
  (function($) {
2
+ //if (typeof wpcf7cf_options == 'undefined') return; // return if there is no form on the page
3
  //console.log(wpcf7cf_options);
 
 
 
 
 
4
 
5
+ var i=0;
6
+ var options = [];
7
+ while (true) {
8
+ i++;
9
+ if ('wpcf7cf_options_'+i in window) {
10
+ options.push(window['wpcf7cf_options_'+i]);
11
+ continue;
12
+ }
13
+ break;
14
+ }
15
 
16
+ $(document).ready(function() {
17
+ function display_fields(unit_tag, wpcf7cf_conditions) {
18
 
19
+ //console.log(unit_tag);
20
+ //console.log(wpcf7cf_conditions);
21
 
22
+ $("#"+unit_tag+" [data-class='wpcf7cf_group']").hide();
23
+ for (var i=0; i < wpcf7cf_conditions.length; i++) {
24
 
25
+ var condition = wpcf7cf_conditions[i];
26
+ if (condition.then_visibility == 'hide') continue;
 
 
 
 
 
 
 
27
 
28
+ $field = $('#'+unit_tag+' [name='+condition.if_field+']').length ? $('#'+unit_tag+' [name='+condition.if_field+']') : $('#'+unit_tag+' [name='+condition.if_field+'\\[\\]]');
 
 
 
29
 
30
+ if ($field.length == 1) {
31
 
32
+ // single field (tested with text field, single checkbox, select with single value (dropdown), select with multiple values)
33
 
34
+ if ($field.is('select')) {
35
+ $field.find('option:selected').each(function() {
36
+ var $option = $(this);
37
+ if (condition.operator == 'equals' && $option.val() == condition.if_value) {
38
+ $('#'+unit_tag+' #'+condition.then_field).show();
39
+ }
40
+ });
41
+ continue;
42
+ }
43
 
44
+ if (condition.operator == 'equals' && $field.val() == condition.if_value || condition.operator == 'not equals' && $field.val() != condition.if_value) {
45
+ if ($field.attr('type') == 'checkbox' && !$field.attr('checked')) continue;
46
+ $('#'+unit_tag+' #'+condition.then_field).show();
 
 
 
47
  }
 
48
 
49
+
50
+ } else if ($field.length > 1) {
51
+
52
+ // multiple fields (tested with checkboxes, exclusive checkboxes, dropdown with multiple values)
53
+
54
+ var all_values = [];
55
+ var checked_values = [];
56
+ $field.each(function() {
57
+ all_values.push($(this).val());
58
+ if($(this).is(':checked')) {
59
+ checked_values.push($(this).val());
60
+ }
61
+ });
62
+
63
+ if (condition.operator == 'equals' && $.inArray(condition.if_value, checked_values) != -1) {
64
+ $('#'+unit_tag+' #'+condition.then_field).show();
65
+ } else if (condition.operator == 'not equals' && $.inArray(condition.if_value, all_values) != -1 && $.inArray(condition.if_value, checked_values) == -1) {
66
+ $('#'+unit_tag+' #'+condition.then_field).show();
67
+ }
68
  }
69
+
70
  }
71
+ }
72
 
73
+ for (var i = 0; i<options.length; i++) {
74
+ var unit_tag = options[i]['unit_tag'];
75
+ var conditions = options[i]['conditions'];
76
+ display_fields(unit_tag, conditions);
77
+ $('#'+unit_tag+' input, #'+unit_tag+' select, #'+unit_tag+' textarea').change({unit_tag:unit_tag, conditions:conditions}, function(e) {
78
+ console.log('triggered '+e.data.unit_tag);
79
+ display_fields(e.data.unit_tag, e.data.conditions);
80
+ });
81
  }
82
+
83
+ });
 
 
84
 
85
  })( jQuery );
readme.txt CHANGED
@@ -1,11 +1,12 @@
1
  === Conditional Fields for Contact Form 7 ===
2
  Contributors: Jules Colle
 
3
  Author: Jules Colle
4
  Website: http://bdwm.be
5
  Tags: wordpress, contact form 7, forms, conditional fields
6
  Requires at least: 3.6.1
7
  Tested up to: 4.5.2
8
- Stable tag: 0.1.3
9
  License: GPLv2 or later
10
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
 
@@ -38,6 +39,12 @@ Because no questions have been asked frequently about this plugin.
38
 
39
  == Changelog ==
40
 
 
 
 
 
 
 
41
  = 0.1.3 =
42
 
43
  * Removed fielset, id and class attributes for group tags, because they weren't used anyway and broke the shortcode
1
  === Conditional Fields for Contact Form 7 ===
2
  Contributors: Jules Colle
3
+ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=j_colle%40hotmail%2ecom&lc=US&item_name=Jules%20Colle%20%2d%20WP%20plugins%20%2d%20Responsive%20Gallery%20Grid&item_number=rgg&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted
4
  Author: Jules Colle
5
  Website: http://bdwm.be
6
  Tags: wordpress, contact form 7, forms, conditional fields
7
  Requires at least: 3.6.1
8
  Tested up to: 4.5.2
9
+ Stable tag: 0.1.4
10
  License: GPLv2 or later
11
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
12
 
39
 
40
  == Changelog ==
41
 
42
+ = 0.1.4 =
43
+
44
+ * Prevent conflicts between different forms on one page.
45
+ * Prevent conflicts between multiple instances of the same form on one page. (https://wordpress.org/support/topic/bug-153)
46
+ * Changed regex to convert \[group\] tags to &lt;div&gt; tags, as it was posing some conflicts with other plugins (https://wordpress.org/support/topic/plugin-influence-cf7-send-button-style)
47
+
48
  = 0.1.3 =
49
 
50
  * Removed fielset, id and class attributes for group tags, because they weren't used anyway and broke the shortcode