PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 1.5.10
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v1.5.10
2.4.0 2.3.0 2.2.5 2.2.0 2.1.2 2.1.1 trunk 1.10.0 1.10.01 1.10.02 1.5.0 1.5.01 1.5.02 1.5.1 1.5.10 1.5.20 1.5.21 1.5.22 1.5.23 1.5.24 1.5.25 1.6.0 1.7.0 1.7.1 1.7.2 All 33 releases
fluent-booking / app / Services / BookingFieldService.php

BookingFieldService.php in Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution 1.5.10, at app/Services/BookingFieldService.php

348 lines 11.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentBooking\App\Services;
4
5 use FluentBooking\App\Models\Booking;
6 use FluentBooking\App\Models\CalendarSlot;
7 use FluentBooking\Framework\Support\Arr;
8
9 class BookingFieldService
10 {
11 public static function getCustomFieldsData($postedData, CalendarSlot $slot)
12 {
13 $customFields = self::getCustomFields($slot, true);
14
15 $errors = [];
16
17 $formattedValues = [];
18
19 foreach ($customFields as $fieldKey => $customField) {
20 $value = wp_unslash(Arr::get($postedData, $fieldKey));
21 if (!$value && Arr::isTrue($customField, 'required')) {
22 // translators: %s is the label of the required field
23 $errors[$fieldKey . '.required'] = sprintf(__('%s is required', 'fluent-booking'), $customField['label']);
24 continue;
25 }
26
27 if (is_array($value)) {
28 if ($customField['type'] === 'multi-select') {
29 $value = array_map(
30 function ($item) {
31 return sanitize_text_field(Arr::get($item, 'value'));
32 },
33 $value
34 );
35 } else {
36 $value = array_map('sanitize_text_field', $value);
37 }
38 } else if ($customField['type'] == 'textarea') {
39 $value = sanitize_textarea_field($value);
40 } else {
41 $value = sanitize_text_field($value);
42 }
43
44 $formattedValues[$fieldKey] = $value;
45 }
46
47 if ($errors) {
48 return new \WP_Error('required_field', __('Please fill up the required data', 'fluent-booking'), $errors);
49 }
50
51 return $formattedValues;
52 }
53
54 public static function getBookingFields(CalendarSlot $calendarSlot)
55 {
56 $requiredIndexes = ['name', 'email', 'message', 'cancellation_reason', 'rescheduling_reason'];
57
58 $defaultFields = [
59 'name' => [
60 'index' => 1,
61 'type' => 'text',
62 'name' => 'name',
63 'label' => __('Your Name', 'fluent-booking'),
64 'required' => true,
65 'enabled' => true,
66 'system_defined' => true,
67 'disable_alter' => true,
68 'is_visible' => true,
69 'placeholder' => __('Your Name', 'fluent-booking'),
70 'help_text' => ''
71 ],
72 'email' => [
73 'index' => 2,
74 'type' => 'email',
75 'name' => 'email',
76 'label' => __('Your Email', 'fluent-booking'),
77 'required' => true,
78 'enabled' => true,
79 'system_defined' => true,
80 'disable_alter' => true,
81 'is_visible' => true,
82 'placeholder' => __('Your Email', 'fluent-booking'),
83 'help_text' => ''
84 ],
85 'message' => [
86 'index' => 3,
87 'type' => 'textarea',
88 'name' => 'message',
89 'label' => __('What is this meeting about?', 'fluent-booking'),
90 'required' => false,
91 'enabled' => true,
92 'system_defined' => true,
93 'disable_alter' => false,
94 'help_text' => ''
95 ],
96 'cancellation_reason' => [
97 'index' => 4,
98 'type' => 'textarea',
99 'name' => 'cancellation_reason',
100 'label' => __('Reason for cancellation', 'fluent-booking'),
101 'placeholder' => __('Why are you cancelling?', 'fluent-booking'),
102 'required' => true,
103 'enabled' => true,
104 'system_defined' => true,
105 'disable_alter' => false,
106 'help_text' => ''
107 ],
108 'rescheduling_reason' => [
109 'index' => 5,
110 'type' => 'textarea',
111 'name' => 'rescheduling_reason',
112 'label' => __('Reason for reschedule', 'fluent-booking'),
113 'placeholder' => __('Let others know why you need to reschedule', 'fluent-booking'),
114 'required' => true,
115 'enabled' => true,
116 'system_defined' => true,
117 'disable_alter' => false,
118 'help_text' => ''
119 ],
120 ];
121
122 if ($calendarSlot->isGuestFieldRequired()) {
123 $requiredIndexes[] = 'guests';
124 $defaultFields['guests'] = [
125 'index' => 6,
126 'type' => 'multi-guests',
127 'name' => 'guests',
128 'label' => __('Additional Guests', 'fluent-booking'),
129 'limit' => 10,
130 'required' => false,
131 'enabled' => false,
132 'system_defined' => true,
133 'disable_alter' => false
134 ];
135 }
136 if ($calendarSlot->isLocationFieldRequired()) {
137 $requiredIndexes[] = 'location';
138 $defaultFields['location'] = [
139 'index' => 7,
140 'type' => 'radio',
141 'name' => 'location',
142 'label' => __('Location', 'fluent-booking'),
143 'options' => LocationService::getLocationOptions($calendarSlot),
144 'required' => true,
145 'enabled' => true,
146 'system_defined' => true,
147 'disable_alter' => true,
148 'placeholder' => esc_attr__('Location', 'fluent-booking')
149 ];
150 } else if ($calendarSlot->isPhoneRequired()) {
151 $requiredIndexes[] = 'phone_number';
152 $defaultFields['phone_number'] = [
153 'index' => 8,
154 'type' => 'phone',
155 'name' => 'phone_number',
156 'label' => __('Your Phone Number', 'fluent-booking'),
157 'required' => true,
158 'enabled' => true,
159 'system_defined' => true,
160 'disable_alter' => true,
161 'is_sms_number' => true,
162 'help_text' => ''
163 ];
164 } else if ($calendarSlot->isAddressRequired()) {
165 $requiredIndexes[] = 'address';
166 $defaultFields['address'] = [
167 'index' => 9,
168 'type' => 'text',
169 'name' => 'address',
170 'label' => __('Your Address', 'fluent-booking'),
171 'required' => true,
172 'enabled' => true,
173 'system_defined' => true,
174 'disable_alter' => true,
175 'placeholder' => esc_attr__('Address', 'fluent-booking'),
176 'help_text' => ''
177 ];
178 }
179
180 $dbFields = $calendarSlot->getMeta('booking_fields', []);
181
182 if (!$dbFields) {
183 $dbFields = array_values($defaultFields);
184 }
185
186 $existingFields = [];
187 foreach ($dbFields as $dbField) {
188 $name = $dbField['name'];
189 $existingFields[$name] = $dbField;
190 }
191
192 if (empty($defaultFields['location'])) {
193 unset($existingFields['location']);
194 } else {
195 if (empty($existingFields['location'])) {
196 $existingFields['location'] = $defaultFields['location'];
197 } else {
198 $existingFields['location']['options'] = $defaultFields['location']['options'];
199 }
200 }
201
202 if (empty($defaultFields['phone_number'])) {
203 unset($existingFields['phone_number']);
204 }
205
206 if (empty($defaultFields['address'])) {
207 unset($existingFields['address']);
208 }
209
210 foreach ($requiredIndexes as $index) {
211 if (empty($existingFields[$index]) && !empty($defaultFields[$index])) {
212 $existingFields[$index] = $defaultFields[$index];
213 }
214 }
215
216 $paymentField = apply_filters('fluent_booking/payment_booking_field', [], $calendarSlot, $existingFields);
217
218 if (!$paymentField) {
219 unset($existingFields['payment_method']);
220 } else {
221 $existingFields['payment_method'] = $paymentField;
222 }
223
224 $existingFields['email']['disabled'] = false;
225
226 return array_values($existingFields);
227 }
228
229 public static function getBookingFieldLabels(CalendarSlot $calendarSlot, $enabledOnly = false)
230 {
231 $fields = self::getBookingFields($calendarSlot);
232 $labels = [];
233
234 foreach ($fields as $field) {
235 if ($enabledOnly && !Arr::isTrue($field, 'enabled')) {
236 continue;
237 }
238
239 $labels[$field['name']] = $field['label'];
240 }
241
242 return $labels;
243 }
244
245 public static function generateFieldName($calendarEvent, $fieldLabel)
246 {
247 $fieldName = 'custom_' . sanitize_title($fieldLabel);
248 $bookingFields = self::getBookingFields($calendarEvent);
249
250 $matched = 0;
251 foreach ($bookingFields as $field) {
252 if (strpos($field['name'], $fieldName) !== false) {
253 $matched++;
254 }
255 }
256
257 if ($matched) {
258 $fieldName .= '_' . $matched;
259 }
260 return $fieldName;
261 }
262
263 public static function getFormattedCustomBookingData(Booking $booking)
264 {
265 $customFormData = $booking->getMeta('custom_fields_data', []);
266 if (!$customFormData) {
267 return [];
268 }
269
270 $labels = self::getBookingFieldLabels($booking->calendar_event);
271
272 $formattedData = [];
273
274 foreach ($customFormData as $dataKey => $value) {
275 if (isset($labels[$dataKey])) {
276 $label = $labels[$dataKey];
277 } else {
278 $label = $dataKey;
279 }
280 $formattedData[$dataKey] = [
281 'label' => $label,
282 'value' => is_array($value) ? implode(', ', $value) : $value
283 ];
284 }
285
286 return $formattedData;
287 }
288
289 public static function getCustomFields($calendarEvent, $withConfig = false)
290 {
291 $existingFields = $calendarEvent->getMeta('booking_fields', []);
292
293 if (!$existingFields) {
294 return [];
295 }
296
297 $customFields = [];
298
299 foreach ($existingFields as $existingField) {
300 if (Arr::get($existingField, 'system_defined') || !Arr::isTrue($existingField, 'enabled')) {
301 continue;
302 }
303 if ($withConfig) {
304 $customFields[$existingField['name']] = $existingField;
305 } else {
306 $customFields[$existingField['name']] = $existingField['label'];
307 }
308 }
309
310 return $customFields;
311 }
312
313 public static function getBookingFieldByName($calendarEvent, $name)
314 {
315 $fields = self::getBookingFields($calendarEvent);
316
317 foreach ($fields as $field) {
318 if (Arr::get($field, 'name') == $name) {
319 return $field;
320 }
321 }
322
323 return [];
324 }
325
326 public static function hasPhoneNumberField($fields)
327 {
328 if(!$fields) {
329 return false;
330 }
331
332 foreach ($fields as $field) {
333 if ($field['type'] == 'phone') {
334 return true;
335 } else if ($field['name'] == 'location') {
336 if (!empty($field['options'])) {
337 foreach ($field['options'] as $option) {
338 if (Arr::get($option, 'type') == 'phone_guest') {
339 return true;
340 }
341 }
342 }
343 }
344 }
345 return false;
346 }
347 }
348