create-form.php
1 month ago
delete-form.php
5 months ago
duplicate-form.php
5 months ago
form-field-schema.php
2 months ago
form-metadata.php
1 month ago
get-form-stats.php
5 months ago
get-form.php
5 months ago
get-shortcode.php
5 months ago
list-forms.php
2 months ago
update-form.php
1 month ago
form-field-schema.php
185 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Form Field Schema Trait. |
| 4 | * |
| 5 | * Shared field-type schema and sanitization for form abilities. |
| 6 | * |
| 7 | * @package sureforms |
| 8 | * @since 2.5.2 |
| 9 | */ |
| 10 | |
| 11 | namespace SRFM\Inc\Abilities\Forms; |
| 12 | |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; // Exit if accessed directly. |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Trait Form_Field_Schema |
| 19 | * |
| 20 | * Provides reusable form-field schema definition and field sanitization |
| 21 | * for Create_Form and Update_Form abilities. |
| 22 | * |
| 23 | * @since 2.5.2 |
| 24 | */ |
| 25 | trait Form_Field_Schema { |
| 26 | /** |
| 27 | * Get the form field items schema (field types + properties). |
| 28 | * |
| 29 | * @since 2.5.2 |
| 30 | * @return array<string,mixed> |
| 31 | */ |
| 32 | protected function get_form_field_schema() { |
| 33 | $field_types = [ |
| 34 | 'input', |
| 35 | 'email', |
| 36 | 'url', |
| 37 | 'textarea', |
| 38 | 'multi-choice', |
| 39 | 'checkbox', |
| 40 | 'gdpr', |
| 41 | 'number', |
| 42 | 'phone', |
| 43 | 'dropdown', |
| 44 | 'address', |
| 45 | 'inline-button', |
| 46 | 'payment', |
| 47 | ]; |
| 48 | |
| 49 | /** |
| 50 | * Filter the allowed field types for form abilities. |
| 51 | * |
| 52 | * Pro and third-party plugins can use this to add their own field types. |
| 53 | * |
| 54 | * @param array<string> $field_types Array of field type slugs. |
| 55 | * @since 2.5.2 |
| 56 | */ |
| 57 | $field_types = apply_filters( 'srfm_ability_form_field_types', $field_types ); |
| 58 | |
| 59 | // Core field properties. |
| 60 | $field_properties = [ |
| 61 | 'label' => [ |
| 62 | 'type' => 'string', |
| 63 | 'description' => __( 'Field label. e.g. First Name', 'sureforms' ), |
| 64 | ], |
| 65 | 'required' => [ |
| 66 | 'type' => 'boolean', |
| 67 | 'description' => __( 'Whether the field is required.', 'sureforms' ), |
| 68 | ], |
| 69 | 'fieldType' => [ |
| 70 | 'type' => 'string', |
| 71 | 'description' => __( 'The field type.', 'sureforms' ), |
| 72 | 'enum' => $field_types, |
| 73 | ], |
| 74 | 'helpText' => [ |
| 75 | 'type' => 'string', |
| 76 | 'description' => __( 'Help text describing the field.', 'sureforms' ), |
| 77 | ], |
| 78 | 'defaultValue' => [ |
| 79 | 'type' => 'string', |
| 80 | 'description' => __( 'Default value for the field.', 'sureforms' ), |
| 81 | ], |
| 82 | 'fieldOptions' => [ |
| 83 | 'type' => 'array', |
| 84 | 'description' => __( 'Options for dropdown or multi-choice fields.', 'sureforms' ), |
| 85 | 'items' => [ |
| 86 | 'type' => 'object', |
| 87 | 'properties' => [ |
| 88 | 'optionTitle' => [ 'type' => 'string' ], |
| 89 | 'label' => [ 'type' => 'string' ], |
| 90 | ], |
| 91 | ], |
| 92 | ], |
| 93 | 'singleSelection' => [ |
| 94 | 'type' => 'boolean', |
| 95 | 'description' => __( 'Allow only single selection in multi-choice field.', 'sureforms' ), |
| 96 | ], |
| 97 | 'isUnique' => [ |
| 98 | 'type' => 'boolean', |
| 99 | 'description' => __( 'Whether the field value must be unique.', 'sureforms' ), |
| 100 | ], |
| 101 | 'textLength' => [ |
| 102 | 'type' => 'integer', |
| 103 | 'description' => __( 'Maximum character length.', 'sureforms' ), |
| 104 | ], |
| 105 | // `placeholder` — @since 2.10.0 (added alongside the HTML-form converter) |
| 106 | 'placeholder' => [ |
| 107 | 'type' => 'string', |
| 108 | 'description' => __( 'Placeholder text shown inside the empty input/textarea or as the empty first option in a dropdown.', 'sureforms' ), |
| 109 | ], |
| 110 | 'className' => [ |
| 111 | 'type' => 'string', |
| 112 | 'description' => __( 'Additional CSS class(es) for the field wrapper. Space-separate multiple classes, e.g. "vk-0 highlight".', 'sureforms' ), |
| 113 | ], |
| 114 | ]; |
| 115 | |
| 116 | /** |
| 117 | * Filter additional field properties for form ability schemas. |
| 118 | * |
| 119 | * Pro and third-party plugins can use this to add field-specific |
| 120 | * properties (e.g. upload, rating, date-picker, time-picker options). |
| 121 | * |
| 122 | * @param array<string,array<string,mixed>> $properties Additional field properties. Default empty array. |
| 123 | * @since 2.5.2 |
| 124 | */ |
| 125 | $additional_properties = apply_filters( 'srfm_ability_form_field_properties', [] ); |
| 126 | |
| 127 | if ( ! empty( $additional_properties ) && is_array( $additional_properties ) ) { |
| 128 | $field_properties = array_merge( $field_properties, $additional_properties ); |
| 129 | } |
| 130 | |
| 131 | return $field_properties; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Sanitize form field string properties before passing to Field_Mapping. |
| 136 | * |
| 137 | * @param array<int,array<string,mixed>> $fields Raw form fields from input. |
| 138 | * @since 2.5.2 |
| 139 | * @return array<int,array<string,mixed>> Sanitized form fields. |
| 140 | */ |
| 141 | protected function sanitize_form_fields( array $fields ) { |
| 142 | foreach ( $fields as $index => $field ) { |
| 143 | if ( ! is_array( $field ) ) { |
| 144 | continue; |
| 145 | } |
| 146 | if ( isset( $field['label'] ) && is_string( $field['label'] ) ) { |
| 147 | $fields[ $index ]['label'] = sanitize_text_field( $field['label'] ); |
| 148 | } |
| 149 | if ( isset( $field['helpText'] ) && is_string( $field['helpText'] ) ) { |
| 150 | $fields[ $index ]['helpText'] = sanitize_text_field( $field['helpText'] ); |
| 151 | } |
| 152 | if ( isset( $field['defaultValue'] ) && is_string( $field['defaultValue'] ) ) { |
| 153 | $fields[ $index ]['defaultValue'] = sanitize_text_field( $field['defaultValue'] ); |
| 154 | } |
| 155 | if ( isset( $field['placeholder'] ) && is_string( $field['placeholder'] ) ) { |
| 156 | $fields[ $index ]['placeholder'] = sanitize_text_field( $field['placeholder'] ); |
| 157 | } |
| 158 | if ( isset( $field['className'] ) && is_string( $field['className'] ) ) { |
| 159 | $classes = preg_split( '/\s+/', trim( $field['className'] ) ); |
| 160 | $fields[ $index ]['className'] = is_array( $classes ) |
| 161 | ? implode( ' ', array_filter( array_map( 'sanitize_html_class', $classes ) ) ) |
| 162 | : ''; |
| 163 | } |
| 164 | if ( ! empty( $field['fieldOptions'] ) && is_array( $field['fieldOptions'] ) ) { |
| 165 | // @phpstan-ignore-next-line -- $field['fieldOptions'] is validated as array above. |
| 166 | $field_options = $field['fieldOptions']; |
| 167 | foreach ( $field_options as $opt_index => $option ) { |
| 168 | if ( ! is_array( $option ) ) { |
| 169 | continue; |
| 170 | } |
| 171 | if ( isset( $option['optionTitle'] ) && is_string( $option['optionTitle'] ) ) { |
| 172 | $option['optionTitle'] = sanitize_text_field( $option['optionTitle'] ); |
| 173 | } |
| 174 | if ( isset( $option['label'] ) && is_string( $option['label'] ) ) { |
| 175 | $option['label'] = sanitize_text_field( $option['label'] ); |
| 176 | } |
| 177 | $field_options[ $opt_index ] = $option; |
| 178 | } |
| 179 | $fields[ $index ]['fieldOptions'] = $field_options; |
| 180 | } |
| 181 | } |
| 182 | return $fields; |
| 183 | } |
| 184 | } |
| 185 |