PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.4.8
Booking for Appointments and Events Calendar – Amelia v2.4.8
2.4.9 2.4.8 2.4.7 2.4.6 2.4.5 2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Infrastructure / WP / ShortcodeService / AmeliaBookingShortcodeService.php
ameliabooking / src / Infrastructure / WP / ShortcodeService Last commit date
AmeliaBookingShortcodeService.php 1 week ago AmeliaShortcodeService.php 4 months ago BookingShortcodeService.php 8 months ago CatalogBookingShortcodeService.php 2 months ago CatalogShortcodeService.php 8 months ago EventsCalendarBookingShortcodeService.php 2 months ago EventsListBookingShortcodeService.php 2 months ago EventsShortcodeService.php 8 months ago ShortcodeAliasService.php 1 week ago StepBookingShortcodeService.php 2 months ago
AmeliaBookingShortcodeService.php
356 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(): void
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 wp_add_inline_script(
182 AMELIA_DEV ? 'amelia_booking_scripts_dev_main' : $scriptId,
183 'window.wpAmeliaNonce = ' . wp_json_encode(wp_create_nonce('ajax-nonce')) . ';',
184 'before'
185 );
186
187 if (!empty($_GET['ameliaCache']) || !empty($_GET['ameliaWcCache'])) {
188 /** @var CacheApplicationService $cacheAS */
189 $cacheAS = self::$container->get('application.cache.service');
190
191 try {
192 $cacheData = !empty($_GET['ameliaCache']) ?
193 $cacheAS->getCacheByName($_GET['ameliaCache']) : $cacheAS->getWcCacheByName($_GET['ameliaWcCache']);
194
195 wp_localize_script(
196 $scriptId,
197 'ameliaCache',
198 [$cacheData ? str_replace('&quot;', '\\"', json_encode($cacheData)) : '']
199 );
200 } catch (QueryExecutionException $e) {
201 }
202 }
203
204 if ($settingsService->getSetting('activation', 'stash')) {
205 /** @var StashApplicationService $stashAS */
206 $stashAS = self::$container->get('application.stash.service');
207
208 wp_localize_script(
209 $scriptId,
210 'ameliaEntities',
211 $stashAS->getStash()
212 );
213 }
214
215 do_action('amelia_scripts_loaded');
216 }
217
218 /**
219 * @param string $tag
220 * @param string $handle
221 * @param string $src
222 *
223 * @return string
224 */
225 public static function prepareScripts($tag, $handle, $src)
226 {
227 switch ($handle) {
228 case ('amelia_booking_scripts_dev_vite'):
229 case ('amelia_booking_scripts_dev_main'):
230 case ('amelia_dev_vite_client'):
231 case ('amelia_dev_main_script'):
232 case ('amelia_prod_main_script'):
233 return "<script type='module' src='{$src}'></script>";
234
235 case ('amelia_booking_script_index'):
236 $settingsService = new SettingsService(new SettingsStorage());
237
238 if ($settingsService->getSetting('activation', 'v3RelativePath')) {
239 $customUrl = $settingsService->getSetting('activation', 'customUrl');
240
241 $position = strpos($src, $customUrl['pluginPath'] . 'v3/public/assets/public.');
242
243 if ($position !== false) {
244 $src = substr($src, $position);
245 }
246 } elseif (strpos($src, 'http://') === 0) {
247 $src = substr($src, strpos(substr($src, 7), '/') + 7);
248 } elseif (strpos($src, 'https://') === 0) {
249 $src = substr($src, strpos(substr($src, 8), '/') + 8);
250 }
251
252 $asyncLoading = $settingsService->getSetting('activation', 'v3AsyncLoading') ?
253 'async' : '';
254
255 return "<script type='module' {$asyncLoading} crossorigin src='{$src}'></script>";
256
257 case ('amelia_booking_script_vendor'):
258 return "<link rel='modulepreload' href='{$src}'>";
259
260 default:
261 return $tag;
262 }
263 }
264
265 /**
266 * @param string $tag
267 * @param string $handle
268 * @param string $href
269 *
270 * @return string
271 */
272 public static function prepareStyles($tag, $handle, $href)
273 {
274 switch ($handle) {
275 case ('amelia_booking_v3_style'):
276 case ('amelia_booking_style_index'):
277 case ('amelia_booking_style_vendor'):
278 $settingsService = new SettingsService(new SettingsStorage());
279
280 if ($settingsService->getSetting('activation', 'v3RelativePath')) {
281 $customUrl = $settingsService->getSetting('activation', 'customUrl');
282
283 $position = strpos($href, $customUrl['pluginPath'] . 'v3/public/assets/style.');
284
285 if ($position !== false) {
286 $href = substr($href, $position);
287 }
288 }
289
290 return "<link rel='stylesheet' href='{$href}'>";
291
292 default:
293 return $tag;
294 }
295 }
296
297 /**
298 * Enqueue PayPal script
299 */
300 protected static function enqueuePaypalScript(SettingsService $settingsService): void
301 {
302 if ($settingsService->getSetting('payments', 'payPal')['enabled'] === true) {
303 $payPalSettings = $settingsService->getSetting('payments', 'payPal');
304 $payPalClientId = $payPalSettings['sandboxMode']
305 ? ($payPalSettings['testApiClientId'] ?? '')
306 : ($payPalSettings['liveApiClientId'] ?? '');
307 $payPalCurrency = $settingsService->getSetting('payments', 'currency') ?: 'USD';
308 wp_enqueue_script(
309 'amelia_paypal_script',
310 'https://www.paypal.com/sdk/js?client-id=' . urlencode($payPalClientId)
311 . '&currency=' . urlencode($payPalCurrency)
312 . '&components=buttons'
313 . '&disable-funding=venmo,paylater,card,sepa,bancontact,blik,eps,giropay,ideal,mercadopago,mybank,p24,sofort',
314 [],
315 null,
316 false
317 );
318 }
319 }
320
321 /**
322 * @param string $viewPath
323 * @param array $params
324 * @return string
325 */
326 protected static function renderView(string $viewPath, array $params): string
327 {
328 if (!empty($_GET['ameliaWcCache']) || !empty($_GET['ameliaCache'])) {
329 $params['ivy'] = '';
330 }
331
332 self::prepareScriptsAndStyles();
333
334 /** @var SettingsService $settingsService */
335 $settingsService = self::$container->get('domain.settings.service');
336
337 $html = !empty($params['ivy']) && $settingsService->isFeatureEnabled('ivy') && PluginInstaller::isPluginActive('ivyforms')
338 ? IvyFormsService::shortcode($params['ivy'])
339 : '';
340
341 if (empty($html)) {
342 $params['ivy'] = '';
343 }
344
345 ob_start();
346
347 include AMELIA_PATH . '/view/frontend/' . $viewPath;
348
349 $html .= ob_get_contents();
350
351 ob_end_clean();
352
353 return $html;
354 }
355 }
356