← All changes
|
classes/views/frm-fields/front-end/checkbox-field.php
+35
-7
6.26
→
trunk
View file →
| @@ -7,9 +7,19 @@ | ||
| 7 | 7 | * Show the checkbox field on the front-end. |
| 8 | 8 | * Extra line breaks show as space on the front-end when |
| 9 | 9 | * the form is double filtered and not minimized. |
| 10 | 10 | * |
| 11 | + * @var array $field | |
| 12 | + * @var string $field_name | |
| 13 | + * @var bool $read_only | |
| 14 | + * @var string $html_id | |
| 15 | + * @var array|null $args | |
| 16 | + * | |
| 11 | 17 | * @phpcs:disable Generic.WhiteSpace.ScopeIndent |
| 18 | + * | |
| 19 | + * @since 6.30 Added `$hide_label`. | |
| 20 | + * | |
| 21 | + * @var bool $hide_label | |
| 12 | 22 | */ |
| 13 | 23 | |
| 14 | 24 | if ( isset( $field['post_field'] ) && $field['post_field'] === 'post_category' ) { |
| 15 | 25 | $type = $field['type']; |
| @@ -14,8 +24,12 @@ | ||
| 14 | 24 | if ( isset( $field['post_field'] ) && $field['post_field'] === 'post_category' ) { |
| 15 | 25 | $type = $field['type']; |
| 16 | 26 | do_action( 'frm_after_checkbox', compact( 'field', 'field_name', 'type' ) ); |
| 17 | 27 | } elseif ( $field['options'] ) { |
| 28 | + if ( FrmFieldsHelper::should_skip_rendering_choices_for_field( $field ) ) { | |
| 29 | + return; | |
| 30 | + } | |
| 31 | + | |
| 18 | 32 | $option_index = 0; |
| 19 | 33 | |
| 20 | 34 | foreach ( $field['options'] as $opt_key => $opt ) { |
| 21 | 35 | if ( isset( $shortcode_atts ) && isset( $shortcode_atts['opt'] ) && $shortcode_atts['opt'] !== $opt_key ) { |
| @@ -21,8 +35,12 @@ | ||
| 21 | 35 | if ( isset( $shortcode_atts ) && isset( $shortcode_atts['opt'] ) && $shortcode_atts['opt'] !== $opt_key ) { |
| 22 | 36 | continue; |
| 23 | 37 | } |
| 24 | 38 | |
| 39 | + if ( FrmFieldsHelper::should_hide_field_choice( $opt_key, $field ) ) { | |
| 40 | + continue; | |
| 41 | + } | |
| 42 | + | |
| 25 | 43 | $field_val = FrmFieldsHelper::get_value_from_array( $opt, $opt_key, $field ); |
| 26 | 44 | $opt = FrmFieldsHelper::get_label_from_array( $opt, $opt_key, $field ); |
| 27 | 45 | |
| 28 | 46 | /** |
| @@ -30,11 +48,11 @@ | ||
| 30 | 48 | * |
| 31 | 49 | * @since 5.0.04 |
| 32 | 50 | * |
| 33 | 51 | * @param string $label Label HTML. |
| 34 | - * @param array $args The arguments. Contains `field`. | |
| 52 | + * @param array $args The arguments. Contains `field` and `field_val`. | |
| 35 | 53 | */ |
| 36 | - $label = apply_filters( 'frm_choice_field_option_label', $opt, compact( 'field' ) ); | |
| 54 | + $label = apply_filters( 'frm_choice_field_option_label', $opt, compact( 'field', 'field_val' ) ); | |
| 37 | 55 | |
| 38 | 56 | // init. |
| 39 | 57 | $checked = ''; |
| 40 | 58 | |
| @@ -40,9 +58,9 @@ | ||
| 40 | 58 | |
| 41 | 59 | if ( ! FrmFieldsHelper::is_other_opt( $opt_key ) ) { |
| 42 | 60 | // Let the checked state of 'Other' fields be determined solely by FrmFieldsHelper::prepare_other_input as below. |
| 43 | 61 | // Without this check, one 'Other' field being checked leads to making all 'Other' fields checked on submit error |
| 44 | - // since they all have the same value attr of 'Other'. | |
| 62 | + // Since they all have the same value attr of 'Other'. | |
| 45 | 63 | $checked = FrmAppHelper::check_selected( $field['value'], $field_val ) ? ' checked="checked"' : ''; |
| 46 | 64 | } |
| 47 | 65 | |
| 48 | 66 | // Check if other opt, and get values for other field if needed |
| @@ -48,19 +66,21 @@ | ||
| 48 | 66 | // Check if other opt, and get values for other field if needed |
| 49 | 67 | $other_opt = false; |
| 50 | 68 | $other_args = FrmFieldsHelper::prepare_other_input( compact( 'field', 'field_name', 'opt_key', 'field_val' ), $other_opt, $checked ); |
| 51 | 69 | |
| 70 | + $should_echo_disabled_att = FrmFieldsHelper::should_disable_choice( $opt_key, $checked, $field ); | |
| 52 | 71 | ?> |
| 53 | 72 | <div class="<?php echo esc_attr( apply_filters( 'frm_checkbox_class', 'frm_checkbox', $field, $field_val ) ); ?>" id="<?php echo esc_attr( FrmFieldsHelper::get_checkbox_id( $field, $opt_key ) ); ?>"><?php |
| 54 | 73 | |
| 55 | - if ( ! isset( $shortcode_atts ) || ! isset( $shortcode_atts['label'] ) || $shortcode_atts['label'] ) { | |
| 74 | + if ( empty( $hide_label ) ) { | |
| 56 | 75 | $label_attributes = array( |
| 57 | 76 | 'for' => $html_id . '-' . $opt_key, |
| 58 | 77 | ); |
| 59 | 78 | |
| 60 | - if ( $read_only ) { | |
| 79 | + if ( $read_only || $should_echo_disabled_att ) { | |
| 61 | 80 | $label_attributes['class'] = 'frm-label-disabled'; |
| 62 | 81 | } |
| 82 | + | |
| 63 | 83 | ?> |
| 64 | 84 | <label <?php FrmAppHelper::array_to_html_params( $label_attributes, true ); ?>> |
| 65 | 85 | <?php |
| 66 | 86 | } |
| @@ -65,12 +85,17 @@ | ||
| 65 | 85 | <?php |
| 66 | 86 | } |
| 67 | 87 | |
| 68 | 88 | ?><input type="checkbox" name="<?php echo esc_attr( $field_name ); ?>[<?php echo esc_attr( $other_opt ? $opt_key : '' ); ?>]" id="<?php echo esc_attr( $html_id ); ?>-<?php echo esc_attr( $opt_key ); ?>" value="<?php echo esc_attr( $field_val ); ?>"<?php |
| 89 | + | |
| 69 | 90 | echo $checked . ' '; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 70 | 91 | |
| 71 | 92 | do_action( 'frm_field_input_html', $field ); |
| 72 | 93 | |
| 94 | + if ( $should_echo_disabled_att ) { | |
| 95 | + echo 'disabled="disabled" data-max-reached="1" '; | |
| 96 | + } | |
| 97 | + | |
| 73 | 98 | if ( 0 === $option_index && FrmField::is_required( $field ) ) { |
| 74 | 99 | echo ' aria-required="true" '; |
| 75 | 100 | } |
| 76 | 101 | |
| @@ -75,10 +100,13 @@ | ||
| 75 | 100 | } |
| 76 | 101 | |
| 77 | 102 | ?> /><?php |
| 78 | 103 | |
| 79 | - if ( ! isset( $shortcode_atts ) || ! isset( $shortcode_atts['label'] ) || $shortcode_atts['label'] ) { | |
| 80 | - echo ' ' . FrmAppHelper::kses( $label, 'all' ) . '</label>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 104 | + if ( empty( $hide_label ) ) { | |
| 105 | + echo ' '; | |
| 106 | + FrmAppHelper::kses_echo( $label, 'all' ); | |
| 107 | + FrmFieldsHelper::after_choice_input( $field, $opt_key ); | |
| 108 | + echo '</label>'; | |
| 81 | 109 | } |
| 82 | 110 | |
| 83 | 111 | $other_args = array( |
| 84 | 112 | 'other_opt' => $other_opt, |