| 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 |
* Stores attribute for aria-describedby. |
| 294 |
* |
| 295 |
* @var string |
| 296 |
* @since 0.0.6 |
| 297 |
*/ |
| 298 |
protected $aria_described_by; |
| 299 |
|
| 300 |
/** |
| 301 |
* Stores the minimum number of selections required. |
| 302 |
* |
| 303 |
* @var string |
| 304 |
* @since 0.0.13 |
| 305 |
*/ |
| 306 |
protected $min_selection; |
| 307 |
|
| 308 |
/** |
| 309 |
* Stores the maximum number of selections allowed. |
| 310 |
* |
| 311 |
* @var string |
| 312 |
* @since 0.0.13 |
| 313 |
*/ |
| 314 |
protected $max_selection; |
| 315 |
|
| 316 |
/** |
| 317 |
* Setter for the properties of class based on block attributes. |
| 318 |
* |
| 319 |
* @param array<mixed> $attributes Block attributes. |
| 320 |
* @since 0.0.2 |
| 321 |
* @return void |
| 322 |
*/ |
| 323 |
protected function set_properties( $attributes ) { |
| 324 |
$this->required = isset( $attributes['required'] ) ? $attributes['required'] : false; |
| 325 |
$this->field_width = isset( $attributes['fieldWidth'] ) ? $attributes['fieldWidth'] : ''; |
| 326 |
$this->label = isset( $attributes['label'] ) ? $attributes['label'] : ''; |
| 327 |
$this->help = isset( $attributes['help'] ) ? $attributes['help'] : ''; |
| 328 |
$this->block_id = isset( $attributes['block_id'] ) ? Helper::get_string_value( $attributes['block_id'] ) : ''; |
| 329 |
$this->form_id = isset( $attributes['formId'] ) ? Helper::get_string_value( $attributes['formId'] ) : ''; |
| 330 |
$this->block_slug = isset( $attributes['slug'] ) ? $attributes['slug'] : ''; |
| 331 |
$this->class_name = isset( $attributes['className'] ) ? ' ' . $attributes['className'] : ''; |
| 332 |
$this->placeholder = isset( $attributes['placeholder'] ) ? $attributes['placeholder'] : ''; |
| 333 |
$this->default = isset( $attributes['defaultValue'] ) ? $attributes['defaultValue'] : ''; |
| 334 |
$this->checked = isset( $attributes['checked'] ) ? $attributes['checked'] : ''; |
| 335 |
$this->options = isset( $attributes['options'] ) ? $attributes['options'] : ''; |
| 336 |
$this->is_unique = isset( $attributes['isUnique'] ) ? $attributes['isUnique'] : false; |
| 337 |
$this->conditional_class = apply_filters( 'srfm_conditional_logic_classes', $this->form_id, $this->block_id ); |
| 338 |
$this->aria_require_attr = $this->required ? 'true' : 'false'; |
| 339 |
$this->block_width = $this->field_width ? ' srfm-block-width-' . str_replace( '.', '-', $this->field_width ) : ''; |
| 340 |
$this->placeholder_attr = $this->placeholder ? ' placeholder="' . $this->placeholder . '" ' : ''; |
| 341 |
$this->default_value_attr = $this->default ? ' value="' . $this->default . '" ' : ''; |
| 342 |
$this->checked_attr = $this->checked ? 'checked' : ''; |
| 343 |
$this->aria_unique = $this->is_unique ? 'true' : 'false'; |
| 344 |
$this->allowed_tags = [ |
| 345 |
'a' => [ |
| 346 |
'href' => [], |
| 347 |
'target' => [], |
| 348 |
], |
| 349 |
]; |
| 350 |
$this->min_selection = isset( $attributes['minValue'] ) ? $attributes['minValue'] : ''; |
| 351 |
$this->max_selection = isset( $attributes['maxValue'] ) ? $attributes['maxValue'] : ''; |
| 352 |
} |
| 353 |
|
| 354 |
/** |
| 355 |
* Setter for the label of input field if available, otherwise provided value is utilized. |
| 356 |
* Invokes the set_field_name() function to set the field_name property. |
| 357 |
* |
| 358 |
* @param string $value The default fallback text. |
| 359 |
* @since 0.0.2 |
| 360 |
* @return void |
| 361 |
*/ |
| 362 |
protected function set_input_label( $value ) { |
| 363 |
$this->input_label_fallback = $this->label ? $this->label : $value; |
| 364 |
$this->input_label = '-lbl-' . Helper::encrypt( $this->input_label_fallback ); |
| 365 |
$this->set_field_name( $this->input_label ); |
| 366 |
} |
| 367 |
|
| 368 |
/** |
| 369 |
* Setter for the field name property. |
| 370 |
* |
| 371 |
* @param string $string Contains $input_label value or the $unique_slug based on the block. |
| 372 |
* @since 0.0.2 |
| 373 |
* @return void |
| 374 |
*/ |
| 375 |
protected function set_field_name( $string ) { |
| 376 |
$this->field_name = $string . '-' . $this->block_slug; |
| 377 |
} |
| 378 |
|
| 379 |
/** |
| 380 |
* Setter for error message for the block. |
| 381 |
* |
| 382 |
* @param array<mixed> $attributes Block attributes, expected to contain 'errorMsg' key. |
| 383 |
* @param string $key meta key name. |
| 384 |
* @since 0.0.2 |
| 385 |
* @return void |
| 386 |
*/ |
| 387 |
protected function set_error_msg( $attributes, $key = '' ) { |
| 388 |
if ( empty( $key ) ) { |
| 389 |
$this->error_msg = isset( $attributes['errorMsg'] ) ? $attributes['errorMsg'] : ''; |
| 390 |
} else { |
| 391 |
$this->error_msg = isset( $attributes['errorMsg'] ) && $attributes['errorMsg'] ? $attributes['errorMsg'] : Helper::get_default_dynamic_block_option( $key ); |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
/** |
| 396 |
* Setter for duplicate message value for the block. |
| 397 |
* |
| 398 |
* @param array<mixed> $attributes Block attributes, expected to contain 'errorMsg' key. |
| 399 |
* @param string $key meta key name. |
| 400 |
* @since 0.0.2 |
| 401 |
* @return void |
| 402 |
*/ |
| 403 |
protected function set_duplicate_msg( $attributes, $key ) { |
| 404 |
$this->duplicate_msg = ! empty( $attributes['duplicateMsg'] ) ? $attributes['duplicateMsg'] : Helper::get_default_dynamic_block_option( $key ); |
| 405 |
} |
| 406 |
|
| 407 |
/** |
| 408 |
* Setter for unique slug. |
| 409 |
* |
| 410 |
* @since 0.0.2 |
| 411 |
* @return void |
| 412 |
*/ |
| 413 |
protected function set_unique_slug() { |
| 414 |
$this->unique_slug = 'srfm-' . $this->slug . '-' . $this->block_id . $this->input_label; |
| 415 |
} |
| 416 |
|
| 417 |
/** |
| 418 |
* Setter for the markup properties used in rendering the HTML markup of blocks. |
| 419 |
* The parameters are for generating the label and error message markup of blocks. |
| 420 |
* |
| 421 |
* @param string $input_label Optional. Additional label to be appended to the block ID. |
| 422 |
* @param bool $override Optional. Override for error markup. Default is false. |
| 423 |
* @since 0.0.2 |
| 424 |
* @return void |
| 425 |
*/ |
| 426 |
protected function set_markup_properties( $input_label = '', $override = false ) { |
| 427 |
$this->help_markup = Helper::generate_common_form_markup( $this->form_id, 'help', '', '', $this->block_id, false, $this->help ); |
| 428 |
$this->error_msg_markup = Helper::generate_common_form_markup( $this->form_id, 'error', '', '', $this->block_id, boolval( $this->required || $this->min_selection || $this->max_selection ), '', $this->error_msg, false, '', $override ); |
| 429 |
$this->label_markup = Helper::generate_common_form_markup( $this->form_id, 'label', $this->label, $this->slug, $this->block_id . $input_label, boolval( $this->required ) ); |
| 430 |
$this->error_svg = Helper::fetch_svg( 'error', 'srfm-error-icon' ); |
| 431 |
$this->duplicate_msg_markup = Helper::generate_common_form_markup( $this->form_id, 'error', '', '', $this->block_id, boolval( $this->required ), '', $this->error_msg, false, $this->duplicate_msg, $override ); |
| 432 |
} |
| 433 |
|
| 434 |
/** |
| 435 |
* This function creates placeholder markup from label |
| 436 |
* works when user selects option 'Use labels as placeholder' |
| 437 |
* |
| 438 |
* @param string $input_label label of block where functionality is required. |
| 439 |
* @since 0.0.7 |
| 440 |
* @return void |
| 441 |
*/ |
| 442 |
protected function set_label_as_placeholder( $input_label = '' ) { |
| 443 |
$this->placeholder_attr = ''; |
| 444 |
$placeholder = Helper::generate_common_form_markup( $this->form_id, 'placeholder', $this->label, $this->slug, $this->block_id . $input_label, boolval( $this->required ) ); |
| 445 |
if ( ! empty( $placeholder ) ) { |
| 446 |
$this->label_markup = ''; |
| 447 |
$this->placeholder_attr = ' placeholder="' . $placeholder . '" '; |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
/** |
| 452 |
* Setter for the aria-describedby attribute. |
| 453 |
* |
| 454 |
* @since 0.0.6 |
| 455 |
* @return void |
| 456 |
*/ |
| 457 |
protected function set_aria_described_by() { |
| 458 |
$this->aria_described_by .= ! empty( $this->help ) ? ' srfm-description-' . $this->block_id : ''; |
| 459 |
$this->aria_described_by .= ! empty( $this->required ) ? ' srfm-error-' . $this->block_id : ''; |
| 460 |
} |
| 461 |
|
| 462 |
/** |
| 463 |
* Render the sureforms default |
| 464 |
* |
| 465 |
* @since 0.0.2 |
| 466 |
* @return string|boolean |
| 467 |
*/ |
| 468 |
public function markup() { |
| 469 |
return ''; |
| 470 |
} |
| 471 |
} |
| 472 |
|