|
<?php |
|
|
|
if ( ! function_exists( 'wpcf7_add_form_tag_a11ycheckbox_radio' ) ) { |
|
/** |
|
* Customize Contact Form 7 (5.1.1) |
|
* Freely adapted from contact-form-7/modules/checkbox.php by Takayuki Miyoshi to include id/for attributes for better accessibility. |
|
* These functions create new shortcodes: a11ycheckbox, a11ycheckbox* and a11yradio. |
|
* @link https://contactform7.com/2015/01/10/adding-a-custom-form-tag/ |
|
*/ |
|
add_action( 'wpcf7_init', 'wpcf7_add_form_tag_a11ycheckbox_radio' ); |
|
|
|
function wpcf7_add_form_tag_a11ycheckbox_radio() { |
|
wpcf7_add_form_tag( array( 'a11ycheckbox', 'a11ycheckbox*', 'a11yradio'), |
|
'wpcf7_add_form_tag_a11y_handler', |
|
array( |
|
'name-attr' => true, |
|
'selectable-values' => true, |
|
'multiple-controls-container' => true, |
|
) |
|
); |
|
} |
|
|
|
function wpcf7_add_form_tag_a11y_handler( $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'; |
|
} |
|
|
|
$label_first = $tag->has_option( 'label_first' ); |
|
$use_label_element = $tag->has_option( 'use_label_element' ); |
|
$exclusive = $tag->has_option( 'exclusive' ); |
|
$free_text = $tag->has_option( 'free_text' ); |
|
$multiple = false; |
|
|
|
if ( 'a11ycheckbox' == $tag->basetype ) { |
|
$multiple = ! $exclusive; |
|
} else { // radio |
|
$exclusive = false; |
|
} |
|
|
|
if ( $exclusive ) { |
|
$class .= ' wpcf7-exclusive-checkbox'; |
|
} |
|
|
|
$atts = array(); |
|
|
|
$atts['class'] = $tag->get_class_option( $class ); |
|
$atts['id'] = $tag->get_id_option(); |
|
|
|
$html = ''; |
|
$count = 0; |
|
|
|
if ( $data = (array) $tag->get_data_option() ) { |
|
if ( $free_text ) { |
|
$tag->values = array_merge( |
|
array_slice( $tag->values, 0, -1 ), |
|
array_values( $data ), |
|
array_slice( $tag->values, -1 ) ); |
|
$tag->labels = array_merge( |
|
array_slice( $tag->labels, 0, -1 ), |
|
array_values( $data ), |
|
array_slice( $tag->labels, -1 ) ); |
|
} else { |
|
$tag->values = array_merge( $tag->values, array_values( $data ) ); |
|
$tag->labels = array_merge( $tag->labels, array_values( $data ) ); |
|
} |
|
} |
|
|
|
$values = $tag->values; |
|
$labels = $tag->labels; |
|
|
|
$default_choice = $tag->get_default_option( null, array( |
|
'multiple' => $multiple, |
|
) ); |
|
|
|
$hangover = wpcf7_get_hangover( $tag->name, $multiple ? array() : '' ); |
|
|
|
foreach ( $values as $key => $value ) { |
|
if ( $hangover ) { |
|
$checked = in_array( $value, (array) $hangover, true ); |
|
} else { |
|
$checked = in_array( $value, (array) $default_choice, true ); |
|
} |
|
|
|
if ( isset( $labels[$key] ) ) { |
|
$label = $labels[$key]; |
|
} else { |
|
$label = $value; |
|
} |
|
|
|
$item_atts = array( |
|
'type' => str_replace('a11y', '', $tag->basetype), |
|
'name' => $tag->name . ( $multiple ? '[]' : '' ), |
|
'value' => $value, |
|
'checked' => $checked ? 'checked' : '', |
|
|
|
// Add ID to each input |
|
'id' => $tag->basetype . '_' . '_' . $tag->name . '_' . $key |
|
); |
|
|
|
$item_atts = wpcf7_format_atts( $item_atts ); |
|
|
|
if ( $label_first ) { // put label first, input last |
|
$item = sprintf( |
|
'<span class="wpcf7-list-item-label">%1$s</span><input %2$s />', |
|
esc_html( $label ), $item_atts ); |
|
} else { |
|
$item = sprintf( |
|
'<input %2$s /><span class="wpcf7-list-item-label">%1$s</span>', |
|
esc_html( $label ), $item_atts ); |
|
} |
|
|
|
if ( $use_label_element ) { |
|
$item = '<label for="'. $tag->basetype . '_' . '_' . $tag->name . '_' . $key .'">' . $item . '</label>'; |
|
} |
|
|
|
$class = 'wpcf7-list-item'; |
|
$count += 1; |
|
|
|
if ( 1 == $count ) { |
|
$class .= ' first'; |
|
} |
|
|
|
if ( count( $values ) == $count ) { // last round |
|
$class .= ' last'; |
|
} |
|
|
|
$item = '<span class="' . esc_attr( $class ) . '">' . $item . '</span>'; |
|
$html .= $item; |
|
} |
|
|
|
$atts = wpcf7_format_atts( $atts ); |
|
|
|
$html = sprintf( |
|
'<span class="wpcf7-form-control-wrap %1$s"><span %2$s>%3$s</span>%4$s</span>', |
|
sanitize_html_class( $tag->name ), $atts, $html, $validation_error ); |
|
|
|
return $html; |
|
} |
|
|
|
/* Validation filter for the newly created radio and checkbox fields */ |
|
|
|
add_filter( 'wpcf7_validate_a11ycheckbox', 'wpcf7_a42_checkbox_validation_filter', 10, 2 ); |
|
add_filter( 'wpcf7_validate_a11ycheckbox*', 'wpcf7_a42_checkbox_validation_filter', 10, 2 ); |
|
add_filter( 'wpcf7_validate_a11yradio', 'wpcf7_a42_checkbox_validation_filter', 10, 2 ); |
|
|
|
function wpcf7_a42_checkbox_validation_filter( $result, $tag ) { |
|
$name = $tag->name; |
|
$is_required = $tag->is_required() || 'a11yradio' == str_replace('a11y', '', $tag->type); |
|
$value = isset( $_POST[$name] ) ? (array) $_POST[$name] : array(); |
|
|
|
if ( $is_required and empty( $value ) ) { |
|
$result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) ); |
|
} |
|
|
|
return $result; |
|
} |
|
} |