PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / trunk
Fluent Support – Helpdesk & Customer Support Ticket System vtrunk
2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 68 releases
fluent-support / app / Hooks / Handlers / Menu.php

Menu.php in Fluent Support – Helpdesk & Customer Support Ticket System trunk, at app/Hooks/Handlers/Menu.php

533 lines 21.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\AgentGroup;
8 use FluentSupport\App\Models\MailBox;
9 use FluentSupport\App\Models\Product;
10 use FluentSupport\App\Models\TagPivot;
11 use FluentSupport\App\Models\TicketTag;
12 use FluentSupport\App\Modules\PermissionManager;
13 use FluentSupport\App\Services\Helper;
14 use FluentSupport\App\Services\Notifications\NotificationSettings;
15 use FluentSupport\App\Services\TranslationStrings;
16 use FluentSupport\App\Services\Integrations\FluentBooking\FluentBookingService;
17 use FluentSupport\App\Vite;
18
19 class Menu
20 {
21 public function add()
22 {
23 $capability = PermissionManager::getMenuPermission();
24
25 if (!$capability) {
26 return;
27 }
28
29 $menuPosition = 25;
30 if (defined('FLUENTCRM')) {
31 $menuPosition = 4;
32 }
33
34 /*
35 * Filter Fluent Support menu position in WordPress dashboard
36 * @param integer $menuPosition
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 $capability,
44 'fluent-support',
45 array($this, 'renderApp'),
46 $this->getMenuIcon(),
47 $menuPosition
48 );
49
50 add_submenu_page(
51 'fluent-support',
52 __('Dashboard', 'fluent-support'),
53 __('Dashboard', 'fluent-support'),
54 $capability,
55 'fluent-support',
56 array($this, 'renderApp')
57 );
58
59 add_submenu_page(
60 'fluent-support',
61 __('Tickets', 'fluent-support'),
62 __('Tickets', 'fluent-support'),
63 $capability,
64 'fluent-support#/tickets',
65 array($this, 'renderApp')
66 );
67
68 if (PermissionManager::currentUserCan('fst_view_all_reports')) {
69 add_submenu_page(
70 'fluent-support',
71 __('Reports', 'fluent-support'),
72 __('Reports', 'fluent-support'),
73 $capability,
74 'fluent-support#/reports',
75 array($this, 'renderApp')
76 );
77 }
78
79 if (PermissionManager::currentUserCan('fst_sensitive_data')) {
80 add_submenu_page(
81 'fluent-support',
82 __('Customers', 'fluent-support'),
83 __('Customers', 'fluent-support'),
84 $capability,
85 'fluent-support#/customers',
86 array($this, 'renderApp')
87 );
88 }
89
90 if (PermissionManager::currentUserCan('fst_view_activity_logs')) {
91 add_submenu_page(
92 'fluent-support',
93 __('Activities', 'fluent-support'),
94 __('Activities', 'fluent-support'),
95 $capability,
96 'fluent-support#/activity',
97 array($this, 'renderApp')
98 );
99 }
100
101 if (PermissionManager::currentUserCan('fst_manage_settings')) {
102 add_submenu_page(
103 'fluent-support',
104 __('Business Inboxes', 'fluent-support'),
105 __('Business Inboxes', 'fluent-support'),
106 $capability,
107 'fluent-support#/mailboxes',
108 array($this, 'renderApp')
109 );
110 }
111
112 if (PermissionManager::currentUserCan('fst_manage_workflows')) {
113 add_submenu_page(
114 'fluent-support',
115 __('Workflows', 'fluent-support'),
116 __('Workflows', 'fluent-support'),
117 $capability,
118 'fluent-support#/workflows',
119 array($this, 'renderApp')
120 );
121 }
122
123 if (PermissionManager::currentUserCan('fst_manage_saved_replies')) {
124 add_submenu_page(
125 'fluent-support',
126 __('Saved Replies', 'fluent-support'),
127 __('Saved Replies', 'fluent-support'),
128 $capability,
129 'fluent-support#/saved-replies',
130 array($this, 'renderApp')
131 );
132 }
133
134 if (PermissionManager::currentUserCan('fst_manage_settings')) {
135 add_submenu_page(
136 'fluent-support',
137 __('Settings', 'fluent-support'),
138 __('Settings', 'fluent-support'),
139 $capability,
140 'fluent-support#/settings',
141 array($this, 'renderApp')
142 );
143 }
144 }
145
146 public function getMenuItems()
147 {
148 $baseUrl = apply_filters('fluent_support/base_url', admin_url('admin.php?page=fluent-support#/'));
149
150 $menuItems = [
151 'dashboard' => [
152 'key' => 'dashboard',
153 'label' => __('Dashboard', 'fluent-support'),
154 'permalink' => $baseUrl,
155 ],
156 'tickets' => [
157 'key' => 'tickets',
158 'label' => __('Tickets', 'fluent-support'),
159 'permalink' => $baseUrl . 'tickets',
160 ],
161 'reports' => [
162 'key' => 'reports',
163 'label' => __('Reports', 'fluent-support'),
164 'permalink' => $baseUrl . 'reports',
165 ],
166 ];
167
168 $canManageSettings = PermissionManager::currentUserCan('fst_manage_settings');
169
170 if ($canManageSettings) {
171 $menuItems['mailboxes'] = [
172 'key' => 'mailboxes',
173 'label' => __('Business Inboxes', 'fluent-support'),
174 'permalink' => $baseUrl . 'mailboxes',
175 ];
176 }
177
178 if (PermissionManager::currentUserCan('fst_view_activity_logs')) {
179 $menuItems['activity'] = [
180 'key' => 'activity',
181 'label' => __('Activities', 'fluent-support'),
182 'permalink' => $baseUrl . 'activity',
183 ];
184 }
185
186 if (PermissionManager::currentUserCan('fst_sensitive_data')) {
187 $menuItems['customers'] = [
188 'key' => 'customers',
189 'label' => __('Customers', 'fluent-support'),
190 'permalink' => $baseUrl . 'customers',
191 ];
192 }
193
194 $moreChildren = [];
195
196 if (PermissionManager::currentUserCan('fst_manage_saved_replies')) {
197 $moreChildren['saved_replies'] = [
198 'key' => 'saved_replies',
199 'label' => __('Saved Replies', 'fluent-support'),
200 'permalink' => $baseUrl . 'saved-replies',
201 ];
202 }
203
204 if (PermissionManager::currentUserCan('fst_manage_workflows')) {
205 $moreChildren['workflows'] = [
206 'key' => 'workflows',
207 'label' => __('Workflows', 'fluent-support'),
208 'permalink' => $baseUrl . 'workflows',
209 ];
210 }
211
212 if (!empty($moreChildren)) {
213 $menuItems['more'] = [
214 'key' => 'more',
215 'label' => __('More', 'fluent-support'),
216 'permalink' => '#',
217 'children' => $moreChildren,
218 ];
219 }
220
221 if ($canManageSettings) {
222 $menuItems['settings'] = [
223 'key' => 'settings',
224 'label' => __('Global Settings', 'fluent-support'),
225 'permalink' => $baseUrl . 'settings',
226 ];
227 }
228
229 return apply_filters('fluent_support/menu_items', $menuItems);
230 }
231
232 public function renderApp()
233 {
234 $app = App::getInstance();
235
236 $assets = $app['url.assets'];
237
238 $allItems = $this->getMenuItems();
239
240 $secondaryKeys = ['settings'];
241 $menuItems = [];
242 $secondaryItems = [];
243
244 foreach ($allItems as $key => $item) {
245 if (in_array($key, $secondaryKeys, true)) {
246 $secondaryItems[] = $item;
247 } else {
248 $menuItems[] = $item;
249 }
250 }
251
252 $menuItems = apply_filters('fluent_support/primary_menu_items', $menuItems);
253 $secondaryItems = apply_filters('fluent_support/secondary_menu_items', $secondaryItems);
254
255 if (!defined('FLUENT_SUPPORT_PRO_DIR_FILE')) {
256 $secondaryItems[] = [
257 'key' => 'upgrade_to_pro',
258 'label' => 'Upgrade to Pro',
259 'permalink' => Helper::getUpgradeUrl('sidebar_cta')
260 ];
261 }
262
263 $baseUrl = apply_filters('fluent_support/base_url', admin_url('admin.php?page=fluent-support#/'));
264
265 $this->enqueueAssets();
266
267 do_action('fluent_support/admin_app_loaded', $app);
268 $app->view->render('admin.menu', [
269 'base_url' => $baseUrl,
270 'logo' => $assets . 'images/logo.svg',
271 'settingsLogo' => $assets . 'images/gear.svg',
272 'upgradeLogo' => $assets . 'images/crown.svg',
273 'assets' => $assets,
274 'menuItems' => $menuItems,
275 'secondaryItems' => isset($secondaryItems) ? $secondaryItems : [],
276 ]);
277 }
278
279 public function maybeEnqueueAssets()
280 {
281 if (isset($_GET['page']) && sanitize_text_field(wp_unslash($_GET['page'])) === 'fluent-support') {
282 add_action('admin_head', [$this, 'printDarkModeInit'], 1);
283 $this->enqueueAssets();
284 }
285 }
286
287 public function printDarkModeInit()
288 {
289 ?>
290 <script>
291 (function () {
292 var savedTheme = localStorage.getItem('fs-theme');
293
294 // Check if dark mode should be active
295 // localStorage stores: 'dark', 'light', or 'system:dark' / 'system:light'
296 var isDark = savedTheme
297 ? savedTheme.split(':').pop() === 'dark'
298 : window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
299
300 if (isDark) {
301 // Apply immediately to <html> to prevent white flash
302 document.documentElement.classList.add('fs-dark-mode');
303
304 // Watch for <body> to appear and apply class as soon as it exists
305 new MutationObserver(function (mutations, observer) {
306 if (document.body) {
307 document.body.classList.add('fs-dark-mode');
308 observer.disconnect();
309 }
310 }).observe(document.documentElement, { childList: true });
311 }
312 })();
313 </script>
314 <?php
315 }
316
317 public function enqueueAssets()
318 {
319 $app = App::getInstance();
320
321 $assets = $app['url.assets'];
322
323 // Inject Vite HMR client for dev mode
324 add_action('admin_head', function () {
325 Vite::injectViteClient();
326 }, 1);
327
328 wp_enqueue_script('dompurify', $assets . 'libs/purify/purify.min.js', [], '3.4.13');
329
330 if (is_rtl()) {
331 wp_enqueue_style('fluent_support_admin_app_rtl', $assets . 'admin/css/alpha-admin-rtl.css', [], FLUENT_SUPPORT_VERSION);
332 } else {
333 wp_enqueue_style('fluent_support_admin_app', Vite::getEnqueuePath('admin/css/alpha-admin.css'), [], FLUENT_SUPPORT_VERSION);
334 }
335
336 $agents = Agent::select(['id', 'first_name', 'last_name'])
337 ->where('person_type', 'agent')
338 ->get()->toArray();
339
340 foreach ($agents as $index => $agent) {
341 $agents[$index]['id'] = strval($agent['id']);
342 }
343
344 $agentGroups = AgentGroup::select(['id', 'title', 'settings'])->get();
345 $groupPivots = TagPivot::where('source_type', 'agent_group')->get();
346 foreach ($agentGroups as $group) {
347 $group->agent_ids = $groupPivots->where('tag_id', $group->id)
348 ->pluck('source_id')
349 ->map(function ($id) { return strval($id); })
350 ->values()
351 ->toArray();
352 }
353
354 $me = Helper::getAgentByUserId(get_current_user_id());
355
356 if (!$me && current_user_can('manage_options')) {
357 // we should create the agent
358 $user = wp_get_current_user();
359 $me = Agent::create([
360 'email' => $user->user_email,
361 'first_name' => $user->first_name,
362 'last_name' => $user->last_name,
363 'user_id' => $user->ID
364 ]);
365 }
366
367 $me->permissions = PermissionManager::currentUserPermissions();
368
369 do_action('fluent_support_loading_app', $app);
370
371 // Editor default styles.
372 add_filter('user_can_richedit', '__return_true');
373 wp_tinymce_inline_scripts();
374 wp_enqueue_editor();
375 wp_enqueue_media();
376
377 wp_enqueue_script(
378 'fluent_support_admin_app_start',
379 Vite::getEnqueuePath('admin/js/start.js'),
380 array('jquery'),
381 FLUENT_SUPPORT_VERSION,
382 true
383 );
384
385 wp_enqueue_script(
386 'fluent_support_global_admin',
387 Vite::getEnqueuePath('admin/js/global_admin.js'),
388 array('jquery'),
389 FLUENT_SUPPORT_VERSION,
390 true
391 );
392
393 $integrationDrivers = [];
394 if (!defined('FLUENTSUPPORTPRO')) {
395 $integrationDrivers = [
396 [
397 'key' => 'telegram_settings',
398 'title' => __('Telegram', 'fluent-support'),
399 'description' => __('Send Telegram notifications to Group, Channel or individual person inbox and reply from Telegram inbox', 'fluent-support'),
400 'promo_heading' => __('Get activity notification to Telegram Messenger and reply directly from Telegram inbox', 'fluent-support'),
401 'require_pro' => true
402 ],
403 [
404 'key' => 'slack_settings',
405 'title' => __('Slack', 'fluent-support'),
406 'description' => __('Send ticket activity notifications to slack', 'fluent-support'),
407 'promo_heading' => __('Get activity notification to Slack Channel and keep your support team super engaged', 'fluent-support'),
408 'require_pro' => true
409 ]
410 ];
411 }
412
413 /*
414 * Filter integration driver
415 * @param array $integrationDrivers
416 */
417 $integrationDrivers = apply_filters('fluent_support/integration_drivers', $integrationDrivers);
418
419 $tags = TicketTag::select(['id', 'title'])->get()->toArray();
420
421 $tags = array_map(function ($tag) {
422 $tag['id'] = strval($tag['id']);
423 return $tag;
424 }, $tags);
425
426 $i18ns = TranslationStrings::getAdminStrings();
427 $i18ns['allowed_files_and_size'] = Helper::getFileUploadMessage();
428 $i18ns['open'] = __('Open', 'fluent-support');
429 $i18ns['all'] = __('All', 'fluent-support');
430
431 /*
432 * Filter agent portal localize javascript data
433 *
434 * @since v1.0.0
435 *
436 * @param array $appVars
437 */
438 $appVars = apply_filters('fluent_support_app_vars', array(
439 'slug' => $slug = $app->config->get('app.slug'),
440 'nonce' => wp_create_nonce($slug),
441 'rest' => $this->getRestInfo($app),
442 'brand_logo' => $this->getMenuIcon(),
443 'firstEntry' => '',
444 'lastEntry' => '',
445 'asset_url' => $assets,
446 'support_agents' => $agents,
447 'agent_groups' => $agentGroups,
448 'support_products' => Product::select(['id', 'title'])->orderedByTitle()->get(),
449 'client_priorities' => Helper::customerTicketPriorities(),
450 'ticket_statuses' => Helper::ticketStatuses(),
451 'ticket_statuses_group' => Helper::ticketStatusGroups(),
452 'changeable_ticket_statuses' => Helper::changeableTicketStatuses(),
453 'admin_priorities' => Helper::adminTicketPriorities(),
454 'mailboxes' => MailBox::getAccessibleBoxes(),
455 // Whether the site has any inbox at all, regardless of what this agent may see.
456 // `mailboxes` is restriction-filtered and can legitimately be empty, so it cannot
457 // double as the "is Fluent Support set up yet?" signal the setup wizard keys off.
458 'has_mailboxes' => MailBox::count() > 0,
459 'me' => $me,
460 // Deleting a customer needs fst_sensitive_data plus administrator rights unless
461 // the site turns the administrator part off.
462 'customer_delete_requires_admin' => (bool) apply_filters('fluent_support/customer_delete_requires_admin', true),
463 'pref' => [
464 'go_back_after_reply' => 'yes'
465 ],
466 'notification_integrations' => $integrationDrivers,
467 'internal_notification_settings' => (new NotificationSettings())->get(false),
468 'server_time' => gmdate('Y-m-d\TH:i:sP'),
469 'has_email_parser' => defined('FLUENTSUPPORTPRO_PLUGIN_VERSION'),
470 'ticket_tags' => $tags,
471 'i18n' => $i18ns,
472 'custom_fields' => apply_filters('fluent_support/ticket_custom_fields', []),
473 'has_file_upload' => !!Helper::ticketAcceptedFileMiles(),
474 'repost_export_options' => Helper::getExportOptions(),
475 'enable_draft_mode' => Helper::getBusinessSettings('enable_draft_mode', 'no'),
476 'keyboard_shortcuts' => Helper::getBusinessSettings('keyboard_shortcuts', 'no'),
477 'agent_time_tracking' => Helper::getBusinessSettings('agent_time_tracking', 'no'),
478 'max_file_upload' => Helper::getBusinessSettings('max_file_upload', 3),
479 'ajaxurl' => admin_url('admin-ajax.php'),
480 'auth_provider' => Helper::getAuthProvider(),
481 'fluent_bot_integration' => Helper::fluentBotIntegrationStatus(),
482 ));
483
484 $appVars['fluent_booking'] = (new FluentBookingService())->getStatus();
485
486 if (defined('FLUENTCRM')) {
487 $appVars['fluentcrm_config'] = Helper::getFluentCRMTagConfig();
488 }
489
490 if (defined('FLUENT_BOARDS')) {
491 $appVars['fluent_boards'] = true;
492 }
493
494 $appVars['has_pro'] = defined('FLUENTSUPPORTPRO_PLUGIN_VERSION');
495 $appVars['version'] = FLUENT_SUPPORT_VERSION;
496 // Single source of truth for upgrade CTAs; the SPA only re-points utm_content on this.
497 $appVars['upgrade_url'] = Helper::getUpgradeUrl();
498 if ($appVars['has_pro']) {
499 $appVars['agent_feedback_rating'] = Helper::getBusinessSettings('agent_feedback_rating', 'no');
500 $appVars['open_ai_integration'] = Helper::openAIIntegrationStatus();
501 }
502
503 wp_localize_script('fluent_support_admin_app_start', 'fluentSupportAdmin', $appVars);
504 }
505
506 protected function getRestInfo($app)
507 {
508 $ns = $app->config->get('app.rest_namespace');
509 $v = $app->config->get('app.rest_version');
510
511 return [
512 'base_url' => esc_url_raw(rest_url()),
513 'url' => rest_url($ns . '/' . $v),
514 'nonce' => wp_create_nonce('wp_rest'),
515 'namespace' => $ns,
516 'version' => $v,
517 ];
518 }
519
520 protected function getMenuIcon()
521 {
522 $app = App::getInstance();
523
524 $assets = $app['path.assets'];
525
526 return 'data:image/svg+xml;base64,' . base64_encode(
527 '<svg width="100" height="100" viewBox="0 0 100 100" fill="none" xmlns="http://www.w3.org/2000/svg">
528 <path d="M90 0C95.5228 1.28852e-06 100 4.47715 100 10V90C100 95.5228 95.5228 100 90 100H10C4.47715 100 0 95.5228 0 90V10C1.28855e-06 4.47715 4.47715 0 10 0H90ZM70.2314 44.8672L70.2266 44.876H69.4941C68.7844 44.876 68.0791 44.9715 67.3965 45.1523L60.3203 47.0508L22.0703 57.3096C21.4826 57.4 20.9177 57.604 20.4023 57.9023H20.3428C18.7515 58.8292 17.7751 60.5292 17.7705 62.3691V81.9424C17.7662 82.0596 17.8564 82.1632 17.9736 82.168C18.0595 82.168 18.1409 82.1224 18.1816 82.041C19.9811 78.7134 22.5589 75.8698 25.7012 73.7539C27.0801 72.7774 28.5854 71.9902 30.1768 71.416C30.6694 71.2352 31.167 71.0821 31.6777 70.9375L34.3682 70.209L70.9092 60.4209L71.6641 60.2129C72.0348 60.1496 72.3973 60.0494 72.75 59.9229C76.7916 58.503 78.9165 54.0818 77.4971 50.04C76.4074 46.9384 73.4817 44.8626 70.1992 44.8535L70.2314 44.8672ZM77.1172 19.3086C76.8415 19.3267 76.5612 19.3583 76.29 19.417C75.9194 19.4532 75.5485 19.5217 75.1914 19.6211L21.79 33.9219C20.1217 34.365 18.7609 35.5766 18.1279 37.1816C18.1189 37.2178 18.1001 37.2588 18.082 37.2949C18.046 37.3851 18.0144 37.4798 17.9873 37.5654C17.9602 37.6513 17.915 37.7422 17.915 37.8281C17.9015 37.8733 17.8928 37.9187 17.8838 37.9639C17.8567 38.0765 17.8118 38.2653 17.8115 38.2979V38.3975C17.7889 38.5105 17.7796 38.6285 17.7705 38.7461V45.6494C17.7705 45.8348 17.7844 46.0164 17.8115 46.1973C17.8343 48.3176 19.5613 50.0217 21.6816 50.0127C21.8895 50.0127 22.0978 49.9951 22.3057 49.959C22.6085 49.9454 22.9113 49.8954 23.2051 49.8096L68.7705 37.5928C69.9958 37.2627 71.2669 37.1404 72.5283 37.2354C76.9635 37.5068 80.8557 40.2878 82.5557 44.3975V25.2637C82.5557 25.0286 82.5425 24.8066 82.5244 24.5986V24.4717C82.5242 21.619 80.2141 19.3087 77.3613 19.3086H77.1172Z" fill="#9CA1A8"/>
529 </svg>'
530 );
531 }
532 }
533