# fluentform/1.2.4/app/Modules/Integration/MailChimpSubscriber.php

Fluent Forms – Customizable Contact Forms, Survey, Quiz, &amp; Conversational Form Builder, version 1.2.4. 97 lines.

- Page: https://pluginprobe.com/plugins/fluentform/1.2.4/code/app/Modules/Integration/MailChimpSubscriber.php
- Raw: https://pluginprobe.com/plugins/fluentform/1.2.4/raw/app/Modules/Integration/MailChimpSubscriber.php
- Modified: 2018-02-03T11:29:32+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/fluentform/1.2.4/code/app/Modules/Integration/MailChimpSubscriber.php#L10-L20`.

```php
<?php

namespace FluentForm\App\Modules\Integration;

use FluentForm\App\Services\ConditionAssesor;
use FluentForm\Framework\Helpers\ArrayHelper;
use FluentForm\App\Services\Integrations\MailChimp;

trait MailChimpSubscriber
{
    /**
     * Enabled MailChimp feed settings.
     *
     * @var array $feeds
     */
    protected $feeds = [];

    /**
     * Form input data.
     *
     * @param array $formData
     */
    public function setApplicableFeeds($formData)
    {
        $feeds = $this->getAll();

        foreach ($feeds as $feed) {
            if ($this->isApplicable($feed, $formData)) {
                $email = ArrayHelper::get($formData, ArrayHelper::get($feed->formattedValue, 'fieldEmailAddress'));

                if (is_string($email) && is_email($email)) {
                    $feed->formattedValue['fieldEmailAddress'] = $email;

                    $this->feeds[] = $feed;
                }
            }
        }
    }

    /**
     * Determine if the feed is eligible to be applied.
     *
     * @param $feed
     * @param $formData
     *
     * @return bool
     */
    public function isApplicable(&$feed, &$formData)
    {
        return ArrayHelper::get($feed->formattedValue, 'enabled') &&
               ArrayHelper::get($feed->formattedValue, 'list_id') &&
               ConditionAssesor::evaluate($feed->formattedValue, $formData);
    }

    /**
     * Subscribe a user to the list on form submission.
     *
     * @param $formData
     */
    public function subscribe($formData)
    {
        if ($this->isConfigured()) {

            // Prepare applicable feeds.
            $this->setApplicableFeeds($formData);

            foreach ($this->feeds as $feed) {
                $mergeFields = [];

                foreach (ArrayHelper::get($feed->formattedValue, 'merge_fields', []) as $field => $getter) {
                    $value = ArrayHelper::get($formData, $getter, '');

                    $mergeFields[$field] = is_array($value) ? implode(' ', $value) : $value;
                }

                $status = $feed->formattedValue['doubleOptIn'] ? 'pending' : 'subscribed';

                $arguments = [
                    'email_address' => $feed->formattedValue['fieldEmailAddress'],
                    'status'        => $status,
                    'merge_fields'  => (object) $mergeFields,
                    'double_optin'  => $feed->formattedValue['doubleOptIn'],
                    'vip'           => $feed->formattedValue['markAsVIP'],
                ];

                $settings = get_option('_fluentform_mailchimp_details');

                $MailChimp = new MailChimp($settings['apiKey']);

                $endPoint = 'lists/'.$feed->formattedValue['list_id'].'/members/'
                            .md5(strtolower($feed->formattedValue['fieldEmailAddress']));

                $MailChimp->put($endPoint, $arguments);
            }
        }
    }
}
```
