PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 4.3.21
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v4.3.21
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 3.6.66 All 195 releases
fluentform / app / Modules / Component / ComponentInitTrait.php

ComponentInitTrait.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 4.3.21, at app/Modules/Component/ComponentInitTrait.php

264 lines 7.7 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\Modules\Component;
4
5 trait ComponentInitTrait
6 {
7 /**
8 * Initialize the component and register the
9 * component by registering actions/filters.
10 */
11 public function _init()
12 {
13 // Validate certain important required properties
14 $this->_fluentFormValidateComponent();
15
16 // Hook to add component in the editor's components array
17 add_filter('fluent_editor_components', [$this, '_fluentEditorComponenstCallback']);
18
19 // Hook to add search keywords for the component
20 add_filter('fluent_editor_element_search_tags', [$this, '_fluentEditorElementSearchTagsCallback']);
21
22 // Hook for component's editor items/options placement settings
23 add_filter('fluent_editor_element_settings_placement', [$this, '_fluentEditorElementSettingsPlacementCallback']);
24
25 // Hook for component's customization settings
26 add_filter('fluent_editor_element_customization_settings', [$this, '_fluentEditorElementCustomizationSettingsCallback']);
27
28 // Hook for response data preperation on form submission
29 add_filter('fluentform_insert_response_data', [$this, '_fluentformInsertResponseDataCallback'], 10, 3);
30
31 // Hook to add component type in fluentform field types to be available in FormFieldParser.
32 add_filter('fluentform_form_input_types', [$this, '_fluentformFormInputTypesCallback']);
33
34 // Component render/compile hook (for form)
35 add_action('fluentform_render_item_' . $this->element(), [$this, '_elementRenderHookCallback'], 10, 2);
36
37 // Component's used element response transformation hook (for entries)
38 add_filter('fluentform_response_render_' . $this->element(), [$this, '_elementEntryFormatCallback'], 10, 3);
39 }
40
41 /**
42 * Validate certain required properties
43 *
44 * @return void
45 */
46 private function _fluentFormValidateComponent()
47 {
48 $name = $this->name();
49 if (! $name || ! is_string($name)) {
50 wp_die('The name must be a valid string.');
51 }
52
53 $label = $this->label();
54 if (! $label || ! is_string($label)) {
55 wp_die('The label must be a valid string.');
56 }
57
58 $element = $this->element();
59 if (! $element || ! is_string($element)) {
60 $elements = 'text_input, text_email, textarea';
61 wp_die("The element must be one of the available elements, i.e: $elements e.t.c.");
62 }
63
64 $template = $this->template();
65 if (! $template || ! is_string($template)) {
66 $templates = 'inputText, selectCountry. addressFields';
67 wp_die("The template must be one of the available templates, i.e: $templates e.t.c.");
68 }
69
70 $group = $this->group();
71 $groups = ['general', 'advanced', 'container'];
72 if (! $group || ! in_array($group, $groups)) {
73 wp_die('Invalid group, available groups: ' . implode(', ', $groups) . '.');
74 }
75 }
76
77 /**
78 * Add the component in fluentform editor's components array.
79 *
80 * @param array $components
81 *
82 * @return array $components
83 */
84 public function _fluentEditorComponenstCallback($components)
85 {
86 $name = $this->name();
87 $label = $this->label();
88 $group = $this->group();
89 $index = $this->index();
90 $element = $this->element();
91 $template = $this->template();
92 $iconClass = $this->elementIconClass();
93 $validationRules = $this->validationRules();
94
95 $settings = $this->settings([
96 'label' => $label,
97 'container_class' => '',
98 'label_placement' => '',
99 'help_message' => '',
100 'admin_field_label' => $label,
101 'conditional_logics' => [],
102 'validation_rules' => $validationRules,
103 ]);
104
105 $attributes = $this->attributes([
106 'id' => '',
107 'name' => $name,
108 'class' => '',
109 'value' => '',
110 'placeholder' => '',
111 ]);
112
113 $editorOptions = $this->options([
114 'title' => $label,
115 'template' => $template,
116 'icon_class' => $iconClass,
117 ]);
118
119 if (is_null($index)) {
120 $index = count($components[$group]) + 1;
121 }
122
123 $components[$group][] = [
124 'index' => $index,
125 'element' => $element,
126 'settings' => $settings,
127 'attributes' => $attributes,
128 'editor_options' => $editorOptions,
129 ];
130
131 return $components;
132 }
133
134 /**
135 * Add the keywords for the component to search in the editor.
136 *
137 * @param array $keywords
138 *
139 * @return array $keywords
140 */
141 public function _fluentEditorElementSearchTagsCallback($keywords)
142 {
143 $newKeywords = $this->searchBy();
144
145 if (! $newKeywords) {
146 return $keywords;
147 }
148
149 $existingKeywords = [];
150 $element = $this->element();
151 if (isset($keywords[$element])) {
152 $existingKeywords = $keywords[$element];
153 }
154
155 $keywords[$element] = array_merge(
156 $existingKeywords,
157 $newKeywords
158 );
159
160 return $keywords;
161 }
162
163 /**
164 * Configure placements of input customization options in editor.
165 *
166 * @param array $placemenSettings
167 *
168 * @return array $placemenSettings
169 */
170 public function _fluentEditorElementSettingsPlacementCallback($placementSettings)
171 {
172 $default = [
173 'general' => [
174 'label',
175 'label_placement',
176 'admin_field_label',
177 'placeholder',
178 'value',
179 'validation_rules',
180 ],
181 'advanced' => [
182 'name',
183 'class',
184 'help_message',
185 'container_class',
186 'conditional_logics',
187 ],
188 ];
189
190 $placementSettings[$this->element()] = array_merge_recursive(
191 $this->placementSettings($default),
192 $placementSettings
193 );
194
195 return $placementSettings;
196 }
197
198 /**
199 * Configure input customization options/items in the editor.
200 *
201 * @param array $customizationSettings
202 *
203 * @return array $customizationSettings
204 */
205 public function _fluentEditorElementCustomizationSettingsCallback($customizationSettings)
206 {
207 return $this->customizationSettings($customizationSettings);
208 }
209
210 /**
211 * Prepare the submission data for the element on Form Submission.
212 *
213 * @param array $formData
214 * @param int $formId
215 * @param array $inputConfigs
216 *
217 * @return array $formData
218 */
219 public function _fluentformInsertResponseDataCallback($formData, $formId, $inputConfigs)
220 {
221 return $this->onSubmit($formData, $formId, $inputConfigs);
222 }
223
224 /**
225 * Add the component type in fluentform field
226 * types to be available in FormFieldParser.
227 *
228 * @param array $types
229 *
230 * @return array $types
231 */
232 public function _fluentformFormInputTypesCallback($types)
233 {
234 return $this->addType($types);
235 }
236
237 /**
238 * Render the component.
239 *
240 * @param array $data
241 * @param \stdClass $form
242 *
243 * @return void
244 */
245 public function _elementRenderHookCallback($item, $form)
246 {
247 $this->render($item, $form);
248 }
249
250 /**
251 * Element's entry value transformation.
252 *
253 * @param mixed $value
254 * @param string $field
255 * @param int $formId
256 *
257 * @return mixed $value
258 */
259 public function _elementEntryFormatCallback($value, $field, $formId)
260 {
261 return $this->formatEntryValue($value, $field, $formId);
262 }
263 }
264