| @@ -24,16 +24,8 @@ | ||
| 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 | - /** | |
| 36 | 28 | * Flag indicating if the field is required. |
| 37 | 29 | * |
| 38 | 30 | * @var bool |
| 39 | 31 | * @since 0.0.2 |
| @@ -281,8 +273,16 @@ | ||
| 281 | 273 | */ |
| 282 | 274 | protected $label_markup; |
| 283 | 275 | |
| 284 | 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 | 285 | * Stores the duplicate message markup for a field. |
| 286 | 286 | * |
| 287 | 287 | * @var string |
| 288 | 288 | * @since 0.0.2 |
| @@ -313,39 +313,8 @@ | ||
| 313 | 313 | */ |
| 314 | 314 | protected $max_selection; |
| 315 | 315 | |
| 316 | 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 | 317 | * Render the sureforms default |
| 349 | 318 | * |
| 350 | 319 | * @since 0.0.2 |
| 351 | 320 | * @return string|bool |
| @@ -354,34 +323,8 @@ | ||
| 354 | 323 | return ''; |
| 355 | 324 | } |
| 356 | 325 | |
| 357 | 326 | /** |
| 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 | 327 | * Setter for the properties of class based on block attributes. |
| 385 | 328 | * |
| 386 | 329 | * @param array<mixed> $attributes Block attributes. |
| 387 | 330 | * @since 0.0.2 |
| @@ -387,36 +330,26 @@ | ||
| 387 | 330 | * @since 0.0.2 |
| 388 | 331 | * @return void |
| 389 | 332 | */ |
| 390 | 333 | 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 ) ); | |
| 334 | + $this->required = $attributes['required'] ?? false; | |
| 335 | + $this->field_width = $attributes['fieldWidth'] ?? ''; | |
| 336 | + $this->label = $attributes['label'] ?? ''; | |
| 337 | + $this->help = $attributes['help'] ?? ''; | |
| 338 | + $this->block_id = isset( $attributes['block_id'] ) ? Helper::get_string_value( $attributes['block_id'] ) : ''; | |
| 339 | + $this->form_id = isset( $attributes['formId'] ) ? Helper::get_string_value( $attributes['formId'] ) : ''; | |
| 340 | + $this->block_slug = $attributes['slug'] ?? ''; | |
| 341 | + $this->class_name = isset( $attributes['className'] ) ? ' ' . $attributes['className'] : ''; | |
| 342 | + $this->placeholder = $attributes['placeholder'] ?? ''; | |
| 343 | + $this->default = $attributes['defaultValue'] ?? ''; | |
| 344 | + $this->checked = $attributes['checked'] ?? ''; | |
| 345 | + $this->options = $attributes['options'] ?? ''; | |
| 413 | 346 | $this->is_unique = $attributes['isUnique'] ?? false; |
| 414 | 347 | $this->conditional_class = apply_filters( 'srfm_conditional_logic_classes', $this->form_id, $this->block_id ); |
| 415 | 348 | $this->data_require_attr = $this->required ? 'true' : 'false'; |
| 416 | 349 | $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 ) . '" ' : ''; | |
| 350 | + $this->placeholder_attr = $this->placeholder ? ' placeholder="' . $this->placeholder . '" ' : ''; | |
| 351 | + $this->default_value_attr = $this->default ? ' value="' . $this->default . '" ' : ''; | |
| 419 | 352 | $this->checked_attr = $this->checked ? 'checked' : ''; |
| 420 | 353 | $this->aria_unique = $this->is_unique ? 'true' : 'false'; |
| 421 | 354 | $this->allowed_tags = [ |
| 422 | 355 | 'a' => [ |
| @@ -425,11 +358,8 @@ | ||
| 425 | 358 | ], |
| 426 | 359 | ]; |
| 427 | 360 | $this->min_selection = $attributes['minValue'] ?? ''; |
| 428 | 361 | $this->max_selection = $attributes['maxValue'] ?? ''; |
| 429 | - | |
| 430 | - $this->is_editing = isset( $attributes['isEditing'] ) ? true : false; | |
| 431 | - $this->entry_id = $attributes['entryID'] ?? 0; | |
| 432 | 362 | } |
| 433 | 363 | |
| 434 | 364 | /** |
| 435 | 365 | * Setter for the label of input field if available, otherwise provided value is utilized. |
| @@ -440,9 +370,9 @@ | ||
| 440 | 370 | * @return void |
| 441 | 371 | */ |
| 442 | 372 | protected function set_input_label( $value ) { |
| 443 | 373 | $this->input_label_fallback = $this->label ? $this->label : $value; |
| 444 | - $this->input_label = '-lbl-' . Helper::encode( $this->input_label_fallback ); | |
| 374 | + $this->input_label = '-lbl-' . Helper::encrypt( $this->input_label_fallback ); | |
| 445 | 375 | $this->set_field_name( $this->input_label ); |
| 446 | 376 | } |
| 447 | 377 | |
| 448 | 378 | /** |
| @@ -503,13 +433,12 @@ | ||
| 503 | 433 | * @since 0.0.2 |
| 504 | 434 | * @return void |
| 505 | 435 | */ |
| 506 | 436 | 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 | - | |
| 437 | + $this->help_markup = Helper::generate_common_form_markup( $this->form_id, 'help', '', '', $this->block_id, false, $this->help ); | |
| 438 | + $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 ); | |
| 439 | + $this->label_markup = Helper::generate_common_form_markup( $this->form_id, 'label', $this->label, $this->slug, $this->block_id . $input_label, boolval( $this->required ) ); | |
| 440 | + $this->error_svg = Helper::fetch_svg( 'error', 'srfm-error-icon' ); | |
| 512 | 441 | $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 | 442 | } |
| 514 | 443 | |
| 515 | 444 | /** |
| @@ -524,11 +453,9 @@ | ||
| 524 | 453 | $this->placeholder_attr = ''; |
| 525 | 454 | $placeholder = Helper::generate_common_form_markup( $this->form_id, 'placeholder', $this->label, $this->slug, $this->block_id . $input_label, boolval( $this->required ) ); |
| 526 | 455 | if ( ! empty( $placeholder ) ) { |
| 527 | 456 | $this->label_markup = ''; |
| 528 | - $this->placeholder_attr = ' placeholder="' . esc_attr( $placeholder ) . '" '; | |
| 529 | - } elseif ( '' !== $this->placeholder ) { | |
| 530 | - $this->placeholder_attr = ' placeholder="' . esc_attr( $this->placeholder ) . '" '; | |
| 457 | + $this->placeholder_attr = ' placeholder="' . $placeholder . '" '; | |
| 531 | 458 | } |
| 532 | 459 | } |
| 533 | 460 | |
| 534 | 461 | /** |
| @@ -537,8 +464,8 @@ | ||
| 537 | 464 | * @since 0.0.6 |
| 538 | 465 | * @return void |
| 539 | 466 | */ |
| 540 | 467 | 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 ) : ''; | |
| 468 | + $this->aria_described_by .= ! empty( $this->help ) ? ' srfm-description-' . $this->block_id : ''; | |
| 469 | + $this->aria_described_by .= ! empty( $this->required ) ? ' srfm-error-' . $this->block_id : ''; | |
| 543 | 470 | } |
| 544 | 471 | } |