| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use FluentSupport\App\App; |
| 6 |
use FluentSupport\App\Models\Customer; |
| 7 |
use FluentSupport\App\Models\Product; |
| 8 |
use FluentSupport\App\Modules\PermissionManager; |
| 9 |
use FluentSupport\App\Services\Blocks\BlockHelper; |
| 10 |
use FluentSupport\App\Services\Helper; |
| 11 |
use FluentSupport\App\Services\TranslationStrings; |
| 12 |
use FluentSupport\App\Vite; |
| 13 |
use FluentSupport\Framework\Support\Arr; |
| 14 |
use FluentSupportPro\App\Services\ProHelper; |
| 15 |
|
| 16 |
class CustomerPortalHandler |
| 17 |
{ |
| 18 |
public function renderPortal($args = []) |
| 19 |
{ |
| 20 |
/** |
| 21 |
* This hook filter customer portal access permission error message. |
| 22 |
* If a customer has no access to the portal, then the message will be displayed. |
| 23 |
* @param string $invalidPermissionMessage |
| 24 |
* @return string |
| 25 |
* @since 1.6.0 |
| 26 |
*/ |
| 27 |
$invalidPermissionMessage = apply_filters( |
| 28 |
'fluent_support/customer_portal_invalid_permission_message', |
| 29 |
esc_html__('You don\'t have permission to access customer support portal', 'fluent-support') |
| 30 |
); |
| 31 |
|
| 32 |
$person = Helper::getCurrentCustomer(); |
| 33 |
|
| 34 |
if (!$person && PermissionManager::currentUserPermissions()) { |
| 35 |
$adminPortalUrl = Helper::getPortalAdminBaseUrl(); |
| 36 |
|
| 37 |
/** |
| 38 |
* This hook filter is responsible for generating error message |
| 39 |
* when a support staff try to access customer portal |
| 40 |
* @param string $agentPermissionErrMessage |
| 41 |
* @return string |
| 42 |
* @since 1.6.0 |
| 43 |
*/ |
| 44 |
$msg = __('Customer Portal is only accessible by Customers. Looks like you are a support staff', 'fluent-support'); |
| 45 |
$agentPermissionErrMessage = apply_filters( |
| 46 |
'fluent_support/customer_portal_agent_permission_error_message', |
| 47 |
$msg |
| 48 |
); |
| 49 |
return '<div style="text-align: center;"><h3>' . esc_html($agentPermissionErrMessage) . '</h3><a href="' . esc_url($adminPortalUrl) . '">' . esc_html__('Go to Support Admin Page', 'fluent-support') . '</a></div>'; |
| 50 |
} else if ($this->hasCustomerPortalAccess()) { |
| 51 |
|
| 52 |
/* |
| 53 |
* Filter customer portal access settings |
| 54 |
* |
| 55 |
* @since v1.0.0 |
| 56 |
* |
| 57 |
* @param array $canAccess |
| 58 |
*/ |
| 59 |
$canAccess = apply_filters('fluent_support/user_portal_access_config', [ |
| 60 |
'status' => true, |
| 61 |
'message' => $invalidPermissionMessage |
| 62 |
]); |
| 63 |
|
| 64 |
if (empty($canAccess['status'])) { |
| 65 |
$invalidPermissionMessage = Arr::get($canAccess, 'message', $invalidPermissionMessage); |
| 66 |
return '<div id="fluent_support_client_app" style="text-align: center;"><h3 class="fs_customer_restriction">' . esc_html($invalidPermissionMessage) . '</h3></div>'; |
| 67 |
} |
| 68 |
|
| 69 |
if (!$person) { |
| 70 |
$this->maybeCreateCustomer(); |
| 71 |
} |
| 72 |
|
| 73 |
if (isset($args['attributes']) && !empty($args['attributes'])) { |
| 74 |
BlockHelper::processAttributesAndPrepareStyle($args['attributes']); |
| 75 |
} |
| 76 |
|
| 77 |
$this->enqueueScripts(); |
| 78 |
return '<div id="fluent_support_client_app"><h3 class="fs_loading_text">' . __('Loading Customer Portal. Please wait...', 'fluent-support') . '</h3></div>'; |
| 79 |
} else { |
| 80 |
|
| 81 |
$businessSettings = Helper::getBusinessSettings(); |
| 82 |
$loggedInMessage = Arr::get($businessSettings, 'login_message', ''); |
| 83 |
|
| 84 |
$loggedInMessage = str_replace('[fluent_support_portal]', '', $loggedInMessage); |
| 85 |
|
| 86 |
// Pass portal's show-signup / show-reset-password to auth/login shortcodes |
| 87 |
// by temporarily overriding defaults via the existing filter. |
| 88 |
$overrideDefaults = function ($defaults) use ($args) { |
| 89 |
$defaults['show-signup'] = Arr::get($args, 'show-signup', 'true'); |
| 90 |
$defaults['show-reset-password'] = Arr::get($args, 'show-reset-password', 'true'); |
| 91 |
return $defaults; |
| 92 |
}; |
| 93 |
|
| 94 |
$loggedInMessage = wp_kses_post($loggedInMessage); |
| 95 |
|
| 96 |
add_filter('fluent_support/auth_shortcode_defaults', $overrideDefaults); |
| 97 |
$result = do_shortcode($loggedInMessage); |
| 98 |
|
| 99 |
remove_filter('fluent_support/auth_shortcode_defaults', $overrideDefaults); |
| 100 |
|
| 101 |
return $result; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
public function hasCustomerPortalAccess() |
| 106 |
{ |
| 107 |
$userId = get_current_user_id(); |
| 108 |
|
| 109 |
if ($userId) { |
| 110 |
return true; |
| 111 |
} |
| 112 |
|
| 113 |
return $this->isSignedTicketView(); |
| 114 |
} |
| 115 |
|
| 116 |
protected function isSignedTicketView() |
| 117 |
{ |
| 118 |
if (!Helper::isPublicSignedTicketEnabled()) { |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
return isset($_REQUEST['fs_view']) && $_REQUEST['fs_view'] == 'ticket' && isset($_REQUEST['support_hash']) && isset($_REQUEST['ticket_id']); |
| 123 |
} |
| 124 |
|
| 125 |
private function maybeCreateCustomer() |
| 126 |
{ |
| 127 |
$userId = get_current_user_id(); |
| 128 |
if (!$userId) { |
| 129 |
return false; |
| 130 |
} |
| 131 |
|
| 132 |
$person = Helper::getCurrentPerson(); |
| 133 |
if ($person) { |
| 134 |
return $person; |
| 135 |
} |
| 136 |
|
| 137 |
$user = get_user_by('ID', $userId); |
| 138 |
|
| 139 |
$request = App::request(); |
| 140 |
|
| 141 |
$onBehalf = [ |
| 142 |
'user_id' => $user->ID, |
| 143 |
'email' => $user->user_email, |
| 144 |
'last_ip_address' => $request->getIp() |
| 145 |
]; |
| 146 |
|
| 147 |
$customFields = Helper::getBusinessSettings('custom_registration_form_field'); |
| 148 |
|
| 149 |
if (!empty($customFields)) { |
| 150 |
$onBehalf = $this->processCustomFields($customFields, $onBehalf); |
| 151 |
} |
| 152 |
|
| 153 |
return Customer::maybeCreateCustomer($onBehalf); |
| 154 |
} |
| 155 |
|
| 156 |
private function processCustomFields($customFields, $onBehalf) |
| 157 |
{ |
| 158 |
$userMeta = get_user_meta(get_current_user_id()); |
| 159 |
$customData = []; |
| 160 |
|
| 161 |
foreach ($customFields as $field) { |
| 162 |
if (isset($userMeta[$field])) { |
| 163 |
$customData[$field] = is_array($userMeta[$field]) ? $userMeta[$field][0] : $userMeta[$field]; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
if ($customData) { |
| 168 |
$onBehalf = array_merge($onBehalf, $customData); |
| 169 |
} |
| 170 |
|
| 171 |
return $onBehalf; |
| 172 |
} |
| 173 |
|
| 174 |
public function enqueueScripts() |
| 175 |
{ |
| 176 |
$app = App::getInstance(); |
| 177 |
|
| 178 |
$ns = $app->config->get('app.rest_namespace'); |
| 179 |
$v = $app->config->get('app.rest_version'); |
| 180 |
$slug = $app->config->get('app.slug'); |
| 181 |
|
| 182 |
$restInfo = [ |
| 183 |
'base_url' => esc_url_raw(rest_url()), |
| 184 |
'url' => rest_url($ns . '/' . $v . '/customer-portal'), |
| 185 |
'nonce' => wp_create_nonce('wp_rest'), |
| 186 |
'namespace' => $ns, |
| 187 |
'version' => $v, |
| 188 |
]; |
| 189 |
|
| 190 |
$assets = $app['url.assets']; |
| 191 |
|
| 192 |
|
| 193 |
$i18ns = TranslationStrings::getPortalStrings(); |
| 194 |
|
| 195 |
$i18ns['allowed_files_and_size'] = Helper::getFileUploadMessage(); |
| 196 |
|
| 197 |
$data = [ |
| 198 |
'rest' => $restInfo, |
| 199 |
'nonce' => wp_create_nonce($slug), |
| 200 |
'ticket_statuses' => Helper::ticketStatuses(), |
| 201 |
'support_products' => Product::select(['id', 'title'])->get(), |
| 202 |
'product_field_required' => Helper::isProductRequired(), |
| 203 |
'customer_ticket_priorities' => Helper::customerTicketPriorities(), |
| 204 |
'view_tickets_url' => '#/', |
| 205 |
'i18n' => $i18ns, |
| 206 |
'fallback_image' => $assets . 'images/icons/file.svg', |
| 207 |
'has_file_upload' => !!Helper::ticketAcceptedFileMiles(), |
| 208 |
'has_rich_text_editor' => true, |
| 209 |
'customer_status' => static::customerStatus()->status ?? static::customerStatus(), |
| 210 |
'max_file_upload' => Helper::getBusinessSettings('max_file_upload', 3), |
| 211 |
'agent_feedback_rating' => Helper::getBusinessSettings('agent_feedback_rating', 'no'), |
| 212 |
'can_view_private_ticket_number' => current_user_can('manage_options') || PermissionManager::currentUserCan([ |
| 213 |
'fst_view_tickets', |
| 214 |
'fst_manage_own_tickets', |
| 215 |
'fst_manage_unassigned_tickets', |
| 216 |
'fst_manage_other_tickets' |
| 217 |
]), |
| 218 |
]; |
| 219 |
|
| 220 |
if ($this->isSignedTicketView()) { |
| 221 |
$data['intended_ticket_hash'] = sanitize_text_field($_REQUEST['support_hash']); |
| 222 |
$data['view_tickets_url'] = Helper::getPortalBaseUrl() . '/#'; |
| 223 |
} else { |
| 224 |
add_filter('user_can_richedit', '__return_true'); |
| 225 |
} |
| 226 |
/* |
| 227 |
* Filter customer portal localize javascript data |
| 228 |
* |
| 229 |
* @since v1.0.0 |
| 230 |
* |
| 231 |
* @param array $data |
| 232 |
*/ |
| 233 |
$data = apply_filters('fluent_support/customer_portal_vars', $data); |
| 234 |
|
| 235 |
if (!empty($data['has_rich_text_editor'])) { |
| 236 |
wp_tinymce_inline_scripts(); |
| 237 |
wp_enqueue_editor(); |
| 238 |
} |
| 239 |
|
| 240 |
// Inject Vite HMR client for dev mode |
| 241 |
add_action('wp_head', function () { |
| 242 |
Vite::injectViteClient(); |
| 243 |
}, 1); |
| 244 |
|
| 245 |
wp_enqueue_script('dompurify', $assets . 'libs/purify/purify.min.js', [], '3.4.12'); |
| 246 |
wp_enqueue_script('fs_tk_customer_portal', Vite::getEnqueuePath('portal/js/app.js'), ['jquery'], FLUENT_SUPPORT_VERSION, true); |
| 247 |
|
| 248 |
if (is_rtl()) { |
| 249 |
wp_enqueue_style('fs_tk_customer_portal_rtl', $assets . 'portal/css/app-rtl.css', [], FLUENT_SUPPORT_VERSION); |
| 250 |
} else { |
| 251 |
wp_enqueue_style('fs_tk_customer_portal', Vite::getEnqueuePath('portal/css/app.css'), [], FLUENT_SUPPORT_VERSION); |
| 252 |
} |
| 253 |
|
| 254 |
wp_localize_script('fs_tk_customer_portal', 'fs_customer_portal', $data); |
| 255 |
} |
| 256 |
|
| 257 |
protected static function customerStatus() |
| 258 |
{ |
| 259 |
$user = get_current_user_id(); |
| 260 |
|
| 261 |
if (!$user && isset($_REQUEST['support_hash']) && isset($_REQUEST['ticket_id']) && isset($_REQUEST['fs_view']) && $_REQUEST['fs_view'] == 'ticket') { |
| 262 |
return true; |
| 263 |
} |
| 264 |
|
| 265 |
return Customer::where('user_id', $user)->select(['status'])->first(); |
| 266 |
} |
| 267 |
} |
| 268 |
|