| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Services\Integrations; |
| 4 |
|
| 5 |
use FluentCrm\App\Models\Subscriber; |
| 6 |
use FluentSupport\App\Models\Customer; |
| 7 |
use FluentSupport\App\Models\Ticket; |
| 8 |
use FluentSupport\App\Services\Helper; |
| 9 |
|
| 10 |
class FluentCRM |
| 11 |
{ |
| 12 |
public function boot() |
| 13 |
{ |
| 14 |
add_action('fluent_support/customer_created', array($this, 'maybeCreateContact'), 10, 1); |
| 15 |
add_filter('fluentcrm-support_tickets_providers', array($this, 'pushProvider')); |
| 16 |
add_filter('fluentcrm-get_support_tickets_fluent_support', array($this, 'getSupportTickets'), 10, 2); |
| 17 |
} |
| 18 |
|
| 19 |
public function pushProvider($providers) |
| 20 |
{ |
| 21 |
$providers['fluent_support'] = [ |
| 22 |
'title' => __('Support Tickets by Fluent Support', 'fluent-crm'), |
| 23 |
'name' => __('Fluent Support', 'fluent-crm') |
| 24 |
]; |
| 25 |
|
| 26 |
return $providers; |
| 27 |
} |
| 28 |
|
| 29 |
public function getSupportTickets($data, $subscriber) |
| 30 |
{ |
| 31 |
$supportPerson = Customer::where('email', $subscriber->email)->first(); |
| 32 |
|
| 33 |
if (!$supportPerson) { |
| 34 |
return $data; |
| 35 |
} |
| 36 |
|
| 37 |
$tickets = Ticket::where('customer_id', $supportPerson->id)->paginate(); |
| 38 |
|
| 39 |
$formattedTickets = []; |
| 40 |
foreach ($tickets as $ticket) { |
| 41 |
$ticketUrl = Helper::getPortalAdminBaseUrl() . 'tickets/' . $ticket->id . '/view'; |
| 42 |
$actionHTML = '<a target="_blank" href="' . $ticketUrl . '">' . __('View Ticket', 'fluent-crm') . '</a>'; |
| 43 |
$formattedTickets[] = [ |
| 44 |
'id' => '#' . $ticket->id, |
| 45 |
'title' => $ticket->title, |
| 46 |
'status' => '<span class="el-tag">' . __($ticket->status, 'fluent-crm') . '</span>', |
| 47 |
'Submitted at' => human_time_diff(strtotime($ticket->created_at), current_time('timestamp')) . __(' ago', 'fluent-crm'), |
| 48 |
'action' => $actionHTML |
| 49 |
]; |
| 50 |
} |
| 51 |
|
| 52 |
return [ |
| 53 |
'total' => $tickets->total, |
| 54 |
'data' => $formattedTickets |
| 55 |
]; |
| 56 |
} |
| 57 |
|
| 58 |
public function maybeCreateContact($customer) |
| 59 |
{ |
| 60 |
$syncSettings = $this->getSyncSettings(); |
| 61 |
if (!$syncSettings['enabled'] != 'yes') { |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
$email = $customer->email; |
| 66 |
|
| 67 |
$subscriber = Subscriber::where('email', $email)->first(); |
| 68 |
|
| 69 |
$customerData = array_filter([ |
| 70 |
'first_name' => $customer->first_name, |
| 71 |
'last_name' => $customer->last_name, |
| 72 |
'email' => $customer->email, |
| 73 |
'address_line_1' => $customer->address_line_1, |
| 74 |
'address_line_2' => $customer->address_line_2, |
| 75 |
'postal_code' => $customer->zip, |
| 76 |
'city' => $customer->city, |
| 77 |
'state' => $customer->state, |
| 78 |
'country' => $customer->country, |
| 79 |
]); |
| 80 |
|
| 81 |
|
| 82 |
if ($subscriber) { |
| 83 |
unset($customerData['email']); |
| 84 |
$subscriber->fill($customerData); |
| 85 |
$subscriber->save(); |
| 86 |
} else { |
| 87 |
$customerData['status'] = $syncSettings['default_status']; |
| 88 |
$subscriber = FluentCrmApi('contacts')->createOrUpdate($customerData); |
| 89 |
|
| 90 |
if (!$subscriber) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
if ($customerData['status'] == 'pending' && $subscriber->status == 'pending') { |
| 95 |
$subscriber->sendDoubleOptinEmail(); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
if ($syncSettings['assigned_list']) { |
| 100 |
$subscriber->attachLists([$syncSettings['assigned_list']]); |
| 101 |
} |
| 102 |
|
| 103 |
if ($syncSettings['assigned_tags']) { |
| 104 |
$subscriber->attachTags($syncSettings['assigned_tags']); |
| 105 |
} |
| 106 |
|
| 107 |
return true; |
| 108 |
} |
| 109 |
|
| 110 |
private function getSyncSettings() |
| 111 |
{ |
| 112 |
$settings = Helper::getOption('_fluentcrm_intergration_settings'); |
| 113 |
|
| 114 |
$settingDefault = [ |
| 115 |
'enabled' => 'no', |
| 116 |
'default_status' => 'subscribed', |
| 117 |
'assigned_list' => '', |
| 118 |
'assigned_tags' => [] |
| 119 |
]; |
| 120 |
|
| 121 |
if (!$settings) { |
| 122 |
return $settingDefault; |
| 123 |
} |
| 124 |
|
| 125 |
return wp_parse_args($settings, $settingDefault); |
| 126 |
} |
| 127 |
} |
| 128 |
|