PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 2.1.1
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v2.1.1
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 2.1.1, at app/Hooks/Handlers/BlockEditorHandler.php

408 lines 14.1 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' => $event->short_description
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 = array_map('intval', (array) Arr::get($attributes, 'calendarIds', []));
325
326 $title = sanitize_text_field(Arr::get($attributes, 'title'));
327
328 $period = sanitize_key(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 $shortcode = sprintf(
339 '[fluent_booking_lists title="%s" period=%s per_page=%d filter=%s pagination=%s no_bookings="%s" calendar_ids=%s]',
340 esc_attr($title),
341 $period,
342 $perPage,
343 $showFilter,
344 $showPagination,
345 esc_attr($noBookingsMessage),
346 implode(',', $calendarIds)
347 );
348
349 return do_shortcode($shortcode);
350 }
351
352 public function fcalRenderBlock($attributes)
353 {
354 $primaryColor = sanitize_hex_color(Arr::get($attributes, 'primary_color', ''));
355 $dateRadius = self::sanitizeCssLength(Arr::get($attributes, 'date_round', ''));
356 $avatarRadius = self::sanitizeCssLength(Arr::get($attributes, 'avatarStyle', ''));
357
358 $output = '<style>:root {';
359 if ($primaryColor) {
360 $output .= '--fcal_primary_color: ' . $primaryColor . ' !important;';
361 }
362 if ($dateRadius) {
363 $output .= '--fcal_date_radius: ' . $dateRadius . ' !important;';
364 }
365 if ($avatarRadius) {
366 $output .= '--fcal_avatar_radius: ' . $avatarRadius . ' !important;';
367 }
368 $output .= '}</style>';
369
370 $slotId = (int) Arr::get($attributes, 'slotId');
371 $disableHost = Arr::isTrue($attributes, 'hideHostInfo') ? 'yes' : 'no';
372 $theme = sanitize_key(Arr::get($attributes, 'theme', 'light'));
373 $eventHash = sanitize_text_field(Arr::get($attributes, 'eventHash', ''));
374 $align = sanitize_html_class(Arr::get($attributes, 'align', ''));
375
376 $slot = CalendarSlot::find($slotId);
377
378 if (!$slot) {
379 $slot = CalendarSlot::where('hash', $eventHash)->first();
380 if (!$slot) {
381 return '';
382 }
383 $slotId = (int) $slot->id;
384 $eventHash = (string) $slot->hash;
385 }
386
387 $output .= '<div class="fluent-booking-calendar-block align' . esc_attr($align) . '">';
388
389 $output .= do_shortcode(sprintf(
390 '[fluent_booking id=%d disable_author=%s theme=%s hash=%s]',
391 $slotId,
392 $disableHost,
393 $theme,
394 esc_attr($eventHash)
395 ));
396
397 $output .= '</div>';
398 return $output;
399 }
400
401 private static function sanitizeCssLength($value)
402 {
403 $value = trim((string) $value);
404
405 return preg_match('/^\d{1,3}(\.\d+)?(px|%|em|rem)$/', $value) ? $value : '';
406 }
407 }
408