PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 1.5.21
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v1.5.21
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 / Hooks / Handlers / BlockEditorHandler.php

BlockEditorHandler.php in Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution 1.5.21, at app/Hooks/Handlers/BlockEditorHandler.php

378 lines 13.2 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\Hooks\Handlers;
4
5 use FluentBooking\App\App;
6 use FluentBooking\App\Models\Calendar;
7 use FluentBooking\App\Models\CalendarSlot;
8 use FluentBooking\App\Services\CalendarEventService;
9 use FluentBooking\App\Services\Helper;
10 use FluentBooking\Framework\Support\Arr;
11
12 class BlockEditorHandler
13 {
14 public function init()
15 {
16 add_action('enqueue_block_editor_assets', function () {
17 $app = App::getInstance();
18 $assets = $app['url.assets'];
19
20 wp_enqueue_script(
21 'fluent-booking/calendar',
22 $assets . 'admin/fluent-booking-index.js',
23 array('wp-blocks', 'wp-components', 'wp-block-editor', 'wp-element'),
24 FLUENT_BOOKING_ASSETS_VERSION,
25 true
26 );
27
28 wp_localize_script('fluent-booking/calendar', 'fluentCalendarGutenbergVars', [
29 'ajaxurl' => admin_url('admin-ajax.php'),
30 ]);
31
32 wp_enqueue_script(
33 'fluent-booking/team-management',
34 $assets . 'admin/fluent-booking-team-management-index.js',
35 array('wp-blocks', 'wp-components', 'wp-block-editor', 'wp-element'),
36 FLUENT_BOOKING_ASSETS_VERSION,
37 true
38 );
39
40 wp_enqueue_script(
41 'fluent-booking/calendar-management',
42 $assets . 'admin/fluent-booking-calendar-management-index.js',
43 array('wp-blocks', 'wp-components', 'wp-block-editor', 'wp-element'),
44 FLUENT_BOOKING_ASSETS_VERSION,
45 true
46 );
47
48 wp_enqueue_script(
49 'fluent-booking/booking-management',
50 $assets . 'admin/fluent-booking-booking-management-index.js',
51 array('wp-blocks', 'wp-components', 'wp-block-editor', 'wp-element'),
52 FLUENT_BOOKING_ASSETS_VERSION,
53 true
54 );
55
56 $calendars = Calendar::with(['events' => function ($query) {
57 $query->where('status', 'active');
58 }])->where('status', 'active')->get();
59
60 $formattedCalendars = [];
61
62 foreach ($calendars as $calendar) {
63 if ($calendar->events->isEmpty()) {
64 continue;
65 }
66
67 $formattedEvents = [];
68 foreach ($calendar->events as $event) {
69 $formattedEvents[] = [
70 'id' => (string)$event->id,
71 'title' => $event->title,
72 'color_schema' => $event->color_schema,
73 'durations' => $event->getAvailableDurations(),
74 'locations' => $event->defaultLocationHtml(),
75 'payment_html' => $event->getPaymentHtml(),
76 'loc_settings' => $event->location_settings,
77 'description' => Helper::excerpt($event->getDescription())
78 ];
79 }
80
81 $formattedCalendars[$calendar->id] = [
82 'id' => (string)$calendar->id,
83 'title' => $calendar->title,
84 'description' => wpautop(Helper::excerpt($calendar->description, 200)),
85 'author' => $calendar->getAuthorProfile(),
86 'event_order' => $calendar->getMeta('event_order'),
87 'events' => $formattedEvents
88 ];
89 }
90
91 wp_localize_script('fluent-booking/calendar', 'fluent_booking_block', [
92 'assets_url' => $assets,
93 'hosts' => $formattedCalendars
94 ]);
95 });
96
97 register_block_type('fluent-booking/calendar', array(
98 'editor_script' => 'fluent-booking/calendar',
99 'render_callback' => array($this, 'fcalRenderBlock'),
100 'attributes' => [
101 'slotId' => [
102 'type' => 'string',
103 'default' => '',
104 ],
105 'calendarId' => [
106 'type' => 'string',
107 'default' => '',
108 ],
109 'eventHash' => [
110 'type' => 'string',
111 'default' => '',
112 ],
113 'avatar_rounded' => [
114 'type' => 'boolean',
115 'default' => false
116 ],
117 'primary_color' => [
118 'type' => 'string',
119 'default' => '#4587EC'
120 ],
121 'date_round' => [
122 'type' => 'string',
123 'default' => '4px'
124 ],
125 'avatarStyle' => [
126 'type' => 'string',
127 'default' => '8px'
128 ],
129 'hideHostInfo' => [
130 'type' => 'string',
131 'default' => 'no'
132 ],
133 'theme' => [
134 'type' => 'string',
135 'default' => 'light'
136 ]
137 ]
138 ));
139
140 register_block_type('fluent-booking/team-management', array(
141 'editor_script' => 'fluent-booking/team-management',
142 'render_callback' => array($this, 'fcalRenderTeamManagementBlock'),
143 'attributes' => [
144 'title' => [
145 'type' => 'string',
146 'default' => ''
147 ],
148 'description' => [
149 'type' => 'string',
150 'default' => ''
151 ],
152 'headerImage' => [
153 'type' => 'object',
154 'default' => ''
155 ],
156 'hosts' => [
157 'type' => 'object',
158 'default' => ''
159 ]
160 ]
161 ));
162
163 register_block_type('fluent-booking/calendar-management', array(
164 'editor_script' => 'fluent-booking/calendar-management',
165 'render_callback' => array($this, 'fcalRenderCalendarManagementBlock'),
166 'attributes' => array(
167 'title' => array(
168 'type' => 'string',
169 'default' => ''
170 ),
171 'description' => array(
172 'type' => 'string',
173 'default' => ''
174 ),
175 'headerImage' => array(
176 'type' => 'object',
177 'default' => ''
178 ),
179 'calendarId' => array(
180 'type' => 'string',
181 'default' => ''
182 ),
183 'eventIds' => array(
184 'type' => 'array',
185 'default' => []
186 ),
187 'hideInfo' => array(
188 'type' => 'boolean',
189 'default' => false
190 )
191 )
192 ));
193
194 register_block_type('fluent-booking/booking-management', array(
195 'editor_script' => 'fluent-booking/booking-management',
196 'render_callback' => array($this, 'fcalRenderBookingManagementBlock'),
197 'attributes' => [
198 'title' => [
199 'type' => 'string',
200 'default' => __('My Bookings', 'fluent-booking')
201 ],
202 'showFilter' => [
203 'type' => 'boolean',
204 'default' => true
205 ],
206 'showPagination' => [
207 'type' => 'boolean',
208 'default' => true
209 ],
210 'period' => [
211 'type' => 'string',
212 'default' => 'all'
213 ],
214 'perPage' => [
215 'type' => 'number',
216 'default' => 5
217 ],
218 'noBookingsMessage' => [
219 'type' => 'string',
220 'default' => __('No bookings found', 'fluent-booking')
221 ],
222 'calendarIds' => [
223 'type' => 'array',
224 'default' => ['all']
225 ]
226 ]
227 ));
228 }
229
230 public function fcalRenderTeamManagementBlock($attributes)
231 {
232 $hosts = Arr::get($attributes, 'calendarHosts', []);
233
234 $wrapperClassName = Arr::get($attributes, 'className');
235
236 if (!$hosts) {
237 return '';
238 }
239
240 $hostItems = [];
241
242 foreach ($hosts as $config) {
243 $calendar = Calendar::find($config['id']);
244 if (!$calendar) {
245 continue;
246 }
247
248 $eventIds = Arr::get($config, 'events', []);
249 if (!$eventIds) {
250 continue;
251 }
252
253 $eventsQuery = CalendarSlot::where('calendar_id', $calendar->id)
254 ->where('status', 'active');
255
256 if (!in_array('all', $eventIds)) {
257 $eventsQuery->whereIn('id', $eventIds);
258 }
259
260 $events = $eventsQuery->get();
261
262 if ($events->isEmpty()) {
263 continue;
264 }
265
266 $events = CalendarEventService::processEvents($calendar, $events);
267
268 $calendar->activeEvents = $events;
269
270 $hostItems[$calendar->id] = $calendar;
271 }
272
273 return (new FrontEndHandler())->renderTeamHosts($hostItems, [
274 'title' => Arr::get($attributes, 'title'),
275 'description' => Arr::get($attributes, 'description'),
276 'logo' => Arr::get($attributes, 'headerImage.url'),
277 'wrapper_class' => $wrapperClassName
278 ]);
279 }
280
281 public function fcalRenderCalendarManagementBlock($attributes)
282 {
283 $calendarId = Arr::get($attributes, 'calendarId');
284
285 $eventIds = Arr::get($attributes, 'eventIds', []);
286
287 if (!$calendarId || !$eventIds) {
288 return '';
289 }
290
291 $calendar = Calendar::find($calendarId);
292 if (!$calendar) {
293 return '';
294 }
295
296 $eventsQuery = CalendarSlot::where('calendar_id', $calendar->id)
297 ->where('status', 'active');
298
299 if (!in_array('all', $eventIds)) {
300 $eventsQuery->whereIn('id', $eventIds);
301 }
302
303 $events = $eventsQuery->get();
304
305 if ($events->isEmpty()) {
306 return '';
307 }
308
309 $events = CalendarEventService::processEvents($calendar, $events);
310
311 $calendar->activeEvents = $events;
312
313 return (new FrontEndHandler())->renderCalendarBlock($calendar, [
314 'title' => Arr::get($attributes, 'title'),
315 'description' => Arr::get($attributes, 'description'),
316 'wrapper_class' => Arr::get($attributes, 'className'),
317 'logo' => Arr::get($attributes, 'headerImage.url'),
318 'hide_info' => Arr::isTrue($attributes, 'hideInfo')
319 ]);
320 }
321
322 public function fcalRenderBookingManagementBlock($attributes)
323 {
324 $calendarIds = Arr::get($attributes, 'calendarIds', []);
325
326 $title = sanitize_text_field(Arr::get($attributes, 'title'));
327
328 $period = sanitize_text_field(Arr::get($attributes, 'period', 'all'));
329
330 $perPage = intval(Arr::get($attributes, 'perPage', 10));
331
332 $showFilter = Arr::isTrue($attributes, 'showFilter', true) ? 'show' : 'hide';
333
334 $showPagination = Arr::isTrue($attributes, 'showPagination', true) ? 'show' : 'hide';
335
336 $noBookingsMessage = sanitize_text_field(Arr::get($attributes, 'noBookingsMessage'));
337
338 return do_shortcode("[fluent_booking_lists title=\"$title\" period=$period per_page=$perPage filter=$showFilter pagination=$showPagination no_bookings=\"$noBookingsMessage\" calendar_ids=" . implode(',', $calendarIds) . "]");
339 }
340
341 public function fcalRenderBlock($attributes)
342 {
343 $output = '<style>
344 :root {
345 --fcal_primary_color: ' . esc_attr($attributes['primary_color']) . ' !important;
346 --fcal_date_radius: ' . esc_attr($attributes['date_round']) . ' !important;
347 --fcal_avatar_radius: ' . esc_attr($attributes['avatarStyle']) . ' !important;
348 }
349 </style>';
350
351 $slotId = (int) $attributes['slotId'];
352 $disableHost = $attributes['hideHostInfo'];
353 $theme = Arr::get($attributes, 'theme', 'light');
354
355 $slot = CalendarSlot::find($slotId);
356
357 if (!$slot) {
358 $eventHash = Arr::get($attributes, 'eventHash');
359 if ($eventHash) {
360 $slot = CalendarSlot::where('hash', $eventHash)->first();
361 }
362 if (!$slot) {
363 return '';
364 }
365
366 $slotId = $slot->id;
367 }
368
369
370 $output .= '<div class="fluent-booking-calendar-block align' . Arr::get($attributes, 'align') . '">';
371
372 $output .= do_shortcode("[fluent_booking id=$slotId disable_author=$disableHost theme=$theme]");
373
374 $output .= '</div>';
375 return $output;
376 }
377 }
378