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 0.2.2 0.2.3
cf7-conditional-fields / cf7cf.php
cf7-conditional-fields Last commit date
js 9 years ago admin-style.css 9 years ago admin.php 9 years ago cf7cf.php 9 years ago contact-form-7-conditional-fields.php 9 years ago readme.txt 9 years ago screenshot-1.png 10 years ago screenshot-2.png 10 years ago style.css 9 years ago
cf7cf.php
347 lines
1 <?php
2 class ContactForm7ConditionalFields {
3 private $hidden_fields = array();
4 private $visible_groups = array();
5 private $hidden_groups = array();
6
7 function __construct() {
8
9 add_action('wpcf7_enqueue_scripts', array(__CLASS__, 'enqueue_js'));
10 add_action('wpcf7_enqueue_styles', array(__CLASS__, 'enqueue_css'));
11
12 // Register shortcodes
13 add_action('wpcf7_init', array(__CLASS__, 'add_shortcodes'));
14
15 // Tag generator
16 add_action('load-contact_page_wpcf7-new', array(__CLASS__, 'tag_generator'));
17 add_action('load-toplevel_page_wpcf7', array(__CLASS__, 'tag_generator'));
18
19 // compatibility with CF7 multi-step forms by Webhead LLC.
20 add_filter( 'wpcf7_posted_data', array($this,'cf7msm_merge_post_with_cookie'), 8, 1 );
21
22 // compatibility with CF7 Multi Step by NinjaTeam https://wordpress.org/plugins/cf7-multi-step/
23 add_action('wp_ajax_cf7mls_validation', array($this,'cf7mls_validation_callback'),9);
24 add_action('wp_ajax_nopriv_cf7mls_validation', array($this,'cf7mls_validation_callback'),9);
25
26 add_filter( 'wpcf7_posted_data', array($this, 'remove_hidden_post_data') );
27 add_filter( 'wpcf7_mail_components', array($this, 'hide_hidden_mail_fields') );
28 add_filter('wpcf7_additional_mail', array($this, 'hide_hidden_mail_fields_additional_mail'), 10, 2);
29
30 add_filter( 'wpcf7_validate', array($this, 'skip_validation_for_hidden_fields'), 2, 2 );
31
32 register_activation_hook(__FILE__, array($this, 'activate'));
33
34 if (is_admin()) {
35 require_once dirname(__FILE__) . '/admin.php';
36 }
37 }
38
39 function activate() {
40 //add options with add_option and stuff
41 }
42
43 public static function enqueue_js() {
44 // nothing here. We will only load the CF7 script if there is a CF7 form on the page.
45 }
46
47 public static function enqueue_css() {
48 wp_enqueue_style('cf7cf-style', plugins_url('style.css', __FILE__), array(), WPCF7CF_VERSION);
49 }
50
51 public static function add_shortcodes() {
52 //wpcf7_add_shortcode('group', array(__CLASS__, 'shortcode_handler'), true);
53 //add_shortcode( 'group', array(__CLASS__, 'group_shortcode_handler') );
54 if (function_exists('wpcf7_add_form_tag'))
55 wpcf7_add_form_tag('group', array(__CLASS__, 'shortcode_handler'), true);
56 else if (function_exists('wpcf7_add_shortcode')) {
57 wpcf7_add_shortcode('group', array(__CLASS__, 'shortcode_handler'), true);
58 } else {
59 throw new Exception('functions wpcf7_add_form_tag and wpcf7_add_shortcode not found.');
60 }
61 }
62
63 function group_shortcode_handler( $atts, $content = "" ) {
64 return $content;
65 }
66
67 function shortcode_handler($tag) {
68 //$tag = new WPCF7_Shortcode($tag);
69 $tag = new WPCF7_FormTag($tag);
70 //ob_start();
71 //print_r($tag);
72 //return print_r($tag, true);
73 return $tag->content;
74 }
75
76
77 public static function tag_generator() {
78 if (! function_exists( 'wpcf7_add_tag_generator'))
79 return;
80
81 wpcf7_add_tag_generator('group',
82 __('Conditional fields Group', 'wpcf7cf'),
83 'wpcf7-tg-pane-group',
84 array(__CLASS__, 'tg_pane')
85 );
86 }
87
88 static function tg_pane( $contact_form, $args = '' ) {
89 $args = wp_parse_args( $args, array() );
90 $type = 'group';
91
92 $description = __( "Generate a group tag to group form elements that can be shown conditionally.", 'cf7cf' );
93
94 ?>
95 <div class="control-box">
96 <fieldset>
97 <legend><?php echo sprintf( esc_html( $description ) ); ?></legend>
98
99 <table class="form-table">
100 <tbody>
101
102 <tr>
103 <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-name' ); ?>"><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?></label></th>
104 <td><input type="text" name="name" class="tg-name oneline" id="<?php echo esc_attr( $args['content'] . '-name' ); ?>" /></td>
105 </tr>
106
107 </tbody>
108 </table>
109 </fieldset>
110 </div>
111
112 <div class="insert-box">
113 <input type="text" name="<?php echo $type; ?>" class="tag code" readonly="readonly" onfocus="this.select()" />
114
115 <div class="submitbox">
116 <input type="button" class="button button-primary insert-tag" value="<?php echo esc_attr( __( 'Insert Tag', 'contact-form-7' ) ); ?>" />
117 </div>
118
119 <br class="clear" />
120 </div>
121 <?php
122 }
123
124 /**
125 * Remove validation requirements for fields that are hidden at the time of form submission.
126 * Called using add_filter( 'wpcf7_validate_[tag_type]', array($this, 'skip_validation_for_hidden_fields'), 2, 2 );
127 * where the priority of 2 causes this to kill any validations with a priority higher than 2
128 *
129 * @param $result
130 * @param $tag
131 *
132 * @return mixed
133 */
134 function skip_validation_for_hidden_fields($result, $tags) {
135
136 if (count($this->hidden_fields) == 0) return $result;
137
138 $return_result = new WPCF7_Validation();
139
140 $invalid_fields = $result->get_invalid_fields();
141
142 if (!is_array($invalid_fields) || count($invalid_fields) == 0) return $result;
143
144 foreach ($invalid_fields as $invalid_field_key => $invalid_field_data) {
145 if (!in_array($invalid_field_key, $this->hidden_fields)) {
146 // the invalid field is not a hidden field, so we'll add it to the final validation result
147 $return_result->invalidate($invalid_field_key, $invalid_field_data['reason']);
148 }
149 }
150
151 return $return_result;
152 }
153
154
155 /**
156 * When a CF7 form is posted, check the form for hidden fields, then remove those fields from the post data
157 *
158 * @param $posted_data
159 *
160 * @return mixed
161 */
162 function remove_hidden_post_data($posted_data) {
163 $this->set_hidden_fields_arrays($posted_data);
164
165 foreach( $this->hidden_fields as $name => $value ) {
166 unset( $posted_data[$name] );
167 }
168
169 return $posted_data;
170 }
171
172 function cf7msm_merge_post_with_cookie($posted_data) {
173
174 if (!function_exists('cf7msm_get')) return $posted_data;
175
176 if (!$posted_data) {
177 $posted_data = WPCF7_Submission::get_instance()->get_posted_data();
178 }
179
180 // this will temporarily set the hidden fields data to the posted_data.
181 // later this function will be called again with the updated posted_data
182 $this->set_hidden_fields_arrays($posted_data);
183
184 // get cookie data
185 $cookie_data = cf7msm_get('cf7msm_posted_data');
186 $cookie_data_hidden_group_fields = json_decode(stripslashes($cookie_data['_wpcf7cf_hidden_group_fields']));
187 $cookie_data_hidden_groups = json_decode(stripslashes($cookie_data['_wpcf7cf_hidden_groups']));
188 $cookie_data_visible_groups = json_decode(stripslashes($cookie_data['_wpcf7cf_visible_groups']));
189
190 // remove all the currently posted data from the cookie data (we don't wanna add it twice)
191 $cookie_data_hidden_group_fields = array_diff($cookie_data_hidden_group_fields, array_keys($posted_data));
192 $cookie_data_hidden_groups = array_diff((array) $cookie_data_hidden_groups, $this->hidden_groups, $this->visible_groups);
193 $cookie_data_visible_groups = array_diff((array) $cookie_data_visible_groups, $this->hidden_groups, $this->visible_groups);
194
195 // update current post data with cookie data
196 $posted_data['_wpcf7cf_hidden_group_fields'] = addslashes(json_encode(array_merge((array) $cookie_data_hidden_group_fields, $this->hidden_fields)));
197 $posted_data['_wpcf7cf_hidden_groups'] = addslashes(json_encode(array_merge((array) $cookie_data_hidden_groups, $this->hidden_groups)));
198 $posted_data['_wpcf7cf_visible_groups'] = addslashes(json_encode(array_merge((array) $cookie_data_visible_groups, $this->visible_groups)));
199
200 return $posted_data;
201 }
202
203 // compatibility with CF7 Multi Step by NinjaTeam https://wordpress.org/plugins/cf7-multi-step/
204 function cf7mls_validation_callback() {
205 $this->set_hidden_fields_arrays($_POST);
206 }
207
208
209 /**
210 * Finds the currently submitted form and set the hidden_fields variables accoringly
211 *
212 * @param bool|array $posted_data
213 */
214 function set_hidden_fields_arrays($posted_data = false) {
215
216 if (!$posted_data) {
217 $posted_data = WPCF7_Submission::get_instance()->get_posted_data();
218 }
219
220 $hidden_fields = json_decode(stripslashes($posted_data['_wpcf7cf_hidden_group_fields']));
221 if (is_array($hidden_fields) && count($hidden_fields) > 0) {
222 foreach ($hidden_fields as $field) {
223 $this->hidden_fields[] = $field;
224 if (wpcf7cf_endswith($field, '[]')) {
225 $this->hidden_fields[] = substr($field,0,strlen($field)-2);
226 }
227 }
228 }
229 $this->hidden_groups = json_decode(stripslashes($posted_data['_wpcf7cf_hidden_groups']));
230 $this->visible_groups = json_decode(stripslashes($posted_data['_wpcf7cf_visible_groups']));
231 }
232
233 function hide_hidden_mail_fields( $components ) {
234 $regex = '@\[[\t ]*([a-zA-Z_][0-9a-zA-Z:._-]*)[\t ]*\](.*?)\[[\t ]*/[\t ]*\1[\t ]*\]@s';
235 // [1] = name [2] = contents
236
237 $components['body'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $components['body'] );
238 $components['subject'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $components['subject'] );
239 $components['sender'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $components['sender'] );
240 $components['recipient'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $components['recipient'] );
241 $components['additional_headers'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $components['additional_headers'] );
242
243 return $components;
244 }
245
246 function hide_hidden_mail_fields_additional_mail($additional_mail, $contact_form) {
247
248 if (!is_array($additional_mail) || !array_key_exists('mail_2', $additional_mail)) return $additional_mail;
249
250 $regex = '@\[[\t ]*([a-zA-Z_][0-9a-zA-Z:._-]*)[\t ]*\](.*?)\[[\t ]*/[\t ]*\1[\t ]*\]@s';
251
252 $additional_mail['mail_2']['body'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $additional_mail['mail_2']['body'] );
253 $additional_mail['mail_2']['subject'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $additional_mail['mail_2']['subject'] );
254 $additional_mail['mail_2']['sender'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $additional_mail['mail_2']['sender'] );
255 $additional_mail['mail_2']['recipient'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $additional_mail['mail_2']['recipient'] );
256 $additional_mail['mail_2']['additional_headers'] = preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $additional_mail['mail_2']['additional_headers'] );
257
258 return $additional_mail;
259 }
260
261 function hide_hidden_mail_fields_regex_callback ( $matches ) {
262 $name = $matches[1];
263 $content = $matches[2];
264 if ( in_array( $name, $this->hidden_groups ) ) {
265 // The tag name represents a hidden group, so replace everything from [tagname] to [/tagname] with nothing
266 return '';
267 } elseif ( in_array( $name, $this->visible_groups ) ) {
268 // The tag name represents a visible group, so remove the tags themselves, but return everything else
269 //return $content;
270 $regex = '@\[[\t ]*([a-zA-Z_][0-9a-zA-Z:._-]*)[\t ]*\](.*?)\[[\t ]*/[\t ]*\1[\t ]*\]@s';
271
272 // instead of just returning the $content, return the preg_replaced content :)
273 return preg_replace_callback($regex, array($this, 'hide_hidden_mail_fields_regex_callback'), $content );
274 } else {
275 // The tag name doesn't represent a group that was used in the form. Leave it alone (return the entire match).
276 return $matches[0];
277 }
278 }
279 }
280
281 new ContactForm7ConditionalFields;
282
283 add_filter( 'wpcf7_contact_form_properties', 'wpcf7cf_properties', 10, 2 );
284
285 function wpcf7cf_properties($properties, $wpcf7form) {
286 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.
287 $form = $properties['form'];
288
289 $find = array(
290 '/\[group\s*\]/s', // matches [group ] or [group]
291 '/\[group\s+([^\s\]]*)\s*([^\]]*)\]/s', // matches [group something some:thing] or [group something som ]
292 // doesn't match [group-special something]
293 '/\[\/group\]/s'
294 );
295
296 $replace = array(
297 '<div data-class="wpcf7cf_group">',
298 '<div id="$1" data-class="wpcf7cf_group">',
299 '</div>'
300 );
301
302 $form = preg_replace( $find, $replace, $form );
303
304 $properties['form'] = $form;
305 }
306 return $properties;
307 }
308
309 $global_count = 0;
310
311 add_action('wpcf7_contact_form', 'wpcf7cf_enqueue_scripts', 10, 1);
312 function wpcf7cf_enqueue_scripts(WPCF7_ContactForm $cf7form) {
313
314 if (is_admin()) return;
315
316 global $global_count, $post;
317 $global_count++;
318
319 if ( in_the_loop() ) {
320 $post_id = empty($post->ID) ? '0' : $post->ID;
321 $unit_tag = 'wpcf7-f'.$cf7form->id().'-p'.$post_id.'-o'.$global_count;
322 } else {
323 $unit_tag = 'wpcf7-f'.$cf7form->id().'-o'.$global_count;
324 }
325
326 $options = array(
327 'form_id' => $cf7form->id(),
328 'unit_tag' => $unit_tag,
329 'conditions' => get_post_meta($cf7form->id(),'wpcf7cf_options', true),
330 );
331
332 wp_enqueue_script('cf7cf-scripts', plugins_url('js/scripts.js', __FILE__), array('jquery'), WPCF7CF_VERSION, true);
333 wp_localize_script('cf7cf-scripts', 'wpcf7cf_options_'.$global_count, $options);
334 }
335
336 add_action('wpcf7_form_hidden_fields', 'wpcf7cf_form_hidden_fields',10,1);
337
338 function wpcf7cf_form_hidden_fields($hidden_fields) {
339 return array('_wpcf7cf_hidden_group_fields' => '', '_wpcf7cf_hidden_groups' => '', '_wpcf7cf_visible_groups' => '');
340 }
341
342 function wpcf7cf_endswith($string, $test) {
343 $strlen = strlen($string);
344 $testlen = strlen($test);
345 if ($testlen > $strlen) return false;
346 return substr_compare($string, $test, $strlen - $testlen, $testlen) === 0;
347 }