SubmenuPageHandler.php
452 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Infrastructure\WP\WPMenu; |
| 4 | |
| 5 | use AmeliaBooking\Application\Services\Helper\HelperService; |
| 6 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 7 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 8 | use AmeliaBooking\Infrastructure\WP\Integrations\WooCommerce\WooCommerceService; |
| 9 | use AmeliaBooking\Infrastructure\WP\Translations\BackendStrings; |
| 10 | use AmeliaBooking\Infrastructure\Licence\Licence; |
| 11 | |
| 12 | /** |
| 13 | * Renders menu pages |
| 14 | */ |
| 15 | class SubmenuPageHandler |
| 16 | { |
| 17 | /** @var SettingsService $settingsService */ |
| 18 | private $settingsService; |
| 19 | |
| 20 | /** |
| 21 | * SubmenuPageHandler constructor. |
| 22 | * |
| 23 | * @param SettingsService $settingsService |
| 24 | */ |
| 25 | public function __construct(SettingsService $settingsService) |
| 26 | { |
| 27 | $this->settingsService = $settingsService; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Submenu page render function |
| 32 | * |
| 33 | * @param $page |
| 34 | */ |
| 35 | public function render($page) |
| 36 | { |
| 37 | if ($page !== 'wpamelia-customize-new') { |
| 38 | $this->renderOld($page); |
| 39 | } else { |
| 40 | $this->renderNew($page); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | private function renderOld($page) |
| 45 | { |
| 46 | // Enqueue Scripts |
| 47 | wp_enqueue_script( |
| 48 | 'amelia_booking_scripts', |
| 49 | AMELIA_URL . 'public/js/backend/amelia-booking.js', |
| 50 | [], |
| 51 | AMELIA_VERSION |
| 52 | ); |
| 53 | |
| 54 | if (in_array($page, ['wpamelia-locations', 'wpamelia-settings', 'wpamelia-appointments', 'wpamelia-events', 'wpamelia-dashboard', 'wpamelia-calendar', 'wpamelia-services'])) { |
| 55 | $gmapApiKey = $this->settingsService->getSetting('general', 'gMapApiKey'); |
| 56 | |
| 57 | if ($gmapApiKey) { |
| 58 | wp_enqueue_script( |
| 59 | 'google_maps_api', |
| 60 | "https://maps.googleapis.com/maps/api/js?key={$gmapApiKey}&libraries=places&loading=async&callback=Function.prototype" |
| 61 | ); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
| 66 | if ($page === 'wpamelia-customers') { |
| 67 | wp_enqueue_script( |
| 68 | 'papaparse', |
| 69 | "https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.3.1/papaparse.min.js" |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | if ($page === 'wpamelia-notifications') { |
| 75 | wp_enqueue_script('amelia_paddle', Licence::getPaddleUrl()); |
| 76 | } |
| 77 | |
| 78 | // Enqueue Styles |
| 79 | wp_enqueue_style( |
| 80 | 'amelia_booking_styles', |
| 81 | AMELIA_URL . 'public/css/backend/amelia-booking.css', |
| 82 | [], |
| 83 | AMELIA_VERSION |
| 84 | ); |
| 85 | |
| 86 | // WordPress enqueue |
| 87 | wp_enqueue_media(); |
| 88 | |
| 89 | wp_localize_script( |
| 90 | 'amelia_booking_scripts', |
| 91 | 'wpAmeliaLanguages', |
| 92 | HelperService::getLanguages() |
| 93 | ); |
| 94 | |
| 95 | $wcSettings = $this->settingsService->getSetting('payments', 'wc'); |
| 96 | |
| 97 | if ($wcSettings['enabled'] && WooCommerceService::isEnabled()) { |
| 98 | wp_localize_script( |
| 99 | 'amelia_booking_scripts', |
| 100 | 'wpAmeliaWcProducts', |
| 101 | WooCommerceService::getInitialProducts() |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | // Strings Localization |
| 106 | switch ($page) { |
| 107 | case ('wpamelia-locations'): |
| 108 | wp_localize_script( |
| 109 | 'amelia_booking_scripts', |
| 110 | 'wpAmeliaLabels', |
| 111 | array_merge( |
| 112 | BackendStrings::getEntityFormStrings(), |
| 113 | BackendStrings::getLocationStrings(), |
| 114 | BackendStrings::getCommonStrings() |
| 115 | ) |
| 116 | ); |
| 117 | |
| 118 | break; |
| 119 | case ('wpamelia-services'): |
| 120 | wp_localize_script( |
| 121 | 'amelia_booking_scripts', |
| 122 | 'wpAmeliaLabels', |
| 123 | array_merge( |
| 124 | BackendStrings::getPaymentStrings(), |
| 125 | BackendStrings::getSettingsStrings(), |
| 126 | BackendStrings::getEntityFormStrings(), |
| 127 | BackendStrings::getServiceStrings(), |
| 128 | BackendStrings::getBookableStrings(), |
| 129 | BackendStrings::getCommonStrings(), |
| 130 | BackendStrings::getAppointmentStrings(), |
| 131 | BackendStrings::getRecurringStrings(), |
| 132 | BackendStrings::getCustomerStrings(), |
| 133 | BackendStrings::getUserStrings() |
| 134 | ) |
| 135 | ); |
| 136 | |
| 137 | break; |
| 138 | case ('wpamelia-employees'): |
| 139 | wp_localize_script( |
| 140 | 'amelia_booking_scripts', |
| 141 | 'wpAmeliaLabels', |
| 142 | array_merge( |
| 143 | BackendStrings::getEntityFormStrings(), |
| 144 | BackendStrings::getUserStrings(), |
| 145 | BackendStrings::getEmployeeStrings(), |
| 146 | BackendStrings::getStripeStrings(), |
| 147 | BackendStrings::getCommonStrings(), |
| 148 | BackendStrings::getScheduleStrings() |
| 149 | ) |
| 150 | ); |
| 151 | |
| 152 | break; |
| 153 | case ('wpamelia-customers'): |
| 154 | wp_localize_script( |
| 155 | 'amelia_booking_scripts', |
| 156 | 'wpAmeliaLabels', |
| 157 | array_merge( |
| 158 | BackendStrings::getEntityFormStrings(), |
| 159 | BackendStrings::getUserStrings(), |
| 160 | BackendStrings::getCustomerStrings(), |
| 161 | BackendStrings::getCommonStrings(), |
| 162 | BackendStrings::getScheduleStrings(), |
| 163 | BackendStrings::getImportStrings() |
| 164 | ) |
| 165 | ); |
| 166 | |
| 167 | break; |
| 168 | case ('wpamelia-finance'): |
| 169 | wp_localize_script( |
| 170 | 'amelia_booking_scripts', |
| 171 | 'wpAmeliaLabels', |
| 172 | array_merge( |
| 173 | BackendStrings::getEntityFormStrings(), |
| 174 | BackendStrings::getCommonStrings(), |
| 175 | BackendStrings::getFinanceStrings(), |
| 176 | BackendStrings::getPaymentStrings(), |
| 177 | BackendStrings::getEventStrings() |
| 178 | ) |
| 179 | ); |
| 180 | |
| 181 | break; |
| 182 | case ('wpamelia-appointments'): |
| 183 | wp_localize_script( |
| 184 | 'amelia_booking_scripts', |
| 185 | 'wpAmeliaLabels', |
| 186 | array_merge( |
| 187 | BackendStrings::getNotificationsStrings(), |
| 188 | BackendStrings::getEntityFormStrings(), |
| 189 | BackendStrings::getCommonStrings(), |
| 190 | BackendStrings::getUserStrings(), |
| 191 | BackendStrings::getCustomerStrings(), |
| 192 | BackendStrings::getAppointmentStrings(), |
| 193 | BackendStrings::getPaymentStrings(), |
| 194 | BackendStrings::getRecurringStrings() |
| 195 | ) |
| 196 | ); |
| 197 | |
| 198 | break; |
| 199 | |
| 200 | case ('wpamelia-events'): |
| 201 | wp_localize_script( |
| 202 | 'amelia_booking_scripts', |
| 203 | 'wpAmeliaLabels', |
| 204 | array_merge( |
| 205 | BackendStrings::getSettingsStrings(), |
| 206 | BackendStrings::getEntityFormStrings(), |
| 207 | BackendStrings::getCommonStrings(), |
| 208 | BackendStrings::getUserStrings(), |
| 209 | BackendStrings::getCustomerStrings(), |
| 210 | BackendStrings::getAppointmentStrings(), |
| 211 | BackendStrings::getEventStrings(), |
| 212 | BackendStrings::getBookableStrings(), |
| 213 | BackendStrings::getRecurringStrings() |
| 214 | ) |
| 215 | ); |
| 216 | |
| 217 | break; |
| 218 | |
| 219 | case ('wpamelia-dashboard'): |
| 220 | wp_localize_script( |
| 221 | 'amelia_booking_scripts', |
| 222 | 'wpAmeliaLabels', |
| 223 | array_merge( |
| 224 | BackendStrings::getEntityFormStrings(), |
| 225 | BackendStrings::getCommonStrings(), |
| 226 | BackendStrings::getAppointmentStrings(), |
| 227 | BackendStrings::getUserStrings(), |
| 228 | BackendStrings::getCustomerStrings(), |
| 229 | BackendStrings::getDashboardStrings(), |
| 230 | BackendStrings::getPaymentStrings(), |
| 231 | BackendStrings::getRecurringStrings(), |
| 232 | BackendStrings::getNotificationsStrings() |
| 233 | ) |
| 234 | ); |
| 235 | |
| 236 | break; |
| 237 | case ('wpamelia-calendar'): |
| 238 | wp_localize_script( |
| 239 | 'amelia_booking_scripts', |
| 240 | 'wpAmeliaLabels', |
| 241 | array_merge( |
| 242 | BackendStrings::getEntityFormStrings(), |
| 243 | BackendStrings::getCommonStrings(), |
| 244 | BackendStrings::getAppointmentStrings(), |
| 245 | BackendStrings::getUserStrings(), |
| 246 | BackendStrings::getCustomerStrings(), |
| 247 | BackendStrings::getCalendarStrings(), |
| 248 | BackendStrings::getPaymentStrings(), |
| 249 | BackendStrings::getEventStrings(), |
| 250 | BackendStrings::getBookableStrings(), |
| 251 | BackendStrings::getRecurringStrings() |
| 252 | ) |
| 253 | ); |
| 254 | |
| 255 | break; |
| 256 | case ('wpamelia-notifications'): |
| 257 | wp_localize_script( |
| 258 | 'amelia_booking_scripts', |
| 259 | 'wpAmeliaLabels', |
| 260 | array_merge( |
| 261 | BackendStrings::getCommonStrings(), |
| 262 | BackendStrings::getPaymentStrings(), |
| 263 | BackendStrings::getNotificationsStrings() |
| 264 | ) |
| 265 | ); |
| 266 | |
| 267 | break; |
| 268 | |
| 269 | case ('wpamelia-smsnotifications'): |
| 270 | wp_localize_script( |
| 271 | 'amelia_booking_scripts', |
| 272 | 'wpAmeliaLabels', |
| 273 | array_merge( |
| 274 | BackendStrings::getCommonStrings(), |
| 275 | BackendStrings::getNotificationsStrings() |
| 276 | ) |
| 277 | ); |
| 278 | |
| 279 | break; |
| 280 | case ('wpamelia-settings'): |
| 281 | wp_localize_script( |
| 282 | 'amelia_booking_scripts', |
| 283 | 'wpAmeliaLabels', |
| 284 | array_merge( |
| 285 | BackendStrings::getEntityFormStrings(), |
| 286 | BackendStrings::getUserStrings(), |
| 287 | BackendStrings::getEmployeeStrings(), |
| 288 | BackendStrings::getStripeStrings(), |
| 289 | BackendStrings::getFinanceStrings(), |
| 290 | BackendStrings::getCommonStrings(), |
| 291 | BackendStrings::getScheduleStrings(), |
| 292 | BackendStrings::getSettingsStrings(), |
| 293 | BackendStrings::getNotificationsStrings() |
| 294 | ) |
| 295 | ); |
| 296 | |
| 297 | break; |
| 298 | case ('wpamelia-customize'): |
| 299 | wp_localize_script( |
| 300 | 'amelia_booking_scripts', |
| 301 | 'wpAmeliaLabels', |
| 302 | array_merge( |
| 303 | BackendStrings::getCommonStrings(), |
| 304 | BackendStrings::getCustomizeStrings() |
| 305 | ) |
| 306 | ); |
| 307 | |
| 308 | break; |
| 309 | case ('wpamelia-cf'): |
| 310 | wp_localize_script( |
| 311 | 'amelia_booking_scripts', |
| 312 | 'wpAmeliaLabels', |
| 313 | array_merge( |
| 314 | BackendStrings::getCommonStrings(), |
| 315 | BackendStrings::getCustomizeStrings() |
| 316 | ) |
| 317 | ); |
| 318 | |
| 319 | break; |
| 320 | case ('wpamelia-whats-new'): |
| 321 | wp_localize_script( |
| 322 | 'amelia_booking_scripts', |
| 323 | 'wpAmeliaLabels', |
| 324 | array_merge( |
| 325 | BackendStrings::getWhatsNewStrings(), |
| 326 | BackendStrings::getNotificationsStrings(), |
| 327 | BackendStrings::getRecurringStrings(), |
| 328 | BackendStrings::getCommonStrings(), |
| 329 | BackendStrings::getAppointmentStrings() |
| 330 | ) |
| 331 | ); |
| 332 | |
| 333 | break; |
| 334 | case ('wpamelia-lite-vs-premium'): |
| 335 | wp_localize_script( |
| 336 | 'amelia_booking_scripts', |
| 337 | 'wpAmeliaLabels', |
| 338 | BackendStrings::getLiteVsPremiumStrings() |
| 339 | ); |
| 340 | |
| 341 | break; |
| 342 | } |
| 343 | |
| 344 | // Settings Localization |
| 345 | wp_localize_script( |
| 346 | 'amelia_booking_scripts', |
| 347 | 'wpAmeliaSettings', |
| 348 | $this->settingsService->getFrontendSettings() |
| 349 | ); |
| 350 | |
| 351 | wp_localize_script( |
| 352 | 'amelia_booking_scripts', |
| 353 | 'localeLanguage', |
| 354 | [AMELIA_LOCALE] |
| 355 | ); |
| 356 | |
| 357 | wp_localize_script( |
| 358 | 'amelia_booking_scripts', |
| 359 | 'wpAmeliaTimeZone', |
| 360 | [DateTimeService::getTimeZone()->getName()] |
| 361 | ); |
| 362 | |
| 363 | include AMELIA_PATH . '/view/backend/view.php'; |
| 364 | } |
| 365 | |
| 366 | private function renderNew($page) |
| 367 | { |
| 368 | $scriptId = AMELIA_DEV ? 'amelia_booking_scripts_dev_vite' : 'amelia_booking_script_index'; |
| 369 | |
| 370 | if (AMELIA_DEV) { |
| 371 | wp_enqueue_script( |
| 372 | 'amelia_booking_scripts_dev_vite', |
| 373 | 'http://localhost:3000/@vite/client', |
| 374 | [], |
| 375 | null, |
| 376 | false |
| 377 | ); |
| 378 | |
| 379 | wp_enqueue_script( |
| 380 | 'amelia_booking_scripts_dev_main', |
| 381 | 'http://localhost:3000/src/assets/js/admin/admin.js', |
| 382 | [], |
| 383 | null, |
| 384 | true |
| 385 | ); |
| 386 | } else { |
| 387 | wp_enqueue_script( |
| 388 | $scriptId, |
| 389 | AMELIA_URL . 'v3/public/assets/admin.94a3c439.js', |
| 390 | [], |
| 391 | AMELIA_VERSION, |
| 392 | true |
| 393 | ); |
| 394 | } |
| 395 | |
| 396 | wp_localize_script( |
| 397 | $scriptId, |
| 398 | 'localeLanguage', |
| 399 | [AMELIA_LOCALE] |
| 400 | ); |
| 401 | |
| 402 | wp_localize_script( |
| 403 | $scriptId, |
| 404 | 'wpAmeliaLanguages', |
| 405 | HelperService::getLanguages() |
| 406 | ); |
| 407 | |
| 408 | // Settings Localization |
| 409 | wp_localize_script( |
| 410 | $scriptId, |
| 411 | 'wpAmeliaSettings', |
| 412 | $this->settingsService->getFrontendSettings() |
| 413 | ); |
| 414 | |
| 415 | // Labels |
| 416 | wp_localize_script( |
| 417 | $scriptId, |
| 418 | 'wpAmeliaLabels', |
| 419 | array_merge( |
| 420 | BackendStrings::getCommonStrings(), |
| 421 | BackendStrings::getSettingsStrings(), |
| 422 | BackendStrings::getCustomizeStrings() |
| 423 | ) |
| 424 | ); |
| 425 | |
| 426 | wp_localize_script( |
| 427 | $scriptId, |
| 428 | 'localeLanguage', |
| 429 | [AMELIA_LOCALE] |
| 430 | ); |
| 431 | |
| 432 | wp_localize_script( |
| 433 | $scriptId, |
| 434 | 'wpAmeliaTimeZone', |
| 435 | [DateTimeService::getTimeZone()->getName()] |
| 436 | ); |
| 437 | |
| 438 | wp_localize_script( |
| 439 | $scriptId, |
| 440 | 'wpAmeliaUrls', |
| 441 | [ |
| 442 | 'wpAmeliaUseUploadsAmeliaPath' => AMELIA_UPLOADS_FILES_PATH_USE, |
| 443 | 'wpAmeliaPluginURL' => AMELIA_URL, |
| 444 | 'wpAmeliaPluginAjaxURL' => AMELIA_ACTION_URL |
| 445 | ] |
| 446 | ); |
| 447 | |
| 448 | include AMELIA_PATH . '/view/backend/view-new.php'; |
| 449 | } |
| 450 | |
| 451 | } |
| 452 |