function wpcf7_text_form_tag_handler($tag) { $tag = new WPCF7_FormTag($tag); if (empty($tag->name)) { return ''; } $validation_error = wpcf7_get_validation_error($tag->name); $class = wpcf7_form_controls_class($tag->type, 'wpcf7-text'); if (in_array($tag->basetype, array('email', 'url', 'tel'))) { $class .= ' wpcf7-validates-as-' . $tag->basetype; } if ($validation_error) { $class .= ' wpcf7-not-valid'; } $atts = array(); $atts['size'] = $tag->get_size_option('40'); $atts['maxlength'] = $tag->get_maxlength_option(); $atts['minlength'] = $tag->get_minlength_option(); if ($atts['maxlength'] && $atts['minlength'] && $atts['maxlength'] < $atts['minlength']) { unset($atts['maxlength'], $atts['minlength']); } $atts['class'] = $tag->get_class_option($class); $atts['id'] = $tag->get_id_option(); $atts['tabindex'] = $tag->get_option('tabindex', 'int', true); $atts['autocomplete'] = $tag->get_option('autocomplete', '[-0-9a-zA-Z]+', true); if ($tag->has_option('readonly')) { $atts['readonly'] = 'readonly'; } if ($tag->is_required()) { $atts['aria-required'] = 'true'; } $atts['aria-invalid'] = $validation_error ? 'true' : 'false'; $value = (string) reset($tag->values); if ($tag->has_option('placeholder') || $tag->has_option('watermark')) { $atts['placeholder'] = $value; $value = ''; } $value = $tag->get_default_option($value); $value = wpcf7_get_hangover($tag->name, $value); $atts['value'] = $value; if (wpcf7_support_html5()) { $atts['type'] = $tag->basetype; } else { $atts['type'] = 'text'; } $atts['name'] = $tag->name; $atts = wpcf7_format_atts($atts); $html = sprintf('<span class="wpcf7-form-control-wrap %1$s"><input %2$s />%3$s</span>', sanitize_html_class($tag->name), $atts, $validation_error); return $html; }
function wpcf7_number_form_tag_handler($tag) { $tag = new WPCF7_FormTag($tag); if (empty($tag->name)) { return ''; } $validation_error = wpcf7_get_validation_error($tag->name); $class = wpcf7_form_controls_class($tag->type); $class .= ' wpcf7-validates-as-number'; if ($validation_error) { $class .= ' wpcf7-not-valid'; } $atts = array(); $atts['class'] = $tag->get_class_option($class); $atts['id'] = $tag->get_id_option(); $atts['tabindex'] = $tag->get_option('tabindex', 'int', true); $atts['min'] = $tag->get_option('min', 'signed_int', true); $atts['max'] = $tag->get_option('max', 'signed_int', true); $atts['step'] = $tag->get_option('step', 'int', true); if ($tag->has_option('readonly')) { $atts['readonly'] = 'readonly'; } if ($tag->is_required()) { $atts['aria-required'] = 'true'; } $atts['aria-invalid'] = $validation_error ? 'true' : 'false'; $value = (string) reset($tag->values); if ($tag->has_option('placeholder') || $tag->has_option('watermark')) { $atts['placeholder'] = $value; $value = ''; } $value = $tag->get_default_option($value); $value = wpcf7_get_hangover($tag->name, $value); $atts['value'] = $value; if (wpcf7_support_html5()) { $atts['type'] = $tag->basetype; } else { $atts['type'] = 'text'; } $atts['name'] = $tag->name; $atts = wpcf7_format_atts($atts); $html = sprintf('<span class="wpcf7-form-control-wrap %1$s"><input %2$s />%3$s</span>', sanitize_html_class($tag->name), $atts, $validation_error); return $html; }
function wpcf7_textarea_form_tag_handler($tag) { $tag = new WPCF7_FormTag($tag); if (empty($tag->name)) { return ''; } $validation_error = wpcf7_get_validation_error($tag->name); $class = wpcf7_form_controls_class($tag->type); if ($validation_error) { $class .= ' wpcf7-not-valid'; } $atts = array(); $atts['cols'] = $tag->get_cols_option('40'); $atts['rows'] = $tag->get_rows_option('10'); $atts['maxlength'] = $tag->get_maxlength_option(); $atts['minlength'] = $tag->get_minlength_option(); if ($atts['maxlength'] && $atts['minlength'] && $atts['maxlength'] < $atts['minlength']) { unset($atts['maxlength'], $atts['minlength']); } $atts['class'] = $tag->get_class_option($class); $atts['id'] = $tag->get_id_option(); $atts['tabindex'] = $tag->get_option('tabindex', 'int', true); $atts['autocomplete'] = $tag->get_option('autocomplete', '[-0-9a-zA-Z]+', true); if ($tag->has_option('readonly')) { $atts['readonly'] = 'readonly'; } if ($tag->is_required()) { $atts['aria-required'] = 'true'; } $atts['aria-invalid'] = $validation_error ? 'true' : 'false'; $value = empty($tag->content) ? (string) reset($tag->values) : $tag->content; if ($tag->has_option('placeholder') || $tag->has_option('watermark')) { $atts['placeholder'] = $value; $value = ''; } $value = $tag->get_default_option($value); $value = wpcf7_get_hangover($tag->name, $value); $atts['name'] = $tag->name; $atts = wpcf7_format_atts($atts); $html = sprintf('<span class="wpcf7-form-control-wrap %1$s"><textarea %2$s>%3$s</textarea>%4$s</span>', sanitize_html_class($tag->name), $atts, esc_textarea($value), $validation_error); return $html; }
function wpcf7_acceptance_validation_filter($result, $tag) { if (!wpcf7_acceptance_as_validation()) { return $result; } $tag = new WPCF7_FormTag($tag); $name = $tag->name; $value = !empty($_POST[$name]) ? 1 : 0; $invert = $tag->has_option('invert'); if ($invert && $value || !$invert && !$value) { $result->invalidate($tag, wpcf7_get_message('accept_terms')); } return $result; }
function wpcf7_count_form_tag_handler($tag) { $tag = new WPCF7_FormTag($tag); if (empty($tag->name)) { return ''; } $targets = wpcf7_scan_form_tags(array('name' => $tag->name)); $maxlength = $minlength = null; while ($targets) { $target = array_shift($targets); $target = new WPCF7_FormTag($target); if ('count' != $target->type) { $maxlength = $target->get_maxlength_option(); $minlength = $target->get_minlength_option(); break; } } if ($maxlength && $minlength && $maxlength < $minlength) { $maxlength = $minlength = null; } if ($tag->has_option('down')) { $value = (int) $maxlength; $class = 'wpcf7-character-count down'; } else { $value = '0'; $class = 'wpcf7-character-count up'; } $atts = array(); $atts['id'] = $tag->get_id_option(); $atts['class'] = $tag->get_class_option($class); $atts['data-target-name'] = $tag->name; $atts['data-starting-value'] = $value; $atts['data-current-value'] = $value; $atts['data-maximum-value'] = $maxlength; $atts['data-minimum-value'] = $minlength; $atts = wpcf7_format_atts($atts); $html = sprintf('<span %1$s>%2$s</span>', $atts, $value); return $html; }
function wpcf7_select_form_tag_handler($tag) { $tag = new WPCF7_FormTag($tag); if (empty($tag->name)) { return ''; } $validation_error = wpcf7_get_validation_error($tag->name); $class = wpcf7_form_controls_class($tag->type); if ($validation_error) { $class .= ' wpcf7-not-valid'; } $atts = array(); $atts['class'] = $tag->get_class_option($class); $atts['id'] = $tag->get_id_option(); $atts['tabindex'] = $tag->get_option('tabindex', 'int', true); if ($tag->is_required()) { $atts['aria-required'] = 'true'; } $atts['aria-invalid'] = $validation_error ? 'true' : 'false'; $multiple = $tag->has_option('multiple'); $include_blank = $tag->has_option('include_blank'); $first_as_label = $tag->has_option('first_as_label'); $values = $tag->values; $labels = $tag->labels; if ($data = (array) $tag->get_data_option()) { $values = array_merge($values, array_values($data)); $labels = array_merge($labels, array_values($data)); } $defaults = array(); $default_choice = $tag->get_default_option(null, 'multiple=1'); foreach ($default_choice as $value) { $key = array_search($value, $values, true); if (false !== $key) { $defaults[] = (int) $key + 1; } } if ($matches = $tag->get_first_match_option('/^default:([0-9_]+)$/')) { $defaults = array_merge($defaults, explode('_', $matches[1])); } $defaults = array_unique($defaults); $shifted = false; if ($include_blank || empty($values)) { array_unshift($labels, '---'); array_unshift($values, ''); $shifted = true; } elseif ($first_as_label) { $values[0] = ''; } $html = ''; $hangover = wpcf7_get_hangover($tag->name); foreach ($values as $key => $value) { $selected = false; if ($hangover) { if ($multiple) { $selected = in_array(esc_sql($value), (array) $hangover); } else { $selected = $hangover == esc_sql($value); } } else { if (!$shifted && in_array((int) $key + 1, (array) $defaults)) { $selected = true; } elseif ($shifted && in_array((int) $key, (array) $defaults)) { $selected = true; } } $item_atts = array('value' => $value, 'selected' => $selected ? 'selected' : ''); $item_atts = wpcf7_format_atts($item_atts); $label = isset($labels[$key]) ? $labels[$key] : $value; $html .= sprintf('<option %1$s>%2$s</option>', $item_atts, esc_html($label)); } if ($multiple) { $atts['multiple'] = 'multiple'; } $atts['name'] = $tag->name . ($multiple ? '[]' : ''); $atts = wpcf7_format_atts($atts); $html = sprintf('<span class="wpcf7-form-control-wrap %1$s"><select %2$s>%3$s</select>%4$s</span>', sanitize_html_class($tag->name), $atts, $html, $validation_error); return $html; }
function wpcf7_checkbox_posted_data($posted_data) { $tags = wpcf7_scan_form_tags(array('type' => array('checkbox', 'checkbox*', 'radio'))); if (empty($tags)) { return $posted_data; } foreach ($tags as $tag) { $tag = new WPCF7_FormTag($tag); if (!isset($posted_data[$tag->name])) { continue; } $posted_items = (array) $posted_data[$tag->name]; if ($tag->has_option('free_text')) { if (WPCF7_USE_PIPE) { $values = $tag->pipes->collect_afters(); } else { $values = $tag->values; } $last = array_pop($values); $last = html_entity_decode($last, ENT_QUOTES, 'UTF-8'); if (in_array($last, $posted_items)) { $posted_items = array_diff($posted_items, array($last)); $free_text_name = sprintf('_wpcf7_%1$s_free_text_%2$s', $tag->basetype, $tag->name); $free_text = $posted_data[$free_text_name]; if (!empty($free_text)) { $posted_items[] = trim($last . ' ' . $free_text); } else { $posted_items[] = $last; } } } $posted_data[$tag->name] = $posted_items; } return $posted_data; }
public function validate_form() { $body = $this->contact_form->prop('form'); $pattern = '%<label(?:[ \\t\\n]+.*?)?>(.+?)</label>%s'; if (preg_match_all($pattern, $body, $matches)) { $manager = WPCF7_FormTagsManager::get_instance(); foreach ($matches[1] as $insidelabel) { $tags = $manager->scan($insidelabel); $fields_count = 0; foreach ($tags as $tag) { $tag = new WPCF7_FormTag($tag); if (in_array($tag->basetype, array('checkbox', 'radio'))) { $fields_count += count($tag->values); if ($tag->has_option('free_text')) { $fields_count += 1; } } elseif (!empty($tag->name)) { $fields_count += 1; } if (1 < $fields_count) { $this->add_error('form.body', self::error_multiple_controls_in_label, array('link' => __('http://contactform7.com/configuration-errors/#form.body:error_multiple_controls_in_label', 'contact-form-7'))); return; } } } } }
function wpcf7_captcha_form_tag_handler($tag) { $tag = new WPCF7_FormTag($tag); if ('captchac' == $tag->type && !class_exists('ReallySimpleCaptcha')) { return '<em>' . __('To use CAPTCHA, you need <a href="http://wordpress.org/extend/plugins/really-simple-captcha/">Really Simple CAPTCHA</a> plugin installed.', 'contact-form-7') . '</em>'; } if (empty($tag->name)) { return ''; } $validation_error = wpcf7_get_validation_error($tag->name); $class = wpcf7_form_controls_class($tag->type); if ('captchac' == $tag->type) { // CAPTCHA-Challenge (image) $class .= ' wpcf7-captcha-' . $tag->name; $atts = array(); $atts['class'] = $tag->get_class_option($class); $atts['id'] = $tag->get_id_option(); $op = array('img_size' => array(72, 24), 'base' => array(6, 18), 'font_size' => 14, 'font_char_width' => 15); $op = array_merge($op, wpcf7_captchac_options($tag->options)); if (!($filename = wpcf7_generate_captcha($op))) { return ''; } if (!empty($op['img_size'])) { if (isset($op['img_size'][0])) { $atts['width'] = $op['img_size'][0]; } if (isset($op['img_size'][1])) { $atts['height'] = $op['img_size'][1]; } } $atts['alt'] = 'captcha'; $atts['src'] = wpcf7_captcha_url($filename); $atts = wpcf7_format_atts($atts); $prefix = substr($filename, 0, strrpos($filename, '.')); $html = sprintf('<input type="hidden" name="_wpcf7_captcha_challenge_%1$s" value="%2$s" /><img %3$s />', $tag->name, $prefix, $atts); return $html; } elseif ('captchar' == $tag->type) { // CAPTCHA-Response (input) if ($validation_error) { $class .= ' wpcf7-not-valid'; } $atts = array(); $atts['size'] = $tag->get_size_option('40'); $atts['maxlength'] = $tag->get_maxlength_option(); $atts['minlength'] = $tag->get_minlength_option(); if ($atts['maxlength'] && $atts['minlength'] && $atts['maxlength'] < $atts['minlength']) { unset($atts['maxlength'], $atts['minlength']); } $atts['class'] = $tag->get_class_option($class); $atts['id'] = $tag->get_id_option(); $atts['tabindex'] = $tag->get_option('tabindex', 'int', true); $atts['autocomplete'] = 'off'; $atts['aria-invalid'] = $validation_error ? 'true' : 'false'; $value = (string) reset($tag->values); if (wpcf7_is_posted()) { $value = ''; } if ($tag->has_option('placeholder') || $tag->has_option('watermark')) { $atts['placeholder'] = $value; $value = ''; } $atts['value'] = $value; $atts['type'] = 'text'; $atts['name'] = $tag->name; $atts = wpcf7_format_atts($atts); $html = sprintf('<span class="wpcf7-form-control-wrap %1$s"><input %2$s />%3$s</span>', sanitize_html_class($tag->name), $atts, $validation_error); return $html; } }