[44] | 1 | <?php
|
---|
| 2 | /**
|
---|
| 3 | ** A base module for [checkbox], [checkbox*], and [radio]
|
---|
| 4 | **/
|
---|
| 5 |
|
---|
| 6 | /* Shortcode handler */
|
---|
| 7 |
|
---|
| 8 | function wpcf7_checkbox_shortcode_handler( $tag ) {
|
---|
| 9 | global $wpcf7_contact_form;
|
---|
| 10 |
|
---|
| 11 | if ( ! is_array( $tag ) )
|
---|
| 12 | return '';
|
---|
| 13 |
|
---|
| 14 | $type = $tag['type'];
|
---|
| 15 | $name = $tag['name'];
|
---|
| 16 | $options = (array) $tag['options'];
|
---|
| 17 | $values = (array) $tag['values'];
|
---|
| 18 |
|
---|
| 19 | if ( empty( $name ) )
|
---|
| 20 | return '';
|
---|
| 21 |
|
---|
| 22 | $atts = '';
|
---|
| 23 | $id_att = '';
|
---|
| 24 | $class_att = '';
|
---|
| 25 |
|
---|
| 26 | $defaults = array();
|
---|
| 27 |
|
---|
| 28 | $label_first = false;
|
---|
| 29 | $use_label_element = false;
|
---|
| 30 |
|
---|
| 31 | if ( 'checkbox*' == $type )
|
---|
| 32 | $class_att .= ' wpcf7-validates-as-required';
|
---|
| 33 |
|
---|
| 34 | if ( 'checkbox' == $type || 'checkbox*' == $type )
|
---|
| 35 | $class_att .= ' wpcf7-checkbox';
|
---|
| 36 |
|
---|
| 37 | if ( 'radio' == $type )
|
---|
| 38 | $class_att .= ' wpcf7-radio';
|
---|
| 39 |
|
---|
| 40 | foreach ( $options as $option ) {
|
---|
| 41 | if ( preg_match( '%^id:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) {
|
---|
| 42 | $id_att = $matches[1];
|
---|
| 43 |
|
---|
| 44 | } elseif ( preg_match( '%^class:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) {
|
---|
| 45 | $class_att .= ' ' . $matches[1];
|
---|
| 46 |
|
---|
| 47 | } elseif ( preg_match( '/^default:([0-9_]+)$/', $option, $matches ) ) {
|
---|
| 48 | $defaults = explode( '_', $matches[1] );
|
---|
| 49 |
|
---|
| 50 | } elseif ( preg_match( '%^label[_-]?first$%', $option ) ) {
|
---|
| 51 | $label_first = true;
|
---|
| 52 |
|
---|
| 53 | } elseif ( preg_match( '%^use[_-]?label[_-]?element$%', $option ) ) {
|
---|
| 54 | $use_label_element = true;
|
---|
| 55 |
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | if ( $id_att )
|
---|
| 60 | $atts .= ' id="' . trim( $id_att ) . '"';
|
---|
| 61 |
|
---|
| 62 | if ( $class_att )
|
---|
| 63 | $atts .= ' class="' . trim( $class_att ) . '"';
|
---|
| 64 |
|
---|
| 65 | $multiple = preg_match( '/^checkbox[*]?$/', $type ) && ! preg_grep( '%^exclusive$%', $options );
|
---|
| 66 |
|
---|
| 67 | $html = '';
|
---|
| 68 |
|
---|
| 69 | if ( preg_match( '/^checkbox[*]?$/', $type ) && ! $multiple && WPCF7_LOAD_JS )
|
---|
| 70 | $onclick = ' onclick="wpcf7ExclusiveCheckbox(this);"';
|
---|
| 71 |
|
---|
| 72 | $input_type = rtrim( $type, '*' );
|
---|
| 73 |
|
---|
| 74 | $posted = is_a( $wpcf7_contact_form, 'WPCF7_ContactForm' ) && $wpcf7_contact_form->is_posted();
|
---|
| 75 |
|
---|
| 76 | foreach ( $values as $key => $value ) {
|
---|
| 77 | $checked = false;
|
---|
| 78 |
|
---|
| 79 | if ( in_array( $key + 1, (array) $defaults ) )
|
---|
| 80 | $checked = true;
|
---|
| 81 |
|
---|
| 82 | if ( $posted) {
|
---|
| 83 | if ( $multiple && in_array( esc_sql( $value ), (array) $_POST[$name] ) )
|
---|
| 84 | $checked = true;
|
---|
| 85 | if ( ! $multiple && $_POST[$name] == esc_sql( $value ) )
|
---|
| 86 | $checked = true;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | $checked = $checked ? ' checked="checked"' : '';
|
---|
| 90 |
|
---|
| 91 | if ( is_array( $tag['labels'] ) && isset( $tag['labels'][$key] ) )
|
---|
| 92 | $label = $tag['labels'][$key];
|
---|
| 93 | else
|
---|
| 94 | $label = $value;
|
---|
| 95 |
|
---|
| 96 | if ( $label_first ) { // put label first, input last
|
---|
| 97 | $item = '<span class="wpcf7-list-item-label">' . esc_html( $label ) . '</span> ';
|
---|
| 98 | $item .= '<input type="' . $input_type . '" name="' . $name . ( $multiple ? '[]' : '' ) . '" value="' . esc_attr( $value ) . '"' . $checked . $onclick . ' />';
|
---|
| 99 | } else {
|
---|
| 100 | $item = '<input type="' . $input_type . '" name="' . $name . ( $multiple ? '[]' : '' ) . '" value="' . esc_attr( $value ) . '"' . $checked . $onclick . ' />';
|
---|
| 101 | $item .= ' <span class="wpcf7-list-item-label">' . esc_html( $label ) . '</span>';
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | if ( $use_label_element )
|
---|
| 105 | $item = '<label>' . $item . '</label>';
|
---|
| 106 |
|
---|
| 107 | $item = '<span class="wpcf7-list-item">' . $item . '</span>';
|
---|
| 108 | $html .= $item;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | $html = '<span' . $atts . '>' . $html . '</span>';
|
---|
| 112 |
|
---|
| 113 | $validation_error = '';
|
---|
| 114 | if ( is_a( $wpcf7_contact_form, 'WPCF7_ContactForm' ) )
|
---|
| 115 | $validation_error = $wpcf7_contact_form->validation_error( $name );
|
---|
| 116 |
|
---|
| 117 | $html = '<span class="wpcf7-form-control-wrap ' . $name . '">' . $html . $validation_error . '</span>';
|
---|
| 118 |
|
---|
| 119 | return $html;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | wpcf7_add_shortcode( 'checkbox', 'wpcf7_checkbox_shortcode_handler', true );
|
---|
| 123 | wpcf7_add_shortcode( 'checkbox*', 'wpcf7_checkbox_shortcode_handler', true );
|
---|
| 124 | wpcf7_add_shortcode( 'radio', 'wpcf7_checkbox_shortcode_handler', true );
|
---|
| 125 |
|
---|
| 126 |
|
---|
| 127 | /* Validation filter */
|
---|
| 128 |
|
---|
| 129 | function wpcf7_checkbox_validation_filter( $result, $tag ) {
|
---|
| 130 | global $wpcf7_contact_form;
|
---|
| 131 |
|
---|
| 132 | $type = $tag['type'];
|
---|
| 133 | $name = $tag['name'];
|
---|
| 134 | $values = $tag['values'];
|
---|
| 135 |
|
---|
| 136 | if ( is_array( $_POST[$name] ) ) {
|
---|
| 137 | foreach ( $_POST[$name] as $key => $value ) {
|
---|
| 138 | $value = stripslashes( $value );
|
---|
| 139 | if ( ! in_array( $value, (array) $values ) ) // Not in given choices.
|
---|
| 140 | unset( $_POST[$name][$key] );
|
---|
| 141 | }
|
---|
| 142 | } else {
|
---|
| 143 | $value = stripslashes( $_POST[$name] );
|
---|
| 144 | if ( ! in_array( $value, (array) $values ) ) // Not in given choices.
|
---|
| 145 | $_POST[$name] = '';
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | if ( 'checkbox*' == $type ) {
|
---|
| 149 | if ( empty( $_POST[$name] ) ) {
|
---|
| 150 | $result['valid'] = false;
|
---|
| 151 | $result['reason'][$name] = $wpcf7_contact_form->message( 'invalid_required' );
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | return $result;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | add_filter( 'wpcf7_validate_checkbox', 'wpcf7_checkbox_validation_filter', 10, 2 );
|
---|
| 159 | add_filter( 'wpcf7_validate_checkbox*', 'wpcf7_checkbox_validation_filter', 10, 2 );
|
---|
| 160 | add_filter( 'wpcf7_validate_radio', 'wpcf7_checkbox_validation_filter', 10, 2 );
|
---|
| 161 |
|
---|
| 162 | ?> |
---|