| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Services\Integrations\FluentCrm; |
| 4 |
|
| 5 |
use FluentCrm\App\Models\Subscriber; |
| 6 |
use FluentSupport\App\Models\Customer; |
| 7 |
use FluentSupport\App\Models\Ticket; |
| 8 |
use FluentSupport\App\Modules\PermissionManager; |
| 9 |
use FluentSupport\App\Services\Helper; |
| 10 |
use FluentSupport\App\Services\Tickets\AgentTicketAccess; |
| 11 |
|
| 12 |
class FluentCRMWidgets |
| 13 |
{ |
| 14 |
public function boot() |
| 15 |
{ |
| 16 |
add_action('fluent_support/customer_created', array($this, 'maybeCreateContact'), 10, 1); |
| 17 |
add_filter('fluentcrm-support_tickets_providers', array($this, 'pushProvider')); |
| 18 |
add_filter('fluentcrm-get_support_tickets_fluent_support', array($this, 'getSupportTickets'), 10, 2); |
| 19 |
|
| 20 |
new CreateTicketAction(); |
| 21 |
} |
| 22 |
|
| 23 |
public function pushProvider($providers) |
| 24 |
{ |
| 25 |
$providers['fluent_support'] = [ |
| 26 |
'title' => __('Support Tickets by Fluent Support', 'fluent-support'), |
| 27 |
'name' => __('Fluent Support', 'fluent-support') |
| 28 |
]; |
| 29 |
|
| 30 |
return $providers; |
| 31 |
} |
| 32 |
|
| 33 |
public function getSupportTickets($data, $subscriber) |
| 34 |
{ |
| 35 |
// CRM access isn't ticket access: require the single-ticket view gate, then list only openable tickets. |
| 36 |
$agent = Helper::getAgentByUserId(); |
| 37 |
|
| 38 |
$canViewTickets = PermissionManager::userCan([ |
| 39 |
'fst_view_tickets', |
| 40 |
'fst_draft_reply', |
| 41 |
'fst_manage_own_tickets', |
| 42 |
'fst_manage_unassigned_tickets', |
| 43 |
'fst_manage_other_tickets' |
| 44 |
]); |
| 45 |
|
| 46 |
if (!$agent || !$canViewTickets) { |
| 47 |
return $data; |
| 48 |
} |
| 49 |
|
| 50 |
$supportPerson = Customer::where('email', $subscriber->email)->first(); |
| 51 |
|
| 52 |
if (!$supportPerson) { |
| 53 |
return $data; |
| 54 |
} |
| 55 |
|
| 56 |
$tickets = (new AgentTicketAccess()) |
| 57 |
->applyAccessScope(Ticket::where('customer_id', $supportPerson->id), $agent) |
| 58 |
->latest('id') |
| 59 |
->paginate(); |
| 60 |
|
| 61 |
$formattedTickets = []; |
| 62 |
foreach ($tickets as $ticket) { |
| 63 |
$ticketUrl = Helper::getPortalAdminBaseUrl() . 'tickets/' . $ticket->id . '/view'; |
| 64 |
$actionHTML = '<a target="_blank" href="' . $ticketUrl . '">' . __('View Ticket', 'fluent-support') . '</a>'; |
| 65 |
$formattedTickets[] = [ |
| 66 |
'id' => '#' . $ticket->id, |
| 67 |
// FluentCRM renders these columns as HTML. |
| 68 |
'title' => esc_html($ticket->title), |
| 69 |
'status' => '<span class="el-tag">' . $ticket->status . '</span>', |
| 70 |
/* translators: %s is a human-readable time difference, e.g. "2 hours" */ |
| 71 |
'Submitted at' => sprintf(__('%s ago', 'fluent-support'), human_time_diff(strtotime($ticket->created_at), current_time('timestamp'))), |
| 72 |
'action' => $actionHTML |
| 73 |
]; |
| 74 |
} |
| 75 |
|
| 76 |
return [ |
| 77 |
'total' => $tickets->total(), |
| 78 |
'data' => $formattedTickets |
| 79 |
]; |
| 80 |
} |
| 81 |
|
| 82 |
public function maybeCreateContact($customer) |
| 83 |
{ |
| 84 |
$syncSettings = $this->getSyncSettings(); |
| 85 |
if ($syncSettings['enabled'] != 'yes') { |
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
$email = $customer->email; |
| 90 |
|
| 91 |
if (!class_exists('\FluentCrm\App\Models\Subscriber')) { |
| 92 |
return false; |
| 93 |
} |
| 94 |
|
| 95 |
$subscriber = Subscriber::where('email', $email)->first(); |
| 96 |
|
| 97 |
$customerData = array_filter([ |
| 98 |
'first_name' => $customer->first_name, |
| 99 |
'last_name' => $customer->last_name, |
| 100 |
'email' => $customer->email, |
| 101 |
'address_line_1' => $customer->address_line_1, |
| 102 |
'address_line_2' => $customer->address_line_2, |
| 103 |
'postal_code' => $customer->zip, |
| 104 |
'city' => $customer->city, |
| 105 |
'state' => $customer->state, |
| 106 |
'country' => $customer->country, |
| 107 |
]); |
| 108 |
|
| 109 |
|
| 110 |
if ($subscriber) { |
| 111 |
unset($customerData['email']); |
| 112 |
$subscriber->fill($customerData); |
| 113 |
$subscriber->save(); |
| 114 |
} else { |
| 115 |
$customerData['status'] = $syncSettings['default_status']; |
| 116 |
$subscriber = FluentCrmApi('contacts')->createOrUpdate($customerData); |
| 117 |
|
| 118 |
if (!$subscriber) { |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
if ($customerData['status'] == 'pending' && $subscriber->status == 'pending') { |
| 123 |
$subscriber->sendDoubleOptinEmail(); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
if ($syncSettings['assigned_list']) { |
| 128 |
$subscriber->attachLists([$syncSettings['assigned_list']]); |
| 129 |
} |
| 130 |
|
| 131 |
if ($syncSettings['assigned_tags']) { |
| 132 |
$subscriber->attachTags($syncSettings['assigned_tags']); |
| 133 |
} |
| 134 |
|
| 135 |
return true; |
| 136 |
} |
| 137 |
|
| 138 |
private function getSyncSettings() |
| 139 |
{ |
| 140 |
$settings = Helper::getOption('_fluentcrm_intergration_settings'); |
| 141 |
|
| 142 |
$settingDefault = [ |
| 143 |
'enabled' => 'no', |
| 144 |
'default_status' => 'subscribed', |
| 145 |
'assigned_list' => '', |
| 146 |
'assigned_tags' => [] |
| 147 |
]; |
| 148 |
|
| 149 |
if (!$settings) { |
| 150 |
return $settingDefault; |
| 151 |
} |
| 152 |
|
| 153 |
return wp_parse_args($settings, $settingDefault); |
| 154 |
} |
| 155 |
} |
| 156 |
|