PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.1.3
Booking for Appointments and Events Calendar – Amelia v2.1.3
2.4.9 2.4.8 2.4.7 2.4.6 2.4.5 2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Infrastructure / WP / GutenbergBlock / GutenbergBlock.php
ameliabooking / src / Infrastructure / WP / GutenbergBlock Last commit date
AmeliaBookingGutenbergBlock.php 5 months ago AmeliaCatalogBookingGutenbergBlock.php 5 months ago AmeliaCatalogGutenbergBlock.php 5 months ago AmeliaEventsCalendarBookingGutenbergBlock.php 5 months ago AmeliaEventsGutenbergBlock.php 5 months ago AmeliaEventsListBookingGutenbergBlock.php 5 months ago AmeliaStepBookingGutenbergBlock.php 5 months ago GutenbergBlock.php 5 months ago
GutenbergBlock.php
397 lines
1 <?php
2
3 /**
4 * @copyright © Melograno Ventures. All rights reserved.
5 * @licence See LICENCE.md for license details.
6 */
7
8 namespace AmeliaBooking\Infrastructure\WP\GutenbergBlock;
9
10 use AmeliaBooking\Application\Services\Bookable\BookableApplicationService;
11 use AmeliaBooking\Application\Services\Bookable\AbstractPackageApplicationService;
12 use AmeliaBooking\Application\Services\Booking\EventApplicationService;
13 use AmeliaBooking\Application\Services\Location\AbstractLocationApplicationService;
14 use AmeliaBooking\Application\Services\User\ProviderApplicationService;
15 use AmeliaBooking\Domain\Collection\Collection;
16 use AmeliaBooking\Domain\Entity\Bookable\Service\Service;
17 use AmeliaBooking\Domain\Entity\User\Provider;
18 use AmeliaBooking\Domain\Factory\Bookable\Service\ServiceFactory;
19 use AmeliaBooking\Domain\Services\Booking\EventDomainService;
20 use AmeliaBooking\Domain\Services\DateTime\DateTimeService;
21 use AmeliaBooking\Infrastructure\Common\Container;
22 use AmeliaBooking\Infrastructure\Repository\Bookable\Service\CategoryRepository;
23 use AmeliaBooking\Infrastructure\Repository\Bookable\Service\ServiceRepository;
24 use AmeliaBooking\Infrastructure\Repository\Booking\Event\EventTagsRepository;
25 use AmeliaBooking\Infrastructure\Repository\User\ProviderRepository;
26 use Exception;
27
28 /**
29 * Class GutenbergBlock
30 *
31 * @package AmeliaBooking\Infrastructure\WP\GutenbergBlock
32 */
33 class GutenbergBlock
34 {
35 /** @var Container $container */
36 private static $container;
37
38 /** @var Collection */
39 private static $entities;
40
41 public static function init()
42 {
43 $class = get_called_class();
44
45 // Register block type for frontend rendering (dynamic blocks with save: null)
46 if (function_exists('register_block_type')) {
47 add_action('init', function () use ($class) {
48 call_user_func([$class, 'registerBlockForRendering']);
49 });
50 }
51
52 // Editor-only: enqueue scripts, styles, and localize data
53 if (is_admin() && function_exists('register_block_type')) {
54 if (
55 substr($_SERVER['PHP_SELF'], '-8') == 'post.php' ||
56 substr($_SERVER['PHP_SELF'], '-12') == 'post-new.php'
57 ) {
58 if (self::isGutenbergActive()) {
59 add_action(
60 'enqueue_block_editor_assets',
61 function () use ($class) {
62 call_user_func([$class, 'registerBlockType']);
63 }
64 );
65 }
66 }
67 }
68 }
69
70 /**
71 * Enqueue shared Amelia block icon script
72 */
73 public static function enqueueSharedIcon()
74 {
75 static $enqueued = false;
76
77 if (!$enqueued) {
78 wp_enqueue_script(
79 'amelia_block_icon',
80 AMELIA_URL . 'public/js/gutenberg/amelia-block-icon.js',
81 array('wp-element'),
82 AMELIA_VERSION
83 );
84 $enqueued = true;
85 }
86 }
87
88 /**
89 * Enqueue shared Amelia placeholder styles for block editor
90 */
91 public static function enqueueSharedStyles()
92 {
93 static $enqueued = false;
94
95 if (!$enqueued) {
96 wp_enqueue_style(
97 'amelia_gutenberg_placeholder_styles',
98 AMELIA_URL . 'public/js/gutenberg/amelia-gutenberg-placeholder.css',
99 [],
100 AMELIA_VERSION
101 );
102 $enqueued = true;
103 }
104 }
105
106 /**
107 * Register block type with attributes and render_callback for frontend rendering.
108 * Override in child classes.
109 */
110 public static function registerBlockForRendering()
111 {
112 }
113
114 /**
115 * Register block for gutenberg
116 */
117 public static function registerBlockType()
118 {
119 }
120
121 /**
122 * Check if Block Editor is active.
123 *
124 * @return bool
125 */
126 public static function isGutenbergActive()
127 {
128 // Gutenberg plugin is installed and activated.
129 $gutenberg = !(false === has_filter('replace_editor', 'gutenberg_init'));
130
131 // Block editor since 5.0.
132 $block_editor = version_compare($GLOBALS['wp_version'], '5.0-beta', '>');
133
134 if (!$gutenberg && !$block_editor) {
135 return false;
136 }
137
138 if (self::isClassicEditorPluginActive()) {
139 $editor_option = get_option('classic-editor-replace');
140 $block_editor_active = array('no-replace', 'block');
141
142 return in_array($editor_option, $block_editor_active, true);
143 }
144
145 // Fix for conflict with Avada - Fusion builder and gutenberg blocks
146 if (class_exists('FusionBuilder') && !(isset($_GET['gutenberg-editor']))) {
147 return false;
148 }
149
150 // Fix for conflict with Disable Gutenberg plugin
151 if (class_exists('DisableGutenberg')) {
152 return false;
153 }
154
155 // Fix for conflict with WP Bakery Page Builder
156 if (class_exists('Vc_Manager') && (isset($_GET['classic-editor']))) {
157 return false;
158 }
159
160 // Fix for conflict with WooCommerce product page
161 if (isset($_GET['post_type']) && $_GET['post_type'] === 'product' && class_exists('WooCommerce')) {
162 return false;
163 }
164
165 return true;
166 }
167
168 /**
169 * Check if Classic Editor plugin is active
170 *
171 * @return bool
172 */
173 public static function isClassicEditorPluginActive()
174 {
175
176 if (!function_exists('is_plugin_active')) {
177 include_once ABSPATH . 'wp-admin/includes/plugin.php';
178 }
179
180 if (is_plugin_active('classic-editor/classic-editor.php')) {
181 return true;
182 }
183
184 return false;
185 }
186
187 /**
188 * Set Amelia Container
189 *
190 * @param $container
191 */
192 public static function setContainer($container)
193 {
194 self::$container = $container;
195 }
196
197 /**
198 * Get entities data for front-end
199 */
200 public static function getEntitiesData()
201 {
202 return (new self())->getAllEntitiesForGutenbergBlocks();
203 }
204
205 /**
206 * Get Entities for Gutenberg blocks
207 */
208 public function getAllEntitiesForGutenbergBlocks()
209 {
210 if (!empty(self::$entities)) {
211 return self::$entities;
212 }
213
214 try {
215 self::setContainer(require AMELIA_PATH . '/src/Infrastructure/ContainerConfig/container.php');
216
217 /** @var AbstractLocationApplicationService $locationAS */
218 $locationAS = self::$container->get('application.location.service');
219
220 $locations = $locationAS->getAllOrderedByName();
221
222 $resultData['locations'] = $locations->toArray();
223
224 /** @var ServiceRepository $serviceRepository */
225 $serviceRepository = self::$container->get('domain.bookable.service.repository');
226 /** @var CategoryRepository $categoryRepository */
227 $categoryRepository = self::$container->get('domain.bookable.category.repository');
228 /** @var BookableApplicationService $bookableAS */
229 $bookableAS = self::$container->get('application.bookable.service');
230
231 $services = $serviceRepository->getAllArrayIndexedById();
232
233 $categories = $categoryRepository->getAllIndexedById();
234
235 $bookableAS->addServicesToCategories($categories, $services);
236
237 $resultData['categories'] = $categories->toArray();
238
239 /** @var ProviderRepository $providerRepository */
240 $providerRepository = self::$container->get('domain.users.providers.repository');
241
242 /** @var ProviderApplicationService $providerAS */
243 $providerAS = self::$container->get('application.user.provider.service');
244
245 /** @var Collection $providers */
246 $providers = $providerRepository->getByFieldValue('type', 'provider');
247
248 $providerServicesData = $providerRepository->getProvidersServices();
249
250 foreach ((array)$providerServicesData as $providerKey => $providerServices) {
251 /** @var Provider $provider */
252 $provider = $providers->getItem($providerKey);
253
254 $providerServiceList = new Collection();
255
256 foreach ((array)$providerServices as $serviceKey => $providerService) {
257 /** @var Service $service */
258 $service = $services->getItem($serviceKey);
259
260 if ($service && $provider) {
261 $providerServiceList->addItem(
262 ServiceFactory::create(array_merge($service->toArray(), $providerService)),
263 $service->getId()->getValue()
264 );
265 }
266 }
267
268 $provider->setServiceList($providerServiceList);
269 }
270
271 /** @var Provider $currentUser */
272 $currentUser = self::$container->get('logged.in.user');
273
274 $resultData['employees'] = $providerAS->removeAllExceptUser(
275 $providers->toArray(),
276 $currentUser
277 );
278
279 $finalData = self::getOnlyCatSerLocEmp($resultData);
280
281 /** @var EventApplicationService $eventAS */
282 $eventAS = self::$container->get('application.booking.event.service');
283
284 /** @var Collection $events */
285 $events = $eventAS->getEventsByCriteria(
286 [
287 'dates' => [DateTimeService::getNowDateTime()],
288 ],
289 [
290 'fetchEventsPeriods' => true,
291 ],
292 100
293 );
294
295 $finalData['events'] = $events->toArray();
296
297 /** @var EventDomainService $eventDS */
298 $eventDS = self::$container->get('domain.booking.event.service');
299
300 $finalData['events'] = $eventDS->getShortcodeForEventList(self::$container, $finalData['events']);
301
302 /** @var EventTagsRepository $eventTagsRepository */
303 $eventTagsRepository = self::$container->get('domain.booking.event.tag.repository');
304
305 /** @var Collection $tags * */
306 $tags = $eventTagsRepository->getAllDistinctByCriteria(
307 [
308 'eventIds' => array_column($finalData['events'], 'id')
309 ]
310 );
311
312 /** @var AbstractPackageApplicationService $packageApplicationService */
313 $packageApplicationService = self::$container->get('application.bookable.package');
314
315 $finalData['packages'] = $packageApplicationService->getPackagesArray();
316
317 $finalData['tags'] = $tags->toArray();
318
319 self::$entities = ['data' => $finalData];
320
321 return self::$entities;
322 } catch (Exception $exception) {
323 return ['data' => [
324 'categories' => [],
325 'servicesList' => [],
326 'locations' => [],
327 'employees' => [],
328 'events' => [],
329 'tags' => [],
330 'packages' => [],
331 ]];
332 }
333 }
334
335 /**
336 * Get only Categories, Services, Employees and Locations for Gutenberg blocks
337 */
338 public static function getOnlyCatSerLocEmp($resultData)
339 {
340 $data = [];
341 $data['categories'] = [];
342 $data['servicesList'] = [];
343 if ($resultData['categories'] !== []) {
344 for ($i = 0; $i < count($resultData['categories']); $i++) {
345 $data['categories'][] = [
346 'id' => $resultData['categories'][$i]['id'],
347 'name' => $resultData['categories'][$i]['name']
348 ];
349 if ($resultData['categories'][$i]['serviceList'] !== []) {
350 for ($j = 0; $j < count($resultData['categories'][$i]['serviceList']); $j++) {
351 if (!$resultData['categories'][$i]['serviceList'][$j]['show']) {
352 continue;
353 }
354
355 $data['servicesList'][] = [
356 'id' => $resultData['categories'][$i]['serviceList'][$j]['id'],
357 'name' => $resultData['categories'][$i]['serviceList'][$j]['name']
358 ];
359 }
360 }
361 }
362 } else {
363 $data['categories'] = [];
364 $data['servicesList'] = [];
365 }
366
367 if ($resultData['locations'] !== []) {
368 for ($i = 0; $i < count($resultData['locations']); $i++) {
369 $data['locations'][] = [
370 'id' => $resultData['locations'][$i]['id'],
371 'name' => $resultData['locations'][$i]['name']
372 ];
373 }
374 } else {
375 $data['locations'] = [];
376 }
377
378 if ($resultData['employees'] !== []) {
379 for ($i = 0; $i < count($resultData['employees']); $i++) {
380 if (!$resultData['employees'][$i]['show']) {
381 continue;
382 }
383
384 $data['employees'][] = [
385 'id' => $resultData['employees'][$i]['id'],
386 'firstName' => $resultData['employees'][$i]['firstName'],
387 'lastName' => $resultData['employees'][$i]['lastName'],
388 ];
389 }
390 } else {
391 $data['employees'] = [];
392 }
393
394 return $data;
395 }
396 }
397