| @@ -1,22 +1,110 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentSupport\App\Services\Integrations; |
| 4 | 4 | |
| 5 | +use FluentSupport\App\Services\Helper; | |
| 5 | 6 | |
| 6 | -use FluentSupport\App\Modules\IntegrationSettingsModule; | |
| 7 | -use FluentSupport\App\Services\Integrations\Slack\SlackNotification; | |
| 8 | - | |
| 9 | 7 | class IntegrationInit |
| 10 | 8 | { |
| 9 | + const SUPPORT_ENDPOINT = 'support-tickets'; | |
| 10 | + | |
| 11 | 11 | public function init() |
| 12 | 12 | { |
| 13 | - if(defined('FLUENTCRM')) { | |
| 14 | - (new FluentCRM())->boot(); | |
| 13 | + $this->registerWooCommercePortal(); | |
| 14 | + add_action('template_redirect', [$this, 'maybeRedirectPortalUrl']); | |
| 15 | + | |
| 16 | + if(defined('FLUENTCRM') && class_exists('\FluentSupport\App\Services\Integrations\FluentCrm\FluentCRMWidgets')) { | |
| 17 | + (new \FluentSupport\App\Services\Integrations\FluentCrm\FluentCRMWidgets())->boot(); | |
| 15 | 18 | } |
| 16 | 19 | |
| 17 | - if(defined('FLUENTFORM')) { | |
| 18 | - new \FluentSupport\App\Services\Integrations\FluentForm\FeedIntegration(wpFluentForm()); | |
| 20 | + if (defined('FLUENTFORM') && class_exists('\FluentSupport\App\Services\Integrations\FluentForm\FeedIntegration')) { | |
| 21 | + new \FluentSupport\App\Services\Integrations\FluentForm\FeedIntegration(); | |
| 19 | 22 | } |
| 23 | + | |
| 24 | + if (defined('FLUENTCART_VERSION') && class_exists('\FluentSupport\App\Services\Integrations\FluentCart\FluentCart')) { | |
| 25 | + (new \FluentSupport\App\Services\Integrations\FluentCart\FluentCart())->boot(); | |
| 26 | + } | |
| 27 | + | |
| 28 | + if (defined('FLUENT_BOOKING_VERSION') && class_exists('\FluentSupport\App\Services\Integrations\FluentBooking\FluentBookingService')) { | |
| 29 | + $fluentBookingService = new \FluentSupport\App\Services\Integrations\FluentBooking\FluentBookingService(); | |
| 30 | + if ($fluentBookingService->isActive()) { | |
| 31 | + $fluentBookingService->init(); | |
| 32 | + } | |
| 33 | + } | |
| 34 | + | |
| 35 | + if (defined('FLUENT_COMMUNITY_PLUGIN_VERSION') && class_exists('\FluentSupport\App\Services\Integrations\FluentCommunity\FluentCommunityIntegration')) { | |
| 36 | + (new \FluentSupport\App\Services\Integrations\FluentCommunity\FluentCommunityIntegration())->boot(); | |
| 37 | + } | |
| 38 | + } | |
| 39 | + | |
| 40 | + private function registerWooCommercePortal() | |
| 41 | + { | |
| 42 | + if (!defined('WC_PLUGIN_FILE')) { | |
| 43 | + return; | |
| 44 | + } | |
| 45 | + | |
| 46 | + add_rewrite_endpoint(self::SUPPORT_ENDPOINT, EP_ROOT | EP_PAGES); | |
| 47 | + | |
| 48 | + add_filter('woocommerce_get_query_vars', function ($queryVars) { | |
| 49 | + $queryVars[self::SUPPORT_ENDPOINT] = self::SUPPORT_ENDPOINT; | |
| 50 | + return $queryVars; | |
| 51 | + }); | |
| 52 | + | |
| 53 | + add_filter('woocommerce_account_menu_items', function ($items) { | |
| 54 | + if (Helper::getBusinessSettings('ticket_link_portal') !== 'woocommerce') { | |
| 55 | + return $items; | |
| 56 | + } | |
| 57 | + | |
| 58 | + $supportPosition = apply_filters('fluent_support/woo_menu_link_position', 3); | |
| 59 | + if (count($items) <= $supportPosition) { | |
| 60 | + $supportPosition = count($items) - 1; | |
| 61 | + } | |
| 62 | + | |
| 63 | + $supportLabel = apply_filters('fluent_support/woo_menu_label', __('Support', 'fluent-support')); | |
| 64 | + | |
| 65 | + return array_slice($items, 0, $supportPosition, true) | |
| 66 | + + [self::SUPPORT_ENDPOINT => $supportLabel] | |
| 67 | + + array_slice($items, $supportPosition, null, true); | |
| 68 | + }); | |
| 69 | + | |
| 70 | + add_action('woocommerce_account_' . self::SUPPORT_ENDPOINT . '_endpoint', function () { | |
| 71 | + if (Helper::getBusinessSettings('ticket_link_portal') === 'woocommerce') { | |
| 72 | + echo do_shortcode('[fluent_support_portal]'); | |
| 73 | + } | |
| 74 | + }); | |
| 75 | + } | |
| 76 | + | |
| 77 | + public function maybeRedirectPortalUrl() | |
| 78 | + { | |
| 79 | + $requestUri = isset($_SERVER['REQUEST_URI']) ? esc_url_raw(wp_unslash($_SERVER['REQUEST_URI'])) : ''; | |
| 80 | + if (!$requestUri) { | |
| 81 | + return; | |
| 82 | + } | |
| 83 | + | |
| 84 | + $requestPath = trim((string) wp_parse_url($requestUri, PHP_URL_PATH), '/'); | |
| 85 | + $pathSegments = explode('/', $requestPath); | |
| 86 | + $hasKnownEndpoint = in_array(self::SUPPORT_ENDPOINT, $pathSegments, true); | |
| 87 | + | |
| 88 | + if (!$hasKnownEndpoint && strpos($requestUri, 'fs_view=ticket') === false) { | |
| 89 | + return; | |
| 90 | + } | |
| 91 | + | |
| 92 | + $baseUrl = Helper::getPortalBaseUrl(); | |
| 93 | + $basePath = trim((string) wp_parse_url($baseUrl, PHP_URL_PATH), '/'); | |
| 94 | + | |
| 95 | + if ($requestPath === $basePath) { | |
| 96 | + return; | |
| 97 | + } | |
| 98 | + | |
| 99 | + $redirectUrl = $baseUrl; | |
| 100 | + $query = (string) wp_parse_url($requestUri, PHP_URL_QUERY); | |
| 101 | + | |
| 102 | + if ($query) { | |
| 103 | + $redirectUrl .= '?' . $query; | |
| 104 | + } | |
| 105 | + | |
| 106 | + wp_safe_redirect($redirectUrl, 302); | |
| 107 | + exit; | |
| 20 | 108 | } |
| 21 | 109 | |
| 22 | 110 | } |