| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Integration; |
| 4 |
|
| 5 |
use FluentForm\Framework\Foundation\Application; |
| 6 |
use FluentForm\App\Services\Integrations\MailChimp; |
| 7 |
use FluentForm\App\Modules\Integration\MailChimpSubscriber as Subscriber; |
| 8 |
|
| 9 |
class MailChimpIntegration extends BaseIntegration |
| 10 |
{ |
| 11 |
/** |
| 12 |
* MailChimp Subscriber that handles & process all the subscribing logics. |
| 13 |
*/ |
| 14 |
use Subscriber; |
| 15 |
|
| 16 |
private $key = 'mailchimp_feeds'; |
| 17 |
|
| 18 |
private $app; |
| 19 |
|
| 20 |
public function __construct(Application $application) |
| 21 |
{ |
| 22 |
parent::__construct($this->key, $application->request->get('form_id', false), true); |
| 23 |
$this->app = $application; |
| 24 |
} |
| 25 |
|
| 26 |
public function getMailChimpSettings() |
| 27 |
{ |
| 28 |
$globalStatus = $this->isConfigured(); |
| 29 |
$integrations = $this->getAll(); |
| 30 |
|
| 31 |
wp_send_json_success(array( |
| 32 |
'global_status' => $globalStatus, |
| 33 |
'integrations' => $integrations, |
| 34 |
'configure_url' => admin_url('admin.php?page=fluent_forms_settings#mailchimp') |
| 35 |
), 200); |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
public function getMailChimpLists() |
| 40 |
{ |
| 41 |
if (! $this->isConfigured()) { |
| 42 |
wp_send_json_error(array( |
| 43 |
'error' => __('MailChimp is not configured yet', 'fluentform') |
| 44 |
), 400); |
| 45 |
} |
| 46 |
$settings = get_option('_fluentform_mailchimp_details'); |
| 47 |
|
| 48 |
try { |
| 49 |
$MailChimp = new MailChimp($settings['apiKey']); |
| 50 |
$lists = $MailChimp->get('lists', array('count' => 9999)); |
| 51 |
if (! $MailChimp->success()) { |
| 52 |
throw new \Exception($MailChimp->getLastError()); |
| 53 |
} |
| 54 |
} catch (\Exception $exception) { |
| 55 |
wp_send_json_error(array( |
| 56 |
'message' => $exception->getMessage() |
| 57 |
), 400); |
| 58 |
} |
| 59 |
|
| 60 |
$formattedLists = array(); |
| 61 |
|
| 62 |
foreach ($lists['lists'] as $list) { |
| 63 |
$formattedLists[$list['id']] = $list; |
| 64 |
} |
| 65 |
|
| 66 |
wp_send_json_success(array( |
| 67 |
'lists' => $formattedLists |
| 68 |
), 200); |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
public function getMailChimpList() |
| 73 |
{ |
| 74 |
if (! $this->isConfigured()) { |
| 75 |
wp_send_json_error(array( |
| 76 |
'error' => __('MailChimp is not configured yet', 'fluentform') |
| 77 |
), 400); |
| 78 |
} |
| 79 |
$settings = get_option('_fluentform_mailchimp_details'); |
| 80 |
$list_id = $this->app->request->get('listId'); |
| 81 |
|
| 82 |
try { |
| 83 |
$MailChimp = new MailChimp($settings['apiKey']); |
| 84 |
$list = $MailChimp->get('lists/'.$list_id.'/merge-fields', array('count' => 9999)); |
| 85 |
if (! $MailChimp->success()) { |
| 86 |
throw new \Exception($MailChimp->getLastError()); |
| 87 |
} |
| 88 |
} catch (\Exception $exception) { |
| 89 |
wp_send_json_error(array( |
| 90 |
'message' => $exception->getMessage() |
| 91 |
), 400); |
| 92 |
} |
| 93 |
|
| 94 |
$mergedFields = $list['merge_fields']; |
| 95 |
$fields = array(); |
| 96 |
|
| 97 |
foreach ($mergedFields as $merged_field) { |
| 98 |
$fields[$merged_field['tag']] = $merged_field['name']; |
| 99 |
} |
| 100 |
|
| 101 |
wp_send_json_success(array( |
| 102 |
'merge_fields' => $fields |
| 103 |
), 200); |
| 104 |
} |
| 105 |
|
| 106 |
public function saveNotification() |
| 107 |
{ |
| 108 |
if (! $this->isConfigured()) { |
| 109 |
wp_send_json_error(array( |
| 110 |
'error' => __('MailChimp is not configured yet', 'fluentform') |
| 111 |
), 400); |
| 112 |
} |
| 113 |
$notification = $this->app->request->get('notification'); |
| 114 |
$notification_id = $this->app->request->get('notification_id'); |
| 115 |
$notification = json_decode($notification, true); |
| 116 |
|
| 117 |
// validate notification now |
| 118 |
$this->validate($notification); |
| 119 |
$notification = fluentFormSanitizer($notification); |
| 120 |
|
| 121 |
if ($notification_id) { |
| 122 |
$this->update($notification_id, $notification); |
| 123 |
$message = __('MailChimp Field successfully updated', 'fluentform'); |
| 124 |
} else { |
| 125 |
$notification_id = $this->save($notification); |
| 126 |
$message = __('MailChimp Field successfully created', 'fluentform'); |
| 127 |
} |
| 128 |
|
| 129 |
wp_send_json_success(array( |
| 130 |
'message' => $message, |
| 131 |
'notification_id' => $notification_id |
| 132 |
), 200); |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
public function deleteNotification() |
| 137 |
{ |
| 138 |
$settingsId = $this->app->request->get('id'); |
| 139 |
$this->delete($settingsId); |
| 140 |
wp_send_json_success(array( |
| 141 |
'message' => __('Selected MailChimp Feed is deleted', 'fluentform'), |
| 142 |
'integrations' => $this->getAll() |
| 143 |
)); |
| 144 |
} |
| 145 |
|
| 146 |
private function isConfigured() |
| 147 |
{ |
| 148 |
$globalStatus = get_option('_fluentform_mailchimp_details'); |
| 149 |
return $globalStatus && $globalStatus['status']; |
| 150 |
} |
| 151 |
|
| 152 |
private function validate($notification) |
| 153 |
{ |
| 154 |
$validate = fluentValidator($notification, array( |
| 155 |
'name' => 'required', |
| 156 |
'list_id' => 'required', |
| 157 |
'fieldEmailAddress' => 'required' |
| 158 |
), array( |
| 159 |
'name.required' => __('MailChimp Feed Name is required', 'fluentform'), |
| 160 |
'list.required' => __(' MailChimp List is required', 'fluentform'), |
| 161 |
'fieldEmailAddress.required' => __('Email Address is required') |
| 162 |
))->validate(); |
| 163 |
|
| 164 |
if ($validate->fails()) { |
| 165 |
wp_send_json_error(array( |
| 166 |
'errors' => $validate->errors(), |
| 167 |
'message' => __('Please fix the errors', 'fluentform') |
| 168 |
), 400); |
| 169 |
} |
| 170 |
return true; |
| 171 |
} |
| 172 |
} |