address-markup.php
1 month ago
base.php
2 months ago
checkbox-markup.php
11 months ago
countries.json
1 year ago
dropdown-markup.php
3 weeks ago
email-markup.php
1 month ago
gdpr-markup.php
3 months ago
inlinebutton-markup.php
1 week ago
input-markup.php
3 months ago
multichoice-markup.php
1 month ago
number-markup.php
3 months ago
payment-markup.php
1 month ago
phone-markup.php
2 weeks ago
textarea-markup.php
1 month ago
url-markup.php
3 months ago
base.php
545 lines
| 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 | * Stores the attributes of the block. |
| 29 | * |
| 30 | * @var array<mixed> $attributes Block attributes. |
| 31 | * @since 1.3.0 |
| 32 | */ |
| 33 | protected $attributes; |
| 34 | |
| 35 | /** |
| 36 | * Flag indicating if the field is required. |
| 37 | * |
| 38 | * @var bool |
| 39 | * @since 0.0.2 |
| 40 | */ |
| 41 | protected $required; |
| 42 | |
| 43 | /** |
| 44 | * Width of the field. |
| 45 | * |
| 46 | * @var string |
| 47 | * @since 0.0.2 |
| 48 | */ |
| 49 | protected $field_width; |
| 50 | |
| 51 | /** |
| 52 | * Stores the label for an input field. |
| 53 | * The value of this variable specifies the text displayed as the label for the corresponding input field when rendered. |
| 54 | * |
| 55 | * @var string $label Label used for the input field. |
| 56 | * @since 0.0.2 |
| 57 | */ |
| 58 | protected $label; |
| 59 | |
| 60 | /** |
| 61 | * Stores the string that provides help text. |
| 62 | * |
| 63 | * @var string $help |
| 64 | * @since 0.0.2 |
| 65 | */ |
| 66 | protected $help; |
| 67 | |
| 68 | /** |
| 69 | * Validation error message for the fields. |
| 70 | * |
| 71 | * @var string $error_msg Input field validation error message. |
| 72 | * @since 0.0.2 |
| 73 | */ |
| 74 | protected $error_msg; |
| 75 | |
| 76 | /** |
| 77 | * Represents the identifier of the block. |
| 78 | * |
| 79 | * @var string $block_id Unique identifier representing the block. |
| 80 | * @since 0.0.2 |
| 81 | */ |
| 82 | protected $block_id; |
| 83 | |
| 84 | /** |
| 85 | * Stores the ID of the form. |
| 86 | * |
| 87 | * @var string $form_id Form ID. |
| 88 | * @since 0.0.2 |
| 89 | */ |
| 90 | protected $form_id; |
| 91 | |
| 92 | /** |
| 93 | * Stores the block slug. |
| 94 | * |
| 95 | * @var string |
| 96 | * @since 0.0.2 |
| 97 | */ |
| 98 | protected $block_slug; |
| 99 | |
| 100 | /** |
| 101 | * Stores the conditional class. |
| 102 | * |
| 103 | * @var string $conditional_class class name. |
| 104 | * @since 0.0.2 |
| 105 | */ |
| 106 | protected $conditional_class; |
| 107 | |
| 108 | /** |
| 109 | * Indicates whether the attribute should be set to true or false. |
| 110 | * |
| 111 | * @var string $data_require_attr Value of the data-required attribute. |
| 112 | * @since 0.0.2 |
| 113 | */ |
| 114 | protected $data_require_attr; |
| 115 | |
| 116 | /** |
| 117 | * Dynamically sets the CSS class for block width based on the field width. |
| 118 | * |
| 119 | * @var string $block_width The CSS class for block width, dynamically generated from $field_width. |
| 120 | * @since 0.0.2 |
| 121 | */ |
| 122 | protected $block_width; |
| 123 | |
| 124 | /** |
| 125 | * Stores the class name. |
| 126 | * |
| 127 | * @var string $class_name The value of the class name attribute. |
| 128 | * @since 0.0.2 |
| 129 | */ |
| 130 | protected $class_name; |
| 131 | |
| 132 | /** |
| 133 | * Stores the placeholder text. |
| 134 | * |
| 135 | * @var string $placeholder HTML field placeholder. |
| 136 | * @since 0.0.2 |
| 137 | */ |
| 138 | protected $placeholder; |
| 139 | |
| 140 | /** |
| 141 | * Stores the HTML placeholder attribute. |
| 142 | * |
| 143 | * @var string $placeholder_attr HTML field placeholder attribute. |
| 144 | * @since 0.0.2 |
| 145 | */ |
| 146 | protected $placeholder_attr; |
| 147 | |
| 148 | /** |
| 149 | * Default value for the field. |
| 150 | * |
| 151 | * @var string |
| 152 | * @since 0.0.2 |
| 153 | */ |
| 154 | protected $default; |
| 155 | |
| 156 | /** |
| 157 | * HTML attribute string for the default value. |
| 158 | * |
| 159 | * @var string |
| 160 | * @since 0.0.2 |
| 161 | */ |
| 162 | protected $default_value_attr; |
| 163 | |
| 164 | /** |
| 165 | * Stores the input label. |
| 166 | * |
| 167 | * @var string $input_label input label. |
| 168 | * @since 0.0.2 |
| 169 | */ |
| 170 | protected $input_label; |
| 171 | |
| 172 | /** |
| 173 | * Stores the default fallback value of the label for an input field if nothing is specified. |
| 174 | * |
| 175 | * @var string $input_label_fallback Default fallback value for the input label. |
| 176 | * @since 0.0.2 |
| 177 | */ |
| 178 | protected $input_label_fallback; |
| 179 | |
| 180 | /** |
| 181 | * Stores the field name. |
| 182 | * |
| 183 | * @var string $field_name HTML field name. |
| 184 | * @since 0.0.2 |
| 185 | */ |
| 186 | protected $field_name; |
| 187 | |
| 188 | /** |
| 189 | * Stores the slug. |
| 190 | * |
| 191 | * @var string $slug slug value. |
| 192 | * @since 0.0.2 |
| 193 | */ |
| 194 | protected $slug; |
| 195 | |
| 196 | /** |
| 197 | * Unique slug which combines the slug, block ID, and input label. |
| 198 | * |
| 199 | * @var string |
| 200 | * @since 0.0.2 |
| 201 | */ |
| 202 | protected $unique_slug; |
| 203 | |
| 204 | /** |
| 205 | * Checked state for the field. |
| 206 | * |
| 207 | * @var string |
| 208 | * @since 0.0.2 |
| 209 | */ |
| 210 | protected $checked; |
| 211 | |
| 212 | /** |
| 213 | * HTML attribute string for the checked state. |
| 214 | * |
| 215 | * @var string |
| 216 | * @since 0.0.2 |
| 217 | */ |
| 218 | protected $checked_attr; |
| 219 | |
| 220 | /** |
| 221 | * Options for the field. |
| 222 | * |
| 223 | * @var array<mixed> |
| 224 | * @since 0.0.2 |
| 225 | */ |
| 226 | protected $options; |
| 227 | |
| 228 | /** |
| 229 | * Allowed HTML tags for the field. |
| 230 | * |
| 231 | * @var array<string, array<array<string>>> |
| 232 | * @since 0.0.2 |
| 233 | */ |
| 234 | protected $allowed_tags; |
| 235 | |
| 236 | /** |
| 237 | * Flag indicating if the field value must be unique. |
| 238 | * |
| 239 | * @var bool |
| 240 | * @since 0.0.2 |
| 241 | */ |
| 242 | protected $is_unique; |
| 243 | |
| 244 | /** |
| 245 | * Stores the flag if the field value must be unique. |
| 246 | * |
| 247 | * @var string |
| 248 | * @since 0.0.2 |
| 249 | */ |
| 250 | protected $aria_unique; |
| 251 | |
| 252 | /** |
| 253 | * Duplicate value message for the field. |
| 254 | * |
| 255 | * @var string |
| 256 | * @since 0.0.2 |
| 257 | */ |
| 258 | protected $duplicate_msg; |
| 259 | |
| 260 | /** |
| 261 | * Stores the help text markup. |
| 262 | * |
| 263 | * @var string |
| 264 | * @since 0.0.2 |
| 265 | */ |
| 266 | protected $help_markup; |
| 267 | |
| 268 | /** |
| 269 | * Stores the error message markup. |
| 270 | * |
| 271 | * @var string |
| 272 | * @since 0.0.2 |
| 273 | */ |
| 274 | protected $error_msg_markup; |
| 275 | |
| 276 | /** |
| 277 | * Stores the HTML label markup. |
| 278 | * |
| 279 | * @var string |
| 280 | * @since 0.0.2 |
| 281 | */ |
| 282 | protected $label_markup; |
| 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 | * Whether or not block is render in editing mode. |
| 318 | * If it is true, then block is currently rendered in edit entry. |
| 319 | * |
| 320 | * @var bool |
| 321 | * @since 1.3.0 |
| 322 | */ |
| 323 | protected $is_editing = false; |
| 324 | |
| 325 | /** |
| 326 | * Currently rendered entry ID. |
| 327 | * |
| 328 | * @var int |
| 329 | * @since 1.3.0 |
| 330 | */ |
| 331 | protected $entry_id = 0; |
| 332 | |
| 333 | /** |
| 334 | * Configuration array for the field. |
| 335 | * |
| 336 | * This associative array contains configuration settings for the field, |
| 337 | * which can be used for various JavaScript functionalities such as |
| 338 | * calculation, validation, and conditional logic (in future). |
| 339 | * The array data will be stored in the block field's "data-field-config" |
| 340 | * attribute as a JSON string. |
| 341 | * |
| 342 | * @var string $field_config Configuration settings for the field. |
| 343 | * @since 1.5.0 |
| 344 | */ |
| 345 | protected $field_config; |
| 346 | |
| 347 | /** |
| 348 | * Render the sureforms default |
| 349 | * |
| 350 | * @since 0.0.2 |
| 351 | * @return string|bool |
| 352 | */ |
| 353 | public function markup() { |
| 354 | return ''; |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Constructor for the Base class. |
| 359 | * |
| 360 | * @param array<mixed> $extra_classes Extra classes to be added to the field. |
| 361 | * @since 1.5.0 |
| 362 | * @return string |
| 363 | */ |
| 364 | public function get_field_classes( $extra_classes = [] ) { |
| 365 | $common_classes = [ |
| 366 | 'srfm-block-single', |
| 367 | 'srfm-block', |
| 368 | "srfm-{$this->slug}-block", |
| 369 | "srf-{$this->slug}-{$this->block_id}-block", |
| 370 | $this->block_width, |
| 371 | $this->class_name, |
| 372 | "srfm-slug-{$this->block_slug}", |
| 373 | $this->conditional_class, |
| 374 | ]; |
| 375 | |
| 376 | if ( ! empty( $extra_classes ) && is_array( $extra_classes ) ) { |
| 377 | $common_classes = array_merge( $common_classes, $extra_classes ); |
| 378 | } |
| 379 | |
| 380 | return Helper::join_strings( $common_classes ); |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Setter for the properties of class based on block attributes. |
| 385 | * |
| 386 | * @param array<mixed> $attributes Block attributes. |
| 387 | * @since 0.0.2 |
| 388 | * @return void |
| 389 | */ |
| 390 | protected function set_properties( $attributes ) { |
| 391 | $default_classes = isset( $attributes['className'] ) ? ' ' . $attributes['className'] : ''; |
| 392 | $filter_classes = apply_filters( 'srfm_field_classes', $default_classes, [ 'attributes' => $attributes ] ); |
| 393 | $field_config = apply_filters( 'srfm_field_config', [], [ 'attributes' => $attributes ] ); |
| 394 | |
| 395 | $this->field_config = $field_config ? htmlspecialchars( Helper::get_string_value( wp_json_encode( $field_config, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) ), ENT_QUOTES, 'UTF-8' ) : ''; |
| 396 | $this->attributes = $attributes; |
| 397 | $this->required = $attributes['required'] ?? false; |
| 398 | $this->field_width = $attributes['fieldWidth'] ?? ''; |
| 399 | $this->label = $attributes['label'] ?? ''; |
| 400 | $this->help = $attributes['help'] ?? ''; |
| 401 | $this->block_id = isset( $attributes['block_id'] ) ? Helper::get_string_value( $attributes['block_id'] ) : ''; |
| 402 | $this->form_id = isset( $attributes['formId'] ) ? Helper::get_string_value( $attributes['formId'] ) : ''; |
| 403 | $this->block_slug = $attributes['slug'] ?? ''; |
| 404 | $this->class_name = $filter_classes; |
| 405 | $this->placeholder = $attributes['placeholder'] ?? ''; |
| 406 | $this->default = $attributes['defaultValue'] ?? ''; |
| 407 | $this->checked = $attributes['checked'] ?? ''; |
| 408 | // Inject the block type slug (e.g. 'dropdown', 'multi-choice') into the attributes passed to |
| 409 | // the filter. The field's `slug` attribute can be prefixed when the block is inside a repeater |
| 410 | // (e.g. 'srfm-repeater-srfm-dropdown'), so filter consumers need a reliable block type identifier. |
| 411 | $block_args = [ 'block_args' => [ 'slug' => $this->slug ] ]; |
| 412 | $this->options = apply_filters( 'srfm_field_options', $attributes['options'] ?? '', array_merge( $attributes, $block_args ) ); |
| 413 | $this->is_unique = $attributes['isUnique'] ?? false; |
| 414 | $this->conditional_class = apply_filters( 'srfm_conditional_logic_classes', $this->form_id, $this->block_id ); |
| 415 | $this->data_require_attr = $this->required ? 'true' : 'false'; |
| 416 | $this->block_width = $this->field_width ? ' srfm-block-width-' . str_replace( '.', '-', $this->field_width ) : ''; |
| 417 | $this->placeholder_attr = '' !== $this->placeholder ? ' placeholder="' . esc_attr( $this->placeholder ) . '" ' : ''; |
| 418 | $this->default_value_attr = '' !== $this->default ? ' value="' . esc_attr( $this->default ) . '" ' : ''; |
| 419 | $this->checked_attr = $this->checked ? 'checked' : ''; |
| 420 | $this->aria_unique = $this->is_unique ? 'true' : 'false'; |
| 421 | $this->allowed_tags = [ |
| 422 | 'a' => [ |
| 423 | 'href' => [], |
| 424 | 'target' => [], |
| 425 | ], |
| 426 | ]; |
| 427 | $this->min_selection = $attributes['minValue'] ?? ''; |
| 428 | $this->max_selection = $attributes['maxValue'] ?? ''; |
| 429 | |
| 430 | $this->is_editing = isset( $attributes['isEditing'] ) ? true : false; |
| 431 | $this->entry_id = $attributes['entryID'] ?? 0; |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Setter for the label of input field if available, otherwise provided value is utilized. |
| 436 | * Invokes the set_field_name() function to set the field_name property. |
| 437 | * |
| 438 | * @param string $value The default fallback text. |
| 439 | * @since 0.0.2 |
| 440 | * @return void |
| 441 | */ |
| 442 | protected function set_input_label( $value ) { |
| 443 | $this->input_label_fallback = $this->label ? $this->label : $value; |
| 444 | $this->input_label = '-lbl-' . Helper::encrypt( $this->input_label_fallback ); |
| 445 | $this->set_field_name( $this->input_label ); |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * Setter for the field name property. |
| 450 | * |
| 451 | * @param string $string Contains $input_label value or the $unique_slug based on the block. |
| 452 | * @since 0.0.2 |
| 453 | * @return void |
| 454 | */ |
| 455 | protected function set_field_name( $string ) { |
| 456 | $this->field_name = $string . '-' . $this->block_slug; |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Setter for error message for the block. |
| 461 | * |
| 462 | * @param array<mixed> $attributes Block attributes, expected to contain 'errorMsg' key. |
| 463 | * @param string $key meta key name. |
| 464 | * @since 0.0.2 |
| 465 | * @return void |
| 466 | */ |
| 467 | protected function set_error_msg( $attributes, $key = '' ) { |
| 468 | if ( empty( $key ) ) { |
| 469 | $this->error_msg = $attributes['errorMsg'] ?? ''; |
| 470 | } else { |
| 471 | $this->error_msg = isset( $attributes['errorMsg'] ) && $attributes['errorMsg'] ? $attributes['errorMsg'] : Helper::get_default_dynamic_block_option( $key ); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * Setter for duplicate message value for the block. |
| 477 | * |
| 478 | * @param array<mixed> $attributes Block attributes, expected to contain 'errorMsg' key. |
| 479 | * @param string $key meta key name. |
| 480 | * @since 0.0.2 |
| 481 | * @return void |
| 482 | */ |
| 483 | protected function set_duplicate_msg( $attributes, $key ) { |
| 484 | $this->duplicate_msg = ! empty( $attributes['duplicateMsg'] ) ? $attributes['duplicateMsg'] : Helper::get_default_dynamic_block_option( $key ); |
| 485 | } |
| 486 | |
| 487 | /** |
| 488 | * Setter for unique slug. |
| 489 | * |
| 490 | * @since 0.0.2 |
| 491 | * @return void |
| 492 | */ |
| 493 | protected function set_unique_slug() { |
| 494 | $this->unique_slug = 'srfm-' . $this->slug . '-' . $this->block_id . $this->input_label; |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Setter for the markup properties used in rendering the HTML markup of blocks. |
| 499 | * The parameters are for generating the label and error message markup of blocks. |
| 500 | * |
| 501 | * @param string $input_label Optional. Additional label to be appended to the block ID. |
| 502 | * @param bool $override Optional. Override for error markup. Default is false. |
| 503 | * @since 0.0.2 |
| 504 | * @return void |
| 505 | */ |
| 506 | protected function set_markup_properties( $input_label = '', $override = false ) { |
| 507 | $this->help_markup = Helper::generate_common_form_markup( $this->form_id, 'help', '', '', $this->block_id, false, $this->help ); |
| 508 | $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 ); |
| 509 | $type = in_array( $this->slug, [ 'multi-choice', 'dropdown', 'address' ], true ) ? 'label_text' : 'label'; |
| 510 | $this->label_markup = Helper::generate_common_form_markup( $this->form_id, $type, $this->label, $this->slug, $this->block_id . $input_label, boolval( $this->required ) ); |
| 511 | |
| 512 | $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 ); |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * This function creates placeholder markup from label |
| 517 | * works when user selects option 'Use labels as placeholder' |
| 518 | * |
| 519 | * @param string $input_label label of block where functionality is required. |
| 520 | * @since 0.0.7 |
| 521 | * @return void |
| 522 | */ |
| 523 | protected function set_label_as_placeholder( $input_label = '' ) { |
| 524 | $this->placeholder_attr = ''; |
| 525 | $placeholder = Helper::generate_common_form_markup( $this->form_id, 'placeholder', $this->label, $this->slug, $this->block_id . $input_label, boolval( $this->required ) ); |
| 526 | if ( ! empty( $placeholder ) ) { |
| 527 | $this->label_markup = ''; |
| 528 | $this->placeholder_attr = ' placeholder="' . esc_attr( $placeholder ) . '" '; |
| 529 | } elseif ( '' !== $this->placeholder ) { |
| 530 | $this->placeholder_attr = ' placeholder="' . esc_attr( $this->placeholder ) . '" '; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | /** |
| 535 | * Setter for the aria-describedby attribute. |
| 536 | * |
| 537 | * @since 0.0.6 |
| 538 | * @return void |
| 539 | */ |
| 540 | protected function set_aria_described_by() { |
| 541 | $this->aria_described_by .= ' srfm-error-' . esc_attr( $this->block_id ); |
| 542 | $this->aria_described_by .= ! empty( $this->help ) ? ' srfm-description-' . esc_attr( $this->block_id ) : ''; |
| 543 | } |
| 544 | } |
| 545 |