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 0.2.2 0.2.3
cf7-conditional-fields / cf7cf.php
cf7-conditional-fields Last commit date
js 7 years ago admin-style.css 7 years ago admin-style.css.map 7 years ago admin-style.scss 7 years ago admin.php 7 years ago cf7cf.php 7 years ago contact-form-7-conditional-fields.php 7 years ago init.php 7 years ago readme.txt 7 years ago style.css 7 years ago tg_pane_group.php 7 years ago wpcf7cf-options.php 7 years ago
cf7cf.php
368 lines
1 <?php
2
3 class ContactForm7ConditionalFields {
4 private $hidden_fields = array();
5 private $visible_groups = array();
6 private $hidden_groups = array();
7
8 function __construct() {
9
10 // can't use wpcf7_enqueue_scripts hook, because it's possible that people
11 // want to disable the CF7 scripts. but in this case Conditional fields should still work.
12 // add_action('wpcf7_enqueue_scripts', array(__CLASS__, 'enqueue_js')); // <-- don't use this
13 add_action('wpcf7_contact_form', array(__CLASS__, 'enqueue_js'));
14 add_action('wpcf7_enqueue_styles', array(__CLASS__, 'enqueue_css'));
15
16 // Register shortcodes
17 add_action('wpcf7_init', array(__CLASS__, 'add_shortcodes'));
18
19 // Tag generator
20 add_action('load-contact_page_wpcf7-new', array(__CLASS__, 'tag_generator'));
21 add_action('load-toplevel_page_wpcf7', array(__CLASS__, 'tag_generator'));
22
23 // compatibility with CF7 multi-step forms by Webhead LLC.
24 add_filter( 'wpcf7_posted_data', array($this,'cf7msm_merge_post_with_cookie'), 8, 1 );
25
26 // compatibility with CF7 Multi Step by NinjaTeam https://wordpress.org/plugins/cf7-multi-step/
27 add_action('wp_ajax_cf7mls_validation', array($this,'cf7mls_validation_callback'),9);
28 add_action('wp_ajax_nopriv_cf7mls_validation', array($this,'cf7mls_validation_callback'),9);
29
30 // check which fields are hidden during form submission and change some stuff accordingly
31 add_filter( 'wpcf7_posted_data', array($this, 'remove_hidden_post_data') );
32
33 add_filter( 'wpcf7_validate', array($this, 'skip_validation_for_hidden_fields'), 2, 2 );
34
35 // validation messages
36 add_action('wpcf7_config_validator_validate', array($this,'wpcf7cf_config_validator_validate'));
37
38
39 add_action("wpcf7_before_send_mail", [$this, 'hide_hidden_mail_fields'], 10, 3);
40
41 register_activation_hook(__FILE__, array($this, 'activate'));
42
43 if (is_admin()) {
44 require_once dirname(__FILE__) . '/admin.php';
45 }
46 }
47
48 /**
49 * Suppress invalid mailbox syntax errors on fields that contain existing conditional
50 */
51 function wpcf7cf_config_validator_validate(WPCF7_ConfigValidator $wpcf7_config_validator) {
52
53 // TODO: For now we kill every syntax error once a [groupname] tag is detected.
54 // Ideally, this function should check each string inside the group for invalid syntax.
55 // 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.
56
57 $cf = $wpcf7_config_validator->contact_form();
58 $all_group_tags = $cf->scan_form_tags();
59
60 foreach ($wpcf7_config_validator->collect_error_messages() as $err_type => $err) {
61
62
63 $parts = explode('.',$err_type);
64 $property = $parts[0];
65 $sub_prop = $parts[1];
66 $prop_val = $cf->prop($property)[$sub_prop];
67
68
69 // TODO 2: Dirty hack. Because of TODO 2 we are just going to kill the error message if we detect the string '[/'
70 // Start removing here.
71 if (strpos($prop_val, '[/') !== false) {
72 $wpcf7_config_validator->remove_error($err_type, WPCF7_ConfigValidator::error_invalid_mailbox_syntax);
73 continue;
74 }
75 // TODO 2: Stop removing here. and uncomment code below.
76
77 // foreach ($all_group_tags as $form_tag) {
78 // if (strpos($prop_val, '['.$form_tag->name.']') !== false) {
79 // $wpcf7_config_validator->remove_error($err_type, WPCF7_ConfigValidator::error_invalid_mailbox_syntax);
80 // }
81 // }
82
83 }
84
85 return new WPCF7_ConfigValidator($wpcf7_config_validator->contact_form());
86 }
87
88 function activate() {
89 //add options with add_option and stuff
90 }
91
92 public static function enqueue_js() {
93 if (is_admin()) return;
94 wp_enqueue_script('wpcf7cf-scripts', plugins_url('js/scripts.js', __FILE__), array('jquery'), WPCF7CF_VERSION, true);
95 }
96
97 public static function enqueue_css() {
98 wp_enqueue_style('cf7cf-style', plugins_url('style.css', __FILE__), array(), WPCF7CF_VERSION);
99 }
100
101 public static function add_shortcodes() {
102 if (function_exists('wpcf7_add_form_tag'))
103 wpcf7_add_form_tag('group', array(__CLASS__, 'shortcode_handler'), true);
104 else if (function_exists('wpcf7_add_shortcode')) {
105 wpcf7_add_shortcode('group', array(__CLASS__, 'shortcode_handler'), true);
106 } else {
107 throw new Exception('functions wpcf7_add_form_tag and wpcf7_add_shortcode not found.');
108 }
109 }
110
111 function group_shortcode_handler( $atts, $content = "" ) {
112 return $content;
113 }
114
115 public static function shortcode_handler($tag) {
116 //$tag = new WPCF7_Shortcode($tag);
117 $tag = new WPCF7_FormTag($tag);
118 //ob_start();
119 //print_r($tag);
120 //return print_r($tag, true);
121 return $tag->content;
122 }
123
124
125 public static function tag_generator() {
126 if (! function_exists( 'wpcf7_add_tag_generator'))
127 return;
128
129 wpcf7_add_tag_generator('group',
130 __('Conditional fields Group', 'wpcf7cf'),
131 'wpcf7-tg-pane-group',
132 array(__CLASS__, 'tg_pane')
133 );
134
135 do_action('wpcf7cf_tag_generator');
136 }
137
138 static function tg_pane( $contact_form, $args = '' ) {
139 $args = wp_parse_args( $args, array() );
140 $type = 'group';
141
142 $description = __( "Generate a group tag to group form elements that can be shown conditionally.", 'cf7cf' );
143
144 include 'tg_pane_group.php';
145 }
146
147 /**
148 * Remove validation requirements for fields that are hidden at the time of form submission.
149 * Called using add_filter( 'wpcf7_validate_[tag_type]', array($this, 'skip_validation_for_hidden_fields'), 2, 2 );
150 * where the priority of 2 causes this to kill any validations with a priority higher than 2
151 *
152 * @param $result
153 * @param $tag
154 *
155 * @return mixed
156 */
157 function skip_validation_for_hidden_fields($result, $tags) {
158
159 if (count($this->hidden_fields) == 0) return $result;
160
161 $return_result = new WPCF7_Validation();
162
163 $invalid_fields = $result->get_invalid_fields();
164
165 if (!is_array($invalid_fields) || count($invalid_fields) == 0) return $result;
166
167 foreach ($invalid_fields as $invalid_field_key => $invalid_field_data) {
168 if (!in_array($invalid_field_key, $this->hidden_fields)) {
169 // the invalid field is not a hidden field, so we'll add it to the final validation result
170 $return_result->invalidate($invalid_field_key, $invalid_field_data['reason']);
171 }
172 }
173
174 return $return_result;
175 }
176
177
178 /**
179 * When a CF7 form is posted, check the form for hidden fields, then remove those fields from the post data
180 *
181 * @param $posted_data
182 *
183 * @return mixed
184 */
185 function remove_hidden_post_data($posted_data) {
186 $this->set_hidden_fields_arrays($posted_data);
187
188 // TODO: activating the code below will change the behaviour of the plugin, as all hidden fields will not be POSTed.
189 // We might consider activating this based on a setting in the future. But for now, we're not gonna use it.
190
191 // foreach( $this->hidden_fields as $name => $value ) {
192 // unset( $posted_data[$value] ); // Yes, it should be $value, not $name. https://github.com/pwkip/contact-form-7-conditional-fields/pull/17
193 // }
194
195 return $posted_data;
196 }
197
198 function cf7msm_merge_post_with_cookie($posted_data) {
199
200 if (!function_exists('cf7msm_get') || !key_exists('cf7msm_posted_data',$_COOKIE)) return $posted_data;
201
202 if (!$posted_data) {
203 $posted_data = WPCF7_Submission::get_instance()->get_posted_data();
204 }
205
206 // this will temporarily set the hidden fields data to the posted_data.
207 // later this function will be called again with the updated posted_data
208 $this->set_hidden_fields_arrays($posted_data);
209
210 // get cookie data
211 $cookie_data = cf7msm_get('cf7msm_posted_data');
212 $cookie_data_hidden_group_fields = json_decode(stripslashes($cookie_data['_wpcf7cf_hidden_group_fields']));
213 $cookie_data_hidden_groups = json_decode(stripslashes($cookie_data['_wpcf7cf_hidden_groups']));
214 $cookie_data_visible_groups = json_decode(stripslashes($cookie_data['_wpcf7cf_visible_groups']));
215
216 // remove all the currently posted data from the cookie data (we don't wanna add it twice)
217 $cookie_data_hidden_group_fields = array_diff($cookie_data_hidden_group_fields, array_keys($posted_data));
218 $cookie_data_hidden_groups = array_diff((array) $cookie_data_hidden_groups, $this->hidden_groups, $this->visible_groups);
219 $cookie_data_visible_groups = array_diff((array) $cookie_data_visible_groups, $this->hidden_groups, $this->visible_groups);
220
221 // update current post data with cookie data
222 $posted_data['_wpcf7cf_hidden_group_fields'] = addslashes(json_encode(array_merge((array) $cookie_data_hidden_group_fields, $this->hidden_fields)));
223 $posted_data['_wpcf7cf_hidden_groups'] = addslashes(json_encode(array_merge((array) $cookie_data_hidden_groups, $this->hidden_groups)));
224 $posted_data['_wpcf7cf_visible_groups'] = addslashes(json_encode(array_merge((array) $cookie_data_visible_groups, $this->visible_groups)));
225
226 return $posted_data;
227 }
228
229 // compatibility with CF7 Multi Step by NinjaTeam https://wordpress.org/plugins/cf7-multi-step/
230 function cf7mls_validation_callback() {
231 $this->set_hidden_fields_arrays($_POST);
232 }
233
234 /**
235 * Finds the currently submitted form and set the hidden_fields variables accoringly
236 *
237 * @param bool|array $posted_data
238 */
239 function set_hidden_fields_arrays($posted_data = false) {
240
241 if (!$posted_data) {
242 $posted_data = WPCF7_Submission::get_instance()->get_posted_data();
243 }
244
245 $hidden_fields = json_decode(stripslashes($posted_data['_wpcf7cf_hidden_group_fields']));
246 if (is_array($hidden_fields) && count($hidden_fields) > 0) {
247 foreach ($hidden_fields as $field) {
248 $this->hidden_fields[] = $field;
249 if (wpcf7cf_endswith($field, '[]')) {
250 $this->hidden_fields[] = substr($field,0,strlen($field)-2);
251 }
252 }
253 }
254 $this->hidden_groups = json_decode(stripslashes($posted_data['_wpcf7cf_hidden_groups']));
255 $this->visible_groups = json_decode(stripslashes($posted_data['_wpcf7cf_visible_groups']));
256 }
257
258 function hide_hidden_mail_fields($form,$abort,$submission) {
259 $props = $form->get_properties();
260 $mails = ['mail','mail_2','messages'];
261 foreach ($mails as $mail) {
262 foreach ($props[$mail] as $key=>$val) {
263 $props[$mail][$key] = preg_replace_callback(WPCF7CF_REGEX_MAIL_GROUP, array($this, 'hide_hidden_mail_fields_regex_callback'), $val );
264 }
265 }
266 $form->set_properties($props);
267 }
268
269 function hide_hidden_mail_fields_regex_callback ( $matches ) {
270 $name = $matches[1];
271 $content = $matches[2];
272 if ( in_array( $name, $this->hidden_groups ) ) {
273 // The tag name represents a hidden group, so replace everything from [tagname] to [/tagname] with nothing
274 return '';
275 } elseif ( in_array( $name, $this->visible_groups ) ) {
276 // The tag name represents a visible group, so remove the tags themselves, but return everything else
277 // instead of just returning the $content, return the preg_replaced content :)
278 return preg_replace_callback(WPCF7CF_REGEX_MAIL_GROUP, array($this, 'hide_hidden_mail_fields_regex_callback'), $content );
279 } else {
280 // The tag name doesn't represent a group that was used in the form. Leave it alone (return the entire match).
281 return $matches[0];
282 }
283 }
284 }
285
286 new ContactForm7ConditionalFields;
287
288 add_filter( 'wpcf7_contact_form_properties', 'wpcf7cf_properties', 10, 2 );
289
290 function wpcf7cf_properties($properties, $wpcf7form) {
291 // 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.
292 // 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)
293 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.
294 $form = $properties['form'];
295
296 $form_parts = preg_split('/(\[\/?group(?:\]|\s.*?\]))/',$form, -1,PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
297
298 ob_start();
299
300 $stack = array();
301
302 foreach ($form_parts as $form_part) {
303 if (substr($form_part,0,7) == '[group ') {
304 $tag_parts = explode(' ',rtrim($form_part,']'));
305
306 array_shift($tag_parts);
307
308 $tag_id = $tag_parts[0];
309 $tag_html_type = 'div';
310 $tag_html_data = array();
311
312 foreach ($tag_parts as $i => $tag_part) {
313 if ($i==0) continue;
314 else if ($tag_part == 'inline') $tag_html_type = 'span';
315 else if ($tag_part == 'clear_on_hide') $tag_html_data[] = 'data-clear_on_hide';
316 }
317
318 array_push($stack,$tag_html_type);
319
320 echo '<'.$tag_html_type.' data-id="'.$tag_id.'" '.implode(' ',$tag_html_data).' data-class="wpcf7cf_group">';
321 } else if ($form_part == '[/group]') {
322 echo '</'.array_pop($stack).'>';
323 } else {
324 echo $form_part;
325 }
326 }
327
328 $properties['form'] = ob_get_clean();
329 }
330 return $properties;
331 }
332
333 add_action('wpcf7_form_hidden_fields', 'wpcf7cf_form_hidden_fields',10,1);
334
335 function wpcf7cf_form_hidden_fields($hidden_fields) {
336
337 $current_form = wpcf7_get_current_contact_form();
338 $current_form_id = $current_form->id();
339
340 $options = array(
341 'form_id' => $current_form_id,
342 'conditions' => get_post_meta($current_form_id,'wpcf7cf_options', true),
343 'settings' => get_option(WPCF7CF_OPTIONS)
344 );
345
346 unset($options['settings']['license_key']); // don't show license key in the source code duh.
347
348 return array_merge($hidden_fields, array(
349 '_wpcf7cf_hidden_group_fields' => '',
350 '_wpcf7cf_hidden_groups' => '',
351 '_wpcf7cf_visible_groups' => '',
352 '_wpcf7cf_options' => ''.json_encode($options),
353 ));
354 }
355
356 function wpcf7cf_endswith($string, $test) {
357 $strlen = strlen($string);
358 $testlen = strlen($test);
359 if ($testlen > $strlen) return false;
360 return substr_compare($string, $test, $strlen - $testlen, $testlen) === 0;
361 }
362
363 add_filter( 'wpcf7_form_tag_data_option', 'wpcf7cf_form_tag_data_option', 10, 3 );
364
365 function wpcf7cf_form_tag_data_option($output, $args, $nog) {
366 $data = array();
367 return $data;
368 }