PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 1.2.4
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v1.2.4
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / app / Services / Slack.php

Slack.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 1.2.4, at app/Services/Slack.php

119 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\App\Services;
4
5 use FluentForm\Framework\Helpers\ArrayHelper;
6 use FluentForm\App\Modules\Form\FormDataParser;
7 use FluentForm\App\Modules\Form\FormFieldsParser;
8
9 class Slack
10 {
11 /**
12 * The slack integration settings of the form.
13 *
14 * @var array $settings
15 */
16 protected $settings = [];
17
18 /**
19 * Determine whether the slack notification should be sent or not.
20 *
21 * @param $formId
22 *
23 * @return boolean
24 */
25 public function shouldApply($formId)
26 {
27 $this->settings = wpFluent()->table('fluentform_form_meta')
28 ->where('form_id', $formId)
29 ->where('meta_key', 'slack')
30 ->first();
31
32 $this->settings = $this->settings ? json_decode($this->settings->value, true) : [];
33
34 return ArrayHelper::get($this->settings, 'enabled', false);
35 }
36
37 /**
38 * Handle slack notifier.
39 *
40 * @param $submissionId
41 * @param $formData
42 * @param $form
43 */
44 public function handle($submissionId, $formData, $form)
45 {
46 if (!$this->shouldApply($form->id)) {
47 return;
48 }
49
50 $inputs = FormFieldsParser::getEntryInputs($form);
51
52 $labels = FormFieldsParser::getAdminLabels($form, $formFields);
53
54 $formData = FormDataParser::parseData((object) $formData, $inputs, $form->id);
55
56 $title = __("New submission on ".$form->title, 'fluentform');
57
58 $fields = [];
59
60 foreach ($formData as $attribute => $value) {
61 $value = str_replace('&', '&amp;', $value);
62 $value = str_replace('<', '&lt;', $value);
63 $value = str_replace('>', "&gt;", $value);
64
65 $fields[] = [
66 'title' => $labels[$attribute],
67 'value' => $value,
68 'short' => false
69 ];
70 }
71
72 $slackHook = ArrayHelper::get($this->settings, 'webhook');
73
74 $titleLink = admin_url('admin.php?page=fluent_forms&form_id='
75 .$form->id
76 .'&route=entries#/entries/'
77 .$submissionId
78 );
79
80 $body = [
81 'payload' => json_encode([
82 'attachments' => [
83 [
84 'color' => '#0078ff',
85 'fallback' => $title,
86 'title' => $title,
87 'title_link' => $titleLink,
88 'fields' => $fields,
89 'footer' => 'fluentform',
90 'ts' => current_time('timestamp')
91 ]
92 ]
93 ])
94 ];
95
96 wp_remote_post($slackHook, [
97 'method' => 'POST',
98 'timeout' => 30,
99 'redirection' => 5,
100 'httpversion' => '1.0',
101 'blocking' => false,
102 'headers' => [],
103 'body' => $body,
104 'cookies' => []
105 ]);
106 }
107
108 /**
109 * Invoke slack notifier.
110 *
111 * @param $submissionId
112 * @param $formData
113 * @param $form
114 */
115 public static function notify($submissionId, $formData, $form)
116 {
117 (new static)->handle($submissionId, $formData, $form);
118 }
119 }