PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 0.0.4
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v0.0.4
2.12.6 2.12.5 2.12.4 2.12.3 2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 0.0.2 0.0.3 All 96 releases
sureforms / inc / fields / base.php

base.php in SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz 0.0.4, at inc/fields/base.php

418 lines 10.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Form Field Base Class.
4 *
5 * This file defines the base class for form fields in the SureForms package.
6 *
7 * @package SureForms
8 * @since 0.0.1
9 */
10
11 namespace SRFM\Inc\Fields;
12
13 use SRFM\Inc\Helper;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit; // Exit if accessed directly.
17 }
18
19 /**
20 * Field Base Class
21 *
22 * Defines the base class for form fields.
23 *
24 * @since 0.0.1
25 */
26 class Base {
27 /**
28 * Flag indicating if the field is required.
29 *
30 * @var bool
31 * @since 0.0.2
32 */
33 protected $required;
34
35 /**
36 * Width of the field.
37 *
38 * @var string
39 * @since 0.0.2
40 */
41 protected $field_width;
42
43 /**
44 * Stores the label for an input field.
45 * The value of this variable specifies the text displayed as the label for the corresponding input field when rendered.
46 *
47 * @var string $label Label used for the input field.
48 * @since 0.0.2
49 */
50 protected $label;
51
52 /**
53 * Stores the string that provides help text.
54 *
55 * @var string $help
56 * @since 0.0.2
57 */
58 protected $help;
59
60 /**
61 * Validation error message for the fields.
62 *
63 * @var string $error_msg Input field validation error message.
64 * @since 0.0.2
65 */
66 protected $error_msg;
67
68 /**
69 * Represents the identifier of the block.
70 *
71 * @var string $block_id Unique identifier representing the block.
72 * @since 0.0.2
73 */
74 protected $block_id;
75
76 /**
77 * Stores the ID of the form.
78 *
79 * @var string $form_id Form ID.
80 * @since 0.0.2
81 */
82 protected $form_id;
83
84 /**
85 * Stores the block slug.
86 *
87 * @var string
88 * @since 0.0.2
89 */
90 protected $block_slug;
91
92 /**
93 * Stores the conditional class.
94 *
95 * @var string $conditional_class class name.
96 * @since 0.0.2
97 */
98 protected $conditional_class;
99
100 /**
101 * Indicates whether the attribute should be set to true or false.
102 *
103 * @var string $aria_require_attr Value of the aria-required attribute.
104 * @since 0.0.2
105 */
106 protected $aria_require_attr;
107
108 /**
109 * Dynamically sets the CSS class for block width based on the field width.
110 *
111 * @var string $block_width The CSS class for block width, dynamically generated from $field_width.
112 * @since 0.0.2
113 */
114 protected $block_width;
115
116 /**
117 * Stores the class name.
118 *
119 * @var string $class_name The value of the class name attribute.
120 * @since 0.0.2
121 */
122 protected $class_name;
123
124 /**
125 * Stores the placeholder text.
126 *
127 * @var string $placeholder HTML field placeholder.
128 * @since 0.0.2
129 */
130 protected $placeholder;
131
132 /**
133 * Stores the HTML placeholder attribute.
134 *
135 * @var string $placeholder_attr HTML field placeholder attribute.
136 * @since 0.0.2
137 */
138 protected $placeholder_attr;
139
140 /**
141 * Default value for the field.
142 *
143 * @var string
144 * @since 0.0.2
145 */
146 protected $default;
147
148 /**
149 * HTML attribute string for the default value.
150 *
151 * @var string
152 * @since 0.0.2
153 */
154 protected $default_value_attr;
155
156 /**
157 * Stores the input label.
158 *
159 * @var string $input_label input label.
160 * @since 0.0.2
161 */
162 protected $input_label;
163
164 /**
165 * Stores the default fallback value of the label for an input field if nothing is specified.
166 *
167 * @var string $input_label_fallback Default fallback value for the input label.
168 * @since 0.0.2
169 */
170 protected $input_label_fallback;
171
172 /**
173 * Stores the field name.
174 *
175 * @var string $field_name HTML field name.
176 * @since 0.0.2
177 */
178 protected $field_name;
179
180 /**
181 * Stores the slug.
182 *
183 * @var string $slug slug value.
184 * @since 0.0.2
185 */
186 protected $slug;
187
188 /**
189 * Unique slug which combines the slug, block ID, and input label.
190 *
191 * @var string
192 * @since 0.0.2
193 */
194 protected $unique_slug;
195
196 /**
197 * Checked state for the field.
198 *
199 * @var string
200 * @since 0.0.2
201 */
202 protected $checked;
203
204 /**
205 * HTML attribute string for the checked state.
206 *
207 * @var string
208 * @since 0.0.2
209 */
210 protected $checked_attr;
211
212 /**
213 * Options for the field.
214 *
215 * @var array<mixed>
216 * @since 0.0.2
217 */
218 protected $options;
219
220 /**
221 * Allowed HTML tags for the field.
222 *
223 * @var array<string, array<array<string>>>
224 * @since 0.0.2
225 */
226 protected $allowed_tags;
227
228 /**
229 * Flag indicating if the field value must be unique.
230 *
231 * @var bool
232 * @since 0.0.2
233 */
234 protected $is_unique;
235
236 /**
237 * Stores the flag if the field value must be unique.
238 *
239 * @var string
240 * @since 0.0.2
241 */
242 protected $aria_unique;
243
244 /**
245 * Duplicate value message for the field.
246 *
247 * @var string
248 * @since 0.0.2
249 */
250 protected $duplicate_msg;
251
252 /**
253 * Stores the help text markup.
254 *
255 * @var string
256 * @since 0.0.2
257 */
258 protected $help_markup;
259
260 /**
261 * Stores the error message markup.
262 *
263 * @var string
264 * @since 0.0.2
265 */
266 protected $error_msg_markup;
267
268 /**
269 * Stores the HTML label markup.
270 *
271 * @var string
272 * @since 0.0.2
273 */
274 protected $label_markup;
275
276 /**
277 * Stores the error icon to be used in HTML markup.
278 *
279 * @var string
280 * @since 0.0.2
281 */
282 protected $error_svg;
283
284 /**
285 * Stores the duplicate message markup for a field.
286 *
287 * @var string
288 * @since 0.0.2
289 */
290 protected $duplicate_msg_markup;
291
292 /**
293 * Setter for the properties of class based on block attributes.
294 *
295 * @param array<mixed> $attributes Block attributes.
296 * @since 0.0.2
297 * @return void
298 */
299 protected function set_properties( $attributes ) {
300 $this->required = isset( $attributes['required'] ) ? $attributes['required'] : false;
301 $this->field_width = isset( $attributes['fieldWidth'] ) ? $attributes['fieldWidth'] : '';
302 $this->label = isset( $attributes['label'] ) ? $attributes['label'] : '';
303 $this->help = isset( $attributes['help'] ) ? $attributes['help'] : '';
304 $this->block_id = isset( $attributes['block_id'] ) ? Helper::get_string_value( $attributes['block_id'] ) : '';
305 $this->form_id = isset( $attributes['formId'] ) ? Helper::get_string_value( $attributes['formId'] ) : '';
306 $this->block_slug = isset( $attributes['slug'] ) ? $attributes['slug'] : '';
307 $this->class_name = isset( $attributes['className'] ) ? ' ' . $attributes['className'] : '';
308 $this->placeholder = isset( $attributes['placeholder'] ) ? $attributes['placeholder'] : '';
309 $this->default = isset( $attributes['defaultValue'] ) ? $attributes['defaultValue'] : '';
310 $this->checked = isset( $attributes['checked'] ) ? $attributes['checked'] : '';
311 $this->options = isset( $attributes['options'] ) ? $attributes['options'] : '';
312 $this->is_unique = isset( $attributes['isUnique'] ) ? $attributes['isUnique'] : false;
313 $this->conditional_class = apply_filters( 'srfm_conditional_logic_classes', $this->form_id, $this->block_id );
314 $this->aria_require_attr = $this->required ? 'true' : 'false';
315 $this->block_width = $this->field_width ? ' srfm-block-width-' . str_replace( '.', '-', $this->field_width ) : '';
316 $this->placeholder_attr = $this->placeholder ? ' placeholder="' . $this->placeholder . '" ' : '';
317 $this->default_value_attr = $this->default ? ' value="' . $this->default . '" ' : '';
318 $this->checked_attr = $this->checked ? 'checked' : '';
319 $this->aria_unique = $this->is_unique ? 'true' : 'false';
320 $this->allowed_tags = [
321 'a' => [
322 'href' => [],
323 'target' => [],
324 ],
325 ];
326 }
327
328 /**
329 * Setter for the label of input field if available, otherwise provided value is utilized.
330 * Invokes the set_field_name() function to set the field_name property.
331 *
332 * @param string $value The default fallback text.
333 * @since 0.0.2
334 * @return void
335 */
336 protected function set_input_label( $value ) {
337 $this->input_label_fallback = $this->label ? $this->label : $value;
338 $this->input_label = '-lbl-' . Helper::encrypt( $this->input_label_fallback );
339 $this->set_field_name( $this->input_label );
340 }
341
342 /**
343 * Setter for the field name property.
344 *
345 * @param string $string Contains $input_label value or the $unique_slug based on the block.
346 * @since 0.0.2
347 * @return void
348 */
349 protected function set_field_name( $string ) {
350 $this->field_name = $string . '-' . $this->block_slug;
351 }
352
353 /**
354 * Setter for error message for the block.
355 *
356 * @param array<mixed> $attributes Block attributes, expected to contain 'errorMsg' key.
357 * @param string $key meta key name.
358 * @since 0.0.2
359 * @return void
360 */
361 protected function set_error_msg( $attributes, $key = '' ) {
362 if ( empty( $key ) ) {
363 $this->error_msg = isset( $attributes['errorMsg'] ) ? $attributes['errorMsg'] : '';
364 } else {
365 $this->error_msg = isset( $attributes['errorMsg'] ) && $attributes['errorMsg'] ? $attributes['errorMsg'] : Helper::get_default_dynamic_block_option( $key );
366 }
367 }
368
369 /**
370 * Setter for duplicate message value for the block.
371 *
372 * @param array<mixed> $attributes Block attributes, expected to contain 'errorMsg' key.
373 * @param string $key meta key name.
374 * @since 0.0.2
375 * @return void
376 */
377 protected function set_duplicate_msg( $attributes, $key ) {
378 $this->duplicate_msg = ! empty( $attributes['duplicateMsg'] ) ? $attributes['duplicateMsg'] : Helper::get_default_dynamic_block_option( $key );
379 }
380
381 /**
382 * Setter for unique slug.
383 *
384 * @since 0.0.2
385 * @return void
386 */
387 protected function set_unique_slug() {
388 $this->unique_slug = 'srfm-' . $this->slug . '-' . $this->block_id . $this->input_label;
389 }
390
391 /**
392 * Setter for the markup properties used in rendering the HTML markup of blocks.
393 * The parameters are for generating the label and error message markup of blocks.
394 *
395 * @param string $input_label Optional. Additional label to be appended to the block ID.
396 * @param bool $override Optional. Override for error markup. Default is false.
397 * @since 0.0.2
398 * @return void
399 */
400 protected function set_markup_properties( $input_label = '', $override = false ) {
401 $this->help_markup = Helper::generate_common_form_markup( $this->form_id, 'help', '', '', '', false, $this->help );
402 $this->error_msg_markup = Helper::generate_common_form_markup( $this->form_id, 'error', '', '', '', boolval( $this->required ), '', $this->error_msg, false, '', $override );
403 $this->label_markup = Helper::generate_common_form_markup( $this->form_id, 'label', $this->label, $this->slug, $this->block_id . $input_label, boolval( $this->required ) );
404 $this->error_svg = Helper::fetch_svg( 'error', 'srfm-error-icon' );
405 $this->duplicate_msg_markup = Helper::generate_common_form_markup( $this->form_id, 'error', '', '', '', boolval( $this->required ), '', $this->error_msg, false, $this->duplicate_msg, $this->is_unique );
406 }
407
408 /**
409 * Render the sureforms default
410 *
411 * @since 0.0.2
412 * @return string|boolean
413 */
414 public function markup() {
415 return '';
416 }
417 }
418