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 0.2.2 0.2.3
cf7-conditional-fields / cf7cf.php
cf7-conditional-fields Last commit date
js 5 years ago jsdoc-out 5 years ago Wpcf7cfMailParser.php 5 years ago admin-style.css 6 years ago admin-style.css.map 6 years ago admin-style.scss 6 years ago admin.php 5 years ago cf7cf.php 5 years ago contact-form-7-conditional-fields.php 5 years ago init.php 5 years ago readme.txt 5 years ago style.css 6 years ago tg_pane_group.php 5 years ago wpcf7cf-options.php 5 years ago
cf7cf.php
511 lines
1 <?php
2
3 class CF7CF {
4 private $hidden_fields = array();
5 private $visible_groups = array();
6 private $hidden_groups = array();
7 private $repeaters = array();
8
9 function __construct() {
10
11 // can't use wpcf7_enqueue_scripts hook, because it's possible that people
12 // want to disable the CF7 scripts. but in this case Conditional fields should still work.
13 // add_action('wpcf7_enqueue_scripts', array(__CLASS__, 'enqueue_js')); // <-- don't use this
14
15 // Enqueue_scripts moved to function outside class.
16
17 // add_action('wp_enqueue_scripts', array(__CLASS__, 'enqueue_js'), 20);
18 // add_action('wpcf7_enqueue_styles', array(__CLASS__, 'enqueue_css'));
19
20 // Register shortcodes
21 add_action('wpcf7_init', array(__CLASS__, 'add_shortcodes'));
22
23 // Tag generator
24 add_action('admin_init', array(__CLASS__, 'tag_generator'), 590);
25
26 // compatibility with CF7 multi-step forms by Webhead LLC.
27 add_filter( 'wpcf7_posted_data', array($this,'cf7msm_merge_post_with_cookie'), 8, 1 );
28
29 // compatibility with CF7 Multi Step by NinjaTeam https://wordpress.org/plugins/cf7-multi-step/
30 add_action('wp_ajax_cf7mls_validation', array($this,'cf7mls_validation_callback'),9);
31 add_action('wp_ajax_nopriv_cf7mls_validation', array($this,'cf7mls_validation_callback'),9);
32
33 add_filter( 'wpcf7_validate', array($this, 'skip_validation_for_hidden_fields'), 2, 2 );
34
35 add_filter( 'wpcf7_validate_file*', array($this, 'skip_validation_for_hidden_file_field'), 30, 3);
36 add_filter( 'wpcf7_validate_multifile*', array($this, 'skip_validation_for_hidden_file_field'), 30, 3);
37
38 // validation messages
39 add_action('wpcf7_config_validator_validate', array($this,'wpcf7cf_config_validator_validate'));
40
41
42 add_action("wpcf7_before_send_mail", [$this, 'hide_hidden_mail_fields'], 10, 3);
43
44 register_activation_hook(__FILE__, array($this, 'activate'));
45
46 if (is_admin()) {
47 require_once dirname(__FILE__) . '/admin.php';
48 }
49 }
50
51
52
53 /**
54 * Suppress invalid mailbox syntax errors on fields that contain existing conditional
55 */
56 function wpcf7cf_config_validator_validate(WPCF7_ConfigValidator $wpcf7_config_validator) {
57
58 // TODO: For now we kill every syntax error once a [groupname] tag is detected.
59 // Ideally, this function should check each string inside the group for invalid syntax.
60 // TODO 2: ajax validation not working yet, because $cf->scan_form_tags() does not seem to contain group tags if it's an ajax request. Need to investigate.
61
62 $cf = $wpcf7_config_validator->contact_form();
63 $all_group_tags = $cf->scan_form_tags();
64
65 foreach ($wpcf7_config_validator->collect_error_messages() as $err_type => $err) {
66
67 // print_r($err_type);
68
69 $parts = explode('.',$err_type);
70
71 $property = $parts[0];
72
73 if ($property == 'form') continue; // the 'form' field can be safely validated by CF7. No need to suppress it.
74
75 $sub_prop = $parts[1];
76 $prop_val = $cf->prop($property)[$sub_prop];
77
78
79 // TODO 2: Dirty hack. Because of TODO 2 we are just going to kill the error message if we detect the string '[/'
80 // Start removing here.
81 if (strpos($prop_val, '[/') !== false) {
82 $wpcf7_config_validator->remove_error($err_type, WPCF7_ConfigValidator::error_invalid_mailbox_syntax);
83 continue;
84 }
85 // TODO 2: Stop removing here. and uncomment code below.
86
87 // foreach ($all_group_tags as $form_tag) {
88 // if (strpos($prop_val, '['.$form_tag->name.']') !== false) {
89 // $wpcf7_config_validator->remove_error($err_type, WPCF7_ConfigValidator::error_invalid_mailbox_syntax);
90 // }
91 // }
92
93 }
94
95 return new WPCF7_ConfigValidator($wpcf7_config_validator->contact_form());
96 }
97
98 function activate() {
99 //add options with add_option and stuff
100 }
101
102 public static function add_shortcodes() {
103 if (function_exists('wpcf7_add_form_tag'))
104 wpcf7_add_form_tag('group', array(__CLASS__, 'shortcode_handler'), true);
105 else if (function_exists('wpcf7_add_shortcode')) {
106 wpcf7_add_shortcode('group', array(__CLASS__, 'shortcode_handler'), true);
107 } else {
108 throw new Exception('functions wpcf7_add_form_tag and wpcf7_add_shortcode not found.');
109 }
110 }
111
112 function group_shortcode_handler( $atts, $content = "" ) {
113 return $content;
114 }
115
116 public static function shortcode_handler($tag) {
117 //$tag = new WPCF7_Shortcode($tag);
118 $tag = new WPCF7_FormTag($tag);
119 //ob_start();
120 //print_r($tag);
121 //return print_r($tag, true);
122 return $tag->content;
123 }
124
125
126 public static function tag_generator() {
127 if (! function_exists( 'wpcf7_add_tag_generator'))
128 return;
129
130 wpcf7_add_tag_generator('group',
131 __('Conditional Fields Group', 'cf7-conditional-fields'),
132 'wpcf7-tg-pane-group',
133 array(__CLASS__, 'tg_pane')
134 );
135
136 do_action('wpcf7cf_tag_generator');
137 }
138
139 static function tg_pane( $contact_form, $args = '' ) {
140 $args = wp_parse_args( $args, array() );
141 $type = 'group';
142
143 $description = __( "Generate a group tag to group form elements that can be shown conditionally.", 'cf7-conditional-fields' );
144
145 include 'tg_pane_group.php';
146 }
147
148 /**
149 * Remove validation requirements for fields that are hidden at the time of form submission.
150 * Required/invalid fields should never trigger validation errors if they are inside a hidden group during submission.
151 * Called using add_filter( 'wpcf7_validate_[tag_type]', array($this, 'skip_validation_for_hidden_fields'), 2, 2 );
152 * where the priority of 2 causes this to kill any validations with a priority higher than 2
153 *
154 * @param $result
155 * @param $tag
156 *
157 * @return mixed
158 */
159 function skip_validation_for_hidden_fields($result, $tags, $args = []) {
160
161 if(isset($_POST)) {
162 $this->set_hidden_fields_arrays($_POST);
163 }
164
165 $invalid_fields = $result->get_invalid_fields();
166 $return_result = new WPCF7_Validation();
167
168 if (count($this->hidden_fields) == 0 || !is_array($invalid_fields) || count($invalid_fields) == 0) {
169 $return_result = $result;
170 } else {
171 foreach ($invalid_fields as $invalid_field_key => $invalid_field_data) {
172 if (!in_array($invalid_field_key, $this->hidden_fields)) {
173 // the invalid field is not a hidden field, so we'll add it to the final validation result
174 $return_result->invalidate($invalid_field_key, $invalid_field_data['reason']);
175 }
176 }
177 }
178
179 return apply_filters('wpcf7cf_validate', $return_result, $tags);
180
181 }
182
183 /**
184 * Does the same thing as skip_validation_for_hidden_fields, but CF7 will check files again later
185 * via the wpcf7_unship_uploaded_files function
186 * so we need to skip validation a second time for individual file fields
187 */
188 function skip_validation_for_hidden_file_field($result, $tag, $args=[]) {
189 return $this->skip_validation_for_hidden_fields( $result, [ $tag ] );
190 }
191
192 function cf7msm_merge_post_with_cookie($posted_data) {
193
194 if (!function_exists('cf7msm_get') || !key_exists('cf7msm_posted_data',$_COOKIE)) return $posted_data;
195
196 if (!$posted_data) {
197 $posted_data = WPCF7_Submission::get_instance()->get_posted_data();
198 }
199
200 // this will temporarily set the hidden fields data to the posted_data.
201 // later this function will be called again with the updated posted_data
202 $this->set_hidden_fields_arrays($_POST);
203
204 // get cookie data
205 $cookie_data = cf7msm_get('cf7msm_posted_data');
206 $cookie_data_hidden_group_fields = json_decode(stripslashes($cookie_data['_wpcf7cf_hidden_group_fields']));
207 $cookie_data_hidden_groups = json_decode(stripslashes($cookie_data['_wpcf7cf_hidden_groups']));
208 $cookie_data_visible_groups = json_decode(stripslashes($cookie_data['_wpcf7cf_visible_groups']));
209
210 // remove all the currently posted data from the cookie data (we don't wanna add it twice)
211 $cookie_data_hidden_group_fields = array_diff($cookie_data_hidden_group_fields, array_keys($posted_data));
212 $cookie_data_hidden_groups = array_diff((array) $cookie_data_hidden_groups, $this->hidden_groups, $this->visible_groups);
213 $cookie_data_visible_groups = array_diff((array) $cookie_data_visible_groups, $this->hidden_groups, $this->visible_groups);
214
215 // update current post data with cookie data
216 $posted_data['_wpcf7cf_hidden_group_fields'] = addslashes(json_encode(array_merge((array) $cookie_data_hidden_group_fields, $this->hidden_fields)));
217 $posted_data['_wpcf7cf_hidden_groups'] = addslashes(json_encode(array_merge((array) $cookie_data_hidden_groups, $this->hidden_groups)));
218 $posted_data['_wpcf7cf_visible_groups'] = addslashes(json_encode(array_merge((array) $cookie_data_visible_groups, $this->visible_groups)));
219
220 return $posted_data;
221 }
222
223 // compatibility with CF7 Multi Step by NinjaTeam https://wordpress.org/plugins/cf7-multi-step/
224 function cf7mls_validation_callback() {
225 $this->set_hidden_fields_arrays($_POST);
226 }
227
228 /**
229 * Finds the currently submitted form and set the hidden_fields variables accoringly
230 *
231 * @param bool|array $posted_data
232 */
233 function set_hidden_fields_arrays($posted_data = false) {
234
235 if (!$posted_data) $posted_data = $_POST;
236
237 $hidden_fields = json_decode(stripslashes($posted_data['_wpcf7cf_hidden_group_fields']));
238 if (is_array($hidden_fields) && count($hidden_fields) > 0) {
239 foreach ($hidden_fields as $field) {
240 $this->hidden_fields[] = $field;
241 if (wpcf7cf_endswith($field, '[]')) {
242 $this->hidden_fields[] = substr($field,0,strlen($field)-2);
243 }
244 }
245 }
246 $this->hidden_groups = json_decode(stripslashes($posted_data['_wpcf7cf_hidden_groups']));
247 $this->visible_groups = json_decode(stripslashes($posted_data['_wpcf7cf_visible_groups']));
248 $this->repeaters = json_decode(stripslashes($posted_data['_wpcf7cf_repeaters']));
249 $this->steps = json_decode(stripslashes($posted_data['_wpcf7cf_steps']));
250 }
251
252 function hide_hidden_mail_fields($form,$abort,$submission) {
253 $props = $form->get_properties();
254 $mails = ['mail','mail_2','messages'];
255 foreach ($mails as $mail) {
256 if (!is_array($props[$mail])) { continue; }
257 foreach ($props[$mail] as $key=>$val) {
258
259 $parser = new Wpcf7cfMailParser($val, $this->visible_groups, $this->hidden_groups, $this->repeaters, $_POST);
260
261 // $props[$mail][$key] = preg_replace_callback(WPCF7CF_REGEX_MAIL_GROUP, array($this, 'hide_hidden_mail_fields_regex_callback'), $val );
262 $props[$mail][$key] = $parser->getParsedMail();
263 }
264
265
266 }
267
268
269 //$props['mail']['body'] = 'xxx';
270 $form->set_properties($props);
271 }
272
273 function hide_hidden_mail_fields_regex_callback ( $matches ) {
274 $name = $matches[1];
275 $content = $matches[2];
276 if ( in_array( $name, $this->hidden_groups ) ) {
277 // The tag name represents a hidden group, so replace everything from [tagname] to [/tagname] with nothing
278 return '';
279 } elseif ( in_array( $name, $this->visible_groups ) ) {
280 // The tag name represents a visible group, so remove the tags themselves, but return everything else
281 // instead of just returning the $content, return the preg_replaced content :)
282 return preg_replace_callback(WPCF7CF_REGEX_MAIL_GROUP, array($this, 'hide_hidden_mail_fields_regex_callback'), $content );
283 } else {
284 // The tag name doesn't represent a group that was used in the form. Leave it alone (return the entire match).
285 return $matches[0];
286 }
287 }
288
289 public static function parse_conditions($string, $format='array') {
290 // Parse stuff like "show [g1] if [field] equals 2" to Array
291
292 preg_match_all(WPCF7CF_REGEX_CONDITIONS, $string, $matches);
293
294 $conditions = [];
295
296 $prev_then_field = '';
297 foreach ($matches[0] as $i=>$line) {
298 $then_field = $matches[1][$i];
299 $if_field = $matches[2][$i];
300 $operator = $matches[3][$i];
301 $if_value = $matches[4][$i];
302
303 $index = count($conditions);
304
305 if ($then_field == '') {
306 $index = $index -1;
307 $then_field = $prev_then_field;
308 } else {
309 $conditions[$index]['then_field'] = $then_field;
310 }
311
312 $conditions[$index]['and_rules'][] = [
313 'if_field' => $if_field,
314 'operator' => $operator,
315 'if_value' => $if_value,
316 ];
317
318 $prev_then_field = $then_field;
319
320 }
321
322 $conditions = array_values($conditions);
323
324 if ($format == 'array') {
325 return $conditions;
326 } else if ($format == 'json') {
327 return json_encode($conditions);
328 }
329 }
330
331 /**
332 * load the conditions from the form's post_meta
333 *
334 * @param string $form_id
335 * @return void
336 */
337 public static function getConditions($form_id) {
338 // make sure conditions are an array.
339 $options = get_post_meta($form_id,'wpcf7cf_options',true);
340 return is_array($options) ? $options : array(); // the meta key 'wpcf7cf_options' is a bit misleading at this point, because it only holds the form's conditions, no other options/settings
341 }
342
343 /**
344 * load the conditions from the form's post_meta as plain text
345 *
346 * @param string $form_id
347 * @return void
348 */
349 public static function getConditionsPlainText($form_id) {
350 return CF7CF::serializeConditions(CF7CF::getConditions($form_id));
351 }
352
353 public static function serializeConditions($array) {
354
355 $lines = [];
356
357 foreach ($array as $entry) {
358 $then_field = $entry['then_field'];
359 $and_rules = $entry['and_rules'];
360 $indent = strlen($then_field) + 4;
361 foreach ($and_rules as $i => $rule) {
362 $if_field = $rule['if_field'];
363 $operator = $rule['operator'];
364 $if_value = $rule['if_value'];
365
366 if ($i == 0) {
367 $lines[] = "show [${then_field}] if [${if_field}] ${operator} \"${if_value}\"";
368 } else {
369 $lines[] = str_repeat(' ',$indent)."and if [${if_field}] ${operator} \"${if_value}\"";
370 }
371 }
372 }
373
374 return implode("\n", $lines);
375 }
376
377 /**
378 * save the conditions to the form's post_meta
379 *
380 * @param string $form_id
381 * @return void
382 */
383 public static function setConditions($form_id, $conditions) {
384 return update_post_meta($form_id,'wpcf7cf_options',$conditions); // the meta key 'wpcf7cf_options' is a bit misleading at this point, because it only holds the form's conditions, no other options/settings
385 }
386 }
387
388 new CF7CF;
389
390 add_filter( 'wpcf7_contact_form_properties', 'wpcf7cf_properties', 10, 2 );
391
392 function wpcf7cf_properties($properties, $wpcf7form) {
393 // TODO: This function is called serveral times. The problem is that the filter is called each time we call get_properties() on a contact form.
394 // TODO: I haven't found a better way to solve this problem yet, any suggestions or push requests are welcome. (same problem in PRO/repeater.php)
395 if (!is_admin() || (defined('DOING_AJAX') && DOING_AJAX)) { // TODO: kind of hacky. maybe find a better solution. Needed because otherwise the group tags will be replaced in the editor as well.
396 $form = $properties['form'];
397
398 $form_parts = preg_split('/(\[\/?group(?:\]|\s.*?\]))/',$form, -1,PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
399
400 ob_start();
401
402 $stack = array();
403
404 foreach ($form_parts as $form_part) {
405 if (substr($form_part,0,7) == '[group ') {
406 $tag_parts = explode(' ',rtrim($form_part,']'));
407
408 array_shift($tag_parts);
409
410 $tag_id = $tag_parts[0];
411 $tag_html_type = 'div';
412 $tag_html_data = array();
413
414 foreach ($tag_parts as $i => $tag_part) {
415 if ($i==0) continue;
416 else if ($tag_part == 'inline') $tag_html_type = 'span';
417 else if ($tag_part == 'clear_on_hide') $tag_html_data[] = 'data-clear_on_hide';
418 else if ($tag_part == 'disable_on_hide' && WPCF7CF_IS_PRO) $tag_html_data[] = 'data-disable_on_hide';
419 }
420
421 array_push($stack,$tag_html_type);
422
423 echo '<'.$tag_html_type.' data-id="'.$tag_id.'" data-orig_data_id="'.$tag_id.'" '.implode(' ',$tag_html_data).' data-class="wpcf7cf_group">';
424 } else if ($form_part == '[/group]') {
425 echo '</'.array_pop($stack).'>';
426 } else {
427 echo $form_part;
428 }
429 }
430
431 $properties['form'] = ob_get_clean();
432 }
433 return $properties;
434 }
435
436 add_action('wpcf7_form_hidden_fields', 'wpcf7cf_form_hidden_fields',10,1);
437
438 function wpcf7cf_form_hidden_fields($hidden_fields) {
439
440 $current_form = wpcf7_get_current_contact_form();
441 $current_form_id = $current_form->id();
442
443 $options = array(
444 'form_id' => $current_form_id,
445 'conditions' => CF7CF::getConditions($current_form_id),
446 'settings' => wpcf7cf_get_settings()
447 );
448
449 unset($options['settings']['license_key']); // don't show license key in the source code duh.
450
451 return array_merge($hidden_fields, array(
452 '_wpcf7cf_hidden_group_fields' => '',
453 '_wpcf7cf_hidden_groups' => '',
454 '_wpcf7cf_visible_groups' => '',
455 '_wpcf7cf_repeaters' => '[]',
456 '_wpcf7cf_steps' => '{}',
457 '_wpcf7cf_options' => ''.json_encode($options),
458 ));
459 }
460
461 function wpcf7cf_endswith($string, $test) {
462 $strlen = strlen($string);
463 $testlen = strlen($test);
464 if ($testlen > $strlen) return false;
465 return substr_compare($string, $test, $strlen - $testlen, $testlen) === 0;
466 }
467
468 add_filter( 'wpcf7_form_tag_data_option', 'wpcf7cf_form_tag_data_option', 10, 3 );
469
470 function wpcf7cf_form_tag_data_option($output, $args, $nog) {
471 $data = array();
472 return $data;
473 }
474
475 /* Scripts & Styles */
476
477 function wpcf7cf_load_js() {
478 return apply_filters( 'wpcf7cf_load_js', WPCF7CF_LOAD_JS );
479 }
480
481 function wpcf7cf_load_css() {
482 return apply_filters( 'wpcf7cf_load_css', WPCF7CF_LOAD_CSS );
483 }
484
485 add_action( 'wp_enqueue_scripts', 'wpcf7cf_do_enqueue_scripts', 20, 0 );
486
487 function wpcf7cf_do_enqueue_scripts() {
488 if ( wpcf7cf_load_js() ) {
489 wpcf7cf_enqueue_scripts();
490 }
491
492 if ( wpcf7cf_load_css() ) {
493 wpcf7cf_enqueue_styles();
494 }
495 }
496
497 function wpcf7cf_enqueue_scripts() {
498 if (is_admin()) return;
499 wp_enqueue_script('wpcf7cf-scripts', plugins_url('js/scripts.js', __FILE__), array('jquery'), WPCF7CF_VERSION, true);
500 wp_localize_script('wpcf7cf-scripts', 'wpcf7cf_global_settings',
501 array(
502 'ajaxurl' => admin_url('admin-ajax.php'),
503 )
504 );
505
506 }
507
508 function wpcf7cf_enqueue_styles() {
509 if (is_admin()) return;
510 wp_enqueue_style('cf7cf-style', plugins_url('style.css', __FILE__), array(), WPCF7CF_VERSION);
511 }