PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 1.5.20
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v1.5.20
2.5.0 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 All 34 releases
fluent-booking / app / Hooks / Handlers / BlockEditorHandler.php

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

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