PluginProbe ʕ •ᴥ•ʔ
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 2.12.3
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v2.12.3
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 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7 0.0.8 0.0.9 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.1.0 1.1.1 1.1.2 1.10.0 1.10.1 1.11.0 1.12.0 1.12.1 1.12.2 1.12.3 1.13.0 1.13.1 1.13.2 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.5.0 1.5.1 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.8.0 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.2.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.0 2.5.2 2.6.0
sureforms / inc / abilities / forms / form-field-schema.php
sureforms / inc / abilities / forms Last commit date
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