PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 2.12.8
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v2.12.8
2.12.8 2.12.7 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 All 98 releases
← All changes | inc/fields/base.php +120 -47 0.0.13 → 2.12.8 View file →
@@ -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,16 +281,8 @@
273 281 */
274 282 protected $label_markup;
275 283
276 284 /**
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,8 +313,75 @@
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 + * 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 + /**
317 384 * Setter for the properties of class based on block attributes.
318 385 *
319 386 * @param array<mixed> $attributes Block attributes.
320 387 * @since 0.0.2
@@ -320,26 +387,36 @@
320 387 * @since 0.0.2
321 388 * @return void
322 389 */
323 390 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;
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;
337 414 $this->conditional_class = apply_filters( 'srfm_conditional_logic_classes', $this->form_id, $this->block_id );
338 - $this->aria_require_attr = $this->required ? 'true' : 'false';
415 + $this->data_require_attr = $this->required ? 'true' : 'false';
339 416 $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 . '" ' : '';
417 + $this->placeholder_attr = '' !== $this->placeholder ? ' placeholder="' . esc_attr( $this->placeholder ) . '" ' : '';
418 + $this->default_value_attr = '' !== $this->default ? ' value="' . esc_attr( $this->default ) . '" ' : '';
342 419 $this->checked_attr = $this->checked ? 'checked' : '';
343 420 $this->aria_unique = $this->is_unique ? 'true' : 'false';
344 421 $this->allowed_tags = [
345 422 'a' => [
@@ -346,10 +423,13 @@
346 423 'href' => [],
347 424 'target' => [],
348 425 ],
349 426 ];
350 - $this->min_selection = isset( $attributes['minValue'] ) ? $attributes['minValue'] : '';
351 - $this->max_selection = isset( $attributes['maxValue'] ) ? $attributes['maxValue'] : '';
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;
352 432 }
353 433
354 434 /**
355 435 * Setter for the label of input field if available, otherwise provided value is utilized.
@@ -360,9 +440,9 @@
360 440 * @return void
361 441 */
362 442 protected function set_input_label( $value ) {
363 443 $this->input_label_fallback = $this->label ? $this->label : $value;
364 - $this->input_label = '-lbl-' . Helper::encrypt( $this->input_label_fallback );
444 + $this->input_label = '-lbl-' . Helper::encode( $this->input_label_fallback );
365 445 $this->set_field_name( $this->input_label );
366 446 }
367 447
368 448 /**
@@ -385,9 +465,9 @@
385 465 * @return void
386 466 */
387 467 protected function set_error_msg( $attributes, $key = '' ) {
388 468 if ( empty( $key ) ) {
389 - $this->error_msg = isset( $attributes['errorMsg'] ) ? $attributes['errorMsg'] : '';
469 + $this->error_msg = $attributes['errorMsg'] ?? '';
390 470 } else {
391 471 $this->error_msg = isset( $attributes['errorMsg'] ) && $attributes['errorMsg'] ? $attributes['errorMsg'] : Helper::get_default_dynamic_block_option( $key );
392 472 }
393 473 }
@@ -423,12 +503,13 @@
423 503 * @since 0.0.2
424 504 * @return void
425 505 */
426 506 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' );
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 +
431 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 );
432 513 }
433 514
434 515 /**
@@ -443,9 +524,11 @@
443 524 $this->placeholder_attr = '';
444 525 $placeholder = Helper::generate_common_form_markup( $this->form_id, 'placeholder', $this->label, $this->slug, $this->block_id . $input_label, boolval( $this->required ) );
445 526 if ( ! empty( $placeholder ) ) {
446 527 $this->label_markup = '';
447 - $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 ) . '" ';
448 531 }
449 532 }
450 533
451 534 /**
@@ -454,18 +537,8 @@
454 537 * @since 0.0.6
455 538 * @return void
456 539 */
457 540 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 '';
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 ) : '';
470 543 }
471 544 }