← All changes
|
app/Services/Integrations/FluentForms/BookingElement.php
+46
-10
1.5.20
→
2.5.0
View file →
| @@ -15,8 +15,9 @@ | ||
| 15 | 15 | use FluentBooking\App\Services\PermissionManager; |
| 16 | 16 | use FluentBooking\App\Hooks\Handlers\FrontEndHandler; |
| 17 | 17 | use FluentForm\App\Services\FormBuilder\BaseFieldManager; |
| 18 | 18 | use FluentForm\Framework\Helpers\ArrayHelper; |
| 19 | +use FluentBooking\App\Vite; | |
| 19 | 20 | |
| 20 | 21 | class BookingElement extends BaseFieldManager |
| 21 | 22 | { |
| 22 | 23 | /** |
| @@ -34,12 +35,12 @@ | ||
| 34 | 35 | 'advanced' |
| 35 | 36 | ); |
| 36 | 37 | |
| 37 | 38 | add_filter('fluentform/response_render_fcal_booking', array($this, 'renderResponse'), 10, 3); |
| 38 | - add_filter('fluentform/select_group_component_ajax_options', array($this, 'getCalendarOptions')); | |
| 39 | + add_filter('fluentform/select_group_component_ajax_options', array($this, 'getCalendarOptions'), 10, 2); | |
| 39 | 40 | |
| 40 | 41 | add_action('fluentform/loading_editor_assets', function () { |
| 41 | - wp_enqueue_script('fluentcal_ff_editor_extended', FLUENT_BOOKING_URL . 'assets/admin/fluentform.js', [], '1.0.0', true); | |
| 42 | + wp_enqueue_script('fluentcal_ff_editor_extended', FLUENT_BOOKING_URL . 'assets/admin/fluentform.js', [], FLUENT_BOOKING_ASSETS_VERSION, true); | |
| 42 | 43 | }); |
| 43 | 44 | } |
| 44 | 45 | |
| 45 | 46 | function getComponent() |
| @@ -154,16 +155,12 @@ | ||
| 154 | 155 | $localizeData['time_format'] = (Helper::getGlobalSettings())['time_format']; |
| 155 | 156 | |
| 156 | 157 | $assetUrl = App::getInstance('url.assets'); |
| 157 | 158 | |
| 158 | - wp_enqueue_script( | |
| 159 | - 'fluentform-calendar-public', | |
| 160 | - $assetUrl . 'public/js/fluentform.js', [], | |
| 161 | - FLUENT_BOOKING_ASSETS_VERSION, true | |
| 162 | - ); | |
| 159 | + Vite::enqueueScript('fluentform-calendar-public', 'ff_public', [], FLUENT_BOOKING_ASSETS_VERSION); | |
| 163 | 160 | |
| 164 | 161 | if (BookingFieldService::hasPhoneNumberField($localizeData['form_fields'])) { |
| 165 | - wp_enqueue_script('fluent-booking-phone-field', $assetUrl . 'public/js/phone-field.js', [], FLUENT_BOOKING_ASSETS_VERSION, true); | |
| 162 | + Vite::enqueueScript('fluent-booking-phone-field', 'phone_field', [], FLUENT_BOOKING_ASSETS_VERSION); | |
| 166 | 163 | ?> |
| 167 | 164 | <style> |
| 168 | 165 | .fcal_phone_wrapper .flag { |
| 169 | 166 | background: url(<?php echo esc_url($assetUrl.'images/flags_responsive.png'); ?>) no-repeat; |
| @@ -250,11 +247,50 @@ | ||
| 250 | 247 | { |
| 251 | 248 | return __('Text Response', 'fluent-booking'); |
| 252 | 249 | } |
| 253 | 250 | |
| 254 | - public function getCalendarOptions() | |
| 251 | + public function getCalendarOptions($options = [], $requestData = []) | |
| 255 | 252 | { |
| 256 | - return apply_filters('fluent_booking/ff_editor_calendar_options', CalendarService::getCalendarOptionsByHost()); | |
| 253 | + $eventId = (int) Arr::get($requestData, 'fcal_event_id'); | |
| 254 | + | |
| 255 | + if (!$eventId) { | |
| 256 | + return apply_filters('fluent_booking/ff_editor_calendar_options', CalendarService::getCalendarOptionsByHost()); | |
| 257 | + } | |
| 258 | + | |
| 259 | + return $this->getMappableFields($eventId); | |
| 260 | + } | |
| 261 | + | |
| 262 | + /** | |
| 263 | + * Custom booking fields of an event that a Fluent Forms field can fill. | |
| 264 | + * File, checkbox and terms fields are left out: their stored value contract | |
| 265 | + * (a FluentBooking upload URL, 'Yes', 'Accepted') differs from what Fluent Forms submits. | |
| 266 | + * | |
| 267 | + * @param int $eventId | |
| 268 | + * @return array field name => label | |
| 269 | + */ | |
| 270 | + private function getMappableFields($eventId) | |
| 271 | + { | |
| 272 | + $event = CalendarSlot::find($eventId); | |
| 273 | + | |
| 274 | + if (!$event || !$event->calendar) { | |
| 275 | + return []; | |
| 276 | + } | |
| 277 | + | |
| 278 | + // Same scope as getCalendarOptionsByHost(): Fluent Forms only checks dashboard access. | |
| 279 | + if (!PermissionManager::hasAllCalendarAccess(true) && $event->calendar->user_id != get_current_user_id()) { | |
| 280 | + return []; | |
| 281 | + } | |
| 282 | + | |
| 283 | + $fields = []; | |
| 284 | + | |
| 285 | + foreach (BookingFieldService::getCustomFields($event, true) as $fieldKey => $field) { | |
| 286 | + if (in_array(Arr::get($field, 'type'), ['file', 'checkbox', 'terms-and-conditions'], true)) { | |
| 287 | + continue; | |
| 288 | + } | |
| 289 | + $fields[$fieldKey] = Arr::get($field, 'label', $fieldKey); | |
| 290 | + } | |
| 291 | + | |
| 292 | + return $fields; | |
| 257 | 293 | } |
| 258 | 294 | |
| 259 | 295 | /** |
| 260 | 296 | * Build unique ID concatenating form id and name attribute |