| 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 |
$invalidPermissionMessage = __('You don\'t have permission to access customer support portal', 'fluent-support'); |
| 20 |
|
| 21 |
$person = Helper::getCurrentPerson(); |
| 22 |
if ($person && $person->status === 'inactive') { |
| 23 |
return '<div id="fluent_support_client_app" style="text-align: center;"><h3 class="fs_customer_restriction">' . $invalidPermissionMessage . '</h3></div>'; |
| 24 |
} else if (PermissionManager::currentUserPermissions()) { |
| 25 |
$adminPortalUrl = Helper::getPortalAdminBaseUrl(); |
| 26 |
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>'; |
| 27 |
} else if ($this->hasCustomerPortalAccess()) { |
| 28 |
|
| 29 |
$canAccess = apply_filters('fluent_support/user_portal_access_config', [ |
| 30 |
'status' => true, |
| 31 |
'message' => $invalidPermissionMessage |
| 32 |
]); |
| 33 |
|
| 34 |
if (empty($canAccess['status'])) { |
| 35 |
$invalidPermissionMessage = Arr::get($canAccess, 'message', $invalidPermissionMessage); |
| 36 |
return '<div id="fluent_support_client_app" style="text-align: center;"><h3 class="fs_customer_restriction">' . $invalidPermissionMessage . '</h3></div>'; |
| 37 |
} |
| 38 |
|
| 39 |
if (!$person) { |
| 40 |
$this->maybeCreateCustomer(); |
| 41 |
} |
| 42 |
|
| 43 |
$this->enqueueScripts(); |
| 44 |
|
| 45 |
return '<div id="fluent_support_client_app"><h3 class="fs_loading_text">' . __('Loading Customer Portal. Please wait...', 'fluent-support') . '</h3></div>'; |
| 46 |
} else { |
| 47 |
|
| 48 |
$businessSettings = Helper::getBusinessSettings(); |
| 49 |
$loggedInMessage = Arr::get($businessSettings, 'login_message', ''); |
| 50 |
|
| 51 |
$loggedInMessage = str_replace('[fluent_support_portal]', '', $loggedInMessage); |
| 52 |
|
| 53 |
return do_shortcode($loggedInMessage); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
public function enqueueScripts() |
| 58 |
{ |
| 59 |
$app = App::getInstance(); |
| 60 |
|
| 61 |
$ns = $app->config->get('app.rest_namespace'); |
| 62 |
$v = $app->config->get('app.rest_version'); |
| 63 |
$slug = $app->config->get('app.slug'); |
| 64 |
|
| 65 |
$restInfo = [ |
| 66 |
'base_url' => esc_url_raw(rest_url()), |
| 67 |
'url' => rest_url($ns . '/' . $v . '/customer-portal'), |
| 68 |
'nonce' => wp_create_nonce('wp_rest'), |
| 69 |
'namespace' => $ns, |
| 70 |
'version' => $v, |
| 71 |
]; |
| 72 |
|
| 73 |
$assets = $app['url.assets']; |
| 74 |
|
| 75 |
wp_enqueue_script('fs_tk_customer_portal', $assets . 'portal/js/app.js', ['jquery'], FLUENT_SUPPORT_VERSION); |
| 76 |
wp_enqueue_style('fs_tk_customer_portal', $assets . 'portal/css/app.css', [], FLUENT_SUPPORT_VERSION); |
| 77 |
|
| 78 |
$i18ns = [ |
| 79 |
'Conversation' => __('Conversation', 'fluent-support'), |
| 80 |
'Click Here to Write a reply' => __('Click Here to Write a reply', 'fluent-support'), |
| 81 |
'All' => __('All', 'fluent-support'), |
| 82 |
'Open' => __('Open', 'fluent-support'), |
| 83 |
'Closed' => __('Closed', 'fluent-support'), |
| 84 |
'Date' => __('Date', 'fluent-support'), |
| 85 |
'Status' => __('Status', 'fluent-support'), |
| 86 |
'Next' => __('Next', 'fluent-support'), |
| 87 |
'Prev' => __('Prev', 'fluent-support'), |
| 88 |
'agent_and_officials_can_see' => __('Only you and official support agents can view this conversation', 'fluent-support'), |
| 89 |
'reopen_ticket_instruction' => __('If you still have related issues. Please reopen this ticket and reply', 'fluent-support'), |
| 90 |
'ticket_closed' => __('This ticket has been closed at', 'fluent-support'), |
| 91 |
'Close Ticket' => __('Close Ticket', 'fluent-support'), |
| 92 |
'replied' => __('replied', 'fluent-support'), |
| 93 |
'conversation_started' => __('started the conversation', 'fluent-support'), |
| 94 |
'Click to upload' => __('Click to upload', 'fluent-support'), |
| 95 |
'This ticket is' => __('This ticket is', 'fluent-support'), |
| 96 |
'Private' => __('Private', 'fluent-support'), |
| 97 |
'Write a reply' => __('Write a reply', 'fluent-support'), |
| 98 |
'Reply' => __('Reply', 'fluent-support'), |
| 99 |
'You' => __('You', 'fluent-support'), |
| 100 |
'no_open_support_tickets' => __('Looks like you did not open any support tickets yet', 'fluent-support'), |
| 101 |
'no_support_ticket_found' => __('No support tickets found for this filter', 'fluent-support'), |
| 102 |
'Delete Customer' => __('Delete Customer', 'fluent-support'), |
| 103 |
'Update Customer' => __('Update Customer', 'fluent-support'), |
| 104 |
'Create Customer' => __('Create Customer', 'fluent-support'), |
| 105 |
|
| 106 |
'subject_placeholder' => __('What\'s this support ticket about', 'fluent-support'), |
| 107 |
'service_placeholder' => __('Select related Product/Service', 'fluent-support'), |
| 108 |
'suggestion_loading' => __('Looking for similar articles...', 'fluent-support'), |
| 109 |
'articles_heading' => __('Suggested articles', 'fluent-support'), |
| 110 |
'priority_placeholder' => __('Select Priority', 'fluent-support'), |
| 111 |
|
| 112 |
'subject' => __('Subject', 'fluent-support-pro'), |
| 113 |
'ticket_details' => __('Ticket Details', 'fluent-support-pro'), |
| 114 |
'details_help' => __('Please provide details about your problem', 'fluent-support-pro'), |
| 115 |
'product_services' => __('Related Product/Service', 'fluent-support-pro'), |
| 116 |
'priority' => __('Priority', 'fluent-support-pro'), |
| 117 |
'btn_text' => __('Create Ticket', 'fluent-support-pro'), |
| 118 |
'submit_heading' => __('Submit a Support Ticket', 'fluent-support-pro'), |
| 119 |
'create_ticket_cta' => __('Create a New Ticket', 'fluent-support') |
| 120 |
]; |
| 121 |
|
| 122 |
$i18ns['allowed_files_and_size'] = Helper::getFileUploadMessage(); |
| 123 |
|
| 124 |
$data = [ |
| 125 |
'rest' => $restInfo, |
| 126 |
'nonce' => wp_create_nonce($slug), |
| 127 |
'support_products' => Product::select(['id', 'title'])->get(), |
| 128 |
'customer_ticket_priorities' => Helper::customerTicketPriorities(), |
| 129 |
'view_tickets_url' => '#/', |
| 130 |
'i18n' => $i18ns, |
| 131 |
'fallback_image' => $assets . 'images/file.png', |
| 132 |
'has_file_upload' => !!Helper::ticketAcceptedFileMiles(), |
| 133 |
'has_rich_text_editor' => true |
| 134 |
]; |
| 135 |
|
| 136 |
if ($this->isSignedTicketView()) { |
| 137 |
$data['intended_ticket_hash'] = sanitize_text_field($_REQUEST['support_hash']); |
| 138 |
$data['view_tickets_url'] = Helper::getPortalBaseUrl() . '/#'; |
| 139 |
} else { |
| 140 |
add_filter('user_can_richedit', '__return_true'); |
| 141 |
} |
| 142 |
|
| 143 |
$data = apply_filters('fluent_support/customer_portal_vars', $data); |
| 144 |
|
| 145 |
if (!empty($data['has_rich_text_editor'])) { |
| 146 |
wp_tinymce_inline_scripts(); |
| 147 |
wp_enqueue_editor(); |
| 148 |
} |
| 149 |
|
| 150 |
wp_localize_script('fs_tk_customer_portal', 'fs_customer_portal', $data); |
| 151 |
} |
| 152 |
|
| 153 |
public function hasCustomerPortalAccess() |
| 154 |
{ |
| 155 |
$userId = get_current_user_id(); |
| 156 |
|
| 157 |
if ($userId) { |
| 158 |
return true; |
| 159 |
} |
| 160 |
|
| 161 |
return $this->isSignedTicketView(); |
| 162 |
} |
| 163 |
|
| 164 |
protected static function customerStatus() |
| 165 |
{ |
| 166 |
$user = get_current_user_id(); |
| 167 |
return Customer::where('user_id', $user)->select(['status'])->first(); |
| 168 |
} |
| 169 |
|
| 170 |
protected function isSignedTicketView() |
| 171 |
{ |
| 172 |
if (!Helper::isPublicSignedTicketEnabled()) { |
| 173 |
return false; |
| 174 |
} |
| 175 |
|
| 176 |
return isset($_REQUEST['fs_view']) && $_REQUEST['fs_view'] == 'ticket' && isset($_REQUEST['support_hash']) && isset($_REQUEST['ticket_id']); |
| 177 |
} |
| 178 |
|
| 179 |
private function maybeCreateCustomer() |
| 180 |
{ |
| 181 |
$userId = get_current_user_id(); |
| 182 |
if (!$userId) { |
| 183 |
return false; |
| 184 |
} |
| 185 |
|
| 186 |
$person = Helper::getCurrentPerson(); |
| 187 |
if ($person) { |
| 188 |
return $person; |
| 189 |
} |
| 190 |
|
| 191 |
$user = get_user_by('ID', $userId); |
| 192 |
|
| 193 |
$request = App::request(); |
| 194 |
|
| 195 |
$onBehalf = [ |
| 196 |
'user_id' => $user->id, |
| 197 |
'email' => $user->user_email, |
| 198 |
'last_ip_address' => $request->getIp() |
| 199 |
]; |
| 200 |
|
| 201 |
return Customer::maybeCreateCustomer($onBehalf); |
| 202 |
} |
| 203 |
} |
| 204 |
|