Version Description
- Added support for required conditional fields inside hidden groups. A big thank you to @stevish for implementing this.
- Added support for conditional fields in the email messages. This one also goes entirely to @stevish. Thanks man!
- Added @stevish as a contributer to the project :)
- Fix form not working in widgets or other places outside of the loop. Thanks to @ciprianolaru for the solution (https://wordpress.org/support/topic/problem-with-unit_tag-when-not-in-the-loop-form-not-used-in-post-or-page/#post-8299801)
Download this release
Release Info
Developer | Jules Colle |
Plugin | Conditional Fields for Contact Form 7 |
Version | 0.2 |
Comparing to | |
See all releases |
Code changes from version 0.1.7 to 0.2
- admin.php +202 -202
- contact-form-7-conditional-fields.php +396 -196
- js/scripts.js +13 -3
- readme.txt +92 -78
admin.php
CHANGED
@@ -1,203 +1,203 @@
|
|
1 |
-
<?php
|
2 |
-
|
3 |
-
add_action( 'admin_enqueue_scripts', 'wpcf7cf_admin_enqueue_scripts', 11 ); // set priority so scripts and styles get loaded later.
|
4 |
-
|
5 |
-
function wpcf7cf_admin_enqueue_scripts( $hook_suffix ) {
|
6 |
-
wp_enqueue_style( 'contact-form-7-cf-admin', wpcf7cf_plugin_url( 'admin-style.css' ), array(), WPCF7CF_VERSION, 'all' );
|
7 |
-
wp_enqueue_script('cf7cf-scripts-admin', wpcf7cf_plugin_url( 'js/scripts_admin.js' ),array(), WPCF7CF_VERSION,true);
|
8 |
-
}
|
9 |
-
|
10 |
-
add_filter('wpcf7_editor_panels', 'add_conditional_panel');
|
11 |
-
|
12 |
-
function add_conditional_panel($panels) {
|
13 |
-
$panels['contitional-panel'] = array(
|
14 |
-
'title' => __( 'Conditional fields', 'wpcf7cf' ),
|
15 |
-
'callback' => 'wpcf7cf_editor_panel_conditional'
|
16 |
-
);
|
17 |
-
return $panels;
|
18 |
-
}
|
19 |
-
|
20 |
-
function all_field_options($post, $selected = '-1') {
|
21 |
-
$all_fields = $post->form_scan_shortcode();
|
22 |
-
?>
|
23 |
-
<option value="-1" <?php echo $selected == '-1'?'selected':'' ?>>-- Select field --</option>
|
24 |
-
<?php
|
25 |
-
foreach ($all_fields as $tag) {
|
26 |
-
if ($tag['type'] == 'group' || $tag['name'] == '') continue;
|
27 |
-
?>
|
28 |
-
<option value="<?php echo $tag['name']; ?>" <?php echo $selected == $tag['name']?'selected':'' ?>><?php echo $tag['name']; ?></option>
|
29 |
-
<?php
|
30 |
-
}
|
31 |
-
}
|
32 |
-
|
33 |
-
function all_group_options($post, $selected = '-1') {
|
34 |
-
$all_groups = $post->form_scan_shortcode(array('type'=>'group'));
|
35 |
-
|
36 |
-
?>
|
37 |
-
<option value="-1" <?php echo $selected == '-1'?'selected':'' ?>>-- Select group --</option>
|
38 |
-
<?php
|
39 |
-
foreach ($all_groups as $tag) {
|
40 |
-
?>
|
41 |
-
<option value="<?php echo $tag['name']; ?>" <?php echo $selected == $tag['name']?'selected':'' ?>><?php echo $tag['name']; ?></option>
|
42 |
-
<?php
|
43 |
-
}
|
44 |
-
}
|
45 |
-
|
46 |
-
function all_operator_options($selected = 'equals') {
|
47 |
-
$all_options = array('equals', 'not equals');
|
48 |
-
foreach($all_options as $option) {
|
49 |
-
?>
|
50 |
-
<option value="<?php echo $option ?>" <?php echo $selected == $option?'selected':'' ?>><?php echo $option ?></option>
|
51 |
-
<?php
|
52 |
-
}
|
53 |
-
}
|
54 |
-
|
55 |
-
function all_display_options($selected = 'show') {
|
56 |
-
$all_options = array('show', 'hide');
|
57 |
-
foreach($all_options as $option) {
|
58 |
-
?>
|
59 |
-
<option value="<?php echo $option ?>" <?php echo $selected == $option?'selected':'' ?>><?php echo $option ?></option>
|
60 |
-
<?php
|
61 |
-
}
|
62 |
-
}
|
63 |
-
|
64 |
-
function wpcf7cf_editor_panel_conditional($form) {
|
65 |
-
|
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 |
-
|
75 |
-
|
76 |
-
<div id="wpcf7cf-new-entry">
|
77 |
-
if
|
78 |
-
<select name="wpcf7cf_options[{id}][if_field]" class="if-field-select"><?php all_field_options($form); ?></select>
|
79 |
-
<select name="wpcf7cf_options[{id}][operator]" class="operator"><?php all_operator_options(); ?></select>
|
80 |
-
<input name="wpcf7cf_options[{id}][if_value]" class="if-value" type="text" placeholder="value">
|
81 |
-
then
|
82 |
-
<select name="wpcf7cf_options[{id}][then_visibility]" class="then-visibility"><?php all_display_options() ?></select>
|
83 |
-
<select name="wpcf7cf_options[{id}][then_field]" class="then-field-select"><?php all_group_options($form); ?></select>
|
84 |
-
</div>
|
85 |
-
<a id="wpcf7cf-delete-button" class="delete-button" title="delete rule" href="#"><span class="dashicons dashicons-dismiss"></span> Remove rule</a>
|
86 |
-
<a id="wpcf7cf-add-button" title="add new rule" href="#"><span class="dashicons dashicons-plus-alt"></span> add new conditional rule</a>
|
87 |
-
|
88 |
-
<div id="wpcf7cf-entries">
|
89 |
-
<?php
|
90 |
-
$i = 0;
|
91 |
-
foreach($wpcf7cf_entries as $id => $entry) {
|
92 |
-
?>
|
93 |
-
<div class="entry" id="entry-<?php echo $i ?>">
|
94 |
-
if
|
95 |
-
<select name="wpcf7cf_options[<?php echo $i ?>][if_field]" class="if-field-select"><?php all_field_options($form, $entry['if_field']); ?></select>
|
96 |
-
<select name="wpcf7cf_options[<?php echo $i ?>][operator]" class="operator"><?php all_operator_options($entry['operator']) ?></select>
|
97 |
-
<input name="wpcf7cf_options[<?php echo $i ?>][if_value]" class="if-value" type="text" placeholder="value" value="<?php echo $entry['if_value'] ?>">
|
98 |
-
then
|
99 |
-
<select name="wpcf7cf_options[<?php echo $i ?>][then_visibility]" class="then-visibility"><?php all_display_options($entry['then_visibility']) ?></select>
|
100 |
-
<select name="wpcf7cf_options[<?php echo $i ?>][then_field]" class="then-field-select"><?php all_group_options($form, $entry['then_field']); ?></select>
|
101 |
-
<a style="display: inline-block;" href="#" title="delete rule" class="delete-button"><span class="dashicons dashicons-dismiss"></span> Remove rule</a>
|
102 |
-
</div>
|
103 |
-
<?php
|
104 |
-
$i++;
|
105 |
-
}
|
106 |
-
?>
|
107 |
-
</div>
|
108 |
-
|
109 |
-
|
110 |
-
<div id="wpcf7cf-text-entries">
|
111 |
-
<p><a href="#" id="wpcf7cf-settings-to-text">Export settings</a></p>
|
112 |
-
<div id="wpcf7cf-settings-text-wrap">
|
113 |
-
<textarea id="wpcf7cf-settings-text"></textarea>
|
114 |
-
<p><a href="#" id="wpcf7cf-settings-text-clear">Clear</a></p>
|
115 |
-
</div>
|
116 |
-
</div>
|
117 |
-
|
118 |
-
<script>
|
119 |
-
(function($) {
|
120 |
-
var index = $('#wpcf7cf-entries .entry').length;
|
121 |
-
|
122 |
-
$('.delete-button').click(function(){
|
123 |
-
|
124 |
-
//if (confirm('You sure?')===false) return false;
|
125 |
-
$(this).parent().remove();
|
126 |
-
return false;
|
127 |
-
|
128 |
-
});
|
129 |
-
|
130 |
-
$('#wpcf7cf-add-button').click(function(){
|
131 |
-
|
132 |
-
// if ($('#wpcf7cf-new-entry .if-field-select').val() == '-1' || $('#wpcf7cf-new-entry .then-field-select').val() == '-1') {
|
133 |
-
// alert('Please select 2 fields');
|
134 |
-
// $(this).parent().remove();
|
135 |
-
// return false;
|
136 |
-
// } else if($('#wpcf7cf-new-entry .if-field-select').val() == $('#wpcf7cf-new-entry .then-field-select').val()) {
|
137 |
-
// alert('The fields cannot be the same');
|
138 |
-
// $(this).parent().remove();
|
139 |
-
// return false;
|
140 |
-
// }
|
141 |
-
|
142 |
-
var $delete_button = $('#wpcf7cf-delete-button').clone().removeAttr('id');
|
143 |
-
$('<div class="entry" id="entry-'+index+'">'+($('#wpcf7cf-new-entry').html().replace(/{id}/g, index))+'</div>').prependTo('#wpcf7cf-entries').append($delete_button);
|
144 |
-
$delete_button.click(function(){
|
145 |
-
|
146 |
-
//if (confirm('You sure?')===false) return false;
|
147 |
-
$(this).parent().remove();
|
148 |
-
return false;
|
149 |
-
|
150 |
-
});
|
151 |
-
index++;
|
152 |
-
return false;
|
153 |
-
|
154 |
-
});
|
155 |
-
|
156 |
-
// settings to
|
157 |
-
|
158 |
-
$('#wpcf7cf-settings-text-wrap').hide();
|
159 |
-
|
160 |
-
$('#wpcf7cf-settings-to-text').click(function() {
|
161 |
-
$('#wpcf7cf-settings-text-wrap').show();
|
162 |
-
|
163 |
-
$('#wpcf7cf-settings-text').val('');
|
164 |
-
$('#wpcf7cf-entries .entry').each(function() {
|
165 |
-
var $entry = $(this);
|
166 |
-
var line = 'if [' + $entry.find('.if-field-select').val() + ']'
|
167 |
-
+ ' ' + $entry.find('.operator').val()
|
168 |
-
+ ' "' + $entry.find('.if-value').val() + '" then '
|
169 |
-
+ $entry.find('.then-visibility').val()
|
170 |
-
+ ' [' + $entry.find('.then-field-select').val() + ']';
|
171 |
-
$('#wpcf7cf-settings-text').val($('#wpcf7cf-settings-text').val() + line + "\n" ).select();
|
172 |
-
});
|
173 |
-
return false;
|
174 |
-
});
|
175 |
-
|
176 |
-
$('#wpcf7cf-settings-text-clear').click(function() {
|
177 |
-
$('#wpcf7cf-settings-text-wrap').hide();
|
178 |
-
$('#wpcf7cf-settings-text').val('');
|
179 |
-
return false;
|
180 |
-
});
|
181 |
-
|
182 |
-
})( jQuery );
|
183 |
-
</script>
|
184 |
-
<?php
|
185 |
-
}
|
186 |
-
|
187 |
-
// define the wpcf7_save_contact_form callback
|
188 |
-
function wpcf7cf_save_contact_form( $contact_form )
|
189 |
-
{
|
190 |
-
if ( ! isset( $_POST ) || empty( $_POST ) || ! isset( $_POST['wpcf7cf_options'] ) || ! is_array( $_POST['wpcf7cf_options'] ) )
|
191 |
-
return;
|
192 |
-
$post_id = $contact_form->id();
|
193 |
-
if ( ! $post_id )
|
194 |
-
return;
|
195 |
-
|
196 |
-
unset($_POST['wpcf7cf_options']['{id}']); // remove the dummy entry
|
197 |
-
|
198 |
-
update_post_meta( $post_id, 'wpcf7cf_options', $_POST['wpcf7cf_options'] );
|
199 |
-
|
200 |
-
};
|
201 |
-
|
202 |
-
// add the action
|
203 |
add_action( 'wpcf7_save_contact_form', 'wpcf7cf_save_contact_form', 10, 1 );
|
1 |
+
<?php
|
2 |
+
|
3 |
+
add_action( 'admin_enqueue_scripts', 'wpcf7cf_admin_enqueue_scripts', 11 ); // set priority so scripts and styles get loaded later.
|
4 |
+
|
5 |
+
function wpcf7cf_admin_enqueue_scripts( $hook_suffix ) {
|
6 |
+
wp_enqueue_style( 'contact-form-7-cf-admin', wpcf7cf_plugin_url( 'admin-style.css' ), array(), WPCF7CF_VERSION, 'all' );
|
7 |
+
wp_enqueue_script('cf7cf-scripts-admin', wpcf7cf_plugin_url( 'js/scripts_admin.js' ),array(), WPCF7CF_VERSION,true);
|
8 |
+
}
|
9 |
+
|
10 |
+
add_filter('wpcf7_editor_panels', 'add_conditional_panel');
|
11 |
+
|
12 |
+
function add_conditional_panel($panels) {
|
13 |
+
$panels['contitional-panel'] = array(
|
14 |
+
'title' => __( 'Conditional fields', 'wpcf7cf' ),
|
15 |
+
'callback' => 'wpcf7cf_editor_panel_conditional'
|
16 |
+
);
|
17 |
+
return $panels;
|
18 |
+
}
|
19 |
+
|
20 |
+
function all_field_options($post, $selected = '-1') {
|
21 |
+
$all_fields = $post->form_scan_shortcode();
|
22 |
+
?>
|
23 |
+
<option value="-1" <?php echo $selected == '-1'?'selected':'' ?>>-- Select field --</option>
|
24 |
+
<?php
|
25 |
+
foreach ($all_fields as $tag) {
|
26 |
+
if ($tag['type'] == 'group' || $tag['name'] == '') continue;
|
27 |
+
?>
|
28 |
+
<option value="<?php echo $tag['name']; ?>" <?php echo $selected == $tag['name']?'selected':'' ?>><?php echo $tag['name']; ?></option>
|
29 |
+
<?php
|
30 |
+
}
|
31 |
+
}
|
32 |
+
|
33 |
+
function all_group_options($post, $selected = '-1') {
|
34 |
+
$all_groups = $post->form_scan_shortcode(array('type'=>'group'));
|
35 |
+
|
36 |
+
?>
|
37 |
+
<option value="-1" <?php echo $selected == '-1'?'selected':'' ?>>-- Select group --</option>
|
38 |
+
<?php
|
39 |
+
foreach ($all_groups as $tag) {
|
40 |
+
?>
|
41 |
+
<option value="<?php echo $tag['name']; ?>" <?php echo $selected == $tag['name']?'selected':'' ?>><?php echo $tag['name']; ?></option>
|
42 |
+
<?php
|
43 |
+
}
|
44 |
+
}
|
45 |
+
|
46 |
+
function all_operator_options($selected = 'equals') {
|
47 |
+
$all_options = array('equals', 'not equals');
|
48 |
+
foreach($all_options as $option) {
|
49 |
+
?>
|
50 |
+
<option value="<?php echo $option ?>" <?php echo $selected == $option?'selected':'' ?>><?php echo $option ?></option>
|
51 |
+
<?php
|
52 |
+
}
|
53 |
+
}
|
54 |
+
|
55 |
+
function all_display_options($selected = 'show') {
|
56 |
+
$all_options = array('show', 'hide');
|
57 |
+
foreach($all_options as $option) {
|
58 |
+
?>
|
59 |
+
<option value="<?php echo $option ?>" <?php echo $selected == $option?'selected':'' ?>><?php echo $option ?></option>
|
60 |
+
<?php
|
61 |
+
}
|
62 |
+
}
|
63 |
+
|
64 |
+
function wpcf7cf_editor_panel_conditional($form) {
|
65 |
+
|
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 |
+
|
75 |
+
|
76 |
+
<div id="wpcf7cf-new-entry">
|
77 |
+
if
|
78 |
+
<select name="wpcf7cf_options[{id}][if_field]" class="if-field-select"><?php all_field_options($form); ?></select>
|
79 |
+
<select name="wpcf7cf_options[{id}][operator]" class="operator"><?php all_operator_options(); ?></select>
|
80 |
+
<input name="wpcf7cf_options[{id}][if_value]" class="if-value" type="text" placeholder="value">
|
81 |
+
then
|
82 |
+
<select name="wpcf7cf_options[{id}][then_visibility]" class="then-visibility"><?php all_display_options() ?></select>
|
83 |
+
<select name="wpcf7cf_options[{id}][then_field]" class="then-field-select"><?php all_group_options($form); ?></select>
|
84 |
+
</div>
|
85 |
+
<a id="wpcf7cf-delete-button" class="delete-button" title="delete rule" href="#"><span class="dashicons dashicons-dismiss"></span> Remove rule</a>
|
86 |
+
<a id="wpcf7cf-add-button" title="add new rule" href="#"><span class="dashicons dashicons-plus-alt"></span> add new conditional rule</a>
|
87 |
+
|
88 |
+
<div id="wpcf7cf-entries">
|
89 |
+
<?php
|
90 |
+
$i = 0;
|
91 |
+
foreach($wpcf7cf_entries as $id => $entry) {
|
92 |
+
?>
|
93 |
+
<div class="entry" id="entry-<?php echo $i ?>">
|
94 |
+
if
|
95 |
+
<select name="wpcf7cf_options[<?php echo $i ?>][if_field]" class="if-field-select"><?php all_field_options($form, $entry['if_field']); ?></select>
|
96 |
+
<select name="wpcf7cf_options[<?php echo $i ?>][operator]" class="operator"><?php all_operator_options($entry['operator']) ?></select>
|
97 |
+
<input name="wpcf7cf_options[<?php echo $i ?>][if_value]" class="if-value" type="text" placeholder="value" value="<?php echo $entry['if_value'] ?>">
|
98 |
+
then
|
99 |
+
<select name="wpcf7cf_options[<?php echo $i ?>][then_visibility]" class="then-visibility"><?php all_display_options($entry['then_visibility']) ?></select>
|
100 |
+
<select name="wpcf7cf_options[<?php echo $i ?>][then_field]" class="then-field-select"><?php all_group_options($form, $entry['then_field']); ?></select>
|
101 |
+
<a style="display: inline-block;" href="#" title="delete rule" class="delete-button"><span class="dashicons dashicons-dismiss"></span> Remove rule</a>
|
102 |
+
</div>
|
103 |
+
<?php
|
104 |
+
$i++;
|
105 |
+
}
|
106 |
+
?>
|
107 |
+
</div>
|
108 |
+
|
109 |
+
|
110 |
+
<div id="wpcf7cf-text-entries">
|
111 |
+
<p><a href="#" id="wpcf7cf-settings-to-text">Export settings</a></p>
|
112 |
+
<div id="wpcf7cf-settings-text-wrap">
|
113 |
+
<textarea id="wpcf7cf-settings-text"></textarea>
|
114 |
+
<p><a href="#" id="wpcf7cf-settings-text-clear">Clear</a></p>
|
115 |
+
</div>
|
116 |
+
</div>
|
117 |
+
|
118 |
+
<script>
|
119 |
+
(function($) {
|
120 |
+
var index = $('#wpcf7cf-entries .entry').length;
|
121 |
+
|
122 |
+
$('.delete-button').click(function(){
|
123 |
+
|
124 |
+
//if (confirm('You sure?')===false) return false;
|
125 |
+
$(this).parent().remove();
|
126 |
+
return false;
|
127 |
+
|
128 |
+
});
|
129 |
+
|
130 |
+
$('#wpcf7cf-add-button').click(function(){
|
131 |
+
|
132 |
+
// if ($('#wpcf7cf-new-entry .if-field-select').val() == '-1' || $('#wpcf7cf-new-entry .then-field-select').val() == '-1') {
|
133 |
+
// alert('Please select 2 fields');
|
134 |
+
// $(this).parent().remove();
|
135 |
+
// return false;
|
136 |
+
// } else if($('#wpcf7cf-new-entry .if-field-select').val() == $('#wpcf7cf-new-entry .then-field-select').val()) {
|
137 |
+
// alert('The fields cannot be the same');
|
138 |
+
// $(this).parent().remove();
|
139 |
+
// return false;
|
140 |
+
// }
|
141 |
+
|
142 |
+
var $delete_button = $('#wpcf7cf-delete-button').clone().removeAttr('id');
|
143 |
+
$('<div class="entry" id="entry-'+index+'">'+($('#wpcf7cf-new-entry').html().replace(/{id}/g, index))+'</div>').prependTo('#wpcf7cf-entries').append($delete_button);
|
144 |
+
$delete_button.click(function(){
|
145 |
+
|
146 |
+
//if (confirm('You sure?')===false) return false;
|
147 |
+
$(this).parent().remove();
|
148 |
+
return false;
|
149 |
+
|
150 |
+
});
|
151 |
+
index++;
|
152 |
+
return false;
|
153 |
+
|
154 |
+
});
|
155 |
+
|
156 |
+
// settings to
|
157 |
+
|
158 |
+
$('#wpcf7cf-settings-text-wrap').hide();
|
159 |
+
|
160 |
+
$('#wpcf7cf-settings-to-text').click(function() {
|
161 |
+
$('#wpcf7cf-settings-text-wrap').show();
|
162 |
+
|
163 |
+
$('#wpcf7cf-settings-text').val('');
|
164 |
+
$('#wpcf7cf-entries .entry').each(function() {
|
165 |
+
var $entry = $(this);
|
166 |
+
var line = 'if [' + $entry.find('.if-field-select').val() + ']'
|
167 |
+
+ ' ' + $entry.find('.operator').val()
|
168 |
+
+ ' "' + $entry.find('.if-value').val() + '" then '
|
169 |
+
+ $entry.find('.then-visibility').val()
|
170 |
+
+ ' [' + $entry.find('.then-field-select').val() + ']';
|
171 |
+
$('#wpcf7cf-settings-text').val($('#wpcf7cf-settings-text').val() + line + "\n" ).select();
|
172 |
+
});
|
173 |
+
return false;
|
174 |
+
});
|
175 |
+
|
176 |
+
$('#wpcf7cf-settings-text-clear').click(function() {
|
177 |
+
$('#wpcf7cf-settings-text-wrap').hide();
|
178 |
+
$('#wpcf7cf-settings-text').val('');
|
179 |
+
return false;
|
180 |
+
});
|
181 |
+
|
182 |
+
})( jQuery );
|
183 |
+
</script>
|
184 |
+
<?php
|
185 |
+
}
|
186 |
+
|
187 |
+
// define the wpcf7_save_contact_form callback
|
188 |
+
function wpcf7cf_save_contact_form( $contact_form )
|
189 |
+
{
|
190 |
+
if ( ! isset( $_POST ) || empty( $_POST ) || ! isset( $_POST['wpcf7cf_options'] ) || ! is_array( $_POST['wpcf7cf_options'] ) )
|
191 |
+
return;
|
192 |
+
$post_id = $contact_form->id();
|
193 |
+
if ( ! $post_id )
|
194 |
+
return;
|
195 |
+
|
196 |
+
unset($_POST['wpcf7cf_options']['{id}']); // remove the dummy entry
|
197 |
+
|
198 |
+
update_post_meta( $post_id, 'wpcf7cf_options', $_POST['wpcf7cf_options'] );
|
199 |
+
|
200 |
+
};
|
201 |
+
|
202 |
+
// add the action
|
203 |
add_action( 'wpcf7_save_contact_form', 'wpcf7cf_save_contact_form', 10, 1 );
|
contact-form-7-conditional-fields.php
CHANGED
@@ -1,196 +1,396 @@
|
|
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.
|
8 |
-
Author URI: http://bdwm.be/
|
9 |
-
*/
|
10 |
-
|
11 |
-
/**
|
12 |
-
* This program is free software; you can redistribute it and/or modify
|
13 |
-
* it under the terms of the GNU General Public License as published by
|
14 |
-
* the Free Software Foundation; either version 2 of the License, or
|
15 |
-
* (at your option) any later version.
|
16 |
-
*
|
17 |
-
* This program is distributed in the hope that it will be useful,
|
18 |
-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
19 |
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
20 |
-
* GNU General Public License for more details.
|
21 |
-
*
|
22 |
-
* You should have received a copy of the GNU General Public License
|
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', '0.
|
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, '/' ) );
|
38 |
-
}
|
39 |
-
|
40 |
-
function wpcf7cf_plugin_url( $path = '' ) {
|
41 |
-
$url = plugins_url( $path, WPCF7CF_PLUGIN );
|
42 |
-
if ( is_ssl() && 'http:' == substr( $url, 0, 5 ) ) {
|
43 |
-
$url = 'https:' . substr( $url, 5 );
|
44 |
-
}
|
45 |
-
return $url;
|
46 |
-
}
|
47 |
-
|
48 |
-
class ContactForm7ConditionalFields {
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
add_action('
|
57 |
-
|
58 |
-
//
|
59 |
-
add_action('
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
<
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
}
|
175 |
-
|
176 |
-
$
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
13 |
+
* it under the terms of the GNU General Public License as published by
|
14 |
+
* the Free Software Foundation; either version 2 of the License, or
|
15 |
+
* (at your option) any later version.
|
16 |
+
*
|
17 |
+
* This program is distributed in the hope that it will be useful,
|
18 |
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
19 |
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
20 |
+
* GNU General Public License for more details.
|
21 |
+
*
|
22 |
+
* You should have received a copy of the GNU General Public License
|
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', '0.2' );
|
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, '/' ) );
|
38 |
+
}
|
39 |
+
|
40 |
+
function wpcf7cf_plugin_url( $path = '' ) {
|
41 |
+
$url = plugins_url( $path, WPCF7CF_PLUGIN );
|
42 |
+
if ( is_ssl() && 'http:' == substr( $url, 0, 5 ) ) {
|
43 |
+
$url = 'https:' . substr( $url, 5 );
|
44 |
+
}
|
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 |
+
|
68 |
+
register_activation_hook(__FILE__, array($this, 'activate'));
|
69 |
+
|
70 |
+
if (is_admin()) {
|
71 |
+
require_once dirname(__FILE__) . '/admin.php';
|
72 |
+
}
|
73 |
+
}
|
74 |
+
|
75 |
+
function activate() {
|
76 |
+
//add options with add_option and stuff
|
77 |
+
}
|
78 |
+
|
79 |
+
public static function enqueue_js() {
|
80 |
+
// nothing here. We will only load the CF7 script if there is a CF7 form on the page.
|
81 |
+
}
|
82 |
+
|
83 |
+
public static function enqueue_css() {
|
84 |
+
|
85 |
+
wp_enqueue_style('cf7cf-style', plugins_url('style.css', __FILE__));
|
86 |
+
}
|
87 |
+
|
88 |
+
public static function add_shortcodes() {
|
89 |
+
wpcf7_add_shortcode('group', array(__CLASS__, 'shortcode_handler'), true);
|
90 |
+
//add_shortcode( 'group', array(__CLASS__, 'group_shortcode_handler') );
|
91 |
+
}
|
92 |
+
|
93 |
+
function group_shortcode_handler( $atts, $content = "" ) {
|
94 |
+
return $content;
|
95 |
+
}
|
96 |
+
|
97 |
+
function shortcode_handler($tag) {
|
98 |
+
$tag = new WPCF7_Shortcode($tag);
|
99 |
+
//ob_start();
|
100 |
+
//print_r($tag);
|
101 |
+
//return print_r($tag, true);
|
102 |
+
return $tag->content;
|
103 |
+
}
|
104 |
+
|
105 |
+
|
106 |
+
public static function tag_generator() {
|
107 |
+
if (! function_exists( 'wpcf7_add_tag_generator'))
|
108 |
+
return;
|
109 |
+
|
110 |
+
wpcf7_add_tag_generator('group',
|
111 |
+
__('Conditional fields Group', 'wpcf7cf'),
|
112 |
+
'wpcf7-tg-pane-group',
|
113 |
+
array(__CLASS__, 'tg_pane')
|
114 |
+
);
|
115 |
+
}
|
116 |
+
|
117 |
+
function tg_pane( $contact_form, $args = '' ) {
|
118 |
+
$args = wp_parse_args( $args, array() );
|
119 |
+
$type = 'group';
|
120 |
+
|
121 |
+
$description = __( "Generate a group tag to group form elements that can be shown conditionally.", 'cf7cf' );
|
122 |
+
|
123 |
+
?>
|
124 |
+
<div class="control-box">
|
125 |
+
<fieldset>
|
126 |
+
<legend><?php echo sprintf( esc_html( $description ) ); ?></legend>
|
127 |
+
|
128 |
+
<table class="form-table">
|
129 |
+
<tbody>
|
130 |
+
|
131 |
+
<tr>
|
132 |
+
<th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-name' ); ?>"><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?></label></th>
|
133 |
+
<td><input type="text" name="name" class="tg-name oneline" id="<?php echo esc_attr( $args['content'] . '-name' ); ?>" /></td>
|
134 |
+
</tr>
|
135 |
+
|
136 |
+
</tbody>
|
137 |
+
</table>
|
138 |
+
</fieldset>
|
139 |
+
</div>
|
140 |
+
|
141 |
+
<div class="insert-box">
|
142 |
+
<input type="text" name="<?php echo $type; ?>" class="tag code" readonly="readonly" onfocus="this.select()" />
|
143 |
+
|
144 |
+
<div class="submitbox">
|
145 |
+
<input type="button" class="button button-primary insert-tag" value="<?php echo esc_attr( __( 'Insert Tag', 'contact-form-7' ) ); ?>" />
|
146 |
+
</div>
|
147 |
+
|
148 |
+
<br class="clear" />
|
149 |
+
</div>
|
150 |
+
<?php
|
151 |
+
}
|
152 |
+
|
153 |
+
/**
|
154 |
+
* Remove validation requirements for fields that are hidden at the time of form submission.
|
155 |
+
* Called using add_filter( 'wpcf7_validate_[tag_type]', array($this, 'skip_validation_for_hidden_fields'), 2, 2 );
|
156 |
+
* where the priority of 2 causes this to kill any validations with a priority higher than 2
|
157 |
+
*
|
158 |
+
* @param $result
|
159 |
+
* @param $tag
|
160 |
+
*
|
161 |
+
* @return mixed
|
162 |
+
*/
|
163 |
+
function skip_validation_for_hidden_fields($result, $tag) {
|
164 |
+
global $wp_filter;
|
165 |
+
$hidden_fields = $this->get_hidden_fields();
|
166 |
+
|
167 |
+
// If this field is hidden, skip the rest of the validation hooks
|
168 |
+
if( in_array($tag['name'], $hidden_fields) ) {
|
169 |
+
// end() skips to the end of the $wp_filter array, effectively skipping any filters with a priority
|
170 |
+
// lower than whatever priority this function was given.
|
171 |
+
// In our case, this means that a hidden field won't be marked as "Invalid", regardless of its contents
|
172 |
+
// unless it's done by a filter of priority 1 or 2
|
173 |
+
end( $wp_filter[ current_filter() ] );
|
174 |
+
}
|
175 |
+
|
176 |
+
return $result;
|
177 |
+
}
|
178 |
+
|
179 |
+
|
180 |
+
/**
|
181 |
+
* When a CF7 form is posted, check the form for hidden fields, then remove those fields from the post data
|
182 |
+
*
|
183 |
+
* @param $posted_data
|
184 |
+
*
|
185 |
+
* @return mixed
|
186 |
+
*/
|
187 |
+
function remove_hidden_post_data($posted_data) {
|
188 |
+
$hidden_fields = $this->get_hidden_fields($posted_data);
|
189 |
+
|
190 |
+
foreach( $hidden_fields as $name => $value ) {
|
191 |
+
unset( $posted_data[$name] );
|
192 |
+
}
|
193 |
+
return $posted_data;
|
194 |
+
}
|
195 |
+
|
196 |
+
|
197 |
+
/**
|
198 |
+
* Finds the currently submitted form and returns an array of fields that are hidden and should be ignored
|
199 |
+
*
|
200 |
+
* @param bool|array $posted_data
|
201 |
+
* @return mixed
|
202 |
+
*/
|
203 |
+
function get_hidden_fields($posted_data = false) {
|
204 |
+
if ( isset( $posted_data['_wpcf7'] ) ) {
|
205 |
+
// When called by wpcf7cf_remove_hidden_post_data() and $posted_data is available
|
206 |
+
$form_id = $posted_data['_wpcf7'];
|
207 |
+
} else {
|
208 |
+
// When called from skip_validation_for_hidden_fields(), use WPCF7_Submission object to get form id
|
209 |
+
$form_id = WPCF7_Submission::get_instance()->get_posted_data( '_wpcf7' );
|
210 |
+
}
|
211 |
+
|
212 |
+
// We only need to run through this once, so check to see if the global variable exists.
|
213 |
+
if ( 0 == count( $this->hidden_fields ) ) {
|
214 |
+
// Get the WPCF7_ContactForm object for this form
|
215 |
+
$contact_form = WPCF7_ContactForm::get_instance( $form_id );
|
216 |
+
|
217 |
+
// While we have the contact form object, find all used tags so we can add our validation filter
|
218 |
+
foreach( (array) $contact_form->form_scan_shortcode() as $tag ) {
|
219 |
+
//Priority of 2 allows other filters at priority 1 or 2 to actually validate hidden fields (just in case)
|
220 |
+
add_filter( 'wpcf7_validate_' . $tag['type'], array($this, 'skip_validation_for_hidden_fields'), 2, 2 );
|
221 |
+
}
|
222 |
+
|
223 |
+
// Get the form properties so we have access to the form itself
|
224 |
+
$form_properties = $contact_form->get_properties();
|
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();
|
231 |
+
foreach ($divs as $div) {
|
232 |
+
$is_group = false;
|
233 |
+
$id = 0;
|
234 |
+
foreach($div->attributes as $attribute) {
|
235 |
+
if ( 'data-class' == $attribute->name && 'wpcf7cf_group' == $attribute->value ) {
|
236 |
+
// Group divs will have a data-class of wpcf7cf_group
|
237 |
+
$is_group = true;
|
238 |
+
}
|
239 |
+
if ( 'id' == $attribute->name ) {
|
240 |
+
$id = $attribute->value;
|
241 |
+
}
|
242 |
+
}
|
243 |
+
if ( $is_group ) {
|
244 |
+
$groups[$id] = array();
|
245 |
+
// Match all tag names (format = [tag_type tag_name] or [tag_type tag_name options values etc...])
|
246 |
+
preg_match_all("/\[[^\s\]]* ([^\s\]]*)[^\]]*\]/", $div->textContent, $matches);
|
247 |
+
foreach( $matches[1] as $tag ) {
|
248 |
+
$groups[$id][] = $tag;
|
249 |
+
}
|
250 |
+
}
|
251 |
+
}
|
252 |
+
// $groups is now an array of groups (by id) with an array of the name of each tag that is inside that group.
|
253 |
+
|
254 |
+
|
255 |
+
$visible_groups = $this->get_visible_groups($posted_data);
|
256 |
+
|
257 |
+
// Iterate through the groups.
|
258 |
+
// When we find one that's not in the $visible_groups array, add its tags to our list of hidden tags
|
259 |
+
foreach( $groups as $group => $fields ) {
|
260 |
+
if ( ! in_array($group, $visible_groups) ) {
|
261 |
+
$this->hidden_groups[] = $group;
|
262 |
+
$this->hidden_fields = array_merge($this->hidden_fields, $fields);
|
263 |
+
}
|
264 |
+
}
|
265 |
+
|
266 |
+
|
267 |
+
}
|
268 |
+
return $this->hidden_fields;
|
269 |
+
}
|
270 |
+
|
271 |
+
function get_visible_groups($posted_data) {
|
272 |
+
// Groups are hidden by default. Find all the visible ones and mark them.
|
273 |
+
// This is a duplicate of the logic in js/scripts.js and needs to be included
|
274 |
+
// so our verification is done server-side. If we ran this verification in
|
275 |
+
// javascript, then all the form's normal validation could be overridden.
|
276 |
+
//
|
277 |
+
// Unfortunately, separate javascript and php validation is probably necessary since to use only php
|
278 |
+
// would mean that every onChange() would require an ajax call, and that'd get too slow.
|
279 |
+
$form_id = $posted_data['_wpcf7'];
|
280 |
+
if( $this->visible_groups ) {
|
281 |
+
return $this->visible_groups;
|
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 |
+
}
|
301 |
+
}
|
302 |
+
return $this->visible_groups;
|
303 |
+
}
|
304 |
+
|
305 |
+
function hide_hidden_mail_fields( $components ) {
|
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];
|
331 |
+
if ( in_array( $name, $this->hidden_groups ) ) {
|
332 |
+
// The tag name represents a hidden group, so replace everything from [tagname] to [/tagname] with nothing
|
333 |
+
return '';
|
334 |
+
} elseif ( in_array( $name, $this->visible_groups ) ) {
|
335 |
+
// The tag name represents a visible group, so remove the tags themselves, but return everything else
|
336 |
+
return $content;
|
337 |
+
} else {
|
338 |
+
// The tag name doesn't represent a group that was used in the form. Leave it alone (return the entire match).
|
339 |
+
return $matches[0];
|
340 |
+
}
|
341 |
+
}
|
342 |
+
}
|
343 |
+
|
344 |
+
new ContactForm7ConditionalFields;
|
345 |
+
|
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(
|
353 |
+
'/\[group\s*\]/s', // matches [group ] or [group]
|
354 |
+
'/\[group\s+([^\s\]]*)\s*([^\]]*)\]/s', // matches [group something some:thing] or [group something som ]
|
355 |
+
// doesn't match [group-special something]
|
356 |
+
'/\[\/group\]/s'
|
357 |
+
);
|
358 |
+
|
359 |
+
$replace = array(
|
360 |
+
'<div data-class="wpcf7cf_group">',
|
361 |
+
'<div id="$1" data-class="wpcf7cf_group">',
|
362 |
+
'</div>'
|
363 |
+
);
|
364 |
+
|
365 |
+
$form = preg_replace( $find, $replace, $form );
|
366 |
+
|
367 |
+
$properties['form'] = $form;
|
368 |
+
}
|
369 |
+
return $properties;
|
370 |
+
}
|
371 |
+
|
372 |
+
$global_count = 0;
|
373 |
+
|
374 |
+
add_action('wpcf7_contact_form', 'wpcf7cf_enqueue_scripts', 10, 1);
|
375 |
+
function wpcf7cf_enqueue_scripts(WPCF7_ContactForm $cf7form) {
|
376 |
+
|
377 |
+
if (is_admin()) return;
|
378 |
+
|
379 |
+
global $global_count, $post;
|
380 |
+
$global_count++;
|
381 |
+
|
382 |
+
if ( in_the_loop() ) {
|
383 |
+
$unit_tag = 'wpcf7-f'.$cf7form->id().'-p'.$post->ID.'-o'.$global_count;
|
384 |
+
} else {
|
385 |
+
$unit_tag = 'wpcf7-f'.$cf7form->id().'-o'.$global_count;
|
386 |
+
}
|
387 |
+
|
388 |
+
$options = array(
|
389 |
+
'form_id' => $cf7form->id(),
|
390 |
+
'unit_tag' => $unit_tag,
|
391 |
+
'conditions' => get_post_meta($cf7form->id(),'wpcf7cf_options', true),
|
392 |
+
);
|
393 |
+
|
394 |
+
wp_enqueue_script('cf7cf-scripts', plugins_url('js/scripts.js', __FILE__), array('jquery'), WPCF7CF_VERSION, true);
|
395 |
+
wp_localize_script('cf7cf-scripts', 'wpcf7cf_options_'.$global_count, $options);
|
396 |
+
}
|
js/scripts.js
CHANGED
@@ -43,12 +43,22 @@ var cf7signature_resized = 0; // for compatibility with contact-form-7-signature
|
|
43 |
// single field (tested with text field, single checkbox, select with single value (dropdown), select with multiple values)
|
44 |
|
45 |
if ($field.is('select')) {
|
46 |
-
|
|
|
|
|
|
|
|
|
47 |
var $option = $(this);
|
48 |
if (condition.operator == 'equals' && $option.val() == condition.if_value) {
|
49 |
-
|
|
|
|
|
50 |
}
|
51 |
});
|
|
|
|
|
|
|
|
|
52 |
continue;
|
53 |
}
|
54 |
|
@@ -91,5 +101,5 @@ var cf7signature_resized = 0; // for compatibility with contact-form-7-signature
|
|
91 |
}
|
92 |
|
93 |
});
|
94 |
-
|
95 |
})( jQuery );
|
43 |
// single field (tested with text field, single checkbox, select with single value (dropdown), select with multiple values)
|
44 |
|
45 |
if ($field.is('select')) {
|
46 |
+
var show = false;
|
47 |
+
if(condition.operator == 'not equals') {
|
48 |
+
show = true;
|
49 |
+
}
|
50 |
+
$field.find('option:selected').each(function () {
|
51 |
var $option = $(this);
|
52 |
if (condition.operator == 'equals' && $option.val() == condition.if_value) {
|
53 |
+
show = true;
|
54 |
+
} else if (condition.operator == 'not equals' && $option.val() == condition.if_value) {
|
55 |
+
show = false;
|
56 |
}
|
57 |
});
|
58 |
+
if(show == true) {
|
59 |
+
$('#' + unit_tag + ' #' + condition.then_field).show();
|
60 |
+
}
|
61 |
+
|
62 |
continue;
|
63 |
}
|
64 |
|
101 |
}
|
102 |
|
103 |
});
|
104 |
+
|
105 |
})( jQuery );
|
readme.txt
CHANGED
@@ -1,78 +1,92 @@
|
|
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¤cy_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.
|
9 |
-
Stable tag: 0.
|
10 |
-
License: GPLv2 or later
|
11 |
-
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
12 |
-
|
13 |
-
Adds conditional logic to Contact Form 7.
|
14 |
-
|
15 |
-
== Description ==
|
16 |
-
|
17 |
-
This plugin adds conditional logic to [Contact Form 7](https://wordpress.org/plugins/contact-form-7/).
|
18 |
-
|
19 |
-
If you edit your CF7 form, you will see an additional tag called "Conditional fields Group". Everything you put between the start and end tag will be hidden by default.
|
20 |
-
After you added the field group(s), you should click Save.
|
21 |
-
Then you should go to the "Conditional fields" tab to create one or more conditions that will make the group(s) appear.
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
= 0.
|
51 |
-
*
|
52 |
-
*
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
*
|
58 |
-
*
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
*
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
= 0.1 =
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
=== Conditional Fields for Contact Form 7 ===
|
2 |
+
Contributors: Jules Colle, stevish
|
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¤cy_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.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 |
+
|
13 |
+
Adds conditional logic to Contact Form 7.
|
14 |
+
|
15 |
+
== Description ==
|
16 |
+
|
17 |
+
This plugin adds conditional logic to [Contact Form 7](https://wordpress.org/plugins/contact-form-7/).
|
18 |
+
|
19 |
+
If you edit your CF7 form, you will see an additional tag called "Conditional fields Group". Everything you put between the start and end tag will be hidden by default.
|
20 |
+
After you added the field group(s), you should click Save.
|
21 |
+
Then you should go to the "Conditional fields" tab to create one or more conditions that will make the group(s) appear.
|
22 |
+
|
23 |
+
= How to use it =
|
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 ==
|
34 |
+
|
35 |
+
Please follow the [standard installation procedure for WordPress plugins](http://codex.wordpress.org/Managing_Plugins#Installing_Plugins).
|
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 |
+
|
45 |
+
1. Back End
|
46 |
+
2. Front End
|
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!
|
53 |
+
* Added @stevish as a contributer to the project :)
|
54 |
+
* Fix form not working in widgets or other places outside of the loop. Thanks to @ciprianolaru for the solution (https://wordpress.org/support/topic/problem-with-unit_tag-when-not-in-the-loop-form-not-used-in-post-or-page/#post-8299801)
|
55 |
+
|
56 |
+
= 0.1.7 =
|
57 |
+
* Fix popup warning to leave page even tough no changes have been made. Thanks to @hhmaster2045 for reporting the bug. https://wordpress.org/support/topic/popup-warning-to-leave-page-even-though-no-changes-have-been-made
|
58 |
+
* Added export option for easier troubleshooting.
|
59 |
+
* Don't include front end javascript in backend.
|
60 |
+
|
61 |
+
= 0.1.6 =
|
62 |
+
* made compatible with wpcf7-form-control-signature-wrap plugin https://wordpress.org/support/topic/signature-add-on-not-working
|
63 |
+
|
64 |
+
= 0.1.5 =
|
65 |
+
* fixed PHP notice thanks to @natalia_c https://wordpress.org/support/topic/php-notice-80
|
66 |
+
* tested with WP 4.5.3
|
67 |
+
|
68 |
+
= 0.1.4 =
|
69 |
+
|
70 |
+
* Prevent conflicts between different forms on one page.
|
71 |
+
* Prevent conflicts between multiple instances of the same form on one page. (https://wordpress.org/support/topic/bug-153)
|
72 |
+
* 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)
|
73 |
+
|
74 |
+
= 0.1.3 =
|
75 |
+
|
76 |
+
* Removed fielset, id and class attributes for group tags, because they weren't used anyway and broke the shortcode
|
77 |
+
* If extra attributes are added to the group shortcode, this will no longer break functionality (even though no attributes are supported)
|
78 |
+
|
79 |
+
= 0.1.2 =
|
80 |
+
|
81 |
+
* Make code work with select element that allows multiple options.
|
82 |
+
* Only load javascript on pages that contain a CF7 form
|
83 |
+
|
84 |
+
= 0.1.1 =
|
85 |
+
|
86 |
+
Fixed bug with exclusive checkboxes (https://wordpress.org/support/topic/groups-not-showing)
|
87 |
+
|
88 |
+
= 0.1 =
|
89 |
+
|
90 |
+
First release
|
91 |
+
|
92 |
+
|