| 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 |
if ( isset( $form_styling['disableDefaultStyles'] ) ) { |
| 166 |
$styling_data['disable_default_styles'] = (bool) $form_styling['disableDefaultStyles']; |
| 167 |
} |
| 168 |
|
| 169 |
$post_metas['_srfm_forms_styling'] = $styling_data; |
| 170 |
} |
| 171 |
|
| 172 |
// Email notification — merges into first notification entry. |
| 173 |
$email = Helper::get_array_value( $meta_data['emailNotification'] ?? [] ); |
| 174 |
if ( ! empty( $email ) ) { |
| 175 |
$notif_data = Helper::get_array_value( $post_metas['_srfm_email_notification'] ?? [] ); |
| 176 |
$item = ! empty( $notif_data[0] ) && is_array( $notif_data[0] ) |
| 177 |
? $notif_data[0] |
| 178 |
: [ |
| 179 |
'id' => 1, |
| 180 |
'status' => true, |
| 181 |
'is_raw_format' => false, |
| 182 |
'name' => '', |
| 183 |
'email_to' => '{admin_email}', |
| 184 |
'email_reply_to' => '{admin_email}', |
| 185 |
'from_name' => '{site_title}', |
| 186 |
'from_email' => '{admin_email}', |
| 187 |
'email_cc' => '', |
| 188 |
'email_bcc' => '', |
| 189 |
'subject' => '', |
| 190 |
'email_body' => '{all_data}', |
| 191 |
]; |
| 192 |
|
| 193 |
$field_map = [ |
| 194 |
'status' => 'status', |
| 195 |
'name' => 'name', |
| 196 |
'emailTo' => 'email_to', |
| 197 |
'emailReplyTo' => 'email_reply_to', |
| 198 |
'fromName' => 'from_name', |
| 199 |
'fromEmail' => 'from_email', |
| 200 |
'emailCc' => 'email_cc', |
| 201 |
'emailBcc' => 'email_bcc', |
| 202 |
'subject' => 'subject', |
| 203 |
'emailBody' => 'email_body', |
| 204 |
]; |
| 205 |
|
| 206 |
foreach ( $field_map as $input_key => $meta_key ) { |
| 207 |
if ( ! isset( $email[ $input_key ] ) ) { |
| 208 |
continue; |
| 209 |
} |
| 210 |
if ( 'status' === $input_key ) { |
| 211 |
$item[ $meta_key ] = (bool) $email[ $input_key ]; |
| 212 |
} else { |
| 213 |
$item[ $meta_key ] = sanitize_text_field( Helper::get_string_value( $email[ $input_key ] ) ); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
$notif_data[0] = $item; |
| 218 |
$post_metas['_srfm_email_notification'] = $notif_data; |
| 219 |
} |
| 220 |
|
| 221 |
// Form restriction — stored as JSON string. |
| 222 |
$restriction = Helper::get_array_value( $meta_data['formRestriction'] ?? [] ); |
| 223 |
if ( ! empty( $restriction ) ) { |
| 224 |
$restriction_json = Helper::get_string_value( $post_metas['_srfm_form_restriction'] ?? '' ); |
| 225 |
$restriction_data = ! empty( $restriction_json ) ? json_decode( $restriction_json, true ) : []; |
| 226 |
if ( ! is_array( $restriction_data ) ) { |
| 227 |
$restriction_data = []; |
| 228 |
} |
| 229 |
|
| 230 |
$bool_fields = [ 'status', 'schedulingStatus' ]; |
| 231 |
$int_fields = [ 'maxEntries' ]; |
| 232 |
$string_fields = [ |
| 233 |
'date', |
| 234 |
'hours', |
| 235 |
'minutes', |
| 236 |
'meridiem', |
| 237 |
'message', |
| 238 |
'startDate', |
| 239 |
'startHours', |
| 240 |
'startMinutes', |
| 241 |
'startMeridiem', |
| 242 |
'schedulingNotStartedMessage', |
| 243 |
'schedulingEndedMessage', |
| 244 |
]; |
| 245 |
|
| 246 |
foreach ( $bool_fields as $key ) { |
| 247 |
if ( isset( $restriction[ $key ] ) ) { |
| 248 |
$restriction_data[ $key ] = (bool) $restriction[ $key ]; |
| 249 |
} |
| 250 |
} |
| 251 |
foreach ( $int_fields as $key ) { |
| 252 |
if ( isset( $restriction[ $key ] ) ) { |
| 253 |
$restriction_data[ $key ] = absint( $restriction[ $key ] ); |
| 254 |
} |
| 255 |
} |
| 256 |
foreach ( $string_fields as $key ) { |
| 257 |
if ( isset( $restriction[ $key ] ) ) { |
| 258 |
$restriction_data[ $key ] = sanitize_text_field( Helper::get_string_value( $restriction[ $key ] ) ); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
$post_metas['_srfm_form_restriction'] = wp_json_encode( $restriction_data ); |
| 263 |
} |
| 264 |
|
| 265 |
// Form custom CSS. |
| 266 |
if ( ! empty( $meta_data['formCustomCss'] ) ) { |
| 267 |
$post_metas['_srfm_form_custom_css'] = wp_kses_post( Helper::get_string_value( $meta_data['formCustomCss'] ) ); |
| 268 |
} |
| 269 |
|
| 270 |
return $post_metas; |
| 271 |
} |
| 272 |
} |
| 273 |
|