| @@ -5,9 +5,8 @@ | ||
| 5 | 5 | |
| 6 | 6 | /** |
| 7 | 7 | * @since 3.0 |
| 8 | 8 | */ |
| 9 | - | |
| 10 | 9 | class FrmFieldFormHtml { |
| 11 | 10 | |
| 12 | 11 | private $html; |
| 13 | 12 | |
| @@ -41,9 +40,9 @@ | ||
| 41 | 40 | /** |
| 42 | 41 | * @since 3.0 |
| 43 | 42 | * |
| 44 | 43 | * @param string $param |
| 45 | - * @param array $atts | |
| 44 | + * @param array $atts | |
| 46 | 45 | */ |
| 47 | 46 | private function _set( $param, $atts ) { |
| 48 | 47 | if ( isset( $atts[ $param ] ) ) { |
| 49 | 48 | $this->{$param} = $atts[ $param ]; |
| @@ -144,18 +143,18 @@ | ||
| 144 | 143 | /** |
| 145 | 144 | * @since 3.0 |
| 146 | 145 | */ |
| 147 | 146 | private function replace_field_values() { |
| 148 | - //replace [id] | |
| 147 | + // Replace [id]. | |
| 149 | 148 | $this->html = str_replace( '[id]', $this->field_id, $this->html ); |
| 150 | 149 | |
| 151 | 150 | // set the label for |
| 152 | 151 | $this->html = str_replace( 'field_[key]', $this->html_id, $this->html ); |
| 153 | 152 | |
| 154 | - //replace [key] | |
| 153 | + // Replace [key]. | |
| 155 | 154 | $this->html = str_replace( '[key]', $this->field_obj->get_field_column( 'field_key' ), $this->html ); |
| 156 | 155 | |
| 157 | - //replace [field_name] | |
| 156 | + // Replace [field_name]. | |
| 158 | 157 | $this->html = str_replace( '[field_name]', FrmAppHelper::maybe_kses( $this->field_obj->get_field_column( 'name' ) ), $this->html ); |
| 159 | 158 | } |
| 160 | 159 | |
| 161 | 160 | /** |
| @@ -219,9 +218,9 @@ | ||
| 219 | 218 | |
| 220 | 219 | if ( is_string( $inner_html[2] ) ) { |
| 221 | 220 | $has_id = strpos( $inner_html[2], ' id=' ); |
| 222 | 221 | if ( ! $has_id ) { |
| 223 | - $id = 'frm_' . $id . '_' . $this->html_id; | |
| 222 | + $id = 'frm_' . $id . '_' . $this->html_id; | |
| 224 | 223 | $this->html = str_replace( 'class="frm_' . $param, 'id="' . esc_attr( $id ) . '" class="frm_' . esc_attr( $param ), $this->html ); |
| 225 | 224 | } |
| 226 | 225 | } |
| 227 | 226 | } |
| @@ -247,9 +246,9 @@ | ||
| 247 | 246 | /** |
| 248 | 247 | * Pull the HTML between [if error] and [/if error] shortcodes. |
| 249 | 248 | * |
| 250 | 249 | * @param string $html |
| 251 | - * @return string|false | |
| 250 | + * @return false|string | |
| 252 | 251 | */ |
| 253 | 252 | private static function get_error_body( $html ) { |
| 254 | 253 | $start = strpos( $html, '[if error]' ); |
| 255 | 254 | if ( false === $start ) { |
| @@ -295,12 +294,12 @@ | ||
| 295 | 294 | private function replace_form_shortcodes() { |
| 296 | 295 | if ( ! empty( $this->form ) ) { |
| 297 | 296 | $form = (array) $this->form; |
| 298 | 297 | |
| 299 | - //replace [form_key] | |
| 298 | + // Replace [form_key]. | |
| 300 | 299 | $this->html = str_replace( '[form_key]', $form['form_key'], $this->html ); |
| 301 | 300 | |
| 302 | - //replace [form_name] | |
| 301 | + // Replace [form_name]. | |
| 303 | 302 | $this->html = str_replace( '[form_name]', $form['name'], $this->html ); |
| 304 | 303 | } |
| 305 | 304 | } |
| 306 | 305 | |
| @@ -321,9 +320,9 @@ | ||
| 321 | 320 | */ |
| 322 | 321 | private function filter_for_more_shortcodes() { |
| 323 | 322 | $atts = $this->pass_args; |
| 324 | 323 | |
| 325 | - //If field is not in repeating section | |
| 324 | + // If field is not in repeating section. | |
| 326 | 325 | if ( empty( $atts['section_id'] ) ) { |
| 327 | 326 | $atts = array( |
| 328 | 327 | 'errors' => $this->pass_args['errors'], |
| 329 | 328 | 'form' => $this->form, |
| @@ -356,11 +355,11 @@ | ||
| 356 | 355 | $tag = FrmShortcodeHelper::get_shortcode_tag( $shortcodes, $short_key ); |
| 357 | 356 | |
| 358 | 357 | $replace_with = ''; |
| 359 | 358 | |
| 360 | - if ( $tag == 'deletelink' && FrmAppHelper::pro_is_installed() ) { | |
| 359 | + if ( $tag === 'deletelink' && FrmAppHelper::pro_is_installed() ) { | |
| 361 | 360 | $replace_with = FrmProEntriesController::entry_delete_link( $shortcode_atts ); |
| 362 | - } elseif ( $tag == 'input' ) { | |
| 361 | + } elseif ( $tag === 'input' ) { | |
| 363 | 362 | $replace_with = $this->replace_input_shortcode( $shortcode_atts ); |
| 364 | 363 | } |
| 365 | 364 | |
| 366 | 365 | $this->html = str_replace( $shortcodes[0][ $short_key ], $replace_with, $this->html ); |
| @@ -384,9 +383,9 @@ | ||
| 384 | 383 | * @return array |
| 385 | 384 | */ |
| 386 | 385 | private function prepare_input_shortcode_atts( $shortcode_atts ) { |
| 387 | 386 | if ( isset( $shortcode_atts['opt'] ) ) { |
| 388 | - $shortcode_atts['opt'] --; | |
| 387 | + --$shortcode_atts['opt']; | |
| 389 | 388 | } |
| 390 | 389 | |
| 391 | 390 | $field_class = isset( $shortcode_atts['class'] ) ? $shortcode_atts['class'] : ''; |
| 392 | 391 | $this->field_obj->set_field_column( 'input_class', $field_class ); |
| @@ -394,9 +393,9 @@ | ||
| 394 | 393 | if ( isset( $shortcode_atts['class'] ) ) { |
| 395 | 394 | unset( $shortcode_atts['class'] ); |
| 396 | 395 | } |
| 397 | 396 | |
| 398 | - $shortcode_atts['aria-invalid'] = isset( $this->pass_args['errors'][ 'field' . $this->field_id ] ) ? 'true' : 'false'; | |
| 397 | + $this->field_obj->set_aria_invalid_error( $shortcode_atts, $this->pass_args ); | |
| 399 | 398 | |
| 400 | 399 | $this->field_obj->set_field_column( 'shortcodes', $shortcode_atts ); |
| 401 | 400 | |
| 402 | 401 | return $shortcode_atts; |
| @@ -430,13 +429,15 @@ | ||
| 430 | 429 | */ |
| 431 | 430 | private function add_field_div_classes() { |
| 432 | 431 | $classes = $this->get_field_div_classes(); |
| 433 | 432 | |
| 434 | - if ( $this->field_obj->get_field_column( 'type' ) == 'html' && strpos( $this->html, '[error_class]' ) === false ) { | |
| 433 | + if ( in_array( $this->field_obj->get_field_column( 'type' ), array( 'html', 'summary' ), true ) && strpos( $this->html, '[error_class]' ) === false ) { | |
| 435 | 434 | // there is no error_class shortcode for HTML fields |
| 436 | - $this->html = str_replace( 'class="frm_form_field', 'class="frm_form_field ' . $classes, $this->html ); | |
| 435 | + $this->html = str_replace( 'class="frm_form_field', 'class="frm_form_field ' . esc_attr( $classes ), $this->html ); | |
| 436 | + return; | |
| 437 | 437 | } |
| 438 | - $this->html = str_replace( '[error_class]', $classes, $this->html ); | |
| 438 | + | |
| 439 | + $this->html = str_replace( '[error_class]', esc_attr( $classes ), $this->html ); | |
| 439 | 440 | } |
| 440 | 441 | |
| 441 | 442 | /** |
| 442 | 443 | * Get the classes for a field div |
| @@ -452,9 +453,9 @@ | ||
| 452 | 453 | // Add label position class |
| 453 | 454 | $settings = $this->field_obj->display_field_settings(); |
| 454 | 455 | if ( isset( $settings['label_position'] ) && $settings['label_position'] ) { |
| 455 | 456 | $label_position = $this->field_obj->get_field_column( 'label' ); |
| 456 | - $classes .= ' frm_' . $label_position . '_container'; | |
| 457 | + $classes .= ' frm_' . $label_position . '_container'; | |
| 457 | 458 | |
| 458 | 459 | // Add class if field has value, to be used for floating label styling. |
| 459 | 460 | if ( 'inside' === $label_position && $this->field_obj->get_field_column( 'value' ) ) { |
| 460 | 461 | $classes .= ' frm_label_float_top'; |
| @@ -472,9 +473,14 @@ | ||
| 472 | 473 | |
| 473 | 474 | $classes .= $this->field_obj->get_container_class(); |
| 474 | 475 | |
| 475 | 476 | // Get additional classes |
| 476 | - return apply_filters( 'frm_field_div_classes', $classes, $this->field_obj->get_field(), array( 'field_id' => $this->field_id ) ); | |
| 477 | + $classes = apply_filters( 'frm_field_div_classes', $classes, $this->field_obj->get_field(), array( 'field_id' => $this->field_id ) ); | |
| 478 | + | |
| 479 | + // Remove unexpected characters from class. | |
| 480 | + $classes = implode( ' ', array_map( 'FrmFormsHelper::sanitize_layout_class', explode( ' ', $classes ) ) ); | |
| 481 | + | |
| 482 | + return $classes; | |
| 477 | 483 | } |
| 478 | 484 | |
| 479 | 485 | /** |
| 480 | 486 | * This filters shortcodes in the field HTML |
| @@ -500,25 +506,17 @@ | ||
| 500 | 506 | if ( ! in_array( $field_type, array( 'radio', 'checkbox', 'data', 'product', 'scale' ), true ) ) { |
| 501 | 507 | return; |
| 502 | 508 | } |
| 503 | 509 | |
| 504 | - $field = (array) $this->field_obj->get_field(); | |
| 505 | - $attributes = array(); | |
| 506 | - $is_radio = 'radio' === $field_type || 'scale' === $field_type; | |
| 507 | - $type_requires_aria_required = true; | |
| 510 | + $field = (array) $this->field_obj->get_field(); | |
| 511 | + $attributes = array(); | |
| 508 | 512 | |
| 509 | 513 | // Check if the field type is 'data' or 'product'. |
| 510 | 514 | if ( in_array( $field_type, array( 'data', 'product' ), true ) ) { |
| 511 | 515 | $data_type = FrmField::get_option( $field, 'data_type' ); |
| 512 | - // Check if the data type isn't 'radio' or 'checkbox'. | |
| 513 | - if ( 'radio' !== $data_type && 'checkbox' !== $data_type ) { | |
| 514 | - // If data type aren't 'radio' or 'checkbox', doesn't need to add 'aria-required' to multiple input container. | |
| 515 | - $type_requires_aria_required = false; | |
| 516 | - } | |
| 517 | - // Check if data type is 'radio' | |
| 518 | - if ( 'radio' === $data_type ) { | |
| 519 | - $is_radio = true; | |
| 520 | - } | |
| 516 | + $is_radio = 'radio' === $data_type; | |
| 517 | + } else { | |
| 518 | + $is_radio = 'radio' === $field_type || 'scale' === $field_type; | |
| 521 | 519 | } |
| 522 | 520 | |
| 523 | 521 | // Add 'role' attribute to the field. |
| 524 | 522 | $attributes['role'] = $is_radio ? 'radiogroup' : 'group'; |
| @@ -523,15 +521,10 @@ | ||
| 523 | 521 | // Add 'role' attribute to the field. |
| 524 | 522 | $attributes['role'] = $is_radio ? 'radiogroup' : 'group'; |
| 525 | 523 | |
| 526 | 524 | // Add 'aria-required' attribute to the field if required. |
| 527 | - if ( $type_requires_aria_required && '1' === $field['required'] ) { | |
| 525 | + if ( $is_radio && '1' === $field['required'] ) { | |
| 528 | 526 | $attributes['aria-required'] = 'true'; |
| 529 | - } | |
| 530 | - | |
| 531 | - // Add 'tabindex = "0"' attribute to the radio field. | |
| 532 | - if ( $is_radio ) { | |
| 533 | - $attributes['tabindex'] = 0; | |
| 534 | 527 | } |
| 535 | 528 | |
| 536 | 529 | // Concatenate attributes into a string, and replace the role="group" in the HTML with the attributes string. |
| 537 | 530 | $html_attributes = FrmAppHelper::array_to_html_params( $attributes ); |