| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Modules\Integrations\FluentPlugins; |
| 4 |
|
| 5 |
use FluentCart\App\Modules\Integrations\BaseIntegrationManager; |
| 6 |
use FluentCart\App\Vite; |
| 7 |
use FluentCart\Framework\Support\Arr; |
| 8 |
use FluentCrm\App\Models\CustomContactField; |
| 9 |
use FluentCrm\App\Models\Lists; |
| 10 |
use FluentCrm\App\Models\Tag; |
| 11 |
|
| 12 |
class FluentCRMConnect extends BaseIntegrationManager |
| 13 |
{ |
| 14 |
protected $runOnBackgroundForProduct = false; |
| 15 |
|
| 16 |
public function __construct() |
| 17 |
{ |
| 18 |
parent::__construct('FluentCRM', 'fluentcrm', 12); |
| 19 |
|
| 20 |
$this->description = __('FluentCRM is a Self Hosted Email Marketing Automation Plugin for WordPress. Add/Remove tags, lists, run automations on order activities.', 'fluent-cart'); |
| 21 |
$this->logo = Vite::getAssetUrl('images/integrations/fluentcrm.svg'); |
| 22 |
$this->disableGlobalSettings = true; |
| 23 |
$this->installable = 'fluent-crm/fluent-crm.php'; |
| 24 |
$this->integrationId = 1; |
| 25 |
|
| 26 |
|
| 27 |
if ($this->isConfigured()) { |
| 28 |
add_filter('fluent_cart/checkout_page_name_fields_schema', [$this, 'maybeSetNameEmailAtCheckout'], 10, 1); |
| 29 |
} |
| 30 |
|
| 31 |
} |
| 32 |
|
| 33 |
public function maybeSetNameEmailAtCheckout($fields) |
| 34 |
{ |
| 35 |
if (!empty($fields['billing_full_name']['value']) || !empty($fields['billing_email']['value']) || get_current_user_id()) { |
| 36 |
return $fields; |
| 37 |
} |
| 38 |
|
| 39 |
$currentContent = fluentcrm_get_current_contact(); |
| 40 |
if (!$currentContent) { |
| 41 |
return $fields; |
| 42 |
} |
| 43 |
|
| 44 |
if (isset($fields['billing_full_name']) && empty($fields['billing_full_name']['value'])) { |
| 45 |
$fields['billing_full_name']['value'] = $currentContent->full_name; |
| 46 |
} |
| 47 |
|
| 48 |
if (isset($fields['billing_email']) && empty($fields['billing_email']['value'])) { |
| 49 |
$fields['billing_email']['value'] = $currentContent->email; |
| 50 |
} |
| 51 |
|
| 52 |
return $fields; |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
public function isConfigured() |
| 57 |
{ |
| 58 |
return defined('FLUENTCRM'); |
| 59 |
} |
| 60 |
|
| 61 |
public function getApiSettings() |
| 62 |
{ |
| 63 |
return [ |
| 64 |
'status' => defined('FLUENTCRM'), |
| 65 |
'api_key' => '' |
| 66 |
]; |
| 67 |
} |
| 68 |
|
| 69 |
public function getIntegrationDefaults($settings) |
| 70 |
{ |
| 71 |
return [ |
| 72 |
'enabled' => 'yes', |
| 73 |
'list_name' => '', |
| 74 |
'name' => '', |
| 75 |
'list_ids' => [], |
| 76 |
'remove_list_ids' => [], |
| 77 |
'other_fields' => [], |
| 78 |
'tag_ids' => [], |
| 79 |
'remove_tag_ids' => [], |
| 80 |
'tag_routers' => [], |
| 81 |
'event_trigger' => [], |
| 82 |
'tag_ids_selection_type' => 'simple', |
| 83 |
'double_opt_in' => 'yes', |
| 84 |
'watch_on_access_revoke' => 'no', |
| 85 |
'note' => '' |
| 86 |
]; |
| 87 |
} |
| 88 |
|
| 89 |
public function getSettingsFields($settings, $args = []) |
| 90 |
{ |
| 91 |
$fieldOptions = []; |
| 92 |
foreach ((new CustomContactField)->getGlobalFields()['fields'] as $field) { |
| 93 |
$fieldOptions[$field['slug']] = $field['label']; |
| 94 |
} |
| 95 |
|
| 96 |
$crmLists = $this->getLists(); |
| 97 |
$crmTags = $this->getTags(); |
| 98 |
|
| 99 |
$fields = [ |
| 100 |
'name' => [ |
| 101 |
'key' => 'name', |
| 102 |
'label' => __('Feed Title', 'fluent-cart'), |
| 103 |
'required' => true, |
| 104 |
'placeholder' => __('Name', 'fluent-cart'), |
| 105 |
'component' => 'text', |
| 106 |
'inline_tip' => __('Name of this feed, it will be used to identify this feed in the list of feeds', 'fluent-cart') |
| 107 |
], |
| 108 |
'list_ids' => [ |
| 109 |
'key' => 'list_ids', |
| 110 |
'label' => __('Add to Lists', 'fluent-cart'), |
| 111 |
'placeholder' => __('Select FluentCRM Lists', 'fluent-cart'), |
| 112 |
'inline_tip' => __('Select the FluentCRM Lists you would like to add your contact to.', 'fluent-cart'), |
| 113 |
'component' => 'select', |
| 114 |
'is_multiple' => true, |
| 115 |
'required' => false, |
| 116 |
'options' => $crmLists |
| 117 |
], |
| 118 |
'tag_ids' => [ |
| 119 |
'key' => 'tag_ids', |
| 120 |
'require_list' => false, |
| 121 |
'label' => __('Add to Tags', 'fluent-cart'), |
| 122 |
'placeholder' => __('Select Tags', 'fluent-cart'), |
| 123 |
'component' => 'select', |
| 124 |
'is_multiple' => true, |
| 125 |
'options' => $crmTags, |
| 126 |
'inline_tip' => __('Select the tags you would like to add your contact to.', 'fluent-cart') |
| 127 |
], |
| 128 |
'remove_list_ids' => [ |
| 129 |
'key' => 'remove_list_ids', |
| 130 |
'label' => __('Remove From Lists', 'fluent-cart'), |
| 131 |
'placeholder' => __('Select FluentCRM Lists', 'fluent-cart'), |
| 132 |
'inline_tip' => __('Select the FluentCRM Lists you would like to remove from your contact.', 'fluent-cart'), |
| 133 |
'component' => 'select', |
| 134 |
'is_multiple' => true, |
| 135 |
'required' => false, |
| 136 |
'options' => $crmLists |
| 137 |
], |
| 138 |
'remove_tag_ids' => [ |
| 139 |
'key' => 'remove_tag_ids', |
| 140 |
'require_list' => false, |
| 141 |
'label' => __('Remove From Tags', 'fluent-cart'), |
| 142 |
'placeholder' => __('Select Tags', 'fluent-cart'), |
| 143 |
'component' => 'select', |
| 144 |
'is_multiple' => true, |
| 145 |
'options' => $crmTags, |
| 146 |
'inline_tip' => __('Select the tags you would like to remove from your contact.', 'fluent-cart') |
| 147 |
], |
| 148 |
// 'other_fields' => [ |
| 149 |
// 'key' => 'other_fields', |
| 150 |
// 'require_list' => false, |
| 151 |
// 'label' => __('Custom Fields', 'fluent-cart'), |
| 152 |
// 'tips' => esc_html__('Select which FluentCart fields pair with their respective FluentCRM fields.', 'fluent-cart'), |
| 153 |
// 'component' => 'dropdown_many_fields', |
| 154 |
// 'remote_text' => __('FluentCRM Field', 'fluent-cart'), |
| 155 |
// 'local_text' => __('Value', 'fluent-cart'), |
| 156 |
// 'options' => $fieldOptions |
| 157 |
// ], |
| 158 |
'note' => [ |
| 159 |
'key' => 'note', |
| 160 |
'label' => __('Note', 'fluent-cart'), |
| 161 |
'inline_tip' => __('This note will be added to the Contact\'s Profile', 'fluent-cart'), |
| 162 |
'component' => 'value_textarea' |
| 163 |
], |
| 164 |
'double_opt_in' => [ |
| 165 |
'key' => 'double_opt_in', |
| 166 |
'component' => 'yes-no-checkbox', |
| 167 |
'checkbox_label' => __('Enable Double Opt-in', 'fluent-cart'), |
| 168 |
'inline_tip' => __('When the double opt-in option is enabled. FluentCRM will send a double opt-in email to the contact for new and not subscribed contacts.', 'fluent-cart') |
| 169 |
], |
| 170 |
'watch_on_access_revoke' => [ |
| 171 |
'key' => 'watch_on_access_revoke', |
| 172 |
'component' => 'yes-no-checkbox', |
| 173 |
'checkbox_label' => __('Remove from selected Tags/Lists on Refund or Subscription Access Expiration ', 'fluent-cart'), |
| 174 |
'inline_tip' => __('If you enable this, on refund or subscription validity expiration, the selected tags and lists will be removed from the contact.', 'fluent-cart') |
| 175 |
] |
| 176 |
]; |
| 177 |
|
| 178 |
$fields = array_values($fields); |
| 179 |
$fields[] = $this->actionFields(); |
| 180 |
|
| 181 |
return [ |
| 182 |
'fields' => $fields, |
| 183 |
'button_require_list' => false, |
| 184 |
'integration_title' => __('FluentCRM', 'fluent-cart') |
| 185 |
]; |
| 186 |
} |
| 187 |
|
| 188 |
/* |
| 189 |
* For Handling Notifications broadcast |
| 190 |
*/ |
| 191 |
public function processAction($order, $eventData) |
| 192 |
{ |
| 193 |
|
| 194 |
$feedConfig = Arr::get($eventData, 'feed', []); |
| 195 |
|
| 196 |
$customer = $order->customer; |
| 197 |
$contactData = [ |
| 198 |
'email' => $customer->email, |
| 199 |
'first_name' => $customer->first_name, |
| 200 |
'last_name' => $customer->last_name, |
| 201 |
]; |
| 202 |
|
| 203 |
$isRevokedHook = Arr::get($eventData, 'is_revoke_hook') === 'yes'; |
| 204 |
|
| 205 |
$listIds = array_filter((array)Arr::get($feedConfig, 'list_ids', []), 'intval'); |
| 206 |
$tagIds = array_filter((array)Arr::get($feedConfig, 'tag_ids', []), 'intval'); |
| 207 |
$removeListIds = array_filter((array)Arr::get($feedConfig, 'remove_list_ids', []), 'intval'); |
| 208 |
$removeTagIds = array_filter((array)Arr::get($feedConfig, 'remove_tag_ids', []), 'intval'); |
| 209 |
$note = Arr::get($feedConfig, 'note', ''); |
| 210 |
$doubleOptIn = Arr::get($feedConfig, 'double_opt_in', false) === 'yes'; |
| 211 |
|
| 212 |
if ($isRevokedHook) { |
| 213 |
$contact = FluentCrmApi('contacts')->getContact($customer->email); |
| 214 |
if (!$contact) { |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
if ($listIds) { |
| 219 |
$contact->detachLists($listIds); |
| 220 |
} |
| 221 |
|
| 222 |
if ($tagIds) { |
| 223 |
$contact->detachTags($tagIds); |
| 224 |
} |
| 225 |
|
| 226 |
return; |
| 227 |
} |
| 228 |
|
| 229 |
if ($order->billing_address) { |
| 230 |
$contactData['address_line_1'] = $order->billing_address->address_1; |
| 231 |
$contactData['address_line_2'] = $order->billing_address->address_2; |
| 232 |
$contactData['city'] = $order->billing_address->city; |
| 233 |
$contactData['state'] = $order->billing_address->state; |
| 234 |
$contactData['postal_code'] = $order->billing_address->postcode; |
| 235 |
$contactData['country'] = $order->billing_address->country; |
| 236 |
$contactData['phone'] = $order->billing_address->phone; |
| 237 |
} |
| 238 |
|
| 239 |
$contactData['timezone'] = Arr::get($order->config, 'user_tz', ''); |
| 240 |
$contactData = array_filter($contactData); |
| 241 |
|
| 242 |
// When double opt-in is disabled, mark as subscribed immediately |
| 243 |
if (!$doubleOptIn) { |
| 244 |
$contactData['status'] = 'subscribed'; |
| 245 |
} else { |
| 246 |
// When double opt-in is enabled, keep as pending until they confirm |
| 247 |
$contactData['status'] = 'pending'; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
$contact = FluentCrmApi('contacts')->createOrUpdate($contactData, !$doubleOptIn); |
| 252 |
if ($doubleOptIn && $contact->status != 'subscribed') { |
| 253 |
// if double opt-in is enabled |
| 254 |
$contact->sendDoubleOptinEmail(); |
| 255 |
} |
| 256 |
|
| 257 |
if ($removeListIds) { |
| 258 |
$contact->detachLists($removeListIds); |
| 259 |
} |
| 260 |
|
| 261 |
if ($removeTagIds) { |
| 262 |
$contact->detachTags($removeTagIds); |
| 263 |
} |
| 264 |
|
| 265 |
if ($listIds) { |
| 266 |
$contact->attachLists($listIds); |
| 267 |
} |
| 268 |
|
| 269 |
if ($tagIds) { |
| 270 |
$contact->attachTags($tagIds); |
| 271 |
} |
| 272 |
|
| 273 |
if ($note) { |
| 274 |
$note = $this->parseSmartCode($note, $order); |
| 275 |
if ($note) { |
| 276 |
$note = wp_kses_post(wpautop($note)); |
| 277 |
\FluentCrm\App\Models\SubscriberNote::create([ |
| 278 |
'subscriber_id' => $contact->id, |
| 279 |
'description' => $note, |
| 280 |
'type' => 'info' |
| 281 |
]); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
$order->addLog( |
| 286 |
__('FluentCRM Contact Created', 'fluent-cart'), |
| 287 |
__('Contact has been created or updated in FluentCRM.', 'fluent-cart') . ' ' . __('Contact ID: ', 'fluent-cart') . $contact->id, |
| 288 |
'info', |
| 289 |
'FluentCRM Integration' |
| 290 |
); |
| 291 |
|
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Internal methods |
| 296 |
*/ |
| 297 |
private function getTags() |
| 298 |
{ |
| 299 |
$tags = Tag::orderBy('title', 'ASC')->get(); |
| 300 |
$formattedTags = []; |
| 301 |
foreach ($tags as $tag) { |
| 302 |
$formattedTags[(string)$tag->id] = $tag->title; |
| 303 |
} |
| 304 |
|
| 305 |
return $formattedTags; |
| 306 |
} |
| 307 |
|
| 308 |
private function getLists() |
| 309 |
{ |
| 310 |
$lists = Lists::query()->orderBy('title', 'ASC') |
| 311 |
->get(); |
| 312 |
|
| 313 |
$formattedLists = []; |
| 314 |
foreach ($lists as $list) { |
| 315 |
$formattedLists[(string)$list->id] = $list->title; |
| 316 |
} |
| 317 |
|
| 318 |
return $formattedLists; |
| 319 |
} |
| 320 |
} |
| 321 |
|