| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use FluentSupport\App\App; |
| 6 |
use FluentSupport\App\Models\Agent; |
| 7 |
use FluentSupport\App\Models\Customer; |
| 8 |
use FluentSupport\App\Models\Product; |
| 9 |
use FluentSupport\App\Modules\PermissionManager; |
| 10 |
use FluentSupport\App\Services\EmailNotification\Settings; |
| 11 |
use FluentSupport\App\Services\Helper; |
| 12 |
use FluentSupport\App\Services\TransStrings; |
| 13 |
use FluentSupport\Framework\Support\Arr; |
| 14 |
|
| 15 |
class CustomerPortalHandler |
| 16 |
{ |
| 17 |
public function renderPortal() |
| 18 |
{ |
| 19 |
$person = Helper::getCurrentPerson(); |
| 20 |
if ($person && $person->status === 'inactive') { |
| 21 |
return '<div id="fluent_support_client_app" style="text-align: center;"><h3 class="fs_customer_restriction">' . __('You don’t have permission to view the tickets', 'fluent-support') . '</h3></div>'; |
| 22 |
} else if (PermissionManager::currentUserPermissions()) { |
| 23 |
$adminPortalUrl = Helper::getPortalAdminBaseUrl(); |
| 24 |
return '<div style="text-align: center;"><h3>' . __('Customer Portal is only accessible by Customers. Looks like you are a support staff', 'fluent-support') . '</h3><a href="' . $adminPortalUrl . '">' . __('Go to Support Admin Page', 'fluent-support') . '</a></div>'; |
| 25 |
} else if ($this->hasCustomerPortalAccess()) { |
| 26 |
$this->enqueueScripts(); |
| 27 |
if (!$person) { |
| 28 |
$this->maybeCreateCustomer(); |
| 29 |
} |
| 30 |
return '<div id="fluent_support_client_app"><h3 class="fs_loading_text">' . __('Loading Customer Portal. Please wait...', 'fluent-support') . '</h3></div>'; |
| 31 |
} else { |
| 32 |
$businessSettings = Helper::getBusinessSettings(); |
| 33 |
return do_shortcode(Arr::get($businessSettings, 'login_message', '')); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
public function enqueueScripts() |
| 38 |
{ |
| 39 |
$app = App::getInstance(); |
| 40 |
|
| 41 |
$ns = $app->config->get('app.rest_namespace'); |
| 42 |
$v = $app->config->get('app.rest_version'); |
| 43 |
$slug = $app->config->get('app.slug'); |
| 44 |
|
| 45 |
$restInfo = [ |
| 46 |
'base_url' => esc_url_raw(rest_url()), |
| 47 |
'url' => rest_url($ns . '/' . $v . '/customer-portal'), |
| 48 |
'nonce' => wp_create_nonce('wp_rest'), |
| 49 |
'namespace' => $ns, |
| 50 |
'version' => $v, |
| 51 |
]; |
| 52 |
|
| 53 |
$assets = $app['url.assets']; |
| 54 |
|
| 55 |
wp_enqueue_script('fs_tk_customer_portal', $assets . 'portal/js/app.js', ['jquery'], FLUENT_SUPPORT_VERSION); |
| 56 |
wp_enqueue_style('fs_tk_customer_portal', $assets . 'portal/css/app.css', [], FLUENT_SUPPORT_VERSION); |
| 57 |
|
| 58 |
$i18ns = [ |
| 59 |
'Conversation' => __('Conversation', 'fluent-support'), |
| 60 |
'Click Here to Write a reply' => __('Click Here to Write a reply', 'fluent-support'), |
| 61 |
'All' => __('All', 'fluent-support'), |
| 62 |
'Open' => __('Open', 'fluent-support'), |
| 63 |
'Closed' => __('Closed', 'fluent-support'), |
| 64 |
'Date' => __('Date', 'fluent-support'), |
| 65 |
'Status' => __('Status', 'fluent-support'), |
| 66 |
'Next' => __('Next', 'fluent-support'), |
| 67 |
'Prev' => __('Prev', 'fluent-support'), |
| 68 |
'agent_and_officials_can_see' => __('Only you and official support agents can view this conversation', 'fluent-support'), |
| 69 |
'reopen_ticket_instruction' => __('If you still have related issues. Please reopen this ticket and reply', 'fluent-support'), |
| 70 |
'ticket_closed' => __('This ticket has been closed at', 'fluent-support'), |
| 71 |
|
| 72 |
'subject_placeholder' => __('What\'s about this support ticket', 'fluent-support'), |
| 73 |
'service_placeholder' => __('Select related Product/Service', 'fluent-support'), |
| 74 |
'suggestion_loading' => __('Looking for similar articles...', 'fluent-support'), |
| 75 |
'articles_heading' => __('Suggested articles', 'fluent-support'), |
| 76 |
'priority_placeholder' => __('Select Priority', 'fluent-support'), |
| 77 |
|
| 78 |
'subject' => __('Subject', 'fluent-support-pro'), |
| 79 |
'ticket_details' => __('Ticket Details', 'fluent-support-pro'), |
| 80 |
'details_help' => __('Please provide details about your problem', 'fluent-support-pro'), |
| 81 |
'product_services' => __('Related Product/Service', 'fluent-support-pro'), |
| 82 |
'priority' => __('Priority', 'fluent-support-pro'), |
| 83 |
'btn_text' => __('Create Ticket', 'fluent-support-pro'), |
| 84 |
'submit_heading' => __('Submit a Support Ticket', 'fluent-support-pro'), |
| 85 |
'create_ticket_cta' => __('Create a New Ticket', 'fluent-support') |
| 86 |
]; |
| 87 |
|
| 88 |
$i18ns['allowed_files_and_size'] = Helper::getFileUploadMessage(); |
| 89 |
|
| 90 |
$data = [ |
| 91 |
'rest' => $restInfo, |
| 92 |
'nonce' => wp_create_nonce($slug), |
| 93 |
'support_products' => Product::select(['id', 'title'])->get(), |
| 94 |
'customer_ticket_priorities' => Helper::customerTicketPriorities(), |
| 95 |
'view_tickets_url' => '#/', |
| 96 |
'i18n' => $i18ns, |
| 97 |
'fallback_image' => $assets . 'images/file.png', |
| 98 |
'has_file_upload' => !!Helper::ticketAcceptedFileMiles(), |
| 99 |
'has_rich_text_editor' => true |
| 100 |
]; |
| 101 |
|
| 102 |
if ($this->isSignedTicketView()) { |
| 103 |
$data['intended_ticket_hash'] = sanitize_text_field($_REQUEST['support_hash']); |
| 104 |
$data['view_tickets_url'] = Helper::getPortalBaseUrl() . '/#'; |
| 105 |
} else { |
| 106 |
add_filter('user_can_richedit', '__return_true'); |
| 107 |
} |
| 108 |
|
| 109 |
$data = apply_filters('fluent_support/customer_portal_vars', $data); |
| 110 |
|
| 111 |
if (!empty($data['has_rich_text_editor'])) { |
| 112 |
wp_tinymce_inline_scripts(); |
| 113 |
wp_enqueue_editor(); |
| 114 |
} |
| 115 |
|
| 116 |
wp_localize_script('fs_tk_customer_portal', 'fs_customer_portal', $data); |
| 117 |
} |
| 118 |
|
| 119 |
public function hasCustomerPortalAccess() |
| 120 |
{ |
| 121 |
$userId = get_current_user_id(); |
| 122 |
|
| 123 |
if ($userId) { |
| 124 |
return true; |
| 125 |
} |
| 126 |
|
| 127 |
return $this->isSignedTicketView(); |
| 128 |
} |
| 129 |
|
| 130 |
protected static function customerStatus() |
| 131 |
{ |
| 132 |
$user = get_current_user_id(); |
| 133 |
return Customer::where('user_id', $user)->select(['status'])->first(); |
| 134 |
} |
| 135 |
|
| 136 |
protected function isSignedTicketView() |
| 137 |
{ |
| 138 |
if (!Helper::isPublicSignedTicketEnabled()) { |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
return isset($_REQUEST['fs_view']) && $_REQUEST['fs_view'] == 'ticket' && isset($_REQUEST['support_hash']) && isset($_REQUEST['ticket_id']); |
| 143 |
} |
| 144 |
|
| 145 |
private function maybeCreateCustomer() |
| 146 |
{ |
| 147 |
$userId = get_current_user_id(); |
| 148 |
if (!$userId) { |
| 149 |
return false; |
| 150 |
} |
| 151 |
$person = Helper::getCurrentPerson(); |
| 152 |
if ($person) { |
| 153 |
return true; |
| 154 |
} |
| 155 |
|
| 156 |
$user = get_user_by('ID', $userId); |
| 157 |
|
| 158 |
$request = App::request(); |
| 159 |
|
| 160 |
$onBehalf = [ |
| 161 |
'user_id' => $user->id, |
| 162 |
'email' => $user->user_email, |
| 163 |
'last_ip_address' => $request->getIp() |
| 164 |
]; |
| 165 |
|
| 166 |
Customer::maybeCreateCustomer($onBehalf); |
| 167 |
return true; |
| 168 |
} |
| 169 |
} |
| 170 |
|