PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.11
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.11
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / app / Services / FormBuilder / Components / Checkable.php

Checkable.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 6.2.11, at app/Services/FormBuilder/Components/Checkable.php

193 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\App\Services\FormBuilder\Components;
4
5 use FluentForm\Framework\Helpers\ArrayHelper;
6
7 class Checkable extends BaseComponent
8 {
9 /**
10 * Compile and echo the html element
11 *
12 * @param array $data [element data]
13 * @param \stdClass $form [Form Object]
14 *
15 * @return void
16 */
17 public function compile($data, $form)
18 {
19 $elementName = $data['element'];
20
21 $data = apply_filters_deprecated(
22 'fluentform_rendering_field_data_' . $elementName,
23 [
24 $data,
25 $form
26 ],
27 FLUENTFORM_FRAMEWORK_UPGRADE,
28 'fluentform/rendering_field_data_' . $elementName,
29 'Use fluentform/rendering_field_data_' . $elementName . ' instead of fluentform_rendering_field_data_' . $elementName
30 );
31
32 $data = apply_filters('fluentform/rendering_field_data_' . $elementName, $data, $form);
33
34 $data['attributes']['class'] = trim(
35 'ff-el-form-check-input ' .
36 'ff-el-form-check-' . $data['attributes']['type'] . ' ' .
37 ArrayHelper::get($data, 'attributes.class')
38 );
39
40 if ('checkbox' == $data['attributes']['type']) {
41 $data['attributes']['name'] = $data['attributes']['name'] . '[]';
42 }
43
44 $defaultValues = (array) $this->extractValueFromAttributes($data);
45
46 if ($dynamicValues = $this->extractDynamicValues($data, $form)) {
47 $defaultValues = $dynamicValues;
48 }
49
50 $elMarkup = '';
51
52 $firstTabIndex = \FluentForm\App\Helpers\Helper::getNextTabIndex();
53
54 if (! $formattedOptions = ArrayHelper::get($data, 'settings.advanced_options')) {
55 $options = ArrayHelper::get($data, 'options', []);
56 $formattedOptions = [];
57 foreach ($options as $value => $label) {
58 $formattedOptions[] = [
59 'label' => $label,
60 'value' => $value,
61 'calc_value' => '',
62 'image' => '',
63 ];
64 }
65 }
66
67 $hasImageOption = ArrayHelper::get($data, 'settings.enable_image_input');
68
69 if ($hasImageOption) {
70 if (empty($data['settings']['layout_class'])) {
71 $data['settings']['layout_class'] = 'ff_list_buttons';
72 }
73 $elMarkup .= '<div class="ff_el_checkable_photo_holders">';
74 }
75
76 $data['settings']['container_class'] .= ' ' . ArrayHelper::get($data, 'settings.layout_class');
77
78 if ('yes' == ArrayHelper::get($data, 'settings.randomize_options')) {
79 shuffle($formattedOptions);
80 }
81
82 // Add "Other" option if enabled
83 $enableOtherOption = ArrayHelper::get($data, 'settings.enable_other_option') === 'yes';
84 if ($enableOtherOption && in_array($data['attributes']['type'], ['checkbox', 'radio']) && defined('FLUENTFORMPRO')) {
85 $fieldName = sanitize_text_field(str_replace(['[', ']'], '', $data['attributes']['name']));
86 $otherLabel = ArrayHelper::get($data, 'settings.other_option_label', __('Other', 'fluentform'));
87 $formattedOptions[] = [
88 'label' => $otherLabel,
89 'value' => '__ff_other_' . $fieldName . '__',
90 'calc_value' => '',
91 'image' => '',
92 'is_other' => true,
93 ];
94 }
95 // @todo : Find a alternative screen reader support
96 // $legendId = $this->getUniqueid(str_replace(['[', ']'], ['', ''], $data['attributes']['name']));
97 // $elMarkup .= '<fieldset role="group" style="border: none!important;margin: 0!important;padding: 0!important;background-color: transparent!important;box-shadow: none!important;outline: none!important; min-inline-size: 100%;" aria-labelledby="legend_' . $legendId . '">';
98 //
99 // $elMarkup .= '<legend style=" position: absolute;width: 1px;height: 1px;padding: 0;margin: 0;overflow: hidden;clip: rect(0, 0, 0, 0);border: 0;" role="heading" id="legend_' . $legendId . '" class="ff-sreader-only">' . esc_attr($this->removeShortcode($data['settings']['label'])) . '</legend>';
100
101 $otherInputHtml = '';
102 foreach ($formattedOptions as $option) {
103 $displayType = isset($data['settings']['display_type']) ? ' ff-el-form-check-' . $data['settings']['display_type'] : '';
104 $parentClass = 'ff-el-form-check' . esc_attr($displayType) . '';
105
106 if (in_array($option['value'], $defaultValues)) {
107 $data['attributes']['checked'] = true;
108 $parentClass .= ' ff_item_selected';
109 } else {
110 $data['attributes']['checked'] = false;
111 }
112
113 if ($firstTabIndex) {
114 $data['attributes']['tabindex'] = $firstTabIndex;
115 $firstTabIndex = '-1';
116 }
117
118 $data['attributes']['value'] = $option['value'];
119 $data['attributes']['data-calc_value'] = ArrayHelper::get($option, 'calc_value');
120
121 $atts = $this->buildAttributes($data['attributes']);
122
123 $id = $this->getUniqueid(str_replace(['[', ']'], ['', ''], $data['attributes']['name']));
124
125 if ($hasImageOption) {
126 $parentClass .= ' ff-el-image-holder';
127 }
128
129 $elMarkup .= "<div class='" . esc_attr($parentClass) . "'>";
130
131 $id = esc_attr($id);
132
133 $label = fluentform_sanitize_html($option['label']);
134 $ariaLabel = esc_attr($label);
135 // Here we can push the visual items
136 if ($hasImageOption) {
137 $elMarkup .= "<label style='background-image: url(" . esc_url($option['image']) . ")' class='ff-el-image-input-src' for='{$id}' aria-label='{$this->removeShortcode($ariaLabel)}'></label>";
138 }
139
140 $ariaRequired = 'false';
141 if (ArrayHelper::get($data, 'settings.validation_rules.required.value')) {
142 $ariaRequired = 'true';
143 }
144
145 $disabled = ArrayHelper::get($option, 'disabled') ? 'disabled' : '';
146
147 $isOtherOption = ArrayHelper::get($option, 'is_other', false);
148 $otherClass = $isOtherOption ? ' ff-other-option' : '';
149
150 $elMarkup .= "<label class='ff-el-form-check-label{$otherClass}' for='{$id}'><input {$disabled} {$atts} id='{$id}' aria-label='{$this->removeShortcode($ariaLabel)}' aria-invalid='false' aria-required={$ariaRequired}> <span>" . $label . '</span></label>';
151
152 // Add text input for "Other" option
153 if ($isOtherOption && defined('FLUENTFORMPRO')) {
154 $otherPlaceholder = ArrayHelper::get($data, 'settings.other_option_placeholder', __('Please specify...', 'fluentform'));
155 $fieldName = str_replace(['[', ']'], '', $data['attributes']['name']);
156 $otherInputName = $fieldName . '__ff_other_input__';
157 $otherValue = '';
158
159 $marginTop = $hasImageOption ? '20px' : '8px';
160 $otherInputHtml .= "<div class='ff-other-input-wrapper' style='display: none; margin-top: {$marginTop};' data-field='{$fieldName}'>";
161 $otherInputHtml .= "<input type='text' name='" . esc_attr($otherInputName) . "' class='ff-el-form-control' placeholder='" . esc_attr($otherPlaceholder) . "' value='" . esc_attr($otherValue) . "'>";
162 $otherInputHtml .= "</div>";
163 }
164
165 $elMarkup .= '</div>';
166 }
167
168 if ($hasImageOption) {
169 $elMarkup .= '</div>';
170 }
171 if ($otherInputHtml) {
172 $elMarkup .= $otherInputHtml;
173 }
174 // $elMarkup .= '</fieldset>';
175
176 $html = $this->buildElementMarkup($elMarkup, $data, $form);
177
178 $data = apply_filters_deprecated(
179 'fluentform_rendering_field_html_' . $elementName,
180 [
181 $html,
182 $data,
183 $form
184 ],
185 FLUENTFORM_FRAMEWORK_UPGRADE,
186 'fluentform/rendering_field_html_' . $elementName,
187 'Use fluentform/rendering_field_html_' . $elementName . ' instead of fluentform_nonce_verify.'
188 );
189
190 $this->printContent('fluentform/rendering_field_html_' . $elementName, $html, $data, $form);
191 }
192 }
193