AmeliaBookingShortcodeService.php
1 year ago
AmeliaShortcodeService.php
1 year ago
BookingShortcodeService.php
1 year ago
CatalogBookingShortcodeService.php
1 year ago
CatalogShortcodeService.php
1 year ago
EventsCalendarBookingShortcodeService.php
1 year ago
EventsListBookingShortcodeService.php
1 year ago
EventsShortcodeService.php
1 year ago
StepBookingShortcodeService.php
1 year ago
AmeliaBookingShortcodeService.php
252 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @copyright © TMS-Plugins. All rights reserved. |
| 5 | * @licence See LICENCE.md for license details. |
| 6 | */ |
| 7 | |
| 8 | namespace AmeliaBooking\Infrastructure\WP\ShortcodeService; |
| 9 | |
| 10 | use AmeliaBooking\Application\Services\Cache\CacheApplicationService; |
| 11 | use AmeliaBooking\Application\Services\Stash\StashApplicationService; |
| 12 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 13 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 14 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 15 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 16 | use AmeliaBooking\Infrastructure\Repository\CustomField\CustomFieldRepository; |
| 17 | use AmeliaBooking\Infrastructure\WP\SettingsService\SettingsStorage; |
| 18 | use AmeliaBooking\Infrastructure\WP\Translations\FrontendStrings; |
| 19 | use Interop\Container\Exception\ContainerException; |
| 20 | |
| 21 | /** |
| 22 | * Class AmeliaBookingShortcodeService |
| 23 | * |
| 24 | * @package AmeliaBooking\Infrastructure\WP\ShortcodeService |
| 25 | */ |
| 26 | class AmeliaBookingShortcodeService |
| 27 | { |
| 28 | public static $counter = 1000; |
| 29 | |
| 30 | /** |
| 31 | * Prepare scripts and styles |
| 32 | * @throws ContainerException |
| 33 | * @throws InvalidArgumentException |
| 34 | */ |
| 35 | public static function prepareScriptsAndStyles() |
| 36 | { |
| 37 | $container = null; |
| 38 | |
| 39 | self::$counter++; |
| 40 | |
| 41 | if (self::$counter > 1001) { |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | $settingsService = new SettingsService(new SettingsStorage()); |
| 46 | |
| 47 | if ($settingsService->getSetting('payments', 'payPal')['enabled'] === true) { |
| 48 | wp_enqueue_script('amelia_paypal_script', 'https://www.paypalobjects.com/api/checkout.js'); |
| 49 | } |
| 50 | |
| 51 | if ($settingsService->getSetting('payments', 'stripe')['enabled'] === true) { |
| 52 | wp_enqueue_script('amelia_stripe_script', 'https://js.stripe.com/v3/'); |
| 53 | } |
| 54 | |
| 55 | if ($settingsService->getSetting('payments', 'razorpay')['enabled'] === true) { |
| 56 | wp_enqueue_script('amelia_razorpay_script', 'https://checkout.razorpay.com/v1/checkout.js'); |
| 57 | } |
| 58 | |
| 59 | $gmapApiKey = $settingsService->getSetting('general', 'gMapApiKey'); |
| 60 | |
| 61 | if ($gmapApiKey) { |
| 62 | $container = $container ?: require AMELIA_PATH . '/src/Infrastructure/ContainerConfig/container.php'; |
| 63 | |
| 64 | /** @var CustomFieldRepository $customFieldRepository */ |
| 65 | $customFieldRepository = $container->get('domain.customField.repository'); |
| 66 | |
| 67 | $addressCustomFields = $customFieldRepository->getByFieldValue('type', 'address'); |
| 68 | |
| 69 | if (count($addressCustomFields->getItems())) { |
| 70 | wp_enqueue_script( |
| 71 | 'amelia_google_maps_api', |
| 72 | "https://maps.googleapis.com/maps/api/js?key={$gmapApiKey}&libraries=places&loading=async" |
| 73 | ); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | $scriptId = AMELIA_DEV ? 'amelia_booking_scripts_dev_vite' : 'amelia_booking_script_index'; |
| 78 | |
| 79 | if (AMELIA_DEV) { |
| 80 | wp_enqueue_script( |
| 81 | $scriptId, |
| 82 | 'http://localhost:3000/@vite/client', |
| 83 | [], |
| 84 | null, |
| 85 | false |
| 86 | ); |
| 87 | |
| 88 | wp_enqueue_script( |
| 89 | 'amelia_booking_scripts_dev_main', |
| 90 | 'http://localhost:3000/src/assets/js/public/public.js', |
| 91 | [], |
| 92 | null, |
| 93 | true |
| 94 | ); |
| 95 | } else { |
| 96 | wp_enqueue_script( |
| 97 | $scriptId, |
| 98 | AMELIA_URL . 'v3/public/assets/public.75c7a649.js', |
| 99 | [], |
| 100 | AMELIA_VERSION, |
| 101 | true |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | $ameliaLocale = apply_filters('amelia_modify_locale_filter', AMELIA_LOCALE) ?: AMELIA_LOCALE; |
| 106 | |
| 107 | wp_localize_script( |
| 108 | $scriptId, |
| 109 | 'localeLanguage', |
| 110 | [$ameliaLocale] |
| 111 | ); |
| 112 | |
| 113 | wp_localize_script( |
| 114 | $scriptId, |
| 115 | 'wpAmeliaSettings', |
| 116 | $settingsService->getFrontendSettings() |
| 117 | ); |
| 118 | |
| 119 | // Strings Localization |
| 120 | wp_localize_script( |
| 121 | $scriptId, |
| 122 | 'wpAmeliaLabels', |
| 123 | FrontendStrings::getAllStrings() |
| 124 | ); |
| 125 | |
| 126 | wp_localize_script( |
| 127 | $scriptId, |
| 128 | 'wpAmeliaTimeZone', |
| 129 | [DateTimeService::getTimeZone()->getName()] |
| 130 | ); |
| 131 | |
| 132 | $ameliaUrl = AMELIA_URL; |
| 133 | |
| 134 | $ameliaActionUrl = AMELIA_ACTION_URL; |
| 135 | |
| 136 | if (strpos($ameliaUrl, 'http://') === 0) { |
| 137 | $ameliaUrl = substr($ameliaUrl, strpos(substr($ameliaUrl, 7), '/') + 7); |
| 138 | |
| 139 | $ameliaActionUrl = substr($ameliaActionUrl, strpos(substr($ameliaActionUrl, 7), '/') + 7); |
| 140 | } elseif (strpos($ameliaUrl, 'https://') === 0) { |
| 141 | $ameliaUrl = substr($ameliaUrl, strpos(substr($ameliaUrl, 8), '/') + 8); |
| 142 | |
| 143 | $ameliaActionUrl = substr($ameliaActionUrl, strpos(substr($ameliaActionUrl, 8), '/') + 8); |
| 144 | } |
| 145 | |
| 146 | wp_localize_script( |
| 147 | $scriptId, |
| 148 | 'wpAmeliaUrls', |
| 149 | [ |
| 150 | 'wpAmeliaUseUploadsAmeliaPath' => AMELIA_UPLOADS_FILES_PATH_USE, |
| 151 | 'wpAmeliaPluginURL' => $ameliaUrl, |
| 152 | 'wpAmeliaPluginAjaxURL' => $ameliaActionUrl, |
| 153 | ] |
| 154 | ); |
| 155 | |
| 156 | if (!empty($_GET['ameliaCache']) || !empty($_GET['ameliaWcCache'])) { |
| 157 | $container = $container ?: require AMELIA_PATH . '/src/Infrastructure/ContainerConfig/container.php'; |
| 158 | |
| 159 | /** @var CacheApplicationService $cacheAS */ |
| 160 | $cacheAS = $container->get('application.cache.service'); |
| 161 | |
| 162 | try { |
| 163 | $cacheData = !empty($_GET['ameliaCache']) ? |
| 164 | $cacheAS->getCacheByName($_GET['ameliaCache']) : $cacheAS->getWcCacheByName($_GET['ameliaWcCache']); |
| 165 | |
| 166 | wp_localize_script( |
| 167 | $scriptId, |
| 168 | 'ameliaCache', |
| 169 | [$cacheData ? str_replace('"', '\\"', json_encode($cacheData)) : ''] |
| 170 | ); |
| 171 | } catch (QueryExecutionException $e) { |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | if ($settingsService->getSetting('activation', 'stash')) { |
| 176 | $container = $container ?: require AMELIA_PATH . '/src/Infrastructure/ContainerConfig/container.php'; |
| 177 | |
| 178 | /** @var StashApplicationService $stashAS */ |
| 179 | $stashAS = $container->get('application.stash.service'); |
| 180 | |
| 181 | wp_localize_script( |
| 182 | $scriptId, |
| 183 | 'ameliaEntities', |
| 184 | $stashAS->getStash() |
| 185 | ); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * @param string $tag |
| 191 | * @param string $handle |
| 192 | * @param string $src |
| 193 | * |
| 194 | * @return string |
| 195 | */ |
| 196 | public static function prepareScripts($tag, $handle, $src) |
| 197 | { |
| 198 | switch ($handle) { |
| 199 | case ('amelia_booking_scripts_dev_vite'): |
| 200 | case ('amelia_booking_scripts_dev_main'): |
| 201 | return "<script type='module' src='{$src}'></script>"; |
| 202 | |
| 203 | case ('amelia_booking_script_index'): |
| 204 | $settingsService = new SettingsService(new SettingsStorage()); |
| 205 | |
| 206 | if ($settingsService->getSetting('activation', 'v3RelativePath')) { |
| 207 | $customUrl = $settingsService->getSetting('activation', 'customUrl'); |
| 208 | |
| 209 | $position = strpos($src, $customUrl['pluginPath'] . 'v3/public/assets/public.'); |
| 210 | |
| 211 | if ($position !== false) { |
| 212 | $src = substr($src, $position); |
| 213 | } |
| 214 | } elseif (strpos($src, 'http://') === 0) { |
| 215 | $src = substr($src, strpos(substr($src, 7), '/') + 7); |
| 216 | } elseif (strpos($src, 'https://') === 0) { |
| 217 | $src = substr($src, strpos(substr($src, 8), '/') + 8); |
| 218 | } |
| 219 | |
| 220 | $asyncLoading = $settingsService->getSetting('activation', 'v3AsyncLoading') ? |
| 221 | 'async' : ''; |
| 222 | |
| 223 | return "<script type='module' {$asyncLoading} crossorigin src='{$src}'></script>"; |
| 224 | |
| 225 | case ('amelia_booking_script_vendor'): |
| 226 | return "<link rel='modulepreload' href='{$src}'>"; |
| 227 | |
| 228 | default: |
| 229 | return $tag; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * @param string $tag |
| 235 | * @param string $handle |
| 236 | * @param string $href |
| 237 | * |
| 238 | * @return string |
| 239 | */ |
| 240 | public static function prepareStyles($tag, $handle, $href) |
| 241 | { |
| 242 | switch ($handle) { |
| 243 | case ('amelia_booking_style_index'): |
| 244 | case ('amelia_booking_style_vendor'): |
| 245 | return "<link rel='stylesheet' href='{$href}'>"; |
| 246 | |
| 247 | default: |
| 248 | return $tag; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 |