| 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\MailBox; |
| 8 |
use FluentSupport\App\Models\Product; |
| 9 |
use FluentSupport\App\Models\TicketTag; |
| 10 |
use FluentSupport\App\Modules\PermissionManager; |
| 11 |
use FluentSupport\App\Services\Helper; |
| 12 |
use FluentSupport\App\Services\TransStrings; |
| 13 |
|
| 14 |
class Menu |
| 15 |
{ |
| 16 |
public function add() |
| 17 |
{ |
| 18 |
$currentUserPermissions = PermissionManager::currentUserPermissions(); |
| 19 |
|
| 20 |
if (!$currentUserPermissions) { |
| 21 |
return; |
| 22 |
} |
| 23 |
|
| 24 |
$permission = 'fst_view_dashboard'; |
| 25 |
|
| 26 |
$isAdmin = false; |
| 27 |
|
| 28 |
if (current_user_can('manage_options')) { |
| 29 |
$permission = 'manage_options'; |
| 30 |
$isAdmin = true; |
| 31 |
} |
| 32 |
|
| 33 |
$menuPosition = 25; |
| 34 |
if (defined('FLUENTCRM')) { |
| 35 |
$menuPosition = 4; |
| 36 |
} |
| 37 |
|
| 38 |
$menuPosition = apply_filters('fluent_support/admin_menu_position', $menuPosition); |
| 39 |
|
| 40 |
add_menu_page( |
| 41 |
__('Fluent Support', 'fluent-support'), |
| 42 |
__('Fluent Support', 'fluent-support'), |
| 43 |
$permission, |
| 44 |
'fluent-support', |
| 45 |
array($this, 'renderApp'), |
| 46 |
$this->getMenuIcon(), |
| 47 |
$menuPosition |
| 48 |
); |
| 49 |
|
| 50 |
|
| 51 |
add_submenu_page( |
| 52 |
'fluent-support', |
| 53 |
__('Dashboard', 'fluent-support'), |
| 54 |
__('Dashboard', 'fluent-support'), |
| 55 |
$permission, |
| 56 |
'fluent-support', |
| 57 |
array($this, 'renderApp') |
| 58 |
); |
| 59 |
|
| 60 |
add_submenu_page( |
| 61 |
'fluent-support', |
| 62 |
__('Tickets', 'fluent-support'), |
| 63 |
__('Tickets', 'fluent-support'), |
| 64 |
($isAdmin) ? 'manage_options' : 'fst_manage_own_tickets', |
| 65 |
'fluent-support#/tickets', |
| 66 |
array($this, 'renderApp') |
| 67 |
); |
| 68 |
|
| 69 |
add_submenu_page( |
| 70 |
'fluent-support', |
| 71 |
__('Workflows', 'fluent-support'), |
| 72 |
__('Workflows', 'fluent-support'), |
| 73 |
($isAdmin) ? 'manage_options' : 'fst_manage_workflows', |
| 74 |
'fluent-support#/workflows', |
| 75 |
array($this, 'renderApp') |
| 76 |
); |
| 77 |
|
| 78 |
add_submenu_page( |
| 79 |
'fluent-support', |
| 80 |
__('Activities', 'fluent-support'), |
| 81 |
__('Activities', 'fluent-support'), |
| 82 |
($isAdmin) ? 'manage_options' : 'fst_view_activity_logs', |
| 83 |
'fluent-support#/activity-logger', |
| 84 |
array($this, 'renderApp') |
| 85 |
); |
| 86 |
|
| 87 |
add_submenu_page( |
| 88 |
'fluent-support', |
| 89 |
__('Settings', 'fluent-support'), |
| 90 |
__('Settings', 'fluent-support'), |
| 91 |
($isAdmin) ? 'manage_options' : 'fst_manage_settings', |
| 92 |
'fluent-support#/settings', |
| 93 |
array($this, 'renderApp') |
| 94 |
); |
| 95 |
|
| 96 |
add_submenu_page( |
| 97 |
'fluent-support', |
| 98 |
__('Reports', 'fluent-support'), |
| 99 |
__('Reports', 'fluent-support'), |
| 100 |
($isAdmin) ? 'manage_options' : 'fst_sensitive_data', |
| 101 |
'fluent-support#/reports', |
| 102 |
array($this, 'renderApp') |
| 103 |
); |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
public function renderApp() |
| 108 |
{ |
| 109 |
$app = App::getInstance(); |
| 110 |
|
| 111 |
$assets = $app['url.assets']; |
| 112 |
|
| 113 |
$baseUrl = apply_filters('fluent_support/base_url', admin_url('admin.php?page=fluent-support#/')); |
| 114 |
|
| 115 |
$menuItems = [ |
| 116 |
[ |
| 117 |
'key' => 'dashboard', |
| 118 |
'label' => __('Dashboard', 'fluent-support'), |
| 119 |
'permalink' => $baseUrl |
| 120 |
], |
| 121 |
[ |
| 122 |
'key' => 'tickets', |
| 123 |
'label' => __('Tickets', 'fluent-support'), |
| 124 |
'permalink' => $baseUrl . 'tickets', |
| 125 |
], |
| 126 |
[ |
| 127 |
'key' => 'reports', |
| 128 |
'label' => __('Reports', 'fluent-support'), |
| 129 |
'permalink' => $baseUrl . 'reports' |
| 130 |
] |
| 131 |
]; |
| 132 |
|
| 133 |
$hasSensitiveAccess = PermissionManager::currentUserCan('fst_sensitive_data'); |
| 134 |
if ($hasSensitiveAccess) { |
| 135 |
$menuItems[] = [ |
| 136 |
'key' => 'customers', |
| 137 |
'label' => __('Customers', 'fluent-support'), |
| 138 |
'permalink' => $baseUrl . 'customers' |
| 139 |
]; |
| 140 |
} |
| 141 |
|
| 142 |
if (PermissionManager::currentUserCan('fst_manage_saved_replies')) { |
| 143 |
$secondayItems = [ |
| 144 |
[ |
| 145 |
'key' => 'saved_replies', |
| 146 |
'label' => __('Saved Replies', 'fluent-support'), |
| 147 |
'permalink' => $baseUrl . 'saved-replies' |
| 148 |
] |
| 149 |
]; |
| 150 |
} |
| 151 |
|
| 152 |
if (PermissionManager::currentUserCan('fst_view_activity_logs')) { |
| 153 |
$secondayItems[] = [ |
| 154 |
'key' => 'activity_logger', |
| 155 |
'label' => __('Activities', 'fluent-support'), |
| 156 |
'permalink' => $baseUrl . 'activity-logger' |
| 157 |
]; |
| 158 |
} |
| 159 |
|
| 160 |
$canManageSettings = PermissionManager::currentUserCan('fst_manage_settings'); |
| 161 |
|
| 162 |
if ($canManageSettings) { |
| 163 |
$secondayItems[] = [ |
| 164 |
'key' => 'mailboxes', |
| 165 |
'label' => __('Business Settings', 'fluent-support'), |
| 166 |
'permalink' => $baseUrl . 'mailboxes' |
| 167 |
]; |
| 168 |
} |
| 169 |
|
| 170 |
if (PermissionManager::currentUserCan('fst_manage_workflows')) { |
| 171 |
$secondayItems[] = [ |
| 172 |
'key' => 'workflows', |
| 173 |
'label' => __('Workflows', 'fluent-support'), |
| 174 |
'permalink' => $baseUrl . 'workflows' |
| 175 |
]; |
| 176 |
} |
| 177 |
|
| 178 |
if ($canManageSettings) { |
| 179 |
$secondayItems[] = [ |
| 180 |
'key' => 'settings', |
| 181 |
'label' => __('Global Settings', 'fluent-support'), |
| 182 |
'permalink' => $baseUrl . 'settings' |
| 183 |
]; |
| 184 |
} |
| 185 |
|
| 186 |
$menuItems = apply_filters('fluent_support/primary_menu_items', $menuItems); |
| 187 |
$secondayItems = apply_filters('fluent_support/secondary_menu_items', $secondayItems); |
| 188 |
|
| 189 |
$app = App::getInstance(); |
| 190 |
$this->enqueueAssets(); |
| 191 |
$app->view->render('admin.menu', [ |
| 192 |
'base_url' => $baseUrl, |
| 193 |
'logo' => $assets . 'images/logo.svg', |
| 194 |
'menuItems' => $menuItems, |
| 195 |
'secondaryItems' => $secondayItems |
| 196 |
]); |
| 197 |
} |
| 198 |
|
| 199 |
public function maybeEnqueueAssets() |
| 200 |
{ |
| 201 |
if (isset($_GET['page']) && $_GET['page'] == 'fluent-support') { |
| 202 |
$this->enqueueAssets(); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
public function enqueueAssets() |
| 207 |
{ |
| 208 |
$app = App::getInstance(); |
| 209 |
|
| 210 |
$assets = $app['url.assets']; |
| 211 |
|
| 212 |
wp_enqueue_style( |
| 213 |
'fluent_support_admin_app', $assets . 'admin/css/alpha-admin.css', [], FLUENT_SUPPORT_VERSION |
| 214 |
); |
| 215 |
|
| 216 |
$agents = Agent::select(['id', 'first_name', 'last_name']) |
| 217 |
->where('person_type', 'agent') |
| 218 |
->get()->toArray(); |
| 219 |
|
| 220 |
foreach ($agents as $index => $agent) { |
| 221 |
$agents[$index]['id'] = strval($agent['id']); |
| 222 |
} |
| 223 |
|
| 224 |
$me = Helper::getAgentByUserId(get_current_user_id()); |
| 225 |
|
| 226 |
if (!$me && current_user_can('manage_options')) { |
| 227 |
// we should create the agent |
| 228 |
$user = wp_get_current_user(); |
| 229 |
$me = Agent::create([ |
| 230 |
'email' => $user->user_email, |
| 231 |
'first_name' => $user->first_name, |
| 232 |
'last_name' => $user->last_name, |
| 233 |
'user_id' => $user->ID |
| 234 |
]); |
| 235 |
} |
| 236 |
|
| 237 |
$me->permissions = PermissionManager::currentUserPermissions(); |
| 238 |
|
| 239 |
do_action('fluent_support_loading_app', $app); |
| 240 |
|
| 241 |
// Editor default styles. |
| 242 |
add_filter('user_can_richedit', '__return_true'); |
| 243 |
wp_tinymce_inline_scripts(); |
| 244 |
wp_enqueue_editor(); |
| 245 |
wp_enqueue_media(); |
| 246 |
|
| 247 |
wp_enqueue_script( |
| 248 |
'fluent_support_admin_app_start', |
| 249 |
$assets . 'admin/js/start.js', |
| 250 |
array('jquery'), |
| 251 |
FLUENT_SUPPORT_VERSION, |
| 252 |
true |
| 253 |
); |
| 254 |
|
| 255 |
wp_enqueue_script( |
| 256 |
'fluent_support_global_admin', |
| 257 |
$assets . 'admin/js/global_admin.js', |
| 258 |
array('jquery'), |
| 259 |
FLUENT_SUPPORT_VERSION, |
| 260 |
true |
| 261 |
); |
| 262 |
|
| 263 |
$integrationDrivers = []; |
| 264 |
if (!defined('FLUENTSUPPORTPRO')) { |
| 265 |
$integrationDrivers = [ |
| 266 |
[ |
| 267 |
'key' => 'telegram_settings', |
| 268 |
'title' => __('Telegram', 'fluent-support'), |
| 269 |
'description' => __('Send Telegram notifications to Group, Channel or individual person inbox and reply from Telegram inbox', 'fluent-support'), |
| 270 |
'promo_heading' => __('Get activity notification to Telegram Messenger and reply directly from Telegram inbox', 'fluent-support'), |
| 271 |
'require_pro' => true |
| 272 |
], |
| 273 |
[ |
| 274 |
'key' => 'slack_settings', |
| 275 |
'title' => __('Slack', 'fluent-support'), |
| 276 |
'description' => __('Send ticket activity notifications to slack', 'fluent-support'), |
| 277 |
'promo_heading' => __('Get activity notification to Slack Channel and keep your support team super engaged', 'fluent-support'), |
| 278 |
'require_pro' => true |
| 279 |
] |
| 280 |
]; |
| 281 |
} |
| 282 |
|
| 283 |
$integrationDrivers = apply_filters('fluent_support/integration_drivers', $integrationDrivers); |
| 284 |
|
| 285 |
$tags = TicketTag::select(['id', 'title'])->get()->toArray(); |
| 286 |
|
| 287 |
$tags = array_map(function ($tag) { |
| 288 |
$tag['id'] = strval($tag['id']); |
| 289 |
return $tag; |
| 290 |
}, $tags); |
| 291 |
|
| 292 |
$i18ns = TransStrings::getTransStrings(); |
| 293 |
$i18ns['allowed_files_and_size'] = Helper::getFileUploadMessage(); |
| 294 |
|
| 295 |
$appVars = apply_filters('fluent_support_app_vars', array( |
| 296 |
'slug' => $slug = $app->config->get('app.slug'), |
| 297 |
'nonce' => wp_create_nonce($slug), |
| 298 |
'rest' => $this->getRestInfo($app), |
| 299 |
'brand_logo' => $this->getMenuIcon(), |
| 300 |
'firstEntry' => '', |
| 301 |
'lastEntry' => '', |
| 302 |
'asset_url' => $assets, |
| 303 |
'support_agents' => $agents, |
| 304 |
'support_products' => Product::select(['id', 'title'])->get(), |
| 305 |
'client_priorities' => Helper::customerTicketPriorities(), |
| 306 |
'admin_priorities' => Helper::adminTicketPriorities(), |
| 307 |
'mailboxes' => MailBox::select(['id', 'name', 'settings'])->get(), |
| 308 |
'me' => $me, |
| 309 |
'pref' => [ |
| 310 |
'go_back_after_reply' => 'yes' |
| 311 |
], |
| 312 |
'notification_integrations' => $integrationDrivers, |
| 313 |
'server_time' => current_time('mysql'), |
| 314 |
'has_email_parser' => defined('FLUENTSUPPORTPRO_PLUGIN_VERSION'), |
| 315 |
'ticket_tags' => $tags, |
| 316 |
'i18n' => $i18ns, |
| 317 |
'custom_fields' => apply_filters('fluent_support/ticket_custom_fields', []), |
| 318 |
'has_file_upload' => !!Helper::ticketAcceptedFileMiles() |
| 319 |
)); |
| 320 |
|
| 321 |
if(defined('FLUENTCRM')) { |
| 322 |
$appVars['fluentcrm_config'] = Helper::getFluentCRMTagConfig(); |
| 323 |
} |
| 324 |
|
| 325 |
$appVars['has_pro'] = defined('FLUENTSUPPORTPRO_PLUGIN_VERSION'); |
| 326 |
wp_localize_script('fluent_support_admin_app_start', 'fluentSupportAdmin', $appVars); |
| 327 |
do_action('fluent_support/admin_app_loaded', $app); |
| 328 |
|
| 329 |
} |
| 330 |
|
| 331 |
protected function getRestInfo($app) |
| 332 |
{ |
| 333 |
$ns = $app->config->get('app.rest_namespace'); |
| 334 |
$v = $app->config->get('app.rest_version'); |
| 335 |
|
| 336 |
return [ |
| 337 |
'base_url' => esc_url_raw(rest_url()), |
| 338 |
'url' => rest_url($ns . '/' . $v), |
| 339 |
'nonce' => wp_create_nonce('wp_rest'), |
| 340 |
'namespace' => $ns, |
| 341 |
'version' => $v, |
| 342 |
]; |
| 343 |
} |
| 344 |
|
| 345 |
protected function getMenuIcon() |
| 346 |
{ |
| 347 |
$app = App::getInstance(); |
| 348 |
|
| 349 |
$assets = $app['path.assets']; |
| 350 |
|
| 351 |
return 'data:image/svg+xml;base64,' . base64_encode( |
| 352 |
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1200 1200"><defs><style>.cls-1{fill:#fff;fill-rule:evenodd;}</style></defs><path class="cls-1" d="M1154.68,162.74v328l-.06-.05c-28.42-68.67-94.1-118-171.88-122.75a193.27,193.27,0,0,0-64.45,6.17L137.57,583.44A70.3,70.3,0,0,1,122.08,586a62.79,62.79,0,0,1-10.77.94A66.34,66.34,0,0,1,45,521.43a73.54,73.54,0,0,1-.61-9.28v-111c0-.16,0-.33,0-.5s0-.27,0-.39c0-1.27.16-2.49.22-3.77s.05-2.39.17-3.55c.16-2.06.44-4.06.72-6.06.11-.55.16-1.16.27-1.72.34-1.94.73-3.83,1.23-5.71.16-.84.33-1.56.55-2.34.45-1.55.89-3.05,1.33-4.55s1.06-3,1.61-4.55c.28-.61.5-1.28.78-1.94a92.91,92.91,0,0,1,62.12-54.85L1028.49,66.08a100.17,100.17,0,0,1,18.82-3.44,94.46,94.46,0,0,1,14.21-1.89c1.39,0,2.72-.11,4.11-.11a88.59,88.59,0,0,1,88.55,88.61c0,.55-.06,1.16-.06,1.77A102.6,102.6,0,0,1,1154.68,162.74Z"/><path class="cls-1" d="M943.32,498.86c-2.07,0-4.08.05-6.09.15-2.12-.1-4.29-.15-6.46-.15a139.56,139.56,0,0,0-35.94,4.71l-121.22,32.5L118.06,711.86A86,86,0,0,0,89.43,722c-.38.16-.69.37-1,.53a88.53,88.53,0,0,0-44,76.59v335.42a3.68,3.68,0,0,0,6.93,1.76,400,400,0,0,1,128.84-142,1.75,1.75,0,0,1,.32-.21A321.92,321.92,0,0,1,257.12,954a.22.22,0,0,1,.11,0q12.61-4.61,25.72-8.1l46.43-12.44,626.16-167.8,13-3.5a121.56,121.56,0,0,0,18.58-5,132.87,132.87,0,0,0-43.77-258.32Z"/></svg>' |
| 353 |
); |
| 354 |
} |
| 355 |
} |
| 356 |
|