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