key, $application->request->get('form_id', false), true); $this->app = $application; } public function getMailChimpSettings() { $globalStatus = $this->isConfigured(); $integrations = $this->getAll(); wp_send_json_success(array( 'global_status' => $globalStatus, 'integrations' => $integrations, 'configure_url' => admin_url('admin.php?page=fluent_forms_settings#mailchimp') ), 200); } public function getMailChimpLists() { if (! $this->isConfigured()) { wp_send_json_error(array( 'error' => __('MailChimp is not configured yet', 'fluentform') ), 400); } $settings = get_option('_fluentform_mailchimp_details'); try { $MailChimp = new MailChimp($settings['apiKey']); $lists = $MailChimp->get('lists', array('count' => 9999)); if (! $MailChimp->success()) { throw new \Exception($MailChimp->getLastError()); } } catch (\Exception $exception) { wp_send_json_error(array( 'message' => $exception->getMessage() ), 400); } $formattedLists = array(); foreach ($lists['lists'] as $list) { $formattedLists[$list['id']] = $list; } wp_send_json_success(array( 'lists' => $formattedLists ), 200); } public function getMailChimpList() { if (! $this->isConfigured()) { wp_send_json_error(array( 'error' => __('MailChimp is not configured yet', 'fluentform') ), 400); } $settings = get_option('_fluentform_mailchimp_details'); $list_id = $this->app->request->get('listId'); try { $MailChimp = new MailChimp($settings['apiKey']); $list = $MailChimp->get('lists/'.$list_id.'/merge-fields', array('count' => 9999)); if (! $MailChimp->success()) { throw new \Exception($MailChimp->getLastError()); } } catch (\Exception $exception) { wp_send_json_error(array( 'message' => $exception->getMessage() ), 400); } $mergedFields = $list['merge_fields']; $fields = array(); foreach ($mergedFields as $merged_field) { $fields[$merged_field['tag']] = $merged_field['name']; } wp_send_json_success(array( 'merge_fields' => $fields ), 200); } public function saveNotification() { if (! $this->isConfigured()) { wp_send_json_error(array( 'error' => __('MailChimp is not configured yet', 'fluentform') ), 400); } $notification = $this->app->request->get('notification'); $notification_id = $this->app->request->get('notification_id'); $notification = json_decode($notification, true); // validate notification now $this->validate($notification); $notification = fluentFormSanitizer($notification); if ($notification_id) { $this->update($notification_id, $notification); $message = __('MailChimp Field successfully updated', 'fluentform'); } else { $notification_id = $this->save($notification); $message = __('MailChimp Field successfully created', 'fluentform'); } wp_send_json_success(array( 'message' => $message, 'notification_id' => $notification_id ), 200); } public function deleteNotification() { $settingsId = $this->app->request->get('id'); $this->delete($settingsId); wp_send_json_success(array( 'message' => __('Selected MailChimp Feed is deleted', 'fluentform'), 'integrations' => $this->getAll() )); } private function isConfigured() { $globalStatus = get_option('_fluentform_mailchimp_details'); return $globalStatus && $globalStatus['status']; } private function validate($notification) { $validate = fluentValidator($notification, array( 'name' => 'required', 'list_id' => 'required', 'fieldEmailAddress' => 'required' ), array( 'name.required' => __('MailChimp Feed Name is required', 'fluentform'), 'list.required' => __(' MailChimp List is required', 'fluentform'), 'fieldEmailAddress.required' => __('Email Address is required') ))->validate(); if ($validate->fails()) { wp_send_json_error(array( 'errors' => $validate->errors(), 'message' => __('Please fix the errors', 'fluentform') ), 400); } return true; } }