| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use FluentSupport\App\App; |
| 6 |
use FluentSupport\App\Modules\PermissionManager; |
| 7 |
use FluentSupport\App\Services\Tickets\TicketStats; |
| 8 |
use FluentSupport\App\Services\Helper; |
| 9 |
use FluentSupport\App\Vite; |
| 10 |
|
| 11 |
class AdminBarHandler |
| 12 |
{ |
| 13 |
public function init() |
| 14 |
{ |
| 15 |
$currentUserPermissions = PermissionManager::currentUserPermissions(); |
| 16 |
|
| 17 |
if (!$currentUserPermissions) { |
| 18 |
return; |
| 19 |
} |
| 20 |
|
| 21 |
if( Helper::showTicketSummaryAdminBar() ) { |
| 22 |
add_action('admin_bar_menu', [$this, 'showTicketSummary'], 999); |
| 23 |
} |
| 24 |
|
| 25 |
} |
| 26 |
|
| 27 |
public function showTicketSummary($adminBar) |
| 28 |
{ |
| 29 |
wp_enqueue_script('fst_global_summary', Vite::getEnqueuePath('admin/js/global_summary.js'), ['jquery'], FLUENT_SUPPORT_VERSION); |
| 30 |
|
| 31 |
wp_localize_script('fst_global_summary', 'fst_bar_vars', [ |
| 32 |
'rest' => $this->getRestInfo(), |
| 33 |
'links' => (new TicketStats())->getQuickLinks(), |
| 34 |
'trans' => [ |
| 35 |
'Quick Summary' => __('Quick Summary', 'fluent-support') |
| 36 |
] |
| 37 |
]); |
| 38 |
|
| 39 |
$args = [ |
| 40 |
'parent' => 'top-secondary', |
| 41 |
'id' => 'fst_global_summary', |
| 42 |
'title' => __('Ticket Summary', 'fluent-support'), |
| 43 |
'href' => '#', |
| 44 |
'meta' => false |
| 45 |
]; |
| 46 |
|
| 47 |
$adminBar->add_node( $args ); |
| 48 |
} |
| 49 |
|
| 50 |
protected function getRestInfo() |
| 51 |
{ |
| 52 |
$app = App::getInstance(); |
| 53 |
|
| 54 |
$ns = $app->config->get('app.rest_namespace'); |
| 55 |
$v = $app->config->get('app.rest_version'); |
| 56 |
|
| 57 |
return [ |
| 58 |
'base_url' => esc_url_raw(rest_url()), |
| 59 |
'url' => rest_url($ns . '/' . $v), |
| 60 |
'nonce' => wp_create_nonce('wp_rest'), |
| 61 |
'namespace' => $ns, |
| 62 |
'version' => $v |
| 63 |
]; |
| 64 |
} |
| 65 |
|
| 66 |
public function initAdminWidget () |
| 67 |
{ |
| 68 |
// This widget should be displayed for certain high-level users only. |
| 69 |
if (!current_user_can('manage_options')) { |
| 70 |
return; |
| 71 |
} |
| 72 |
$widget_key = 'fluent_support_reports_widget'; |
| 73 |
|
| 74 |
wp_add_dashboard_widget( $widget_key, |
| 75 |
__('Fluent Support Stats', 'fluent-support'), |
| 76 |
[$this, 'dashWidgetContent'] |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
public function dashWidgetContent () |
| 81 |
{ |
| 82 |
$ticketStats = (new TicketStats())->getQuickLinks(); |
| 83 |
|
| 84 |
?> |
| 85 |
<div class="fs_dash_wrapper"> |
| 86 |
<table class="fs_dash_table wp-list-table widefat fixed striped"> |
| 87 |
<thead> |
| 88 |
<tr> |
| 89 |
<th><?php esc_html_e('Title', 'fluent-support'); ?></th> |
| 90 |
<th><?php esc_html_e('Count', 'fluent-support'); ?></th> |
| 91 |
<th><?php esc_html_e('Action', 'fluent-support'); ?></th> |
| 92 |
</tr> |
| 93 |
</thead> |
| 94 |
<tbody> |
| 95 |
<?php foreach ($ticketStats as $stat): ?> |
| 96 |
<tr> |
| 97 |
<td><?php echo esc_html($stat['title']); ?></td> |
| 98 |
<td><?php echo esc_html($stat['number']); ?></td> |
| 99 |
<td> |
| 100 |
<a href="<?php echo esc_url($stat['url']); ?>"> |
| 101 |
<?php esc_html_e('View', 'fluent-support'); ?> |
| 102 |
</a> |
| 103 |
</td> |
| 104 |
</tr> |
| 105 |
<?php endforeach; ?> |
| 106 |
</tbody> |
| 107 |
</table> |
| 108 |
</div> |
| 109 |
<?php |
| 110 |
} |
| 111 |
} |
| 112 |
|