PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.14
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.14
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 / Modules / Component / ComponentInitTrait.php

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

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