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