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