Conditional Fields for Contact Form 7 - Version 0.2.1

Version Description

  • Bug fix: arrow kept spinning after submitting a form without conditional fields. (https://wordpress.org/support/topic/version-0-2-gives-a-continues-spinning-arrow-after-submitting/)
  • Removed anonymous functions from code, so the plugin also works for PHP versions older than 5.3.
  • Suppress errors generated if user uses invalid HTML markup in their form code. These errors could prevent form success message from appearing.
Download this release

Release Info

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

Code changes from version 0.2 to 0.2.1

Files changed (2) hide show
  1. contact-form-7-conditional-fields.php +38 -34
  2. readme.txt +6 -1
contact-form-7-conditional-fields.php CHANGED
@@ -1,12 +1,13 @@
1
  <?php
 
2
  /**
3
  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.2
8
  Author URI: http://bdwm.be/
9
- */
10
 
11
  /**
12
  * This program is free software; you can redistribute it and/or modify
@@ -26,7 +27,7 @@ Author URI: http://bdwm.be/
26
  ?>
27
  <?php
28
 
29
- define( 'WPCF7CF_VERSION', '0.2' );
30
  define( 'WPCF7CF_REQUIRED_WP_VERSION', '4.1' );
31
  define( 'WPCF7CF_PLUGIN', __FILE__ );
32
  define( 'WPCF7CF_PLUGIN_BASENAME', plugin_basename( WPCF7CF_PLUGIN ) );
@@ -225,6 +226,7 @@ class ContactForm7ConditionalFields {
225
 
226
  //Find out which tags are in which groups
227
  $dom = new DOMDocument();
 
228
  $dom->loadHTML($form_properties['form']);
229
  $divs = $dom->getElementsByTagName('div');
230
  $groups = array();
@@ -282,19 +284,21 @@ class ContactForm7ConditionalFields {
282
  }
283
  $this->visible_groups = array();
284
  $conditions = get_post_meta($form_id,'wpcf7cf_options', true);
285
- foreach( $conditions as $condition ) {
286
- if ( $condition['then_visibility'] == 'show' ) {
287
- if ( is_array($posted_data[ $condition['if_field'] ]) ) {
288
- if ( 'not equals' == $condition['operator'] && ! in_array( $condition['if_value'], $posted_data[ $condition['if_field'] ] ) ) {
289
- $this->visible_groups[] = $condition['then_field'];
290
- } elseif ( 'equals' == $condition['operator'] && in_array( $condition['if_value'], $posted_data[ $condition['if_field'] ] ) ) {
291
- $this->visible_groups[] = $condition['then_field'];
292
- }
293
- } else {
294
- if ( 'not equals' == $condition['operator'] && $condition['if_value'] != $posted_data[ $condition['if_field'] ] ) {
295
- $this->visible_groups[] = $condition['then_field'];
296
- } elseif ( 'equals' == $condition['operator'] && $condition['if_value'] == $posted_data[ $condition['if_field'] ] ) {
297
- $this->visible_groups[] = $condition['then_field'];
 
 
298
  }
299
  }
300
  }
@@ -306,25 +310,25 @@ class ContactForm7ConditionalFields {
306
  $regex = '@\[[\t ]*([a-zA-Z_][0-9a-zA-Z:._-]*)[\t ]*\](.*?)\[[\t ]*/[\t ]*\1[\t ]*\]@s';
307
  // [1] = name [2] = contents
308
 
309
- $components['body'] = preg_replace_callback(
310
- $regex,
311
- function ( $matches ) {
312
- $name = $matches[1];
313
- $content = $matches[2];
314
- if ( in_array( $name, $this->hidden_groups ) ) {
315
- // The tag name represents a hidden group, so replace everything from [tagname] to [/tagname] with nothing
316
- return '';
317
- } elseif ( in_array( $name, $this->visible_groups ) ) {
318
- // The tag name represents a visible group, so remove the tags themselves, but return everything else
319
- return $content;
320
- } else {
321
- // The tag name doesn't represent a group that was used in the form. Leave it alone (return the entire match).
322
- return $matches[0];
323
- }
324
- },
325
- $components['body'] );
326
  return $components;
327
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328
  function hide_hidden_mail_fields_callback( $matches ) {
329
  $name = $matches[1];
330
  $content = $matches[2];
@@ -346,7 +350,7 @@ new ContactForm7ConditionalFields;
346
  add_filter( 'wpcf7_contact_form_properties', 'wpcf7cf_properties', 10, 2 );
347
 
348
  function wpcf7cf_properties($properties, $wpcf7form) {
349
- if (!is_admin()) { // TODO: kind of hacky. maybe find a better solution. Needed because otherwise the group tags will be replaced in the editor aswell.
350
  $form = $properties['form'];
351
 
352
  $find = array(
1
  <?php
2
+
3
  /**
4
  Plugin Name: Contact Form 7 Conditional Fields
5
  Plugin URI: http://bdwm.be/
6
  Description: Adds support for conditional fields to Contact Form 7. This plugin depends on Contact Form 7.
7
  Author: Jules Colle
8
+ Version: 0.2.1
9
  Author URI: http://bdwm.be/
10
+ */
11
 
12
  /**
13
  * This program is free software; you can redistribute it and/or modify
27
  ?>
28
  <?php
29
 
30
+ define( 'WPCF7CF_VERSION', '0.2.1' );
31
  define( 'WPCF7CF_REQUIRED_WP_VERSION', '4.1' );
32
  define( 'WPCF7CF_PLUGIN', __FILE__ );
33
  define( 'WPCF7CF_PLUGIN_BASENAME', plugin_basename( WPCF7CF_PLUGIN ) );
226
 
227
  //Find out which tags are in which groups
228
  $dom = new DOMDocument();
229
+ libxml_use_internal_errors(true); //suppress warnings if invalid HTML markup. (We are not doing anything else with this for now)
230
  $dom->loadHTML($form_properties['form']);
231
  $divs = $dom->getElementsByTagName('div');
232
  $groups = array();
284
  }
285
  $this->visible_groups = array();
286
  $conditions = get_post_meta($form_id,'wpcf7cf_options', true);
287
+ if (is_array($conditions)) {
288
+ foreach( $conditions as $condition ) {
289
+ if ( $condition['then_visibility'] == 'show' ) {
290
+ if ( is_array($posted_data[ $condition['if_field'] ]) ) {
291
+ if ( 'not equals' == $condition['operator'] && ! in_array( $condition['if_value'], $posted_data[ $condition['if_field'] ] ) ) {
292
+ $this->visible_groups[] = $condition['then_field'];
293
+ } elseif ( 'equals' == $condition['operator'] && in_array( $condition['if_value'], $posted_data[ $condition['if_field'] ] ) ) {
294
+ $this->visible_groups[] = $condition['then_field'];
295
+ }
296
+ } else {
297
+ if ( 'not equals' == $condition['operator'] && $condition['if_value'] != $posted_data[ $condition['if_field'] ] ) {
298
+ $this->visible_groups[] = $condition['then_field'];
299
+ } elseif ( 'equals' == $condition['operator'] && $condition['if_value'] == $posted_data[ $condition['if_field'] ] ) {
300
+ $this->visible_groups[] = $condition['then_field'];
301
+ }
302
  }
303
  }
304
  }
310
  $regex = '@\[[\t ]*([a-zA-Z_][0-9a-zA-Z:._-]*)[\t ]*\](.*?)\[[\t ]*/[\t ]*\1[\t ]*\]@s';
311
  // [1] = name [2] = contents
312
 
313
+ $components['body'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $components['body'] );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
314
  return $components;
315
  }
316
+
317
+ function hide_hidden_mail_fields_regex_callback ( $matches ) {
318
+ $name = $matches[1];
319
+ $content = $matches[2];
320
+ if ( in_array( $name, $this->hidden_groups ) ) {
321
+ // The tag name represents a hidden group, so replace everything from [tagname] to [/tagname] with nothing
322
+ return '';
323
+ } elseif ( in_array( $name, $this->visible_groups ) ) {
324
+ // The tag name represents a visible group, so remove the tags themselves, but return everything else
325
+ return $content;
326
+ } else {
327
+ // The tag name doesn't represent a group that was used in the form. Leave it alone (return the entire match).
328
+ return $matches[0];
329
+ }
330
+ }
331
+
332
  function hide_hidden_mail_fields_callback( $matches ) {
333
  $name = $matches[1];
334
  $content = $matches[2];
350
  add_filter( 'wpcf7_contact_form_properties', 'wpcf7cf_properties', 10, 2 );
351
 
352
  function wpcf7cf_properties($properties, $wpcf7form) {
353
+ 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.
354
  $form = $properties['form'];
355
 
356
  $find = array(
readme.txt CHANGED
@@ -6,7 +6,7 @@ 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.6.1
9
- Stable tag: 0.2
10
  License: GPLv2 or later
11
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
12
 
@@ -47,6 +47,11 @@ Because no questions have been asked frequently about this plugin.
47
 
48
  == Changelog ==
49
 
 
 
 
 
 
50
  = 0.2 =
51
  * Added support for required conditional fields inside hidden groups. A big thank you to @stevish for implementing this.
52
  * Added support for conditional fields in the email messages. This one also goes entirely to @stevish. Thanks man!
6
  Tags: wordpress, contact form 7, forms, conditional fields
7
  Requires at least: 3.6.1
8
  Tested up to: 4.6.1
9
+ Stable tag: 0.2.1
10
  License: GPLv2 or later
11
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
12
 
47
 
48
  == Changelog ==
49
 
50
+ = 0.2.1 =
51
+ * Bug fix: arrow kept spinning after submitting a form without conditional fields. (https://wordpress.org/support/topic/version-0-2-gives-a-continues-spinning-arrow-after-submitting/)
52
+ * Removed anonymous functions from code, so the plugin also works for PHP versions older than 5.3.
53
+ * Suppress errors generated if user uses invalid HTML markup in their form code. These errors could prevent form success message from appearing.
54
+
55
  = 0.2 =
56
  * Added support for required conditional fields inside hidden groups. A big thank you to @stevish for implementing this.
57
  * Added support for conditional fields in the email messages. This one also goes entirely to @stevish. Thanks man!