| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Services\EmailNotification; |
| 4 |
|
| 5 |
use FluentSupport\App\Services\Helper; |
| 6 |
|
| 7 |
class Settings |
| 8 |
{ |
| 9 |
|
| 10 |
public function getEmailSettingsKeys() |
| 11 |
{ |
| 12 |
$key = apply_filters('fluent_support/email_setting_keys', [ |
| 13 |
'ticket_created_email_to_customer', |
| 14 |
'ticket_replied_by_agent_email_to_customer', |
| 15 |
'ticket_closed_by_agent_email_to_customer', |
| 16 |
'ticket_created_email_to_admin', |
| 17 |
'ticket_replied_by_customer_email_to_admin', |
| 18 |
'ticket_agent_on_change' |
| 19 |
]); |
| 20 |
return $key; |
| 21 |
} |
| 22 |
|
| 23 |
public function get($settingsKey) |
| 24 |
{ |
| 25 |
if ($settingsKey == 'global_business_settings') { |
| 26 |
return [ |
| 27 |
'settings' => $this->globalBusinessSettings(), |
| 28 |
'fields' => $this->getGlobalBusinessSettingsFields() |
| 29 |
]; |
| 30 |
} |
| 31 |
|
| 32 |
return [ |
| 33 |
'settings' => [], |
| 34 |
'fields' => [] |
| 35 |
]; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* save method will save the requested settings by settings key |
| 40 |
* @param $settingsKey |
| 41 |
* @param $settings |
| 42 |
* @return mixed |
| 43 |
*/ |
| 44 |
public function save($settingsKey, $settings) |
| 45 |
{ |
| 46 |
if ($settingsKey == 'global_business_settings' && empty($settings['accepted_file_types'])) { |
| 47 |
$settings['accepted_file_types'] = []; |
| 48 |
} |
| 49 |
|
| 50 |
return Helper::updateOption($settingsKey, $settings); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* globalBusinessSettings method will fetch global settings from database, parse and return |
| 55 |
* @param bool $cached |
| 56 |
* @return array|mixed |
| 57 |
*/ |
| 58 |
public function globalBusinessSettings($cached = true) |
| 59 |
{ |
| 60 |
static $settings; |
| 61 |
|
| 62 |
if($cached && $settings) { |
| 63 |
return $settings; |
| 64 |
} |
| 65 |
|
| 66 |
$defaults = [ |
| 67 |
'portal_page_id' => '', |
| 68 |
'login_message' => sprintf(__('%1sPlease login or create an account to access the Customer Support Portal%2s [fluent_support_auth]', 'fluent-support'), '<p>', '</p>'), |
| 69 |
'disable_public_ticket' => 'no', |
| 70 |
'accepted_file_types' => ['images', 'csv', 'documents', 'zip', 'json'], |
| 71 |
'max_file_size' => 2, |
| 72 |
'del_files_on_close' => 'no' |
| 73 |
]; |
| 74 |
|
| 75 |
//Get default/existing settings from database using the key global_business_settings |
| 76 |
$existingSettings = Helper::getOption('global_business_settings', []); |
| 77 |
|
| 78 |
if (!$existingSettings) { |
| 79 |
$settings = $defaults; |
| 80 |
return $settings; |
| 81 |
} |
| 82 |
|
| 83 |
$settings = wp_parse_args($existingSettings, $defaults); |
| 84 |
|
| 85 |
return $settings; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
/** |
| 90 |
* getGlobalBusinessSettingsFields method will prepare the list of field, and it's property that will be used in global settings form |
| 91 |
* @return array |
| 92 |
*/ |
| 93 |
private function getGlobalBusinessSettingsFields() |
| 94 |
{ |
| 95 |
|
| 96 |
$mimeGroups = Helper::getMimeGroups(); |
| 97 |
|
| 98 |
$formattedMimeGroups = []; |
| 99 |
|
| 100 |
foreach ($mimeGroups as $mimeGroup => $mime) { |
| 101 |
$formattedMimeGroups[$mimeGroup] = $mime['title']; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
$fields = [ |
| 106 |
'portal_page_id' => [ |
| 107 |
'type' => 'input-options', |
| 108 |
'label' => __('Portal Page', 'fluent-support'), |
| 109 |
'show_id' => true, |
| 110 |
'placeholder' => __('Select Portal Page', 'fluent-support'), |
| 111 |
'options' => Helper::getWPPages(),//Get list of published pages |
| 112 |
'inline_help' => __('Please provide the page id where you want to show the tickets for your customers. Use shortcode <code>[fluent_support_portal]</code> in that page', 'fluent-support') |
| 113 |
], |
| 114 |
'login_message' => [ |
| 115 |
'type' => 'wp-editor', |
| 116 |
'label' => __('Message for non logged in users', 'fluent-support'), |
| 117 |
'inline_help' => 'Please provide message for not logged in users. You can place login shortcode too Use shortcode <code>[fluent_support_login]</code> to show built-in login form. For the user registration use this shortcode <code>[fluent_support_signup]</code> and for both form please use <code>[fluent_support_auth]</code>' |
| 118 |
], |
| 119 |
'disable_public_ticket' => [ |
| 120 |
'type' => 'inline-checkbox', |
| 121 |
'true_label' => 'yes', |
| 122 |
'false-label' => 'no', |
| 123 |
'checkbox_label' => __('Disable Public Ticket interaction', 'fluent-support'), |
| 124 |
'inline_help' => __('If you enable this then only logged in user can reply the tickets. Otherwise, url will be signed and intended user can reply without logging in', 'fluent-support') |
| 125 |
], |
| 126 |
'accepted_file_types' => [ |
| 127 |
'wrapper_class' => 'fs_half_field', |
| 128 |
'type' => 'checkbox-group', |
| 129 |
'label' => __('Accepted File Types', 'fluent-support'), |
| 130 |
'options' => $formattedMimeGroups |
| 131 |
], |
| 132 |
'max_file_size' => [ |
| 133 |
'wrapper_class' => 'fs_half_field', |
| 134 |
'type' => 'input-text', |
| 135 |
'data_type' => 'number', |
| 136 |
'label' => 'Max File Size (in MegaByte)', |
| 137 |
], |
| 138 |
'del_files_on_close' => [ |
| 139 |
'type' => 'inline-checkbox', |
| 140 |
'true_label' => 'yes', |
| 141 |
'false-label' => 'no', |
| 142 |
'checkbox_label' => __('Delete all attachments on ticket close', 'fluent-support'), |
| 143 |
'inline_help' => __('If you enable this then when a ticket get closed it will delete all the attachments associated with the particular ticket.', 'fluent-support') |
| 144 |
] |
| 145 |
]; |
| 146 |
|
| 147 |
return $fields; |
| 148 |
} |
| 149 |
|
| 150 |
public function saveBoxEmailSettings($box, $emailKey, $settings) |
| 151 |
{ |
| 152 |
return $box->saveMeta('_email_' . $emailKey, $settings); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* getBoxEmailSettings method will reply the email settings |
| 157 |
* @param $box |
| 158 |
* @param $emailKey |
| 159 |
* @return array|false |
| 160 |
*/ |
| 161 |
public function getBoxEmailSettings($box, $emailKey) |
| 162 |
{ |
| 163 |
if (!$box) { |
| 164 |
return false; |
| 165 |
} |
| 166 |
|
| 167 |
#ticket_closed_by_agent_email_to_customer 2 times, is it wrong or right!!! |
| 168 |
$strictSubjectKeys = apply_filters('fluent_support/strict_subjects', [ |
| 169 |
'ticket_replied_by_agent_email_to_customer', |
| 170 |
'ticket_closed_by_agent_email_to_customer', |
| 171 |
'ticket_created_email_to_customer' |
| 172 |
]); |
| 173 |
|
| 174 |
$settingsDefaults = [ |
| 175 |
'ticket_created_email_to_customer' => [ |
| 176 |
'key' => 'ticket_created_email_to_customer', |
| 177 |
'title' => __('Ticket Created (To Customer)', 'fluent-support'), |
| 178 |
'description' => __('This email will be sent when a customer submit a support ticket', 'fluent-support'), |
| 179 |
'email_subject' => 'Re: {{ticket.title}} #{{ticket.id}}', |
| 180 |
'default_status' => 'yes', |
| 181 |
'send_attachments'=> 'no' |
| 182 |
], |
| 183 |
'ticket_replied_by_agent_email_to_customer' => [ |
| 184 |
'key' => 'ticket_replied_by_agent_email_to_customer', |
| 185 |
'title' => __('Replied by Agent (To Customer)', 'fluent-support'), |
| 186 |
'description' => __('This email will be sent when an agent reply to a ticket', 'fluent-support'), |
| 187 |
'email_subject' => 'Re: {{ticket.title}} #{{ticket.id}}', |
| 188 |
'default_status' => 'yes', |
| 189 |
'send_attachments'=> 'no' |
| 190 |
], |
| 191 |
'ticket_closed_by_agent_email_to_customer' => [ |
| 192 |
'key' => 'ticket_closed_by_agent_email_to_customer', |
| 193 |
'title' => __('Ticket Closed by Agent (To Customer)', 'fluent-support'), |
| 194 |
'description' => __('This email will be sent when an agent close a ticket', 'fluent-support'), |
| 195 |
'email_subject' => 'Re: {{ticket.title}} #{{ticket.id}}', |
| 196 |
'default_status' => 'yes', |
| 197 |
'send_attachments'=> 'no' |
| 198 |
], |
| 199 |
'ticket_created_email_to_admin' => [ |
| 200 |
'key' => 'ticket_created_email_to_admin', |
| 201 |
'title' => __('Ticket Created (To Admin)', 'fluent-support'), |
| 202 |
'description' => __('This email will be sent when the business when a new ticket has been submitted', 'fluent-support'), |
| 203 |
'email_subject' => 'New Ticket: {{ticket.title}} #{{ticket.id}}', |
| 204 |
'default_status' => 'yes', |
| 205 |
'send_attachments'=> 'no' |
| 206 |
], |
| 207 |
'ticket_replied_by_customer_email_to_admin' => [ |
| 208 |
'key' => 'ticket_replied_by_customer_email_to_admin', |
| 209 |
'title' => __('Replied by Customer (To Agent/Admin)', 'fluent-support'), |
| 210 |
'description' => __('This email will be sent to Assigned Agent or Admin when a customer reply to a ticket', 'fluent-support'), |
| 211 |
'email_subject' => 'New Response: {{ticket.title}} #{{ticket.id}}', |
| 212 |
'default_status' => 'yes', |
| 213 |
'send_attachments'=> 'no' |
| 214 |
], |
| 215 |
'ticket_agent_on_change' => [ |
| 216 |
'key' => 'ticket_agent_on_change', |
| 217 |
'title' => __('Ticket Agent Change (To Agent)', 'fluent-support'), |
| 218 |
'description' => __('This email will be sent to newly assigned agent', 'fluent-support'), |
| 219 |
'email_subject' => 'Ticket Agent Change: {{ticket.title}} #{{ticket.id}}', |
| 220 |
'default_status' => 'yes', |
| 221 |
'send_attachments'=> 'no' |
| 222 |
] |
| 223 |
]; |
| 224 |
|
| 225 |
if (!isset($settingsDefaults[$emailKey])) { |
| 226 |
return false; |
| 227 |
} |
| 228 |
|
| 229 |
$savedSettings = (array)$box->getMeta('_email_' . $emailKey, []); |
| 230 |
|
| 231 |
|
| 232 |
if (!$savedSettings) { |
| 233 |
$savedSettings = [ |
| 234 |
'key' => $settingsDefaults[$emailKey]['key'], |
| 235 |
'title' => $settingsDefaults[$emailKey]['title'], |
| 236 |
'email_subject' => $settingsDefaults[$emailKey]['email_subject'], |
| 237 |
'email_body' => $this->getDefaultEmailBody($emailKey, $box->box_type), |
| 238 |
'status' => $settingsDefaults[$emailKey]['default_status'], |
| 239 |
'can_edit_subject' => (in_array($emailKey, $strictSubjectKeys) && $box->box_type == 'email') ? 'no' : 'yes', |
| 240 |
'send_attachments' => $settingsDefaults[$emailKey]['send_attachments'] |
| 241 |
]; |
| 242 |
|
| 243 |
if ($box->box_type == 'email' && in_array($emailKey, $strictSubjectKeys)) { |
| 244 |
$savedSettings['email_subject'] = 'Re: {{ticket.title}}'; |
| 245 |
$savedSettings['can_edit_subject'] = 'no'; |
| 246 |
} |
| 247 |
|
| 248 |
return $savedSettings; |
| 249 |
} |
| 250 |
|
| 251 |
$savedSettings['key'] = $settingsDefaults[$emailKey]['key']; |
| 252 |
$savedSettings['title'] = $settingsDefaults[$emailKey]['title']; |
| 253 |
$savedSettings['description'] = $settingsDefaults[$emailKey]['description']; |
| 254 |
|
| 255 |
if ($box->box_type == 'email' && in_array($emailKey, $strictSubjectKeys)) { |
| 256 |
$savedSettings['email_subject'] = 'Re: {{ticket.title}}'; |
| 257 |
$savedSettings['can_edit_subject'] = 'no'; |
| 258 |
} |
| 259 |
|
| 260 |
if (empty($savedSettings['email_subject'])) { |
| 261 |
$savedSettings['email_subject'] = $settingsDefaults[$emailKey]['email_subject']; |
| 262 |
} |
| 263 |
|
| 264 |
if (empty($savedSettings['status'])) { |
| 265 |
$savedSettings['status'] = $settingsDefaults[$emailKey]['default_status']; |
| 266 |
} |
| 267 |
|
| 268 |
if (empty($savedSettings['email_body'])) { |
| 269 |
$savedSettings['email_body'] = $this->getDefaultEmailBody($emailKey, $box->box_type); |
| 270 |
} |
| 271 |
|
| 272 |
if (empty($savedSettings['send_attachments'])) { |
| 273 |
$savedSettings['send_attachments'] = $settingsDefaults[$emailKey]['send_attachments']; |
| 274 |
} |
| 275 |
|
| 276 |
return $savedSettings; |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* getDefaultEmailBody method will return html for email body |
| 281 |
* @param $emailKey |
| 282 |
* @param string $type |
| 283 |
* @return string |
| 284 |
*/ |
| 285 |
private function getDefaultEmailBody($emailKey, $type = 'web') |
| 286 |
{ |
| 287 |
if ($emailKey == 'ticket_created_email_to_customer') { |
| 288 |
if ($type == 'web') { |
| 289 |
return '<p>Hi <strong><em>{{customer.full_name}}</em>,</strong></p><p>Your request (<a href="{{ticket.public_url}}">#{{ticket.id}}</a>) has been received, and is being reviewed by our support staff.</p><p>To add additional comments, follow the link below:</p><h4><a href="{{ticket.public_url}}">View Ticket</a></h4><p> </p><p>or follow this link: {{ticket.public_url}}</p><hr /><p>{{business.name}}</p>'; |
| 290 |
} else { |
| 291 |
return '<p>Hi <strong><em>{{customer.full_name}}</em>,</strong></p><p>Your request has been received, and is being reviewed by our support staff.</p><p>Our support staff will reply back to you soon</p>'; |
| 292 |
} |
| 293 |
} else if ($emailKey == 'ticket_replied_by_agent_email_to_customer') { |
| 294 |
if ($type == 'web') { |
| 295 |
return '<p>Hi <strong><em>{{customer.full_name}}</em>,</strong></p><p>An agent just replied to your ticket "<strong>{{ticket.title}}</strong>" (<a href="{{ticket.public_url}}">#{{ticket.id}}</a>). To view his reply or add additional comments, click the button below:</p><h4><a href="{{ticket.public_url}}">View Ticket</a></h4><p>or follow this link: {{ticket.public_url}}</p><hr /><p>Regards,<br />{{business.name}}</p>'; |
| 296 |
} else { |
| 297 |
return '{{response.full_content}}<p>Regards,<br />{{agent.full_name}}</p>'; |
| 298 |
} |
| 299 |
} else if ($emailKey == 'ticket_closed_by_agent_email_to_customer') { |
| 300 |
if ($type == 'web') { |
| 301 |
return '<p>Hi <strong><em>{{customer.full_name}},</strong></p><p>Your ticket - {{ticket.title}}</p><p>We hope that the ticket was resolved to your satisfaction. If you feel that the ticket should not be closed or if the ticket has not been resolved, please reopen the ticket (<a href="{{ticket.public_url}}">#{{ticket.id}}</a>)</p><p>Regards,<br />{{business.name}}</p>'; |
| 302 |
} else { |
| 303 |
return '<p>Hi <strong><em>{{customer.full_name}},</strong></p><p>Your ticket - {{ticket.title}}</p><p>We hope that the ticket was resolved to your satisfaction. If you feel that the ticket should not be closed or if the ticket has not been resolved, please feel free to reply back.<p>Regards,<br />{{business.name}}</p>'; |
| 304 |
} |
| 305 |
} else if ($emailKey == 'ticket_created_email_to_admin') { |
| 306 |
return '<p>A new ticket (<a href="{{ticket.admin_url}}">{{ticket.title}}</a>) as been submitted by {{customer.full_name}}</p><h4>Ticket Body</h4><p>{{ticket.content}}</p><p><b><a href="{{ticket.admin_url}}">View Ticket</a></b></p>'; |
| 307 |
} else if ($emailKey == 'ticket_replied_by_customer_email_to_admin') { |
| 308 |
return '<p>A new response has been added to "<a href="{{ticket.admin_url}}">{{ticket.title}}</a>" by {{customer.full_name}}</p><h4>Response Body</h4><p>{{response.content}}</p><p><b><a href="{{ticket.admin_url}}">View Ticket</a></b></p>'; |
| 309 |
} else if($emailKey == 'ticket_agent_on_change') { |
| 310 |
return '<p>Hi <strong><em>{{agent.full_name}}</em>,</strong></p><p>Ticket "<a href="{{ticket.admin_url}}">#{{ticket.id}}</a>" assigned to you.</p>'; |
| 311 |
} |
| 312 |
|
| 313 |
return ''; |
| 314 |
} |
| 315 |
} |
| 316 |
|