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
AmeliaShortcodeService.php
244 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\CustomField\AbstractCustomFieldApplicationService; |
| 12 | use AmeliaBooking\Application\Services\Stash\StashApplicationService; |
| 13 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 14 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 15 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 16 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 17 | use AmeliaBooking\Infrastructure\WP\SettingsService\SettingsStorage; |
| 18 | use AmeliaBooking\Infrastructure\WP\Translations\FrontendStrings; |
| 19 | |
| 20 | /** |
| 21 | * Class AmeliaShortcodeService |
| 22 | * |
| 23 | * @package AmeliaBooking\Infrastructure\WP\ShortcodeService |
| 24 | */ |
| 25 | class AmeliaShortcodeService |
| 26 | { |
| 27 | public static $counter = 0; |
| 28 | |
| 29 | /** |
| 30 | * Prepare scripts and styles |
| 31 | * @throws InvalidArgumentException |
| 32 | */ |
| 33 | public static function prepareScriptsAndStyles() |
| 34 | { |
| 35 | $container = null; |
| 36 | |
| 37 | self::$counter++; |
| 38 | |
| 39 | if (self::$counter > 1) { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | $settingsService = new SettingsService(new SettingsStorage()); |
| 44 | |
| 45 | self::enqueuePaypalScript($settingsService); |
| 46 | |
| 47 | if ($settingsService->getSetting('payments', 'razorpay')['enabled'] === true) { |
| 48 | wp_enqueue_script('amelia_razorpay_script', 'https://checkout.razorpay.com/v1/checkout.js'); |
| 49 | } |
| 50 | |
| 51 | $gmapApiKey = $settingsService->getSetting('general', 'gMapApiKey'); |
| 52 | |
| 53 | if ($gmapApiKey) { |
| 54 | wp_enqueue_script( |
| 55 | 'amelia_google_maps_api', |
| 56 | "https://maps.googleapis.com/maps/api/js?key={$gmapApiKey}&libraries=places&loading=async" |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | // Fix for Divi theme. |
| 61 | // Don't enqueue script if it's activated Divi Visual Page Builder |
| 62 | if (empty($_GET['et_fb'])) { |
| 63 | wp_enqueue_script( |
| 64 | 'amelia_booking_scripts', |
| 65 | AMELIA_URL . 'public/js/frontend/amelia-booking.js', |
| 66 | [], |
| 67 | AMELIA_VERSION, |
| 68 | true |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | if ($settingsService->getSetting('payments', 'stripe')['enabled'] === true) { |
| 73 | wp_enqueue_script('amelia_stripe_js', 'https://js.stripe.com/v3/'); |
| 74 | } |
| 75 | |
| 76 | if ($settingsService->getSetting('payments', 'square')['enabled'] === true) { |
| 77 | if ($settingsService->getSetting('payments', 'square')['testMode'] === true) { |
| 78 | wp_enqueue_script('amelia_square_js', 'https://sandbox.web.squarecdn.com/v1/square.js'); |
| 79 | } else { |
| 80 | wp_enqueue_script('amelia_square_js', 'https://web.squarecdn.com/v1/square.js'); |
| 81 | } |
| 82 | wp_enqueue_style( |
| 83 | 'amelia_google_button_style', |
| 84 | 'https://developers.google.com/reference/sdks/web/static/styles/code-preview.css', |
| 85 | [], |
| 86 | null, |
| 87 | 'all' |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | // Enqueue Styles |
| 92 | wp_enqueue_style( |
| 93 | 'amelia_booking_styles_vendor', |
| 94 | AMELIA_URL . 'public/css/frontend/vendor.css', |
| 95 | [], |
| 96 | AMELIA_VERSION |
| 97 | ); |
| 98 | |
| 99 | $customUrl = $settingsService->getSetting('activation', 'customUrl'); |
| 100 | |
| 101 | if ( |
| 102 | $settingsService->getSetting('customization', 'useGenerated') === null || |
| 103 | $settingsService->getSetting('customization', 'useGenerated') |
| 104 | ) { |
| 105 | wp_enqueue_style( |
| 106 | 'amelia_booking_styles', |
| 107 | (!empty($customUrl['uploadsPath']) ? $customUrl['uploadsPath'] : AMELIA_UPLOADS_URL) . '/amelia/css/amelia-booking.' . |
| 108 | $settingsService->getSetting('customization', 'hash') . '.css', |
| 109 | [], |
| 110 | AMELIA_VERSION |
| 111 | ); |
| 112 | } else { |
| 113 | wp_enqueue_style( |
| 114 | 'amelia_booking_styles', |
| 115 | AMELIA_URL . 'public/css/frontend/amelia-booking-' . str_replace('.', '-', AMELIA_VERSION) . '.css', |
| 116 | [], |
| 117 | AMELIA_VERSION |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | // Underscore |
| 122 | wp_enqueue_script('undescore', includes_url('js') . '/underscore.min.js'); |
| 123 | |
| 124 | // Strings Localization |
| 125 | wp_localize_script( |
| 126 | 'amelia_booking_scripts', |
| 127 | 'wpAmeliaLabels', |
| 128 | FrontendStrings::getAllStrings() |
| 129 | ); |
| 130 | |
| 131 | // Settings Localization |
| 132 | wp_localize_script( |
| 133 | 'amelia_booking_scripts', |
| 134 | 'wpAmeliaSettings', |
| 135 | $settingsService->getFrontendSettings() |
| 136 | ); |
| 137 | |
| 138 | wp_localize_script( |
| 139 | 'amelia_booking_scripts', |
| 140 | 'wpAmeliaUrls', |
| 141 | [ |
| 142 | 'wpAmeliaUseUploadsAmeliaPath' => AMELIA_UPLOADS_FILES_PATH_USE, |
| 143 | 'wpAmeliaPluginURL' => empty($customUrl['enabled']) |
| 144 | ? AMELIA_URL : AMELIA_HOME_URL . $customUrl['pluginPath'], |
| 145 | 'wpAmeliaPluginAjaxURL' => empty($customUrl['enabled']) |
| 146 | ? AMELIA_ACTION_URL : AMELIA_HOME_URL . $customUrl['ajaxPath'] . '?' . AMELIA_ACTION_SLUG . '', |
| 147 | ] |
| 148 | ); |
| 149 | |
| 150 | wp_localize_script( |
| 151 | 'amelia_booking_scripts', |
| 152 | 'localeLanguage', |
| 153 | [AMELIA_LOCALE] |
| 154 | ); |
| 155 | |
| 156 | $localeSubstitutes = $settingsService->getSetting('general', 'calendarLocaleSubstitutes'); |
| 157 | |
| 158 | if (isset($localeSubstitutes[AMELIA_LOCALE])) { |
| 159 | wp_localize_script( |
| 160 | 'amelia_booking_scripts', |
| 161 | 'ameliaCalendarLocale', |
| 162 | [$localeSubstitutes[AMELIA_LOCALE]] |
| 163 | ); |
| 164 | } |
| 165 | |
| 166 | $allowedUploadedFileExtensions = |
| 167 | !empty($settingsService->getSetting('general', 'customFieldsAllowedExtensions')) |
| 168 | ? $settingsService->getSetting('general', 'customFieldsAllowedExtensions') |
| 169 | : AbstractCustomFieldApplicationService::$allowedUploadedFileExtensions; |
| 170 | |
| 171 | wp_localize_script( |
| 172 | 'amelia_booking_scripts', |
| 173 | 'fileUploadExtensions', |
| 174 | array_keys($allowedUploadedFileExtensions) |
| 175 | ); |
| 176 | |
| 177 | if ($settingsService->getSetting('activation', 'stash')) { |
| 178 | $container = require AMELIA_PATH . '/src/Infrastructure/ContainerConfig/container.php'; |
| 179 | |
| 180 | /** @var StashApplicationService $stashAS */ |
| 181 | $stashAS = $container->get('application.stash.service'); |
| 182 | |
| 183 | wp_localize_script( |
| 184 | 'amelia_booking_scripts', |
| 185 | 'ameliaEntities', |
| 186 | $stashAS->getStash() |
| 187 | ); |
| 188 | } |
| 189 | |
| 190 | if (!empty($_GET['ameliaCache']) || !empty($_GET['ameliaWcCache'])) { |
| 191 | $container = $container ?: require AMELIA_PATH . '/src/Infrastructure/ContainerConfig/container.php'; |
| 192 | |
| 193 | /** @var CacheApplicationService $cacheAS */ |
| 194 | $cacheAS = $container->get('application.cache.service'); |
| 195 | |
| 196 | try { |
| 197 | $cacheData = !empty($_GET['ameliaCache']) ? |
| 198 | $cacheAS->getCacheByName(sanitize_text_field($_GET['ameliaCache'])) : |
| 199 | $cacheAS->getWcCacheByName(sanitize_text_field($_GET['ameliaWcCache'])); |
| 200 | |
| 201 | wp_localize_script( |
| 202 | 'amelia_booking_scripts', |
| 203 | 'ameliaCache', |
| 204 | [$cacheData ? str_replace('"', '\\"', json_encode($cacheData)) : ''] |
| 205 | ); |
| 206 | } catch (QueryExecutionException $e) { |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | wp_localize_script( |
| 211 | 'amelia_booking_scripts', |
| 212 | 'wpAmeliaTimeZone', |
| 213 | [DateTimeService::getTimeZone()->getName()] |
| 214 | ); |
| 215 | |
| 216 | do_action('ameliaScriptsLoaded'); |
| 217 | do_action('amelia_scripts_loaded'); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Enqueue PayPal script |
| 222 | */ |
| 223 | protected static function enqueuePaypalScript(SettingsService $settingsService): void |
| 224 | { |
| 225 | if ($settingsService->getSetting('payments', 'payPal')['enabled'] === true) { |
| 226 | $payPalSettings = $settingsService->getSetting('payments', 'payPal'); |
| 227 | $payPalClientId = $payPalSettings['sandboxMode'] |
| 228 | ? ($payPalSettings['testApiClientId'] ?? '') |
| 229 | : ($payPalSettings['liveApiClientId'] ?? ''); |
| 230 | $payPalCurrency = $settingsService->getSetting('payments', 'currency') ?: 'USD'; |
| 231 | wp_enqueue_script( |
| 232 | 'amelia_paypal_script', |
| 233 | 'https://www.paypal.com/sdk/js?client-id=' . urlencode($payPalClientId) |
| 234 | . '¤cy=' . urlencode($payPalCurrency) |
| 235 | . '&components=buttons' |
| 236 | . '&disable-funding=venmo,paylater,card,sepa,bancontact,blik,eps,giropay,ideal,mercadopago,mybank,p24,sofort', |
| 237 | [], |
| 238 | null, |
| 239 | false |
| 240 | ); |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 |