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