akismet
11 months ago
constant-contact
9 months ago
recaptcha
9 months ago
sendinblue
9 months ago
stripe
9 months ago
turnstile
9 months ago
acceptance.php
11 months ago
checkbox.php
9 months ago
count.php
1 year ago
date.php
9 months ago
disallowed-list.php
9 months ago
doi-helper.php
4 years ago
file.php
11 months ago
flamingo.php
9 months ago
hidden.php
7 years ago
listo.php
2 years ago
number.php
9 months ago
quiz.php
11 months ago
really-simple-captcha.php
9 months ago
reflection.php
3 years ago
response.php
6 years ago
select.php
9 months ago
submit.php
11 months ago
text.php
9 months ago
textarea.php
11 months ago
checkbox.php
442 lines
| 1 | <?php |
| 2 | /** |
| 3 | ** A base module for [checkbox], [checkbox*], and [radio] |
| 4 | **/ |
| 5 | |
| 6 | /* form_tag handler */ |
| 7 | |
| 8 | add_action( 'wpcf7_init', 'wpcf7_add_form_tag_checkbox', 10, 0 ); |
| 9 | |
| 10 | function wpcf7_add_form_tag_checkbox() { |
| 11 | wpcf7_add_form_tag( array( 'checkbox', 'checkbox*', 'radio' ), |
| 12 | 'wpcf7_checkbox_form_tag_handler', |
| 13 | array( |
| 14 | 'name-attr' => true, |
| 15 | 'selectable-values' => true, |
| 16 | 'multiple-controls-container' => true, |
| 17 | ) |
| 18 | ); |
| 19 | } |
| 20 | |
| 21 | function wpcf7_checkbox_form_tag_handler( $tag ) { |
| 22 | if ( empty( $tag->name ) ) { |
| 23 | return ''; |
| 24 | } |
| 25 | |
| 26 | $validation_error = wpcf7_get_validation_error( $tag->name ); |
| 27 | |
| 28 | $class = wpcf7_form_controls_class( $tag->type ); |
| 29 | |
| 30 | if ( $validation_error ) { |
| 31 | $class .= ' wpcf7-not-valid'; |
| 32 | } |
| 33 | |
| 34 | $label_first = $tag->has_option( 'label_first' ); |
| 35 | $use_label_element = $tag->has_option( 'use_label_element' ); |
| 36 | $exclusive = $tag->has_option( 'exclusive' ); |
| 37 | $free_text = $tag->has_option( 'free_text' ); |
| 38 | $multiple = false; |
| 39 | |
| 40 | if ( 'checkbox' === $tag->basetype ) { |
| 41 | $multiple = ! $exclusive; |
| 42 | } else { // radio |
| 43 | $exclusive = false; |
| 44 | } |
| 45 | |
| 46 | if ( $exclusive ) { |
| 47 | $class .= ' wpcf7-exclusive-checkbox'; |
| 48 | } |
| 49 | |
| 50 | $atts = array(); |
| 51 | |
| 52 | $atts['class'] = $tag->get_class_option( $class ); |
| 53 | $atts['id'] = $tag->get_id_option(); |
| 54 | |
| 55 | if ( $validation_error ) { |
| 56 | $atts['aria-describedby'] = wpcf7_get_validation_error_reference( |
| 57 | $tag->name |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | $tabindex = $tag->get_option( 'tabindex', 'signed_int', true ); |
| 62 | |
| 63 | if ( false !== $tabindex ) { |
| 64 | $tabindex = (int) $tabindex; |
| 65 | } |
| 66 | |
| 67 | $html = ''; |
| 68 | $count = 0; |
| 69 | |
| 70 | if ( $data = (array) $tag->get_data_option() ) { |
| 71 | if ( $free_text ) { |
| 72 | $tag->values = array_merge( |
| 73 | array_slice( $tag->values, 0, -1 ), |
| 74 | array_values( $data ), |
| 75 | array_slice( $tag->values, -1 ) |
| 76 | ); |
| 77 | |
| 78 | $tag->labels = array_merge( |
| 79 | array_slice( $tag->labels, 0, -1 ), |
| 80 | array_values( $data ), |
| 81 | array_slice( $tag->labels, -1 ) |
| 82 | ); |
| 83 | } else { |
| 84 | $tag->values = array_merge( $tag->values, array_values( $data ) ); |
| 85 | $tag->labels = array_merge( $tag->labels, array_values( $data ) ); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | $values = $tag->values; |
| 90 | $labels = $tag->labels; |
| 91 | |
| 92 | $default_choice = $tag->get_default_option( null, array( |
| 93 | 'multiple' => $multiple, |
| 94 | ) ); |
| 95 | |
| 96 | $hangover = wpcf7_get_hangover( $tag->name, $multiple ? array() : '' ); |
| 97 | |
| 98 | foreach ( $values as $key => $value ) { |
| 99 | if ( $hangover ) { |
| 100 | $checked = in_array( $value, (array) $hangover, true ); |
| 101 | } else { |
| 102 | $checked = in_array( $value, (array) $default_choice, true ); |
| 103 | } |
| 104 | |
| 105 | if ( isset( $labels[$key] ) ) { |
| 106 | $label = $labels[$key]; |
| 107 | } else { |
| 108 | $label = $value; |
| 109 | } |
| 110 | |
| 111 | $item_atts = array( |
| 112 | 'type' => $tag->basetype, |
| 113 | 'name' => $tag->name . ( $multiple ? '[]' : '' ), |
| 114 | 'value' => $value, |
| 115 | 'checked' => $checked, |
| 116 | 'tabindex' => $tabindex, |
| 117 | ); |
| 118 | |
| 119 | $item_atts = wpcf7_format_atts( $item_atts ); |
| 120 | |
| 121 | if ( $label_first ) { // put label first, input last |
| 122 | $item = sprintf( |
| 123 | '<span class="wpcf7-list-item-label">%1$s</span><input %2$s />', |
| 124 | esc_html( $label ), |
| 125 | $item_atts |
| 126 | ); |
| 127 | } else { |
| 128 | $item = sprintf( |
| 129 | '<input %2$s /><span class="wpcf7-list-item-label">%1$s</span>', |
| 130 | esc_html( $label ), |
| 131 | $item_atts |
| 132 | ); |
| 133 | } |
| 134 | |
| 135 | if ( $use_label_element ) { |
| 136 | $item = '<label>' . $item . '</label>'; |
| 137 | } |
| 138 | |
| 139 | if ( false !== $tabindex and 0 < $tabindex ) { |
| 140 | $tabindex += 1; |
| 141 | } |
| 142 | |
| 143 | $class = 'wpcf7-list-item'; |
| 144 | $count += 1; |
| 145 | |
| 146 | if ( 1 === $count ) { |
| 147 | $class .= ' first'; |
| 148 | } |
| 149 | |
| 150 | if ( count( $values ) === $count ) { // last round |
| 151 | $class .= ' last'; |
| 152 | |
| 153 | if ( $free_text ) { |
| 154 | $free_text_name = sprintf( '_wpcf7_free_text_%s', $tag->name ); |
| 155 | |
| 156 | $free_text_atts = array( |
| 157 | 'type' => 'text', |
| 158 | 'name' => $free_text_name, |
| 159 | 'class' => 'wpcf7-free-text', |
| 160 | 'tabindex' => $tabindex, |
| 161 | ); |
| 162 | |
| 163 | if ( wpcf7_is_posted() ) { |
| 164 | $free_text_atts['value'] = wpcf7_superglobal_post( $free_text_name ); |
| 165 | } |
| 166 | |
| 167 | $item .= sprintf( |
| 168 | ' <input %s />', |
| 169 | wpcf7_format_atts( $free_text_atts ) |
| 170 | ); |
| 171 | |
| 172 | $class .= ' has-free-text'; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | $item = '<span class="' . esc_attr( $class ) . '">' . $item . '</span>'; |
| 177 | $html .= $item; |
| 178 | } |
| 179 | |
| 180 | $html = sprintf( |
| 181 | '<span class="wpcf7-form-control-wrap" data-name="%1$s"><span %2$s>%3$s</span>%4$s</span>', |
| 182 | esc_attr( $tag->name ), |
| 183 | wpcf7_format_atts( $atts ), |
| 184 | $html, |
| 185 | $validation_error |
| 186 | ); |
| 187 | |
| 188 | return $html; |
| 189 | } |
| 190 | |
| 191 | |
| 192 | add_action( |
| 193 | 'wpcf7_swv_create_schema', |
| 194 | 'wpcf7_swv_add_checkbox_rules', |
| 195 | 10, 2 |
| 196 | ); |
| 197 | |
| 198 | function wpcf7_swv_add_checkbox_rules( $schema, $contact_form ) { |
| 199 | $tags = $contact_form->scan_form_tags( array( |
| 200 | 'basetype' => array( 'checkbox', 'radio' ), |
| 201 | ) ); |
| 202 | |
| 203 | foreach ( $tags as $tag ) { |
| 204 | if ( $tag->is_required() or 'radio' === $tag->type ) { |
| 205 | $schema->add_rule( |
| 206 | wpcf7_swv_create_rule( 'required', array( |
| 207 | 'field' => $tag->name, |
| 208 | 'error' => wpcf7_get_message( 'invalid_required' ), |
| 209 | ) ) |
| 210 | ); |
| 211 | } |
| 212 | |
| 213 | if ( 'radio' === $tag->type or $tag->has_option( 'exclusive' ) ) { |
| 214 | $schema->add_rule( |
| 215 | wpcf7_swv_create_rule( 'maxitems', array( |
| 216 | 'field' => $tag->name, |
| 217 | 'threshold' => 1, |
| 218 | 'error' => $contact_form->filter_message( |
| 219 | __( 'Too many items are selected.', 'contact-form-7' ) |
| 220 | ), |
| 221 | ) ) |
| 222 | ); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | |
| 228 | add_action( |
| 229 | 'wpcf7_swv_create_schema', |
| 230 | 'wpcf7_swv_add_checkbox_enum_rules', |
| 231 | 20, 2 |
| 232 | ); |
| 233 | |
| 234 | function wpcf7_swv_add_checkbox_enum_rules( $schema, $contact_form ) { |
| 235 | $tags = $contact_form->scan_form_tags( array( |
| 236 | 'basetype' => array( 'checkbox', 'radio' ), |
| 237 | ) ); |
| 238 | |
| 239 | $values = array_reduce( |
| 240 | $tags, |
| 241 | function ( $values, $tag ) { |
| 242 | if ( $tag->has_option( 'free_text' ) ) { |
| 243 | $values[$tag->name] = 'free_text'; |
| 244 | } |
| 245 | |
| 246 | if ( |
| 247 | isset( $values[$tag->name] ) and |
| 248 | ! is_array( $values[$tag->name] ) // Maybe 'free_text' |
| 249 | ) { |
| 250 | return $values; |
| 251 | } |
| 252 | |
| 253 | if ( ! isset( $values[$tag->name] ) ) { |
| 254 | $values[$tag->name] = array(); |
| 255 | } |
| 256 | |
| 257 | $tag_values = array_merge( |
| 258 | (array) $tag->values, |
| 259 | (array) $tag->get_data_option() |
| 260 | ); |
| 261 | |
| 262 | $values[$tag->name] = array_merge( |
| 263 | $values[$tag->name], |
| 264 | $tag_values |
| 265 | ); |
| 266 | |
| 267 | return $values; |
| 268 | }, |
| 269 | array() |
| 270 | ); |
| 271 | |
| 272 | foreach ( $values as $field => $field_values ) { |
| 273 | if ( ! is_array( $field_values ) ) { // Maybe 'free_text' |
| 274 | continue; |
| 275 | } |
| 276 | |
| 277 | $field_values = array_map( |
| 278 | static function ( $value ) { |
| 279 | return html_entity_decode( |
| 280 | (string) $value, |
| 281 | ENT_QUOTES | ENT_HTML5, |
| 282 | 'UTF-8' |
| 283 | ); |
| 284 | }, |
| 285 | $field_values |
| 286 | ); |
| 287 | |
| 288 | $field_values = array_filter( |
| 289 | array_unique( $field_values ), |
| 290 | static function ( $value ) { |
| 291 | return '' !== $value; |
| 292 | } |
| 293 | ); |
| 294 | |
| 295 | $schema->add_rule( |
| 296 | wpcf7_swv_create_rule( 'enum', array( |
| 297 | 'field' => $field, |
| 298 | 'accept' => array_values( $field_values ), |
| 299 | 'error' => $contact_form->filter_message( |
| 300 | __( 'Undefined value was submitted through this field.', 'contact-form-7' ) |
| 301 | ), |
| 302 | ) ) |
| 303 | ); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | |
| 308 | add_filter( 'wpcf7_posted_data_checkbox', |
| 309 | 'wpcf7_posted_data_checkbox', |
| 310 | 10, 3 |
| 311 | ); |
| 312 | |
| 313 | add_filter( 'wpcf7_posted_data_checkbox*', |
| 314 | 'wpcf7_posted_data_checkbox', |
| 315 | 10, 3 |
| 316 | ); |
| 317 | |
| 318 | add_filter( 'wpcf7_posted_data_radio', |
| 319 | 'wpcf7_posted_data_checkbox', |
| 320 | 10, 3 |
| 321 | ); |
| 322 | |
| 323 | function wpcf7_posted_data_checkbox( $value, $value_orig, $form_tag ) { |
| 324 | if ( $form_tag->has_option( 'free_text' ) ) { |
| 325 | $value = (array) $value; |
| 326 | |
| 327 | $free_text_name = sprintf( '_wpcf7_free_text_%s', $form_tag->name ); |
| 328 | $free_text = wpcf7_superglobal_post( $free_text_name ); |
| 329 | |
| 330 | $last_val = array_pop( $value ); |
| 331 | |
| 332 | if ( isset( $last_val ) ) { |
| 333 | $last_val = sprintf( '%s %s', $last_val, $free_text ); |
| 334 | $value[] = trim( $last_val ); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | return $value; |
| 339 | } |
| 340 | |
| 341 | |
| 342 | /* Tag generator */ |
| 343 | |
| 344 | add_action( 'wpcf7_admin_init', |
| 345 | 'wpcf7_add_tag_generator_checkbox_and_radio', 30, 0 ); |
| 346 | |
| 347 | function wpcf7_add_tag_generator_checkbox_and_radio() { |
| 348 | $tag_generator = WPCF7_TagGenerator::get_instance(); |
| 349 | |
| 350 | $basetypes = array( |
| 351 | 'checkbox' => __( 'checkboxes', 'contact-form-7' ), |
| 352 | 'radio' => __( 'radio buttons', 'contact-form-7' ), |
| 353 | ); |
| 354 | |
| 355 | foreach ( $basetypes as $id => $title ) { |
| 356 | $tag_generator->add( $id, $title, |
| 357 | 'wpcf7_tag_generator_checkbox', |
| 358 | array( 'version' => '2' ) |
| 359 | ); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | function wpcf7_tag_generator_checkbox( $contact_form, $options ) { |
| 364 | $field_types = array( |
| 365 | 'checkbox' => array( |
| 366 | 'display_name' => __( 'Checkboxes', 'contact-form-7' ), |
| 367 | 'heading' => __( 'Checkboxes form-tag generator', 'contact-form-7' ), |
| 368 | 'description' => __( 'Generates a form-tag for a group of <a href="https://contactform7.com/checkboxes-radio-buttons-and-menus/">checkboxes</a>.', 'contact-form-7' ), |
| 369 | ), |
| 370 | 'radio' => array( |
| 371 | 'display_name' => __( 'Radio buttons', 'contact-form-7' ), |
| 372 | 'heading' => __( 'Radio buttons form-tag generator', 'contact-form-7' ), |
| 373 | 'description' => __( 'Generates a form-tag for a group of <a href="https://contactform7.com/checkboxes-radio-buttons-and-menus/">radio buttons</a>.', 'contact-form-7' ), |
| 374 | ), |
| 375 | ); |
| 376 | |
| 377 | $basetype = $options['id']; |
| 378 | |
| 379 | if ( ! in_array( $basetype, array_keys( $field_types ), true ) ) { |
| 380 | $basetype = 'checkbox'; |
| 381 | } |
| 382 | |
| 383 | $tgg = new WPCF7_TagGeneratorGenerator( $options['content'] ); |
| 384 | |
| 385 | $formatter = new WPCF7_HTMLFormatter(); |
| 386 | |
| 387 | $formatter->append_start_tag( 'header', array( |
| 388 | 'class' => 'description-box', |
| 389 | ) ); |
| 390 | |
| 391 | $formatter->append_start_tag( 'h3' ); |
| 392 | |
| 393 | $formatter->append_preformatted( |
| 394 | esc_html( $field_types[$basetype]['heading'] ) |
| 395 | ); |
| 396 | |
| 397 | $formatter->end_tag( 'h3' ); |
| 398 | |
| 399 | $formatter->append_start_tag( 'p' ); |
| 400 | |
| 401 | $formatter->append_preformatted( |
| 402 | wp_kses_data( $field_types[$basetype]['description'] ) |
| 403 | ); |
| 404 | |
| 405 | $formatter->end_tag( 'header' ); |
| 406 | |
| 407 | $formatter->append_start_tag( 'div', array( |
| 408 | 'class' => 'control-box', |
| 409 | ) ); |
| 410 | |
| 411 | $formatter->call_user_func( static function () use ( $tgg, $field_types, $basetype ) { |
| 412 | $tgg->print( 'field_type', array( |
| 413 | 'with_required' => 'checkbox' === $basetype, |
| 414 | 'select_options' => array( |
| 415 | $basetype => $field_types[$basetype]['display_name'], |
| 416 | ), |
| 417 | ) ); |
| 418 | |
| 419 | $tgg->print( 'field_name' ); |
| 420 | |
| 421 | $tgg->print( 'class_attr' ); |
| 422 | |
| 423 | $tgg->print( 'selectable_values', array( |
| 424 | 'use_label_element' => 'checked', |
| 425 | ) ); |
| 426 | } ); |
| 427 | |
| 428 | $formatter->end_tag( 'div' ); |
| 429 | |
| 430 | $formatter->append_start_tag( 'footer', array( |
| 431 | 'class' => 'insert-box', |
| 432 | ) ); |
| 433 | |
| 434 | $formatter->call_user_func( static function () use ( $tgg, $field_types ) { |
| 435 | $tgg->print( 'insert_box_content' ); |
| 436 | |
| 437 | $tgg->print( 'mail_tag_tip' ); |
| 438 | } ); |
| 439 | |
| 440 | $formatter->print(); |
| 441 | } |
| 442 |