# fluentform/1.2.4/app/Services/Slack.php

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

- Page: https://pluginprobe.com/plugins/fluentform/1.2.4/code/app/Services/Slack.php
- Raw: https://pluginprobe.com/plugins/fluentform/1.2.4/raw/app/Services/Slack.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/Services/Slack.php#L10-L20`.

```php
<?php

namespace FluentForm\App\Services;

use FluentForm\Framework\Helpers\ArrayHelper;
use FluentForm\App\Modules\Form\FormDataParser;
use FluentForm\App\Modules\Form\FormFieldsParser;

class Slack
{
    /**
     * The slack integration settings of the form.
     *
     * @var array $settings
     */
    protected $settings = [];

    /**
     * Determine whether the slack notification should be sent or not.
     *
     * @param $formId
     *
     * @return boolean
     */
    public function shouldApply($formId)
    {
        $this->settings = wpFluent()->table('fluentform_form_meta')
            ->where('form_id', $formId)
            ->where('meta_key', 'slack')
            ->first();

        $this->settings = $this->settings ? json_decode($this->settings->value, true) : [];

        return ArrayHelper::get($this->settings, 'enabled', false);
    }

    /**
     * Handle slack notifier.
     *
     * @param $submissionId
     * @param $formData
     * @param $form
     */
    public function handle($submissionId, $formData, $form)
    {
        if (!$this->shouldApply($form->id)) {
            return;
        }

        $inputs = FormFieldsParser::getEntryInputs($form);

        $labels = FormFieldsParser::getAdminLabels($form, $formFields);

        $formData = FormDataParser::parseData((object) $formData, $inputs, $form->id);

        $title = __("New submission on ".$form->title, 'fluentform');

        $fields = [];

        foreach ($formData as $attribute => $value) {
            $value = str_replace('&', '&amp;', $value);
            $value = str_replace('<', '&lt;', $value);
            $value = str_replace('>', "&gt;", $value);

            $fields[] = [
                'title' => $labels[$attribute],
                'value' => $value,
                'short' => false
            ];
        }

        $slackHook = ArrayHelper::get($this->settings, 'webhook');

        $titleLink = admin_url('admin.php?page=fluent_forms&form_id='
            .$form->id
            .'&route=entries#/entries/'
            .$submissionId
        );

        $body = [
            'payload' => json_encode([
                'attachments' => [
                    [
                        'color'      => '#0078ff',
                        'fallback'   => $title,
                        'title'      => $title,
                        'title_link' => $titleLink,
                        'fields'     => $fields,
                        'footer'     => 'fluentform',
                        'ts'         => current_time('timestamp')
                    ]
                ]
            ])
        ];

        wp_remote_post($slackHook, [
            'method'      => 'POST',
            'timeout'     => 30,
            'redirection' => 5,
            'httpversion' => '1.0',
            'blocking'    => false,
            'headers'     => [],
            'body'        => $body,
            'cookies'     => []
        ]);
    }

    /**
     * Invoke slack notifier.
     *
     * @param $submissionId
     * @param $formData
     * @param $form
     */
    public static function notify($submissionId, $formData, $form)
    {
        (new static)->handle($submissionId, $formData, $form);
    }
}
```
