| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Services\Notifications; |
| 4 |
|
| 5 |
use FluentSupport\App\Services\Helper; |
| 6 |
use FluentSupport\Database\Migrations\NotificationsMigrator; |
| 7 |
use FluentSupport\Database\Migrations\NotificationUsersMigrator; |
| 8 |
|
| 9 |
class NotificationSettings |
| 10 |
{ |
| 11 |
const OPTION_KEY = '_internal_notification_settings'; |
| 12 |
|
| 13 |
public function get($cached = true) |
| 14 |
{ |
| 15 |
static $settings; |
| 16 |
|
| 17 |
if ($cached && $settings) { |
| 18 |
return $settings; |
| 19 |
} |
| 20 |
|
| 21 |
$settings = $this->normalize(wp_parse_args(Helper::getOption(static::OPTION_KEY, []), $this->defaults())); |
| 22 |
$settings['enabled'] = $this->getGlobalEnabled(); |
| 23 |
|
| 24 |
return $settings; |
| 25 |
} |
| 26 |
|
| 27 |
public function save(array $settings) |
| 28 |
{ |
| 29 |
$settings = $this->normalize(wp_parse_args($settings, $this->defaults())); |
| 30 |
|
| 31 |
if ($settings['enabled'] === 'yes') { |
| 32 |
$this->ensureNotificationTables(); |
| 33 |
} |
| 34 |
|
| 35 |
return Helper::updateOption(static::OPTION_KEY, $settings); |
| 36 |
} |
| 37 |
|
| 38 |
public function defaults() |
| 39 |
{ |
| 40 |
return [ |
| 41 |
'enabled' => 'no', |
| 42 |
'assignment_notifications' => 'yes', |
| 43 |
'agent_reply_notifications' => 'yes', |
| 44 |
'customer_reply_notifications' => 'yes', |
| 45 |
'workflow_notifications' => 'yes', |
| 46 |
'status_notifications' => 'yes', |
| 47 |
'self_notifications' => 'no' |
| 48 |
]; |
| 49 |
} |
| 50 |
|
| 51 |
public function getFields() |
| 52 |
{ |
| 53 |
return [ |
| 54 |
'enabled' => [ |
| 55 |
'type' => 'inline-checkbox', |
| 56 |
'true_label' => 'yes', |
| 57 |
'false-label' => 'no', |
| 58 |
'checkbox_label' => __('Enable Internal Notifications', 'fluent-support'), |
| 59 |
'inline_help' => __('Enable the in-app notification bell, unread counts, and notification event storage for agents.', 'fluent-support') |
| 60 |
], |
| 61 |
'assignment_notifications' => [ |
| 62 |
'type' => 'inline-checkbox', |
| 63 |
'true_label' => 'yes', |
| 64 |
'false-label' => 'no', |
| 65 |
'checkbox_label' => __('Enable Assignment Notifications', 'fluent-support'), |
| 66 |
'dependency' => [ |
| 67 |
'depends_on' => 'enabled', |
| 68 |
'operator' => '=', |
| 69 |
'value' => 'yes' |
| 70 |
] |
| 71 |
], |
| 72 |
'customer_reply_notifications' => [ |
| 73 |
'type' => 'inline-checkbox', |
| 74 |
'true_label' => 'yes', |
| 75 |
'false-label' => 'no', |
| 76 |
'checkbox_label' => __('Enable Customer Reply Notifications', 'fluent-support'), |
| 77 |
'dependency' => [ |
| 78 |
'depends_on' => 'enabled', |
| 79 |
'operator' => '=', |
| 80 |
'value' => 'yes' |
| 81 |
] |
| 82 |
], |
| 83 |
'workflow_notifications' => [ |
| 84 |
'type' => 'inline-checkbox', |
| 85 |
'true_label' => 'yes', |
| 86 |
'false-label' => 'no', |
| 87 |
'checkbox_label' => __('Enable Workflow Notifications', 'fluent-support'), |
| 88 |
'dependency' => [ |
| 89 |
'depends_on' => 'enabled', |
| 90 |
'operator' => '=', |
| 91 |
'value' => 'yes' |
| 92 |
] |
| 93 |
], |
| 94 |
'status_notifications' => [ |
| 95 |
'type' => 'inline-checkbox', |
| 96 |
'true_label' => 'yes', |
| 97 |
'false-label' => 'no', |
| 98 |
'checkbox_label' => __('Enable Ticket Status Notifications', 'fluent-support'), |
| 99 |
'dependency' => [ |
| 100 |
'depends_on' => 'enabled', |
| 101 |
'operator' => '=', |
| 102 |
'value' => 'yes' |
| 103 |
] |
| 104 |
], |
| 105 |
'agent_reply_notifications' => [ |
| 106 |
'type' => 'inline-checkbox', |
| 107 |
'true_label' => 'yes', |
| 108 |
'false-label' => 'no', |
| 109 |
'checkbox_label' => __('Notify Assigned Agent When Another Agent Replies', 'fluent-support'), |
| 110 |
'dependency' => [ |
| 111 |
'depends_on' => 'enabled', |
| 112 |
'operator' => '=', |
| 113 |
'value' => 'yes' |
| 114 |
] |
| 115 |
], |
| 116 |
'self_notifications' => [ |
| 117 |
'type' => 'inline-checkbox', |
| 118 |
'true_label' => 'yes', |
| 119 |
'false-label' => 'no', |
| 120 |
'checkbox_label' => __('Allow Self Notifications', 'fluent-support'), |
| 121 |
'inline_help' => __('By default, agents are not notified for their own actions. Enable this only if agents should receive notifications when they are already the normal recipient for an action they performed themselves.', 'fluent-support'), |
| 122 |
'dependency' => [ |
| 123 |
'depends_on' => 'enabled', |
| 124 |
'operator' => '=', |
| 125 |
'value' => 'yes' |
| 126 |
] |
| 127 |
], |
| 128 |
]; |
| 129 |
} |
| 130 |
|
| 131 |
public function isEnabled() |
| 132 |
{ |
| 133 |
return $this->get()['enabled'] === 'yes'; |
| 134 |
} |
| 135 |
|
| 136 |
public function canUseNotificationTables($ensureTables = false) |
| 137 |
{ |
| 138 |
if (!$this->isEnabled()) { |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
if ($ensureTables) { |
| 143 |
$this->ensureNotificationTables(); |
| 144 |
} |
| 145 |
|
| 146 |
return $this->notificationTablesExist(); |
| 147 |
} |
| 148 |
|
| 149 |
public function allowsSelfNotifications() |
| 150 |
{ |
| 151 |
return $this->get()['self_notifications'] === 'yes'; |
| 152 |
} |
| 153 |
|
| 154 |
public function isEventEnabled($eventType) |
| 155 |
{ |
| 156 |
$eventType = NotificationEventMap::normalizeEventType($eventType); |
| 157 |
$settings = $this->get(); |
| 158 |
|
| 159 |
$eventSettingsMap = [ |
| 160 |
NotificationEventMap::TICKET_ASSIGNED => 'assignment_notifications', |
| 161 |
NotificationEventMap::TICKET_REASSIGNED => 'assignment_notifications', |
| 162 |
NotificationEventMap::AGENT_REPLIED => 'agent_reply_notifications', |
| 163 |
NotificationEventMap::CUSTOMER_REPLIED => 'customer_reply_notifications', |
| 164 |
NotificationEventMap::TICKET_CLOSED => 'status_notifications', |
| 165 |
NotificationEventMap::TICKET_REOPENED => 'status_notifications', |
| 166 |
NotificationEventMap::WORKFLOW_TRIGGERED => 'workflow_notifications', |
| 167 |
]; |
| 168 |
|
| 169 |
$settingKey = $eventSettingsMap[$eventType] ?? null; |
| 170 |
|
| 171 |
if (!$settingKey) { |
| 172 |
return true; |
| 173 |
} |
| 174 |
|
| 175 |
return ($settings[$settingKey] ?? 'yes') === 'yes'; |
| 176 |
} |
| 177 |
|
| 178 |
protected function normalize(array $settings) |
| 179 |
{ |
| 180 |
$toggleKeys = [ |
| 181 |
'enabled', |
| 182 |
'assignment_notifications', |
| 183 |
'agent_reply_notifications', |
| 184 |
'customer_reply_notifications', |
| 185 |
'workflow_notifications', |
| 186 |
'status_notifications', |
| 187 |
'self_notifications', |
| 188 |
]; |
| 189 |
|
| 190 |
foreach ($toggleKeys as $toggleKey) { |
| 191 |
$settings[$toggleKey] = (($settings[$toggleKey] ?? 'no') === 'yes') ? 'yes' : 'no'; |
| 192 |
} |
| 193 |
|
| 194 |
unset($settings['polling_interval']); |
| 195 |
unset($settings['mention_notifications']); |
| 196 |
unset($settings['enabled_categories']); |
| 197 |
|
| 198 |
return $settings; |
| 199 |
} |
| 200 |
|
| 201 |
protected function getGlobalEnabled() |
| 202 |
{ |
| 203 |
$globalSettings = Helper::getOption('global_business_settings', []); |
| 204 |
|
| 205 |
if (is_array($globalSettings) && array_key_exists('internal_notifications_enabled', $globalSettings)) { |
| 206 |
return $globalSettings['internal_notifications_enabled'] === 'yes' ? 'yes' : 'no'; |
| 207 |
} |
| 208 |
|
| 209 |
return 'no'; |
| 210 |
} |
| 211 |
|
| 212 |
public function ensureNotificationTables() |
| 213 |
{ |
| 214 |
if (!function_exists('dbDelta')) { |
| 215 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 216 |
} |
| 217 |
|
| 218 |
NotificationsMigrator::migrate(); |
| 219 |
NotificationUsersMigrator::migrate(); |
| 220 |
} |
| 221 |
|
| 222 |
public function notificationTablesExist() |
| 223 |
{ |
| 224 |
global $wpdb; |
| 225 |
|
| 226 |
$notificationsTable = $wpdb->prefix . NotificationsMigrator::$tableName; |
| 227 |
$notificationUsersTable = $wpdb->prefix . NotificationUsersMigrator::$tableName; |
| 228 |
|
| 229 |
return $wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $notificationsTable)) === $notificationsTable |
| 230 |
&& $wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $notificationUsersTable)) === $notificationUsersTable; |
| 231 |
} |
| 232 |
} |
| 233 |
|