| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\Integrations\MailChimp; |
| 4 |
|
| 5 |
use FluentForm\App\Services\Integrations\IntegrationManager; |
| 6 |
use FluentForm\App\Services\Integrations\MailChimp\MailChimpSubscriber as Subscriber; |
| 7 |
use FluentForm\Framework\Foundation\Application; |
| 8 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 9 |
|
| 10 |
class MailChimpIntegration extends IntegrationManager |
| 11 |
{ |
| 12 |
/** |
| 13 |
* MailChimp Subscriber that handles & process all the subscribing logics. |
| 14 |
*/ |
| 15 |
use Subscriber; |
| 16 |
|
| 17 |
public function __construct(Application $application) |
| 18 |
{ |
| 19 |
parent::__construct( |
| 20 |
$application, |
| 21 |
'MailChimp', |
| 22 |
'mailchimp', |
| 23 |
'_fluentform_mailchimp_details', |
| 24 |
'mailchimp_feeds', |
| 25 |
12 |
| 26 |
); |
| 27 |
|
| 28 |
$this->description = 'WP Fluent Forms Mailchimp module allows you to create Mailchimp newsletter signup forms in WordPress'; |
| 29 |
|
| 30 |
$this->logo = $this->app->url('public/img/integrations/mailchimp.png'); |
| 31 |
$this->registerAdminHooks(); |
| 32 |
|
| 33 |
add_action('wp_ajax_fluentform_mailchimp_interest_groups', array($this, 'fetchInterestGroups')); |
| 34 |
|
| 35 |
// add_filter('fluentform_notifying_async_mailchimp', '__return_false'); |
| 36 |
} |
| 37 |
|
| 38 |
public function getGlobalFields($fields) |
| 39 |
{ |
| 40 |
return [ |
| 41 |
'logo' => $this->logo, |
| 42 |
'menu_title' => __('Mailchimp Settings', 'fluentform'), |
| 43 |
'menu_description' => __('Mailchimp is a marketing platform for small businesses. Send beautiful emails, connect your e-commerce store, advertise, and build your brand. Use Fluent Form to collect customer information and automatically add it to your Mailchimp campaign list. If you don\'t have a Mailchimp account, you can <a href="http://www.mailchimp.com/" target="_blank">sign up for one here.</a>', 'fluentform'), |
| 44 |
'valid_message' => __('Your Mailchimp API Key is valid', 'fluentform'), |
| 45 |
'invalid_message' => __('Your Mailchimp API Key is not valid', 'fluentform'), |
| 46 |
'save_button_text' => __('Save Settings', 'fluentform'), |
| 47 |
'fields' => [ |
| 48 |
'apiKey' => [ |
| 49 |
'type' => 'text', |
| 50 |
'label_tips' => __("Enter your Mailchimp API Key, if you do not have <br>Please login to your MailChimp account and go to<br>Profile -> Extras -> Api Keys", 'fluentform'), |
| 51 |
'label' => __('Mailchimp API Key', 'fluentform'), |
| 52 |
] |
| 53 |
], |
| 54 |
'hide_on_valid' => true, |
| 55 |
'discard_settings' => [ |
| 56 |
'section_description' => 'Your Mailchimp API integration is up and running', |
| 57 |
'button_text' => 'Disconnect Mailchimp', |
| 58 |
'data' => [ |
| 59 |
'apiKey' => '' |
| 60 |
], |
| 61 |
'show_verify' => true |
| 62 |
] |
| 63 |
]; |
| 64 |
} |
| 65 |
|
| 66 |
public function getGlobalSettings($settings) |
| 67 |
{ |
| 68 |
$globalSettings = get_option($this->optionKey); |
| 69 |
if (!$globalSettings) { |
| 70 |
$globalSettings = []; |
| 71 |
} |
| 72 |
$defaults = [ |
| 73 |
'apiKey' => '', |
| 74 |
'status' => '' |
| 75 |
]; |
| 76 |
|
| 77 |
return wp_parse_args($globalSettings, $defaults); |
| 78 |
} |
| 79 |
|
| 80 |
public function saveGlobalSettings($mailChimp) |
| 81 |
{ |
| 82 |
if (!$mailChimp['apiKey']) { |
| 83 |
$mailChimpSettings = [ |
| 84 |
'apiKey' => '', |
| 85 |
'status' => false |
| 86 |
]; |
| 87 |
// Update the reCaptcha details with siteKey & secretKey. |
| 88 |
update_option($this->optionKey, $mailChimpSettings, 'no'); |
| 89 |
wp_send_json_success([ |
| 90 |
'message' => __('Your settings has been updated and disconnected', 'fluentform'), |
| 91 |
'status' => false |
| 92 |
], 200); |
| 93 |
} |
| 94 |
|
| 95 |
// Verify API key now |
| 96 |
try { |
| 97 |
$MailChimp = new MailChimp($mailChimp['apiKey']); |
| 98 |
$result = $MailChimp->get('lists'); |
| 99 |
if (!$MailChimp->success()) { |
| 100 |
throw new \Exception($MailChimp->getLastError()); |
| 101 |
} |
| 102 |
} catch (\Exception $exception) { |
| 103 |
wp_send_json_error([ |
| 104 |
'message' => $exception->getMessage() |
| 105 |
], 400); |
| 106 |
} |
| 107 |
|
| 108 |
// MailChimp key is verified now, Proceed now |
| 109 |
|
| 110 |
$mailChimpSettings = [ |
| 111 |
'apiKey' => sanitize_text_field($mailChimp['apiKey']), |
| 112 |
'status' => true |
| 113 |
]; |
| 114 |
|
| 115 |
// Update the reCaptcha details with siteKey & secretKey. |
| 116 |
update_option($this->optionKey, $mailChimpSettings, 'no`'); |
| 117 |
|
| 118 |
wp_send_json_success([ |
| 119 |
'message' => __('Your mailchimp api key has been verfied and successfully set', 'fluentform'), |
| 120 |
'status' => true |
| 121 |
], 200); |
| 122 |
} |
| 123 |
|
| 124 |
public function pushIntegration($integrations, $formId) |
| 125 |
{ |
| 126 |
$integrations['mailchimp'] = [ |
| 127 |
'title' => __('Mailchimp Feed', 'fluentform'), |
| 128 |
'logo' => $this->logo, |
| 129 |
'is_active' => $this->isConfigured(), |
| 130 |
'configure_title' => __('Configuration required!', 'fluentform'), |
| 131 |
'global_configure_url' => admin_url('admin.php?page=fluent_forms_settings#mailchimp'), |
| 132 |
'configure_message' => __('Mailchimp is not configured yet! Please configure your mailchimp api first', 'fluentform'), |
| 133 |
'configure_button_text' => __('Set Mailchimp API', 'fluentform') |
| 134 |
]; |
| 135 |
|
| 136 |
return $integrations; |
| 137 |
} |
| 138 |
|
| 139 |
public function getIntegrationDefaults($settings, $formId) |
| 140 |
{ |
| 141 |
$settings = [ |
| 142 |
'conditionals' => [ |
| 143 |
'conditions' => [], |
| 144 |
'status' => false, |
| 145 |
'type' => 'all' |
| 146 |
], |
| 147 |
'enabled' => true, |
| 148 |
'list_id' => '', |
| 149 |
'list_name' => '', |
| 150 |
'name' => '', |
| 151 |
'merge_fields' => (object)[], |
| 152 |
'tags' => '', |
| 153 |
'markAsVIP' => false, |
| 154 |
'fieldEmailAddress' => '', |
| 155 |
'doubleOptIn' => false, |
| 156 |
'note' => '' |
| 157 |
]; |
| 158 |
|
| 159 |
return $settings; |
| 160 |
} |
| 161 |
|
| 162 |
public function getSettingsFields($settings, $formId) |
| 163 |
{ |
| 164 |
return [ |
| 165 |
'fields' => [ |
| 166 |
[ |
| 167 |
'key' => 'name', |
| 168 |
'label' => 'Name', |
| 169 |
'required' => true, |
| 170 |
'placeholder' => 'Your Feed Name', |
| 171 |
'component' => 'text' |
| 172 |
], |
| 173 |
[ |
| 174 |
'key' => 'list_id', |
| 175 |
'label' => 'Mailchimp List', |
| 176 |
'placeholder' => 'Select Mailchimp List', |
| 177 |
'tips' => 'Select the Mailchimp list you would like to add your contacts to.', |
| 178 |
'component' => 'list_ajax_options', |
| 179 |
'options' => $this->getLists(), |
| 180 |
], |
| 181 |
[ |
| 182 |
'key' => 'merge_fields', |
| 183 |
'require_list' => true, |
| 184 |
'label' => 'Map Fields', |
| 185 |
'tips' => 'Associate your Mailchimp merge tags to the appropriate Fluent Form fields by selecting the appropriate form field from the list.', |
| 186 |
'component' => 'map_fields', |
| 187 |
'field_label_remote' => 'Mailchimp Field', |
| 188 |
'field_label_local' => 'Form Field', |
| 189 |
'primary_fileds' => [ |
| 190 |
[ |
| 191 |
'key' => 'fieldEmailAddress', |
| 192 |
'label' => 'Email Address', |
| 193 |
'required' => true, |
| 194 |
'input_options' => 'emails' |
| 195 |
] |
| 196 |
] |
| 197 |
], |
| 198 |
[ |
| 199 |
'key' => 'interest_group', |
| 200 |
'require_list' => true, |
| 201 |
'label' => 'Interest Group', |
| 202 |
'tips' => 'You can map your mailchimp interest group for this contact', |
| 203 |
'component' => 'chained_fields', |
| 204 |
'sub_type' => 'radio', |
| 205 |
'category_label' => 'Select Interest Category', |
| 206 |
'subcategory_label' => 'Select Interest', |
| 207 |
'remote_url' => admin_url('admin-ajax.php?action=fluentform_mailchimp_interest_groups'), |
| 208 |
'inline_tip' => 'Select the mailchimp interest category and interest' |
| 209 |
], |
| 210 |
[ |
| 211 |
'key' => 'tags', |
| 212 |
'require_list' => true, |
| 213 |
'label' => 'Tags', |
| 214 |
'tips' => 'Associate tags to your Mailchimp contacts with a comma separated list (e.g. new lead, FluentForms, web source). Commas within a merge tag value will be created as a single tag.', |
| 215 |
'component' => 'value_text', |
| 216 |
'inline_tip' => 'Please provide each tag by comma separated value' |
| 217 |
], |
| 218 |
[ |
| 219 |
'key' => 'note', |
| 220 |
'require_list' => true, |
| 221 |
'label' => 'Note', |
| 222 |
'tips' => 'You can write a note for this contact', |
| 223 |
'component' => 'value_textarea' |
| 224 |
], |
| 225 |
[ |
| 226 |
'key' => 'doubleOptIn', |
| 227 |
'require_list' => true, |
| 228 |
'label' => 'Double Opt-in', |
| 229 |
'tips' => 'When the double opt-in option is enabled,<br />Mailchimp will send a confirmation email<br />to the user and will only add them to your <br /Mailchimp list upon confirmation.', |
| 230 |
'component' => 'checkbox-single', |
| 231 |
'checkbox_label' => 'Enable Double Opt-in' |
| 232 |
], |
| 233 |
[ |
| 234 |
'key' => 'markAsVIP', |
| 235 |
'require_list' => true, |
| 236 |
'label' => 'VIP', |
| 237 |
'tips' => 'When enabled,<br /> This contact will be marked as VIP.', |
| 238 |
'component' => 'checkbox-single', |
| 239 |
'checkbox_label' => 'Mark as VIP Contact' |
| 240 |
], |
| 241 |
[ |
| 242 |
'require_list' => true, |
| 243 |
'key' => 'conditionals', |
| 244 |
'label' => 'Conditional Logics', |
| 245 |
'tips' => 'Allow mailchimp integration conditionally based on your submission values', |
| 246 |
'component' => 'conditional_block' |
| 247 |
], |
| 248 |
[ |
| 249 |
'require_list' => true, |
| 250 |
'key' => 'enabled', |
| 251 |
'label' => 'Status', |
| 252 |
'component' => 'checkbox-single', |
| 253 |
'checkbox_label' => 'Enable This feed' |
| 254 |
] |
| 255 |
], |
| 256 |
'button_require_list' => true, |
| 257 |
'integration_title' => 'Mailchimp' |
| 258 |
]; |
| 259 |
} |
| 260 |
|
| 261 |
public function setFeedAtributes($feed, $formId) |
| 262 |
{ |
| 263 |
$feed['provider'] = 'mailchimp'; |
| 264 |
$feed['provider_logo'] = $this->logo; |
| 265 |
return $feed; |
| 266 |
} |
| 267 |
|
| 268 |
public function prepareIntegrationFeed($setting, $feed, $formId) |
| 269 |
{ |
| 270 |
$defaults = $this->getIntegrationDefaults([], $formId); |
| 271 |
|
| 272 |
foreach ($setting as $settingKey => $settingValue) { |
| 273 |
if ($settingValue == 'true') { |
| 274 |
$setting[$settingKey] = true; |
| 275 |
} else if ($settingValue == 'false') { |
| 276 |
$setting[$settingKey] = false; |
| 277 |
} else if ($settingKey == 'conditionals') { |
| 278 |
if ($settingValue['status'] == 'true') { |
| 279 |
$settingValue['status'] = true; |
| 280 |
} else if ($settingValue['status'] == 'false') { |
| 281 |
$settingValue['status'] = false; |
| 282 |
} |
| 283 |
$setting['conditionals'] = $settingValue; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
if (!empty($setting['list_id'])) { |
| 288 |
$setting['list_id'] = (string)$setting['list_id']; |
| 289 |
} |
| 290 |
|
| 291 |
$settings['markAsVIP'] = ArrayHelper::isTrue($setting, 'markAsVIP'); |
| 292 |
$settings['doubleOptIn'] = ArrayHelper::isTrue($setting, 'doubleOptIn'); |
| 293 |
|
| 294 |
return wp_parse_args($setting, $defaults); |
| 295 |
} |
| 296 |
|
| 297 |
private function getLists() |
| 298 |
{ |
| 299 |
$settings = get_option('_fluentform_mailchimp_details'); |
| 300 |
try { |
| 301 |
$MailChimp = new MailChimp($settings['apiKey']); |
| 302 |
$lists = $MailChimp->get('lists', array('count' => 9999)); |
| 303 |
if (!$MailChimp->success()) { |
| 304 |
return []; |
| 305 |
} |
| 306 |
} catch (\Exception $exception) { |
| 307 |
return []; |
| 308 |
} |
| 309 |
|
| 310 |
$formattedLists = []; |
| 311 |
foreach ($lists['lists'] as $list) { |
| 312 |
$formattedLists[$list['id']] = $list['name']; |
| 313 |
} |
| 314 |
|
| 315 |
return $formattedLists; |
| 316 |
} |
| 317 |
|
| 318 |
public function getMergeFields($list, $listId, $formId) |
| 319 |
{ |
| 320 |
|
| 321 |
if (!$this->isConfigured()) { |
| 322 |
return false; |
| 323 |
} |
| 324 |
$settings = get_option('_fluentform_mailchimp_details'); |
| 325 |
|
| 326 |
try { |
| 327 |
$MailChimp = new MailChimp($settings['apiKey']); |
| 328 |
$list = $MailChimp->get('lists/' . $listId . '/merge-fields', array('count' => 9999)); |
| 329 |
if (!$MailChimp->success()) { |
| 330 |
return false; |
| 331 |
} |
| 332 |
} catch (\Exception $exception) { |
| 333 |
return false; |
| 334 |
} |
| 335 |
|
| 336 |
$mergedFields = $list['merge_fields']; |
| 337 |
$fields = array(); |
| 338 |
|
| 339 |
foreach ($mergedFields as $merged_field) { |
| 340 |
$fields[$merged_field['tag']] = $merged_field['name']; |
| 341 |
} |
| 342 |
|
| 343 |
return $fields; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
public function fetchInterestGroups() |
| 348 |
{ |
| 349 |
$settings = $_REQUEST['settings']; |
| 350 |
|
| 351 |
$listId = ArrayHelper::get($settings, 'list_id'); |
| 352 |
if(!$listId) { |
| 353 |
wp_send_json_success([ |
| 354 |
'categories' => [], |
| 355 |
'subcategories' => [], |
| 356 |
'reset_values' => true |
| 357 |
]); |
| 358 |
} |
| 359 |
|
| 360 |
$categoryId = ArrayHelper::get($settings, 'interest_group.category'); |
| 361 |
$categories = $this->getInterestCategories($listId); |
| 362 |
|
| 363 |
$subCategories = []; |
| 364 |
if($categoryId) { |
| 365 |
$subCategories = $this->getInterestSubCategories($listId, $categoryId); |
| 366 |
} |
| 367 |
|
| 368 |
wp_send_json_success([ |
| 369 |
'categories' => $categories, |
| 370 |
'subcategories' => $subCategories, |
| 371 |
'reset_values' => !$categories && !$subCategories |
| 372 |
]); |
| 373 |
} |
| 374 |
|
| 375 |
private function getInterestCategories($listId) |
| 376 |
{ |
| 377 |
$settings = get_option('_fluentform_mailchimp_details'); |
| 378 |
try { |
| 379 |
$MailChimp = new MailChimp($settings['apiKey']); |
| 380 |
$categories = $MailChimp->get('/lists/'.$listId.'/interest-categories', array( |
| 381 |
'count' => 9999, |
| 382 |
'fields' => 'categories.id,categories.title' |
| 383 |
)); |
| 384 |
if (!$MailChimp->success()) { |
| 385 |
return []; |
| 386 |
} |
| 387 |
} catch (\Exception $exception) { |
| 388 |
return []; |
| 389 |
} |
| 390 |
$categories = ArrayHelper::get($categories, 'categories', []); |
| 391 |
$formattedLists = []; |
| 392 |
foreach ($categories as $list) { |
| 393 |
$formattedLists[] = [ |
| 394 |
'value' => $list['id'], |
| 395 |
'label' => $list['title'] |
| 396 |
]; |
| 397 |
} |
| 398 |
return $formattedLists; |
| 399 |
} |
| 400 |
|
| 401 |
private function getInterestSubCategories($listId, $categoryId) |
| 402 |
{ |
| 403 |
$settings = get_option('_fluentform_mailchimp_details'); |
| 404 |
try { |
| 405 |
$MailChimp = new MailChimp($settings['apiKey']); |
| 406 |
$categories = $MailChimp->get('/lists/'.$listId.'/interest-categories/'.$categoryId.'/interests', array( |
| 407 |
'count' => 9999, |
| 408 |
'fields' => 'interests.id,interests.name' |
| 409 |
)); |
| 410 |
if (!$MailChimp->success()) { |
| 411 |
return []; |
| 412 |
} |
| 413 |
} catch (\Exception $exception) { |
| 414 |
return []; |
| 415 |
} |
| 416 |
$categories = ArrayHelper::get($categories, 'interests', []); |
| 417 |
$formattedLists = []; |
| 418 |
foreach ($categories as $list) { |
| 419 |
$formattedLists[] = [ |
| 420 |
'value' => $list['id'], |
| 421 |
'label' => $list['name'] |
| 422 |
]; |
| 423 |
} |
| 424 |
return $formattedLists; |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
/* |
| 429 |
* For Handling Notifications broadcast |
| 430 |
*/ |
| 431 |
public function notify($feed, $formData, $entry, $form) |
| 432 |
{ |
| 433 |
$response = $this->subscribe($feed, $formData, $entry, $form); |
| 434 |
|
| 435 |
if ($response == true) { |
| 436 |
do_action('ff_integration_action_result', $feed, 'success', 'MailChimp feed has been successfully initialed and pushed data'); |
| 437 |
} else { |
| 438 |
$message = 'Mailchimp feed has been failed to deliver feed'; |
| 439 |
if (is_wp_error($response)) { |
| 440 |
$message = $response->get_error_message(); |
| 441 |
} |
| 442 |
do_action('ff_integration_action_result', $feed, 'failed', $message); |
| 443 |
} |
| 444 |
} |
| 445 |
} |
| 446 |
|