AmeliaBookingShortcodeService.php
2 weeks ago
AmeliaShortcodeService.php
2 months ago
BookingShortcodeService.php
6 months ago
CatalogBookingShortcodeService.php
2 weeks ago
CatalogShortcodeService.php
6 months ago
EventsCalendarBookingShortcodeService.php
2 weeks ago
EventsListBookingShortcodeService.php
2 weeks ago
EventsShortcodeService.php
6 months ago
StepBookingShortcodeService.php
2 weeks ago
AmeliaBookingShortcodeService.php
350 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\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\Container; |
| 16 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 17 | use AmeliaBooking\Infrastructure\Repository\CustomField\CustomFieldRepository; |
| 18 | use AmeliaBooking\Infrastructure\WP\SettingsService\SettingsStorage; |
| 19 | use AmeliaBooking\Infrastructure\WP\Translations\FrontendStrings; |
| 20 | use AmeliaBooking\Infrastructure\WP\Integrations\IvyForms\IvyFormsService; |
| 21 | use AmeliaBooking\Infrastructure\WP\Integrations\PluginInstaller; |
| 22 | |
| 23 | /** |
| 24 | * Class AmeliaBookingShortcodeService |
| 25 | * |
| 26 | * @package AmeliaBooking\Infrastructure\WP\ShortcodeService |
| 27 | */ |
| 28 | class AmeliaBookingShortcodeService |
| 29 | { |
| 30 | public static $counter = 1000; |
| 31 | |
| 32 | /** @var Container $container */ |
| 33 | protected static $container = null; |
| 34 | |
| 35 | /** |
| 36 | * Prepare scripts and styles |
| 37 | * @throws InvalidArgumentException |
| 38 | */ |
| 39 | public static function prepareScriptsAndStyles() |
| 40 | { |
| 41 | self::$container = self::$container ?: require AMELIA_PATH . '/src/Infrastructure/ContainerConfig/container.php'; |
| 42 | |
| 43 | self::$counter++; |
| 44 | |
| 45 | if (self::$counter > 1001) { |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | /** @var SettingsService $settingsService */ |
| 50 | $settingsService = self::$container->get('domain.settings.service'); |
| 51 | |
| 52 | self::enqueuePaypalScript($settingsService); |
| 53 | |
| 54 | if ($settingsService->getSetting('payments', 'stripe')['enabled'] === true) { |
| 55 | wp_enqueue_script('amelia_stripe_script', 'https://js.stripe.com/v3/'); |
| 56 | } |
| 57 | |
| 58 | if ($settingsService->getSetting('payments', 'square')['enabled'] === true) { |
| 59 | if ($settingsService->getSetting('payments', 'square')['testMode'] === true) { |
| 60 | wp_enqueue_script('amelia_square_js', 'https://sandbox.web.squarecdn.com/v1/square.js'); |
| 61 | } else { |
| 62 | wp_enqueue_script('amelia_square_js', 'https://web.squarecdn.com/v1/square.js'); |
| 63 | } |
| 64 | wp_enqueue_style( |
| 65 | 'amelia_google_button_style', |
| 66 | 'https://developers.google.com/reference/sdks/web/static/styles/code-preview.css', |
| 67 | [], |
| 68 | null, |
| 69 | 'all' |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | if ($settingsService->getSetting('payments', 'razorpay')['enabled'] === true) { |
| 74 | wp_enqueue_script('amelia_razorpay_script', 'https://checkout.razorpay.com/v1/checkout.js'); |
| 75 | } |
| 76 | |
| 77 | $gmapApiKey = $settingsService->getSetting('general', 'gMapApiKey'); |
| 78 | |
| 79 | if ($gmapApiKey) { |
| 80 | /** @var CustomFieldRepository $customFieldRepository */ |
| 81 | $customFieldRepository = self::$container->get('domain.customField.repository'); |
| 82 | |
| 83 | $addressCustomFields = $customFieldRepository->getByFieldValue('type', 'address'); |
| 84 | |
| 85 | if (count($addressCustomFields->getItems())) { |
| 86 | wp_enqueue_script( |
| 87 | 'amelia_google_maps_api', |
| 88 | "https://maps.googleapis.com/maps/api/js?key={$gmapApiKey}&libraries=places&loading=async" |
| 89 | ); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | $scriptId = AMELIA_DEV ? 'amelia_booking_scripts_dev_vite' : 'amelia_booking_script_index'; |
| 94 | |
| 95 | if (AMELIA_DEV) { |
| 96 | wp_enqueue_script( |
| 97 | $scriptId, |
| 98 | 'http://localhost:3000/@vite/client', |
| 99 | [], |
| 100 | null, |
| 101 | false |
| 102 | ); |
| 103 | |
| 104 | wp_enqueue_script( |
| 105 | 'amelia_booking_scripts_dev_main', |
| 106 | 'http://localhost:3000/src/assets/js/public/public.js', |
| 107 | [], |
| 108 | null, |
| 109 | true |
| 110 | ); |
| 111 | } else { |
| 112 | wp_enqueue_script( |
| 113 | $scriptId, |
| 114 | AMELIA_URL . 'v3/public/assets/public.js', |
| 115 | [], |
| 116 | AMELIA_VERSION, |
| 117 | true |
| 118 | ); |
| 119 | |
| 120 | // Vite bundles all Vue/Element/Maz CSS into one file (cssCodeSplit: false). |
| 121 | // Dynamic chunk imports do not inject this stylesheet in WordPress; enqueue explicitly. |
| 122 | wp_enqueue_style( |
| 123 | 'amelia_booking_v3_style', |
| 124 | AMELIA_URL . 'v3/public/assets/style.css', |
| 125 | [], |
| 126 | AMELIA_VERSION |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | $ameliaLocale = apply_filters('amelia_modify_locale_filter', AMELIA_LOCALE) ?: AMELIA_LOCALE; |
| 131 | |
| 132 | wp_localize_script( |
| 133 | $scriptId, |
| 134 | 'localeLanguage', |
| 135 | [$ameliaLocale] |
| 136 | ); |
| 137 | |
| 138 | wp_localize_script( |
| 139 | $scriptId, |
| 140 | 'wpAmeliaSettings', |
| 141 | $settingsService->getFrontendSettings() |
| 142 | ); |
| 143 | |
| 144 | // Strings Localization |
| 145 | wp_localize_script( |
| 146 | $scriptId, |
| 147 | 'wpAmeliaLabels', |
| 148 | FrontendStrings::getAllStrings() |
| 149 | ); |
| 150 | |
| 151 | wp_localize_script( |
| 152 | $scriptId, |
| 153 | 'wpAmeliaTimeZone', |
| 154 | [DateTimeService::getTimeZone()->getName()] |
| 155 | ); |
| 156 | |
| 157 | $ameliaUrl = AMELIA_URL; |
| 158 | |
| 159 | $ameliaActionUrl = AMELIA_ACTION_URL; |
| 160 | |
| 161 | if (strpos($ameliaUrl, 'http://') === 0) { |
| 162 | $ameliaUrl = substr($ameliaUrl, strpos(substr($ameliaUrl, 7), '/') + 7); |
| 163 | |
| 164 | $ameliaActionUrl = substr($ameliaActionUrl, strpos(substr($ameliaActionUrl, 7), '/') + 7); |
| 165 | } elseif (strpos($ameliaUrl, 'https://') === 0) { |
| 166 | $ameliaUrl = substr($ameliaUrl, strpos(substr($ameliaUrl, 8), '/') + 8); |
| 167 | |
| 168 | $ameliaActionUrl = substr($ameliaActionUrl, strpos(substr($ameliaActionUrl, 8), '/') + 8); |
| 169 | } |
| 170 | |
| 171 | wp_localize_script( |
| 172 | $scriptId, |
| 173 | 'wpAmeliaUrls', |
| 174 | [ |
| 175 | 'wpAmeliaUseUploadsAmeliaPath' => AMELIA_UPLOADS_FILES_PATH_USE, |
| 176 | 'wpAmeliaPluginURL' => $ameliaUrl, |
| 177 | 'wpAmeliaPluginAjaxURL' => $ameliaActionUrl, |
| 178 | ] |
| 179 | ); |
| 180 | |
| 181 | if (!empty($_GET['ameliaCache']) || !empty($_GET['ameliaWcCache'])) { |
| 182 | /** @var CacheApplicationService $cacheAS */ |
| 183 | $cacheAS = self::$container->get('application.cache.service'); |
| 184 | |
| 185 | try { |
| 186 | $cacheData = !empty($_GET['ameliaCache']) ? |
| 187 | $cacheAS->getCacheByName($_GET['ameliaCache']) : $cacheAS->getWcCacheByName($_GET['ameliaWcCache']); |
| 188 | |
| 189 | wp_localize_script( |
| 190 | $scriptId, |
| 191 | 'ameliaCache', |
| 192 | [$cacheData ? str_replace('"', '\\"', json_encode($cacheData)) : ''] |
| 193 | ); |
| 194 | } catch (QueryExecutionException $e) { |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | if ($settingsService->getSetting('activation', 'stash')) { |
| 199 | /** @var StashApplicationService $stashAS */ |
| 200 | $stashAS = self::$container->get('application.stash.service'); |
| 201 | |
| 202 | wp_localize_script( |
| 203 | $scriptId, |
| 204 | 'ameliaEntities', |
| 205 | $stashAS->getStash() |
| 206 | ); |
| 207 | } |
| 208 | |
| 209 | do_action('amelia_scripts_loaded'); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * @param string $tag |
| 214 | * @param string $handle |
| 215 | * @param string $src |
| 216 | * |
| 217 | * @return string |
| 218 | */ |
| 219 | public static function prepareScripts($tag, $handle, $src) |
| 220 | { |
| 221 | switch ($handle) { |
| 222 | case ('amelia_booking_scripts_dev_vite'): |
| 223 | case ('amelia_booking_scripts_dev_main'): |
| 224 | case ('amelia_dev_vite_client'): |
| 225 | case ('amelia_dev_main_script'): |
| 226 | case ('amelia_prod_main_script'): |
| 227 | return "<script type='module' src='{$src}'></script>"; |
| 228 | |
| 229 | case ('amelia_booking_script_index'): |
| 230 | $settingsService = new SettingsService(new SettingsStorage()); |
| 231 | |
| 232 | if ($settingsService->getSetting('activation', 'v3RelativePath')) { |
| 233 | $customUrl = $settingsService->getSetting('activation', 'customUrl'); |
| 234 | |
| 235 | $position = strpos($src, $customUrl['pluginPath'] . 'v3/public/assets/public.'); |
| 236 | |
| 237 | if ($position !== false) { |
| 238 | $src = substr($src, $position); |
| 239 | } |
| 240 | } elseif (strpos($src, 'http://') === 0) { |
| 241 | $src = substr($src, strpos(substr($src, 7), '/') + 7); |
| 242 | } elseif (strpos($src, 'https://') === 0) { |
| 243 | $src = substr($src, strpos(substr($src, 8), '/') + 8); |
| 244 | } |
| 245 | |
| 246 | $asyncLoading = $settingsService->getSetting('activation', 'v3AsyncLoading') ? |
| 247 | 'async' : ''; |
| 248 | |
| 249 | return "<script type='module' {$asyncLoading} crossorigin src='{$src}'></script>"; |
| 250 | |
| 251 | case ('amelia_booking_script_vendor'): |
| 252 | return "<link rel='modulepreload' href='{$src}'>"; |
| 253 | |
| 254 | default: |
| 255 | return $tag; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * @param string $tag |
| 261 | * @param string $handle |
| 262 | * @param string $href |
| 263 | * |
| 264 | * @return string |
| 265 | */ |
| 266 | public static function prepareStyles($tag, $handle, $href) |
| 267 | { |
| 268 | switch ($handle) { |
| 269 | case ('amelia_booking_v3_style'): |
| 270 | case ('amelia_booking_style_index'): |
| 271 | case ('amelia_booking_style_vendor'): |
| 272 | $settingsService = new SettingsService(new SettingsStorage()); |
| 273 | |
| 274 | if ($settingsService->getSetting('activation', 'v3RelativePath')) { |
| 275 | $customUrl = $settingsService->getSetting('activation', 'customUrl'); |
| 276 | |
| 277 | $position = strpos($href, $customUrl['pluginPath'] . 'v3/public/assets/style.'); |
| 278 | |
| 279 | if ($position !== false) { |
| 280 | $href = substr($href, $position); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | return "<link rel='stylesheet' href='{$href}'>"; |
| 285 | |
| 286 | default: |
| 287 | return $tag; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Enqueue PayPal script |
| 293 | */ |
| 294 | protected static function enqueuePaypalScript(SettingsService $settingsService): void |
| 295 | { |
| 296 | if ($settingsService->getSetting('payments', 'payPal')['enabled'] === true) { |
| 297 | $payPalSettings = $settingsService->getSetting('payments', 'payPal'); |
| 298 | $payPalClientId = $payPalSettings['sandboxMode'] |
| 299 | ? ($payPalSettings['testApiClientId'] ?? '') |
| 300 | : ($payPalSettings['liveApiClientId'] ?? ''); |
| 301 | $payPalCurrency = $settingsService->getSetting('payments', 'currency') ?: 'USD'; |
| 302 | wp_enqueue_script( |
| 303 | 'amelia_paypal_script', |
| 304 | 'https://www.paypal.com/sdk/js?client-id=' . urlencode($payPalClientId) |
| 305 | . '¤cy=' . urlencode($payPalCurrency) |
| 306 | . '&components=buttons' |
| 307 | . '&disable-funding=venmo,paylater,card,sepa,bancontact,blik,eps,giropay,ideal,mercadopago,mybank,p24,sofort', |
| 308 | [], |
| 309 | null, |
| 310 | false |
| 311 | ); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * @param string $viewPath |
| 317 | * @param array $params |
| 318 | * @return string |
| 319 | */ |
| 320 | protected static function renderView(string $viewPath, array $params): string |
| 321 | { |
| 322 | if (!empty($_GET['ameliaWcCache']) || !empty($_GET['ameliaCache'])) { |
| 323 | $params['ivy'] = ''; |
| 324 | } |
| 325 | |
| 326 | self::prepareScriptsAndStyles(); |
| 327 | |
| 328 | /** @var SettingsService $settingsService */ |
| 329 | $settingsService = self::$container->get('domain.settings.service'); |
| 330 | |
| 331 | $html = !empty($params['ivy']) && $settingsService->isFeatureEnabled('ivy') && PluginInstaller::isPluginActive('ivyforms') |
| 332 | ? IvyFormsService::shortcode($params['ivy']) |
| 333 | : ''; |
| 334 | |
| 335 | if (empty($html)) { |
| 336 | $params['ivy'] = ''; |
| 337 | } |
| 338 | |
| 339 | ob_start(); |
| 340 | |
| 341 | include AMELIA_PATH . '/view/frontend/' . $viewPath; |
| 342 | |
| 343 | $html .= ob_get_contents(); |
| 344 | |
| 345 | ob_end_clean(); |
| 346 | |
| 347 | return $html; |
| 348 | } |
| 349 | } |
| 350 |