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

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