| @@ -24,8 +24,16 @@ | ||
| 24 | 24 | * @since 0.0.1 |
| 25 | 25 | */ |
| 26 | 26 | class Base { |
| 27 | 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 | + /** | |
| 28 | 36 | * Flag indicating if the field is required. |
| 29 | 37 | * |
| 30 | 38 | * @var bool |
| 31 | 39 | * @since 0.0.2 |
| @@ -99,12 +107,12 @@ | ||
| 99 | 107 | |
| 100 | 108 | /** |
| 101 | 109 | * Indicates whether the attribute should be set to true or false. |
| 102 | 110 | * |
| 103 | - * @var string $aria_require_attr Value of the aria-required attribute. | |
| 111 | + * @var string $data_require_attr Value of the data-required attribute. | |
| 104 | 112 | * @since 0.0.2 |
| 105 | 113 | */ |
| 106 | - protected $aria_require_attr; | |
| 114 | + protected $data_require_attr; | |
| 107 | 115 | |
| 108 | 116 | /** |
| 109 | 117 | * Dynamically sets the CSS class for block width based on the field width. |
| 110 | 118 | * |
| @@ -273,31 +281,106 @@ | ||
| 273 | 281 | */ |
| 274 | 282 | protected $label_markup; |
| 275 | 283 | |
| 276 | 284 | /** |
| 277 | - * Stores the error icon to be used in HTML markup. | |
| 285 | + * Stores the duplicate message markup for a field. | |
| 278 | 286 | * |
| 279 | 287 | * @var string |
| 280 | 288 | * @since 0.0.2 |
| 281 | 289 | */ |
| 282 | - protected $error_svg; | |
| 290 | + protected $duplicate_msg_markup; | |
| 283 | 291 | |
| 284 | 292 | /** |
| 285 | - * Stores the duplicate message markup for a field. | |
| 293 | + * Stores attribute for aria-describedby. | |
| 286 | 294 | * |
| 287 | 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 | + * | |
| 288 | 350 | * @since 0.0.2 |
| 351 | + * @return string|bool | |
| 289 | 352 | */ |
| 290 | - protected $duplicate_msg_markup; | |
| 353 | + public function markup() { | |
| 354 | + return ''; | |
| 355 | + } | |
| 291 | 356 | |
| 292 | 357 | /** |
| 293 | - * Stores attribute for aria-describedby. | |
| 358 | + * Constructor for the Base class. | |
| 294 | 359 | * |
| 295 | - * @var string | |
| 296 | - * @since 0.0.6 | |
| 360 | + * @param array<mixed> $extra_classes Extra classes to be added to the field. | |
| 361 | + * @since 1.5.0 | |
| 362 | + * @return string | |
| 297 | 363 | */ |
| 298 | - protected $aria_described_by; | |
| 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 | + ]; | |
| 299 | 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 | + | |
| 300 | 383 | /** |
| 301 | 384 | * Setter for the properties of class based on block attributes. |
| 302 | 385 | * |
| 303 | 386 | * @param array<mixed> $attributes Block attributes. |
| @@ -304,26 +387,36 @@ | ||
| 304 | 387 | * @since 0.0.2 |
| 305 | 388 | * @return void |
| 306 | 389 | */ |
| 307 | 390 | protected function set_properties( $attributes ) { |
| 308 | - $this->required = isset( $attributes['required'] ) ? $attributes['required'] : false; | |
| 309 | - $this->field_width = isset( $attributes['fieldWidth'] ) ? $attributes['fieldWidth'] : ''; | |
| 310 | - $this->label = isset( $attributes['label'] ) ? $attributes['label'] : ''; | |
| 311 | - $this->help = isset( $attributes['help'] ) ? $attributes['help'] : ''; | |
| 312 | - $this->block_id = isset( $attributes['block_id'] ) ? Helper::get_string_value( $attributes['block_id'] ) : ''; | |
| 313 | - $this->form_id = isset( $attributes['formId'] ) ? Helper::get_string_value( $attributes['formId'] ) : ''; | |
| 314 | - $this->block_slug = isset( $attributes['slug'] ) ? $attributes['slug'] : ''; | |
| 315 | - $this->class_name = isset( $attributes['className'] ) ? ' ' . $attributes['className'] : ''; | |
| 316 | - $this->placeholder = isset( $attributes['placeholder'] ) ? $attributes['placeholder'] : ''; | |
| 317 | - $this->default = isset( $attributes['defaultValue'] ) ? $attributes['defaultValue'] : ''; | |
| 318 | - $this->checked = isset( $attributes['checked'] ) ? $attributes['checked'] : ''; | |
| 319 | - $this->options = isset( $attributes['options'] ) ? $attributes['options'] : ''; | |
| 320 | - $this->is_unique = isset( $attributes['isUnique'] ) ? $attributes['isUnique'] : false; | |
| 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; | |
| 321 | 414 | $this->conditional_class = apply_filters( 'srfm_conditional_logic_classes', $this->form_id, $this->block_id ); |
| 322 | - $this->aria_require_attr = $this->required ? 'true' : 'false'; | |
| 415 | + $this->data_require_attr = $this->required ? 'true' : 'false'; | |
| 323 | 416 | $this->block_width = $this->field_width ? ' srfm-block-width-' . str_replace( '.', '-', $this->field_width ) : ''; |
| 324 | - $this->placeholder_attr = $this->placeholder ? ' placeholder="' . $this->placeholder . '" ' : ''; | |
| 325 | - $this->default_value_attr = $this->default ? ' value="' . $this->default . '" ' : ''; | |
| 417 | + $this->placeholder_attr = '' !== $this->placeholder ? ' placeholder="' . esc_attr( $this->placeholder ) . '" ' : ''; | |
| 418 | + $this->default_value_attr = '' !== $this->default ? ' value="' . esc_attr( $this->default ) . '" ' : ''; | |
| 326 | 419 | $this->checked_attr = $this->checked ? 'checked' : ''; |
| 327 | 420 | $this->aria_unique = $this->is_unique ? 'true' : 'false'; |
| 328 | 421 | $this->allowed_tags = [ |
| 329 | 422 | 'a' => [ |
| @@ -330,8 +423,13 @@ | ||
| 330 | 423 | 'href' => [], |
| 331 | 424 | 'target' => [], |
| 332 | 425 | ], |
| 333 | 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; | |
| 334 | 432 | } |
| 335 | 433 | |
| 336 | 434 | /** |
| 337 | 435 | * Setter for the label of input field if available, otherwise provided value is utilized. |
| @@ -342,9 +440,9 @@ | ||
| 342 | 440 | * @return void |
| 343 | 441 | */ |
| 344 | 442 | protected function set_input_label( $value ) { |
| 345 | 443 | $this->input_label_fallback = $this->label ? $this->label : $value; |
| 346 | - $this->input_label = '-lbl-' . Helper::encrypt( $this->input_label_fallback ); | |
| 444 | + $this->input_label = '-lbl-' . Helper::encode( $this->input_label_fallback ); | |
| 347 | 445 | $this->set_field_name( $this->input_label ); |
| 348 | 446 | } |
| 349 | 447 | |
| 350 | 448 | /** |
| @@ -367,9 +465,9 @@ | ||
| 367 | 465 | * @return void |
| 368 | 466 | */ |
| 369 | 467 | protected function set_error_msg( $attributes, $key = '' ) { |
| 370 | 468 | if ( empty( $key ) ) { |
| 371 | - $this->error_msg = isset( $attributes['errorMsg'] ) ? $attributes['errorMsg'] : ''; | |
| 469 | + $this->error_msg = $attributes['errorMsg'] ?? ''; | |
| 372 | 470 | } else { |
| 373 | 471 | $this->error_msg = isset( $attributes['errorMsg'] ) && $attributes['errorMsg'] ? $attributes['errorMsg'] : Helper::get_default_dynamic_block_option( $key ); |
| 374 | 472 | } |
| 375 | 473 | } |
| @@ -405,13 +503,14 @@ | ||
| 405 | 503 | * @since 0.0.2 |
| 406 | 504 | * @return void |
| 407 | 505 | */ |
| 408 | 506 | protected function set_markup_properties( $input_label = '', $override = false ) { |
| 409 | - $this->help_markup = Helper::generate_common_form_markup( $this->form_id, 'help', '', '', $this->block_id, false, $this->help ); | |
| 410 | - $this->error_msg_markup = Helper::generate_common_form_markup( $this->form_id, 'error', '', '', $this->block_id, boolval( $this->required ), '', $this->error_msg, false, '', $override ); | |
| 411 | - $this->label_markup = Helper::generate_common_form_markup( $this->form_id, 'label', $this->label, $this->slug, $this->block_id . $input_label, boolval( $this->required ) ); | |
| 412 | - $this->error_svg = Helper::fetch_svg( 'error', 'srfm-error-icon' ); | |
| 413 | - $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, $this->is_unique ); | |
| 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 ); | |
| 414 | 513 | } |
| 415 | 514 | |
| 416 | 515 | /** |
| 417 | 516 | * This function creates placeholder markup from label |
| @@ -425,9 +524,11 @@ | ||
| 425 | 524 | $this->placeholder_attr = ''; |
| 426 | 525 | $placeholder = Helper::generate_common_form_markup( $this->form_id, 'placeholder', $this->label, $this->slug, $this->block_id . $input_label, boolval( $this->required ) ); |
| 427 | 526 | if ( ! empty( $placeholder ) ) { |
| 428 | 527 | $this->label_markup = ''; |
| 429 | - $this->placeholder_attr = ' placeholder="' . $placeholder . '" '; | |
| 528 | + $this->placeholder_attr = ' placeholder="' . esc_attr( $placeholder ) . '" '; | |
| 529 | + } elseif ( '' !== $this->placeholder ) { | |
| 530 | + $this->placeholder_attr = ' placeholder="' . esc_attr( $this->placeholder ) . '" '; | |
| 430 | 531 | } |
| 431 | 532 | } |
| 432 | 533 | |
| 433 | 534 | /** |
| @@ -436,18 +537,8 @@ | ||
| 436 | 537 | * @since 0.0.6 |
| 437 | 538 | * @return void |
| 438 | 539 | */ |
| 439 | 540 | protected function set_aria_described_by() { |
| 440 | - $this->aria_described_by .= ! empty( $this->help ) ? ' srfm-description-' . $this->block_id : ''; | |
| 441 | - $this->aria_described_by .= ! empty( $this->required ) ? ' srfm-error-' . $this->block_id : ''; | |
| 442 | - } | |
| 443 | - | |
| 444 | - /** | |
| 445 | - * Render the sureforms default | |
| 446 | - * | |
| 447 | - * @since 0.0.2 | |
| 448 | - * @return string|boolean | |
| 449 | - */ | |
| 450 | - public function markup() { | |
| 451 | - return ''; | |
| 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 ) : ''; | |
| 452 | 543 | } |
| 453 | 544 | } |