| 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\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 extends AmeliaShortcodeService |
| 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 |
self::enqueuePaypalScript($settingsService); |
| 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', 'square')['enabled'] === true) { |
| 54 |
if ($settingsService->getSetting('payments', 'square')['testMode'] === true) { |
| 55 |
wp_enqueue_script('amelia_square_js', 'https://sandbox.web.squarecdn.com/v1/square.js'); |
| 56 |
} else { |
| 57 |
wp_enqueue_script('amelia_square_js', 'https://web.squarecdn.com/v1/square.js'); |
| 58 |
} |
| 59 |
wp_enqueue_style( |
| 60 |
'amelia_google_button_style', |
| 61 |
'https://developers.google.com/reference/sdks/web/static/styles/code-preview.css', |
| 62 |
[], |
| 63 |
null, |
| 64 |
'all' |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
if ($settingsService->getSetting('payments', 'razorpay')['enabled'] === true) { |
| 69 |
wp_enqueue_script('amelia_razorpay_script', 'https://checkout.razorpay.com/v1/checkout.js'); |
| 70 |
} |
| 71 |
|
| 72 |
$gmapApiKey = $settingsService->getSetting('general', 'gMapApiKey'); |
| 73 |
|
| 74 |
if ($gmapApiKey) { |
| 75 |
$container = require AMELIA_PATH . '/src/Infrastructure/ContainerConfig/container.php'; |
| 76 |
|
| 77 |
/** @var CustomFieldRepository $customFieldRepository */ |
| 78 |
$customFieldRepository = $container->get('domain.customField.repository'); |
| 79 |
|
| 80 |
$addressCustomFields = $customFieldRepository->getByFieldValue('type', 'address'); |
| 81 |
|
| 82 |
if (count($addressCustomFields->getItems())) { |
| 83 |
wp_enqueue_script( |
| 84 |
'amelia_google_maps_api', |
| 85 |
"https://maps.googleapis.com/maps/api/js?key={$gmapApiKey}&libraries=places&loading=async" |
| 86 |
); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
$scriptId = AMELIA_DEV ? 'amelia_booking_scripts_dev_vite' : 'amelia_booking_script_index'; |
| 91 |
|
| 92 |
if (AMELIA_DEV) { |
| 93 |
wp_enqueue_script( |
| 94 |
$scriptId, |
| 95 |
'http://localhost:3000/@vite/client', |
| 96 |
[], |
| 97 |
null, |
| 98 |
false |
| 99 |
); |
| 100 |
|
| 101 |
wp_enqueue_script( |
| 102 |
'amelia_booking_scripts_dev_main', |
| 103 |
'http://localhost:3000/src/assets/js/public/public.js', |
| 104 |
[], |
| 105 |
null, |
| 106 |
true |
| 107 |
); |
| 108 |
} else { |
| 109 |
wp_enqueue_script( |
| 110 |
$scriptId, |
| 111 |
AMELIA_URL . 'v3/public/assets/public.f73e6204.js', |
| 112 |
[], |
| 113 |
AMELIA_VERSION, |
| 114 |
true |
| 115 |
); |
| 116 |
} |
| 117 |
|
| 118 |
$ameliaLocale = apply_filters('amelia_modify_locale_filter', AMELIA_LOCALE) ?: AMELIA_LOCALE; |
| 119 |
|
| 120 |
wp_localize_script( |
| 121 |
$scriptId, |
| 122 |
'localeLanguage', |
| 123 |
[$ameliaLocale] |
| 124 |
); |
| 125 |
|
| 126 |
wp_localize_script( |
| 127 |
$scriptId, |
| 128 |
'wpAmeliaSettings', |
| 129 |
$settingsService->getFrontendSettings() |
| 130 |
); |
| 131 |
|
| 132 |
// Strings Localization |
| 133 |
wp_localize_script( |
| 134 |
$scriptId, |
| 135 |
'wpAmeliaLabels', |
| 136 |
FrontendStrings::getAllStrings() |
| 137 |
); |
| 138 |
|
| 139 |
wp_localize_script( |
| 140 |
$scriptId, |
| 141 |
'wpAmeliaTimeZone', |
| 142 |
[DateTimeService::getTimeZone()->getName()] |
| 143 |
); |
| 144 |
|
| 145 |
$ameliaUrl = AMELIA_URL; |
| 146 |
|
| 147 |
$ameliaActionUrl = AMELIA_ACTION_URL; |
| 148 |
|
| 149 |
if (strpos($ameliaUrl, 'http://') === 0) { |
| 150 |
$ameliaUrl = substr($ameliaUrl, strpos(substr($ameliaUrl, 7), '/') + 7); |
| 151 |
|
| 152 |
$ameliaActionUrl = substr($ameliaActionUrl, strpos(substr($ameliaActionUrl, 7), '/') + 7); |
| 153 |
} elseif (strpos($ameliaUrl, 'https://') === 0) { |
| 154 |
$ameliaUrl = substr($ameliaUrl, strpos(substr($ameliaUrl, 8), '/') + 8); |
| 155 |
|
| 156 |
$ameliaActionUrl = substr($ameliaActionUrl, strpos(substr($ameliaActionUrl, 8), '/') + 8); |
| 157 |
} |
| 158 |
|
| 159 |
wp_localize_script( |
| 160 |
$scriptId, |
| 161 |
'wpAmeliaUrls', |
| 162 |
[ |
| 163 |
'wpAmeliaUseUploadsAmeliaPath' => AMELIA_UPLOADS_FILES_PATH_USE, |
| 164 |
'wpAmeliaPluginURL' => $ameliaUrl, |
| 165 |
'wpAmeliaPluginAjaxURL' => $ameliaActionUrl, |
| 166 |
] |
| 167 |
); |
| 168 |
|
| 169 |
if (!empty($_GET['ameliaCache']) || !empty($_GET['ameliaWcCache'])) { |
| 170 |
$container = $container ?: require AMELIA_PATH . '/src/Infrastructure/ContainerConfig/container.php'; |
| 171 |
|
| 172 |
/** @var CacheApplicationService $cacheAS */ |
| 173 |
$cacheAS = $container->get('application.cache.service'); |
| 174 |
|
| 175 |
try { |
| 176 |
$cacheData = !empty($_GET['ameliaCache']) ? |
| 177 |
$cacheAS->getCacheByName($_GET['ameliaCache']) : $cacheAS->getWcCacheByName($_GET['ameliaWcCache']); |
| 178 |
|
| 179 |
wp_localize_script( |
| 180 |
$scriptId, |
| 181 |
'ameliaCache', |
| 182 |
[$cacheData ? str_replace('"', '\\"', json_encode($cacheData)) : ''] |
| 183 |
); |
| 184 |
} catch (QueryExecutionException $e) { |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
if ($settingsService->getSetting('activation', 'stash')) { |
| 189 |
$container = $container ?: require AMELIA_PATH . '/src/Infrastructure/ContainerConfig/container.php'; |
| 190 |
|
| 191 |
/** @var StashApplicationService $stashAS */ |
| 192 |
$stashAS = $container->get('application.stash.service'); |
| 193 |
|
| 194 |
wp_localize_script( |
| 195 |
$scriptId, |
| 196 |
'ameliaEntities', |
| 197 |
$stashAS->getStash() |
| 198 |
); |
| 199 |
} |
| 200 |
|
| 201 |
do_action('amelia_scripts_loaded'); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* @param string $tag |
| 206 |
* @param string $handle |
| 207 |
* @param string $src |
| 208 |
* |
| 209 |
* @return string |
| 210 |
*/ |
| 211 |
public static function prepareScripts($tag, $handle, $src) |
| 212 |
{ |
| 213 |
switch ($handle) { |
| 214 |
case ('amelia_booking_scripts_dev_vite'): |
| 215 |
case ('amelia_booking_scripts_dev_main'): |
| 216 |
case ('amelia_dev_vite_client'): |
| 217 |
case ('amelia_dev_main_script'): |
| 218 |
case ('amelia_prod_main_script'): |
| 219 |
return "<script type='module' src='{$src}'></script>"; |
| 220 |
|
| 221 |
case ('amelia_booking_script_index'): |
| 222 |
$settingsService = new SettingsService(new SettingsStorage()); |
| 223 |
|
| 224 |
if ($settingsService->getSetting('activation', 'v3RelativePath')) { |
| 225 |
$customUrl = $settingsService->getSetting('activation', 'customUrl'); |
| 226 |
|
| 227 |
$position = strpos($src, $customUrl['pluginPath'] . 'v3/public/assets/public.'); |
| 228 |
|
| 229 |
if ($position !== false) { |
| 230 |
$src = substr($src, $position); |
| 231 |
} |
| 232 |
} elseif (strpos($src, 'http://') === 0) { |
| 233 |
$src = substr($src, strpos(substr($src, 7), '/') + 7); |
| 234 |
} elseif (strpos($src, 'https://') === 0) { |
| 235 |
$src = substr($src, strpos(substr($src, 8), '/') + 8); |
| 236 |
} |
| 237 |
|
| 238 |
$asyncLoading = $settingsService->getSetting('activation', 'v3AsyncLoading') ? |
| 239 |
'async' : ''; |
| 240 |
|
| 241 |
return "<script type='module' {$asyncLoading} crossorigin src='{$src}'></script>"; |
| 242 |
|
| 243 |
case ('amelia_booking_script_vendor'): |
| 244 |
return "<link rel='modulepreload' href='{$src}'>"; |
| 245 |
|
| 246 |
default: |
| 247 |
return $tag; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* @param string $tag |
| 253 |
* @param string $handle |
| 254 |
* @param string $href |
| 255 |
* |
| 256 |
* @return string |
| 257 |
*/ |
| 258 |
public static function prepareStyles($tag, $handle, $href) |
| 259 |
{ |
| 260 |
switch ($handle) { |
| 261 |
case ('amelia_booking_style_index'): |
| 262 |
case ('amelia_booking_style_vendor'): |
| 263 |
return "<link rel='stylesheet' href='{$href}'>"; |
| 264 |
|
| 265 |
default: |
| 266 |
return $tag; |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
|