AmeliaBookingGutenbergBlock.php
8 months ago
AmeliaCatalogBookingGutenbergBlock.php
8 months ago
AmeliaCatalogGutenbergBlock.php
8 months ago
AmeliaEventsCalendarBookingGutenbergBlock.php
8 months ago
AmeliaEventsGutenbergBlock.php
8 months ago
AmeliaEventsListBookingGutenbergBlock.php
8 months ago
AmeliaStepBookingGutenbergBlock.php
8 months ago
GutenbergBlock.php
8 months ago
GutenbergBlock.php
365 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 | /** |
| 42 | * Register WP Ajax actions. |
| 43 | */ |
| 44 | public static function init() |
| 45 | { |
| 46 | if (is_admin() && function_exists('register_block_type')) { |
| 47 | if ( |
| 48 | substr($_SERVER['PHP_SELF'], '-8') == 'post.php' || |
| 49 | substr($_SERVER['PHP_SELF'], '-12') == 'post-new.php' |
| 50 | ) { |
| 51 | if (self::isGutenbergActive()) { |
| 52 | $class = get_called_class(); |
| 53 | add_action( |
| 54 | 'enqueue_block_editor_assets', |
| 55 | function () use ($class) { |
| 56 | $class::registerBlockType(); |
| 57 | } |
| 58 | ); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Enqueue shared Amelia block icon script |
| 66 | */ |
| 67 | public static function enqueueSharedIcon() |
| 68 | { |
| 69 | static $enqueued = false; |
| 70 | |
| 71 | if (!$enqueued) { |
| 72 | wp_enqueue_script( |
| 73 | 'amelia_block_icon', |
| 74 | AMELIA_URL . 'public/js/gutenberg/amelia-block-icon.js', |
| 75 | array('wp-element'), |
| 76 | AMELIA_VERSION |
| 77 | ); |
| 78 | $enqueued = true; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Register block for gutenberg |
| 84 | */ |
| 85 | public static function registerBlockType() |
| 86 | { |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Check if Block Editor is active. |
| 91 | * |
| 92 | * @return bool |
| 93 | */ |
| 94 | public static function isGutenbergActive() |
| 95 | { |
| 96 | // Gutenberg plugin is installed and activated. |
| 97 | $gutenberg = !(false === has_filter('replace_editor', 'gutenberg_init')); |
| 98 | |
| 99 | // Block editor since 5.0. |
| 100 | $block_editor = version_compare($GLOBALS['wp_version'], '5.0-beta', '>'); |
| 101 | |
| 102 | if (!$gutenberg && !$block_editor) { |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | if (self::isClassicEditorPluginActive()) { |
| 107 | $editor_option = get_option('classic-editor-replace'); |
| 108 | $block_editor_active = array('no-replace', 'block'); |
| 109 | |
| 110 | return in_array($editor_option, $block_editor_active, true); |
| 111 | } |
| 112 | |
| 113 | // Fix for conflict with Avada - Fusion builder and gutenberg blocks |
| 114 | if (class_exists('FusionBuilder') && !(isset($_GET['gutenberg-editor']))) { |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | // Fix for conflict with Disable Gutenberg plugin |
| 119 | if (class_exists('DisableGutenberg')) { |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | // Fix for conflict with WP Bakery Page Builder |
| 124 | if (class_exists('Vc_Manager') && (isset($_GET['classic-editor']))) { |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | // Fix for conflict with WooCommerce product page |
| 129 | if (isset($_GET['post_type']) && $_GET['post_type'] === 'product' && class_exists('WooCommerce')) { |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Check if Classic Editor plugin is active |
| 138 | * |
| 139 | * @return bool |
| 140 | */ |
| 141 | public static function isClassicEditorPluginActive() |
| 142 | { |
| 143 | |
| 144 | if (!function_exists('is_plugin_active')) { |
| 145 | include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 146 | } |
| 147 | |
| 148 | if (is_plugin_active('classic-editor/classic-editor.php')) { |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Set Amelia Container |
| 157 | * |
| 158 | * @param $container |
| 159 | */ |
| 160 | public static function setContainer($container) |
| 161 | { |
| 162 | self::$container = $container; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Get entities data for front-end |
| 167 | */ |
| 168 | public static function getEntitiesData() |
| 169 | { |
| 170 | return (new self())->getAllEntitiesForGutenbergBlocks(); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Get Entities for Gutenberg blocks |
| 175 | */ |
| 176 | public function getAllEntitiesForGutenbergBlocks() |
| 177 | { |
| 178 | if (!empty(self::$entities)) { |
| 179 | return self::$entities; |
| 180 | } |
| 181 | |
| 182 | try { |
| 183 | self::setContainer(require AMELIA_PATH . '/src/Infrastructure/ContainerConfig/container.php'); |
| 184 | |
| 185 | /** @var AbstractLocationApplicationService $locationAS */ |
| 186 | $locationAS = self::$container->get('application.location.service'); |
| 187 | |
| 188 | $locations = $locationAS->getAllOrderedByName(); |
| 189 | |
| 190 | $resultData['locations'] = $locations->toArray(); |
| 191 | |
| 192 | /** @var ServiceRepository $serviceRepository */ |
| 193 | $serviceRepository = self::$container->get('domain.bookable.service.repository'); |
| 194 | /** @var CategoryRepository $categoryRepository */ |
| 195 | $categoryRepository = self::$container->get('domain.bookable.category.repository'); |
| 196 | /** @var BookableApplicationService $bookableAS */ |
| 197 | $bookableAS = self::$container->get('application.bookable.service'); |
| 198 | |
| 199 | $services = $serviceRepository->getAllArrayIndexedById(); |
| 200 | |
| 201 | $categories = $categoryRepository->getAllIndexedById(); |
| 202 | |
| 203 | $bookableAS->addServicesToCategories($categories, $services); |
| 204 | |
| 205 | $resultData['categories'] = $categories->toArray(); |
| 206 | |
| 207 | /** @var ProviderRepository $providerRepository */ |
| 208 | $providerRepository = self::$container->get('domain.users.providers.repository'); |
| 209 | |
| 210 | /** @var ProviderApplicationService $providerAS */ |
| 211 | $providerAS = self::$container->get('application.user.provider.service'); |
| 212 | |
| 213 | /** @var Collection $providers */ |
| 214 | $providers = $providerRepository->getByFieldValue('type', 'provider'); |
| 215 | |
| 216 | $providerServicesData = $providerRepository->getProvidersServices(); |
| 217 | |
| 218 | foreach ((array)$providerServicesData as $providerKey => $providerServices) { |
| 219 | /** @var Provider $provider */ |
| 220 | $provider = $providers->getItem($providerKey); |
| 221 | |
| 222 | $providerServiceList = new Collection(); |
| 223 | |
| 224 | foreach ((array)$providerServices as $serviceKey => $providerService) { |
| 225 | /** @var Service $service */ |
| 226 | $service = $services->getItem($serviceKey); |
| 227 | |
| 228 | if ($service && $provider) { |
| 229 | $providerServiceList->addItem( |
| 230 | ServiceFactory::create(array_merge($service->toArray(), $providerService)), |
| 231 | $service->getId()->getValue() |
| 232 | ); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | $provider->setServiceList($providerServiceList); |
| 237 | } |
| 238 | |
| 239 | /** @var Provider $currentUser */ |
| 240 | $currentUser = self::$container->get('logged.in.user'); |
| 241 | |
| 242 | $resultData['employees'] = $providerAS->removeAllExceptUser( |
| 243 | $providers->toArray(), |
| 244 | $currentUser |
| 245 | ); |
| 246 | |
| 247 | $finalData = self::getOnlyCatSerLocEmp($resultData); |
| 248 | |
| 249 | /** @var EventApplicationService $eventAS */ |
| 250 | $eventAS = self::$container->get('application.booking.event.service'); |
| 251 | |
| 252 | /** @var Collection $events */ |
| 253 | $events = $eventAS->getEventsByCriteria( |
| 254 | [ |
| 255 | 'dates' => [DateTimeService::getNowDateTime()], |
| 256 | ], |
| 257 | [ |
| 258 | 'fetchEventsPeriods' => true, |
| 259 | ], |
| 260 | 100 |
| 261 | ); |
| 262 | |
| 263 | $finalData['events'] = $events->toArray(); |
| 264 | |
| 265 | /** @var EventDomainService $eventDS */ |
| 266 | $eventDS = self::$container->get('domain.booking.event.service'); |
| 267 | |
| 268 | $finalData['events'] = $eventDS->getShortcodeForEventList(self::$container, $finalData['events']); |
| 269 | |
| 270 | /** @var EventTagsRepository $eventTagsRepository */ |
| 271 | $eventTagsRepository = self::$container->get('domain.booking.event.tag.repository'); |
| 272 | |
| 273 | /** @var Collection $tags * */ |
| 274 | $tags = $eventTagsRepository->getAllDistinctByCriteria( |
| 275 | [ |
| 276 | 'eventIds' => array_column($finalData['events'], 'id') |
| 277 | ] |
| 278 | ); |
| 279 | |
| 280 | /** @var AbstractPackageApplicationService $packageApplicationService */ |
| 281 | $packageApplicationService = self::$container->get('application.bookable.package'); |
| 282 | |
| 283 | $finalData['packages'] = $packageApplicationService->getPackagesArray(); |
| 284 | |
| 285 | $finalData['tags'] = $tags->toArray(); |
| 286 | |
| 287 | self::$entities = ['data' => $finalData]; |
| 288 | |
| 289 | return self::$entities; |
| 290 | } catch (Exception $exception) { |
| 291 | return ['data' => [ |
| 292 | 'categories' => [], |
| 293 | 'servicesList' => [], |
| 294 | 'locations' => [], |
| 295 | 'employees' => [], |
| 296 | 'events' => [], |
| 297 | 'tags' => [], |
| 298 | 'packages' => [], |
| 299 | ]]; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Get only Categories, Services, Employees and Locations for Gutenberg blocks |
| 305 | */ |
| 306 | public static function getOnlyCatSerLocEmp($resultData) |
| 307 | { |
| 308 | $data = []; |
| 309 | $data['categories'] = []; |
| 310 | $data['servicesList'] = []; |
| 311 | if ($resultData['categories'] !== []) { |
| 312 | for ($i = 0; $i < count($resultData['categories']); $i++) { |
| 313 | $data['categories'][] = [ |
| 314 | 'id' => $resultData['categories'][$i]['id'], |
| 315 | 'name' => $resultData['categories'][$i]['name'] |
| 316 | ]; |
| 317 | if ($resultData['categories'][$i]['serviceList'] !== []) { |
| 318 | for ($j = 0; $j < count($resultData['categories'][$i]['serviceList']); $j++) { |
| 319 | if (!$resultData['categories'][$i]['serviceList'][$j]['show']) { |
| 320 | continue; |
| 321 | } |
| 322 | |
| 323 | $data['servicesList'][] = [ |
| 324 | 'id' => $resultData['categories'][$i]['serviceList'][$j]['id'], |
| 325 | 'name' => $resultData['categories'][$i]['serviceList'][$j]['name'] |
| 326 | ]; |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | } else { |
| 331 | $data['categories'] = []; |
| 332 | $data['servicesList'] = []; |
| 333 | } |
| 334 | |
| 335 | if ($resultData['locations'] !== []) { |
| 336 | for ($i = 0; $i < count($resultData['locations']); $i++) { |
| 337 | $data['locations'][] = [ |
| 338 | 'id' => $resultData['locations'][$i]['id'], |
| 339 | 'name' => $resultData['locations'][$i]['name'] |
| 340 | ]; |
| 341 | } |
| 342 | } else { |
| 343 | $data['locations'] = []; |
| 344 | } |
| 345 | |
| 346 | if ($resultData['employees'] !== []) { |
| 347 | for ($i = 0; $i < count($resultData['employees']); $i++) { |
| 348 | if (!$resultData['employees'][$i]['show']) { |
| 349 | continue; |
| 350 | } |
| 351 | |
| 352 | $data['employees'][] = [ |
| 353 | 'id' => $resultData['employees'][$i]['id'], |
| 354 | 'firstName' => $resultData['employees'][$i]['firstName'], |
| 355 | 'lastName' => $resultData['employees'][$i]['lastName'], |
| 356 | ]; |
| 357 | } |
| 358 | } else { |
| 359 | $data['employees'] = []; |
| 360 | } |
| 361 | |
| 362 | return $data; |
| 363 | } |
| 364 | } |
| 365 |