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