PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 2.7.0
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v2.7.0
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 0.0.2 0.0.3 All 96 releases
sureforms / inc / abilities / forms / form-metadata.php
form-metadata.php
270 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Form Metadata Trait.
4 *
5 * Shared metadata override logic for form abilities.
6 *
7 * @package sureforms
8 * @since 2.5.2
9 */
10
11 namespace SRFM\Inc\Abilities\Forms;
12
13 use SRFM\Inc\Helper;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit; // Exit if accessed directly.
17 }
18
19 /**
20 * Trait Form_Metadata
21 *
22 * Provides the shared apply_metadata_overrides() method used by
23 * Create_Form and Update_Form abilities.
24 *
25 * @since 2.5.2
26 */
27 trait Form_Metadata {
28 /**
29 * Apply metadata overrides from ability input to post meta.
30 *
31 * @param array<string,mixed> $post_metas Default or current post meta values.
32 * @param array<string,mixed> $meta_data Metadata from ability input.
33 * @since 2.5.2
34 * @return array<string,mixed>
35 */
36 protected function apply_metadata_overrides( $post_metas, $meta_data ) {
37 if ( empty( $meta_data ) ) {
38 return $post_metas;
39 }
40
41 // General settings.
42 $general = Helper::get_array_value( $meta_data['general'] ?? [] );
43 if ( ! empty( $general ) ) {
44 if ( isset( $general['submitText'] ) ) {
45 $post_metas['_srfm_submit_button_text'] = sanitize_text_field( Helper::get_string_value( $general['submitText'] ) );
46 }
47 if ( isset( $general['useLabelAsPlaceholder'] ) ) {
48 $post_metas['_srfm_use_label_as_placeholder'] = (bool) $general['useLabelAsPlaceholder'];
49 }
50 }
51
52 // Confirmation message.
53 $form_confirmation = Helper::get_array_value( $meta_data['formConfirmation'] ?? [] );
54 if ( ! empty( $form_confirmation['confirmationMessage'] ) ) {
55 $confirmation_data = Helper::get_array_value( $post_metas['_srfm_form_confirmation'] ?? [] );
56 $item = ! empty( $confirmation_data[0] ) && is_array( $confirmation_data[0] )
57 ? $confirmation_data[0]
58 : [
59 'id' => 1,
60 'confirmation_type' => 'same page',
61 'page_url' => '',
62 'custom_url' => '',
63 'message' => '',
64 'submission_action' => 'hide form',
65 ];
66
67 $item['message'] = wp_kses_post( Helper::get_string_value( $form_confirmation['confirmationMessage'] ) );
68 $confirmation_data[0] = $item;
69
70 $post_metas['_srfm_form_confirmation'] = $confirmation_data;
71 }
72
73 // Instant form settings.
74 $instant = Helper::get_array_value( $meta_data['instantForm'] ?? [] );
75 if ( ! empty( $instant ) ) {
76 $instant_settings = Helper::get_array_value( $post_metas['_srfm_instant_form_settings'] ?? [] );
77
78 if ( isset( $instant['instantForm'] ) ) {
79 $instant_settings['enable_instant_form'] = (bool) $instant['instantForm'];
80 }
81 if ( isset( $instant['showTitle'] ) ) {
82 $instant_settings['single_page_form_title'] = (bool) $instant['showTitle'];
83 }
84 if ( ! empty( $instant['formWidth'] ) ) {
85 $width = Helper::get_integer_value( $instant['formWidth'] );
86 if ( $width >= 560 && $width <= 1000 ) {
87 $instant_settings['form_container_width'] = $width;
88 }
89 }
90 if ( ! empty( $instant['formBackgroundColor'] ) ) {
91 $instant_settings['bg_color'] = sanitize_hex_color( Helper::get_string_value( $instant['formBackgroundColor'] ) );
92 }
93
94 $post_metas['_srfm_instant_form_settings'] = $instant_settings;
95 }
96
97 // Styling.
98 $styling = Helper::get_array_value( $meta_data['styling'] ?? [] );
99 if ( ! empty( $styling ) ) {
100 if ( ! empty( $styling['submitAlignment'] ) ) {
101 $valid_alignments = [ 'left', 'center', 'right', 'full-width' ];
102 if ( in_array( $styling['submitAlignment'], $valid_alignments, true ) ) {
103 $post_metas['_srfm_submit_alignment'] = sanitize_text_field( Helper::get_string_value( $styling['submitAlignment'] ) );
104 }
105 }
106
107 if ( 'full-width' === ( $styling['submitAlignment'] ?? '' ) ) {
108 $post_metas['_srfm_submit_width'] = '100%';
109 $post_metas['_srfm_submit_width_backend'] = '100%';
110 }
111 }
112
113 // Compliance settings.
114 $compliance = Helper::get_array_value( $meta_data['compliance'] ?? [] );
115 if ( ! empty( $compliance ) ) {
116 $compliance_data = Helper::get_array_value( $post_metas['_srfm_compliance'] ?? [] );
117 $item = ! empty( $compliance_data[0] ) && is_array( $compliance_data[0] )
118 ? $compliance_data[0]
119 : [
120 'id' => 'gdpr',
121 'gdpr' => false,
122 'do_not_store_entries' => false,
123 'auto_delete_entries' => false,
124 'auto_delete_days' => '',
125 ];
126
127 if ( ! empty( $compliance['enableCompliance'] ) ) {
128 $item['gdpr'] = true;
129
130 if ( ! empty( $compliance['neverStoreEntries'] ) ) {
131 $item['do_not_store_entries'] = true;
132 $item['auto_delete_entries'] = false;
133 } elseif ( ! empty( $compliance['autoDeleteEntries'] ) ) {
134 $item['do_not_store_entries'] = false;
135 $item['auto_delete_entries'] = true;
136 if ( ! empty( $compliance['autoDeleteEntriesDays'] ) ) {
137 $item['auto_delete_days'] = sanitize_text_field( Helper::get_string_value( $compliance['autoDeleteEntriesDays'] ) );
138 }
139 }
140 }
141
142 $post_metas['_srfm_compliance'] = [ $item ];
143 }
144
145 // Form styling.
146 $form_styling = Helper::get_array_value( $meta_data['formStyling'] ?? [] );
147 if ( ! empty( $form_styling ) ) {
148 $styling_data = Helper::get_array_value( $post_metas['_srfm_forms_styling'] ?? [] );
149
150 if ( ! empty( $form_styling['primaryColor'] ) ) {
151 $styling_data['primary_color'] = sanitize_hex_color( Helper::get_string_value( $form_styling['primaryColor'] ) );
152 }
153 if ( ! empty( $form_styling['textColor'] ) ) {
154 $styling_data['text_color'] = sanitize_hex_color( Helper::get_string_value( $form_styling['textColor'] ) );
155 }
156 if ( ! empty( $form_styling['textColorOnPrimary'] ) ) {
157 $styling_data['text_color_on_primary'] = sanitize_hex_color( Helper::get_string_value( $form_styling['textColorOnPrimary'] ) );
158 }
159 if ( ! empty( $form_styling['fieldSpacing'] ) ) {
160 $valid = [ 'small', 'medium', 'large' ];
161 if ( in_array( $form_styling['fieldSpacing'], $valid, true ) ) {
162 $styling_data['field_spacing'] = $form_styling['fieldSpacing'];
163 }
164 }
165
166 $post_metas['_srfm_forms_styling'] = $styling_data;
167 }
168
169 // Email notification — merges into first notification entry.
170 $email = Helper::get_array_value( $meta_data['emailNotification'] ?? [] );
171 if ( ! empty( $email ) ) {
172 $notif_data = Helper::get_array_value( $post_metas['_srfm_email_notification'] ?? [] );
173 $item = ! empty( $notif_data[0] ) && is_array( $notif_data[0] )
174 ? $notif_data[0]
175 : [
176 'id' => 1,
177 'status' => true,
178 'is_raw_format' => false,
179 'name' => '',
180 'email_to' => '{admin_email}',
181 'email_reply_to' => '{admin_email}',
182 'from_name' => '{site_title}',
183 'from_email' => '{admin_email}',
184 'email_cc' => '',
185 'email_bcc' => '',
186 'subject' => '',
187 'email_body' => '{all_data}',
188 ];
189
190 $field_map = [
191 'status' => 'status',
192 'name' => 'name',
193 'emailTo' => 'email_to',
194 'emailReplyTo' => 'email_reply_to',
195 'fromName' => 'from_name',
196 'fromEmail' => 'from_email',
197 'emailCc' => 'email_cc',
198 'emailBcc' => 'email_bcc',
199 'subject' => 'subject',
200 'emailBody' => 'email_body',
201 ];
202
203 foreach ( $field_map as $input_key => $meta_key ) {
204 if ( ! isset( $email[ $input_key ] ) ) {
205 continue;
206 }
207 if ( 'status' === $input_key ) {
208 $item[ $meta_key ] = (bool) $email[ $input_key ];
209 } else {
210 $item[ $meta_key ] = sanitize_text_field( Helper::get_string_value( $email[ $input_key ] ) );
211 }
212 }
213
214 $notif_data[0] = $item;
215 $post_metas['_srfm_email_notification'] = $notif_data;
216 }
217
218 // Form restriction — stored as JSON string.
219 $restriction = Helper::get_array_value( $meta_data['formRestriction'] ?? [] );
220 if ( ! empty( $restriction ) ) {
221 $restriction_json = Helper::get_string_value( $post_metas['_srfm_form_restriction'] ?? '' );
222 $restriction_data = ! empty( $restriction_json ) ? json_decode( $restriction_json, true ) : [];
223 if ( ! is_array( $restriction_data ) ) {
224 $restriction_data = [];
225 }
226
227 $bool_fields = [ 'status', 'schedulingStatus' ];
228 $int_fields = [ 'maxEntries' ];
229 $string_fields = [
230 'date',
231 'hours',
232 'minutes',
233 'meridiem',
234 'message',
235 'startDate',
236 'startHours',
237 'startMinutes',
238 'startMeridiem',
239 'schedulingNotStartedMessage',
240 'schedulingEndedMessage',
241 ];
242
243 foreach ( $bool_fields as $key ) {
244 if ( isset( $restriction[ $key ] ) ) {
245 $restriction_data[ $key ] = (bool) $restriction[ $key ];
246 }
247 }
248 foreach ( $int_fields as $key ) {
249 if ( isset( $restriction[ $key ] ) ) {
250 $restriction_data[ $key ] = absint( $restriction[ $key ] );
251 }
252 }
253 foreach ( $string_fields as $key ) {
254 if ( isset( $restriction[ $key ] ) ) {
255 $restriction_data[ $key ] = sanitize_text_field( Helper::get_string_value( $restriction[ $key ] ) );
256 }
257 }
258
259 $post_metas['_srfm_form_restriction'] = wp_json_encode( $restriction_data );
260 }
261
262 // Form custom CSS.
263 if ( ! empty( $meta_data['formCustomCss'] ) ) {
264 $post_metas['_srfm_form_custom_css'] = wp_kses_post( Helper::get_string_value( $meta_data['formCustomCss'] ) );
265 }
266
267 return $post_metas;
268 }
269 }
270