| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Services\Integrations\FluentCommunity; |
| 4 |
|
| 5 |
/** |
| 6 |
* Renders the Fluent Support portal inside the FluentCommunity portal |
| 7 |
* when the portal destination is set to fluent_community. |
| 8 |
*/ |
| 9 |
class FluentCommunityIntegration |
| 10 |
{ |
| 11 |
public function boot() |
| 12 |
{ |
| 13 |
if (!$this->shouldUseCommunityPortal()) { |
| 14 |
return; |
| 15 |
} |
| 16 |
|
| 17 |
// Tell FC 'support' is a known route so it doesn't redirect to all_feeds. |
| 18 |
add_filter('fluent_community/app_route_paths', [$this, 'registerSupportRoute']); |
| 19 |
|
| 20 |
add_action('fluent_community/rendering_path_ssr_support', [$this, 'renderSupportPortal']); |
| 21 |
|
| 22 |
// FC's Vue router has no /support route and would fall through to NotFound. |
| 23 |
// Turn SPA navigation to /support into a full page load so the SSR hook runs. |
| 24 |
// Needed on every portal page, not only the support route. |
| 25 |
add_action('fluent_community/portal_footer', [$this, 'outputRouterGuard'], 999); |
| 26 |
} |
| 27 |
|
| 28 |
public function registerSupportRoute($paths) |
| 29 |
{ |
| 30 |
$paths[] = 'support'; |
| 31 |
return $paths; |
| 32 |
} |
| 33 |
|
| 34 |
public function renderSupportPortal($parts = []) |
| 35 |
{ |
| 36 |
if (!is_user_logged_in()) { |
| 37 |
wp_safe_redirect($this->getCommunityAuthUrl()); |
| 38 |
exit; |
| 39 |
} |
| 40 |
|
| 41 |
(new PortalRenderer())->render(); |
| 42 |
} |
| 43 |
|
| 44 |
public function outputRouterGuard() |
| 45 |
{ |
| 46 |
$supportUrl = \FluentCommunity\App\Services\Helper::baseUrl('support/'); |
| 47 |
?> |
| 48 |
<script> |
| 49 |
(function () { |
| 50 |
var supportUrl = <?php echo wp_json_encode(esc_url($supportUrl)); ?>; |
| 51 |
|
| 52 |
function addRouterGuard() { |
| 53 |
var router = window.fluentFrameworkAppRouter; |
| 54 |
if (!router || typeof router.beforeEach !== 'function') { |
| 55 |
return; |
| 56 |
} |
| 57 |
router.beforeEach(function (to, from, next) { |
| 58 |
if (to.path === '/support' || to.path.indexOf('/support/') === 0) { |
| 59 |
window.location.href = supportUrl; |
| 60 |
return false; |
| 61 |
} |
| 62 |
next(); |
| 63 |
}); |
| 64 |
} |
| 65 |
|
| 66 |
if (document.readyState === 'loading') { |
| 67 |
document.addEventListener('DOMContentLoaded', addRouterGuard); |
| 68 |
} else { |
| 69 |
addRouterGuard(); |
| 70 |
} |
| 71 |
})(); |
| 72 |
</script> |
| 73 |
<?php |
| 74 |
} |
| 75 |
|
| 76 |
private function getCommunityAuthUrl() |
| 77 |
{ |
| 78 |
$supportUrl = \FluentCommunity\App\Services\Helper::baseUrl('support/'); |
| 79 |
$authUrl = \FluentCommunity\App\Services\Helper::getAuthUrl(); |
| 80 |
|
| 81 |
if (!$authUrl) { |
| 82 |
$authUrl = add_query_arg('fcom_action', 'auth', \FluentCommunity\App\Services\Helper::baseUrl('')); |
| 83 |
} |
| 84 |
|
| 85 |
return add_query_arg('redirect_to', $supportUrl, $authUrl); |
| 86 |
} |
| 87 |
|
| 88 |
private function shouldUseCommunityPortal() |
| 89 |
{ |
| 90 |
return \FluentSupport\App\Services\Helper::getBusinessSettings('ticket_link_portal') === 'fluent_community'; |
| 91 |
} |
| 92 |
} |
| 93 |
|